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.
35 #include "parameters.h"
42 #include "copy-relocs.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
49 #include "attributes.h"
56 template<bool big_endian
>
57 class Output_data_plt_arm
;
59 template<bool big_endian
>
62 template<bool big_endian
>
63 class Arm_input_section
;
65 template<bool big_endian
>
66 class Arm_output_section
;
68 template<bool big_endian
>
71 template<bool big_endian
>
75 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
77 // Maximum branch offsets for ARM, THUMB and THUMB2.
78 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
79 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
80 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
81 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
82 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
83 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
85 // The arm target class.
87 // This is a very simple port of gold for ARM-EABI. It is intended for
88 // supporting Android only for the time being. Only these relocation types
117 // R_ARM_THM_MOVW_ABS_NC
118 // R_ARM_THM_MOVT_ABS
119 // R_ARM_MOVW_PREL_NC
121 // R_ARM_THM_MOVW_PREL_NC
122 // R_ARM_THM_MOVT_PREL
125 // - Support more relocation types as needed.
126 // - Make PLTs more flexible for different architecture features like
128 // There are probably a lot more.
130 // Instruction template class. This class is similar to the insn_sequence
131 // struct in bfd/elf32-arm.c.
136 // Types of instruction templates.
140 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
141 // templates with class-specific semantics. Currently this is used
142 // only by the Cortex_a8_stub class for handling condition codes in
143 // conditional branches.
144 THUMB16_SPECIAL_TYPE
,
150 // Factory methods to create instruction templates in different formats.
152 static const Insn_template
153 thumb16_insn(uint32_t data
)
154 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
156 // A Thumb conditional branch, in which the proper condition is inserted
157 // when we build the stub.
158 static const Insn_template
159 thumb16_bcond_insn(uint32_t data
)
160 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
162 static const Insn_template
163 thumb32_insn(uint32_t data
)
164 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
166 static const Insn_template
167 thumb32_b_insn(uint32_t data
, int reloc_addend
)
169 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
173 static const Insn_template
174 arm_insn(uint32_t data
)
175 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
177 static const Insn_template
178 arm_rel_insn(unsigned data
, int reloc_addend
)
179 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
181 static const Insn_template
182 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
183 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
185 // Accessors. This class is used for read-only objects so no modifiers
190 { return this->data_
; }
192 // Return the instruction sequence type of this.
195 { return this->type_
; }
197 // Return the ARM relocation type of this.
200 { return this->r_type_
; }
204 { return this->reloc_addend_
; }
206 // Return size of instruction template in bytes.
210 // Return byte-alignment of instruction template.
215 // We make the constructor private to ensure that only the factory
218 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
219 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
222 // Instruction specific data. This is used to store information like
223 // some of the instruction bits.
225 // Instruction template type.
227 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
228 unsigned int r_type_
;
229 // Relocation addend.
230 int32_t reloc_addend_
;
233 // Macro for generating code to stub types. One entry per long/short
237 DEF_STUB(long_branch_any_any) \
238 DEF_STUB(long_branch_v4t_arm_thumb) \
239 DEF_STUB(long_branch_thumb_only) \
240 DEF_STUB(long_branch_v4t_thumb_thumb) \
241 DEF_STUB(long_branch_v4t_thumb_arm) \
242 DEF_STUB(short_branch_v4t_thumb_arm) \
243 DEF_STUB(long_branch_any_arm_pic) \
244 DEF_STUB(long_branch_any_thumb_pic) \
245 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
246 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
247 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
248 DEF_STUB(long_branch_thumb_only_pic) \
249 DEF_STUB(a8_veneer_b_cond) \
250 DEF_STUB(a8_veneer_b) \
251 DEF_STUB(a8_veneer_bl) \
252 DEF_STUB(a8_veneer_blx)
256 #define DEF_STUB(x) arm_stub_##x,
262 // First reloc stub type.
263 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
264 // Last reloc stub type.
265 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
267 // First Cortex-A8 stub type.
268 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
269 // Last Cortex-A8 stub type.
270 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
273 arm_stub_type_last
= arm_stub_a8_veneer_blx
277 // Stub template class. Templates are meant to be read-only objects.
278 // A stub template for a stub type contains all read-only attributes
279 // common to all stubs of the same type.
284 Stub_template(Stub_type
, const Insn_template
*, size_t);
292 { return this->type_
; }
294 // Return an array of instruction templates.
297 { return this->insns_
; }
299 // Return size of template in number of instructions.
302 { return this->insn_count_
; }
304 // Return size of template in bytes.
307 { return this->size_
; }
309 // Return alignment of the stub template.
312 { return this->alignment_
; }
314 // Return whether entry point is in thumb mode.
316 entry_in_thumb_mode() const
317 { return this->entry_in_thumb_mode_
; }
319 // Return number of relocations in this template.
322 { return this->relocs_
.size(); }
324 // Return index of the I-th instruction with relocation.
326 reloc_insn_index(size_t i
) const
328 gold_assert(i
< this->relocs_
.size());
329 return this->relocs_
[i
].first
;
332 // Return the offset of the I-th instruction with relocation from the
333 // beginning of the stub.
335 reloc_offset(size_t i
) const
337 gold_assert(i
< this->relocs_
.size());
338 return this->relocs_
[i
].second
;
342 // This contains information about an instruction template with a relocation
343 // and its offset from start of stub.
344 typedef std::pair
<size_t, section_size_type
> Reloc
;
346 // A Stub_template may not be copied. We want to share templates as much
348 Stub_template(const Stub_template
&);
349 Stub_template
& operator=(const Stub_template
&);
353 // Points to an array of Insn_templates.
354 const Insn_template
* insns_
;
355 // Number of Insn_templates in insns_[].
357 // Size of templated instructions in bytes.
359 // Alignment of templated instructions.
361 // Flag to indicate if entry is in thumb mode.
362 bool entry_in_thumb_mode_
;
363 // A table of reloc instruction indices and offsets. We can find these by
364 // looking at the instruction templates but we pre-compute and then stash
365 // them here for speed.
366 std::vector
<Reloc
> relocs_
;
370 // A class for code stubs. This is a base class for different type of
371 // stubs used in the ARM target.
377 static const section_offset_type invalid_offset
=
378 static_cast<section_offset_type
>(-1);
381 Stub(const Stub_template
* stub_template
)
382 : stub_template_(stub_template
), offset_(invalid_offset
)
389 // Return the stub template.
391 stub_template() const
392 { return this->stub_template_
; }
394 // Return offset of code stub from beginning of its containing stub table.
398 gold_assert(this->offset_
!= invalid_offset
);
399 return this->offset_
;
402 // Set offset of code stub from beginning of its containing stub table.
404 set_offset(section_offset_type offset
)
405 { this->offset_
= offset
; }
407 // Return the relocation target address of the i-th relocation in the
408 // stub. This must be defined in a child class.
410 reloc_target(size_t i
)
411 { return this->do_reloc_target(i
); }
413 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
415 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
416 { this->do_write(view
, view_size
, big_endian
); }
418 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
419 // for the i-th instruction.
421 thumb16_special(size_t i
)
422 { return this->do_thumb16_special(i
); }
425 // This must be defined in the child class.
427 do_reloc_target(size_t) = 0;
429 // This may be overridden in the child class.
431 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
434 this->do_fixed_endian_write
<true>(view
, view_size
);
436 this->do_fixed_endian_write
<false>(view
, view_size
);
439 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
440 // instruction template.
442 do_thumb16_special(size_t)
443 { gold_unreachable(); }
446 // A template to implement do_write.
447 template<bool big_endian
>
449 do_fixed_endian_write(unsigned char*, section_size_type
);
452 const Stub_template
* stub_template_
;
453 // Offset within the section of containing this stub.
454 section_offset_type offset_
;
457 // Reloc stub class. These are stubs we use to fix up relocation because
458 // of limited branch ranges.
460 class Reloc_stub
: public Stub
463 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
464 // We assume we never jump to this address.
465 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
467 // Return destination address.
469 destination_address() const
471 gold_assert(this->destination_address_
!= this->invalid_address
);
472 return this->destination_address_
;
475 // Set destination address.
477 set_destination_address(Arm_address address
)
479 gold_assert(address
!= this->invalid_address
);
480 this->destination_address_
= address
;
483 // Reset destination address.
485 reset_destination_address()
486 { this->destination_address_
= this->invalid_address
; }
488 // Determine stub type for a branch of a relocation of R_TYPE going
489 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
490 // the branch target is a thumb instruction. TARGET is used for look
491 // up ARM-specific linker settings.
493 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
494 Arm_address branch_target
, bool target_is_thumb
);
496 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
497 // and an addend. Since we treat global and local symbol differently, we
498 // use a Symbol object for a global symbol and a object-index pair for
503 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
504 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
505 // and R_SYM must not be invalid_index.
506 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
507 unsigned int r_sym
, int32_t addend
)
508 : stub_type_(stub_type
), addend_(addend
)
512 this->r_sym_
= Reloc_stub::invalid_index
;
513 this->u_
.symbol
= symbol
;
517 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
518 this->r_sym_
= r_sym
;
519 this->u_
.relobj
= relobj
;
526 // Accessors: Keys are meant to be read-only object so no modifiers are
532 { return this->stub_type_
; }
534 // Return the local symbol index or invalid_index.
537 { return this->r_sym_
; }
539 // Return the symbol if there is one.
542 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
544 // Return the relobj if there is one.
547 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
549 // Whether this equals to another key k.
551 eq(const Key
& k
) const
553 return ((this->stub_type_
== k
.stub_type_
)
554 && (this->r_sym_
== k
.r_sym_
)
555 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
556 ? (this->u_
.relobj
== k
.u_
.relobj
)
557 : (this->u_
.symbol
== k
.u_
.symbol
))
558 && (this->addend_
== k
.addend_
));
561 // Return a hash value.
565 return (this->stub_type_
567 ^ gold::string_hash
<char>(
568 (this->r_sym_
!= Reloc_stub::invalid_index
)
569 ? this->u_
.relobj
->name().c_str()
570 : this->u_
.symbol
->name())
574 // Functors for STL associative containers.
578 operator()(const Key
& k
) const
579 { return k
.hash_value(); }
585 operator()(const Key
& k1
, const Key
& k2
) const
586 { return k1
.eq(k2
); }
589 // Name of key. This is mainly for debugging.
595 Stub_type stub_type_
;
596 // If this is a local symbol, this is the index in the defining object.
597 // Otherwise, it is invalid_index for a global symbol.
599 // If r_sym_ is invalid index. This points to a global symbol.
600 // Otherwise, this points a relobj. We used the unsized and target
601 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
602 // Arm_relobj. This is done to avoid making the stub class a template
603 // as most of the stub machinery is endianity-neutral. However, it
604 // may require a bit of casting done by users of this class.
607 const Symbol
* symbol
;
608 const Relobj
* relobj
;
610 // Addend associated with a reloc.
615 // Reloc_stubs are created via a stub factory. So these are protected.
616 Reloc_stub(const Stub_template
* stub_template
)
617 : Stub(stub_template
), destination_address_(invalid_address
)
623 friend class Stub_factory
;
625 // Return the relocation target address of the i-th relocation in the
628 do_reloc_target(size_t i
)
630 // All reloc stub have only one relocation.
632 return this->destination_address_
;
636 // Address of destination.
637 Arm_address destination_address_
;
640 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
641 // THUMB branch that meets the following conditions:
643 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
644 // branch address is 0xffe.
645 // 2. The branch target address is in the same page as the first word of the
647 // 3. The branch follows a 32-bit instruction which is not a branch.
649 // To do the fix up, we need to store the address of the branch instruction
650 // and its target at least. We also need to store the original branch
651 // instruction bits for the condition code in a conditional branch. The
652 // condition code is used in a special instruction template. We also want
653 // to identify input sections needing Cortex-A8 workaround quickly. We store
654 // extra information about object and section index of the code section
655 // containing a branch being fixed up. The information is used to mark
656 // the code section when we finalize the Cortex-A8 stubs.
659 class Cortex_a8_stub
: public Stub
665 // Return the object of the code section containing the branch being fixed
669 { return this->relobj_
; }
671 // Return the section index of the code section containing the branch being
675 { return this->shndx_
; }
677 // Return the source address of stub. This is the address of the original
678 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
681 source_address() const
682 { return this->source_address_
; }
684 // Return the destination address of the stub. This is the branch taken
685 // address of the original branch instruction. LSB is 1 if it is a THUMB
686 // instruction address.
688 destination_address() const
689 { return this->destination_address_
; }
691 // Return the instruction being fixed up.
693 original_insn() const
694 { return this->original_insn_
; }
697 // Cortex_a8_stubs are created via a stub factory. So these are protected.
698 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
699 unsigned int shndx
, Arm_address source_address
,
700 Arm_address destination_address
, uint32_t original_insn
)
701 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
702 source_address_(source_address
| 1U),
703 destination_address_(destination_address
),
704 original_insn_(original_insn
)
707 friend class Stub_factory
;
709 // Return the relocation target address of the i-th relocation in the
712 do_reloc_target(size_t i
)
714 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
716 // The conditional branch veneer has two relocations.
718 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
722 // All other Cortex-A8 stubs have only one relocation.
724 return this->destination_address_
;
728 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
730 do_thumb16_special(size_t);
733 // Object of the code section containing the branch being fixed up.
735 // Section index of the code section containing the branch begin fixed up.
737 // Source address of original branch.
738 Arm_address source_address_
;
739 // Destination address of the original branch.
740 Arm_address destination_address_
;
741 // Original branch instruction. This is needed for copying the condition
742 // code from a condition branch to its stub.
743 uint32_t original_insn_
;
746 // Stub factory class.
751 // Return the unique instance of this class.
752 static const Stub_factory
&
755 static Stub_factory singleton
;
759 // Make a relocation stub.
761 make_reloc_stub(Stub_type stub_type
) const
763 gold_assert(stub_type
>= arm_stub_reloc_first
764 && stub_type
<= arm_stub_reloc_last
);
765 return new Reloc_stub(this->stub_templates_
[stub_type
]);
768 // Make a Cortex-A8 stub.
770 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
771 Arm_address source
, Arm_address destination
,
772 uint32_t original_insn
) const
774 gold_assert(stub_type
>= arm_stub_cortex_a8_first
775 && stub_type
<= arm_stub_cortex_a8_last
);
776 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
777 source
, destination
, original_insn
);
781 // Constructor and destructor are protected since we only return a single
782 // instance created in Stub_factory::get_instance().
786 // A Stub_factory may not be copied since it is a singleton.
787 Stub_factory(const Stub_factory
&);
788 Stub_factory
& operator=(Stub_factory
&);
790 // Stub templates. These are initialized in the constructor.
791 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
794 // A class to hold stubs for the ARM target.
796 template<bool big_endian
>
797 class Stub_table
: public Output_data
800 Stub_table(Arm_input_section
<big_endian
>* owner
)
801 : Output_data(), owner_(owner
), reloc_stubs_(), cortex_a8_stubs_(),
802 prev_data_size_(0), prev_addralign_(1)
808 // Owner of this stub table.
809 Arm_input_section
<big_endian
>*
811 { return this->owner_
; }
813 // Whether this stub table is empty.
816 { return this->reloc_stubs_
.empty() && this->cortex_a8_stubs_
.empty(); }
818 // Return the current data size.
820 current_data_size() const
821 { return this->current_data_size_for_child(); }
823 // Add a STUB with using KEY. Caller is reponsible for avoid adding
824 // if already a STUB with the same key has been added.
826 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
828 const Stub_template
* stub_template
= stub
->stub_template();
829 gold_assert(stub_template
->type() == key
.stub_type());
830 this->reloc_stubs_
[key
] = stub
;
833 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
834 // Caller is reponsible for avoid adding if already a STUB with the same
835 // address has been added.
837 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
839 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
840 this->cortex_a8_stubs_
.insert(value
);
843 // Remove all Cortex-A8 stubs.
845 remove_all_cortex_a8_stubs();
847 // Look up a relocation stub using KEY. Return NULL if there is none.
849 find_reloc_stub(const Reloc_stub::Key
& key
) const
851 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
852 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
855 // Relocate stubs in this stub table.
857 relocate_stubs(const Relocate_info
<32, big_endian
>*,
858 Target_arm
<big_endian
>*, Output_section
*,
859 unsigned char*, Arm_address
, section_size_type
);
861 // Update data size and alignment at the end of a relaxation pass. Return
862 // true if either data size or alignment is different from that of the
863 // previous relaxation pass.
865 update_data_size_and_addralign();
867 // Finalize stubs. Set the offsets of all stubs and mark input sections
868 // needing the Cortex-A8 workaround.
872 // Apply Cortex-A8 workaround to an address range.
874 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
875 unsigned char*, Arm_address
,
879 // Write out section contents.
881 do_write(Output_file
*);
883 // Return the required alignment.
886 { return this->prev_addralign_
; }
888 // Reset address and file offset.
890 do_reset_address_and_file_offset()
891 { this->set_current_data_size_for_child(this->prev_data_size_
); }
893 // Set final data size.
895 set_final_data_size()
896 { this->set_data_size(this->current_data_size()); }
899 // Relocate one stub.
901 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
902 Target_arm
<big_endian
>*, Output_section
*,
903 unsigned char*, Arm_address
, section_size_type
);
905 // Unordered map of relocation stubs.
907 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
908 Reloc_stub::Key::equal_to
>
911 // List of Cortex-A8 stubs ordered by addresses of branches being
912 // fixed up in output.
913 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
915 // Owner of this stub table.
916 Arm_input_section
<big_endian
>* owner_
;
917 // The relocation stubs.
918 Reloc_stub_map reloc_stubs_
;
919 // The cortex_a8_stubs.
920 Cortex_a8_stub_list cortex_a8_stubs_
;
921 // data size of this in the previous pass.
922 off_t prev_data_size_
;
923 // address alignment of this in the previous pass.
924 uint64_t prev_addralign_
;
927 // A class to wrap an ordinary input section containing executable code.
929 template<bool big_endian
>
930 class Arm_input_section
: public Output_relaxed_input_section
933 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
934 : Output_relaxed_input_section(relobj
, shndx
, 1),
935 original_addralign_(1), original_size_(0), stub_table_(NULL
)
945 // Whether this is a stub table owner.
947 is_stub_table_owner() const
948 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
950 // Return the stub table.
951 Stub_table
<big_endian
>*
953 { return this->stub_table_
; }
955 // Set the stub_table.
957 set_stub_table(Stub_table
<big_endian
>* stub_table
)
958 { this->stub_table_
= stub_table
; }
960 // Downcast a base pointer to an Arm_input_section pointer. This is
961 // not type-safe but we only use Arm_input_section not the base class.
962 static Arm_input_section
<big_endian
>*
963 as_arm_input_section(Output_relaxed_input_section
* poris
)
964 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
967 // Write data to output file.
969 do_write(Output_file
*);
971 // Return required alignment of this.
975 if (this->is_stub_table_owner())
976 return std::max(this->stub_table_
->addralign(),
977 this->original_addralign_
);
979 return this->original_addralign_
;
982 // Finalize data size.
984 set_final_data_size();
986 // Reset address and file offset.
988 do_reset_address_and_file_offset();
992 do_output_offset(const Relobj
* object
, unsigned int shndx
,
993 section_offset_type offset
,
994 section_offset_type
* poutput
) const
996 if ((object
== this->relobj())
997 && (shndx
== this->shndx())
999 && (convert_types
<uint64_t, section_offset_type
>(offset
)
1000 <= this->original_size_
))
1010 // Copying is not allowed.
1011 Arm_input_section(const Arm_input_section
&);
1012 Arm_input_section
& operator=(const Arm_input_section
&);
1014 // Address alignment of the original input section.
1015 uint64_t original_addralign_
;
1016 // Section size of the original input section.
1017 uint64_t original_size_
;
1019 Stub_table
<big_endian
>* stub_table_
;
1022 // Arm output section class. This is defined mainly to add a number of
1023 // stub generation methods.
1025 template<bool big_endian
>
1026 class Arm_output_section
: public Output_section
1029 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1030 elfcpp::Elf_Xword flags
)
1031 : Output_section(name
, type
, flags
)
1034 ~Arm_output_section()
1037 // Group input sections for stub generation.
1039 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1041 // Downcast a base pointer to an Arm_output_section pointer. This is
1042 // not type-safe but we only use Arm_output_section not the base class.
1043 static Arm_output_section
<big_endian
>*
1044 as_arm_output_section(Output_section
* os
)
1045 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1049 typedef Output_section::Input_section Input_section
;
1050 typedef Output_section::Input_section_list Input_section_list
;
1052 // Create a stub group.
1053 void create_stub_group(Input_section_list::const_iterator
,
1054 Input_section_list::const_iterator
,
1055 Input_section_list::const_iterator
,
1056 Target_arm
<big_endian
>*,
1057 std::vector
<Output_relaxed_input_section
*>*);
1060 // Arm_relobj class.
1062 template<bool big_endian
>
1063 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1066 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1068 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1069 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1070 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1071 stub_tables_(), local_symbol_is_thumb_function_(),
1072 attributes_section_data_(NULL
), mapping_symbols_info_(),
1073 section_has_cortex_a8_workaround_(NULL
)
1077 { delete this->attributes_section_data_
; }
1079 // Return the stub table of the SHNDX-th section if there is one.
1080 Stub_table
<big_endian
>*
1081 stub_table(unsigned int shndx
) const
1083 gold_assert(shndx
< this->stub_tables_
.size());
1084 return this->stub_tables_
[shndx
];
1087 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1089 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1091 gold_assert(shndx
< this->stub_tables_
.size());
1092 this->stub_tables_
[shndx
] = stub_table
;
1095 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1096 // index. This is only valid after do_count_local_symbol is called.
1098 local_symbol_is_thumb_function(unsigned int r_sym
) const
1100 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1101 return this->local_symbol_is_thumb_function_
[r_sym
];
1104 // Scan all relocation sections for stub generation.
1106 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1109 // Convert regular input section with index SHNDX to a relaxed section.
1111 convert_input_section_to_relaxed_section(unsigned shndx
)
1113 // The stubs have relocations and we need to process them after writing
1114 // out the stubs. So relocation now must follow section write.
1115 this->invalidate_section_offset(shndx
);
1116 this->set_relocs_must_follow_section_writes();
1119 // Downcast a base pointer to an Arm_relobj pointer. This is
1120 // not type-safe but we only use Arm_relobj not the base class.
1121 static Arm_relobj
<big_endian
>*
1122 as_arm_relobj(Relobj
* relobj
)
1123 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1125 // Processor-specific flags in ELF file header. This is valid only after
1128 processor_specific_flags() const
1129 { return this->processor_specific_flags_
; }
1131 // Attribute section data This is the contents of the .ARM.attribute section
1133 const Attributes_section_data
*
1134 attributes_section_data() const
1135 { return this->attributes_section_data_
; }
1137 // Mapping symbol location.
1138 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1140 // Functor for STL container.
1141 struct Mapping_symbol_position_less
1144 operator()(const Mapping_symbol_position
& p1
,
1145 const Mapping_symbol_position
& p2
) const
1147 return (p1
.first
< p2
.first
1148 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1152 // We only care about the first character of a mapping symbol, so
1153 // we only store that instead of the whole symbol name.
1154 typedef std::map
<Mapping_symbol_position
, char,
1155 Mapping_symbol_position_less
> Mapping_symbols_info
;
1157 // Whether a section contains any Cortex-A8 workaround.
1159 section_has_cortex_a8_workaround(unsigned int shndx
) const
1161 return (this->section_has_cortex_a8_workaround_
!= NULL
1162 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1165 // Mark a section that has Cortex-A8 workaround.
1167 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1169 if (this->section_has_cortex_a8_workaround_
== NULL
)
1170 this->section_has_cortex_a8_workaround_
=
1171 new std::vector
<bool>(this->shnum(), false);
1172 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1176 // Post constructor setup.
1180 // Call parent's setup method.
1181 Sized_relobj
<32, big_endian
>::do_setup();
1183 // Initialize look-up tables.
1184 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1185 this->stub_tables_
.swap(empty_stub_table_list
);
1188 // Count the local symbols.
1190 do_count_local_symbols(Stringpool_template
<char>*,
1191 Stringpool_template
<char>*);
1194 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1195 const unsigned char* pshdrs
,
1196 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1198 // Read the symbol information.
1200 do_read_symbols(Read_symbols_data
* sd
);
1202 // Process relocs for garbage collection.
1204 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1207 // List of stub tables.
1208 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1209 Stub_table_list stub_tables_
;
1210 // Bit vector to tell if a local symbol is a thumb function or not.
1211 // This is only valid after do_count_local_symbol is called.
1212 std::vector
<bool> local_symbol_is_thumb_function_
;
1213 // processor-specific flags in ELF file header.
1214 elfcpp::Elf_Word processor_specific_flags_
;
1215 // Object attributes if there is an .ARM.attributes section or NULL.
1216 Attributes_section_data
* attributes_section_data_
;
1217 // Mapping symbols information.
1218 Mapping_symbols_info mapping_symbols_info_
;
1219 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1220 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1223 // Arm_dynobj class.
1225 template<bool big_endian
>
1226 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1229 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1230 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1231 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1232 processor_specific_flags_(0), attributes_section_data_(NULL
)
1236 { delete this->attributes_section_data_
; }
1238 // Downcast a base pointer to an Arm_relobj pointer. This is
1239 // not type-safe but we only use Arm_relobj not the base class.
1240 static Arm_dynobj
<big_endian
>*
1241 as_arm_dynobj(Dynobj
* dynobj
)
1242 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1244 // Processor-specific flags in ELF file header. This is valid only after
1247 processor_specific_flags() const
1248 { return this->processor_specific_flags_
; }
1250 // Attributes section data.
1251 const Attributes_section_data
*
1252 attributes_section_data() const
1253 { return this->attributes_section_data_
; }
1256 // Read the symbol information.
1258 do_read_symbols(Read_symbols_data
* sd
);
1261 // processor-specific flags in ELF file header.
1262 elfcpp::Elf_Word processor_specific_flags_
;
1263 // Object attributes if there is an .ARM.attributes section or NULL.
1264 Attributes_section_data
* attributes_section_data_
;
1267 // Functor to read reloc addends during stub generation.
1269 template<int sh_type
, bool big_endian
>
1270 struct Stub_addend_reader
1272 // Return the addend for a relocation of a particular type. Depending
1273 // on whether this is a REL or RELA relocation, read the addend from a
1274 // view or from a Reloc object.
1275 elfcpp::Elf_types
<32>::Elf_Swxword
1277 unsigned int /* r_type */,
1278 const unsigned char* /* view */,
1279 const typename Reloc_types
<sh_type
,
1280 32, big_endian
>::Reloc
& /* reloc */) const;
1283 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1285 template<bool big_endian
>
1286 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1288 elfcpp::Elf_types
<32>::Elf_Swxword
1291 const unsigned char*,
1292 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1295 // Specialized Stub_addend_reader for RELA type relocation sections.
1296 // We currently do not handle RELA type relocation sections but it is trivial
1297 // to implement the addend reader. This is provided for completeness and to
1298 // make it easier to add support for RELA relocation sections in the future.
1300 template<bool big_endian
>
1301 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1303 elfcpp::Elf_types
<32>::Elf_Swxword
1306 const unsigned char*,
1307 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1308 big_endian
>::Reloc
& reloc
) const
1309 { return reloc
.get_r_addend(); }
1312 // Cortex_a8_reloc class. We keep record of relocation that may need
1313 // the Cortex-A8 erratum workaround.
1315 class Cortex_a8_reloc
1318 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1319 Arm_address destination
)
1320 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1326 // Accessors: This is a read-only class.
1328 // Return the relocation stub associated with this relocation if there is
1332 { return this->reloc_stub_
; }
1334 // Return the relocation type.
1337 { return this->r_type_
; }
1339 // Return the destination address of the relocation. LSB stores the THUMB
1343 { return this->destination_
; }
1346 // Associated relocation stub if there is one, or NULL.
1347 const Reloc_stub
* reloc_stub_
;
1349 unsigned int r_type_
;
1350 // Destination address of this relocation. LSB is used to distinguish
1352 Arm_address destination_
;
1355 // Utilities for manipulating integers of up to 32-bits
1359 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1360 // an int32_t. NO_BITS must be between 1 to 32.
1361 template<int no_bits
>
1362 static inline int32_t
1363 sign_extend(uint32_t bits
)
1365 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1367 return static_cast<int32_t>(bits
);
1368 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1370 uint32_t top_bit
= 1U << (no_bits
- 1);
1371 int32_t as_signed
= static_cast<int32_t>(bits
);
1372 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1375 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1376 template<int no_bits
>
1378 has_overflow(uint32_t bits
)
1380 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1383 int32_t max
= (1 << (no_bits
- 1)) - 1;
1384 int32_t min
= -(1 << (no_bits
- 1));
1385 int32_t as_signed
= static_cast<int32_t>(bits
);
1386 return as_signed
> max
|| as_signed
< min
;
1389 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1390 // fits in the given number of bits as either a signed or unsigned value.
1391 // For example, has_signed_unsigned_overflow<8> would check
1392 // -128 <= bits <= 255
1393 template<int no_bits
>
1395 has_signed_unsigned_overflow(uint32_t bits
)
1397 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1400 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1401 int32_t min
= -(1 << (no_bits
- 1));
1402 int32_t as_signed
= static_cast<int32_t>(bits
);
1403 return as_signed
> max
|| as_signed
< min
;
1406 // Select bits from A and B using bits in MASK. For each n in [0..31],
1407 // the n-th bit in the result is chosen from the n-th bits of A and B.
1408 // A zero selects A and a one selects B.
1409 static inline uint32_t
1410 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1411 { return (a
& ~mask
) | (b
& mask
); }
1414 template<bool big_endian
>
1415 class Target_arm
: public Sized_target
<32, big_endian
>
1418 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1421 // When were are relocating a stub, we pass this as the relocation number.
1422 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1425 : Sized_target
<32, big_endian
>(&arm_info
),
1426 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1427 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1428 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1429 should_force_pic_veneer_(false), arm_input_section_map_(),
1430 attributes_section_data_(NULL
), fix_cortex_a8_(false),
1431 cortex_a8_relocs_info_()
1434 // Whether we can use BLX.
1437 { return this->may_use_blx_
; }
1439 // Set use-BLX flag.
1441 set_may_use_blx(bool value
)
1442 { this->may_use_blx_
= value
; }
1444 // Whether we force PCI branch veneers.
1446 should_force_pic_veneer() const
1447 { return this->should_force_pic_veneer_
; }
1449 // Set PIC veneer flag.
1451 set_should_force_pic_veneer(bool value
)
1452 { this->should_force_pic_veneer_
= value
; }
1454 // Whether we use THUMB-2 instructions.
1456 using_thumb2() const
1458 Object_attribute
* attr
=
1459 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1460 int arch
= attr
->int_value();
1461 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1464 // Whether we use THUMB/THUMB-2 instructions only.
1466 using_thumb_only() const
1468 Object_attribute
* attr
=
1469 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1470 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1471 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1473 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1474 return attr
->int_value() == 'M';
1477 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1479 may_use_arm_nop() const
1481 Object_attribute
* attr
=
1482 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1483 int arch
= attr
->int_value();
1484 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1485 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1486 || arch
== elfcpp::TAG_CPU_ARCH_V7
1487 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1490 // Whether we have THUMB-2 NOP.W instruction.
1492 may_use_thumb2_nop() const
1494 Object_attribute
* attr
=
1495 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1496 int arch
= attr
->int_value();
1497 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1498 || arch
== elfcpp::TAG_CPU_ARCH_V7
1499 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1502 // Process the relocations to determine unreferenced sections for
1503 // garbage collection.
1505 gc_process_relocs(Symbol_table
* symtab
,
1507 Sized_relobj
<32, big_endian
>* object
,
1508 unsigned int data_shndx
,
1509 unsigned int sh_type
,
1510 const unsigned char* prelocs
,
1512 Output_section
* output_section
,
1513 bool needs_special_offset_handling
,
1514 size_t local_symbol_count
,
1515 const unsigned char* plocal_symbols
);
1517 // Scan the relocations to look for symbol adjustments.
1519 scan_relocs(Symbol_table
* symtab
,
1521 Sized_relobj
<32, big_endian
>* object
,
1522 unsigned int data_shndx
,
1523 unsigned int sh_type
,
1524 const unsigned char* prelocs
,
1526 Output_section
* output_section
,
1527 bool needs_special_offset_handling
,
1528 size_t local_symbol_count
,
1529 const unsigned char* plocal_symbols
);
1531 // Finalize the sections.
1533 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1535 // Return the value to use for a dynamic symbol which requires special
1538 do_dynsym_value(const Symbol
*) const;
1540 // Relocate a section.
1542 relocate_section(const Relocate_info
<32, big_endian
>*,
1543 unsigned int sh_type
,
1544 const unsigned char* prelocs
,
1546 Output_section
* output_section
,
1547 bool needs_special_offset_handling
,
1548 unsigned char* view
,
1549 Arm_address view_address
,
1550 section_size_type view_size
,
1551 const Reloc_symbol_changes
*);
1553 // Scan the relocs during a relocatable link.
1555 scan_relocatable_relocs(Symbol_table
* symtab
,
1557 Sized_relobj
<32, big_endian
>* object
,
1558 unsigned int data_shndx
,
1559 unsigned int sh_type
,
1560 const unsigned char* prelocs
,
1562 Output_section
* output_section
,
1563 bool needs_special_offset_handling
,
1564 size_t local_symbol_count
,
1565 const unsigned char* plocal_symbols
,
1566 Relocatable_relocs
*);
1568 // Relocate a section during a relocatable link.
1570 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1571 unsigned int sh_type
,
1572 const unsigned char* prelocs
,
1574 Output_section
* output_section
,
1575 off_t offset_in_output_section
,
1576 const Relocatable_relocs
*,
1577 unsigned char* view
,
1578 Arm_address view_address
,
1579 section_size_type view_size
,
1580 unsigned char* reloc_view
,
1581 section_size_type reloc_view_size
);
1583 // Return whether SYM is defined by the ABI.
1585 do_is_defined_by_abi(Symbol
* sym
) const
1586 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1588 // Return the size of the GOT section.
1592 gold_assert(this->got_
!= NULL
);
1593 return this->got_
->data_size();
1596 // Map platform-specific reloc types
1598 get_real_reloc_type (unsigned int r_type
);
1601 // Methods to support stub-generations.
1604 // Return the stub factory
1606 stub_factory() const
1607 { return this->stub_factory_
; }
1609 // Make a new Arm_input_section object.
1610 Arm_input_section
<big_endian
>*
1611 new_arm_input_section(Relobj
*, unsigned int);
1613 // Find the Arm_input_section object corresponding to the SHNDX-th input
1614 // section of RELOBJ.
1615 Arm_input_section
<big_endian
>*
1616 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1618 // Make a new Stub_table
1619 Stub_table
<big_endian
>*
1620 new_stub_table(Arm_input_section
<big_endian
>*);
1622 // Scan a section for stub generation.
1624 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1625 const unsigned char*, size_t, Output_section
*,
1626 bool, const unsigned char*, Arm_address
,
1631 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1632 Output_section
*, unsigned char*, Arm_address
,
1635 // Get the default ARM target.
1636 static Target_arm
<big_endian
>*
1639 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1640 && parameters
->target().is_big_endian() == big_endian
);
1641 return static_cast<Target_arm
<big_endian
>*>(
1642 parameters
->sized_target
<32, big_endian
>());
1645 // Whether relocation type uses LSB to distinguish THUMB addresses.
1647 reloc_uses_thumb_bit(unsigned int r_type
);
1649 // Whether NAME belongs to a mapping symbol.
1651 is_mapping_symbol_name(const char* name
)
1655 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
1656 && (name
[2] == '\0' || name
[2] == '.'));
1659 // Whether we work around the Cortex-A8 erratum.
1661 fix_cortex_a8() const
1662 { return this->fix_cortex_a8_
; }
1665 // Make an ELF object.
1667 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1668 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1671 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1672 const elfcpp::Ehdr
<32, !big_endian
>&)
1673 { gold_unreachable(); }
1676 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1677 const elfcpp::Ehdr
<64, false>&)
1678 { gold_unreachable(); }
1681 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1682 const elfcpp::Ehdr
<64, true>&)
1683 { gold_unreachable(); }
1685 // Make an output section.
1687 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1688 elfcpp::Elf_Xword flags
)
1689 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1692 do_adjust_elf_header(unsigned char* view
, int len
) const;
1694 // We only need to generate stubs, and hence perform relaxation if we are
1695 // not doing relocatable linking.
1697 do_may_relax() const
1698 { return !parameters
->options().relocatable(); }
1701 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1703 // Determine whether an object attribute tag takes an integer, a
1706 do_attribute_arg_type(int tag
) const;
1708 // Reorder tags during output.
1710 do_attributes_order(int num
) const;
1713 // The class which scans relocations.
1718 : issued_non_pic_error_(false)
1722 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1723 Sized_relobj
<32, big_endian
>* object
,
1724 unsigned int data_shndx
,
1725 Output_section
* output_section
,
1726 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1727 const elfcpp::Sym
<32, big_endian
>& lsym
);
1730 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1731 Sized_relobj
<32, big_endian
>* object
,
1732 unsigned int data_shndx
,
1733 Output_section
* output_section
,
1734 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1739 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1740 unsigned int r_type
);
1743 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1744 unsigned int r_type
, Symbol
*);
1747 check_non_pic(Relobj
*, unsigned int r_type
);
1749 // Almost identical to Symbol::needs_plt_entry except that it also
1750 // handles STT_ARM_TFUNC.
1752 symbol_needs_plt_entry(const Symbol
* sym
)
1754 // An undefined symbol from an executable does not need a PLT entry.
1755 if (sym
->is_undefined() && !parameters
->options().shared())
1758 return (!parameters
->doing_static_link()
1759 && (sym
->type() == elfcpp::STT_FUNC
1760 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1761 && (sym
->is_from_dynobj()
1762 || sym
->is_undefined()
1763 || sym
->is_preemptible()));
1766 // Whether we have issued an error about a non-PIC compilation.
1767 bool issued_non_pic_error_
;
1770 // The class which implements relocation.
1780 // Return whether the static relocation needs to be applied.
1782 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1785 Output_section
* output_section
);
1787 // Do a relocation. Return false if the caller should not issue
1788 // any warnings about this relocation.
1790 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1791 Output_section
*, size_t relnum
,
1792 const elfcpp::Rel
<32, big_endian
>&,
1793 unsigned int r_type
, const Sized_symbol
<32>*,
1794 const Symbol_value
<32>*,
1795 unsigned char*, Arm_address
,
1798 // Return whether we want to pass flag NON_PIC_REF for this
1799 // reloc. This means the relocation type accesses a symbol not via
1802 reloc_is_non_pic (unsigned int r_type
)
1806 // These relocation types reference GOT or PLT entries explicitly.
1807 case elfcpp::R_ARM_GOT_BREL
:
1808 case elfcpp::R_ARM_GOT_ABS
:
1809 case elfcpp::R_ARM_GOT_PREL
:
1810 case elfcpp::R_ARM_GOT_BREL12
:
1811 case elfcpp::R_ARM_PLT32_ABS
:
1812 case elfcpp::R_ARM_TLS_GD32
:
1813 case elfcpp::R_ARM_TLS_LDM32
:
1814 case elfcpp::R_ARM_TLS_IE32
:
1815 case elfcpp::R_ARM_TLS_IE12GP
:
1817 // These relocate types may use PLT entries.
1818 case elfcpp::R_ARM_CALL
:
1819 case elfcpp::R_ARM_THM_CALL
:
1820 case elfcpp::R_ARM_JUMP24
:
1821 case elfcpp::R_ARM_THM_JUMP24
:
1822 case elfcpp::R_ARM_THM_JUMP19
:
1823 case elfcpp::R_ARM_PLT32
:
1824 case elfcpp::R_ARM_THM_XPC22
:
1833 // A class which returns the size required for a relocation type,
1834 // used while scanning relocs during a relocatable link.
1835 class Relocatable_size_for_reloc
1839 get_size_for_reloc(unsigned int, Relobj
*);
1842 // Get the GOT section, creating it if necessary.
1843 Output_data_got
<32, big_endian
>*
1844 got_section(Symbol_table
*, Layout
*);
1846 // Get the GOT PLT section.
1848 got_plt_section() const
1850 gold_assert(this->got_plt_
!= NULL
);
1851 return this->got_plt_
;
1854 // Create a PLT entry for a global symbol.
1856 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1858 // Get the PLT section.
1859 const Output_data_plt_arm
<big_endian
>*
1862 gold_assert(this->plt_
!= NULL
);
1866 // Get the dynamic reloc section, creating it if necessary.
1868 rel_dyn_section(Layout
*);
1870 // Return true if the symbol may need a COPY relocation.
1871 // References from an executable object to non-function symbols
1872 // defined in a dynamic object may need a COPY relocation.
1874 may_need_copy_reloc(Symbol
* gsym
)
1876 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1877 && gsym
->may_need_copy_reloc());
1880 // Add a potential copy relocation.
1882 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1883 Sized_relobj
<32, big_endian
>* object
,
1884 unsigned int shndx
, Output_section
* output_section
,
1885 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1887 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1888 symtab
->get_sized_symbol
<32>(sym
),
1889 object
, shndx
, output_section
, reloc
,
1890 this->rel_dyn_section(layout
));
1893 // Whether two EABI versions are compatible.
1895 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1897 // Merge processor-specific flags from input object and those in the ELF
1898 // header of the output.
1900 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1902 // Get the secondary compatible architecture.
1904 get_secondary_compatible_arch(const Attributes_section_data
*);
1906 // Set the secondary compatible architecture.
1908 set_secondary_compatible_arch(Attributes_section_data
*, int);
1911 tag_cpu_arch_combine(const char*, int, int*, int, int);
1913 // Helper to print AEABI enum tag value.
1915 aeabi_enum_name(unsigned int);
1917 // Return string value for TAG_CPU_name.
1919 tag_cpu_name_value(unsigned int);
1921 // Merge object attributes from input object and those in the output.
1923 merge_object_attributes(const char*, const Attributes_section_data
*);
1925 // Helper to get an AEABI object attribute
1927 get_aeabi_object_attribute(int tag
) const
1929 Attributes_section_data
* pasd
= this->attributes_section_data_
;
1930 gold_assert(pasd
!= NULL
);
1931 Object_attribute
* attr
=
1932 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
1933 gold_assert(attr
!= NULL
);
1938 // Methods to support stub-generations.
1941 // Group input sections for stub generation.
1943 group_sections(Layout
*, section_size_type
, bool);
1945 // Scan a relocation for stub generation.
1947 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1948 const Sized_symbol
<32>*, unsigned int,
1949 const Symbol_value
<32>*,
1950 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1952 // Scan a relocation section for stub.
1953 template<int sh_type
>
1955 scan_reloc_section_for_stubs(
1956 const Relocate_info
<32, big_endian
>* relinfo
,
1957 const unsigned char* prelocs
,
1959 Output_section
* output_section
,
1960 bool needs_special_offset_handling
,
1961 const unsigned char* view
,
1962 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1965 // Information about this specific target which we pass to the
1966 // general Target structure.
1967 static const Target::Target_info arm_info
;
1969 // The types of GOT entries needed for this platform.
1972 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1975 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1977 // Map input section to Arm_input_section.
1978 typedef Unordered_map
<Input_section_specifier
,
1979 Arm_input_section
<big_endian
>*,
1980 Input_section_specifier::hash
,
1981 Input_section_specifier::equal_to
>
1982 Arm_input_section_map
;
1984 // Map output addresses to relocs for Cortex-A8 erratum.
1985 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
1986 Cortex_a8_relocs_info
;
1989 Output_data_got
<32, big_endian
>* got_
;
1991 Output_data_plt_arm
<big_endian
>* plt_
;
1992 // The GOT PLT section.
1993 Output_data_space
* got_plt_
;
1994 // The dynamic reloc section.
1995 Reloc_section
* rel_dyn_
;
1996 // Relocs saved to avoid a COPY reloc.
1997 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1998 // Space for variables copied with a COPY reloc.
1999 Output_data_space
* dynbss_
;
2000 // Vector of Stub_tables created.
2001 Stub_table_list stub_tables_
;
2003 const Stub_factory
&stub_factory_
;
2004 // Whether we can use BLX.
2006 // Whether we force PIC branch veneers.
2007 bool should_force_pic_veneer_
;
2008 // Map for locating Arm_input_sections.
2009 Arm_input_section_map arm_input_section_map_
;
2010 // Attributes section data in output.
2011 Attributes_section_data
* attributes_section_data_
;
2012 // Whether we want to fix code for Cortex-A8 erratum.
2013 bool fix_cortex_a8_
;
2014 // Map addresses to relocs for Cortex-A8 erratum.
2015 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2018 template<bool big_endian
>
2019 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2022 big_endian
, // is_big_endian
2023 elfcpp::EM_ARM
, // machine_code
2024 false, // has_make_symbol
2025 false, // has_resolve
2026 false, // has_code_fill
2027 true, // is_default_stack_executable
2029 "/usr/lib/libc.so.1", // dynamic_linker
2030 0x8000, // default_text_segment_address
2031 0x1000, // abi_pagesize (overridable by -z max-page-size)
2032 0x1000, // common_pagesize (overridable by -z common-page-size)
2033 elfcpp::SHN_UNDEF
, // small_common_shndx
2034 elfcpp::SHN_UNDEF
, // large_common_shndx
2035 0, // small_common_section_flags
2036 0, // large_common_section_flags
2037 ".ARM.attributes", // attributes_section
2038 "aeabi" // attributes_vendor
2041 // Arm relocate functions class
2044 template<bool big_endian
>
2045 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2050 STATUS_OKAY
, // No error during relocation.
2051 STATUS_OVERFLOW
, // Relocation oveflow.
2052 STATUS_BAD_RELOC
// Relocation cannot be applied.
2056 typedef Relocate_functions
<32, big_endian
> Base
;
2057 typedef Arm_relocate_functions
<big_endian
> This
;
2059 // Encoding of imm16 argument for movt and movw ARM instructions
2062 // imm16 := imm4 | imm12
2064 // 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
2065 // +-------+---------------+-------+-------+-----------------------+
2066 // | | |imm4 | |imm12 |
2067 // +-------+---------------+-------+-------+-----------------------+
2069 // Extract the relocation addend from VAL based on the ARM
2070 // instruction encoding described above.
2071 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2072 extract_arm_movw_movt_addend(
2073 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2075 // According to the Elf ABI for ARM Architecture the immediate
2076 // field is sign-extended to form the addend.
2077 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2080 // Insert X into VAL based on the ARM instruction encoding described
2082 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2083 insert_val_arm_movw_movt(
2084 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2085 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2089 val
|= (x
& 0xf000) << 4;
2093 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2096 // imm16 := imm4 | i | imm3 | imm8
2098 // 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
2099 // +---------+-+-----------+-------++-+-----+-------+---------------+
2100 // | |i| |imm4 || |imm3 | |imm8 |
2101 // +---------+-+-----------+-------++-+-----+-------+---------------+
2103 // Extract the relocation addend from VAL based on the Thumb2
2104 // instruction encoding described above.
2105 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2106 extract_thumb_movw_movt_addend(
2107 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2109 // According to the Elf ABI for ARM Architecture the immediate
2110 // field is sign-extended to form the addend.
2111 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2112 | ((val
>> 15) & 0x0800)
2113 | ((val
>> 4) & 0x0700)
2117 // Insert X into VAL based on the Thumb2 instruction encoding
2119 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2120 insert_val_thumb_movw_movt(
2121 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2122 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2125 val
|= (x
& 0xf000) << 4;
2126 val
|= (x
& 0x0800) << 15;
2127 val
|= (x
& 0x0700) << 4;
2128 val
|= (x
& 0x00ff);
2132 // Handle ARM long branches.
2133 static typename
This::Status
2134 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2135 unsigned char *, const Sized_symbol
<32>*,
2136 const Arm_relobj
<big_endian
>*, unsigned int,
2137 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2139 // Handle THUMB long branches.
2140 static typename
This::Status
2141 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2142 unsigned char *, const Sized_symbol
<32>*,
2143 const Arm_relobj
<big_endian
>*, unsigned int,
2144 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2148 // Return the branch offset of a 32-bit THUMB branch.
2149 static inline int32_t
2150 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2152 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2153 // involving the J1 and J2 bits.
2154 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2155 uint32_t upper
= upper_insn
& 0x3ffU
;
2156 uint32_t lower
= lower_insn
& 0x7ffU
;
2157 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2158 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2159 uint32_t i1
= j1
^ s
? 0 : 1;
2160 uint32_t i2
= j2
^ s
? 0 : 1;
2162 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2163 | (upper
<< 12) | (lower
<< 1));
2166 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2167 // UPPER_INSN is the original upper instruction of the branch. Caller is
2168 // responsible for overflow checking and BLX offset adjustment.
2169 static inline uint16_t
2170 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2172 uint32_t s
= offset
< 0 ? 1 : 0;
2173 uint32_t bits
= static_cast<uint32_t>(offset
);
2174 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2177 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2178 // LOWER_INSN is the original lower instruction of the branch. Caller is
2179 // responsible for overflow checking and BLX offset adjustment.
2180 static inline uint16_t
2181 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2183 uint32_t s
= offset
< 0 ? 1 : 0;
2184 uint32_t bits
= static_cast<uint32_t>(offset
);
2185 return ((lower_insn
& ~0x2fffU
)
2186 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2187 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2188 | ((bits
>> 1) & 0x7ffU
));
2191 // Return the branch offset of a 32-bit THUMB conditional branch.
2192 static inline int32_t
2193 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2195 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2196 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2197 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2198 uint32_t lower
= (lower_insn
& 0x07ffU
);
2199 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2201 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2204 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2205 // instruction. UPPER_INSN is the original upper instruction of the branch.
2206 // Caller is responsible for overflow checking.
2207 static inline uint16_t
2208 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2210 uint32_t s
= offset
< 0 ? 1 : 0;
2211 uint32_t bits
= static_cast<uint32_t>(offset
);
2212 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2215 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2216 // instruction. LOWER_INSN is the original lower instruction of the branch.
2217 // Caller is reponsible for overflow checking.
2218 static inline uint16_t
2219 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2221 uint32_t bits
= static_cast<uint32_t>(offset
);
2222 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2223 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2224 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2226 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
2229 // R_ARM_ABS8: S + A
2230 static inline typename
This::Status
2231 abs8(unsigned char *view
,
2232 const Sized_relobj
<32, big_endian
>* object
,
2233 const Symbol_value
<32>* psymval
)
2235 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2236 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2237 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2238 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2239 Reltype addend
= utils::sign_extend
<8>(val
);
2240 Reltype x
= psymval
->value(object
, addend
);
2241 val
= utils::bit_select(val
, x
, 0xffU
);
2242 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2243 return (utils::has_signed_unsigned_overflow
<8>(x
)
2244 ? This::STATUS_OVERFLOW
2245 : This::STATUS_OKAY
);
2248 // R_ARM_THM_ABS5: S + A
2249 static inline typename
This::Status
2250 thm_abs5(unsigned char *view
,
2251 const Sized_relobj
<32, big_endian
>* object
,
2252 const Symbol_value
<32>* psymval
)
2254 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2255 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2256 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2257 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2258 Reltype addend
= (val
& 0x7e0U
) >> 6;
2259 Reltype x
= psymval
->value(object
, addend
);
2260 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2261 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2262 return (utils::has_overflow
<5>(x
)
2263 ? This::STATUS_OVERFLOW
2264 : This::STATUS_OKAY
);
2267 // R_ARM_ABS12: S + A
2268 static inline typename
This::Status
2269 abs12(unsigned char *view
,
2270 const Sized_relobj
<32, big_endian
>* object
,
2271 const Symbol_value
<32>* psymval
)
2273 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2274 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2275 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2276 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2277 Reltype addend
= val
& 0x0fffU
;
2278 Reltype x
= psymval
->value(object
, addend
);
2279 val
= utils::bit_select(val
, x
, 0x0fffU
);
2280 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2281 return (utils::has_overflow
<12>(x
)
2282 ? This::STATUS_OVERFLOW
2283 : This::STATUS_OKAY
);
2286 // R_ARM_ABS16: S + A
2287 static inline typename
This::Status
2288 abs16(unsigned char *view
,
2289 const Sized_relobj
<32, big_endian
>* object
,
2290 const Symbol_value
<32>* psymval
)
2292 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2293 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2294 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2295 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2296 Reltype addend
= utils::sign_extend
<16>(val
);
2297 Reltype x
= psymval
->value(object
, addend
);
2298 val
= utils::bit_select(val
, x
, 0xffffU
);
2299 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2300 return (utils::has_signed_unsigned_overflow
<16>(x
)
2301 ? This::STATUS_OVERFLOW
2302 : This::STATUS_OKAY
);
2305 // R_ARM_ABS32: (S + A) | T
2306 static inline typename
This::Status
2307 abs32(unsigned char *view
,
2308 const Sized_relobj
<32, big_endian
>* object
,
2309 const Symbol_value
<32>* psymval
,
2310 Arm_address thumb_bit
)
2312 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2313 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2314 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2315 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2316 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2317 return This::STATUS_OKAY
;
2320 // R_ARM_REL32: (S + A) | T - P
2321 static inline typename
This::Status
2322 rel32(unsigned char *view
,
2323 const Sized_relobj
<32, big_endian
>* object
,
2324 const Symbol_value
<32>* psymval
,
2325 Arm_address address
,
2326 Arm_address thumb_bit
)
2328 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2329 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2330 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2331 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2332 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2333 return This::STATUS_OKAY
;
2336 // R_ARM_THM_CALL: (S + A) | T - P
2337 static inline typename
This::Status
2338 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2339 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2340 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2341 Arm_address address
, Arm_address thumb_bit
,
2342 bool is_weakly_undefined_without_plt
)
2344 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2345 object
, r_sym
, psymval
, address
, thumb_bit
,
2346 is_weakly_undefined_without_plt
);
2349 // R_ARM_THM_JUMP24: (S + A) | T - P
2350 static inline typename
This::Status
2351 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2352 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2353 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2354 Arm_address address
, Arm_address thumb_bit
,
2355 bool is_weakly_undefined_without_plt
)
2357 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2358 object
, r_sym
, psymval
, address
, thumb_bit
,
2359 is_weakly_undefined_without_plt
);
2362 // R_ARM_THM_JUMP24: (S + A) | T - P
2363 static typename
This::Status
2364 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
2365 const Symbol_value
<32>* psymval
, Arm_address address
,
2366 Arm_address thumb_bit
);
2368 // R_ARM_THM_XPC22: (S + A) | T - P
2369 static inline typename
This::Status
2370 thm_xpc22(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_XPC22
, relinfo
, view
, gsym
,
2377 object
, r_sym
, psymval
, address
, thumb_bit
,
2378 is_weakly_undefined_without_plt
);
2381 // R_ARM_BASE_PREL: B(S) + A - P
2382 static inline typename
This::Status
2383 base_prel(unsigned char* view
,
2385 Arm_address address
)
2387 Base::rel32(view
, origin
- address
);
2391 // R_ARM_BASE_ABS: B(S) + A
2392 static inline typename
This::Status
2393 base_abs(unsigned char* view
,
2396 Base::rel32(view
, origin
);
2400 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2401 static inline typename
This::Status
2402 got_brel(unsigned char* view
,
2403 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2405 Base::rel32(view
, got_offset
);
2406 return This::STATUS_OKAY
;
2409 // R_ARM_GOT_PREL: GOT(S) + A - P
2410 static inline typename
This::Status
2411 got_prel(unsigned char *view
,
2412 Arm_address got_entry
,
2413 Arm_address address
)
2415 Base::rel32(view
, got_entry
- address
);
2416 return This::STATUS_OKAY
;
2419 // R_ARM_PLT32: (S + A) | T - P
2420 static inline typename
This::Status
2421 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2422 unsigned char *view
,
2423 const Sized_symbol
<32>* gsym
,
2424 const Arm_relobj
<big_endian
>* object
,
2426 const Symbol_value
<32>* psymval
,
2427 Arm_address address
,
2428 Arm_address thumb_bit
,
2429 bool is_weakly_undefined_without_plt
)
2431 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2432 object
, r_sym
, psymval
, address
, thumb_bit
,
2433 is_weakly_undefined_without_plt
);
2436 // R_ARM_XPC25: (S + A) | T - P
2437 static inline typename
This::Status
2438 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2439 unsigned char *view
,
2440 const Sized_symbol
<32>* gsym
,
2441 const Arm_relobj
<big_endian
>* object
,
2443 const Symbol_value
<32>* psymval
,
2444 Arm_address address
,
2445 Arm_address thumb_bit
,
2446 bool is_weakly_undefined_without_plt
)
2448 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2449 object
, r_sym
, psymval
, address
, thumb_bit
,
2450 is_weakly_undefined_without_plt
);
2453 // R_ARM_CALL: (S + A) | T - P
2454 static inline typename
This::Status
2455 call(const Relocate_info
<32, big_endian
>* relinfo
,
2456 unsigned char *view
,
2457 const Sized_symbol
<32>* gsym
,
2458 const Arm_relobj
<big_endian
>* object
,
2460 const Symbol_value
<32>* psymval
,
2461 Arm_address address
,
2462 Arm_address thumb_bit
,
2463 bool is_weakly_undefined_without_plt
)
2465 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2466 object
, r_sym
, psymval
, address
, thumb_bit
,
2467 is_weakly_undefined_without_plt
);
2470 // R_ARM_JUMP24: (S + A) | T - P
2471 static inline typename
This::Status
2472 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2473 unsigned char *view
,
2474 const Sized_symbol
<32>* gsym
,
2475 const Arm_relobj
<big_endian
>* object
,
2477 const Symbol_value
<32>* psymval
,
2478 Arm_address address
,
2479 Arm_address thumb_bit
,
2480 bool is_weakly_undefined_without_plt
)
2482 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2483 object
, r_sym
, psymval
, address
, thumb_bit
,
2484 is_weakly_undefined_without_plt
);
2487 // R_ARM_PREL: (S + A) | T - P
2488 static inline typename
This::Status
2489 prel31(unsigned char *view
,
2490 const Sized_relobj
<32, big_endian
>* object
,
2491 const Symbol_value
<32>* psymval
,
2492 Arm_address address
,
2493 Arm_address thumb_bit
)
2495 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2496 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2497 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2498 Valtype addend
= utils::sign_extend
<31>(val
);
2499 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2500 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2501 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2502 return (utils::has_overflow
<31>(x
) ?
2503 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2506 // R_ARM_MOVW_ABS_NC: (S + A) | T
2507 static inline typename
This::Status
2508 movw_abs_nc(unsigned char *view
,
2509 const Sized_relobj
<32, big_endian
>* object
,
2510 const Symbol_value
<32>* psymval
,
2511 Arm_address thumb_bit
)
2513 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2514 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2515 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2516 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2517 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2518 val
= This::insert_val_arm_movw_movt(val
, x
);
2519 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2520 return This::STATUS_OKAY
;
2523 // R_ARM_MOVT_ABS: S + A
2524 static inline typename
This::Status
2525 movt_abs(unsigned char *view
,
2526 const Sized_relobj
<32, big_endian
>* object
,
2527 const Symbol_value
<32>* psymval
)
2529 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2530 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2531 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2532 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2533 Valtype x
= psymval
->value(object
, addend
) >> 16;
2534 val
= This::insert_val_arm_movw_movt(val
, x
);
2535 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2536 return This::STATUS_OKAY
;
2539 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2540 static inline typename
This::Status
2541 thm_movw_abs_nc(unsigned char *view
,
2542 const Sized_relobj
<32, big_endian
>* object
,
2543 const Symbol_value
<32>* psymval
,
2544 Arm_address thumb_bit
)
2546 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2547 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2548 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2549 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2550 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2551 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2552 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2553 val
= This::insert_val_thumb_movw_movt(val
, x
);
2554 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2555 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2556 return This::STATUS_OKAY
;
2559 // R_ARM_THM_MOVT_ABS: S + A
2560 static inline typename
This::Status
2561 thm_movt_abs(unsigned char *view
,
2562 const Sized_relobj
<32, big_endian
>* object
,
2563 const Symbol_value
<32>* psymval
)
2565 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2566 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2567 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2568 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2569 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2570 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2571 Reltype x
= psymval
->value(object
, addend
) >> 16;
2572 val
= This::insert_val_thumb_movw_movt(val
, x
);
2573 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2574 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2575 return This::STATUS_OKAY
;
2578 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2579 static inline typename
This::Status
2580 movw_prel_nc(unsigned char *view
,
2581 const Sized_relobj
<32, big_endian
>* object
,
2582 const Symbol_value
<32>* psymval
,
2583 Arm_address address
,
2584 Arm_address thumb_bit
)
2586 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2587 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2588 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2589 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2590 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2591 val
= This::insert_val_arm_movw_movt(val
, x
);
2592 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2593 return This::STATUS_OKAY
;
2596 // R_ARM_MOVT_PREL: S + A - P
2597 static inline typename
This::Status
2598 movt_prel(unsigned char *view
,
2599 const Sized_relobj
<32, big_endian
>* object
,
2600 const Symbol_value
<32>* psymval
,
2601 Arm_address address
)
2603 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2604 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2605 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2606 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2607 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2608 val
= This::insert_val_arm_movw_movt(val
, x
);
2609 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2610 return This::STATUS_OKAY
;
2613 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2614 static inline typename
This::Status
2615 thm_movw_prel_nc(unsigned char *view
,
2616 const Sized_relobj
<32, big_endian
>* object
,
2617 const Symbol_value
<32>* psymval
,
2618 Arm_address address
,
2619 Arm_address thumb_bit
)
2621 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2622 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2623 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2624 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2625 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2626 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2627 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2628 val
= This::insert_val_thumb_movw_movt(val
, x
);
2629 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2630 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2631 return This::STATUS_OKAY
;
2634 // R_ARM_THM_MOVT_PREL: S + A - P
2635 static inline typename
This::Status
2636 thm_movt_prel(unsigned char *view
,
2637 const Sized_relobj
<32, big_endian
>* object
,
2638 const Symbol_value
<32>* psymval
,
2639 Arm_address address
)
2641 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2642 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2643 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2644 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2645 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2646 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2647 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2648 val
= This::insert_val_thumb_movw_movt(val
, x
);
2649 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2650 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2651 return This::STATUS_OKAY
;
2655 // Relocate ARM long branches. This handles relocation types
2656 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2657 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2658 // undefined and we do not use PLT in this relocation. In such a case,
2659 // the branch is converted into an NOP.
2661 template<bool big_endian
>
2662 typename Arm_relocate_functions
<big_endian
>::Status
2663 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2664 unsigned int r_type
,
2665 const Relocate_info
<32, big_endian
>* relinfo
,
2666 unsigned char *view
,
2667 const Sized_symbol
<32>* gsym
,
2668 const Arm_relobj
<big_endian
>* object
,
2670 const Symbol_value
<32>* psymval
,
2671 Arm_address address
,
2672 Arm_address thumb_bit
,
2673 bool is_weakly_undefined_without_plt
)
2675 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2676 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2677 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2679 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2680 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2681 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2682 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2683 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2684 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2685 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2687 // Check that the instruction is valid.
2688 if (r_type
== elfcpp::R_ARM_CALL
)
2690 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2691 return This::STATUS_BAD_RELOC
;
2693 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2695 if (!insn_is_b
&& !insn_is_cond_bl
)
2696 return This::STATUS_BAD_RELOC
;
2698 else if (r_type
== elfcpp::R_ARM_PLT32
)
2700 if (!insn_is_any_branch
)
2701 return This::STATUS_BAD_RELOC
;
2703 else if (r_type
== elfcpp::R_ARM_XPC25
)
2705 // FIXME: AAELF document IH0044C does not say much about it other
2706 // than it being obsolete.
2707 if (!insn_is_any_branch
)
2708 return This::STATUS_BAD_RELOC
;
2713 // A branch to an undefined weak symbol is turned into a jump to
2714 // the next instruction unless a PLT entry will be created.
2715 // Do the same for local undefined symbols.
2716 // The jump to the next instruction is optimized as a NOP depending
2717 // on the architecture.
2718 const Target_arm
<big_endian
>* arm_target
=
2719 Target_arm
<big_endian
>::default_target();
2720 if (is_weakly_undefined_without_plt
)
2722 Valtype cond
= val
& 0xf0000000U
;
2723 if (arm_target
->may_use_arm_nop())
2724 val
= cond
| 0x0320f000;
2726 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2727 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2728 return This::STATUS_OKAY
;
2731 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2732 Valtype branch_target
= psymval
->value(object
, addend
);
2733 int32_t branch_offset
= branch_target
- address
;
2735 // We need a stub if the branch offset is too large or if we need
2737 bool may_use_blx
= arm_target
->may_use_blx();
2738 Reloc_stub
* stub
= NULL
;
2739 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2740 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2741 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2743 Stub_type stub_type
=
2744 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2746 if (stub_type
!= arm_stub_none
)
2748 Stub_table
<big_endian
>* stub_table
=
2749 object
->stub_table(relinfo
->data_shndx
);
2750 gold_assert(stub_table
!= NULL
);
2752 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2753 stub
= stub_table
->find_reloc_stub(stub_key
);
2754 gold_assert(stub
!= NULL
);
2755 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2756 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2757 branch_offset
= branch_target
- address
;
2758 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2759 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2763 // At this point, if we still need to switch mode, the instruction
2764 // must either be a BLX or a BL that can be converted to a BLX.
2768 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2769 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2772 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2773 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2774 return (utils::has_overflow
<26>(branch_offset
)
2775 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2778 // Relocate THUMB long branches. This handles relocation types
2779 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2780 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2781 // undefined and we do not use PLT in this relocation. In such a case,
2782 // the branch is converted into an NOP.
2784 template<bool big_endian
>
2785 typename Arm_relocate_functions
<big_endian
>::Status
2786 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2787 unsigned int r_type
,
2788 const Relocate_info
<32, big_endian
>* relinfo
,
2789 unsigned char *view
,
2790 const Sized_symbol
<32>* gsym
,
2791 const Arm_relobj
<big_endian
>* object
,
2793 const Symbol_value
<32>* psymval
,
2794 Arm_address address
,
2795 Arm_address thumb_bit
,
2796 bool is_weakly_undefined_without_plt
)
2798 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2799 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2800 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2801 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2803 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2805 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2806 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2808 // Check that the instruction is valid.
2809 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2811 if (!is_bl_insn
&& !is_blx_insn
)
2812 return This::STATUS_BAD_RELOC
;
2814 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2816 // This cannot be a BLX.
2818 return This::STATUS_BAD_RELOC
;
2820 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2822 // Check for Thumb to Thumb call.
2824 return This::STATUS_BAD_RELOC
;
2827 gold_warning(_("%s: Thumb BLX instruction targets "
2828 "thumb function '%s'."),
2829 object
->name().c_str(),
2830 (gsym
? gsym
->name() : "(local)"));
2831 // Convert BLX to BL.
2832 lower_insn
|= 0x1000U
;
2838 // A branch to an undefined weak symbol is turned into a jump to
2839 // the next instruction unless a PLT entry will be created.
2840 // The jump to the next instruction is optimized as a NOP.W for
2841 // Thumb-2 enabled architectures.
2842 const Target_arm
<big_endian
>* arm_target
=
2843 Target_arm
<big_endian
>::default_target();
2844 if (is_weakly_undefined_without_plt
)
2846 if (arm_target
->may_use_thumb2_nop())
2848 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2849 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2853 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2854 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2856 return This::STATUS_OKAY
;
2859 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
2860 Arm_address branch_target
= psymval
->value(object
, addend
);
2861 int32_t branch_offset
= branch_target
- address
;
2863 // We need a stub if the branch offset is too large or if we need
2865 bool may_use_blx
= arm_target
->may_use_blx();
2866 bool thumb2
= arm_target
->using_thumb2();
2868 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2869 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2871 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2872 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2873 || ((thumb_bit
== 0)
2874 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2875 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2877 Stub_type stub_type
=
2878 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2880 if (stub_type
!= arm_stub_none
)
2882 Stub_table
<big_endian
>* stub_table
=
2883 object
->stub_table(relinfo
->data_shndx
);
2884 gold_assert(stub_table
!= NULL
);
2886 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2887 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2888 gold_assert(stub
!= NULL
);
2889 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2890 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2891 branch_offset
= branch_target
- address
;
2895 // At this point, if we still need to switch mode, the instruction
2896 // must either be a BLX or a BL that can be converted to a BLX.
2899 gold_assert(may_use_blx
2900 && (r_type
== elfcpp::R_ARM_THM_CALL
2901 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2902 // Make sure this is a BLX.
2903 lower_insn
&= ~0x1000U
;
2907 // Make sure this is a BL.
2908 lower_insn
|= 0x1000U
;
2911 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2912 // For a BLX instruction, make sure that the relocation is rounded up
2913 // to a word boundary. This follows the semantics of the instruction
2914 // which specifies that bit 1 of the target address will come from bit
2915 // 1 of the base address.
2916 branch_offset
= (branch_offset
+ 2) & ~3;
2918 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2919 // We use the Thumb-2 encoding, which is safe even if dealing with
2920 // a Thumb-1 instruction by virtue of our overflow check above. */
2921 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
2922 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
2924 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2925 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2928 ? utils::has_overflow
<25>(branch_offset
)
2929 : utils::has_overflow
<23>(branch_offset
))
2930 ? This::STATUS_OVERFLOW
2931 : This::STATUS_OKAY
);
2934 // Relocate THUMB-2 long conditional branches.
2935 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2936 // undefined and we do not use PLT in this relocation. In such a case,
2937 // the branch is converted into an NOP.
2939 template<bool big_endian
>
2940 typename Arm_relocate_functions
<big_endian
>::Status
2941 Arm_relocate_functions
<big_endian
>::thm_jump19(
2942 unsigned char *view
,
2943 const Arm_relobj
<big_endian
>* object
,
2944 const Symbol_value
<32>* psymval
,
2945 Arm_address address
,
2946 Arm_address thumb_bit
)
2948 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2949 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2950 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2951 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2952 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
2954 Arm_address branch_target
= psymval
->value(object
, addend
);
2955 int32_t branch_offset
= branch_target
- address
;
2957 // ??? Should handle interworking? GCC might someday try to
2958 // use this for tail calls.
2959 // FIXME: We do support thumb entry to PLT yet.
2962 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
2963 return This::STATUS_BAD_RELOC
;
2966 // Put RELOCATION back into the insn.
2967 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
2968 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
2970 // Put the relocated value back in the object file:
2971 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2972 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2974 return (utils::has_overflow
<21>(branch_offset
)
2975 ? This::STATUS_OVERFLOW
2976 : This::STATUS_OKAY
);
2979 // Get the GOT section, creating it if necessary.
2981 template<bool big_endian
>
2982 Output_data_got
<32, big_endian
>*
2983 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2985 if (this->got_
== NULL
)
2987 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2989 this->got_
= new Output_data_got
<32, big_endian
>();
2992 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2994 | elfcpp::SHF_WRITE
),
2995 this->got_
, false, true, true,
2998 // The old GNU linker creates a .got.plt section. We just
2999 // create another set of data in the .got section. Note that we
3000 // always create a PLT if we create a GOT, although the PLT
3002 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
3003 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3005 | elfcpp::SHF_WRITE
),
3006 this->got_plt_
, false, false,
3009 // The first three entries are reserved.
3010 this->got_plt_
->set_current_data_size(3 * 4);
3012 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3013 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3014 Symbol_table::PREDEFINED
,
3016 0, 0, elfcpp::STT_OBJECT
,
3018 elfcpp::STV_HIDDEN
, 0,
3024 // Get the dynamic reloc section, creating it if necessary.
3026 template<bool big_endian
>
3027 typename Target_arm
<big_endian
>::Reloc_section
*
3028 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
3030 if (this->rel_dyn_
== NULL
)
3032 gold_assert(layout
!= NULL
);
3033 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
3034 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
3035 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
3036 false, false, false);
3038 return this->rel_dyn_
;
3041 // Insn_template methods.
3043 // Return byte size of an instruction template.
3046 Insn_template::size() const
3048 switch (this->type())
3051 case THUMB16_SPECIAL_TYPE
:
3062 // Return alignment of an instruction template.
3065 Insn_template::alignment() const
3067 switch (this->type())
3070 case THUMB16_SPECIAL_TYPE
:
3081 // Stub_template methods.
3083 Stub_template::Stub_template(
3084 Stub_type type
, const Insn_template
* insns
,
3086 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
3087 entry_in_thumb_mode_(false), relocs_()
3091 // Compute byte size and alignment of stub template.
3092 for (size_t i
= 0; i
< insn_count
; i
++)
3094 unsigned insn_alignment
= insns
[i
].alignment();
3095 size_t insn_size
= insns
[i
].size();
3096 gold_assert((offset
& (insn_alignment
- 1)) == 0);
3097 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
3098 switch (insns
[i
].type())
3100 case Insn_template::THUMB16_TYPE
:
3101 case Insn_template::THUMB16_SPECIAL_TYPE
:
3103 this->entry_in_thumb_mode_
= true;
3106 case Insn_template::THUMB32_TYPE
:
3107 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
3108 this->relocs_
.push_back(Reloc(i
, offset
));
3110 this->entry_in_thumb_mode_
= true;
3113 case Insn_template::ARM_TYPE
:
3114 // Handle cases where the target is encoded within the
3116 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
3117 this->relocs_
.push_back(Reloc(i
, offset
));
3120 case Insn_template::DATA_TYPE
:
3121 // Entry point cannot be data.
3122 gold_assert(i
!= 0);
3123 this->relocs_
.push_back(Reloc(i
, offset
));
3129 offset
+= insn_size
;
3131 this->size_
= offset
;
3136 // Template to implement do_write for a specific target endianity.
3138 template<bool big_endian
>
3140 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
3142 const Stub_template
* stub_template
= this->stub_template();
3143 const Insn_template
* insns
= stub_template
->insns();
3145 // FIXME: We do not handle BE8 encoding yet.
3146 unsigned char* pov
= view
;
3147 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
3149 switch (insns
[i
].type())
3151 case Insn_template::THUMB16_TYPE
:
3152 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
3154 case Insn_template::THUMB16_SPECIAL_TYPE
:
3155 elfcpp::Swap
<16, big_endian
>::writeval(
3157 this->thumb16_special(i
));
3159 case Insn_template::THUMB32_TYPE
:
3161 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
3162 uint32_t lo
= insns
[i
].data() & 0xffff;
3163 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
3164 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
3167 case Insn_template::ARM_TYPE
:
3168 case Insn_template::DATA_TYPE
:
3169 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
3174 pov
+= insns
[i
].size();
3176 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
3179 // Reloc_stub::Key methods.
3181 // Dump a Key as a string for debugging.
3184 Reloc_stub::Key::name() const
3186 if (this->r_sym_
== invalid_index
)
3188 // Global symbol key name
3189 // <stub-type>:<symbol name>:<addend>.
3190 const std::string sym_name
= this->u_
.symbol
->name();
3191 // We need to print two hex number and two colons. So just add 100 bytes
3192 // to the symbol name size.
3193 size_t len
= sym_name
.size() + 100;
3194 char* buffer
= new char[len
];
3195 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
3196 sym_name
.c_str(), this->addend_
);
3197 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3199 return std::string(buffer
);
3203 // local symbol key name
3204 // <stub-type>:<object>:<r_sym>:<addend>.
3205 const size_t len
= 200;
3207 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3208 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3209 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3210 return std::string(buffer
);
3214 // Reloc_stub methods.
3216 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3217 // LOCATION to DESTINATION.
3218 // This code is based on the arm_type_of_stub function in
3219 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3223 Reloc_stub::stub_type_for_reloc(
3224 unsigned int r_type
,
3225 Arm_address location
,
3226 Arm_address destination
,
3227 bool target_is_thumb
)
3229 Stub_type stub_type
= arm_stub_none
;
3231 // This is a bit ugly but we want to avoid using a templated class for
3232 // big and little endianities.
3234 bool should_force_pic_veneer
;
3237 if (parameters
->target().is_big_endian())
3239 const Target_arm
<true>* big_endian_target
=
3240 Target_arm
<true>::default_target();
3241 may_use_blx
= big_endian_target
->may_use_blx();
3242 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3243 thumb2
= big_endian_target
->using_thumb2();
3244 thumb_only
= big_endian_target
->using_thumb_only();
3248 const Target_arm
<false>* little_endian_target
=
3249 Target_arm
<false>::default_target();
3250 may_use_blx
= little_endian_target
->may_use_blx();
3251 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3252 thumb2
= little_endian_target
->using_thumb2();
3253 thumb_only
= little_endian_target
->using_thumb_only();
3256 int64_t branch_offset
= (int64_t)destination
- location
;
3258 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3260 // Handle cases where:
3261 // - this call goes too far (different Thumb/Thumb2 max
3263 // - it's a Thumb->Arm call and blx is not available, or it's a
3264 // Thumb->Arm branch (not bl). A stub is needed in this case.
3266 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3267 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3269 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3270 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3271 || ((!target_is_thumb
)
3272 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3273 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3275 if (target_is_thumb
)
3280 stub_type
= (parameters
->options().shared()
3281 || should_force_pic_veneer
)
3284 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3285 // V5T and above. Stub starts with ARM code, so
3286 // we must be able to switch mode before
3287 // reaching it, which is only possible for 'bl'
3288 // (ie R_ARM_THM_CALL relocation).
3289 ? arm_stub_long_branch_any_thumb_pic
3290 // On V4T, use Thumb code only.
3291 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3295 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3296 ? arm_stub_long_branch_any_any
// V5T and above.
3297 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3301 stub_type
= (parameters
->options().shared()
3302 || should_force_pic_veneer
)
3303 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3304 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3311 // FIXME: We should check that the input section is from an
3312 // object that has interwork enabled.
3314 stub_type
= (parameters
->options().shared()
3315 || should_force_pic_veneer
)
3318 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3319 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3320 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3324 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3325 ? arm_stub_long_branch_any_any
// V5T and above.
3326 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3328 // Handle v4t short branches.
3329 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3330 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3331 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3332 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3336 else if (r_type
== elfcpp::R_ARM_CALL
3337 || r_type
== elfcpp::R_ARM_JUMP24
3338 || r_type
== elfcpp::R_ARM_PLT32
)
3340 if (target_is_thumb
)
3344 // FIXME: We should check that the input section is from an
3345 // object that has interwork enabled.
3347 // We have an extra 2-bytes reach because of
3348 // the mode change (bit 24 (H) of BLX encoding).
3349 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3350 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3351 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3352 || (r_type
== elfcpp::R_ARM_JUMP24
)
3353 || (r_type
== elfcpp::R_ARM_PLT32
))
3355 stub_type
= (parameters
->options().shared()
3356 || should_force_pic_veneer
)
3359 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3360 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3364 ? arm_stub_long_branch_any_any
// V5T and above.
3365 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3371 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3372 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3374 stub_type
= (parameters
->options().shared()
3375 || should_force_pic_veneer
)
3376 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3377 : arm_stub_long_branch_any_any
; /// non-PIC.
3385 // Cortex_a8_stub methods.
3387 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3388 // I is the position of the instruction template in the stub template.
3391 Cortex_a8_stub::do_thumb16_special(size_t i
)
3393 // The only use of this is to copy condition code from a conditional
3394 // branch being worked around to the corresponding conditional branch in
3396 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3398 uint16_t data
= this->stub_template()->insns()[i
].data();
3399 gold_assert((data
& 0xff00U
) == 0xd000U
);
3400 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3404 // Stub_factory methods.
3406 Stub_factory::Stub_factory()
3408 // The instruction template sequences are declared as static
3409 // objects and initialized first time the constructor runs.
3411 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3412 // to reach the stub if necessary.
3413 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3415 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3416 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3417 // dcd R_ARM_ABS32(X)
3420 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3422 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3424 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3425 Insn_template::arm_insn(0xe12fff1c), // bx ip
3426 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3427 // dcd R_ARM_ABS32(X)
3430 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3431 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3433 Insn_template::thumb16_insn(0xb401), // push {r0}
3434 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3435 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3436 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3437 Insn_template::thumb16_insn(0x4760), // bx ip
3438 Insn_template::thumb16_insn(0xbf00), // nop
3439 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3440 // dcd R_ARM_ABS32(X)
3443 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3445 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3447 Insn_template::thumb16_insn(0x4778), // bx pc
3448 Insn_template::thumb16_insn(0x46c0), // nop
3449 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3450 Insn_template::arm_insn(0xe12fff1c), // bx ip
3451 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3452 // dcd R_ARM_ABS32(X)
3455 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3457 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3459 Insn_template::thumb16_insn(0x4778), // bx pc
3460 Insn_template::thumb16_insn(0x46c0), // nop
3461 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3462 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3463 // dcd R_ARM_ABS32(X)
3466 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3467 // one, when the destination is close enough.
3468 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3470 Insn_template::thumb16_insn(0x4778), // bx pc
3471 Insn_template::thumb16_insn(0x46c0), // nop
3472 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3475 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3476 // blx to reach the stub if necessary.
3477 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3479 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3480 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3481 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3482 // dcd R_ARM_REL32(X-4)
3485 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3486 // blx to reach the stub if necessary. We can not add into pc;
3487 // it is not guaranteed to mode switch (different in ARMv6 and
3489 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3491 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3492 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3493 Insn_template::arm_insn(0xe12fff1c), // bx ip
3494 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3495 // dcd R_ARM_REL32(X)
3498 // V4T ARM -> ARM long branch stub, PIC.
3499 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3501 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3502 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3503 Insn_template::arm_insn(0xe12fff1c), // bx ip
3504 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3505 // dcd R_ARM_REL32(X)
3508 // V4T Thumb -> ARM long branch stub, PIC.
3509 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3511 Insn_template::thumb16_insn(0x4778), // bx pc
3512 Insn_template::thumb16_insn(0x46c0), // nop
3513 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3514 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3515 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3516 // dcd R_ARM_REL32(X)
3519 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3521 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3523 Insn_template::thumb16_insn(0xb401), // push {r0}
3524 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3525 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3526 Insn_template::thumb16_insn(0x4484), // add ip, r0
3527 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3528 Insn_template::thumb16_insn(0x4760), // bx ip
3529 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3530 // dcd R_ARM_REL32(X)
3533 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3535 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3537 Insn_template::thumb16_insn(0x4778), // bx pc
3538 Insn_template::thumb16_insn(0x46c0), // nop
3539 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3540 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3541 Insn_template::arm_insn(0xe12fff1c), // bx ip
3542 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3543 // dcd R_ARM_REL32(X)
3546 // Cortex-A8 erratum-workaround stubs.
3548 // Stub used for conditional branches (which may be beyond +/-1MB away,
3549 // so we can't use a conditional branch to reach this stub).
3556 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3558 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3559 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3560 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3564 // Stub used for b.w and bl.w instructions.
3566 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3568 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3571 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3573 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3576 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3577 // instruction (which switches to ARM mode) to point to this stub. Jump to
3578 // the real destination using an ARM-mode branch.
3579 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3581 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3584 // Fill in the stub template look-up table. Stub templates are constructed
3585 // per instance of Stub_factory for fast look-up without locking
3586 // in a thread-enabled environment.
3588 this->stub_templates_
[arm_stub_none
] =
3589 new Stub_template(arm_stub_none
, NULL
, 0);
3591 #define DEF_STUB(x) \
3595 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3596 Stub_type type = arm_stub_##x; \
3597 this->stub_templates_[type] = \
3598 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3606 // Stub_table methods.
3608 // Removel all Cortex-A8 stub.
3610 template<bool big_endian
>
3612 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
3614 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3615 p
!= this->cortex_a8_stubs_
.end();
3618 this->cortex_a8_stubs_
.clear();
3621 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3623 template<bool big_endian
>
3625 Stub_table
<big_endian
>::relocate_stub(
3627 const Relocate_info
<32, big_endian
>* relinfo
,
3628 Target_arm
<big_endian
>* arm_target
,
3629 Output_section
* output_section
,
3630 unsigned char* view
,
3631 Arm_address address
,
3632 section_size_type view_size
)
3634 const Stub_template
* stub_template
= stub
->stub_template();
3635 if (stub_template
->reloc_count() != 0)
3637 // Adjust view to cover the stub only.
3638 section_size_type offset
= stub
->offset();
3639 section_size_type stub_size
= stub_template
->size();
3640 gold_assert(offset
+ stub_size
<= view_size
);
3642 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
3643 address
+ offset
, stub_size
);
3647 // Relocate all stubs in this stub table.
3649 template<bool big_endian
>
3651 Stub_table
<big_endian
>::relocate_stubs(
3652 const Relocate_info
<32, big_endian
>* relinfo
,
3653 Target_arm
<big_endian
>* arm_target
,
3654 Output_section
* output_section
,
3655 unsigned char* view
,
3656 Arm_address address
,
3657 section_size_type view_size
)
3659 // If we are passed a view bigger than the stub table's. we need to
3661 gold_assert(address
== this->address()
3663 == static_cast<section_size_type
>(this->data_size())));
3665 // Relocate all relocation stubs.
3666 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3667 p
!= this->reloc_stubs_
.end();
3669 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3670 address
, view_size
);
3672 // Relocate all Cortex-A8 stubs.
3673 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3674 p
!= this->cortex_a8_stubs_
.end();
3676 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3677 address
, view_size
);
3680 // Write out the stubs to file.
3682 template<bool big_endian
>
3684 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3686 off_t offset
= this->offset();
3687 const section_size_type oview_size
=
3688 convert_to_section_size_type(this->data_size());
3689 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3691 // Write relocation stubs.
3692 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3693 p
!= this->reloc_stubs_
.end();
3696 Reloc_stub
* stub
= p
->second
;
3697 Arm_address address
= this->address() + stub
->offset();
3699 == align_address(address
,
3700 stub
->stub_template()->alignment()));
3701 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3705 // Write Cortex-A8 stubs.
3706 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3707 p
!= this->cortex_a8_stubs_
.end();
3710 Cortex_a8_stub
* stub
= p
->second
;
3711 Arm_address address
= this->address() + stub
->offset();
3713 == align_address(address
,
3714 stub
->stub_template()->alignment()));
3715 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3719 of
->write_output_view(this->offset(), oview_size
, oview
);
3722 // Update the data size and address alignment of the stub table at the end
3723 // of a relaxation pass. Return true if either the data size or the
3724 // alignment changed in this relaxation pass.
3726 template<bool big_endian
>
3728 Stub_table
<big_endian
>::update_data_size_and_addralign()
3731 unsigned addralign
= 1;
3733 // Go over all stubs in table to compute data size and address alignment.
3735 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3736 p
!= this->reloc_stubs_
.end();
3739 const Stub_template
* stub_template
= p
->second
->stub_template();
3740 addralign
= std::max(addralign
, stub_template
->alignment());
3741 size
= (align_address(size
, stub_template
->alignment())
3742 + stub_template
->size());
3745 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3746 p
!= this->cortex_a8_stubs_
.end();
3749 const Stub_template
* stub_template
= p
->second
->stub_template();
3750 addralign
= std::max(addralign
, stub_template
->alignment());
3751 size
= (align_address(size
, stub_template
->alignment())
3752 + stub_template
->size());
3755 // Check if either data size or alignment changed in this pass.
3756 // Update prev_data_size_ and prev_addralign_. These will be used
3757 // as the current data size and address alignment for the next pass.
3758 bool changed
= size
!= this->prev_data_size_
;
3759 this->prev_data_size_
= size
;
3761 if (addralign
!= this->prev_addralign_
)
3763 this->prev_addralign_
= addralign
;
3768 // Finalize the stubs. This sets the offsets of the stubs within the stub
3769 // table. It also marks all input sections needing Cortex-A8 workaround.
3771 template<bool big_endian
>
3773 Stub_table
<big_endian
>::finalize_stubs()
3776 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3777 p
!= this->reloc_stubs_
.end();
3780 Reloc_stub
* stub
= p
->second
;
3781 const Stub_template
* stub_template
= stub
->stub_template();
3782 uint64_t stub_addralign
= stub_template
->alignment();
3783 off
= align_address(off
, stub_addralign
);
3784 stub
->set_offset(off
);
3785 off
+= stub_template
->size();
3788 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3789 p
!= this->cortex_a8_stubs_
.end();
3792 Cortex_a8_stub
* stub
= p
->second
;
3793 const Stub_template
* stub_template
= stub
->stub_template();
3794 uint64_t stub_addralign
= stub_template
->alignment();
3795 off
= align_address(off
, stub_addralign
);
3796 stub
->set_offset(off
);
3797 off
+= stub_template
->size();
3799 // Mark input section so that we can determine later if a code section
3800 // needs the Cortex-A8 workaround quickly.
3801 Arm_relobj
<big_endian
>* arm_relobj
=
3802 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
3803 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
3806 gold_assert(off
<= this->prev_data_size_
);
3809 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3810 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3811 // of the address range seen by the linker.
3813 template<bool big_endian
>
3815 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
3816 Target_arm
<big_endian
>* arm_target
,
3817 unsigned char* view
,
3818 Arm_address view_address
,
3819 section_size_type view_size
)
3821 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3822 for (Cortex_a8_stub_list::const_iterator p
=
3823 this->cortex_a8_stubs_
.lower_bound(view_address
);
3824 ((p
!= this->cortex_a8_stubs_
.end())
3825 && (p
->first
< (view_address
+ view_size
)));
3828 // We do not store the THUMB bit in the LSB of either the branch address
3829 // or the stub offset. There is no need to strip the LSB.
3830 Arm_address branch_address
= p
->first
;
3831 const Cortex_a8_stub
* stub
= p
->second
;
3832 Arm_address stub_address
= this->address() + stub
->offset();
3834 // Offset of the branch instruction relative to this view.
3835 section_size_type offset
=
3836 convert_to_section_size_type(branch_address
- view_address
);
3837 gold_assert((offset
+ 4) <= view_size
);
3839 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
3840 view
+ offset
, branch_address
);
3844 // Arm_input_section methods.
3846 // Initialize an Arm_input_section.
3848 template<bool big_endian
>
3850 Arm_input_section
<big_endian
>::init()
3852 Relobj
* relobj
= this->relobj();
3853 unsigned int shndx
= this->shndx();
3855 // Cache these to speed up size and alignment queries. It is too slow
3856 // to call section_addraglin and section_size every time.
3857 this->original_addralign_
= relobj
->section_addralign(shndx
);
3858 this->original_size_
= relobj
->section_size(shndx
);
3860 // We want to make this look like the original input section after
3861 // output sections are finalized.
3862 Output_section
* os
= relobj
->output_section(shndx
);
3863 off_t offset
= relobj
->output_section_offset(shndx
);
3864 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3865 this->set_address(os
->address() + offset
);
3866 this->set_file_offset(os
->offset() + offset
);
3868 this->set_current_data_size(this->original_size_
);
3869 this->finalize_data_size();
3872 template<bool big_endian
>
3874 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3876 // We have to write out the original section content.
3877 section_size_type section_size
;
3878 const unsigned char* section_contents
=
3879 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3880 of
->write(this->offset(), section_contents
, section_size
);
3882 // If this owns a stub table and it is not empty, write it.
3883 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3884 this->stub_table_
->write(of
);
3887 // Finalize data size.
3889 template<bool big_endian
>
3891 Arm_input_section
<big_endian
>::set_final_data_size()
3893 // If this owns a stub table, finalize its data size as well.
3894 if (this->is_stub_table_owner())
3896 uint64_t address
= this->address();
3898 // The stub table comes after the original section contents.
3899 address
+= this->original_size_
;
3900 address
= align_address(address
, this->stub_table_
->addralign());
3901 off_t offset
= this->offset() + (address
- this->address());
3902 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3903 address
+= this->stub_table_
->data_size();
3904 gold_assert(address
== this->address() + this->current_data_size());
3907 this->set_data_size(this->current_data_size());
3910 // Reset address and file offset.
3912 template<bool big_endian
>
3914 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3916 // Size of the original input section contents.
3917 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3919 // If this is a stub table owner, account for the stub table size.
3920 if (this->is_stub_table_owner())
3922 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3924 // Reset the stub table's address and file offset. The
3925 // current data size for child will be updated after that.
3926 stub_table_
->reset_address_and_file_offset();
3927 off
= align_address(off
, stub_table_
->addralign());
3928 off
+= stub_table
->current_data_size();
3931 this->set_current_data_size(off
);
3934 // Arm_output_section methods.
3936 // Create a stub group for input sections from BEGIN to END. OWNER
3937 // points to the input section to be the owner a new stub table.
3939 template<bool big_endian
>
3941 Arm_output_section
<big_endian
>::create_stub_group(
3942 Input_section_list::const_iterator begin
,
3943 Input_section_list::const_iterator end
,
3944 Input_section_list::const_iterator owner
,
3945 Target_arm
<big_endian
>* target
,
3946 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3948 // Currently we convert ordinary input sections into relaxed sections only
3949 // at this point but we may want to support creating relaxed input section
3950 // very early. So we check here to see if owner is already a relaxed
3953 Arm_input_section
<big_endian
>* arm_input_section
;
3954 if (owner
->is_relaxed_input_section())
3957 Arm_input_section
<big_endian
>::as_arm_input_section(
3958 owner
->relaxed_input_section());
3962 gold_assert(owner
->is_input_section());
3963 // Create a new relaxed input section.
3965 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3966 new_relaxed_sections
->push_back(arm_input_section
);
3969 // Create a stub table.
3970 Stub_table
<big_endian
>* stub_table
=
3971 target
->new_stub_table(arm_input_section
);
3973 arm_input_section
->set_stub_table(stub_table
);
3975 Input_section_list::const_iterator p
= begin
;
3976 Input_section_list::const_iterator prev_p
;
3978 // Look for input sections or relaxed input sections in [begin ... end].
3981 if (p
->is_input_section() || p
->is_relaxed_input_section())
3983 // The stub table information for input sections live
3984 // in their objects.
3985 Arm_relobj
<big_endian
>* arm_relobj
=
3986 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3987 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3991 while (prev_p
!= end
);
3994 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3995 // of stub groups. We grow a stub group by adding input section until the
3996 // size is just below GROUP_SIZE. The last input section will be converted
3997 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3998 // input section after the stub table, effectively double the group size.
4000 // This is similar to the group_sections() function in elf32-arm.c but is
4001 // implemented differently.
4003 template<bool big_endian
>
4005 Arm_output_section
<big_endian
>::group_sections(
4006 section_size_type group_size
,
4007 bool stubs_always_after_branch
,
4008 Target_arm
<big_endian
>* target
)
4010 // We only care about sections containing code.
4011 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
4014 // States for grouping.
4017 // No group is being built.
4019 // A group is being built but the stub table is not found yet.
4020 // We keep group a stub group until the size is just under GROUP_SIZE.
4021 // The last input section in the group will be used as the stub table.
4022 FINDING_STUB_SECTION
,
4023 // A group is being built and we have already found a stub table.
4024 // We enter this state to grow a stub group by adding input section
4025 // after the stub table. This effectively doubles the group size.
4029 // Any newly created relaxed sections are stored here.
4030 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
4032 State state
= NO_GROUP
;
4033 section_size_type off
= 0;
4034 section_size_type group_begin_offset
= 0;
4035 section_size_type group_end_offset
= 0;
4036 section_size_type stub_table_end_offset
= 0;
4037 Input_section_list::const_iterator group_begin
=
4038 this->input_sections().end();
4039 Input_section_list::const_iterator stub_table
=
4040 this->input_sections().end();
4041 Input_section_list::const_iterator group_end
= this->input_sections().end();
4042 for (Input_section_list::const_iterator p
= this->input_sections().begin();
4043 p
!= this->input_sections().end();
4046 section_size_type section_begin_offset
=
4047 align_address(off
, p
->addralign());
4048 section_size_type section_end_offset
=
4049 section_begin_offset
+ p
->data_size();
4051 // Check to see if we should group the previously seens sections.
4057 case FINDING_STUB_SECTION
:
4058 // Adding this section makes the group larger than GROUP_SIZE.
4059 if (section_end_offset
- group_begin_offset
>= group_size
)
4061 if (stubs_always_after_branch
)
4063 gold_assert(group_end
!= this->input_sections().end());
4064 this->create_stub_group(group_begin
, group_end
, group_end
,
4065 target
, &new_relaxed_sections
);
4070 // But wait, there's more! Input sections up to
4071 // stub_group_size bytes after the stub table can be
4072 // handled by it too.
4073 state
= HAS_STUB_SECTION
;
4074 stub_table
= group_end
;
4075 stub_table_end_offset
= group_end_offset
;
4080 case HAS_STUB_SECTION
:
4081 // Adding this section makes the post stub-section group larger
4083 if (section_end_offset
- stub_table_end_offset
>= group_size
)
4085 gold_assert(group_end
!= this->input_sections().end());
4086 this->create_stub_group(group_begin
, group_end
, stub_table
,
4087 target
, &new_relaxed_sections
);
4096 // If we see an input section and currently there is no group, start
4097 // a new one. Skip any empty sections.
4098 if ((p
->is_input_section() || p
->is_relaxed_input_section())
4099 && (p
->relobj()->section_size(p
->shndx()) != 0))
4101 if (state
== NO_GROUP
)
4103 state
= FINDING_STUB_SECTION
;
4105 group_begin_offset
= section_begin_offset
;
4108 // Keep track of the last input section seen.
4110 group_end_offset
= section_end_offset
;
4113 off
= section_end_offset
;
4116 // Create a stub group for any ungrouped sections.
4117 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
4119 gold_assert(group_end
!= this->input_sections().end());
4120 this->create_stub_group(group_begin
, group_end
,
4121 (state
== FINDING_STUB_SECTION
4124 target
, &new_relaxed_sections
);
4127 // Convert input section into relaxed input section in a batch.
4128 if (!new_relaxed_sections
.empty())
4129 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
4131 // Update the section offsets
4132 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
4134 Arm_relobj
<big_endian
>* arm_relobj
=
4135 Arm_relobj
<big_endian
>::as_arm_relobj(
4136 new_relaxed_sections
[i
]->relobj());
4137 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
4138 // Tell Arm_relobj that this input section is converted.
4139 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
4143 // Arm_relobj methods.
4145 // Scan relocations for stub generation.
4147 template<bool big_endian
>
4149 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
4150 Target_arm
<big_endian
>* arm_target
,
4151 const Symbol_table
* symtab
,
4152 const Layout
* layout
)
4154 unsigned int shnum
= this->shnum();
4155 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4157 // Read the section headers.
4158 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4162 // To speed up processing, we set up hash tables for fast lookup of
4163 // input offsets to output addresses.
4164 this->initialize_input_to_output_maps();
4166 const Relobj::Output_sections
& out_sections(this->output_sections());
4168 Relocate_info
<32, big_endian
> relinfo
;
4169 relinfo
.symtab
= symtab
;
4170 relinfo
.layout
= layout
;
4171 relinfo
.object
= this;
4173 const unsigned char* p
= pshdrs
+ shdr_size
;
4174 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4176 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
4178 unsigned int sh_type
= shdr
.get_sh_type();
4179 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
4182 off_t sh_size
= shdr
.get_sh_size();
4186 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4187 if (index
>= this->shnum())
4189 // Ignore reloc section with bad info. This error will be
4190 // reported in the final link.
4194 Output_section
* os
= out_sections
[index
];
4196 || symtab
->is_section_folded(this, index
))
4198 // This relocation section is against a section which we
4199 // discarded or if the section is folded into another
4200 // section due to ICF.
4203 Arm_address output_offset
= this->get_output_section_offset(index
);
4205 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4207 // Ignore reloc section with unexpected symbol table. The
4208 // error will be reported in the final link.
4212 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
4213 sh_size
, true, false);
4215 unsigned int reloc_size
;
4216 if (sh_type
== elfcpp::SHT_REL
)
4217 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4219 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4221 if (reloc_size
!= shdr
.get_sh_entsize())
4223 // Ignore reloc section with unexpected entsize. The error
4224 // will be reported in the final link.
4228 size_t reloc_count
= sh_size
/ reloc_size
;
4229 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
4231 // Ignore reloc section with uneven size. The error will be
4232 // reported in the final link.
4236 gold_assert(output_offset
!= invalid_address
4237 || this->relocs_must_follow_section_writes());
4239 // Get the section contents. This does work for the case in which
4240 // we modify the contents of an input section. We need to pass the
4241 // output view under such circumstances.
4242 section_size_type input_view_size
= 0;
4243 const unsigned char* input_view
=
4244 this->section_contents(index
, &input_view_size
, false);
4246 relinfo
.reloc_shndx
= i
;
4247 relinfo
.data_shndx
= index
;
4248 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
4250 output_offset
== invalid_address
,
4256 // After we've done the relocations, we release the hash tables,
4257 // since we no longer need them.
4258 this->free_input_to_output_maps();
4261 // Count the local symbols. The ARM backend needs to know if a symbol
4262 // is a THUMB function or not. For global symbols, it is easy because
4263 // the Symbol object keeps the ELF symbol type. For local symbol it is
4264 // harder because we cannot access this information. So we override the
4265 // do_count_local_symbol in parent and scan local symbols to mark
4266 // THUMB functions. This is not the most efficient way but I do not want to
4267 // slow down other ports by calling a per symbol targer hook inside
4268 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4270 template<bool big_endian
>
4272 Arm_relobj
<big_endian
>::do_count_local_symbols(
4273 Stringpool_template
<char>* pool
,
4274 Stringpool_template
<char>* dynpool
)
4276 // We need to fix-up the values of any local symbols whose type are
4279 // Ask parent to count the local symbols.
4280 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
4281 const unsigned int loccount
= this->local_symbol_count();
4285 // Intialize the thumb function bit-vector.
4286 std::vector
<bool> empty_vector(loccount
, false);
4287 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
4289 // Read the symbol table section header.
4290 const unsigned int symtab_shndx
= this->symtab_shndx();
4291 elfcpp::Shdr
<32, big_endian
>
4292 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
4293 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
4295 // Read the local symbols.
4296 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
4297 gold_assert(loccount
== symtabshdr
.get_sh_info());
4298 off_t locsize
= loccount
* sym_size
;
4299 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
4300 locsize
, true, true);
4302 // For mapping symbol processing, we need to read the symbol names.
4303 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
4304 if (strtab_shndx
>= this->shnum())
4306 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
4310 elfcpp::Shdr
<32, big_endian
>
4311 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
4312 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
4314 this->error(_("symbol table name section has wrong type: %u"),
4315 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
4318 const char* pnames
=
4319 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
4320 strtabshdr
.get_sh_size(),
4323 // Loop over the local symbols and mark any local symbols pointing
4324 // to THUMB functions.
4326 // Skip the first dummy symbol.
4328 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
4329 this->local_values();
4330 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
4332 elfcpp::Sym
<32, big_endian
> sym(psyms
);
4333 elfcpp::STT st_type
= sym
.get_st_type();
4334 Symbol_value
<32>& lv((*plocal_values
)[i
]);
4335 Arm_address input_value
= lv
.input_value();
4337 // Check to see if this is a mapping symbol.
4338 const char* sym_name
= pnames
+ sym
.get_st_name();
4339 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
4341 unsigned int input_shndx
= sym
.get_st_shndx();
4343 // Strip of LSB in case this is a THUMB symbol.
4344 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
4345 this->mapping_symbols_info_
[msp
] = sym_name
[1];
4348 if (st_type
== elfcpp::STT_ARM_TFUNC
4349 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
4351 // This is a THUMB function. Mark this and canonicalize the
4352 // symbol value by setting LSB.
4353 this->local_symbol_is_thumb_function_
[i
] = true;
4354 if ((input_value
& 1) == 0)
4355 lv
.set_input_value(input_value
| 1);
4360 // Relocate sections.
4361 template<bool big_endian
>
4363 Arm_relobj
<big_endian
>::do_relocate_sections(
4364 const Symbol_table
* symtab
,
4365 const Layout
* layout
,
4366 const unsigned char* pshdrs
,
4367 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
4369 // Call parent to relocate sections.
4370 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
4373 // We do not generate stubs if doing a relocatable link.
4374 if (parameters
->options().relocatable())
4377 // Relocate stub tables.
4378 unsigned int shnum
= this->shnum();
4380 Target_arm
<big_endian
>* arm_target
=
4381 Target_arm
<big_endian
>::default_target();
4383 Relocate_info
<32, big_endian
> relinfo
;
4384 relinfo
.symtab
= symtab
;
4385 relinfo
.layout
= layout
;
4386 relinfo
.object
= this;
4388 for (unsigned int i
= 1; i
< shnum
; ++i
)
4390 Arm_input_section
<big_endian
>* arm_input_section
=
4391 arm_target
->find_arm_input_section(this, i
);
4393 if (arm_input_section
== NULL
4394 || !arm_input_section
->is_stub_table_owner()
4395 || arm_input_section
->stub_table()->empty())
4398 // We cannot discard a section if it owns a stub table.
4399 Output_section
* os
= this->output_section(i
);
4400 gold_assert(os
!= NULL
);
4402 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
4403 relinfo
.reloc_shdr
= NULL
;
4404 relinfo
.data_shndx
= i
;
4405 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
4407 gold_assert((*pviews
)[i
].view
!= NULL
);
4409 // We are passed the output section view. Adjust it to cover the
4411 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
4412 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
4413 && ((stub_table
->address() + stub_table
->data_size())
4414 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
4416 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
4417 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
4418 Arm_address address
= stub_table
->address();
4419 section_size_type view_size
= stub_table
->data_size();
4421 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
4426 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4429 template<bool big_endian
>
4430 Attributes_section_data
*
4431 read_arm_attributes_section(
4433 Read_symbols_data
*sd
)
4435 // Read the attributes section if there is one.
4436 // We read from the end because gas seems to put it near the end of
4437 // the section headers.
4438 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4439 const unsigned char *ps
=
4440 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
4441 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
4443 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4444 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
4446 section_offset_type section_offset
= shdr
.get_sh_offset();
4447 section_size_type section_size
=
4448 convert_to_section_size_type(shdr
.get_sh_size());
4449 File_view
* view
= object
->get_lasting_view(section_offset
,
4450 section_size
, true, false);
4451 return new Attributes_section_data(view
->data(), section_size
);
4457 // Read the symbol information.
4459 template<bool big_endian
>
4461 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4463 // Call parent class to read symbol information.
4464 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
4466 // Read processor-specific flags in ELF file header.
4467 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4468 elfcpp::Elf_sizes
<32>::ehdr_size
,
4470 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4471 this->processor_specific_flags_
= ehdr
.get_e_flags();
4472 this->attributes_section_data_
=
4473 read_arm_attributes_section
<big_endian
>(this, sd
);
4476 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4477 // sections for unwinding. These sections are referenced implicitly by
4478 // text sections linked in the section headers. If we ignore these implict
4479 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4480 // will be garbage-collected incorrectly. Hence we override the same function
4481 // in the base class to handle these implicit references.
4483 template<bool big_endian
>
4485 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
4487 Read_relocs_data
* rd
)
4489 // First, call base class method to process relocations in this object.
4490 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
4492 unsigned int shnum
= this->shnum();
4493 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4494 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4498 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4499 // to these from the linked text sections.
4500 const unsigned char* ps
= pshdrs
+ shdr_size
;
4501 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
4503 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4504 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
4506 // Found an .ARM.exidx section, add it to the set of reachable
4507 // sections from its linked text section.
4508 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
4509 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
4514 // Arm_dynobj methods.
4516 // Read the symbol information.
4518 template<bool big_endian
>
4520 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4522 // Call parent class to read symbol information.
4523 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
4525 // Read processor-specific flags in ELF file header.
4526 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4527 elfcpp::Elf_sizes
<32>::ehdr_size
,
4529 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4530 this->processor_specific_flags_
= ehdr
.get_e_flags();
4531 this->attributes_section_data_
=
4532 read_arm_attributes_section
<big_endian
>(this, sd
);
4535 // Stub_addend_reader methods.
4537 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4539 template<bool big_endian
>
4540 elfcpp::Elf_types
<32>::Elf_Swxword
4541 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
4542 unsigned int r_type
,
4543 const unsigned char* view
,
4544 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
4546 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
4550 case elfcpp::R_ARM_CALL
:
4551 case elfcpp::R_ARM_JUMP24
:
4552 case elfcpp::R_ARM_PLT32
:
4554 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4555 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4556 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4557 return utils::sign_extend
<26>(val
<< 2);
4560 case elfcpp::R_ARM_THM_CALL
:
4561 case elfcpp::R_ARM_THM_JUMP24
:
4562 case elfcpp::R_ARM_THM_XPC22
:
4564 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4565 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4566 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4567 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4568 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
4571 case elfcpp::R_ARM_THM_JUMP19
:
4573 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4574 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4575 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4576 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4577 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
4585 // A class to handle the PLT data.
4587 template<bool big_endian
>
4588 class Output_data_plt_arm
: public Output_section_data
4591 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
4594 Output_data_plt_arm(Layout
*, Output_data_space
*);
4596 // Add an entry to the PLT.
4598 add_entry(Symbol
* gsym
);
4600 // Return the .rel.plt section data.
4601 const Reloc_section
*
4603 { return this->rel_
; }
4607 do_adjust_output_section(Output_section
* os
);
4609 // Write to a map file.
4611 do_print_to_mapfile(Mapfile
* mapfile
) const
4612 { mapfile
->print_output_data(this, _("** PLT")); }
4615 // Template for the first PLT entry.
4616 static const uint32_t first_plt_entry
[5];
4618 // Template for subsequent PLT entries.
4619 static const uint32_t plt_entry
[3];
4621 // Set the final size.
4623 set_final_data_size()
4625 this->set_data_size(sizeof(first_plt_entry
)
4626 + this->count_
* sizeof(plt_entry
));
4629 // Write out the PLT data.
4631 do_write(Output_file
*);
4633 // The reloc section.
4634 Reloc_section
* rel_
;
4635 // The .got.plt section.
4636 Output_data_space
* got_plt_
;
4637 // The number of PLT entries.
4638 unsigned int count_
;
4641 // Create the PLT section. The ordinary .got section is an argument,
4642 // since we need to refer to the start. We also create our own .got
4643 // section just for PLT entries.
4645 template<bool big_endian
>
4646 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
4647 Output_data_space
* got_plt
)
4648 : Output_section_data(4), got_plt_(got_plt
), count_(0)
4650 this->rel_
= new Reloc_section(false);
4651 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
4652 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
4656 template<bool big_endian
>
4658 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
4663 // Add an entry to the PLT.
4665 template<bool big_endian
>
4667 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
4669 gold_assert(!gsym
->has_plt_offset());
4671 // Note that when setting the PLT offset we skip the initial
4672 // reserved PLT entry.
4673 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
4674 + sizeof(first_plt_entry
));
4678 section_offset_type got_offset
= this->got_plt_
->current_data_size();
4680 // Every PLT entry needs a GOT entry which points back to the PLT
4681 // entry (this will be changed by the dynamic linker, normally
4682 // lazily when the function is called).
4683 this->got_plt_
->set_current_data_size(got_offset
+ 4);
4685 // Every PLT entry needs a reloc.
4686 gsym
->set_needs_dynsym_entry();
4687 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
4690 // Note that we don't need to save the symbol. The contents of the
4691 // PLT are independent of which symbols are used. The symbols only
4692 // appear in the relocations.
4696 // FIXME: This is not very flexible. Right now this has only been tested
4697 // on armv5te. If we are to support additional architecture features like
4698 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4700 // The first entry in the PLT.
4701 template<bool big_endian
>
4702 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
4704 0xe52de004, // str lr, [sp, #-4]!
4705 0xe59fe004, // ldr lr, [pc, #4]
4706 0xe08fe00e, // add lr, pc, lr
4707 0xe5bef008, // ldr pc, [lr, #8]!
4708 0x00000000, // &GOT[0] - .
4711 // Subsequent entries in the PLT.
4713 template<bool big_endian
>
4714 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
4716 0xe28fc600, // add ip, pc, #0xNN00000
4717 0xe28cca00, // add ip, ip, #0xNN000
4718 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4721 // Write out the PLT. This uses the hand-coded instructions above,
4722 // and adjusts them as needed. This is all specified by the arm ELF
4723 // Processor Supplement.
4725 template<bool big_endian
>
4727 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4729 const off_t offset
= this->offset();
4730 const section_size_type oview_size
=
4731 convert_to_section_size_type(this->data_size());
4732 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4734 const off_t got_file_offset
= this->got_plt_
->offset();
4735 const section_size_type got_size
=
4736 convert_to_section_size_type(this->got_plt_
->data_size());
4737 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4739 unsigned char* pov
= oview
;
4741 Arm_address plt_address
= this->address();
4742 Arm_address got_address
= this->got_plt_
->address();
4744 // Write first PLT entry. All but the last word are constants.
4745 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4746 / sizeof(plt_entry
[0]));
4747 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4748 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4749 // Last word in first PLT entry is &GOT[0] - .
4750 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4751 got_address
- (plt_address
+ 16));
4752 pov
+= sizeof(first_plt_entry
);
4754 unsigned char* got_pov
= got_view
;
4756 memset(got_pov
, 0, 12);
4759 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4760 unsigned int plt_offset
= sizeof(first_plt_entry
);
4761 unsigned int plt_rel_offset
= 0;
4762 unsigned int got_offset
= 12;
4763 const unsigned int count
= this->count_
;
4764 for (unsigned int i
= 0;
4767 pov
+= sizeof(plt_entry
),
4769 plt_offset
+= sizeof(plt_entry
),
4770 plt_rel_offset
+= rel_size
,
4773 // Set and adjust the PLT entry itself.
4774 int32_t offset
= ((got_address
+ got_offset
)
4775 - (plt_address
+ plt_offset
+ 8));
4777 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4778 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4779 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4780 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4781 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4782 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4783 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4785 // Set the entry in the GOT.
4786 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4789 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4790 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4792 of
->write_output_view(offset
, oview_size
, oview
);
4793 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4796 // Create a PLT entry for a global symbol.
4798 template<bool big_endian
>
4800 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
4803 if (gsym
->has_plt_offset())
4806 if (this->plt_
== NULL
)
4808 // Create the GOT sections first.
4809 this->got_section(symtab
, layout
);
4811 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
4812 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4814 | elfcpp::SHF_EXECINSTR
),
4815 this->plt_
, false, false, false, false);
4817 this->plt_
->add_entry(gsym
);
4820 // Report an unsupported relocation against a local symbol.
4822 template<bool big_endian
>
4824 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4825 Sized_relobj
<32, big_endian
>* object
,
4826 unsigned int r_type
)
4828 gold_error(_("%s: unsupported reloc %u against local symbol"),
4829 object
->name().c_str(), r_type
);
4832 // We are about to emit a dynamic relocation of type R_TYPE. If the
4833 // dynamic linker does not support it, issue an error. The GNU linker
4834 // only issues a non-PIC error for an allocated read-only section.
4835 // Here we know the section is allocated, but we don't know that it is
4836 // read-only. But we check for all the relocation types which the
4837 // glibc dynamic linker supports, so it seems appropriate to issue an
4838 // error even if the section is not read-only.
4840 template<bool big_endian
>
4842 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4843 unsigned int r_type
)
4847 // These are the relocation types supported by glibc for ARM.
4848 case elfcpp::R_ARM_RELATIVE
:
4849 case elfcpp::R_ARM_COPY
:
4850 case elfcpp::R_ARM_GLOB_DAT
:
4851 case elfcpp::R_ARM_JUMP_SLOT
:
4852 case elfcpp::R_ARM_ABS32
:
4853 case elfcpp::R_ARM_ABS32_NOI
:
4854 case elfcpp::R_ARM_PC24
:
4855 // FIXME: The following 3 types are not supported by Android's dynamic
4857 case elfcpp::R_ARM_TLS_DTPMOD32
:
4858 case elfcpp::R_ARM_TLS_DTPOFF32
:
4859 case elfcpp::R_ARM_TLS_TPOFF32
:
4863 // This prevents us from issuing more than one error per reloc
4864 // section. But we can still wind up issuing more than one
4865 // error per object file.
4866 if (this->issued_non_pic_error_
)
4868 object
->error(_("requires unsupported dynamic reloc; "
4869 "recompile with -fPIC"));
4870 this->issued_non_pic_error_
= true;
4873 case elfcpp::R_ARM_NONE
:
4878 // Scan a relocation for a local symbol.
4879 // FIXME: This only handles a subset of relocation types used by Android
4880 // on ARM v5te devices.
4882 template<bool big_endian
>
4884 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4887 Sized_relobj
<32, big_endian
>* object
,
4888 unsigned int data_shndx
,
4889 Output_section
* output_section
,
4890 const elfcpp::Rel
<32, big_endian
>& reloc
,
4891 unsigned int r_type
,
4892 const elfcpp::Sym
<32, big_endian
>&)
4894 r_type
= get_real_reloc_type(r_type
);
4897 case elfcpp::R_ARM_NONE
:
4900 case elfcpp::R_ARM_ABS32
:
4901 case elfcpp::R_ARM_ABS32_NOI
:
4902 // If building a shared library (or a position-independent
4903 // executable), we need to create a dynamic relocation for
4904 // this location. The relocation applied at link time will
4905 // apply the link-time value, so we flag the location with
4906 // an R_ARM_RELATIVE relocation so the dynamic loader can
4907 // relocate it easily.
4908 if (parameters
->options().output_is_position_independent())
4910 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4911 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4912 // If we are to add more other reloc types than R_ARM_ABS32,
4913 // we need to add check_non_pic(object, r_type) here.
4914 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4915 output_section
, data_shndx
,
4916 reloc
.get_r_offset());
4920 case elfcpp::R_ARM_REL32
:
4921 case elfcpp::R_ARM_THM_CALL
:
4922 case elfcpp::R_ARM_CALL
:
4923 case elfcpp::R_ARM_PREL31
:
4924 case elfcpp::R_ARM_JUMP24
:
4925 case elfcpp::R_ARM_PLT32
:
4926 case elfcpp::R_ARM_THM_ABS5
:
4927 case elfcpp::R_ARM_ABS8
:
4928 case elfcpp::R_ARM_ABS12
:
4929 case elfcpp::R_ARM_ABS16
:
4930 case elfcpp::R_ARM_BASE_ABS
:
4931 case elfcpp::R_ARM_MOVW_ABS_NC
:
4932 case elfcpp::R_ARM_MOVT_ABS
:
4933 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4934 case elfcpp::R_ARM_THM_MOVT_ABS
:
4935 case elfcpp::R_ARM_MOVW_PREL_NC
:
4936 case elfcpp::R_ARM_MOVT_PREL
:
4937 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4938 case elfcpp::R_ARM_THM_MOVT_PREL
:
4941 case elfcpp::R_ARM_GOTOFF32
:
4942 // We need a GOT section:
4943 target
->got_section(symtab
, layout
);
4946 case elfcpp::R_ARM_BASE_PREL
:
4947 // FIXME: What about this?
4950 case elfcpp::R_ARM_GOT_BREL
:
4951 case elfcpp::R_ARM_GOT_PREL
:
4953 // The symbol requires a GOT entry.
4954 Output_data_got
<32, big_endian
>* got
=
4955 target
->got_section(symtab
, layout
);
4956 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4957 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4959 // If we are generating a shared object, we need to add a
4960 // dynamic RELATIVE relocation for this symbol's GOT entry.
4961 if (parameters
->options().output_is_position_independent())
4963 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4964 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4965 rel_dyn
->add_local_relative(
4966 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4967 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4973 case elfcpp::R_ARM_TARGET1
:
4974 // This should have been mapped to another type already.
4976 case elfcpp::R_ARM_COPY
:
4977 case elfcpp::R_ARM_GLOB_DAT
:
4978 case elfcpp::R_ARM_JUMP_SLOT
:
4979 case elfcpp::R_ARM_RELATIVE
:
4980 // These are relocations which should only be seen by the
4981 // dynamic linker, and should never be seen here.
4982 gold_error(_("%s: unexpected reloc %u in object file"),
4983 object
->name().c_str(), r_type
);
4987 unsupported_reloc_local(object
, r_type
);
4992 // Report an unsupported relocation against a global symbol.
4994 template<bool big_endian
>
4996 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4997 Sized_relobj
<32, big_endian
>* object
,
4998 unsigned int r_type
,
5001 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5002 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5005 // Scan a relocation for a global symbol.
5006 // FIXME: This only handles a subset of relocation types used by Android
5007 // on ARM v5te devices.
5009 template<bool big_endian
>
5011 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
5014 Sized_relobj
<32, big_endian
>* object
,
5015 unsigned int data_shndx
,
5016 Output_section
* output_section
,
5017 const elfcpp::Rel
<32, big_endian
>& reloc
,
5018 unsigned int r_type
,
5021 r_type
= get_real_reloc_type(r_type
);
5024 case elfcpp::R_ARM_NONE
:
5027 case elfcpp::R_ARM_ABS32
:
5028 case elfcpp::R_ARM_ABS32_NOI
:
5030 // Make a dynamic relocation if necessary.
5031 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
5033 if (target
->may_need_copy_reloc(gsym
))
5035 target
->copy_reloc(symtab
, layout
, object
,
5036 data_shndx
, output_section
, gsym
, reloc
);
5038 else if (gsym
->can_use_relative_reloc(false))
5040 // If we are to add more other reloc types than R_ARM_ABS32,
5041 // we need to add check_non_pic(object, r_type) here.
5042 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5043 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
5044 output_section
, object
,
5045 data_shndx
, reloc
.get_r_offset());
5049 // If we are to add more other reloc types than R_ARM_ABS32,
5050 // we need to add check_non_pic(object, r_type) here.
5051 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5052 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5053 data_shndx
, reloc
.get_r_offset());
5059 case elfcpp::R_ARM_MOVW_ABS_NC
:
5060 case elfcpp::R_ARM_MOVT_ABS
:
5061 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5062 case elfcpp::R_ARM_THM_MOVT_ABS
:
5063 case elfcpp::R_ARM_MOVW_PREL_NC
:
5064 case elfcpp::R_ARM_MOVT_PREL
:
5065 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5066 case elfcpp::R_ARM_THM_MOVT_PREL
:
5069 case elfcpp::R_ARM_THM_ABS5
:
5070 case elfcpp::R_ARM_ABS8
:
5071 case elfcpp::R_ARM_ABS12
:
5072 case elfcpp::R_ARM_ABS16
:
5073 case elfcpp::R_ARM_BASE_ABS
:
5075 // No dynamic relocs of this kinds.
5076 // Report the error in case of PIC.
5077 int flags
= Symbol::NON_PIC_REF
;
5078 if (gsym
->type() == elfcpp::STT_FUNC
5079 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5080 flags
|= Symbol::FUNCTION_CALL
;
5081 if (gsym
->needs_dynamic_reloc(flags
))
5082 check_non_pic(object
, r_type
);
5086 case elfcpp::R_ARM_REL32
:
5087 case elfcpp::R_ARM_PREL31
:
5089 // Make a dynamic relocation if necessary.
5090 int flags
= Symbol::NON_PIC_REF
;
5091 if (gsym
->needs_dynamic_reloc(flags
))
5093 if (target
->may_need_copy_reloc(gsym
))
5095 target
->copy_reloc(symtab
, layout
, object
,
5096 data_shndx
, output_section
, gsym
, reloc
);
5100 check_non_pic(object
, r_type
);
5101 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5102 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5103 data_shndx
, reloc
.get_r_offset());
5109 case elfcpp::R_ARM_JUMP24
:
5110 case elfcpp::R_ARM_THM_JUMP24
:
5111 case elfcpp::R_ARM_CALL
:
5112 case elfcpp::R_ARM_THM_CALL
:
5114 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
5115 target
->make_plt_entry(symtab
, layout
, gsym
);
5118 // Check to see if this is a function that would need a PLT
5119 // but does not get one because the function symbol is untyped.
5120 // This happens in assembly code missing a proper .type directive.
5121 if ((!gsym
->is_undefined() || parameters
->options().shared())
5122 && !parameters
->doing_static_link()
5123 && gsym
->type() == elfcpp::STT_NOTYPE
5124 && (gsym
->is_from_dynobj()
5125 || gsym
->is_undefined()
5126 || gsym
->is_preemptible()))
5127 gold_error(_("%s is not a function."),
5128 gsym
->demangled_name().c_str());
5132 case elfcpp::R_ARM_PLT32
:
5133 // If the symbol is fully resolved, this is just a relative
5134 // local reloc. Otherwise we need a PLT entry.
5135 if (gsym
->final_value_is_known())
5137 // If building a shared library, we can also skip the PLT entry
5138 // if the symbol is defined in the output file and is protected
5140 if (gsym
->is_defined()
5141 && !gsym
->is_from_dynobj()
5142 && !gsym
->is_preemptible())
5144 target
->make_plt_entry(symtab
, layout
, gsym
);
5147 case elfcpp::R_ARM_GOTOFF32
:
5148 // We need a GOT section.
5149 target
->got_section(symtab
, layout
);
5152 case elfcpp::R_ARM_BASE_PREL
:
5153 // FIXME: What about this?
5156 case elfcpp::R_ARM_GOT_BREL
:
5157 case elfcpp::R_ARM_GOT_PREL
:
5159 // The symbol requires a GOT entry.
5160 Output_data_got
<32, big_endian
>* got
=
5161 target
->got_section(symtab
, layout
);
5162 if (gsym
->final_value_is_known())
5163 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5166 // If this symbol is not fully resolved, we need to add a
5167 // GOT entry with a dynamic relocation.
5168 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5169 if (gsym
->is_from_dynobj()
5170 || gsym
->is_undefined()
5171 || gsym
->is_preemptible())
5172 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
5173 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
5176 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
5177 rel_dyn
->add_global_relative(
5178 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
5179 gsym
->got_offset(GOT_TYPE_STANDARD
));
5185 case elfcpp::R_ARM_TARGET1
:
5186 // This should have been mapped to another type already.
5188 case elfcpp::R_ARM_COPY
:
5189 case elfcpp::R_ARM_GLOB_DAT
:
5190 case elfcpp::R_ARM_JUMP_SLOT
:
5191 case elfcpp::R_ARM_RELATIVE
:
5192 // These are relocations which should only be seen by the
5193 // dynamic linker, and should never be seen here.
5194 gold_error(_("%s: unexpected reloc %u in object file"),
5195 object
->name().c_str(), r_type
);
5199 unsupported_reloc_global(object
, r_type
, gsym
);
5204 // Process relocations for gc.
5206 template<bool big_endian
>
5208 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
5210 Sized_relobj
<32, big_endian
>* object
,
5211 unsigned int data_shndx
,
5213 const unsigned char* prelocs
,
5215 Output_section
* output_section
,
5216 bool needs_special_offset_handling
,
5217 size_t local_symbol_count
,
5218 const unsigned char* plocal_symbols
)
5220 typedef Target_arm
<big_endian
> Arm
;
5221 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5223 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
5232 needs_special_offset_handling
,
5237 // Scan relocations for a section.
5239 template<bool big_endian
>
5241 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
5243 Sized_relobj
<32, big_endian
>* object
,
5244 unsigned int data_shndx
,
5245 unsigned int sh_type
,
5246 const unsigned char* prelocs
,
5248 Output_section
* output_section
,
5249 bool needs_special_offset_handling
,
5250 size_t local_symbol_count
,
5251 const unsigned char* plocal_symbols
)
5253 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5254 if (sh_type
== elfcpp::SHT_RELA
)
5256 gold_error(_("%s: unsupported RELA reloc section"),
5257 object
->name().c_str());
5261 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
5270 needs_special_offset_handling
,
5275 // Finalize the sections.
5277 template<bool big_endian
>
5279 Target_arm
<big_endian
>::do_finalize_sections(
5281 const Input_objects
* input_objects
,
5282 Symbol_table
* symtab
)
5284 // Merge processor-specific flags.
5285 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
5286 p
!= input_objects
->relobj_end();
5289 Arm_relobj
<big_endian
>* arm_relobj
=
5290 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
5291 this->merge_processor_specific_flags(
5293 arm_relobj
->processor_specific_flags());
5294 this->merge_object_attributes(arm_relobj
->name().c_str(),
5295 arm_relobj
->attributes_section_data());
5299 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
5300 p
!= input_objects
->dynobj_end();
5303 Arm_dynobj
<big_endian
>* arm_dynobj
=
5304 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
5305 this->merge_processor_specific_flags(
5307 arm_dynobj
->processor_specific_flags());
5308 this->merge_object_attributes(arm_dynobj
->name().c_str(),
5309 arm_dynobj
->attributes_section_data());
5313 Object_attribute
* attr
=
5314 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
5315 if (attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
5316 this->set_may_use_blx(true);
5318 // Fill in some more dynamic tags.
5319 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5321 : this->plt_
->rel_plt());
5322 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
5323 this->rel_dyn_
, true);
5325 // Emit any relocs we saved in an attempt to avoid generating COPY
5327 if (this->copy_relocs_
.any_saved_relocs())
5328 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
5330 // Handle the .ARM.exidx section.
5331 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
5332 if (exidx_section
!= NULL
5333 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
5334 && !parameters
->options().relocatable())
5336 // Create __exidx_start and __exdix_end symbols.
5337 symtab
->define_in_output_data("__exidx_start", NULL
,
5338 Symbol_table::PREDEFINED
,
5339 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5340 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5342 symtab
->define_in_output_data("__exidx_end", NULL
,
5343 Symbol_table::PREDEFINED
,
5344 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5345 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5348 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5349 // the .ARM.exidx section.
5350 if (!layout
->script_options()->saw_phdrs_clause())
5352 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
5354 Output_segment
* exidx_segment
=
5355 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
5356 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
5361 // Create an .ARM.attributes section if there is not one already.
5362 Output_attributes_section_data
* attributes_section
=
5363 new Output_attributes_section_data(*this->attributes_section_data_
);
5364 layout
->add_output_section_data(".ARM.attributes",
5365 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
5366 attributes_section
, false, false, false,
5370 // Return whether a direct absolute static relocation needs to be applied.
5371 // In cases where Scan::local() or Scan::global() has created
5372 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5373 // of the relocation is carried in the data, and we must not
5374 // apply the static relocation.
5376 template<bool big_endian
>
5378 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
5379 const Sized_symbol
<32>* gsym
,
5382 Output_section
* output_section
)
5384 // If the output section is not allocated, then we didn't call
5385 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5387 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
5390 // For local symbols, we will have created a non-RELATIVE dynamic
5391 // relocation only if (a) the output is position independent,
5392 // (b) the relocation is absolute (not pc- or segment-relative), and
5393 // (c) the relocation is not 32 bits wide.
5395 return !(parameters
->options().output_is_position_independent()
5396 && (ref_flags
& Symbol::ABSOLUTE_REF
)
5399 // For global symbols, we use the same helper routines used in the
5400 // scan pass. If we did not create a dynamic relocation, or if we
5401 // created a RELATIVE dynamic relocation, we should apply the static
5403 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
5404 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
5405 && gsym
->can_use_relative_reloc(ref_flags
5406 & Symbol::FUNCTION_CALL
);
5407 return !has_dyn
|| is_rel
;
5410 // Perform a relocation.
5412 template<bool big_endian
>
5414 Target_arm
<big_endian
>::Relocate::relocate(
5415 const Relocate_info
<32, big_endian
>* relinfo
,
5417 Output_section
*output_section
,
5419 const elfcpp::Rel
<32, big_endian
>& rel
,
5420 unsigned int r_type
,
5421 const Sized_symbol
<32>* gsym
,
5422 const Symbol_value
<32>* psymval
,
5423 unsigned char* view
,
5424 Arm_address address
,
5425 section_size_type
/* view_size */ )
5427 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
5429 r_type
= get_real_reloc_type(r_type
);
5431 const Arm_relobj
<big_endian
>* object
=
5432 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5434 // If the final branch target of a relocation is THUMB instruction, this
5435 // is 1. Otherwise it is 0.
5436 Arm_address thumb_bit
= 0;
5437 Symbol_value
<32> symval
;
5438 bool is_weakly_undefined_without_plt
= false;
5439 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
5443 // This is a global symbol. Determine if we use PLT and if the
5444 // final target is THUMB.
5445 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
5447 // This uses a PLT, change the symbol value.
5448 symval
.set_output_value(target
->plt_section()->address()
5449 + gsym
->plt_offset());
5452 else if (gsym
->is_weak_undefined())
5454 // This is a weakly undefined symbol and we do not use PLT
5455 // for this relocation. A branch targeting this symbol will
5456 // be converted into an NOP.
5457 is_weakly_undefined_without_plt
= true;
5461 // Set thumb bit if symbol:
5462 // -Has type STT_ARM_TFUNC or
5463 // -Has type STT_FUNC, is defined and with LSB in value set.
5465 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5466 || (gsym
->type() == elfcpp::STT_FUNC
5467 && !gsym
->is_undefined()
5468 && ((psymval
->value(object
, 0) & 1) != 0)))
5475 // This is a local symbol. Determine if the final target is THUMB.
5476 // We saved this information when all the local symbols were read.
5477 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
5478 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5479 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
5484 // This is a fake relocation synthesized for a stub. It does not have
5485 // a real symbol. We just look at the LSB of the symbol value to
5486 // determine if the target is THUMB or not.
5487 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
5490 // Strip LSB if this points to a THUMB target.
5492 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5493 && ((psymval
->value(object
, 0) & 1) != 0))
5495 Arm_address stripped_value
=
5496 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
5497 symval
.set_output_value(stripped_value
);
5501 // Get the GOT offset if needed.
5502 // The GOT pointer points to the end of the GOT section.
5503 // We need to subtract the size of the GOT section to get
5504 // the actual offset to use in the relocation.
5505 bool have_got_offset
= false;
5506 unsigned int got_offset
= 0;
5509 case elfcpp::R_ARM_GOT_BREL
:
5510 case elfcpp::R_ARM_GOT_PREL
:
5513 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5514 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
5515 - target
->got_size());
5519 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5520 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5521 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
5522 - target
->got_size());
5524 have_got_offset
= true;
5531 // To look up relocation stubs, we need to pass the symbol table index of
5533 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5535 typename
Arm_relocate_functions::Status reloc_status
=
5536 Arm_relocate_functions::STATUS_OKAY
;
5539 case elfcpp::R_ARM_NONE
:
5542 case elfcpp::R_ARM_ABS8
:
5543 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5545 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
5548 case elfcpp::R_ARM_ABS12
:
5549 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5551 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
5554 case elfcpp::R_ARM_ABS16
:
5555 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5557 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
5560 case elfcpp::R_ARM_ABS32
:
5561 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5563 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5567 case elfcpp::R_ARM_ABS32_NOI
:
5568 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5570 // No thumb bit for this relocation: (S + A)
5571 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5575 case elfcpp::R_ARM_MOVW_ABS_NC
:
5576 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5578 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
5582 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5583 "a shared object; recompile with -fPIC"));
5586 case elfcpp::R_ARM_MOVT_ABS
:
5587 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5589 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
5591 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5592 "a shared object; recompile with -fPIC"));
5595 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5596 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5598 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
5602 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5603 "making a shared object; recompile with -fPIC"));
5606 case elfcpp::R_ARM_THM_MOVT_ABS
:
5607 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5609 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
5612 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5613 "making a shared object; recompile with -fPIC"));
5616 case elfcpp::R_ARM_MOVW_PREL_NC
:
5617 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
5622 case elfcpp::R_ARM_MOVT_PREL
:
5623 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
5627 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5628 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
5633 case elfcpp::R_ARM_THM_MOVT_PREL
:
5634 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
5638 case elfcpp::R_ARM_REL32
:
5639 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5640 address
, thumb_bit
);
5643 case elfcpp::R_ARM_THM_ABS5
:
5644 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5646 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
5649 case elfcpp::R_ARM_THM_CALL
:
5651 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
5652 psymval
, address
, thumb_bit
,
5653 is_weakly_undefined_without_plt
);
5656 case elfcpp::R_ARM_XPC25
:
5658 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
5659 psymval
, address
, thumb_bit
,
5660 is_weakly_undefined_without_plt
);
5663 case elfcpp::R_ARM_THM_XPC22
:
5665 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
5666 psymval
, address
, thumb_bit
,
5667 is_weakly_undefined_without_plt
);
5670 case elfcpp::R_ARM_GOTOFF32
:
5672 Arm_address got_origin
;
5673 got_origin
= target
->got_plt_section()->address();
5674 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5675 got_origin
, thumb_bit
);
5679 case elfcpp::R_ARM_BASE_PREL
:
5682 // Get the addressing origin of the output segment defining the
5683 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5684 gold_assert(gsym
!= NULL
);
5685 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5686 origin
= gsym
->output_segment()->vaddr();
5687 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5688 origin
= gsym
->output_data()->address();
5691 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5692 _("cannot find origin of R_ARM_BASE_PREL"));
5695 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
5699 case elfcpp::R_ARM_BASE_ABS
:
5701 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5706 // Get the addressing origin of the output segment defining
5707 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5709 // R_ARM_BASE_ABS with the NULL symbol will give the
5710 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5711 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5712 origin
= target
->got_plt_section()->address();
5713 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5714 origin
= gsym
->output_segment()->vaddr();
5715 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5716 origin
= gsym
->output_data()->address();
5719 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5720 _("cannot find origin of R_ARM_BASE_ABS"));
5724 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5728 case elfcpp::R_ARM_GOT_BREL
:
5729 gold_assert(have_got_offset
);
5730 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5733 case elfcpp::R_ARM_GOT_PREL
:
5734 gold_assert(have_got_offset
);
5735 // Get the address origin for GOT PLT, which is allocated right
5736 // after the GOT section, to calculate an absolute address of
5737 // the symbol GOT entry (got_origin + got_offset).
5738 Arm_address got_origin
;
5739 got_origin
= target
->got_plt_section()->address();
5740 reloc_status
= Arm_relocate_functions::got_prel(view
,
5741 got_origin
+ got_offset
,
5745 case elfcpp::R_ARM_PLT32
:
5746 gold_assert(gsym
== NULL
5747 || gsym
->has_plt_offset()
5748 || gsym
->final_value_is_known()
5749 || (gsym
->is_defined()
5750 && !gsym
->is_from_dynobj()
5751 && !gsym
->is_preemptible()));
5753 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5754 psymval
, address
, thumb_bit
,
5755 is_weakly_undefined_without_plt
);
5758 case elfcpp::R_ARM_CALL
:
5760 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5761 psymval
, address
, thumb_bit
,
5762 is_weakly_undefined_without_plt
);
5765 case elfcpp::R_ARM_JUMP24
:
5767 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5768 psymval
, address
, thumb_bit
,
5769 is_weakly_undefined_without_plt
);
5772 case elfcpp::R_ARM_THM_JUMP24
:
5774 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5775 psymval
, address
, thumb_bit
,
5776 is_weakly_undefined_without_plt
);
5779 case elfcpp::R_ARM_PREL31
:
5780 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5781 address
, thumb_bit
);
5784 case elfcpp::R_ARM_TARGET1
:
5785 // This should have been mapped to another type already.
5787 case elfcpp::R_ARM_COPY
:
5788 case elfcpp::R_ARM_GLOB_DAT
:
5789 case elfcpp::R_ARM_JUMP_SLOT
:
5790 case elfcpp::R_ARM_RELATIVE
:
5791 // These are relocations which should only be seen by the
5792 // dynamic linker, and should never be seen here.
5793 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5794 _("unexpected reloc %u in object file"),
5799 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5800 _("unsupported reloc %u"),
5805 // Report any errors.
5806 switch (reloc_status
)
5808 case Arm_relocate_functions::STATUS_OKAY
:
5810 case Arm_relocate_functions::STATUS_OVERFLOW
:
5811 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5812 _("relocation overflow in relocation %u"),
5815 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5816 gold_error_at_location(
5820 _("unexpected opcode while processing relocation %u"),
5830 // Relocate section data.
5832 template<bool big_endian
>
5834 Target_arm
<big_endian
>::relocate_section(
5835 const Relocate_info
<32, big_endian
>* relinfo
,
5836 unsigned int sh_type
,
5837 const unsigned char* prelocs
,
5839 Output_section
* output_section
,
5840 bool needs_special_offset_handling
,
5841 unsigned char* view
,
5842 Arm_address address
,
5843 section_size_type view_size
,
5844 const Reloc_symbol_changes
* reloc_symbol_changes
)
5846 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5847 gold_assert(sh_type
== elfcpp::SHT_REL
);
5849 Arm_input_section
<big_endian
>* arm_input_section
=
5850 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5852 // This is an ARM input section and the view covers the whole output
5854 if (arm_input_section
!= NULL
)
5856 gold_assert(needs_special_offset_handling
);
5857 Arm_address section_address
= arm_input_section
->address();
5858 section_size_type section_size
= arm_input_section
->data_size();
5860 gold_assert((arm_input_section
->address() >= address
)
5861 && ((arm_input_section
->address()
5862 + arm_input_section
->data_size())
5863 <= (address
+ view_size
)));
5865 off_t offset
= section_address
- address
;
5868 view_size
= section_size
;
5871 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5878 needs_special_offset_handling
,
5882 reloc_symbol_changes
);
5885 // Return the size of a relocation while scanning during a relocatable
5888 template<bool big_endian
>
5890 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5891 unsigned int r_type
,
5894 r_type
= get_real_reloc_type(r_type
);
5897 case elfcpp::R_ARM_NONE
:
5900 case elfcpp::R_ARM_ABS8
:
5903 case elfcpp::R_ARM_ABS16
:
5904 case elfcpp::R_ARM_THM_ABS5
:
5907 case elfcpp::R_ARM_ABS32
:
5908 case elfcpp::R_ARM_ABS32_NOI
:
5909 case elfcpp::R_ARM_ABS12
:
5910 case elfcpp::R_ARM_BASE_ABS
:
5911 case elfcpp::R_ARM_REL32
:
5912 case elfcpp::R_ARM_THM_CALL
:
5913 case elfcpp::R_ARM_GOTOFF32
:
5914 case elfcpp::R_ARM_BASE_PREL
:
5915 case elfcpp::R_ARM_GOT_BREL
:
5916 case elfcpp::R_ARM_GOT_PREL
:
5917 case elfcpp::R_ARM_PLT32
:
5918 case elfcpp::R_ARM_CALL
:
5919 case elfcpp::R_ARM_JUMP24
:
5920 case elfcpp::R_ARM_PREL31
:
5921 case elfcpp::R_ARM_MOVW_ABS_NC
:
5922 case elfcpp::R_ARM_MOVT_ABS
:
5923 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5924 case elfcpp::R_ARM_THM_MOVT_ABS
:
5925 case elfcpp::R_ARM_MOVW_PREL_NC
:
5926 case elfcpp::R_ARM_MOVT_PREL
:
5927 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5928 case elfcpp::R_ARM_THM_MOVT_PREL
:
5931 case elfcpp::R_ARM_TARGET1
:
5932 // This should have been mapped to another type already.
5934 case elfcpp::R_ARM_COPY
:
5935 case elfcpp::R_ARM_GLOB_DAT
:
5936 case elfcpp::R_ARM_JUMP_SLOT
:
5937 case elfcpp::R_ARM_RELATIVE
:
5938 // These are relocations which should only be seen by the
5939 // dynamic linker, and should never be seen here.
5940 gold_error(_("%s: unexpected reloc %u in object file"),
5941 object
->name().c_str(), r_type
);
5945 object
->error(_("unsupported reloc %u in object file"), r_type
);
5950 // Scan the relocs during a relocatable link.
5952 template<bool big_endian
>
5954 Target_arm
<big_endian
>::scan_relocatable_relocs(
5955 Symbol_table
* symtab
,
5957 Sized_relobj
<32, big_endian
>* object
,
5958 unsigned int data_shndx
,
5959 unsigned int sh_type
,
5960 const unsigned char* prelocs
,
5962 Output_section
* output_section
,
5963 bool needs_special_offset_handling
,
5964 size_t local_symbol_count
,
5965 const unsigned char* plocal_symbols
,
5966 Relocatable_relocs
* rr
)
5968 gold_assert(sh_type
== elfcpp::SHT_REL
);
5970 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5971 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5973 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5974 Scan_relocatable_relocs
>(
5982 needs_special_offset_handling
,
5988 // Relocate a section during a relocatable link.
5990 template<bool big_endian
>
5992 Target_arm
<big_endian
>::relocate_for_relocatable(
5993 const Relocate_info
<32, big_endian
>* relinfo
,
5994 unsigned int sh_type
,
5995 const unsigned char* prelocs
,
5997 Output_section
* output_section
,
5998 off_t offset_in_output_section
,
5999 const Relocatable_relocs
* rr
,
6000 unsigned char* view
,
6001 Arm_address view_address
,
6002 section_size_type view_size
,
6003 unsigned char* reloc_view
,
6004 section_size_type reloc_view_size
)
6006 gold_assert(sh_type
== elfcpp::SHT_REL
);
6008 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
6013 offset_in_output_section
,
6022 // Return the value to use for a dynamic symbol which requires special
6023 // treatment. This is how we support equality comparisons of function
6024 // pointers across shared library boundaries, as described in the
6025 // processor specific ABI supplement.
6027 template<bool big_endian
>
6029 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6031 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6032 return this->plt_section()->address() + gsym
->plt_offset();
6035 // Map platform-specific relocs to real relocs
6037 template<bool big_endian
>
6039 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
6043 case elfcpp::R_ARM_TARGET1
:
6044 // This is either R_ARM_ABS32 or R_ARM_REL32;
6045 return elfcpp::R_ARM_ABS32
;
6047 case elfcpp::R_ARM_TARGET2
:
6048 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6049 return elfcpp::R_ARM_GOT_PREL
;
6056 // Whether if two EABI versions V1 and V2 are compatible.
6058 template<bool big_endian
>
6060 Target_arm
<big_endian
>::are_eabi_versions_compatible(
6061 elfcpp::Elf_Word v1
,
6062 elfcpp::Elf_Word v2
)
6064 // v4 and v5 are the same spec before and after it was released,
6065 // so allow mixing them.
6066 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
6067 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
6073 // Combine FLAGS from an input object called NAME and the processor-specific
6074 // flags in the ELF header of the output. Much of this is adapted from the
6075 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6076 // in bfd/elf32-arm.c.
6078 template<bool big_endian
>
6080 Target_arm
<big_endian
>::merge_processor_specific_flags(
6081 const std::string
& name
,
6082 elfcpp::Elf_Word flags
)
6084 if (this->are_processor_specific_flags_set())
6086 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
6088 // Nothing to merge if flags equal to those in output.
6089 if (flags
== out_flags
)
6092 // Complain about various flag mismatches.
6093 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
6094 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
6095 if (!this->are_eabi_versions_compatible(version1
, version2
))
6096 gold_error(_("Source object %s has EABI version %d but output has "
6097 "EABI version %d."),
6099 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
6100 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
6104 // If the input is the default architecture and had the default
6105 // flags then do not bother setting the flags for the output
6106 // architecture, instead allow future merges to do this. If no
6107 // future merges ever set these flags then they will retain their
6108 // uninitialised values, which surprise surprise, correspond
6109 // to the default values.
6113 // This is the first time, just copy the flags.
6114 // We only copy the EABI version for now.
6115 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
6119 // Adjust ELF file header.
6120 template<bool big_endian
>
6122 Target_arm
<big_endian
>::do_adjust_elf_header(
6123 unsigned char* view
,
6126 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
6128 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
6129 unsigned char e_ident
[elfcpp::EI_NIDENT
];
6130 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
6132 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6133 == elfcpp::EF_ARM_EABI_UNKNOWN
)
6134 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
6136 e_ident
[elfcpp::EI_OSABI
] = 0;
6137 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
6139 // FIXME: Do EF_ARM_BE8 adjustment.
6141 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
6142 oehdr
.put_e_ident(e_ident
);
6145 // do_make_elf_object to override the same function in the base class.
6146 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6147 // to store ARM specific information. Hence we need to have our own
6148 // ELF object creation.
6150 template<bool big_endian
>
6152 Target_arm
<big_endian
>::do_make_elf_object(
6153 const std::string
& name
,
6154 Input_file
* input_file
,
6155 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
6157 int et
= ehdr
.get_e_type();
6158 if (et
== elfcpp::ET_REL
)
6160 Arm_relobj
<big_endian
>* obj
=
6161 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6165 else if (et
== elfcpp::ET_DYN
)
6167 Sized_dynobj
<32, big_endian
>* obj
=
6168 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6174 gold_error(_("%s: unsupported ELF file type %d"),
6180 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6181 // Returns -1 if no architecture could be read.
6182 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6184 template<bool big_endian
>
6186 Target_arm
<big_endian
>::get_secondary_compatible_arch(
6187 const Attributes_section_data
* pasd
)
6189 const Object_attribute
*known_attributes
=
6190 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6192 // Note: the tag and its argument below are uleb128 values, though
6193 // currently-defined values fit in one byte for each.
6194 const std::string
& sv
=
6195 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
6197 && sv
.data()[0] == elfcpp::Tag_CPU_arch
6198 && (sv
.data()[1] & 128) != 128)
6199 return sv
.data()[1];
6201 // This tag is "safely ignorable", so don't complain if it looks funny.
6205 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6206 // The tag is removed if ARCH is -1.
6207 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6209 template<bool big_endian
>
6211 Target_arm
<big_endian
>::set_secondary_compatible_arch(
6212 Attributes_section_data
* pasd
,
6215 Object_attribute
*known_attributes
=
6216 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6220 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
6224 // Note: the tag and its argument below are uleb128 values, though
6225 // currently-defined values fit in one byte for each.
6227 sv
[0] = elfcpp::Tag_CPU_arch
;
6228 gold_assert(arch
!= 0);
6232 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
6235 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6237 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6239 template<bool big_endian
>
6241 Target_arm
<big_endian
>::tag_cpu_arch_combine(
6244 int* secondary_compat_out
,
6246 int secondary_compat
)
6248 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6249 static const int v6t2
[] =
6261 static const int v6k
[] =
6274 static const int v7
[] =
6288 static const int v6_m
[] =
6303 static const int v6s_m
[] =
6319 static const int v7e_m
[] =
6336 static const int v4t_plus_v6_m
[] =
6352 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
6354 static const int *comb
[] =
6362 // Pseudo-architecture.
6366 // Check we've not got a higher architecture than we know about.
6368 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
6370 gold_error(_("%s: unknown CPU architecture"), name
);
6374 // Override old tag if we have a Tag_also_compatible_with on the output.
6376 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
6377 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
6378 oldtag
= T(V4T_PLUS_V6_M
);
6380 // And override the new tag if we have a Tag_also_compatible_with on the
6383 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
6384 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
6385 newtag
= T(V4T_PLUS_V6_M
);
6387 // Architectures before V6KZ add features monotonically.
6388 int tagh
= std::max(oldtag
, newtag
);
6389 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
6392 int tagl
= std::min(oldtag
, newtag
);
6393 int result
= comb
[tagh
- T(V6T2
)][tagl
];
6395 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6396 // as the canonical version.
6397 if (result
== T(V4T_PLUS_V6_M
))
6400 *secondary_compat_out
= T(V6_M
);
6403 *secondary_compat_out
= -1;
6407 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6408 name
, oldtag
, newtag
);
6416 // Helper to print AEABI enum tag value.
6418 template<bool big_endian
>
6420 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
6422 static const char *aeabi_enum_names
[] =
6423 { "", "variable-size", "32-bit", "" };
6424 const size_t aeabi_enum_names_size
=
6425 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
6427 if (value
< aeabi_enum_names_size
)
6428 return std::string(aeabi_enum_names
[value
]);
6432 sprintf(buffer
, "<unknown value %u>", value
);
6433 return std::string(buffer
);
6437 // Return the string value to store in TAG_CPU_name.
6439 template<bool big_endian
>
6441 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
6443 static const char *name_table
[] = {
6444 // These aren't real CPU names, but we can't guess
6445 // that from the architecture version alone.
6461 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
6463 if (value
< name_table_size
)
6464 return std::string(name_table
[value
]);
6468 sprintf(buffer
, "<unknown CPU value %u>", value
);
6469 return std::string(buffer
);
6473 // Merge object attributes from input file called NAME with those of the
6474 // output. The input object attributes are in the object pointed by PASD.
6476 template<bool big_endian
>
6478 Target_arm
<big_endian
>::merge_object_attributes(
6480 const Attributes_section_data
* pasd
)
6482 // Return if there is no attributes section data.
6486 // If output has no object attributes, just copy.
6487 if (this->attributes_section_data_
== NULL
)
6489 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
6493 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
6494 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
6495 Object_attribute
* out_attr
=
6496 this->attributes_section_data_
->known_attributes(vendor
);
6498 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6499 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
6500 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
6502 // Ignore mismatches if the object doesn't use floating point. */
6503 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
6504 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
6505 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
6506 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
6507 gold_error(_("%s uses VFP register arguments, output does not"),
6511 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
6513 // Merge this attribute with existing attributes.
6516 case elfcpp::Tag_CPU_raw_name
:
6517 case elfcpp::Tag_CPU_name
:
6518 // These are merged after Tag_CPU_arch.
6521 case elfcpp::Tag_ABI_optimization_goals
:
6522 case elfcpp::Tag_ABI_FP_optimization_goals
:
6523 // Use the first value seen.
6526 case elfcpp::Tag_CPU_arch
:
6528 unsigned int saved_out_attr
= out_attr
->int_value();
6529 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6530 int secondary_compat
=
6531 this->get_secondary_compatible_arch(pasd
);
6532 int secondary_compat_out
=
6533 this->get_secondary_compatible_arch(
6534 this->attributes_section_data_
);
6535 out_attr
[i
].set_int_value(
6536 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
6537 &secondary_compat_out
,
6538 in_attr
[i
].int_value(),
6540 this->set_secondary_compatible_arch(this->attributes_section_data_
,
6541 secondary_compat_out
);
6543 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6544 if (out_attr
[i
].int_value() == saved_out_attr
)
6545 ; // Leave the names alone.
6546 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
6548 // The output architecture has been changed to match the
6549 // input architecture. Use the input names.
6550 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
6551 in_attr
[elfcpp::Tag_CPU_name
].string_value());
6552 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
6553 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
6557 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
6558 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
6561 // If we still don't have a value for Tag_CPU_name,
6562 // make one up now. Tag_CPU_raw_name remains blank.
6563 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
6565 const std::string cpu_name
=
6566 this->tag_cpu_name_value(out_attr
[i
].int_value());
6567 // FIXME: If we see an unknown CPU, this will be set
6568 // to "<unknown CPU n>", where n is the attribute value.
6569 // This is different from BFD, which leaves the name alone.
6570 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
6575 case elfcpp::Tag_ARM_ISA_use
:
6576 case elfcpp::Tag_THUMB_ISA_use
:
6577 case elfcpp::Tag_WMMX_arch
:
6578 case elfcpp::Tag_Advanced_SIMD_arch
:
6579 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6580 case elfcpp::Tag_ABI_FP_rounding
:
6581 case elfcpp::Tag_ABI_FP_exceptions
:
6582 case elfcpp::Tag_ABI_FP_user_exceptions
:
6583 case elfcpp::Tag_ABI_FP_number_model
:
6584 case elfcpp::Tag_VFP_HP_extension
:
6585 case elfcpp::Tag_CPU_unaligned_access
:
6586 case elfcpp::Tag_T2EE_use
:
6587 case elfcpp::Tag_Virtualization_use
:
6588 case elfcpp::Tag_MPextension_use
:
6589 // Use the largest value specified.
6590 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6591 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6594 case elfcpp::Tag_ABI_align8_preserved
:
6595 case elfcpp::Tag_ABI_PCS_RO_data
:
6596 // Use the smallest value specified.
6597 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6598 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6601 case elfcpp::Tag_ABI_align8_needed
:
6602 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
6603 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
6604 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
6607 // This error message should be enabled once all non-conformant
6608 // binaries in the toolchain have had the attributes set
6610 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6614 case elfcpp::Tag_ABI_FP_denormal
:
6615 case elfcpp::Tag_ABI_PCS_GOT_use
:
6617 // These tags have 0 = don't care, 1 = strong requirement,
6618 // 2 = weak requirement.
6619 static const int order_021
[3] = {0, 2, 1};
6621 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6622 // value if greater than 2 (for future-proofing).
6623 if ((in_attr
[i
].int_value() > 2
6624 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6625 || (in_attr
[i
].int_value() <= 2
6626 && out_attr
[i
].int_value() <= 2
6627 && (order_021
[in_attr
[i
].int_value()]
6628 > order_021
[out_attr
[i
].int_value()])))
6629 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6633 case elfcpp::Tag_CPU_arch_profile
:
6634 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
6636 // 0 will merge with anything.
6637 // 'A' and 'S' merge to 'A'.
6638 // 'R' and 'S' merge to 'R'.
6639 // 'M' and 'A|R|S' is an error.
6640 if (out_attr
[i
].int_value() == 0
6641 || (out_attr
[i
].int_value() == 'S'
6642 && (in_attr
[i
].int_value() == 'A'
6643 || in_attr
[i
].int_value() == 'R')))
6644 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6645 else if (in_attr
[i
].int_value() == 0
6646 || (in_attr
[i
].int_value() == 'S'
6647 && (out_attr
[i
].int_value() == 'A'
6648 || out_attr
[i
].int_value() == 'R')))
6653 (_("conflicting architecture profiles %c/%c"),
6654 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
6655 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
6659 case elfcpp::Tag_VFP_arch
:
6676 // Values greater than 6 aren't defined, so just pick the
6678 if (in_attr
[i
].int_value() > 6
6679 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6681 *out_attr
= *in_attr
;
6684 // The output uses the superset of input features
6685 // (ISA version) and registers.
6686 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
6687 vfp_versions
[out_attr
[i
].int_value()].ver
);
6688 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
6689 vfp_versions
[out_attr
[i
].int_value()].regs
);
6690 // This assumes all possible supersets are also a valid
6693 for (newval
= 6; newval
> 0; newval
--)
6695 if (regs
== vfp_versions
[newval
].regs
6696 && ver
== vfp_versions
[newval
].ver
)
6699 out_attr
[i
].set_int_value(newval
);
6702 case elfcpp::Tag_PCS_config
:
6703 if (out_attr
[i
].int_value() == 0)
6704 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6705 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6707 // It's sometimes ok to mix different configs, so this is only
6709 gold_warning(_("%s: conflicting platform configuration"), name
);
6712 case elfcpp::Tag_ABI_PCS_R9_use
:
6713 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
6714 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
6715 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
6717 gold_error(_("%s: conflicting use of R9"), name
);
6719 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
6720 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6722 case elfcpp::Tag_ABI_PCS_RW_data
:
6723 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6724 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6725 != elfcpp::AEABI_R9_SB
)
6726 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6727 != elfcpp::AEABI_R9_unused
))
6729 gold_error(_("%s: SB relative addressing conflicts with use "
6733 // Use the smallest value specified.
6734 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6735 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6737 case elfcpp::Tag_ABI_PCS_wchar_t
:
6738 // FIXME: Make it possible to turn off this warning.
6739 if (out_attr
[i
].int_value()
6740 && in_attr
[i
].int_value()
6741 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6743 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6744 "use %u-byte wchar_t; use of wchar_t values "
6745 "across objects may fail"),
6746 name
, in_attr
[i
].int_value(),
6747 out_attr
[i
].int_value());
6749 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
6750 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6752 case elfcpp::Tag_ABI_enum_size
:
6753 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
6755 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
6756 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
6758 // The existing object is compatible with anything.
6759 // Use whatever requirements the new object has.
6760 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6762 // FIXME: Make it possible to turn off this warning.
6763 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
6764 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6766 unsigned int in_value
= in_attr
[i
].int_value();
6767 unsigned int out_value
= out_attr
[i
].int_value();
6768 gold_warning(_("%s uses %s enums yet the output is to use "
6769 "%s enums; use of enum values across objects "
6772 this->aeabi_enum_name(in_value
).c_str(),
6773 this->aeabi_enum_name(out_value
).c_str());
6777 case elfcpp::Tag_ABI_VFP_args
:
6780 case elfcpp::Tag_ABI_WMMX_args
:
6781 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6783 gold_error(_("%s uses iWMMXt register arguments, output does "
6788 case Object_attribute::Tag_compatibility
:
6789 // Merged in target-independent code.
6791 case elfcpp::Tag_ABI_HardFP_use
:
6792 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6793 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
6794 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
6795 out_attr
[i
].set_int_value(3);
6796 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6797 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6799 case elfcpp::Tag_ABI_FP_16bit_format
:
6800 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6802 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6803 gold_error(_("fp16 format mismatch between %s and output"),
6806 if (in_attr
[i
].int_value() != 0)
6807 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6810 case elfcpp::Tag_nodefaults
:
6811 // This tag is set if it exists, but the value is unused (and is
6812 // typically zero). We don't actually need to do anything here -
6813 // the merge happens automatically when the type flags are merged
6816 case elfcpp::Tag_also_compatible_with
:
6817 // Already done in Tag_CPU_arch.
6819 case elfcpp::Tag_conformance
:
6820 // Keep the attribute if it matches. Throw it away otherwise.
6821 // No attribute means no claim to conform.
6822 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
6823 out_attr
[i
].set_string_value("");
6828 const char* err_object
= NULL
;
6830 // The "known_obj_attributes" table does contain some undefined
6831 // attributes. Ensure that there are unused.
6832 if (out_attr
[i
].int_value() != 0
6833 || out_attr
[i
].string_value() != "")
6834 err_object
= "output";
6835 else if (in_attr
[i
].int_value() != 0
6836 || in_attr
[i
].string_value() != "")
6839 if (err_object
!= NULL
)
6841 // Attribute numbers >=64 (mod 128) can be safely ignored.
6843 gold_error(_("%s: unknown mandatory EABI object attribute "
6847 gold_warning(_("%s: unknown EABI object attribute %d"),
6851 // Only pass on attributes that match in both inputs.
6852 if (!in_attr
[i
].matches(out_attr
[i
]))
6854 out_attr
[i
].set_int_value(0);
6855 out_attr
[i
].set_string_value("");
6860 // If out_attr was copied from in_attr then it won't have a type yet.
6861 if (in_attr
[i
].type() && !out_attr
[i
].type())
6862 out_attr
[i
].set_type(in_attr
[i
].type());
6865 // Merge Tag_compatibility attributes and any common GNU ones.
6866 this->attributes_section_data_
->merge(name
, pasd
);
6868 // Check for any attributes not known on ARM.
6869 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
6870 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
6871 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
6872 Other_attributes
* out_other_attributes
=
6873 this->attributes_section_data_
->other_attributes(vendor
);
6874 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
6876 while (in_iter
!= in_other_attributes
->end()
6877 || out_iter
!= out_other_attributes
->end())
6879 const char* err_object
= NULL
;
6882 // The tags for each list are in numerical order.
6883 // If the tags are equal, then merge.
6884 if (out_iter
!= out_other_attributes
->end()
6885 && (in_iter
== in_other_attributes
->end()
6886 || in_iter
->first
> out_iter
->first
))
6888 // This attribute only exists in output. We can't merge, and we
6889 // don't know what the tag means, so delete it.
6890 err_object
= "output";
6891 err_tag
= out_iter
->first
;
6892 int saved_tag
= out_iter
->first
;
6893 delete out_iter
->second
;
6894 out_other_attributes
->erase(out_iter
);
6895 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6897 else if (in_iter
!= in_other_attributes
->end()
6898 && (out_iter
!= out_other_attributes
->end()
6899 || in_iter
->first
< out_iter
->first
))
6901 // This attribute only exists in input. We can't merge, and we
6902 // don't know what the tag means, so ignore it.
6904 err_tag
= in_iter
->first
;
6907 else // The tags are equal.
6909 // As present, all attributes in the list are unknown, and
6910 // therefore can't be merged meaningfully.
6911 err_object
= "output";
6912 err_tag
= out_iter
->first
;
6914 // Only pass on attributes that match in both inputs.
6915 if (!in_iter
->second
->matches(*(out_iter
->second
)))
6917 // No match. Delete the attribute.
6918 int saved_tag
= out_iter
->first
;
6919 delete out_iter
->second
;
6920 out_other_attributes
->erase(out_iter
);
6921 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6925 // Matched. Keep the attribute and move to the next.
6933 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6934 if ((err_tag
& 127) < 64)
6936 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6937 err_object
, err_tag
);
6941 gold_warning(_("%s: unknown EABI object attribute %d"),
6942 err_object
, err_tag
);
6948 // Return whether a relocation type used the LSB to distinguish THUMB
6950 template<bool big_endian
>
6952 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
6956 case elfcpp::R_ARM_PC24
:
6957 case elfcpp::R_ARM_ABS32
:
6958 case elfcpp::R_ARM_REL32
:
6959 case elfcpp::R_ARM_SBREL32
:
6960 case elfcpp::R_ARM_THM_CALL
:
6961 case elfcpp::R_ARM_GLOB_DAT
:
6962 case elfcpp::R_ARM_JUMP_SLOT
:
6963 case elfcpp::R_ARM_GOTOFF32
:
6964 case elfcpp::R_ARM_PLT32
:
6965 case elfcpp::R_ARM_CALL
:
6966 case elfcpp::R_ARM_JUMP24
:
6967 case elfcpp::R_ARM_THM_JUMP24
:
6968 case elfcpp::R_ARM_SBREL31
:
6969 case elfcpp::R_ARM_PREL31
:
6970 case elfcpp::R_ARM_MOVW_ABS_NC
:
6971 case elfcpp::R_ARM_MOVW_PREL_NC
:
6972 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6973 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6974 case elfcpp::R_ARM_THM_JUMP19
:
6975 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
6976 case elfcpp::R_ARM_ALU_PC_G0_NC
:
6977 case elfcpp::R_ARM_ALU_PC_G0
:
6978 case elfcpp::R_ARM_ALU_PC_G1_NC
:
6979 case elfcpp::R_ARM_ALU_PC_G1
:
6980 case elfcpp::R_ARM_ALU_PC_G2
:
6981 case elfcpp::R_ARM_ALU_SB_G0_NC
:
6982 case elfcpp::R_ARM_ALU_SB_G0
:
6983 case elfcpp::R_ARM_ALU_SB_G1_NC
:
6984 case elfcpp::R_ARM_ALU_SB_G1
:
6985 case elfcpp::R_ARM_ALU_SB_G2
:
6986 case elfcpp::R_ARM_MOVW_BREL_NC
:
6987 case elfcpp::R_ARM_MOVW_BREL
:
6988 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
6989 case elfcpp::R_ARM_THM_MOVW_BREL
:
6996 // Stub-generation methods for Target_arm.
6998 // Make a new Arm_input_section object.
7000 template<bool big_endian
>
7001 Arm_input_section
<big_endian
>*
7002 Target_arm
<big_endian
>::new_arm_input_section(
7006 Input_section_specifier
iss(relobj
, shndx
);
7008 Arm_input_section
<big_endian
>* arm_input_section
=
7009 new Arm_input_section
<big_endian
>(relobj
, shndx
);
7010 arm_input_section
->init();
7012 // Register new Arm_input_section in map for look-up.
7013 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
7014 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
7016 // Make sure that it we have not created another Arm_input_section
7017 // for this input section already.
7018 gold_assert(ins
.second
);
7020 return arm_input_section
;
7023 // Find the Arm_input_section object corresponding to the SHNDX-th input
7024 // section of RELOBJ.
7026 template<bool big_endian
>
7027 Arm_input_section
<big_endian
>*
7028 Target_arm
<big_endian
>::find_arm_input_section(
7030 unsigned int shndx
) const
7032 Input_section_specifier
iss(relobj
, shndx
);
7033 typename
Arm_input_section_map::const_iterator p
=
7034 this->arm_input_section_map_
.find(iss
);
7035 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
7038 // Make a new stub table.
7040 template<bool big_endian
>
7041 Stub_table
<big_endian
>*
7042 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
7044 Stub_table
<big_endian
>* stub_table
=
7045 new Stub_table
<big_endian
>(owner
);
7046 this->stub_tables_
.push_back(stub_table
);
7048 stub_table
->set_address(owner
->address() + owner
->data_size());
7049 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
7050 stub_table
->finalize_data_size();
7055 // Scan a relocation for stub generation.
7057 template<bool big_endian
>
7059 Target_arm
<big_endian
>::scan_reloc_for_stub(
7060 const Relocate_info
<32, big_endian
>* relinfo
,
7061 unsigned int r_type
,
7062 const Sized_symbol
<32>* gsym
,
7064 const Symbol_value
<32>* psymval
,
7065 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
7066 Arm_address address
)
7068 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
7070 const Arm_relobj
<big_endian
>* arm_relobj
=
7071 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7073 bool target_is_thumb
;
7074 Symbol_value
<32> symval
;
7077 // This is a global symbol. Determine if we use PLT and if the
7078 // final target is THUMB.
7079 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
7081 // This uses a PLT, change the symbol value.
7082 symval
.set_output_value(this->plt_section()->address()
7083 + gsym
->plt_offset());
7085 target_is_thumb
= false;
7087 else if (gsym
->is_undefined())
7088 // There is no need to generate a stub symbol is undefined.
7093 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
7094 || (gsym
->type() == elfcpp::STT_FUNC
7095 && !gsym
->is_undefined()
7096 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
7101 // This is a local symbol. Determine if the final target is THUMB.
7102 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
7105 // Strip LSB if this points to a THUMB target.
7107 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
7108 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
7110 Arm_address stripped_value
=
7111 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
7112 symval
.set_output_value(stripped_value
);
7116 // Get the symbol value.
7117 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
7119 // Owing to pipelining, the PC relative branches below actually skip
7120 // two instructions when the branch offset is 0.
7121 Arm_address destination
;
7124 case elfcpp::R_ARM_CALL
:
7125 case elfcpp::R_ARM_JUMP24
:
7126 case elfcpp::R_ARM_PLT32
:
7128 destination
= value
+ addend
+ 8;
7130 case elfcpp::R_ARM_THM_CALL
:
7131 case elfcpp::R_ARM_THM_XPC22
:
7132 case elfcpp::R_ARM_THM_JUMP24
:
7133 case elfcpp::R_ARM_THM_JUMP19
:
7135 destination
= value
+ addend
+ 4;
7141 Reloc_stub
* stub
= NULL
;
7142 Stub_type stub_type
=
7143 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
7145 if (stub_type
!= arm_stub_none
)
7147 // Try looking up an existing stub from a stub table.
7148 Stub_table
<big_endian
>* stub_table
=
7149 arm_relobj
->stub_table(relinfo
->data_shndx
);
7150 gold_assert(stub_table
!= NULL
);
7152 // Locate stub by destination.
7153 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
7155 // Create a stub if there is not one already
7156 stub
= stub_table
->find_reloc_stub(stub_key
);
7159 // create a new stub and add it to stub table.
7160 stub
= this->stub_factory().make_reloc_stub(stub_type
);
7161 stub_table
->add_reloc_stub(stub
, stub_key
);
7164 // Record the destination address.
7165 stub
->set_destination_address(destination
7166 | (target_is_thumb
? 1 : 0));
7169 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7170 if (this->fix_cortex_a8_
7171 && (r_type
== elfcpp::R_ARM_THM_JUMP24
7172 || r_type
== elfcpp::R_ARM_THM_JUMP19
7173 || r_type
== elfcpp::R_ARM_THM_CALL
7174 || r_type
== elfcpp::R_ARM_THM_XPC22
)
7175 && (address
& 0xfffU
) == 0xffeU
)
7177 // Found a candidate. Note we haven't checked the destination is
7178 // within 4K here: if we do so (and don't create a record) we can't
7179 // tell that a branch should have been relocated when scanning later.
7180 this->cortex_a8_relocs_info_
[address
] =
7181 new Cortex_a8_reloc(stub
, r_type
,
7182 destination
| (target_is_thumb
? 1 : 0));
7186 // This function scans a relocation sections for stub generation.
7187 // The template parameter Relocate must be a class type which provides
7188 // a single function, relocate(), which implements the machine
7189 // specific part of a relocation.
7191 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7192 // SHT_REL or SHT_RELA.
7194 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7195 // of relocs. OUTPUT_SECTION is the output section.
7196 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7197 // mapped to output offsets.
7199 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7200 // VIEW_SIZE is the size. These refer to the input section, unless
7201 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7202 // the output section.
7204 template<bool big_endian
>
7205 template<int sh_type
>
7207 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
7208 const Relocate_info
<32, big_endian
>* relinfo
,
7209 const unsigned char* prelocs
,
7211 Output_section
* output_section
,
7212 bool needs_special_offset_handling
,
7213 const unsigned char* view
,
7214 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
7217 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
7218 const int reloc_size
=
7219 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
7221 Arm_relobj
<big_endian
>* arm_object
=
7222 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7223 unsigned int local_count
= arm_object
->local_symbol_count();
7225 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
7227 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
7229 Reltype
reloc(prelocs
);
7231 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
7232 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
7233 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
7235 r_type
= this->get_real_reloc_type(r_type
);
7237 // Only a few relocation types need stubs.
7238 if ((r_type
!= elfcpp::R_ARM_CALL
)
7239 && (r_type
!= elfcpp::R_ARM_JUMP24
)
7240 && (r_type
!= elfcpp::R_ARM_PLT32
)
7241 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
7242 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
7243 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
7244 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
7247 section_offset_type offset
=
7248 convert_to_section_size_type(reloc
.get_r_offset());
7250 if (needs_special_offset_handling
)
7252 offset
= output_section
->output_offset(relinfo
->object
,
7253 relinfo
->data_shndx
,
7260 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
7261 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
7262 stub_addend_reader(r_type
, view
+ offset
, reloc
);
7264 const Sized_symbol
<32>* sym
;
7266 Symbol_value
<32> symval
;
7267 const Symbol_value
<32> *psymval
;
7268 if (r_sym
< local_count
)
7271 psymval
= arm_object
->local_symbol(r_sym
);
7273 // If the local symbol belongs to a section we are discarding,
7274 // and that section is a debug section, try to find the
7275 // corresponding kept section and map this symbol to its
7276 // counterpart in the kept section. The symbol must not
7277 // correspond to a section we are folding.
7279 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7281 && shndx
!= elfcpp::SHN_UNDEF
7282 && !arm_object
->is_section_included(shndx
)
7283 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
7285 if (comdat_behavior
== CB_UNDETERMINED
)
7288 arm_object
->section_name(relinfo
->data_shndx
);
7289 comdat_behavior
= get_comdat_behavior(name
.c_str());
7291 if (comdat_behavior
== CB_PRETEND
)
7294 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
7295 arm_object
->map_to_kept_section(shndx
, &found
);
7297 symval
.set_output_value(value
+ psymval
->input_value());
7299 symval
.set_output_value(0);
7303 symval
.set_output_value(0);
7305 symval
.set_no_output_symtab_entry();
7311 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
7312 gold_assert(gsym
!= NULL
);
7313 if (gsym
->is_forwarder())
7314 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7316 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
7317 if (sym
->has_symtab_index())
7318 symval
.set_output_symtab_index(sym
->symtab_index());
7320 symval
.set_no_output_symtab_entry();
7322 // We need to compute the would-be final value of this global
7324 const Symbol_table
* symtab
= relinfo
->symtab
;
7325 const Sized_symbol
<32>* sized_symbol
=
7326 symtab
->get_sized_symbol
<32>(gsym
);
7327 Symbol_table::Compute_final_value_status status
;
7329 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
7331 // Skip this if the symbol has not output section.
7332 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
7335 symval
.set_output_value(value
);
7339 // If symbol is a section symbol, we don't know the actual type of
7340 // destination. Give up.
7341 if (psymval
->is_section_symbol())
7344 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
7345 addend
, view_address
+ offset
);
7349 // Scan an input section for stub generation.
7351 template<bool big_endian
>
7353 Target_arm
<big_endian
>::scan_section_for_stubs(
7354 const Relocate_info
<32, big_endian
>* relinfo
,
7355 unsigned int sh_type
,
7356 const unsigned char* prelocs
,
7358 Output_section
* output_section
,
7359 bool needs_special_offset_handling
,
7360 const unsigned char* view
,
7361 Arm_address view_address
,
7362 section_size_type view_size
)
7364 if (sh_type
== elfcpp::SHT_REL
)
7365 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
7370 needs_special_offset_handling
,
7374 else if (sh_type
== elfcpp::SHT_RELA
)
7375 // We do not support RELA type relocations yet. This is provided for
7377 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
7382 needs_special_offset_handling
,
7390 // Group input sections for stub generation.
7392 // We goup input sections in an output sections so that the total size,
7393 // including any padding space due to alignment is smaller than GROUP_SIZE
7394 // unless the only input section in group is bigger than GROUP_SIZE already.
7395 // Then an ARM stub table is created to follow the last input section
7396 // in group. For each group an ARM stub table is created an is placed
7397 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7398 // extend the group after the stub table.
7400 template<bool big_endian
>
7402 Target_arm
<big_endian
>::group_sections(
7404 section_size_type group_size
,
7405 bool stubs_always_after_branch
)
7407 // Group input sections and insert stub table
7408 Layout::Section_list section_list
;
7409 layout
->get_allocated_sections(§ion_list
);
7410 for (Layout::Section_list::const_iterator p
= section_list
.begin();
7411 p
!= section_list
.end();
7414 Arm_output_section
<big_endian
>* output_section
=
7415 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
7416 output_section
->group_sections(group_size
, stubs_always_after_branch
,
7421 // Relaxation hook. This is where we do stub generation.
7423 template<bool big_endian
>
7425 Target_arm
<big_endian
>::do_relax(
7427 const Input_objects
* input_objects
,
7428 Symbol_table
* symtab
,
7431 // No need to generate stubs if this is a relocatable link.
7432 gold_assert(!parameters
->options().relocatable());
7434 // If this is the first pass, we need to group input sections into
7438 // Determine the stub group size. The group size is the absolute
7439 // value of the parameter --stub-group-size. If --stub-group-size
7440 // is passed a negative value, we restict stubs to be always after
7441 // the stubbed branches.
7442 int32_t stub_group_size_param
=
7443 parameters
->options().stub_group_size();
7444 bool stubs_always_after_branch
= stub_group_size_param
< 0;
7445 section_size_type stub_group_size
= abs(stub_group_size_param
);
7447 if (stub_group_size
== 1)
7450 // Thumb branch range is +-4MB has to be used as the default
7451 // maximum size (a given section can contain both ARM and Thumb
7452 // code, so the worst case has to be taken into account).
7454 // This value is 24K less than that, which allows for 2025
7455 // 12-byte stubs. If we exceed that, then we will fail to link.
7456 // The user will have to relink with an explicit group size
7458 stub_group_size
= 4170000;
7461 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
7464 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
7465 if (this->fix_cortex_a8_
)
7467 // Clear all Cortex-A8 reloc information.
7468 for (typename
Cortex_a8_relocs_info::const_iterator p
=
7469 this->cortex_a8_relocs_info_
.begin();
7470 p
!= this->cortex_a8_relocs_info_
.end();
7473 this->cortex_a8_relocs_info_
.clear();
7476 // scan relocs for stubs
7477 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
7478 op
!= input_objects
->relobj_end();
7481 Arm_relobj
<big_endian
>* arm_relobj
=
7482 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
7483 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
7486 // Check all stub tables to see if any of them have their data sizes
7487 // or addresses alignments changed. These are the only things that
7489 bool any_stub_table_changed
= false;
7490 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7491 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7494 if ((*sp
)->update_data_size_and_addralign())
7495 any_stub_table_changed
= true;
7498 // Finalize the stubs in the last relaxation pass.
7499 if (!any_stub_table_changed
)
7500 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7501 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7503 (*sp
)->finalize_stubs();
7505 return any_stub_table_changed
;
7510 template<bool big_endian
>
7512 Target_arm
<big_endian
>::relocate_stub(
7514 const Relocate_info
<32, big_endian
>* relinfo
,
7515 Output_section
* output_section
,
7516 unsigned char* view
,
7517 Arm_address address
,
7518 section_size_type view_size
)
7521 const Stub_template
* stub_template
= stub
->stub_template();
7522 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
7524 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
7525 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
7527 unsigned int r_type
= insn
->r_type();
7528 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
7529 section_size_type reloc_size
= insn
->size();
7530 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
7532 // This is the address of the stub destination.
7533 Arm_address target
= stub
->reloc_target(i
);
7534 Symbol_value
<32> symval
;
7535 symval
.set_output_value(target
);
7537 // Synthesize a fake reloc just in case. We don't have a symbol so
7539 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
7540 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
7541 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
7542 reloc_write
.put_r_offset(reloc_offset
);
7543 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
7544 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
7546 relocate
.relocate(relinfo
, this, output_section
,
7547 this->fake_relnum_for_stubs
, rel
, r_type
,
7548 NULL
, &symval
, view
+ reloc_offset
,
7549 address
+ reloc_offset
, reloc_size
);
7553 // Determine whether an object attribute tag takes an integer, a
7556 template<bool big_endian
>
7558 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
7560 if (tag
== Object_attribute::Tag_compatibility
)
7561 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7562 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
7563 else if (tag
== elfcpp::Tag_nodefaults
)
7564 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7565 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
7566 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
7567 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
7569 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
7571 return ((tag
& 1) != 0
7572 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7573 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
7576 // Reorder attributes.
7578 // The ABI defines that Tag_conformance should be emitted first, and that
7579 // Tag_nodefaults should be second (if either is defined). This sets those
7580 // two positions, and bumps up the position of all the remaining tags to
7583 template<bool big_endian
>
7585 Target_arm
<big_endian
>::do_attributes_order(int num
) const
7587 // Reorder the known object attributes in output. We want to move
7588 // Tag_conformance to position 4 and Tag_conformance to position 5
7589 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7591 return elfcpp::Tag_conformance
;
7593 return elfcpp::Tag_nodefaults
;
7594 if ((num
- 2) < elfcpp::Tag_nodefaults
)
7596 if ((num
- 1) < elfcpp::Tag_conformance
)
7601 template<bool big_endian
>
7602 class Target_selector_arm
: public Target_selector
7605 Target_selector_arm()
7606 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
7607 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
7611 do_instantiate_target()
7612 { return new Target_arm
<big_endian
>(); }
7615 Target_selector_arm
<false> target_selector_arm
;
7616 Target_selector_arm
<true> target_selector_armbe
;
7618 } // End anonymous namespace.