1 // arm.cc -- arm target support for gold.
2
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-arm.c.
8
9 // This file is part of gold.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
25
26 #include "gold.h"
27
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
33 #include <map>
34 #include <utility>
35 #include <set>
36
37 #include "elfcpp.h"
38 #include "parameters.h"
39 #include "reloc.h"
40 #include "arm.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "layout.h"
44 #include "output.h"
45 #include "copy-relocs.h"
46 #include "target.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
49 #include "tls.h"
50 #include "defstd.h"
51 #include "gc.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
54 #include "nacl.h"
55
56 namespace
57 {
58
59 using namespace gold;
60
61 template<bool big_endian>
62 class Output_data_plt_arm;
63
64 template<bool big_endian>
65 class Output_data_plt_arm_short;
66
67 template<bool big_endian>
68 class Output_data_plt_arm_long;
69
70 template<bool big_endian>
71 class Stub_table;
72
73 template<bool big_endian>
74 class Arm_input_section;
75
76 class Arm_exidx_cantunwind;
77
78 class Arm_exidx_merged_section;
79
80 class Arm_exidx_fixup;
81
82 template<bool big_endian>
83 class Arm_output_section;
84
85 class Arm_exidx_input_section;
86
87 template<bool big_endian>
88 class Arm_relobj;
89
90 template<bool big_endian>
91 class Arm_relocate_functions;
92
93 template<bool big_endian>
94 class Arm_output_data_got;
95
96 template<bool big_endian>
97 class Target_arm;
98
99 // For convenience.
100 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
101
102 // Maximum branch offsets for ARM, THUMB and THUMB2.
103 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
104 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
105 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
106 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
107 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
108 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
109
110 // Thread Control Block size.
111 const size_t ARM_TCB_SIZE = 8;
112
113 // The arm target class.
114 //
115 // This is a very simple port of gold for ARM-EABI. It is intended for
116 // supporting Android only for the time being.
117 //
118 // TODOs:
119 // - Implement all static relocation types documented in arm-reloc.def.
120 // - Make PLTs more flexible for different architecture features like
121 // Thumb-2 and BE8.
122 // There are probably a lot more.
123
124 // Ideally we would like to avoid using global variables but this is used
125 // very in many places and sometimes in loops. If we use a function
126 // returning a static instance of Arm_reloc_property_table, it will be very
127 // slow in an threaded environment since the static instance needs to be
128 // locked. The pointer is below initialized in the
129 // Target::do_select_as_default_target() hook so that we do not spend time
130 // building the table if we are not linking ARM objects.
131 //
132 // An alternative is to to process the information in arm-reloc.def in
133 // compilation time and generate a representation of it in PODs only. That
134 // way we can avoid initialization when the linker starts.
135
136 Arm_reloc_property_table* arm_reloc_property_table = NULL;
137
138 // Instruction template class. This class is similar to the insn_sequence
139 // struct in bfd/elf32-arm.c.
140
141 class Insn_template
142 {
143 public:
144 // Types of instruction templates.
145 enum Type
146 {
147 THUMB16_TYPE = 1,
148 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
149 // templates with class-specific semantics. Currently this is used
150 // only by the Cortex_a8_stub class for handling condition codes in
151 // conditional branches.
152 THUMB16_SPECIAL_TYPE,
153 THUMB32_TYPE,
154 ARM_TYPE,
155 DATA_TYPE
156 };
157
158 // Factory methods to create instruction templates in different formats.
159
160 static const Insn_template
thumb16_insn(uint32_t data)161 thumb16_insn(uint32_t data)
162 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
163
164 // A Thumb conditional branch, in which the proper condition is inserted
165 // when we build the stub.
166 static const Insn_template
thumb16_bcond_insn(uint32_t data)167 thumb16_bcond_insn(uint32_t data)
168 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
169
170 static const Insn_template
thumb32_insn(uint32_t data)171 thumb32_insn(uint32_t data)
172 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
173
174 static const Insn_template
thumb32_b_insn(uint32_t data,int reloc_addend)175 thumb32_b_insn(uint32_t data, int reloc_addend)
176 {
177 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
178 reloc_addend);
179 }
180
181 static const Insn_template
arm_insn(uint32_t data)182 arm_insn(uint32_t data)
183 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
184
185 static const Insn_template
arm_rel_insn(unsigned data,int reloc_addend)186 arm_rel_insn(unsigned data, int reloc_addend)
187 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
188
189 static const Insn_template
data_word(unsigned data,unsigned int r_type,int reloc_addend)190 data_word(unsigned data, unsigned int r_type, int reloc_addend)
191 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
192
193 // Accessors. This class is used for read-only objects so no modifiers
194 // are provided.
195
196 uint32_t
data() const197 data() const
198 { return this->data_; }
199
200 // Return the instruction sequence type of this.
201 Type
type() const202 type() const
203 { return this->type_; }
204
205 // Return the ARM relocation type of this.
206 unsigned int
r_type() const207 r_type() const
208 { return this->r_type_; }
209
210 int32_t
reloc_addend() const211 reloc_addend() const
212 { return this->reloc_addend_; }
213
214 // Return size of instruction template in bytes.
215 size_t
216 size() const;
217
218 // Return byte-alignment of instruction template.
219 unsigned
220 alignment() const;
221
222 private:
223 // We make the constructor private to ensure that only the factory
224 // methods are used.
225 inline
Insn_template(unsigned data,Type type,unsigned int r_type,int reloc_addend)226 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
227 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
228 { }
229
230 // Instruction specific data. This is used to store information like
231 // some of the instruction bits.
232 uint32_t data_;
233 // Instruction template type.
234 Type type_;
235 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
236 unsigned int r_type_;
237 // Relocation addend.
238 int32_t reloc_addend_;
239 };
240
241 // Macro for generating code to stub types. One entry per long/short
242 // branch stub
243
244 #define DEF_STUBS \
245 DEF_STUB(long_branch_any_any) \
246 DEF_STUB(long_branch_v4t_arm_thumb) \
247 DEF_STUB(long_branch_thumb_only) \
248 DEF_STUB(long_branch_v4t_thumb_thumb) \
249 DEF_STUB(long_branch_v4t_thumb_arm) \
250 DEF_STUB(short_branch_v4t_thumb_arm) \
251 DEF_STUB(long_branch_any_arm_pic) \
252 DEF_STUB(long_branch_any_thumb_pic) \
253 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
254 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
255 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
256 DEF_STUB(long_branch_thumb_only_pic) \
257 DEF_STUB(a8_veneer_b_cond) \
258 DEF_STUB(a8_veneer_b) \
259 DEF_STUB(a8_veneer_bl) \
260 DEF_STUB(a8_veneer_blx) \
261 DEF_STUB(v4_veneer_bx)
262
263 // Stub types.
264
265 #define DEF_STUB(x) arm_stub_##x,
266 typedef enum
267 {
268 arm_stub_none,
269 DEF_STUBS
270
271 // First reloc stub type.
272 arm_stub_reloc_first = arm_stub_long_branch_any_any,
273 // Last reloc stub type.
274 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
275
276 // First Cortex-A8 stub type.
277 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
278 // Last Cortex-A8 stub type.
279 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
280
281 // Last stub type.
282 arm_stub_type_last = arm_stub_v4_veneer_bx
283 } Stub_type;
284 #undef DEF_STUB
285
286 // Stub template class. Templates are meant to be read-only objects.
287 // A stub template for a stub type contains all read-only attributes
288 // common to all stubs of the same type.
289
290 class Stub_template
291 {
292 public:
293 Stub_template(Stub_type, const Insn_template*, size_t);
294
~Stub_template()295 ~Stub_template()
296 { }
297
298 // Return stub type.
299 Stub_type
type() const300 type() const
301 { return this->type_; }
302
303 // Return an array of instruction templates.
304 const Insn_template*
insns() const305 insns() const
306 { return this->insns_; }
307
308 // Return size of template in number of instructions.
309 size_t
insn_count() const310 insn_count() const
311 { return this->insn_count_; }
312
313 // Return size of template in bytes.
314 size_t
size() const315 size() const
316 { return this->size_; }
317
318 // Return alignment of the stub template.
319 unsigned
alignment() const320 alignment() const
321 { return this->alignment_; }
322
323 // Return whether entry point is in thumb mode.
324 bool
entry_in_thumb_mode() const325 entry_in_thumb_mode() const
326 { return this->entry_in_thumb_mode_; }
327
328 // Return number of relocations in this template.
329 size_t
reloc_count() const330 reloc_count() const
331 { return this->relocs_.size(); }
332
333 // Return index of the I-th instruction with relocation.
334 size_t
reloc_insn_index(size_t i) const335 reloc_insn_index(size_t i) const
336 {
337 gold_assert(i < this->relocs_.size());
338 return this->relocs_[i].first;
339 }
340
341 // Return the offset of the I-th instruction with relocation from the
342 // beginning of the stub.
343 section_size_type
reloc_offset(size_t i) const344 reloc_offset(size_t i) const
345 {
346 gold_assert(i < this->relocs_.size());
347 return this->relocs_[i].second;
348 }
349
350 private:
351 // This contains information about an instruction template with a relocation
352 // and its offset from start of stub.
353 typedef std::pair<size_t, section_size_type> Reloc;
354
355 // A Stub_template may not be copied. We want to share templates as much
356 // as possible.
357 Stub_template(const Stub_template&);
358 Stub_template& operator=(const Stub_template&);
359
360 // Stub type.
361 Stub_type type_;
362 // Points to an array of Insn_templates.
363 const Insn_template* insns_;
364 // Number of Insn_templates in insns_[].
365 size_t insn_count_;
366 // Size of templated instructions in bytes.
367 size_t size_;
368 // Alignment of templated instructions.
369 unsigned alignment_;
370 // Flag to indicate if entry is in thumb mode.
371 bool entry_in_thumb_mode_;
372 // A table of reloc instruction indices and offsets. We can find these by
373 // looking at the instruction templates but we pre-compute and then stash
374 // them here for speed.
375 std::vector<Reloc> relocs_;
376 };
377
378 //
379 // A class for code stubs. This is a base class for different type of
380 // stubs used in the ARM target.
381 //
382
383 class Stub
384 {
385 private:
386 static const section_offset_type invalid_offset =
387 static_cast<section_offset_type>(-1);
388
389 public:
Stub(const Stub_template * stub_template)390 Stub(const Stub_template* stub_template)
391 : stub_template_(stub_template), offset_(invalid_offset)
392 { }
393
394 virtual
~Stub()395 ~Stub()
396 { }
397
398 // Return the stub template.
399 const Stub_template*
stub_template() const400 stub_template() const
401 { return this->stub_template_; }
402
403 // Return offset of code stub from beginning of its containing stub table.
404 section_offset_type
offset() const405 offset() const
406 {
407 gold_assert(this->offset_ != invalid_offset);
408 return this->offset_;
409 }
410
411 // Set offset of code stub from beginning of its containing stub table.
412 void
set_offset(section_offset_type offset)413 set_offset(section_offset_type offset)
414 { this->offset_ = offset; }
415
416 // Return the relocation target address of the i-th relocation in the
417 // stub. This must be defined in a child class.
418 Arm_address
reloc_target(size_t i)419 reloc_target(size_t i)
420 { return this->do_reloc_target(i); }
421
422 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
423 void
write(unsigned char * view,section_size_type view_size,bool big_endian)424 write(unsigned char* view, section_size_type view_size, bool big_endian)
425 { this->do_write(view, view_size, big_endian); }
426
427 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
428 // for the i-th instruction.
429 uint16_t
thumb16_special(size_t i)430 thumb16_special(size_t i)
431 { return this->do_thumb16_special(i); }
432
433 protected:
434 // This must be defined in the child class.
435 virtual Arm_address
436 do_reloc_target(size_t) = 0;
437
438 // This may be overridden in the child class.
439 virtual void
do_write(unsigned char * view,section_size_type view_size,bool big_endian)440 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
441 {
442 if (big_endian)
443 this->do_fixed_endian_write<true>(view, view_size);
444 else
445 this->do_fixed_endian_write<false>(view, view_size);
446 }
447
448 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
449 // instruction template.
450 virtual uint16_t
do_thumb16_special(size_t)451 do_thumb16_special(size_t)
452 { gold_unreachable(); }
453
454 private:
455 // A template to implement do_write.
456 template<bool big_endian>
457 void inline
458 do_fixed_endian_write(unsigned char*, section_size_type);
459
460 // Its template.
461 const Stub_template* stub_template_;
462 // Offset within the section of containing this stub.
463 section_offset_type offset_;
464 };
465
466 // Reloc stub class. These are stubs we use to fix up relocation because
467 // of limited branch ranges.
468
469 class Reloc_stub : public Stub
470 {
471 public:
472 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
473 // We assume we never jump to this address.
474 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
475
476 // Return destination address.
477 Arm_address
destination_address() const478 destination_address() const
479 {
480 gold_assert(this->destination_address_ != this->invalid_address);
481 return this->destination_address_;
482 }
483
484 // Set destination address.
485 void
set_destination_address(Arm_address address)486 set_destination_address(Arm_address address)
487 {
488 gold_assert(address != this->invalid_address);
489 this->destination_address_ = address;
490 }
491
492 // Reset destination address.
493 void
reset_destination_address()494 reset_destination_address()
495 { this->destination_address_ = this->invalid_address; }
496
497 // Determine stub type for a branch of a relocation of R_TYPE going
498 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
499 // the branch target is a thumb instruction. TARGET is used for look
500 // up ARM-specific linker settings.
501 static Stub_type
502 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
503 Arm_address branch_target, bool target_is_thumb);
504
505 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
506 // and an addend. Since we treat global and local symbol differently, we
507 // use a Symbol object for a global symbol and a object-index pair for
508 // a local symbol.
509 class Key
510 {
511 public:
512 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
513 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
514 // and R_SYM must not be invalid_index.
Key(Stub_type stub_type,const Symbol * symbol,const Relobj * relobj,unsigned int r_sym,int32_t addend)515 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
516 unsigned int r_sym, int32_t addend)
517 : stub_type_(stub_type), addend_(addend)
518 {
519 if (symbol != NULL)
520 {
521 this->r_sym_ = Reloc_stub::invalid_index;
522 this->u_.symbol = symbol;
523 }
524 else
525 {
526 gold_assert(relobj != NULL && r_sym != invalid_index);
527 this->r_sym_ = r_sym;
528 this->u_.relobj = relobj;
529 }
530 }
531
~Key()532 ~Key()
533 { }
534
535 // Accessors: Keys are meant to be read-only object so no modifiers are
536 // provided.
537
538 // Return stub type.
539 Stub_type
stub_type() const540 stub_type() const
541 { return this->stub_type_; }
542
543 // Return the local symbol index or invalid_index.
544 unsigned int
r_sym() const545 r_sym() const
546 { return this->r_sym_; }
547
548 // Return the symbol if there is one.
549 const Symbol*
symbol() const550 symbol() const
551 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
552
553 // Return the relobj if there is one.
554 const Relobj*
relobj() const555 relobj() const
556 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
557
558 // Whether this equals to another key k.
559 bool
eq(const Key & k) const560 eq(const Key& k) const
561 {
562 return ((this->stub_type_ == k.stub_type_)
563 && (this->r_sym_ == k.r_sym_)
564 && ((this->r_sym_ != Reloc_stub::invalid_index)
565 ? (this->u_.relobj == k.u_.relobj)
566 : (this->u_.symbol == k.u_.symbol))
567 && (this->addend_ == k.addend_));
568 }
569
570 // Return a hash value.
571 size_t
hash_value() const572 hash_value() const
573 {
574 return (this->stub_type_
575 ^ this->r_sym_
576 ^ gold::string_hash<char>(
577 (this->r_sym_ != Reloc_stub::invalid_index)
578 ? this->u_.relobj->name().c_str()
579 : this->u_.symbol->name())
580 ^ this->addend_);
581 }
582
583 // Functors for STL associative containers.
584 struct hash
585 {
586 size_t
operator ()__anon49345ebd0111::Reloc_stub::Key::hash587 operator()(const Key& k) const
588 { return k.hash_value(); }
589 };
590
591 struct equal_to
592 {
593 bool
operator ()__anon49345ebd0111::Reloc_stub::Key::equal_to594 operator()(const Key& k1, const Key& k2) const
595 { return k1.eq(k2); }
596 };
597
598 // Name of key. This is mainly for debugging.
599 std::string
600 name() const;
601
602 private:
603 // Stub type.
604 Stub_type stub_type_;
605 // If this is a local symbol, this is the index in the defining object.
606 // Otherwise, it is invalid_index for a global symbol.
607 unsigned int r_sym_;
608 // If r_sym_ is an invalid index, this points to a global symbol.
609 // Otherwise, it points to a relobj. We used the unsized and target
610 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
611 // Arm_relobj, in order to avoid making the stub class a template
612 // as most of the stub machinery is endianness-neutral. However, it
613 // may require a bit of casting done by users of this class.
614 union
615 {
616 const Symbol* symbol;
617 const Relobj* relobj;
618 } u_;
619 // Addend associated with a reloc.
620 int32_t addend_;
621 };
622
623 protected:
624 // Reloc_stubs are created via a stub factory. So these are protected.
Reloc_stub(const Stub_template * stub_template)625 Reloc_stub(const Stub_template* stub_template)
626 : Stub(stub_template), destination_address_(invalid_address)
627 { }
628
~Reloc_stub()629 ~Reloc_stub()
630 { }
631
632 friend class Stub_factory;
633
634 // Return the relocation target address of the i-th relocation in the
635 // stub.
636 Arm_address
do_reloc_target(size_t i)637 do_reloc_target(size_t i)
638 {
639 // All reloc stub have only one relocation.
640 gold_assert(i == 0);
641 return this->destination_address_;
642 }
643
644 private:
645 // Address of destination.
646 Arm_address destination_address_;
647 };
648
649 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
650 // THUMB branch that meets the following conditions:
651 //
652 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
653 // branch address is 0xffe.
654 // 2. The branch target address is in the same page as the first word of the
655 // branch.
656 // 3. The branch follows a 32-bit instruction which is not a branch.
657 //
658 // To do the fix up, we need to store the address of the branch instruction
659 // and its target at least. We also need to store the original branch
660 // instruction bits for the condition code in a conditional branch. The
661 // condition code is used in a special instruction template. We also want
662 // to identify input sections needing Cortex-A8 workaround quickly. We store
663 // extra information about object and section index of the code section
664 // containing a branch being fixed up. The information is used to mark
665 // the code section when we finalize the Cortex-A8 stubs.
666 //
667
668 class Cortex_a8_stub : public Stub
669 {
670 public:
~Cortex_a8_stub()671 ~Cortex_a8_stub()
672 { }
673
674 // Return the object of the code section containing the branch being fixed
675 // up.
676 Relobj*
relobj() const677 relobj() const
678 { return this->relobj_; }
679
680 // Return the section index of the code section containing the branch being
681 // fixed up.
682 unsigned int
shndx() const683 shndx() const
684 { return this->shndx_; }
685
686 // Return the source address of stub. This is the address of the original
687 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
688 // instruction.
689 Arm_address
source_address() const690 source_address() const
691 { return this->source_address_; }
692
693 // Return the destination address of the stub. This is the branch taken
694 // address of the original branch instruction. LSB is 1 if it is a THUMB
695 // instruction address.
696 Arm_address
destination_address() const697 destination_address() const
698 { return this->destination_address_; }
699
700 // Return the instruction being fixed up.
701 uint32_t
original_insn() const702 original_insn() const
703 { return this->original_insn_; }
704
705 protected:
706 // Cortex_a8_stubs are created via a stub factory. So these are protected.
Cortex_a8_stub(const Stub_template * stub_template,Relobj * relobj,unsigned int shndx,Arm_address source_address,Arm_address destination_address,uint32_t original_insn)707 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
708 unsigned int shndx, Arm_address source_address,
709 Arm_address destination_address, uint32_t original_insn)
710 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
711 source_address_(source_address | 1U),
712 destination_address_(destination_address),
713 original_insn_(original_insn)
714 { }
715
716 friend class Stub_factory;
717
718 // Return the relocation target address of the i-th relocation in the
719 // stub.
720 Arm_address
do_reloc_target(size_t i)721 do_reloc_target(size_t i)
722 {
723 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
724 {
725 // The conditional branch veneer has two relocations.
726 gold_assert(i < 2);
727 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
728 }
729 else
730 {
731 // All other Cortex-A8 stubs have only one relocation.
732 gold_assert(i == 0);
733 return this->destination_address_;
734 }
735 }
736
737 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
738 uint16_t
739 do_thumb16_special(size_t);
740
741 private:
742 // Object of the code section containing the branch being fixed up.
743 Relobj* relobj_;
744 // Section index of the code section containing the branch begin fixed up.
745 unsigned int shndx_;
746 // Source address of original branch.
747 Arm_address source_address_;
748 // Destination address of the original branch.
749 Arm_address destination_address_;
750 // Original branch instruction. This is needed for copying the condition
751 // code from a condition branch to its stub.
752 uint32_t original_insn_;
753 };
754
755 // ARMv4 BX Rx branch relocation stub class.
756 class Arm_v4bx_stub : public Stub
757 {
758 public:
~Arm_v4bx_stub()759 ~Arm_v4bx_stub()
760 { }
761
762 // Return the associated register.
763 uint32_t
reg() const764 reg() const
765 { return this->reg_; }
766
767 protected:
768 // Arm V4BX stubs are created via a stub factory. So these are protected.
Arm_v4bx_stub(const Stub_template * stub_template,const uint32_t reg)769 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
770 : Stub(stub_template), reg_(reg)
771 { }
772
773 friend class Stub_factory;
774
775 // Return the relocation target address of the i-th relocation in the
776 // stub.
777 Arm_address
do_reloc_target(size_t)778 do_reloc_target(size_t)
779 { gold_unreachable(); }
780
781 // This may be overridden in the child class.
782 virtual void
do_write(unsigned char * view,section_size_type view_size,bool big_endian)783 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
784 {
785 if (big_endian)
786 this->do_fixed_endian_v4bx_write<true>(view, view_size);
787 else
788 this->do_fixed_endian_v4bx_write<false>(view, view_size);
789 }
790
791 private:
792 // A template to implement do_write.
793 template<bool big_endian>
794 void inline
do_fixed_endian_v4bx_write(unsigned char * view,section_size_type)795 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
796 {
797 const Insn_template* insns = this->stub_template()->insns();
798 elfcpp::Swap<32, big_endian>::writeval(view,
799 (insns[0].data()
800 + (this->reg_ << 16)));
801 view += insns[0].size();
802 elfcpp::Swap<32, big_endian>::writeval(view,
803 (insns[1].data() + this->reg_));
804 view += insns[1].size();
805 elfcpp::Swap<32, big_endian>::writeval(view,
806 (insns[2].data() + this->reg_));
807 }
808
809 // A register index (r0-r14), which is associated with the stub.
810 uint32_t reg_;
811 };
812
813 // Stub factory class.
814
815 class Stub_factory
816 {
817 public:
818 // Return the unique instance of this class.
819 static const Stub_factory&
get_instance()820 get_instance()
821 {
822 static Stub_factory singleton;
823 return singleton;
824 }
825
826 // Make a relocation stub.
827 Reloc_stub*
make_reloc_stub(Stub_type stub_type) const828 make_reloc_stub(Stub_type stub_type) const
829 {
830 gold_assert(stub_type >= arm_stub_reloc_first
831 && stub_type <= arm_stub_reloc_last);
832 return new Reloc_stub(this->stub_templates_[stub_type]);
833 }
834
835 // Make a Cortex-A8 stub.
836 Cortex_a8_stub*
make_cortex_a8_stub(Stub_type stub_type,Relobj * relobj,unsigned int shndx,Arm_address source,Arm_address destination,uint32_t original_insn) const837 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
838 Arm_address source, Arm_address destination,
839 uint32_t original_insn) const
840 {
841 gold_assert(stub_type >= arm_stub_cortex_a8_first
842 && stub_type <= arm_stub_cortex_a8_last);
843 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
844 source, destination, original_insn);
845 }
846
847 // Make an ARM V4BX relocation stub.
848 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
849 Arm_v4bx_stub*
make_arm_v4bx_stub(uint32_t reg) const850 make_arm_v4bx_stub(uint32_t reg) const
851 {
852 gold_assert(reg < 0xf);
853 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
854 reg);
855 }
856
857 private:
858 // Constructor and destructor are protected since we only return a single
859 // instance created in Stub_factory::get_instance().
860
861 Stub_factory();
862
863 // A Stub_factory may not be copied since it is a singleton.
864 Stub_factory(const Stub_factory&);
865 Stub_factory& operator=(Stub_factory&);
866
867 // Stub templates. These are initialized in the constructor.
868 const Stub_template* stub_templates_[arm_stub_type_last+1];
869 };
870
871 // A class to hold stubs for the ARM target.
872
873 template<bool big_endian>
874 class Stub_table : public Output_data
875 {
876 public:
Stub_table(Arm_input_section<big_endian> * owner)877 Stub_table(Arm_input_section<big_endian>* owner)
878 : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
879 reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
880 prev_data_size_(0), prev_addralign_(1), padding_(0)
881 { }
882
~Stub_table()883 ~Stub_table()
884 { }
885
886 // Owner of this stub table.
887 Arm_input_section<big_endian>*
owner() const888 owner() const
889 { return this->owner_; }
890
891 // Whether this stub table is empty.
892 bool
empty() const893 empty() const
894 {
895 return (this->reloc_stubs_.empty()
896 && this->cortex_a8_stubs_.empty()
897 && this->arm_v4bx_stubs_.empty());
898 }
899
900 // Return the current data size.
901 off_t
current_data_size() const902 current_data_size() const
903 { return this->current_data_size_for_child(); }
904
905 // Add a STUB using KEY. The caller is responsible for avoiding addition
906 // if a STUB with the same key has already been added.
907 void
add_reloc_stub(Reloc_stub * stub,const Reloc_stub::Key & key)908 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
909 {
910 const Stub_template* stub_template = stub->stub_template();
911 gold_assert(stub_template->type() == key.stub_type());
912 this->reloc_stubs_[key] = stub;
913
914 // Assign stub offset early. We can do this because we never remove
915 // reloc stubs and they are in the beginning of the stub table.
916 uint64_t align = stub_template->alignment();
917 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
918 stub->set_offset(this->reloc_stubs_size_);
919 this->reloc_stubs_size_ += stub_template->size();
920 this->reloc_stubs_addralign_ =
921 std::max(this->reloc_stubs_addralign_, align);
922 }
923
924 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
925 // The caller is responsible for avoiding addition if a STUB with the same
926 // address has already been added.
927 void
add_cortex_a8_stub(Arm_address address,Cortex_a8_stub * stub)928 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
929 {
930 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
931 this->cortex_a8_stubs_.insert(value);
932 }
933
934 // Add an ARM V4BX relocation stub. A register index will be retrieved
935 // from the stub.
936 void
add_arm_v4bx_stub(Arm_v4bx_stub * stub)937 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
938 {
939 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
940 this->arm_v4bx_stubs_[stub->reg()] = stub;
941 }
942
943 // Remove all Cortex-A8 stubs.
944 void
945 remove_all_cortex_a8_stubs();
946
947 // Look up a relocation stub using KEY. Return NULL if there is none.
948 Reloc_stub*
find_reloc_stub(const Reloc_stub::Key & key) const949 find_reloc_stub(const Reloc_stub::Key& key) const
950 {
951 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
952 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
953 }
954
955 // Look up an arm v4bx relocation stub using the register index.
956 // Return NULL if there is none.
957 Arm_v4bx_stub*
find_arm_v4bx_stub(const uint32_t reg) const958 find_arm_v4bx_stub(const uint32_t reg) const
959 {
960 gold_assert(reg < 0xf);
961 return this->arm_v4bx_stubs_[reg];
962 }
963
964 // Relocate stubs in this stub table.
965 void
966 relocate_stubs(const Relocate_info<32, big_endian>*,
967 Target_arm<big_endian>*, Output_section*,
968 unsigned char*, Arm_address, section_size_type);
969
970 // Update data size and alignment at the end of a relaxation pass. Return
971 // true if either data size or alignment is different from that of the
972 // previous relaxation pass.
973 bool
974 update_data_size_and_addralign();
975
976 // Finalize stubs. Set the offsets of all stubs and mark input sections
977 // needing the Cortex-A8 workaround.
978 void
979 finalize_stubs();
980
981 // Apply Cortex-A8 workaround to an address range.
982 void
983 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
984 unsigned char*, Arm_address,
985 section_size_type);
986
987 protected:
988 // Write out section contents.
989 void
990 do_write(Output_file*);
991
992 // Return the required alignment.
993 uint64_t
do_addralign() const994 do_addralign() const
995 { return this->prev_addralign_; }
996
997 // Reset address and file offset.
998 void
do_reset_address_and_file_offset()999 do_reset_address_and_file_offset()
1000 {
1001 this->set_current_data_size_for_child(
1002 this->prev_data_size_ + this->padding_);
1003 }
1004
1005 // Set final data size.
1006 void
set_final_data_size()1007 set_final_data_size()
1008 { this->set_data_size(this->current_data_size()); }
1009
1010 // Relocate one stub.
1011 void
1012 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1013 Target_arm<big_endian>*, Output_section*,
1014 unsigned char*, Arm_address, section_size_type);
1015
1016 // Unordered map of relocation stubs.
1017 typedef
1018 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1019 Reloc_stub::Key::equal_to>
1020 Reloc_stub_map;
1021
1022 // List of Cortex-A8 stubs ordered by addresses of branches being
1023 // fixed up in output.
1024 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1025 // List of Arm V4BX relocation stubs ordered by associated registers.
1026 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1027
1028 // Owner of this stub table.
1029 Arm_input_section<big_endian>* owner_;
1030 // The relocation stubs.
1031 Reloc_stub_map reloc_stubs_;
1032 // Size of reloc stubs.
1033 off_t reloc_stubs_size_;
1034 // Maximum address alignment of reloc stubs.
1035 uint64_t reloc_stubs_addralign_;
1036 // The cortex_a8_stubs.
1037 Cortex_a8_stub_list cortex_a8_stubs_;
1038 // The Arm V4BX relocation stubs.
1039 Arm_v4bx_stub_list arm_v4bx_stubs_;
1040 // data size of this in the previous pass.
1041 off_t prev_data_size_;
1042 // address alignment of this in the previous pass.
1043 uint64_t prev_addralign_;
1044 off_t padding_;
1045 };
1046
1047 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1048 // we add to the end of an EXIDX input section that goes into the output.
1049
1050 class Arm_exidx_cantunwind : public Output_section_data
1051 {
1052 public:
Arm_exidx_cantunwind(Relobj * relobj,unsigned int shndx)1053 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1054 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1055 { }
1056
1057 // Return the object containing the section pointed by this.
1058 Relobj*
relobj() const1059 relobj() const
1060 { return this->relobj_; }
1061
1062 // Return the section index of the section pointed by this.
1063 unsigned int
shndx() const1064 shndx() const
1065 { return this->shndx_; }
1066
1067 protected:
1068 void
do_write(Output_file * of)1069 do_write(Output_file* of)
1070 {
1071 if (parameters->target().is_big_endian())
1072 this->do_fixed_endian_write<true>(of);
1073 else
1074 this->do_fixed_endian_write<false>(of);
1075 }
1076
1077 // Write to a map file.
1078 void
do_print_to_mapfile(Mapfile * mapfile) const1079 do_print_to_mapfile(Mapfile* mapfile) const
1080 { mapfile->print_output_data(this, _("** ARM cantunwind")); }
1081
1082 private:
1083 // Implement do_write for a given endianness.
1084 template<bool big_endian>
1085 void inline
1086 do_fixed_endian_write(Output_file*);
1087
1088 // The object containing the section pointed by this.
1089 Relobj* relobj_;
1090 // The section index of the section pointed by this.
1091 unsigned int shndx_;
1092 };
1093
1094 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1095 // Offset map is used to map input section offset within the EXIDX section
1096 // to the output offset from the start of this EXIDX section.
1097
1098 typedef std::map<section_offset_type, section_offset_type>
1099 Arm_exidx_section_offset_map;
1100
1101 // Arm_exidx_merged_section class. This represents an EXIDX input section
1102 // with some of its entries merged.
1103
1104 class Arm_exidx_merged_section : public Output_relaxed_input_section
1105 {
1106 public:
1107 // Constructor for Arm_exidx_merged_section.
1108 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1109 // SECTION_OFFSET_MAP points to a section offset map describing how
1110 // parts of the input section are mapped to output. DELETED_BYTES is
1111 // the number of bytes deleted from the EXIDX input section.
1112 Arm_exidx_merged_section(
1113 const Arm_exidx_input_section& exidx_input_section,
1114 const Arm_exidx_section_offset_map& section_offset_map,
1115 uint32_t deleted_bytes);
1116
1117 // Build output contents.
1118 void
1119 build_contents(const unsigned char*, section_size_type);
1120
1121 // Return the original EXIDX input section.
1122 const Arm_exidx_input_section&
exidx_input_section() const1123 exidx_input_section() const
1124 { return this->exidx_input_section_; }
1125
1126 // Return the section offset map.
1127 const Arm_exidx_section_offset_map&
section_offset_map() const1128 section_offset_map() const
1129 { return this->section_offset_map_; }
1130
1131 protected:
1132 // Write merged section into file OF.
1133 void
1134 do_write(Output_file* of);
1135
1136 bool
1137 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1138 section_offset_type*) const;
1139
1140 private:
1141 // Original EXIDX input section.
1142 const Arm_exidx_input_section& exidx_input_section_;
1143 // Section offset map.
1144 const Arm_exidx_section_offset_map& section_offset_map_;
1145 // Merged section contents. We need to keep build the merged section
1146 // and save it here to avoid accessing the original EXIDX section when
1147 // we cannot lock the sections' object.
1148 unsigned char* section_contents_;
1149 };
1150
1151 // A class to wrap an ordinary input section containing executable code.
1152
1153 template<bool big_endian>
1154 class Arm_input_section : public Output_relaxed_input_section
1155 {
1156 public:
Arm_input_section(Relobj * relobj,unsigned int shndx)1157 Arm_input_section(Relobj* relobj, unsigned int shndx)
1158 : Output_relaxed_input_section(relobj, shndx, 1),
1159 original_addralign_(1), original_size_(0), stub_table_(NULL),
1160 original_contents_(NULL)
1161 { }
1162
~Arm_input_section()1163 ~Arm_input_section()
1164 { delete[] this->original_contents_; }
1165
1166 // Initialize.
1167 void
1168 init();
1169
1170 // Whether this is a stub table owner.
1171 bool
is_stub_table_owner() const1172 is_stub_table_owner() const
1173 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1174
1175 // Return the stub table.
1176 Stub_table<big_endian>*
stub_table() const1177 stub_table() const
1178 { return this->stub_table_; }
1179
1180 // Set the stub_table.
1181 void
set_stub_table(Stub_table<big_endian> * stub_table)1182 set_stub_table(Stub_table<big_endian>* stub_table)
1183 { this->stub_table_ = stub_table; }
1184
1185 // Downcast a base pointer to an Arm_input_section pointer. This is
1186 // not type-safe but we only use Arm_input_section not the base class.
1187 static Arm_input_section<big_endian>*
as_arm_input_section(Output_relaxed_input_section * poris)1188 as_arm_input_section(Output_relaxed_input_section* poris)
1189 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1190
1191 // Return the original size of the section.
1192 uint32_t
original_size() const1193 original_size() const
1194 { return this->original_size_; }
1195
1196 protected:
1197 // Write data to output file.
1198 void
1199 do_write(Output_file*);
1200
1201 // Return required alignment of this.
1202 uint64_t
do_addralign() const1203 do_addralign() const
1204 {
1205 if (this->is_stub_table_owner())
1206 return std::max(this->stub_table_->addralign(),
1207 static_cast<uint64_t>(this->original_addralign_));
1208 else
1209 return this->original_addralign_;
1210 }
1211
1212 // Finalize data size.
1213 void
1214 set_final_data_size();
1215
1216 // Reset address and file offset.
1217 void
1218 do_reset_address_and_file_offset();
1219
1220 // Output offset.
1221 bool
do_output_offset(const Relobj * object,unsigned int shndx,section_offset_type offset,section_offset_type * poutput) const1222 do_output_offset(const Relobj* object, unsigned int shndx,
1223 section_offset_type offset,
1224 section_offset_type* poutput) const
1225 {
1226 if ((object == this->relobj())
1227 && (shndx == this->shndx())
1228 && (offset >= 0)
1229 && (offset <=
1230 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1231 {
1232 *poutput = offset;
1233 return true;
1234 }
1235 else
1236 return false;
1237 }
1238
1239 private:
1240 // Copying is not allowed.
1241 Arm_input_section(const Arm_input_section&);
1242 Arm_input_section& operator=(const Arm_input_section&);
1243
1244 // Address alignment of the original input section.
1245 uint32_t original_addralign_;
1246 // Section size of the original input section.
1247 uint32_t original_size_;
1248 // Stub table.
1249 Stub_table<big_endian>* stub_table_;
1250 // Original section contents. We have to make a copy here since the file
1251 // containing the original section may not be locked when we need to access
1252 // the contents.
1253 unsigned char* original_contents_;
1254 };
1255
1256 // Arm_exidx_fixup class. This is used to define a number of methods
1257 // and keep states for fixing up EXIDX coverage.
1258
1259 class Arm_exidx_fixup
1260 {
1261 public:
Arm_exidx_fixup(Output_section * exidx_output_section,bool merge_exidx_entries=true)1262 Arm_exidx_fixup(Output_section* exidx_output_section,
1263 bool merge_exidx_entries = true)
1264 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1265 last_inlined_entry_(0), last_input_section_(NULL),
1266 section_offset_map_(NULL), first_output_text_section_(NULL),
1267 merge_exidx_entries_(merge_exidx_entries)
1268 { }
1269
~Arm_exidx_fixup()1270 ~Arm_exidx_fixup()
1271 { delete this->section_offset_map_; }
1272
1273 // Process an EXIDX section for entry merging. SECTION_CONTENTS points
1274 // to the EXIDX contents and SECTION_SIZE is the size of the contents. Return
1275 // number of bytes to be deleted in output. If parts of the input EXIDX
1276 // section are merged a heap allocated Arm_exidx_section_offset_map is store
1277 // in the located PSECTION_OFFSET_MAP. The caller owns the map and is
1278 // responsible for releasing it.
1279 template<bool big_endian>
1280 uint32_t
1281 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1282 const unsigned char* section_contents,
1283 section_size_type section_size,
1284 Arm_exidx_section_offset_map** psection_offset_map);
1285
1286 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1287 // input section, if there is not one already.
1288 void
1289 add_exidx_cantunwind_as_needed();
1290
1291 // Return the output section for the text section which is linked to the
1292 // first exidx input in output.
1293 Output_section*
first_output_text_section() const1294 first_output_text_section() const
1295 { return this->first_output_text_section_; }
1296
1297 private:
1298 // Copying is not allowed.
1299 Arm_exidx_fixup(const Arm_exidx_fixup&);
1300 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1301
1302 // Type of EXIDX unwind entry.
1303 enum Unwind_type
1304 {
1305 // No type.
1306 UT_NONE,
1307 // EXIDX_CANTUNWIND.
1308 UT_EXIDX_CANTUNWIND,
1309 // Inlined entry.
1310 UT_INLINED_ENTRY,
1311 // Normal entry.
1312 UT_NORMAL_ENTRY,
1313 };
1314
1315 // Process an EXIDX entry. We only care about the second word of the
1316 // entry. Return true if the entry can be deleted.
1317 bool
1318 process_exidx_entry(uint32_t second_word);
1319
1320 // Update the current section offset map during EXIDX section fix-up.
1321 // If there is no map, create one. INPUT_OFFSET is the offset of a
1322 // reference point, DELETED_BYTES is the number of deleted by in the
1323 // section so far. If DELETE_ENTRY is true, the reference point and
1324 // all offsets after the previous reference point are discarded.
1325 void
1326 update_offset_map(section_offset_type input_offset,
1327 section_size_type deleted_bytes, bool delete_entry);
1328
1329 // EXIDX output section.
1330 Output_section* exidx_output_section_;
1331 // Unwind type of the last EXIDX entry processed.
1332 Unwind_type last_unwind_type_;
1333 // Last seen inlined EXIDX entry.
1334 uint32_t last_inlined_entry_;
1335 // Last processed EXIDX input section.
1336 const Arm_exidx_input_section* last_input_section_;
1337 // Section offset map created in process_exidx_section.
1338 Arm_exidx_section_offset_map* section_offset_map_;
1339 // Output section for the text section which is linked to the first exidx
1340 // input in output.
1341 Output_section* first_output_text_section_;
1342
1343 bool merge_exidx_entries_;
1344 };
1345
1346 // Arm output section class. This is defined mainly to add a number of
1347 // stub generation methods.
1348
1349 template<bool big_endian>
1350 class Arm_output_section : public Output_section
1351 {
1352 public:
1353 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1354
1355 // We need to force SHF_LINK_ORDER in a SHT_ARM_EXIDX section.
Arm_output_section(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags)1356 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1357 elfcpp::Elf_Xword flags)
1358 : Output_section(name, type,
1359 (type == elfcpp::SHT_ARM_EXIDX
1360 ? flags | elfcpp::SHF_LINK_ORDER
1361 : flags))
1362 {
1363 if (type == elfcpp::SHT_ARM_EXIDX)
1364 this->set_always_keeps_input_sections();
1365 }
1366
~Arm_output_section()1367 ~Arm_output_section()
1368 { }
1369
1370 // Group input sections for stub generation.
1371 void
1372 group_sections(section_size_type, bool, Target_arm<big_endian>*, const Task*);
1373
1374 // Downcast a base pointer to an Arm_output_section pointer. This is
1375 // not type-safe but we only use Arm_output_section not the base class.
1376 static Arm_output_section<big_endian>*
as_arm_output_section(Output_section * os)1377 as_arm_output_section(Output_section* os)
1378 { return static_cast<Arm_output_section<big_endian>*>(os); }
1379
1380 // Append all input text sections in this into LIST.
1381 void
1382 append_text_sections_to_list(Text_section_list* list);
1383
1384 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1385 // is a list of text input sections sorted in ascending order of their
1386 // output addresses.
1387 void
1388 fix_exidx_coverage(Layout* layout,
1389 const Text_section_list& sorted_text_section,
1390 Symbol_table* symtab,
1391 bool merge_exidx_entries,
1392 const Task* task);
1393
1394 // Link an EXIDX section into its corresponding text section.
1395 void
1396 set_exidx_section_link();
1397
1398 private:
1399 // For convenience.
1400 typedef Output_section::Input_section Input_section;
1401 typedef Output_section::Input_section_list Input_section_list;
1402
1403 // Create a stub group.
1404 void create_stub_group(Input_section_list::const_iterator,
1405 Input_section_list::const_iterator,
1406 Input_section_list::const_iterator,
1407 Target_arm<big_endian>*,
1408 std::vector<Output_relaxed_input_section*>*,
1409 const Task* task);
1410 };
1411
1412 // Arm_exidx_input_section class. This represents an EXIDX input section.
1413
1414 class Arm_exidx_input_section
1415 {
1416 public:
1417 static const section_offset_type invalid_offset =
1418 static_cast<section_offset_type>(-1);
1419
Arm_exidx_input_section(Relobj * relobj,unsigned int shndx,unsigned int link,uint32_t size,uint32_t addralign,uint32_t text_size)1420 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1421 unsigned int link, uint32_t size,
1422 uint32_t addralign, uint32_t text_size)
1423 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1424 addralign_(addralign), text_size_(text_size), has_errors_(false)
1425 { }
1426
~Arm_exidx_input_section()1427 ~Arm_exidx_input_section()
1428 { }
1429
1430 // Accessors: This is a read-only class.
1431
1432 // Return the object containing this EXIDX input section.
1433 Relobj*
relobj() const1434 relobj() const
1435 { return this->relobj_; }
1436
1437 // Return the section index of this EXIDX input section.
1438 unsigned int
shndx() const1439 shndx() const
1440 { return this->shndx_; }
1441
1442 // Return the section index of linked text section in the same object.
1443 unsigned int
link() const1444 link() const
1445 { return this->link_; }
1446
1447 // Return size of the EXIDX input section.
1448 uint32_t
size() const1449 size() const
1450 { return this->size_; }
1451
1452 // Return address alignment of EXIDX input section.
1453 uint32_t
addralign() const1454 addralign() const
1455 { return this->addralign_; }
1456
1457 // Return size of the associated text input section.
1458 uint32_t
text_size() const1459 text_size() const
1460 { return this->text_size_; }
1461
1462 // Whether there are any errors in the EXIDX input section.
1463 bool
has_errors() const1464 has_errors() const
1465 { return this->has_errors_; }
1466
1467 // Set has-errors flag.
1468 void
set_has_errors()1469 set_has_errors()
1470 { this->has_errors_ = true; }
1471
1472 private:
1473 // Object containing this.
1474 Relobj* relobj_;
1475 // Section index of this.
1476 unsigned int shndx_;
1477 // text section linked to this in the same object.
1478 unsigned int link_;
1479 // Size of this. For ARM 32-bit is sufficient.
1480 uint32_t size_;
1481 // Address alignment of this. For ARM 32-bit is sufficient.
1482 uint32_t addralign_;
1483 // Size of associated text section.
1484 uint32_t text_size_;
1485 // Whether this has any errors.
1486 bool has_errors_;
1487 };
1488
1489 // Arm_relobj class.
1490
1491 template<bool big_endian>
1492 class Arm_relobj : public Sized_relobj_file<32, big_endian>
1493 {
1494 public:
1495 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1496
Arm_relobj(const std::string & name,Input_file * input_file,off_t offset,const typename elfcpp::Ehdr<32,big_endian> & ehdr)1497 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1498 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1499 : Sized_relobj_file<32, big_endian>(name, input_file, offset, ehdr),
1500 stub_tables_(), local_symbol_is_thumb_function_(),
1501 attributes_section_data_(NULL), mapping_symbols_info_(),
1502 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1503 output_local_symbol_count_needs_update_(false),
1504 merge_flags_and_attributes_(true)
1505 { }
1506
~Arm_relobj()1507 ~Arm_relobj()
1508 { delete this->attributes_section_data_; }
1509
1510 // Return the stub table of the SHNDX-th section if there is one.
1511 Stub_table<big_endian>*
stub_table(unsigned int shndx) const1512 stub_table(unsigned int shndx) const
1513 {
1514 gold_assert(shndx < this->stub_tables_.size());
1515 return this->stub_tables_[shndx];
1516 }
1517
1518 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1519 void
set_stub_table(unsigned int shndx,Stub_table<big_endian> * stub_table)1520 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1521 {
1522 gold_assert(shndx < this->stub_tables_.size());
1523 this->stub_tables_[shndx] = stub_table;
1524 }
1525
1526 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1527 // index. This is only valid after do_count_local_symbol is called.
1528 bool
local_symbol_is_thumb_function(unsigned int r_sym) const1529 local_symbol_is_thumb_function(unsigned int r_sym) const
1530 {
1531 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1532 return this->local_symbol_is_thumb_function_[r_sym];
1533 }
1534
1535 // Scan all relocation sections for stub generation.
1536 void
1537 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1538 const Layout*);
1539
1540 // Convert regular input section with index SHNDX to a relaxed section.
1541 void
convert_input_section_to_relaxed_section(unsigned shndx)1542 convert_input_section_to_relaxed_section(unsigned shndx)
1543 {
1544 // The stubs have relocations and we need to process them after writing
1545 // out the stubs. So relocation now must follow section write.
1546 this->set_section_offset(shndx, -1ULL);
1547 this->set_relocs_must_follow_section_writes();
1548 }
1549
1550 // Downcast a base pointer to an Arm_relobj pointer. This is
1551 // not type-safe but we only use Arm_relobj not the base class.
1552 static Arm_relobj<big_endian>*
as_arm_relobj(Relobj * relobj)1553 as_arm_relobj(Relobj* relobj)
1554 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1555
1556 // Processor-specific flags in ELF file header. This is valid only after
1557 // reading symbols.
1558 elfcpp::Elf_Word
processor_specific_flags() const1559 processor_specific_flags() const
1560 { return this->processor_specific_flags_; }
1561
1562 // Attribute section data This is the contents of the .ARM.attribute section
1563 // if there is one.
1564 const Attributes_section_data*
attributes_section_data() const1565 attributes_section_data() const
1566 { return this->attributes_section_data_; }
1567
1568 // Mapping symbol location.
1569 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1570
1571 // Functor for STL container.
1572 struct Mapping_symbol_position_less
1573 {
1574 bool
operator ()__anon49345ebd0111::Arm_relobj::Mapping_symbol_position_less1575 operator()(const Mapping_symbol_position& p1,
1576 const Mapping_symbol_position& p2) const
1577 {
1578 return (p1.first < p2.first
1579 || (p1.first == p2.first && p1.second < p2.second));
1580 }
1581 };
1582
1583 // We only care about the first character of a mapping symbol, so
1584 // we only store that instead of the whole symbol name.
1585 typedef std::map<Mapping_symbol_position, char,
1586 Mapping_symbol_position_less> Mapping_symbols_info;
1587
1588 // Whether a section contains any Cortex-A8 workaround.
1589 bool
section_has_cortex_a8_workaround(unsigned int shndx) const1590 section_has_cortex_a8_workaround(unsigned int shndx) const
1591 {
1592 return (this->section_has_cortex_a8_workaround_ != NULL
1593 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1594 }
1595
1596 // Mark a section that has Cortex-A8 workaround.
1597 void
mark_section_for_cortex_a8_workaround(unsigned int shndx)1598 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1599 {
1600 if (this->section_has_cortex_a8_workaround_ == NULL)
1601 this->section_has_cortex_a8_workaround_ =
1602 new std::vector<bool>(this->shnum(), false);
1603 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1604 }
1605
1606 // Return the EXIDX section of an text section with index SHNDX or NULL
1607 // if the text section has no associated EXIDX section.
1608 const Arm_exidx_input_section*
exidx_input_section_by_link(unsigned int shndx) const1609 exidx_input_section_by_link(unsigned int shndx) const
1610 {
1611 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1612 return ((p != this->exidx_section_map_.end()
1613 && p->second->link() == shndx)
1614 ? p->second
1615 : NULL);
1616 }
1617
1618 // Return the EXIDX section with index SHNDX or NULL if there is none.
1619 const Arm_exidx_input_section*
exidx_input_section_by_shndx(unsigned shndx) const1620 exidx_input_section_by_shndx(unsigned shndx) const
1621 {
1622 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1623 return ((p != this->exidx_section_map_.end()
1624 && p->second->shndx() == shndx)
1625 ? p->second
1626 : NULL);
1627 }
1628
1629 // Whether output local symbol count needs updating.
1630 bool
output_local_symbol_count_needs_update() const1631 output_local_symbol_count_needs_update() const
1632 { return this->output_local_symbol_count_needs_update_; }
1633
1634 // Set output_local_symbol_count_needs_update flag to be true.
1635 void
set_output_local_symbol_count_needs_update()1636 set_output_local_symbol_count_needs_update()
1637 { this->output_local_symbol_count_needs_update_ = true; }
1638
1639 // Update output local symbol count at the end of relaxation.
1640 void
1641 update_output_local_symbol_count();
1642
1643 // Whether we want to merge processor-specific flags and attributes.
1644 bool
merge_flags_and_attributes() const1645 merge_flags_and_attributes() const
1646 { return this->merge_flags_and_attributes_; }
1647
1648 // Export list of EXIDX section indices.
1649 void
get_exidx_shndx_list(std::vector<unsigned int> * list) const1650 get_exidx_shndx_list(std::vector<unsigned int>* list) const
1651 {
1652 list->clear();
1653 for (Exidx_section_map::const_iterator p = this->exidx_section_map_.begin();
1654 p != this->exidx_section_map_.end();
1655 ++p)
1656 {
1657 if (p->second->shndx() == p->first)
1658 list->push_back(p->first);
1659 }
1660 // Sort list to make result independent of implementation of map.
1661 std::sort(list->begin(), list->end());
1662 }
1663
1664 protected:
1665 // Post constructor setup.
1666 void
do_setup()1667 do_setup()
1668 {
1669 // Call parent's setup method.
1670 Sized_relobj_file<32, big_endian>::do_setup();
1671
1672 // Initialize look-up tables.
1673 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1674 this->stub_tables_.swap(empty_stub_table_list);
1675 }
1676
1677 // Count the local symbols.
1678 void
1679 do_count_local_symbols(Stringpool_template<char>*,
1680 Stringpool_template<char>*);
1681
1682 void
1683 do_relocate_sections(
1684 const Symbol_table* symtab, const Layout* layout,
1685 const unsigned char* pshdrs, Output_file* of,
1686 typename Sized_relobj_file<32, big_endian>::Views* pivews);
1687
1688 // Read the symbol information.
1689 void
1690 do_read_symbols(Read_symbols_data* sd);
1691
1692 // Process relocs for garbage collection.
1693 void
1694 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1695
1696 private:
1697
1698 // Whether a section needs to be scanned for relocation stubs.
1699 bool
1700 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1701 const Relobj::Output_sections&,
1702 const Symbol_table*, const unsigned char*);
1703
1704 // Whether a section is a scannable text section.
1705 bool
1706 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1707 const Output_section*, const Symbol_table*);
1708
1709 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1710 bool
1711 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1712 unsigned int, Output_section*,
1713 const Symbol_table*);
1714
1715 // Scan a section for the Cortex-A8 erratum.
1716 void
1717 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1718 unsigned int, Output_section*,
1719 Target_arm<big_endian>*);
1720
1721 // Find the linked text section of an EXIDX section by looking at the
1722 // first relocation of the EXIDX section. PSHDR points to the section
1723 // headers of a relocation section and PSYMS points to the local symbols.
1724 // PSHNDX points to a location storing the text section index if found.
1725 // Return whether we can find the linked section.
1726 bool
1727 find_linked_text_section(const unsigned char* pshdr,
1728 const unsigned char* psyms, unsigned int* pshndx);
1729
1730 //
1731 // Make a new Arm_exidx_input_section object for EXIDX section with
1732 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1733 // index of the linked text section.
1734 void
1735 make_exidx_input_section(unsigned int shndx,
1736 const elfcpp::Shdr<32, big_endian>& shdr,
1737 unsigned int text_shndx,
1738 const elfcpp::Shdr<32, big_endian>& text_shdr);
1739
1740 // Return the output address of either a plain input section or a
1741 // relaxed input section. SHNDX is the section index.
1742 Arm_address
1743 simple_input_section_output_address(unsigned int, Output_section*);
1744
1745 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1746 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1747 Exidx_section_map;
1748
1749 // List of stub tables.
1750 Stub_table_list stub_tables_;
1751 // Bit vector to tell if a local symbol is a thumb function or not.
1752 // This is only valid after do_count_local_symbol is called.
1753 std::vector<bool> local_symbol_is_thumb_function_;
1754 // processor-specific flags in ELF file header.
1755 elfcpp::Elf_Word processor_specific_flags_;
1756 // Object attributes if there is an .ARM.attributes section or NULL.
1757 Attributes_section_data* attributes_section_data_;
1758 // Mapping symbols information.
1759 Mapping_symbols_info mapping_symbols_info_;
1760 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1761 std::vector<bool>* section_has_cortex_a8_workaround_;
1762 // Map a text section to its associated .ARM.exidx section, if there is one.
1763 Exidx_section_map exidx_section_map_;
1764 // Whether output local symbol count needs updating.
1765 bool output_local_symbol_count_needs_update_;
1766 // Whether we merge processor flags and attributes of this object to
1767 // output.
1768 bool merge_flags_and_attributes_;
1769 };
1770
1771 // Arm_dynobj class.
1772
1773 template<bool big_endian>
1774 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1775 {
1776 public:
Arm_dynobj(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<32,big_endian> & ehdr)1777 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1778 const elfcpp::Ehdr<32, big_endian>& ehdr)
1779 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1780 processor_specific_flags_(0), attributes_section_data_(NULL)
1781 { }
1782
~Arm_dynobj()1783 ~Arm_dynobj()
1784 { delete this->attributes_section_data_; }
1785
1786 // Downcast a base pointer to an Arm_relobj pointer. This is
1787 // not type-safe but we only use Arm_relobj not the base class.
1788 static Arm_dynobj<big_endian>*
as_arm_dynobj(Dynobj * dynobj)1789 as_arm_dynobj(Dynobj* dynobj)
1790 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1791
1792 // Processor-specific flags in ELF file header. This is valid only after
1793 // reading symbols.
1794 elfcpp::Elf_Word
processor_specific_flags() const1795 processor_specific_flags() const
1796 { return this->processor_specific_flags_; }
1797
1798 // Attributes section data.
1799 const Attributes_section_data*
attributes_section_data() const1800 attributes_section_data() const
1801 { return this->attributes_section_data_; }
1802
1803 protected:
1804 // Read the symbol information.
1805 void
1806 do_read_symbols(Read_symbols_data* sd);
1807
1808 private:
1809 // processor-specific flags in ELF file header.
1810 elfcpp::Elf_Word processor_specific_flags_;
1811 // Object attributes if there is an .ARM.attributes section or NULL.
1812 Attributes_section_data* attributes_section_data_;
1813 };
1814
1815 // Functor to read reloc addends during stub generation.
1816
1817 template<int sh_type, bool big_endian>
1818 struct Stub_addend_reader
1819 {
1820 // Return the addend for a relocation of a particular type. Depending
1821 // on whether this is a REL or RELA relocation, read the addend from a
1822 // view or from a Reloc object.
1823 elfcpp::Elf_types<32>::Elf_Swxword
1824 operator()(
1825 unsigned int /* r_type */,
1826 const unsigned char* /* view */,
1827 const typename Reloc_types<sh_type,
1828 32, big_endian>::Reloc& /* reloc */) const;
1829 };
1830
1831 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1832
1833 template<bool big_endian>
1834 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1835 {
1836 elfcpp::Elf_types<32>::Elf_Swxword
1837 operator()(
1838 unsigned int,
1839 const unsigned char*,
1840 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1841 };
1842
1843 // Specialized Stub_addend_reader for RELA type relocation sections.
1844 // We currently do not handle RELA type relocation sections but it is trivial
1845 // to implement the addend reader. This is provided for completeness and to
1846 // make it easier to add support for RELA relocation sections in the future.
1847
1848 template<bool big_endian>
1849 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1850 {
1851 elfcpp::Elf_types<32>::Elf_Swxword
operator ()__anon49345ebd0111::Stub_addend_reader1852 operator()(
1853 unsigned int,
1854 const unsigned char*,
1855 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1856 big_endian>::Reloc& reloc) const
1857 { return reloc.get_r_addend(); }
1858 };
1859
1860 // Cortex_a8_reloc class. We keep record of relocation that may need
1861 // the Cortex-A8 erratum workaround.
1862
1863 class Cortex_a8_reloc
1864 {
1865 public:
Cortex_a8_reloc(Reloc_stub * reloc_stub,unsigned r_type,Arm_address destination)1866 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1867 Arm_address destination)
1868 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1869 { }
1870
~Cortex_a8_reloc()1871 ~Cortex_a8_reloc()
1872 { }
1873
1874 // Accessors: This is a read-only class.
1875
1876 // Return the relocation stub associated with this relocation if there is
1877 // one.
1878 const Reloc_stub*
reloc_stub() const1879 reloc_stub() const
1880 { return this->reloc_stub_; }
1881
1882 // Return the relocation type.
1883 unsigned int
r_type() const1884 r_type() const
1885 { return this->r_type_; }
1886
1887 // Return the destination address of the relocation. LSB stores the THUMB
1888 // bit.
1889 Arm_address
destination() const1890 destination() const
1891 { return this->destination_; }
1892
1893 private:
1894 // Associated relocation stub if there is one, or NULL.
1895 const Reloc_stub* reloc_stub_;
1896 // Relocation type.
1897 unsigned int r_type_;
1898 // Destination address of this relocation. LSB is used to distinguish
1899 // ARM/THUMB mode.
1900 Arm_address destination_;
1901 };
1902
1903 // Arm_output_data_got class. We derive this from Output_data_got to add
1904 // extra methods to handle TLS relocations in a static link.
1905
1906 template<bool big_endian>
1907 class Arm_output_data_got : public Output_data_got<32, big_endian>
1908 {
1909 public:
Arm_output_data_got(Symbol_table * symtab,Layout * layout)1910 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1911 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1912 { }
1913
1914 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1915 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1916 // applied in a static link.
1917 void
add_static_reloc(unsigned int got_offset,unsigned int r_type,Symbol * gsym)1918 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1919 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1920
1921 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1922 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1923 // relocation that needs to be applied in a static link.
1924 void
add_static_reloc(unsigned int got_offset,unsigned int r_type,Sized_relobj_file<32,big_endian> * relobj,unsigned int index)1925 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1926 Sized_relobj_file<32, big_endian>* relobj,
1927 unsigned int index)
1928 {
1929 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1930 index));
1931 }
1932
1933 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1934 // The first one is initialized to be 1, which is the module index for
1935 // the main executable and the second one 0. A reloc of the type
1936 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1937 // be applied by gold. GSYM is a global symbol.
1938 void
1939 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1940
1941 // Same as the above but for a local symbol in OBJECT with INDEX.
1942 void
1943 add_tls_gd32_with_static_reloc(unsigned int got_type,
1944 Sized_relobj_file<32, big_endian>* object,
1945 unsigned int index);
1946
1947 protected:
1948 // Write out the GOT table.
1949 void
1950 do_write(Output_file*);
1951
1952 private:
1953 // This class represent dynamic relocations that need to be applied by
1954 // gold because we are using TLS relocations in a static link.
1955 class Static_reloc
1956 {
1957 public:
Static_reloc(unsigned int got_offset,unsigned int r_type,Symbol * gsym)1958 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1959 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1960 { this->u_.global.symbol = gsym; }
1961
Static_reloc(unsigned int got_offset,unsigned int r_type,Sized_relobj_file<32,big_endian> * relobj,unsigned int index)1962 Static_reloc(unsigned int got_offset, unsigned int r_type,
1963 Sized_relobj_file<32, big_endian>* relobj, unsigned int index)
1964 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1965 {
1966 this->u_.local.relobj = relobj;
1967 this->u_.local.index = index;
1968 }
1969
1970 // Return the GOT offset.
1971 unsigned int
got_offset() const1972 got_offset() const
1973 { return this->got_offset_; }
1974
1975 // Relocation type.
1976 unsigned int
r_type() const1977 r_type() const
1978 { return this->r_type_; }
1979
1980 // Whether the symbol is global or not.
1981 bool
symbol_is_global() const1982 symbol_is_global() const
1983 { return this->symbol_is_global_; }
1984
1985 // For a relocation against a global symbol, the global symbol.
1986 Symbol*
symbol() const1987 symbol() const
1988 {
1989 gold_assert(this->symbol_is_global_);
1990 return this->u_.global.symbol;
1991 }
1992
1993 // For a relocation against a local symbol, the defining object.
1994 Sized_relobj_file<32, big_endian>*
relobj() const1995 relobj() const
1996 {
1997 gold_assert(!this->symbol_is_global_);
1998 return this->u_.local.relobj;
1999 }
2000
2001 // For a relocation against a local symbol, the local symbol index.
2002 unsigned int
index() const2003 index() const
2004 {
2005 gold_assert(!this->symbol_is_global_);
2006 return this->u_.local.index;
2007 }
2008
2009 private:
2010 // GOT offset of the entry to which this relocation is applied.
2011 unsigned int got_offset_;
2012 // Type of relocation.
2013 unsigned int r_type_;
2014 // Whether this relocation is against a global symbol.
2015 bool symbol_is_global_;
2016 // A global or local symbol.
2017 union
2018 {
2019 struct
2020 {
2021 // For a global symbol, the symbol itself.
2022 Symbol* symbol;
2023 } global;
2024 struct
2025 {
2026 // For a local symbol, the object defining object.
2027 Sized_relobj_file<32, big_endian>* relobj;
2028 // For a local symbol, the symbol index.
2029 unsigned int index;
2030 } local;
2031 } u_;
2032 };
2033
2034 // Symbol table of the output object.
2035 Symbol_table* symbol_table_;
2036 // Layout of the output object.
2037 Layout* layout_;
2038 // Static relocs to be applied to the GOT.
2039 std::vector<Static_reloc> static_relocs_;
2040 };
2041
2042 // The ARM target has many relocation types with odd-sizes or noncontiguous
2043 // bits. The default handling of relocatable relocation cannot process these
2044 // relocations. So we have to extend the default code.
2045
2046 template<bool big_endian, int sh_type, typename Classify_reloc>
2047 class Arm_scan_relocatable_relocs :
2048 public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
2049 {
2050 public:
2051 // Return the strategy to use for a local symbol which is a section
2052 // symbol, given the relocation type.
2053 inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int r_type,Relobj *)2054 local_section_strategy(unsigned int r_type, Relobj*)
2055 {
2056 if (sh_type == elfcpp::SHT_RELA)
2057 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2058 else
2059 {
2060 if (r_type == elfcpp::R_ARM_TARGET1
2061 || r_type == elfcpp::R_ARM_TARGET2)
2062 {
2063 const Target_arm<big_endian>* arm_target =
2064 Target_arm<big_endian>::default_target();
2065 r_type = arm_target->get_real_reloc_type(r_type);
2066 }
2067
2068 switch(r_type)
2069 {
2070 // Relocations that write nothing. These exclude R_ARM_TARGET1
2071 // and R_ARM_TARGET2.
2072 case elfcpp::R_ARM_NONE:
2073 case elfcpp::R_ARM_V4BX:
2074 case elfcpp::R_ARM_TLS_GOTDESC:
2075 case elfcpp::R_ARM_TLS_CALL:
2076 case elfcpp::R_ARM_TLS_DESCSEQ:
2077 case elfcpp::R_ARM_THM_TLS_CALL:
2078 case elfcpp::R_ARM_GOTRELAX:
2079 case elfcpp::R_ARM_GNU_VTENTRY:
2080 case elfcpp::R_ARM_GNU_VTINHERIT:
2081 case elfcpp::R_ARM_THM_TLS_DESCSEQ16:
2082 case elfcpp::R_ARM_THM_TLS_DESCSEQ32:
2083 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
2084 // These should have been converted to something else above.
2085 case elfcpp::R_ARM_TARGET1:
2086 case elfcpp::R_ARM_TARGET2:
2087 gold_unreachable();
2088 // Relocations that write full 32 bits and
2089 // have alignment of 1.
2090 case elfcpp::R_ARM_ABS32:
2091 case elfcpp::R_ARM_REL32:
2092 case elfcpp::R_ARM_SBREL32:
2093 case elfcpp::R_ARM_GOTOFF32:
2094 case elfcpp::R_ARM_BASE_PREL:
2095 case elfcpp::R_ARM_GOT_BREL:
2096 case elfcpp::R_ARM_BASE_ABS:
2097 case elfcpp::R_ARM_ABS32_NOI:
2098 case elfcpp::R_ARM_REL32_NOI:
2099 case elfcpp::R_ARM_PLT32_ABS:
2100 case elfcpp::R_ARM_GOT_ABS:
2101 case elfcpp::R_ARM_GOT_PREL:
2102 case elfcpp::R_ARM_TLS_GD32:
2103 case elfcpp::R_ARM_TLS_LDM32:
2104 case elfcpp::R_ARM_TLS_LDO32:
2105 case elfcpp::R_ARM_TLS_IE32:
2106 case elfcpp::R_ARM_TLS_LE32:
2107 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED;
2108 default:
2109 // For all other static relocations, return RELOC_SPECIAL.
2110 return Relocatable_relocs::RELOC_SPECIAL;
2111 }
2112 }
2113 }
2114 };
2115
2116 template<bool big_endian>
2117 class Target_arm : public Sized_target<32, big_endian>
2118 {
2119 public:
2120 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2121 Reloc_section;
2122
2123 // When were are relocating a stub, we pass this as the relocation number.
2124 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2125
Target_arm(const Target::Target_info * info=& arm_info)2126 Target_arm(const Target::Target_info* info = &arm_info)
2127 : Sized_target<32, big_endian>(info),
2128 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2129 rel_dyn_(NULL), rel_irelative_(NULL), copy_relocs_(elfcpp::R_ARM_COPY),
2130 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2131 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2132 should_force_pic_veneer_(false),
2133 arm_input_section_map_(), attributes_section_data_(NULL),
2134 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2135 { }
2136
2137 // Whether we force PCI branch veneers.
2138 bool
should_force_pic_veneer() const2139 should_force_pic_veneer() const
2140 { return this->should_force_pic_veneer_; }
2141
2142 // Set PIC veneer flag.
2143 void
set_should_force_pic_veneer(bool value)2144 set_should_force_pic_veneer(bool value)
2145 { this->should_force_pic_veneer_ = value; }
2146
2147 // Whether we use THUMB-2 instructions.
2148 bool
using_thumb2() const2149 using_thumb2() const
2150 {
2151 Object_attribute* attr =
2152 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2153 int arch = attr->int_value();
2154 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2155 }
2156
2157 // Whether we use THUMB/THUMB-2 instructions only.
2158 bool
using_thumb_only() const2159 using_thumb_only() const
2160 {
2161 Object_attribute* attr =
2162 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2163
2164 if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2165 || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2166 return true;
2167 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2168 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2169 return false;
2170 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2171 return attr->int_value() == 'M';
2172 }
2173
2174 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2175 bool
may_use_arm_nop() const2176 may_use_arm_nop() const
2177 {
2178 Object_attribute* attr =
2179 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2180 int arch = attr->int_value();
2181 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2182 || arch == elfcpp::TAG_CPU_ARCH_V6K
2183 || arch == elfcpp::TAG_CPU_ARCH_V7
2184 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2185 }
2186
2187 // Whether we have THUMB-2 NOP.W instruction.
2188 bool
may_use_thumb2_nop() const2189 may_use_thumb2_nop() const
2190 {
2191 Object_attribute* attr =
2192 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2193 int arch = attr->int_value();
2194 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2195 || arch == elfcpp::TAG_CPU_ARCH_V7
2196 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2197 }
2198
2199 // Whether we have v4T interworking instructions available.
2200 bool
may_use_v4t_interworking() const2201 may_use_v4t_interworking() const
2202 {
2203 Object_attribute* attr =
2204 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2205 int arch = attr->int_value();
2206 return (arch != elfcpp::TAG_CPU_ARCH_PRE_V4
2207 && arch != elfcpp::TAG_CPU_ARCH_V4);
2208 }
2209
2210 // Whether we have v5T interworking instructions available.
2211 bool
may_use_v5t_interworking() const2212 may_use_v5t_interworking() const
2213 {
2214 Object_attribute* attr =
2215 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2216 int arch = attr->int_value();
2217 if (parameters->options().fix_arm1176())
2218 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2219 || arch == elfcpp::TAG_CPU_ARCH_V7
2220 || arch == elfcpp::TAG_CPU_ARCH_V6_M
2221 || arch == elfcpp::TAG_CPU_ARCH_V6S_M
2222 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2223 else
2224 return (arch != elfcpp::TAG_CPU_ARCH_PRE_V4
2225 && arch != elfcpp::TAG_CPU_ARCH_V4
2226 && arch != elfcpp::TAG_CPU_ARCH_V4T);
2227 }
2228
2229 // Process the relocations to determine unreferenced sections for
2230 // garbage collection.
2231 void
2232 gc_process_relocs(Symbol_table* symtab,
2233 Layout* layout,
2234 Sized_relobj_file<32, big_endian>* object,
2235 unsigned int data_shndx,
2236 unsigned int sh_type,
2237 const unsigned char* prelocs,
2238 size_t reloc_count,
2239 Output_section* output_section,
2240 bool needs_special_offset_handling,
2241 size_t local_symbol_count,
2242 const unsigned char* plocal_symbols);
2243
2244 // Scan the relocations to look for symbol adjustments.
2245 void
2246 scan_relocs(Symbol_table* symtab,
2247 Layout* layout,
2248 Sized_relobj_file<32, big_endian>* object,
2249 unsigned int data_shndx,
2250 unsigned int sh_type,
2251 const unsigned char* prelocs,
2252 size_t reloc_count,
2253 Output_section* output_section,
2254 bool needs_special_offset_handling,
2255 size_t local_symbol_count,
2256 const unsigned char* plocal_symbols);
2257
2258 // Finalize the sections.
2259 void
2260 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2261
2262 // Return the value to use for a dynamic symbol which requires special
2263 // treatment.
2264 uint64_t
2265 do_dynsym_value(const Symbol*) const;
2266
2267 // Return the plt address for globals. Since we have irelative plt entries,
2268 // address calculation is not as straightforward as plt_address + plt_offset.
2269 uint64_t
do_plt_address_for_global(const Symbol * gsym) const2270 do_plt_address_for_global(const Symbol* gsym) const
2271 { return this->plt_section()->address_for_global(gsym); }
2272
2273 // Return the plt address for locals. Since we have irelative plt entries,
2274 // address calculation is not as straightforward as plt_address + plt_offset.
2275 uint64_t
do_plt_address_for_local(const Relobj * relobj,unsigned int symndx) const2276 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2277 { return this->plt_section()->address_for_local(relobj, symndx); }
2278
2279 // Relocate a section.
2280 void
2281 relocate_section(const Relocate_info<32, big_endian>*,
2282 unsigned int sh_type,
2283 const unsigned char* prelocs,
2284 size_t reloc_count,
2285 Output_section* output_section,
2286 bool needs_special_offset_handling,
2287 unsigned char* view,
2288 Arm_address view_address,
2289 section_size_type view_size,
2290 const Reloc_symbol_changes*);
2291
2292 // Scan the relocs during a relocatable link.
2293 void
2294 scan_relocatable_relocs(Symbol_table* symtab,
2295 Layout* layout,
2296 Sized_relobj_file<32, big_endian>* object,
2297 unsigned int data_shndx,
2298 unsigned int sh_type,
2299 const unsigned char* prelocs,
2300 size_t reloc_count,
2301 Output_section* output_section,
2302 bool needs_special_offset_handling,
2303 size_t local_symbol_count,
2304 const unsigned char* plocal_symbols,
2305 Relocatable_relocs*);
2306
2307 // Emit relocations for a section.
2308 void
2309 relocate_relocs(const Relocate_info<32, big_endian>*,
2310 unsigned int sh_type,
2311 const unsigned char* prelocs,
2312 size_t reloc_count,
2313 Output_section* output_section,
2314 typename elfcpp::Elf_types<32>::Elf_Off
2315 offset_in_output_section,
2316 const Relocatable_relocs*,
2317 unsigned char* view,
2318 Arm_address view_address,
2319 section_size_type view_size,
2320 unsigned char* reloc_view,
2321 section_size_type reloc_view_size);
2322
2323 // Perform target-specific processing in a relocatable link. This is
2324 // only used if we use the relocation strategy RELOC_SPECIAL.
2325 void
2326 relocate_special_relocatable(const Relocate_info<32, big_endian>* relinfo,
2327 unsigned int sh_type,
2328 const unsigned char* preloc_in,
2329 size_t relnum,
2330 Output_section* output_section,
2331 typename elfcpp::Elf_types<32>::Elf_Off
2332 offset_in_output_section,
2333 unsigned char* view,
2334 typename elfcpp::Elf_types<32>::Elf_Addr
2335 view_address,
2336 section_size_type view_size,
2337 unsigned char* preloc_out);
2338
2339 // Return whether SYM is defined by the ABI.
2340 bool
do_is_defined_by_abi(const Symbol * sym) const2341 do_is_defined_by_abi(const Symbol* sym) const
2342 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2343
2344 // Return whether there is a GOT section.
2345 bool
has_got_section() const2346 has_got_section() const
2347 { return this->got_ != NULL; }
2348
2349 // Return the size of the GOT section.
2350 section_size_type
got_size() const2351 got_size() const
2352 {
2353 gold_assert(this->got_ != NULL);
2354 return this->got_->data_size();
2355 }
2356
2357 // Return the number of entries in the GOT.
2358 unsigned int
got_entry_count() const2359 got_entry_count() const
2360 {
2361 if (!this->has_got_section())
2362 return 0;
2363 return this->got_size() / 4;
2364 }
2365
2366 // Return the number of entries in the PLT.
2367 unsigned int
2368 plt_entry_count() const;
2369
2370 // Return the offset of the first non-reserved PLT entry.
2371 unsigned int
2372 first_plt_entry_offset() const;
2373
2374 // Return the size of each PLT entry.
2375 unsigned int
2376 plt_entry_size() const;
2377
2378 // Get the section to use for IRELATIVE relocations, create it if necessary.
2379 Reloc_section*
2380 rel_irelative_section(Layout*);
2381
2382 // Map platform-specific reloc types
2383 static unsigned int
2384 get_real_reloc_type(unsigned int r_type);
2385
2386 //
2387 // Methods to support stub-generations.
2388 //
2389
2390 // Return the stub factory
2391 const Stub_factory&
stub_factory() const2392 stub_factory() const
2393 { return this->stub_factory_; }
2394
2395 // Make a new Arm_input_section object.
2396 Arm_input_section<big_endian>*
2397 new_arm_input_section(Relobj*, unsigned int);
2398
2399 // Find the Arm_input_section object corresponding to the SHNDX-th input
2400 // section of RELOBJ.
2401 Arm_input_section<big_endian>*
2402 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2403
2404 // Make a new Stub_table
2405 Stub_table<big_endian>*
2406 new_stub_table(Arm_input_section<big_endian>*);
2407
2408 // Scan a section for stub generation.
2409 void
2410 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2411 const unsigned char*, size_t, Output_section*,
2412 bool, const unsigned char*, Arm_address,
2413 section_size_type);
2414
2415 // Relocate a stub.
2416 void
2417 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2418 Output_section*, unsigned char*, Arm_address,
2419 section_size_type);
2420
2421 // Get the default ARM target.
2422 static Target_arm<big_endian>*
default_target()2423 default_target()
2424 {
2425 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2426 && parameters->target().is_big_endian() == big_endian);
2427 return static_cast<Target_arm<big_endian>*>(
2428 parameters->sized_target<32, big_endian>());
2429 }
2430
2431 // Whether NAME belongs to a mapping symbol.
2432 static bool
is_mapping_symbol_name(const char * name)2433 is_mapping_symbol_name(const char* name)
2434 {
2435 return (name
2436 && name[0] == '$'
2437 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2438 && (name[2] == '\0' || name[2] == '.'));
2439 }
2440
2441 // Whether we work around the Cortex-A8 erratum.
2442 bool
fix_cortex_a8() const2443 fix_cortex_a8() const
2444 { return this->fix_cortex_a8_; }
2445
2446 // Whether we merge exidx entries in debuginfo.
2447 bool
merge_exidx_entries() const2448 merge_exidx_entries() const
2449 { return parameters->options().merge_exidx_entries(); }
2450
2451 // Whether we fix R_ARM_V4BX relocation.
2452 // 0 - do not fix
2453 // 1 - replace with MOV instruction (armv4 target)
2454 // 2 - make interworking veneer (>= armv4t targets only)
2455 General_options::Fix_v4bx
fix_v4bx() const2456 fix_v4bx() const
2457 { return parameters->options().fix_v4bx(); }
2458
2459 // Scan a span of THUMB code section for Cortex-A8 erratum.
2460 void
2461 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2462 section_size_type, section_size_type,
2463 const unsigned char*, Arm_address);
2464
2465 // Apply Cortex-A8 workaround to a branch.
2466 void
2467 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2468 unsigned char*, Arm_address);
2469
2470 protected:
2471 // Make the PLT-generator object.
2472 Output_data_plt_arm<big_endian>*
make_data_plt(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)2473 make_data_plt(Layout* layout,
2474 Arm_output_data_got<big_endian>* got,
2475 Output_data_space* got_plt,
2476 Output_data_space* got_irelative)
2477 { return this->do_make_data_plt(layout, got, got_plt, got_irelative); }
2478
2479 // Make an ELF object.
2480 Object*
2481 do_make_elf_object(const std::string&, Input_file*, off_t,
2482 const elfcpp::Ehdr<32, big_endian>& ehdr);
2483
2484 Object*
do_make_elf_object(const std::string &,Input_file *,off_t,const elfcpp::Ehdr<32,!big_endian> &)2485 do_make_elf_object(const std::string&, Input_file*, off_t,
2486 const elfcpp::Ehdr<32, !big_endian>&)
2487 { gold_unreachable(); }
2488
2489 Object*
do_make_elf_object(const std::string &,Input_file *,off_t,const elfcpp::Ehdr<64,false> &)2490 do_make_elf_object(const std::string&, Input_file*, off_t,
2491 const elfcpp::Ehdr<64, false>&)
2492 { gold_unreachable(); }
2493
2494 Object*
do_make_elf_object(const std::string &,Input_file *,off_t,const elfcpp::Ehdr<64,true> &)2495 do_make_elf_object(const std::string&, Input_file*, off_t,
2496 const elfcpp::Ehdr<64, true>&)
2497 { gold_unreachable(); }
2498
2499 // Make an output section.
2500 Output_section*
do_make_output_section(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags)2501 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2502 elfcpp::Elf_Xword flags)
2503 { return new Arm_output_section<big_endian>(name, type, flags); }
2504
2505 void
2506 do_adjust_elf_header(unsigned char* view, int len);
2507
2508 // We only need to generate stubs, and hence perform relaxation if we are
2509 // not doing relocatable linking.
2510 bool
do_may_relax() const2511 do_may_relax() const
2512 { return !parameters->options().relocatable(); }
2513
2514 bool
2515 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2516
2517 // Determine whether an object attribute tag takes an integer, a
2518 // string or both.
2519 int
2520 do_attribute_arg_type(int tag) const;
2521
2522 // Reorder tags during output.
2523 int
2524 do_attributes_order(int num) const;
2525
2526 // This is called when the target is selected as the default.
2527 void
do_select_as_default_target()2528 do_select_as_default_target()
2529 {
2530 // No locking is required since there should only be one default target.
2531 // We cannot have both the big-endian and little-endian ARM targets
2532 // as the default.
2533 gold_assert(arm_reloc_property_table == NULL);
2534 arm_reloc_property_table = new Arm_reloc_property_table();
2535 }
2536
2537 // Virtual function which is set to return true by a target if
2538 // it can use relocation types to determine if a function's
2539 // pointer is taken.
2540 virtual bool
do_can_check_for_function_pointers() const2541 do_can_check_for_function_pointers() const
2542 { return true; }
2543
2544 // Whether a section called SECTION_NAME may have function pointers to
2545 // sections not eligible for safe ICF folding.
2546 virtual bool
do_section_may_have_icf_unsafe_pointers(const char * section_name) const2547 do_section_may_have_icf_unsafe_pointers(const char* section_name) const
2548 {
2549 return (!is_prefix_of(".ARM.exidx", section_name)
2550 && !is_prefix_of(".ARM.extab", section_name)
2551 && Target::do_section_may_have_icf_unsafe_pointers(section_name));
2552 }
2553
2554 virtual void
2555 do_define_standard_symbols(Symbol_table*, Layout*);
2556
2557 virtual Output_data_plt_arm<big_endian>*
do_make_data_plt(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)2558 do_make_data_plt(Layout* layout,
2559 Arm_output_data_got<big_endian>* got,
2560 Output_data_space* got_plt,
2561 Output_data_space* got_irelative)
2562 {
2563 gold_assert(got_plt != NULL && got_irelative != NULL);
2564 if (parameters->options().long_plt())
2565 return new Output_data_plt_arm_long<big_endian>(
2566 layout, got, got_plt, got_irelative);
2567 else
2568 return new Output_data_plt_arm_short<big_endian>(
2569 layout, got, got_plt, got_irelative);
2570 }
2571
2572 private:
2573 // The class which scans relocations.
2574 class Scan
2575 {
2576 public:
Scan()2577 Scan()
2578 : issued_non_pic_error_(false)
2579 { }
2580
2581 static inline int
2582 get_reference_flags(unsigned int r_type);
2583
2584 inline void
2585 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2586 Sized_relobj_file<32, big_endian>* object,
2587 unsigned int data_shndx,
2588 Output_section* output_section,
2589 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2590 const elfcpp::Sym<32, big_endian>& lsym,
2591 bool is_discarded);
2592
2593 inline void
2594 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2595 Sized_relobj_file<32, big_endian>* object,
2596 unsigned int data_shndx,
2597 Output_section* output_section,
2598 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2599 Symbol* gsym);
2600
2601 inline bool
2602 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2603 Sized_relobj_file<32, big_endian>* ,
2604 unsigned int ,
2605 Output_section* ,
2606 const elfcpp::Rel<32, big_endian>& ,
2607 unsigned int ,
2608 const elfcpp::Sym<32, big_endian>&);
2609
2610 inline bool
2611 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2612 Sized_relobj_file<32, big_endian>* ,
2613 unsigned int ,
2614 Output_section* ,
2615 const elfcpp::Rel<32, big_endian>& ,
2616 unsigned int , Symbol*);
2617
2618 private:
2619 static void
2620 unsupported_reloc_local(Sized_relobj_file<32, big_endian>*,
2621 unsigned int r_type);
2622
2623 static void
2624 unsupported_reloc_global(Sized_relobj_file<32, big_endian>*,
2625 unsigned int r_type, Symbol*);
2626
2627 void
2628 check_non_pic(Relobj*, unsigned int r_type);
2629
2630 // Almost identical to Symbol::needs_plt_entry except that it also
2631 // handles STT_ARM_TFUNC.
2632 static bool
symbol_needs_plt_entry(const Symbol * sym)2633 symbol_needs_plt_entry(const Symbol* sym)
2634 {
2635 // An undefined symbol from an executable does not need a PLT entry.
2636 if (sym->is_undefined() && !parameters->options().shared())
2637 return false;
2638
2639 if (sym->type() == elfcpp::STT_GNU_IFUNC)
2640 return true;
2641
2642 return (!parameters->doing_static_link()
2643 && (sym->type() == elfcpp::STT_FUNC
2644 || sym->type() == elfcpp::STT_ARM_TFUNC)
2645 && (sym->is_from_dynobj()
2646 || sym->is_undefined()
2647 || sym->is_preemptible()));
2648 }
2649
2650 inline bool
2651 possible_function_pointer_reloc(unsigned int r_type);
2652
2653 // Whether a plt entry is needed for ifunc.
2654 bool
2655 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, big_endian>*,
2656 unsigned int r_type);
2657
2658 // Whether we have issued an error about a non-PIC compilation.
2659 bool issued_non_pic_error_;
2660 };
2661
2662 // The class which implements relocation.
2663 class Relocate
2664 {
2665 public:
Relocate()2666 Relocate()
2667 { }
2668
~Relocate()2669 ~Relocate()
2670 { }
2671
2672 // Return whether the static relocation needs to be applied.
2673 inline bool
2674 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2675 unsigned int r_type,
2676 bool is_32bit,
2677 Output_section* output_section);
2678
2679 // Do a relocation. Return false if the caller should not issue
2680 // any warnings about this relocation.
2681 inline bool
2682 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2683 Output_section*, size_t relnum,
2684 const elfcpp::Rel<32, big_endian>&,
2685 unsigned int r_type, const Sized_symbol<32>*,
2686 const Symbol_value<32>*,
2687 unsigned char*, Arm_address,
2688 section_size_type);
2689
2690 // Return whether we want to pass flag NON_PIC_REF for this
2691 // reloc. This means the relocation type accesses a symbol not via
2692 // GOT or PLT.
2693 static inline bool
reloc_is_non_pic(unsigned int r_type)2694 reloc_is_non_pic(unsigned int r_type)
2695 {
2696 switch (r_type)
2697 {
2698 // These relocation types reference GOT or PLT entries explicitly.
2699 case elfcpp::R_ARM_GOT_BREL:
2700 case elfcpp::R_ARM_GOT_ABS:
2701 case elfcpp::R_ARM_GOT_PREL:
2702 case elfcpp::R_ARM_GOT_BREL12:
2703 case elfcpp::R_ARM_PLT32_ABS:
2704 case elfcpp::R_ARM_TLS_GD32:
2705 case elfcpp::R_ARM_TLS_LDM32:
2706 case elfcpp::R_ARM_TLS_IE32:
2707 case elfcpp::R_ARM_TLS_IE12GP:
2708
2709 // These relocate types may use PLT entries.
2710 case elfcpp::R_ARM_CALL:
2711 case elfcpp::R_ARM_THM_CALL:
2712 case elfcpp::R_ARM_JUMP24:
2713 case elfcpp::R_ARM_THM_JUMP24:
2714 case elfcpp::R_ARM_THM_JUMP19:
2715 case elfcpp::R_ARM_PLT32:
2716 case elfcpp::R_ARM_THM_XPC22:
2717 case elfcpp::R_ARM_PREL31:
2718 case elfcpp::R_ARM_SBREL31:
2719 return false;
2720
2721 default:
2722 return true;
2723 }
2724 }
2725
2726 private:
2727 // Do a TLS relocation.
2728 inline typename Arm_relocate_functions<big_endian>::Status
2729 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2730 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2731 const Sized_symbol<32>*, const Symbol_value<32>*,
2732 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2733 section_size_type);
2734
2735 };
2736
2737 // A class which returns the size required for a relocation type,
2738 // used while scanning relocs during a relocatable link.
2739 class Relocatable_size_for_reloc
2740 {
2741 public:
2742 unsigned int
2743 get_size_for_reloc(unsigned int, Relobj*);
2744 };
2745
2746 // Adjust TLS relocation type based on the options and whether this
2747 // is a local symbol.
2748 static tls::Tls_optimization
2749 optimize_tls_reloc(bool is_final, int r_type);
2750
2751 // Get the GOT section, creating it if necessary.
2752 Arm_output_data_got<big_endian>*
2753 got_section(Symbol_table*, Layout*);
2754
2755 // Get the GOT PLT section.
2756 Output_data_space*
got_plt_section() const2757 got_plt_section() const
2758 {
2759 gold_assert(this->got_plt_ != NULL);
2760 return this->got_plt_;
2761 }
2762
2763 // Create the PLT section.
2764 void
2765 make_plt_section(Symbol_table* symtab, Layout* layout);
2766
2767 // Create a PLT entry for a global symbol.
2768 void
2769 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2770
2771 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
2772 void
2773 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
2774 Sized_relobj_file<32, big_endian>* relobj,
2775 unsigned int local_sym_index);
2776
2777 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2778 void
2779 define_tls_base_symbol(Symbol_table*, Layout*);
2780
2781 // Create a GOT entry for the TLS module index.
2782 unsigned int
2783 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2784 Sized_relobj_file<32, big_endian>* object);
2785
2786 // Get the PLT section.
2787 const Output_data_plt_arm<big_endian>*
plt_section() const2788 plt_section() const
2789 {
2790 gold_assert(this->plt_ != NULL);
2791 return this->plt_;
2792 }
2793
2794 // Get the dynamic reloc section, creating it if necessary.
2795 Reloc_section*
2796 rel_dyn_section(Layout*);
2797
2798 // Get the section to use for TLS_DESC relocations.
2799 Reloc_section*
2800 rel_tls_desc_section(Layout*) const;
2801
2802 // Return true if the symbol may need a COPY relocation.
2803 // References from an executable object to non-function symbols
2804 // defined in a dynamic object may need a COPY relocation.
2805 bool
may_need_copy_reloc(Symbol * gsym)2806 may_need_copy_reloc(Symbol* gsym)
2807 {
2808 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2809 && gsym->may_need_copy_reloc());
2810 }
2811
2812 // Add a potential copy relocation.
2813 void
copy_reloc(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * object,unsigned int shndx,Output_section * output_section,Symbol * sym,const elfcpp::Rel<32,big_endian> & reloc)2814 copy_reloc(Symbol_table* symtab, Layout* layout,
2815 Sized_relobj_file<32, big_endian>* object,
2816 unsigned int shndx, Output_section* output_section,
2817 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2818 {
2819 this->copy_relocs_.copy_reloc(symtab, layout,
2820 symtab->get_sized_symbol<32>(sym),
2821 object, shndx, output_section, reloc,
2822 this->rel_dyn_section(layout));
2823 }
2824
2825 // Whether two EABI versions are compatible.
2826 static bool
2827 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2828
2829 // Merge processor-specific flags from input object and those in the ELF
2830 // header of the output.
2831 void
2832 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2833
2834 // Get the secondary compatible architecture.
2835 static int
2836 get_secondary_compatible_arch(const Attributes_section_data*);
2837
2838 // Set the secondary compatible architecture.
2839 static void
2840 set_secondary_compatible_arch(Attributes_section_data*, int);
2841
2842 static int
2843 tag_cpu_arch_combine(const char*, int, int*, int, int);
2844
2845 // Helper to print AEABI enum tag value.
2846 static std::string
2847 aeabi_enum_name(unsigned int);
2848
2849 // Return string value for TAG_CPU_name.
2850 static std::string
2851 tag_cpu_name_value(unsigned int);
2852
2853 // Query attributes object to see if integer divide instructions may be
2854 // present in an object.
2855 static bool
2856 attributes_accept_div(int arch, int profile,
2857 const Object_attribute* div_attr);
2858
2859 // Query attributes object to see if integer divide instructions are
2860 // forbidden to be in the object. This is not the inverse of
2861 // attributes_accept_div.
2862 static bool
2863 attributes_forbid_div(const Object_attribute* div_attr);
2864
2865 // Merge object attributes from input object and those in the output.
2866 void
2867 merge_object_attributes(const char*, const Attributes_section_data*);
2868
2869 // Helper to get an AEABI object attribute
2870 Object_attribute*
get_aeabi_object_attribute(int tag) const2871 get_aeabi_object_attribute(int tag) const
2872 {
2873 Attributes_section_data* pasd = this->attributes_section_data_;
2874 gold_assert(pasd != NULL);
2875 Object_attribute* attr =
2876 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2877 gold_assert(attr != NULL);
2878 return attr;
2879 }
2880
2881 //
2882 // Methods to support stub-generations.
2883 //
2884
2885 // Group input sections for stub generation.
2886 void
2887 group_sections(Layout*, section_size_type, bool, const Task*);
2888
2889 // Scan a relocation for stub generation.
2890 void
2891 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2892 const Sized_symbol<32>*, unsigned int,
2893 const Symbol_value<32>*,
2894 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2895
2896 // Scan a relocation section for stub.
2897 template<int sh_type>
2898 void
2899 scan_reloc_section_for_stubs(
2900 const Relocate_info<32, big_endian>* relinfo,
2901 const unsigned char* prelocs,
2902 size_t reloc_count,
2903 Output_section* output_section,
2904 bool needs_special_offset_handling,
2905 const unsigned char* view,
2906 elfcpp::Elf_types<32>::Elf_Addr view_address,
2907 section_size_type);
2908
2909 // Fix .ARM.exidx section coverage.
2910 void
2911 fix_exidx_coverage(Layout*, const Input_objects*,
2912 Arm_output_section<big_endian>*, Symbol_table*,
2913 const Task*);
2914
2915 // Functors for STL set.
2916 struct output_section_address_less_than
2917 {
2918 bool
operator ()__anon49345ebd0111::Target_arm::output_section_address_less_than2919 operator()(const Output_section* s1, const Output_section* s2) const
2920 { return s1->address() < s2->address(); }
2921 };
2922
2923 // Information about this specific target which we pass to the
2924 // general Target structure.
2925 static const Target::Target_info arm_info;
2926
2927 // The types of GOT entries needed for this platform.
2928 // These values are exposed to the ABI in an incremental link.
2929 // Do not renumber existing values without changing the version
2930 // number of the .gnu_incremental_inputs section.
2931 enum Got_type
2932 {
2933 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2934 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2935 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2936 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2937 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2938 };
2939
2940 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2941
2942 // Map input section to Arm_input_section.
2943 typedef Unordered_map<Section_id,
2944 Arm_input_section<big_endian>*,
2945 Section_id_hash>
2946 Arm_input_section_map;
2947
2948 // Map output addresses to relocs for Cortex-A8 erratum.
2949 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2950 Cortex_a8_relocs_info;
2951
2952 // The GOT section.
2953 Arm_output_data_got<big_endian>* got_;
2954 // The PLT section.
2955 Output_data_plt_arm<big_endian>* plt_;
2956 // The GOT PLT section.
2957 Output_data_space* got_plt_;
2958 // The GOT section for IRELATIVE relocations.
2959 Output_data_space* got_irelative_;
2960 // The dynamic reloc section.
2961 Reloc_section* rel_dyn_;
2962 // The section to use for IRELATIVE relocs.
2963 Reloc_section* rel_irelative_;
2964 // Relocs saved to avoid a COPY reloc.
2965 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2966 // Offset of the GOT entry for the TLS module index.
2967 unsigned int got_mod_index_offset_;
2968 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2969 bool tls_base_symbol_defined_;
2970 // Vector of Stub_tables created.
2971 Stub_table_list stub_tables_;
2972 // Stub factory.
2973 const Stub_factory &stub_factory_;
2974 // Whether we force PIC branch veneers.
2975 bool should_force_pic_veneer_;
2976 // Map for locating Arm_input_sections.
2977 Arm_input_section_map arm_input_section_map_;
2978 // Attributes section data in output.
2979 Attributes_section_data* attributes_section_data_;
2980 // Whether we want to fix code for Cortex-A8 erratum.
2981 bool fix_cortex_a8_;
2982 // Map addresses to relocs for Cortex-A8 erratum.
2983 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2984 };
2985
2986 template<bool big_endian>
2987 const Target::Target_info Target_arm<big_endian>::arm_info =
2988 {
2989 32, // size
2990 big_endian, // is_big_endian
2991 elfcpp::EM_ARM, // machine_code
2992 false, // has_make_symbol
2993 false, // has_resolve
2994 false, // has_code_fill
2995 true, // is_default_stack_executable
2996 false, // can_icf_inline_merge_sections
2997 '\0', // wrap_char
2998 "/usr/lib/libc.so.1", // dynamic_linker
2999 0x8000, // default_text_segment_address
3000 0x1000, // abi_pagesize (overridable by -z max-page-size)
3001 0x1000, // common_pagesize (overridable by -z common-page-size)
3002 false, // isolate_execinstr
3003 0, // rosegment_gap
3004 elfcpp::SHN_UNDEF, // small_common_shndx
3005 elfcpp::SHN_UNDEF, // large_common_shndx
3006 0, // small_common_section_flags
3007 0, // large_common_section_flags
3008 ".ARM.attributes", // attributes_section
3009 "aeabi", // attributes_vendor
3010 "_start" // entry_symbol_name
3011 };
3012
3013 // Arm relocate functions class
3014 //
3015
3016 template<bool big_endian>
3017 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
3018 {
3019 public:
3020 typedef enum
3021 {
3022 STATUS_OKAY, // No error during relocation.
3023 STATUS_OVERFLOW, // Relocation overflow.
3024 STATUS_BAD_RELOC // Relocation cannot be applied.
3025 } Status;
3026
3027 private:
3028 typedef Relocate_functions<32, big_endian> Base;
3029 typedef Arm_relocate_functions<big_endian> This;
3030
3031 // Encoding of imm16 argument for movt and movw ARM instructions
3032 // from ARM ARM:
3033 //
3034 // imm16 := imm4 | imm12
3035 //
3036 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
3037 // +-------+---------------+-------+-------+-----------------------+
3038 // | | |imm4 | |imm12 |
3039 // +-------+---------------+-------+-------+-----------------------+
3040
3041 // Extract the relocation addend from VAL based on the ARM
3042 // instruction encoding described above.
3043 static inline typename elfcpp::Swap<32, big_endian>::Valtype
extract_arm_movw_movt_addend(typename elfcpp::Swap<32,big_endian>::Valtype val)3044 extract_arm_movw_movt_addend(
3045 typename elfcpp::Swap<32, big_endian>::Valtype val)
3046 {
3047 // According to the Elf ABI for ARM Architecture the immediate
3048 // field is sign-extended to form the addend.
3049 return Bits<16>::sign_extend32(((val >> 4) & 0xf000) | (val & 0xfff));
3050 }
3051
3052 // Insert X into VAL based on the ARM instruction encoding described
3053 // above.
3054 static inline typename elfcpp::Swap<32, big_endian>::Valtype
insert_val_arm_movw_movt(typename elfcpp::Swap<32,big_endian>::Valtype val,typename elfcpp::Swap<32,big_endian>::Valtype x)3055 insert_val_arm_movw_movt(
3056 typename elfcpp::Swap<32, big_endian>::Valtype val,
3057 typename elfcpp::Swap<32, big_endian>::Valtype x)
3058 {
3059 val &= 0xfff0f000;
3060 val |= x & 0x0fff;
3061 val |= (x & 0xf000) << 4;
3062 return val;
3063 }
3064
3065 // Encoding of imm16 argument for movt and movw Thumb2 instructions
3066 // from ARM ARM:
3067 //
3068 // imm16 := imm4 | i | imm3 | imm8
3069 //
3070 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
3071 // +---------+-+-----------+-------++-+-----+-------+---------------+
3072 // | |i| |imm4 || |imm3 | |imm8 |
3073 // +---------+-+-----------+-------++-+-----+-------+---------------+
3074
3075 // Extract the relocation addend from VAL based on the Thumb2
3076 // instruction encoding described above.
3077 static inline typename elfcpp::Swap<32, big_endian>::Valtype
extract_thumb_movw_movt_addend(typename elfcpp::Swap<32,big_endian>::Valtype val)3078 extract_thumb_movw_movt_addend(
3079 typename elfcpp::Swap<32, big_endian>::Valtype val)
3080 {
3081 // According to the Elf ABI for ARM Architecture the immediate
3082 // field is sign-extended to form the addend.
3083 return Bits<16>::sign_extend32(((val >> 4) & 0xf000)
3084 | ((val >> 15) & 0x0800)
3085 | ((val >> 4) & 0x0700)
3086 | (val & 0x00ff));
3087 }
3088
3089 // Insert X into VAL based on the Thumb2 instruction encoding
3090 // described above.
3091 static inline typename elfcpp::Swap<32, big_endian>::Valtype
insert_val_thumb_movw_movt(typename elfcpp::Swap<32,big_endian>::Valtype val,typename elfcpp::Swap<32,big_endian>::Valtype x)3092 insert_val_thumb_movw_movt(
3093 typename elfcpp::Swap<32, big_endian>::Valtype val,
3094 typename elfcpp::Swap<32, big_endian>::Valtype x)
3095 {
3096 val &= 0xfbf08f00;
3097 val |= (x & 0xf000) << 4;
3098 val |= (x & 0x0800) << 15;
3099 val |= (x & 0x0700) << 4;
3100 val |= (x & 0x00ff);
3101 return val;
3102 }
3103
3104 // Calculate the smallest constant Kn for the specified residual.
3105 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3106 static uint32_t
calc_grp_kn(typename elfcpp::Swap<32,big_endian>::Valtype residual)3107 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
3108 {
3109 int32_t msb;
3110
3111 if (residual == 0)
3112 return 0;
3113 // Determine the most significant bit in the residual and
3114 // align the resulting value to a 2-bit boundary.
3115 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
3116 ;
3117 // The desired shift is now (msb - 6), or zero, whichever
3118 // is the greater.
3119 return (((msb - 6) < 0) ? 0 : (msb - 6));
3120 }
3121
3122 // Calculate the final residual for the specified group index.
3123 // If the passed group index is less than zero, the method will return
3124 // the value of the specified residual without any change.
3125 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3126 static typename elfcpp::Swap<32, big_endian>::Valtype
calc_grp_residual(typename elfcpp::Swap<32,big_endian>::Valtype residual,const int group)3127 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3128 const int group)
3129 {
3130 for (int n = 0; n <= group; n++)
3131 {
3132 // Calculate which part of the value to mask.
3133 uint32_t shift = calc_grp_kn(residual);
3134 // Calculate the residual for the next time around.
3135 residual &= ~(residual & (0xff << shift));
3136 }
3137
3138 return residual;
3139 }
3140
3141 // Calculate the value of Gn for the specified group index.
3142 // We return it in the form of an encoded constant-and-rotation.
3143 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3144 static typename elfcpp::Swap<32, big_endian>::Valtype
calc_grp_gn(typename elfcpp::Swap<32,big_endian>::Valtype residual,const int group)3145 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3146 const int group)
3147 {
3148 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
3149 uint32_t shift = 0;
3150
3151 for (int n = 0; n <= group; n++)
3152 {
3153 // Calculate which part of the value to mask.
3154 shift = calc_grp_kn(residual);
3155 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
3156 gn = residual & (0xff << shift);
3157 // Calculate the residual for the next time around.
3158 residual &= ~gn;
3159 }
3160 // Return Gn in the form of an encoded constant-and-rotation.
3161 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
3162 }
3163
3164 public:
3165 // Handle ARM long branches.
3166 static typename This::Status
3167 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3168 unsigned char*, const Sized_symbol<32>*,
3169 const Arm_relobj<big_endian>*, unsigned int,
3170 const Symbol_value<32>*, Arm_address, Arm_address, bool);
3171
3172 // Handle THUMB long branches.
3173 static typename This::Status
3174 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3175 unsigned char*, const Sized_symbol<32>*,
3176 const Arm_relobj<big_endian>*, unsigned int,
3177 const Symbol_value<32>*, Arm_address, Arm_address, bool);
3178
3179
3180 // Return the branch offset of a 32-bit THUMB branch.
3181 static inline int32_t
thumb32_branch_offset(uint16_t upper_insn,uint16_t lower_insn)3182 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3183 {
3184 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
3185 // involving the J1 and J2 bits.
3186 uint32_t s = (upper_insn & (1U << 10)) >> 10;
3187 uint32_t upper = upper_insn & 0x3ffU;
3188 uint32_t lower = lower_insn & 0x7ffU;
3189 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
3190 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
3191 uint32_t i1 = j1 ^ s ? 0 : 1;
3192 uint32_t i2 = j2 ^ s ? 0 : 1;
3193
3194 return Bits<25>::sign_extend32((s << 24) | (i1 << 23) | (i2 << 22)
3195 | (upper << 12) | (lower << 1));
3196 }
3197
3198 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
3199 // UPPER_INSN is the original upper instruction of the branch. Caller is
3200 // responsible for overflow checking and BLX offset adjustment.
3201 static inline uint16_t
thumb32_branch_upper(uint16_t upper_insn,int32_t offset)3202 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
3203 {
3204 uint32_t s = offset < 0 ? 1 : 0;
3205 uint32_t bits = static_cast<uint32_t>(offset);
3206 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
3207 }
3208
3209 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
3210 // LOWER_INSN is the original lower instruction of the branch. Caller is
3211 // responsible for overflow checking and BLX offset adjustment.
3212 static inline uint16_t
thumb32_branch_lower(uint16_t lower_insn,int32_t offset)3213 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
3214 {
3215 uint32_t s = offset < 0 ? 1 : 0;
3216 uint32_t bits = static_cast<uint32_t>(offset);
3217 return ((lower_insn & ~0x2fffU)
3218 | ((((bits >> 23) & 1) ^ !s) << 13)
3219 | ((((bits >> 22) & 1) ^ !s) << 11)
3220 | ((bits >> 1) & 0x7ffU));
3221 }
3222
3223 // Return the branch offset of a 32-bit THUMB conditional branch.
3224 static inline int32_t
thumb32_cond_branch_offset(uint16_t upper_insn,uint16_t lower_insn)3225 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3226 {
3227 uint32_t s = (upper_insn & 0x0400U) >> 10;
3228 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
3229 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
3230 uint32_t lower = (lower_insn & 0x07ffU);
3231 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
3232
3233 return Bits<21>::sign_extend32((upper << 12) | (lower << 1));
3234 }
3235
3236 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
3237 // instruction. UPPER_INSN is the original upper instruction of the branch.
3238 // Caller is responsible for overflow checking.
3239 static inline uint16_t
thumb32_cond_branch_upper(uint16_t upper_insn,int32_t offset)3240 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
3241 {
3242 uint32_t s = offset < 0 ? 1 : 0;
3243 uint32_t bits = static_cast<uint32_t>(offset);
3244 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
3245 }
3246
3247 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
3248 // instruction. LOWER_INSN is the original lower instruction of the branch.
3249 // The caller is responsible for overflow checking.
3250 static inline uint16_t
thumb32_cond_branch_lower(uint16_t lower_insn,int32_t offset)3251 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
3252 {
3253 uint32_t bits = static_cast<uint32_t>(offset);
3254 uint32_t j2 = (bits & 0x00080000U) >> 19;
3255 uint32_t j1 = (bits & 0x00040000U) >> 18;
3256 uint32_t lo = (bits & 0x00000ffeU) >> 1;
3257
3258 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
3259 }
3260
3261 // R_ARM_ABS8: S + A
3262 static inline typename This::Status
abs8(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval)3263 abs8(unsigned char* view,
3264 const Sized_relobj_file<32, big_endian>* object,
3265 const Symbol_value<32>* psymval)
3266 {
3267 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3268 Valtype* wv = reinterpret_cast<Valtype*>(view);
3269 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3270 int32_t addend = Bits<8>::sign_extend32(val);
3271 Arm_address x = psymval->value(object, addend);
3272 val = Bits<32>::bit_select32(val, x, 0xffU);
3273 elfcpp::Swap<8, big_endian>::writeval(wv, val);
3274
3275 // R_ARM_ABS8 permits signed or unsigned results.
3276 return (Bits<8>::has_signed_unsigned_overflow32(x)
3277 ? This::STATUS_OVERFLOW
3278 : This::STATUS_OKAY);
3279 }
3280
3281 // R_ARM_THM_ABS5: S + A
3282 static inline typename This::Status
thm_abs5(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval)3283 thm_abs5(unsigned char* view,
3284 const Sized_relobj_file<32, big_endian>* object,
3285 const Symbol_value<32>* psymval)
3286 {
3287 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3288 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3289 Valtype* wv = reinterpret_cast<Valtype*>(view);
3290 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3291 Reltype addend = (val & 0x7e0U) >> 6;
3292 Reltype x = psymval->value(object, addend);
3293 val = Bits<32>::bit_select32(val, x << 6, 0x7e0U);
3294 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3295 return (Bits<5>::has_overflow32(x)
3296 ? This::STATUS_OVERFLOW
3297 : This::STATUS_OKAY);
3298 }
3299
3300 // R_ARM_ABS12: S + A
3301 static inline typename This::Status
abs12(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval)3302 abs12(unsigned char* view,
3303 const Sized_relobj_file<32, big_endian>* object,
3304 const Symbol_value<32>* psymval)
3305 {
3306 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3307 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3308 Valtype* wv = reinterpret_cast<Valtype*>(view);
3309 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3310 Reltype addend = val & 0x0fffU;
3311 Reltype x = psymval->value(object, addend);
3312 val = Bits<32>::bit_select32(val, x, 0x0fffU);
3313 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3314 return (Bits<12>::has_overflow32(x)
3315 ? This::STATUS_OVERFLOW
3316 : This::STATUS_OKAY);
3317 }
3318
3319 // R_ARM_ABS16: S + A
3320 static inline typename This::Status
abs16(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval)3321 abs16(unsigned char* view,
3322 const Sized_relobj_file<32, big_endian>* object,
3323 const Symbol_value<32>* psymval)
3324 {
3325 typedef typename elfcpp::Swap_unaligned<16, big_endian>::Valtype Valtype;
3326 Valtype val = elfcpp::Swap_unaligned<16, big_endian>::readval(view);
3327 int32_t addend = Bits<16>::sign_extend32(val);
3328 Arm_address x = psymval->value(object, addend);
3329 val = Bits<32>::bit_select32(val, x, 0xffffU);
3330 elfcpp::Swap_unaligned<16, big_endian>::writeval(view, val);
3331
3332 // R_ARM_ABS16 permits signed or unsigned results.
3333 return (Bits<16>::has_signed_unsigned_overflow32(x)
3334 ? This::STATUS_OVERFLOW
3335 : This::STATUS_OKAY);
3336 }
3337
3338 // R_ARM_ABS32: (S + A) | T
3339 static inline typename This::Status
abs32(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address thumb_bit)3340 abs32(unsigned char* view,
3341 const Sized_relobj_file<32, big_endian>* object,
3342 const Symbol_value<32>* psymval,
3343 Arm_address thumb_bit)
3344 {
3345 typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
3346 Valtype addend = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
3347 Valtype x = psymval->value(object, addend) | thumb_bit;
3348 elfcpp::Swap_unaligned<32, big_endian>::writeval(view, x);
3349 return This::STATUS_OKAY;
3350 }
3351
3352 // R_ARM_REL32: (S + A) | T - P
3353 static inline typename This::Status
rel32(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit)3354 rel32(unsigned char* view,
3355 const Sized_relobj_file<32, big_endian>* object,
3356 const Symbol_value<32>* psymval,
3357 Arm_address address,
3358 Arm_address thumb_bit)
3359 {
3360 typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
3361 Valtype addend = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
3362 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3363 elfcpp::Swap_unaligned<32, big_endian>::writeval(view, x);
3364 return This::STATUS_OKAY;
3365 }
3366
3367 // R_ARM_THM_JUMP24: (S + A) | T - P
3368 static typename This::Status
3369 thm_jump19(unsigned char* view, const Arm_relobj<big_endian>* object,
3370 const Symbol_value<32>* psymval, Arm_address address,
3371 Arm_address thumb_bit);
3372
3373 // R_ARM_THM_JUMP6: S + A – P
3374 static inline typename This::Status
thm_jump6(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address)3375 thm_jump6(unsigned char* view,
3376 const Sized_relobj_file<32, big_endian>* object,
3377 const Symbol_value<32>* psymval,
3378 Arm_address address)
3379 {
3380 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3381 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3382 Valtype* wv = reinterpret_cast<Valtype*>(view);
3383 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3384 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3385 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3386 Reltype x = (psymval->value(object, addend) - address);
3387 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3388 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3389 // CZB does only forward jumps.
3390 return ((x > 0x007e)
3391 ? This::STATUS_OVERFLOW
3392 : This::STATUS_OKAY);
3393 }
3394
3395 // R_ARM_THM_JUMP8: S + A – P
3396 static inline typename This::Status
thm_jump8(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address)3397 thm_jump8(unsigned char* view,
3398 const Sized_relobj_file<32, big_endian>* object,
3399 const Symbol_value<32>* psymval,
3400 Arm_address address)
3401 {
3402 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3403 Valtype* wv = reinterpret_cast<Valtype*>(view);
3404 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3405 int32_t addend = Bits<8>::sign_extend32((val & 0x00ff) << 1);
3406 int32_t x = (psymval->value(object, addend) - address);
3407 elfcpp::Swap<16, big_endian>::writeval(wv, ((val & 0xff00)
3408 | ((x & 0x01fe) >> 1)));
3409 // We do a 9-bit overflow check because x is right-shifted by 1 bit.
3410 return (Bits<9>::has_overflow32(x)
3411 ? This::STATUS_OVERFLOW
3412 : This::STATUS_OKAY);
3413 }
3414
3415 // R_ARM_THM_JUMP11: S + A – P
3416 static inline typename This::Status
thm_jump11(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address)3417 thm_jump11(unsigned char* view,
3418 const Sized_relobj_file<32, big_endian>* object,
3419 const Symbol_value<32>* psymval,
3420 Arm_address address)
3421 {
3422 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3423 Valtype* wv = reinterpret_cast<Valtype*>(view);
3424 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3425 int32_t addend = Bits<11>::sign_extend32((val & 0x07ff) << 1);
3426 int32_t x = (psymval->value(object, addend) - address);
3427 elfcpp::Swap<16, big_endian>::writeval(wv, ((val & 0xf800)
3428 | ((x & 0x0ffe) >> 1)));
3429 // We do a 12-bit overflow check because x is right-shifted by 1 bit.
3430 return (Bits<12>::has_overflow32(x)
3431 ? This::STATUS_OVERFLOW
3432 : This::STATUS_OKAY);
3433 }
3434
3435 // R_ARM_BASE_PREL: B(S) + A - P
3436 static inline typename This::Status
base_prel(unsigned char * view,Arm_address origin,Arm_address address)3437 base_prel(unsigned char* view,
3438 Arm_address origin,
3439 Arm_address address)
3440 {
3441 Base::rel32(view, origin - address);
3442 return STATUS_OKAY;
3443 }
3444
3445 // R_ARM_BASE_ABS: B(S) + A
3446 static inline typename This::Status
base_abs(unsigned char * view,Arm_address origin)3447 base_abs(unsigned char* view,
3448 Arm_address origin)
3449 {
3450 Base::rel32(view, origin);
3451 return STATUS_OKAY;
3452 }
3453
3454 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3455 static inline typename This::Status
got_brel(unsigned char * view,typename elfcpp::Swap<32,big_endian>::Valtype got_offset)3456 got_brel(unsigned char* view,
3457 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3458 {
3459 Base::rel32(view, got_offset);
3460 return This::STATUS_OKAY;
3461 }
3462
3463 // R_ARM_GOT_PREL: GOT(S) + A - P
3464 static inline typename This::Status
got_prel(unsigned char * view,Arm_address got_entry,Arm_address address)3465 got_prel(unsigned char* view,
3466 Arm_address got_entry,
3467 Arm_address address)
3468 {
3469 Base::rel32(view, got_entry - address);
3470 return This::STATUS_OKAY;
3471 }
3472
3473 // R_ARM_PREL: (S + A) | T - P
3474 static inline typename This::Status
prel31(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit)3475 prel31(unsigned char* view,
3476 const Sized_relobj_file<32, big_endian>* object,
3477 const Symbol_value<32>* psymval,
3478 Arm_address address,
3479 Arm_address thumb_bit)
3480 {
3481 typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
3482 Valtype val = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
3483 Valtype addend = Bits<31>::sign_extend32(val);
3484 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3485 val = Bits<32>::bit_select32(val, x, 0x7fffffffU);
3486 elfcpp::Swap_unaligned<32, big_endian>::writeval(view, val);
3487 return (Bits<31>::has_overflow32(x)
3488 ? This::STATUS_OVERFLOW
3489 : This::STATUS_OKAY);
3490 }
3491
3492 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3493 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3494 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3495 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3496 static inline typename This::Status
movw(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address relative_address_base,Arm_address thumb_bit,bool check_overflow)3497 movw(unsigned char* view,
3498 const Sized_relobj_file<32, big_endian>* object,
3499 const Symbol_value<32>* psymval,
3500 Arm_address relative_address_base,
3501 Arm_address thumb_bit,
3502 bool check_overflow)
3503 {
3504 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3505 Valtype* wv = reinterpret_cast<Valtype*>(view);
3506 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3507 Valtype addend = This::extract_arm_movw_movt_addend(val);
3508 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3509 - relative_address_base);
3510 val = This::insert_val_arm_movw_movt(val, x);
3511 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3512 return ((check_overflow && Bits<16>::has_overflow32(x))
3513 ? This::STATUS_OVERFLOW
3514 : This::STATUS_OKAY);
3515 }
3516
3517 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3518 // R_ARM_MOVT_PREL: S + A - P
3519 // R_ARM_MOVT_BREL: S + A - B(S)
3520 static inline typename This::Status
movt(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address relative_address_base)3521 movt(unsigned char* view,
3522 const Sized_relobj_file<32, big_endian>* object,
3523 const Symbol_value<32>* psymval,
3524 Arm_address relative_address_base)
3525 {
3526 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3527 Valtype* wv = reinterpret_cast<Valtype*>(view);
3528 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3529 Valtype addend = This::extract_arm_movw_movt_addend(val);
3530 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3531 val = This::insert_val_arm_movw_movt(val, x);
3532 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3533 // FIXME: IHI0044D says that we should check for overflow.
3534 return This::STATUS_OKAY;
3535 }
3536
3537 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3538 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3539 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3540 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3541 static inline typename This::Status
thm_movw(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address relative_address_base,Arm_address thumb_bit,bool check_overflow)3542 thm_movw(unsigned char* view,
3543 const Sized_relobj_file<32, big_endian>* object,
3544 const Symbol_value<32>* psymval,
3545 Arm_address relative_address_base,
3546 Arm_address thumb_bit,
3547 bool check_overflow)
3548 {
3549 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3550 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3551 Valtype* wv = reinterpret_cast<Valtype*>(view);
3552 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3553 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3554 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3555 Reltype x =
3556 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3557 val = This::insert_val_thumb_movw_movt(val, x);
3558 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3559 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3560 return ((check_overflow && Bits<16>::has_overflow32(x))
3561 ? This::STATUS_OVERFLOW
3562 : This::STATUS_OKAY);
3563 }
3564
3565 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3566 // R_ARM_THM_MOVT_PREL: S + A - P
3567 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3568 static inline typename This::Status
thm_movt(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address relative_address_base)3569 thm_movt(unsigned char* view,
3570 const Sized_relobj_file<32, big_endian>* object,
3571 const Symbol_value<32>* psymval,
3572 Arm_address relative_address_base)
3573 {
3574 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3575 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3576 Valtype* wv = reinterpret_cast<Valtype*>(view);
3577 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3578 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3579 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3580 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3581 val = This::insert_val_thumb_movw_movt(val, x);
3582 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3583 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3584 return This::STATUS_OKAY;
3585 }
3586
3587 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3588 static inline typename This::Status
thm_alu11(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit)3589 thm_alu11(unsigned char* view,
3590 const Sized_relobj_file<32, big_endian>* object,
3591 const Symbol_value<32>* psymval,
3592 Arm_address address,
3593 Arm_address thumb_bit)
3594 {
3595 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3596 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3597 Valtype* wv = reinterpret_cast<Valtype*>(view);
3598 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3599 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3600
3601 // f e d c b|a|9|8 7 6 5|4|3 2 1 0||f|e d c|b a 9 8|7 6 5 4 3 2 1 0
3602 // -----------------------------------------------------------------------
3603 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3604 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3605 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3606 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3607 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3608 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3609
3610 // Determine a sign for the addend.
3611 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3612 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3613 // Thumb2 addend encoding:
3614 // imm12 := i | imm3 | imm8
3615 int32_t addend = (insn & 0xff)
3616 | ((insn & 0x00007000) >> 4)
3617 | ((insn & 0x04000000) >> 15);
3618 // Apply a sign to the added.
3619 addend *= sign;
3620
3621 int32_t x = (psymval->value(object, addend) | thumb_bit)
3622 - (address & 0xfffffffc);
3623 Reltype val = abs(x);
3624 // Mask out the value and a distinct part of the ADD/SUB opcode
3625 // (bits 7:5 of opword).
3626 insn = (insn & 0xfb0f8f00)
3627 | (val & 0xff)
3628 | ((val & 0x700) << 4)
3629 | ((val & 0x800) << 15);
3630 // Set the opcode according to whether the value to go in the
3631 // place is negative.
3632 if (x < 0)
3633 insn |= 0x00a00000;
3634
3635 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3636 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3637 return ((val > 0xfff) ?
3638 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3639 }
3640
3641 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3642 static inline typename This::Status
thm_pc8(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address)3643 thm_pc8(unsigned char* view,
3644 const Sized_relobj_file<32, big_endian>* object,
3645 const Symbol_value<32>* psymval,
3646 Arm_address address)
3647 {
3648 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3649 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3650 Valtype* wv = reinterpret_cast<Valtype*>(view);
3651 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3652 Reltype addend = ((insn & 0x00ff) << 2);
3653 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3654 Reltype val = abs(x);
3655 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3656
3657 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3658 return ((val > 0x03fc)
3659 ? This::STATUS_OVERFLOW
3660 : This::STATUS_OKAY);
3661 }
3662
3663 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3664 static inline typename This::Status
thm_pc12(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,Arm_address address)3665 thm_pc12(unsigned char* view,
3666 const Sized_relobj_file<32, big_endian>* object,
3667 const Symbol_value<32>* psymval,
3668 Arm_address address)
3669 {
3670 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3671 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3672 Valtype* wv = reinterpret_cast<Valtype*>(view);
3673 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3674 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3675 // Determine a sign for the addend (positive if the U bit is 1).
3676 const int sign = (insn & 0x00800000) ? 1 : -1;
3677 int32_t addend = (insn & 0xfff);
3678 // Apply a sign to the added.
3679 addend *= sign;
3680
3681 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3682 Reltype val = abs(x);
3683 // Mask out and apply the value and the U bit.
3684 insn = (insn & 0xff7ff000) | (val & 0xfff);
3685 // Set the U bit according to whether the value to go in the
3686 // place is positive.
3687 if (x >= 0)
3688 insn |= 0x00800000;
3689
3690 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3691 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3692 return ((val > 0xfff) ?
3693 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3694 }
3695
3696 // R_ARM_V4BX
3697 static inline typename This::Status
v4bx(const Relocate_info<32,big_endian> * relinfo,unsigned char * view,const Arm_relobj<big_endian> * object,const Arm_address address,const bool is_interworking)3698 v4bx(const Relocate_info<32, big_endian>* relinfo,
3699 unsigned char* view,
3700 const Arm_relobj<big_endian>* object,
3701 const Arm_address address,
3702 const bool is_interworking)
3703 {
3704
3705 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3706 Valtype* wv = reinterpret_cast<Valtype*>(view);
3707 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3708
3709 // Ensure that we have a BX instruction.
3710 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3711 const uint32_t reg = (val & 0xf);
3712 if (is_interworking && reg != 0xf)
3713 {
3714 Stub_table<big_endian>* stub_table =
3715 object->stub_table(relinfo->data_shndx);
3716 gold_assert(stub_table != NULL);
3717
3718 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3719 gold_assert(stub != NULL);
3720
3721 int32_t veneer_address =
3722 stub_table->address() + stub->offset() - 8 - address;
3723 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3724 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3725 // Replace with a branch to veneer (B <addr>)
3726 val = (val & 0xf0000000) | 0x0a000000
3727 | ((veneer_address >> 2) & 0x00ffffff);
3728 }
3729 else
3730 {
3731 // Preserve Rm (lowest four bits) and the condition code
3732 // (highest four bits). Other bits encode MOV PC,Rm.
3733 val = (val & 0xf000000f) | 0x01a0f000;
3734 }
3735 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3736 return This::STATUS_OKAY;
3737 }
3738
3739 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3740 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3741 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3742 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3743 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3744 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3745 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3746 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3747 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3748 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3749 static inline typename This::Status
arm_grp_alu(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,const int group,Arm_address address,Arm_address thumb_bit,bool check_overflow)3750 arm_grp_alu(unsigned char* view,
3751 const Sized_relobj_file<32, big_endian>* object,
3752 const Symbol_value<32>* psymval,
3753 const int group,
3754 Arm_address address,
3755 Arm_address thumb_bit,
3756 bool check_overflow)
3757 {
3758 gold_assert(group >= 0 && group < 3);
3759 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3760 Valtype* wv = reinterpret_cast<Valtype*>(view);
3761 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3762
3763 // ALU group relocations are allowed only for the ADD/SUB instructions.
3764 // (0x00800000 - ADD, 0x00400000 - SUB)
3765 const Valtype opcode = insn & 0x01e00000;
3766 if (opcode != 0x00800000 && opcode != 0x00400000)
3767 return This::STATUS_BAD_RELOC;
3768
3769 // Determine a sign for the addend.
3770 const int sign = (opcode == 0x00800000) ? 1 : -1;
3771 // shifter = rotate_imm * 2
3772 const uint32_t shifter = (insn & 0xf00) >> 7;
3773 // Initial addend value.
3774 int32_t addend = insn & 0xff;
3775 // Rotate addend right by shifter.
3776 addend = (addend >> shifter) | (addend << (32 - shifter));
3777 // Apply a sign to the added.
3778 addend *= sign;
3779
3780 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3781 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3782 // Check for overflow if required
3783 if (check_overflow
3784 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3785 return This::STATUS_OVERFLOW;
3786
3787 // Mask out the value and the ADD/SUB part of the opcode; take care
3788 // not to destroy the S bit.
3789 insn &= 0xff1ff000;
3790 // Set the opcode according to whether the value to go in the
3791 // place is negative.
3792 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3793 // Encode the offset (encoded Gn).
3794 insn |= gn;
3795
3796 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3797 return This::STATUS_OKAY;
3798 }
3799
3800 // R_ARM_LDR_PC_G0: S + A - P
3801 // R_ARM_LDR_PC_G1: S + A - P
3802 // R_ARM_LDR_PC_G2: S + A - P
3803 // R_ARM_LDR_SB_G0: S + A - B(S)
3804 // R_ARM_LDR_SB_G1: S + A - B(S)
3805 // R_ARM_LDR_SB_G2: S + A - B(S)
3806 static inline typename This::Status
arm_grp_ldr(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,const int group,Arm_address address)3807 arm_grp_ldr(unsigned char* view,
3808 const Sized_relobj_file<32, big_endian>* object,
3809 const Symbol_value<32>* psymval,
3810 const int group,
3811 Arm_address address)
3812 {
3813 gold_assert(group >= 0 && group < 3);
3814 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3815 Valtype* wv = reinterpret_cast<Valtype*>(view);
3816 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3817
3818 const int sign = (insn & 0x00800000) ? 1 : -1;
3819 int32_t addend = (insn & 0xfff) * sign;
3820 int32_t x = (psymval->value(object, addend) - address);
3821 // Calculate the relevant G(n-1) value to obtain this stage residual.
3822 Valtype residual =
3823 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3824 if (residual >= 0x1000)
3825 return This::STATUS_OVERFLOW;
3826
3827 // Mask out the value and U bit.
3828 insn &= 0xff7ff000;
3829 // Set the U bit for non-negative values.
3830 if (x >= 0)
3831 insn |= 0x00800000;
3832 insn |= residual;
3833
3834 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3835 return This::STATUS_OKAY;
3836 }
3837
3838 // R_ARM_LDRS_PC_G0: S + A - P
3839 // R_ARM_LDRS_PC_G1: S + A - P
3840 // R_ARM_LDRS_PC_G2: S + A - P
3841 // R_ARM_LDRS_SB_G0: S + A - B(S)
3842 // R_ARM_LDRS_SB_G1: S + A - B(S)
3843 // R_ARM_LDRS_SB_G2: S + A - B(S)
3844 static inline typename This::Status
arm_grp_ldrs(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,const int group,Arm_address address)3845 arm_grp_ldrs(unsigned char* view,
3846 const Sized_relobj_file<32, big_endian>* object,
3847 const Symbol_value<32>* psymval,
3848 const int group,
3849 Arm_address address)
3850 {
3851 gold_assert(group >= 0 && group < 3);
3852 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3853 Valtype* wv = reinterpret_cast<Valtype*>(view);
3854 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3855
3856 const int sign = (insn & 0x00800000) ? 1 : -1;
3857 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3858 int32_t x = (psymval->value(object, addend) - address);
3859 // Calculate the relevant G(n-1) value to obtain this stage residual.
3860 Valtype residual =
3861 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3862 if (residual >= 0x100)
3863 return This::STATUS_OVERFLOW;
3864
3865 // Mask out the value and U bit.
3866 insn &= 0xff7ff0f0;
3867 // Set the U bit for non-negative values.
3868 if (x >= 0)
3869 insn |= 0x00800000;
3870 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3871
3872 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3873 return This::STATUS_OKAY;
3874 }
3875
3876 // R_ARM_LDC_PC_G0: S + A - P
3877 // R_ARM_LDC_PC_G1: S + A - P
3878 // R_ARM_LDC_PC_G2: S + A - P
3879 // R_ARM_LDC_SB_G0: S + A - B(S)
3880 // R_ARM_LDC_SB_G1: S + A - B(S)
3881 // R_ARM_LDC_SB_G2: S + A - B(S)
3882 static inline typename This::Status
arm_grp_ldc(unsigned char * view,const Sized_relobj_file<32,big_endian> * object,const Symbol_value<32> * psymval,const int group,Arm_address address)3883 arm_grp_ldc(unsigned char* view,
3884 const Sized_relobj_file<32, big_endian>* object,
3885 const Symbol_value<32>* psymval,
3886 const int group,
3887 Arm_address address)
3888 {
3889 gold_assert(group >= 0 && group < 3);
3890 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3891 Valtype* wv = reinterpret_cast<Valtype*>(view);
3892 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3893
3894 const int sign = (insn & 0x00800000) ? 1 : -1;
3895 int32_t addend = ((insn & 0xff) << 2) * sign;
3896 int32_t x = (psymval->value(object, addend) - address);
3897 // Calculate the relevant G(n-1) value to obtain this stage residual.
3898 Valtype residual =
3899 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3900 if ((residual & 0x3) != 0 || residual >= 0x400)
3901 return This::STATUS_OVERFLOW;
3902
3903 // Mask out the value and U bit.
3904 insn &= 0xff7fff00;
3905 // Set the U bit for non-negative values.
3906 if (x >= 0)
3907 insn |= 0x00800000;
3908 insn |= (residual >> 2);
3909
3910 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3911 return This::STATUS_OKAY;
3912 }
3913 };
3914
3915 // Relocate ARM long branches. This handles relocation types
3916 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3917 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3918 // undefined and we do not use PLT in this relocation. In such a case,
3919 // the branch is converted into an NOP.
3920
3921 template<bool big_endian>
3922 typename Arm_relocate_functions<big_endian>::Status
arm_branch_common(unsigned int r_type,const Relocate_info<32,big_endian> * relinfo,unsigned char * view,const Sized_symbol<32> * gsym,const Arm_relobj<big_endian> * object,unsigned int r_sym,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit,bool is_weakly_undefined_without_plt)3923 Arm_relocate_functions<big_endian>::arm_branch_common(
3924 unsigned int r_type,
3925 const Relocate_info<32, big_endian>* relinfo,
3926 unsigned char* view,
3927 const Sized_symbol<32>* gsym,
3928 const Arm_relobj<big_endian>* object,
3929 unsigned int r_sym,
3930 const Symbol_value<32>* psymval,
3931 Arm_address address,
3932 Arm_address thumb_bit,
3933 bool is_weakly_undefined_without_plt)
3934 {
3935 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3936 Valtype* wv = reinterpret_cast<Valtype*>(view);
3937 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3938
3939 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3940 && ((val & 0x0f000000UL) == 0x0a000000UL);
3941 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3942 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3943 && ((val & 0x0f000000UL) == 0x0b000000UL);
3944 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3945 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3946
3947 // Check that the instruction is valid.
3948 if (r_type == elfcpp::R_ARM_CALL)
3949 {
3950 if (!insn_is_uncond_bl && !insn_is_blx)
3951 return This::STATUS_BAD_RELOC;
3952 }
3953 else if (r_type == elfcpp::R_ARM_JUMP24)
3954 {
3955 if (!insn_is_b && !insn_is_cond_bl)
3956 return This::STATUS_BAD_RELOC;
3957 }
3958 else if (r_type == elfcpp::R_ARM_PLT32)
3959 {
3960 if (!insn_is_any_branch)
3961 return This::STATUS_BAD_RELOC;
3962 }
3963 else if (r_type == elfcpp::R_ARM_XPC25)
3964 {
3965 // FIXME: AAELF document IH0044C does not say much about it other
3966 // than it being obsolete.
3967 if (!insn_is_any_branch)
3968 return This::STATUS_BAD_RELOC;
3969 }
3970 else
3971 gold_unreachable();
3972
3973 // A branch to an undefined weak symbol is turned into a jump to
3974 // the next instruction unless a PLT entry will be created.
3975 // Do the same for local undefined symbols.
3976 // The jump to the next instruction is optimized as a NOP depending
3977 // on the architecture.
3978 const Target_arm<big_endian>* arm_target =
3979 Target_arm<big_endian>::default_target();
3980 if (is_weakly_undefined_without_plt)
3981 {
3982 gold_assert(!parameters->options().relocatable());
3983 Valtype cond = val & 0xf0000000U;
3984 if (arm_target->may_use_arm_nop())
3985 val = cond | 0x0320f000;
3986 else
3987 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3988 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3989 return This::STATUS_OKAY;
3990 }
3991
3992 Valtype addend = Bits<26>::sign_extend32(val << 2);
3993 Valtype branch_target = psymval->value(object, addend);
3994 int32_t branch_offset = branch_target - address;
3995
3996 // We need a stub if the branch offset is too large or if we need
3997 // to switch mode.
3998 bool may_use_blx = arm_target->may_use_v5t_interworking();
3999 Reloc_stub* stub = NULL;
4000
4001 if (!parameters->options().relocatable()
4002 && (Bits<26>::has_overflow32(branch_offset)
4003 || ((thumb_bit != 0)
4004 && !(may_use_blx && r_type == elfcpp::R_ARM_CALL))))
4005 {
4006 Valtype unadjusted_branch_target = psymval->value(object, 0);
4007
4008 Stub_type stub_type =
4009 Reloc_stub::stub_type_for_reloc(r_type, address,
4010 unadjusted_branch_target,
4011 (thumb_bit != 0));
4012 if (stub_type != arm_stub_none)
4013 {
4014 Stub_table<big_endian>* stub_table =
4015 object->stub_table(relinfo->data_shndx);
4016 gold_assert(stub_table != NULL);
4017
4018 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4019 stub = stub_table->find_reloc_stub(stub_key);
4020 gold_assert(stub != NULL);
4021 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
4022 branch_target = stub_table->address() + stub->offset() + addend;
4023 branch_offset = branch_target - address;
4024 gold_assert(!Bits<26>::has_overflow32(branch_offset));
4025 }
4026 }
4027
4028 // At this point, if we still need to switch mode, the instruction
4029 // must either be a BLX or a BL that can be converted to a BLX.
4030 if (thumb_bit != 0)
4031 {
4032 // Turn BL to BLX.
4033 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
4034 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
4035 }
4036
4037 val = Bits<32>::bit_select32(val, (branch_offset >> 2), 0xffffffUL);
4038 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4039 return (Bits<26>::has_overflow32(branch_offset)
4040 ? This::STATUS_OVERFLOW
4041 : This::STATUS_OKAY);
4042 }
4043
4044 // Relocate THUMB long branches. This handles relocation types
4045 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
4046 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
4047 // undefined and we do not use PLT in this relocation. In such a case,
4048 // the branch is converted into an NOP.
4049
4050 template<bool big_endian>
4051 typename Arm_relocate_functions<big_endian>::Status
thumb_branch_common(unsigned int r_type,const Relocate_info<32,big_endian> * relinfo,unsigned char * view,const Sized_symbol<32> * gsym,const Arm_relobj<big_endian> * object,unsigned int r_sym,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit,bool is_weakly_undefined_without_plt)4052 Arm_relocate_functions<big_endian>::thumb_branch_common(
4053 unsigned int r_type,
4054 const Relocate_info<32, big_endian>* relinfo,
4055 unsigned char* view,
4056 const Sized_symbol<32>* gsym,
4057 const Arm_relobj<big_endian>* object,
4058 unsigned int r_sym,
4059 const Symbol_value<32>* psymval,
4060 Arm_address address,
4061 Arm_address thumb_bit,
4062 bool is_weakly_undefined_without_plt)
4063 {
4064 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4065 Valtype* wv = reinterpret_cast<Valtype*>(view);
4066 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4067 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4068
4069 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
4070 // into account.
4071 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
4072 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
4073
4074 // Check that the instruction is valid.
4075 if (r_type == elfcpp::R_ARM_THM_CALL)
4076 {
4077 if (!is_bl_insn && !is_blx_insn)
4078 return This::STATUS_BAD_RELOC;
4079 }
4080 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
4081 {
4082 // This cannot be a BLX.
4083 if (!is_bl_insn)
4084 return This::STATUS_BAD_RELOC;
4085 }
4086 else if (r_type == elfcpp::R_ARM_THM_XPC22)
4087 {
4088 // Check for Thumb to Thumb call.
4089 if (!is_blx_insn)
4090 return This::STATUS_BAD_RELOC;
4091 if (thumb_bit != 0)
4092 {
4093 gold_warning(_("%s: Thumb BLX instruction targets "
4094 "thumb function '%s'."),
4095 object->name().c_str(),
4096 (gsym ? gsym->name() : "(local)"));
4097 // Convert BLX to BL.
4098 lower_insn |= 0x1000U;
4099 }
4100 }
4101 else
4102 gold_unreachable();
4103
4104 // A branch to an undefined weak symbol is turned into a jump to
4105 // the next instruction unless a PLT entry will be created.
4106 // The jump to the next instruction is optimized as a NOP.W for
4107 // Thumb-2 enabled architectures.
4108 const Target_arm<big_endian>* arm_target =
4109 Target_arm<big_endian>::default_target();
4110 if (is_weakly_undefined_without_plt)
4111 {
4112 gold_assert(!parameters->options().relocatable());
4113 if (arm_target->may_use_thumb2_nop())
4114 {
4115 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
4116 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
4117 }
4118 else
4119 {
4120 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
4121 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
4122 }
4123 return This::STATUS_OKAY;
4124 }
4125
4126 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
4127 Arm_address branch_target = psymval->value(object, addend);
4128
4129 // For BLX, bit 1 of target address comes from bit 1 of base address.
4130 bool may_use_blx = arm_target->may_use_v5t_interworking();
4131 if (thumb_bit == 0 && may_use_blx)
4132 branch_target = Bits<32>::bit_select32(branch_target, address, 0x2);
4133
4134 int32_t branch_offset = branch_target - address;
4135
4136 // We need a stub if the branch offset is too large or if we need
4137 // to switch mode.
4138 bool thumb2 = arm_target->using_thumb2();
4139 if (!parameters->options().relocatable()
4140 && ((!thumb2 && Bits<23>::has_overflow32(branch_offset))
4141 || (thumb2 && Bits<25>::has_overflow32(branch_offset))
4142 || ((thumb_bit == 0)
4143 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4144 || r_type == elfcpp::R_ARM_THM_JUMP24))))
4145 {
4146 Arm_address unadjusted_branch_target = psymval->value(object, 0);
4147
4148 Stub_type stub_type =
4149 Reloc_stub::stub_type_for_reloc(r_type, address,
4150 unadjusted_branch_target,
4151 (thumb_bit != 0));
4152
4153 if (stub_type != arm_stub_none)
4154 {
4155 Stub_table<big_endian>* stub_table =
4156 object->stub_table(relinfo->data_shndx);
4157 gold_assert(stub_table != NULL);
4158
4159 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4160 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4161 gold_assert(stub != NULL);
4162 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
4163 branch_target = stub_table->address() + stub->offset() + addend;
4164 if (thumb_bit == 0 && may_use_blx)
4165 branch_target = Bits<32>::bit_select32(branch_target, address, 0x2);
4166 branch_offset = branch_target - address;
4167 }
4168 }
4169
4170 // At this point, if we still need to switch mode, the instruction
4171 // must either be a BLX or a BL that can be converted to a BLX.
4172 if (thumb_bit == 0)
4173 {
4174 gold_assert(may_use_blx
4175 && (r_type == elfcpp::R_ARM_THM_CALL
4176 || r_type == elfcpp::R_ARM_THM_XPC22));
4177 // Make sure this is a BLX.
4178 lower_insn &= ~0x1000U;
4179 }
4180 else
4181 {
4182 // Make sure this is a BL.
4183 lower_insn |= 0x1000U;
4184 }
4185
4186 // For a BLX instruction, make sure that the relocation is rounded up
4187 // to a word boundary. This follows the semantics of the instruction
4188 // which specifies that bit 1 of the target address will come from bit
4189 // 1 of the base address.
4190 if ((lower_insn & 0x5000U) == 0x4000U)
4191 gold_assert((branch_offset & 3) == 0);
4192
4193 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
4194 // We use the Thumb-2 encoding, which is safe even if dealing with
4195 // a Thumb-1 instruction by virtue of our overflow check above. */
4196 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
4197 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
4198
4199 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4200 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4201
4202 gold_assert(!Bits<25>::has_overflow32(branch_offset));
4203
4204 return ((thumb2
4205 ? Bits<25>::has_overflow32(branch_offset)
4206 : Bits<23>::has_overflow32(branch_offset))
4207 ? This::STATUS_OVERFLOW
4208 : This::STATUS_OKAY);
4209 }
4210
4211 // Relocate THUMB-2 long conditional branches.
4212 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
4213 // undefined and we do not use PLT in this relocation. In such a case,
4214 // the branch is converted into an NOP.
4215
4216 template<bool big_endian>
4217 typename Arm_relocate_functions<big_endian>::Status
thm_jump19(unsigned char * view,const Arm_relobj<big_endian> * object,const Symbol_value<32> * psymval,Arm_address address,Arm_address thumb_bit)4218 Arm_relocate_functions<big_endian>::thm_jump19(
4219 unsigned char* view,
4220 const Arm_relobj<big_endian>* object,
4221 const Symbol_value<32>* psymval,
4222 Arm_address address,
4223 Arm_address thumb_bit)
4224 {
4225 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4226 Valtype* wv = reinterpret_cast<Valtype*>(view);
4227 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4228 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4229 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
4230
4231 Arm_address branch_target = psymval->value(object, addend);
4232 int32_t branch_offset = branch_target - address;
4233
4234 // ??? Should handle interworking? GCC might someday try to
4235 // use this for tail calls.
4236 // FIXME: We do support thumb entry to PLT yet.
4237 if (thumb_bit == 0)
4238 {
4239 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
4240 return This::STATUS_BAD_RELOC;
4241 }
4242
4243 // Put RELOCATION back into the insn.
4244 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
4245 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
4246
4247 // Put the relocated value back in the object file:
4248 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4249 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4250
4251 return (Bits<21>::has_overflow32(branch_offset)
4252 ? This::STATUS_OVERFLOW
4253 : This::STATUS_OKAY);
4254 }
4255
4256 // Get the GOT section, creating it if necessary.
4257
4258 template<bool big_endian>
4259 Arm_output_data_got<big_endian>*
got_section(Symbol_table * symtab,Layout * layout)4260 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
4261 {
4262 if (this->got_ == NULL)
4263 {
4264 gold_assert(symtab != NULL && layout != NULL);
4265
4266 // When using -z now, we can treat .got as a relro section.
4267 // Without -z now, it is modified after program startup by lazy
4268 // PLT relocations.
4269 bool is_got_relro = parameters->options().now();
4270 Output_section_order got_order = (is_got_relro
4271 ? ORDER_RELRO_LAST
4272 : ORDER_DATA);
4273
4274 // Unlike some targets (.e.g x86), ARM does not use separate .got and
4275 // .got.plt sections in output. The output .got section contains both
4276 // PLT and non-PLT GOT entries.
4277 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4278
4279 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4280 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
4281 this->got_, got_order, is_got_relro);
4282
4283 // The old GNU linker creates a .got.plt section. We just
4284 // create another set of data in the .got section. Note that we
4285 // always create a PLT if we create a GOT, although the PLT
4286 // might be empty.
4287 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4288 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4289 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
4290 this->got_plt_, got_order, is_got_relro);
4291
4292 // The first three entries are reserved.
4293 this->got_plt_->set_current_data_size(3 * 4);
4294
4295 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4296 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4297 Symbol_table::PREDEFINED,
4298 this->got_plt_,
4299 0, 0, elfcpp::STT_OBJECT,
4300 elfcpp::STB_LOCAL,
4301 elfcpp::STV_HIDDEN, 0,
4302 false, false);
4303
4304 // If there are any IRELATIVE relocations, they get GOT entries
4305 // in .got.plt after the jump slot entries.
4306 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
4307 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4308 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
4309 this->got_irelative_,
4310 got_order, is_got_relro);
4311
4312 }
4313 return this->got_;
4314 }
4315
4316 // Get the dynamic reloc section, creating it if necessary.
4317
4318 template<bool big_endian>
4319 typename Target_arm<big_endian>::Reloc_section*
rel_dyn_section(Layout * layout)4320 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4321 {
4322 if (this->rel_dyn_ == NULL)
4323 {
4324 gold_assert(layout != NULL);
4325 // Create both relocation sections in the same place, so as to ensure
4326 // their relative order in the output section.
4327 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4328 this->rel_irelative_ = new Reloc_section(false);
4329 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4330 elfcpp::SHF_ALLOC, this->rel_dyn_,
4331 ORDER_DYNAMIC_RELOCS, false);
4332 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4333 elfcpp::SHF_ALLOC, this->rel_irelative_,
4334 ORDER_DYNAMIC_RELOCS, false);
4335 }
4336 return this->rel_dyn_;
4337 }
4338
4339
4340 // Get the section to use for IRELATIVE relocs, creating it if necessary. These
4341 // go in .rela.dyn, but only after all other dynamic relocations. They need to
4342 // follow the other dynamic relocations so that they can refer to global
4343 // variables initialized by those relocs.
4344
4345 template<bool big_endian>
4346 typename Target_arm<big_endian>::Reloc_section*
rel_irelative_section(Layout * layout)4347 Target_arm<big_endian>::rel_irelative_section(Layout* layout)
4348 {
4349 if (this->rel_irelative_ == NULL)
4350 {
4351 // Delegate the creation to rel_dyn_section so as to ensure their order in
4352 // the output section.
4353 this->rel_dyn_section(layout);
4354 gold_assert(this->rel_irelative_ != NULL
4355 && (this->rel_dyn_->output_section()
4356 == this->rel_irelative_->output_section()));
4357 }
4358 return this->rel_irelative_;
4359 }
4360
4361
4362 // Insn_template methods.
4363
4364 // Return byte size of an instruction template.
4365
4366 size_t
size() const4367 Insn_template::size() const
4368 {
4369 switch (this->type())
4370 {
4371 case THUMB16_TYPE:
4372 case THUMB16_SPECIAL_TYPE:
4373 return 2;
4374 case ARM_TYPE:
4375 case THUMB32_TYPE:
4376 case DATA_TYPE:
4377 return 4;
4378 default:
4379 gold_unreachable();
4380 }
4381 }
4382
4383 // Return alignment of an instruction template.
4384
4385 unsigned
alignment() const4386 Insn_template::alignment() const
4387 {
4388 switch (this->type())
4389 {
4390 case THUMB16_TYPE:
4391 case THUMB16_SPECIAL_TYPE:
4392 case THUMB32_TYPE:
4393 return 2;
4394 case ARM_TYPE:
4395 case DATA_TYPE:
4396 return 4;
4397 default:
4398 gold_unreachable();
4399 }
4400 }
4401
4402 // Stub_template methods.
4403
Stub_template(Stub_type type,const Insn_template * insns,size_t insn_count)4404 Stub_template::Stub_template(
4405 Stub_type type, const Insn_template* insns,
4406 size_t insn_count)
4407 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4408 entry_in_thumb_mode_(false), relocs_()
4409 {
4410 off_t offset = 0;
4411
4412 // Compute byte size and alignment of stub template.
4413 for (size_t i = 0; i < insn_count; i++)
4414 {
4415 unsigned insn_alignment = insns[i].alignment();
4416 size_t insn_size = insns[i].size();
4417 gold_assert((offset & (insn_alignment - 1)) == 0);
4418 this->alignment_ = std::max(this->alignment_, insn_alignment);
4419 switch (insns[i].type())
4420 {
4421 case Insn_template::THUMB16_TYPE:
4422 case Insn_template::THUMB16_SPECIAL_TYPE:
4423 if (i == 0)
4424 this->entry_in_thumb_mode_ = true;
4425 break;
4426
4427 case Insn_template::THUMB32_TYPE:
4428 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4429 this->relocs_.push_back(Reloc(i, offset));
4430 if (i == 0)
4431 this->entry_in_thumb_mode_ = true;
4432 break;
4433
4434 case Insn_template::ARM_TYPE:
4435 // Handle cases where the target is encoded within the
4436 // instruction.
4437 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4438 this->relocs_.push_back(Reloc(i, offset));
4439 break;
4440
4441 case Insn_template::DATA_TYPE:
4442 // Entry point cannot be data.
4443 gold_assert(i != 0);
4444 this->relocs_.push_back(Reloc(i, offset));
4445 break;
4446
4447 default:
4448 gold_unreachable();
4449 }
4450 offset += insn_size;
4451 }
4452 this->size_ = offset;
4453 }
4454
4455 // Stub methods.
4456
4457 // Template to implement do_write for a specific target endianness.
4458
4459 template<bool big_endian>
4460 void inline
do_fixed_endian_write(unsigned char * view,section_size_type view_size)4461 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4462 {
4463 const Stub_template* stub_template = this->stub_template();
4464 const Insn_template* insns = stub_template->insns();
4465
4466 // FIXME: We do not handle BE8 encoding yet.
4467 unsigned char* pov = view;
4468 for (size_t i = 0; i < stub_template->insn_count(); i++)
4469 {
4470 switch (insns[i].type())
4471 {
4472 case Insn_template::THUMB16_TYPE:
4473 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4474 break;
4475 case Insn_template::THUMB16_SPECIAL_TYPE:
4476 elfcpp::Swap<16, big_endian>::writeval(
4477 pov,
4478 this->thumb16_special(i));
4479 break;
4480 case Insn_template::THUMB32_TYPE:
4481 {
4482 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4483 uint32_t lo = insns[i].data() & 0xffff;
4484 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4485 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4486 }
4487 break;
4488 case Insn_template::ARM_TYPE:
4489 case Insn_template::DATA_TYPE:
4490 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4491 break;
4492 default:
4493 gold_unreachable();
4494 }
4495 pov += insns[i].size();
4496 }
4497 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4498 }
4499
4500 // Reloc_stub::Key methods.
4501
4502 // Dump a Key as a string for debugging.
4503
4504 std::string
name() const4505 Reloc_stub::Key::name() const
4506 {
4507 if (this->r_sym_ == invalid_index)
4508 {
4509 // Global symbol key name
4510 // <stub-type>:<symbol name>:<addend>.
4511 const std::string sym_name = this->u_.symbol->name();
4512 // We need to print two hex number and two colons. So just add 100 bytes
4513 // to the symbol name size.
4514 size_t len = sym_name.size() + 100;
4515 char* buffer = new char[len];
4516 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4517 sym_name.c_str(), this->addend_);
4518 gold_assert(c > 0 && c < static_cast<int>(len));
4519 delete[] buffer;
4520 return std::string(buffer);
4521 }
4522 else
4523 {
4524 // local symbol key name
4525 // <stub-type>:<object>:<r_sym>:<addend>.
4526 const size_t len = 200;
4527 char buffer[len];
4528 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4529 this->u_.relobj, this->r_sym_, this->addend_);
4530 gold_assert(c > 0 && c < static_cast<int>(len));
4531 return std::string(buffer);
4532 }
4533 }
4534
4535 // Reloc_stub methods.
4536
4537 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4538 // LOCATION to DESTINATION.
4539 // This code is based on the arm_type_of_stub function in
4540 // bfd/elf32-arm.c. We have changed the interface a little to keep the Stub
4541 // class simple.
4542
4543 Stub_type
stub_type_for_reloc(unsigned int r_type,Arm_address location,Arm_address destination,bool target_is_thumb)4544 Reloc_stub::stub_type_for_reloc(
4545 unsigned int r_type,
4546 Arm_address location,
4547 Arm_address destination,
4548 bool target_is_thumb)
4549 {
4550 Stub_type stub_type = arm_stub_none;
4551
4552 // This is a bit ugly but we want to avoid using a templated class for
4553 // big and little endianities.
4554 bool may_use_blx;
4555 bool should_force_pic_veneer = parameters->options().pic_veneer();
4556 bool thumb2;
4557 bool thumb_only;
4558 if (parameters->target().is_big_endian())
4559 {
4560 const Target_arm<true>* big_endian_target =
4561 Target_arm<true>::default_target();
4562 may_use_blx = big_endian_target->may_use_v5t_interworking();
4563 should_force_pic_veneer |= big_endian_target->should_force_pic_veneer();
4564 thumb2 = big_endian_target->using_thumb2();
4565 thumb_only = big_endian_target->using_thumb_only();
4566 }
4567 else
4568 {
4569 const Target_arm<false>* little_endian_target =
4570 Target_arm<false>::default_target();
4571 may_use_blx = little_endian_target->may_use_v5t_interworking();
4572 should_force_pic_veneer |=
4573 little_endian_target->should_force_pic_veneer();
4574 thumb2 = little_endian_target->using_thumb2();
4575 thumb_only = little_endian_target->using_thumb_only();
4576 }
4577
4578 int64_t branch_offset;
4579 bool output_is_position_independent =
4580 parameters->options().output_is_position_independent();
4581 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4582 {
4583 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4584 // base address (instruction address + 4).
4585 if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4586 destination = Bits<32>::bit_select32(destination, location, 0x2);
4587 branch_offset = static_cast<int64_t>(destination) - location;
4588
4589 // Handle cases where:
4590 // - this call goes too far (different Thumb/Thumb2 max
4591 // distance)
4592 // - it's a Thumb->Arm call and blx is not available, or it's a
4593 // Thumb->Arm branch (not bl). A stub is needed in this case.
4594 if ((!thumb2
4595 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4596 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4597 || (thumb2
4598 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4599 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4600 || ((!target_is_thumb)
4601 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4602 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4603 {
4604 if (target_is_thumb)
4605 {
4606 // Thumb to thumb.
4607 if (!thumb_only)
4608 {
4609 stub_type = (output_is_position_independent
4610 || should_force_pic_veneer)
4611 // PIC stubs.
4612 ? ((may_use_blx
4613 && (r_type == elfcpp::R_ARM_THM_CALL))
4614 // V5T and above. Stub starts with ARM code, so
4615 // we must be able to switch mode before
4616 // reaching it, which is only possible for 'bl'
4617 // (ie R_ARM_THM_CALL relocation).
4618 ? arm_stub_long_branch_any_thumb_pic
4619 // On V4T, use Thumb code only.
4620 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4621
4622 // non-PIC stubs.
4623 : ((may_use_blx
4624 && (r_type == elfcpp::R_ARM_THM_CALL))
4625 ? arm_stub_long_branch_any_any // V5T and above.
4626 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4627 }
4628 else
4629 {
4630 stub_type = (output_is_position_independent
4631 || should_force_pic_veneer)
4632 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4633 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4634 }
4635 }
4636 else
4637 {
4638 // Thumb to arm.
4639
4640 // FIXME: We should check that the input section is from an
4641 // object that has interwork enabled.
4642
4643 stub_type = (output_is_position_independent
4644 || should_force_pic_veneer)
4645 // PIC stubs.
4646 ? ((may_use_blx
4647 && (r_type == elfcpp::R_ARM_THM_CALL))
4648 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4649 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4650
4651 // non-PIC stubs.
4652 : ((may_use_blx
4653 && (r_type == elfcpp::R_ARM_THM_CALL))
4654 ? arm_stub_long_branch_any_any // V5T and above.
4655 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4656
4657 // Handle v4t short branches.
4658 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4659 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4660 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4661 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4662 }
4663 }
4664 }
4665 else if (r_type == elfcpp::R_ARM_CALL
4666 || r_type == elfcpp::R_ARM_JUMP24
4667 || r_type == elfcpp::R_ARM_PLT32)
4668 {
4669 branch_offset = static_cast<int64_t>(destination) - location;
4670 if (target_is_thumb)
4671 {
4672 // Arm to thumb.
4673
4674 // FIXME: We should check that the input section is from an
4675 // object that has interwork enabled.
4676
4677 // We have an extra 2-bytes reach because of
4678 // the mode change (bit 24 (H) of BLX encoding).
4679 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4680 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4681 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4682 || (r_type == elfcpp::R_ARM_JUMP24)
4683 || (r_type == elfcpp::R_ARM_PLT32))
4684 {
4685 stub_type = (output_is_position_independent
4686 || should_force_pic_veneer)
4687 // PIC stubs.
4688 ? (may_use_blx
4689 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4690 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4691
4692 // non-PIC stubs.
4693 : (may_use_blx
4694 ? arm_stub_long_branch_any_any // V5T and above.
4695 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4696 }
4697 }
4698 else
4699 {
4700 // Arm to arm.
4701 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4702 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4703 {
4704 stub_type = (output_is_position_independent
4705 || should_force_pic_veneer)
4706 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4707 : arm_stub_long_branch_any_any; /// non-PIC.
4708 }
4709 }
4710 }
4711
4712 return stub_type;
4713 }
4714
4715 // Cortex_a8_stub methods.
4716
4717 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4718 // I is the position of the instruction template in the stub template.
4719
4720 uint16_t
do_thumb16_special(size_t i)4721 Cortex_a8_stub::do_thumb16_special(size_t i)
4722 {
4723 // The only use of this is to copy condition code from a conditional
4724 // branch being worked around to the corresponding conditional branch in
4725 // to the stub.
4726 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4727 && i == 0);
4728 uint16_t data = this->stub_template()->insns()[i].data();
4729 gold_assert((data & 0xff00U) == 0xd000U);
4730 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4731 return data;
4732 }
4733
4734 // Stub_factory methods.
4735
Stub_factory()4736 Stub_factory::Stub_factory()
4737 {
4738 // The instruction template sequences are declared as static
4739 // objects and initialized first time the constructor runs.
4740
4741 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4742 // to reach the stub if necessary.
4743 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4744 {
4745 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4746 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4747 // dcd R_ARM_ABS32(X)
4748 };
4749
4750 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4751 // available.
4752 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4753 {
4754 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4755 Insn_template::arm_insn(0xe12fff1c), // bx ip
4756 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4757 // dcd R_ARM_ABS32(X)
4758 };
4759
4760 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4761 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4762 {
4763 Insn_template::thumb16_insn(0xb401), // push {r0}
4764 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4765 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4766 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4767 Insn_template::thumb16_insn(0x4760), // bx ip
4768 Insn_template::thumb16_insn(0xbf00), // nop
4769 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4770 // dcd R_ARM_ABS32(X)
4771 };
4772
4773 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4774 // allowed.
4775 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4776 {
4777 Insn_template::thumb16_insn(0x4778), // bx pc
4778 Insn_template::thumb16_insn(0x46c0), // nop
4779 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4780 Insn_template::arm_insn(0xe12fff1c), // bx ip
4781 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4782 // dcd R_ARM_ABS32(X)
4783 };
4784
4785 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4786 // available.
4787 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4788 {
4789 Insn_template::thumb16_insn(0x4778), // bx pc
4790 Insn_template::thumb16_insn(0x46c0), // nop
4791 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4792 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4793 // dcd R_ARM_ABS32(X)
4794 };
4795
4796 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4797 // one, when the destination is close enough.
4798 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4799 {
4800 Insn_template::thumb16_insn(0x4778), // bx pc
4801 Insn_template::thumb16_insn(0x46c0), // nop
4802 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4803 };
4804
4805 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4806 // blx to reach the stub if necessary.
4807 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4808 {
4809 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4810 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4811 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4812 // dcd R_ARM_REL32(X-4)
4813 };
4814
4815 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4816 // blx to reach the stub if necessary. We can not add into pc;
4817 // it is not guaranteed to mode switch (different in ARMv6 and
4818 // ARMv7).
4819 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4820 {
4821 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4822 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4823 Insn_template::arm_insn(0xe12fff1c), // bx ip
4824 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4825 // dcd R_ARM_REL32(X)
4826 };
4827
4828 // V4T ARM -> ARM long branch stub, PIC.
4829 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4830 {
4831 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4832 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4833 Insn_template::arm_insn(0xe12fff1c), // bx ip
4834 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4835 // dcd R_ARM_REL32(X)
4836 };
4837
4838 // V4T Thumb -> ARM long branch stub, PIC.
4839 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4840 {
4841 Insn_template::thumb16_insn(0x4778), // bx pc
4842 Insn_template::thumb16_insn(0x46c0), // nop
4843 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4844 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4845 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4846 // dcd R_ARM_REL32(X)
4847 };
4848
4849 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4850 // architectures.
4851 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4852 {
4853 Insn_template::thumb16_insn(0xb401), // push {r0}
4854 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4855 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4856 Insn_template::thumb16_insn(0x4484), // add ip, r0
4857 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4858 Insn_template::thumb16_insn(0x4760), // bx ip
4859 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4860 // dcd R_ARM_REL32(X)
4861 };
4862
4863 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4864 // allowed.
4865 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4866 {
4867 Insn_template::thumb16_insn(0x4778), // bx pc
4868 Insn_template::thumb16_insn(0x46c0), // nop
4869 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4870 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4871 Insn_template::arm_insn(0xe12fff1c), // bx ip
4872 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4873 // dcd R_ARM_REL32(X)
4874 };
4875
4876 // Cortex-A8 erratum-workaround stubs.
4877
4878 // Stub used for conditional branches (which may be beyond +/-1MB away,
4879 // so we can't use a conditional branch to reach this stub).
4880
4881 // original code:
4882 //
4883 // b<cond> X
4884 // after:
4885 //
4886 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4887 {
4888 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4889 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4890 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4891 // b.w X
4892 };
4893
4894 // Stub used for b.w and bl.w instructions.
4895
4896 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4897 {
4898 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4899 };
4900
4901 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4902 {
4903 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4904 };
4905
4906 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4907 // instruction (which switches to ARM mode) to point to this stub. Jump to
4908 // the real destination using an ARM-mode branch.
4909 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4910 {
4911 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4912 };
4913
4914 // Stub used to provide an interworking for R_ARM_V4BX relocation
4915 // (bx r[n] instruction).
4916 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4917 {
4918 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4919 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4920 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4921 };
4922
4923 // Fill in the stub template look-up table. Stub templates are constructed
4924 // per instance of Stub_factory for fast look-up without locking
4925 // in a thread-enabled environment.
4926
4927 this->stub_templates_[arm_stub_none] =
4928 new Stub_template(arm_stub_none, NULL, 0);
4929
4930 #define DEF_STUB(x) \
4931 do \
4932 { \
4933 size_t array_size \
4934 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4935 Stub_type type = arm_stub_##x; \
4936 this->stub_templates_[type] = \
4937 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4938 } \
4939 while (0);
4940
4941 DEF_STUBS
4942 #undef DEF_STUB
4943 }
4944
4945 // Stub_table methods.
4946
4947 // Remove all Cortex-A8 stub.
4948
4949 template<bool big_endian>
4950 void
remove_all_cortex_a8_stubs()4951 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4952 {
4953 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4954 p != this->cortex_a8_stubs_.end();
4955 ++p)
4956 delete p->second;
4957 this->cortex_a8_stubs_.clear();
4958 }
4959
4960 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4961
4962 template<bool big_endian>
4963 void
relocate_stub(Stub * stub,const Relocate_info<32,big_endian> * relinfo,Target_arm<big_endian> * arm_target,Output_section * output_section,unsigned char * view,Arm_address address,section_size_type view_size)4964 Stub_table<big_endian>::relocate_stub(
4965 Stub* stub,
4966 const Relocate_info<32, big_endian>* relinfo,
4967 Target_arm<big_endian>* arm_target,
4968 Output_section* output_section,
4969 unsigned char* view,
4970 Arm_address address,
4971 section_size_type view_size)
4972 {
4973 const Stub_template* stub_template = stub->stub_template();
4974 if (stub_template->reloc_count() != 0)
4975 {
4976 // Adjust view to cover the stub only.
4977 section_size_type offset = stub->offset();
4978 section_size_type stub_size = stub_template->size();
4979 gold_assert(offset + stub_size <= view_size);
4980
4981 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4982 address + offset, stub_size);
4983 }
4984 }
4985
4986 // Relocate all stubs in this stub table.
4987
4988 template<bool big_endian>
4989 void
relocate_stubs(const Relocate_info<32,big_endian> * relinfo,Target_arm<big_endian> * arm_target,Output_section * output_section,unsigned char * view,Arm_address address,section_size_type view_size)4990 Stub_table<big_endian>::relocate_stubs(
4991 const Relocate_info<32, big_endian>* relinfo,
4992 Target_arm<big_endian>* arm_target,
4993 Output_section* output_section,
4994 unsigned char* view,
4995 Arm_address address,
4996 section_size_type view_size)
4997 {
4998 // If we are passed a view bigger than the stub table's. we need to
4999 // adjust the view.
5000 gold_assert(address == this->address()
5001 && (view_size
5002 == static_cast<section_size_type>(this->data_size())));
5003
5004 // Relocate all relocation stubs.
5005 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
5006 p != this->reloc_stubs_.end();
5007 ++p)
5008 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
5009 address, view_size);
5010
5011 // Relocate all Cortex-A8 stubs.
5012 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
5013 p != this->cortex_a8_stubs_.end();
5014 ++p)
5015 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
5016 address, view_size);
5017
5018 // Relocate all ARM V4BX stubs.
5019 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
5020 p != this->arm_v4bx_stubs_.end();
5021 ++p)
5022 {
5023 if (*p != NULL)
5024 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
5025 address, view_size);
5026 }
5027 }
5028
5029 // Write out the stubs to file.
5030
5031 template<bool big_endian>
5032 void
do_write(Output_file * of)5033 Stub_table<big_endian>::do_write(Output_file* of)
5034 {
5035 off_t offset = this->offset();
5036 const section_size_type oview_size =
5037 convert_to_section_size_type(this->data_size());
5038 unsigned char* const oview = of->get_output_view(offset, oview_size);
5039
5040 // Write relocation stubs.
5041 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
5042 p != this->reloc_stubs_.end();
5043 ++p)
5044 {
5045 Reloc_stub* stub = p->second;
5046 Arm_address address = this->address() + stub->offset();
5047 gold_assert(address
5048 == align_address(address,
5049 stub->stub_template()->alignment()));
5050 stub->write(oview + stub->offset(), stub->stub_template()->size(),
5051 big_endian);
5052 }
5053
5054 // Write Cortex-A8 stubs.
5055 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
5056 p != this->cortex_a8_stubs_.end();
5057 ++p)
5058 {
5059 Cortex_a8_stub* stub = p->second;
5060 Arm_address address = this->address() + stub->offset();
5061 gold_assert(address
5062 == align_address(address,
5063 stub->stub_template()->alignment()));
5064 stub->write(oview + stub->offset(), stub->stub_template()->size(),
5065 big_endian);
5066 }
5067
5068 // Write ARM V4BX relocation stubs.
5069 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5070 p != this->arm_v4bx_stubs_.end();
5071 ++p)
5072 {
5073 if (*p == NULL)
5074 continue;
5075
5076 Arm_address address = this->address() + (*p)->offset();
5077 gold_assert(address
5078 == align_address(address,
5079 (*p)->stub_template()->alignment()));
5080 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
5081 big_endian);
5082 }
5083
5084 if (parameters->options().stub_group_auto_padding())
5085 {
5086 // Zero-fill padding area.
5087 gold_assert((unsigned int)(this->prev_data_size_ + this->padding_) <= oview_size);
5088 unsigned char* p_padding_area = oview + this->prev_data_size_;
5089 for (unsigned int i = 0; i < this->padding_; ++i)
5090 *(p_padding_area + i) = 0;
5091 }
5092
5093 of->write_output_view(this->offset(), oview_size, oview);
5094 }
5095
5096 // Update the data size and address alignment of the stub table at the end
5097 // of a relaxation pass. Return true if either the data size or the
5098 // alignment changed in this relaxation pass.
5099
5100 template<bool big_endian>
5101 bool
update_data_size_and_addralign()5102 Stub_table<big_endian>::update_data_size_and_addralign()
5103 {
5104 // Go over all stubs in table to compute data size and address alignment.
5105 off_t size = this->reloc_stubs_size_;
5106 unsigned addralign = this->reloc_stubs_addralign_;
5107
5108 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
5109 p != this->cortex_a8_stubs_.end();
5110 ++p)
5111 {
5112 const Stub_template* stub_template = p->second->stub_template();
5113 addralign = std::max(addralign, stub_template->alignment());
5114 size = (align_address(size, stub_template->alignment())
5115 + stub_template->size());
5116 }
5117
5118 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5119 p != this->arm_v4bx_stubs_.end();
5120 ++p)
5121 {
5122 if (*p == NULL)
5123 continue;
5124
5125 const Stub_template* stub_template = (*p)->stub_template();
5126 addralign = std::max(addralign, stub_template->alignment());
5127 size = (align_address(size, stub_template->alignment())
5128 + stub_template->size());
5129 }
5130
5131 unsigned int prev_padding = this->padding_;
5132
5133 // Smart padding.
5134 if (parameters->options().stub_group_auto_padding())
5135 {
5136 if(size > this->prev_data_size_)
5137 {
5138 // Stub table has to grow 'delta' bytes.
5139 unsigned int delta = size - this->prev_data_size_;
5140 // Test to see if this delta grow could be "absorbed" by the
5141 // "padding_" we added in previously iteration.
5142 if (delta <= this->padding_)
5143 {
5144 // Yes! Grow into padding area, shrink padding, keep stub table
5145 // size unchanged.
5146 this->padding_ -= delta;
5147 }
5148 else
5149 {
5150 // No! Delta is too much to fit in padding area. Heuristically, we
5151 // increase padding. Padding is about 0.5% of huge increment, or
5152 // 2% of moderate increment, or 0% for smaller ones..
5153 if (delta >= 0x50000)
5154 this->padding_ = 0x250;
5155 else if (delta >= 0x30000)
5156 this->padding_ = 0x150;
5157 else if (delta >= 0x10000)
5158 this->padding_ = 0x100;
5159 else if (delta >= 0x500)
5160 {
5161 // Set padding to 2% of stub table growth delta or 0x40,
5162 // whichever is smaller.
5163 this->padding_ = std::min((unsigned int)(delta * 0.02),
5164 (unsigned int)0x40);
5165 }
5166 }
5167 }
5168 else if (size < this->prev_data_size_)
5169 {
5170 // Stub table shrinks, this is rare, but not impossible.
5171 unsigned int delta = this->prev_data_size_ - size;
5172 // So let padding increase to absorb the shrinking. Still we get an
5173 // unchanged stub table.
5174 this->padding_ += delta;
5175 }
5176 }
5177
5178 // Check if either data size or alignment changed in this pass.
5179 // Update prev_data_size_ and prev_addralign_. These will be used
5180 // as the current data size and address alignment for the next pass.
5181 bool changed = (size + this->padding_) !=
5182 this->prev_data_size_ + prev_padding;
5183
5184 this->prev_data_size_ = size;
5185
5186 if (addralign != this->prev_addralign_)
5187 changed = true;
5188 this->prev_addralign_ = addralign;
5189
5190 return changed;
5191 }
5192
5193 // Finalize the stubs. This sets the offsets of the stubs within the stub
5194 // table. It also marks all input sections needing Cortex-A8 workaround.
5195
5196 template<bool big_endian>
5197 void
finalize_stubs()5198 Stub_table<big_endian>::finalize_stubs()
5199 {
5200 off_t off = this->reloc_stubs_size_;
5201 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
5202 p != this->cortex_a8_stubs_.end();
5203 ++p)
5204 {
5205 Cortex_a8_stub* stub = p->second;
5206 const Stub_template* stub_template = stub->stub_template();
5207 uint64_t stub_addralign = stub_template->alignment();
5208 off = align_address(off, stub_addralign);
5209 stub->set_offset(off);
5210 off += stub_template->size();
5211
5212 // Mark input section so that we can determine later if a code section
5213 // needs the Cortex-A8 workaround quickly.
5214 Arm_relobj<big_endian>* arm_relobj =
5215 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
5216 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
5217 }
5218
5219 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5220 p != this->arm_v4bx_stubs_.end();
5221 ++p)
5222 {
5223 if (*p == NULL)
5224 continue;
5225
5226 const Stub_template* stub_template = (*p)->stub_template();
5227 uint64_t stub_addralign = stub_template->alignment();
5228 off = align_address(off, stub_addralign);
5229 (*p)->set_offset(off);
5230 off += stub_template->size();
5231 }
5232
5233 gold_assert(off <= this->prev_data_size_);
5234 }
5235
5236 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
5237 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
5238 // of the address range seen by the linker.
5239
5240 template<bool big_endian>
5241 void
apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian> * arm_target,unsigned char * view,Arm_address view_address,section_size_type view_size)5242 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
5243 Target_arm<big_endian>* arm_target,
5244 unsigned char* view,
5245 Arm_address view_address,
5246 section_size_type view_size)
5247 {
5248 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
5249 for (Cortex_a8_stub_list::const_iterator p =
5250 this->cortex_a8_stubs_.lower_bound(view_address);
5251 ((p != this->cortex_a8_stubs_.end())
5252 && (p->first < (view_address + view_size)));
5253 ++p)
5254 {
5255 // We do not store the THUMB bit in the LSB of either the branch address
5256 // or the stub offset. There is no need to strip the LSB.
5257 Arm_address branch_address = p->first;
5258 const Cortex_a8_stub* stub = p->second;
5259 Arm_address stub_address = this->address() + stub->offset();
5260
5261 // Offset of the branch instruction relative to this view.
5262 section_size_type offset =
5263 convert_to_section_size_type(branch_address - view_address);
5264 gold_assert((offset + 4) <= view_size);
5265
5266 arm_target->apply_cortex_a8_workaround(stub, stub_address,
5267 view + offset, branch_address);
5268 }
5269 }
5270
5271 // Arm_input_section methods.
5272
5273 // Initialize an Arm_input_section.
5274
5275 template<bool big_endian>
5276 void
init()5277 Arm_input_section<big_endian>::init()
5278 {
5279 Relobj* relobj = this->relobj();
5280 unsigned int shndx = this->shndx();
5281
5282 // We have to cache original size, alignment and contents to avoid locking
5283 // the original file.
5284 this->original_addralign_ =
5285 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
5286
5287 // This is not efficient but we expect only a small number of relaxed
5288 // input sections for stubs.
5289 section_size_type section_size;
5290 const unsigned char* section_contents =
5291 relobj->section_contents(shndx, §ion_size, false);
5292 this->original_size_ =
5293 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
5294
5295 gold_assert(this->original_contents_ == NULL);
5296 this->original_contents_ = new unsigned char[section_size];
5297 memcpy(this->original_contents_, section_contents, section_size);
5298
5299 // We want to make this look like the original input section after
5300 // output sections are finalized.
5301 Output_section* os = relobj->output_section(shndx);
5302 off_t offset = relobj->output_section_offset(shndx);
5303 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
5304 this->set_address(os->address() + offset);
5305 this->set_file_offset(os->offset() + offset);
5306
5307 this->set_current_data_size(this->original_size_);
5308 this->finalize_data_size();
5309 }
5310
5311 template<bool big_endian>
5312 void
do_write(Output_file * of)5313 Arm_input_section<big_endian>::do_write(Output_file* of)
5314 {
5315 // We have to write out the original section content.
5316 gold_assert(this->original_contents_ != NULL);
5317 of->write(this->offset(), this->original_contents_,
5318 this->original_size_);
5319
5320 // If this owns a stub table and it is not empty, write it.
5321 if (this->is_stub_table_owner() && !this->stub_table_->empty())
5322 this->stub_table_->write(of);
5323 }
5324
5325 // Finalize data size.
5326
5327 template<bool big_endian>
5328 void
set_final_data_size()5329 Arm_input_section<big_endian>::set_final_data_size()
5330 {
5331 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5332
5333 if (this->is_stub_table_owner())
5334 {
5335 this->stub_table_->finalize_data_size();
5336 off = align_address(off, this->stub_table_->addralign());
5337 off += this->stub_table_->data_size();
5338 }
5339 this->set_data_size(off);
5340 }
5341
5342 // Reset address and file offset.
5343
5344 template<bool big_endian>
5345 void
do_reset_address_and_file_offset()5346 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
5347 {
5348 // Size of the original input section contents.
5349 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5350
5351 // If this is a stub table owner, account for the stub table size.
5352 if (this->is_stub_table_owner())
5353 {
5354 Stub_table<big_endian>* stub_table = this->stub_table_;
5355
5356 // Reset the stub table's address and file offset. The
5357 // current data size for child will be updated after that.
5358 stub_table_->reset_address_and_file_offset();
5359 off = align_address(off, stub_table_->addralign());
5360 off += stub_table->current_data_size();
5361 }
5362
5363 this->set_current_data_size(off);
5364 }
5365
5366 // Arm_exidx_cantunwind methods.
5367
5368 // Write this to Output file OF for a fixed endianness.
5369
5370 template<bool big_endian>
5371 void
do_fixed_endian_write(Output_file * of)5372 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
5373 {
5374 off_t offset = this->offset();
5375 const section_size_type oview_size = 8;
5376 unsigned char* const oview = of->get_output_view(offset, oview_size);
5377
5378 Output_section* os = this->relobj_->output_section(this->shndx_);
5379 gold_assert(os != NULL);
5380
5381 Arm_relobj<big_endian>* arm_relobj =
5382 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5383 Arm_address output_offset =
5384 arm_relobj->get_output_section_offset(this->shndx_);
5385 Arm_address section_start;
5386 section_size_type section_size;
5387
5388 // Find out the end of the text section referred by this.
5389 if (output_offset != Arm_relobj<big_endian>::invalid_address)
5390 {
5391 section_start = os->address() + output_offset;
5392 const Arm_exidx_input_section* exidx_input_section =
5393 arm_relobj->exidx_input_section_by_link(this->shndx_);
5394 gold_assert(exidx_input_section != NULL);
5395 section_size =
5396 convert_to_section_size_type(exidx_input_section->text_size());
5397 }
5398 else
5399 {
5400 // Currently this only happens for a relaxed section.
5401 const Output_relaxed_input_section* poris =
5402 os->find_relaxed_input_section(this->relobj_, this->shndx_);
5403 gold_assert(poris != NULL);
5404 section_start = poris->address();
5405 section_size = convert_to_section_size_type(poris->data_size());
5406 }
5407
5408 // We always append this to the end of an EXIDX section.
5409 Arm_address output_address = section_start + section_size;
5410
5411 // Write out the entry. The first word either points to the beginning
5412 // or after the end of a text section. The second word is the special
5413 // EXIDX_CANTUNWIND value.
5414 uint32_t prel31_offset = output_address - this->address();
5415 if (Bits<31>::has_overflow32(offset))
5416 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5417 elfcpp::Swap_unaligned<32, big_endian>::writeval(oview,
5418 prel31_offset & 0x7fffffffU);
5419 elfcpp::Swap_unaligned<32, big_endian>::writeval(oview + 4,
5420 elfcpp::EXIDX_CANTUNWIND);
5421
5422 of->write_output_view(this->offset(), oview_size, oview);
5423 }
5424
5425 // Arm_exidx_merged_section methods.
5426
5427 // Constructor for Arm_exidx_merged_section.
5428 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5429 // SECTION_OFFSET_MAP points to a section offset map describing how
5430 // parts of the input section are mapped to output. DELETED_BYTES is
5431 // the number of bytes deleted from the EXIDX input section.
5432
Arm_exidx_merged_section(const Arm_exidx_input_section & exidx_input_section,const Arm_exidx_section_offset_map & section_offset_map,uint32_t deleted_bytes)5433 Arm_exidx_merged_section::Arm_exidx_merged_section(
5434 const Arm_exidx_input_section& exidx_input_section,
5435 const Arm_exidx_section_offset_map& section_offset_map,
5436 uint32_t deleted_bytes)
5437 : Output_relaxed_input_section(exidx_input_section.relobj(),
5438 exidx_input_section.shndx(),
5439 exidx_input_section.addralign()),
5440 exidx_input_section_(exidx_input_section),
5441 section_offset_map_(section_offset_map)
5442 {
5443 // If we retain or discard the whole EXIDX input section, we would
5444 // not be here.
5445 gold_assert(deleted_bytes != 0
5446 && deleted_bytes != this->exidx_input_section_.size());
5447
5448 // Fix size here so that we do not need to implement set_final_data_size.
5449 uint32_t size = exidx_input_section.size() - deleted_bytes;
5450 this->set_data_size(size);
5451 this->fix_data_size();
5452
5453 // Allocate buffer for section contents and build contents.
5454 this->section_contents_ = new unsigned char[size];
5455 }
5456
5457 // Build the contents of a merged EXIDX output section.
5458
5459 void
build_contents(const unsigned char * original_contents,section_size_type original_size)5460 Arm_exidx_merged_section::build_contents(
5461 const unsigned char* original_contents,
5462 section_size_type original_size)
5463 {
5464 // Go over spans of input offsets and write only those that are not
5465 // discarded.
5466 section_offset_type in_start = 0;
5467 section_offset_type out_start = 0;
5468 section_offset_type in_max =
5469 convert_types<section_offset_type>(original_size);
5470 section_offset_type out_max =
5471 convert_types<section_offset_type>(this->data_size());
5472 for (Arm_exidx_section_offset_map::const_iterator p =
5473 this->section_offset_map_.begin();
5474 p != this->section_offset_map_.end();
5475 ++p)
5476 {
5477 section_offset_type in_end = p->first;
5478 gold_assert(in_end >= in_start);
5479 section_offset_type out_end = p->second;
5480 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5481 if (out_end != -1)
5482 {
5483 size_t out_chunk_size =
5484 convert_types<size_t>(out_end - out_start + 1);
5485
5486 gold_assert(out_chunk_size == in_chunk_size
5487 && in_end < in_max && out_end < out_max);
5488
5489 memcpy(this->section_contents_ + out_start,
5490 original_contents + in_start,
5491 out_chunk_size);
5492 out_start += out_chunk_size;
5493 }
5494 in_start += in_chunk_size;
5495 }
5496 }
5497
5498 // Given an input OBJECT, an input section index SHNDX within that
5499 // object, and an OFFSET relative to the start of that input
5500 // section, return whether or not the corresponding offset within
5501 // the output section is known. If this function returns true, it
5502 // sets *POUTPUT to the output offset. The value -1 indicates that
5503 // this input offset is being discarded.
5504
5505 bool
do_output_offset(const Relobj * relobj,unsigned int shndx,section_offset_type offset,section_offset_type * poutput) const5506 Arm_exidx_merged_section::do_output_offset(
5507 const Relobj* relobj,
5508 unsigned int shndx,
5509 section_offset_type offset,
5510 section_offset_type* poutput) const
5511 {
5512 // We only handle offsets for the original EXIDX input section.
5513 if (relobj != this->exidx_input_section_.relobj()
5514 || shndx != this->exidx_input_section_.shndx())
5515 return false;
5516
5517 section_offset_type section_size =
5518 convert_types<section_offset_type>(this->exidx_input_section_.size());
5519 if (offset < 0 || offset >= section_size)
5520 // Input offset is out of valid range.
5521 *poutput = -1;
5522 else
5523 {
5524 // We need to look up the section offset map to determine the output
5525 // offset. Find the reference point in map that is first offset
5526 // bigger than or equal to this offset.
5527 Arm_exidx_section_offset_map::const_iterator p =
5528 this->section_offset_map_.lower_bound(offset);
5529
5530 // The section offset maps are build such that this should not happen if
5531 // input offset is in the valid range.
5532 gold_assert(p != this->section_offset_map_.end());
5533
5534 // We need to check if this is dropped.
5535 section_offset_type ref = p->first;
5536 section_offset_type mapped_ref = p->second;
5537
5538 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5539 // Offset is present in output.
5540 *poutput = mapped_ref + (offset - ref);
5541 else
5542 // Offset is discarded owing to EXIDX entry merging.
5543 *poutput = -1;
5544 }
5545
5546 return true;
5547 }
5548
5549 // Write this to output file OF.
5550
5551 void
do_write(Output_file * of)5552 Arm_exidx_merged_section::do_write(Output_file* of)
5553 {
5554 off_t offset = this->offset();
5555 const section_size_type oview_size = this->data_size();
5556 unsigned char* const oview = of->get_output_view(offset, oview_size);
5557
5558 Output_section* os = this->relobj()->output_section(this->shndx());
5559 gold_assert(os != NULL);
5560
5561 memcpy(oview, this->section_contents_, oview_size);
5562 of->write_output_view(this->offset(), oview_size, oview);
5563 }
5564
5565 // Arm_exidx_fixup methods.
5566
5567 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5568 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5569 // points to the end of the last seen EXIDX section.
5570
5571 void
add_exidx_cantunwind_as_needed()5572 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5573 {
5574 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5575 && this->last_input_section_ != NULL)
5576 {
5577 Relobj* relobj = this->last_input_section_->relobj();
5578 unsigned int text_shndx = this->last_input_section_->link();
5579 Arm_exidx_cantunwind* cantunwind =
5580 new Arm_exidx_cantunwind(relobj, text_shndx);
5581 this->exidx_output_section_->add_output_section_data(cantunwind);
5582 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5583 }
5584 }
5585
5586 // Process an EXIDX section entry in input. Return whether this entry
5587 // can be deleted in the output. SECOND_WORD in the second word of the
5588 // EXIDX entry.
5589
5590 bool
process_exidx_entry(uint32_t second_word)5591 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5592 {
5593 bool delete_entry;
5594 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5595 {
5596 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5597 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5598 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5599 }
5600 else if ((second_word & 0x80000000) != 0)
5601 {
5602 // Inlined unwinding data. Merge if equal to previous.
5603 delete_entry = (merge_exidx_entries_
5604 && this->last_unwind_type_ == UT_INLINED_ENTRY
5605 && this->last_inlined_entry_ == second_word);
5606 this->last_unwind_type_ = UT_INLINED_ENTRY;
5607 this->last_inlined_entry_ = second_word;
5608 }
5609 else
5610 {
5611 // Normal table entry. In theory we could merge these too,
5612 // but duplicate entries are likely to be much less common.
5613 delete_entry = false;
5614 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5615 }
5616 return delete_entry;
5617 }
5618
5619 // Update the current section offset map during EXIDX section fix-up.
5620 // If there is no map, create one. INPUT_OFFSET is the offset of a
5621 // reference point, DELETED_BYTES is the number of deleted by in the
5622 // section so far. If DELETE_ENTRY is true, the reference point and
5623 // all offsets after the previous reference point are discarded.
5624
5625 void
update_offset_map(section_offset_type input_offset,section_size_type deleted_bytes,bool delete_entry)5626 Arm_exidx_fixup::update_offset_map(
5627 section_offset_type input_offset,
5628 section_size_type deleted_bytes,
5629 bool delete_entry)
5630 {
5631 if (this->section_offset_map_ == NULL)
5632 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5633 section_offset_type output_offset;
5634 if (delete_entry)
5635 output_offset = Arm_exidx_input_section::invalid_offset;
5636 else
5637 output_offset = input_offset - deleted_bytes;
5638 (*this->section_offset_map_)[input_offset] = output_offset;
5639 }
5640
5641 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5642 // bytes deleted. SECTION_CONTENTS points to the contents of the EXIDX
5643 // section and SECTION_SIZE is the number of bytes pointed by SECTION_CONTENTS.
5644 // If some entries are merged, also store a pointer to a newly created
5645 // Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The caller
5646 // owns the map and is responsible for releasing it after use.
5647
5648 template<bool big_endian>
5649 uint32_t
process_exidx_section(const Arm_exidx_input_section * exidx_input_section,const unsigned char * section_contents,section_size_type section_size,Arm_exidx_section_offset_map ** psection_offset_map)5650 Arm_exidx_fixup::process_exidx_section(
5651 const Arm_exidx_input_section* exidx_input_section,
5652 const unsigned char* section_contents,
5653 section_size_type section_size,
5654 Arm_exidx_section_offset_map** psection_offset_map)
5655 {
5656 Relobj* relobj = exidx_input_section->relobj();
5657 unsigned shndx = exidx_input_section->shndx();
5658
5659 if ((section_size % 8) != 0)
5660 {
5661 // Something is wrong with this section. Better not touch it.
5662 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5663 relobj->name().c_str(), shndx);
5664 this->last_input_section_ = exidx_input_section;
5665 this->last_unwind_type_ = UT_NONE;
5666 return 0;
5667 }
5668
5669 uint32_t deleted_bytes = 0;
5670 bool prev_delete_entry = false;
5671 gold_assert(this->section_offset_map_ == NULL);
5672
5673 for (section_size_type i = 0; i < section_size; i += 8)
5674 {
5675 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5676 const Valtype* wv =
5677 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5678 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5679
5680 bool delete_entry = this->process_exidx_entry(second_word);
5681
5682 // Entry deletion causes changes in output offsets. We use a std::map
5683 // to record these. And entry (x, y) means input offset x
5684 // is mapped to output offset y. If y is invalid_offset, then x is
5685 // dropped in the output. Because of the way std::map::lower_bound
5686 // works, we record the last offset in a region w.r.t to keeping or
5687 // dropping. If there is no entry (x0, y0) for an input offset x0,
5688 // the output offset y0 of it is determined by the output offset y1 of
5689 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5690 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Otherwise, y1
5691 // y0 is also -1.
5692 if (delete_entry != prev_delete_entry && i != 0)
5693 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5694
5695 // Update total deleted bytes for this entry.
5696 if (delete_entry)
5697 deleted_bytes += 8;
5698
5699 prev_delete_entry = delete_entry;
5700 }
5701
5702 // If section offset map is not NULL, make an entry for the end of
5703 // section.
5704 if (this->section_offset_map_ != NULL)
5705 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5706
5707 *psection_offset_map = this->section_offset_map_;
5708 this->section_offset_map_ = NULL;
5709 this->last_input_section_ = exidx_input_section;
5710
5711 // Set the first output text section so that we can link the EXIDX output
5712 // section to it. Ignore any EXIDX input section that is completely merged.
5713 if (this->first_output_text_section_ == NULL
5714 && deleted_bytes != section_size)
5715 {
5716 unsigned int link = exidx_input_section->link();
5717 Output_section* os = relobj->output_section(link);
5718 gold_assert(os != NULL);
5719 this->first_output_text_section_ = os;
5720 }
5721
5722 return deleted_bytes;
5723 }
5724
5725 // Arm_output_section methods.
5726
5727 // Create a stub group for input sections from BEGIN to END. OWNER
5728 // points to the input section to be the owner a new stub table.
5729
5730 template<bool big_endian>
5731 void
create_stub_group(Input_section_list::const_iterator begin,Input_section_list::const_iterator end,Input_section_list::const_iterator owner,Target_arm<big_endian> * target,std::vector<Output_relaxed_input_section * > * new_relaxed_sections,const Task * task)5732 Arm_output_section<big_endian>::create_stub_group(
5733 Input_section_list::const_iterator begin,
5734 Input_section_list::const_iterator end,
5735 Input_section_list::const_iterator owner,
5736 Target_arm<big_endian>* target,
5737 std::vector<Output_relaxed_input_section*>* new_relaxed_sections,
5738 const Task* task)
5739 {
5740 // We use a different kind of relaxed section in an EXIDX section.
5741 // The static casting from Output_relaxed_input_section to
5742 // Arm_input_section is invalid in an EXIDX section. We are okay
5743 // because we should not be calling this for an EXIDX section.
5744 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5745
5746 // Currently we convert ordinary input sections into relaxed sections only
5747 // at this point but we may want to support creating relaxed input section
5748 // very early. So we check here to see if owner is already a relaxed
5749 // section.
5750
5751 Arm_input_section<big_endian>* arm_input_section;
5752 if (owner->is_relaxed_input_section())
5753 {
5754 arm_input_section =
5755 Arm_input_section<big_endian>::as_arm_input_section(
5756 owner->relaxed_input_section());
5757 }
5758 else
5759 {
5760 gold_assert(owner->is_input_section());
5761 // Create a new relaxed input section. We need to lock the original
5762 // file.
5763 Task_lock_obj<Object> tl(task, owner->relobj());
5764 arm_input_section =
5765 target->new_arm_input_section(owner->relobj(), owner->shndx());
5766 new_relaxed_sections->push_back(arm_input_section);
5767 }
5768
5769 // Create a stub table.
5770 Stub_table<big_endian>* stub_table =
5771 target->new_stub_table(arm_input_section);
5772
5773 arm_input_section->set_stub_table(stub_table);
5774
5775 Input_section_list::const_iterator p = begin;
5776 Input_section_list::const_iterator prev_p;
5777
5778 // Look for input sections or relaxed input sections in [begin ... end].
5779 do
5780 {
5781 if (p->is_input_section() || p->is_relaxed_input_section())
5782 {
5783 // The stub table information for input sections live
5784 // in their objects.
5785 Arm_relobj<big_endian>* arm_relobj =
5786 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5787 arm_relobj->set_stub_table(p->shndx(), stub_table);
5788 }
5789 prev_p = p++;
5790 }
5791 while (prev_p != end);
5792 }
5793
5794 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5795 // of stub groups. We grow a stub group by adding input section until the
5796 // size is just below GROUP_SIZE. The last input section will be converted
5797 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5798 // input section after the stub table, effectively double the group size.
5799 //
5800 // This is similar to the group_sections() function in elf32-arm.c but is
5801 // implemented differently.
5802
5803 template<bool big_endian>
5804 void
group_sections(section_size_type group_size,bool stubs_always_after_branch,Target_arm<big_endian> * target,const Task * task)5805 Arm_output_section<big_endian>::group_sections(
5806 section_size_type group_size,
5807 bool stubs_always_after_branch,
5808 Target_arm<big_endian>* target,
5809 const Task* task)
5810 {
5811 // States for grouping.
5812 typedef enum
5813 {
5814 // No group is being built.
5815 NO_GROUP,
5816 // A group is being built but the stub table is not found yet.
5817 // We keep group a stub group until the size is just under GROUP_SIZE.
5818 // The last input section in the group will be used as the stub table.
5819 FINDING_STUB_SECTION,
5820 // A group is being built and we have already found a stub table.
5821 // We enter this state to grow a stub group by adding input section
5822 // after the stub table. This effectively doubles the group size.
5823 HAS_STUB_SECTION
5824 } State;
5825
5826 // Any newly created relaxed sections are stored here.
5827 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5828
5829 State state = NO_GROUP;
5830 section_size_type off = 0;
5831 section_size_type group_begin_offset = 0;
5832 section_size_type group_end_offset = 0;
5833 section_size_type stub_table_end_offset = 0;
5834 Input_section_list::const_iterator group_begin =
5835 this->input_sections().end();
5836 Input_section_list::const_iterator stub_table =
5837 this->input_sections().end();
5838 Input_section_list::const_iterator group_end = this->input_sections().end();
5839 for (Input_section_list::const_iterator p = this->input_sections().begin();
5840 p != this->input_sections().end();
5841 ++p)
5842 {
5843 section_size_type section_begin_offset =
5844 align_address(off, p->addralign());
5845 section_size_type section_end_offset =
5846 section_begin_offset + p->data_size();
5847
5848 // Check to see if we should group the previously seen sections.
5849 switch (state)
5850 {
5851 case NO_GROUP:
5852 break;
5853
5854 case FINDING_STUB_SECTION:
5855 // Adding this section makes the group larger than GROUP_SIZE.
5856 if (section_end_offset - group_begin_offset >= group_size)
5857 {
5858 if (stubs_always_after_branch)
5859 {
5860 gold_assert(group_end != this->input_sections().end());
5861 this->create_stub_group(group_begin, group_end, group_end,
5862 target, &new_relaxed_sections,
5863 task);
5864 state = NO_GROUP;
5865 }
5866 else
5867 {
5868 // But wait, there's more! Input sections up to
5869 // stub_group_size bytes after the stub table can be
5870 // handled by it too.
5871 state = HAS_STUB_SECTION;
5872 stub_table = group_end;
5873 stub_table_end_offset = group_end_offset;
5874 }
5875 }
5876 break;
5877
5878 case HAS_STUB_SECTION:
5879 // Adding this section makes the post stub-section group larger
5880 // than GROUP_SIZE.
5881 if (section_end_offset - stub_table_end_offset >= group_size)
5882 {
5883 gold_assert(group_end != this->input_sections().end());
5884 this->create_stub_group(group_begin, group_end, stub_table,
5885 target, &new_relaxed_sections, task);
5886 state = NO_GROUP;
5887 }
5888 break;
5889
5890 default:
5891 gold_unreachable();
5892 }
5893
5894 // If we see an input section and currently there is no group, start
5895 // a new one. Skip any empty sections. We look at the data size
5896 // instead of calling p->relobj()->section_size() to avoid locking.
5897 if ((p->is_input_section() || p->is_relaxed_input_section())
5898 && (p->data_size() != 0))
5899 {
5900 if (state == NO_GROUP)
5901 {
5902 state = FINDING_STUB_SECTION;
5903 group_begin = p;
5904 group_begin_offset = section_begin_offset;
5905 }
5906
5907 // Keep track of the last input section seen.
5908 group_end = p;
5909 group_end_offset = section_end_offset;
5910 }
5911
5912 off = section_end_offset;
5913 }
5914
5915 // Create a stub group for any ungrouped sections.
5916 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5917 {
5918 gold_assert(group_end != this->input_sections().end());
5919 this->create_stub_group(group_begin, group_end,
5920 (state == FINDING_STUB_SECTION
5921 ? group_end
5922 : stub_table),
5923 target, &new_relaxed_sections, task);
5924 }
5925
5926 // Convert input section into relaxed input section in a batch.
5927 if (!new_relaxed_sections.empty())
5928 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5929
5930 // Update the section offsets
5931 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5932 {
5933 Arm_relobj<big_endian>* arm_relobj =
5934 Arm_relobj<big_endian>::as_arm_relobj(
5935 new_relaxed_sections[i]->relobj());
5936 unsigned int shndx = new_relaxed_sections[i]->shndx();
5937 // Tell Arm_relobj that this input section is converted.
5938 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5939 }
5940 }
5941
5942 // Append non empty text sections in this to LIST in ascending
5943 // order of their position in this.
5944
5945 template<bool big_endian>
5946 void
append_text_sections_to_list(Text_section_list * list)5947 Arm_output_section<big_endian>::append_text_sections_to_list(
5948 Text_section_list* list)
5949 {
5950 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5951
5952 for (Input_section_list::const_iterator p = this->input_sections().begin();
5953 p != this->input_sections().end();
5954 ++p)
5955 {
5956 // We only care about plain or relaxed input sections. We also
5957 // ignore any merged sections.
5958 if (p->is_input_section() || p->is_relaxed_input_section())
5959 list->push_back(Text_section_list::value_type(p->relobj(),
5960 p->shndx()));
5961 }
5962 }
5963
5964 template<bool big_endian>
5965 void
fix_exidx_coverage(Layout * layout,const Text_section_list & sorted_text_sections,Symbol_table * symtab,bool merge_exidx_entries,const Task * task)5966 Arm_output_section<big_endian>::fix_exidx_coverage(
5967 Layout* layout,
5968 const Text_section_list& sorted_text_sections,
5969 Symbol_table* symtab,
5970 bool merge_exidx_entries,
5971 const Task* task)
5972 {
5973 // We should only do this for the EXIDX output section.
5974 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5975
5976 // We don't want the relaxation loop to undo these changes, so we discard
5977 // the current saved states and take another one after the fix-up.
5978 this->discard_states();
5979
5980 // Remove all input sections.
5981 uint64_t address = this->address();
5982 typedef std::list<Output_section::Input_section> Input_section_list;
5983 Input_section_list input_sections;
5984 this->reset_address_and_file_offset();
5985 this->get_input_sections(address, std::string(""), &input_sections);
5986
5987 if (!this->input_sections().empty())
5988 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5989
5990 // Go through all the known input sections and record them.
5991 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5992 typedef Unordered_map<Section_id, const Output_section::Input_section*,
5993 Section_id_hash> Text_to_exidx_map;
5994 Text_to_exidx_map text_to_exidx_map;
5995 for (Input_section_list::const_iterator p = input_sections.begin();
5996 p != input_sections.end();
5997 ++p)
5998 {
5999 // This should never happen. At this point, we should only see
6000 // plain EXIDX input sections.
6001 gold_assert(!p->is_relaxed_input_section());
6002 text_to_exidx_map[Section_id(p->relobj(), p->shndx())] = &(*p);
6003 }
6004
6005 Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
6006
6007 // Go over the sorted text sections.
6008 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
6009 Section_id_set processed_input_sections;
6010 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
6011 p != sorted_text_sections.end();
6012 ++p)
6013 {
6014 Relobj* relobj = p->first;
6015 unsigned int shndx = p->second;
6016
6017 Arm_relobj<big_endian>* arm_relobj =
6018 Arm_relobj<big_endian>::as_arm_relobj(relobj);
6019 const Arm_exidx_input_section* exidx_input_section =
6020 arm_relobj->exidx_input_section_by_link(shndx);
6021
6022 // If this text section has no EXIDX section or if the EXIDX section
6023 // has errors, force an EXIDX_CANTUNWIND entry pointing to the end
6024 // of the last seen EXIDX section.
6025 if (exidx_input_section == NULL || exidx_input_section->has_errors())
6026 {
6027 exidx_fixup.add_exidx_cantunwind_as_needed();
6028 continue;
6029 }
6030
6031 Relobj* exidx_relobj = exidx_input_section->relobj();
6032 unsigned int exidx_shndx = exidx_input_section->shndx();
6033 Section_id sid(exidx_relobj, exidx_shndx);
6034 Text_to_exidx_map::const_iterator iter = text_to_exidx_map.find(sid);
6035 if (iter == text_to_exidx_map.end())
6036 {
6037 // This is odd. We have not seen this EXIDX input section before.
6038 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
6039 // issue a warning instead. We assume the user knows what he
6040 // or she is doing. Otherwise, this is an error.
6041 if (layout->script_options()->saw_sections_clause())
6042 gold_warning(_("unwinding may not work because EXIDX input section"
6043 " %u of %s is not in EXIDX output section"),
6044 exidx_shndx, exidx_relobj->name().c_str());
6045 else
6046 gold_error(_("unwinding may not work because EXIDX input section"
6047 " %u of %s is not in EXIDX output section"),
6048 exidx_shndx, exidx_relobj->name().c_str());
6049
6050 exidx_fixup.add_exidx_cantunwind_as_needed();
6051 continue;
6052 }
6053
6054 // We need to access the contents of the EXIDX section, lock the
6055 // object here.
6056 Task_lock_obj<Object> tl(task, exidx_relobj);
6057 section_size_type exidx_size;
6058 const unsigned char* exidx_contents =
6059 exidx_relobj->section_contents(exidx_shndx, &exidx_size, false);
6060
6061 // Fix up coverage and append input section to output data list.
6062 Arm_exidx_section_offset_map* section_offset_map = NULL;
6063 uint32_t deleted_bytes =
6064 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
6065 exidx_contents,
6066 exidx_size,
6067 §ion_offset_map);
6068
6069 if (deleted_bytes == exidx_input_section->size())
6070 {
6071 // The whole EXIDX section got merged. Remove it from output.
6072 gold_assert(section_offset_map == NULL);
6073 exidx_relobj->set_output_section(exidx_shndx, NULL);
6074
6075 // All local symbols defined in this input section will be dropped.
6076 // We need to adjust output local symbol count.
6077 arm_relobj->set_output_local_symbol_count_needs_update();
6078 }
6079 else if (deleted_bytes > 0)
6080 {
6081 // Some entries are merged. We need to convert this EXIDX input
6082 // section into a relaxed section.
6083 gold_assert(section_offset_map != NULL);
6084
6085 Arm_exidx_merged_section* merged_section =
6086 new Arm_exidx_merged_section(*exidx_input_section,
6087 *section_offset_map, deleted_bytes);
6088 merged_section->build_contents(exidx_contents, exidx_size);
6089
6090 const std::string secname = exidx_relobj->section_name(exidx_shndx);
6091 this->add_relaxed_input_section(layout, merged_section, secname);
6092 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
6093
6094 // All local symbols defined in discarded portions of this input
6095 // section will be dropped. We need to adjust output local symbol
6096 // count.
6097 arm_relobj->set_output_local_symbol_count_needs_update();
6098 }
6099 else
6100 {
6101 // Just add back the EXIDX input section.
6102 gold_assert(section_offset_map == NULL);
6103 const Output_section::Input_section* pis = iter->second;
6104 gold_assert(pis->is_input_section());
6105 this->add_script_input_section(*pis);
6106 }
6107
6108 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
6109 }
6110
6111 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
6112 exidx_fixup.add_exidx_cantunwind_as_needed();
6113
6114 // Remove any known EXIDX input sections that are not processed.
6115 for (Input_section_list::const_iterator p = input_sections.begin();
6116 p != input_sections.end();
6117 ++p)
6118 {
6119 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
6120 == processed_input_sections.end())
6121 {
6122 // We discard a known EXIDX section because its linked
6123 // text section has been folded by ICF. We also discard an
6124 // EXIDX section with error, the output does not matter in this
6125 // case. We do this to avoid triggering asserts.
6126 Arm_relobj<big_endian>* arm_relobj =
6127 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
6128 const Arm_exidx_input_section* exidx_input_section =
6129 arm_relobj->exidx_input_section_by_shndx(p->shndx());
6130 gold_assert(exidx_input_section != NULL);
6131 if (!exidx_input_section->has_errors())
6132 {
6133 unsigned int text_shndx = exidx_input_section->link();
6134 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
6135 }
6136
6137 // Remove this from link. We also need to recount the
6138 // local symbols.
6139 p->relobj()->set_output_section(p->shndx(), NULL);
6140 arm_relobj->set_output_local_symbol_count_needs_update();
6141 }
6142 }
6143
6144 // Link exidx output section to the first seen output section and
6145 // set correct entry size.
6146 this->set_link_section(exidx_fixup.first_output_text_section());
6147 this->set_entsize(8);
6148
6149 // Make changes permanent.
6150 this->save_states();
6151 this->set_section_offsets_need_adjustment();
6152 }
6153
6154 // Link EXIDX output sections to text output sections.
6155
6156 template<bool big_endian>
6157 void
set_exidx_section_link()6158 Arm_output_section<big_endian>::set_exidx_section_link()
6159 {
6160 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
6161 if (!this->input_sections().empty())
6162 {
6163 Input_section_list::const_iterator p = this->input_sections().begin();
6164 Arm_relobj<big_endian>* arm_relobj =
6165 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
6166 unsigned exidx_shndx = p->shndx();
6167 const Arm_exidx_input_section* exidx_input_section =
6168 arm_relobj->exidx_input_section_by_shndx(exidx_shndx);
6169 gold_assert(exidx_input_section != NULL);
6170 unsigned int text_shndx = exidx_input_section->link();
6171 Output_section* os = arm_relobj->output_section(text_shndx);
6172 this->set_link_section(os);
6173 }
6174 }
6175
6176 // Arm_relobj methods.
6177
6178 // Determine if an input section is scannable for stub processing. SHDR is
6179 // the header of the section and SHNDX is the section index. OS is the output
6180 // section for the input section and SYMTAB is the global symbol table used to
6181 // look up ICF information.
6182
6183 template<bool big_endian>
6184 bool
section_is_scannable(const elfcpp::Shdr<32,big_endian> & shdr,unsigned int shndx,const Output_section * os,const Symbol_table * symtab)6185 Arm_relobj<big_endian>::section_is_scannable(
6186 const elfcpp::Shdr<32, big_endian>& shdr,
6187 unsigned int shndx,
6188 const Output_section* os,
6189 const Symbol_table* symtab)
6190 {
6191 // Skip any empty sections, unallocated sections or sections whose
6192 // type are not SHT_PROGBITS.
6193 if (shdr.get_sh_size() == 0
6194 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
6195 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
6196 return false;
6197
6198 // Skip any discarded or ICF'ed sections.
6199 if (os == NULL || symtab->is_section_folded(this, shndx))
6200 return false;
6201
6202 // If this requires special offset handling, check to see if it is
6203 // a relaxed section. If this is not, then it is a merged section that
6204 // we cannot handle.
6205 if (this->is_output_section_offset_invalid(shndx))
6206 {
6207 const Output_relaxed_input_section* poris =
6208 os->find_relaxed_input_section(this, shndx);
6209 if (poris == NULL)
6210 return false;
6211 }
6212
6213 return true;
6214 }
6215
6216 // Determine if we want to scan the SHNDX-th section for relocation stubs.
6217 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6218
6219 template<bool big_endian>
6220 bool
section_needs_reloc_stub_scanning(const elfcpp::Shdr<32,big_endian> & shdr,const Relobj::Output_sections & out_sections,const Symbol_table * symtab,const unsigned char * pshdrs)6221 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
6222 const elfcpp::Shdr<32, big_endian>& shdr,
6223 const Relobj::Output_sections& out_sections,
6224 const Symbol_table* symtab,
6225 const unsigned char* pshdrs)
6226 {
6227 unsigned int sh_type = shdr.get_sh_type();
6228 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
6229 return false;
6230
6231 // Ignore empty section.
6232 off_t sh_size = shdr.get_sh_size();
6233 if (sh_size == 0)
6234 return false;
6235
6236 // Ignore reloc section with unexpected symbol table. The
6237 // error will be reported in the final link.
6238 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
6239 return false;
6240
6241 unsigned int reloc_size;
6242 if (sh_type == elfcpp::SHT_REL)
6243 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6244 else
6245 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6246
6247 // Ignore reloc section with unexpected entsize or uneven size.
6248 // The error will be reported in the final link.
6249 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
6250 return false;
6251
6252 // Ignore reloc section with bad info. This error will be
6253 // reported in the final link.
6254 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
6255 if (index >= this->shnum())
6256 return false;
6257
6258 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6259 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
6260 return this->section_is_scannable(text_shdr, index,
6261 out_sections[index], symtab);
6262 }
6263
6264 // Return the output address of either a plain input section or a relaxed
6265 // input section. SHNDX is the section index. We define and use this
6266 // instead of calling Output_section::output_address because that is slow
6267 // for large output.
6268
6269 template<bool big_endian>
6270 Arm_address
simple_input_section_output_address(unsigned int shndx,Output_section * os)6271 Arm_relobj<big_endian>::simple_input_section_output_address(
6272 unsigned int shndx,
6273 Output_section* os)
6274 {
6275 if (this->is_output_section_offset_invalid(shndx))
6276 {
6277 const Output_relaxed_input_section* poris =
6278 os->find_relaxed_input_section(this, shndx);
6279 // We do not handle merged sections here.
6280 gold_assert(poris != NULL);
6281 return poris->address();
6282 }
6283 else
6284 return os->address() + this->get_output_section_offset(shndx);
6285 }
6286
6287 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
6288 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6289
6290 template<bool big_endian>
6291 bool
section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32,big_endian> & shdr,unsigned int shndx,Output_section * os,const Symbol_table * symtab)6292 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
6293 const elfcpp::Shdr<32, big_endian>& shdr,
6294 unsigned int shndx,
6295 Output_section* os,
6296 const Symbol_table* symtab)
6297 {
6298 if (!this->section_is_scannable(shdr, shndx, os, symtab))
6299 return false;
6300
6301 // If the section does not cross any 4K-boundaries, it does not need to
6302 // be scanned.
6303 Arm_address address = this->simple_input_section_output_address(shndx, os);
6304 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
6305 return false;
6306
6307 return true;
6308 }
6309
6310 // Scan a section for Cortex-A8 workaround.
6311
6312 template<bool big_endian>
6313 void
scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32,big_endian> & shdr,unsigned int shndx,Output_section * os,Target_arm<big_endian> * arm_target)6314 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
6315 const elfcpp::Shdr<32, big_endian>& shdr,
6316 unsigned int shndx,
6317 Output_section* os,
6318 Target_arm<big_endian>* arm_target)
6319 {
6320 // Look for the first mapping symbol in this section. It should be
6321 // at (shndx, 0).
6322 Mapping_symbol_position section_start(shndx, 0);
6323 typename Mapping_symbols_info::const_iterator p =
6324 this->mapping_symbols_info_.lower_bound(section_start);
6325
6326 // There are no mapping symbols for this section. Treat it as a data-only
6327 // section.
6328 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
6329 return;
6330
6331 Arm_address output_address =
6332 this->simple_input_section_output_address(shndx, os);
6333
6334 // Get the section contents.
6335 section_size_type input_view_size = 0;
6336 const unsigned char* input_view =
6337 this->section_contents(shndx, &input_view_size, false);
6338
6339 // We need to go through the mapping symbols to determine what to
6340 // scan. There are two reasons. First, we should look at THUMB code and
6341 // THUMB code only. Second, we only want to look at the 4K-page boundary
6342 // to speed up the scanning.
6343
6344 while (p != this->mapping_symbols_info_.end()
6345 && p->first.first == shndx)
6346 {
6347 typename Mapping_symbols_info::const_iterator next =
6348 this->mapping_symbols_info_.upper_bound(p->first);
6349
6350 // Only scan part of a section with THUMB code.
6351 if (p->second == 't')
6352 {
6353 // Determine the end of this range.
6354 section_size_type span_start =
6355 convert_to_section_size_type(p->first.second);
6356 section_size_type span_end;
6357 if (next != this->mapping_symbols_info_.end()
6358 && next->first.first == shndx)
6359 span_end = convert_to_section_size_type(next->first.second);
6360 else
6361 span_end = convert_to_section_size_type(shdr.get_sh_size());
6362
6363 if (((span_start + output_address) & ~0xfffUL)
6364 != ((span_end + output_address - 1) & ~0xfffUL))
6365 {
6366 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
6367 span_start, span_end,
6368 input_view,
6369 output_address);
6370 }
6371 }
6372
6373 p = next;
6374 }
6375 }
6376
6377 // Scan relocations for stub generation.
6378
6379 template<bool big_endian>
6380 void
scan_sections_for_stubs(Target_arm<big_endian> * arm_target,const Symbol_table * symtab,const Layout * layout)6381 Arm_relobj<big_endian>::scan_sections_for_stubs(
6382 Target_arm<big_endian>* arm_target,
6383 const Symbol_table* symtab,
6384 const Layout* layout)
6385 {
6386 unsigned int shnum = this->shnum();
6387 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6388
6389 // Read the section headers.
6390 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6391 shnum * shdr_size,
6392 true, true);
6393
6394 // To speed up processing, we set up hash tables for fast lookup of
6395 // input offsets to output addresses.
6396 this->initialize_input_to_output_maps();
6397
6398 const Relobj::Output_sections& out_sections(this->output_sections());
6399
6400 Relocate_info<32, big_endian> relinfo;
6401 relinfo.symtab = symtab;
6402 relinfo.layout = layout;
6403 relinfo.object = this;
6404
6405 // Do relocation stubs scanning.
6406 const unsigned char* p = pshdrs + shdr_size;
6407 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6408 {
6409 const elfcpp::Shdr<32, big_endian> shdr(p);
6410 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
6411 pshdrs))
6412 {
6413 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
6414 Arm_address output_offset = this->get_output_section_offset(index);
6415 Arm_address output_address;
6416 if (output_offset != invalid_address)
6417 output_address = out_sections[index]->address() + output_offset;
6418 else
6419 {
6420 // Currently this only happens for a relaxed section.
6421 const Output_relaxed_input_section* poris =
6422 out_sections[index]->find_relaxed_input_section(this, index);
6423 gold_assert(poris != NULL);
6424 output_address = poris->address();
6425 }
6426
6427 // Get the relocations.
6428 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
6429 shdr.get_sh_size(),
6430 true, false);
6431
6432 // Get the section contents. This does work for the case in which
6433 // we modify the contents of an input section. We need to pass the
6434 // output view under such circumstances.
6435 section_size_type input_view_size = 0;
6436 const unsigned char* input_view =
6437 this->section_contents(index, &input_view_size, false);
6438
6439 relinfo.reloc_shndx = i;
6440 relinfo.data_shndx = index;
6441 unsigned int sh_type = shdr.get_sh_type();
6442 unsigned int reloc_size;
6443 if (sh_type == elfcpp::SHT_REL)
6444 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6445 else
6446 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6447
6448 Output_section* os = out_sections[index];
6449 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6450 shdr.get_sh_size() / reloc_size,
6451 os,
6452 output_offset == invalid_address,
6453 input_view, output_address,
6454 input_view_size);
6455 }
6456 }
6457
6458 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6459 // after its relocation section, if there is one, is processed for
6460 // relocation stubs. Merging this loop with the one above would have been
6461 // complicated since we would have had to make sure that relocation stub
6462 // scanning is done first.
6463 if (arm_target->fix_cortex_a8())
6464 {
6465 const unsigned char* p = pshdrs + shdr_size;
6466 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6467 {
6468 const elfcpp::Shdr<32, big_endian> shdr(p);
6469 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6470 out_sections[i],
6471 symtab))
6472 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6473 arm_target);
6474 }
6475 }
6476
6477 // After we've done the relocations, we release the hash tables,
6478 // since we no longer need them.
6479 this->free_input_to_output_maps();
6480 }
6481
6482 // Count the local symbols. The ARM backend needs to know if a symbol
6483 // is a THUMB function or not. For global symbols, it is easy because
6484 // the Symbol object keeps the ELF symbol type. For local symbol it is
6485 // harder because we cannot access this information. So we override the
6486 // do_count_local_symbol in parent and scan local symbols to mark
6487 // THUMB functions. This is not the most efficient way but I do not want to
6488 // slow down other ports by calling a per symbol target hook inside
6489 // Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6490
6491 template<bool big_endian>
6492 void
do_count_local_symbols(Stringpool_template<char> * pool,Stringpool_template<char> * dynpool)6493 Arm_relobj<big_endian>::do_count_local_symbols(
6494 Stringpool_template<char>* pool,
6495 Stringpool_template<char>* dynpool)
6496 {
6497 // We need to fix-up the values of any local symbols whose type are
6498 // STT_ARM_TFUNC.
6499
6500 // Ask parent to count the local symbols.
6501 Sized_relobj_file<32, big_endian>::do_count_local_symbols(pool, dynpool);
6502 const unsigned int loccount = this->local_symbol_count();
6503 if (loccount == 0)
6504 return;
6505
6506 // Initialize the thumb function bit-vector.
6507 std::vector<bool> empty_vector(loccount, false);
6508 this->local_symbol_is_thumb_function_.swap(empty_vector);
6509
6510 // Read the symbol table section header.
6511 const unsigned int symtab_shndx = this->symtab_shndx();
6512 elfcpp::Shdr<32, big_endian>
6513 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6514 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6515
6516 // Read the local symbols.
6517 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6518 gold_assert(loccount == symtabshdr.get_sh_info());
6519 off_t locsize = loccount * sym_size;
6520 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6521 locsize, true, true);
6522
6523 // For mapping symbol processing, we need to read the symbol names.
6524 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6525 if (strtab_shndx >= this->shnum())
6526 {
6527 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6528 return;
6529 }
6530
6531 elfcpp::Shdr<32, big_endian>
6532 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6533 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6534 {
6535 this->error(_("symbol table name section has wrong type: %u"),
6536 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6537 return;
6538 }
6539 const char* pnames =
6540 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6541 strtabshdr.get_sh_size(),
6542 false, false));
6543
6544 // Loop over the local symbols and mark any local symbols pointing
6545 // to THUMB functions.
6546
6547 // Skip the first dummy symbol.
6548 psyms += sym_size;
6549 typename Sized_relobj_file<32, big_endian>::Local_values* plocal_values =
6550 this->local_values();
6551 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6552 {
6553 elfcpp::Sym<32, big_endian> sym(psyms);
6554 elfcpp::STT st_type = sym.get_st_type();
6555 Symbol_value<32>& lv((*plocal_values)[i]);
6556 Arm_address input_value = lv.input_value();
6557
6558 // Check to see if this is a mapping symbol.
6559 const char* sym_name = pnames + sym.get_st_name();
6560 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6561 {
6562 bool is_ordinary;
6563 unsigned int input_shndx =
6564 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6565 gold_assert(is_ordinary);
6566
6567 // Strip of LSB in case this is a THUMB symbol.
6568 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6569 this->mapping_symbols_info_[msp] = sym_name[1];
6570 }
6571
6572 if (st_type == elfcpp::STT_ARM_TFUNC
6573 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6574 {
6575 // This is a THUMB function. Mark this and canonicalize the
6576 // symbol value by setting LSB.
6577 this->local_symbol_is_thumb_function_[i] = true;
6578 if ((input_value & 1) == 0)
6579 lv.set_input_value(input_value | 1);
6580 }
6581 }
6582 }
6583
6584 // Relocate sections.
6585 template<bool big_endian>
6586 void
do_relocate_sections(const Symbol_table * symtab,const Layout * layout,const unsigned char * pshdrs,Output_file * of,typename Sized_relobj_file<32,big_endian>::Views * pviews)6587 Arm_relobj<big_endian>::do_relocate_sections(
6588 const Symbol_table* symtab,
6589 const Layout* layout,
6590 const unsigned char* pshdrs,
6591 Output_file* of,
6592 typename Sized_relobj_file<32, big_endian>::Views* pviews)
6593 {
6594 // Call parent to relocate sections.
6595 Sized_relobj_file<32, big_endian>::do_relocate_sections(symtab, layout,
6596 pshdrs, of, pviews);
6597
6598 // We do not generate stubs if doing a relocatable link.
6599 if (parameters->options().relocatable())
6600 return;
6601
6602 // Relocate stub tables.
6603 unsigned int shnum = this->shnum();
6604
6605 Target_arm<big_endian>* arm_target =
6606 Target_arm<big_endian>::default_target();
6607
6608 Relocate_info<32, big_endian> relinfo;
6609 relinfo.symtab = symtab;
6610 relinfo.layout = layout;
6611 relinfo.object = this;
6612
6613 for (unsigned int i = 1; i < shnum; ++i)
6614 {
6615 Arm_input_section<big_endian>* arm_input_section =
6616 arm_target->find_arm_input_section(this, i);
6617
6618 if (arm_input_section != NULL
6619 && arm_input_section->is_stub_table_owner()
6620 && !arm_input_section->stub_table()->empty())
6621 {
6622 // We cannot discard a section if it owns a stub table.
6623 Output_section* os = this->output_section(i);
6624 gold_assert(os != NULL);
6625
6626 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6627 relinfo.reloc_shdr = NULL;
6628 relinfo.data_shndx = i;
6629 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6630
6631 gold_assert((*pviews)[i].view != NULL);
6632
6633 // We are passed the output section view. Adjust it to cover the
6634 // stub table only.
6635 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6636 gold_assert((stub_table->address() >= (*pviews)[i].address)
6637 && ((stub_table->address() + stub_table->data_size())
6638 <= (*pviews)[i].address + (*pviews)[i].view_size));
6639
6640 off_t offset = stub_table->address() - (*pviews)[i].address;
6641 unsigned char* view = (*pviews)[i].view + offset;
6642 Arm_address address = stub_table->address();
6643 section_size_type view_size = stub_table->data_size();
6644
6645 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6646 view_size);
6647 }
6648
6649 // Apply Cortex A8 workaround if applicable.
6650 if (this->section_has_cortex_a8_workaround(i))
6651 {
6652 unsigned char* view = (*pviews)[i].view;
6653 Arm_address view_address = (*pviews)[i].address;
6654 section_size_type view_size = (*pviews)[i].view_size;
6655 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6656
6657 // Adjust view to cover section.
6658 Output_section* os = this->output_section(i);
6659 gold_assert(os != NULL);
6660 Arm_address section_address =
6661 this->simple_input_section_output_address(i, os);
6662 uint64_t section_size = this->section_size(i);
6663
6664 gold_assert(section_address >= view_address
6665 && ((section_address + section_size)
6666 <= (view_address + view_size)));
6667
6668 unsigned char* section_view = view + (section_address - view_address);
6669
6670 // Apply the Cortex-A8 workaround to the output address range
6671 // corresponding to this input section.
6672 stub_table->apply_cortex_a8_workaround_to_address_range(
6673 arm_target,
6674 section_view,
6675 section_address,
6676 section_size);
6677 }
6678 }
6679 }
6680
6681 // Find the linked text section of an EXIDX section by looking at the first
6682 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6683 // must be linked to its associated code section via the sh_link field of
6684 // its section header. However, some tools are broken and the link is not
6685 // always set. LD just drops such an EXIDX section silently, causing the
6686 // associated code not unwindabled. Here we try a little bit harder to
6687 // discover the linked code section.
6688 //
6689 // PSHDR points to the section header of a relocation section of an EXIDX
6690 // section. If we can find a linked text section, return true and
6691 // store the text section index in the location PSHNDX. Otherwise
6692 // return false.
6693
6694 template<bool big_endian>
6695 bool
find_linked_text_section(const unsigned char * pshdr,const unsigned char * psyms,unsigned int * pshndx)6696 Arm_relobj<big_endian>::find_linked_text_section(
6697 const unsigned char* pshdr,
6698 const unsigned char* psyms,
6699 unsigned int* pshndx)
6700 {
6701 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6702
6703 // If there is no relocation, we cannot find the linked text section.
6704 size_t reloc_size;
6705 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6706 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6707 else
6708 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6709 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6710
6711 // Get the relocations.
6712 const unsigned char* prelocs =
6713 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6714
6715 // Find the REL31 relocation for the first word of the first EXIDX entry.
6716 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6717 {
6718 Arm_address r_offset;
6719 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6720 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6721 {
6722 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6723 r_info = reloc.get_r_info();
6724 r_offset = reloc.get_r_offset();
6725 }
6726 else
6727 {
6728 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6729 r_info = reloc.get_r_info();
6730 r_offset = reloc.get_r_offset();
6731 }
6732
6733 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6734 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6735 continue;
6736
6737 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6738 if (r_sym == 0
6739 || r_sym >= this->local_symbol_count()
6740 || r_offset != 0)
6741 continue;
6742
6743 // This is the relocation for the first word of the first EXIDX entry.
6744 // We expect to see a local section symbol.
6745 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6746 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6747 if (sym.get_st_type() == elfcpp::STT_SECTION)
6748 {
6749 bool is_ordinary;
6750 *pshndx =
6751 this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6752 gold_assert(is_ordinary);
6753 return true;
6754 }
6755 else
6756 return false;
6757 }
6758
6759 return false;
6760 }
6761
6762 // Make an EXIDX input section object for an EXIDX section whose index is
6763 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6764 // is the section index of the linked text section.
6765
6766 template<bool big_endian>
6767 void
make_exidx_input_section(unsigned int shndx,const elfcpp::Shdr<32,big_endian> & shdr,unsigned int text_shndx,const elfcpp::Shdr<32,big_endian> & text_shdr)6768 Arm_relobj<big_endian>::make_exidx_input_section(
6769 unsigned int shndx,
6770 const elfcpp::Shdr<32, big_endian>& shdr,
6771 unsigned int text_shndx,
6772 const elfcpp::Shdr<32, big_endian>& text_shdr)
6773 {
6774 // Create an Arm_exidx_input_section object for this EXIDX section.
6775 Arm_exidx_input_section* exidx_input_section =
6776 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6777 shdr.get_sh_addralign(),
6778 text_shdr.get_sh_size());
6779
6780 gold_assert(this->exidx_section_map_[shndx] == NULL);
6781 this->exidx_section_map_[shndx] = exidx_input_section;
6782
6783 if (text_shndx == elfcpp::SHN_UNDEF || text_shndx >= this->shnum())
6784 {
6785 gold_error(_("EXIDX section %s(%u) links to invalid section %u in %s"),
6786 this->section_name(shndx).c_str(), shndx, text_shndx,
6787 this->name().c_str());
6788 exidx_input_section->set_has_errors();
6789 }
6790 else if (this->exidx_section_map_[text_shndx] != NULL)
6791 {
6792 unsigned other_exidx_shndx =
6793 this->exidx_section_map_[text_shndx]->shndx();
6794 gold_error(_("EXIDX sections %s(%u) and %s(%u) both link to text section"
6795 "%s(%u) in %s"),
6796 this->section_name(shndx).c_str(), shndx,
6797 this->section_name(other_exidx_shndx).c_str(),
6798 other_exidx_shndx, this->section_name(text_shndx).c_str(),
6799 text_shndx, this->name().c_str());
6800 exidx_input_section->set_has_errors();
6801 }
6802 else
6803 this->exidx_section_map_[text_shndx] = exidx_input_section;
6804
6805 // Check section flags of text section.
6806 if ((text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
6807 {
6808 gold_error(_("EXIDX section %s(%u) links to non-allocated section %s(%u) "
6809 " in %s"),
6810 this->section_name(shndx).c_str(), shndx,
6811 this->section_name(text_shndx).c_str(), text_shndx,
6812 this->name().c_str());
6813 exidx_input_section->set_has_errors();
6814 }
6815 else if ((text_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0)
6816 // I would like to make this an error but currently ld just ignores
6817 // this.
6818 gold_warning(_("EXIDX section %s(%u) links to non-executable section "
6819 "%s(%u) in %s"),
6820 this->section_name(shndx).c_str(), shndx,
6821 this->section_name(text_shndx).c_str(), text_shndx,
6822 this->name().c_str());
6823 }
6824
6825 // Read the symbol information.
6826
6827 template<bool big_endian>
6828 void
do_read_symbols(Read_symbols_data * sd)6829 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6830 {
6831 // Call parent class to read symbol information.
6832 this->base_read_symbols(sd);
6833
6834 // If this input file is a binary file, it has no processor
6835 // specific flags and attributes section.
6836 Input_file::Format format = this->input_file()->format();
6837 if (format != Input_file::FORMAT_ELF)
6838 {
6839 gold_assert(format == Input_file::FORMAT_BINARY);
6840 this->merge_flags_and_attributes_ = false;
6841 return;
6842 }
6843
6844 // Read processor-specific flags in ELF file header.
6845 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6846 elfcpp::Elf_sizes<32>::ehdr_size,
6847 true, false);
6848 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6849 this->processor_specific_flags_ = ehdr.get_e_flags();
6850
6851 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6852 // sections.
6853 std::vector<unsigned int> deferred_exidx_sections;
6854 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6855 const unsigned char* pshdrs = sd->section_headers->data();
6856 const unsigned char* ps = pshdrs + shdr_size;
6857 bool must_merge_flags_and_attributes = false;
6858 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6859 {
6860 elfcpp::Shdr<32, big_endian> shdr(ps);
6861
6862 // Sometimes an object has no contents except the section name string
6863 // table and an empty symbol table with the undefined symbol. We
6864 // don't want to merge processor-specific flags from such an object.
6865 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6866 {
6867 // Symbol table is not empty.
6868 const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6869 elfcpp::Elf_sizes<32>::sym_size;
6870 if (shdr.get_sh_size() > sym_size)
6871 must_merge_flags_and_attributes = true;
6872 }
6873 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6874 // If this is neither an empty symbol table nor a string table,
6875 // be conservative.
6876 must_merge_flags_and_attributes = true;
6877
6878 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6879 {
6880 gold_assert(this->attributes_section_data_ == NULL);
6881 section_offset_type section_offset = shdr.get_sh_offset();
6882 section_size_type section_size =
6883 convert_to_section_size_type(shdr.get_sh_size());
6884 const unsigned char* view =
6885 this->get_view(section_offset, section_size, true, false);
6886 this->attributes_section_data_ =
6887 new Attributes_section_data(view, section_size);
6888 }
6889 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6890 {
6891 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6892 if (text_shndx == elfcpp::SHN_UNDEF)
6893 deferred_exidx_sections.push_back(i);
6894 else
6895 {
6896 elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6897 + text_shndx * shdr_size);
6898 this->make_exidx_input_section(i, shdr, text_shndx, text_shdr);
6899 }
6900 // EHABI 4.4.1 requires that SHF_LINK_ORDER flag to be set.
6901 if ((shdr.get_sh_flags() & elfcpp::SHF_LINK_ORDER) == 0)
6902 gold_warning(_("SHF_LINK_ORDER not set in EXIDX section %s of %s"),
6903 this->section_name(i).c_str(), this->name().c_str());
6904 }
6905 }
6906
6907 // This is rare.
6908 if (!must_merge_flags_and_attributes)
6909 {
6910 gold_assert(deferred_exidx_sections.empty());
6911 this->merge_flags_and_attributes_ = false;
6912 return;
6913 }
6914
6915 // Some tools are broken and they do not set the link of EXIDX sections.
6916 // We look at the first relocation to figure out the linked sections.
6917 if (!deferred_exidx_sections.empty())
6918 {
6919 // We need to go over the section headers again to find the mapping
6920 // from sections being relocated to their relocation sections. This is
6921 // a bit inefficient as we could do that in the loop above. However,
6922 // we do not expect any deferred EXIDX sections normally. So we do not
6923 // want to slow down the most common path.
6924 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6925 Reloc_map reloc_map;
6926 ps = pshdrs + shdr_size;
6927 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6928 {
6929 elfcpp::Shdr<32, big_endian> shdr(ps);
6930 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6931 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6932 {
6933 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6934 if (info_shndx >= this->shnum())
6935 gold_error(_("relocation section %u has invalid info %u"),
6936 i, info_shndx);
6937 Reloc_map::value_type value(info_shndx, i);
6938 std::pair<Reloc_map::iterator, bool> result =
6939 reloc_map.insert(value);
6940 if (!result.second)
6941 gold_error(_("section %u has multiple relocation sections "
6942 "%u and %u"),
6943 info_shndx, i, reloc_map[info_shndx]);
6944 }
6945 }
6946
6947 // Read the symbol table section header.
6948 const unsigned int symtab_shndx = this->symtab_shndx();
6949 elfcpp::Shdr<32, big_endian>
6950 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6951 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6952
6953 // Read the local symbols.
6954 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6955 const unsigned int loccount = this->local_symbol_count();
6956 gold_assert(loccount == symtabshdr.get_sh_info());
6957 off_t locsize = loccount * sym_size;
6958 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6959 locsize, true, true);
6960
6961 // Process the deferred EXIDX sections.
6962 for (unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6963 {
6964 unsigned int shndx = deferred_exidx_sections[i];
6965 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6966 unsigned int text_shndx = elfcpp::SHN_UNDEF;
6967 Reloc_map::const_iterator it = reloc_map.find(shndx);
6968 if (it != reloc_map.end())
6969 find_linked_text_section(pshdrs + it->second * shdr_size,
6970 psyms, &text_shndx);
6971 elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6972 + text_shndx * shdr_size);
6973 this->make_exidx_input_section(shndx, shdr, text_shndx, text_shdr);
6974 }
6975 }
6976 }
6977
6978 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6979 // sections for unwinding. These sections are referenced implicitly by
6980 // text sections linked in the section headers. If we ignore these implicit
6981 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6982 // will be garbage-collected incorrectly. Hence we override the same function
6983 // in the base class to handle these implicit references.
6984
6985 template<bool big_endian>
6986 void
do_gc_process_relocs(Symbol_table * symtab,Layout * layout,Read_relocs_data * rd)6987 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6988 Layout* layout,
6989 Read_relocs_data* rd)
6990 {
6991 // First, call base class method to process relocations in this object.
6992 Sized_relobj_file<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6993
6994 // If --gc-sections is not specified, there is nothing more to do.
6995 // This happens when --icf is used but --gc-sections is not.
6996 if (!parameters->options().gc_sections())
6997 return;
6998
6999 unsigned int shnum = this->shnum();
7000 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
7001 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
7002 shnum * shdr_size,
7003 true, true);
7004
7005 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
7006 // to these from the linked text sections.
7007 const unsigned char* ps = pshdrs + shdr_size;
7008 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
7009 {
7010 elfcpp::Shdr<32, big_endian> shdr(ps);
7011 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
7012 {
7013 // Found an .ARM.exidx section, add it to the set of reachable
7014 // sections from its linked text section.
7015 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
7016 symtab->gc()->add_reference(this, text_shndx, this, i);
7017 }
7018 }
7019 }
7020
7021 // Update output local symbol count. Owing to EXIDX entry merging, some local
7022 // symbols will be removed in output. Adjust output local symbol count
7023 // accordingly. We can only changed the static output local symbol count. It
7024 // is too late to change the dynamic symbols.
7025
7026 template<bool big_endian>
7027 void
update_output_local_symbol_count()7028 Arm_relobj<big_endian>::update_output_local_symbol_count()
7029 {
7030 // Caller should check that this needs updating. We want caller checking
7031 // because output_local_symbol_count_needs_update() is most likely inlined.
7032 gold_assert(this->output_local_symbol_count_needs_update_);
7033
7034 gold_assert(this->symtab_shndx() != -1U);
7035 if (this->symtab_shndx() == 0)
7036 {
7037 // This object has no symbols. Weird but legal.
7038 return;
7039 }
7040
7041 // Read the symbol table section header.
7042 const unsigned int symtab_shndx = this->symtab_shndx();
7043 elfcpp::Shdr<32, big_endian>
7044 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
7045 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
7046
7047 // Read the local symbols.
7048 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
7049 const unsigned int loccount = this->local_symbol_count();
7050 gold_assert(loccount == symtabshdr.get_sh_info());
7051 off_t locsize = loccount * sym_size;
7052 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
7053 locsize, true, true);
7054
7055 // Loop over the local symbols.
7056
7057 typedef typename Sized_relobj_file<32, big_endian>::Output_sections
7058 Output_sections;
7059 const Output_sections& out_sections(this->output_sections());
7060 unsigned int shnum = this->shnum();
7061 unsigned int count = 0;
7062 // Skip the first, dummy, symbol.
7063 psyms += sym_size;
7064 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
7065 {
7066 elfcpp::Sym<32, big_endian> sym(psyms);
7067
7068 Symbol_value<32>& lv((*this->local_values())[i]);
7069
7070 // This local symbol was already discarded by do_count_local_symbols.
7071 if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
7072 continue;
7073
7074 bool is_ordinary;
7075 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
7076 &is_ordinary);
7077
7078 if (shndx < shnum)
7079 {
7080 Output_section* os = out_sections[shndx];
7081
7082 // This local symbol no longer has an output section. Discard it.
7083 if (os == NULL)
7084 {
7085 lv.set_no_output_symtab_entry();
7086 continue;
7087 }
7088
7089 // Currently we only discard parts of EXIDX input sections.
7090 // We explicitly check for a merged EXIDX input section to avoid
7091 // calling Output_section_data::output_offset unless necessary.
7092 if ((this->get_output_section_offset(shndx) == invalid_address)
7093 && (this->exidx_input_section_by_shndx(shndx) != NULL))
7094 {
7095 section_offset_type output_offset =
7096 os->output_offset(this, shndx, lv.input_value());
7097 if (output_offset == -1)
7098 {
7099 // This symbol is defined in a part of an EXIDX input section
7100 // that is discarded due to entry merging.
7101 lv.set_no_output_symtab_entry();
7102 continue;
7103 }
7104 }
7105 }
7106
7107 ++count;
7108 }
7109
7110 this->set_output_local_symbol_count(count);
7111 this->output_local_symbol_count_needs_update_ = false;
7112 }
7113
7114 // Arm_dynobj methods.
7115
7116 // Read the symbol information.
7117
7118 template<bool big_endian>
7119 void
do_read_symbols(Read_symbols_data * sd)7120 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
7121 {
7122 // Call parent class to read symbol information.
7123 this->base_read_symbols(sd);
7124
7125 // Read processor-specific flags in ELF file header.
7126 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
7127 elfcpp::Elf_sizes<32>::ehdr_size,
7128 true, false);
7129 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
7130 this->processor_specific_flags_ = ehdr.get_e_flags();
7131
7132 // Read the attributes section if there is one.
7133 // We read from the end because gas seems to put it near the end of
7134 // the section headers.
7135 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
7136 const unsigned char* ps =
7137 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
7138 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
7139 {
7140 elfcpp::Shdr<32, big_endian> shdr(ps);
7141 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
7142 {
7143 section_offset_type section_offset = shdr.get_sh_offset();
7144 section_size_type section_size =
7145 convert_to_section_size_type(shdr.get_sh_size());
7146 const unsigned char* view =
7147 this->get_view(section_offset, section_size, true, false);
7148 this->attributes_section_data_ =
7149 new Attributes_section_data(view, section_size);
7150 break;
7151 }
7152 }
7153 }
7154
7155 // Stub_addend_reader methods.
7156
7157 // Read the addend of a REL relocation of type R_TYPE at VIEW.
7158
7159 template<bool big_endian>
7160 elfcpp::Elf_types<32>::Elf_Swxword
operator ()(unsigned int r_type,const unsigned char * view,const typename Reloc_types<elfcpp::SHT_REL,32,big_endian>::Reloc &) const7161 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
7162 unsigned int r_type,
7163 const unsigned char* view,
7164 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
7165 {
7166 typedef class Arm_relocate_functions<big_endian> RelocFuncs;
7167
7168 switch (r_type)
7169 {
7170 case elfcpp::R_ARM_CALL:
7171 case elfcpp::R_ARM_JUMP24:
7172 case elfcpp::R_ARM_PLT32:
7173 {
7174 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7175 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7176 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
7177 return Bits<26>::sign_extend32(val << 2);
7178 }
7179
7180 case elfcpp::R_ARM_THM_CALL:
7181 case elfcpp::R_ARM_THM_JUMP24:
7182 case elfcpp::R_ARM_THM_XPC22:
7183 {
7184 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7185 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7186 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
7187 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
7188 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
7189 }
7190
7191 case elfcpp::R_ARM_THM_JUMP19:
7192 {
7193 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7194 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7195 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
7196 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
7197 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
7198 }
7199
7200 default:
7201 gold_unreachable();
7202 }
7203 }
7204
7205 // Arm_output_data_got methods.
7206
7207 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
7208 // The first one is initialized to be 1, which is the module index for
7209 // the main executable and the second one 0. A reloc of the type
7210 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
7211 // be applied by gold. GSYM is a global symbol.
7212 //
7213 template<bool big_endian>
7214 void
add_tls_gd32_with_static_reloc(unsigned int got_type,Symbol * gsym)7215 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
7216 unsigned int got_type,
7217 Symbol* gsym)
7218 {
7219 if (gsym->has_got_offset(got_type))
7220 return;
7221
7222 // We are doing a static link. Just mark it as belong to module 1,
7223 // the executable.
7224 unsigned int got_offset = this->add_constant(1);
7225 gsym->set_got_offset(got_type, got_offset);
7226 got_offset = this->add_constant(0);
7227 this->static_relocs_.push_back(Static_reloc(got_offset,
7228 elfcpp::R_ARM_TLS_DTPOFF32,
7229 gsym));
7230 }
7231
7232 // Same as the above but for a local symbol.
7233
7234 template<bool big_endian>
7235 void
add_tls_gd32_with_static_reloc(unsigned int got_type,Sized_relobj_file<32,big_endian> * object,unsigned int index)7236 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
7237 unsigned int got_type,
7238 Sized_relobj_file<32, big_endian>* object,
7239 unsigned int index)
7240 {
7241 if (object->local_has_got_offset(index, got_type))
7242 return;
7243
7244 // We are doing a static link. Just mark it as belong to module 1,
7245 // the executable.
7246 unsigned int got_offset = this->add_constant(1);
7247 object->set_local_got_offset(index, got_type, got_offset);
7248 got_offset = this->add_constant(0);
7249 this->static_relocs_.push_back(Static_reloc(got_offset,
7250 elfcpp::R_ARM_TLS_DTPOFF32,
7251 object, index));
7252 }
7253
7254 template<bool big_endian>
7255 void
do_write(Output_file * of)7256 Arm_output_data_got<big_endian>::do_write(Output_file* of)
7257 {
7258 // Call parent to write out GOT.
7259 Output_data_got<32, big_endian>::do_write(of);
7260
7261 // We are done if there is no fix up.
7262 if (this->static_relocs_.empty())
7263 return;
7264
7265 gold_assert(parameters->doing_static_link());
7266
7267 const off_t offset = this->offset();
7268 const section_size_type oview_size =
7269 convert_to_section_size_type(this->data_size());
7270 unsigned char* const oview = of->get_output_view(offset, oview_size);
7271
7272 Output_segment* tls_segment = this->layout_->tls_segment();
7273 gold_assert(tls_segment != NULL);
7274
7275 // The thread pointer $tp points to the TCB, which is followed by the
7276 // TLS. So we need to adjust $tp relative addressing by this amount.
7277 Arm_address aligned_tcb_size =
7278 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
7279
7280 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
7281 {
7282 Static_reloc& reloc(this->static_relocs_[i]);
7283
7284 Arm_address value;
7285 if (!reloc.symbol_is_global())
7286 {
7287 Sized_relobj_file<32, big_endian>* object = reloc.relobj();
7288 const Symbol_value<32>* psymval =
7289 reloc.relobj()->local_symbol(reloc.index());
7290
7291 // We are doing static linking. Issue an error and skip this
7292 // relocation if the symbol is undefined or in a discarded_section.
7293 bool is_ordinary;
7294 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7295 if ((shndx == elfcpp::SHN_UNDEF)
7296 || (is_ordinary
7297 && shndx != elfcpp::SHN_UNDEF
7298 && !object->is_section_included(shndx)
7299 && !this->symbol_table_->is_section_folded(object, shndx)))
7300 {
7301 gold_error(_("undefined or discarded local symbol %u from "
7302 " object %s in GOT"),
7303 reloc.index(), reloc.relobj()->name().c_str());
7304 continue;
7305 }
7306
7307 value = psymval->value(object, 0);
7308 }
7309 else
7310 {
7311 const Symbol* gsym = reloc.symbol();
7312 gold_assert(gsym != NULL);
7313 if (gsym->is_forwarder())
7314 gsym = this->symbol_table_->resolve_forwards(gsym);
7315
7316 // We are doing static linking. Issue an error and skip this
7317 // relocation if the symbol is undefined or in a discarded_section
7318 // unless it is a weakly_undefined symbol.
7319 if ((gsym->is_defined_in_discarded_section()
7320 || gsym->is_undefined())
7321 && !gsym->is_weak_undefined())
7322 {
7323 gold_error(_("undefined or discarded symbol %s in GOT"),
7324 gsym->name());
7325 continue;
7326 }
7327
7328 if (!gsym->is_weak_undefined())
7329 {
7330 const Sized_symbol<32>* sym =
7331 static_cast<const Sized_symbol<32>*>(gsym);
7332 value = sym->value();
7333 }
7334 else
7335 value = 0;
7336 }
7337
7338 unsigned got_offset = reloc.got_offset();
7339 gold_assert(got_offset < oview_size);
7340
7341 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7342 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
7343 Valtype x;
7344 switch (reloc.r_type())
7345 {
7346 case elfcpp::R_ARM_TLS_DTPOFF32:
7347 x = value;
7348 break;
7349 case elfcpp::R_ARM_TLS_TPOFF32:
7350 x = value + aligned_tcb_size;
7351 break;
7352 default:
7353 gold_unreachable();
7354 }
7355 elfcpp::Swap<32, big_endian>::writeval(wv, x);
7356 }
7357
7358 of->write_output_view(offset, oview_size, oview);
7359 }
7360
7361 // A class to handle the PLT data.
7362 // This is an abstract base class that handles most of the linker details
7363 // but does not know the actual contents of PLT entries. The derived
7364 // classes below fill in those details.
7365
7366 template<bool big_endian>
7367 class Output_data_plt_arm : public Output_section_data
7368 {
7369 public:
7370 // Unlike aarch64, which records symbol value in "addend" field of relocations
7371 // and could be done at the same time an IRelative reloc is created for the
7372 // symbol, arm puts the symbol value into "GOT" table, which, however, is
7373 // issued later in Output_data_plt_arm::do_write(). So we have a struct here
7374 // to keep necessary symbol information for later use in do_write. We usually
7375 // have only a very limited number of ifuncs, so the extra data required here
7376 // is also limited.
7377
7378 struct IRelative_data
7379 {
IRelative_data__anon49345ebd0111::Output_data_plt_arm::IRelative_data7380 IRelative_data(Sized_symbol<32>* sized_symbol)
7381 : symbol_is_global_(true)
7382 {
7383 u_.global = sized_symbol;
7384 }
7385
IRelative_data__anon49345ebd0111::Output_data_plt_arm::IRelative_data7386 IRelative_data(Sized_relobj_file<32, big_endian>* relobj,
7387 unsigned int index)
7388 : symbol_is_global_(false)
7389 {
7390 u_.local.relobj = relobj;
7391 u_.local.index = index;
7392 }
7393
7394 union
7395 {
7396 Sized_symbol<32>* global;
7397
7398 struct
7399 {
7400 Sized_relobj_file<32, big_endian>* relobj;
7401 unsigned int index;
7402 } local;
7403 } u_;
7404
7405 bool symbol_is_global_;
7406 };
7407
7408 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
7409 Reloc_section;
7410
7411 Output_data_plt_arm(Layout* layout, uint64_t addralign,
7412 Arm_output_data_got<big_endian>* got,
7413 Output_data_space* got_plt,
7414 Output_data_space* got_irelative);
7415
7416 // Add an entry to the PLT.
7417 void
7418 add_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym);
7419
7420 // Add the relocation for a plt entry.
7421 void
7422 add_relocation(Symbol_table* symtab, Layout* layout,
7423 Symbol* gsym, unsigned int got_offset);
7424
7425 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
7426 unsigned int
7427 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
7428 Sized_relobj_file<32, big_endian>* relobj,
7429 unsigned int local_sym_index);
7430
7431 // Return the .rel.plt section data.
7432 const Reloc_section*
rel_plt() const7433 rel_plt() const
7434 { return this->rel_; }
7435
7436 // Return the PLT relocation container for IRELATIVE.
7437 Reloc_section*
7438 rel_irelative(Symbol_table*, Layout*);
7439
7440 // Return the number of PLT entries.
7441 unsigned int
entry_count() const7442 entry_count() const
7443 { return this->count_ + this->irelative_count_; }
7444
7445 // Return the offset of the first non-reserved PLT entry.
7446 unsigned int
first_plt_entry_offset() const7447 first_plt_entry_offset() const
7448 { return this->do_first_plt_entry_offset(); }
7449
7450 // Return the size of a PLT entry.
7451 unsigned int
get_plt_entry_size() const7452 get_plt_entry_size() const
7453 { return this->do_get_plt_entry_size(); }
7454
7455 // Return the PLT address for globals.
7456 uint32_t
7457 address_for_global(const Symbol*) const;
7458
7459 // Return the PLT address for locals.
7460 uint32_t
7461 address_for_local(const Relobj*, unsigned int symndx) const;
7462
7463 protected:
7464 // Fill in the first PLT entry.
7465 void
fill_first_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address)7466 fill_first_plt_entry(unsigned char* pov,
7467 Arm_address got_address,
7468 Arm_address plt_address)
7469 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
7470
7471 void
fill_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address,unsigned int got_offset,unsigned int plt_offset)7472 fill_plt_entry(unsigned char* pov,
7473 Arm_address got_address,
7474 Arm_address plt_address,
7475 unsigned int got_offset,
7476 unsigned int plt_offset)
7477 { do_fill_plt_entry(pov, got_address, plt_address, got_offset, plt_offset); }
7478
7479 virtual unsigned int
7480 do_first_plt_entry_offset() const = 0;
7481
7482 virtual unsigned int
7483 do_get_plt_entry_size() const = 0;
7484
7485 virtual void
7486 do_fill_first_plt_entry(unsigned char* pov,
7487 Arm_address got_address,
7488 Arm_address plt_address) = 0;
7489
7490 virtual void
7491 do_fill_plt_entry(unsigned char* pov,
7492 Arm_address got_address,
7493 Arm_address plt_address,
7494 unsigned int got_offset,
7495 unsigned int plt_offset) = 0;
7496
7497 void
7498 do_adjust_output_section(Output_section* os);
7499
7500 // Write to a map file.
7501 void
do_print_to_mapfile(Mapfile * mapfile) const7502 do_print_to_mapfile(Mapfile* mapfile) const
7503 { mapfile->print_output_data(this, _("** PLT")); }
7504
7505 private:
7506 // Set the final size.
7507 void
set_final_data_size()7508 set_final_data_size()
7509 {
7510 this->set_data_size(this->first_plt_entry_offset()
7511 + ((this->count_ + this->irelative_count_)
7512 * this->get_plt_entry_size()));
7513 }
7514
7515 // Write out the PLT data.
7516 void
7517 do_write(Output_file*);
7518
7519 // Record irelative symbol data.
insert_irelative_data(const IRelative_data & idata)7520 void insert_irelative_data(const IRelative_data& idata)
7521 { irelative_data_vec_.push_back(idata); }
7522
7523 // The reloc section.
7524 Reloc_section* rel_;
7525 // The IRELATIVE relocs, if necessary. These must follow the
7526 // regular PLT relocations.
7527 Reloc_section* irelative_rel_;
7528 // The .got section.
7529 Arm_output_data_got<big_endian>* got_;
7530 // The .got.plt section.
7531 Output_data_space* got_plt_;
7532 // The part of the .got.plt section used for IRELATIVE relocs.
7533 Output_data_space* got_irelative_;
7534 // The number of PLT entries.
7535 unsigned int count_;
7536 // Number of PLT entries with R_ARM_IRELATIVE relocs. These
7537 // follow the regular PLT entries.
7538 unsigned int irelative_count_;
7539 // Vector for irelative data.
7540 typedef std::vector<IRelative_data> IRelative_data_vec;
7541 IRelative_data_vec irelative_data_vec_;
7542 };
7543
7544 // Create the PLT section. The ordinary .got section is an argument,
7545 // since we need to refer to the start. We also create our own .got
7546 // section just for PLT entries.
7547
7548 template<bool big_endian>
Output_data_plt_arm(Layout * layout,uint64_t addralign,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)7549 Output_data_plt_arm<big_endian>::Output_data_plt_arm(
7550 Layout* layout, uint64_t addralign,
7551 Arm_output_data_got<big_endian>* got,
7552 Output_data_space* got_plt,
7553 Output_data_space* got_irelative)
7554 : Output_section_data(addralign), irelative_rel_(NULL),
7555 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
7556 count_(0), irelative_count_(0)
7557 {
7558 this->rel_ = new Reloc_section(false);
7559 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
7560 elfcpp::SHF_ALLOC, this->rel_,
7561 ORDER_DYNAMIC_PLT_RELOCS, false);
7562 }
7563
7564 template<bool big_endian>
7565 void
do_adjust_output_section(Output_section * os)7566 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
7567 {
7568 os->set_entsize(0);
7569 }
7570
7571 // Add an entry to the PLT.
7572
7573 template<bool big_endian>
7574 void
add_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)7575 Output_data_plt_arm<big_endian>::add_entry(Symbol_table* symtab,
7576 Layout* layout,
7577 Symbol* gsym)
7578 {
7579 gold_assert(!gsym->has_plt_offset());
7580
7581 unsigned int* entry_count;
7582 Output_section_data_build* got;
7583
7584 // We have 2 different types of plt entry here, normal and ifunc.
7585
7586 // For normal plt, the offset begins with first_plt_entry_offset(20), and the
7587 // 1st entry offset would be 20, the second 32, third 44 ... etc.
7588
7589 // For ifunc plt, the offset begins with 0. So the first offset would 0,
7590 // second 12, third 24 ... etc.
7591
7592 // IFunc plt entries *always* come after *normal* plt entries.
7593
7594 // Notice, when computing the plt address of a certain symbol, "plt_address +
7595 // plt_offset" is no longer correct. Use target->plt_address_for_global() or
7596 // target->plt_address_for_local() instead.
7597
7598 int begin_offset = 0;
7599 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7600 && gsym->can_use_relative_reloc(false))
7601 {
7602 entry_count = &this->irelative_count_;
7603 got = this->got_irelative_;
7604 // For irelative plt entries, offset is relative to the end of normal plt
7605 // entries, so it starts from 0.
7606 begin_offset = 0;
7607 // Record symbol information.
7608 this->insert_irelative_data(
7609 IRelative_data(symtab->get_sized_symbol<32>(gsym)));
7610 }
7611 else
7612 {
7613 entry_count = &this->count_;
7614 got = this->got_plt_;
7615 // Note that for normal plt entries, when setting the PLT offset we skip
7616 // the initial reserved PLT entry.
7617 begin_offset = this->first_plt_entry_offset();
7618 }
7619
7620 gsym->set_plt_offset(begin_offset
7621 + (*entry_count) * this->get_plt_entry_size());
7622
7623 ++(*entry_count);
7624
7625 section_offset_type got_offset = got->current_data_size();
7626
7627 // Every PLT entry needs a GOT entry which points back to the PLT
7628 // entry (this will be changed by the dynamic linker, normally
7629 // lazily when the function is called).
7630 got->set_current_data_size(got_offset + 4);
7631
7632 // Every PLT entry needs a reloc.
7633 this->add_relocation(symtab, layout, gsym, got_offset);
7634
7635 // Note that we don't need to save the symbol. The contents of the
7636 // PLT are independent of which symbols are used. The symbols only
7637 // appear in the relocations.
7638 }
7639
7640 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
7641 // the PLT offset.
7642
7643 template<bool big_endian>
7644 unsigned int
add_local_ifunc_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * relobj,unsigned int local_sym_index)7645 Output_data_plt_arm<big_endian>::add_local_ifunc_entry(
7646 Symbol_table* symtab,
7647 Layout* layout,
7648 Sized_relobj_file<32, big_endian>* relobj,
7649 unsigned int local_sym_index)
7650 {
7651 this->insert_irelative_data(IRelative_data(relobj, local_sym_index));
7652
7653 // Notice, when computingthe plt entry address, "plt_address + plt_offset" is
7654 // no longer correct. Use target->plt_address_for_local() instead.
7655 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
7656 ++this->irelative_count_;
7657
7658 section_offset_type got_offset = this->got_irelative_->current_data_size();
7659
7660 // Every PLT entry needs a GOT entry which points back to the PLT
7661 // entry.
7662 this->got_irelative_->set_current_data_size(got_offset + 4);
7663
7664
7665 // Every PLT entry needs a reloc.
7666 Reloc_section* rel = this->rel_irelative(symtab, layout);
7667 rel->add_symbolless_local_addend(relobj, local_sym_index,
7668 elfcpp::R_ARM_IRELATIVE,
7669 this->got_irelative_, got_offset);
7670 return plt_offset;
7671 }
7672
7673
7674 // Add the relocation for a PLT entry.
7675
7676 template<bool big_endian>
7677 void
add_relocation(Symbol_table * symtab,Layout * layout,Symbol * gsym,unsigned int got_offset)7678 Output_data_plt_arm<big_endian>::add_relocation(
7679 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
7680 {
7681 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7682 && gsym->can_use_relative_reloc(false))
7683 {
7684 Reloc_section* rel = this->rel_irelative(symtab, layout);
7685 rel->add_symbolless_global_addend(gsym, elfcpp::R_ARM_IRELATIVE,
7686 this->got_irelative_, got_offset);
7687 }
7688 else
7689 {
7690 gsym->set_needs_dynsym_entry();
7691 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
7692 got_offset);
7693 }
7694 }
7695
7696
7697 // Create the irelative relocation data.
7698
7699 template<bool big_endian>
7700 typename Output_data_plt_arm<big_endian>::Reloc_section*
rel_irelative(Symbol_table * symtab,Layout * layout)7701 Output_data_plt_arm<big_endian>::rel_irelative(Symbol_table* symtab,
7702 Layout* layout)
7703 {
7704 if (this->irelative_rel_ == NULL)
7705 {
7706 // Since irelative relocations goes into 'rel.dyn', we delegate the
7707 // creation of irelative_rel_ to where rel_dyn section gets created.
7708 Target_arm<big_endian>* arm_target =
7709 Target_arm<big_endian>::default_target();
7710 this->irelative_rel_ = arm_target->rel_irelative_section(layout);
7711
7712 // Make sure we have a place for the TLSDESC relocations, in
7713 // case we see any later on.
7714 // this->rel_tlsdesc(layout);
7715 if (parameters->doing_static_link())
7716 {
7717 // A statically linked executable will only have a .rel.plt section to
7718 // hold R_ARM_IRELATIVE relocs for STT_GNU_IFUNC symbols. The library
7719 // will use these symbols to locate the IRELATIVE relocs at program
7720 // startup time.
7721 symtab->define_in_output_data("__rel_iplt_start", NULL,
7722 Symbol_table::PREDEFINED,
7723 this->irelative_rel_, 0, 0,
7724 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7725 elfcpp::STV_HIDDEN, 0, false, true);
7726 symtab->define_in_output_data("__rel_iplt_end", NULL,
7727 Symbol_table::PREDEFINED,
7728 this->irelative_rel_, 0, 0,
7729 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7730 elfcpp::STV_HIDDEN, 0, true, true);
7731 }
7732 }
7733 return this->irelative_rel_;
7734 }
7735
7736
7737 // Return the PLT address for a global symbol.
7738
7739 template<bool big_endian>
7740 uint32_t
address_for_global(const Symbol * gsym) const7741 Output_data_plt_arm<big_endian>::address_for_global(const Symbol* gsym) const
7742 {
7743 uint64_t begin_offset = 0;
7744 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7745 && gsym->can_use_relative_reloc(false))
7746 {
7747 begin_offset = (this->first_plt_entry_offset() +
7748 this->count_ * this->get_plt_entry_size());
7749 }
7750 return this->address() + begin_offset + gsym->plt_offset();
7751 }
7752
7753
7754 // Return the PLT address for a local symbol. These are always
7755 // IRELATIVE relocs.
7756
7757 template<bool big_endian>
7758 uint32_t
address_for_local(const Relobj * object,unsigned int r_sym) const7759 Output_data_plt_arm<big_endian>::address_for_local(
7760 const Relobj* object,
7761 unsigned int r_sym) const
7762 {
7763 return (this->address()
7764 + this->first_plt_entry_offset()
7765 + this->count_ * this->get_plt_entry_size()
7766 + object->local_plt_offset(r_sym));
7767 }
7768
7769
7770 template<bool big_endian>
7771 class Output_data_plt_arm_standard : public Output_data_plt_arm<big_endian>
7772 {
7773 public:
Output_data_plt_arm_standard(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)7774 Output_data_plt_arm_standard(Layout* layout,
7775 Arm_output_data_got<big_endian>* got,
7776 Output_data_space* got_plt,
7777 Output_data_space* got_irelative)
7778 : Output_data_plt_arm<big_endian>(layout, 4, got, got_plt, got_irelative)
7779 { }
7780
7781 protected:
7782 // Return the offset of the first non-reserved PLT entry.
7783 virtual unsigned int
do_first_plt_entry_offset() const7784 do_first_plt_entry_offset() const
7785 { return sizeof(first_plt_entry); }
7786
7787 virtual void
7788 do_fill_first_plt_entry(unsigned char* pov,
7789 Arm_address got_address,
7790 Arm_address plt_address);
7791
7792 private:
7793 // Template for the first PLT entry.
7794 static const uint32_t first_plt_entry[5];
7795 };
7796
7797 // ARM PLTs.
7798 // FIXME: This is not very flexible. Right now this has only been tested
7799 // on armv5te. If we are to support additional architecture features like
7800 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7801
7802 // The first entry in the PLT.
7803 template<bool big_endian>
7804 const uint32_t Output_data_plt_arm_standard<big_endian>::first_plt_entry[5] =
7805 {
7806 0xe52de004, // str lr, [sp, #-4]!
7807 0xe59fe004, // ldr lr, [pc, #4]
7808 0xe08fe00e, // add lr, pc, lr
7809 0xe5bef008, // ldr pc, [lr, #8]!
7810 0x00000000, // &GOT[0] - .
7811 };
7812
7813 template<bool big_endian>
7814 void
do_fill_first_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address)7815 Output_data_plt_arm_standard<big_endian>::do_fill_first_plt_entry(
7816 unsigned char* pov,
7817 Arm_address got_address,
7818 Arm_address plt_address)
7819 {
7820 // Write first PLT entry. All but the last word are constants.
7821 const size_t num_first_plt_words = (sizeof(first_plt_entry)
7822 / sizeof(first_plt_entry[0]));
7823 for (size_t i = 0; i < num_first_plt_words - 1; i++)
7824 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7825 // Last word in first PLT entry is &GOT[0] - .
7826 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7827 got_address - (plt_address + 16));
7828 }
7829
7830 // Subsequent entries in the PLT.
7831 // This class generates short (12-byte) entries, for displacements up to 2^28.
7832
7833 template<bool big_endian>
7834 class Output_data_plt_arm_short : public Output_data_plt_arm_standard<big_endian>
7835 {
7836 public:
Output_data_plt_arm_short(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)7837 Output_data_plt_arm_short(Layout* layout,
7838 Arm_output_data_got<big_endian>* got,
7839 Output_data_space* got_plt,
7840 Output_data_space* got_irelative)
7841 : Output_data_plt_arm_standard<big_endian>(layout, got, got_plt, got_irelative)
7842 { }
7843
7844 protected:
7845 // Return the size of a PLT entry.
7846 virtual unsigned int
do_get_plt_entry_size() const7847 do_get_plt_entry_size() const
7848 { return sizeof(plt_entry); }
7849
7850 virtual void
7851 do_fill_plt_entry(unsigned char* pov,
7852 Arm_address got_address,
7853 Arm_address plt_address,
7854 unsigned int got_offset,
7855 unsigned int plt_offset);
7856
7857 private:
7858 // Template for subsequent PLT entries.
7859 static const uint32_t plt_entry[3];
7860 };
7861
7862 template<bool big_endian>
7863 const uint32_t Output_data_plt_arm_short<big_endian>::plt_entry[3] =
7864 {
7865 0xe28fc600, // add ip, pc, #0xNN00000
7866 0xe28cca00, // add ip, ip, #0xNN000
7867 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7868 };
7869
7870 template<bool big_endian>
7871 void
do_fill_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address,unsigned int got_offset,unsigned int plt_offset)7872 Output_data_plt_arm_short<big_endian>::do_fill_plt_entry(
7873 unsigned char* pov,
7874 Arm_address got_address,
7875 Arm_address plt_address,
7876 unsigned int got_offset,
7877 unsigned int plt_offset)
7878 {
7879 int32_t offset = ((got_address + got_offset)
7880 - (plt_address + plt_offset + 8));
7881 if (offset < 0 || offset > 0x0fffffff)
7882 gold_error(_("PLT offset too large, try linking with --long-plt"));
7883
7884 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7885 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7886 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7887 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7888 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7889 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7890 }
7891
7892 // This class generates long (16-byte) entries, for arbitrary displacements.
7893
7894 template<bool big_endian>
7895 class Output_data_plt_arm_long : public Output_data_plt_arm_standard<big_endian>
7896 {
7897 public:
Output_data_plt_arm_long(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)7898 Output_data_plt_arm_long(Layout* layout,
7899 Arm_output_data_got<big_endian>* got,
7900 Output_data_space* got_plt,
7901 Output_data_space* got_irelative)
7902 : Output_data_plt_arm_standard<big_endian>(layout, got, got_plt, got_irelative)
7903 { }
7904
7905 protected:
7906 // Return the size of a PLT entry.
7907 virtual unsigned int
do_get_plt_entry_size() const7908 do_get_plt_entry_size() const
7909 { return sizeof(plt_entry); }
7910
7911 virtual void
7912 do_fill_plt_entry(unsigned char* pov,
7913 Arm_address got_address,
7914 Arm_address plt_address,
7915 unsigned int got_offset,
7916 unsigned int plt_offset);
7917
7918 private:
7919 // Template for subsequent PLT entries.
7920 static const uint32_t plt_entry[4];
7921 };
7922
7923 template<bool big_endian>
7924 const uint32_t Output_data_plt_arm_long<big_endian>::plt_entry[4] =
7925 {
7926 0xe28fc200, // add ip, pc, #0xN0000000
7927 0xe28cc600, // add ip, ip, #0xNN00000
7928 0xe28cca00, // add ip, ip, #0xNN000
7929 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7930 };
7931
7932 template<bool big_endian>
7933 void
do_fill_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address,unsigned int got_offset,unsigned int plt_offset)7934 Output_data_plt_arm_long<big_endian>::do_fill_plt_entry(
7935 unsigned char* pov,
7936 Arm_address got_address,
7937 Arm_address plt_address,
7938 unsigned int got_offset,
7939 unsigned int plt_offset)
7940 {
7941 int32_t offset = ((got_address + got_offset)
7942 - (plt_address + plt_offset + 8));
7943
7944 uint32_t plt_insn0 = plt_entry[0] | (offset >> 28);
7945 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7946 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 20) & 0xff);
7947 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7948 uint32_t plt_insn2 = plt_entry[2] | ((offset >> 12) & 0xff);
7949 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7950 uint32_t plt_insn3 = plt_entry[3] | (offset & 0xfff);
7951 elfcpp::Swap<32, big_endian>::writeval(pov + 12, plt_insn3);
7952 }
7953
7954 // Write out the PLT. This uses the hand-coded instructions above,
7955 // and adjusts them as needed. This is all specified by the arm ELF
7956 // Processor Supplement.
7957
7958 template<bool big_endian>
7959 void
do_write(Output_file * of)7960 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7961 {
7962 const off_t offset = this->offset();
7963 const section_size_type oview_size =
7964 convert_to_section_size_type(this->data_size());
7965 unsigned char* const oview = of->get_output_view(offset, oview_size);
7966
7967 const off_t got_file_offset = this->got_plt_->offset();
7968 gold_assert(got_file_offset + this->got_plt_->data_size()
7969 == this->got_irelative_->offset());
7970 const section_size_type got_size =
7971 convert_to_section_size_type(this->got_plt_->data_size()
7972 + this->got_irelative_->data_size());
7973 unsigned char* const got_view = of->get_output_view(got_file_offset,
7974 got_size);
7975 unsigned char* pov = oview;
7976
7977 Arm_address plt_address = this->address();
7978 Arm_address got_address = this->got_plt_->address();
7979
7980 // Write first PLT entry.
7981 this->fill_first_plt_entry(pov, got_address, plt_address);
7982 pov += this->first_plt_entry_offset();
7983
7984 unsigned char* got_pov = got_view;
7985
7986 memset(got_pov, 0, 12);
7987 got_pov += 12;
7988
7989 unsigned int plt_offset = this->first_plt_entry_offset();
7990 unsigned int got_offset = 12;
7991 const unsigned int count = this->count_ + this->irelative_count_;
7992 gold_assert(this->irelative_count_ == this->irelative_data_vec_.size());
7993 for (unsigned int i = 0;
7994 i < count;
7995 ++i,
7996 pov += this->get_plt_entry_size(),
7997 got_pov += 4,
7998 plt_offset += this->get_plt_entry_size(),
7999 got_offset += 4)
8000 {
8001 // Set and adjust the PLT entry itself.
8002 this->fill_plt_entry(pov, got_address, plt_address,
8003 got_offset, plt_offset);
8004
8005 Arm_address value;
8006 if (i < this->count_)
8007 {
8008 // For non-irelative got entries, the value is the beginning of plt.
8009 value = plt_address;
8010 }
8011 else
8012 {
8013 // For irelative got entries, the value is the (global/local) symbol
8014 // address.
8015 const IRelative_data& idata =
8016 this->irelative_data_vec_[i - this->count_];
8017 if (idata.symbol_is_global_)
8018 {
8019 // Set the entry in the GOT for irelative symbols. The content is
8020 // the address of the ifunc, not the address of plt start.
8021 const Sized_symbol<32>* sized_symbol = idata.u_.global;
8022 gold_assert(sized_symbol->type() == elfcpp::STT_GNU_IFUNC);
8023 value = sized_symbol->value();
8024 }
8025 else
8026 {
8027 value = idata.u_.local.relobj->local_symbol_value(
8028 idata.u_.local.index, 0);
8029 }
8030 }
8031 elfcpp::Swap<32, big_endian>::writeval(got_pov, value);
8032 }
8033
8034 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
8035 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
8036
8037 of->write_output_view(offset, oview_size, oview);
8038 of->write_output_view(got_file_offset, got_size, got_view);
8039 }
8040
8041
8042 // Create a PLT entry for a global symbol.
8043
8044 template<bool big_endian>
8045 void
make_plt_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)8046 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
8047 Symbol* gsym)
8048 {
8049 if (gsym->has_plt_offset())
8050 return;
8051
8052 if (this->plt_ == NULL)
8053 this->make_plt_section(symtab, layout);
8054
8055 this->plt_->add_entry(symtab, layout, gsym);
8056 }
8057
8058
8059 // Create the PLT section.
8060 template<bool big_endian>
8061 void
make_plt_section(Symbol_table * symtab,Layout * layout)8062 Target_arm<big_endian>::make_plt_section(
8063 Symbol_table* symtab, Layout* layout)
8064 {
8065 if (this->plt_ == NULL)
8066 {
8067 // Create the GOT section first.
8068 this->got_section(symtab, layout);
8069
8070 // GOT for irelatives is create along with got.plt.
8071 gold_assert(this->got_ != NULL
8072 && this->got_plt_ != NULL
8073 && this->got_irelative_ != NULL);
8074 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
8075 this->got_irelative_);
8076
8077 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8078 (elfcpp::SHF_ALLOC
8079 | elfcpp::SHF_EXECINSTR),
8080 this->plt_, ORDER_PLT, false);
8081 symtab->define_in_output_data("$a", NULL,
8082 Symbol_table::PREDEFINED,
8083 this->plt_,
8084 0, 0, elfcpp::STT_NOTYPE,
8085 elfcpp::STB_LOCAL,
8086 elfcpp::STV_DEFAULT, 0,
8087 false, false);
8088 }
8089 }
8090
8091
8092 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
8093
8094 template<bool big_endian>
8095 void
make_local_ifunc_plt_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * relobj,unsigned int local_sym_index)8096 Target_arm<big_endian>::make_local_ifunc_plt_entry(
8097 Symbol_table* symtab, Layout* layout,
8098 Sized_relobj_file<32, big_endian>* relobj,
8099 unsigned int local_sym_index)
8100 {
8101 if (relobj->local_has_plt_offset(local_sym_index))
8102 return;
8103 if (this->plt_ == NULL)
8104 this->make_plt_section(symtab, layout);
8105 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
8106 relobj,
8107 local_sym_index);
8108 relobj->set_local_plt_offset(local_sym_index, plt_offset);
8109 }
8110
8111
8112 // Return the number of entries in the PLT.
8113
8114 template<bool big_endian>
8115 unsigned int
plt_entry_count() const8116 Target_arm<big_endian>::plt_entry_count() const
8117 {
8118 if (this->plt_ == NULL)
8119 return 0;
8120 return this->plt_->entry_count();
8121 }
8122
8123 // Return the offset of the first non-reserved PLT entry.
8124
8125 template<bool big_endian>
8126 unsigned int
first_plt_entry_offset() const8127 Target_arm<big_endian>::first_plt_entry_offset() const
8128 {
8129 return this->plt_->first_plt_entry_offset();
8130 }
8131
8132 // Return the size of each PLT entry.
8133
8134 template<bool big_endian>
8135 unsigned int
plt_entry_size() const8136 Target_arm<big_endian>::plt_entry_size() const
8137 {
8138 return this->plt_->get_plt_entry_size();
8139 }
8140
8141 // Get the section to use for TLS_DESC relocations.
8142
8143 template<bool big_endian>
8144 typename Target_arm<big_endian>::Reloc_section*
rel_tls_desc_section(Layout * layout) const8145 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
8146 {
8147 return this->plt_section()->rel_tls_desc(layout);
8148 }
8149
8150 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
8151
8152 template<bool big_endian>
8153 void
define_tls_base_symbol(Symbol_table * symtab,Layout * layout)8154 Target_arm<big_endian>::define_tls_base_symbol(
8155 Symbol_table* symtab,
8156 Layout* layout)
8157 {
8158 if (this->tls_base_symbol_defined_)
8159 return;
8160
8161 Output_segment* tls_segment = layout->tls_segment();
8162 if (tls_segment != NULL)
8163 {
8164 bool is_exec = parameters->options().output_is_executable();
8165 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
8166 Symbol_table::PREDEFINED,
8167 tls_segment, 0, 0,
8168 elfcpp::STT_TLS,
8169 elfcpp::STB_LOCAL,
8170 elfcpp::STV_HIDDEN, 0,
8171 (is_exec
8172 ? Symbol::SEGMENT_END
8173 : Symbol::SEGMENT_START),
8174 true);
8175 }
8176 this->tls_base_symbol_defined_ = true;
8177 }
8178
8179 // Create a GOT entry for the TLS module index.
8180
8181 template<bool big_endian>
8182 unsigned int
got_mod_index_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * object)8183 Target_arm<big_endian>::got_mod_index_entry(
8184 Symbol_table* symtab,
8185 Layout* layout,
8186 Sized_relobj_file<32, big_endian>* object)
8187 {
8188 if (this->got_mod_index_offset_ == -1U)
8189 {
8190 gold_assert(symtab != NULL && layout != NULL && object != NULL);
8191 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
8192 unsigned int got_offset;
8193 if (!parameters->doing_static_link())
8194 {
8195 got_offset = got->add_constant(0);
8196 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
8197 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
8198 got_offset);
8199 }
8200 else
8201 {
8202 // We are doing a static link. Just mark it as belong to module 1,
8203 // the executable.
8204 got_offset = got->add_constant(1);
8205 }
8206
8207 got->add_constant(0);
8208 this->got_mod_index_offset_ = got_offset;
8209 }
8210 return this->got_mod_index_offset_;
8211 }
8212
8213 // Optimize the TLS relocation type based on what we know about the
8214 // symbol. IS_FINAL is true if the final address of this symbol is
8215 // known at link time.
8216
8217 template<bool big_endian>
8218 tls::Tls_optimization
optimize_tls_reloc(bool,int)8219 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
8220 {
8221 // FIXME: Currently we do not do any TLS optimization.
8222 return tls::TLSOPT_NONE;
8223 }
8224
8225 // Get the Reference_flags for a particular relocation.
8226
8227 template<bool big_endian>
8228 int
get_reference_flags(unsigned int r_type)8229 Target_arm<big_endian>::Scan::get_reference_flags(unsigned int r_type)
8230 {
8231 switch (r_type)
8232 {
8233 case elfcpp::R_ARM_NONE:
8234 case elfcpp::R_ARM_V4BX:
8235 case elfcpp::R_ARM_GNU_VTENTRY:
8236 case elfcpp::R_ARM_GNU_VTINHERIT:
8237 // No symbol reference.
8238 return 0;
8239
8240 case elfcpp::R_ARM_ABS32:
8241 case elfcpp::R_ARM_ABS16:
8242 case elfcpp::R_ARM_ABS12:
8243 case elfcpp::R_ARM_THM_ABS5:
8244 case elfcpp::R_ARM_ABS8:
8245 case elfcpp::R_ARM_BASE_ABS:
8246 case elfcpp::R_ARM_MOVW_ABS_NC:
8247 case elfcpp::R_ARM_MOVT_ABS:
8248 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8249 case elfcpp::R_ARM_THM_MOVT_ABS:
8250 case elfcpp::R_ARM_ABS32_NOI:
8251 return Symbol::ABSOLUTE_REF;
8252
8253 case elfcpp::R_ARM_REL32:
8254 case elfcpp::R_ARM_LDR_PC_G0:
8255 case elfcpp::R_ARM_SBREL32:
8256 case elfcpp::R_ARM_THM_PC8:
8257 case elfcpp::R_ARM_BASE_PREL:
8258 case elfcpp::R_ARM_MOVW_PREL_NC:
8259 case elfcpp::R_ARM_MOVT_PREL:
8260 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8261 case elfcpp::R_ARM_THM_MOVT_PREL:
8262 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8263 case elfcpp::R_ARM_THM_PC12:
8264 case elfcpp::R_ARM_REL32_NOI:
8265 case elfcpp::R_ARM_ALU_PC_G0_NC:
8266 case elfcpp::R_ARM_ALU_PC_G0:
8267 case elfcpp::R_ARM_ALU_PC_G1_NC:
8268 case elfcpp::R_ARM_ALU_PC_G1:
8269 case elfcpp::R_ARM_ALU_PC_G2:
8270 case elfcpp::R_ARM_LDR_PC_G1:
8271 case elfcpp::R_ARM_LDR_PC_G2:
8272 case elfcpp::R_ARM_LDRS_PC_G0:
8273 case elfcpp::R_ARM_LDRS_PC_G1:
8274 case elfcpp::R_ARM_LDRS_PC_G2:
8275 case elfcpp::R_ARM_LDC_PC_G0:
8276 case elfcpp::R_ARM_LDC_PC_G1:
8277 case elfcpp::R_ARM_LDC_PC_G2:
8278 case elfcpp::R_ARM_ALU_SB_G0_NC:
8279 case elfcpp::R_ARM_ALU_SB_G0:
8280 case elfcpp::R_ARM_ALU_SB_G1_NC:
8281 case elfcpp::R_ARM_ALU_SB_G1:
8282 case elfcpp::R_ARM_ALU_SB_G2:
8283 case elfcpp::R_ARM_LDR_SB_G0:
8284 case elfcpp::R_ARM_LDR_SB_G1:
8285 case elfcpp::R_ARM_LDR_SB_G2:
8286 case elfcpp::R_ARM_LDRS_SB_G0:
8287 case elfcpp::R_ARM_LDRS_SB_G1:
8288 case elfcpp::R_ARM_LDRS_SB_G2:
8289 case elfcpp::R_ARM_LDC_SB_G0:
8290 case elfcpp::R_ARM_LDC_SB_G1:
8291 case elfcpp::R_ARM_LDC_SB_G2:
8292 case elfcpp::R_ARM_MOVW_BREL_NC:
8293 case elfcpp::R_ARM_MOVT_BREL:
8294 case elfcpp::R_ARM_MOVW_BREL:
8295 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8296 case elfcpp::R_ARM_THM_MOVT_BREL:
8297 case elfcpp::R_ARM_THM_MOVW_BREL:
8298 case elfcpp::R_ARM_GOTOFF32:
8299 case elfcpp::R_ARM_GOTOFF12:
8300 case elfcpp::R_ARM_SBREL31:
8301 return Symbol::RELATIVE_REF;
8302
8303 case elfcpp::R_ARM_PLT32:
8304 case elfcpp::R_ARM_CALL:
8305 case elfcpp::R_ARM_JUMP24:
8306 case elfcpp::R_ARM_THM_CALL:
8307 case elfcpp::R_ARM_THM_JUMP24:
8308 case elfcpp::R_ARM_THM_JUMP19:
8309 case elfcpp::R_ARM_THM_JUMP6:
8310 case elfcpp::R_ARM_THM_JUMP11:
8311 case elfcpp::R_ARM_THM_JUMP8:
8312 // R_ARM_PREL31 is not used to relocate call/jump instructions but
8313 // in unwind tables. It may point to functions via PLTs.
8314 // So we treat it like call/jump relocations above.
8315 case elfcpp::R_ARM_PREL31:
8316 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
8317
8318 case elfcpp::R_ARM_GOT_BREL:
8319 case elfcpp::R_ARM_GOT_ABS:
8320 case elfcpp::R_ARM_GOT_PREL:
8321 // Absolute in GOT.
8322 return Symbol::ABSOLUTE_REF;
8323
8324 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8325 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8326 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8327 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8328 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8329 return Symbol::TLS_REF;
8330
8331 case elfcpp::R_ARM_TARGET1:
8332 case elfcpp::R_ARM_TARGET2:
8333 case elfcpp::R_ARM_COPY:
8334 case elfcpp::R_ARM_GLOB_DAT:
8335 case elfcpp::R_ARM_JUMP_SLOT:
8336 case elfcpp::R_ARM_RELATIVE:
8337 case elfcpp::R_ARM_PC24:
8338 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8339 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8340 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8341 default:
8342 // Not expected. We will give an error later.
8343 return 0;
8344 }
8345 }
8346
8347 // Report an unsupported relocation against a local symbol.
8348
8349 template<bool big_endian>
8350 void
unsupported_reloc_local(Sized_relobj_file<32,big_endian> * object,unsigned int r_type)8351 Target_arm<big_endian>::Scan::unsupported_reloc_local(
8352 Sized_relobj_file<32, big_endian>* object,
8353 unsigned int r_type)
8354 {
8355 gold_error(_("%s: unsupported reloc %u against local symbol"),
8356 object->name().c_str(), r_type);
8357 }
8358
8359 // We are about to emit a dynamic relocation of type R_TYPE. If the
8360 // dynamic linker does not support it, issue an error. The GNU linker
8361 // only issues a non-PIC error for an allocated read-only section.
8362 // Here we know the section is allocated, but we don't know that it is
8363 // read-only. But we check for all the relocation types which the
8364 // glibc dynamic linker supports, so it seems appropriate to issue an
8365 // error even if the section is not read-only.
8366
8367 template<bool big_endian>
8368 void
check_non_pic(Relobj * object,unsigned int r_type)8369 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
8370 unsigned int r_type)
8371 {
8372 switch (r_type)
8373 {
8374 // These are the relocation types supported by glibc for ARM.
8375 case elfcpp::R_ARM_RELATIVE:
8376 case elfcpp::R_ARM_COPY:
8377 case elfcpp::R_ARM_GLOB_DAT:
8378 case elfcpp::R_ARM_JUMP_SLOT:
8379 case elfcpp::R_ARM_ABS32:
8380 case elfcpp::R_ARM_ABS32_NOI:
8381 case elfcpp::R_ARM_IRELATIVE:
8382 case elfcpp::R_ARM_PC24:
8383 // FIXME: The following 3 types are not supported by Android's dynamic
8384 // linker.
8385 case elfcpp::R_ARM_TLS_DTPMOD32:
8386 case elfcpp::R_ARM_TLS_DTPOFF32:
8387 case elfcpp::R_ARM_TLS_TPOFF32:
8388 return;
8389
8390 default:
8391 {
8392 // This prevents us from issuing more than one error per reloc
8393 // section. But we can still wind up issuing more than one
8394 // error per object file.
8395 if (this->issued_non_pic_error_)
8396 return;
8397 const Arm_reloc_property* reloc_property =
8398 arm_reloc_property_table->get_reloc_property(r_type);
8399 gold_assert(reloc_property != NULL);
8400 object->error(_("requires unsupported dynamic reloc %s; "
8401 "recompile with -fPIC"),
8402 reloc_property->name().c_str());
8403 this->issued_non_pic_error_ = true;
8404 return;
8405 }
8406
8407 case elfcpp::R_ARM_NONE:
8408 gold_unreachable();
8409 }
8410 }
8411
8412
8413 // Return whether we need to make a PLT entry for a relocation of the
8414 // given type against a STT_GNU_IFUNC symbol.
8415
8416 template<bool big_endian>
8417 bool
reloc_needs_plt_for_ifunc(Sized_relobj_file<32,big_endian> * object,unsigned int r_type)8418 Target_arm<big_endian>::Scan::reloc_needs_plt_for_ifunc(
8419 Sized_relobj_file<32, big_endian>* object,
8420 unsigned int r_type)
8421 {
8422 int flags = Scan::get_reference_flags(r_type);
8423 if (flags & Symbol::TLS_REF)
8424 {
8425 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
8426 object->name().c_str(), r_type);
8427 return false;
8428 }
8429 return flags != 0;
8430 }
8431
8432
8433 // Scan a relocation for a local symbol.
8434 // FIXME: This only handles a subset of relocation types used by Android
8435 // on ARM v5te devices.
8436
8437 template<bool big_endian>
8438 inline void
local(Symbol_table * symtab,Layout * layout,Target_arm * target,Sized_relobj_file<32,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rel<32,big_endian> & reloc,unsigned int r_type,const elfcpp::Sym<32,big_endian> & lsym,bool is_discarded)8439 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
8440 Layout* layout,
8441 Target_arm* target,
8442 Sized_relobj_file<32, big_endian>* object,
8443 unsigned int data_shndx,
8444 Output_section* output_section,
8445 const elfcpp::Rel<32, big_endian>& reloc,
8446 unsigned int r_type,
8447 const elfcpp::Sym<32, big_endian>& lsym,
8448 bool is_discarded)
8449 {
8450 if (is_discarded)
8451 return;
8452
8453 r_type = get_real_reloc_type(r_type);
8454
8455 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8456 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
8457 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
8458 {
8459 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8460 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
8461 }
8462
8463 switch (r_type)
8464 {
8465 case elfcpp::R_ARM_NONE:
8466 case elfcpp::R_ARM_V4BX:
8467 case elfcpp::R_ARM_GNU_VTENTRY:
8468 case elfcpp::R_ARM_GNU_VTINHERIT:
8469 break;
8470
8471 case elfcpp::R_ARM_ABS32:
8472 case elfcpp::R_ARM_ABS32_NOI:
8473 // If building a shared library (or a position-independent
8474 // executable), we need to create a dynamic relocation for
8475 // this location. The relocation applied at link time will
8476 // apply the link-time value, so we flag the location with
8477 // an R_ARM_RELATIVE relocation so the dynamic loader can
8478 // relocate it easily.
8479 if (parameters->options().output_is_position_independent())
8480 {
8481 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8482 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8483 // If we are to add more other reloc types than R_ARM_ABS32,
8484 // we need to add check_non_pic(object, r_type) here.
8485 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
8486 output_section, data_shndx,
8487 reloc.get_r_offset(), is_ifunc);
8488 }
8489 break;
8490
8491 case elfcpp::R_ARM_ABS16:
8492 case elfcpp::R_ARM_ABS12:
8493 case elfcpp::R_ARM_THM_ABS5:
8494 case elfcpp::R_ARM_ABS8:
8495 case elfcpp::R_ARM_BASE_ABS:
8496 case elfcpp::R_ARM_MOVW_ABS_NC:
8497 case elfcpp::R_ARM_MOVT_ABS:
8498 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8499 case elfcpp::R_ARM_THM_MOVT_ABS:
8500 // If building a shared library (or a position-independent
8501 // executable), we need to create a dynamic relocation for
8502 // this location. Because the addend needs to remain in the
8503 // data section, we need to be careful not to apply this
8504 // relocation statically.
8505 if (parameters->options().output_is_position_independent())
8506 {
8507 check_non_pic(object, r_type);
8508 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8509 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8510 if (lsym.get_st_type() != elfcpp::STT_SECTION)
8511 rel_dyn->add_local(object, r_sym, r_type, output_section,
8512 data_shndx, reloc.get_r_offset());
8513 else
8514 {
8515 gold_assert(lsym.get_st_value() == 0);
8516 unsigned int shndx = lsym.get_st_shndx();
8517 bool is_ordinary;
8518 shndx = object->adjust_sym_shndx(r_sym, shndx,
8519 &is_ordinary);
8520 if (!is_ordinary)
8521 object->error(_("section symbol %u has bad shndx %u"),
8522 r_sym, shndx);
8523 else
8524 rel_dyn->add_local_section(object, shndx,
8525 r_type, output_section,
8526 data_shndx, reloc.get_r_offset());
8527 }
8528 }
8529 break;
8530
8531 case elfcpp::R_ARM_REL32:
8532 case elfcpp::R_ARM_LDR_PC_G0:
8533 case elfcpp::R_ARM_SBREL32:
8534 case elfcpp::R_ARM_THM_CALL:
8535 case elfcpp::R_ARM_THM_PC8:
8536 case elfcpp::R_ARM_BASE_PREL:
8537 case elfcpp::R_ARM_PLT32:
8538 case elfcpp::R_ARM_CALL:
8539 case elfcpp::R_ARM_JUMP24:
8540 case elfcpp::R_ARM_THM_JUMP24:
8541 case elfcpp::R_ARM_SBREL31:
8542 case elfcpp::R_ARM_PREL31:
8543 case elfcpp::R_ARM_MOVW_PREL_NC:
8544 case elfcpp::R_ARM_MOVT_PREL:
8545 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8546 case elfcpp::R_ARM_THM_MOVT_PREL:
8547 case elfcpp::R_ARM_THM_JUMP19:
8548 case elfcpp::R_ARM_THM_JUMP6:
8549 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8550 case elfcpp::R_ARM_THM_PC12:
8551 case elfcpp::R_ARM_REL32_NOI:
8552 case elfcpp::R_ARM_ALU_PC_G0_NC:
8553 case elfcpp::R_ARM_ALU_PC_G0:
8554 case elfcpp::R_ARM_ALU_PC_G1_NC:
8555 case elfcpp::R_ARM_ALU_PC_G1:
8556 case elfcpp::R_ARM_ALU_PC_G2:
8557 case elfcpp::R_ARM_LDR_PC_G1:
8558 case elfcpp::R_ARM_LDR_PC_G2:
8559 case elfcpp::R_ARM_LDRS_PC_G0:
8560 case elfcpp::R_ARM_LDRS_PC_G1:
8561 case elfcpp::R_ARM_LDRS_PC_G2:
8562 case elfcpp::R_ARM_LDC_PC_G0:
8563 case elfcpp::R_ARM_LDC_PC_G1:
8564 case elfcpp::R_ARM_LDC_PC_G2:
8565 case elfcpp::R_ARM_ALU_SB_G0_NC:
8566 case elfcpp::R_ARM_ALU_SB_G0:
8567 case elfcpp::R_ARM_ALU_SB_G1_NC:
8568 case elfcpp::R_ARM_ALU_SB_G1:
8569 case elfcpp::R_ARM_ALU_SB_G2:
8570 case elfcpp::R_ARM_LDR_SB_G0:
8571 case elfcpp::R_ARM_LDR_SB_G1:
8572 case elfcpp::R_ARM_LDR_SB_G2:
8573 case elfcpp::R_ARM_LDRS_SB_G0:
8574 case elfcpp::R_ARM_LDRS_SB_G1:
8575 case elfcpp::R_ARM_LDRS_SB_G2:
8576 case elfcpp::R_ARM_LDC_SB_G0:
8577 case elfcpp::R_ARM_LDC_SB_G1:
8578 case elfcpp::R_ARM_LDC_SB_G2:
8579 case elfcpp::R_ARM_MOVW_BREL_NC:
8580 case elfcpp::R_ARM_MOVT_BREL:
8581 case elfcpp::R_ARM_MOVW_BREL:
8582 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8583 case elfcpp::R_ARM_THM_MOVT_BREL:
8584 case elfcpp::R_ARM_THM_MOVW_BREL:
8585 case elfcpp::R_ARM_THM_JUMP11:
8586 case elfcpp::R_ARM_THM_JUMP8:
8587 // We don't need to do anything for a relative addressing relocation
8588 // against a local symbol if it does not reference the GOT.
8589 break;
8590
8591 case elfcpp::R_ARM_GOTOFF32:
8592 case elfcpp::R_ARM_GOTOFF12:
8593 // We need a GOT section:
8594 target->got_section(symtab, layout);
8595 break;
8596
8597 case elfcpp::R_ARM_GOT_BREL:
8598 case elfcpp::R_ARM_GOT_PREL:
8599 {
8600 // The symbol requires a GOT entry.
8601 Arm_output_data_got<big_endian>* got =
8602 target->got_section(symtab, layout);
8603 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8604 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
8605 {
8606 // If we are generating a shared object, we need to add a
8607 // dynamic RELATIVE relocation for this symbol's GOT entry.
8608 if (parameters->options().output_is_position_independent())
8609 {
8610 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8611 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8612 rel_dyn->add_local_relative(
8613 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
8614 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
8615 }
8616 }
8617 }
8618 break;
8619
8620 case elfcpp::R_ARM_TARGET1:
8621 case elfcpp::R_ARM_TARGET2:
8622 // This should have been mapped to another type already.
8623 // Fall through.
8624 case elfcpp::R_ARM_COPY:
8625 case elfcpp::R_ARM_GLOB_DAT:
8626 case elfcpp::R_ARM_JUMP_SLOT:
8627 case elfcpp::R_ARM_RELATIVE:
8628 // These are relocations which should only be seen by the
8629 // dynamic linker, and should never be seen here.
8630 gold_error(_("%s: unexpected reloc %u in object file"),
8631 object->name().c_str(), r_type);
8632 break;
8633
8634
8635 // These are initial TLS relocs, which are expected when
8636 // linking.
8637 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8638 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8639 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8640 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8641 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8642 {
8643 bool output_is_shared = parameters->options().shared();
8644 const tls::Tls_optimization optimized_type
8645 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
8646 r_type);
8647 switch (r_type)
8648 {
8649 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8650 if (optimized_type == tls::TLSOPT_NONE)
8651 {
8652 // Create a pair of GOT entries for the module index and
8653 // dtv-relative offset.
8654 Arm_output_data_got<big_endian>* got
8655 = target->got_section(symtab, layout);
8656 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8657 unsigned int shndx = lsym.get_st_shndx();
8658 bool is_ordinary;
8659 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8660 if (!is_ordinary)
8661 {
8662 object->error(_("local symbol %u has bad shndx %u"),
8663 r_sym, shndx);
8664 break;
8665 }
8666
8667 if (!parameters->doing_static_link())
8668 got->add_local_pair_with_rel(object, r_sym, shndx,
8669 GOT_TYPE_TLS_PAIR,
8670 target->rel_dyn_section(layout),
8671 elfcpp::R_ARM_TLS_DTPMOD32);
8672 else
8673 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
8674 object, r_sym);
8675 }
8676 else
8677 // FIXME: TLS optimization not supported yet.
8678 gold_unreachable();
8679 break;
8680
8681 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8682 if (optimized_type == tls::TLSOPT_NONE)
8683 {
8684 // Create a GOT entry for the module index.
8685 target->got_mod_index_entry(symtab, layout, object);
8686 }
8687 else
8688 // FIXME: TLS optimization not supported yet.
8689 gold_unreachable();
8690 break;
8691
8692 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8693 break;
8694
8695 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8696 layout->set_has_static_tls();
8697 if (optimized_type == tls::TLSOPT_NONE)
8698 {
8699 // Create a GOT entry for the tp-relative offset.
8700 Arm_output_data_got<big_endian>* got
8701 = target->got_section(symtab, layout);
8702 unsigned int r_sym =
8703 elfcpp::elf_r_sym<32>(reloc.get_r_info());
8704 if (!parameters->doing_static_link())
8705 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
8706 target->rel_dyn_section(layout),
8707 elfcpp::R_ARM_TLS_TPOFF32);
8708 else if (!object->local_has_got_offset(r_sym,
8709 GOT_TYPE_TLS_OFFSET))
8710 {
8711 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
8712 unsigned int got_offset =
8713 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
8714 got->add_static_reloc(got_offset,
8715 elfcpp::R_ARM_TLS_TPOFF32, object,
8716 r_sym);
8717 }
8718 }
8719 else
8720 // FIXME: TLS optimization not supported yet.
8721 gold_unreachable();
8722 break;
8723
8724 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8725 layout->set_has_static_tls();
8726 if (output_is_shared)
8727 {
8728 // We need to create a dynamic relocation.
8729 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
8730 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8731 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8732 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
8733 output_section, data_shndx,
8734 reloc.get_r_offset());
8735 }
8736 break;
8737
8738 default:
8739 gold_unreachable();
8740 }
8741 }
8742 break;
8743
8744 case elfcpp::R_ARM_PC24:
8745 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8746 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8747 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8748 default:
8749 unsupported_reloc_local(object, r_type);
8750 break;
8751 }
8752 }
8753
8754 // Report an unsupported relocation against a global symbol.
8755
8756 template<bool big_endian>
8757 void
unsupported_reloc_global(Sized_relobj_file<32,big_endian> * object,unsigned int r_type,Symbol * gsym)8758 Target_arm<big_endian>::Scan::unsupported_reloc_global(
8759 Sized_relobj_file<32, big_endian>* object,
8760 unsigned int r_type,
8761 Symbol* gsym)
8762 {
8763 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8764 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8765 }
8766
8767 template<bool big_endian>
8768 inline bool
possible_function_pointer_reloc(unsigned int r_type)8769 Target_arm<big_endian>::Scan::possible_function_pointer_reloc(
8770 unsigned int r_type)
8771 {
8772 switch (r_type)
8773 {
8774 case elfcpp::R_ARM_PC24:
8775 case elfcpp::R_ARM_THM_CALL:
8776 case elfcpp::R_ARM_PLT32:
8777 case elfcpp::R_ARM_CALL:
8778 case elfcpp::R_ARM_JUMP24:
8779 case elfcpp::R_ARM_THM_JUMP24:
8780 case elfcpp::R_ARM_SBREL31:
8781 case elfcpp::R_ARM_PREL31:
8782 case elfcpp::R_ARM_THM_JUMP19:
8783 case elfcpp::R_ARM_THM_JUMP6:
8784 case elfcpp::R_ARM_THM_JUMP11:
8785 case elfcpp::R_ARM_THM_JUMP8:
8786 // All the relocations above are branches except SBREL31 and PREL31.
8787 return false;
8788
8789 default:
8790 // Be conservative and assume this is a function pointer.
8791 return true;
8792 }
8793 }
8794
8795 template<bool big_endian>
8796 inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_arm<big_endian> * target,Sized_relobj_file<32,big_endian> *,unsigned int,Output_section *,const elfcpp::Rel<32,big_endian> &,unsigned int r_type,const elfcpp::Sym<32,big_endian> &)8797 Target_arm<big_endian>::Scan::local_reloc_may_be_function_pointer(
8798 Symbol_table*,
8799 Layout*,
8800 Target_arm<big_endian>* target,
8801 Sized_relobj_file<32, big_endian>*,
8802 unsigned int,
8803 Output_section*,
8804 const elfcpp::Rel<32, big_endian>&,
8805 unsigned int r_type,
8806 const elfcpp::Sym<32, big_endian>&)
8807 {
8808 r_type = target->get_real_reloc_type(r_type);
8809 return possible_function_pointer_reloc(r_type);
8810 }
8811
8812 template<bool big_endian>
8813 inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_arm<big_endian> * target,Sized_relobj_file<32,big_endian> *,unsigned int,Output_section *,const elfcpp::Rel<32,big_endian> &,unsigned int r_type,Symbol * gsym)8814 Target_arm<big_endian>::Scan::global_reloc_may_be_function_pointer(
8815 Symbol_table*,
8816 Layout*,
8817 Target_arm<big_endian>* target,
8818 Sized_relobj_file<32, big_endian>*,
8819 unsigned int,
8820 Output_section*,
8821 const elfcpp::Rel<32, big_endian>&,
8822 unsigned int r_type,
8823 Symbol* gsym)
8824 {
8825 // GOT is not a function.
8826 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8827 return false;
8828
8829 r_type = target->get_real_reloc_type(r_type);
8830 return possible_function_pointer_reloc(r_type);
8831 }
8832
8833 // Scan a relocation for a global symbol.
8834
8835 template<bool big_endian>
8836 inline void
global(Symbol_table * symtab,Layout * layout,Target_arm * target,Sized_relobj_file<32,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rel<32,big_endian> & reloc,unsigned int r_type,Symbol * gsym)8837 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
8838 Layout* layout,
8839 Target_arm* target,
8840 Sized_relobj_file<32, big_endian>* object,
8841 unsigned int data_shndx,
8842 Output_section* output_section,
8843 const elfcpp::Rel<32, big_endian>& reloc,
8844 unsigned int r_type,
8845 Symbol* gsym)
8846 {
8847 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
8848 // section. We check here to avoid creating a dynamic reloc against
8849 // _GLOBAL_OFFSET_TABLE_.
8850 if (!target->has_got_section()
8851 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8852 target->got_section(symtab, layout);
8853
8854 // A STT_GNU_IFUNC symbol may require a PLT entry.
8855 if (gsym->type() == elfcpp::STT_GNU_IFUNC
8856 && this->reloc_needs_plt_for_ifunc(object, r_type))
8857 target->make_plt_entry(symtab, layout, gsym);
8858
8859 r_type = get_real_reloc_type(r_type);
8860 switch (r_type)
8861 {
8862 case elfcpp::R_ARM_NONE:
8863 case elfcpp::R_ARM_V4BX:
8864 case elfcpp::R_ARM_GNU_VTENTRY:
8865 case elfcpp::R_ARM_GNU_VTINHERIT:
8866 break;
8867
8868 case elfcpp::R_ARM_ABS32:
8869 case elfcpp::R_ARM_ABS16:
8870 case elfcpp::R_ARM_ABS12:
8871 case elfcpp::R_ARM_THM_ABS5:
8872 case elfcpp::R_ARM_ABS8:
8873 case elfcpp::R_ARM_BASE_ABS:
8874 case elfcpp::R_ARM_MOVW_ABS_NC:
8875 case elfcpp::R_ARM_MOVT_ABS:
8876 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8877 case elfcpp::R_ARM_THM_MOVT_ABS:
8878 case elfcpp::R_ARM_ABS32_NOI:
8879 // Absolute addressing relocations.
8880 {
8881 // Make a PLT entry if necessary.
8882 if (this->symbol_needs_plt_entry(gsym))
8883 {
8884 target->make_plt_entry(symtab, layout, gsym);
8885 // Since this is not a PC-relative relocation, we may be
8886 // taking the address of a function. In that case we need to
8887 // set the entry in the dynamic symbol table to the address of
8888 // the PLT entry.
8889 if (gsym->is_from_dynobj() && !parameters->options().shared())
8890 gsym->set_needs_dynsym_value();
8891 }
8892 // Make a dynamic relocation if necessary.
8893 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8894 {
8895 if (!parameters->options().output_is_position_independent()
8896 && gsym->may_need_copy_reloc())
8897 {
8898 target->copy_reloc(symtab, layout, object,
8899 data_shndx, output_section, gsym, reloc);
8900 }
8901 else if ((r_type == elfcpp::R_ARM_ABS32
8902 || r_type == elfcpp::R_ARM_ABS32_NOI)
8903 && gsym->type() == elfcpp::STT_GNU_IFUNC
8904 && gsym->can_use_relative_reloc(false)
8905 && !gsym->is_from_dynobj()
8906 && !gsym->is_undefined()
8907 && !gsym->is_preemptible())
8908 {
8909 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
8910 // symbol. This makes a function address in a PIE executable
8911 // match the address in a shared library that it links against.
8912 Reloc_section* rel_irelative =
8913 target->rel_irelative_section(layout);
8914 unsigned int r_type = elfcpp::R_ARM_IRELATIVE;
8915 rel_irelative->add_symbolless_global_addend(
8916 gsym, r_type, output_section, object,
8917 data_shndx, reloc.get_r_offset());
8918 }
8919 else if ((r_type == elfcpp::R_ARM_ABS32
8920 || r_type == elfcpp::R_ARM_ABS32_NOI)
8921 && gsym->can_use_relative_reloc(false))
8922 {
8923 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8924 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
8925 output_section, object,
8926 data_shndx, reloc.get_r_offset());
8927 }
8928 else
8929 {
8930 check_non_pic(object, r_type);
8931 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8932 rel_dyn->add_global(gsym, r_type, output_section, object,
8933 data_shndx, reloc.get_r_offset());
8934 }
8935 }
8936 }
8937 break;
8938
8939 case elfcpp::R_ARM_GOTOFF32:
8940 case elfcpp::R_ARM_GOTOFF12:
8941 // We need a GOT section.
8942 target->got_section(symtab, layout);
8943 break;
8944
8945 case elfcpp::R_ARM_REL32:
8946 case elfcpp::R_ARM_LDR_PC_G0:
8947 case elfcpp::R_ARM_SBREL32:
8948 case elfcpp::R_ARM_THM_PC8:
8949 case elfcpp::R_ARM_BASE_PREL:
8950 case elfcpp::R_ARM_MOVW_PREL_NC:
8951 case elfcpp::R_ARM_MOVT_PREL:
8952 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8953 case elfcpp::R_ARM_THM_MOVT_PREL:
8954 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8955 case elfcpp::R_ARM_THM_PC12:
8956 case elfcpp::R_ARM_REL32_NOI:
8957 case elfcpp::R_ARM_ALU_PC_G0_NC:
8958 case elfcpp::R_ARM_ALU_PC_G0:
8959 case elfcpp::R_ARM_ALU_PC_G1_NC:
8960 case elfcpp::R_ARM_ALU_PC_G1:
8961 case elfcpp::R_ARM_ALU_PC_G2:
8962 case elfcpp::R_ARM_LDR_PC_G1:
8963 case elfcpp::R_ARM_LDR_PC_G2:
8964 case elfcpp::R_ARM_LDRS_PC_G0:
8965 case elfcpp::R_ARM_LDRS_PC_G1:
8966 case elfcpp::R_ARM_LDRS_PC_G2:
8967 case elfcpp::R_ARM_LDC_PC_G0:
8968 case elfcpp::R_ARM_LDC_PC_G1:
8969 case elfcpp::R_ARM_LDC_PC_G2:
8970 case elfcpp::R_ARM_ALU_SB_G0_NC:
8971 case elfcpp::R_ARM_ALU_SB_G0:
8972 case elfcpp::R_ARM_ALU_SB_G1_NC:
8973 case elfcpp::R_ARM_ALU_SB_G1:
8974 case elfcpp::R_ARM_ALU_SB_G2:
8975 case elfcpp::R_ARM_LDR_SB_G0:
8976 case elfcpp::R_ARM_LDR_SB_G1:
8977 case elfcpp::R_ARM_LDR_SB_G2:
8978 case elfcpp::R_ARM_LDRS_SB_G0:
8979 case elfcpp::R_ARM_LDRS_SB_G1:
8980 case elfcpp::R_ARM_LDRS_SB_G2:
8981 case elfcpp::R_ARM_LDC_SB_G0:
8982 case elfcpp::R_ARM_LDC_SB_G1:
8983 case elfcpp::R_ARM_LDC_SB_G2:
8984 case elfcpp::R_ARM_MOVW_BREL_NC:
8985 case elfcpp::R_ARM_MOVT_BREL:
8986 case elfcpp::R_ARM_MOVW_BREL:
8987 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8988 case elfcpp::R_ARM_THM_MOVT_BREL:
8989 case elfcpp::R_ARM_THM_MOVW_BREL:
8990 // Relative addressing relocations.
8991 {
8992 // Make a dynamic relocation if necessary.
8993 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8994 {
8995 if (parameters->options().output_is_executable()
8996 && target->may_need_copy_reloc(gsym))
8997 {
8998 target->copy_reloc(symtab, layout, object,
8999 data_shndx, output_section, gsym, reloc);
9000 }
9001 else
9002 {
9003 check_non_pic(object, r_type);
9004 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9005 rel_dyn->add_global(gsym, r_type, output_section, object,
9006 data_shndx, reloc.get_r_offset());
9007 }
9008 }
9009 }
9010 break;
9011
9012 case elfcpp::R_ARM_THM_CALL:
9013 case elfcpp::R_ARM_PLT32:
9014 case elfcpp::R_ARM_CALL:
9015 case elfcpp::R_ARM_JUMP24:
9016 case elfcpp::R_ARM_THM_JUMP24:
9017 case elfcpp::R_ARM_SBREL31:
9018 case elfcpp::R_ARM_PREL31:
9019 case elfcpp::R_ARM_THM_JUMP19:
9020 case elfcpp::R_ARM_THM_JUMP6:
9021 case elfcpp::R_ARM_THM_JUMP11:
9022 case elfcpp::R_ARM_THM_JUMP8:
9023 // All the relocation above are branches except for the PREL31 ones.
9024 // A PREL31 relocation can point to a personality function in a shared
9025 // library. In that case we want to use a PLT because we want to
9026 // call the personality routine and the dynamic linkers we care about
9027 // do not support dynamic PREL31 relocations. An REL31 relocation may
9028 // point to a function whose unwinding behaviour is being described but
9029 // we will not mistakenly generate a PLT for that because we should use
9030 // a local section symbol.
9031
9032 // If the symbol is fully resolved, this is just a relative
9033 // local reloc. Otherwise we need a PLT entry.
9034 if (gsym->final_value_is_known())
9035 break;
9036 // If building a shared library, we can also skip the PLT entry
9037 // if the symbol is defined in the output file and is protected
9038 // or hidden.
9039 if (gsym->is_defined()
9040 && !gsym->is_from_dynobj()
9041 && !gsym->is_preemptible())
9042 break;
9043 target->make_plt_entry(symtab, layout, gsym);
9044 break;
9045
9046 case elfcpp::R_ARM_GOT_BREL:
9047 case elfcpp::R_ARM_GOT_ABS:
9048 case elfcpp::R_ARM_GOT_PREL:
9049 {
9050 // The symbol requires a GOT entry.
9051 Arm_output_data_got<big_endian>* got =
9052 target->got_section(symtab, layout);
9053 if (gsym->final_value_is_known())
9054 {
9055 // For a STT_GNU_IFUNC symbol we want the PLT address.
9056 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9057 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
9058 else
9059 got->add_global(gsym, GOT_TYPE_STANDARD);
9060 }
9061 else
9062 {
9063 // If this symbol is not fully resolved, we need to add a
9064 // GOT entry with a dynamic relocation.
9065 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9066 if (gsym->is_from_dynobj()
9067 || gsym->is_undefined()
9068 || gsym->is_preemptible()
9069 || (gsym->visibility() == elfcpp::STV_PROTECTED
9070 && parameters->options().shared())
9071 || (gsym->type() == elfcpp::STT_GNU_IFUNC
9072 && parameters->options().output_is_position_independent()))
9073 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
9074 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
9075 else
9076 {
9077 // For a STT_GNU_IFUNC symbol we want to write the PLT
9078 // offset into the GOT, so that function pointer
9079 // comparisons work correctly.
9080 bool is_new;
9081 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
9082 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
9083 else
9084 {
9085 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
9086 // Tell the dynamic linker to use the PLT address
9087 // when resolving relocations.
9088 if (gsym->is_from_dynobj()
9089 && !parameters->options().shared())
9090 gsym->set_needs_dynsym_value();
9091 }
9092 if (is_new)
9093 rel_dyn->add_global_relative(
9094 gsym, elfcpp::R_ARM_RELATIVE, got,
9095 gsym->got_offset(GOT_TYPE_STANDARD));
9096 }
9097 }
9098 }
9099 break;
9100
9101 case elfcpp::R_ARM_TARGET1:
9102 case elfcpp::R_ARM_TARGET2:
9103 // These should have been mapped to other types already.
9104 // Fall through.
9105 case elfcpp::R_ARM_COPY:
9106 case elfcpp::R_ARM_GLOB_DAT:
9107 case elfcpp::R_ARM_JUMP_SLOT:
9108 case elfcpp::R_ARM_RELATIVE:
9109 // These are relocations which should only be seen by the
9110 // dynamic linker, and should never be seen here.
9111 gold_error(_("%s: unexpected reloc %u in object file"),
9112 object->name().c_str(), r_type);
9113 break;
9114
9115 // These are initial tls relocs, which are expected when
9116 // linking.
9117 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9118 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9119 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9120 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9121 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9122 {
9123 const bool is_final = gsym->final_value_is_known();
9124 const tls::Tls_optimization optimized_type
9125 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
9126 switch (r_type)
9127 {
9128 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9129 if (optimized_type == tls::TLSOPT_NONE)
9130 {
9131 // Create a pair of GOT entries for the module index and
9132 // dtv-relative offset.
9133 Arm_output_data_got<big_endian>* got
9134 = target->got_section(symtab, layout);
9135 if (!parameters->doing_static_link())
9136 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
9137 target->rel_dyn_section(layout),
9138 elfcpp::R_ARM_TLS_DTPMOD32,
9139 elfcpp::R_ARM_TLS_DTPOFF32);
9140 else
9141 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
9142 }
9143 else
9144 // FIXME: TLS optimization not supported yet.
9145 gold_unreachable();
9146 break;
9147
9148 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9149 if (optimized_type == tls::TLSOPT_NONE)
9150 {
9151 // Create a GOT entry for the module index.
9152 target->got_mod_index_entry(symtab, layout, object);
9153 }
9154 else
9155 // FIXME: TLS optimization not supported yet.
9156 gold_unreachable();
9157 break;
9158
9159 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9160 break;
9161
9162 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9163 layout->set_has_static_tls();
9164 if (optimized_type == tls::TLSOPT_NONE)
9165 {
9166 // Create a GOT entry for the tp-relative offset.
9167 Arm_output_data_got<big_endian>* got
9168 = target->got_section(symtab, layout);
9169 if (!parameters->doing_static_link())
9170 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
9171 target->rel_dyn_section(layout),
9172 elfcpp::R_ARM_TLS_TPOFF32);
9173 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
9174 {
9175 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
9176 unsigned int got_offset =
9177 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
9178 got->add_static_reloc(got_offset,
9179 elfcpp::R_ARM_TLS_TPOFF32, gsym);
9180 }
9181 }
9182 else
9183 // FIXME: TLS optimization not supported yet.
9184 gold_unreachable();
9185 break;
9186
9187 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9188 layout->set_has_static_tls();
9189 if (parameters->options().shared())
9190 {
9191 // We need to create a dynamic relocation.
9192 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9193 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
9194 output_section, object,
9195 data_shndx, reloc.get_r_offset());
9196 }
9197 break;
9198
9199 default:
9200 gold_unreachable();
9201 }
9202 }
9203 break;
9204
9205 case elfcpp::R_ARM_PC24:
9206 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
9207 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
9208 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
9209 default:
9210 unsupported_reloc_global(object, r_type, gsym);
9211 break;
9212 }
9213 }
9214
9215 // Process relocations for gc.
9216
9217 template<bool big_endian>
9218 void
gc_process_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * object,unsigned int data_shndx,unsigned int,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)9219 Target_arm<big_endian>::gc_process_relocs(
9220 Symbol_table* symtab,
9221 Layout* layout,
9222 Sized_relobj_file<32, big_endian>* object,
9223 unsigned int data_shndx,
9224 unsigned int,
9225 const unsigned char* prelocs,
9226 size_t reloc_count,
9227 Output_section* output_section,
9228 bool needs_special_offset_handling,
9229 size_t local_symbol_count,
9230 const unsigned char* plocal_symbols)
9231 {
9232 typedef Target_arm<big_endian> Arm;
9233 typedef typename Target_arm<big_endian>::Scan Scan;
9234
9235 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan,
9236 typename Target_arm::Relocatable_size_for_reloc>(
9237 symtab,
9238 layout,
9239 this,
9240 object,
9241 data_shndx,
9242 prelocs,
9243 reloc_count,
9244 output_section,
9245 needs_special_offset_handling,
9246 local_symbol_count,
9247 plocal_symbols);
9248 }
9249
9250 // Scan relocations for a section.
9251
9252 template<bool big_endian>
9253 void
scan_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)9254 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
9255 Layout* layout,
9256 Sized_relobj_file<32, big_endian>* object,
9257 unsigned int data_shndx,
9258 unsigned int sh_type,
9259 const unsigned char* prelocs,
9260 size_t reloc_count,
9261 Output_section* output_section,
9262 bool needs_special_offset_handling,
9263 size_t local_symbol_count,
9264 const unsigned char* plocal_symbols)
9265 {
9266 typedef typename Target_arm<big_endian>::Scan Scan;
9267 if (sh_type == elfcpp::SHT_RELA)
9268 {
9269 gold_error(_("%s: unsupported RELA reloc section"),
9270 object->name().c_str());
9271 return;
9272 }
9273
9274 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
9275 symtab,
9276 layout,
9277 this,
9278 object,
9279 data_shndx,
9280 prelocs,
9281 reloc_count,
9282 output_section,
9283 needs_special_offset_handling,
9284 local_symbol_count,
9285 plocal_symbols);
9286 }
9287
9288 // Finalize the sections.
9289
9290 template<bool big_endian>
9291 void
do_finalize_sections(Layout * layout,const Input_objects * input_objects,Symbol_table *)9292 Target_arm<big_endian>::do_finalize_sections(
9293 Layout* layout,
9294 const Input_objects* input_objects,
9295 Symbol_table*)
9296 {
9297 bool merged_any_attributes = false;
9298 // Merge processor-specific flags.
9299 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9300 p != input_objects->relobj_end();
9301 ++p)
9302 {
9303 Arm_relobj<big_endian>* arm_relobj =
9304 Arm_relobj<big_endian>::as_arm_relobj(*p);
9305 if (arm_relobj->merge_flags_and_attributes())
9306 {
9307 this->merge_processor_specific_flags(
9308 arm_relobj->name(),
9309 arm_relobj->processor_specific_flags());
9310 this->merge_object_attributes(arm_relobj->name().c_str(),
9311 arm_relobj->attributes_section_data());
9312 merged_any_attributes = true;
9313 }
9314 }
9315
9316 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9317 p != input_objects->dynobj_end();
9318 ++p)
9319 {
9320 Arm_dynobj<big_endian>* arm_dynobj =
9321 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
9322 this->merge_processor_specific_flags(
9323 arm_dynobj->name(),
9324 arm_dynobj->processor_specific_flags());
9325 this->merge_object_attributes(arm_dynobj->name().c_str(),
9326 arm_dynobj->attributes_section_data());
9327 merged_any_attributes = true;
9328 }
9329
9330 // Create an empty uninitialized attribute section if we still don't have it
9331 // at this moment. This happens if there is no attributes sections in all
9332 // inputs.
9333 if (this->attributes_section_data_ == NULL)
9334 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9335
9336 const Object_attribute* cpu_arch_attr =
9337 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
9338 // Check if we need to use Cortex-A8 workaround.
9339 if (parameters->options().user_set_fix_cortex_a8())
9340 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
9341 else
9342 {
9343 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
9344 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
9345 // profile.
9346 const Object_attribute* cpu_arch_profile_attr =
9347 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
9348 this->fix_cortex_a8_ =
9349 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
9350 && (cpu_arch_profile_attr->int_value() == 'A'
9351 || cpu_arch_profile_attr->int_value() == 0));
9352 }
9353
9354 // Check if we can use V4BX interworking.
9355 // The V4BX interworking stub contains BX instruction,
9356 // which is not specified for some profiles.
9357 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
9358 && !this->may_use_v4t_interworking())
9359 gold_error(_("unable to provide V4BX reloc interworking fix up; "
9360 "the target profile does not support BX instruction"));
9361
9362 // Fill in some more dynamic tags.
9363 const Reloc_section* rel_plt = (this->plt_ == NULL
9364 ? NULL
9365 : this->plt_->rel_plt());
9366 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
9367 this->rel_dyn_, true, false);
9368
9369 // Emit any relocs we saved in an attempt to avoid generating COPY
9370 // relocs.
9371 if (this->copy_relocs_.any_saved_relocs())
9372 this->copy_relocs_.emit(this->rel_dyn_section(layout));
9373
9374 // Handle the .ARM.exidx section.
9375 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
9376
9377 if (!parameters->options().relocatable())
9378 {
9379 if (exidx_section != NULL
9380 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
9381 {
9382 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
9383 // the .ARM.exidx section.
9384 if (!layout->script_options()->saw_phdrs_clause())
9385 {
9386 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0,
9387 0)
9388 == NULL);
9389 Output_segment* exidx_segment =
9390 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
9391 exidx_segment->add_output_section_to_nonload(exidx_section,
9392 elfcpp::PF_R);
9393 }
9394 }
9395 }
9396
9397 // Create an .ARM.attributes section if we have merged any attributes
9398 // from inputs.
9399 if (merged_any_attributes)
9400 {
9401 Output_attributes_section_data* attributes_section =
9402 new Output_attributes_section_data(*this->attributes_section_data_);
9403 layout->add_output_section_data(".ARM.attributes",
9404 elfcpp::SHT_ARM_ATTRIBUTES, 0,
9405 attributes_section, ORDER_INVALID,
9406 false);
9407 }
9408
9409 // Fix up links in section EXIDX headers.
9410 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
9411 p != layout->section_list().end();
9412 ++p)
9413 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
9414 {
9415 Arm_output_section<big_endian>* os =
9416 Arm_output_section<big_endian>::as_arm_output_section(*p);
9417 os->set_exidx_section_link();
9418 }
9419 }
9420
9421 // Return whether a direct absolute static relocation needs to be applied.
9422 // In cases where Scan::local() or Scan::global() has created
9423 // a dynamic relocation other than R_ARM_RELATIVE, the addend
9424 // of the relocation is carried in the data, and we must not
9425 // apply the static relocation.
9426
9427 template<bool big_endian>
9428 inline bool
should_apply_static_reloc(const Sized_symbol<32> * gsym,unsigned int r_type,bool is_32bit,Output_section * output_section)9429 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
9430 const Sized_symbol<32>* gsym,
9431 unsigned int r_type,
9432 bool is_32bit,
9433 Output_section* output_section)
9434 {
9435 // If the output section is not allocated, then we didn't call
9436 // scan_relocs, we didn't create a dynamic reloc, and we must apply
9437 // the reloc here.
9438 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
9439 return true;
9440
9441 int ref_flags = Scan::get_reference_flags(r_type);
9442
9443 // For local symbols, we will have created a non-RELATIVE dynamic
9444 // relocation only if (a) the output is position independent,
9445 // (b) the relocation is absolute (not pc- or segment-relative), and
9446 // (c) the relocation is not 32 bits wide.
9447 if (gsym == NULL)
9448 return !(parameters->options().output_is_position_independent()
9449 && (ref_flags & Symbol::ABSOLUTE_REF)
9450 && !is_32bit);
9451
9452 // For global symbols, we use the same helper routines used in the
9453 // scan pass. If we did not create a dynamic relocation, or if we
9454 // created a RELATIVE dynamic relocation, we should apply the static
9455 // relocation.
9456 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
9457 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
9458 && gsym->can_use_relative_reloc(ref_flags
9459 & Symbol::FUNCTION_CALL);
9460 return !has_dyn || is_rel;
9461 }
9462
9463 // Perform a relocation.
9464
9465 template<bool big_endian>
9466 inline bool
relocate(const Relocate_info<32,big_endian> * relinfo,Target_arm * target,Output_section * output_section,size_t relnum,const elfcpp::Rel<32,big_endian> & rel,unsigned int r_type,const Sized_symbol<32> * gsym,const Symbol_value<32> * psymval,unsigned char * view,Arm_address address,section_size_type view_size)9467 Target_arm<big_endian>::Relocate::relocate(
9468 const Relocate_info<32, big_endian>* relinfo,
9469 Target_arm* target,
9470 Output_section* output_section,
9471 size_t relnum,
9472 const elfcpp::Rel<32, big_endian>& rel,
9473 unsigned int r_type,
9474 const Sized_symbol<32>* gsym,
9475 const Symbol_value<32>* psymval,
9476 unsigned char* view,
9477 Arm_address address,
9478 section_size_type view_size)
9479 {
9480 if (view == NULL)
9481 return true;
9482
9483 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
9484
9485 r_type = get_real_reloc_type(r_type);
9486 const Arm_reloc_property* reloc_property =
9487 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9488 if (reloc_property == NULL)
9489 {
9490 std::string reloc_name =
9491 arm_reloc_property_table->reloc_name_in_error_message(r_type);
9492 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9493 _("cannot relocate %s in object file"),
9494 reloc_name.c_str());
9495 return true;
9496 }
9497
9498 const Arm_relobj<big_endian>* object =
9499 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9500
9501 // If the final branch target of a relocation is THUMB instruction, this
9502 // is 1. Otherwise it is 0.
9503 Arm_address thumb_bit = 0;
9504 Symbol_value<32> symval;
9505 bool is_weakly_undefined_without_plt = false;
9506 bool have_got_offset = false;
9507 unsigned int got_offset = 0;
9508
9509 // If the relocation uses the GOT entry of a symbol instead of the symbol
9510 // itself, we don't care about whether the symbol is defined or what kind
9511 // of symbol it is.
9512 if (reloc_property->uses_got_entry())
9513 {
9514 // Get the GOT offset.
9515 // The GOT pointer points to the end of the GOT section.
9516 // We need to subtract the size of the GOT section to get
9517 // the actual offset to use in the relocation.
9518 // TODO: We should move GOT offset computing code in TLS relocations
9519 // to here.
9520 switch (r_type)
9521 {
9522 case elfcpp::R_ARM_GOT_BREL:
9523 case elfcpp::R_ARM_GOT_PREL:
9524 if (gsym != NULL)
9525 {
9526 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
9527 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
9528 - target->got_size());
9529 }
9530 else
9531 {
9532 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9533 gold_assert(object->local_has_got_offset(r_sym,
9534 GOT_TYPE_STANDARD));
9535 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
9536 - target->got_size());
9537 }
9538 have_got_offset = true;
9539 break;
9540
9541 default:
9542 break;
9543 }
9544 }
9545 else if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
9546 {
9547 if (gsym != NULL)
9548 {
9549 // This is a global symbol. Determine if we use PLT and if the
9550 // final target is THUMB.
9551 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
9552 {
9553 // This uses a PLT, change the symbol value.
9554 symval.set_output_value(target->plt_address_for_global(gsym));
9555 psymval = &symval;
9556 }
9557 else if (gsym->is_weak_undefined())
9558 {
9559 // This is a weakly undefined symbol and we do not use PLT
9560 // for this relocation. A branch targeting this symbol will
9561 // be converted into an NOP.
9562 is_weakly_undefined_without_plt = true;
9563 }
9564 else if (gsym->is_undefined() && reloc_property->uses_symbol())
9565 {
9566 // This relocation uses the symbol value but the symbol is
9567 // undefined. Exit early and have the caller reporting an
9568 // error.
9569 return true;
9570 }
9571 else
9572 {
9573 // Set thumb bit if symbol:
9574 // -Has type STT_ARM_TFUNC or
9575 // -Has type STT_FUNC, is defined and with LSB in value set.
9576 thumb_bit =
9577 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
9578 || (gsym->type() == elfcpp::STT_FUNC
9579 && !gsym->is_undefined()
9580 && ((psymval->value(object, 0) & 1) != 0)))
9581 ? 1
9582 : 0);
9583 }
9584 }
9585 else
9586 {
9587 // This is a local symbol. Determine if the final target is THUMB.
9588 // We saved this information when all the local symbols were read.
9589 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
9590 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9591 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
9592
9593 if (psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
9594 {
9595 symval.set_output_value(
9596 target->plt_address_for_local(object, r_sym));
9597 psymval = &symval;
9598 }
9599 }
9600 }
9601 else
9602 {
9603 // This is a fake relocation synthesized for a stub. It does not have
9604 // a real symbol. We just look at the LSB of the symbol value to
9605 // determine if the target is THUMB or not.
9606 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
9607 }
9608
9609 // Strip LSB if this points to a THUMB target.
9610 if (thumb_bit != 0
9611 && reloc_property->uses_thumb_bit()
9612 && ((psymval->value(object, 0) & 1) != 0))
9613 {
9614 Arm_address stripped_value =
9615 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
9616 symval.set_output_value(stripped_value);
9617 psymval = &symval;
9618 }
9619
9620 // To look up relocation stubs, we need to pass the symbol table index of
9621 // a local symbol.
9622 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9623
9624 // Get the addressing origin of the output segment defining the
9625 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
9626 Arm_address sym_origin = 0;
9627 if (reloc_property->uses_symbol_base())
9628 {
9629 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
9630 // R_ARM_BASE_ABS with the NULL symbol will give the
9631 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
9632 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
9633 sym_origin = target->got_plt_section()->address();
9634 else if (gsym == NULL)
9635 sym_origin = 0;
9636 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
9637 sym_origin = gsym->output_segment()->vaddr();
9638 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
9639 sym_origin = gsym->output_data()->address();
9640
9641 // TODO: Assumes the segment base to be zero for the global symbols
9642 // till the proper support for the segment-base-relative addressing
9643 // will be implemented. This is consistent with GNU ld.
9644 }
9645
9646 // For relative addressing relocation, find out the relative address base.
9647 Arm_address relative_address_base = 0;
9648 switch(reloc_property->relative_address_base())
9649 {
9650 case Arm_reloc_property::RAB_NONE:
9651 // Relocations with relative address bases RAB_TLS and RAB_tp are
9652 // handled by relocate_tls. So we do not need to do anything here.
9653 case Arm_reloc_property::RAB_TLS:
9654 case Arm_reloc_property::RAB_tp:
9655 break;
9656 case Arm_reloc_property::RAB_B_S:
9657 relative_address_base = sym_origin;
9658 break;
9659 case Arm_reloc_property::RAB_GOT_ORG:
9660 relative_address_base = target->got_plt_section()->address();
9661 break;
9662 case Arm_reloc_property::RAB_P:
9663 relative_address_base = address;
9664 break;
9665 case Arm_reloc_property::RAB_Pa:
9666 relative_address_base = address & 0xfffffffcU;
9667 break;
9668 default:
9669 gold_unreachable();
9670 }
9671
9672 typename Arm_relocate_functions::Status reloc_status =
9673 Arm_relocate_functions::STATUS_OKAY;
9674 bool check_overflow = reloc_property->checks_overflow();
9675 switch (r_type)
9676 {
9677 case elfcpp::R_ARM_NONE:
9678 break;
9679
9680 case elfcpp::R_ARM_ABS8:
9681 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9682 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
9683 break;
9684
9685 case elfcpp::R_ARM_ABS12:
9686 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9687 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
9688 break;
9689
9690 case elfcpp::R_ARM_ABS16:
9691 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9692 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
9693 break;
9694
9695 case elfcpp::R_ARM_ABS32:
9696 if (should_apply_static_reloc(gsym, r_type, true, output_section))
9697 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
9698 thumb_bit);
9699 break;
9700
9701 case elfcpp::R_ARM_ABS32_NOI:
9702 if (should_apply_static_reloc(gsym, r_type, true, output_section))
9703 // No thumb bit for this relocation: (S + A)
9704 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
9705 0);
9706 break;
9707
9708 case elfcpp::R_ARM_MOVW_ABS_NC:
9709 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9710 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
9711 0, thumb_bit,
9712 check_overflow);
9713 break;
9714
9715 case elfcpp::R_ARM_MOVT_ABS:
9716 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9717 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
9718 break;
9719
9720 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9721 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9722 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
9723 0, thumb_bit, false);
9724 break;
9725
9726 case elfcpp::R_ARM_THM_MOVT_ABS:
9727 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9728 reloc_status = Arm_relocate_functions::thm_movt(view, object,
9729 psymval, 0);
9730 break;
9731
9732 case elfcpp::R_ARM_MOVW_PREL_NC:
9733 case elfcpp::R_ARM_MOVW_BREL_NC:
9734 case elfcpp::R_ARM_MOVW_BREL:
9735 reloc_status =
9736 Arm_relocate_functions::movw(view, object, psymval,
9737 relative_address_base, thumb_bit,
9738 check_overflow);
9739 break;
9740
9741 case elfcpp::R_ARM_MOVT_PREL:
9742 case elfcpp::R_ARM_MOVT_BREL:
9743 reloc_status =
9744 Arm_relocate_functions::movt(view, object, psymval,
9745 relative_address_base);
9746 break;
9747
9748 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9749 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9750 case elfcpp::R_ARM_THM_MOVW_BREL:
9751 reloc_status =
9752 Arm_relocate_functions::thm_movw(view, object, psymval,
9753 relative_address_base,
9754 thumb_bit, check_overflow);
9755 break;
9756
9757 case elfcpp::R_ARM_THM_MOVT_PREL:
9758 case elfcpp::R_ARM_THM_MOVT_BREL:
9759 reloc_status =
9760 Arm_relocate_functions::thm_movt(view, object, psymval,
9761 relative_address_base);
9762 break;
9763
9764 case elfcpp::R_ARM_REL32:
9765 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9766 address, thumb_bit);
9767 break;
9768
9769 case elfcpp::R_ARM_THM_ABS5:
9770 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9771 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
9772 break;
9773
9774 // Thumb long branches.
9775 case elfcpp::R_ARM_THM_CALL:
9776 case elfcpp::R_ARM_THM_XPC22:
9777 case elfcpp::R_ARM_THM_JUMP24:
9778 reloc_status =
9779 Arm_relocate_functions::thumb_branch_common(
9780 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9781 thumb_bit, is_weakly_undefined_without_plt);
9782 break;
9783
9784 case elfcpp::R_ARM_GOTOFF32:
9785 {
9786 Arm_address got_origin;
9787 got_origin = target->got_plt_section()->address();
9788 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9789 got_origin, thumb_bit);
9790 }
9791 break;
9792
9793 case elfcpp::R_ARM_BASE_PREL:
9794 gold_assert(gsym != NULL);
9795 reloc_status =
9796 Arm_relocate_functions::base_prel(view, sym_origin, address);
9797 break;
9798
9799 case elfcpp::R_ARM_BASE_ABS:
9800 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9801 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
9802 break;
9803
9804 case elfcpp::R_ARM_GOT_BREL:
9805 gold_assert(have_got_offset);
9806 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
9807 break;
9808
9809 case elfcpp::R_ARM_GOT_PREL:
9810 gold_assert(have_got_offset);
9811 // Get the address origin for GOT PLT, which is allocated right
9812 // after the GOT section, to calculate an absolute address of
9813 // the symbol GOT entry (got_origin + got_offset).
9814 Arm_address got_origin;
9815 got_origin = target->got_plt_section()->address();
9816 reloc_status = Arm_relocate_functions::got_prel(view,
9817 got_origin + got_offset,
9818 address);
9819 break;
9820
9821 case elfcpp::R_ARM_PLT32:
9822 case elfcpp::R_ARM_CALL:
9823 case elfcpp::R_ARM_JUMP24:
9824 case elfcpp::R_ARM_XPC25:
9825 gold_assert(gsym == NULL
9826 || gsym->has_plt_offset()
9827 || gsym->final_value_is_known()
9828 || (gsym->is_defined()
9829 && !gsym->is_from_dynobj()
9830 && !gsym->is_preemptible()));
9831 reloc_status =
9832 Arm_relocate_functions::arm_branch_common(
9833 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9834 thumb_bit, is_weakly_undefined_without_plt);
9835 break;
9836
9837 case elfcpp::R_ARM_THM_JUMP19:
9838 reloc_status =
9839 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
9840 thumb_bit);
9841 break;
9842
9843 case elfcpp::R_ARM_THM_JUMP6:
9844 reloc_status =
9845 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
9846 break;
9847
9848 case elfcpp::R_ARM_THM_JUMP8:
9849 reloc_status =
9850 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
9851 break;
9852
9853 case elfcpp::R_ARM_THM_JUMP11:
9854 reloc_status =
9855 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
9856 break;
9857
9858 case elfcpp::R_ARM_PREL31:
9859 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
9860 address, thumb_bit);
9861 break;
9862
9863 case elfcpp::R_ARM_V4BX:
9864 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
9865 {
9866 const bool is_v4bx_interworking =
9867 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
9868 reloc_status =
9869 Arm_relocate_functions::v4bx(relinfo, view, object, address,
9870 is_v4bx_interworking);
9871 }
9872 break;
9873
9874 case elfcpp::R_ARM_THM_PC8:
9875 reloc_status =
9876 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
9877 break;
9878
9879 case elfcpp::R_ARM_THM_PC12:
9880 reloc_status =
9881 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
9882 break;
9883
9884 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9885 reloc_status =
9886 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
9887 thumb_bit);
9888 break;
9889
9890 case elfcpp::R_ARM_ALU_PC_G0_NC:
9891 case elfcpp::R_ARM_ALU_PC_G0:
9892 case elfcpp::R_ARM_ALU_PC_G1_NC:
9893 case elfcpp::R_ARM_ALU_PC_G1:
9894 case elfcpp::R_ARM_ALU_PC_G2:
9895 case elfcpp::R_ARM_ALU_SB_G0_NC:
9896 case elfcpp::R_ARM_ALU_SB_G0:
9897 case elfcpp::R_ARM_ALU_SB_G1_NC:
9898 case elfcpp::R_ARM_ALU_SB_G1:
9899 case elfcpp::R_ARM_ALU_SB_G2:
9900 reloc_status =
9901 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
9902 reloc_property->group_index(),
9903 relative_address_base,
9904 thumb_bit, check_overflow);
9905 break;
9906
9907 case elfcpp::R_ARM_LDR_PC_G0:
9908 case elfcpp::R_ARM_LDR_PC_G1:
9909 case elfcpp::R_ARM_LDR_PC_G2:
9910 case elfcpp::R_ARM_LDR_SB_G0:
9911 case elfcpp::R_ARM_LDR_SB_G1:
9912 case elfcpp::R_ARM_LDR_SB_G2:
9913 reloc_status =
9914 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
9915 reloc_property->group_index(),
9916 relative_address_base);
9917 break;
9918
9919 case elfcpp::R_ARM_LDRS_PC_G0:
9920 case elfcpp::R_ARM_LDRS_PC_G1:
9921 case elfcpp::R_ARM_LDRS_PC_G2:
9922 case elfcpp::R_ARM_LDRS_SB_G0:
9923 case elfcpp::R_ARM_LDRS_SB_G1:
9924 case elfcpp::R_ARM_LDRS_SB_G2:
9925 reloc_status =
9926 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
9927 reloc_property->group_index(),
9928 relative_address_base);
9929 break;
9930
9931 case elfcpp::R_ARM_LDC_PC_G0:
9932 case elfcpp::R_ARM_LDC_PC_G1:
9933 case elfcpp::R_ARM_LDC_PC_G2:
9934 case elfcpp::R_ARM_LDC_SB_G0:
9935 case elfcpp::R_ARM_LDC_SB_G1:
9936 case elfcpp::R_ARM_LDC_SB_G2:
9937 reloc_status =
9938 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
9939 reloc_property->group_index(),
9940 relative_address_base);
9941 break;
9942
9943 // These are initial tls relocs, which are expected when
9944 // linking.
9945 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9946 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9947 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9948 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9949 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9950 reloc_status =
9951 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
9952 view, address, view_size);
9953 break;
9954
9955 // The known and unknown unsupported and/or deprecated relocations.
9956 case elfcpp::R_ARM_PC24:
9957 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
9958 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
9959 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
9960 default:
9961 // Just silently leave the method. We should get an appropriate error
9962 // message in the scan methods.
9963 break;
9964 }
9965
9966 // Report any errors.
9967 switch (reloc_status)
9968 {
9969 case Arm_relocate_functions::STATUS_OKAY:
9970 break;
9971 case Arm_relocate_functions::STATUS_OVERFLOW:
9972 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9973 _("relocation overflow in %s"),
9974 reloc_property->name().c_str());
9975 break;
9976 case Arm_relocate_functions::STATUS_BAD_RELOC:
9977 gold_error_at_location(
9978 relinfo,
9979 relnum,
9980 rel.get_r_offset(),
9981 _("unexpected opcode while processing relocation %s"),
9982 reloc_property->name().c_str());
9983 break;
9984 default:
9985 gold_unreachable();
9986 }
9987
9988 return true;
9989 }
9990
9991 // Perform a TLS relocation.
9992
9993 template<bool big_endian>
9994 inline typename Arm_relocate_functions<big_endian>::Status
relocate_tls(const Relocate_info<32,big_endian> * relinfo,Target_arm<big_endian> * target,size_t relnum,const elfcpp::Rel<32,big_endian> & rel,unsigned int r_type,const Sized_symbol<32> * gsym,const Symbol_value<32> * psymval,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr address,section_size_type)9995 Target_arm<big_endian>::Relocate::relocate_tls(
9996 const Relocate_info<32, big_endian>* relinfo,
9997 Target_arm<big_endian>* target,
9998 size_t relnum,
9999 const elfcpp::Rel<32, big_endian>& rel,
10000 unsigned int r_type,
10001 const Sized_symbol<32>* gsym,
10002 const Symbol_value<32>* psymval,
10003 unsigned char* view,
10004 elfcpp::Elf_types<32>::Elf_Addr address,
10005 section_size_type /*view_size*/ )
10006 {
10007 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
10008 typedef Relocate_functions<32, big_endian> RelocFuncs;
10009 Output_segment* tls_segment = relinfo->layout->tls_segment();
10010
10011 const Sized_relobj_file<32, big_endian>* object = relinfo->object;
10012
10013 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
10014
10015 const bool is_final = (gsym == NULL
10016 ? !parameters->options().shared()
10017 : gsym->final_value_is_known());
10018 const tls::Tls_optimization optimized_type
10019 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
10020 switch (r_type)
10021 {
10022 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
10023 {
10024 unsigned int got_type = GOT_TYPE_TLS_PAIR;
10025 unsigned int got_offset;
10026 if (gsym != NULL)
10027 {
10028 gold_assert(gsym->has_got_offset(got_type));
10029 got_offset = gsym->got_offset(got_type) - target->got_size();
10030 }
10031 else
10032 {
10033 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
10034 gold_assert(object->local_has_got_offset(r_sym, got_type));
10035 got_offset = (object->local_got_offset(r_sym, got_type)
10036 - target->got_size());
10037 }
10038 if (optimized_type == tls::TLSOPT_NONE)
10039 {
10040 Arm_address got_entry =
10041 target->got_plt_section()->address() + got_offset;
10042
10043 // Relocate the field with the PC relative offset of the pair of
10044 // GOT entries.
10045 RelocFuncs::pcrel32_unaligned(view, got_entry, address);
10046 return ArmRelocFuncs::STATUS_OKAY;
10047 }
10048 }
10049 break;
10050
10051 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
10052 if (optimized_type == tls::TLSOPT_NONE)
10053 {
10054 // Relocate the field with the offset of the GOT entry for
10055 // the module index.
10056 unsigned int got_offset;
10057 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
10058 - target->got_size());
10059 Arm_address got_entry =
10060 target->got_plt_section()->address() + got_offset;
10061
10062 // Relocate the field with the PC relative offset of the pair of
10063 // GOT entries.
10064 RelocFuncs::pcrel32_unaligned(view, got_entry, address);
10065 return ArmRelocFuncs::STATUS_OKAY;
10066 }
10067 break;
10068
10069 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
10070 RelocFuncs::rel32_unaligned(view, value);
10071 return ArmRelocFuncs::STATUS_OKAY;
10072
10073 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
10074 if (optimized_type == tls::TLSOPT_NONE)
10075 {
10076 // Relocate the field with the offset of the GOT entry for
10077 // the tp-relative offset of the symbol.
10078 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
10079 unsigned int got_offset;
10080 if (gsym != NULL)
10081 {
10082 gold_assert(gsym->has_got_offset(got_type));
10083 got_offset = gsym->got_offset(got_type);
10084 }
10085 else
10086 {
10087 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
10088 gold_assert(object->local_has_got_offset(r_sym, got_type));
10089 got_offset = object->local_got_offset(r_sym, got_type);
10090 }
10091
10092 // All GOT offsets are relative to the end of the GOT.
10093 got_offset -= target->got_size();
10094
10095 Arm_address got_entry =
10096 target->got_plt_section()->address() + got_offset;
10097
10098 // Relocate the field with the PC relative offset of the GOT entry.
10099 RelocFuncs::pcrel32_unaligned(view, got_entry, address);
10100 return ArmRelocFuncs::STATUS_OKAY;
10101 }
10102 break;
10103
10104 case elfcpp::R_ARM_TLS_LE32: // Local-exec
10105 // If we're creating a shared library, a dynamic relocation will
10106 // have been created for this location, so do not apply it now.
10107 if (!parameters->options().shared())
10108 {
10109 gold_assert(tls_segment != NULL);
10110
10111 // $tp points to the TCB, which is followed by the TLS, so we
10112 // need to add TCB size to the offset.
10113 Arm_address aligned_tcb_size =
10114 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
10115 RelocFuncs::rel32_unaligned(view, value + aligned_tcb_size);
10116
10117 }
10118 return ArmRelocFuncs::STATUS_OKAY;
10119
10120 default:
10121 gold_unreachable();
10122 }
10123
10124 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
10125 _("unsupported reloc %u"),
10126 r_type);
10127 return ArmRelocFuncs::STATUS_BAD_RELOC;
10128 }
10129
10130 // Relocate section data.
10131
10132 template<bool big_endian>
10133 void
relocate_section(const Relocate_info<32,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,Arm_address address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)10134 Target_arm<big_endian>::relocate_section(
10135 const Relocate_info<32, big_endian>* relinfo,
10136 unsigned int sh_type,
10137 const unsigned char* prelocs,
10138 size_t reloc_count,
10139 Output_section* output_section,
10140 bool needs_special_offset_handling,
10141 unsigned char* view,
10142 Arm_address address,
10143 section_size_type view_size,
10144 const Reloc_symbol_changes* reloc_symbol_changes)
10145 {
10146 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
10147 gold_assert(sh_type == elfcpp::SHT_REL);
10148
10149 // See if we are relocating a relaxed input section. If so, the view
10150 // covers the whole output section and we need to adjust accordingly.
10151 if (needs_special_offset_handling)
10152 {
10153 const Output_relaxed_input_section* poris =
10154 output_section->find_relaxed_input_section(relinfo->object,
10155 relinfo->data_shndx);
10156 if (poris != NULL)
10157 {
10158 Arm_address section_address = poris->address();
10159 section_size_type section_size = poris->data_size();
10160
10161 gold_assert((section_address >= address)
10162 && ((section_address + section_size)
10163 <= (address + view_size)));
10164
10165 off_t offset = section_address - address;
10166 view += offset;
10167 address += offset;
10168 view_size = section_size;
10169 }
10170 }
10171
10172 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
10173 Arm_relocate, gold::Default_comdat_behavior>(
10174 relinfo,
10175 this,
10176 prelocs,
10177 reloc_count,
10178 output_section,
10179 needs_special_offset_handling,
10180 view,
10181 address,
10182 view_size,
10183 reloc_symbol_changes);
10184 }
10185
10186 // Return the size of a relocation while scanning during a relocatable
10187 // link.
10188
10189 template<bool big_endian>
10190 unsigned int
get_size_for_reloc(unsigned int r_type,Relobj * object)10191 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
10192 unsigned int r_type,
10193 Relobj* object)
10194 {
10195 r_type = get_real_reloc_type(r_type);
10196 const Arm_reloc_property* arp =
10197 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10198 if (arp != NULL)
10199 return arp->size();
10200 else
10201 {
10202 std::string reloc_name =
10203 arm_reloc_property_table->reloc_name_in_error_message(r_type);
10204 gold_error(_("%s: unexpected %s in object file"),
10205 object->name().c_str(), reloc_name.c_str());
10206 return 0;
10207 }
10208 }
10209
10210 // Scan the relocs during a relocatable link.
10211
10212 template<bool big_endian>
10213 void
scan_relocatable_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols,Relocatable_relocs * rr)10214 Target_arm<big_endian>::scan_relocatable_relocs(
10215 Symbol_table* symtab,
10216 Layout* layout,
10217 Sized_relobj_file<32, big_endian>* object,
10218 unsigned int data_shndx,
10219 unsigned int sh_type,
10220 const unsigned char* prelocs,
10221 size_t reloc_count,
10222 Output_section* output_section,
10223 bool needs_special_offset_handling,
10224 size_t local_symbol_count,
10225 const unsigned char* plocal_symbols,
10226 Relocatable_relocs* rr)
10227 {
10228 gold_assert(sh_type == elfcpp::SHT_REL);
10229
10230 typedef Arm_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
10231 Relocatable_size_for_reloc> Scan_relocatable_relocs;
10232
10233 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
10234 Scan_relocatable_relocs>(
10235 symtab,
10236 layout,
10237 object,
10238 data_shndx,
10239 prelocs,
10240 reloc_count,
10241 output_section,
10242 needs_special_offset_handling,
10243 local_symbol_count,
10244 plocal_symbols,
10245 rr);
10246 }
10247
10248 // Emit relocations for a section.
10249
10250 template<bool big_endian>
10251 void
relocate_relocs(const Relocate_info<32,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,const Relocatable_relocs * rr,unsigned char * view,Arm_address view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)10252 Target_arm<big_endian>::relocate_relocs(
10253 const Relocate_info<32, big_endian>* relinfo,
10254 unsigned int sh_type,
10255 const unsigned char* prelocs,
10256 size_t reloc_count,
10257 Output_section* output_section,
10258 typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
10259 const Relocatable_relocs* rr,
10260 unsigned char* view,
10261 Arm_address view_address,
10262 section_size_type view_size,
10263 unsigned char* reloc_view,
10264 section_size_type reloc_view_size)
10265 {
10266 gold_assert(sh_type == elfcpp::SHT_REL);
10267
10268 gold::relocate_relocs<32, big_endian, elfcpp::SHT_REL>(
10269 relinfo,
10270 prelocs,
10271 reloc_count,
10272 output_section,
10273 offset_in_output_section,
10274 rr,
10275 view,
10276 view_address,
10277 view_size,
10278 reloc_view,
10279 reloc_view_size);
10280 }
10281
10282 // Perform target-specific processing in a relocatable link. This is
10283 // only used if we use the relocation strategy RELOC_SPECIAL.
10284
10285 template<bool big_endian>
10286 void
relocate_special_relocatable(const Relocate_info<32,big_endian> * relinfo,unsigned int sh_type,const unsigned char * preloc_in,size_t relnum,Output_section * output_section,typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr view_address,section_size_type,unsigned char * preloc_out)10287 Target_arm<big_endian>::relocate_special_relocatable(
10288 const Relocate_info<32, big_endian>* relinfo,
10289 unsigned int sh_type,
10290 const unsigned char* preloc_in,
10291 size_t relnum,
10292 Output_section* output_section,
10293 typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
10294 unsigned char* view,
10295 elfcpp::Elf_types<32>::Elf_Addr view_address,
10296 section_size_type,
10297 unsigned char* preloc_out)
10298 {
10299 // We can only handle REL type relocation sections.
10300 gold_assert(sh_type == elfcpp::SHT_REL);
10301
10302 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc Reltype;
10303 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc_write
10304 Reltype_write;
10305 const Arm_address invalid_address = static_cast<Arm_address>(0) - 1;
10306
10307 const Arm_relobj<big_endian>* object =
10308 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10309 const unsigned int local_count = object->local_symbol_count();
10310
10311 Reltype reloc(preloc_in);
10312 Reltype_write reloc_write(preloc_out);
10313
10314 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10315 const unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10316 const unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10317
10318 const Arm_reloc_property* arp =
10319 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10320 gold_assert(arp != NULL);
10321
10322 // Get the new symbol index.
10323 // We only use RELOC_SPECIAL strategy in local relocations.
10324 gold_assert(r_sym < local_count);
10325
10326 // We are adjusting a section symbol. We need to find
10327 // the symbol table index of the section symbol for
10328 // the output section corresponding to input section
10329 // in which this symbol is defined.
10330 bool is_ordinary;
10331 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10332 gold_assert(is_ordinary);
10333 Output_section* os = object->output_section(shndx);
10334 gold_assert(os != NULL);
10335 gold_assert(os->needs_symtab_index());
10336 unsigned int new_symndx = os->symtab_index();
10337
10338 // Get the new offset--the location in the output section where
10339 // this relocation should be applied.
10340
10341 Arm_address offset = reloc.get_r_offset();
10342 Arm_address new_offset;
10343 if (offset_in_output_section != invalid_address)
10344 new_offset = offset + offset_in_output_section;
10345 else
10346 {
10347 section_offset_type sot_offset =
10348 convert_types<section_offset_type, Arm_address>(offset);
10349 section_offset_type new_sot_offset =
10350 output_section->output_offset(object, relinfo->data_shndx,
10351 sot_offset);
10352 gold_assert(new_sot_offset != -1);
10353 new_offset = new_sot_offset;
10354 }
10355
10356 // In an object file, r_offset is an offset within the section.
10357 // In an executable or dynamic object, generated by
10358 // --emit-relocs, r_offset is an absolute address.
10359 if (!parameters->options().relocatable())
10360 {
10361 new_offset += view_address;
10362 if (offset_in_output_section != invalid_address)
10363 new_offset -= offset_in_output_section;
10364 }
10365
10366 reloc_write.put_r_offset(new_offset);
10367 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10368
10369 // Handle the reloc addend.
10370 // The relocation uses a section symbol in the input file.
10371 // We are adjusting it to use a section symbol in the output
10372 // file. The input section symbol refers to some address in
10373 // the input section. We need the relocation in the output
10374 // file to refer to that same address. This adjustment to
10375 // the addend is the same calculation we use for a simple
10376 // absolute relocation for the input section symbol.
10377
10378 const Symbol_value<32>* psymval = object->local_symbol(r_sym);
10379
10380 // Handle THUMB bit.
10381 Symbol_value<32> symval;
10382 Arm_address thumb_bit =
10383 object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
10384 if (thumb_bit != 0
10385 && arp->uses_thumb_bit()
10386 && ((psymval->value(object, 0) & 1) != 0))
10387 {
10388 Arm_address stripped_value =
10389 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
10390 symval.set_output_value(stripped_value);
10391 psymval = &symval;
10392 }
10393
10394 unsigned char* paddend = view + offset;
10395 typename Arm_relocate_functions<big_endian>::Status reloc_status =
10396 Arm_relocate_functions<big_endian>::STATUS_OKAY;
10397 switch (r_type)
10398 {
10399 case elfcpp::R_ARM_ABS8:
10400 reloc_status = Arm_relocate_functions<big_endian>::abs8(paddend, object,
10401 psymval);
10402 break;
10403
10404 case elfcpp::R_ARM_ABS12:
10405 reloc_status = Arm_relocate_functions<big_endian>::abs12(paddend, object,
10406 psymval);
10407 break;
10408
10409 case elfcpp::R_ARM_ABS16:
10410 reloc_status = Arm_relocate_functions<big_endian>::abs16(paddend, object,
10411 psymval);
10412 break;
10413
10414 case elfcpp::R_ARM_THM_ABS5:
10415 reloc_status = Arm_relocate_functions<big_endian>::thm_abs5(paddend,
10416 object,
10417 psymval);
10418 break;
10419
10420 case elfcpp::R_ARM_MOVW_ABS_NC:
10421 case elfcpp::R_ARM_MOVW_PREL_NC:
10422 case elfcpp::R_ARM_MOVW_BREL_NC:
10423 case elfcpp::R_ARM_MOVW_BREL:
10424 reloc_status = Arm_relocate_functions<big_endian>::movw(
10425 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
10426 break;
10427
10428 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
10429 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
10430 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
10431 case elfcpp::R_ARM_THM_MOVW_BREL:
10432 reloc_status = Arm_relocate_functions<big_endian>::thm_movw(
10433 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
10434 break;
10435
10436 case elfcpp::R_ARM_THM_CALL:
10437 case elfcpp::R_ARM_THM_XPC22:
10438 case elfcpp::R_ARM_THM_JUMP24:
10439 reloc_status =
10440 Arm_relocate_functions<big_endian>::thumb_branch_common(
10441 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
10442 false);
10443 break;
10444
10445 case elfcpp::R_ARM_PLT32:
10446 case elfcpp::R_ARM_CALL:
10447 case elfcpp::R_ARM_JUMP24:
10448 case elfcpp::R_ARM_XPC25:
10449 reloc_status =
10450 Arm_relocate_functions<big_endian>::arm_branch_common(
10451 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
10452 false);
10453 break;
10454
10455 case elfcpp::R_ARM_THM_JUMP19:
10456 reloc_status =
10457 Arm_relocate_functions<big_endian>::thm_jump19(paddend, object,
10458 psymval, 0, thumb_bit);
10459 break;
10460
10461 case elfcpp::R_ARM_THM_JUMP6:
10462 reloc_status =
10463 Arm_relocate_functions<big_endian>::thm_jump6(paddend, object, psymval,
10464 0);
10465 break;
10466
10467 case elfcpp::R_ARM_THM_JUMP8:
10468 reloc_status =
10469 Arm_relocate_functions<big_endian>::thm_jump8(paddend, object, psymval,
10470 0);
10471 break;
10472
10473 case elfcpp::R_ARM_THM_JUMP11:
10474 reloc_status =
10475 Arm_relocate_functions<big_endian>::thm_jump11(paddend, object, psymval,
10476 0);
10477 break;
10478
10479 case elfcpp::R_ARM_PREL31:
10480 reloc_status =
10481 Arm_relocate_functions<big_endian>::prel31(paddend, object, psymval, 0,
10482 thumb_bit);
10483 break;
10484
10485 case elfcpp::R_ARM_THM_PC8:
10486 reloc_status =
10487 Arm_relocate_functions<big_endian>::thm_pc8(paddend, object, psymval,
10488 0);
10489 break;
10490
10491 case elfcpp::R_ARM_THM_PC12:
10492 reloc_status =
10493 Arm_relocate_functions<big_endian>::thm_pc12(paddend, object, psymval,
10494 0);
10495 break;
10496
10497 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
10498 reloc_status =
10499 Arm_relocate_functions<big_endian>::thm_alu11(paddend, object, psymval,
10500 0, thumb_bit);
10501 break;
10502
10503 // These relocation truncate relocation results so we cannot handle them
10504 // in a relocatable link.
10505 case elfcpp::R_ARM_MOVT_ABS:
10506 case elfcpp::R_ARM_THM_MOVT_ABS:
10507 case elfcpp::R_ARM_MOVT_PREL:
10508 case elfcpp::R_ARM_MOVT_BREL:
10509 case elfcpp::R_ARM_THM_MOVT_PREL:
10510 case elfcpp::R_ARM_THM_MOVT_BREL:
10511 case elfcpp::R_ARM_ALU_PC_G0_NC:
10512 case elfcpp::R_ARM_ALU_PC_G0:
10513 case elfcpp::R_ARM_ALU_PC_G1_NC:
10514 case elfcpp::R_ARM_ALU_PC_G1:
10515 case elfcpp::R_ARM_ALU_PC_G2:
10516 case elfcpp::R_ARM_ALU_SB_G0_NC:
10517 case elfcpp::R_ARM_ALU_SB_G0:
10518 case elfcpp::R_ARM_ALU_SB_G1_NC:
10519 case elfcpp::R_ARM_ALU_SB_G1:
10520 case elfcpp::R_ARM_ALU_SB_G2:
10521 case elfcpp::R_ARM_LDR_PC_G0:
10522 case elfcpp::R_ARM_LDR_PC_G1:
10523 case elfcpp::R_ARM_LDR_PC_G2:
10524 case elfcpp::R_ARM_LDR_SB_G0:
10525 case elfcpp::R_ARM_LDR_SB_G1:
10526 case elfcpp::R_ARM_LDR_SB_G2:
10527 case elfcpp::R_ARM_LDRS_PC_G0:
10528 case elfcpp::R_ARM_LDRS_PC_G1:
10529 case elfcpp::R_ARM_LDRS_PC_G2:
10530 case elfcpp::R_ARM_LDRS_SB_G0:
10531 case elfcpp::R_ARM_LDRS_SB_G1:
10532 case elfcpp::R_ARM_LDRS_SB_G2:
10533 case elfcpp::R_ARM_LDC_PC_G0:
10534 case elfcpp::R_ARM_LDC_PC_G1:
10535 case elfcpp::R_ARM_LDC_PC_G2:
10536 case elfcpp::R_ARM_LDC_SB_G0:
10537 case elfcpp::R_ARM_LDC_SB_G1:
10538 case elfcpp::R_ARM_LDC_SB_G2:
10539 gold_error(_("cannot handle %s in a relocatable link"),
10540 arp->name().c_str());
10541 break;
10542
10543 default:
10544 gold_unreachable();
10545 }
10546
10547 // Report any errors.
10548 switch (reloc_status)
10549 {
10550 case Arm_relocate_functions<big_endian>::STATUS_OKAY:
10551 break;
10552 case Arm_relocate_functions<big_endian>::STATUS_OVERFLOW:
10553 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10554 _("relocation overflow in %s"),
10555 arp->name().c_str());
10556 break;
10557 case Arm_relocate_functions<big_endian>::STATUS_BAD_RELOC:
10558 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10559 _("unexpected opcode while processing relocation %s"),
10560 arp->name().c_str());
10561 break;
10562 default:
10563 gold_unreachable();
10564 }
10565 }
10566
10567 // Return the value to use for a dynamic symbol which requires special
10568 // treatment. This is how we support equality comparisons of function
10569 // pointers across shared library boundaries, as described in the
10570 // processor specific ABI supplement.
10571
10572 template<bool big_endian>
10573 uint64_t
do_dynsym_value(const Symbol * gsym) const10574 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
10575 {
10576 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
10577 return this->plt_address_for_global(gsym);
10578 }
10579
10580 // Map platform-specific relocs to real relocs
10581 //
10582 template<bool big_endian>
10583 unsigned int
get_real_reloc_type(unsigned int r_type)10584 Target_arm<big_endian>::get_real_reloc_type(unsigned int r_type)
10585 {
10586 switch (r_type)
10587 {
10588 case elfcpp::R_ARM_TARGET1:
10589 // This is either R_ARM_ABS32 or R_ARM_REL32;
10590 return elfcpp::R_ARM_ABS32;
10591
10592 case elfcpp::R_ARM_TARGET2:
10593 // This can be any reloc type but usually is R_ARM_GOT_PREL
10594 return elfcpp::R_ARM_GOT_PREL;
10595
10596 default:
10597 return r_type;
10598 }
10599 }
10600
10601 // Whether if two EABI versions V1 and V2 are compatible.
10602
10603 template<bool big_endian>
10604 bool
are_eabi_versions_compatible(elfcpp::Elf_Word v1,elfcpp::Elf_Word v2)10605 Target_arm<big_endian>::are_eabi_versions_compatible(
10606 elfcpp::Elf_Word v1,
10607 elfcpp::Elf_Word v2)
10608 {
10609 // v4 and v5 are the same spec before and after it was released,
10610 // so allow mixing them.
10611 if ((v1 == elfcpp::EF_ARM_EABI_UNKNOWN || v2 == elfcpp::EF_ARM_EABI_UNKNOWN)
10612 || (v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
10613 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
10614 return true;
10615
10616 return v1 == v2;
10617 }
10618
10619 // Combine FLAGS from an input object called NAME and the processor-specific
10620 // flags in the ELF header of the output. Much of this is adapted from the
10621 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
10622 // in bfd/elf32-arm.c.
10623
10624 template<bool big_endian>
10625 void
merge_processor_specific_flags(const std::string & name,elfcpp::Elf_Word flags)10626 Target_arm<big_endian>::merge_processor_specific_flags(
10627 const std::string& name,
10628 elfcpp::Elf_Word flags)
10629 {
10630 if (this->are_processor_specific_flags_set())
10631 {
10632 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
10633
10634 // Nothing to merge if flags equal to those in output.
10635 if (flags == out_flags)
10636 return;
10637
10638 // Complain about various flag mismatches.
10639 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
10640 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
10641 if (!this->are_eabi_versions_compatible(version1, version2)
10642 && parameters->options().warn_mismatch())
10643 gold_error(_("Source object %s has EABI version %d but output has "
10644 "EABI version %d."),
10645 name.c_str(),
10646 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
10647 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
10648 }
10649 else
10650 {
10651 // If the input is the default architecture and had the default
10652 // flags then do not bother setting the flags for the output
10653 // architecture, instead allow future merges to do this. If no
10654 // future merges ever set these flags then they will retain their
10655 // uninitialised values, which surprise surprise, correspond
10656 // to the default values.
10657 if (flags == 0)
10658 return;
10659
10660 // This is the first time, just copy the flags.
10661 // We only copy the EABI version for now.
10662 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
10663 }
10664 }
10665
10666 // Adjust ELF file header.
10667 template<bool big_endian>
10668 void
do_adjust_elf_header(unsigned char * view,int len)10669 Target_arm<big_endian>::do_adjust_elf_header(
10670 unsigned char* view,
10671 int len)
10672 {
10673 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
10674
10675 elfcpp::Ehdr<32, big_endian> ehdr(view);
10676 elfcpp::Elf_Word flags = this->processor_specific_flags();
10677 unsigned char e_ident[elfcpp::EI_NIDENT];
10678 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
10679
10680 if (elfcpp::arm_eabi_version(flags)
10681 == elfcpp::EF_ARM_EABI_UNKNOWN)
10682 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
10683 else
10684 e_ident[elfcpp::EI_OSABI] = 0;
10685 e_ident[elfcpp::EI_ABIVERSION] = 0;
10686
10687 // FIXME: Do EF_ARM_BE8 adjustment.
10688
10689 // If we're working in EABI_VER5, set the hard/soft float ABI flags
10690 // as appropriate.
10691 if (elfcpp::arm_eabi_version(flags) == elfcpp::EF_ARM_EABI_VER5)
10692 {
10693 elfcpp::Elf_Half type = ehdr.get_e_type();
10694 if (type == elfcpp::ET_EXEC || type == elfcpp::ET_DYN)
10695 {
10696 Object_attribute* attr = this->get_aeabi_object_attribute(elfcpp::Tag_ABI_VFP_args);
10697 if (attr->int_value() == elfcpp::AEABI_VFP_args_vfp)
10698 flags |= elfcpp::EF_ARM_ABI_FLOAT_HARD;
10699 else
10700 flags |= elfcpp::EF_ARM_ABI_FLOAT_SOFT;
10701 this->set_processor_specific_flags(flags);
10702 }
10703 }
10704 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
10705 oehdr.put_e_ident(e_ident);
10706 oehdr.put_e_flags(this->processor_specific_flags());
10707 }
10708
10709 // do_make_elf_object to override the same function in the base class.
10710 // We need to use a target-specific sub-class of
10711 // Sized_relobj_file<32, big_endian> to store ARM specific information.
10712 // Hence we need to have our own ELF object creation.
10713
10714 template<bool big_endian>
10715 Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<32,big_endian> & ehdr)10716 Target_arm<big_endian>::do_make_elf_object(
10717 const std::string& name,
10718 Input_file* input_file,
10719 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
10720 {
10721 int et = ehdr.get_e_type();
10722 // ET_EXEC files are valid input for --just-symbols/-R,
10723 // and we treat them as relocatable objects.
10724 if (et == elfcpp::ET_REL
10725 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
10726 {
10727 Arm_relobj<big_endian>* obj =
10728 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
10729 obj->setup();
10730 return obj;
10731 }
10732 else if (et == elfcpp::ET_DYN)
10733 {
10734 Sized_dynobj<32, big_endian>* obj =
10735 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
10736 obj->setup();
10737 return obj;
10738 }
10739 else
10740 {
10741 gold_error(_("%s: unsupported ELF file type %d"),
10742 name.c_str(), et);
10743 return NULL;
10744 }
10745 }
10746
10747 // Read the architecture from the Tag_also_compatible_with attribute, if any.
10748 // Returns -1 if no architecture could be read.
10749 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
10750
10751 template<bool big_endian>
10752 int
get_secondary_compatible_arch(const Attributes_section_data * pasd)10753 Target_arm<big_endian>::get_secondary_compatible_arch(
10754 const Attributes_section_data* pasd)
10755 {
10756 const Object_attribute* known_attributes =
10757 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10758
10759 // Note: the tag and its argument below are uleb128 values, though
10760 // currently-defined values fit in one byte for each.
10761 const std::string& sv =
10762 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
10763 if (sv.size() == 2
10764 && sv.data()[0] == elfcpp::Tag_CPU_arch
10765 && (sv.data()[1] & 128) != 128)
10766 return sv.data()[1];
10767
10768 // This tag is "safely ignorable", so don't complain if it looks funny.
10769 return -1;
10770 }
10771
10772 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
10773 // The tag is removed if ARCH is -1.
10774 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
10775
10776 template<bool big_endian>
10777 void
set_secondary_compatible_arch(Attributes_section_data * pasd,int arch)10778 Target_arm<big_endian>::set_secondary_compatible_arch(
10779 Attributes_section_data* pasd,
10780 int arch)
10781 {
10782 Object_attribute* known_attributes =
10783 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10784
10785 if (arch == -1)
10786 {
10787 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
10788 return;
10789 }
10790
10791 // Note: the tag and its argument below are uleb128 values, though
10792 // currently-defined values fit in one byte for each.
10793 char sv[3];
10794 sv[0] = elfcpp::Tag_CPU_arch;
10795 gold_assert(arch != 0);
10796 sv[1] = arch;
10797 sv[2] = '\0';
10798
10799 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
10800 }
10801
10802 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
10803 // into account.
10804 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
10805
10806 template<bool big_endian>
10807 int
tag_cpu_arch_combine(const char * name,int oldtag,int * secondary_compat_out,int newtag,int secondary_compat)10808 Target_arm<big_endian>::tag_cpu_arch_combine(
10809 const char* name,
10810 int oldtag,
10811 int* secondary_compat_out,
10812 int newtag,
10813 int secondary_compat)
10814 {
10815 #define T(X) elfcpp::TAG_CPU_ARCH_##X
10816 static const int v6t2[] =
10817 {
10818 T(V6T2), // PRE_V4.
10819 T(V6T2), // V4.
10820 T(V6T2), // V4T.
10821 T(V6T2), // V5T.
10822 T(V6T2), // V5TE.
10823 T(V6T2), // V5TEJ.
10824 T(V6T2), // V6.
10825 T(V7), // V6KZ.
10826 T(V6T2) // V6T2.
10827 };
10828 static const int v6k[] =
10829 {
10830 T(V6K), // PRE_V4.
10831 T(V6K), // V4.
10832 T(V6K), // V4T.
10833 T(V6K), // V5T.
10834 T(V6K), // V5TE.
10835 T(V6K), // V5TEJ.
10836 T(V6K), // V6.
10837 T(V6KZ), // V6KZ.
10838 T(V7), // V6T2.
10839 T(V6K) // V6K.
10840 };
10841 static const int v7[] =
10842 {
10843 T(V7), // PRE_V4.
10844 T(V7), // V4.
10845 T(V7), // V4T.
10846 T(V7), // V5T.
10847 T(V7), // V5TE.
10848 T(V7), // V5TEJ.
10849 T(V7), // V6.
10850 T(V7), // V6KZ.
10851 T(V7), // V6T2.
10852 T(V7), // V6K.
10853 T(V7) // V7.
10854 };
10855 static const int v6_m[] =
10856 {
10857 -1, // PRE_V4.
10858 -1, // V4.
10859 T(V6K), // V4T.
10860 T(V6K), // V5T.
10861 T(V6K), // V5TE.
10862 T(V6K), // V5TEJ.
10863 T(V6K), // V6.
10864 T(V6KZ), // V6KZ.
10865 T(V7), // V6T2.
10866 T(V6K), // V6K.
10867 T(V7), // V7.
10868 T(V6_M) // V6_M.
10869 };
10870 static const int v6s_m[] =
10871 {
10872 -1, // PRE_V4.
10873 -1, // V4.
10874 T(V6K), // V4T.
10875 T(V6K), // V5T.
10876 T(V6K), // V5TE.
10877 T(V6K), // V5TEJ.
10878 T(V6K), // V6.
10879 T(V6KZ), // V6KZ.
10880 T(V7), // V6T2.
10881 T(V6K), // V6K.
10882 T(V7), // V7.
10883 T(V6S_M), // V6_M.
10884 T(V6S_M) // V6S_M.
10885 };
10886 static const int v7e_m[] =
10887 {
10888 -1, // PRE_V4.
10889 -1, // V4.
10890 T(V7E_M), // V4T.
10891 T(V7E_M), // V5T.
10892 T(V7E_M), // V5TE.
10893 T(V7E_M), // V5TEJ.
10894 T(V7E_M), // V6.
10895 T(V7E_M), // V6KZ.
10896 T(V7E_M), // V6T2.
10897 T(V7E_M), // V6K.
10898 T(V7E_M), // V7.
10899 T(V7E_M), // V6_M.
10900 T(V7E_M), // V6S_M.
10901 T(V7E_M) // V7E_M.
10902 };
10903 static const int v8[] =
10904 {
10905 T(V8), // PRE_V4.
10906 T(V8), // V4.
10907 T(V8), // V4T.
10908 T(V8), // V5T.
10909 T(V8), // V5TE.
10910 T(V8), // V5TEJ.
10911 T(V8), // V6.
10912 T(V8), // V6KZ.
10913 T(V8), // V6T2.
10914 T(V8), // V6K.
10915 T(V8), // V7.
10916 T(V8), // V6_M.
10917 T(V8), // V6S_M.
10918 T(V8), // V7E_M.
10919 T(V8) // V8.
10920 };
10921 static const int v4t_plus_v6_m[] =
10922 {
10923 -1, // PRE_V4.
10924 -1, // V4.
10925 T(V4T), // V4T.
10926 T(V5T), // V5T.
10927 T(V5TE), // V5TE.
10928 T(V5TEJ), // V5TEJ.
10929 T(V6), // V6.
10930 T(V6KZ), // V6KZ.
10931 T(V6T2), // V6T2.
10932 T(V6K), // V6K.
10933 T(V7), // V7.
10934 T(V6_M), // V6_M.
10935 T(V6S_M), // V6S_M.
10936 T(V7E_M), // V7E_M.
10937 T(V8), // V8.
10938 T(V4T_PLUS_V6_M) // V4T plus V6_M.
10939 };
10940 static const int* comb[] =
10941 {
10942 v6t2,
10943 v6k,
10944 v7,
10945 v6_m,
10946 v6s_m,
10947 v7e_m,
10948 v8,
10949 // Pseudo-architecture.
10950 v4t_plus_v6_m
10951 };
10952
10953 // Check we've not got a higher architecture than we know about.
10954
10955 if (oldtag > elfcpp::MAX_TAG_CPU_ARCH || newtag > elfcpp::MAX_TAG_CPU_ARCH)
10956 {
10957 gold_error(_("%s: unknown CPU architecture"), name);
10958 return -1;
10959 }
10960
10961 // Override old tag if we have a Tag_also_compatible_with on the output.
10962
10963 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
10964 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
10965 oldtag = T(V4T_PLUS_V6_M);
10966
10967 // And override the new tag if we have a Tag_also_compatible_with on the
10968 // input.
10969
10970 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
10971 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
10972 newtag = T(V4T_PLUS_V6_M);
10973
10974 // Architectures before V6KZ add features monotonically.
10975 int tagh = std::max(oldtag, newtag);
10976 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
10977 return tagh;
10978
10979 int tagl = std::min(oldtag, newtag);
10980 int result = comb[tagh - T(V6T2)][tagl];
10981
10982 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
10983 // as the canonical version.
10984 if (result == T(V4T_PLUS_V6_M))
10985 {
10986 result = T(V4T);
10987 *secondary_compat_out = T(V6_M);
10988 }
10989 else
10990 *secondary_compat_out = -1;
10991
10992 if (result == -1)
10993 {
10994 gold_error(_("%s: conflicting CPU architectures %d/%d"),
10995 name, oldtag, newtag);
10996 return -1;
10997 }
10998
10999 return result;
11000 #undef T
11001 }
11002
11003 // Helper to print AEABI enum tag value.
11004
11005 template<bool big_endian>
11006 std::string
aeabi_enum_name(unsigned int value)11007 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
11008 {
11009 static const char* aeabi_enum_names[] =
11010 { "", "variable-size", "32-bit", "" };
11011 const size_t aeabi_enum_names_size =
11012 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
11013
11014 if (value < aeabi_enum_names_size)
11015 return std::string(aeabi_enum_names[value]);
11016 else
11017 {
11018 char buffer[100];
11019 sprintf(buffer, "<unknown value %u>", value);
11020 return std::string(buffer);
11021 }
11022 }
11023
11024 // Return the string value to store in TAG_CPU_name.
11025
11026 template<bool big_endian>
11027 std::string
tag_cpu_name_value(unsigned int value)11028 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
11029 {
11030 static const char* name_table[] = {
11031 // These aren't real CPU names, but we can't guess
11032 // that from the architecture version alone.
11033 "Pre v4",
11034 "ARM v4",
11035 "ARM v4T",
11036 "ARM v5T",
11037 "ARM v5TE",
11038 "ARM v5TEJ",
11039 "ARM v6",
11040 "ARM v6KZ",
11041 "ARM v6T2",
11042 "ARM v6K",
11043 "ARM v7",
11044 "ARM v6-M",
11045 "ARM v6S-M",
11046 "ARM v7E-M",
11047 "ARM v8"
11048 };
11049 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
11050
11051 if (value < name_table_size)
11052 return std::string(name_table[value]);
11053 else
11054 {
11055 char buffer[100];
11056 sprintf(buffer, "<unknown CPU value %u>", value);
11057 return std::string(buffer);
11058 }
11059 }
11060
11061 // Query attributes object to see if integer divide instructions may be
11062 // present in an object.
11063
11064 template<bool big_endian>
11065 bool
attributes_accept_div(int arch,int profile,const Object_attribute * div_attr)11066 Target_arm<big_endian>::attributes_accept_div(int arch, int profile,
11067 const Object_attribute* div_attr)
11068 {
11069 switch (div_attr->int_value())
11070 {
11071 case 0:
11072 // Integer divide allowed if instruction contained in
11073 // archetecture.
11074 if (arch == elfcpp::TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
11075 return true;
11076 else if (arch >= elfcpp::TAG_CPU_ARCH_V7E_M)
11077 return true;
11078 else
11079 return false;
11080
11081 case 1:
11082 // Integer divide explicitly prohibited.
11083 return false;
11084
11085 default:
11086 // Unrecognised case - treat as allowing divide everywhere.
11087 case 2:
11088 // Integer divide allowed in ARM state.
11089 return true;
11090 }
11091 }
11092
11093 // Query attributes object to see if integer divide instructions are
11094 // forbidden to be in the object. This is not the inverse of
11095 // attributes_accept_div.
11096
11097 template<bool big_endian>
11098 bool
attributes_forbid_div(const Object_attribute * div_attr)11099 Target_arm<big_endian>::attributes_forbid_div(const Object_attribute* div_attr)
11100 {
11101 return div_attr->int_value() == 1;
11102 }
11103
11104 // Merge object attributes from input file called NAME with those of the
11105 // output. The input object attributes are in the object pointed by PASD.
11106
11107 template<bool big_endian>
11108 void
merge_object_attributes(const char * name,const Attributes_section_data * pasd)11109 Target_arm<big_endian>::merge_object_attributes(
11110 const char* name,
11111 const Attributes_section_data* pasd)
11112 {
11113 // Return if there is no attributes section data.
11114 if (pasd == NULL)
11115 return;
11116
11117 // If output has no object attributes, just copy.
11118 const int vendor = Object_attribute::OBJ_ATTR_PROC;
11119 if (this->attributes_section_data_ == NULL)
11120 {
11121 this->attributes_section_data_ = new Attributes_section_data(*pasd);
11122 Object_attribute* out_attr =
11123 this->attributes_section_data_->known_attributes(vendor);
11124
11125 // We do not output objects with Tag_MPextension_use_legacy - we move
11126 // the attribute's value to Tag_MPextension_use. */
11127 if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
11128 {
11129 if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
11130 && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
11131 != out_attr[elfcpp::Tag_MPextension_use].int_value())
11132 {
11133 gold_error(_("%s has both the current and legacy "
11134 "Tag_MPextension_use attributes"),
11135 name);
11136 }
11137
11138 out_attr[elfcpp::Tag_MPextension_use] =
11139 out_attr[elfcpp::Tag_MPextension_use_legacy];
11140 out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
11141 out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
11142 }
11143
11144 return;
11145 }
11146
11147 const Object_attribute* in_attr = pasd->known_attributes(vendor);
11148 Object_attribute* out_attr =
11149 this->attributes_section_data_->known_attributes(vendor);
11150
11151 // This needs to happen before Tag_ABI_FP_number_model is merged. */
11152 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
11153 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
11154 {
11155 // Ignore mismatches if the object doesn't use floating point. */
11156 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
11157 == elfcpp::AEABI_FP_number_model_none
11158 || (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
11159 != elfcpp::AEABI_FP_number_model_none
11160 && out_attr[elfcpp::Tag_ABI_VFP_args].int_value()
11161 == elfcpp::AEABI_VFP_args_compatible))
11162 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
11163 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
11164 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
11165 != elfcpp::AEABI_FP_number_model_none
11166 && in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
11167 != elfcpp::AEABI_VFP_args_compatible
11168 && parameters->options().warn_mismatch())
11169 gold_error(_("%s uses VFP register arguments, output does not"),
11170 name);
11171 }
11172
11173 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
11174 {
11175 // Merge this attribute with existing attributes.
11176 switch (i)
11177 {
11178 case elfcpp::Tag_CPU_raw_name:
11179 case elfcpp::Tag_CPU_name:
11180 // These are merged after Tag_CPU_arch.
11181 break;
11182
11183 case elfcpp::Tag_ABI_optimization_goals:
11184 case elfcpp::Tag_ABI_FP_optimization_goals:
11185 // Use the first value seen.
11186 break;
11187
11188 case elfcpp::Tag_CPU_arch:
11189 {
11190 unsigned int saved_out_attr = out_attr->int_value();
11191 // Merge Tag_CPU_arch and Tag_also_compatible_with.
11192 int secondary_compat =
11193 this->get_secondary_compatible_arch(pasd);
11194 int secondary_compat_out =
11195 this->get_secondary_compatible_arch(
11196 this->attributes_section_data_);
11197 out_attr[i].set_int_value(
11198 tag_cpu_arch_combine(name, out_attr[i].int_value(),
11199 &secondary_compat_out,
11200 in_attr[i].int_value(),
11201 secondary_compat));
11202 this->set_secondary_compatible_arch(this->attributes_section_data_,
11203 secondary_compat_out);
11204
11205 // Merge Tag_CPU_name and Tag_CPU_raw_name.
11206 if (out_attr[i].int_value() == saved_out_attr)
11207 ; // Leave the names alone.
11208 else if (out_attr[i].int_value() == in_attr[i].int_value())
11209 {
11210 // The output architecture has been changed to match the
11211 // input architecture. Use the input names.
11212 out_attr[elfcpp::Tag_CPU_name].set_string_value(
11213 in_attr[elfcpp::Tag_CPU_name].string_value());
11214 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
11215 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
11216 }
11217 else
11218 {
11219 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
11220 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
11221 }
11222
11223 // If we still don't have a value for Tag_CPU_name,
11224 // make one up now. Tag_CPU_raw_name remains blank.
11225 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
11226 {
11227 const std::string cpu_name =
11228 this->tag_cpu_name_value(out_attr[i].int_value());
11229 // FIXME: If we see an unknown CPU, this will be set
11230 // to "<unknown CPU n>", where n is the attribute value.
11231 // This is different from BFD, which leaves the name alone.
11232 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
11233 }
11234 }
11235 break;
11236
11237 case elfcpp::Tag_ARM_ISA_use:
11238 case elfcpp::Tag_THUMB_ISA_use:
11239 case elfcpp::Tag_WMMX_arch:
11240 case elfcpp::Tag_Advanced_SIMD_arch:
11241 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
11242 case elfcpp::Tag_ABI_FP_rounding:
11243 case elfcpp::Tag_ABI_FP_exceptions:
11244 case elfcpp::Tag_ABI_FP_user_exceptions:
11245 case elfcpp::Tag_ABI_FP_number_model:
11246 case elfcpp::Tag_VFP_HP_extension:
11247 case elfcpp::Tag_CPU_unaligned_access:
11248 case elfcpp::Tag_T2EE_use:
11249 case elfcpp::Tag_Virtualization_use:
11250 case elfcpp::Tag_MPextension_use:
11251 // Use the largest value specified.
11252 if (in_attr[i].int_value() > out_attr[i].int_value())
11253 out_attr[i].set_int_value(in_attr[i].int_value());
11254 break;
11255
11256 case elfcpp::Tag_ABI_align8_preserved:
11257 case elfcpp::Tag_ABI_PCS_RO_data:
11258 // Use the smallest value specified.
11259 if (in_attr[i].int_value() < out_attr[i].int_value())
11260 out_attr[i].set_int_value(in_attr[i].int_value());
11261 break;
11262
11263 case elfcpp::Tag_ABI_align8_needed:
11264 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
11265 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
11266 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
11267 == 0)))
11268 {
11269 // This error message should be enabled once all non-conforming
11270 // binaries in the toolchain have had the attributes set
11271 // properly.
11272 // gold_error(_("output 8-byte data alignment conflicts with %s"),
11273 // name);
11274 }
11275 // Fall through.
11276 case elfcpp::Tag_ABI_FP_denormal:
11277 case elfcpp::Tag_ABI_PCS_GOT_use:
11278 {
11279 // These tags have 0 = don't care, 1 = strong requirement,
11280 // 2 = weak requirement.
11281 static const int order_021[3] = {0, 2, 1};
11282
11283 // Use the "greatest" from the sequence 0, 2, 1, or the largest
11284 // value if greater than 2 (for future-proofing).
11285 if ((in_attr[i].int_value() > 2
11286 && in_attr[i].int_value() > out_attr[i].int_value())
11287 || (in_attr[i].int_value() <= 2
11288 && out_attr[i].int_value() <= 2
11289 && (order_021[in_attr[i].int_value()]
11290 > order_021[out_attr[i].int_value()])))
11291 out_attr[i].set_int_value(in_attr[i].int_value());
11292 }
11293 break;
11294
11295 case elfcpp::Tag_CPU_arch_profile:
11296 if (out_attr[i].int_value() != in_attr[i].int_value())
11297 {
11298 // 0 will merge with anything.
11299 // 'A' and 'S' merge to 'A'.
11300 // 'R' and 'S' merge to 'R'.
11301 // 'M' and 'A|R|S' is an error.
11302 if (out_attr[i].int_value() == 0
11303 || (out_attr[i].int_value() == 'S'
11304 && (in_attr[i].int_value() == 'A'
11305 || in_attr[i].int_value() == 'R')))
11306 out_attr[i].set_int_value(in_attr[i].int_value());
11307 else if (in_attr[i].int_value() == 0
11308 || (in_attr[i].int_value() == 'S'
11309 && (out_attr[i].int_value() == 'A'
11310 || out_attr[i].int_value() == 'R')))
11311 ; // Do nothing.
11312 else if (parameters->options().warn_mismatch())
11313 {
11314 gold_error
11315 (_("conflicting architecture profiles %c/%c"),
11316 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
11317 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
11318 }
11319 }
11320 break;
11321 case elfcpp::Tag_VFP_arch:
11322 {
11323 static const struct
11324 {
11325 int ver;
11326 int regs;
11327 } vfp_versions[7] =
11328 {
11329 {0, 0},
11330 {1, 16},
11331 {2, 16},
11332 {3, 32},
11333 {3, 16},
11334 {4, 32},
11335 {4, 16}
11336 };
11337
11338 // Values greater than 6 aren't defined, so just pick the
11339 // biggest.
11340 if (in_attr[i].int_value() > 6
11341 && in_attr[i].int_value() > out_attr[i].int_value())
11342 {
11343 *out_attr = *in_attr;
11344 break;
11345 }
11346 // The output uses the superset of input features
11347 // (ISA version) and registers.
11348 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
11349 vfp_versions[out_attr[i].int_value()].ver);
11350 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
11351 vfp_versions[out_attr[i].int_value()].regs);
11352 // This assumes all possible supersets are also a valid
11353 // options.
11354 int newval;
11355 for (newval = 6; newval > 0; newval--)
11356 {
11357 if (regs == vfp_versions[newval].regs
11358 && ver == vfp_versions[newval].ver)
11359 break;
11360 }
11361 out_attr[i].set_int_value(newval);
11362 }
11363 break;
11364 case elfcpp::Tag_PCS_config:
11365 if (out_attr[i].int_value() == 0)
11366 out_attr[i].set_int_value(in_attr[i].int_value());
11367 else if (in_attr[i].int_value() != 0
11368 && out_attr[i].int_value() != 0
11369 && parameters->options().warn_mismatch())
11370 {
11371 // It's sometimes ok to mix different configs, so this is only
11372 // a warning.
11373 gold_warning(_("%s: conflicting platform configuration"), name);
11374 }
11375 break;
11376 case elfcpp::Tag_ABI_PCS_R9_use:
11377 if (in_attr[i].int_value() != out_attr[i].int_value()
11378 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
11379 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
11380 && parameters->options().warn_mismatch())
11381 {
11382 gold_error(_("%s: conflicting use of R9"), name);
11383 }
11384 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
11385 out_attr[i].set_int_value(in_attr[i].int_value());
11386 break;
11387 case elfcpp::Tag_ABI_PCS_RW_data:
11388 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
11389 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
11390 != elfcpp::AEABI_R9_SB)
11391 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
11392 != elfcpp::AEABI_R9_unused)
11393 && parameters->options().warn_mismatch())
11394 {
11395 gold_error(_("%s: SB relative addressing conflicts with use "
11396 "of R9"),
11397 name);
11398 }
11399 // Use the smallest value specified.
11400 if (in_attr[i].int_value() < out_attr[i].int_value())
11401 out_attr[i].set_int_value(in_attr[i].int_value());
11402 break;
11403 case elfcpp::Tag_ABI_PCS_wchar_t:
11404 if (out_attr[i].int_value()
11405 && in_attr[i].int_value()
11406 && out_attr[i].int_value() != in_attr[i].int_value()
11407 && parameters->options().warn_mismatch()
11408 && parameters->options().wchar_size_warning())
11409 {
11410 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
11411 "use %u-byte wchar_t; use of wchar_t values "
11412 "across objects may fail"),
11413 name, in_attr[i].int_value(),
11414 out_attr[i].int_value());
11415 }
11416 else if (in_attr[i].int_value() && !out_attr[i].int_value())
11417 out_attr[i].set_int_value(in_attr[i].int_value());
11418 break;
11419 case elfcpp::Tag_ABI_enum_size:
11420 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
11421 {
11422 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
11423 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
11424 {
11425 // The existing object is compatible with anything.
11426 // Use whatever requirements the new object has.
11427 out_attr[i].set_int_value(in_attr[i].int_value());
11428 }
11429 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
11430 && out_attr[i].int_value() != in_attr[i].int_value()
11431 && parameters->options().warn_mismatch()
11432 && parameters->options().enum_size_warning())
11433 {
11434 unsigned int in_value = in_attr[i].int_value();
11435 unsigned int out_value = out_attr[i].int_value();
11436 gold_warning(_("%s uses %s enums yet the output is to use "
11437 "%s enums; use of enum values across objects "
11438 "may fail"),
11439 name,
11440 this->aeabi_enum_name(in_value).c_str(),
11441 this->aeabi_enum_name(out_value).c_str());
11442 }
11443 }
11444 break;
11445 case elfcpp::Tag_ABI_VFP_args:
11446 // Already done.
11447 break;
11448 case elfcpp::Tag_ABI_WMMX_args:
11449 if (in_attr[i].int_value() != out_attr[i].int_value()
11450 && parameters->options().warn_mismatch())
11451 {
11452 gold_error(_("%s uses iWMMXt register arguments, output does "
11453 "not"),
11454 name);
11455 }
11456 break;
11457 case Object_attribute::Tag_compatibility:
11458 // Merged in target-independent code.
11459 break;
11460 case elfcpp::Tag_ABI_HardFP_use:
11461 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
11462 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
11463 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
11464 out_attr[i].set_int_value(3);
11465 else if (in_attr[i].int_value() > out_attr[i].int_value())
11466 out_attr[i].set_int_value(in_attr[i].int_value());
11467 break;
11468 case elfcpp::Tag_ABI_FP_16bit_format:
11469 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
11470 {
11471 if (in_attr[i].int_value() != out_attr[i].int_value()
11472 && parameters->options().warn_mismatch())
11473 gold_error(_("fp16 format mismatch between %s and output"),
11474 name);
11475 }
11476 if (in_attr[i].int_value() != 0)
11477 out_attr[i].set_int_value(in_attr[i].int_value());
11478 break;
11479
11480 case elfcpp::Tag_DIV_use:
11481 {
11482 // A value of zero on input means that the divide
11483 // instruction may be used if available in the base
11484 // architecture as specified via Tag_CPU_arch and
11485 // Tag_CPU_arch_profile. A value of 1 means that the user
11486 // did not want divide instructions. A value of 2
11487 // explicitly means that divide instructions were allowed
11488 // in ARM and Thumb state.
11489 int arch = this->
11490 get_aeabi_object_attribute(elfcpp::Tag_CPU_arch)->
11491 int_value();
11492 int profile = this->
11493 get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile)->
11494 int_value();
11495 if (in_attr[i].int_value() == out_attr[i].int_value())
11496 {
11497 // Do nothing.
11498 }
11499 else if (attributes_forbid_div(&in_attr[i])
11500 && !attributes_accept_div(arch, profile, &out_attr[i]))
11501 out_attr[i].set_int_value(1);
11502 else if (attributes_forbid_div(&out_attr[i])
11503 && attributes_accept_div(arch, profile, &in_attr[i]))
11504 out_attr[i].set_int_value(in_attr[i].int_value());
11505 else if (in_attr[i].int_value() == 2)
11506 out_attr[i].set_int_value(in_attr[i].int_value());
11507 }
11508 break;
11509
11510 case elfcpp::Tag_MPextension_use_legacy:
11511 // We don't output objects with Tag_MPextension_use_legacy - we
11512 // move the value to Tag_MPextension_use.
11513 if (in_attr[i].int_value() != 0
11514 && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
11515 {
11516 if (in_attr[elfcpp::Tag_MPextension_use].int_value()
11517 != in_attr[i].int_value())
11518 {
11519 gold_error(_("%s has has both the current and legacy "
11520 "Tag_MPextension_use attributes"),
11521 name);
11522 }
11523 }
11524
11525 if (in_attr[i].int_value()
11526 > out_attr[elfcpp::Tag_MPextension_use].int_value())
11527 out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
11528
11529 break;
11530
11531 case elfcpp::Tag_nodefaults:
11532 // This tag is set if it exists, but the value is unused (and is
11533 // typically zero). We don't actually need to do anything here -
11534 // the merge happens automatically when the type flags are merged
11535 // below.
11536 break;
11537 case elfcpp::Tag_also_compatible_with:
11538 // Already done in Tag_CPU_arch.
11539 break;
11540 case elfcpp::Tag_conformance:
11541 // Keep the attribute if it matches. Throw it away otherwise.
11542 // No attribute means no claim to conform.
11543 if (in_attr[i].string_value() != out_attr[i].string_value())
11544 out_attr[i].set_string_value("");
11545 break;
11546
11547 default:
11548 {
11549 const char* err_object = NULL;
11550
11551 // The "known_obj_attributes" table does contain some undefined
11552 // attributes. Ensure that there are unused.
11553 if (out_attr[i].int_value() != 0
11554 || out_attr[i].string_value() != "")
11555 err_object = "output";
11556 else if (in_attr[i].int_value() != 0
11557 || in_attr[i].string_value() != "")
11558 err_object = name;
11559
11560 if (err_object != NULL
11561 && parameters->options().warn_mismatch())
11562 {
11563 // Attribute numbers >=64 (mod 128) can be safely ignored.
11564 if ((i & 127) < 64)
11565 gold_error(_("%s: unknown mandatory EABI object attribute "
11566 "%d"),
11567 err_object, i);
11568 else
11569 gold_warning(_("%s: unknown EABI object attribute %d"),
11570 err_object, i);
11571 }
11572
11573 // Only pass on attributes that match in both inputs.
11574 if (!in_attr[i].matches(out_attr[i]))
11575 {
11576 out_attr[i].set_int_value(0);
11577 out_attr[i].set_string_value("");
11578 }
11579 }
11580 }
11581
11582 // If out_attr was copied from in_attr then it won't have a type yet.
11583 if (in_attr[i].type() && !out_attr[i].type())
11584 out_attr[i].set_type(in_attr[i].type());
11585 }
11586
11587 // Merge Tag_compatibility attributes and any common GNU ones.
11588 this->attributes_section_data_->merge(name, pasd);
11589
11590 // Check for any attributes not known on ARM.
11591 typedef Vendor_object_attributes::Other_attributes Other_attributes;
11592 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
11593 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
11594 Other_attributes* out_other_attributes =
11595 this->attributes_section_data_->other_attributes(vendor);
11596 Other_attributes::iterator out_iter = out_other_attributes->begin();
11597
11598 while (in_iter != in_other_attributes->end()
11599 || out_iter != out_other_attributes->end())
11600 {
11601 const char* err_object = NULL;
11602 int err_tag = 0;
11603
11604 // The tags for each list are in numerical order.
11605 // If the tags are equal, then merge.
11606 if (out_iter != out_other_attributes->end()
11607 && (in_iter == in_other_attributes->end()
11608 || in_iter->first > out_iter->first))
11609 {
11610 // This attribute only exists in output. We can't merge, and we
11611 // don't know what the tag means, so delete it.
11612 err_object = "output";
11613 err_tag = out_iter->first;
11614 int saved_tag = out_iter->first;
11615 delete out_iter->second;
11616 out_other_attributes->erase(out_iter);
11617 out_iter = out_other_attributes->upper_bound(saved_tag);
11618 }
11619 else if (in_iter != in_other_attributes->end()
11620 && (out_iter != out_other_attributes->end()
11621 || in_iter->first < out_iter->first))
11622 {
11623 // This attribute only exists in input. We can't merge, and we
11624 // don't know what the tag means, so ignore it.
11625 err_object = name;
11626 err_tag = in_iter->first;
11627 ++in_iter;
11628 }
11629 else // The tags are equal.
11630 {
11631 // As present, all attributes in the list are unknown, and
11632 // therefore can't be merged meaningfully.
11633 err_object = "output";
11634 err_tag = out_iter->first;
11635
11636 // Only pass on attributes that match in both inputs.
11637 if (!in_iter->second->matches(*(out_iter->second)))
11638 {
11639 // No match. Delete the attribute.
11640 int saved_tag = out_iter->first;
11641 delete out_iter->second;
11642 out_other_attributes->erase(out_iter);
11643 out_iter = out_other_attributes->upper_bound(saved_tag);
11644 }
11645 else
11646 {
11647 // Matched. Keep the attribute and move to the next.
11648 ++out_iter;
11649 ++in_iter;
11650 }
11651 }
11652
11653 if (err_object && parameters->options().warn_mismatch())
11654 {
11655 // Attribute numbers >=64 (mod 128) can be safely ignored. */
11656 if ((err_tag & 127) < 64)
11657 {
11658 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
11659 err_object, err_tag);
11660 }
11661 else
11662 {
11663 gold_warning(_("%s: unknown EABI object attribute %d"),
11664 err_object, err_tag);
11665 }
11666 }
11667 }
11668 }
11669
11670 // Stub-generation methods for Target_arm.
11671
11672 // Make a new Arm_input_section object.
11673
11674 template<bool big_endian>
11675 Arm_input_section<big_endian>*
new_arm_input_section(Relobj * relobj,unsigned int shndx)11676 Target_arm<big_endian>::new_arm_input_section(
11677 Relobj* relobj,
11678 unsigned int shndx)
11679 {
11680 Section_id sid(relobj, shndx);
11681
11682 Arm_input_section<big_endian>* arm_input_section =
11683 new Arm_input_section<big_endian>(relobj, shndx);
11684 arm_input_section->init();
11685
11686 // Register new Arm_input_section in map for look-up.
11687 std::pair<typename Arm_input_section_map::iterator, bool> ins =
11688 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
11689
11690 // Make sure that it we have not created another Arm_input_section
11691 // for this input section already.
11692 gold_assert(ins.second);
11693
11694 return arm_input_section;
11695 }
11696
11697 // Find the Arm_input_section object corresponding to the SHNDX-th input
11698 // section of RELOBJ.
11699
11700 template<bool big_endian>
11701 Arm_input_section<big_endian>*
find_arm_input_section(Relobj * relobj,unsigned int shndx) const11702 Target_arm<big_endian>::find_arm_input_section(
11703 Relobj* relobj,
11704 unsigned int shndx) const
11705 {
11706 Section_id sid(relobj, shndx);
11707 typename Arm_input_section_map::const_iterator p =
11708 this->arm_input_section_map_.find(sid);
11709 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
11710 }
11711
11712 // Make a new stub table.
11713
11714 template<bool big_endian>
11715 Stub_table<big_endian>*
new_stub_table(Arm_input_section<big_endian> * owner)11716 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
11717 {
11718 Stub_table<big_endian>* stub_table =
11719 new Stub_table<big_endian>(owner);
11720 this->stub_tables_.push_back(stub_table);
11721
11722 stub_table->set_address(owner->address() + owner->data_size());
11723 stub_table->set_file_offset(owner->offset() + owner->data_size());
11724 stub_table->finalize_data_size();
11725
11726 return stub_table;
11727 }
11728
11729 // Scan a relocation for stub generation.
11730
11731 template<bool big_endian>
11732 void
scan_reloc_for_stub(const Relocate_info<32,big_endian> * relinfo,unsigned int r_type,const Sized_symbol<32> * gsym,unsigned int r_sym,const Symbol_value<32> * psymval,elfcpp::Elf_types<32>::Elf_Swxword addend,Arm_address address)11733 Target_arm<big_endian>::scan_reloc_for_stub(
11734 const Relocate_info<32, big_endian>* relinfo,
11735 unsigned int r_type,
11736 const Sized_symbol<32>* gsym,
11737 unsigned int r_sym,
11738 const Symbol_value<32>* psymval,
11739 elfcpp::Elf_types<32>::Elf_Swxword addend,
11740 Arm_address address)
11741 {
11742 const Arm_relobj<big_endian>* arm_relobj =
11743 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
11744
11745 bool target_is_thumb;
11746 Symbol_value<32> symval;
11747 if (gsym != NULL)
11748 {
11749 // This is a global symbol. Determine if we use PLT and if the
11750 // final target is THUMB.
11751 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
11752 {
11753 // This uses a PLT, change the symbol value.
11754 symval.set_output_value(this->plt_address_for_global(gsym));
11755 psymval = &symval;
11756 target_is_thumb = false;
11757 }
11758 else if (gsym->is_undefined())
11759 // There is no need to generate a stub symbol is undefined.
11760 return;
11761 else
11762 {
11763 target_is_thumb =
11764 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
11765 || (gsym->type() == elfcpp::STT_FUNC
11766 && !gsym->is_undefined()
11767 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
11768 }
11769 }
11770 else
11771 {
11772 // This is a local symbol. Determine if the final target is THUMB.
11773 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
11774 }
11775
11776 // Strip LSB if this points to a THUMB target.
11777 const Arm_reloc_property* reloc_property =
11778 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
11779 gold_assert(reloc_property != NULL);
11780 if (target_is_thumb
11781 && reloc_property->uses_thumb_bit()
11782 && ((psymval->value(arm_relobj, 0) & 1) != 0))
11783 {
11784 Arm_address stripped_value =
11785 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
11786 symval.set_output_value(stripped_value);
11787 psymval = &symval;
11788 }
11789
11790 // Get the symbol value.
11791 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
11792
11793 // Owing to pipelining, the PC relative branches below actually skip
11794 // two instructions when the branch offset is 0.
11795 Arm_address destination;
11796 switch (r_type)
11797 {
11798 case elfcpp::R_ARM_CALL:
11799 case elfcpp::R_ARM_JUMP24:
11800 case elfcpp::R_ARM_PLT32:
11801 // ARM branches.
11802 destination = value + addend + 8;
11803 break;
11804 case elfcpp::R_ARM_THM_CALL:
11805 case elfcpp::R_ARM_THM_XPC22:
11806 case elfcpp::R_ARM_THM_JUMP24:
11807 case elfcpp::R_ARM_THM_JUMP19:
11808 // THUMB branches.
11809 destination = value + addend + 4;
11810 break;
11811 default:
11812 gold_unreachable();
11813 }
11814
11815 Reloc_stub* stub = NULL;
11816 Stub_type stub_type =
11817 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
11818 target_is_thumb);
11819 if (stub_type != arm_stub_none)
11820 {
11821 // Try looking up an existing stub from a stub table.
11822 Stub_table<big_endian>* stub_table =
11823 arm_relobj->stub_table(relinfo->data_shndx);
11824 gold_assert(stub_table != NULL);
11825
11826 // Locate stub by destination.
11827 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
11828
11829 // Create a stub if there is not one already
11830 stub = stub_table->find_reloc_stub(stub_key);
11831 if (stub == NULL)
11832 {
11833 // create a new stub and add it to stub table.
11834 stub = this->stub_factory().make_reloc_stub(stub_type);
11835 stub_table->add_reloc_stub(stub, stub_key);
11836 }
11837
11838 // Record the destination address.
11839 stub->set_destination_address(destination
11840 | (target_is_thumb ? 1 : 0));
11841 }
11842
11843 // For Cortex-A8, we need to record a relocation at 4K page boundary.
11844 if (this->fix_cortex_a8_
11845 && (r_type == elfcpp::R_ARM_THM_JUMP24
11846 || r_type == elfcpp::R_ARM_THM_JUMP19
11847 || r_type == elfcpp::R_ARM_THM_CALL
11848 || r_type == elfcpp::R_ARM_THM_XPC22)
11849 && (address & 0xfffU) == 0xffeU)
11850 {
11851 // Found a candidate. Note we haven't checked the destination is
11852 // within 4K here: if we do so (and don't create a record) we can't
11853 // tell that a branch should have been relocated when scanning later.
11854 this->cortex_a8_relocs_info_[address] =
11855 new Cortex_a8_reloc(stub, r_type,
11856 destination | (target_is_thumb ? 1 : 0));
11857 }
11858 }
11859
11860 // This function scans a relocation sections for stub generation.
11861 // The template parameter Relocate must be a class type which provides
11862 // a single function, relocate(), which implements the machine
11863 // specific part of a relocation.
11864
11865 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
11866 // SHT_REL or SHT_RELA.
11867
11868 // PRELOCS points to the relocation data. RELOC_COUNT is the number
11869 // of relocs. OUTPUT_SECTION is the output section.
11870 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
11871 // mapped to output offsets.
11872
11873 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
11874 // VIEW_SIZE is the size. These refer to the input section, unless
11875 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
11876 // the output section.
11877
11878 template<bool big_endian>
11879 template<int sh_type>
11880 void inline
scan_reloc_section_for_stubs(const Relocate_info<32,big_endian> * relinfo,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,const unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr view_address,section_size_type)11881 Target_arm<big_endian>::scan_reloc_section_for_stubs(
11882 const Relocate_info<32, big_endian>* relinfo,
11883 const unsigned char* prelocs,
11884 size_t reloc_count,
11885 Output_section* output_section,
11886 bool needs_special_offset_handling,
11887 const unsigned char* view,
11888 elfcpp::Elf_types<32>::Elf_Addr view_address,
11889 section_size_type)
11890 {
11891 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
11892 const int reloc_size =
11893 Reloc_types<sh_type, 32, big_endian>::reloc_size;
11894
11895 Arm_relobj<big_endian>* arm_object =
11896 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
11897 unsigned int local_count = arm_object->local_symbol_count();
11898
11899 gold::Default_comdat_behavior default_comdat_behavior;
11900 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
11901
11902 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11903 {
11904 Reltype reloc(prelocs);
11905
11906 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
11907 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
11908 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
11909
11910 r_type = this->get_real_reloc_type(r_type);
11911
11912 // Only a few relocation types need stubs.
11913 if ((r_type != elfcpp::R_ARM_CALL)
11914 && (r_type != elfcpp::R_ARM_JUMP24)
11915 && (r_type != elfcpp::R_ARM_PLT32)
11916 && (r_type != elfcpp::R_ARM_THM_CALL)
11917 && (r_type != elfcpp::R_ARM_THM_XPC22)
11918 && (r_type != elfcpp::R_ARM_THM_JUMP24)
11919 && (r_type != elfcpp::R_ARM_THM_JUMP19)
11920 && (r_type != elfcpp::R_ARM_V4BX))
11921 continue;
11922
11923 section_offset_type offset =
11924 convert_to_section_size_type(reloc.get_r_offset());
11925
11926 if (needs_special_offset_handling)
11927 {
11928 offset = output_section->output_offset(relinfo->object,
11929 relinfo->data_shndx,
11930 offset);
11931 if (offset == -1)
11932 continue;
11933 }
11934
11935 // Create a v4bx stub if --fix-v4bx-interworking is used.
11936 if (r_type == elfcpp::R_ARM_V4BX)
11937 {
11938 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
11939 {
11940 // Get the BX instruction.
11941 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
11942 const Valtype* wv =
11943 reinterpret_cast<const Valtype*>(view + offset);
11944 elfcpp::Elf_types<32>::Elf_Swxword insn =
11945 elfcpp::Swap<32, big_endian>::readval(wv);
11946 const uint32_t reg = (insn & 0xf);
11947
11948 if (reg < 0xf)
11949 {
11950 // Try looking up an existing stub from a stub table.
11951 Stub_table<big_endian>* stub_table =
11952 arm_object->stub_table(relinfo->data_shndx);
11953 gold_assert(stub_table != NULL);
11954
11955 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
11956 {
11957 // create a new stub and add it to stub table.
11958 Arm_v4bx_stub* stub =
11959 this->stub_factory().make_arm_v4bx_stub(reg);
11960 gold_assert(stub != NULL);
11961 stub_table->add_arm_v4bx_stub(stub);
11962 }
11963 }
11964 }
11965 continue;
11966 }
11967
11968 // Get the addend.
11969 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
11970 elfcpp::Elf_types<32>::Elf_Swxword addend =
11971 stub_addend_reader(r_type, view + offset, reloc);
11972
11973 const Sized_symbol<32>* sym;
11974
11975 Symbol_value<32> symval;
11976 const Symbol_value<32> *psymval;
11977 bool is_defined_in_discarded_section;
11978 unsigned int shndx;
11979 if (r_sym < local_count)
11980 {
11981 sym = NULL;
11982 psymval = arm_object->local_symbol(r_sym);
11983
11984 // If the local symbol belongs to a section we are discarding,
11985 // and that section is a debug section, try to find the
11986 // corresponding kept section and map this symbol to its
11987 // counterpart in the kept section. The symbol must not
11988 // correspond to a section we are folding.
11989 bool is_ordinary;
11990 shndx = psymval->input_shndx(&is_ordinary);
11991 is_defined_in_discarded_section =
11992 (is_ordinary
11993 && shndx != elfcpp::SHN_UNDEF
11994 && !arm_object->is_section_included(shndx)
11995 && !relinfo->symtab->is_section_folded(arm_object, shndx));
11996
11997 // We need to compute the would-be final value of this local
11998 // symbol.
11999 if (!is_defined_in_discarded_section)
12000 {
12001 typedef Sized_relobj_file<32, big_endian> ObjType;
12002 typename ObjType::Compute_final_local_value_status status =
12003 arm_object->compute_final_local_value(r_sym, psymval, &symval,
12004 relinfo->symtab);
12005 if (status == ObjType::CFLV_OK)
12006 {
12007 // Currently we cannot handle a branch to a target in
12008 // a merged section. If this is the case, issue an error
12009 // and also free the merge symbol value.
12010 if (!symval.has_output_value())
12011 {
12012 const std::string& section_name =
12013 arm_object->section_name(shndx);
12014 arm_object->error(_("cannot handle branch to local %u "
12015 "in a merged section %s"),
12016 r_sym, section_name.c_str());
12017 }
12018 psymval = &symval;
12019 }
12020 else
12021 {
12022 // We cannot determine the final value.
12023 continue;
12024 }
12025 }
12026 }
12027 else
12028 {
12029 const Symbol* gsym;
12030 gsym = arm_object->global_symbol(r_sym);
12031 gold_assert(gsym != NULL);
12032 if (gsym->is_forwarder())
12033 gsym = relinfo->symtab->resolve_forwards(gsym);
12034
12035 sym = static_cast<const Sized_symbol<32>*>(gsym);
12036 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
12037 symval.set_output_symtab_index(sym->symtab_index());
12038 else
12039 symval.set_no_output_symtab_entry();
12040
12041 // We need to compute the would-be final value of this global
12042 // symbol.
12043 const Symbol_table* symtab = relinfo->symtab;
12044 const Sized_symbol<32>* sized_symbol =
12045 symtab->get_sized_symbol<32>(gsym);
12046 Symbol_table::Compute_final_value_status status;
12047 Arm_address value =
12048 symtab->compute_final_value<32>(sized_symbol, &status);
12049
12050 // Skip this if the symbol has not output section.
12051 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
12052 continue;
12053 symval.set_output_value(value);
12054
12055 if (gsym->type() == elfcpp::STT_TLS)
12056 symval.set_is_tls_symbol();
12057 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
12058 symval.set_is_ifunc_symbol();
12059 psymval = &symval;
12060
12061 is_defined_in_discarded_section =
12062 (gsym->is_defined_in_discarded_section()
12063 && gsym->is_undefined());
12064 shndx = 0;
12065 }
12066
12067 Symbol_value<32> symval2;
12068 if (is_defined_in_discarded_section)
12069 {
12070 if (comdat_behavior == CB_UNDETERMINED)
12071 {
12072 std::string name = arm_object->section_name(relinfo->data_shndx);
12073 comdat_behavior = default_comdat_behavior.get(name.c_str());
12074 }
12075 if (comdat_behavior == CB_PRETEND)
12076 {
12077 // FIXME: This case does not work for global symbols.
12078 // We have no place to store the original section index.
12079 // Fortunately this does not matter for comdat sections,
12080 // only for sections explicitly discarded by a linker
12081 // script.
12082 bool found;
12083 typename elfcpp::Elf_types<32>::Elf_Addr value =
12084 arm_object->map_to_kept_section(shndx, &found);
12085 if (found)
12086 symval2.set_output_value(value + psymval->input_value());
12087 else
12088 symval2.set_output_value(0);
12089 }
12090 else
12091 {
12092 if (comdat_behavior == CB_WARNING)
12093 gold_warning_at_location(relinfo, i, offset,
12094 _("relocation refers to discarded "
12095 "section"));
12096 symval2.set_output_value(0);
12097 }
12098 symval2.set_no_output_symtab_entry();
12099 psymval = &symval2;
12100 }
12101
12102 // If symbol is a section symbol, we don't know the actual type of
12103 // destination. Give up.
12104 if (psymval->is_section_symbol())
12105 continue;
12106
12107 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
12108 addend, view_address + offset);
12109 }
12110 }
12111
12112 // Scan an input section for stub generation.
12113
12114 template<bool big_endian>
12115 void
scan_section_for_stubs(const Relocate_info<32,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,const unsigned char * view,Arm_address view_address,section_size_type view_size)12116 Target_arm<big_endian>::scan_section_for_stubs(
12117 const Relocate_info<32, big_endian>* relinfo,
12118 unsigned int sh_type,
12119 const unsigned char* prelocs,
12120 size_t reloc_count,
12121 Output_section* output_section,
12122 bool needs_special_offset_handling,
12123 const unsigned char* view,
12124 Arm_address view_address,
12125 section_size_type view_size)
12126 {
12127 if (sh_type == elfcpp::SHT_REL)
12128 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
12129 relinfo,
12130 prelocs,
12131 reloc_count,
12132 output_section,
12133 needs_special_offset_handling,
12134 view,
12135 view_address,
12136 view_size);
12137 else if (sh_type == elfcpp::SHT_RELA)
12138 // We do not support RELA type relocations yet. This is provided for
12139 // completeness.
12140 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
12141 relinfo,
12142 prelocs,
12143 reloc_count,
12144 output_section,
12145 needs_special_offset_handling,
12146 view,
12147 view_address,
12148 view_size);
12149 else
12150 gold_unreachable();
12151 }
12152
12153 // Group input sections for stub generation.
12154 //
12155 // We group input sections in an output section so that the total size,
12156 // including any padding space due to alignment is smaller than GROUP_SIZE
12157 // unless the only input section in group is bigger than GROUP_SIZE already.
12158 // Then an ARM stub table is created to follow the last input section
12159 // in group. For each group an ARM stub table is created an is placed
12160 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
12161 // extend the group after the stub table.
12162
12163 template<bool big_endian>
12164 void
group_sections(Layout * layout,section_size_type group_size,bool stubs_always_after_branch,const Task * task)12165 Target_arm<big_endian>::group_sections(
12166 Layout* layout,
12167 section_size_type group_size,
12168 bool stubs_always_after_branch,
12169 const Task* task)
12170 {
12171 // Group input sections and insert stub table
12172 Layout::Section_list section_list;
12173 layout->get_executable_sections(§ion_list);
12174 for (Layout::Section_list::const_iterator p = section_list.begin();
12175 p != section_list.end();
12176 ++p)
12177 {
12178 Arm_output_section<big_endian>* output_section =
12179 Arm_output_section<big_endian>::as_arm_output_section(*p);
12180 output_section->group_sections(group_size, stubs_always_after_branch,
12181 this, task);
12182 }
12183 }
12184
12185 // Relaxation hook. This is where we do stub generation.
12186
12187 template<bool big_endian>
12188 bool
do_relax(int pass,const Input_objects * input_objects,Symbol_table * symtab,Layout * layout,const Task * task)12189 Target_arm<big_endian>::do_relax(
12190 int pass,
12191 const Input_objects* input_objects,
12192 Symbol_table* symtab,
12193 Layout* layout,
12194 const Task* task)
12195 {
12196 // No need to generate stubs if this is a relocatable link.
12197 gold_assert(!parameters->options().relocatable());
12198
12199 // If this is the first pass, we need to group input sections into
12200 // stub groups.
12201 bool done_exidx_fixup = false;
12202 typedef typename Stub_table_list::iterator Stub_table_iterator;
12203 if (pass == 1)
12204 {
12205 // Determine the stub group size. The group size is the absolute
12206 // value of the parameter --stub-group-size. If --stub-group-size
12207 // is passed a negative value, we restrict stubs to be always after
12208 // the stubbed branches.
12209 int32_t stub_group_size_param =
12210 parameters->options().stub_group_size();
12211 bool stubs_always_after_branch = stub_group_size_param < 0;
12212 section_size_type stub_group_size = abs(stub_group_size_param);
12213
12214 if (stub_group_size == 1)
12215 {
12216 // Default value.
12217 // Thumb branch range is +-4MB has to be used as the default
12218 // maximum size (a given section can contain both ARM and Thumb
12219 // code, so the worst case has to be taken into account). If we are
12220 // fixing cortex-a8 errata, the branch range has to be even smaller,
12221 // since wide conditional branch has a range of +-1MB only.
12222 //
12223 // This value is 48K less than that, which allows for 4096
12224 // 12-byte stubs. If we exceed that, then we will fail to link.
12225 // The user will have to relink with an explicit group size
12226 // option.
12227 stub_group_size = 4145152;
12228 }
12229
12230 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
12231 // page as the first half of a 32-bit branch straddling two 4K pages.
12232 // This is a crude way of enforcing that. In addition, long conditional
12233 // branches of THUMB-2 have a range of +-1M. If we are fixing cortex-A8
12234 // erratum, limit the group size to (1M - 12k) to avoid unreachable
12235 // cortex-A8 stubs from long conditional branches.
12236 if (this->fix_cortex_a8_)
12237 {
12238 stubs_always_after_branch = true;
12239 const section_size_type cortex_a8_group_size = 1024 * (1024 - 12);
12240 stub_group_size = std::max(stub_group_size, cortex_a8_group_size);
12241 }
12242
12243 group_sections(layout, stub_group_size, stubs_always_after_branch, task);
12244
12245 // Also fix .ARM.exidx section coverage.
12246 Arm_output_section<big_endian>* exidx_output_section = NULL;
12247 for (Layout::Section_list::const_iterator p =
12248 layout->section_list().begin();
12249 p != layout->section_list().end();
12250 ++p)
12251 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
12252 {
12253 if (exidx_output_section == NULL)
12254 exidx_output_section =
12255 Arm_output_section<big_endian>::as_arm_output_section(*p);
12256 else
12257 // We cannot handle this now.
12258 gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
12259 "non-relocatable link"),
12260 exidx_output_section->name(),
12261 (*p)->name());
12262 }
12263
12264 if (exidx_output_section != NULL)
12265 {
12266 this->fix_exidx_coverage(layout, input_objects, exidx_output_section,
12267 symtab, task);
12268 done_exidx_fixup = true;
12269 }
12270 }
12271 else
12272 {
12273 // If this is not the first pass, addresses and file offsets have
12274 // been reset at this point, set them here.
12275 for (Stub_table_iterator sp = this->stub_tables_.begin();
12276 sp != this->stub_tables_.end();
12277 ++sp)
12278 {
12279 Arm_input_section<big_endian>* owner = (*sp)->owner();
12280 off_t off = align_address(owner->original_size(),
12281 (*sp)->addralign());
12282 (*sp)->set_address_and_file_offset(owner->address() + off,
12283 owner->offset() + off);
12284 }
12285 }
12286
12287 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
12288 // beginning of each relaxation pass, just blow away all the stubs.
12289 // Alternatively, we could selectively remove only the stubs and reloc
12290 // information for code sections that have moved since the last pass.
12291 // That would require more book-keeping.
12292 if (this->fix_cortex_a8_)
12293 {
12294 // Clear all Cortex-A8 reloc information.
12295 for (typename Cortex_a8_relocs_info::const_iterator p =
12296 this->cortex_a8_relocs_info_.begin();
12297 p != this->cortex_a8_relocs_info_.end();
12298 ++p)
12299 delete p->second;
12300 this->cortex_a8_relocs_info_.clear();
12301
12302 // Remove all Cortex-A8 stubs.
12303 for (Stub_table_iterator sp = this->stub_tables_.begin();
12304 sp != this->stub_tables_.end();
12305 ++sp)
12306 (*sp)->remove_all_cortex_a8_stubs();
12307 }
12308
12309 // Scan relocs for relocation stubs
12310 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
12311 op != input_objects->relobj_end();
12312 ++op)
12313 {
12314 Arm_relobj<big_endian>* arm_relobj =
12315 Arm_relobj<big_endian>::as_arm_relobj(*op);
12316 // Lock the object so we can read from it. This is only called
12317 // single-threaded from Layout::finalize, so it is OK to lock.
12318 Task_lock_obj<Object> tl(task, arm_relobj);
12319 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
12320 }
12321
12322 // Check all stub tables to see if any of them have their data sizes
12323 // or addresses alignments changed. These are the only things that
12324 // matter.
12325 bool any_stub_table_changed = false;
12326 Unordered_set<const Output_section*> sections_needing_adjustment;
12327 for (Stub_table_iterator sp = this->stub_tables_.begin();
12328 (sp != this->stub_tables_.end()
12329 && (parameters->options().stub_group_auto_padding()
12330 || !any_stub_table_changed));
12331 ++sp)
12332 {
12333 if ((*sp)->update_data_size_and_addralign())
12334 {
12335 // Update data size of stub table owner.
12336 Arm_input_section<big_endian>* owner = (*sp)->owner();
12337 uint64_t address = owner->address();
12338 off_t offset = owner->offset();
12339 owner->reset_address_and_file_offset();
12340 owner->set_address_and_file_offset(address, offset);
12341
12342 sections_needing_adjustment.insert(owner->output_section());
12343 any_stub_table_changed = true;
12344 }
12345 }
12346
12347 // Output_section_data::output_section() returns a const pointer but we
12348 // need to update output sections, so we record all output sections needing
12349 // update above and scan the sections here to find out what sections need
12350 // to be updated.
12351 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
12352 p != layout->section_list().end();
12353 ++p)
12354 {
12355 if (sections_needing_adjustment.find(*p)
12356 != sections_needing_adjustment.end())
12357 (*p)->set_section_offsets_need_adjustment();
12358 }
12359
12360 // Stop relaxation if no EXIDX fix-up and no stub table change.
12361 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
12362
12363 // Finalize the stubs in the last relaxation pass.
12364 if (!continue_relaxation)
12365 {
12366 for (Stub_table_iterator sp = this->stub_tables_.begin();
12367 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
12368 ++sp)
12369 (*sp)->finalize_stubs();
12370
12371 // Update output local symbol counts of objects if necessary.
12372 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
12373 op != input_objects->relobj_end();
12374 ++op)
12375 {
12376 Arm_relobj<big_endian>* arm_relobj =
12377 Arm_relobj<big_endian>::as_arm_relobj(*op);
12378
12379 // Update output local symbol counts. We need to discard local
12380 // symbols defined in parts of input sections that are discarded by
12381 // relaxation.
12382 if (arm_relobj->output_local_symbol_count_needs_update())
12383 {
12384 // We need to lock the object's file to update it.
12385 Task_lock_obj<Object> tl(task, arm_relobj);
12386 arm_relobj->update_output_local_symbol_count();
12387 }
12388 }
12389 }
12390
12391 return continue_relaxation;
12392 }
12393
12394 // Relocate a stub.
12395
12396 template<bool big_endian>
12397 void
relocate_stub(Stub * stub,const Relocate_info<32,big_endian> * relinfo,Output_section * output_section,unsigned char * view,Arm_address address,section_size_type view_size)12398 Target_arm<big_endian>::relocate_stub(
12399 Stub* stub,
12400 const Relocate_info<32, big_endian>* relinfo,
12401 Output_section* output_section,
12402 unsigned char* view,
12403 Arm_address address,
12404 section_size_type view_size)
12405 {
12406 Relocate relocate;
12407 const Stub_template* stub_template = stub->stub_template();
12408 for (size_t i = 0; i < stub_template->reloc_count(); i++)
12409 {
12410 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
12411 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
12412
12413 unsigned int r_type = insn->r_type();
12414 section_size_type reloc_offset = stub_template->reloc_offset(i);
12415 section_size_type reloc_size = insn->size();
12416 gold_assert(reloc_offset + reloc_size <= view_size);
12417
12418 // This is the address of the stub destination.
12419 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
12420 Symbol_value<32> symval;
12421 symval.set_output_value(target);
12422
12423 // Synthesize a fake reloc just in case. We don't have a symbol so
12424 // we use 0.
12425 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
12426 memset(reloc_buffer, 0, sizeof(reloc_buffer));
12427 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
12428 reloc_write.put_r_offset(reloc_offset);
12429 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
12430 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
12431
12432 relocate.relocate(relinfo, this, output_section,
12433 this->fake_relnum_for_stubs, rel, r_type,
12434 NULL, &symval, view + reloc_offset,
12435 address + reloc_offset, reloc_size);
12436 }
12437 }
12438
12439 // Determine whether an object attribute tag takes an integer, a
12440 // string or both.
12441
12442 template<bool big_endian>
12443 int
do_attribute_arg_type(int tag) const12444 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
12445 {
12446 if (tag == Object_attribute::Tag_compatibility)
12447 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
12448 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
12449 else if (tag == elfcpp::Tag_nodefaults)
12450 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
12451 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
12452 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
12453 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
12454 else if (tag < 32)
12455 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
12456 else
12457 return ((tag & 1) != 0
12458 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
12459 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
12460 }
12461
12462 // Reorder attributes.
12463 //
12464 // The ABI defines that Tag_conformance should be emitted first, and that
12465 // Tag_nodefaults should be second (if either is defined). This sets those
12466 // two positions, and bumps up the position of all the remaining tags to
12467 // compensate.
12468
12469 template<bool big_endian>
12470 int
do_attributes_order(int num) const12471 Target_arm<big_endian>::do_attributes_order(int num) const
12472 {
12473 // Reorder the known object attributes in output. We want to move
12474 // Tag_conformance to position 4 and Tag_conformance to position 5
12475 // and shift everything between 4 .. Tag_conformance - 1 to make room.
12476 if (num == 4)
12477 return elfcpp::Tag_conformance;
12478 if (num == 5)
12479 return elfcpp::Tag_nodefaults;
12480 if ((num - 2) < elfcpp::Tag_nodefaults)
12481 return num - 2;
12482 if ((num - 1) < elfcpp::Tag_conformance)
12483 return num - 1;
12484 return num;
12485 }
12486
12487 // Scan a span of THUMB code for Cortex-A8 erratum.
12488
12489 template<bool big_endian>
12490 void
scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian> * arm_relobj,unsigned int shndx,section_size_type span_start,section_size_type span_end,const unsigned char * view,Arm_address address)12491 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
12492 Arm_relobj<big_endian>* arm_relobj,
12493 unsigned int shndx,
12494 section_size_type span_start,
12495 section_size_type span_end,
12496 const unsigned char* view,
12497 Arm_address address)
12498 {
12499 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
12500 //
12501 // The opcode is BLX.W, BL.W, B.W, Bcc.W
12502 // The branch target is in the same 4KB region as the
12503 // first half of the branch.
12504 // The instruction before the branch is a 32-bit
12505 // length non-branch instruction.
12506 section_size_type i = span_start;
12507 bool last_was_32bit = false;
12508 bool last_was_branch = false;
12509 while (i < span_end)
12510 {
12511 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
12512 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
12513 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
12514 bool is_blx = false, is_b = false;
12515 bool is_bl = false, is_bcc = false;
12516
12517 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
12518 if (insn_32bit)
12519 {
12520 // Load the rest of the insn (in manual-friendly order).
12521 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
12522
12523 // Encoding T4: B<c>.W.
12524 is_b = (insn & 0xf800d000U) == 0xf0009000U;
12525 // Encoding T1: BL<c>.W.
12526 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
12527 // Encoding T2: BLX<c>.W.
12528 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
12529 // Encoding T3: B<c>.W (not permitted in IT block).
12530 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
12531 && (insn & 0x07f00000U) != 0x03800000U);
12532 }
12533
12534 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
12535
12536 // If this instruction is a 32-bit THUMB branch that crosses a 4K
12537 // page boundary and it follows 32-bit non-branch instruction,
12538 // we need to work around.
12539 if (is_32bit_branch
12540 && ((address + i) & 0xfffU) == 0xffeU
12541 && last_was_32bit
12542 && !last_was_branch)
12543 {
12544 // Check to see if there is a relocation stub for this branch.
12545 bool force_target_arm = false;
12546 bool force_target_thumb = false;
12547 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
12548 Cortex_a8_relocs_info::const_iterator p =
12549 this->cortex_a8_relocs_info_.find(address + i);
12550
12551 if (p != this->cortex_a8_relocs_info_.end())
12552 {
12553 cortex_a8_reloc = p->second;
12554 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
12555
12556 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
12557 && !target_is_thumb)
12558 force_target_arm = true;
12559 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
12560 && target_is_thumb)
12561 force_target_thumb = true;
12562 }
12563
12564 off_t offset;
12565 Stub_type stub_type = arm_stub_none;
12566
12567 // Check if we have an offending branch instruction.
12568 uint16_t upper_insn = (insn >> 16) & 0xffffU;
12569 uint16_t lower_insn = insn & 0xffffU;
12570 typedef class Arm_relocate_functions<big_endian> RelocFuncs;
12571
12572 if (cortex_a8_reloc != NULL
12573 && cortex_a8_reloc->reloc_stub() != NULL)
12574 // We've already made a stub for this instruction, e.g.
12575 // it's a long branch or a Thumb->ARM stub. Assume that
12576 // stub will suffice to work around the A8 erratum (see
12577 // setting of always_after_branch above).
12578 ;
12579 else if (is_bcc)
12580 {
12581 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
12582 lower_insn);
12583 stub_type = arm_stub_a8_veneer_b_cond;
12584 }
12585 else if (is_b || is_bl || is_blx)
12586 {
12587 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
12588 lower_insn);
12589 if (is_blx)
12590 offset &= ~3;
12591
12592 stub_type = (is_blx
12593 ? arm_stub_a8_veneer_blx
12594 : (is_bl
12595 ? arm_stub_a8_veneer_bl
12596 : arm_stub_a8_veneer_b));
12597 }
12598
12599 if (stub_type != arm_stub_none)
12600 {
12601 Arm_address pc_for_insn = address + i + 4;
12602
12603 // The original instruction is a BL, but the target is
12604 // an ARM instruction. If we were not making a stub,
12605 // the BL would have been converted to a BLX. Use the
12606 // BLX stub instead in that case.
12607 if (this->may_use_v5t_interworking() && force_target_arm
12608 && stub_type == arm_stub_a8_veneer_bl)
12609 {
12610 stub_type = arm_stub_a8_veneer_blx;
12611 is_blx = true;
12612 is_bl = false;
12613 }
12614 // Conversely, if the original instruction was
12615 // BLX but the target is Thumb mode, use the BL stub.
12616 else if (force_target_thumb
12617 && stub_type == arm_stub_a8_veneer_blx)
12618 {
12619 stub_type = arm_stub_a8_veneer_bl;
12620 is_blx = false;
12621 is_bl = true;
12622 }
12623
12624 if (is_blx)
12625 pc_for_insn &= ~3;
12626
12627 // If we found a relocation, use the proper destination,
12628 // not the offset in the (unrelocated) instruction.
12629 // Note this is always done if we switched the stub type above.
12630 if (cortex_a8_reloc != NULL)
12631 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
12632
12633 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
12634
12635 // Add a new stub if destination address in in the same page.
12636 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
12637 {
12638 Cortex_a8_stub* stub =
12639 this->stub_factory_.make_cortex_a8_stub(stub_type,
12640 arm_relobj, shndx,
12641 address + i,
12642 target, insn);
12643 Stub_table<big_endian>* stub_table =
12644 arm_relobj->stub_table(shndx);
12645 gold_assert(stub_table != NULL);
12646 stub_table->add_cortex_a8_stub(address + i, stub);
12647 }
12648 }
12649 }
12650
12651 i += insn_32bit ? 4 : 2;
12652 last_was_32bit = insn_32bit;
12653 last_was_branch = is_32bit_branch;
12654 }
12655 }
12656
12657 // Apply the Cortex-A8 workaround.
12658
12659 template<bool big_endian>
12660 void
apply_cortex_a8_workaround(const Cortex_a8_stub * stub,Arm_address stub_address,unsigned char * insn_view,Arm_address insn_address)12661 Target_arm<big_endian>::apply_cortex_a8_workaround(
12662 const Cortex_a8_stub* stub,
12663 Arm_address stub_address,
12664 unsigned char* insn_view,
12665 Arm_address insn_address)
12666 {
12667 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
12668 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
12669 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
12670 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
12671 off_t branch_offset = stub_address - (insn_address + 4);
12672
12673 typedef class Arm_relocate_functions<big_endian> RelocFuncs;
12674 switch (stub->stub_template()->type())
12675 {
12676 case arm_stub_a8_veneer_b_cond:
12677 // For a conditional branch, we re-write it to be an unconditional
12678 // branch to the stub. We use the THUMB-2 encoding here.
12679 upper_insn = 0xf000U;
12680 lower_insn = 0xb800U;
12681 // Fall through
12682 case arm_stub_a8_veneer_b:
12683 case arm_stub_a8_veneer_bl:
12684 case arm_stub_a8_veneer_blx:
12685 if ((lower_insn & 0x5000U) == 0x4000U)
12686 // For a BLX instruction, make sure that the relocation is
12687 // rounded up to a word boundary. This follows the semantics of
12688 // the instruction which specifies that bit 1 of the target
12689 // address will come from bit 1 of the base address.
12690 branch_offset = (branch_offset + 2) & ~3;
12691
12692 // Put BRANCH_OFFSET back into the insn.
12693 gold_assert(!Bits<25>::has_overflow32(branch_offset));
12694 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
12695 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
12696 break;
12697
12698 default:
12699 gold_unreachable();
12700 }
12701
12702 // Put the relocated value back in the object file:
12703 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
12704 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
12705 }
12706
12707 // Target selector for ARM. Note this is never instantiated directly.
12708 // It's only used in Target_selector_arm_nacl, below.
12709
12710 template<bool big_endian>
12711 class Target_selector_arm : public Target_selector
12712 {
12713 public:
Target_selector_arm()12714 Target_selector_arm()
12715 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
12716 (big_endian ? "elf32-bigarm" : "elf32-littlearm"),
12717 (big_endian ? "armelfb" : "armelf"))
12718 { }
12719
12720 Target*
do_instantiate_target()12721 do_instantiate_target()
12722 { return new Target_arm<big_endian>(); }
12723 };
12724
12725 // Fix .ARM.exidx section coverage.
12726
12727 template<bool big_endian>
12728 void
fix_exidx_coverage(Layout * layout,const Input_objects * input_objects,Arm_output_section<big_endian> * exidx_section,Symbol_table * symtab,const Task * task)12729 Target_arm<big_endian>::fix_exidx_coverage(
12730 Layout* layout,
12731 const Input_objects* input_objects,
12732 Arm_output_section<big_endian>* exidx_section,
12733 Symbol_table* symtab,
12734 const Task* task)
12735 {
12736 // We need to look at all the input sections in output in ascending
12737 // order of of output address. We do that by building a sorted list
12738 // of output sections by addresses. Then we looks at the output sections
12739 // in order. The input sections in an output section are already sorted
12740 // by addresses within the output section.
12741
12742 typedef std::set<Output_section*, output_section_address_less_than>
12743 Sorted_output_section_list;
12744 Sorted_output_section_list sorted_output_sections;
12745
12746 // Find out all the output sections of input sections pointed by
12747 // EXIDX input sections.
12748 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
12749 p != input_objects->relobj_end();
12750 ++p)
12751 {
12752 Arm_relobj<big_endian>* arm_relobj =
12753 Arm_relobj<big_endian>::as_arm_relobj(*p);
12754 std::vector<unsigned int> shndx_list;
12755 arm_relobj->get_exidx_shndx_list(&shndx_list);
12756 for (size_t i = 0; i < shndx_list.size(); ++i)
12757 {
12758 const Arm_exidx_input_section* exidx_input_section =
12759 arm_relobj->exidx_input_section_by_shndx(shndx_list[i]);
12760 gold_assert(exidx_input_section != NULL);
12761 if (!exidx_input_section->has_errors())
12762 {
12763 unsigned int text_shndx = exidx_input_section->link();
12764 Output_section* os = arm_relobj->output_section(text_shndx);
12765 if (os != NULL && (os->flags() & elfcpp::SHF_ALLOC) != 0)
12766 sorted_output_sections.insert(os);
12767 }
12768 }
12769 }
12770
12771 // Go over the output sections in ascending order of output addresses.
12772 typedef typename Arm_output_section<big_endian>::Text_section_list
12773 Text_section_list;
12774 Text_section_list sorted_text_sections;
12775 for (typename Sorted_output_section_list::iterator p =
12776 sorted_output_sections.begin();
12777 p != sorted_output_sections.end();
12778 ++p)
12779 {
12780 Arm_output_section<big_endian>* arm_output_section =
12781 Arm_output_section<big_endian>::as_arm_output_section(*p);
12782 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
12783 }
12784
12785 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
12786 merge_exidx_entries(), task);
12787 }
12788
12789 template<bool big_endian>
12790 void
do_define_standard_symbols(Symbol_table * symtab,Layout * layout)12791 Target_arm<big_endian>::do_define_standard_symbols(
12792 Symbol_table* symtab,
12793 Layout* layout)
12794 {
12795 // Handle the .ARM.exidx section.
12796 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
12797
12798 if (exidx_section != NULL)
12799 {
12800 // Create __exidx_start and __exidx_end symbols.
12801 symtab->define_in_output_data("__exidx_start",
12802 NULL, // version
12803 Symbol_table::PREDEFINED,
12804 exidx_section,
12805 0, // value
12806 0, // symsize
12807 elfcpp::STT_NOTYPE,
12808 elfcpp::STB_GLOBAL,
12809 elfcpp::STV_HIDDEN,
12810 0, // nonvis
12811 false, // offset_is_from_end
12812 true); // only_if_ref
12813
12814 symtab->define_in_output_data("__exidx_end",
12815 NULL, // version
12816 Symbol_table::PREDEFINED,
12817 exidx_section,
12818 0, // value
12819 0, // symsize
12820 elfcpp::STT_NOTYPE,
12821 elfcpp::STB_GLOBAL,
12822 elfcpp::STV_HIDDEN,
12823 0, // nonvis
12824 true, // offset_is_from_end
12825 true); // only_if_ref
12826 }
12827 else
12828 {
12829 // Define __exidx_start and __exidx_end even when .ARM.exidx
12830 // section is missing to match ld's behaviour.
12831 symtab->define_as_constant("__exidx_start", NULL,
12832 Symbol_table::PREDEFINED,
12833 0, 0, elfcpp::STT_OBJECT,
12834 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
12835 true, false);
12836 symtab->define_as_constant("__exidx_end", NULL,
12837 Symbol_table::PREDEFINED,
12838 0, 0, elfcpp::STT_OBJECT,
12839 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
12840 true, false);
12841 }
12842 }
12843
12844 // NaCl variant. It uses different PLT contents.
12845
12846 template<bool big_endian>
12847 class Output_data_plt_arm_nacl;
12848
12849 template<bool big_endian>
12850 class Target_arm_nacl : public Target_arm<big_endian>
12851 {
12852 public:
Target_arm_nacl()12853 Target_arm_nacl()
12854 : Target_arm<big_endian>(&arm_nacl_info)
12855 { }
12856
12857 protected:
12858 virtual Output_data_plt_arm<big_endian>*
do_make_data_plt(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)12859 do_make_data_plt(
12860 Layout* layout,
12861 Arm_output_data_got<big_endian>* got,
12862 Output_data_space* got_plt,
12863 Output_data_space* got_irelative)
12864 { return new Output_data_plt_arm_nacl<big_endian>(
12865 layout, got, got_plt, got_irelative); }
12866
12867 private:
12868 static const Target::Target_info arm_nacl_info;
12869 };
12870
12871 template<bool big_endian>
12872 const Target::Target_info Target_arm_nacl<big_endian>::arm_nacl_info =
12873 {
12874 32, // size
12875 big_endian, // is_big_endian
12876 elfcpp::EM_ARM, // machine_code
12877 false, // has_make_symbol
12878 false, // has_resolve
12879 false, // has_code_fill
12880 true, // is_default_stack_executable
12881 false, // can_icf_inline_merge_sections
12882 '\0', // wrap_char
12883 "/lib/ld-nacl-arm.so.1", // dynamic_linker
12884 0x20000, // default_text_segment_address
12885 0x10000, // abi_pagesize (overridable by -z max-page-size)
12886 0x10000, // common_pagesize (overridable by -z common-page-size)
12887 true, // isolate_execinstr
12888 0x10000000, // rosegment_gap
12889 elfcpp::SHN_UNDEF, // small_common_shndx
12890 elfcpp::SHN_UNDEF, // large_common_shndx
12891 0, // small_common_section_flags
12892 0, // large_common_section_flags
12893 ".ARM.attributes", // attributes_section
12894 "aeabi", // attributes_vendor
12895 "_start" // entry_symbol_name
12896 };
12897
12898 template<bool big_endian>
12899 class Output_data_plt_arm_nacl : public Output_data_plt_arm<big_endian>
12900 {
12901 public:
Output_data_plt_arm_nacl(Layout * layout,Arm_output_data_got<big_endian> * got,Output_data_space * got_plt,Output_data_space * got_irelative)12902 Output_data_plt_arm_nacl(
12903 Layout* layout,
12904 Arm_output_data_got<big_endian>* got,
12905 Output_data_space* got_plt,
12906 Output_data_space* got_irelative)
12907 : Output_data_plt_arm<big_endian>(layout, 16, got, got_plt, got_irelative)
12908 { }
12909
12910 protected:
12911 // Return the offset of the first non-reserved PLT entry.
12912 virtual unsigned int
do_first_plt_entry_offset() const12913 do_first_plt_entry_offset() const
12914 { return sizeof(first_plt_entry); }
12915
12916 // Return the size of a PLT entry.
12917 virtual unsigned int
do_get_plt_entry_size() const12918 do_get_plt_entry_size() const
12919 { return sizeof(plt_entry); }
12920
12921 virtual void
12922 do_fill_first_plt_entry(unsigned char* pov,
12923 Arm_address got_address,
12924 Arm_address plt_address);
12925
12926 virtual void
12927 do_fill_plt_entry(unsigned char* pov,
12928 Arm_address got_address,
12929 Arm_address plt_address,
12930 unsigned int got_offset,
12931 unsigned int plt_offset);
12932
12933 private:
arm_movw_immediate(uint32_t value)12934 inline uint32_t arm_movw_immediate(uint32_t value)
12935 {
12936 return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
12937 }
12938
arm_movt_immediate(uint32_t value)12939 inline uint32_t arm_movt_immediate(uint32_t value)
12940 {
12941 return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
12942 }
12943
12944 // Template for the first PLT entry.
12945 static const uint32_t first_plt_entry[16];
12946
12947 // Template for subsequent PLT entries.
12948 static const uint32_t plt_entry[4];
12949 };
12950
12951 // The first entry in the PLT.
12952 template<bool big_endian>
12953 const uint32_t Output_data_plt_arm_nacl<big_endian>::first_plt_entry[16] =
12954 {
12955 // First bundle:
12956 0xe300c000, // movw ip, #:lower16:&GOT[2]-.+8
12957 0xe340c000, // movt ip, #:upper16:&GOT[2]-.+8
12958 0xe08cc00f, // add ip, ip, pc
12959 0xe52dc008, // str ip, [sp, #-8]!
12960 // Second bundle:
12961 0xe3ccc103, // bic ip, ip, #0xc0000000
12962 0xe59cc000, // ldr ip, [ip]
12963 0xe3ccc13f, // bic ip, ip, #0xc000000f
12964 0xe12fff1c, // bx ip
12965 // Third bundle:
12966 0xe320f000, // nop
12967 0xe320f000, // nop
12968 0xe320f000, // nop
12969 // .Lplt_tail:
12970 0xe50dc004, // str ip, [sp, #-4]
12971 // Fourth bundle:
12972 0xe3ccc103, // bic ip, ip, #0xc0000000
12973 0xe59cc000, // ldr ip, [ip]
12974 0xe3ccc13f, // bic ip, ip, #0xc000000f
12975 0xe12fff1c, // bx ip
12976 };
12977
12978 template<bool big_endian>
12979 void
do_fill_first_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address)12980 Output_data_plt_arm_nacl<big_endian>::do_fill_first_plt_entry(
12981 unsigned char* pov,
12982 Arm_address got_address,
12983 Arm_address plt_address)
12984 {
12985 // Write first PLT entry. All but first two words are constants.
12986 const size_t num_first_plt_words = (sizeof(first_plt_entry)
12987 / sizeof(first_plt_entry[0]));
12988
12989 int32_t got_displacement = got_address + 8 - (plt_address + 16);
12990
12991 elfcpp::Swap<32, big_endian>::writeval
12992 (pov + 0, first_plt_entry[0] | arm_movw_immediate (got_displacement));
12993 elfcpp::Swap<32, big_endian>::writeval
12994 (pov + 4, first_plt_entry[1] | arm_movt_immediate (got_displacement));
12995
12996 for (size_t i = 2; i < num_first_plt_words; ++i)
12997 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
12998 }
12999
13000 // Subsequent entries in the PLT.
13001
13002 template<bool big_endian>
13003 const uint32_t Output_data_plt_arm_nacl<big_endian>::plt_entry[4] =
13004 {
13005 0xe300c000, // movw ip, #:lower16:&GOT[n]-.+8
13006 0xe340c000, // movt ip, #:upper16:&GOT[n]-.+8
13007 0xe08cc00f, // add ip, ip, pc
13008 0xea000000, // b .Lplt_tail
13009 };
13010
13011 template<bool big_endian>
13012 void
do_fill_plt_entry(unsigned char * pov,Arm_address got_address,Arm_address plt_address,unsigned int got_offset,unsigned int plt_offset)13013 Output_data_plt_arm_nacl<big_endian>::do_fill_plt_entry(
13014 unsigned char* pov,
13015 Arm_address got_address,
13016 Arm_address plt_address,
13017 unsigned int got_offset,
13018 unsigned int plt_offset)
13019 {
13020 // Calculate the displacement between the PLT slot and the
13021 // common tail that's part of the special initial PLT slot.
13022 int32_t tail_displacement = (plt_address + (11 * sizeof(uint32_t))
13023 - (plt_address + plt_offset
13024 + sizeof(plt_entry) + sizeof(uint32_t)));
13025 gold_assert((tail_displacement & 3) == 0);
13026 tail_displacement >>= 2;
13027
13028 gold_assert ((tail_displacement & 0xff000000) == 0
13029 || (-tail_displacement & 0xff000000) == 0);
13030
13031 // Calculate the displacement between the PLT slot and the entry
13032 // in the GOT. The offset accounts for the value produced by
13033 // adding to pc in the penultimate instruction of the PLT stub.
13034 const int32_t got_displacement = (got_address + got_offset
13035 - (plt_address + sizeof(plt_entry)));
13036
13037 elfcpp::Swap<32, big_endian>::writeval
13038 (pov + 0, plt_entry[0] | arm_movw_immediate (got_displacement));
13039 elfcpp::Swap<32, big_endian>::writeval
13040 (pov + 4, plt_entry[1] | arm_movt_immediate (got_displacement));
13041 elfcpp::Swap<32, big_endian>::writeval
13042 (pov + 8, plt_entry[2]);
13043 elfcpp::Swap<32, big_endian>::writeval
13044 (pov + 12, plt_entry[3] | (tail_displacement & 0x00ffffff));
13045 }
13046
13047 // Target selectors.
13048
13049 template<bool big_endian>
13050 class Target_selector_arm_nacl
13051 : public Target_selector_nacl<Target_selector_arm<big_endian>,
13052 Target_arm_nacl<big_endian> >
13053 {
13054 public:
Target_selector_arm_nacl()13055 Target_selector_arm_nacl()
13056 : Target_selector_nacl<Target_selector_arm<big_endian>,
13057 Target_arm_nacl<big_endian> >(
13058 "arm",
13059 big_endian ? "elf32-bigarm-nacl" : "elf32-littlearm-nacl",
13060 big_endian ? "armelfb_nacl" : "armelf_nacl")
13061 { }
13062 };
13063
13064 Target_selector_arm_nacl<false> target_selector_arm;
13065 Target_selector_arm_nacl<true> target_selector_armbe;
13066
13067 } // End anonymous namespace.
13068