1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 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
9 // This file is part of gold.
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.
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.
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.
37 #include "parameters.h"
44 #include "copy-relocs.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
51 #include "attributes.h"
58 template<bool big_endian
>
59 class Output_data_plt_arm
;
61 template<bool big_endian
>
64 template<bool big_endian
>
65 class Arm_input_section
;
67 template<bool big_endian
>
68 class Arm_output_section
;
70 template<bool big_endian
>
73 template<bool big_endian
>
77 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
79 // Maximum branch offsets for ARM, THUMB and THUMB2.
80 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
81 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
82 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
83 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
84 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
85 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
87 // The arm target class.
89 // This is a very simple port of gold for ARM-EABI. It is intended for
90 // supporting Android only for the time being. Only these relocation types
119 // R_ARM_THM_MOVW_ABS_NC
120 // R_ARM_THM_MOVT_ABS
121 // R_ARM_MOVW_PREL_NC
123 // R_ARM_THM_MOVW_PREL_NC
124 // R_ARM_THM_MOVT_PREL
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
130 // There are probably a lot more.
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
138 // Types of instruction templates.
142 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
143 // templates with class-specific semantics. Currently this is used
144 // only by the Cortex_a8_stub class for handling condition codes in
145 // conditional branches.
146 THUMB16_SPECIAL_TYPE
,
152 // Factory methods to create instruction templates in different formats.
154 static const Insn_template
155 thumb16_insn(uint32_t data
)
156 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
158 // A Thumb conditional branch, in which the proper condition is inserted
159 // when we build the stub.
160 static const Insn_template
161 thumb16_bcond_insn(uint32_t data
)
162 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
164 static const Insn_template
165 thumb32_insn(uint32_t data
)
166 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
168 static const Insn_template
169 thumb32_b_insn(uint32_t data
, int reloc_addend
)
171 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
175 static const Insn_template
176 arm_insn(uint32_t data
)
177 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
179 static const Insn_template
180 arm_rel_insn(unsigned data
, int reloc_addend
)
181 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
183 static const Insn_template
184 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
185 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
187 // Accessors. This class is used for read-only objects so no modifiers
192 { return this->data_
; }
194 // Return the instruction sequence type of this.
197 { return this->type_
; }
199 // Return the ARM relocation type of this.
202 { return this->r_type_
; }
206 { return this->reloc_addend_
; }
208 // Return size of instruction template in bytes.
212 // Return byte-alignment of instruction template.
217 // We make the constructor private to ensure that only the factory
220 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
221 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
224 // Instruction specific data. This is used to store information like
225 // some of the instruction bits.
227 // Instruction template type.
229 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
230 unsigned int r_type_
;
231 // Relocation addend.
232 int32_t reloc_addend_
;
235 // Macro for generating code to stub types. One entry per long/short
239 DEF_STUB(long_branch_any_any) \
240 DEF_STUB(long_branch_v4t_arm_thumb) \
241 DEF_STUB(long_branch_thumb_only) \
242 DEF_STUB(long_branch_v4t_thumb_thumb) \
243 DEF_STUB(long_branch_v4t_thumb_arm) \
244 DEF_STUB(short_branch_v4t_thumb_arm) \
245 DEF_STUB(long_branch_any_arm_pic) \
246 DEF_STUB(long_branch_any_thumb_pic) \
247 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
248 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
249 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
250 DEF_STUB(long_branch_thumb_only_pic) \
251 DEF_STUB(a8_veneer_b_cond) \
252 DEF_STUB(a8_veneer_b) \
253 DEF_STUB(a8_veneer_bl) \
254 DEF_STUB(a8_veneer_blx)
258 #define DEF_STUB(x) arm_stub_##x,
264 // First reloc stub type.
265 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
266 // Last reloc stub type.
267 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
269 // First Cortex-A8 stub type.
270 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
271 // Last Cortex-A8 stub type.
272 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
275 arm_stub_type_last
= arm_stub_a8_veneer_blx
279 // Stub template class. Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
286 Stub_template(Stub_type
, const Insn_template
*, size_t);
294 { return this->type_
; }
296 // Return an array of instruction templates.
299 { return this->insns_
; }
301 // Return size of template in number of instructions.
304 { return this->insn_count_
; }
306 // Return size of template in bytes.
309 { return this->size_
; }
311 // Return alignment of the stub template.
314 { return this->alignment_
; }
316 // Return whether entry point is in thumb mode.
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_
; }
321 // Return number of relocations in this template.
324 { return this->relocs_
.size(); }
326 // Return index of the I-th instruction with relocation.
328 reloc_insn_index(size_t i
) const
330 gold_assert(i
< this->relocs_
.size());
331 return this->relocs_
[i
].first
;
334 // Return the offset of the I-th instruction with relocation from the
335 // beginning of the stub.
337 reloc_offset(size_t i
) const
339 gold_assert(i
< this->relocs_
.size());
340 return this->relocs_
[i
].second
;
344 // This contains information about an instruction template with a relocation
345 // and its offset from start of stub.
346 typedef std::pair
<size_t, section_size_type
> Reloc
;
348 // A Stub_template may not be copied. We want to share templates as much
350 Stub_template(const Stub_template
&);
351 Stub_template
& operator=(const Stub_template
&);
355 // Points to an array of Insn_templates.
356 const Insn_template
* insns_
;
357 // Number of Insn_templates in insns_[].
359 // Size of templated instructions in bytes.
361 // Alignment of templated instructions.
363 // Flag to indicate if entry is in thumb mode.
364 bool entry_in_thumb_mode_
;
365 // A table of reloc instruction indices and offsets. We can find these by
366 // looking at the instruction templates but we pre-compute and then stash
367 // them here for speed.
368 std::vector
<Reloc
> relocs_
;
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
379 static const section_offset_type invalid_offset
=
380 static_cast<section_offset_type
>(-1);
383 Stub(const Stub_template
* stub_template
)
384 : stub_template_(stub_template
), offset_(invalid_offset
)
391 // Return the stub template.
393 stub_template() const
394 { return this->stub_template_
; }
396 // Return offset of code stub from beginning of its containing stub table.
400 gold_assert(this->offset_
!= invalid_offset
);
401 return this->offset_
;
404 // Set offset of code stub from beginning of its containing stub table.
406 set_offset(section_offset_type offset
)
407 { this->offset_
= offset
; }
409 // Return the relocation target address of the i-th relocation in the
410 // stub. This must be defined in a child class.
412 reloc_target(size_t i
)
413 { return this->do_reloc_target(i
); }
415 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
417 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
418 { this->do_write(view
, view_size
, big_endian
); }
420 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421 // for the i-th instruction.
423 thumb16_special(size_t i
)
424 { return this->do_thumb16_special(i
); }
427 // This must be defined in the child class.
429 do_reloc_target(size_t) = 0;
431 // This may be overridden in the child class.
433 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
436 this->do_fixed_endian_write
<true>(view
, view_size
);
438 this->do_fixed_endian_write
<false>(view
, view_size
);
441 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442 // instruction template.
444 do_thumb16_special(size_t)
445 { gold_unreachable(); }
448 // A template to implement do_write.
449 template<bool big_endian
>
451 do_fixed_endian_write(unsigned char*, section_size_type
);
454 const Stub_template
* stub_template_
;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_
;
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
462 class Reloc_stub
: public Stub
465 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
466 // We assume we never jump to this address.
467 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
469 // Return destination address.
471 destination_address() const
473 gold_assert(this->destination_address_
!= this->invalid_address
);
474 return this->destination_address_
;
477 // Set destination address.
479 set_destination_address(Arm_address address
)
481 gold_assert(address
!= this->invalid_address
);
482 this->destination_address_
= address
;
485 // Reset destination address.
487 reset_destination_address()
488 { this->destination_address_
= this->invalid_address
; }
490 // Determine stub type for a branch of a relocation of R_TYPE going
491 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
492 // the branch target is a thumb instruction. TARGET is used for look
493 // up ARM-specific linker settings.
495 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
496 Arm_address branch_target
, bool target_is_thumb
);
498 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
499 // and an addend. Since we treat global and local symbol differently, we
500 // use a Symbol object for a global symbol and a object-index pair for
505 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
507 // and R_SYM must not be invalid_index.
508 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
509 unsigned int r_sym
, int32_t addend
)
510 : stub_type_(stub_type
), addend_(addend
)
514 this->r_sym_
= Reloc_stub::invalid_index
;
515 this->u_
.symbol
= symbol
;
519 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
520 this->r_sym_
= r_sym
;
521 this->u_
.relobj
= relobj
;
528 // Accessors: Keys are meant to be read-only object so no modifiers are
534 { return this->stub_type_
; }
536 // Return the local symbol index or invalid_index.
539 { return this->r_sym_
; }
541 // Return the symbol if there is one.
544 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
546 // Return the relobj if there is one.
549 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
551 // Whether this equals to another key k.
553 eq(const Key
& k
) const
555 return ((this->stub_type_
== k
.stub_type_
)
556 && (this->r_sym_
== k
.r_sym_
)
557 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
558 ? (this->u_
.relobj
== k
.u_
.relobj
)
559 : (this->u_
.symbol
== k
.u_
.symbol
))
560 && (this->addend_
== k
.addend_
));
563 // Return a hash value.
567 return (this->stub_type_
569 ^ gold::string_hash
<char>(
570 (this->r_sym_
!= Reloc_stub::invalid_index
)
571 ? this->u_
.relobj
->name().c_str()
572 : this->u_
.symbol
->name())
576 // Functors for STL associative containers.
580 operator()(const Key
& k
) const
581 { return k
.hash_value(); }
587 operator()(const Key
& k1
, const Key
& k2
) const
588 { return k1
.eq(k2
); }
591 // Name of key. This is mainly for debugging.
597 Stub_type stub_type_
;
598 // If this is a local symbol, this is the index in the defining object.
599 // Otherwise, it is invalid_index for a global symbol.
601 // If r_sym_ is invalid index. This points to a global symbol.
602 // Otherwise, this points a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj. This is done to avoid making the stub class a template
605 // as most of the stub machinery is endianity-neutral. However, it
606 // may require a bit of casting done by users of this class.
609 const Symbol
* symbol
;
610 const Relobj
* relobj
;
612 // Addend associated with a reloc.
617 // Reloc_stubs are created via a stub factory. So these are protected.
618 Reloc_stub(const Stub_template
* stub_template
)
619 : Stub(stub_template
), destination_address_(invalid_address
)
625 friend class Stub_factory
;
627 // Return the relocation target address of the i-th relocation in the
630 do_reloc_target(size_t i
)
632 // All reloc stub have only one relocation.
634 return this->destination_address_
;
638 // Address of destination.
639 Arm_address destination_address_
;
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 // branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
649 // 3. The branch follows a 32-bit instruction which is not a branch.
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least. We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch. The
654 // condition code is used in a special instruction template. We also want
655 // to identify input sections needing Cortex-A8 workaround quickly. We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up. The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
661 class Cortex_a8_stub
: public Stub
667 // Return the object of the code section containing the branch being fixed
671 { return this->relobj_
; }
673 // Return the section index of the code section containing the branch being
677 { return this->shndx_
; }
679 // Return the source address of stub. This is the address of the original
680 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
683 source_address() const
684 { return this->source_address_
; }
686 // Return the destination address of the stub. This is the branch taken
687 // address of the original branch instruction. LSB is 1 if it is a THUMB
688 // instruction address.
690 destination_address() const
691 { return this->destination_address_
; }
693 // Return the instruction being fixed up.
695 original_insn() const
696 { return this->original_insn_
; }
699 // Cortex_a8_stubs are created via a stub factory. So these are protected.
700 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
701 unsigned int shndx
, Arm_address source_address
,
702 Arm_address destination_address
, uint32_t original_insn
)
703 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
704 source_address_(source_address
| 1U),
705 destination_address_(destination_address
),
706 original_insn_(original_insn
)
709 friend class Stub_factory
;
711 // Return the relocation target address of the i-th relocation in the
714 do_reloc_target(size_t i
)
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
718 // The conditional branch veneer has two relocations.
720 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
724 // All other Cortex-A8 stubs have only one relocation.
726 return this->destination_address_
;
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
732 do_thumb16_special(size_t);
735 // Object of the code section containing the branch being fixed up.
737 // Section index of the code section containing the branch begin fixed up.
739 // Source address of original branch.
740 Arm_address source_address_
;
741 // Destination address of the original branch.
742 Arm_address destination_address_
;
743 // Original branch instruction. This is needed for copying the condition
744 // code from a condition branch to its stub.
745 uint32_t original_insn_
;
748 // Stub factory class.
753 // Return the unique instance of this class.
754 static const Stub_factory
&
757 static Stub_factory singleton
;
761 // Make a relocation stub.
763 make_reloc_stub(Stub_type stub_type
) const
765 gold_assert(stub_type
>= arm_stub_reloc_first
766 && stub_type
<= arm_stub_reloc_last
);
767 return new Reloc_stub(this->stub_templates_
[stub_type
]);
770 // Make a Cortex-A8 stub.
772 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
773 Arm_address source
, Arm_address destination
,
774 uint32_t original_insn
) const
776 gold_assert(stub_type
>= arm_stub_cortex_a8_first
777 && stub_type
<= arm_stub_cortex_a8_last
);
778 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
779 source
, destination
, original_insn
);
783 // Constructor and destructor are protected since we only return a single
784 // instance created in Stub_factory::get_instance().
788 // A Stub_factory may not be copied since it is a singleton.
789 Stub_factory(const Stub_factory
&);
790 Stub_factory
& operator=(Stub_factory
&);
792 // Stub templates. These are initialized in the constructor.
793 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
796 // A class to hold stubs for the ARM target.
798 template<bool big_endian
>
799 class Stub_table
: public Output_data
802 Stub_table(Arm_input_section
<big_endian
>* owner
)
803 : Output_data(), owner_(owner
), reloc_stubs_(), cortex_a8_stubs_(),
804 prev_data_size_(0), prev_addralign_(1)
810 // Owner of this stub table.
811 Arm_input_section
<big_endian
>*
813 { return this->owner_
; }
815 // Whether this stub table is empty.
818 { return this->reloc_stubs_
.empty() && this->cortex_a8_stubs_
.empty(); }
820 // Return the current data size.
822 current_data_size() const
823 { return this->current_data_size_for_child(); }
825 // Add a STUB with using KEY. Caller is reponsible for avoid adding
826 // if already a STUB with the same key has been added.
828 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
830 const Stub_template
* stub_template
= stub
->stub_template();
831 gold_assert(stub_template
->type() == key
.stub_type());
832 this->reloc_stubs_
[key
] = stub
;
835 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
836 // Caller is reponsible for avoid adding if already a STUB with the same
837 // address has been added.
839 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
841 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
842 this->cortex_a8_stubs_
.insert(value
);
845 // Remove all Cortex-A8 stubs.
847 remove_all_cortex_a8_stubs();
849 // Look up a relocation stub using KEY. Return NULL if there is none.
851 find_reloc_stub(const Reloc_stub::Key
& key
) const
853 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
854 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
857 // Relocate stubs in this stub table.
859 relocate_stubs(const Relocate_info
<32, big_endian
>*,
860 Target_arm
<big_endian
>*, Output_section
*,
861 unsigned char*, Arm_address
, section_size_type
);
863 // Update data size and alignment at the end of a relaxation pass. Return
864 // true if either data size or alignment is different from that of the
865 // previous relaxation pass.
867 update_data_size_and_addralign();
869 // Finalize stubs. Set the offsets of all stubs and mark input sections
870 // needing the Cortex-A8 workaround.
874 // Apply Cortex-A8 workaround to an address range.
876 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
877 unsigned char*, Arm_address
,
881 // Write out section contents.
883 do_write(Output_file
*);
885 // Return the required alignment.
888 { return this->prev_addralign_
; }
890 // Reset address and file offset.
892 do_reset_address_and_file_offset()
893 { this->set_current_data_size_for_child(this->prev_data_size_
); }
895 // Set final data size.
897 set_final_data_size()
898 { this->set_data_size(this->current_data_size()); }
901 // Relocate one stub.
903 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
904 Target_arm
<big_endian
>*, Output_section
*,
905 unsigned char*, Arm_address
, section_size_type
);
907 // Unordered map of relocation stubs.
909 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
910 Reloc_stub::Key::equal_to
>
913 // List of Cortex-A8 stubs ordered by addresses of branches being
914 // fixed up in output.
915 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
917 // Owner of this stub table.
918 Arm_input_section
<big_endian
>* owner_
;
919 // The relocation stubs.
920 Reloc_stub_map reloc_stubs_
;
921 // The cortex_a8_stubs.
922 Cortex_a8_stub_list cortex_a8_stubs_
;
923 // data size of this in the previous pass.
924 off_t prev_data_size_
;
925 // address alignment of this in the previous pass.
926 uint64_t prev_addralign_
;
929 // A class to wrap an ordinary input section containing executable code.
931 template<bool big_endian
>
932 class Arm_input_section
: public Output_relaxed_input_section
935 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
936 : Output_relaxed_input_section(relobj
, shndx
, 1),
937 original_addralign_(1), original_size_(0), stub_table_(NULL
)
947 // Whether this is a stub table owner.
949 is_stub_table_owner() const
950 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
952 // Return the stub table.
953 Stub_table
<big_endian
>*
955 { return this->stub_table_
; }
957 // Set the stub_table.
959 set_stub_table(Stub_table
<big_endian
>* stub_table
)
960 { this->stub_table_
= stub_table
; }
962 // Downcast a base pointer to an Arm_input_section pointer. This is
963 // not type-safe but we only use Arm_input_section not the base class.
964 static Arm_input_section
<big_endian
>*
965 as_arm_input_section(Output_relaxed_input_section
* poris
)
966 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
969 // Write data to output file.
971 do_write(Output_file
*);
973 // Return required alignment of this.
977 if (this->is_stub_table_owner())
978 return std::max(this->stub_table_
->addralign(),
979 this->original_addralign_
);
981 return this->original_addralign_
;
984 // Finalize data size.
986 set_final_data_size();
988 // Reset address and file offset.
990 do_reset_address_and_file_offset();
994 do_output_offset(const Relobj
* object
, unsigned int shndx
,
995 section_offset_type offset
,
996 section_offset_type
* poutput
) const
998 if ((object
== this->relobj())
999 && (shndx
== this->shndx())
1001 && (convert_types
<uint64_t, section_offset_type
>(offset
)
1002 <= this->original_size_
))
1012 // Copying is not allowed.
1013 Arm_input_section(const Arm_input_section
&);
1014 Arm_input_section
& operator=(const Arm_input_section
&);
1016 // Address alignment of the original input section.
1017 uint64_t original_addralign_
;
1018 // Section size of the original input section.
1019 uint64_t original_size_
;
1021 Stub_table
<big_endian
>* stub_table_
;
1024 // Arm output section class. This is defined mainly to add a number of
1025 // stub generation methods.
1027 template<bool big_endian
>
1028 class Arm_output_section
: public Output_section
1031 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1032 elfcpp::Elf_Xword flags
)
1033 : Output_section(name
, type
, flags
)
1036 ~Arm_output_section()
1039 // Group input sections for stub generation.
1041 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1043 // Downcast a base pointer to an Arm_output_section pointer. This is
1044 // not type-safe but we only use Arm_output_section not the base class.
1045 static Arm_output_section
<big_endian
>*
1046 as_arm_output_section(Output_section
* os
)
1047 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1051 typedef Output_section::Input_section Input_section
;
1052 typedef Output_section::Input_section_list Input_section_list
;
1054 // Create a stub group.
1055 void create_stub_group(Input_section_list::const_iterator
,
1056 Input_section_list::const_iterator
,
1057 Input_section_list::const_iterator
,
1058 Target_arm
<big_endian
>*,
1059 std::vector
<Output_relaxed_input_section
*>*);
1062 // Arm_relobj class.
1064 template<bool big_endian
>
1065 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1068 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1070 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1071 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1072 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1073 stub_tables_(), local_symbol_is_thumb_function_(),
1074 attributes_section_data_(NULL
), mapping_symbols_info_(),
1075 section_has_cortex_a8_workaround_(NULL
)
1079 { delete this->attributes_section_data_
; }
1081 // Return the stub table of the SHNDX-th section if there is one.
1082 Stub_table
<big_endian
>*
1083 stub_table(unsigned int shndx
) const
1085 gold_assert(shndx
< this->stub_tables_
.size());
1086 return this->stub_tables_
[shndx
];
1089 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1091 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1093 gold_assert(shndx
< this->stub_tables_
.size());
1094 this->stub_tables_
[shndx
] = stub_table
;
1097 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1098 // index. This is only valid after do_count_local_symbol is called.
1100 local_symbol_is_thumb_function(unsigned int r_sym
) const
1102 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1103 return this->local_symbol_is_thumb_function_
[r_sym
];
1106 // Scan all relocation sections for stub generation.
1108 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1111 // Convert regular input section with index SHNDX to a relaxed section.
1113 convert_input_section_to_relaxed_section(unsigned shndx
)
1115 // The stubs have relocations and we need to process them after writing
1116 // out the stubs. So relocation now must follow section write.
1117 this->invalidate_section_offset(shndx
);
1118 this->set_relocs_must_follow_section_writes();
1121 // Downcast a base pointer to an Arm_relobj pointer. This is
1122 // not type-safe but we only use Arm_relobj not the base class.
1123 static Arm_relobj
<big_endian
>*
1124 as_arm_relobj(Relobj
* relobj
)
1125 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1127 // Processor-specific flags in ELF file header. This is valid only after
1130 processor_specific_flags() const
1131 { return this->processor_specific_flags_
; }
1133 // Attribute section data This is the contents of the .ARM.attribute section
1135 const Attributes_section_data
*
1136 attributes_section_data() const
1137 { return this->attributes_section_data_
; }
1139 // Mapping symbol location.
1140 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1142 // Functor for STL container.
1143 struct Mapping_symbol_position_less
1146 operator()(const Mapping_symbol_position
& p1
,
1147 const Mapping_symbol_position
& p2
) const
1149 return (p1
.first
< p2
.first
1150 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1154 // We only care about the first character of a mapping symbol, so
1155 // we only store that instead of the whole symbol name.
1156 typedef std::map
<Mapping_symbol_position
, char,
1157 Mapping_symbol_position_less
> Mapping_symbols_info
;
1159 // Whether a section contains any Cortex-A8 workaround.
1161 section_has_cortex_a8_workaround(unsigned int shndx
) const
1163 return (this->section_has_cortex_a8_workaround_
!= NULL
1164 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1167 // Mark a section that has Cortex-A8 workaround.
1169 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1171 if (this->section_has_cortex_a8_workaround_
== NULL
)
1172 this->section_has_cortex_a8_workaround_
=
1173 new std::vector
<bool>(this->shnum(), false);
1174 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1178 // Post constructor setup.
1182 // Call parent's setup method.
1183 Sized_relobj
<32, big_endian
>::do_setup();
1185 // Initialize look-up tables.
1186 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1187 this->stub_tables_
.swap(empty_stub_table_list
);
1190 // Count the local symbols.
1192 do_count_local_symbols(Stringpool_template
<char>*,
1193 Stringpool_template
<char>*);
1196 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1197 const unsigned char* pshdrs
,
1198 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1200 // Read the symbol information.
1202 do_read_symbols(Read_symbols_data
* sd
);
1204 // Process relocs for garbage collection.
1206 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1210 // Whether a section needs to be scanned for relocation stubs.
1212 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1213 const Relobj::Output_sections
&,
1214 const Symbol_table
*);
1216 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1218 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1219 unsigned int, Output_section
*,
1220 const Symbol_table
*);
1222 // Scan a section for the Cortex-A8 erratum.
1224 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1225 unsigned int, Output_section
*,
1226 Target_arm
<big_endian
>*);
1228 // List of stub tables.
1229 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1230 Stub_table_list stub_tables_
;
1231 // Bit vector to tell if a local symbol is a thumb function or not.
1232 // This is only valid after do_count_local_symbol is called.
1233 std::vector
<bool> local_symbol_is_thumb_function_
;
1234 // processor-specific flags in ELF file header.
1235 elfcpp::Elf_Word processor_specific_flags_
;
1236 // Object attributes if there is an .ARM.attributes section or NULL.
1237 Attributes_section_data
* attributes_section_data_
;
1238 // Mapping symbols information.
1239 Mapping_symbols_info mapping_symbols_info_
;
1240 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1241 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1244 // Arm_dynobj class.
1246 template<bool big_endian
>
1247 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1250 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1251 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1252 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1253 processor_specific_flags_(0), attributes_section_data_(NULL
)
1257 { delete this->attributes_section_data_
; }
1259 // Downcast a base pointer to an Arm_relobj pointer. This is
1260 // not type-safe but we only use Arm_relobj not the base class.
1261 static Arm_dynobj
<big_endian
>*
1262 as_arm_dynobj(Dynobj
* dynobj
)
1263 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1265 // Processor-specific flags in ELF file header. This is valid only after
1268 processor_specific_flags() const
1269 { return this->processor_specific_flags_
; }
1271 // Attributes section data.
1272 const Attributes_section_data
*
1273 attributes_section_data() const
1274 { return this->attributes_section_data_
; }
1277 // Read the symbol information.
1279 do_read_symbols(Read_symbols_data
* sd
);
1282 // processor-specific flags in ELF file header.
1283 elfcpp::Elf_Word processor_specific_flags_
;
1284 // Object attributes if there is an .ARM.attributes section or NULL.
1285 Attributes_section_data
* attributes_section_data_
;
1288 // Functor to read reloc addends during stub generation.
1290 template<int sh_type
, bool big_endian
>
1291 struct Stub_addend_reader
1293 // Return the addend for a relocation of a particular type. Depending
1294 // on whether this is a REL or RELA relocation, read the addend from a
1295 // view or from a Reloc object.
1296 elfcpp::Elf_types
<32>::Elf_Swxword
1298 unsigned int /* r_type */,
1299 const unsigned char* /* view */,
1300 const typename Reloc_types
<sh_type
,
1301 32, big_endian
>::Reloc
& /* reloc */) const;
1304 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1306 template<bool big_endian
>
1307 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1309 elfcpp::Elf_types
<32>::Elf_Swxword
1312 const unsigned char*,
1313 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1316 // Specialized Stub_addend_reader for RELA type relocation sections.
1317 // We currently do not handle RELA type relocation sections but it is trivial
1318 // to implement the addend reader. This is provided for completeness and to
1319 // make it easier to add support for RELA relocation sections in the future.
1321 template<bool big_endian
>
1322 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1324 elfcpp::Elf_types
<32>::Elf_Swxword
1327 const unsigned char*,
1328 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1329 big_endian
>::Reloc
& reloc
) const
1330 { return reloc
.get_r_addend(); }
1333 // Cortex_a8_reloc class. We keep record of relocation that may need
1334 // the Cortex-A8 erratum workaround.
1336 class Cortex_a8_reloc
1339 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1340 Arm_address destination
)
1341 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1347 // Accessors: This is a read-only class.
1349 // Return the relocation stub associated with this relocation if there is
1353 { return this->reloc_stub_
; }
1355 // Return the relocation type.
1358 { return this->r_type_
; }
1360 // Return the destination address of the relocation. LSB stores the THUMB
1364 { return this->destination_
; }
1367 // Associated relocation stub if there is one, or NULL.
1368 const Reloc_stub
* reloc_stub_
;
1370 unsigned int r_type_
;
1371 // Destination address of this relocation. LSB is used to distinguish
1373 Arm_address destination_
;
1376 // Utilities for manipulating integers of up to 32-bits
1380 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1381 // an int32_t. NO_BITS must be between 1 to 32.
1382 template<int no_bits
>
1383 static inline int32_t
1384 sign_extend(uint32_t bits
)
1386 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1388 return static_cast<int32_t>(bits
);
1389 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1391 uint32_t top_bit
= 1U << (no_bits
- 1);
1392 int32_t as_signed
= static_cast<int32_t>(bits
);
1393 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1396 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1397 template<int no_bits
>
1399 has_overflow(uint32_t bits
)
1401 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1404 int32_t max
= (1 << (no_bits
- 1)) - 1;
1405 int32_t min
= -(1 << (no_bits
- 1));
1406 int32_t as_signed
= static_cast<int32_t>(bits
);
1407 return as_signed
> max
|| as_signed
< min
;
1410 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1411 // fits in the given number of bits as either a signed or unsigned value.
1412 // For example, has_signed_unsigned_overflow<8> would check
1413 // -128 <= bits <= 255
1414 template<int no_bits
>
1416 has_signed_unsigned_overflow(uint32_t bits
)
1418 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1421 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1422 int32_t min
= -(1 << (no_bits
- 1));
1423 int32_t as_signed
= static_cast<int32_t>(bits
);
1424 return as_signed
> max
|| as_signed
< min
;
1427 // Select bits from A and B using bits in MASK. For each n in [0..31],
1428 // the n-th bit in the result is chosen from the n-th bits of A and B.
1429 // A zero selects A and a one selects B.
1430 static inline uint32_t
1431 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1432 { return (a
& ~mask
) | (b
& mask
); }
1435 template<bool big_endian
>
1436 class Target_arm
: public Sized_target
<32, big_endian
>
1439 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1442 // When were are relocating a stub, we pass this as the relocation number.
1443 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1446 : Sized_target
<32, big_endian
>(&arm_info
),
1447 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1448 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1449 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1450 should_force_pic_veneer_(false), arm_input_section_map_(),
1451 attributes_section_data_(NULL
), fix_cortex_a8_(false),
1452 cortex_a8_relocs_info_()
1455 // Whether we can use BLX.
1458 { return this->may_use_blx_
; }
1460 // Set use-BLX flag.
1462 set_may_use_blx(bool value
)
1463 { this->may_use_blx_
= value
; }
1465 // Whether we force PCI branch veneers.
1467 should_force_pic_veneer() const
1468 { return this->should_force_pic_veneer_
; }
1470 // Set PIC veneer flag.
1472 set_should_force_pic_veneer(bool value
)
1473 { this->should_force_pic_veneer_
= value
; }
1475 // Whether we use THUMB-2 instructions.
1477 using_thumb2() const
1479 Object_attribute
* attr
=
1480 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1481 int arch
= attr
->int_value();
1482 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1485 // Whether we use THUMB/THUMB-2 instructions only.
1487 using_thumb_only() const
1489 Object_attribute
* attr
=
1490 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1491 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1492 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1494 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1495 return attr
->int_value() == 'M';
1498 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1500 may_use_arm_nop() const
1502 Object_attribute
* attr
=
1503 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1504 int arch
= attr
->int_value();
1505 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1506 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1507 || arch
== elfcpp::TAG_CPU_ARCH_V7
1508 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1511 // Whether we have THUMB-2 NOP.W instruction.
1513 may_use_thumb2_nop() const
1515 Object_attribute
* attr
=
1516 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1517 int arch
= attr
->int_value();
1518 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1519 || arch
== elfcpp::TAG_CPU_ARCH_V7
1520 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1523 // Process the relocations to determine unreferenced sections for
1524 // garbage collection.
1526 gc_process_relocs(Symbol_table
* symtab
,
1528 Sized_relobj
<32, big_endian
>* object
,
1529 unsigned int data_shndx
,
1530 unsigned int sh_type
,
1531 const unsigned char* prelocs
,
1533 Output_section
* output_section
,
1534 bool needs_special_offset_handling
,
1535 size_t local_symbol_count
,
1536 const unsigned char* plocal_symbols
);
1538 // Scan the relocations to look for symbol adjustments.
1540 scan_relocs(Symbol_table
* symtab
,
1542 Sized_relobj
<32, big_endian
>* object
,
1543 unsigned int data_shndx
,
1544 unsigned int sh_type
,
1545 const unsigned char* prelocs
,
1547 Output_section
* output_section
,
1548 bool needs_special_offset_handling
,
1549 size_t local_symbol_count
,
1550 const unsigned char* plocal_symbols
);
1552 // Finalize the sections.
1554 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1556 // Return the value to use for a dynamic symbol which requires special
1559 do_dynsym_value(const Symbol
*) const;
1561 // Relocate a section.
1563 relocate_section(const Relocate_info
<32, big_endian
>*,
1564 unsigned int sh_type
,
1565 const unsigned char* prelocs
,
1567 Output_section
* output_section
,
1568 bool needs_special_offset_handling
,
1569 unsigned char* view
,
1570 Arm_address view_address
,
1571 section_size_type view_size
,
1572 const Reloc_symbol_changes
*);
1574 // Scan the relocs during a relocatable link.
1576 scan_relocatable_relocs(Symbol_table
* symtab
,
1578 Sized_relobj
<32, big_endian
>* object
,
1579 unsigned int data_shndx
,
1580 unsigned int sh_type
,
1581 const unsigned char* prelocs
,
1583 Output_section
* output_section
,
1584 bool needs_special_offset_handling
,
1585 size_t local_symbol_count
,
1586 const unsigned char* plocal_symbols
,
1587 Relocatable_relocs
*);
1589 // Relocate a section during a relocatable link.
1591 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1592 unsigned int sh_type
,
1593 const unsigned char* prelocs
,
1595 Output_section
* output_section
,
1596 off_t offset_in_output_section
,
1597 const Relocatable_relocs
*,
1598 unsigned char* view
,
1599 Arm_address view_address
,
1600 section_size_type view_size
,
1601 unsigned char* reloc_view
,
1602 section_size_type reloc_view_size
);
1604 // Return whether SYM is defined by the ABI.
1606 do_is_defined_by_abi(Symbol
* sym
) const
1607 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1609 // Return the size of the GOT section.
1613 gold_assert(this->got_
!= NULL
);
1614 return this->got_
->data_size();
1617 // Map platform-specific reloc types
1619 get_real_reloc_type (unsigned int r_type
);
1622 // Methods to support stub-generations.
1625 // Return the stub factory
1627 stub_factory() const
1628 { return this->stub_factory_
; }
1630 // Make a new Arm_input_section object.
1631 Arm_input_section
<big_endian
>*
1632 new_arm_input_section(Relobj
*, unsigned int);
1634 // Find the Arm_input_section object corresponding to the SHNDX-th input
1635 // section of RELOBJ.
1636 Arm_input_section
<big_endian
>*
1637 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1639 // Make a new Stub_table
1640 Stub_table
<big_endian
>*
1641 new_stub_table(Arm_input_section
<big_endian
>*);
1643 // Scan a section for stub generation.
1645 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1646 const unsigned char*, size_t, Output_section
*,
1647 bool, const unsigned char*, Arm_address
,
1652 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1653 Output_section
*, unsigned char*, Arm_address
,
1656 // Get the default ARM target.
1657 static Target_arm
<big_endian
>*
1660 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1661 && parameters
->target().is_big_endian() == big_endian
);
1662 return static_cast<Target_arm
<big_endian
>*>(
1663 parameters
->sized_target
<32, big_endian
>());
1666 // Whether relocation type uses LSB to distinguish THUMB addresses.
1668 reloc_uses_thumb_bit(unsigned int r_type
);
1670 // Whether NAME belongs to a mapping symbol.
1672 is_mapping_symbol_name(const char* name
)
1676 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
1677 && (name
[2] == '\0' || name
[2] == '.'));
1680 // Whether we work around the Cortex-A8 erratum.
1682 fix_cortex_a8() const
1683 { return this->fix_cortex_a8_
; }
1685 // Scan a span of THUMB code section for Cortex-A8 erratum.
1687 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
1688 section_size_type
, section_size_type
,
1689 const unsigned char*, Arm_address
);
1691 // Apply Cortex-A8 workaround to a branch.
1693 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
1694 unsigned char*, Arm_address
);
1697 // Make an ELF object.
1699 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1700 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1703 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1704 const elfcpp::Ehdr
<32, !big_endian
>&)
1705 { gold_unreachable(); }
1708 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1709 const elfcpp::Ehdr
<64, false>&)
1710 { gold_unreachable(); }
1713 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1714 const elfcpp::Ehdr
<64, true>&)
1715 { gold_unreachable(); }
1717 // Make an output section.
1719 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1720 elfcpp::Elf_Xword flags
)
1721 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1724 do_adjust_elf_header(unsigned char* view
, int len
) const;
1726 // We only need to generate stubs, and hence perform relaxation if we are
1727 // not doing relocatable linking.
1729 do_may_relax() const
1730 { return !parameters
->options().relocatable(); }
1733 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1735 // Determine whether an object attribute tag takes an integer, a
1738 do_attribute_arg_type(int tag
) const;
1740 // Reorder tags during output.
1742 do_attributes_order(int num
) const;
1745 // The class which scans relocations.
1750 : issued_non_pic_error_(false)
1754 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1755 Sized_relobj
<32, big_endian
>* object
,
1756 unsigned int data_shndx
,
1757 Output_section
* output_section
,
1758 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1759 const elfcpp::Sym
<32, big_endian
>& lsym
);
1762 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1763 Sized_relobj
<32, big_endian
>* object
,
1764 unsigned int data_shndx
,
1765 Output_section
* output_section
,
1766 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1771 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1772 unsigned int r_type
);
1775 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1776 unsigned int r_type
, Symbol
*);
1779 check_non_pic(Relobj
*, unsigned int r_type
);
1781 // Almost identical to Symbol::needs_plt_entry except that it also
1782 // handles STT_ARM_TFUNC.
1784 symbol_needs_plt_entry(const Symbol
* sym
)
1786 // An undefined symbol from an executable does not need a PLT entry.
1787 if (sym
->is_undefined() && !parameters
->options().shared())
1790 return (!parameters
->doing_static_link()
1791 && (sym
->type() == elfcpp::STT_FUNC
1792 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1793 && (sym
->is_from_dynobj()
1794 || sym
->is_undefined()
1795 || sym
->is_preemptible()));
1798 // Whether we have issued an error about a non-PIC compilation.
1799 bool issued_non_pic_error_
;
1802 // The class which implements relocation.
1812 // Return whether the static relocation needs to be applied.
1814 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1817 Output_section
* output_section
);
1819 // Do a relocation. Return false if the caller should not issue
1820 // any warnings about this relocation.
1822 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1823 Output_section
*, size_t relnum
,
1824 const elfcpp::Rel
<32, big_endian
>&,
1825 unsigned int r_type
, const Sized_symbol
<32>*,
1826 const Symbol_value
<32>*,
1827 unsigned char*, Arm_address
,
1830 // Return whether we want to pass flag NON_PIC_REF for this
1831 // reloc. This means the relocation type accesses a symbol not via
1834 reloc_is_non_pic (unsigned int r_type
)
1838 // These relocation types reference GOT or PLT entries explicitly.
1839 case elfcpp::R_ARM_GOT_BREL
:
1840 case elfcpp::R_ARM_GOT_ABS
:
1841 case elfcpp::R_ARM_GOT_PREL
:
1842 case elfcpp::R_ARM_GOT_BREL12
:
1843 case elfcpp::R_ARM_PLT32_ABS
:
1844 case elfcpp::R_ARM_TLS_GD32
:
1845 case elfcpp::R_ARM_TLS_LDM32
:
1846 case elfcpp::R_ARM_TLS_IE32
:
1847 case elfcpp::R_ARM_TLS_IE12GP
:
1849 // These relocate types may use PLT entries.
1850 case elfcpp::R_ARM_CALL
:
1851 case elfcpp::R_ARM_THM_CALL
:
1852 case elfcpp::R_ARM_JUMP24
:
1853 case elfcpp::R_ARM_THM_JUMP24
:
1854 case elfcpp::R_ARM_THM_JUMP19
:
1855 case elfcpp::R_ARM_PLT32
:
1856 case elfcpp::R_ARM_THM_XPC22
:
1865 // A class which returns the size required for a relocation type,
1866 // used while scanning relocs during a relocatable link.
1867 class Relocatable_size_for_reloc
1871 get_size_for_reloc(unsigned int, Relobj
*);
1874 // Get the GOT section, creating it if necessary.
1875 Output_data_got
<32, big_endian
>*
1876 got_section(Symbol_table
*, Layout
*);
1878 // Get the GOT PLT section.
1880 got_plt_section() const
1882 gold_assert(this->got_plt_
!= NULL
);
1883 return this->got_plt_
;
1886 // Create a PLT entry for a global symbol.
1888 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1890 // Get the PLT section.
1891 const Output_data_plt_arm
<big_endian
>*
1894 gold_assert(this->plt_
!= NULL
);
1898 // Get the dynamic reloc section, creating it if necessary.
1900 rel_dyn_section(Layout
*);
1902 // Return true if the symbol may need a COPY relocation.
1903 // References from an executable object to non-function symbols
1904 // defined in a dynamic object may need a COPY relocation.
1906 may_need_copy_reloc(Symbol
* gsym
)
1908 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1909 && gsym
->may_need_copy_reloc());
1912 // Add a potential copy relocation.
1914 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1915 Sized_relobj
<32, big_endian
>* object
,
1916 unsigned int shndx
, Output_section
* output_section
,
1917 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1919 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1920 symtab
->get_sized_symbol
<32>(sym
),
1921 object
, shndx
, output_section
, reloc
,
1922 this->rel_dyn_section(layout
));
1925 // Whether two EABI versions are compatible.
1927 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1929 // Merge processor-specific flags from input object and those in the ELF
1930 // header of the output.
1932 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1934 // Get the secondary compatible architecture.
1936 get_secondary_compatible_arch(const Attributes_section_data
*);
1938 // Set the secondary compatible architecture.
1940 set_secondary_compatible_arch(Attributes_section_data
*, int);
1943 tag_cpu_arch_combine(const char*, int, int*, int, int);
1945 // Helper to print AEABI enum tag value.
1947 aeabi_enum_name(unsigned int);
1949 // Return string value for TAG_CPU_name.
1951 tag_cpu_name_value(unsigned int);
1953 // Merge object attributes from input object and those in the output.
1955 merge_object_attributes(const char*, const Attributes_section_data
*);
1957 // Helper to get an AEABI object attribute
1959 get_aeabi_object_attribute(int tag
) const
1961 Attributes_section_data
* pasd
= this->attributes_section_data_
;
1962 gold_assert(pasd
!= NULL
);
1963 Object_attribute
* attr
=
1964 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
1965 gold_assert(attr
!= NULL
);
1970 // Methods to support stub-generations.
1973 // Group input sections for stub generation.
1975 group_sections(Layout
*, section_size_type
, bool);
1977 // Scan a relocation for stub generation.
1979 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1980 const Sized_symbol
<32>*, unsigned int,
1981 const Symbol_value
<32>*,
1982 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1984 // Scan a relocation section for stub.
1985 template<int sh_type
>
1987 scan_reloc_section_for_stubs(
1988 const Relocate_info
<32, big_endian
>* relinfo
,
1989 const unsigned char* prelocs
,
1991 Output_section
* output_section
,
1992 bool needs_special_offset_handling
,
1993 const unsigned char* view
,
1994 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1997 // Information about this specific target which we pass to the
1998 // general Target structure.
1999 static const Target::Target_info arm_info
;
2001 // The types of GOT entries needed for this platform.
2004 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
2007 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2009 // Map input section to Arm_input_section.
2010 typedef Unordered_map
<Input_section_specifier
,
2011 Arm_input_section
<big_endian
>*,
2012 Input_section_specifier::hash
,
2013 Input_section_specifier::equal_to
>
2014 Arm_input_section_map
;
2016 // Map output addresses to relocs for Cortex-A8 erratum.
2017 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2018 Cortex_a8_relocs_info
;
2021 Output_data_got
<32, big_endian
>* got_
;
2023 Output_data_plt_arm
<big_endian
>* plt_
;
2024 // The GOT PLT section.
2025 Output_data_space
* got_plt_
;
2026 // The dynamic reloc section.
2027 Reloc_section
* rel_dyn_
;
2028 // Relocs saved to avoid a COPY reloc.
2029 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2030 // Space for variables copied with a COPY reloc.
2031 Output_data_space
* dynbss_
;
2032 // Vector of Stub_tables created.
2033 Stub_table_list stub_tables_
;
2035 const Stub_factory
&stub_factory_
;
2036 // Whether we can use BLX.
2038 // Whether we force PIC branch veneers.
2039 bool should_force_pic_veneer_
;
2040 // Map for locating Arm_input_sections.
2041 Arm_input_section_map arm_input_section_map_
;
2042 // Attributes section data in output.
2043 Attributes_section_data
* attributes_section_data_
;
2044 // Whether we want to fix code for Cortex-A8 erratum.
2045 bool fix_cortex_a8_
;
2046 // Map addresses to relocs for Cortex-A8 erratum.
2047 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2050 template<bool big_endian
>
2051 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2054 big_endian
, // is_big_endian
2055 elfcpp::EM_ARM
, // machine_code
2056 false, // has_make_symbol
2057 false, // has_resolve
2058 false, // has_code_fill
2059 true, // is_default_stack_executable
2061 "/usr/lib/libc.so.1", // dynamic_linker
2062 0x8000, // default_text_segment_address
2063 0x1000, // abi_pagesize (overridable by -z max-page-size)
2064 0x1000, // common_pagesize (overridable by -z common-page-size)
2065 elfcpp::SHN_UNDEF
, // small_common_shndx
2066 elfcpp::SHN_UNDEF
, // large_common_shndx
2067 0, // small_common_section_flags
2068 0, // large_common_section_flags
2069 ".ARM.attributes", // attributes_section
2070 "aeabi" // attributes_vendor
2073 // Arm relocate functions class
2076 template<bool big_endian
>
2077 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2082 STATUS_OKAY
, // No error during relocation.
2083 STATUS_OVERFLOW
, // Relocation oveflow.
2084 STATUS_BAD_RELOC
// Relocation cannot be applied.
2088 typedef Relocate_functions
<32, big_endian
> Base
;
2089 typedef Arm_relocate_functions
<big_endian
> This
;
2091 // Encoding of imm16 argument for movt and movw ARM instructions
2094 // imm16 := imm4 | imm12
2096 // 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
2097 // +-------+---------------+-------+-------+-----------------------+
2098 // | | |imm4 | |imm12 |
2099 // +-------+---------------+-------+-------+-----------------------+
2101 // Extract the relocation addend from VAL based on the ARM
2102 // instruction encoding described above.
2103 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2104 extract_arm_movw_movt_addend(
2105 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2107 // According to the Elf ABI for ARM Architecture the immediate
2108 // field is sign-extended to form the addend.
2109 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2112 // Insert X into VAL based on the ARM instruction encoding described
2114 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2115 insert_val_arm_movw_movt(
2116 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2117 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2121 val
|= (x
& 0xf000) << 4;
2125 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2128 // imm16 := imm4 | i | imm3 | imm8
2130 // 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
2131 // +---------+-+-----------+-------++-+-----+-------+---------------+
2132 // | |i| |imm4 || |imm3 | |imm8 |
2133 // +---------+-+-----------+-------++-+-----+-------+---------------+
2135 // Extract the relocation addend from VAL based on the Thumb2
2136 // instruction encoding described above.
2137 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2138 extract_thumb_movw_movt_addend(
2139 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2141 // According to the Elf ABI for ARM Architecture the immediate
2142 // field is sign-extended to form the addend.
2143 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2144 | ((val
>> 15) & 0x0800)
2145 | ((val
>> 4) & 0x0700)
2149 // Insert X into VAL based on the Thumb2 instruction encoding
2151 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2152 insert_val_thumb_movw_movt(
2153 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2154 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2157 val
|= (x
& 0xf000) << 4;
2158 val
|= (x
& 0x0800) << 15;
2159 val
|= (x
& 0x0700) << 4;
2160 val
|= (x
& 0x00ff);
2164 // Handle ARM long branches.
2165 static typename
This::Status
2166 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2167 unsigned char *, const Sized_symbol
<32>*,
2168 const Arm_relobj
<big_endian
>*, unsigned int,
2169 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2171 // Handle THUMB long branches.
2172 static typename
This::Status
2173 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2174 unsigned char *, const Sized_symbol
<32>*,
2175 const Arm_relobj
<big_endian
>*, unsigned int,
2176 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2180 // Return the branch offset of a 32-bit THUMB branch.
2181 static inline int32_t
2182 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2184 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2185 // involving the J1 and J2 bits.
2186 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2187 uint32_t upper
= upper_insn
& 0x3ffU
;
2188 uint32_t lower
= lower_insn
& 0x7ffU
;
2189 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2190 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2191 uint32_t i1
= j1
^ s
? 0 : 1;
2192 uint32_t i2
= j2
^ s
? 0 : 1;
2194 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2195 | (upper
<< 12) | (lower
<< 1));
2198 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2199 // UPPER_INSN is the original upper instruction of the branch. Caller is
2200 // responsible for overflow checking and BLX offset adjustment.
2201 static inline uint16_t
2202 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2204 uint32_t s
= offset
< 0 ? 1 : 0;
2205 uint32_t bits
= static_cast<uint32_t>(offset
);
2206 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2209 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2210 // LOWER_INSN is the original lower instruction of the branch. Caller is
2211 // responsible for overflow checking and BLX offset adjustment.
2212 static inline uint16_t
2213 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2215 uint32_t s
= offset
< 0 ? 1 : 0;
2216 uint32_t bits
= static_cast<uint32_t>(offset
);
2217 return ((lower_insn
& ~0x2fffU
)
2218 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2219 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2220 | ((bits
>> 1) & 0x7ffU
));
2223 // Return the branch offset of a 32-bit THUMB conditional branch.
2224 static inline int32_t
2225 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2227 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2228 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2229 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2230 uint32_t lower
= (lower_insn
& 0x07ffU
);
2231 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2233 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2236 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2237 // instruction. UPPER_INSN is the original upper instruction of the branch.
2238 // Caller is responsible for overflow checking.
2239 static inline uint16_t
2240 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2242 uint32_t s
= offset
< 0 ? 1 : 0;
2243 uint32_t bits
= static_cast<uint32_t>(offset
);
2244 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2247 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2248 // instruction. LOWER_INSN is the original lower instruction of the branch.
2249 // Caller is reponsible for overflow checking.
2250 static inline uint16_t
2251 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2253 uint32_t bits
= static_cast<uint32_t>(offset
);
2254 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2255 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2256 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2258 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
2261 // R_ARM_ABS8: S + A
2262 static inline typename
This::Status
2263 abs8(unsigned char *view
,
2264 const Sized_relobj
<32, big_endian
>* object
,
2265 const Symbol_value
<32>* psymval
)
2267 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2268 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2269 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2270 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2271 Reltype addend
= utils::sign_extend
<8>(val
);
2272 Reltype x
= psymval
->value(object
, addend
);
2273 val
= utils::bit_select(val
, x
, 0xffU
);
2274 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2275 return (utils::has_signed_unsigned_overflow
<8>(x
)
2276 ? This::STATUS_OVERFLOW
2277 : This::STATUS_OKAY
);
2280 // R_ARM_THM_ABS5: S + A
2281 static inline typename
This::Status
2282 thm_abs5(unsigned char *view
,
2283 const Sized_relobj
<32, big_endian
>* object
,
2284 const Symbol_value
<32>* psymval
)
2286 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2287 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2288 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2289 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2290 Reltype addend
= (val
& 0x7e0U
) >> 6;
2291 Reltype x
= psymval
->value(object
, addend
);
2292 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2293 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2294 return (utils::has_overflow
<5>(x
)
2295 ? This::STATUS_OVERFLOW
2296 : This::STATUS_OKAY
);
2299 // R_ARM_ABS12: S + A
2300 static inline typename
This::Status
2301 abs12(unsigned char *view
,
2302 const Sized_relobj
<32, big_endian
>* object
,
2303 const Symbol_value
<32>* psymval
)
2305 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2306 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2307 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2308 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2309 Reltype addend
= val
& 0x0fffU
;
2310 Reltype x
= psymval
->value(object
, addend
);
2311 val
= utils::bit_select(val
, x
, 0x0fffU
);
2312 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2313 return (utils::has_overflow
<12>(x
)
2314 ? This::STATUS_OVERFLOW
2315 : This::STATUS_OKAY
);
2318 // R_ARM_ABS16: S + A
2319 static inline typename
This::Status
2320 abs16(unsigned char *view
,
2321 const Sized_relobj
<32, big_endian
>* object
,
2322 const Symbol_value
<32>* psymval
)
2324 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2325 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2326 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2327 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2328 Reltype addend
= utils::sign_extend
<16>(val
);
2329 Reltype x
= psymval
->value(object
, addend
);
2330 val
= utils::bit_select(val
, x
, 0xffffU
);
2331 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2332 return (utils::has_signed_unsigned_overflow
<16>(x
)
2333 ? This::STATUS_OVERFLOW
2334 : This::STATUS_OKAY
);
2337 // R_ARM_ABS32: (S + A) | T
2338 static inline typename
This::Status
2339 abs32(unsigned char *view
,
2340 const Sized_relobj
<32, big_endian
>* object
,
2341 const Symbol_value
<32>* psymval
,
2342 Arm_address thumb_bit
)
2344 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2345 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2346 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2347 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2348 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2349 return This::STATUS_OKAY
;
2352 // R_ARM_REL32: (S + A) | T - P
2353 static inline typename
This::Status
2354 rel32(unsigned char *view
,
2355 const Sized_relobj
<32, big_endian
>* object
,
2356 const Symbol_value
<32>* psymval
,
2357 Arm_address address
,
2358 Arm_address thumb_bit
)
2360 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2361 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2362 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2363 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2364 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2365 return This::STATUS_OKAY
;
2368 // R_ARM_THM_CALL: (S + A) | T - P
2369 static inline typename
This::Status
2370 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2371 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2372 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2373 Arm_address address
, Arm_address thumb_bit
,
2374 bool is_weakly_undefined_without_plt
)
2376 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2377 object
, r_sym
, psymval
, address
, thumb_bit
,
2378 is_weakly_undefined_without_plt
);
2381 // R_ARM_THM_JUMP24: (S + A) | T - P
2382 static inline typename
This::Status
2383 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2384 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2385 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2386 Arm_address address
, Arm_address thumb_bit
,
2387 bool is_weakly_undefined_without_plt
)
2389 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2390 object
, r_sym
, psymval
, address
, thumb_bit
,
2391 is_weakly_undefined_without_plt
);
2394 // R_ARM_THM_JUMP24: (S + A) | T - P
2395 static typename
This::Status
2396 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
2397 const Symbol_value
<32>* psymval
, Arm_address address
,
2398 Arm_address thumb_bit
);
2400 // R_ARM_THM_XPC22: (S + A) | T - P
2401 static inline typename
This::Status
2402 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2403 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2404 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2405 Arm_address address
, Arm_address thumb_bit
,
2406 bool is_weakly_undefined_without_plt
)
2408 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2409 object
, r_sym
, psymval
, address
, thumb_bit
,
2410 is_weakly_undefined_without_plt
);
2413 // R_ARM_BASE_PREL: B(S) + A - P
2414 static inline typename
This::Status
2415 base_prel(unsigned char* view
,
2417 Arm_address address
)
2419 Base::rel32(view
, origin
- address
);
2423 // R_ARM_BASE_ABS: B(S) + A
2424 static inline typename
This::Status
2425 base_abs(unsigned char* view
,
2428 Base::rel32(view
, origin
);
2432 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2433 static inline typename
This::Status
2434 got_brel(unsigned char* view
,
2435 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2437 Base::rel32(view
, got_offset
);
2438 return This::STATUS_OKAY
;
2441 // R_ARM_GOT_PREL: GOT(S) + A - P
2442 static inline typename
This::Status
2443 got_prel(unsigned char *view
,
2444 Arm_address got_entry
,
2445 Arm_address address
)
2447 Base::rel32(view
, got_entry
- address
);
2448 return This::STATUS_OKAY
;
2451 // R_ARM_PLT32: (S + A) | T - P
2452 static inline typename
This::Status
2453 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2454 unsigned char *view
,
2455 const Sized_symbol
<32>* gsym
,
2456 const Arm_relobj
<big_endian
>* object
,
2458 const Symbol_value
<32>* psymval
,
2459 Arm_address address
,
2460 Arm_address thumb_bit
,
2461 bool is_weakly_undefined_without_plt
)
2463 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2464 object
, r_sym
, psymval
, address
, thumb_bit
,
2465 is_weakly_undefined_without_plt
);
2468 // R_ARM_XPC25: (S + A) | T - P
2469 static inline typename
This::Status
2470 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2471 unsigned char *view
,
2472 const Sized_symbol
<32>* gsym
,
2473 const Arm_relobj
<big_endian
>* object
,
2475 const Symbol_value
<32>* psymval
,
2476 Arm_address address
,
2477 Arm_address thumb_bit
,
2478 bool is_weakly_undefined_without_plt
)
2480 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2481 object
, r_sym
, psymval
, address
, thumb_bit
,
2482 is_weakly_undefined_without_plt
);
2485 // R_ARM_CALL: (S + A) | T - P
2486 static inline typename
This::Status
2487 call(const Relocate_info
<32, big_endian
>* relinfo
,
2488 unsigned char *view
,
2489 const Sized_symbol
<32>* gsym
,
2490 const Arm_relobj
<big_endian
>* object
,
2492 const Symbol_value
<32>* psymval
,
2493 Arm_address address
,
2494 Arm_address thumb_bit
,
2495 bool is_weakly_undefined_without_plt
)
2497 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2498 object
, r_sym
, psymval
, address
, thumb_bit
,
2499 is_weakly_undefined_without_plt
);
2502 // R_ARM_JUMP24: (S + A) | T - P
2503 static inline typename
This::Status
2504 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2505 unsigned char *view
,
2506 const Sized_symbol
<32>* gsym
,
2507 const Arm_relobj
<big_endian
>* object
,
2509 const Symbol_value
<32>* psymval
,
2510 Arm_address address
,
2511 Arm_address thumb_bit
,
2512 bool is_weakly_undefined_without_plt
)
2514 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2515 object
, r_sym
, psymval
, address
, thumb_bit
,
2516 is_weakly_undefined_without_plt
);
2519 // R_ARM_PREL: (S + A) | T - P
2520 static inline typename
This::Status
2521 prel31(unsigned char *view
,
2522 const Sized_relobj
<32, big_endian
>* object
,
2523 const Symbol_value
<32>* psymval
,
2524 Arm_address address
,
2525 Arm_address thumb_bit
)
2527 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2528 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2529 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2530 Valtype addend
= utils::sign_extend
<31>(val
);
2531 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2532 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2533 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2534 return (utils::has_overflow
<31>(x
) ?
2535 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2538 // R_ARM_MOVW_ABS_NC: (S + A) | T
2539 static inline typename
This::Status
2540 movw_abs_nc(unsigned char *view
,
2541 const Sized_relobj
<32, big_endian
>* object
,
2542 const Symbol_value
<32>* psymval
,
2543 Arm_address thumb_bit
)
2545 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2546 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2547 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2548 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2549 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2550 val
= This::insert_val_arm_movw_movt(val
, x
);
2551 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2552 return This::STATUS_OKAY
;
2555 // R_ARM_MOVT_ABS: S + A
2556 static inline typename
This::Status
2557 movt_abs(unsigned char *view
,
2558 const Sized_relobj
<32, big_endian
>* object
,
2559 const Symbol_value
<32>* psymval
)
2561 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2562 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2563 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2564 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2565 Valtype x
= psymval
->value(object
, addend
) >> 16;
2566 val
= This::insert_val_arm_movw_movt(val
, x
);
2567 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2568 return This::STATUS_OKAY
;
2571 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2572 static inline typename
This::Status
2573 thm_movw_abs_nc(unsigned char *view
,
2574 const Sized_relobj
<32, big_endian
>* object
,
2575 const Symbol_value
<32>* psymval
,
2576 Arm_address thumb_bit
)
2578 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2579 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2580 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2581 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2582 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2583 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2584 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2585 val
= This::insert_val_thumb_movw_movt(val
, x
);
2586 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2587 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2588 return This::STATUS_OKAY
;
2591 // R_ARM_THM_MOVT_ABS: S + A
2592 static inline typename
This::Status
2593 thm_movt_abs(unsigned char *view
,
2594 const Sized_relobj
<32, big_endian
>* object
,
2595 const Symbol_value
<32>* psymval
)
2597 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2598 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2599 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2600 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2601 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2602 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2603 Reltype x
= psymval
->value(object
, addend
) >> 16;
2604 val
= This::insert_val_thumb_movw_movt(val
, x
);
2605 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2606 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2607 return This::STATUS_OKAY
;
2610 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2611 static inline typename
This::Status
2612 movw_prel_nc(unsigned char *view
,
2613 const Sized_relobj
<32, big_endian
>* object
,
2614 const Symbol_value
<32>* psymval
,
2615 Arm_address address
,
2616 Arm_address thumb_bit
)
2618 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2619 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2620 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2621 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2622 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2623 val
= This::insert_val_arm_movw_movt(val
, x
);
2624 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2625 return This::STATUS_OKAY
;
2628 // R_ARM_MOVT_PREL: S + A - P
2629 static inline typename
This::Status
2630 movt_prel(unsigned char *view
,
2631 const Sized_relobj
<32, big_endian
>* object
,
2632 const Symbol_value
<32>* psymval
,
2633 Arm_address address
)
2635 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2636 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2637 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2638 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2639 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2640 val
= This::insert_val_arm_movw_movt(val
, x
);
2641 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2642 return This::STATUS_OKAY
;
2645 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2646 static inline typename
This::Status
2647 thm_movw_prel_nc(unsigned char *view
,
2648 const Sized_relobj
<32, big_endian
>* object
,
2649 const Symbol_value
<32>* psymval
,
2650 Arm_address address
,
2651 Arm_address thumb_bit
)
2653 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2654 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2655 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2656 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2657 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2658 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2659 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2660 val
= This::insert_val_thumb_movw_movt(val
, x
);
2661 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2662 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2663 return This::STATUS_OKAY
;
2666 // R_ARM_THM_MOVT_PREL: S + A - P
2667 static inline typename
This::Status
2668 thm_movt_prel(unsigned char *view
,
2669 const Sized_relobj
<32, big_endian
>* object
,
2670 const Symbol_value
<32>* psymval
,
2671 Arm_address address
)
2673 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2674 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2675 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2676 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2677 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2678 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2679 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2680 val
= This::insert_val_thumb_movw_movt(val
, x
);
2681 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2682 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2683 return This::STATUS_OKAY
;
2687 // Relocate ARM long branches. This handles relocation types
2688 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2689 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2690 // undefined and we do not use PLT in this relocation. In such a case,
2691 // the branch is converted into an NOP.
2693 template<bool big_endian
>
2694 typename Arm_relocate_functions
<big_endian
>::Status
2695 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2696 unsigned int r_type
,
2697 const Relocate_info
<32, big_endian
>* relinfo
,
2698 unsigned char *view
,
2699 const Sized_symbol
<32>* gsym
,
2700 const Arm_relobj
<big_endian
>* object
,
2702 const Symbol_value
<32>* psymval
,
2703 Arm_address address
,
2704 Arm_address thumb_bit
,
2705 bool is_weakly_undefined_without_plt
)
2707 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2708 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2709 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2711 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2712 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2713 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2714 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2715 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2716 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2717 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2719 // Check that the instruction is valid.
2720 if (r_type
== elfcpp::R_ARM_CALL
)
2722 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2723 return This::STATUS_BAD_RELOC
;
2725 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2727 if (!insn_is_b
&& !insn_is_cond_bl
)
2728 return This::STATUS_BAD_RELOC
;
2730 else if (r_type
== elfcpp::R_ARM_PLT32
)
2732 if (!insn_is_any_branch
)
2733 return This::STATUS_BAD_RELOC
;
2735 else if (r_type
== elfcpp::R_ARM_XPC25
)
2737 // FIXME: AAELF document IH0044C does not say much about it other
2738 // than it being obsolete.
2739 if (!insn_is_any_branch
)
2740 return This::STATUS_BAD_RELOC
;
2745 // A branch to an undefined weak symbol is turned into a jump to
2746 // the next instruction unless a PLT entry will be created.
2747 // Do the same for local undefined symbols.
2748 // The jump to the next instruction is optimized as a NOP depending
2749 // on the architecture.
2750 const Target_arm
<big_endian
>* arm_target
=
2751 Target_arm
<big_endian
>::default_target();
2752 if (is_weakly_undefined_without_plt
)
2754 Valtype cond
= val
& 0xf0000000U
;
2755 if (arm_target
->may_use_arm_nop())
2756 val
= cond
| 0x0320f000;
2758 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2759 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2760 return This::STATUS_OKAY
;
2763 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2764 Valtype branch_target
= psymval
->value(object
, addend
);
2765 int32_t branch_offset
= branch_target
- address
;
2767 // We need a stub if the branch offset is too large or if we need
2769 bool may_use_blx
= arm_target
->may_use_blx();
2770 Reloc_stub
* stub
= NULL
;
2771 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2772 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2773 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2775 Stub_type stub_type
=
2776 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2778 if (stub_type
!= arm_stub_none
)
2780 Stub_table
<big_endian
>* stub_table
=
2781 object
->stub_table(relinfo
->data_shndx
);
2782 gold_assert(stub_table
!= NULL
);
2784 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2785 stub
= stub_table
->find_reloc_stub(stub_key
);
2786 gold_assert(stub
!= NULL
);
2787 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2788 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2789 branch_offset
= branch_target
- address
;
2790 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2791 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2795 // At this point, if we still need to switch mode, the instruction
2796 // must either be a BLX or a BL that can be converted to a BLX.
2800 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2801 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2804 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2805 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2806 return (utils::has_overflow
<26>(branch_offset
)
2807 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2810 // Relocate THUMB long branches. This handles relocation types
2811 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2812 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2813 // undefined and we do not use PLT in this relocation. In such a case,
2814 // the branch is converted into an NOP.
2816 template<bool big_endian
>
2817 typename Arm_relocate_functions
<big_endian
>::Status
2818 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2819 unsigned int r_type
,
2820 const Relocate_info
<32, big_endian
>* relinfo
,
2821 unsigned char *view
,
2822 const Sized_symbol
<32>* gsym
,
2823 const Arm_relobj
<big_endian
>* object
,
2825 const Symbol_value
<32>* psymval
,
2826 Arm_address address
,
2827 Arm_address thumb_bit
,
2828 bool is_weakly_undefined_without_plt
)
2830 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2831 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2832 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2833 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2835 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2837 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2838 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2840 // Check that the instruction is valid.
2841 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2843 if (!is_bl_insn
&& !is_blx_insn
)
2844 return This::STATUS_BAD_RELOC
;
2846 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2848 // This cannot be a BLX.
2850 return This::STATUS_BAD_RELOC
;
2852 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2854 // Check for Thumb to Thumb call.
2856 return This::STATUS_BAD_RELOC
;
2859 gold_warning(_("%s: Thumb BLX instruction targets "
2860 "thumb function '%s'."),
2861 object
->name().c_str(),
2862 (gsym
? gsym
->name() : "(local)"));
2863 // Convert BLX to BL.
2864 lower_insn
|= 0x1000U
;
2870 // A branch to an undefined weak symbol is turned into a jump to
2871 // the next instruction unless a PLT entry will be created.
2872 // The jump to the next instruction is optimized as a NOP.W for
2873 // Thumb-2 enabled architectures.
2874 const Target_arm
<big_endian
>* arm_target
=
2875 Target_arm
<big_endian
>::default_target();
2876 if (is_weakly_undefined_without_plt
)
2878 if (arm_target
->may_use_thumb2_nop())
2880 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2881 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2885 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2886 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2888 return This::STATUS_OKAY
;
2891 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
2892 Arm_address branch_target
= psymval
->value(object
, addend
);
2893 int32_t branch_offset
= branch_target
- address
;
2895 // We need a stub if the branch offset is too large or if we need
2897 bool may_use_blx
= arm_target
->may_use_blx();
2898 bool thumb2
= arm_target
->using_thumb2();
2900 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2901 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2903 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2904 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2905 || ((thumb_bit
== 0)
2906 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2907 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2909 Stub_type stub_type
=
2910 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2912 if (stub_type
!= arm_stub_none
)
2914 Stub_table
<big_endian
>* stub_table
=
2915 object
->stub_table(relinfo
->data_shndx
);
2916 gold_assert(stub_table
!= NULL
);
2918 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2919 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2920 gold_assert(stub
!= NULL
);
2921 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2922 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2923 branch_offset
= branch_target
- address
;
2927 // At this point, if we still need to switch mode, the instruction
2928 // must either be a BLX or a BL that can be converted to a BLX.
2931 gold_assert(may_use_blx
2932 && (r_type
== elfcpp::R_ARM_THM_CALL
2933 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2934 // Make sure this is a BLX.
2935 lower_insn
&= ~0x1000U
;
2939 // Make sure this is a BL.
2940 lower_insn
|= 0x1000U
;
2943 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2944 // For a BLX instruction, make sure that the relocation is rounded up
2945 // to a word boundary. This follows the semantics of the instruction
2946 // which specifies that bit 1 of the target address will come from bit
2947 // 1 of the base address.
2948 branch_offset
= (branch_offset
+ 2) & ~3;
2950 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2951 // We use the Thumb-2 encoding, which is safe even if dealing with
2952 // a Thumb-1 instruction by virtue of our overflow check above. */
2953 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
2954 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
2956 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2957 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2960 ? utils::has_overflow
<25>(branch_offset
)
2961 : utils::has_overflow
<23>(branch_offset
))
2962 ? This::STATUS_OVERFLOW
2963 : This::STATUS_OKAY
);
2966 // Relocate THUMB-2 long conditional branches.
2967 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2968 // undefined and we do not use PLT in this relocation. In such a case,
2969 // the branch is converted into an NOP.
2971 template<bool big_endian
>
2972 typename Arm_relocate_functions
<big_endian
>::Status
2973 Arm_relocate_functions
<big_endian
>::thm_jump19(
2974 unsigned char *view
,
2975 const Arm_relobj
<big_endian
>* object
,
2976 const Symbol_value
<32>* psymval
,
2977 Arm_address address
,
2978 Arm_address thumb_bit
)
2980 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2981 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2982 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2983 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2984 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
2986 Arm_address branch_target
= psymval
->value(object
, addend
);
2987 int32_t branch_offset
= branch_target
- address
;
2989 // ??? Should handle interworking? GCC might someday try to
2990 // use this for tail calls.
2991 // FIXME: We do support thumb entry to PLT yet.
2994 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
2995 return This::STATUS_BAD_RELOC
;
2998 // Put RELOCATION back into the insn.
2999 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
3000 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
3002 // Put the relocated value back in the object file:
3003 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3004 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3006 return (utils::has_overflow
<21>(branch_offset
)
3007 ? This::STATUS_OVERFLOW
3008 : This::STATUS_OKAY
);
3011 // Get the GOT section, creating it if necessary.
3013 template<bool big_endian
>
3014 Output_data_got
<32, big_endian
>*
3015 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
3017 if (this->got_
== NULL
)
3019 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3021 this->got_
= new Output_data_got
<32, big_endian
>();
3024 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3026 | elfcpp::SHF_WRITE
),
3027 this->got_
, false, true, true,
3030 // The old GNU linker creates a .got.plt section. We just
3031 // create another set of data in the .got section. Note that we
3032 // always create a PLT if we create a GOT, although the PLT
3034 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
3035 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3037 | elfcpp::SHF_WRITE
),
3038 this->got_plt_
, false, false,
3041 // The first three entries are reserved.
3042 this->got_plt_
->set_current_data_size(3 * 4);
3044 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3045 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3046 Symbol_table::PREDEFINED
,
3048 0, 0, elfcpp::STT_OBJECT
,
3050 elfcpp::STV_HIDDEN
, 0,
3056 // Get the dynamic reloc section, creating it if necessary.
3058 template<bool big_endian
>
3059 typename Target_arm
<big_endian
>::Reloc_section
*
3060 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
3062 if (this->rel_dyn_
== NULL
)
3064 gold_assert(layout
!= NULL
);
3065 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
3066 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
3067 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
3068 false, false, false);
3070 return this->rel_dyn_
;
3073 // Insn_template methods.
3075 // Return byte size of an instruction template.
3078 Insn_template::size() const
3080 switch (this->type())
3083 case THUMB16_SPECIAL_TYPE
:
3094 // Return alignment of an instruction template.
3097 Insn_template::alignment() const
3099 switch (this->type())
3102 case THUMB16_SPECIAL_TYPE
:
3113 // Stub_template methods.
3115 Stub_template::Stub_template(
3116 Stub_type type
, const Insn_template
* insns
,
3118 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
3119 entry_in_thumb_mode_(false), relocs_()
3123 // Compute byte size and alignment of stub template.
3124 for (size_t i
= 0; i
< insn_count
; i
++)
3126 unsigned insn_alignment
= insns
[i
].alignment();
3127 size_t insn_size
= insns
[i
].size();
3128 gold_assert((offset
& (insn_alignment
- 1)) == 0);
3129 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
3130 switch (insns
[i
].type())
3132 case Insn_template::THUMB16_TYPE
:
3133 case Insn_template::THUMB16_SPECIAL_TYPE
:
3135 this->entry_in_thumb_mode_
= true;
3138 case Insn_template::THUMB32_TYPE
:
3139 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
3140 this->relocs_
.push_back(Reloc(i
, offset
));
3142 this->entry_in_thumb_mode_
= true;
3145 case Insn_template::ARM_TYPE
:
3146 // Handle cases where the target is encoded within the
3148 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
3149 this->relocs_
.push_back(Reloc(i
, offset
));
3152 case Insn_template::DATA_TYPE
:
3153 // Entry point cannot be data.
3154 gold_assert(i
!= 0);
3155 this->relocs_
.push_back(Reloc(i
, offset
));
3161 offset
+= insn_size
;
3163 this->size_
= offset
;
3168 // Template to implement do_write for a specific target endianity.
3170 template<bool big_endian
>
3172 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
3174 const Stub_template
* stub_template
= this->stub_template();
3175 const Insn_template
* insns
= stub_template
->insns();
3177 // FIXME: We do not handle BE8 encoding yet.
3178 unsigned char* pov
= view
;
3179 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
3181 switch (insns
[i
].type())
3183 case Insn_template::THUMB16_TYPE
:
3184 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
3186 case Insn_template::THUMB16_SPECIAL_TYPE
:
3187 elfcpp::Swap
<16, big_endian
>::writeval(
3189 this->thumb16_special(i
));
3191 case Insn_template::THUMB32_TYPE
:
3193 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
3194 uint32_t lo
= insns
[i
].data() & 0xffff;
3195 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
3196 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
3199 case Insn_template::ARM_TYPE
:
3200 case Insn_template::DATA_TYPE
:
3201 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
3206 pov
+= insns
[i
].size();
3208 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
3211 // Reloc_stub::Key methods.
3213 // Dump a Key as a string for debugging.
3216 Reloc_stub::Key::name() const
3218 if (this->r_sym_
== invalid_index
)
3220 // Global symbol key name
3221 // <stub-type>:<symbol name>:<addend>.
3222 const std::string sym_name
= this->u_
.symbol
->name();
3223 // We need to print two hex number and two colons. So just add 100 bytes
3224 // to the symbol name size.
3225 size_t len
= sym_name
.size() + 100;
3226 char* buffer
= new char[len
];
3227 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
3228 sym_name
.c_str(), this->addend_
);
3229 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3231 return std::string(buffer
);
3235 // local symbol key name
3236 // <stub-type>:<object>:<r_sym>:<addend>.
3237 const size_t len
= 200;
3239 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3240 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3241 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3242 return std::string(buffer
);
3246 // Reloc_stub methods.
3248 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3249 // LOCATION to DESTINATION.
3250 // This code is based on the arm_type_of_stub function in
3251 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3255 Reloc_stub::stub_type_for_reloc(
3256 unsigned int r_type
,
3257 Arm_address location
,
3258 Arm_address destination
,
3259 bool target_is_thumb
)
3261 Stub_type stub_type
= arm_stub_none
;
3263 // This is a bit ugly but we want to avoid using a templated class for
3264 // big and little endianities.
3266 bool should_force_pic_veneer
;
3269 if (parameters
->target().is_big_endian())
3271 const Target_arm
<true>* big_endian_target
=
3272 Target_arm
<true>::default_target();
3273 may_use_blx
= big_endian_target
->may_use_blx();
3274 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3275 thumb2
= big_endian_target
->using_thumb2();
3276 thumb_only
= big_endian_target
->using_thumb_only();
3280 const Target_arm
<false>* little_endian_target
=
3281 Target_arm
<false>::default_target();
3282 may_use_blx
= little_endian_target
->may_use_blx();
3283 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3284 thumb2
= little_endian_target
->using_thumb2();
3285 thumb_only
= little_endian_target
->using_thumb_only();
3288 int64_t branch_offset
= (int64_t)destination
- location
;
3290 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3292 // Handle cases where:
3293 // - this call goes too far (different Thumb/Thumb2 max
3295 // - it's a Thumb->Arm call and blx is not available, or it's a
3296 // Thumb->Arm branch (not bl). A stub is needed in this case.
3298 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3299 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3301 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3302 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3303 || ((!target_is_thumb
)
3304 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3305 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3307 if (target_is_thumb
)
3312 stub_type
= (parameters
->options().shared()
3313 || should_force_pic_veneer
)
3316 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3317 // V5T and above. Stub starts with ARM code, so
3318 // we must be able to switch mode before
3319 // reaching it, which is only possible for 'bl'
3320 // (ie R_ARM_THM_CALL relocation).
3321 ? arm_stub_long_branch_any_thumb_pic
3322 // On V4T, use Thumb code only.
3323 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3327 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3328 ? arm_stub_long_branch_any_any
// V5T and above.
3329 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3333 stub_type
= (parameters
->options().shared()
3334 || should_force_pic_veneer
)
3335 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3336 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3343 // FIXME: We should check that the input section is from an
3344 // object that has interwork enabled.
3346 stub_type
= (parameters
->options().shared()
3347 || should_force_pic_veneer
)
3350 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3351 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3352 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3356 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3357 ? arm_stub_long_branch_any_any
// V5T and above.
3358 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3360 // Handle v4t short branches.
3361 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3362 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3363 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3364 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3368 else if (r_type
== elfcpp::R_ARM_CALL
3369 || r_type
== elfcpp::R_ARM_JUMP24
3370 || r_type
== elfcpp::R_ARM_PLT32
)
3372 if (target_is_thumb
)
3376 // FIXME: We should check that the input section is from an
3377 // object that has interwork enabled.
3379 // We have an extra 2-bytes reach because of
3380 // the mode change (bit 24 (H) of BLX encoding).
3381 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3382 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3383 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3384 || (r_type
== elfcpp::R_ARM_JUMP24
)
3385 || (r_type
== elfcpp::R_ARM_PLT32
))
3387 stub_type
= (parameters
->options().shared()
3388 || should_force_pic_veneer
)
3391 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3392 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3396 ? arm_stub_long_branch_any_any
// V5T and above.
3397 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3403 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3404 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3406 stub_type
= (parameters
->options().shared()
3407 || should_force_pic_veneer
)
3408 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3409 : arm_stub_long_branch_any_any
; /// non-PIC.
3417 // Cortex_a8_stub methods.
3419 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3420 // I is the position of the instruction template in the stub template.
3423 Cortex_a8_stub::do_thumb16_special(size_t i
)
3425 // The only use of this is to copy condition code from a conditional
3426 // branch being worked around to the corresponding conditional branch in
3428 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3430 uint16_t data
= this->stub_template()->insns()[i
].data();
3431 gold_assert((data
& 0xff00U
) == 0xd000U
);
3432 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3436 // Stub_factory methods.
3438 Stub_factory::Stub_factory()
3440 // The instruction template sequences are declared as static
3441 // objects and initialized first time the constructor runs.
3443 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3444 // to reach the stub if necessary.
3445 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3447 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3448 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3449 // dcd R_ARM_ABS32(X)
3452 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3454 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3456 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3457 Insn_template::arm_insn(0xe12fff1c), // bx ip
3458 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3459 // dcd R_ARM_ABS32(X)
3462 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3463 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3465 Insn_template::thumb16_insn(0xb401), // push {r0}
3466 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3467 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3468 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3469 Insn_template::thumb16_insn(0x4760), // bx ip
3470 Insn_template::thumb16_insn(0xbf00), // nop
3471 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3472 // dcd R_ARM_ABS32(X)
3475 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3477 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3479 Insn_template::thumb16_insn(0x4778), // bx pc
3480 Insn_template::thumb16_insn(0x46c0), // nop
3481 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3482 Insn_template::arm_insn(0xe12fff1c), // bx ip
3483 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3484 // dcd R_ARM_ABS32(X)
3487 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3489 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3491 Insn_template::thumb16_insn(0x4778), // bx pc
3492 Insn_template::thumb16_insn(0x46c0), // nop
3493 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3494 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3495 // dcd R_ARM_ABS32(X)
3498 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3499 // one, when the destination is close enough.
3500 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3502 Insn_template::thumb16_insn(0x4778), // bx pc
3503 Insn_template::thumb16_insn(0x46c0), // nop
3504 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3507 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3508 // blx to reach the stub if necessary.
3509 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3511 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3512 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3513 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3514 // dcd R_ARM_REL32(X-4)
3517 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3518 // blx to reach the stub if necessary. We can not add into pc;
3519 // it is not guaranteed to mode switch (different in ARMv6 and
3521 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3523 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3524 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3525 Insn_template::arm_insn(0xe12fff1c), // bx ip
3526 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3527 // dcd R_ARM_REL32(X)
3530 // V4T ARM -> ARM long branch stub, PIC.
3531 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3533 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3534 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3535 Insn_template::arm_insn(0xe12fff1c), // bx ip
3536 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3537 // dcd R_ARM_REL32(X)
3540 // V4T Thumb -> ARM long branch stub, PIC.
3541 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3543 Insn_template::thumb16_insn(0x4778), // bx pc
3544 Insn_template::thumb16_insn(0x46c0), // nop
3545 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3546 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3547 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3548 // dcd R_ARM_REL32(X)
3551 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3553 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3555 Insn_template::thumb16_insn(0xb401), // push {r0}
3556 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3557 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3558 Insn_template::thumb16_insn(0x4484), // add ip, r0
3559 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3560 Insn_template::thumb16_insn(0x4760), // bx ip
3561 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3562 // dcd R_ARM_REL32(X)
3565 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3567 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3569 Insn_template::thumb16_insn(0x4778), // bx pc
3570 Insn_template::thumb16_insn(0x46c0), // nop
3571 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3572 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3573 Insn_template::arm_insn(0xe12fff1c), // bx ip
3574 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3575 // dcd R_ARM_REL32(X)
3578 // Cortex-A8 erratum-workaround stubs.
3580 // Stub used for conditional branches (which may be beyond +/-1MB away,
3581 // so we can't use a conditional branch to reach this stub).
3588 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3590 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3591 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3592 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3596 // Stub used for b.w and bl.w instructions.
3598 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3600 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3603 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3605 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3608 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3609 // instruction (which switches to ARM mode) to point to this stub. Jump to
3610 // the real destination using an ARM-mode branch.
3611 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3613 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3616 // Fill in the stub template look-up table. Stub templates are constructed
3617 // per instance of Stub_factory for fast look-up without locking
3618 // in a thread-enabled environment.
3620 this->stub_templates_
[arm_stub_none
] =
3621 new Stub_template(arm_stub_none
, NULL
, 0);
3623 #define DEF_STUB(x) \
3627 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3628 Stub_type type = arm_stub_##x; \
3629 this->stub_templates_[type] = \
3630 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3638 // Stub_table methods.
3640 // Removel all Cortex-A8 stub.
3642 template<bool big_endian
>
3644 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
3646 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3647 p
!= this->cortex_a8_stubs_
.end();
3650 this->cortex_a8_stubs_
.clear();
3653 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3655 template<bool big_endian
>
3657 Stub_table
<big_endian
>::relocate_stub(
3659 const Relocate_info
<32, big_endian
>* relinfo
,
3660 Target_arm
<big_endian
>* arm_target
,
3661 Output_section
* output_section
,
3662 unsigned char* view
,
3663 Arm_address address
,
3664 section_size_type view_size
)
3666 const Stub_template
* stub_template
= stub
->stub_template();
3667 if (stub_template
->reloc_count() != 0)
3669 // Adjust view to cover the stub only.
3670 section_size_type offset
= stub
->offset();
3671 section_size_type stub_size
= stub_template
->size();
3672 gold_assert(offset
+ stub_size
<= view_size
);
3674 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
3675 address
+ offset
, stub_size
);
3679 // Relocate all stubs in this stub table.
3681 template<bool big_endian
>
3683 Stub_table
<big_endian
>::relocate_stubs(
3684 const Relocate_info
<32, big_endian
>* relinfo
,
3685 Target_arm
<big_endian
>* arm_target
,
3686 Output_section
* output_section
,
3687 unsigned char* view
,
3688 Arm_address address
,
3689 section_size_type view_size
)
3691 // If we are passed a view bigger than the stub table's. we need to
3693 gold_assert(address
== this->address()
3695 == static_cast<section_size_type
>(this->data_size())));
3697 // Relocate all relocation stubs.
3698 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3699 p
!= this->reloc_stubs_
.end();
3701 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3702 address
, view_size
);
3704 // Relocate all Cortex-A8 stubs.
3705 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3706 p
!= this->cortex_a8_stubs_
.end();
3708 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3709 address
, view_size
);
3712 // Write out the stubs to file.
3714 template<bool big_endian
>
3716 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3718 off_t offset
= this->offset();
3719 const section_size_type oview_size
=
3720 convert_to_section_size_type(this->data_size());
3721 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3723 // Write relocation stubs.
3724 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3725 p
!= this->reloc_stubs_
.end();
3728 Reloc_stub
* stub
= p
->second
;
3729 Arm_address address
= this->address() + stub
->offset();
3731 == align_address(address
,
3732 stub
->stub_template()->alignment()));
3733 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3737 // Write Cortex-A8 stubs.
3738 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3739 p
!= this->cortex_a8_stubs_
.end();
3742 Cortex_a8_stub
* stub
= p
->second
;
3743 Arm_address address
= this->address() + stub
->offset();
3745 == align_address(address
,
3746 stub
->stub_template()->alignment()));
3747 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3751 of
->write_output_view(this->offset(), oview_size
, oview
);
3754 // Update the data size and address alignment of the stub table at the end
3755 // of a relaxation pass. Return true if either the data size or the
3756 // alignment changed in this relaxation pass.
3758 template<bool big_endian
>
3760 Stub_table
<big_endian
>::update_data_size_and_addralign()
3763 unsigned addralign
= 1;
3765 // Go over all stubs in table to compute data size and address alignment.
3767 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3768 p
!= this->reloc_stubs_
.end();
3771 const Stub_template
* stub_template
= p
->second
->stub_template();
3772 addralign
= std::max(addralign
, stub_template
->alignment());
3773 size
= (align_address(size
, stub_template
->alignment())
3774 + stub_template
->size());
3777 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3778 p
!= this->cortex_a8_stubs_
.end();
3781 const Stub_template
* stub_template
= p
->second
->stub_template();
3782 addralign
= std::max(addralign
, stub_template
->alignment());
3783 size
= (align_address(size
, stub_template
->alignment())
3784 + stub_template
->size());
3787 // Check if either data size or alignment changed in this pass.
3788 // Update prev_data_size_ and prev_addralign_. These will be used
3789 // as the current data size and address alignment for the next pass.
3790 bool changed
= size
!= this->prev_data_size_
;
3791 this->prev_data_size_
= size
;
3793 if (addralign
!= this->prev_addralign_
)
3795 this->prev_addralign_
= addralign
;
3800 // Finalize the stubs. This sets the offsets of the stubs within the stub
3801 // table. It also marks all input sections needing Cortex-A8 workaround.
3803 template<bool big_endian
>
3805 Stub_table
<big_endian
>::finalize_stubs()
3808 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3809 p
!= this->reloc_stubs_
.end();
3812 Reloc_stub
* stub
= p
->second
;
3813 const Stub_template
* stub_template
= stub
->stub_template();
3814 uint64_t stub_addralign
= stub_template
->alignment();
3815 off
= align_address(off
, stub_addralign
);
3816 stub
->set_offset(off
);
3817 off
+= stub_template
->size();
3820 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3821 p
!= this->cortex_a8_stubs_
.end();
3824 Cortex_a8_stub
* stub
= p
->second
;
3825 const Stub_template
* stub_template
= stub
->stub_template();
3826 uint64_t stub_addralign
= stub_template
->alignment();
3827 off
= align_address(off
, stub_addralign
);
3828 stub
->set_offset(off
);
3829 off
+= stub_template
->size();
3831 // Mark input section so that we can determine later if a code section
3832 // needs the Cortex-A8 workaround quickly.
3833 Arm_relobj
<big_endian
>* arm_relobj
=
3834 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
3835 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
3838 gold_assert(off
<= this->prev_data_size_
);
3841 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3842 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3843 // of the address range seen by the linker.
3845 template<bool big_endian
>
3847 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
3848 Target_arm
<big_endian
>* arm_target
,
3849 unsigned char* view
,
3850 Arm_address view_address
,
3851 section_size_type view_size
)
3853 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3854 for (Cortex_a8_stub_list::const_iterator p
=
3855 this->cortex_a8_stubs_
.lower_bound(view_address
);
3856 ((p
!= this->cortex_a8_stubs_
.end())
3857 && (p
->first
< (view_address
+ view_size
)));
3860 // We do not store the THUMB bit in the LSB of either the branch address
3861 // or the stub offset. There is no need to strip the LSB.
3862 Arm_address branch_address
= p
->first
;
3863 const Cortex_a8_stub
* stub
= p
->second
;
3864 Arm_address stub_address
= this->address() + stub
->offset();
3866 // Offset of the branch instruction relative to this view.
3867 section_size_type offset
=
3868 convert_to_section_size_type(branch_address
- view_address
);
3869 gold_assert((offset
+ 4) <= view_size
);
3871 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
3872 view
+ offset
, branch_address
);
3876 // Arm_input_section methods.
3878 // Initialize an Arm_input_section.
3880 template<bool big_endian
>
3882 Arm_input_section
<big_endian
>::init()
3884 Relobj
* relobj
= this->relobj();
3885 unsigned int shndx
= this->shndx();
3887 // Cache these to speed up size and alignment queries. It is too slow
3888 // to call section_addraglin and section_size every time.
3889 this->original_addralign_
= relobj
->section_addralign(shndx
);
3890 this->original_size_
= relobj
->section_size(shndx
);
3892 // We want to make this look like the original input section after
3893 // output sections are finalized.
3894 Output_section
* os
= relobj
->output_section(shndx
);
3895 off_t offset
= relobj
->output_section_offset(shndx
);
3896 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3897 this->set_address(os
->address() + offset
);
3898 this->set_file_offset(os
->offset() + offset
);
3900 this->set_current_data_size(this->original_size_
);
3901 this->finalize_data_size();
3904 template<bool big_endian
>
3906 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3908 // We have to write out the original section content.
3909 section_size_type section_size
;
3910 const unsigned char* section_contents
=
3911 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3912 of
->write(this->offset(), section_contents
, section_size
);
3914 // If this owns a stub table and it is not empty, write it.
3915 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3916 this->stub_table_
->write(of
);
3919 // Finalize data size.
3921 template<bool big_endian
>
3923 Arm_input_section
<big_endian
>::set_final_data_size()
3925 // If this owns a stub table, finalize its data size as well.
3926 if (this->is_stub_table_owner())
3928 uint64_t address
= this->address();
3930 // The stub table comes after the original section contents.
3931 address
+= this->original_size_
;
3932 address
= align_address(address
, this->stub_table_
->addralign());
3933 off_t offset
= this->offset() + (address
- this->address());
3934 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3935 address
+= this->stub_table_
->data_size();
3936 gold_assert(address
== this->address() + this->current_data_size());
3939 this->set_data_size(this->current_data_size());
3942 // Reset address and file offset.
3944 template<bool big_endian
>
3946 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3948 // Size of the original input section contents.
3949 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3951 // If this is a stub table owner, account for the stub table size.
3952 if (this->is_stub_table_owner())
3954 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3956 // Reset the stub table's address and file offset. The
3957 // current data size for child will be updated after that.
3958 stub_table_
->reset_address_and_file_offset();
3959 off
= align_address(off
, stub_table_
->addralign());
3960 off
+= stub_table
->current_data_size();
3963 this->set_current_data_size(off
);
3966 // Arm_output_section methods.
3968 // Create a stub group for input sections from BEGIN to END. OWNER
3969 // points to the input section to be the owner a new stub table.
3971 template<bool big_endian
>
3973 Arm_output_section
<big_endian
>::create_stub_group(
3974 Input_section_list::const_iterator begin
,
3975 Input_section_list::const_iterator end
,
3976 Input_section_list::const_iterator owner
,
3977 Target_arm
<big_endian
>* target
,
3978 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3980 // Currently we convert ordinary input sections into relaxed sections only
3981 // at this point but we may want to support creating relaxed input section
3982 // very early. So we check here to see if owner is already a relaxed
3985 Arm_input_section
<big_endian
>* arm_input_section
;
3986 if (owner
->is_relaxed_input_section())
3989 Arm_input_section
<big_endian
>::as_arm_input_section(
3990 owner
->relaxed_input_section());
3994 gold_assert(owner
->is_input_section());
3995 // Create a new relaxed input section.
3997 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3998 new_relaxed_sections
->push_back(arm_input_section
);
4001 // Create a stub table.
4002 Stub_table
<big_endian
>* stub_table
=
4003 target
->new_stub_table(arm_input_section
);
4005 arm_input_section
->set_stub_table(stub_table
);
4007 Input_section_list::const_iterator p
= begin
;
4008 Input_section_list::const_iterator prev_p
;
4010 // Look for input sections or relaxed input sections in [begin ... end].
4013 if (p
->is_input_section() || p
->is_relaxed_input_section())
4015 // The stub table information for input sections live
4016 // in their objects.
4017 Arm_relobj
<big_endian
>* arm_relobj
=
4018 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
4019 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
4023 while (prev_p
!= end
);
4026 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4027 // of stub groups. We grow a stub group by adding input section until the
4028 // size is just below GROUP_SIZE. The last input section will be converted
4029 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4030 // input section after the stub table, effectively double the group size.
4032 // This is similar to the group_sections() function in elf32-arm.c but is
4033 // implemented differently.
4035 template<bool big_endian
>
4037 Arm_output_section
<big_endian
>::group_sections(
4038 section_size_type group_size
,
4039 bool stubs_always_after_branch
,
4040 Target_arm
<big_endian
>* target
)
4042 // We only care about sections containing code.
4043 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
4046 // States for grouping.
4049 // No group is being built.
4051 // A group is being built but the stub table is not found yet.
4052 // We keep group a stub group until the size is just under GROUP_SIZE.
4053 // The last input section in the group will be used as the stub table.
4054 FINDING_STUB_SECTION
,
4055 // A group is being built and we have already found a stub table.
4056 // We enter this state to grow a stub group by adding input section
4057 // after the stub table. This effectively doubles the group size.
4061 // Any newly created relaxed sections are stored here.
4062 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
4064 State state
= NO_GROUP
;
4065 section_size_type off
= 0;
4066 section_size_type group_begin_offset
= 0;
4067 section_size_type group_end_offset
= 0;
4068 section_size_type stub_table_end_offset
= 0;
4069 Input_section_list::const_iterator group_begin
=
4070 this->input_sections().end();
4071 Input_section_list::const_iterator stub_table
=
4072 this->input_sections().end();
4073 Input_section_list::const_iterator group_end
= this->input_sections().end();
4074 for (Input_section_list::const_iterator p
= this->input_sections().begin();
4075 p
!= this->input_sections().end();
4078 section_size_type section_begin_offset
=
4079 align_address(off
, p
->addralign());
4080 section_size_type section_end_offset
=
4081 section_begin_offset
+ p
->data_size();
4083 // Check to see if we should group the previously seens sections.
4089 case FINDING_STUB_SECTION
:
4090 // Adding this section makes the group larger than GROUP_SIZE.
4091 if (section_end_offset
- group_begin_offset
>= group_size
)
4093 if (stubs_always_after_branch
)
4095 gold_assert(group_end
!= this->input_sections().end());
4096 this->create_stub_group(group_begin
, group_end
, group_end
,
4097 target
, &new_relaxed_sections
);
4102 // But wait, there's more! Input sections up to
4103 // stub_group_size bytes after the stub table can be
4104 // handled by it too.
4105 state
= HAS_STUB_SECTION
;
4106 stub_table
= group_end
;
4107 stub_table_end_offset
= group_end_offset
;
4112 case HAS_STUB_SECTION
:
4113 // Adding this section makes the post stub-section group larger
4115 if (section_end_offset
- stub_table_end_offset
>= group_size
)
4117 gold_assert(group_end
!= this->input_sections().end());
4118 this->create_stub_group(group_begin
, group_end
, stub_table
,
4119 target
, &new_relaxed_sections
);
4128 // If we see an input section and currently there is no group, start
4129 // a new one. Skip any empty sections.
4130 if ((p
->is_input_section() || p
->is_relaxed_input_section())
4131 && (p
->relobj()->section_size(p
->shndx()) != 0))
4133 if (state
== NO_GROUP
)
4135 state
= FINDING_STUB_SECTION
;
4137 group_begin_offset
= section_begin_offset
;
4140 // Keep track of the last input section seen.
4142 group_end_offset
= section_end_offset
;
4145 off
= section_end_offset
;
4148 // Create a stub group for any ungrouped sections.
4149 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
4151 gold_assert(group_end
!= this->input_sections().end());
4152 this->create_stub_group(group_begin
, group_end
,
4153 (state
== FINDING_STUB_SECTION
4156 target
, &new_relaxed_sections
);
4159 // Convert input section into relaxed input section in a batch.
4160 if (!new_relaxed_sections
.empty())
4161 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
4163 // Update the section offsets
4164 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
4166 Arm_relobj
<big_endian
>* arm_relobj
=
4167 Arm_relobj
<big_endian
>::as_arm_relobj(
4168 new_relaxed_sections
[i
]->relobj());
4169 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
4170 // Tell Arm_relobj that this input section is converted.
4171 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
4175 // Arm_relobj methods.
4177 // Determine if we want to scan the SHNDX-th section for relocation stubs.
4178 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4180 template<bool big_endian
>
4182 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
4183 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4184 const Relobj::Output_sections
& out_sections
,
4185 const Symbol_table
*symtab
)
4187 unsigned int sh_type
= shdr
.get_sh_type();
4188 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
4191 // Ignore empty section.
4192 off_t sh_size
= shdr
.get_sh_size();
4196 // Ignore reloc section with bad info. This error will be
4197 // reported in the final link.
4198 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4199 if (index
>= this->shnum())
4202 // This relocation section is against a section which we
4203 // discarded or if the section is folded into another
4204 // section due to ICF.
4205 if (out_sections
[index
] == NULL
|| symtab
->is_section_folded(this, index
))
4208 // Ignore reloc section with unexpected symbol table. The
4209 // error will be reported in the final link.
4210 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4213 unsigned int reloc_size
;
4214 if (sh_type
== elfcpp::SHT_REL
)
4215 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4217 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4219 // Ignore reloc section with unexpected entsize or uneven size.
4220 // The error will be reported in the final link.
4221 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
4227 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4228 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4230 template<bool big_endian
>
4232 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
4233 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4236 const Symbol_table
* symtab
)
4238 // We only scan non-empty code sections.
4239 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) == 0
4240 || shdr
.get_sh_size() == 0)
4243 // Ignore discarded or ICF'ed sections.
4244 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
4247 // Find output address of section.
4248 Arm_address address
= os
->output_address(this, shndx
, 0);
4250 // If the section does not cross any 4K-boundaries, it does not need to
4252 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
4258 // Scan a section for Cortex-A8 workaround.
4260 template<bool big_endian
>
4262 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
4263 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4266 Target_arm
<big_endian
>* arm_target
)
4268 Arm_address output_address
= os
->output_address(this, shndx
, 0);
4270 // Get the section contents.
4271 section_size_type input_view_size
= 0;
4272 const unsigned char* input_view
=
4273 this->section_contents(shndx
, &input_view_size
, false);
4275 // We need to go through the mapping symbols to determine what to
4276 // scan. There are two reasons. First, we should look at THUMB code and
4277 // THUMB code only. Second, we only want to look at the 4K-page boundary
4278 // to speed up the scanning.
4280 // Look for the first mapping symbol in this section. It should be
4282 Mapping_symbol_position
section_start(shndx
, 0);
4283 typename
Mapping_symbols_info::const_iterator p
=
4284 this->mapping_symbols_info_
.lower_bound(section_start
);
4286 if (p
== this->mapping_symbols_info_
.end()
4287 || p
->first
!= section_start
)
4289 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4290 "is no mapping symbols for section %u of %s"),
4291 shndx
, this->name().c_str());
4295 while (p
!= this->mapping_symbols_info_
.end()
4296 && p
->first
.first
== shndx
)
4298 typename
Mapping_symbols_info::const_iterator next
=
4299 this->mapping_symbols_info_
.upper_bound(p
->first
);
4301 // Only scan part of a section with THUMB code.
4302 if (p
->second
== 't')
4304 // Determine the end of this range.
4305 section_size_type span_start
=
4306 convert_to_section_size_type(p
->first
.second
);
4307 section_size_type span_end
;
4308 if (next
!= this->mapping_symbols_info_
.end()
4309 && next
->first
.first
== shndx
)
4310 span_end
= convert_to_section_size_type(next
->first
.second
);
4312 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
4314 if (((span_start
+ output_address
) & ~0xfffUL
)
4315 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
4317 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
4318 span_start
, span_end
,
4328 // Scan relocations for stub generation.
4330 template<bool big_endian
>
4332 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
4333 Target_arm
<big_endian
>* arm_target
,
4334 const Symbol_table
* symtab
,
4335 const Layout
* layout
)
4337 unsigned int shnum
= this->shnum();
4338 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4340 // Read the section headers.
4341 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4345 // To speed up processing, we set up hash tables for fast lookup of
4346 // input offsets to output addresses.
4347 this->initialize_input_to_output_maps();
4349 const Relobj::Output_sections
& out_sections(this->output_sections());
4351 Relocate_info
<32, big_endian
> relinfo
;
4352 relinfo
.symtab
= symtab
;
4353 relinfo
.layout
= layout
;
4354 relinfo
.object
= this;
4356 // Do relocation stubs scanning.
4357 const unsigned char* p
= pshdrs
+ shdr_size
;
4358 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4360 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
4361 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
))
4363 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4364 Arm_address output_offset
= this->get_output_section_offset(index
);
4365 Arm_address output_address
;
4366 if(output_offset
!= invalid_address
)
4367 output_address
= out_sections
[index
]->address() + output_offset
;
4370 // Currently this only happens for a relaxed section.
4371 const Output_relaxed_input_section
* poris
=
4372 out_sections
[index
]->find_relaxed_input_section(this, index
);
4373 gold_assert(poris
!= NULL
);
4374 output_address
= poris
->address();
4377 // Get the relocations.
4378 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
4382 // Get the section contents. This does work for the case in which
4383 // we modify the contents of an input section. We need to pass the
4384 // output view under such circumstances.
4385 section_size_type input_view_size
= 0;
4386 const unsigned char* input_view
=
4387 this->section_contents(index
, &input_view_size
, false);
4389 relinfo
.reloc_shndx
= i
;
4390 relinfo
.data_shndx
= index
;
4391 unsigned int sh_type
= shdr
.get_sh_type();
4392 unsigned int reloc_size
;
4393 if (sh_type
== elfcpp::SHT_REL
)
4394 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4396 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4398 Output_section
* os
= out_sections
[index
];
4399 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
4400 shdr
.get_sh_size() / reloc_size
,
4402 output_offset
== invalid_address
,
4403 input_view
, output_address
,
4408 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
4409 // after its relocation section, if there is one, is processed for
4410 // relocation stubs. Merging this loop with the one above would have been
4411 // complicated since we would have had to make sure that relocation stub
4412 // scanning is done first.
4413 if (arm_target
->fix_cortex_a8())
4415 const unsigned char* p
= pshdrs
+ shdr_size
;
4416 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4418 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
4419 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
4422 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
4427 // After we've done the relocations, we release the hash tables,
4428 // since we no longer need them.
4429 this->free_input_to_output_maps();
4432 // Count the local symbols. The ARM backend needs to know if a symbol
4433 // is a THUMB function or not. For global symbols, it is easy because
4434 // the Symbol object keeps the ELF symbol type. For local symbol it is
4435 // harder because we cannot access this information. So we override the
4436 // do_count_local_symbol in parent and scan local symbols to mark
4437 // THUMB functions. This is not the most efficient way but I do not want to
4438 // slow down other ports by calling a per symbol targer hook inside
4439 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4441 template<bool big_endian
>
4443 Arm_relobj
<big_endian
>::do_count_local_symbols(
4444 Stringpool_template
<char>* pool
,
4445 Stringpool_template
<char>* dynpool
)
4447 // We need to fix-up the values of any local symbols whose type are
4450 // Ask parent to count the local symbols.
4451 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
4452 const unsigned int loccount
= this->local_symbol_count();
4456 // Intialize the thumb function bit-vector.
4457 std::vector
<bool> empty_vector(loccount
, false);
4458 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
4460 // Read the symbol table section header.
4461 const unsigned int symtab_shndx
= this->symtab_shndx();
4462 elfcpp::Shdr
<32, big_endian
>
4463 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
4464 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
4466 // Read the local symbols.
4467 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
4468 gold_assert(loccount
== symtabshdr
.get_sh_info());
4469 off_t locsize
= loccount
* sym_size
;
4470 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
4471 locsize
, true, true);
4473 // For mapping symbol processing, we need to read the symbol names.
4474 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
4475 if (strtab_shndx
>= this->shnum())
4477 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
4481 elfcpp::Shdr
<32, big_endian
>
4482 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
4483 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
4485 this->error(_("symbol table name section has wrong type: %u"),
4486 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
4489 const char* pnames
=
4490 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
4491 strtabshdr
.get_sh_size(),
4494 // Loop over the local symbols and mark any local symbols pointing
4495 // to THUMB functions.
4497 // Skip the first dummy symbol.
4499 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
4500 this->local_values();
4501 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
4503 elfcpp::Sym
<32, big_endian
> sym(psyms
);
4504 elfcpp::STT st_type
= sym
.get_st_type();
4505 Symbol_value
<32>& lv((*plocal_values
)[i
]);
4506 Arm_address input_value
= lv
.input_value();
4508 // Check to see if this is a mapping symbol.
4509 const char* sym_name
= pnames
+ sym
.get_st_name();
4510 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
4512 unsigned int input_shndx
= sym
.get_st_shndx();
4514 // Strip of LSB in case this is a THUMB symbol.
4515 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
4516 this->mapping_symbols_info_
[msp
] = sym_name
[1];
4519 if (st_type
== elfcpp::STT_ARM_TFUNC
4520 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
4522 // This is a THUMB function. Mark this and canonicalize the
4523 // symbol value by setting LSB.
4524 this->local_symbol_is_thumb_function_
[i
] = true;
4525 if ((input_value
& 1) == 0)
4526 lv
.set_input_value(input_value
| 1);
4531 // Relocate sections.
4532 template<bool big_endian
>
4534 Arm_relobj
<big_endian
>::do_relocate_sections(
4535 const Symbol_table
* symtab
,
4536 const Layout
* layout
,
4537 const unsigned char* pshdrs
,
4538 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
4540 // Call parent to relocate sections.
4541 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
4544 // We do not generate stubs if doing a relocatable link.
4545 if (parameters
->options().relocatable())
4548 // Relocate stub tables.
4549 unsigned int shnum
= this->shnum();
4551 Target_arm
<big_endian
>* arm_target
=
4552 Target_arm
<big_endian
>::default_target();
4554 Relocate_info
<32, big_endian
> relinfo
;
4555 relinfo
.symtab
= symtab
;
4556 relinfo
.layout
= layout
;
4557 relinfo
.object
= this;
4559 for (unsigned int i
= 1; i
< shnum
; ++i
)
4561 Arm_input_section
<big_endian
>* arm_input_section
=
4562 arm_target
->find_arm_input_section(this, i
);
4564 if (arm_input_section
!= NULL
4565 && arm_input_section
->is_stub_table_owner()
4566 && !arm_input_section
->stub_table()->empty())
4568 // We cannot discard a section if it owns a stub table.
4569 Output_section
* os
= this->output_section(i
);
4570 gold_assert(os
!= NULL
);
4572 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
4573 relinfo
.reloc_shdr
= NULL
;
4574 relinfo
.data_shndx
= i
;
4575 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
4577 gold_assert((*pviews
)[i
].view
!= NULL
);
4579 // We are passed the output section view. Adjust it to cover the
4581 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
4582 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
4583 && ((stub_table
->address() + stub_table
->data_size())
4584 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
4586 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
4587 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
4588 Arm_address address
= stub_table
->address();
4589 section_size_type view_size
= stub_table
->data_size();
4591 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
4595 // Apply Cortex A8 workaround if applicable.
4596 if (this->section_has_cortex_a8_workaround(i
))
4598 unsigned char* view
= (*pviews
)[i
].view
;
4599 Arm_address view_address
= (*pviews
)[i
].address
;
4600 section_size_type view_size
= (*pviews
)[i
].view_size
;
4601 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
4603 // Adjust view to cover section.
4604 Output_section
* os
= this->output_section(i
);
4605 gold_assert(os
!= NULL
);
4606 Arm_address section_address
= os
->output_address(this, i
, 0);
4607 uint64_t section_size
= this->section_size(i
);
4609 gold_assert(section_address
>= view_address
4610 && ((section_address
+ section_size
)
4611 <= (view_address
+ view_size
)));
4613 unsigned char* section_view
= view
+ (section_address
- view_address
);
4615 // Apply the Cortex-A8 workaround to the output address range
4616 // corresponding to this input section.
4617 stub_table
->apply_cortex_a8_workaround_to_address_range(
4626 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4629 template<bool big_endian
>
4630 Attributes_section_data
*
4631 read_arm_attributes_section(
4633 Read_symbols_data
*sd
)
4635 // Read the attributes section if there is one.
4636 // We read from the end because gas seems to put it near the end of
4637 // the section headers.
4638 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4639 const unsigned char *ps
=
4640 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
4641 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
4643 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4644 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
4646 section_offset_type section_offset
= shdr
.get_sh_offset();
4647 section_size_type section_size
=
4648 convert_to_section_size_type(shdr
.get_sh_size());
4649 File_view
* view
= object
->get_lasting_view(section_offset
,
4650 section_size
, true, false);
4651 return new Attributes_section_data(view
->data(), section_size
);
4657 // Read the symbol information.
4659 template<bool big_endian
>
4661 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4663 // Call parent class to read symbol information.
4664 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
4666 // Read processor-specific flags in ELF file header.
4667 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4668 elfcpp::Elf_sizes
<32>::ehdr_size
,
4670 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4671 this->processor_specific_flags_
= ehdr
.get_e_flags();
4672 this->attributes_section_data_
=
4673 read_arm_attributes_section
<big_endian
>(this, sd
);
4676 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4677 // sections for unwinding. These sections are referenced implicitly by
4678 // text sections linked in the section headers. If we ignore these implict
4679 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4680 // will be garbage-collected incorrectly. Hence we override the same function
4681 // in the base class to handle these implicit references.
4683 template<bool big_endian
>
4685 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
4687 Read_relocs_data
* rd
)
4689 // First, call base class method to process relocations in this object.
4690 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
4692 unsigned int shnum
= this->shnum();
4693 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4694 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4698 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4699 // to these from the linked text sections.
4700 const unsigned char* ps
= pshdrs
+ shdr_size
;
4701 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
4703 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4704 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
4706 // Found an .ARM.exidx section, add it to the set of reachable
4707 // sections from its linked text section.
4708 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
4709 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
4714 // Arm_dynobj methods.
4716 // Read the symbol information.
4718 template<bool big_endian
>
4720 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4722 // Call parent class to read symbol information.
4723 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
4725 // Read processor-specific flags in ELF file header.
4726 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4727 elfcpp::Elf_sizes
<32>::ehdr_size
,
4729 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4730 this->processor_specific_flags_
= ehdr
.get_e_flags();
4731 this->attributes_section_data_
=
4732 read_arm_attributes_section
<big_endian
>(this, sd
);
4735 // Stub_addend_reader methods.
4737 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4739 template<bool big_endian
>
4740 elfcpp::Elf_types
<32>::Elf_Swxword
4741 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
4742 unsigned int r_type
,
4743 const unsigned char* view
,
4744 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
4746 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
4750 case elfcpp::R_ARM_CALL
:
4751 case elfcpp::R_ARM_JUMP24
:
4752 case elfcpp::R_ARM_PLT32
:
4754 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4755 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4756 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4757 return utils::sign_extend
<26>(val
<< 2);
4760 case elfcpp::R_ARM_THM_CALL
:
4761 case elfcpp::R_ARM_THM_JUMP24
:
4762 case elfcpp::R_ARM_THM_XPC22
:
4764 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4765 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4766 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4767 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4768 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
4771 case elfcpp::R_ARM_THM_JUMP19
:
4773 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4774 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4775 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4776 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4777 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
4785 // A class to handle the PLT data.
4787 template<bool big_endian
>
4788 class Output_data_plt_arm
: public Output_section_data
4791 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
4794 Output_data_plt_arm(Layout
*, Output_data_space
*);
4796 // Add an entry to the PLT.
4798 add_entry(Symbol
* gsym
);
4800 // Return the .rel.plt section data.
4801 const Reloc_section
*
4803 { return this->rel_
; }
4807 do_adjust_output_section(Output_section
* os
);
4809 // Write to a map file.
4811 do_print_to_mapfile(Mapfile
* mapfile
) const
4812 { mapfile
->print_output_data(this, _("** PLT")); }
4815 // Template for the first PLT entry.
4816 static const uint32_t first_plt_entry
[5];
4818 // Template for subsequent PLT entries.
4819 static const uint32_t plt_entry
[3];
4821 // Set the final size.
4823 set_final_data_size()
4825 this->set_data_size(sizeof(first_plt_entry
)
4826 + this->count_
* sizeof(plt_entry
));
4829 // Write out the PLT data.
4831 do_write(Output_file
*);
4833 // The reloc section.
4834 Reloc_section
* rel_
;
4835 // The .got.plt section.
4836 Output_data_space
* got_plt_
;
4837 // The number of PLT entries.
4838 unsigned int count_
;
4841 // Create the PLT section. The ordinary .got section is an argument,
4842 // since we need to refer to the start. We also create our own .got
4843 // section just for PLT entries.
4845 template<bool big_endian
>
4846 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
4847 Output_data_space
* got_plt
)
4848 : Output_section_data(4), got_plt_(got_plt
), count_(0)
4850 this->rel_
= new Reloc_section(false);
4851 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
4852 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
4856 template<bool big_endian
>
4858 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
4863 // Add an entry to the PLT.
4865 template<bool big_endian
>
4867 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
4869 gold_assert(!gsym
->has_plt_offset());
4871 // Note that when setting the PLT offset we skip the initial
4872 // reserved PLT entry.
4873 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
4874 + sizeof(first_plt_entry
));
4878 section_offset_type got_offset
= this->got_plt_
->current_data_size();
4880 // Every PLT entry needs a GOT entry which points back to the PLT
4881 // entry (this will be changed by the dynamic linker, normally
4882 // lazily when the function is called).
4883 this->got_plt_
->set_current_data_size(got_offset
+ 4);
4885 // Every PLT entry needs a reloc.
4886 gsym
->set_needs_dynsym_entry();
4887 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
4890 // Note that we don't need to save the symbol. The contents of the
4891 // PLT are independent of which symbols are used. The symbols only
4892 // appear in the relocations.
4896 // FIXME: This is not very flexible. Right now this has only been tested
4897 // on armv5te. If we are to support additional architecture features like
4898 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4900 // The first entry in the PLT.
4901 template<bool big_endian
>
4902 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
4904 0xe52de004, // str lr, [sp, #-4]!
4905 0xe59fe004, // ldr lr, [pc, #4]
4906 0xe08fe00e, // add lr, pc, lr
4907 0xe5bef008, // ldr pc, [lr, #8]!
4908 0x00000000, // &GOT[0] - .
4911 // Subsequent entries in the PLT.
4913 template<bool big_endian
>
4914 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
4916 0xe28fc600, // add ip, pc, #0xNN00000
4917 0xe28cca00, // add ip, ip, #0xNN000
4918 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4921 // Write out the PLT. This uses the hand-coded instructions above,
4922 // and adjusts them as needed. This is all specified by the arm ELF
4923 // Processor Supplement.
4925 template<bool big_endian
>
4927 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4929 const off_t offset
= this->offset();
4930 const section_size_type oview_size
=
4931 convert_to_section_size_type(this->data_size());
4932 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4934 const off_t got_file_offset
= this->got_plt_
->offset();
4935 const section_size_type got_size
=
4936 convert_to_section_size_type(this->got_plt_
->data_size());
4937 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4939 unsigned char* pov
= oview
;
4941 Arm_address plt_address
= this->address();
4942 Arm_address got_address
= this->got_plt_
->address();
4944 // Write first PLT entry. All but the last word are constants.
4945 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4946 / sizeof(plt_entry
[0]));
4947 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4948 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4949 // Last word in first PLT entry is &GOT[0] - .
4950 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4951 got_address
- (plt_address
+ 16));
4952 pov
+= sizeof(first_plt_entry
);
4954 unsigned char* got_pov
= got_view
;
4956 memset(got_pov
, 0, 12);
4959 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4960 unsigned int plt_offset
= sizeof(first_plt_entry
);
4961 unsigned int plt_rel_offset
= 0;
4962 unsigned int got_offset
= 12;
4963 const unsigned int count
= this->count_
;
4964 for (unsigned int i
= 0;
4967 pov
+= sizeof(plt_entry
),
4969 plt_offset
+= sizeof(plt_entry
),
4970 plt_rel_offset
+= rel_size
,
4973 // Set and adjust the PLT entry itself.
4974 int32_t offset
= ((got_address
+ got_offset
)
4975 - (plt_address
+ plt_offset
+ 8));
4977 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4978 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4979 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4980 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4981 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4982 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4983 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4985 // Set the entry in the GOT.
4986 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4989 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4990 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4992 of
->write_output_view(offset
, oview_size
, oview
);
4993 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4996 // Create a PLT entry for a global symbol.
4998 template<bool big_endian
>
5000 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
5003 if (gsym
->has_plt_offset())
5006 if (this->plt_
== NULL
)
5008 // Create the GOT sections first.
5009 this->got_section(symtab
, layout
);
5011 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
5012 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
5014 | elfcpp::SHF_EXECINSTR
),
5015 this->plt_
, false, false, false, false);
5017 this->plt_
->add_entry(gsym
);
5020 // Report an unsupported relocation against a local symbol.
5022 template<bool big_endian
>
5024 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
5025 Sized_relobj
<32, big_endian
>* object
,
5026 unsigned int r_type
)
5028 gold_error(_("%s: unsupported reloc %u against local symbol"),
5029 object
->name().c_str(), r_type
);
5032 // We are about to emit a dynamic relocation of type R_TYPE. If the
5033 // dynamic linker does not support it, issue an error. The GNU linker
5034 // only issues a non-PIC error for an allocated read-only section.
5035 // Here we know the section is allocated, but we don't know that it is
5036 // read-only. But we check for all the relocation types which the
5037 // glibc dynamic linker supports, so it seems appropriate to issue an
5038 // error even if the section is not read-only.
5040 template<bool big_endian
>
5042 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
5043 unsigned int r_type
)
5047 // These are the relocation types supported by glibc for ARM.
5048 case elfcpp::R_ARM_RELATIVE
:
5049 case elfcpp::R_ARM_COPY
:
5050 case elfcpp::R_ARM_GLOB_DAT
:
5051 case elfcpp::R_ARM_JUMP_SLOT
:
5052 case elfcpp::R_ARM_ABS32
:
5053 case elfcpp::R_ARM_ABS32_NOI
:
5054 case elfcpp::R_ARM_PC24
:
5055 // FIXME: The following 3 types are not supported by Android's dynamic
5057 case elfcpp::R_ARM_TLS_DTPMOD32
:
5058 case elfcpp::R_ARM_TLS_DTPOFF32
:
5059 case elfcpp::R_ARM_TLS_TPOFF32
:
5063 // This prevents us from issuing more than one error per reloc
5064 // section. But we can still wind up issuing more than one
5065 // error per object file.
5066 if (this->issued_non_pic_error_
)
5068 object
->error(_("requires unsupported dynamic reloc; "
5069 "recompile with -fPIC"));
5070 this->issued_non_pic_error_
= true;
5073 case elfcpp::R_ARM_NONE
:
5078 // Scan a relocation for a local symbol.
5079 // FIXME: This only handles a subset of relocation types used by Android
5080 // on ARM v5te devices.
5082 template<bool big_endian
>
5084 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
5087 Sized_relobj
<32, big_endian
>* object
,
5088 unsigned int data_shndx
,
5089 Output_section
* output_section
,
5090 const elfcpp::Rel
<32, big_endian
>& reloc
,
5091 unsigned int r_type
,
5092 const elfcpp::Sym
<32, big_endian
>&)
5094 r_type
= get_real_reloc_type(r_type
);
5097 case elfcpp::R_ARM_NONE
:
5100 case elfcpp::R_ARM_ABS32
:
5101 case elfcpp::R_ARM_ABS32_NOI
:
5102 // If building a shared library (or a position-independent
5103 // executable), we need to create a dynamic relocation for
5104 // this location. The relocation applied at link time will
5105 // apply the link-time value, so we flag the location with
5106 // an R_ARM_RELATIVE relocation so the dynamic loader can
5107 // relocate it easily.
5108 if (parameters
->options().output_is_position_independent())
5110 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5111 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5112 // If we are to add more other reloc types than R_ARM_ABS32,
5113 // we need to add check_non_pic(object, r_type) here.
5114 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
5115 output_section
, data_shndx
,
5116 reloc
.get_r_offset());
5120 case elfcpp::R_ARM_REL32
:
5121 case elfcpp::R_ARM_THM_CALL
:
5122 case elfcpp::R_ARM_CALL
:
5123 case elfcpp::R_ARM_PREL31
:
5124 case elfcpp::R_ARM_JUMP24
:
5125 case elfcpp::R_ARM_THM_JUMP24
:
5126 case elfcpp::R_ARM_THM_JUMP19
:
5127 case elfcpp::R_ARM_PLT32
:
5128 case elfcpp::R_ARM_THM_ABS5
:
5129 case elfcpp::R_ARM_ABS8
:
5130 case elfcpp::R_ARM_ABS12
:
5131 case elfcpp::R_ARM_ABS16
:
5132 case elfcpp::R_ARM_BASE_ABS
:
5133 case elfcpp::R_ARM_MOVW_ABS_NC
:
5134 case elfcpp::R_ARM_MOVT_ABS
:
5135 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5136 case elfcpp::R_ARM_THM_MOVT_ABS
:
5137 case elfcpp::R_ARM_MOVW_PREL_NC
:
5138 case elfcpp::R_ARM_MOVT_PREL
:
5139 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5140 case elfcpp::R_ARM_THM_MOVT_PREL
:
5143 case elfcpp::R_ARM_GOTOFF32
:
5144 // We need a GOT section:
5145 target
->got_section(symtab
, layout
);
5148 case elfcpp::R_ARM_BASE_PREL
:
5149 // FIXME: What about this?
5152 case elfcpp::R_ARM_GOT_BREL
:
5153 case elfcpp::R_ARM_GOT_PREL
:
5155 // The symbol requires a GOT entry.
5156 Output_data_got
<32, big_endian
>* got
=
5157 target
->got_section(symtab
, layout
);
5158 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5159 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
5161 // If we are generating a shared object, we need to add a
5162 // dynamic RELATIVE relocation for this symbol's GOT entry.
5163 if (parameters
->options().output_is_position_independent())
5165 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5166 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5167 rel_dyn
->add_local_relative(
5168 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
5169 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5175 case elfcpp::R_ARM_TARGET1
:
5176 // This should have been mapped to another type already.
5178 case elfcpp::R_ARM_COPY
:
5179 case elfcpp::R_ARM_GLOB_DAT
:
5180 case elfcpp::R_ARM_JUMP_SLOT
:
5181 case elfcpp::R_ARM_RELATIVE
:
5182 // These are relocations which should only be seen by the
5183 // dynamic linker, and should never be seen here.
5184 gold_error(_("%s: unexpected reloc %u in object file"),
5185 object
->name().c_str(), r_type
);
5189 unsupported_reloc_local(object
, r_type
);
5194 // Report an unsupported relocation against a global symbol.
5196 template<bool big_endian
>
5198 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
5199 Sized_relobj
<32, big_endian
>* object
,
5200 unsigned int r_type
,
5203 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5204 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5207 // Scan a relocation for a global symbol.
5208 // FIXME: This only handles a subset of relocation types used by Android
5209 // on ARM v5te devices.
5211 template<bool big_endian
>
5213 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
5216 Sized_relobj
<32, big_endian
>* object
,
5217 unsigned int data_shndx
,
5218 Output_section
* output_section
,
5219 const elfcpp::Rel
<32, big_endian
>& reloc
,
5220 unsigned int r_type
,
5223 r_type
= get_real_reloc_type(r_type
);
5226 case elfcpp::R_ARM_NONE
:
5229 case elfcpp::R_ARM_ABS32
:
5230 case elfcpp::R_ARM_ABS32_NOI
:
5232 // Make a dynamic relocation if necessary.
5233 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
5235 if (target
->may_need_copy_reloc(gsym
))
5237 target
->copy_reloc(symtab
, layout
, object
,
5238 data_shndx
, output_section
, gsym
, reloc
);
5240 else if (gsym
->can_use_relative_reloc(false))
5242 // If we are to add more other reloc types than R_ARM_ABS32,
5243 // we need to add check_non_pic(object, r_type) here.
5244 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5245 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
5246 output_section
, object
,
5247 data_shndx
, reloc
.get_r_offset());
5251 // If we are to add more other reloc types than R_ARM_ABS32,
5252 // we need to add check_non_pic(object, r_type) here.
5253 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5254 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5255 data_shndx
, reloc
.get_r_offset());
5261 case elfcpp::R_ARM_MOVW_ABS_NC
:
5262 case elfcpp::R_ARM_MOVT_ABS
:
5263 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5264 case elfcpp::R_ARM_THM_MOVT_ABS
:
5265 case elfcpp::R_ARM_MOVW_PREL_NC
:
5266 case elfcpp::R_ARM_MOVT_PREL
:
5267 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5268 case elfcpp::R_ARM_THM_MOVT_PREL
:
5271 case elfcpp::R_ARM_THM_ABS5
:
5272 case elfcpp::R_ARM_ABS8
:
5273 case elfcpp::R_ARM_ABS12
:
5274 case elfcpp::R_ARM_ABS16
:
5275 case elfcpp::R_ARM_BASE_ABS
:
5277 // No dynamic relocs of this kinds.
5278 // Report the error in case of PIC.
5279 int flags
= Symbol::NON_PIC_REF
;
5280 if (gsym
->type() == elfcpp::STT_FUNC
5281 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5282 flags
|= Symbol::FUNCTION_CALL
;
5283 if (gsym
->needs_dynamic_reloc(flags
))
5284 check_non_pic(object
, r_type
);
5288 case elfcpp::R_ARM_REL32
:
5289 case elfcpp::R_ARM_PREL31
:
5291 // Make a dynamic relocation if necessary.
5292 int flags
= Symbol::NON_PIC_REF
;
5293 if (gsym
->needs_dynamic_reloc(flags
))
5295 if (target
->may_need_copy_reloc(gsym
))
5297 target
->copy_reloc(symtab
, layout
, object
,
5298 data_shndx
, output_section
, gsym
, reloc
);
5302 check_non_pic(object
, r_type
);
5303 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5304 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5305 data_shndx
, reloc
.get_r_offset());
5311 case elfcpp::R_ARM_JUMP24
:
5312 case elfcpp::R_ARM_THM_JUMP24
:
5313 case elfcpp::R_ARM_THM_JUMP19
:
5314 case elfcpp::R_ARM_CALL
:
5315 case elfcpp::R_ARM_THM_CALL
:
5317 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
5318 target
->make_plt_entry(symtab
, layout
, gsym
);
5321 // Check to see if this is a function that would need a PLT
5322 // but does not get one because the function symbol is untyped.
5323 // This happens in assembly code missing a proper .type directive.
5324 if ((!gsym
->is_undefined() || parameters
->options().shared())
5325 && !parameters
->doing_static_link()
5326 && gsym
->type() == elfcpp::STT_NOTYPE
5327 && (gsym
->is_from_dynobj()
5328 || gsym
->is_undefined()
5329 || gsym
->is_preemptible()))
5330 gold_error(_("%s is not a function."),
5331 gsym
->demangled_name().c_str());
5335 case elfcpp::R_ARM_PLT32
:
5336 // If the symbol is fully resolved, this is just a relative
5337 // local reloc. Otherwise we need a PLT entry.
5338 if (gsym
->final_value_is_known())
5340 // If building a shared library, we can also skip the PLT entry
5341 // if the symbol is defined in the output file and is protected
5343 if (gsym
->is_defined()
5344 && !gsym
->is_from_dynobj()
5345 && !gsym
->is_preemptible())
5347 target
->make_plt_entry(symtab
, layout
, gsym
);
5350 case elfcpp::R_ARM_GOTOFF32
:
5351 // We need a GOT section.
5352 target
->got_section(symtab
, layout
);
5355 case elfcpp::R_ARM_BASE_PREL
:
5356 // FIXME: What about this?
5359 case elfcpp::R_ARM_GOT_BREL
:
5360 case elfcpp::R_ARM_GOT_PREL
:
5362 // The symbol requires a GOT entry.
5363 Output_data_got
<32, big_endian
>* got
=
5364 target
->got_section(symtab
, layout
);
5365 if (gsym
->final_value_is_known())
5366 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5369 // If this symbol is not fully resolved, we need to add a
5370 // GOT entry with a dynamic relocation.
5371 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5372 if (gsym
->is_from_dynobj()
5373 || gsym
->is_undefined()
5374 || gsym
->is_preemptible())
5375 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
5376 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
5379 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
5380 rel_dyn
->add_global_relative(
5381 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
5382 gsym
->got_offset(GOT_TYPE_STANDARD
));
5388 case elfcpp::R_ARM_TARGET1
:
5389 // This should have been mapped to another type already.
5391 case elfcpp::R_ARM_COPY
:
5392 case elfcpp::R_ARM_GLOB_DAT
:
5393 case elfcpp::R_ARM_JUMP_SLOT
:
5394 case elfcpp::R_ARM_RELATIVE
:
5395 // These are relocations which should only be seen by the
5396 // dynamic linker, and should never be seen here.
5397 gold_error(_("%s: unexpected reloc %u in object file"),
5398 object
->name().c_str(), r_type
);
5402 unsupported_reloc_global(object
, r_type
, gsym
);
5407 // Process relocations for gc.
5409 template<bool big_endian
>
5411 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
5413 Sized_relobj
<32, big_endian
>* object
,
5414 unsigned int data_shndx
,
5416 const unsigned char* prelocs
,
5418 Output_section
* output_section
,
5419 bool needs_special_offset_handling
,
5420 size_t local_symbol_count
,
5421 const unsigned char* plocal_symbols
)
5423 typedef Target_arm
<big_endian
> Arm
;
5424 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5426 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
5435 needs_special_offset_handling
,
5440 // Scan relocations for a section.
5442 template<bool big_endian
>
5444 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
5446 Sized_relobj
<32, big_endian
>* object
,
5447 unsigned int data_shndx
,
5448 unsigned int sh_type
,
5449 const unsigned char* prelocs
,
5451 Output_section
* output_section
,
5452 bool needs_special_offset_handling
,
5453 size_t local_symbol_count
,
5454 const unsigned char* plocal_symbols
)
5456 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5457 if (sh_type
== elfcpp::SHT_RELA
)
5459 gold_error(_("%s: unsupported RELA reloc section"),
5460 object
->name().c_str());
5464 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
5473 needs_special_offset_handling
,
5478 // Finalize the sections.
5480 template<bool big_endian
>
5482 Target_arm
<big_endian
>::do_finalize_sections(
5484 const Input_objects
* input_objects
,
5485 Symbol_table
* symtab
)
5487 // Merge processor-specific flags.
5488 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
5489 p
!= input_objects
->relobj_end();
5492 Arm_relobj
<big_endian
>* arm_relobj
=
5493 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
5494 this->merge_processor_specific_flags(
5496 arm_relobj
->processor_specific_flags());
5497 this->merge_object_attributes(arm_relobj
->name().c_str(),
5498 arm_relobj
->attributes_section_data());
5502 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
5503 p
!= input_objects
->dynobj_end();
5506 Arm_dynobj
<big_endian
>* arm_dynobj
=
5507 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
5508 this->merge_processor_specific_flags(
5510 arm_dynobj
->processor_specific_flags());
5511 this->merge_object_attributes(arm_dynobj
->name().c_str(),
5512 arm_dynobj
->attributes_section_data());
5516 const Object_attribute
* cpu_arch_attr
=
5517 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
5518 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
5519 this->set_may_use_blx(true);
5521 // Check if we need to use Cortex-A8 workaround.
5522 if (parameters
->options().user_set_fix_cortex_a8())
5523 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
5526 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
5527 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
5529 const Object_attribute
* cpu_arch_profile_attr
=
5530 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
5531 this->fix_cortex_a8_
=
5532 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
5533 && (cpu_arch_profile_attr
->int_value() == 'A'
5534 || cpu_arch_profile_attr
->int_value() == 0));
5537 // Fill in some more dynamic tags.
5538 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5540 : this->plt_
->rel_plt());
5541 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
5542 this->rel_dyn_
, true);
5544 // Emit any relocs we saved in an attempt to avoid generating COPY
5546 if (this->copy_relocs_
.any_saved_relocs())
5547 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
5549 // Handle the .ARM.exidx section.
5550 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
5551 if (exidx_section
!= NULL
5552 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
5553 && !parameters
->options().relocatable())
5555 // Create __exidx_start and __exdix_end symbols.
5556 symtab
->define_in_output_data("__exidx_start", NULL
,
5557 Symbol_table::PREDEFINED
,
5558 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5559 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5561 symtab
->define_in_output_data("__exidx_end", NULL
,
5562 Symbol_table::PREDEFINED
,
5563 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5564 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5567 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5568 // the .ARM.exidx section.
5569 if (!layout
->script_options()->saw_phdrs_clause())
5571 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
5573 Output_segment
* exidx_segment
=
5574 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
5575 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
5580 // Create an .ARM.attributes section if there is not one already.
5581 Output_attributes_section_data
* attributes_section
=
5582 new Output_attributes_section_data(*this->attributes_section_data_
);
5583 layout
->add_output_section_data(".ARM.attributes",
5584 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
5585 attributes_section
, false, false, false,
5589 // Return whether a direct absolute static relocation needs to be applied.
5590 // In cases where Scan::local() or Scan::global() has created
5591 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5592 // of the relocation is carried in the data, and we must not
5593 // apply the static relocation.
5595 template<bool big_endian
>
5597 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
5598 const Sized_symbol
<32>* gsym
,
5601 Output_section
* output_section
)
5603 // If the output section is not allocated, then we didn't call
5604 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5606 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
5609 // For local symbols, we will have created a non-RELATIVE dynamic
5610 // relocation only if (a) the output is position independent,
5611 // (b) the relocation is absolute (not pc- or segment-relative), and
5612 // (c) the relocation is not 32 bits wide.
5614 return !(parameters
->options().output_is_position_independent()
5615 && (ref_flags
& Symbol::ABSOLUTE_REF
)
5618 // For global symbols, we use the same helper routines used in the
5619 // scan pass. If we did not create a dynamic relocation, or if we
5620 // created a RELATIVE dynamic relocation, we should apply the static
5622 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
5623 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
5624 && gsym
->can_use_relative_reloc(ref_flags
5625 & Symbol::FUNCTION_CALL
);
5626 return !has_dyn
|| is_rel
;
5629 // Perform a relocation.
5631 template<bool big_endian
>
5633 Target_arm
<big_endian
>::Relocate::relocate(
5634 const Relocate_info
<32, big_endian
>* relinfo
,
5636 Output_section
*output_section
,
5638 const elfcpp::Rel
<32, big_endian
>& rel
,
5639 unsigned int r_type
,
5640 const Sized_symbol
<32>* gsym
,
5641 const Symbol_value
<32>* psymval
,
5642 unsigned char* view
,
5643 Arm_address address
,
5644 section_size_type
/* view_size */ )
5646 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
5648 r_type
= get_real_reloc_type(r_type
);
5650 const Arm_relobj
<big_endian
>* object
=
5651 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5653 // If the final branch target of a relocation is THUMB instruction, this
5654 // is 1. Otherwise it is 0.
5655 Arm_address thumb_bit
= 0;
5656 Symbol_value
<32> symval
;
5657 bool is_weakly_undefined_without_plt
= false;
5658 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
5662 // This is a global symbol. Determine if we use PLT and if the
5663 // final target is THUMB.
5664 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
5666 // This uses a PLT, change the symbol value.
5667 symval
.set_output_value(target
->plt_section()->address()
5668 + gsym
->plt_offset());
5671 else if (gsym
->is_weak_undefined())
5673 // This is a weakly undefined symbol and we do not use PLT
5674 // for this relocation. A branch targeting this symbol will
5675 // be converted into an NOP.
5676 is_weakly_undefined_without_plt
= true;
5680 // Set thumb bit if symbol:
5681 // -Has type STT_ARM_TFUNC or
5682 // -Has type STT_FUNC, is defined and with LSB in value set.
5684 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5685 || (gsym
->type() == elfcpp::STT_FUNC
5686 && !gsym
->is_undefined()
5687 && ((psymval
->value(object
, 0) & 1) != 0)))
5694 // This is a local symbol. Determine if the final target is THUMB.
5695 // We saved this information when all the local symbols were read.
5696 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
5697 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5698 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
5703 // This is a fake relocation synthesized for a stub. It does not have
5704 // a real symbol. We just look at the LSB of the symbol value to
5705 // determine if the target is THUMB or not.
5706 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
5709 // Strip LSB if this points to a THUMB target.
5711 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5712 && ((psymval
->value(object
, 0) & 1) != 0))
5714 Arm_address stripped_value
=
5715 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
5716 symval
.set_output_value(stripped_value
);
5720 // Get the GOT offset if needed.
5721 // The GOT pointer points to the end of the GOT section.
5722 // We need to subtract the size of the GOT section to get
5723 // the actual offset to use in the relocation.
5724 bool have_got_offset
= false;
5725 unsigned int got_offset
= 0;
5728 case elfcpp::R_ARM_GOT_BREL
:
5729 case elfcpp::R_ARM_GOT_PREL
:
5732 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5733 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
5734 - target
->got_size());
5738 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5739 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5740 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
5741 - target
->got_size());
5743 have_got_offset
= true;
5750 // To look up relocation stubs, we need to pass the symbol table index of
5752 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5754 typename
Arm_relocate_functions::Status reloc_status
=
5755 Arm_relocate_functions::STATUS_OKAY
;
5758 case elfcpp::R_ARM_NONE
:
5761 case elfcpp::R_ARM_ABS8
:
5762 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5764 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
5767 case elfcpp::R_ARM_ABS12
:
5768 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5770 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
5773 case elfcpp::R_ARM_ABS16
:
5774 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5776 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
5779 case elfcpp::R_ARM_ABS32
:
5780 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5782 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5786 case elfcpp::R_ARM_ABS32_NOI
:
5787 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5789 // No thumb bit for this relocation: (S + A)
5790 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5794 case elfcpp::R_ARM_MOVW_ABS_NC
:
5795 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5797 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
5801 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5802 "a shared object; recompile with -fPIC"));
5805 case elfcpp::R_ARM_MOVT_ABS
:
5806 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5808 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
5810 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5811 "a shared object; recompile with -fPIC"));
5814 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5815 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5817 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
5821 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5822 "making a shared object; recompile with -fPIC"));
5825 case elfcpp::R_ARM_THM_MOVT_ABS
:
5826 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5828 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
5831 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5832 "making a shared object; recompile with -fPIC"));
5835 case elfcpp::R_ARM_MOVW_PREL_NC
:
5836 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
5841 case elfcpp::R_ARM_MOVT_PREL
:
5842 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
5846 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5847 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
5852 case elfcpp::R_ARM_THM_MOVT_PREL
:
5853 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
5857 case elfcpp::R_ARM_REL32
:
5858 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5859 address
, thumb_bit
);
5862 case elfcpp::R_ARM_THM_ABS5
:
5863 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5865 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
5868 case elfcpp::R_ARM_THM_CALL
:
5870 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
5871 psymval
, address
, thumb_bit
,
5872 is_weakly_undefined_without_plt
);
5875 case elfcpp::R_ARM_XPC25
:
5877 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
5878 psymval
, address
, thumb_bit
,
5879 is_weakly_undefined_without_plt
);
5882 case elfcpp::R_ARM_THM_XPC22
:
5884 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
5885 psymval
, address
, thumb_bit
,
5886 is_weakly_undefined_without_plt
);
5889 case elfcpp::R_ARM_GOTOFF32
:
5891 Arm_address got_origin
;
5892 got_origin
= target
->got_plt_section()->address();
5893 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5894 got_origin
, thumb_bit
);
5898 case elfcpp::R_ARM_BASE_PREL
:
5901 // Get the addressing origin of the output segment defining the
5902 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5903 gold_assert(gsym
!= NULL
);
5904 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5905 origin
= gsym
->output_segment()->vaddr();
5906 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5907 origin
= gsym
->output_data()->address();
5910 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5911 _("cannot find origin of R_ARM_BASE_PREL"));
5914 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
5918 case elfcpp::R_ARM_BASE_ABS
:
5920 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5925 // Get the addressing origin of the output segment defining
5926 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5928 // R_ARM_BASE_ABS with the NULL symbol will give the
5929 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5930 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5931 origin
= target
->got_plt_section()->address();
5932 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5933 origin
= gsym
->output_segment()->vaddr();
5934 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5935 origin
= gsym
->output_data()->address();
5938 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5939 _("cannot find origin of R_ARM_BASE_ABS"));
5943 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5947 case elfcpp::R_ARM_GOT_BREL
:
5948 gold_assert(have_got_offset
);
5949 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5952 case elfcpp::R_ARM_GOT_PREL
:
5953 gold_assert(have_got_offset
);
5954 // Get the address origin for GOT PLT, which is allocated right
5955 // after the GOT section, to calculate an absolute address of
5956 // the symbol GOT entry (got_origin + got_offset).
5957 Arm_address got_origin
;
5958 got_origin
= target
->got_plt_section()->address();
5959 reloc_status
= Arm_relocate_functions::got_prel(view
,
5960 got_origin
+ got_offset
,
5964 case elfcpp::R_ARM_PLT32
:
5965 gold_assert(gsym
== NULL
5966 || gsym
->has_plt_offset()
5967 || gsym
->final_value_is_known()
5968 || (gsym
->is_defined()
5969 && !gsym
->is_from_dynobj()
5970 && !gsym
->is_preemptible()));
5972 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5973 psymval
, address
, thumb_bit
,
5974 is_weakly_undefined_without_plt
);
5977 case elfcpp::R_ARM_CALL
:
5979 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5980 psymval
, address
, thumb_bit
,
5981 is_weakly_undefined_without_plt
);
5984 case elfcpp::R_ARM_JUMP24
:
5986 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5987 psymval
, address
, thumb_bit
,
5988 is_weakly_undefined_without_plt
);
5991 case elfcpp::R_ARM_THM_JUMP24
:
5993 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5994 psymval
, address
, thumb_bit
,
5995 is_weakly_undefined_without_plt
);
5998 case elfcpp::R_ARM_THM_JUMP19
:
6000 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
6004 case elfcpp::R_ARM_PREL31
:
6005 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
6006 address
, thumb_bit
);
6009 case elfcpp::R_ARM_TARGET1
:
6010 // This should have been mapped to another type already.
6012 case elfcpp::R_ARM_COPY
:
6013 case elfcpp::R_ARM_GLOB_DAT
:
6014 case elfcpp::R_ARM_JUMP_SLOT
:
6015 case elfcpp::R_ARM_RELATIVE
:
6016 // These are relocations which should only be seen by the
6017 // dynamic linker, and should never be seen here.
6018 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6019 _("unexpected reloc %u in object file"),
6024 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6025 _("unsupported reloc %u"),
6030 // Report any errors.
6031 switch (reloc_status
)
6033 case Arm_relocate_functions::STATUS_OKAY
:
6035 case Arm_relocate_functions::STATUS_OVERFLOW
:
6036 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6037 _("relocation overflow in relocation %u"),
6040 case Arm_relocate_functions::STATUS_BAD_RELOC
:
6041 gold_error_at_location(
6045 _("unexpected opcode while processing relocation %u"),
6055 // Relocate section data.
6057 template<bool big_endian
>
6059 Target_arm
<big_endian
>::relocate_section(
6060 const Relocate_info
<32, big_endian
>* relinfo
,
6061 unsigned int sh_type
,
6062 const unsigned char* prelocs
,
6064 Output_section
* output_section
,
6065 bool needs_special_offset_handling
,
6066 unsigned char* view
,
6067 Arm_address address
,
6068 section_size_type view_size
,
6069 const Reloc_symbol_changes
* reloc_symbol_changes
)
6071 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
6072 gold_assert(sh_type
== elfcpp::SHT_REL
);
6074 Arm_input_section
<big_endian
>* arm_input_section
=
6075 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
6077 // This is an ARM input section and the view covers the whole output
6079 if (arm_input_section
!= NULL
)
6081 gold_assert(needs_special_offset_handling
);
6082 Arm_address section_address
= arm_input_section
->address();
6083 section_size_type section_size
= arm_input_section
->data_size();
6085 gold_assert((arm_input_section
->address() >= address
)
6086 && ((arm_input_section
->address()
6087 + arm_input_section
->data_size())
6088 <= (address
+ view_size
)));
6090 off_t offset
= section_address
- address
;
6093 view_size
= section_size
;
6096 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
6103 needs_special_offset_handling
,
6107 reloc_symbol_changes
);
6110 // Return the size of a relocation while scanning during a relocatable
6113 template<bool big_endian
>
6115 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
6116 unsigned int r_type
,
6119 r_type
= get_real_reloc_type(r_type
);
6122 case elfcpp::R_ARM_NONE
:
6125 case elfcpp::R_ARM_ABS8
:
6128 case elfcpp::R_ARM_ABS16
:
6129 case elfcpp::R_ARM_THM_ABS5
:
6132 case elfcpp::R_ARM_ABS32
:
6133 case elfcpp::R_ARM_ABS32_NOI
:
6134 case elfcpp::R_ARM_ABS12
:
6135 case elfcpp::R_ARM_BASE_ABS
:
6136 case elfcpp::R_ARM_REL32
:
6137 case elfcpp::R_ARM_THM_CALL
:
6138 case elfcpp::R_ARM_GOTOFF32
:
6139 case elfcpp::R_ARM_BASE_PREL
:
6140 case elfcpp::R_ARM_GOT_BREL
:
6141 case elfcpp::R_ARM_GOT_PREL
:
6142 case elfcpp::R_ARM_PLT32
:
6143 case elfcpp::R_ARM_CALL
:
6144 case elfcpp::R_ARM_JUMP24
:
6145 case elfcpp::R_ARM_PREL31
:
6146 case elfcpp::R_ARM_MOVW_ABS_NC
:
6147 case elfcpp::R_ARM_MOVT_ABS
:
6148 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6149 case elfcpp::R_ARM_THM_MOVT_ABS
:
6150 case elfcpp::R_ARM_MOVW_PREL_NC
:
6151 case elfcpp::R_ARM_MOVT_PREL
:
6152 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6153 case elfcpp::R_ARM_THM_MOVT_PREL
:
6156 case elfcpp::R_ARM_TARGET1
:
6157 // This should have been mapped to another type already.
6159 case elfcpp::R_ARM_COPY
:
6160 case elfcpp::R_ARM_GLOB_DAT
:
6161 case elfcpp::R_ARM_JUMP_SLOT
:
6162 case elfcpp::R_ARM_RELATIVE
:
6163 // These are relocations which should only be seen by the
6164 // dynamic linker, and should never be seen here.
6165 gold_error(_("%s: unexpected reloc %u in object file"),
6166 object
->name().c_str(), r_type
);
6170 object
->error(_("unsupported reloc %u in object file"), r_type
);
6175 // Scan the relocs during a relocatable link.
6177 template<bool big_endian
>
6179 Target_arm
<big_endian
>::scan_relocatable_relocs(
6180 Symbol_table
* symtab
,
6182 Sized_relobj
<32, big_endian
>* object
,
6183 unsigned int data_shndx
,
6184 unsigned int sh_type
,
6185 const unsigned char* prelocs
,
6187 Output_section
* output_section
,
6188 bool needs_special_offset_handling
,
6189 size_t local_symbol_count
,
6190 const unsigned char* plocal_symbols
,
6191 Relocatable_relocs
* rr
)
6193 gold_assert(sh_type
== elfcpp::SHT_REL
);
6195 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
6196 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
6198 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
6199 Scan_relocatable_relocs
>(
6207 needs_special_offset_handling
,
6213 // Relocate a section during a relocatable link.
6215 template<bool big_endian
>
6217 Target_arm
<big_endian
>::relocate_for_relocatable(
6218 const Relocate_info
<32, big_endian
>* relinfo
,
6219 unsigned int sh_type
,
6220 const unsigned char* prelocs
,
6222 Output_section
* output_section
,
6223 off_t offset_in_output_section
,
6224 const Relocatable_relocs
* rr
,
6225 unsigned char* view
,
6226 Arm_address view_address
,
6227 section_size_type view_size
,
6228 unsigned char* reloc_view
,
6229 section_size_type reloc_view_size
)
6231 gold_assert(sh_type
== elfcpp::SHT_REL
);
6233 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
6238 offset_in_output_section
,
6247 // Return the value to use for a dynamic symbol which requires special
6248 // treatment. This is how we support equality comparisons of function
6249 // pointers across shared library boundaries, as described in the
6250 // processor specific ABI supplement.
6252 template<bool big_endian
>
6254 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6256 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6257 return this->plt_section()->address() + gsym
->plt_offset();
6260 // Map platform-specific relocs to real relocs
6262 template<bool big_endian
>
6264 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
6268 case elfcpp::R_ARM_TARGET1
:
6269 // This is either R_ARM_ABS32 or R_ARM_REL32;
6270 return elfcpp::R_ARM_ABS32
;
6272 case elfcpp::R_ARM_TARGET2
:
6273 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6274 return elfcpp::R_ARM_GOT_PREL
;
6281 // Whether if two EABI versions V1 and V2 are compatible.
6283 template<bool big_endian
>
6285 Target_arm
<big_endian
>::are_eabi_versions_compatible(
6286 elfcpp::Elf_Word v1
,
6287 elfcpp::Elf_Word v2
)
6289 // v4 and v5 are the same spec before and after it was released,
6290 // so allow mixing them.
6291 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
6292 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
6298 // Combine FLAGS from an input object called NAME and the processor-specific
6299 // flags in the ELF header of the output. Much of this is adapted from the
6300 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6301 // in bfd/elf32-arm.c.
6303 template<bool big_endian
>
6305 Target_arm
<big_endian
>::merge_processor_specific_flags(
6306 const std::string
& name
,
6307 elfcpp::Elf_Word flags
)
6309 if (this->are_processor_specific_flags_set())
6311 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
6313 // Nothing to merge if flags equal to those in output.
6314 if (flags
== out_flags
)
6317 // Complain about various flag mismatches.
6318 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
6319 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
6320 if (!this->are_eabi_versions_compatible(version1
, version2
))
6321 gold_error(_("Source object %s has EABI version %d but output has "
6322 "EABI version %d."),
6324 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
6325 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
6329 // If the input is the default architecture and had the default
6330 // flags then do not bother setting the flags for the output
6331 // architecture, instead allow future merges to do this. If no
6332 // future merges ever set these flags then they will retain their
6333 // uninitialised values, which surprise surprise, correspond
6334 // to the default values.
6338 // This is the first time, just copy the flags.
6339 // We only copy the EABI version for now.
6340 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
6344 // Adjust ELF file header.
6345 template<bool big_endian
>
6347 Target_arm
<big_endian
>::do_adjust_elf_header(
6348 unsigned char* view
,
6351 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
6353 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
6354 unsigned char e_ident
[elfcpp::EI_NIDENT
];
6355 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
6357 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6358 == elfcpp::EF_ARM_EABI_UNKNOWN
)
6359 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
6361 e_ident
[elfcpp::EI_OSABI
] = 0;
6362 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
6364 // FIXME: Do EF_ARM_BE8 adjustment.
6366 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
6367 oehdr
.put_e_ident(e_ident
);
6370 // do_make_elf_object to override the same function in the base class.
6371 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6372 // to store ARM specific information. Hence we need to have our own
6373 // ELF object creation.
6375 template<bool big_endian
>
6377 Target_arm
<big_endian
>::do_make_elf_object(
6378 const std::string
& name
,
6379 Input_file
* input_file
,
6380 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
6382 int et
= ehdr
.get_e_type();
6383 if (et
== elfcpp::ET_REL
)
6385 Arm_relobj
<big_endian
>* obj
=
6386 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6390 else if (et
== elfcpp::ET_DYN
)
6392 Sized_dynobj
<32, big_endian
>* obj
=
6393 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6399 gold_error(_("%s: unsupported ELF file type %d"),
6405 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6406 // Returns -1 if no architecture could be read.
6407 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6409 template<bool big_endian
>
6411 Target_arm
<big_endian
>::get_secondary_compatible_arch(
6412 const Attributes_section_data
* pasd
)
6414 const Object_attribute
*known_attributes
=
6415 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6417 // Note: the tag and its argument below are uleb128 values, though
6418 // currently-defined values fit in one byte for each.
6419 const std::string
& sv
=
6420 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
6422 && sv
.data()[0] == elfcpp::Tag_CPU_arch
6423 && (sv
.data()[1] & 128) != 128)
6424 return sv
.data()[1];
6426 // This tag is "safely ignorable", so don't complain if it looks funny.
6430 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6431 // The tag is removed if ARCH is -1.
6432 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6434 template<bool big_endian
>
6436 Target_arm
<big_endian
>::set_secondary_compatible_arch(
6437 Attributes_section_data
* pasd
,
6440 Object_attribute
*known_attributes
=
6441 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6445 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
6449 // Note: the tag and its argument below are uleb128 values, though
6450 // currently-defined values fit in one byte for each.
6452 sv
[0] = elfcpp::Tag_CPU_arch
;
6453 gold_assert(arch
!= 0);
6457 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
6460 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6462 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6464 template<bool big_endian
>
6466 Target_arm
<big_endian
>::tag_cpu_arch_combine(
6469 int* secondary_compat_out
,
6471 int secondary_compat
)
6473 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6474 static const int v6t2
[] =
6486 static const int v6k
[] =
6499 static const int v7
[] =
6513 static const int v6_m
[] =
6528 static const int v6s_m
[] =
6544 static const int v7e_m
[] =
6561 static const int v4t_plus_v6_m
[] =
6577 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
6579 static const int *comb
[] =
6587 // Pseudo-architecture.
6591 // Check we've not got a higher architecture than we know about.
6593 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
6595 gold_error(_("%s: unknown CPU architecture"), name
);
6599 // Override old tag if we have a Tag_also_compatible_with on the output.
6601 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
6602 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
6603 oldtag
= T(V4T_PLUS_V6_M
);
6605 // And override the new tag if we have a Tag_also_compatible_with on the
6608 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
6609 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
6610 newtag
= T(V4T_PLUS_V6_M
);
6612 // Architectures before V6KZ add features monotonically.
6613 int tagh
= std::max(oldtag
, newtag
);
6614 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
6617 int tagl
= std::min(oldtag
, newtag
);
6618 int result
= comb
[tagh
- T(V6T2
)][tagl
];
6620 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6621 // as the canonical version.
6622 if (result
== T(V4T_PLUS_V6_M
))
6625 *secondary_compat_out
= T(V6_M
);
6628 *secondary_compat_out
= -1;
6632 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6633 name
, oldtag
, newtag
);
6641 // Helper to print AEABI enum tag value.
6643 template<bool big_endian
>
6645 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
6647 static const char *aeabi_enum_names
[] =
6648 { "", "variable-size", "32-bit", "" };
6649 const size_t aeabi_enum_names_size
=
6650 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
6652 if (value
< aeabi_enum_names_size
)
6653 return std::string(aeabi_enum_names
[value
]);
6657 sprintf(buffer
, "<unknown value %u>", value
);
6658 return std::string(buffer
);
6662 // Return the string value to store in TAG_CPU_name.
6664 template<bool big_endian
>
6666 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
6668 static const char *name_table
[] = {
6669 // These aren't real CPU names, but we can't guess
6670 // that from the architecture version alone.
6686 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
6688 if (value
< name_table_size
)
6689 return std::string(name_table
[value
]);
6693 sprintf(buffer
, "<unknown CPU value %u>", value
);
6694 return std::string(buffer
);
6698 // Merge object attributes from input file called NAME with those of the
6699 // output. The input object attributes are in the object pointed by PASD.
6701 template<bool big_endian
>
6703 Target_arm
<big_endian
>::merge_object_attributes(
6705 const Attributes_section_data
* pasd
)
6707 // Return if there is no attributes section data.
6711 // If output has no object attributes, just copy.
6712 if (this->attributes_section_data_
== NULL
)
6714 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
6718 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
6719 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
6720 Object_attribute
* out_attr
=
6721 this->attributes_section_data_
->known_attributes(vendor
);
6723 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6724 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
6725 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
6727 // Ignore mismatches if the object doesn't use floating point. */
6728 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
6729 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
6730 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
6731 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
6732 gold_error(_("%s uses VFP register arguments, output does not"),
6736 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
6738 // Merge this attribute with existing attributes.
6741 case elfcpp::Tag_CPU_raw_name
:
6742 case elfcpp::Tag_CPU_name
:
6743 // These are merged after Tag_CPU_arch.
6746 case elfcpp::Tag_ABI_optimization_goals
:
6747 case elfcpp::Tag_ABI_FP_optimization_goals
:
6748 // Use the first value seen.
6751 case elfcpp::Tag_CPU_arch
:
6753 unsigned int saved_out_attr
= out_attr
->int_value();
6754 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6755 int secondary_compat
=
6756 this->get_secondary_compatible_arch(pasd
);
6757 int secondary_compat_out
=
6758 this->get_secondary_compatible_arch(
6759 this->attributes_section_data_
);
6760 out_attr
[i
].set_int_value(
6761 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
6762 &secondary_compat_out
,
6763 in_attr
[i
].int_value(),
6765 this->set_secondary_compatible_arch(this->attributes_section_data_
,
6766 secondary_compat_out
);
6768 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6769 if (out_attr
[i
].int_value() == saved_out_attr
)
6770 ; // Leave the names alone.
6771 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
6773 // The output architecture has been changed to match the
6774 // input architecture. Use the input names.
6775 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
6776 in_attr
[elfcpp::Tag_CPU_name
].string_value());
6777 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
6778 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
6782 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
6783 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
6786 // If we still don't have a value for Tag_CPU_name,
6787 // make one up now. Tag_CPU_raw_name remains blank.
6788 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
6790 const std::string cpu_name
=
6791 this->tag_cpu_name_value(out_attr
[i
].int_value());
6792 // FIXME: If we see an unknown CPU, this will be set
6793 // to "<unknown CPU n>", where n is the attribute value.
6794 // This is different from BFD, which leaves the name alone.
6795 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
6800 case elfcpp::Tag_ARM_ISA_use
:
6801 case elfcpp::Tag_THUMB_ISA_use
:
6802 case elfcpp::Tag_WMMX_arch
:
6803 case elfcpp::Tag_Advanced_SIMD_arch
:
6804 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6805 case elfcpp::Tag_ABI_FP_rounding
:
6806 case elfcpp::Tag_ABI_FP_exceptions
:
6807 case elfcpp::Tag_ABI_FP_user_exceptions
:
6808 case elfcpp::Tag_ABI_FP_number_model
:
6809 case elfcpp::Tag_VFP_HP_extension
:
6810 case elfcpp::Tag_CPU_unaligned_access
:
6811 case elfcpp::Tag_T2EE_use
:
6812 case elfcpp::Tag_Virtualization_use
:
6813 case elfcpp::Tag_MPextension_use
:
6814 // Use the largest value specified.
6815 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6816 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6819 case elfcpp::Tag_ABI_align8_preserved
:
6820 case elfcpp::Tag_ABI_PCS_RO_data
:
6821 // Use the smallest value specified.
6822 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6823 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6826 case elfcpp::Tag_ABI_align8_needed
:
6827 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
6828 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
6829 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
6832 // This error message should be enabled once all non-conformant
6833 // binaries in the toolchain have had the attributes set
6835 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6839 case elfcpp::Tag_ABI_FP_denormal
:
6840 case elfcpp::Tag_ABI_PCS_GOT_use
:
6842 // These tags have 0 = don't care, 1 = strong requirement,
6843 // 2 = weak requirement.
6844 static const int order_021
[3] = {0, 2, 1};
6846 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6847 // value if greater than 2 (for future-proofing).
6848 if ((in_attr
[i
].int_value() > 2
6849 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6850 || (in_attr
[i
].int_value() <= 2
6851 && out_attr
[i
].int_value() <= 2
6852 && (order_021
[in_attr
[i
].int_value()]
6853 > order_021
[out_attr
[i
].int_value()])))
6854 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6858 case elfcpp::Tag_CPU_arch_profile
:
6859 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
6861 // 0 will merge with anything.
6862 // 'A' and 'S' merge to 'A'.
6863 // 'R' and 'S' merge to 'R'.
6864 // 'M' and 'A|R|S' is an error.
6865 if (out_attr
[i
].int_value() == 0
6866 || (out_attr
[i
].int_value() == 'S'
6867 && (in_attr
[i
].int_value() == 'A'
6868 || in_attr
[i
].int_value() == 'R')))
6869 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6870 else if (in_attr
[i
].int_value() == 0
6871 || (in_attr
[i
].int_value() == 'S'
6872 && (out_attr
[i
].int_value() == 'A'
6873 || out_attr
[i
].int_value() == 'R')))
6878 (_("conflicting architecture profiles %c/%c"),
6879 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
6880 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
6884 case elfcpp::Tag_VFP_arch
:
6901 // Values greater than 6 aren't defined, so just pick the
6903 if (in_attr
[i
].int_value() > 6
6904 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6906 *out_attr
= *in_attr
;
6909 // The output uses the superset of input features
6910 // (ISA version) and registers.
6911 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
6912 vfp_versions
[out_attr
[i
].int_value()].ver
);
6913 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
6914 vfp_versions
[out_attr
[i
].int_value()].regs
);
6915 // This assumes all possible supersets are also a valid
6918 for (newval
= 6; newval
> 0; newval
--)
6920 if (regs
== vfp_versions
[newval
].regs
6921 && ver
== vfp_versions
[newval
].ver
)
6924 out_attr
[i
].set_int_value(newval
);
6927 case elfcpp::Tag_PCS_config
:
6928 if (out_attr
[i
].int_value() == 0)
6929 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6930 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6932 // It's sometimes ok to mix different configs, so this is only
6934 gold_warning(_("%s: conflicting platform configuration"), name
);
6937 case elfcpp::Tag_ABI_PCS_R9_use
:
6938 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
6939 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
6940 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
6942 gold_error(_("%s: conflicting use of R9"), name
);
6944 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
6945 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6947 case elfcpp::Tag_ABI_PCS_RW_data
:
6948 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6949 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6950 != elfcpp::AEABI_R9_SB
)
6951 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6952 != elfcpp::AEABI_R9_unused
))
6954 gold_error(_("%s: SB relative addressing conflicts with use "
6958 // Use the smallest value specified.
6959 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6960 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6962 case elfcpp::Tag_ABI_PCS_wchar_t
:
6963 // FIXME: Make it possible to turn off this warning.
6964 if (out_attr
[i
].int_value()
6965 && in_attr
[i
].int_value()
6966 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6968 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6969 "use %u-byte wchar_t; use of wchar_t values "
6970 "across objects may fail"),
6971 name
, in_attr
[i
].int_value(),
6972 out_attr
[i
].int_value());
6974 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
6975 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6977 case elfcpp::Tag_ABI_enum_size
:
6978 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
6980 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
6981 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
6983 // The existing object is compatible with anything.
6984 // Use whatever requirements the new object has.
6985 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6987 // FIXME: Make it possible to turn off this warning.
6988 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
6989 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6991 unsigned int in_value
= in_attr
[i
].int_value();
6992 unsigned int out_value
= out_attr
[i
].int_value();
6993 gold_warning(_("%s uses %s enums yet the output is to use "
6994 "%s enums; use of enum values across objects "
6997 this->aeabi_enum_name(in_value
).c_str(),
6998 this->aeabi_enum_name(out_value
).c_str());
7002 case elfcpp::Tag_ABI_VFP_args
:
7005 case elfcpp::Tag_ABI_WMMX_args
:
7006 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7008 gold_error(_("%s uses iWMMXt register arguments, output does "
7013 case Object_attribute::Tag_compatibility
:
7014 // Merged in target-independent code.
7016 case elfcpp::Tag_ABI_HardFP_use
:
7017 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7018 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
7019 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
7020 out_attr
[i
].set_int_value(3);
7021 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7022 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7024 case elfcpp::Tag_ABI_FP_16bit_format
:
7025 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7027 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7028 gold_error(_("fp16 format mismatch between %s and output"),
7031 if (in_attr
[i
].int_value() != 0)
7032 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7035 case elfcpp::Tag_nodefaults
:
7036 // This tag is set if it exists, but the value is unused (and is
7037 // typically zero). We don't actually need to do anything here -
7038 // the merge happens automatically when the type flags are merged
7041 case elfcpp::Tag_also_compatible_with
:
7042 // Already done in Tag_CPU_arch.
7044 case elfcpp::Tag_conformance
:
7045 // Keep the attribute if it matches. Throw it away otherwise.
7046 // No attribute means no claim to conform.
7047 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
7048 out_attr
[i
].set_string_value("");
7053 const char* err_object
= NULL
;
7055 // The "known_obj_attributes" table does contain some undefined
7056 // attributes. Ensure that there are unused.
7057 if (out_attr
[i
].int_value() != 0
7058 || out_attr
[i
].string_value() != "")
7059 err_object
= "output";
7060 else if (in_attr
[i
].int_value() != 0
7061 || in_attr
[i
].string_value() != "")
7064 if (err_object
!= NULL
)
7066 // Attribute numbers >=64 (mod 128) can be safely ignored.
7068 gold_error(_("%s: unknown mandatory EABI object attribute "
7072 gold_warning(_("%s: unknown EABI object attribute %d"),
7076 // Only pass on attributes that match in both inputs.
7077 if (!in_attr
[i
].matches(out_attr
[i
]))
7079 out_attr
[i
].set_int_value(0);
7080 out_attr
[i
].set_string_value("");
7085 // If out_attr was copied from in_attr then it won't have a type yet.
7086 if (in_attr
[i
].type() && !out_attr
[i
].type())
7087 out_attr
[i
].set_type(in_attr
[i
].type());
7090 // Merge Tag_compatibility attributes and any common GNU ones.
7091 this->attributes_section_data_
->merge(name
, pasd
);
7093 // Check for any attributes not known on ARM.
7094 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
7095 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
7096 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
7097 Other_attributes
* out_other_attributes
=
7098 this->attributes_section_data_
->other_attributes(vendor
);
7099 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
7101 while (in_iter
!= in_other_attributes
->end()
7102 || out_iter
!= out_other_attributes
->end())
7104 const char* err_object
= NULL
;
7107 // The tags for each list are in numerical order.
7108 // If the tags are equal, then merge.
7109 if (out_iter
!= out_other_attributes
->end()
7110 && (in_iter
== in_other_attributes
->end()
7111 || in_iter
->first
> out_iter
->first
))
7113 // This attribute only exists in output. We can't merge, and we
7114 // don't know what the tag means, so delete it.
7115 err_object
= "output";
7116 err_tag
= out_iter
->first
;
7117 int saved_tag
= out_iter
->first
;
7118 delete out_iter
->second
;
7119 out_other_attributes
->erase(out_iter
);
7120 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7122 else if (in_iter
!= in_other_attributes
->end()
7123 && (out_iter
!= out_other_attributes
->end()
7124 || in_iter
->first
< out_iter
->first
))
7126 // This attribute only exists in input. We can't merge, and we
7127 // don't know what the tag means, so ignore it.
7129 err_tag
= in_iter
->first
;
7132 else // The tags are equal.
7134 // As present, all attributes in the list are unknown, and
7135 // therefore can't be merged meaningfully.
7136 err_object
= "output";
7137 err_tag
= out_iter
->first
;
7139 // Only pass on attributes that match in both inputs.
7140 if (!in_iter
->second
->matches(*(out_iter
->second
)))
7142 // No match. Delete the attribute.
7143 int saved_tag
= out_iter
->first
;
7144 delete out_iter
->second
;
7145 out_other_attributes
->erase(out_iter
);
7146 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7150 // Matched. Keep the attribute and move to the next.
7158 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7159 if ((err_tag
& 127) < 64)
7161 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7162 err_object
, err_tag
);
7166 gold_warning(_("%s: unknown EABI object attribute %d"),
7167 err_object
, err_tag
);
7173 // Return whether a relocation type used the LSB to distinguish THUMB
7175 template<bool big_endian
>
7177 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
7181 case elfcpp::R_ARM_PC24
:
7182 case elfcpp::R_ARM_ABS32
:
7183 case elfcpp::R_ARM_REL32
:
7184 case elfcpp::R_ARM_SBREL32
:
7185 case elfcpp::R_ARM_THM_CALL
:
7186 case elfcpp::R_ARM_GLOB_DAT
:
7187 case elfcpp::R_ARM_JUMP_SLOT
:
7188 case elfcpp::R_ARM_GOTOFF32
:
7189 case elfcpp::R_ARM_PLT32
:
7190 case elfcpp::R_ARM_CALL
:
7191 case elfcpp::R_ARM_JUMP24
:
7192 case elfcpp::R_ARM_THM_JUMP24
:
7193 case elfcpp::R_ARM_SBREL31
:
7194 case elfcpp::R_ARM_PREL31
:
7195 case elfcpp::R_ARM_MOVW_ABS_NC
:
7196 case elfcpp::R_ARM_MOVW_PREL_NC
:
7197 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7198 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7199 case elfcpp::R_ARM_THM_JUMP19
:
7200 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7201 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7202 case elfcpp::R_ARM_ALU_PC_G0
:
7203 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7204 case elfcpp::R_ARM_ALU_PC_G1
:
7205 case elfcpp::R_ARM_ALU_PC_G2
:
7206 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7207 case elfcpp::R_ARM_ALU_SB_G0
:
7208 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7209 case elfcpp::R_ARM_ALU_SB_G1
:
7210 case elfcpp::R_ARM_ALU_SB_G2
:
7211 case elfcpp::R_ARM_MOVW_BREL_NC
:
7212 case elfcpp::R_ARM_MOVW_BREL
:
7213 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7214 case elfcpp::R_ARM_THM_MOVW_BREL
:
7221 // Stub-generation methods for Target_arm.
7223 // Make a new Arm_input_section object.
7225 template<bool big_endian
>
7226 Arm_input_section
<big_endian
>*
7227 Target_arm
<big_endian
>::new_arm_input_section(
7231 Input_section_specifier
iss(relobj
, shndx
);
7233 Arm_input_section
<big_endian
>* arm_input_section
=
7234 new Arm_input_section
<big_endian
>(relobj
, shndx
);
7235 arm_input_section
->init();
7237 // Register new Arm_input_section in map for look-up.
7238 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
7239 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
7241 // Make sure that it we have not created another Arm_input_section
7242 // for this input section already.
7243 gold_assert(ins
.second
);
7245 return arm_input_section
;
7248 // Find the Arm_input_section object corresponding to the SHNDX-th input
7249 // section of RELOBJ.
7251 template<bool big_endian
>
7252 Arm_input_section
<big_endian
>*
7253 Target_arm
<big_endian
>::find_arm_input_section(
7255 unsigned int shndx
) const
7257 Input_section_specifier
iss(relobj
, shndx
);
7258 typename
Arm_input_section_map::const_iterator p
=
7259 this->arm_input_section_map_
.find(iss
);
7260 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
7263 // Make a new stub table.
7265 template<bool big_endian
>
7266 Stub_table
<big_endian
>*
7267 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
7269 Stub_table
<big_endian
>* stub_table
=
7270 new Stub_table
<big_endian
>(owner
);
7271 this->stub_tables_
.push_back(stub_table
);
7273 stub_table
->set_address(owner
->address() + owner
->data_size());
7274 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
7275 stub_table
->finalize_data_size();
7280 // Scan a relocation for stub generation.
7282 template<bool big_endian
>
7284 Target_arm
<big_endian
>::scan_reloc_for_stub(
7285 const Relocate_info
<32, big_endian
>* relinfo
,
7286 unsigned int r_type
,
7287 const Sized_symbol
<32>* gsym
,
7289 const Symbol_value
<32>* psymval
,
7290 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
7291 Arm_address address
)
7293 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
7295 const Arm_relobj
<big_endian
>* arm_relobj
=
7296 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7298 bool target_is_thumb
;
7299 Symbol_value
<32> symval
;
7302 // This is a global symbol. Determine if we use PLT and if the
7303 // final target is THUMB.
7304 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
7306 // This uses a PLT, change the symbol value.
7307 symval
.set_output_value(this->plt_section()->address()
7308 + gsym
->plt_offset());
7310 target_is_thumb
= false;
7312 else if (gsym
->is_undefined())
7313 // There is no need to generate a stub symbol is undefined.
7318 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
7319 || (gsym
->type() == elfcpp::STT_FUNC
7320 && !gsym
->is_undefined()
7321 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
7326 // This is a local symbol. Determine if the final target is THUMB.
7327 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
7330 // Strip LSB if this points to a THUMB target.
7332 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
7333 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
7335 Arm_address stripped_value
=
7336 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
7337 symval
.set_output_value(stripped_value
);
7341 // Get the symbol value.
7342 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
7344 // Owing to pipelining, the PC relative branches below actually skip
7345 // two instructions when the branch offset is 0.
7346 Arm_address destination
;
7349 case elfcpp::R_ARM_CALL
:
7350 case elfcpp::R_ARM_JUMP24
:
7351 case elfcpp::R_ARM_PLT32
:
7353 destination
= value
+ addend
+ 8;
7355 case elfcpp::R_ARM_THM_CALL
:
7356 case elfcpp::R_ARM_THM_XPC22
:
7357 case elfcpp::R_ARM_THM_JUMP24
:
7358 case elfcpp::R_ARM_THM_JUMP19
:
7360 destination
= value
+ addend
+ 4;
7366 Reloc_stub
* stub
= NULL
;
7367 Stub_type stub_type
=
7368 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
7370 if (stub_type
!= arm_stub_none
)
7372 // Try looking up an existing stub from a stub table.
7373 Stub_table
<big_endian
>* stub_table
=
7374 arm_relobj
->stub_table(relinfo
->data_shndx
);
7375 gold_assert(stub_table
!= NULL
);
7377 // Locate stub by destination.
7378 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
7380 // Create a stub if there is not one already
7381 stub
= stub_table
->find_reloc_stub(stub_key
);
7384 // create a new stub and add it to stub table.
7385 stub
= this->stub_factory().make_reloc_stub(stub_type
);
7386 stub_table
->add_reloc_stub(stub
, stub_key
);
7389 // Record the destination address.
7390 stub
->set_destination_address(destination
7391 | (target_is_thumb
? 1 : 0));
7394 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7395 if (this->fix_cortex_a8_
7396 && (r_type
== elfcpp::R_ARM_THM_JUMP24
7397 || r_type
== elfcpp::R_ARM_THM_JUMP19
7398 || r_type
== elfcpp::R_ARM_THM_CALL
7399 || r_type
== elfcpp::R_ARM_THM_XPC22
)
7400 && (address
& 0xfffU
) == 0xffeU
)
7402 // Found a candidate. Note we haven't checked the destination is
7403 // within 4K here: if we do so (and don't create a record) we can't
7404 // tell that a branch should have been relocated when scanning later.
7405 this->cortex_a8_relocs_info_
[address
] =
7406 new Cortex_a8_reloc(stub
, r_type
,
7407 destination
| (target_is_thumb
? 1 : 0));
7411 // This function scans a relocation sections for stub generation.
7412 // The template parameter Relocate must be a class type which provides
7413 // a single function, relocate(), which implements the machine
7414 // specific part of a relocation.
7416 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7417 // SHT_REL or SHT_RELA.
7419 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7420 // of relocs. OUTPUT_SECTION is the output section.
7421 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7422 // mapped to output offsets.
7424 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7425 // VIEW_SIZE is the size. These refer to the input section, unless
7426 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7427 // the output section.
7429 template<bool big_endian
>
7430 template<int sh_type
>
7432 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
7433 const Relocate_info
<32, big_endian
>* relinfo
,
7434 const unsigned char* prelocs
,
7436 Output_section
* output_section
,
7437 bool needs_special_offset_handling
,
7438 const unsigned char* view
,
7439 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
7442 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
7443 const int reloc_size
=
7444 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
7446 Arm_relobj
<big_endian
>* arm_object
=
7447 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7448 unsigned int local_count
= arm_object
->local_symbol_count();
7450 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
7452 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
7454 Reltype
reloc(prelocs
);
7456 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
7457 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
7458 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
7460 r_type
= this->get_real_reloc_type(r_type
);
7462 // Only a few relocation types need stubs.
7463 if ((r_type
!= elfcpp::R_ARM_CALL
)
7464 && (r_type
!= elfcpp::R_ARM_JUMP24
)
7465 && (r_type
!= elfcpp::R_ARM_PLT32
)
7466 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
7467 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
7468 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
7469 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
7472 section_offset_type offset
=
7473 convert_to_section_size_type(reloc
.get_r_offset());
7475 if (needs_special_offset_handling
)
7477 offset
= output_section
->output_offset(relinfo
->object
,
7478 relinfo
->data_shndx
,
7485 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
7486 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
7487 stub_addend_reader(r_type
, view
+ offset
, reloc
);
7489 const Sized_symbol
<32>* sym
;
7491 Symbol_value
<32> symval
;
7492 const Symbol_value
<32> *psymval
;
7493 if (r_sym
< local_count
)
7496 psymval
= arm_object
->local_symbol(r_sym
);
7498 // If the local symbol belongs to a section we are discarding,
7499 // and that section is a debug section, try to find the
7500 // corresponding kept section and map this symbol to its
7501 // counterpart in the kept section. The symbol must not
7502 // correspond to a section we are folding.
7504 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7506 && shndx
!= elfcpp::SHN_UNDEF
7507 && !arm_object
->is_section_included(shndx
)
7508 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
7510 if (comdat_behavior
== CB_UNDETERMINED
)
7513 arm_object
->section_name(relinfo
->data_shndx
);
7514 comdat_behavior
= get_comdat_behavior(name
.c_str());
7516 if (comdat_behavior
== CB_PRETEND
)
7519 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
7520 arm_object
->map_to_kept_section(shndx
, &found
);
7522 symval
.set_output_value(value
+ psymval
->input_value());
7524 symval
.set_output_value(0);
7528 symval
.set_output_value(0);
7530 symval
.set_no_output_symtab_entry();
7536 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
7537 gold_assert(gsym
!= NULL
);
7538 if (gsym
->is_forwarder())
7539 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7541 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
7542 if (sym
->has_symtab_index())
7543 symval
.set_output_symtab_index(sym
->symtab_index());
7545 symval
.set_no_output_symtab_entry();
7547 // We need to compute the would-be final value of this global
7549 const Symbol_table
* symtab
= relinfo
->symtab
;
7550 const Sized_symbol
<32>* sized_symbol
=
7551 symtab
->get_sized_symbol
<32>(gsym
);
7552 Symbol_table::Compute_final_value_status status
;
7554 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
7556 // Skip this if the symbol has not output section.
7557 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
7560 symval
.set_output_value(value
);
7564 // If symbol is a section symbol, we don't know the actual type of
7565 // destination. Give up.
7566 if (psymval
->is_section_symbol())
7569 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
7570 addend
, view_address
+ offset
);
7574 // Scan an input section for stub generation.
7576 template<bool big_endian
>
7578 Target_arm
<big_endian
>::scan_section_for_stubs(
7579 const Relocate_info
<32, big_endian
>* relinfo
,
7580 unsigned int sh_type
,
7581 const unsigned char* prelocs
,
7583 Output_section
* output_section
,
7584 bool needs_special_offset_handling
,
7585 const unsigned char* view
,
7586 Arm_address view_address
,
7587 section_size_type view_size
)
7589 if (sh_type
== elfcpp::SHT_REL
)
7590 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
7595 needs_special_offset_handling
,
7599 else if (sh_type
== elfcpp::SHT_RELA
)
7600 // We do not support RELA type relocations yet. This is provided for
7602 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
7607 needs_special_offset_handling
,
7615 // Group input sections for stub generation.
7617 // We goup input sections in an output sections so that the total size,
7618 // including any padding space due to alignment is smaller than GROUP_SIZE
7619 // unless the only input section in group is bigger than GROUP_SIZE already.
7620 // Then an ARM stub table is created to follow the last input section
7621 // in group. For each group an ARM stub table is created an is placed
7622 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7623 // extend the group after the stub table.
7625 template<bool big_endian
>
7627 Target_arm
<big_endian
>::group_sections(
7629 section_size_type group_size
,
7630 bool stubs_always_after_branch
)
7632 // Group input sections and insert stub table
7633 Layout::Section_list section_list
;
7634 layout
->get_allocated_sections(§ion_list
);
7635 for (Layout::Section_list::const_iterator p
= section_list
.begin();
7636 p
!= section_list
.end();
7639 Arm_output_section
<big_endian
>* output_section
=
7640 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
7641 output_section
->group_sections(group_size
, stubs_always_after_branch
,
7646 // Relaxation hook. This is where we do stub generation.
7648 template<bool big_endian
>
7650 Target_arm
<big_endian
>::do_relax(
7652 const Input_objects
* input_objects
,
7653 Symbol_table
* symtab
,
7656 // No need to generate stubs if this is a relocatable link.
7657 gold_assert(!parameters
->options().relocatable());
7659 // If this is the first pass, we need to group input sections into
7663 // Determine the stub group size. The group size is the absolute
7664 // value of the parameter --stub-group-size. If --stub-group-size
7665 // is passed a negative value, we restict stubs to be always after
7666 // the stubbed branches.
7667 int32_t stub_group_size_param
=
7668 parameters
->options().stub_group_size();
7669 bool stubs_always_after_branch
= stub_group_size_param
< 0;
7670 section_size_type stub_group_size
= abs(stub_group_size_param
);
7672 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
7673 // page as the first half of a 32-bit branch straddling two 4K pages.
7674 // This is a crude way of enforcing that.
7675 if (this->fix_cortex_a8_
)
7676 stubs_always_after_branch
= true;
7678 if (stub_group_size
== 1)
7681 // Thumb branch range is +-4MB has to be used as the default
7682 // maximum size (a given section can contain both ARM and Thumb
7683 // code, so the worst case has to be taken into account).
7685 // This value is 24K less than that, which allows for 2025
7686 // 12-byte stubs. If we exceed that, then we will fail to link.
7687 // The user will have to relink with an explicit group size
7689 stub_group_size
= 4170000;
7692 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
7695 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
7696 // beginning of each relaxation pass, just blow away all the stubs.
7697 // Alternatively, we could selectively remove only the stubs and reloc
7698 // information for code sections that have moved since the last pass.
7699 // That would require more book-keeping.
7700 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
7701 if (this->fix_cortex_a8_
)
7703 // Clear all Cortex-A8 reloc information.
7704 for (typename
Cortex_a8_relocs_info::const_iterator p
=
7705 this->cortex_a8_relocs_info_
.begin();
7706 p
!= this->cortex_a8_relocs_info_
.end();
7709 this->cortex_a8_relocs_info_
.clear();
7711 // Remove all Cortex-A8 stubs.
7712 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7713 sp
!= this->stub_tables_
.end();
7715 (*sp
)->remove_all_cortex_a8_stubs();
7718 // Scan relocs for relocation stubs
7719 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
7720 op
!= input_objects
->relobj_end();
7723 Arm_relobj
<big_endian
>* arm_relobj
=
7724 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
7725 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
7728 // Check all stub tables to see if any of them have their data sizes
7729 // or addresses alignments changed. These are the only things that
7731 bool any_stub_table_changed
= false;
7732 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7733 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7736 if ((*sp
)->update_data_size_and_addralign())
7737 any_stub_table_changed
= true;
7740 // Finalize the stubs in the last relaxation pass.
7741 if (!any_stub_table_changed
)
7742 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7743 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7745 (*sp
)->finalize_stubs();
7747 return any_stub_table_changed
;
7752 template<bool big_endian
>
7754 Target_arm
<big_endian
>::relocate_stub(
7756 const Relocate_info
<32, big_endian
>* relinfo
,
7757 Output_section
* output_section
,
7758 unsigned char* view
,
7759 Arm_address address
,
7760 section_size_type view_size
)
7763 const Stub_template
* stub_template
= stub
->stub_template();
7764 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
7766 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
7767 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
7769 unsigned int r_type
= insn
->r_type();
7770 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
7771 section_size_type reloc_size
= insn
->size();
7772 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
7774 // This is the address of the stub destination.
7775 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
7776 Symbol_value
<32> symval
;
7777 symval
.set_output_value(target
);
7779 // Synthesize a fake reloc just in case. We don't have a symbol so
7781 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
7782 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
7783 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
7784 reloc_write
.put_r_offset(reloc_offset
);
7785 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
7786 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
7788 relocate
.relocate(relinfo
, this, output_section
,
7789 this->fake_relnum_for_stubs
, rel
, r_type
,
7790 NULL
, &symval
, view
+ reloc_offset
,
7791 address
+ reloc_offset
, reloc_size
);
7795 // Determine whether an object attribute tag takes an integer, a
7798 template<bool big_endian
>
7800 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
7802 if (tag
== Object_attribute::Tag_compatibility
)
7803 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7804 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
7805 else if (tag
== elfcpp::Tag_nodefaults
)
7806 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7807 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
7808 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
7809 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
7811 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
7813 return ((tag
& 1) != 0
7814 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7815 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
7818 // Reorder attributes.
7820 // The ABI defines that Tag_conformance should be emitted first, and that
7821 // Tag_nodefaults should be second (if either is defined). This sets those
7822 // two positions, and bumps up the position of all the remaining tags to
7825 template<bool big_endian
>
7827 Target_arm
<big_endian
>::do_attributes_order(int num
) const
7829 // Reorder the known object attributes in output. We want to move
7830 // Tag_conformance to position 4 and Tag_conformance to position 5
7831 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7833 return elfcpp::Tag_conformance
;
7835 return elfcpp::Tag_nodefaults
;
7836 if ((num
- 2) < elfcpp::Tag_nodefaults
)
7838 if ((num
- 1) < elfcpp::Tag_conformance
)
7843 // Scan a span of THUMB code for Cortex-A8 erratum.
7845 template<bool big_endian
>
7847 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
7848 Arm_relobj
<big_endian
>* arm_relobj
,
7850 section_size_type span_start
,
7851 section_size_type span_end
,
7852 const unsigned char* view
,
7853 Arm_address address
)
7855 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
7857 // The opcode is BLX.W, BL.W, B.W, Bcc.W
7858 // The branch target is in the same 4KB region as the
7859 // first half of the branch.
7860 // The instruction before the branch is a 32-bit
7861 // length non-branch instruction.
7862 section_size_type i
= span_start
;
7863 bool last_was_32bit
= false;
7864 bool last_was_branch
= false;
7865 while (i
< span_end
)
7867 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
7868 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
7869 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
7870 bool is_blx
= false, is_b
= false;
7871 bool is_bl
= false, is_bcc
= false;
7873 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
7876 // Load the rest of the insn (in manual-friendly order).
7877 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
7879 // Encoding T4: B<c>.W.
7880 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
7881 // Encoding T1: BL<c>.W.
7882 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
7883 // Encoding T2: BLX<c>.W.
7884 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
7885 // Encoding T3: B<c>.W (not permitted in IT block).
7886 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
7887 && (insn
& 0x07f00000U
) != 0x03800000U
);
7890 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
7892 // If this instruction is a 32-bit THUMB branch that crosses a 4K
7893 // page boundary and it follows 32-bit non-branch instruction,
7894 // we need to work around.
7896 && ((address
+ i
) & 0xfffU
) == 0xffeU
7898 && !last_was_branch
)
7900 // Check to see if there is a relocation stub for this branch.
7901 bool force_target_arm
= false;
7902 bool force_target_thumb
= false;
7903 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
7904 Cortex_a8_relocs_info::const_iterator p
=
7905 this->cortex_a8_relocs_info_
.find(address
+ i
);
7907 if (p
!= this->cortex_a8_relocs_info_
.end())
7909 cortex_a8_reloc
= p
->second
;
7910 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
7912 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
7913 && !target_is_thumb
)
7914 force_target_arm
= true;
7915 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
7917 force_target_thumb
= true;
7921 Stub_type stub_type
= arm_stub_none
;
7923 // Check if we have an offending branch instruction.
7924 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
7925 uint16_t lower_insn
= insn
& 0xffffU
;
7926 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
7928 if (cortex_a8_reloc
!= NULL
7929 && cortex_a8_reloc
->reloc_stub() != NULL
)
7930 // We've already made a stub for this instruction, e.g.
7931 // it's a long branch or a Thumb->ARM stub. Assume that
7932 // stub will suffice to work around the A8 erratum (see
7933 // setting of always_after_branch above).
7937 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
7939 stub_type
= arm_stub_a8_veneer_b_cond
;
7941 else if (is_b
|| is_bl
|| is_blx
)
7943 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
7949 ? arm_stub_a8_veneer_blx
7951 ? arm_stub_a8_veneer_bl
7952 : arm_stub_a8_veneer_b
));
7955 if (stub_type
!= arm_stub_none
)
7957 Arm_address pc_for_insn
= address
+ i
+ 4;
7959 // The original instruction is a BL, but the target is
7960 // an ARM instruction. If we were not making a stub,
7961 // the BL would have been converted to a BLX. Use the
7962 // BLX stub instead in that case.
7963 if (this->may_use_blx() && force_target_arm
7964 && stub_type
== arm_stub_a8_veneer_bl
)
7966 stub_type
= arm_stub_a8_veneer_blx
;
7970 // Conversely, if the original instruction was
7971 // BLX but the target is Thumb mode, use the BL stub.
7972 else if (force_target_thumb
7973 && stub_type
== arm_stub_a8_veneer_blx
)
7975 stub_type
= arm_stub_a8_veneer_bl
;
7983 // If we found a relocation, use the proper destination,
7984 // not the offset in the (unrelocated) instruction.
7985 // Note this is always done if we switched the stub type above.
7986 if (cortex_a8_reloc
!= NULL
)
7987 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
7989 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
7991 // Add a new stub if destination address in in the same page.
7992 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
7994 Cortex_a8_stub
* stub
=
7995 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
7999 Stub_table
<big_endian
>* stub_table
=
8000 arm_relobj
->stub_table(shndx
);
8001 gold_assert(stub_table
!= NULL
);
8002 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
8007 i
+= insn_32bit
? 4 : 2;
8008 last_was_32bit
= insn_32bit
;
8009 last_was_branch
= is_32bit_branch
;
8013 // Apply the Cortex-A8 workaround.
8015 template<bool big_endian
>
8017 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
8018 const Cortex_a8_stub
* stub
,
8019 Arm_address stub_address
,
8020 unsigned char* insn_view
,
8021 Arm_address insn_address
)
8023 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8024 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
8025 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8026 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8027 off_t branch_offset
= stub_address
- (insn_address
+ 4);
8029 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8030 switch (stub
->stub_template()->type())
8032 case arm_stub_a8_veneer_b_cond
:
8033 gold_assert(!utils::has_overflow
<21>(branch_offset
));
8034 upper_insn
= RelocFuncs::thumb32_cond_branch_upper(upper_insn
,
8036 lower_insn
= RelocFuncs::thumb32_cond_branch_lower(lower_insn
,
8040 case arm_stub_a8_veneer_b
:
8041 case arm_stub_a8_veneer_bl
:
8042 case arm_stub_a8_veneer_blx
:
8043 if ((lower_insn
& 0x5000U
) == 0x4000U
)
8044 // For a BLX instruction, make sure that the relocation is
8045 // rounded up to a word boundary. This follows the semantics of
8046 // the instruction which specifies that bit 1 of the target
8047 // address will come from bit 1 of the base address.
8048 branch_offset
= (branch_offset
+ 2) & ~3;
8050 // Put BRANCH_OFFSET back into the insn.
8051 gold_assert(!utils::has_overflow
<25>(branch_offset
));
8052 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
8053 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
8060 // Put the relocated value back in the object file:
8061 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
8062 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
8065 template<bool big_endian
>
8066 class Target_selector_arm
: public Target_selector
8069 Target_selector_arm()
8070 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
8071 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
8075 do_instantiate_target()
8076 { return new Target_arm
<big_endian
>(); }
8079 Target_selector_arm
<false> target_selector_arm
;
8080 Target_selector_arm
<true> target_selector_armbe
;
8082 } // End anonymous namespace.