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
), section_has_cortex_a8_workaround_(NULL
)
1076 { delete this->attributes_section_data_
; }
1078 // Return the stub table of the SHNDX-th section if there is one.
1079 Stub_table
<big_endian
>*
1080 stub_table(unsigned int shndx
) const
1082 gold_assert(shndx
< this->stub_tables_
.size());
1083 return this->stub_tables_
[shndx
];
1086 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1088 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1090 gold_assert(shndx
< this->stub_tables_
.size());
1091 this->stub_tables_
[shndx
] = stub_table
;
1094 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1095 // index. This is only valid after do_count_local_symbol is called.
1097 local_symbol_is_thumb_function(unsigned int r_sym
) const
1099 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1100 return this->local_symbol_is_thumb_function_
[r_sym
];
1103 // Scan all relocation sections for stub generation.
1105 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1108 // Convert regular input section with index SHNDX to a relaxed section.
1110 convert_input_section_to_relaxed_section(unsigned shndx
)
1112 // The stubs have relocations and we need to process them after writing
1113 // out the stubs. So relocation now must follow section write.
1114 this->invalidate_section_offset(shndx
);
1115 this->set_relocs_must_follow_section_writes();
1118 // Downcast a base pointer to an Arm_relobj pointer. This is
1119 // not type-safe but we only use Arm_relobj not the base class.
1120 static Arm_relobj
<big_endian
>*
1121 as_arm_relobj(Relobj
* relobj
)
1122 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1124 // Processor-specific flags in ELF file header. This is valid only after
1127 processor_specific_flags() const
1128 { return this->processor_specific_flags_
; }
1130 // Attribute section data This is the contents of the .ARM.attribute section
1132 const Attributes_section_data
*
1133 attributes_section_data() const
1134 { return this->attributes_section_data_
; }
1136 // Whether a section contains any Cortex-A8 workaround.
1138 section_has_cortex_a8_workaround(unsigned int shndx
) const
1140 return (this->section_has_cortex_a8_workaround_
!= NULL
1141 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1144 // Mark a section that has Cortex-A8 workaround.
1146 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1148 if (this->section_has_cortex_a8_workaround_
== NULL
)
1149 this->section_has_cortex_a8_workaround_
=
1150 new std::vector
<bool>(this->shnum(), false);
1151 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1155 // Post constructor setup.
1159 // Call parent's setup method.
1160 Sized_relobj
<32, big_endian
>::do_setup();
1162 // Initialize look-up tables.
1163 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1164 this->stub_tables_
.swap(empty_stub_table_list
);
1167 // Count the local symbols.
1169 do_count_local_symbols(Stringpool_template
<char>*,
1170 Stringpool_template
<char>*);
1173 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1174 const unsigned char* pshdrs
,
1175 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1177 // Read the symbol information.
1179 do_read_symbols(Read_symbols_data
* sd
);
1181 // Process relocs for garbage collection.
1183 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1186 // List of stub tables.
1187 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1188 Stub_table_list stub_tables_
;
1189 // Bit vector to tell if a local symbol is a thumb function or not.
1190 // This is only valid after do_count_local_symbol is called.
1191 std::vector
<bool> local_symbol_is_thumb_function_
;
1192 // processor-specific flags in ELF file header.
1193 elfcpp::Elf_Word processor_specific_flags_
;
1194 // Object attributes if there is an .ARM.attributes section or NULL.
1195 Attributes_section_data
* attributes_section_data_
;
1196 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1197 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1200 // Arm_dynobj class.
1202 template<bool big_endian
>
1203 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1206 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1207 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1208 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1209 processor_specific_flags_(0), attributes_section_data_(NULL
)
1213 { delete this->attributes_section_data_
; }
1215 // Downcast a base pointer to an Arm_relobj pointer. This is
1216 // not type-safe but we only use Arm_relobj not the base class.
1217 static Arm_dynobj
<big_endian
>*
1218 as_arm_dynobj(Dynobj
* dynobj
)
1219 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1221 // Processor-specific flags in ELF file header. This is valid only after
1224 processor_specific_flags() const
1225 { return this->processor_specific_flags_
; }
1227 // Attributes section data.
1228 const Attributes_section_data
*
1229 attributes_section_data() const
1230 { return this->attributes_section_data_
; }
1233 // Read the symbol information.
1235 do_read_symbols(Read_symbols_data
* sd
);
1238 // processor-specific flags in ELF file header.
1239 elfcpp::Elf_Word processor_specific_flags_
;
1240 // Object attributes if there is an .ARM.attributes section or NULL.
1241 Attributes_section_data
* attributes_section_data_
;
1244 // Functor to read reloc addends during stub generation.
1246 template<int sh_type
, bool big_endian
>
1247 struct Stub_addend_reader
1249 // Return the addend for a relocation of a particular type. Depending
1250 // on whether this is a REL or RELA relocation, read the addend from a
1251 // view or from a Reloc object.
1252 elfcpp::Elf_types
<32>::Elf_Swxword
1254 unsigned int /* r_type */,
1255 const unsigned char* /* view */,
1256 const typename Reloc_types
<sh_type
,
1257 32, big_endian
>::Reloc
& /* reloc */) const;
1260 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1262 template<bool big_endian
>
1263 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1265 elfcpp::Elf_types
<32>::Elf_Swxword
1268 const unsigned char*,
1269 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1272 // Specialized Stub_addend_reader for RELA type relocation sections.
1273 // We currently do not handle RELA type relocation sections but it is trivial
1274 // to implement the addend reader. This is provided for completeness and to
1275 // make it easier to add support for RELA relocation sections in the future.
1277 template<bool big_endian
>
1278 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1280 elfcpp::Elf_types
<32>::Elf_Swxword
1283 const unsigned char*,
1284 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1285 big_endian
>::Reloc
& reloc
) const
1286 { return reloc
.get_r_addend(); }
1289 // Utilities for manipulating integers of up to 32-bits
1293 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1294 // an int32_t. NO_BITS must be between 1 to 32.
1295 template<int no_bits
>
1296 static inline int32_t
1297 sign_extend(uint32_t bits
)
1299 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1301 return static_cast<int32_t>(bits
);
1302 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1304 uint32_t top_bit
= 1U << (no_bits
- 1);
1305 int32_t as_signed
= static_cast<int32_t>(bits
);
1306 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1309 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1310 template<int no_bits
>
1312 has_overflow(uint32_t bits
)
1314 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1317 int32_t max
= (1 << (no_bits
- 1)) - 1;
1318 int32_t min
= -(1 << (no_bits
- 1));
1319 int32_t as_signed
= static_cast<int32_t>(bits
);
1320 return as_signed
> max
|| as_signed
< min
;
1323 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1324 // fits in the given number of bits as either a signed or unsigned value.
1325 // For example, has_signed_unsigned_overflow<8> would check
1326 // -128 <= bits <= 255
1327 template<int no_bits
>
1329 has_signed_unsigned_overflow(uint32_t bits
)
1331 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1334 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1335 int32_t min
= -(1 << (no_bits
- 1));
1336 int32_t as_signed
= static_cast<int32_t>(bits
);
1337 return as_signed
> max
|| as_signed
< min
;
1340 // Select bits from A and B using bits in MASK. For each n in [0..31],
1341 // the n-th bit in the result is chosen from the n-th bits of A and B.
1342 // A zero selects A and a one selects B.
1343 static inline uint32_t
1344 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1345 { return (a
& ~mask
) | (b
& mask
); }
1348 template<bool big_endian
>
1349 class Target_arm
: public Sized_target
<32, big_endian
>
1352 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1355 // When were are relocating a stub, we pass this as the relocation number.
1356 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1359 : Sized_target
<32, big_endian
>(&arm_info
),
1360 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1361 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1362 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1363 should_force_pic_veneer_(false), arm_input_section_map_(),
1364 attributes_section_data_(NULL
)
1367 // Whether we can use BLX.
1370 { return this->may_use_blx_
; }
1372 // Set use-BLX flag.
1374 set_may_use_blx(bool value
)
1375 { this->may_use_blx_
= value
; }
1377 // Whether we force PCI branch veneers.
1379 should_force_pic_veneer() const
1380 { return this->should_force_pic_veneer_
; }
1382 // Set PIC veneer flag.
1384 set_should_force_pic_veneer(bool value
)
1385 { this->should_force_pic_veneer_
= value
; }
1387 // Whether we use THUMB-2 instructions.
1389 using_thumb2() const
1391 Object_attribute
* attr
=
1392 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1393 int arch
= attr
->int_value();
1394 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1397 // Whether we use THUMB/THUMB-2 instructions only.
1399 using_thumb_only() const
1401 Object_attribute
* attr
=
1402 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1403 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1404 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1406 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1407 return attr
->int_value() == 'M';
1410 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1412 may_use_arm_nop() const
1414 Object_attribute
* attr
=
1415 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1416 int arch
= attr
->int_value();
1417 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1418 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1419 || arch
== elfcpp::TAG_CPU_ARCH_V7
1420 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1423 // Whether we have THUMB-2 NOP.W instruction.
1425 may_use_thumb2_nop() const
1427 Object_attribute
* attr
=
1428 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1429 int arch
= attr
->int_value();
1430 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1431 || arch
== elfcpp::TAG_CPU_ARCH_V7
1432 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1435 // Process the relocations to determine unreferenced sections for
1436 // garbage collection.
1438 gc_process_relocs(Symbol_table
* symtab
,
1440 Sized_relobj
<32, big_endian
>* object
,
1441 unsigned int data_shndx
,
1442 unsigned int sh_type
,
1443 const unsigned char* prelocs
,
1445 Output_section
* output_section
,
1446 bool needs_special_offset_handling
,
1447 size_t local_symbol_count
,
1448 const unsigned char* plocal_symbols
);
1450 // Scan the relocations to look for symbol adjustments.
1452 scan_relocs(Symbol_table
* symtab
,
1454 Sized_relobj
<32, big_endian
>* object
,
1455 unsigned int data_shndx
,
1456 unsigned int sh_type
,
1457 const unsigned char* prelocs
,
1459 Output_section
* output_section
,
1460 bool needs_special_offset_handling
,
1461 size_t local_symbol_count
,
1462 const unsigned char* plocal_symbols
);
1464 // Finalize the sections.
1466 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1468 // Return the value to use for a dynamic symbol which requires special
1471 do_dynsym_value(const Symbol
*) const;
1473 // Relocate a section.
1475 relocate_section(const Relocate_info
<32, big_endian
>*,
1476 unsigned int sh_type
,
1477 const unsigned char* prelocs
,
1479 Output_section
* output_section
,
1480 bool needs_special_offset_handling
,
1481 unsigned char* view
,
1482 Arm_address view_address
,
1483 section_size_type view_size
,
1484 const Reloc_symbol_changes
*);
1486 // Scan the relocs during a relocatable link.
1488 scan_relocatable_relocs(Symbol_table
* symtab
,
1490 Sized_relobj
<32, big_endian
>* object
,
1491 unsigned int data_shndx
,
1492 unsigned int sh_type
,
1493 const unsigned char* prelocs
,
1495 Output_section
* output_section
,
1496 bool needs_special_offset_handling
,
1497 size_t local_symbol_count
,
1498 const unsigned char* plocal_symbols
,
1499 Relocatable_relocs
*);
1501 // Relocate a section during a relocatable link.
1503 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1504 unsigned int sh_type
,
1505 const unsigned char* prelocs
,
1507 Output_section
* output_section
,
1508 off_t offset_in_output_section
,
1509 const Relocatable_relocs
*,
1510 unsigned char* view
,
1511 Arm_address view_address
,
1512 section_size_type view_size
,
1513 unsigned char* reloc_view
,
1514 section_size_type reloc_view_size
);
1516 // Return whether SYM is defined by the ABI.
1518 do_is_defined_by_abi(Symbol
* sym
) const
1519 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1521 // Return the size of the GOT section.
1525 gold_assert(this->got_
!= NULL
);
1526 return this->got_
->data_size();
1529 // Map platform-specific reloc types
1531 get_real_reloc_type (unsigned int r_type
);
1534 // Methods to support stub-generations.
1537 // Return the stub factory
1539 stub_factory() const
1540 { return this->stub_factory_
; }
1542 // Make a new Arm_input_section object.
1543 Arm_input_section
<big_endian
>*
1544 new_arm_input_section(Relobj
*, unsigned int);
1546 // Find the Arm_input_section object corresponding to the SHNDX-th input
1547 // section of RELOBJ.
1548 Arm_input_section
<big_endian
>*
1549 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1551 // Make a new Stub_table
1552 Stub_table
<big_endian
>*
1553 new_stub_table(Arm_input_section
<big_endian
>*);
1555 // Scan a section for stub generation.
1557 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1558 const unsigned char*, size_t, Output_section
*,
1559 bool, const unsigned char*, Arm_address
,
1564 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1565 Output_section
*, unsigned char*, Arm_address
,
1568 // Get the default ARM target.
1569 static Target_arm
<big_endian
>*
1572 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1573 && parameters
->target().is_big_endian() == big_endian
);
1574 return static_cast<Target_arm
<big_endian
>*>(
1575 parameters
->sized_target
<32, big_endian
>());
1578 // Whether relocation type uses LSB to distinguish THUMB addresses.
1580 reloc_uses_thumb_bit(unsigned int r_type
);
1583 // Make an ELF object.
1585 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1586 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1589 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1590 const elfcpp::Ehdr
<32, !big_endian
>&)
1591 { gold_unreachable(); }
1594 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1595 const elfcpp::Ehdr
<64, false>&)
1596 { gold_unreachable(); }
1599 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1600 const elfcpp::Ehdr
<64, true>&)
1601 { gold_unreachable(); }
1603 // Make an output section.
1605 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1606 elfcpp::Elf_Xword flags
)
1607 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1610 do_adjust_elf_header(unsigned char* view
, int len
) const;
1612 // We only need to generate stubs, and hence perform relaxation if we are
1613 // not doing relocatable linking.
1615 do_may_relax() const
1616 { return !parameters
->options().relocatable(); }
1619 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1621 // Determine whether an object attribute tag takes an integer, a
1624 do_attribute_arg_type(int tag
) const;
1626 // Reorder tags during output.
1628 do_attributes_order(int num
) const;
1631 // The class which scans relocations.
1636 : issued_non_pic_error_(false)
1640 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1641 Sized_relobj
<32, big_endian
>* object
,
1642 unsigned int data_shndx
,
1643 Output_section
* output_section
,
1644 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1645 const elfcpp::Sym
<32, big_endian
>& lsym
);
1648 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1649 Sized_relobj
<32, big_endian
>* object
,
1650 unsigned int data_shndx
,
1651 Output_section
* output_section
,
1652 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1657 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1658 unsigned int r_type
);
1661 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1662 unsigned int r_type
, Symbol
*);
1665 check_non_pic(Relobj
*, unsigned int r_type
);
1667 // Almost identical to Symbol::needs_plt_entry except that it also
1668 // handles STT_ARM_TFUNC.
1670 symbol_needs_plt_entry(const Symbol
* sym
)
1672 // An undefined symbol from an executable does not need a PLT entry.
1673 if (sym
->is_undefined() && !parameters
->options().shared())
1676 return (!parameters
->doing_static_link()
1677 && (sym
->type() == elfcpp::STT_FUNC
1678 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1679 && (sym
->is_from_dynobj()
1680 || sym
->is_undefined()
1681 || sym
->is_preemptible()));
1684 // Whether we have issued an error about a non-PIC compilation.
1685 bool issued_non_pic_error_
;
1688 // The class which implements relocation.
1698 // Return whether the static relocation needs to be applied.
1700 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1703 Output_section
* output_section
);
1705 // Do a relocation. Return false if the caller should not issue
1706 // any warnings about this relocation.
1708 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1709 Output_section
*, size_t relnum
,
1710 const elfcpp::Rel
<32, big_endian
>&,
1711 unsigned int r_type
, const Sized_symbol
<32>*,
1712 const Symbol_value
<32>*,
1713 unsigned char*, Arm_address
,
1716 // Return whether we want to pass flag NON_PIC_REF for this
1717 // reloc. This means the relocation type accesses a symbol not via
1720 reloc_is_non_pic (unsigned int r_type
)
1724 // These relocation types reference GOT or PLT entries explicitly.
1725 case elfcpp::R_ARM_GOT_BREL
:
1726 case elfcpp::R_ARM_GOT_ABS
:
1727 case elfcpp::R_ARM_GOT_PREL
:
1728 case elfcpp::R_ARM_GOT_BREL12
:
1729 case elfcpp::R_ARM_PLT32_ABS
:
1730 case elfcpp::R_ARM_TLS_GD32
:
1731 case elfcpp::R_ARM_TLS_LDM32
:
1732 case elfcpp::R_ARM_TLS_IE32
:
1733 case elfcpp::R_ARM_TLS_IE12GP
:
1735 // These relocate types may use PLT entries.
1736 case elfcpp::R_ARM_CALL
:
1737 case elfcpp::R_ARM_THM_CALL
:
1738 case elfcpp::R_ARM_JUMP24
:
1739 case elfcpp::R_ARM_THM_JUMP24
:
1740 case elfcpp::R_ARM_THM_JUMP19
:
1741 case elfcpp::R_ARM_PLT32
:
1742 case elfcpp::R_ARM_THM_XPC22
:
1751 // A class which returns the size required for a relocation type,
1752 // used while scanning relocs during a relocatable link.
1753 class Relocatable_size_for_reloc
1757 get_size_for_reloc(unsigned int, Relobj
*);
1760 // Get the GOT section, creating it if necessary.
1761 Output_data_got
<32, big_endian
>*
1762 got_section(Symbol_table
*, Layout
*);
1764 // Get the GOT PLT section.
1766 got_plt_section() const
1768 gold_assert(this->got_plt_
!= NULL
);
1769 return this->got_plt_
;
1772 // Create a PLT entry for a global symbol.
1774 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1776 // Get the PLT section.
1777 const Output_data_plt_arm
<big_endian
>*
1780 gold_assert(this->plt_
!= NULL
);
1784 // Get the dynamic reloc section, creating it if necessary.
1786 rel_dyn_section(Layout
*);
1788 // Return true if the symbol may need a COPY relocation.
1789 // References from an executable object to non-function symbols
1790 // defined in a dynamic object may need a COPY relocation.
1792 may_need_copy_reloc(Symbol
* gsym
)
1794 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1795 && gsym
->may_need_copy_reloc());
1798 // Add a potential copy relocation.
1800 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1801 Sized_relobj
<32, big_endian
>* object
,
1802 unsigned int shndx
, Output_section
* output_section
,
1803 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1805 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1806 symtab
->get_sized_symbol
<32>(sym
),
1807 object
, shndx
, output_section
, reloc
,
1808 this->rel_dyn_section(layout
));
1811 // Whether two EABI versions are compatible.
1813 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1815 // Merge processor-specific flags from input object and those in the ELF
1816 // header of the output.
1818 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1820 // Get the secondary compatible architecture.
1822 get_secondary_compatible_arch(const Attributes_section_data
*);
1824 // Set the secondary compatible architecture.
1826 set_secondary_compatible_arch(Attributes_section_data
*, int);
1829 tag_cpu_arch_combine(const char*, int, int*, int, int);
1831 // Helper to print AEABI enum tag value.
1833 aeabi_enum_name(unsigned int);
1835 // Return string value for TAG_CPU_name.
1837 tag_cpu_name_value(unsigned int);
1839 // Merge object attributes from input object and those in the output.
1841 merge_object_attributes(const char*, const Attributes_section_data
*);
1843 // Helper to get an AEABI object attribute
1845 get_aeabi_object_attribute(int tag
) const
1847 Attributes_section_data
* pasd
= this->attributes_section_data_
;
1848 gold_assert(pasd
!= NULL
);
1849 Object_attribute
* attr
=
1850 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
1851 gold_assert(attr
!= NULL
);
1856 // Methods to support stub-generations.
1859 // Group input sections for stub generation.
1861 group_sections(Layout
*, section_size_type
, bool);
1863 // Scan a relocation for stub generation.
1865 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1866 const Sized_symbol
<32>*, unsigned int,
1867 const Symbol_value
<32>*,
1868 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1870 // Scan a relocation section for stub.
1871 template<int sh_type
>
1873 scan_reloc_section_for_stubs(
1874 const Relocate_info
<32, big_endian
>* relinfo
,
1875 const unsigned char* prelocs
,
1877 Output_section
* output_section
,
1878 bool needs_special_offset_handling
,
1879 const unsigned char* view
,
1880 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1883 // Information about this specific target which we pass to the
1884 // general Target structure.
1885 static const Target::Target_info arm_info
;
1887 // The types of GOT entries needed for this platform.
1890 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1893 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1895 // Map input section to Arm_input_section.
1896 typedef Unordered_map
<Input_section_specifier
,
1897 Arm_input_section
<big_endian
>*,
1898 Input_section_specifier::hash
,
1899 Input_section_specifier::equal_to
>
1900 Arm_input_section_map
;
1903 Output_data_got
<32, big_endian
>* got_
;
1905 Output_data_plt_arm
<big_endian
>* plt_
;
1906 // The GOT PLT section.
1907 Output_data_space
* got_plt_
;
1908 // The dynamic reloc section.
1909 Reloc_section
* rel_dyn_
;
1910 // Relocs saved to avoid a COPY reloc.
1911 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1912 // Space for variables copied with a COPY reloc.
1913 Output_data_space
* dynbss_
;
1914 // Vector of Stub_tables created.
1915 Stub_table_list stub_tables_
;
1917 const Stub_factory
&stub_factory_
;
1918 // Whether we can use BLX.
1920 // Whether we force PIC branch veneers.
1921 bool should_force_pic_veneer_
;
1922 // Map for locating Arm_input_sections.
1923 Arm_input_section_map arm_input_section_map_
;
1924 // Attributes section data in output.
1925 Attributes_section_data
* attributes_section_data_
;
1928 template<bool big_endian
>
1929 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1932 big_endian
, // is_big_endian
1933 elfcpp::EM_ARM
, // machine_code
1934 false, // has_make_symbol
1935 false, // has_resolve
1936 false, // has_code_fill
1937 true, // is_default_stack_executable
1939 "/usr/lib/libc.so.1", // dynamic_linker
1940 0x8000, // default_text_segment_address
1941 0x1000, // abi_pagesize (overridable by -z max-page-size)
1942 0x1000, // common_pagesize (overridable by -z common-page-size)
1943 elfcpp::SHN_UNDEF
, // small_common_shndx
1944 elfcpp::SHN_UNDEF
, // large_common_shndx
1945 0, // small_common_section_flags
1946 0, // large_common_section_flags
1947 ".ARM.attributes", // attributes_section
1948 "aeabi" // attributes_vendor
1951 // Arm relocate functions class
1954 template<bool big_endian
>
1955 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1960 STATUS_OKAY
, // No error during relocation.
1961 STATUS_OVERFLOW
, // Relocation oveflow.
1962 STATUS_BAD_RELOC
// Relocation cannot be applied.
1966 typedef Relocate_functions
<32, big_endian
> Base
;
1967 typedef Arm_relocate_functions
<big_endian
> This
;
1969 // Encoding of imm16 argument for movt and movw ARM instructions
1972 // imm16 := imm4 | imm12
1974 // 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
1975 // +-------+---------------+-------+-------+-----------------------+
1976 // | | |imm4 | |imm12 |
1977 // +-------+---------------+-------+-------+-----------------------+
1979 // Extract the relocation addend from VAL based on the ARM
1980 // instruction encoding described above.
1981 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1982 extract_arm_movw_movt_addend(
1983 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1985 // According to the Elf ABI for ARM Architecture the immediate
1986 // field is sign-extended to form the addend.
1987 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1990 // Insert X into VAL based on the ARM instruction encoding described
1992 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1993 insert_val_arm_movw_movt(
1994 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1995 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1999 val
|= (x
& 0xf000) << 4;
2003 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2006 // imm16 := imm4 | i | imm3 | imm8
2008 // 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
2009 // +---------+-+-----------+-------++-+-----+-------+---------------+
2010 // | |i| |imm4 || |imm3 | |imm8 |
2011 // +---------+-+-----------+-------++-+-----+-------+---------------+
2013 // Extract the relocation addend from VAL based on the Thumb2
2014 // instruction encoding described above.
2015 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2016 extract_thumb_movw_movt_addend(
2017 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2019 // According to the Elf ABI for ARM Architecture the immediate
2020 // field is sign-extended to form the addend.
2021 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2022 | ((val
>> 15) & 0x0800)
2023 | ((val
>> 4) & 0x0700)
2027 // Insert X into VAL based on the Thumb2 instruction encoding
2029 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2030 insert_val_thumb_movw_movt(
2031 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2032 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2035 val
|= (x
& 0xf000) << 4;
2036 val
|= (x
& 0x0800) << 15;
2037 val
|= (x
& 0x0700) << 4;
2038 val
|= (x
& 0x00ff);
2042 // Handle ARM long branches.
2043 static typename
This::Status
2044 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2045 unsigned char *, const Sized_symbol
<32>*,
2046 const Arm_relobj
<big_endian
>*, unsigned int,
2047 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2049 // Handle THUMB long branches.
2050 static typename
This::Status
2051 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2052 unsigned char *, const Sized_symbol
<32>*,
2053 const Arm_relobj
<big_endian
>*, unsigned int,
2054 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2058 // R_ARM_ABS8: S + A
2059 static inline typename
This::Status
2060 abs8(unsigned char *view
,
2061 const Sized_relobj
<32, big_endian
>* object
,
2062 const Symbol_value
<32>* psymval
)
2064 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2065 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2066 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2067 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2068 Reltype addend
= utils::sign_extend
<8>(val
);
2069 Reltype x
= psymval
->value(object
, addend
);
2070 val
= utils::bit_select(val
, x
, 0xffU
);
2071 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2072 return (utils::has_signed_unsigned_overflow
<8>(x
)
2073 ? This::STATUS_OVERFLOW
2074 : This::STATUS_OKAY
);
2077 // R_ARM_THM_ABS5: S + A
2078 static inline typename
This::Status
2079 thm_abs5(unsigned char *view
,
2080 const Sized_relobj
<32, big_endian
>* object
,
2081 const Symbol_value
<32>* psymval
)
2083 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2084 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2085 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2086 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2087 Reltype addend
= (val
& 0x7e0U
) >> 6;
2088 Reltype x
= psymval
->value(object
, addend
);
2089 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2090 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2091 return (utils::has_overflow
<5>(x
)
2092 ? This::STATUS_OVERFLOW
2093 : This::STATUS_OKAY
);
2096 // R_ARM_ABS12: S + A
2097 static inline typename
This::Status
2098 abs12(unsigned char *view
,
2099 const Sized_relobj
<32, big_endian
>* object
,
2100 const Symbol_value
<32>* psymval
)
2102 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2103 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2104 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2105 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2106 Reltype addend
= val
& 0x0fffU
;
2107 Reltype x
= psymval
->value(object
, addend
);
2108 val
= utils::bit_select(val
, x
, 0x0fffU
);
2109 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2110 return (utils::has_overflow
<12>(x
)
2111 ? This::STATUS_OVERFLOW
2112 : This::STATUS_OKAY
);
2115 // R_ARM_ABS16: S + A
2116 static inline typename
This::Status
2117 abs16(unsigned char *view
,
2118 const Sized_relobj
<32, big_endian
>* object
,
2119 const Symbol_value
<32>* psymval
)
2121 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2122 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2123 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2124 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2125 Reltype addend
= utils::sign_extend
<16>(val
);
2126 Reltype x
= psymval
->value(object
, addend
);
2127 val
= utils::bit_select(val
, x
, 0xffffU
);
2128 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2129 return (utils::has_signed_unsigned_overflow
<16>(x
)
2130 ? This::STATUS_OVERFLOW
2131 : This::STATUS_OKAY
);
2134 // R_ARM_ABS32: (S + A) | T
2135 static inline typename
This::Status
2136 abs32(unsigned char *view
,
2137 const Sized_relobj
<32, big_endian
>* object
,
2138 const Symbol_value
<32>* psymval
,
2139 Arm_address thumb_bit
)
2141 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2142 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2143 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2144 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2145 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2146 return This::STATUS_OKAY
;
2149 // R_ARM_REL32: (S + A) | T - P
2150 static inline typename
This::Status
2151 rel32(unsigned char *view
,
2152 const Sized_relobj
<32, big_endian
>* object
,
2153 const Symbol_value
<32>* psymval
,
2154 Arm_address address
,
2155 Arm_address thumb_bit
)
2157 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2158 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2159 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2160 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2161 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2162 return This::STATUS_OKAY
;
2165 // R_ARM_THM_CALL: (S + A) | T - P
2166 static inline typename
This::Status
2167 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2168 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2169 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2170 Arm_address address
, Arm_address thumb_bit
,
2171 bool is_weakly_undefined_without_plt
)
2173 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2174 object
, r_sym
, psymval
, address
, thumb_bit
,
2175 is_weakly_undefined_without_plt
);
2178 // R_ARM_THM_JUMP24: (S + A) | T - P
2179 static inline typename
This::Status
2180 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2181 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2182 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2183 Arm_address address
, Arm_address thumb_bit
,
2184 bool is_weakly_undefined_without_plt
)
2186 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2187 object
, r_sym
, psymval
, address
, thumb_bit
,
2188 is_weakly_undefined_without_plt
);
2191 // R_ARM_THM_XPC22: (S + A) | T - P
2192 static inline typename
This::Status
2193 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2194 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2195 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2196 Arm_address address
, Arm_address thumb_bit
,
2197 bool is_weakly_undefined_without_plt
)
2199 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2200 object
, r_sym
, psymval
, address
, thumb_bit
,
2201 is_weakly_undefined_without_plt
);
2204 // R_ARM_BASE_PREL: B(S) + A - P
2205 static inline typename
This::Status
2206 base_prel(unsigned char* view
,
2208 Arm_address address
)
2210 Base::rel32(view
, origin
- address
);
2214 // R_ARM_BASE_ABS: B(S) + A
2215 static inline typename
This::Status
2216 base_abs(unsigned char* view
,
2219 Base::rel32(view
, origin
);
2223 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2224 static inline typename
This::Status
2225 got_brel(unsigned char* view
,
2226 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2228 Base::rel32(view
, got_offset
);
2229 return This::STATUS_OKAY
;
2232 // R_ARM_GOT_PREL: GOT(S) + A - P
2233 static inline typename
This::Status
2234 got_prel(unsigned char *view
,
2235 Arm_address got_entry
,
2236 Arm_address address
)
2238 Base::rel32(view
, got_entry
- address
);
2239 return This::STATUS_OKAY
;
2242 // R_ARM_PLT32: (S + A) | T - P
2243 static inline typename
This::Status
2244 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2245 unsigned char *view
,
2246 const Sized_symbol
<32>* gsym
,
2247 const Arm_relobj
<big_endian
>* object
,
2249 const Symbol_value
<32>* psymval
,
2250 Arm_address address
,
2251 Arm_address thumb_bit
,
2252 bool is_weakly_undefined_without_plt
)
2254 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2255 object
, r_sym
, psymval
, address
, thumb_bit
,
2256 is_weakly_undefined_without_plt
);
2259 // R_ARM_XPC25: (S + A) | T - P
2260 static inline typename
This::Status
2261 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2262 unsigned char *view
,
2263 const Sized_symbol
<32>* gsym
,
2264 const Arm_relobj
<big_endian
>* object
,
2266 const Symbol_value
<32>* psymval
,
2267 Arm_address address
,
2268 Arm_address thumb_bit
,
2269 bool is_weakly_undefined_without_plt
)
2271 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2272 object
, r_sym
, psymval
, address
, thumb_bit
,
2273 is_weakly_undefined_without_plt
);
2276 // R_ARM_CALL: (S + A) | T - P
2277 static inline typename
This::Status
2278 call(const Relocate_info
<32, big_endian
>* relinfo
,
2279 unsigned char *view
,
2280 const Sized_symbol
<32>* gsym
,
2281 const Arm_relobj
<big_endian
>* object
,
2283 const Symbol_value
<32>* psymval
,
2284 Arm_address address
,
2285 Arm_address thumb_bit
,
2286 bool is_weakly_undefined_without_plt
)
2288 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2289 object
, r_sym
, psymval
, address
, thumb_bit
,
2290 is_weakly_undefined_without_plt
);
2293 // R_ARM_JUMP24: (S + A) | T - P
2294 static inline typename
This::Status
2295 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2296 unsigned char *view
,
2297 const Sized_symbol
<32>* gsym
,
2298 const Arm_relobj
<big_endian
>* object
,
2300 const Symbol_value
<32>* psymval
,
2301 Arm_address address
,
2302 Arm_address thumb_bit
,
2303 bool is_weakly_undefined_without_plt
)
2305 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2306 object
, r_sym
, psymval
, address
, thumb_bit
,
2307 is_weakly_undefined_without_plt
);
2310 // R_ARM_PREL: (S + A) | T - P
2311 static inline typename
This::Status
2312 prel31(unsigned char *view
,
2313 const Sized_relobj
<32, big_endian
>* object
,
2314 const Symbol_value
<32>* psymval
,
2315 Arm_address address
,
2316 Arm_address thumb_bit
)
2318 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2319 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2320 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2321 Valtype addend
= utils::sign_extend
<31>(val
);
2322 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2323 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2324 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2325 return (utils::has_overflow
<31>(x
) ?
2326 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2329 // R_ARM_MOVW_ABS_NC: (S + A) | T
2330 static inline typename
This::Status
2331 movw_abs_nc(unsigned char *view
,
2332 const Sized_relobj
<32, big_endian
>* object
,
2333 const Symbol_value
<32>* psymval
,
2334 Arm_address thumb_bit
)
2336 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2337 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2338 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2339 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2340 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2341 val
= This::insert_val_arm_movw_movt(val
, x
);
2342 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2343 return This::STATUS_OKAY
;
2346 // R_ARM_MOVT_ABS: S + A
2347 static inline typename
This::Status
2348 movt_abs(unsigned char *view
,
2349 const Sized_relobj
<32, big_endian
>* object
,
2350 const Symbol_value
<32>* psymval
)
2352 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2353 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2354 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2355 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2356 Valtype x
= psymval
->value(object
, addend
) >> 16;
2357 val
= This::insert_val_arm_movw_movt(val
, x
);
2358 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2359 return This::STATUS_OKAY
;
2362 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2363 static inline typename
This::Status
2364 thm_movw_abs_nc(unsigned char *view
,
2365 const Sized_relobj
<32, big_endian
>* object
,
2366 const Symbol_value
<32>* psymval
,
2367 Arm_address thumb_bit
)
2369 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2370 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2371 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2372 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2373 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2374 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2375 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2376 val
= This::insert_val_thumb_movw_movt(val
, x
);
2377 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2378 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2379 return This::STATUS_OKAY
;
2382 // R_ARM_THM_MOVT_ABS: S + A
2383 static inline typename
This::Status
2384 thm_movt_abs(unsigned char *view
,
2385 const Sized_relobj
<32, big_endian
>* object
,
2386 const Symbol_value
<32>* psymval
)
2388 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2389 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2390 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2391 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2392 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2393 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2394 Reltype x
= psymval
->value(object
, addend
) >> 16;
2395 val
= This::insert_val_thumb_movw_movt(val
, x
);
2396 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2397 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2398 return This::STATUS_OKAY
;
2401 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2402 static inline typename
This::Status
2403 movw_prel_nc(unsigned char *view
,
2404 const Sized_relobj
<32, big_endian
>* object
,
2405 const Symbol_value
<32>* psymval
,
2406 Arm_address address
,
2407 Arm_address thumb_bit
)
2409 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2410 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2411 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2412 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2413 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2414 val
= This::insert_val_arm_movw_movt(val
, x
);
2415 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2416 return This::STATUS_OKAY
;
2419 // R_ARM_MOVT_PREL: S + A - P
2420 static inline typename
This::Status
2421 movt_prel(unsigned char *view
,
2422 const Sized_relobj
<32, big_endian
>* object
,
2423 const Symbol_value
<32>* psymval
,
2424 Arm_address address
)
2426 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2427 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2428 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2429 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2430 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2431 val
= This::insert_val_arm_movw_movt(val
, x
);
2432 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2433 return This::STATUS_OKAY
;
2436 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2437 static inline typename
This::Status
2438 thm_movw_prel_nc(unsigned char *view
,
2439 const Sized_relobj
<32, big_endian
>* object
,
2440 const Symbol_value
<32>* psymval
,
2441 Arm_address address
,
2442 Arm_address thumb_bit
)
2444 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2445 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2446 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2447 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2448 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2449 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2450 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2451 val
= This::insert_val_thumb_movw_movt(val
, x
);
2452 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2453 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2454 return This::STATUS_OKAY
;
2457 // R_ARM_THM_MOVT_PREL: S + A - P
2458 static inline typename
This::Status
2459 thm_movt_prel(unsigned char *view
,
2460 const Sized_relobj
<32, big_endian
>* object
,
2461 const Symbol_value
<32>* psymval
,
2462 Arm_address address
)
2464 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2465 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2466 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2467 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2468 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2469 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2470 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2471 val
= This::insert_val_thumb_movw_movt(val
, x
);
2472 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2473 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2474 return This::STATUS_OKAY
;
2478 // Relocate ARM long branches. This handles relocation types
2479 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2480 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2481 // undefined and we do not use PLT in this relocation. In such a case,
2482 // the branch is converted into an NOP.
2484 template<bool big_endian
>
2485 typename Arm_relocate_functions
<big_endian
>::Status
2486 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2487 unsigned int r_type
,
2488 const Relocate_info
<32, big_endian
>* relinfo
,
2489 unsigned char *view
,
2490 const Sized_symbol
<32>* gsym
,
2491 const Arm_relobj
<big_endian
>* object
,
2493 const Symbol_value
<32>* psymval
,
2494 Arm_address address
,
2495 Arm_address thumb_bit
,
2496 bool is_weakly_undefined_without_plt
)
2498 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2499 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2500 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2502 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2503 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2504 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2505 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2506 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2507 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2508 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2510 // Check that the instruction is valid.
2511 if (r_type
== elfcpp::R_ARM_CALL
)
2513 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2514 return This::STATUS_BAD_RELOC
;
2516 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2518 if (!insn_is_b
&& !insn_is_cond_bl
)
2519 return This::STATUS_BAD_RELOC
;
2521 else if (r_type
== elfcpp::R_ARM_PLT32
)
2523 if (!insn_is_any_branch
)
2524 return This::STATUS_BAD_RELOC
;
2526 else if (r_type
== elfcpp::R_ARM_XPC25
)
2528 // FIXME: AAELF document IH0044C does not say much about it other
2529 // than it being obsolete.
2530 if (!insn_is_any_branch
)
2531 return This::STATUS_BAD_RELOC
;
2536 // A branch to an undefined weak symbol is turned into a jump to
2537 // the next instruction unless a PLT entry will be created.
2538 // Do the same for local undefined symbols.
2539 // The jump to the next instruction is optimized as a NOP depending
2540 // on the architecture.
2541 const Target_arm
<big_endian
>* arm_target
=
2542 Target_arm
<big_endian
>::default_target();
2543 if (is_weakly_undefined_without_plt
)
2545 Valtype cond
= val
& 0xf0000000U
;
2546 if (arm_target
->may_use_arm_nop())
2547 val
= cond
| 0x0320f000;
2549 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2550 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2551 return This::STATUS_OKAY
;
2554 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2555 Valtype branch_target
= psymval
->value(object
, addend
);
2556 int32_t branch_offset
= branch_target
- address
;
2558 // We need a stub if the branch offset is too large or if we need
2560 bool may_use_blx
= arm_target
->may_use_blx();
2561 Reloc_stub
* stub
= NULL
;
2562 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2563 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2564 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2566 Stub_type stub_type
=
2567 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2569 if (stub_type
!= arm_stub_none
)
2571 Stub_table
<big_endian
>* stub_table
=
2572 object
->stub_table(relinfo
->data_shndx
);
2573 gold_assert(stub_table
!= NULL
);
2575 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2576 stub
= stub_table
->find_reloc_stub(stub_key
);
2577 gold_assert(stub
!= NULL
);
2578 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2579 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2580 branch_offset
= branch_target
- address
;
2581 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2582 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2586 // At this point, if we still need to switch mode, the instruction
2587 // must either be a BLX or a BL that can be converted to a BLX.
2591 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2592 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2595 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2596 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2597 return (utils::has_overflow
<26>(branch_offset
)
2598 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2601 // Relocate THUMB long branches. This handles relocation types
2602 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2603 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2604 // undefined and we do not use PLT in this relocation. In such a case,
2605 // the branch is converted into an NOP.
2607 template<bool big_endian
>
2608 typename Arm_relocate_functions
<big_endian
>::Status
2609 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2610 unsigned int r_type
,
2611 const Relocate_info
<32, big_endian
>* relinfo
,
2612 unsigned char *view
,
2613 const Sized_symbol
<32>* gsym
,
2614 const Arm_relobj
<big_endian
>* object
,
2616 const Symbol_value
<32>* psymval
,
2617 Arm_address address
,
2618 Arm_address thumb_bit
,
2619 bool is_weakly_undefined_without_plt
)
2621 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2622 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2623 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2624 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2626 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2628 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2629 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2631 // Check that the instruction is valid.
2632 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2634 if (!is_bl_insn
&& !is_blx_insn
)
2635 return This::STATUS_BAD_RELOC
;
2637 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2639 // This cannot be a BLX.
2641 return This::STATUS_BAD_RELOC
;
2643 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2645 // Check for Thumb to Thumb call.
2647 return This::STATUS_BAD_RELOC
;
2650 gold_warning(_("%s: Thumb BLX instruction targets "
2651 "thumb function '%s'."),
2652 object
->name().c_str(),
2653 (gsym
? gsym
->name() : "(local)"));
2654 // Convert BLX to BL.
2655 lower_insn
|= 0x1000U
;
2661 // A branch to an undefined weak symbol is turned into a jump to
2662 // the next instruction unless a PLT entry will be created.
2663 // The jump to the next instruction is optimized as a NOP.W for
2664 // Thumb-2 enabled architectures.
2665 const Target_arm
<big_endian
>* arm_target
=
2666 Target_arm
<big_endian
>::default_target();
2667 if (is_weakly_undefined_without_plt
)
2669 if (arm_target
->may_use_thumb2_nop())
2671 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2672 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2676 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2677 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2679 return This::STATUS_OKAY
;
2682 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2683 // with Thumb-1) involving the J1 and J2 bits.
2684 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
2685 uint32_t upper
= upper_insn
& 0x3ff;
2686 uint32_t lower
= lower_insn
& 0x7ff;
2687 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
2688 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
2689 uint32_t i1
= j1
^ s
? 0 : 1;
2690 uint32_t i2
= j2
^ s
? 0 : 1;
2692 int32_t addend
= (i1
<< 23) | (i2
<< 22) | (upper
<< 12) | (lower
<< 1);
2694 addend
= (addend
| ((s
? 0 : 1) << 24)) - (1 << 24);
2696 Arm_address branch_target
= psymval
->value(object
, addend
);
2697 int32_t branch_offset
= branch_target
- address
;
2699 // We need a stub if the branch offset is too large or if we need
2701 bool may_use_blx
= arm_target
->may_use_blx();
2702 bool thumb2
= arm_target
->using_thumb2();
2704 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2705 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2707 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2708 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2709 || ((thumb_bit
== 0)
2710 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2711 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2713 Stub_type stub_type
=
2714 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2716 if (stub_type
!= arm_stub_none
)
2718 Stub_table
<big_endian
>* stub_table
=
2719 object
->stub_table(relinfo
->data_shndx
);
2720 gold_assert(stub_table
!= NULL
);
2722 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2723 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2724 gold_assert(stub
!= NULL
);
2725 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2726 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2727 branch_offset
= branch_target
- address
;
2731 // At this point, if we still need to switch mode, the instruction
2732 // must either be a BLX or a BL that can be converted to a BLX.
2735 gold_assert(may_use_blx
2736 && (r_type
== elfcpp::R_ARM_THM_CALL
2737 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2738 // Make sure this is a BLX.
2739 lower_insn
&= ~0x1000U
;
2743 // Make sure this is a BL.
2744 lower_insn
|= 0x1000U
;
2747 uint32_t reloc_sign
= (branch_offset
< 0) ? 1 : 0;
2748 uint32_t relocation
= static_cast<uint32_t>(branch_offset
);
2750 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2751 // For a BLX instruction, make sure that the relocation is rounded up
2752 // to a word boundary. This follows the semantics of the instruction
2753 // which specifies that bit 1 of the target address will come from bit
2754 // 1 of the base address.
2755 relocation
= (relocation
+ 2U) & ~3U;
2757 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2758 // We use the Thumb-2 encoding, which is safe even if dealing with
2759 // a Thumb-1 instruction by virtue of our overflow check above. */
2760 upper_insn
= (upper_insn
& ~0x7ffU
)
2761 | ((relocation
>> 12) & 0x3ffU
)
2762 | (reloc_sign
<< 10);
2763 lower_insn
= (lower_insn
& ~0x2fffU
)
2764 | (((!((relocation
>> 23) & 1U)) ^ reloc_sign
) << 13)
2765 | (((!((relocation
>> 22) & 1U)) ^ reloc_sign
) << 11)
2766 | ((relocation
>> 1) & 0x7ffU
);
2768 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2769 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2772 ? utils::has_overflow
<25>(relocation
)
2773 : utils::has_overflow
<23>(relocation
))
2774 ? This::STATUS_OVERFLOW
2775 : This::STATUS_OKAY
);
2778 // Get the GOT section, creating it if necessary.
2780 template<bool big_endian
>
2781 Output_data_got
<32, big_endian
>*
2782 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2784 if (this->got_
== NULL
)
2786 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2788 this->got_
= new Output_data_got
<32, big_endian
>();
2791 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2793 | elfcpp::SHF_WRITE
),
2794 this->got_
, false, true, true,
2797 // The old GNU linker creates a .got.plt section. We just
2798 // create another set of data in the .got section. Note that we
2799 // always create a PLT if we create a GOT, although the PLT
2801 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2802 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2804 | elfcpp::SHF_WRITE
),
2805 this->got_plt_
, false, false,
2808 // The first three entries are reserved.
2809 this->got_plt_
->set_current_data_size(3 * 4);
2811 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2812 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2813 Symbol_table::PREDEFINED
,
2815 0, 0, elfcpp::STT_OBJECT
,
2817 elfcpp::STV_HIDDEN
, 0,
2823 // Get the dynamic reloc section, creating it if necessary.
2825 template<bool big_endian
>
2826 typename Target_arm
<big_endian
>::Reloc_section
*
2827 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2829 if (this->rel_dyn_
== NULL
)
2831 gold_assert(layout
!= NULL
);
2832 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2833 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2834 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
2835 false, false, false);
2837 return this->rel_dyn_
;
2840 // Insn_template methods.
2842 // Return byte size of an instruction template.
2845 Insn_template::size() const
2847 switch (this->type())
2850 case THUMB16_SPECIAL_TYPE
:
2861 // Return alignment of an instruction template.
2864 Insn_template::alignment() const
2866 switch (this->type())
2869 case THUMB16_SPECIAL_TYPE
:
2880 // Stub_template methods.
2882 Stub_template::Stub_template(
2883 Stub_type type
, const Insn_template
* insns
,
2885 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2886 entry_in_thumb_mode_(false), relocs_()
2890 // Compute byte size and alignment of stub template.
2891 for (size_t i
= 0; i
< insn_count
; i
++)
2893 unsigned insn_alignment
= insns
[i
].alignment();
2894 size_t insn_size
= insns
[i
].size();
2895 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2896 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2897 switch (insns
[i
].type())
2899 case Insn_template::THUMB16_TYPE
:
2901 this->entry_in_thumb_mode_
= true;
2904 case Insn_template::THUMB32_TYPE
:
2905 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2906 this->relocs_
.push_back(Reloc(i
, offset
));
2908 this->entry_in_thumb_mode_
= true;
2911 case Insn_template::ARM_TYPE
:
2912 // Handle cases where the target is encoded within the
2914 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2915 this->relocs_
.push_back(Reloc(i
, offset
));
2918 case Insn_template::DATA_TYPE
:
2919 // Entry point cannot be data.
2920 gold_assert(i
!= 0);
2921 this->relocs_
.push_back(Reloc(i
, offset
));
2927 offset
+= insn_size
;
2929 this->size_
= offset
;
2934 // Template to implement do_write for a specific target endianity.
2936 template<bool big_endian
>
2938 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
2940 const Stub_template
* stub_template
= this->stub_template();
2941 const Insn_template
* insns
= stub_template
->insns();
2943 // FIXME: We do not handle BE8 encoding yet.
2944 unsigned char* pov
= view
;
2945 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2947 switch (insns
[i
].type())
2949 case Insn_template::THUMB16_TYPE
:
2950 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2952 case Insn_template::THUMB16_SPECIAL_TYPE
:
2953 elfcpp::Swap
<16, big_endian
>::writeval(
2955 this->thumb16_special(i
));
2957 case Insn_template::THUMB32_TYPE
:
2959 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2960 uint32_t lo
= insns
[i
].data() & 0xffff;
2961 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2962 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2965 case Insn_template::ARM_TYPE
:
2966 case Insn_template::DATA_TYPE
:
2967 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2972 pov
+= insns
[i
].size();
2974 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2977 // Reloc_stub::Key methods.
2979 // Dump a Key as a string for debugging.
2982 Reloc_stub::Key::name() const
2984 if (this->r_sym_
== invalid_index
)
2986 // Global symbol key name
2987 // <stub-type>:<symbol name>:<addend>.
2988 const std::string sym_name
= this->u_
.symbol
->name();
2989 // We need to print two hex number and two colons. So just add 100 bytes
2990 // to the symbol name size.
2991 size_t len
= sym_name
.size() + 100;
2992 char* buffer
= new char[len
];
2993 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2994 sym_name
.c_str(), this->addend_
);
2995 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2997 return std::string(buffer
);
3001 // local symbol key name
3002 // <stub-type>:<object>:<r_sym>:<addend>.
3003 const size_t len
= 200;
3005 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3006 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3007 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3008 return std::string(buffer
);
3012 // Reloc_stub methods.
3014 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3015 // LOCATION to DESTINATION.
3016 // This code is based on the arm_type_of_stub function in
3017 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3021 Reloc_stub::stub_type_for_reloc(
3022 unsigned int r_type
,
3023 Arm_address location
,
3024 Arm_address destination
,
3025 bool target_is_thumb
)
3027 Stub_type stub_type
= arm_stub_none
;
3029 // This is a bit ugly but we want to avoid using a templated class for
3030 // big and little endianities.
3032 bool should_force_pic_veneer
;
3035 if (parameters
->target().is_big_endian())
3037 const Target_arm
<true>* big_endian_target
=
3038 Target_arm
<true>::default_target();
3039 may_use_blx
= big_endian_target
->may_use_blx();
3040 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3041 thumb2
= big_endian_target
->using_thumb2();
3042 thumb_only
= big_endian_target
->using_thumb_only();
3046 const Target_arm
<false>* little_endian_target
=
3047 Target_arm
<false>::default_target();
3048 may_use_blx
= little_endian_target
->may_use_blx();
3049 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3050 thumb2
= little_endian_target
->using_thumb2();
3051 thumb_only
= little_endian_target
->using_thumb_only();
3054 int64_t branch_offset
= (int64_t)destination
- location
;
3056 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3058 // Handle cases where:
3059 // - this call goes too far (different Thumb/Thumb2 max
3061 // - it's a Thumb->Arm call and blx is not available, or it's a
3062 // Thumb->Arm branch (not bl). A stub is needed in this case.
3064 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3065 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3067 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3068 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3069 || ((!target_is_thumb
)
3070 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3071 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3073 if (target_is_thumb
)
3078 stub_type
= (parameters
->options().shared()
3079 || should_force_pic_veneer
)
3082 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3083 // V5T and above. Stub starts with ARM code, so
3084 // we must be able to switch mode before
3085 // reaching it, which is only possible for 'bl'
3086 // (ie R_ARM_THM_CALL relocation).
3087 ? arm_stub_long_branch_any_thumb_pic
3088 // On V4T, use Thumb code only.
3089 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3093 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3094 ? arm_stub_long_branch_any_any
// V5T and above.
3095 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3099 stub_type
= (parameters
->options().shared()
3100 || should_force_pic_veneer
)
3101 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3102 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3109 // FIXME: We should check that the input section is from an
3110 // object that has interwork enabled.
3112 stub_type
= (parameters
->options().shared()
3113 || should_force_pic_veneer
)
3116 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3117 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3118 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3122 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3123 ? arm_stub_long_branch_any_any
// V5T and above.
3124 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3126 // Handle v4t short branches.
3127 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3128 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3129 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3130 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3134 else if (r_type
== elfcpp::R_ARM_CALL
3135 || r_type
== elfcpp::R_ARM_JUMP24
3136 || r_type
== elfcpp::R_ARM_PLT32
)
3138 if (target_is_thumb
)
3142 // FIXME: We should check that the input section is from an
3143 // object that has interwork enabled.
3145 // We have an extra 2-bytes reach because of
3146 // the mode change (bit 24 (H) of BLX encoding).
3147 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3148 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3149 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3150 || (r_type
== elfcpp::R_ARM_JUMP24
)
3151 || (r_type
== elfcpp::R_ARM_PLT32
))
3153 stub_type
= (parameters
->options().shared()
3154 || should_force_pic_veneer
)
3157 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3158 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3162 ? arm_stub_long_branch_any_any
// V5T and above.
3163 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3169 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3170 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3172 stub_type
= (parameters
->options().shared()
3173 || should_force_pic_veneer
)
3174 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3175 : arm_stub_long_branch_any_any
; /// non-PIC.
3183 // Cortex_a8_stub methods.
3185 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3186 // I is the position of the instruction template in the stub template.
3189 Cortex_a8_stub::do_thumb16_special(size_t i
)
3191 // The only use of this is to copy condition code from a conditional
3192 // branch being worked around to the corresponding conditional branch in
3194 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3196 uint16_t data
= this->stub_template()->insns()[i
].data();
3197 gold_assert((data
& 0xff00U
) == 0xd000U
);
3198 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3202 // Stub_factory methods.
3204 Stub_factory::Stub_factory()
3206 // The instruction template sequences are declared as static
3207 // objects and initialized first time the constructor runs.
3209 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3210 // to reach the stub if necessary.
3211 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3213 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3214 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3215 // dcd R_ARM_ABS32(X)
3218 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3220 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3222 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3223 Insn_template::arm_insn(0xe12fff1c), // bx ip
3224 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3225 // dcd R_ARM_ABS32(X)
3228 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3229 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3231 Insn_template::thumb16_insn(0xb401), // push {r0}
3232 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3233 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3234 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3235 Insn_template::thumb16_insn(0x4760), // bx ip
3236 Insn_template::thumb16_insn(0xbf00), // nop
3237 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3238 // dcd R_ARM_ABS32(X)
3241 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3243 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3245 Insn_template::thumb16_insn(0x4778), // bx pc
3246 Insn_template::thumb16_insn(0x46c0), // nop
3247 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3248 Insn_template::arm_insn(0xe12fff1c), // bx ip
3249 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3250 // dcd R_ARM_ABS32(X)
3253 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3255 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3257 Insn_template::thumb16_insn(0x4778), // bx pc
3258 Insn_template::thumb16_insn(0x46c0), // nop
3259 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3260 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3261 // dcd R_ARM_ABS32(X)
3264 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3265 // one, when the destination is close enough.
3266 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3268 Insn_template::thumb16_insn(0x4778), // bx pc
3269 Insn_template::thumb16_insn(0x46c0), // nop
3270 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3273 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3274 // blx to reach the stub if necessary.
3275 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3277 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3278 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3279 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3280 // dcd R_ARM_REL32(X-4)
3283 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3284 // blx to reach the stub if necessary. We can not add into pc;
3285 // it is not guaranteed to mode switch (different in ARMv6 and
3287 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3289 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3290 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3291 Insn_template::arm_insn(0xe12fff1c), // bx ip
3292 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3293 // dcd R_ARM_REL32(X)
3296 // V4T ARM -> ARM long branch stub, PIC.
3297 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3299 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3300 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3301 Insn_template::arm_insn(0xe12fff1c), // bx ip
3302 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3303 // dcd R_ARM_REL32(X)
3306 // V4T Thumb -> ARM long branch stub, PIC.
3307 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3309 Insn_template::thumb16_insn(0x4778), // bx pc
3310 Insn_template::thumb16_insn(0x46c0), // nop
3311 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3312 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3313 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3314 // dcd R_ARM_REL32(X)
3317 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3319 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3321 Insn_template::thumb16_insn(0xb401), // push {r0}
3322 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3323 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3324 Insn_template::thumb16_insn(0x4484), // add ip, r0
3325 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3326 Insn_template::thumb16_insn(0x4760), // bx ip
3327 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3328 // dcd R_ARM_REL32(X)
3331 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3333 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3335 Insn_template::thumb16_insn(0x4778), // bx pc
3336 Insn_template::thumb16_insn(0x46c0), // nop
3337 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3338 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3339 Insn_template::arm_insn(0xe12fff1c), // bx ip
3340 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3341 // dcd R_ARM_REL32(X)
3344 // Cortex-A8 erratum-workaround stubs.
3346 // Stub used for conditional branches (which may be beyond +/-1MB away,
3347 // so we can't use a conditional branch to reach this stub).
3354 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3356 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3357 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3358 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3362 // Stub used for b.w and bl.w instructions.
3364 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3366 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3369 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3371 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3374 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3375 // instruction (which switches to ARM mode) to point to this stub. Jump to
3376 // the real destination using an ARM-mode branch.
3377 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3379 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3382 // Fill in the stub template look-up table. Stub templates are constructed
3383 // per instance of Stub_factory for fast look-up without locking
3384 // in a thread-enabled environment.
3386 this->stub_templates_
[arm_stub_none
] =
3387 new Stub_template(arm_stub_none
, NULL
, 0);
3389 #define DEF_STUB(x) \
3393 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3394 Stub_type type = arm_stub_##x; \
3395 this->stub_templates_[type] = \
3396 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3404 // Stub_table methods.
3406 // Removel all Cortex-A8 stub.
3408 template<bool big_endian
>
3410 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
3412 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3413 p
!= this->cortex_a8_stubs_
.end();
3416 this->cortex_a8_stubs_
.clear();
3419 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3421 template<bool big_endian
>
3423 Stub_table
<big_endian
>::relocate_stub(
3425 const Relocate_info
<32, big_endian
>* relinfo
,
3426 Target_arm
<big_endian
>* arm_target
,
3427 Output_section
* output_section
,
3428 unsigned char* view
,
3429 Arm_address address
,
3430 section_size_type view_size
)
3432 const Stub_template
* stub_template
= stub
->stub_template();
3433 if (stub_template
->reloc_count() != 0)
3435 // Adjust view to cover the stub only.
3436 section_size_type offset
= stub
->offset();
3437 section_size_type stub_size
= stub_template
->size();
3438 gold_assert(offset
+ stub_size
<= view_size
);
3440 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
3441 address
+ offset
, stub_size
);
3445 // Relocate all stubs in this stub table.
3447 template<bool big_endian
>
3449 Stub_table
<big_endian
>::relocate_stubs(
3450 const Relocate_info
<32, big_endian
>* relinfo
,
3451 Target_arm
<big_endian
>* arm_target
,
3452 Output_section
* output_section
,
3453 unsigned char* view
,
3454 Arm_address address
,
3455 section_size_type view_size
)
3457 // If we are passed a view bigger than the stub table's. we need to
3459 gold_assert(address
== this->address()
3461 == static_cast<section_size_type
>(this->data_size())));
3463 // Relocate all relocation stubs.
3464 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3465 p
!= this->reloc_stubs_
.end();
3467 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3468 address
, view_size
);
3470 // Relocate all Cortex-A8 stubs.
3471 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3472 p
!= this->cortex_a8_stubs_
.end();
3474 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3475 address
, view_size
);
3478 // Write out the stubs to file.
3480 template<bool big_endian
>
3482 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3484 off_t offset
= this->offset();
3485 const section_size_type oview_size
=
3486 convert_to_section_size_type(this->data_size());
3487 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3489 // Write relocation stubs.
3490 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3491 p
!= this->reloc_stubs_
.end();
3494 Reloc_stub
* stub
= p
->second
;
3495 Arm_address address
= this->address() + stub
->offset();
3497 == align_address(address
,
3498 stub
->stub_template()->alignment()));
3499 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3503 // Write Cortex-A8 stubs.
3504 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3505 p
!= this->cortex_a8_stubs_
.end();
3508 Cortex_a8_stub
* stub
= p
->second
;
3509 Arm_address address
= this->address() + stub
->offset();
3511 == align_address(address
,
3512 stub
->stub_template()->alignment()));
3513 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3517 of
->write_output_view(this->offset(), oview_size
, oview
);
3520 // Update the data size and address alignment of the stub table at the end
3521 // of a relaxation pass. Return true if either the data size or the
3522 // alignment changed in this relaxation pass.
3524 template<bool big_endian
>
3526 Stub_table
<big_endian
>::update_data_size_and_addralign()
3529 unsigned addralign
= 1;
3531 // Go over all stubs in table to compute data size and address alignment.
3533 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3534 p
!= this->reloc_stubs_
.end();
3537 const Stub_template
* stub_template
= p
->second
->stub_template();
3538 addralign
= std::max(addralign
, stub_template
->alignment());
3539 size
= (align_address(size
, stub_template
->alignment())
3540 + stub_template
->size());
3543 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3544 p
!= this->cortex_a8_stubs_
.end();
3547 const Stub_template
* stub_template
= p
->second
->stub_template();
3548 addralign
= std::max(addralign
, stub_template
->alignment());
3549 size
= (align_address(size
, stub_template
->alignment())
3550 + stub_template
->size());
3553 // Check if either data size or alignment changed in this pass.
3554 // Update prev_data_size_ and prev_addralign_. These will be used
3555 // as the current data size and address alignment for the next pass.
3556 bool changed
= size
!= this->prev_data_size_
;
3557 this->prev_data_size_
= size
;
3559 if (addralign
!= this->prev_addralign_
)
3561 this->prev_addralign_
= addralign
;
3566 // Finalize the stubs. This sets the offsets of the stubs within the stub
3567 // table. It also marks all input sections needing Cortex-A8 workaround.
3569 template<bool big_endian
>
3571 Stub_table
<big_endian
>::finalize_stubs()
3574 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3575 p
!= this->reloc_stubs_
.end();
3578 Reloc_stub
* stub
= p
->second
;
3579 const Stub_template
* stub_template
= stub
->stub_template();
3580 uint64_t stub_addralign
= stub_template
->alignment();
3581 off
= align_address(off
, stub_addralign
);
3582 stub
->set_offset(off
);
3583 off
+= stub_template
->size();
3586 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3587 p
!= this->cortex_a8_stubs_
.end();
3590 Cortex_a8_stub
* stub
= p
->second
;
3591 const Stub_template
* stub_template
= stub
->stub_template();
3592 uint64_t stub_addralign
= stub_template
->alignment();
3593 off
= align_address(off
, stub_addralign
);
3594 stub
->set_offset(off
);
3595 off
+= stub_template
->size();
3597 // Mark input section so that we can determine later if a code section
3598 // needs the Cortex-A8 workaround quickly.
3599 Arm_relobj
<big_endian
>* arm_relobj
=
3600 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
3601 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
3604 gold_assert(off
<= this->prev_data_size_
);
3607 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3608 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3609 // of the address range seen by the linker.
3611 template<bool big_endian
>
3613 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
3614 Target_arm
<big_endian
>* arm_target
,
3615 unsigned char* view
,
3616 Arm_address view_address
,
3617 section_size_type view_size
)
3619 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3620 for (Cortex_a8_stub_list::const_iterator p
=
3621 this->cortex_a8_stubs_
.lower_bound(view_address
);
3622 ((p
!= this->cortex_a8_stubs_
.end())
3623 && (p
->first
< (view_address
+ view_size
)));
3626 // We do not store the THUMB bit in the LSB of either the branch address
3627 // or the stub offset. There is no need to strip the LSB.
3628 Arm_address branch_address
= p
->first
;
3629 const Cortex_a8_stub
* stub
= p
->second
;
3630 Arm_address stub_address
= this->address() + stub
->offset();
3632 // Offset of the branch instruction relative to this view.
3633 section_size_type offset
=
3634 convert_to_section_size_type(branch_address
- view_address
);
3635 gold_assert((offset
+ 4) <= view_size
);
3637 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
3638 view
+ offset
, branch_address
);
3642 // Arm_input_section methods.
3644 // Initialize an Arm_input_section.
3646 template<bool big_endian
>
3648 Arm_input_section
<big_endian
>::init()
3650 Relobj
* relobj
= this->relobj();
3651 unsigned int shndx
= this->shndx();
3653 // Cache these to speed up size and alignment queries. It is too slow
3654 // to call section_addraglin and section_size every time.
3655 this->original_addralign_
= relobj
->section_addralign(shndx
);
3656 this->original_size_
= relobj
->section_size(shndx
);
3658 // We want to make this look like the original input section after
3659 // output sections are finalized.
3660 Output_section
* os
= relobj
->output_section(shndx
);
3661 off_t offset
= relobj
->output_section_offset(shndx
);
3662 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3663 this->set_address(os
->address() + offset
);
3664 this->set_file_offset(os
->offset() + offset
);
3666 this->set_current_data_size(this->original_size_
);
3667 this->finalize_data_size();
3670 template<bool big_endian
>
3672 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3674 // We have to write out the original section content.
3675 section_size_type section_size
;
3676 const unsigned char* section_contents
=
3677 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3678 of
->write(this->offset(), section_contents
, section_size
);
3680 // If this owns a stub table and it is not empty, write it.
3681 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3682 this->stub_table_
->write(of
);
3685 // Finalize data size.
3687 template<bool big_endian
>
3689 Arm_input_section
<big_endian
>::set_final_data_size()
3691 // If this owns a stub table, finalize its data size as well.
3692 if (this->is_stub_table_owner())
3694 uint64_t address
= this->address();
3696 // The stub table comes after the original section contents.
3697 address
+= this->original_size_
;
3698 address
= align_address(address
, this->stub_table_
->addralign());
3699 off_t offset
= this->offset() + (address
- this->address());
3700 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3701 address
+= this->stub_table_
->data_size();
3702 gold_assert(address
== this->address() + this->current_data_size());
3705 this->set_data_size(this->current_data_size());
3708 // Reset address and file offset.
3710 template<bool big_endian
>
3712 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3714 // Size of the original input section contents.
3715 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3717 // If this is a stub table owner, account for the stub table size.
3718 if (this->is_stub_table_owner())
3720 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3722 // Reset the stub table's address and file offset. The
3723 // current data size for child will be updated after that.
3724 stub_table_
->reset_address_and_file_offset();
3725 off
= align_address(off
, stub_table_
->addralign());
3726 off
+= stub_table
->current_data_size();
3729 this->set_current_data_size(off
);
3732 // Arm_output_section methods.
3734 // Create a stub group for input sections from BEGIN to END. OWNER
3735 // points to the input section to be the owner a new stub table.
3737 template<bool big_endian
>
3739 Arm_output_section
<big_endian
>::create_stub_group(
3740 Input_section_list::const_iterator begin
,
3741 Input_section_list::const_iterator end
,
3742 Input_section_list::const_iterator owner
,
3743 Target_arm
<big_endian
>* target
,
3744 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3746 // Currently we convert ordinary input sections into relaxed sections only
3747 // at this point but we may want to support creating relaxed input section
3748 // very early. So we check here to see if owner is already a relaxed
3751 Arm_input_section
<big_endian
>* arm_input_section
;
3752 if (owner
->is_relaxed_input_section())
3755 Arm_input_section
<big_endian
>::as_arm_input_section(
3756 owner
->relaxed_input_section());
3760 gold_assert(owner
->is_input_section());
3761 // Create a new relaxed input section.
3763 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3764 new_relaxed_sections
->push_back(arm_input_section
);
3767 // Create a stub table.
3768 Stub_table
<big_endian
>* stub_table
=
3769 target
->new_stub_table(arm_input_section
);
3771 arm_input_section
->set_stub_table(stub_table
);
3773 Input_section_list::const_iterator p
= begin
;
3774 Input_section_list::const_iterator prev_p
;
3776 // Look for input sections or relaxed input sections in [begin ... end].
3779 if (p
->is_input_section() || p
->is_relaxed_input_section())
3781 // The stub table information for input sections live
3782 // in their objects.
3783 Arm_relobj
<big_endian
>* arm_relobj
=
3784 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3785 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3789 while (prev_p
!= end
);
3792 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3793 // of stub groups. We grow a stub group by adding input section until the
3794 // size is just below GROUP_SIZE. The last input section will be converted
3795 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3796 // input section after the stub table, effectively double the group size.
3798 // This is similar to the group_sections() function in elf32-arm.c but is
3799 // implemented differently.
3801 template<bool big_endian
>
3803 Arm_output_section
<big_endian
>::group_sections(
3804 section_size_type group_size
,
3805 bool stubs_always_after_branch
,
3806 Target_arm
<big_endian
>* target
)
3808 // We only care about sections containing code.
3809 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3812 // States for grouping.
3815 // No group is being built.
3817 // A group is being built but the stub table is not found yet.
3818 // We keep group a stub group until the size is just under GROUP_SIZE.
3819 // The last input section in the group will be used as the stub table.
3820 FINDING_STUB_SECTION
,
3821 // A group is being built and we have already found a stub table.
3822 // We enter this state to grow a stub group by adding input section
3823 // after the stub table. This effectively doubles the group size.
3827 // Any newly created relaxed sections are stored here.
3828 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3830 State state
= NO_GROUP
;
3831 section_size_type off
= 0;
3832 section_size_type group_begin_offset
= 0;
3833 section_size_type group_end_offset
= 0;
3834 section_size_type stub_table_end_offset
= 0;
3835 Input_section_list::const_iterator group_begin
=
3836 this->input_sections().end();
3837 Input_section_list::const_iterator stub_table
=
3838 this->input_sections().end();
3839 Input_section_list::const_iterator group_end
= this->input_sections().end();
3840 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3841 p
!= this->input_sections().end();
3844 section_size_type section_begin_offset
=
3845 align_address(off
, p
->addralign());
3846 section_size_type section_end_offset
=
3847 section_begin_offset
+ p
->data_size();
3849 // Check to see if we should group the previously seens sections.
3855 case FINDING_STUB_SECTION
:
3856 // Adding this section makes the group larger than GROUP_SIZE.
3857 if (section_end_offset
- group_begin_offset
>= group_size
)
3859 if (stubs_always_after_branch
)
3861 gold_assert(group_end
!= this->input_sections().end());
3862 this->create_stub_group(group_begin
, group_end
, group_end
,
3863 target
, &new_relaxed_sections
);
3868 // But wait, there's more! Input sections up to
3869 // stub_group_size bytes after the stub table can be
3870 // handled by it too.
3871 state
= HAS_STUB_SECTION
;
3872 stub_table
= group_end
;
3873 stub_table_end_offset
= group_end_offset
;
3878 case HAS_STUB_SECTION
:
3879 // Adding this section makes the post stub-section group larger
3881 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3883 gold_assert(group_end
!= this->input_sections().end());
3884 this->create_stub_group(group_begin
, group_end
, stub_table
,
3885 target
, &new_relaxed_sections
);
3894 // If we see an input section and currently there is no group, start
3895 // a new one. Skip any empty sections.
3896 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3897 && (p
->relobj()->section_size(p
->shndx()) != 0))
3899 if (state
== NO_GROUP
)
3901 state
= FINDING_STUB_SECTION
;
3903 group_begin_offset
= section_begin_offset
;
3906 // Keep track of the last input section seen.
3908 group_end_offset
= section_end_offset
;
3911 off
= section_end_offset
;
3914 // Create a stub group for any ungrouped sections.
3915 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3917 gold_assert(group_end
!= this->input_sections().end());
3918 this->create_stub_group(group_begin
, group_end
,
3919 (state
== FINDING_STUB_SECTION
3922 target
, &new_relaxed_sections
);
3925 // Convert input section into relaxed input section in a batch.
3926 if (!new_relaxed_sections
.empty())
3927 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3929 // Update the section offsets
3930 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3932 Arm_relobj
<big_endian
>* arm_relobj
=
3933 Arm_relobj
<big_endian
>::as_arm_relobj(
3934 new_relaxed_sections
[i
]->relobj());
3935 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3936 // Tell Arm_relobj that this input section is converted.
3937 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3941 // Arm_relobj methods.
3943 // Scan relocations for stub generation.
3945 template<bool big_endian
>
3947 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3948 Target_arm
<big_endian
>* arm_target
,
3949 const Symbol_table
* symtab
,
3950 const Layout
* layout
)
3952 unsigned int shnum
= this->shnum();
3953 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3955 // Read the section headers.
3956 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3960 // To speed up processing, we set up hash tables for fast lookup of
3961 // input offsets to output addresses.
3962 this->initialize_input_to_output_maps();
3964 const Relobj::Output_sections
& out_sections(this->output_sections());
3966 Relocate_info
<32, big_endian
> relinfo
;
3967 relinfo
.symtab
= symtab
;
3968 relinfo
.layout
= layout
;
3969 relinfo
.object
= this;
3971 const unsigned char* p
= pshdrs
+ shdr_size
;
3972 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3974 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3976 unsigned int sh_type
= shdr
.get_sh_type();
3977 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3980 off_t sh_size
= shdr
.get_sh_size();
3984 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3985 if (index
>= this->shnum())
3987 // Ignore reloc section with bad info. This error will be
3988 // reported in the final link.
3992 Output_section
* os
= out_sections
[index
];
3994 || symtab
->is_section_folded(this, index
))
3996 // This relocation section is against a section which we
3997 // discarded or if the section is folded into another
3998 // section due to ICF.
4001 Arm_address output_offset
= this->get_output_section_offset(index
);
4003 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4005 // Ignore reloc section with unexpected symbol table. The
4006 // error will be reported in the final link.
4010 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
4011 sh_size
, true, false);
4013 unsigned int reloc_size
;
4014 if (sh_type
== elfcpp::SHT_REL
)
4015 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4017 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4019 if (reloc_size
!= shdr
.get_sh_entsize())
4021 // Ignore reloc section with unexpected entsize. The error
4022 // will be reported in the final link.
4026 size_t reloc_count
= sh_size
/ reloc_size
;
4027 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
4029 // Ignore reloc section with uneven size. The error will be
4030 // reported in the final link.
4034 gold_assert(output_offset
!= invalid_address
4035 || this->relocs_must_follow_section_writes());
4037 // Get the section contents. This does work for the case in which
4038 // we modify the contents of an input section. We need to pass the
4039 // output view under such circumstances.
4040 section_size_type input_view_size
= 0;
4041 const unsigned char* input_view
=
4042 this->section_contents(index
, &input_view_size
, false);
4044 relinfo
.reloc_shndx
= i
;
4045 relinfo
.data_shndx
= index
;
4046 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
4048 output_offset
== invalid_address
,
4054 // After we've done the relocations, we release the hash tables,
4055 // since we no longer need them.
4056 this->free_input_to_output_maps();
4059 // Count the local symbols. The ARM backend needs to know if a symbol
4060 // is a THUMB function or not. For global symbols, it is easy because
4061 // the Symbol object keeps the ELF symbol type. For local symbol it is
4062 // harder because we cannot access this information. So we override the
4063 // do_count_local_symbol in parent and scan local symbols to mark
4064 // THUMB functions. This is not the most efficient way but I do not want to
4065 // slow down other ports by calling a per symbol targer hook inside
4066 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4068 template<bool big_endian
>
4070 Arm_relobj
<big_endian
>::do_count_local_symbols(
4071 Stringpool_template
<char>* pool
,
4072 Stringpool_template
<char>* dynpool
)
4074 // We need to fix-up the values of any local symbols whose type are
4077 // Ask parent to count the local symbols.
4078 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
4079 const unsigned int loccount
= this->local_symbol_count();
4083 // Intialize the thumb function bit-vector.
4084 std::vector
<bool> empty_vector(loccount
, false);
4085 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
4087 // Read the symbol table section header.
4088 const unsigned int symtab_shndx
= this->symtab_shndx();
4089 elfcpp::Shdr
<32, big_endian
>
4090 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
4091 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
4093 // Read the local symbols.
4094 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
4095 gold_assert(loccount
== symtabshdr
.get_sh_info());
4096 off_t locsize
= loccount
* sym_size
;
4097 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
4098 locsize
, true, true);
4100 // Loop over the local symbols and mark any local symbols pointing
4101 // to THUMB functions.
4103 // Skip the first dummy symbol.
4105 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
4106 this->local_values();
4107 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
4109 elfcpp::Sym
<32, big_endian
> sym(psyms
);
4110 elfcpp::STT st_type
= sym
.get_st_type();
4111 Symbol_value
<32>& lv((*plocal_values
)[i
]);
4112 Arm_address input_value
= lv
.input_value();
4114 if (st_type
== elfcpp::STT_ARM_TFUNC
4115 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
4117 // This is a THUMB function. Mark this and canonicalize the
4118 // symbol value by setting LSB.
4119 this->local_symbol_is_thumb_function_
[i
] = true;
4120 if ((input_value
& 1) == 0)
4121 lv
.set_input_value(input_value
| 1);
4126 // Relocate sections.
4127 template<bool big_endian
>
4129 Arm_relobj
<big_endian
>::do_relocate_sections(
4130 const Symbol_table
* symtab
,
4131 const Layout
* layout
,
4132 const unsigned char* pshdrs
,
4133 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
4135 // Call parent to relocate sections.
4136 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
4139 // We do not generate stubs if doing a relocatable link.
4140 if (parameters
->options().relocatable())
4143 // Relocate stub tables.
4144 unsigned int shnum
= this->shnum();
4146 Target_arm
<big_endian
>* arm_target
=
4147 Target_arm
<big_endian
>::default_target();
4149 Relocate_info
<32, big_endian
> relinfo
;
4150 relinfo
.symtab
= symtab
;
4151 relinfo
.layout
= layout
;
4152 relinfo
.object
= this;
4154 for (unsigned int i
= 1; i
< shnum
; ++i
)
4156 Arm_input_section
<big_endian
>* arm_input_section
=
4157 arm_target
->find_arm_input_section(this, i
);
4159 if (arm_input_section
== NULL
4160 || !arm_input_section
->is_stub_table_owner()
4161 || arm_input_section
->stub_table()->empty())
4164 // We cannot discard a section if it owns a stub table.
4165 Output_section
* os
= this->output_section(i
);
4166 gold_assert(os
!= NULL
);
4168 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
4169 relinfo
.reloc_shdr
= NULL
;
4170 relinfo
.data_shndx
= i
;
4171 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
4173 gold_assert((*pviews
)[i
].view
!= NULL
);
4175 // We are passed the output section view. Adjust it to cover the
4177 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
4178 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
4179 && ((stub_table
->address() + stub_table
->data_size())
4180 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
4182 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
4183 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
4184 Arm_address address
= stub_table
->address();
4185 section_size_type view_size
= stub_table
->data_size();
4187 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
4192 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4195 template<bool big_endian
>
4196 Attributes_section_data
*
4197 read_arm_attributes_section(
4199 Read_symbols_data
*sd
)
4201 // Read the attributes section if there is one.
4202 // We read from the end because gas seems to put it near the end of
4203 // the section headers.
4204 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4205 const unsigned char *ps
=
4206 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
4207 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
4209 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4210 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
4212 section_offset_type section_offset
= shdr
.get_sh_offset();
4213 section_size_type section_size
=
4214 convert_to_section_size_type(shdr
.get_sh_size());
4215 File_view
* view
= object
->get_lasting_view(section_offset
,
4216 section_size
, true, false);
4217 return new Attributes_section_data(view
->data(), section_size
);
4223 // Read the symbol information.
4225 template<bool big_endian
>
4227 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4229 // Call parent class to read symbol information.
4230 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
4232 // Read processor-specific flags in ELF file header.
4233 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4234 elfcpp::Elf_sizes
<32>::ehdr_size
,
4236 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4237 this->processor_specific_flags_
= ehdr
.get_e_flags();
4238 this->attributes_section_data_
=
4239 read_arm_attributes_section
<big_endian
>(this, sd
);
4242 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4243 // sections for unwinding. These sections are referenced implicitly by
4244 // text sections linked in the section headers. If we ignore these implict
4245 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4246 // will be garbage-collected incorrectly. Hence we override the same function
4247 // in the base class to handle these implicit references.
4249 template<bool big_endian
>
4251 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
4253 Read_relocs_data
* rd
)
4255 // First, call base class method to process relocations in this object.
4256 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
4258 unsigned int shnum
= this->shnum();
4259 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4260 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4264 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4265 // to these from the linked text sections.
4266 const unsigned char* ps
= pshdrs
+ shdr_size
;
4267 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
4269 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4270 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
4272 // Found an .ARM.exidx section, add it to the set of reachable
4273 // sections from its linked text section.
4274 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
4275 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
4280 // Arm_dynobj methods.
4282 // Read the symbol information.
4284 template<bool big_endian
>
4286 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4288 // Call parent class to read symbol information.
4289 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
4291 // Read processor-specific flags in ELF file header.
4292 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4293 elfcpp::Elf_sizes
<32>::ehdr_size
,
4295 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4296 this->processor_specific_flags_
= ehdr
.get_e_flags();
4297 this->attributes_section_data_
=
4298 read_arm_attributes_section
<big_endian
>(this, sd
);
4301 // Stub_addend_reader methods.
4303 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4305 template<bool big_endian
>
4306 elfcpp::Elf_types
<32>::Elf_Swxword
4307 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
4308 unsigned int r_type
,
4309 const unsigned char* view
,
4310 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
4314 case elfcpp::R_ARM_CALL
:
4315 case elfcpp::R_ARM_JUMP24
:
4316 case elfcpp::R_ARM_PLT32
:
4318 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4319 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4320 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4321 return utils::sign_extend
<26>(val
<< 2);
4324 case elfcpp::R_ARM_THM_CALL
:
4325 case elfcpp::R_ARM_THM_JUMP24
:
4326 case elfcpp::R_ARM_THM_XPC22
:
4328 // Fetch the addend. We use the Thumb-2 encoding (backwards
4329 // compatible with Thumb-1) involving the J1 and J2 bits.
4330 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4331 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4332 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4333 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4335 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
4336 uint32_t upper
= upper_insn
& 0x3ff;
4337 uint32_t lower
= lower_insn
& 0x7ff;
4338 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
4339 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
4340 uint32_t i1
= j1
^ s
? 0 : 1;
4341 uint32_t i2
= j2
^ s
? 0 : 1;
4343 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
4344 | (upper
<< 12) | (lower
<< 1));
4347 case elfcpp::R_ARM_THM_JUMP19
:
4349 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4350 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4351 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4352 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4354 // Reconstruct the top three bits and squish the two 11 bit pieces
4356 uint32_t S
= (upper_insn
& 0x0400) >> 10;
4357 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
4358 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
4360 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
4361 uint32_t lower
= (lower_insn
& 0x07ff);
4362 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
4370 // A class to handle the PLT data.
4372 template<bool big_endian
>
4373 class Output_data_plt_arm
: public Output_section_data
4376 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
4379 Output_data_plt_arm(Layout
*, Output_data_space
*);
4381 // Add an entry to the PLT.
4383 add_entry(Symbol
* gsym
);
4385 // Return the .rel.plt section data.
4386 const Reloc_section
*
4388 { return this->rel_
; }
4392 do_adjust_output_section(Output_section
* os
);
4394 // Write to a map file.
4396 do_print_to_mapfile(Mapfile
* mapfile
) const
4397 { mapfile
->print_output_data(this, _("** PLT")); }
4400 // Template for the first PLT entry.
4401 static const uint32_t first_plt_entry
[5];
4403 // Template for subsequent PLT entries.
4404 static const uint32_t plt_entry
[3];
4406 // Set the final size.
4408 set_final_data_size()
4410 this->set_data_size(sizeof(first_plt_entry
)
4411 + this->count_
* sizeof(plt_entry
));
4414 // Write out the PLT data.
4416 do_write(Output_file
*);
4418 // The reloc section.
4419 Reloc_section
* rel_
;
4420 // The .got.plt section.
4421 Output_data_space
* got_plt_
;
4422 // The number of PLT entries.
4423 unsigned int count_
;
4426 // Create the PLT section. The ordinary .got section is an argument,
4427 // since we need to refer to the start. We also create our own .got
4428 // section just for PLT entries.
4430 template<bool big_endian
>
4431 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
4432 Output_data_space
* got_plt
)
4433 : Output_section_data(4), got_plt_(got_plt
), count_(0)
4435 this->rel_
= new Reloc_section(false);
4436 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
4437 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
4441 template<bool big_endian
>
4443 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
4448 // Add an entry to the PLT.
4450 template<bool big_endian
>
4452 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
4454 gold_assert(!gsym
->has_plt_offset());
4456 // Note that when setting the PLT offset we skip the initial
4457 // reserved PLT entry.
4458 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
4459 + sizeof(first_plt_entry
));
4463 section_offset_type got_offset
= this->got_plt_
->current_data_size();
4465 // Every PLT entry needs a GOT entry which points back to the PLT
4466 // entry (this will be changed by the dynamic linker, normally
4467 // lazily when the function is called).
4468 this->got_plt_
->set_current_data_size(got_offset
+ 4);
4470 // Every PLT entry needs a reloc.
4471 gsym
->set_needs_dynsym_entry();
4472 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
4475 // Note that we don't need to save the symbol. The contents of the
4476 // PLT are independent of which symbols are used. The symbols only
4477 // appear in the relocations.
4481 // FIXME: This is not very flexible. Right now this has only been tested
4482 // on armv5te. If we are to support additional architecture features like
4483 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4485 // The first entry in the PLT.
4486 template<bool big_endian
>
4487 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
4489 0xe52de004, // str lr, [sp, #-4]!
4490 0xe59fe004, // ldr lr, [pc, #4]
4491 0xe08fe00e, // add lr, pc, lr
4492 0xe5bef008, // ldr pc, [lr, #8]!
4493 0x00000000, // &GOT[0] - .
4496 // Subsequent entries in the PLT.
4498 template<bool big_endian
>
4499 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
4501 0xe28fc600, // add ip, pc, #0xNN00000
4502 0xe28cca00, // add ip, ip, #0xNN000
4503 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4506 // Write out the PLT. This uses the hand-coded instructions above,
4507 // and adjusts them as needed. This is all specified by the arm ELF
4508 // Processor Supplement.
4510 template<bool big_endian
>
4512 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4514 const off_t offset
= this->offset();
4515 const section_size_type oview_size
=
4516 convert_to_section_size_type(this->data_size());
4517 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4519 const off_t got_file_offset
= this->got_plt_
->offset();
4520 const section_size_type got_size
=
4521 convert_to_section_size_type(this->got_plt_
->data_size());
4522 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4524 unsigned char* pov
= oview
;
4526 Arm_address plt_address
= this->address();
4527 Arm_address got_address
= this->got_plt_
->address();
4529 // Write first PLT entry. All but the last word are constants.
4530 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4531 / sizeof(plt_entry
[0]));
4532 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4533 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4534 // Last word in first PLT entry is &GOT[0] - .
4535 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4536 got_address
- (plt_address
+ 16));
4537 pov
+= sizeof(first_plt_entry
);
4539 unsigned char* got_pov
= got_view
;
4541 memset(got_pov
, 0, 12);
4544 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4545 unsigned int plt_offset
= sizeof(first_plt_entry
);
4546 unsigned int plt_rel_offset
= 0;
4547 unsigned int got_offset
= 12;
4548 const unsigned int count
= this->count_
;
4549 for (unsigned int i
= 0;
4552 pov
+= sizeof(plt_entry
),
4554 plt_offset
+= sizeof(plt_entry
),
4555 plt_rel_offset
+= rel_size
,
4558 // Set and adjust the PLT entry itself.
4559 int32_t offset
= ((got_address
+ got_offset
)
4560 - (plt_address
+ plt_offset
+ 8));
4562 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4563 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4564 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4565 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4566 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4567 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4568 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4570 // Set the entry in the GOT.
4571 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4574 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4575 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4577 of
->write_output_view(offset
, oview_size
, oview
);
4578 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4581 // Create a PLT entry for a global symbol.
4583 template<bool big_endian
>
4585 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
4588 if (gsym
->has_plt_offset())
4591 if (this->plt_
== NULL
)
4593 // Create the GOT sections first.
4594 this->got_section(symtab
, layout
);
4596 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
4597 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4599 | elfcpp::SHF_EXECINSTR
),
4600 this->plt_
, false, false, false, false);
4602 this->plt_
->add_entry(gsym
);
4605 // Report an unsupported relocation against a local symbol.
4607 template<bool big_endian
>
4609 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4610 Sized_relobj
<32, big_endian
>* object
,
4611 unsigned int r_type
)
4613 gold_error(_("%s: unsupported reloc %u against local symbol"),
4614 object
->name().c_str(), r_type
);
4617 // We are about to emit a dynamic relocation of type R_TYPE. If the
4618 // dynamic linker does not support it, issue an error. The GNU linker
4619 // only issues a non-PIC error for an allocated read-only section.
4620 // Here we know the section is allocated, but we don't know that it is
4621 // read-only. But we check for all the relocation types which the
4622 // glibc dynamic linker supports, so it seems appropriate to issue an
4623 // error even if the section is not read-only.
4625 template<bool big_endian
>
4627 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4628 unsigned int r_type
)
4632 // These are the relocation types supported by glibc for ARM.
4633 case elfcpp::R_ARM_RELATIVE
:
4634 case elfcpp::R_ARM_COPY
:
4635 case elfcpp::R_ARM_GLOB_DAT
:
4636 case elfcpp::R_ARM_JUMP_SLOT
:
4637 case elfcpp::R_ARM_ABS32
:
4638 case elfcpp::R_ARM_ABS32_NOI
:
4639 case elfcpp::R_ARM_PC24
:
4640 // FIXME: The following 3 types are not supported by Android's dynamic
4642 case elfcpp::R_ARM_TLS_DTPMOD32
:
4643 case elfcpp::R_ARM_TLS_DTPOFF32
:
4644 case elfcpp::R_ARM_TLS_TPOFF32
:
4648 // This prevents us from issuing more than one error per reloc
4649 // section. But we can still wind up issuing more than one
4650 // error per object file.
4651 if (this->issued_non_pic_error_
)
4653 object
->error(_("requires unsupported dynamic reloc; "
4654 "recompile with -fPIC"));
4655 this->issued_non_pic_error_
= true;
4658 case elfcpp::R_ARM_NONE
:
4663 // Scan a relocation for a local symbol.
4664 // FIXME: This only handles a subset of relocation types used by Android
4665 // on ARM v5te devices.
4667 template<bool big_endian
>
4669 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4672 Sized_relobj
<32, big_endian
>* object
,
4673 unsigned int data_shndx
,
4674 Output_section
* output_section
,
4675 const elfcpp::Rel
<32, big_endian
>& reloc
,
4676 unsigned int r_type
,
4677 const elfcpp::Sym
<32, big_endian
>&)
4679 r_type
= get_real_reloc_type(r_type
);
4682 case elfcpp::R_ARM_NONE
:
4685 case elfcpp::R_ARM_ABS32
:
4686 case elfcpp::R_ARM_ABS32_NOI
:
4687 // If building a shared library (or a position-independent
4688 // executable), we need to create a dynamic relocation for
4689 // this location. The relocation applied at link time will
4690 // apply the link-time value, so we flag the location with
4691 // an R_ARM_RELATIVE relocation so the dynamic loader can
4692 // relocate it easily.
4693 if (parameters
->options().output_is_position_independent())
4695 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4696 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4697 // If we are to add more other reloc types than R_ARM_ABS32,
4698 // we need to add check_non_pic(object, r_type) here.
4699 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4700 output_section
, data_shndx
,
4701 reloc
.get_r_offset());
4705 case elfcpp::R_ARM_REL32
:
4706 case elfcpp::R_ARM_THM_CALL
:
4707 case elfcpp::R_ARM_CALL
:
4708 case elfcpp::R_ARM_PREL31
:
4709 case elfcpp::R_ARM_JUMP24
:
4710 case elfcpp::R_ARM_PLT32
:
4711 case elfcpp::R_ARM_THM_ABS5
:
4712 case elfcpp::R_ARM_ABS8
:
4713 case elfcpp::R_ARM_ABS12
:
4714 case elfcpp::R_ARM_ABS16
:
4715 case elfcpp::R_ARM_BASE_ABS
:
4716 case elfcpp::R_ARM_MOVW_ABS_NC
:
4717 case elfcpp::R_ARM_MOVT_ABS
:
4718 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4719 case elfcpp::R_ARM_THM_MOVT_ABS
:
4720 case elfcpp::R_ARM_MOVW_PREL_NC
:
4721 case elfcpp::R_ARM_MOVT_PREL
:
4722 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4723 case elfcpp::R_ARM_THM_MOVT_PREL
:
4726 case elfcpp::R_ARM_GOTOFF32
:
4727 // We need a GOT section:
4728 target
->got_section(symtab
, layout
);
4731 case elfcpp::R_ARM_BASE_PREL
:
4732 // FIXME: What about this?
4735 case elfcpp::R_ARM_GOT_BREL
:
4736 case elfcpp::R_ARM_GOT_PREL
:
4738 // The symbol requires a GOT entry.
4739 Output_data_got
<32, big_endian
>* got
=
4740 target
->got_section(symtab
, layout
);
4741 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4742 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4744 // If we are generating a shared object, we need to add a
4745 // dynamic RELATIVE relocation for this symbol's GOT entry.
4746 if (parameters
->options().output_is_position_independent())
4748 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4749 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4750 rel_dyn
->add_local_relative(
4751 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4752 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4758 case elfcpp::R_ARM_TARGET1
:
4759 // This should have been mapped to another type already.
4761 case elfcpp::R_ARM_COPY
:
4762 case elfcpp::R_ARM_GLOB_DAT
:
4763 case elfcpp::R_ARM_JUMP_SLOT
:
4764 case elfcpp::R_ARM_RELATIVE
:
4765 // These are relocations which should only be seen by the
4766 // dynamic linker, and should never be seen here.
4767 gold_error(_("%s: unexpected reloc %u in object file"),
4768 object
->name().c_str(), r_type
);
4772 unsupported_reloc_local(object
, r_type
);
4777 // Report an unsupported relocation against a global symbol.
4779 template<bool big_endian
>
4781 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4782 Sized_relobj
<32, big_endian
>* object
,
4783 unsigned int r_type
,
4786 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4787 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4790 // Scan a relocation for a global symbol.
4791 // FIXME: This only handles a subset of relocation types used by Android
4792 // on ARM v5te devices.
4794 template<bool big_endian
>
4796 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4799 Sized_relobj
<32, big_endian
>* object
,
4800 unsigned int data_shndx
,
4801 Output_section
* output_section
,
4802 const elfcpp::Rel
<32, big_endian
>& reloc
,
4803 unsigned int r_type
,
4806 r_type
= get_real_reloc_type(r_type
);
4809 case elfcpp::R_ARM_NONE
:
4812 case elfcpp::R_ARM_ABS32
:
4813 case elfcpp::R_ARM_ABS32_NOI
:
4815 // Make a dynamic relocation if necessary.
4816 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4818 if (target
->may_need_copy_reloc(gsym
))
4820 target
->copy_reloc(symtab
, layout
, object
,
4821 data_shndx
, output_section
, gsym
, reloc
);
4823 else if (gsym
->can_use_relative_reloc(false))
4825 // If we are to add more other reloc types than R_ARM_ABS32,
4826 // we need to add check_non_pic(object, r_type) here.
4827 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4828 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4829 output_section
, object
,
4830 data_shndx
, reloc
.get_r_offset());
4834 // If we are to add more other reloc types than R_ARM_ABS32,
4835 // we need to add check_non_pic(object, r_type) here.
4836 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4837 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4838 data_shndx
, reloc
.get_r_offset());
4844 case elfcpp::R_ARM_MOVW_ABS_NC
:
4845 case elfcpp::R_ARM_MOVT_ABS
:
4846 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4847 case elfcpp::R_ARM_THM_MOVT_ABS
:
4848 case elfcpp::R_ARM_MOVW_PREL_NC
:
4849 case elfcpp::R_ARM_MOVT_PREL
:
4850 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4851 case elfcpp::R_ARM_THM_MOVT_PREL
:
4854 case elfcpp::R_ARM_THM_ABS5
:
4855 case elfcpp::R_ARM_ABS8
:
4856 case elfcpp::R_ARM_ABS12
:
4857 case elfcpp::R_ARM_ABS16
:
4858 case elfcpp::R_ARM_BASE_ABS
:
4860 // No dynamic relocs of this kinds.
4861 // Report the error in case of PIC.
4862 int flags
= Symbol::NON_PIC_REF
;
4863 if (gsym
->type() == elfcpp::STT_FUNC
4864 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4865 flags
|= Symbol::FUNCTION_CALL
;
4866 if (gsym
->needs_dynamic_reloc(flags
))
4867 check_non_pic(object
, r_type
);
4871 case elfcpp::R_ARM_REL32
:
4872 case elfcpp::R_ARM_PREL31
:
4874 // Make a dynamic relocation if necessary.
4875 int flags
= Symbol::NON_PIC_REF
;
4876 if (gsym
->needs_dynamic_reloc(flags
))
4878 if (target
->may_need_copy_reloc(gsym
))
4880 target
->copy_reloc(symtab
, layout
, object
,
4881 data_shndx
, output_section
, gsym
, reloc
);
4885 check_non_pic(object
, r_type
);
4886 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4887 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4888 data_shndx
, reloc
.get_r_offset());
4894 case elfcpp::R_ARM_JUMP24
:
4895 case elfcpp::R_ARM_THM_JUMP24
:
4896 case elfcpp::R_ARM_CALL
:
4897 case elfcpp::R_ARM_THM_CALL
:
4899 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4900 target
->make_plt_entry(symtab
, layout
, gsym
);
4903 // Check to see if this is a function that would need a PLT
4904 // but does not get one because the function symbol is untyped.
4905 // This happens in assembly code missing a proper .type directive.
4906 if ((!gsym
->is_undefined() || parameters
->options().shared())
4907 && !parameters
->doing_static_link()
4908 && gsym
->type() == elfcpp::STT_NOTYPE
4909 && (gsym
->is_from_dynobj()
4910 || gsym
->is_undefined()
4911 || gsym
->is_preemptible()))
4912 gold_error(_("%s is not a function."),
4913 gsym
->demangled_name().c_str());
4917 case elfcpp::R_ARM_PLT32
:
4918 // If the symbol is fully resolved, this is just a relative
4919 // local reloc. Otherwise we need a PLT entry.
4920 if (gsym
->final_value_is_known())
4922 // If building a shared library, we can also skip the PLT entry
4923 // if the symbol is defined in the output file and is protected
4925 if (gsym
->is_defined()
4926 && !gsym
->is_from_dynobj()
4927 && !gsym
->is_preemptible())
4929 target
->make_plt_entry(symtab
, layout
, gsym
);
4932 case elfcpp::R_ARM_GOTOFF32
:
4933 // We need a GOT section.
4934 target
->got_section(symtab
, layout
);
4937 case elfcpp::R_ARM_BASE_PREL
:
4938 // FIXME: What about this?
4941 case elfcpp::R_ARM_GOT_BREL
:
4942 case elfcpp::R_ARM_GOT_PREL
:
4944 // The symbol requires a GOT entry.
4945 Output_data_got
<32, big_endian
>* got
=
4946 target
->got_section(symtab
, layout
);
4947 if (gsym
->final_value_is_known())
4948 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4951 // If this symbol is not fully resolved, we need to add a
4952 // GOT entry with a dynamic relocation.
4953 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4954 if (gsym
->is_from_dynobj()
4955 || gsym
->is_undefined()
4956 || gsym
->is_preemptible())
4957 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4958 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4961 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4962 rel_dyn
->add_global_relative(
4963 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4964 gsym
->got_offset(GOT_TYPE_STANDARD
));
4970 case elfcpp::R_ARM_TARGET1
:
4971 // This should have been mapped to another type already.
4973 case elfcpp::R_ARM_COPY
:
4974 case elfcpp::R_ARM_GLOB_DAT
:
4975 case elfcpp::R_ARM_JUMP_SLOT
:
4976 case elfcpp::R_ARM_RELATIVE
:
4977 // These are relocations which should only be seen by the
4978 // dynamic linker, and should never be seen here.
4979 gold_error(_("%s: unexpected reloc %u in object file"),
4980 object
->name().c_str(), r_type
);
4984 unsupported_reloc_global(object
, r_type
, gsym
);
4989 // Process relocations for gc.
4991 template<bool big_endian
>
4993 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4995 Sized_relobj
<32, big_endian
>* object
,
4996 unsigned int data_shndx
,
4998 const unsigned char* prelocs
,
5000 Output_section
* output_section
,
5001 bool needs_special_offset_handling
,
5002 size_t local_symbol_count
,
5003 const unsigned char* plocal_symbols
)
5005 typedef Target_arm
<big_endian
> Arm
;
5006 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5008 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
5017 needs_special_offset_handling
,
5022 // Scan relocations for a section.
5024 template<bool big_endian
>
5026 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
5028 Sized_relobj
<32, big_endian
>* object
,
5029 unsigned int data_shndx
,
5030 unsigned int sh_type
,
5031 const unsigned char* prelocs
,
5033 Output_section
* output_section
,
5034 bool needs_special_offset_handling
,
5035 size_t local_symbol_count
,
5036 const unsigned char* plocal_symbols
)
5038 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5039 if (sh_type
== elfcpp::SHT_RELA
)
5041 gold_error(_("%s: unsupported RELA reloc section"),
5042 object
->name().c_str());
5046 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
5055 needs_special_offset_handling
,
5060 // Finalize the sections.
5062 template<bool big_endian
>
5064 Target_arm
<big_endian
>::do_finalize_sections(
5066 const Input_objects
* input_objects
,
5067 Symbol_table
* symtab
)
5069 // Merge processor-specific flags.
5070 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
5071 p
!= input_objects
->relobj_end();
5074 Arm_relobj
<big_endian
>* arm_relobj
=
5075 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
5076 this->merge_processor_specific_flags(
5078 arm_relobj
->processor_specific_flags());
5079 this->merge_object_attributes(arm_relobj
->name().c_str(),
5080 arm_relobj
->attributes_section_data());
5084 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
5085 p
!= input_objects
->dynobj_end();
5088 Arm_dynobj
<big_endian
>* arm_dynobj
=
5089 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
5090 this->merge_processor_specific_flags(
5092 arm_dynobj
->processor_specific_flags());
5093 this->merge_object_attributes(arm_dynobj
->name().c_str(),
5094 arm_dynobj
->attributes_section_data());
5098 Object_attribute
* attr
=
5099 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
5100 if (attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
5101 this->set_may_use_blx(true);
5103 // Fill in some more dynamic tags.
5104 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5106 : this->plt_
->rel_plt());
5107 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
5108 this->rel_dyn_
, true);
5110 // Emit any relocs we saved in an attempt to avoid generating COPY
5112 if (this->copy_relocs_
.any_saved_relocs())
5113 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
5115 // Handle the .ARM.exidx section.
5116 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
5117 if (exidx_section
!= NULL
5118 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
5119 && !parameters
->options().relocatable())
5121 // Create __exidx_start and __exdix_end symbols.
5122 symtab
->define_in_output_data("__exidx_start", NULL
,
5123 Symbol_table::PREDEFINED
,
5124 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5125 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5127 symtab
->define_in_output_data("__exidx_end", NULL
,
5128 Symbol_table::PREDEFINED
,
5129 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5130 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5133 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5134 // the .ARM.exidx section.
5135 if (!layout
->script_options()->saw_phdrs_clause())
5137 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
5139 Output_segment
* exidx_segment
=
5140 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
5141 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
5146 // Create an .ARM.attributes section if there is not one already.
5147 Output_attributes_section_data
* attributes_section
=
5148 new Output_attributes_section_data(*this->attributes_section_data_
);
5149 layout
->add_output_section_data(".ARM.attributes",
5150 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
5151 attributes_section
, false, false, false,
5155 // Return whether a direct absolute static relocation needs to be applied.
5156 // In cases where Scan::local() or Scan::global() has created
5157 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5158 // of the relocation is carried in the data, and we must not
5159 // apply the static relocation.
5161 template<bool big_endian
>
5163 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
5164 const Sized_symbol
<32>* gsym
,
5167 Output_section
* output_section
)
5169 // If the output section is not allocated, then we didn't call
5170 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5172 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
5175 // For local symbols, we will have created a non-RELATIVE dynamic
5176 // relocation only if (a) the output is position independent,
5177 // (b) the relocation is absolute (not pc- or segment-relative), and
5178 // (c) the relocation is not 32 bits wide.
5180 return !(parameters
->options().output_is_position_independent()
5181 && (ref_flags
& Symbol::ABSOLUTE_REF
)
5184 // For global symbols, we use the same helper routines used in the
5185 // scan pass. If we did not create a dynamic relocation, or if we
5186 // created a RELATIVE dynamic relocation, we should apply the static
5188 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
5189 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
5190 && gsym
->can_use_relative_reloc(ref_flags
5191 & Symbol::FUNCTION_CALL
);
5192 return !has_dyn
|| is_rel
;
5195 // Perform a relocation.
5197 template<bool big_endian
>
5199 Target_arm
<big_endian
>::Relocate::relocate(
5200 const Relocate_info
<32, big_endian
>* relinfo
,
5202 Output_section
*output_section
,
5204 const elfcpp::Rel
<32, big_endian
>& rel
,
5205 unsigned int r_type
,
5206 const Sized_symbol
<32>* gsym
,
5207 const Symbol_value
<32>* psymval
,
5208 unsigned char* view
,
5209 Arm_address address
,
5210 section_size_type
/* view_size */ )
5212 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
5214 r_type
= get_real_reloc_type(r_type
);
5216 const Arm_relobj
<big_endian
>* object
=
5217 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5219 // If the final branch target of a relocation is THUMB instruction, this
5220 // is 1. Otherwise it is 0.
5221 Arm_address thumb_bit
= 0;
5222 Symbol_value
<32> symval
;
5223 bool is_weakly_undefined_without_plt
= false;
5224 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
5228 // This is a global symbol. Determine if we use PLT and if the
5229 // final target is THUMB.
5230 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
5232 // This uses a PLT, change the symbol value.
5233 symval
.set_output_value(target
->plt_section()->address()
5234 + gsym
->plt_offset());
5237 else if (gsym
->is_weak_undefined())
5239 // This is a weakly undefined symbol and we do not use PLT
5240 // for this relocation. A branch targeting this symbol will
5241 // be converted into an NOP.
5242 is_weakly_undefined_without_plt
= true;
5246 // Set thumb bit if symbol:
5247 // -Has type STT_ARM_TFUNC or
5248 // -Has type STT_FUNC, is defined and with LSB in value set.
5250 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5251 || (gsym
->type() == elfcpp::STT_FUNC
5252 && !gsym
->is_undefined()
5253 && ((psymval
->value(object
, 0) & 1) != 0)))
5260 // This is a local symbol. Determine if the final target is THUMB.
5261 // We saved this information when all the local symbols were read.
5262 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
5263 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5264 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
5269 // This is a fake relocation synthesized for a stub. It does not have
5270 // a real symbol. We just look at the LSB of the symbol value to
5271 // determine if the target is THUMB or not.
5272 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
5275 // Strip LSB if this points to a THUMB target.
5277 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5278 && ((psymval
->value(object
, 0) & 1) != 0))
5280 Arm_address stripped_value
=
5281 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
5282 symval
.set_output_value(stripped_value
);
5286 // Get the GOT offset if needed.
5287 // The GOT pointer points to the end of the GOT section.
5288 // We need to subtract the size of the GOT section to get
5289 // the actual offset to use in the relocation.
5290 bool have_got_offset
= false;
5291 unsigned int got_offset
= 0;
5294 case elfcpp::R_ARM_GOT_BREL
:
5295 case elfcpp::R_ARM_GOT_PREL
:
5298 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5299 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
5300 - target
->got_size());
5304 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5305 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5306 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
5307 - target
->got_size());
5309 have_got_offset
= true;
5316 // To look up relocation stubs, we need to pass the symbol table index of
5318 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5320 typename
Arm_relocate_functions::Status reloc_status
=
5321 Arm_relocate_functions::STATUS_OKAY
;
5324 case elfcpp::R_ARM_NONE
:
5327 case elfcpp::R_ARM_ABS8
:
5328 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5330 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
5333 case elfcpp::R_ARM_ABS12
:
5334 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5336 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
5339 case elfcpp::R_ARM_ABS16
:
5340 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5342 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
5345 case elfcpp::R_ARM_ABS32
:
5346 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5348 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5352 case elfcpp::R_ARM_ABS32_NOI
:
5353 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5355 // No thumb bit for this relocation: (S + A)
5356 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5360 case elfcpp::R_ARM_MOVW_ABS_NC
:
5361 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5363 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
5367 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5368 "a shared object; recompile with -fPIC"));
5371 case elfcpp::R_ARM_MOVT_ABS
:
5372 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5374 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
5376 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5377 "a shared object; recompile with -fPIC"));
5380 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5381 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5383 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
5387 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5388 "making a shared object; recompile with -fPIC"));
5391 case elfcpp::R_ARM_THM_MOVT_ABS
:
5392 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5394 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
5397 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5398 "making a shared object; recompile with -fPIC"));
5401 case elfcpp::R_ARM_MOVW_PREL_NC
:
5402 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
5407 case elfcpp::R_ARM_MOVT_PREL
:
5408 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
5412 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5413 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
5418 case elfcpp::R_ARM_THM_MOVT_PREL
:
5419 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
5423 case elfcpp::R_ARM_REL32
:
5424 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5425 address
, thumb_bit
);
5428 case elfcpp::R_ARM_THM_ABS5
:
5429 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5431 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
5434 case elfcpp::R_ARM_THM_CALL
:
5436 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
5437 psymval
, address
, thumb_bit
,
5438 is_weakly_undefined_without_plt
);
5441 case elfcpp::R_ARM_XPC25
:
5443 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
5444 psymval
, address
, thumb_bit
,
5445 is_weakly_undefined_without_plt
);
5448 case elfcpp::R_ARM_THM_XPC22
:
5450 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
5451 psymval
, address
, thumb_bit
,
5452 is_weakly_undefined_without_plt
);
5455 case elfcpp::R_ARM_GOTOFF32
:
5457 Arm_address got_origin
;
5458 got_origin
= target
->got_plt_section()->address();
5459 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5460 got_origin
, thumb_bit
);
5464 case elfcpp::R_ARM_BASE_PREL
:
5467 // Get the addressing origin of the output segment defining the
5468 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5469 gold_assert(gsym
!= NULL
);
5470 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5471 origin
= gsym
->output_segment()->vaddr();
5472 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5473 origin
= gsym
->output_data()->address();
5476 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5477 _("cannot find origin of R_ARM_BASE_PREL"));
5480 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
5484 case elfcpp::R_ARM_BASE_ABS
:
5486 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5491 // Get the addressing origin of the output segment defining
5492 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5494 // R_ARM_BASE_ABS with the NULL symbol will give the
5495 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5496 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5497 origin
= target
->got_plt_section()->address();
5498 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5499 origin
= gsym
->output_segment()->vaddr();
5500 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5501 origin
= gsym
->output_data()->address();
5504 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5505 _("cannot find origin of R_ARM_BASE_ABS"));
5509 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5513 case elfcpp::R_ARM_GOT_BREL
:
5514 gold_assert(have_got_offset
);
5515 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5518 case elfcpp::R_ARM_GOT_PREL
:
5519 gold_assert(have_got_offset
);
5520 // Get the address origin for GOT PLT, which is allocated right
5521 // after the GOT section, to calculate an absolute address of
5522 // the symbol GOT entry (got_origin + got_offset).
5523 Arm_address got_origin
;
5524 got_origin
= target
->got_plt_section()->address();
5525 reloc_status
= Arm_relocate_functions::got_prel(view
,
5526 got_origin
+ got_offset
,
5530 case elfcpp::R_ARM_PLT32
:
5531 gold_assert(gsym
== NULL
5532 || gsym
->has_plt_offset()
5533 || gsym
->final_value_is_known()
5534 || (gsym
->is_defined()
5535 && !gsym
->is_from_dynobj()
5536 && !gsym
->is_preemptible()));
5538 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5539 psymval
, address
, thumb_bit
,
5540 is_weakly_undefined_without_plt
);
5543 case elfcpp::R_ARM_CALL
:
5545 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5546 psymval
, address
, thumb_bit
,
5547 is_weakly_undefined_without_plt
);
5550 case elfcpp::R_ARM_JUMP24
:
5552 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5553 psymval
, address
, thumb_bit
,
5554 is_weakly_undefined_without_plt
);
5557 case elfcpp::R_ARM_THM_JUMP24
:
5559 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5560 psymval
, address
, thumb_bit
,
5561 is_weakly_undefined_without_plt
);
5564 case elfcpp::R_ARM_PREL31
:
5565 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5566 address
, thumb_bit
);
5569 case elfcpp::R_ARM_TARGET1
:
5570 // This should have been mapped to another type already.
5572 case elfcpp::R_ARM_COPY
:
5573 case elfcpp::R_ARM_GLOB_DAT
:
5574 case elfcpp::R_ARM_JUMP_SLOT
:
5575 case elfcpp::R_ARM_RELATIVE
:
5576 // These are relocations which should only be seen by the
5577 // dynamic linker, and should never be seen here.
5578 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5579 _("unexpected reloc %u in object file"),
5584 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5585 _("unsupported reloc %u"),
5590 // Report any errors.
5591 switch (reloc_status
)
5593 case Arm_relocate_functions::STATUS_OKAY
:
5595 case Arm_relocate_functions::STATUS_OVERFLOW
:
5596 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5597 _("relocation overflow in relocation %u"),
5600 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5601 gold_error_at_location(
5605 _("unexpected opcode while processing relocation %u"),
5615 // Relocate section data.
5617 template<bool big_endian
>
5619 Target_arm
<big_endian
>::relocate_section(
5620 const Relocate_info
<32, big_endian
>* relinfo
,
5621 unsigned int sh_type
,
5622 const unsigned char* prelocs
,
5624 Output_section
* output_section
,
5625 bool needs_special_offset_handling
,
5626 unsigned char* view
,
5627 Arm_address address
,
5628 section_size_type view_size
,
5629 const Reloc_symbol_changes
* reloc_symbol_changes
)
5631 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5632 gold_assert(sh_type
== elfcpp::SHT_REL
);
5634 Arm_input_section
<big_endian
>* arm_input_section
=
5635 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5637 // This is an ARM input section and the view covers the whole output
5639 if (arm_input_section
!= NULL
)
5641 gold_assert(needs_special_offset_handling
);
5642 Arm_address section_address
= arm_input_section
->address();
5643 section_size_type section_size
= arm_input_section
->data_size();
5645 gold_assert((arm_input_section
->address() >= address
)
5646 && ((arm_input_section
->address()
5647 + arm_input_section
->data_size())
5648 <= (address
+ view_size
)));
5650 off_t offset
= section_address
- address
;
5653 view_size
= section_size
;
5656 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5663 needs_special_offset_handling
,
5667 reloc_symbol_changes
);
5670 // Return the size of a relocation while scanning during a relocatable
5673 template<bool big_endian
>
5675 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5676 unsigned int r_type
,
5679 r_type
= get_real_reloc_type(r_type
);
5682 case elfcpp::R_ARM_NONE
:
5685 case elfcpp::R_ARM_ABS8
:
5688 case elfcpp::R_ARM_ABS16
:
5689 case elfcpp::R_ARM_THM_ABS5
:
5692 case elfcpp::R_ARM_ABS32
:
5693 case elfcpp::R_ARM_ABS32_NOI
:
5694 case elfcpp::R_ARM_ABS12
:
5695 case elfcpp::R_ARM_BASE_ABS
:
5696 case elfcpp::R_ARM_REL32
:
5697 case elfcpp::R_ARM_THM_CALL
:
5698 case elfcpp::R_ARM_GOTOFF32
:
5699 case elfcpp::R_ARM_BASE_PREL
:
5700 case elfcpp::R_ARM_GOT_BREL
:
5701 case elfcpp::R_ARM_GOT_PREL
:
5702 case elfcpp::R_ARM_PLT32
:
5703 case elfcpp::R_ARM_CALL
:
5704 case elfcpp::R_ARM_JUMP24
:
5705 case elfcpp::R_ARM_PREL31
:
5706 case elfcpp::R_ARM_MOVW_ABS_NC
:
5707 case elfcpp::R_ARM_MOVT_ABS
:
5708 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5709 case elfcpp::R_ARM_THM_MOVT_ABS
:
5710 case elfcpp::R_ARM_MOVW_PREL_NC
:
5711 case elfcpp::R_ARM_MOVT_PREL
:
5712 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5713 case elfcpp::R_ARM_THM_MOVT_PREL
:
5716 case elfcpp::R_ARM_TARGET1
:
5717 // This should have been mapped to another type already.
5719 case elfcpp::R_ARM_COPY
:
5720 case elfcpp::R_ARM_GLOB_DAT
:
5721 case elfcpp::R_ARM_JUMP_SLOT
:
5722 case elfcpp::R_ARM_RELATIVE
:
5723 // These are relocations which should only be seen by the
5724 // dynamic linker, and should never be seen here.
5725 gold_error(_("%s: unexpected reloc %u in object file"),
5726 object
->name().c_str(), r_type
);
5730 object
->error(_("unsupported reloc %u in object file"), r_type
);
5735 // Scan the relocs during a relocatable link.
5737 template<bool big_endian
>
5739 Target_arm
<big_endian
>::scan_relocatable_relocs(
5740 Symbol_table
* symtab
,
5742 Sized_relobj
<32, big_endian
>* object
,
5743 unsigned int data_shndx
,
5744 unsigned int sh_type
,
5745 const unsigned char* prelocs
,
5747 Output_section
* output_section
,
5748 bool needs_special_offset_handling
,
5749 size_t local_symbol_count
,
5750 const unsigned char* plocal_symbols
,
5751 Relocatable_relocs
* rr
)
5753 gold_assert(sh_type
== elfcpp::SHT_REL
);
5755 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5756 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5758 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5759 Scan_relocatable_relocs
>(
5767 needs_special_offset_handling
,
5773 // Relocate a section during a relocatable link.
5775 template<bool big_endian
>
5777 Target_arm
<big_endian
>::relocate_for_relocatable(
5778 const Relocate_info
<32, big_endian
>* relinfo
,
5779 unsigned int sh_type
,
5780 const unsigned char* prelocs
,
5782 Output_section
* output_section
,
5783 off_t offset_in_output_section
,
5784 const Relocatable_relocs
* rr
,
5785 unsigned char* view
,
5786 Arm_address view_address
,
5787 section_size_type view_size
,
5788 unsigned char* reloc_view
,
5789 section_size_type reloc_view_size
)
5791 gold_assert(sh_type
== elfcpp::SHT_REL
);
5793 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5798 offset_in_output_section
,
5807 // Return the value to use for a dynamic symbol which requires special
5808 // treatment. This is how we support equality comparisons of function
5809 // pointers across shared library boundaries, as described in the
5810 // processor specific ABI supplement.
5812 template<bool big_endian
>
5814 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5816 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5817 return this->plt_section()->address() + gsym
->plt_offset();
5820 // Map platform-specific relocs to real relocs
5822 template<bool big_endian
>
5824 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5828 case elfcpp::R_ARM_TARGET1
:
5829 // This is either R_ARM_ABS32 or R_ARM_REL32;
5830 return elfcpp::R_ARM_ABS32
;
5832 case elfcpp::R_ARM_TARGET2
:
5833 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5834 return elfcpp::R_ARM_GOT_PREL
;
5841 // Whether if two EABI versions V1 and V2 are compatible.
5843 template<bool big_endian
>
5845 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5846 elfcpp::Elf_Word v1
,
5847 elfcpp::Elf_Word v2
)
5849 // v4 and v5 are the same spec before and after it was released,
5850 // so allow mixing them.
5851 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5852 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5858 // Combine FLAGS from an input object called NAME and the processor-specific
5859 // flags in the ELF header of the output. Much of this is adapted from the
5860 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5861 // in bfd/elf32-arm.c.
5863 template<bool big_endian
>
5865 Target_arm
<big_endian
>::merge_processor_specific_flags(
5866 const std::string
& name
,
5867 elfcpp::Elf_Word flags
)
5869 if (this->are_processor_specific_flags_set())
5871 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5873 // Nothing to merge if flags equal to those in output.
5874 if (flags
== out_flags
)
5877 // Complain about various flag mismatches.
5878 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5879 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5880 if (!this->are_eabi_versions_compatible(version1
, version2
))
5881 gold_error(_("Source object %s has EABI version %d but output has "
5882 "EABI version %d."),
5884 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5885 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5889 // If the input is the default architecture and had the default
5890 // flags then do not bother setting the flags for the output
5891 // architecture, instead allow future merges to do this. If no
5892 // future merges ever set these flags then they will retain their
5893 // uninitialised values, which surprise surprise, correspond
5894 // to the default values.
5898 // This is the first time, just copy the flags.
5899 // We only copy the EABI version for now.
5900 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5904 // Adjust ELF file header.
5905 template<bool big_endian
>
5907 Target_arm
<big_endian
>::do_adjust_elf_header(
5908 unsigned char* view
,
5911 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5913 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5914 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5915 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5917 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5918 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5919 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5921 e_ident
[elfcpp::EI_OSABI
] = 0;
5922 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5924 // FIXME: Do EF_ARM_BE8 adjustment.
5926 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5927 oehdr
.put_e_ident(e_ident
);
5930 // do_make_elf_object to override the same function in the base class.
5931 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5932 // to store ARM specific information. Hence we need to have our own
5933 // ELF object creation.
5935 template<bool big_endian
>
5937 Target_arm
<big_endian
>::do_make_elf_object(
5938 const std::string
& name
,
5939 Input_file
* input_file
,
5940 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5942 int et
= ehdr
.get_e_type();
5943 if (et
== elfcpp::ET_REL
)
5945 Arm_relobj
<big_endian
>* obj
=
5946 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5950 else if (et
== elfcpp::ET_DYN
)
5952 Sized_dynobj
<32, big_endian
>* obj
=
5953 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5959 gold_error(_("%s: unsupported ELF file type %d"),
5965 // Read the architecture from the Tag_also_compatible_with attribute, if any.
5966 // Returns -1 if no architecture could be read.
5967 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5969 template<bool big_endian
>
5971 Target_arm
<big_endian
>::get_secondary_compatible_arch(
5972 const Attributes_section_data
* pasd
)
5974 const Object_attribute
*known_attributes
=
5975 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
5977 // Note: the tag and its argument below are uleb128 values, though
5978 // currently-defined values fit in one byte for each.
5979 const std::string
& sv
=
5980 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
5982 && sv
.data()[0] == elfcpp::Tag_CPU_arch
5983 && (sv
.data()[1] & 128) != 128)
5984 return sv
.data()[1];
5986 // This tag is "safely ignorable", so don't complain if it looks funny.
5990 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5991 // The tag is removed if ARCH is -1.
5992 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5994 template<bool big_endian
>
5996 Target_arm
<big_endian
>::set_secondary_compatible_arch(
5997 Attributes_section_data
* pasd
,
6000 Object_attribute
*known_attributes
=
6001 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6005 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
6009 // Note: the tag and its argument below are uleb128 values, though
6010 // currently-defined values fit in one byte for each.
6012 sv
[0] = elfcpp::Tag_CPU_arch
;
6013 gold_assert(arch
!= 0);
6017 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
6020 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6022 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6024 template<bool big_endian
>
6026 Target_arm
<big_endian
>::tag_cpu_arch_combine(
6029 int* secondary_compat_out
,
6031 int secondary_compat
)
6033 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6034 static const int v6t2
[] =
6046 static const int v6k
[] =
6059 static const int v7
[] =
6073 static const int v6_m
[] =
6088 static const int v6s_m
[] =
6104 static const int v7e_m
[] =
6121 static const int v4t_plus_v6_m
[] =
6137 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
6139 static const int *comb
[] =
6147 // Pseudo-architecture.
6151 // Check we've not got a higher architecture than we know about.
6153 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
6155 gold_error(_("%s: unknown CPU architecture"), name
);
6159 // Override old tag if we have a Tag_also_compatible_with on the output.
6161 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
6162 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
6163 oldtag
= T(V4T_PLUS_V6_M
);
6165 // And override the new tag if we have a Tag_also_compatible_with on the
6168 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
6169 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
6170 newtag
= T(V4T_PLUS_V6_M
);
6172 // Architectures before V6KZ add features monotonically.
6173 int tagh
= std::max(oldtag
, newtag
);
6174 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
6177 int tagl
= std::min(oldtag
, newtag
);
6178 int result
= comb
[tagh
- T(V6T2
)][tagl
];
6180 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6181 // as the canonical version.
6182 if (result
== T(V4T_PLUS_V6_M
))
6185 *secondary_compat_out
= T(V6_M
);
6188 *secondary_compat_out
= -1;
6192 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6193 name
, oldtag
, newtag
);
6201 // Helper to print AEABI enum tag value.
6203 template<bool big_endian
>
6205 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
6207 static const char *aeabi_enum_names
[] =
6208 { "", "variable-size", "32-bit", "" };
6209 const size_t aeabi_enum_names_size
=
6210 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
6212 if (value
< aeabi_enum_names_size
)
6213 return std::string(aeabi_enum_names
[value
]);
6217 sprintf(buffer
, "<unknown value %u>", value
);
6218 return std::string(buffer
);
6222 // Return the string value to store in TAG_CPU_name.
6224 template<bool big_endian
>
6226 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
6228 static const char *name_table
[] = {
6229 // These aren't real CPU names, but we can't guess
6230 // that from the architecture version alone.
6246 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
6248 if (value
< name_table_size
)
6249 return std::string(name_table
[value
]);
6253 sprintf(buffer
, "<unknown CPU value %u>", value
);
6254 return std::string(buffer
);
6258 // Merge object attributes from input file called NAME with those of the
6259 // output. The input object attributes are in the object pointed by PASD.
6261 template<bool big_endian
>
6263 Target_arm
<big_endian
>::merge_object_attributes(
6265 const Attributes_section_data
* pasd
)
6267 // Return if there is no attributes section data.
6271 // If output has no object attributes, just copy.
6272 if (this->attributes_section_data_
== NULL
)
6274 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
6278 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
6279 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
6280 Object_attribute
* out_attr
=
6281 this->attributes_section_data_
->known_attributes(vendor
);
6283 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6284 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
6285 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
6287 // Ignore mismatches if the object doesn't use floating point. */
6288 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
6289 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
6290 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
6291 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
6292 gold_error(_("%s uses VFP register arguments, output does not"),
6296 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
6298 // Merge this attribute with existing attributes.
6301 case elfcpp::Tag_CPU_raw_name
:
6302 case elfcpp::Tag_CPU_name
:
6303 // These are merged after Tag_CPU_arch.
6306 case elfcpp::Tag_ABI_optimization_goals
:
6307 case elfcpp::Tag_ABI_FP_optimization_goals
:
6308 // Use the first value seen.
6311 case elfcpp::Tag_CPU_arch
:
6313 unsigned int saved_out_attr
= out_attr
->int_value();
6314 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6315 int secondary_compat
=
6316 this->get_secondary_compatible_arch(pasd
);
6317 int secondary_compat_out
=
6318 this->get_secondary_compatible_arch(
6319 this->attributes_section_data_
);
6320 out_attr
[i
].set_int_value(
6321 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
6322 &secondary_compat_out
,
6323 in_attr
[i
].int_value(),
6325 this->set_secondary_compatible_arch(this->attributes_section_data_
,
6326 secondary_compat_out
);
6328 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6329 if (out_attr
[i
].int_value() == saved_out_attr
)
6330 ; // Leave the names alone.
6331 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
6333 // The output architecture has been changed to match the
6334 // input architecture. Use the input names.
6335 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
6336 in_attr
[elfcpp::Tag_CPU_name
].string_value());
6337 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
6338 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
6342 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
6343 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
6346 // If we still don't have a value for Tag_CPU_name,
6347 // make one up now. Tag_CPU_raw_name remains blank.
6348 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
6350 const std::string cpu_name
=
6351 this->tag_cpu_name_value(out_attr
[i
].int_value());
6352 // FIXME: If we see an unknown CPU, this will be set
6353 // to "<unknown CPU n>", where n is the attribute value.
6354 // This is different from BFD, which leaves the name alone.
6355 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
6360 case elfcpp::Tag_ARM_ISA_use
:
6361 case elfcpp::Tag_THUMB_ISA_use
:
6362 case elfcpp::Tag_WMMX_arch
:
6363 case elfcpp::Tag_Advanced_SIMD_arch
:
6364 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6365 case elfcpp::Tag_ABI_FP_rounding
:
6366 case elfcpp::Tag_ABI_FP_exceptions
:
6367 case elfcpp::Tag_ABI_FP_user_exceptions
:
6368 case elfcpp::Tag_ABI_FP_number_model
:
6369 case elfcpp::Tag_VFP_HP_extension
:
6370 case elfcpp::Tag_CPU_unaligned_access
:
6371 case elfcpp::Tag_T2EE_use
:
6372 case elfcpp::Tag_Virtualization_use
:
6373 case elfcpp::Tag_MPextension_use
:
6374 // Use the largest value specified.
6375 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6376 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6379 case elfcpp::Tag_ABI_align8_preserved
:
6380 case elfcpp::Tag_ABI_PCS_RO_data
:
6381 // Use the smallest value specified.
6382 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6383 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6386 case elfcpp::Tag_ABI_align8_needed
:
6387 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
6388 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
6389 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
6392 // This error message should be enabled once all non-conformant
6393 // binaries in the toolchain have had the attributes set
6395 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6399 case elfcpp::Tag_ABI_FP_denormal
:
6400 case elfcpp::Tag_ABI_PCS_GOT_use
:
6402 // These tags have 0 = don't care, 1 = strong requirement,
6403 // 2 = weak requirement.
6404 static const int order_021
[3] = {0, 2, 1};
6406 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6407 // value if greater than 2 (for future-proofing).
6408 if ((in_attr
[i
].int_value() > 2
6409 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6410 || (in_attr
[i
].int_value() <= 2
6411 && out_attr
[i
].int_value() <= 2
6412 && (order_021
[in_attr
[i
].int_value()]
6413 > order_021
[out_attr
[i
].int_value()])))
6414 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6418 case elfcpp::Tag_CPU_arch_profile
:
6419 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
6421 // 0 will merge with anything.
6422 // 'A' and 'S' merge to 'A'.
6423 // 'R' and 'S' merge to 'R'.
6424 // 'M' and 'A|R|S' is an error.
6425 if (out_attr
[i
].int_value() == 0
6426 || (out_attr
[i
].int_value() == 'S'
6427 && (in_attr
[i
].int_value() == 'A'
6428 || in_attr
[i
].int_value() == 'R')))
6429 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6430 else if (in_attr
[i
].int_value() == 0
6431 || (in_attr
[i
].int_value() == 'S'
6432 && (out_attr
[i
].int_value() == 'A'
6433 || out_attr
[i
].int_value() == 'R')))
6438 (_("conflicting architecture profiles %c/%c"),
6439 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
6440 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
6444 case elfcpp::Tag_VFP_arch
:
6461 // Values greater than 6 aren't defined, so just pick the
6463 if (in_attr
[i
].int_value() > 6
6464 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6466 *out_attr
= *in_attr
;
6469 // The output uses the superset of input features
6470 // (ISA version) and registers.
6471 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
6472 vfp_versions
[out_attr
[i
].int_value()].ver
);
6473 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
6474 vfp_versions
[out_attr
[i
].int_value()].regs
);
6475 // This assumes all possible supersets are also a valid
6478 for (newval
= 6; newval
> 0; newval
--)
6480 if (regs
== vfp_versions
[newval
].regs
6481 && ver
== vfp_versions
[newval
].ver
)
6484 out_attr
[i
].set_int_value(newval
);
6487 case elfcpp::Tag_PCS_config
:
6488 if (out_attr
[i
].int_value() == 0)
6489 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6490 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6492 // It's sometimes ok to mix different configs, so this is only
6494 gold_warning(_("%s: conflicting platform configuration"), name
);
6497 case elfcpp::Tag_ABI_PCS_R9_use
:
6498 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
6499 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
6500 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
6502 gold_error(_("%s: conflicting use of R9"), name
);
6504 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
6505 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6507 case elfcpp::Tag_ABI_PCS_RW_data
:
6508 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6509 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6510 != elfcpp::AEABI_R9_SB
)
6511 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6512 != elfcpp::AEABI_R9_unused
))
6514 gold_error(_("%s: SB relative addressing conflicts with use "
6518 // Use the smallest value specified.
6519 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6520 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6522 case elfcpp::Tag_ABI_PCS_wchar_t
:
6523 // FIXME: Make it possible to turn off this warning.
6524 if (out_attr
[i
].int_value()
6525 && in_attr
[i
].int_value()
6526 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6528 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6529 "use %u-byte wchar_t; use of wchar_t values "
6530 "across objects may fail"),
6531 name
, in_attr
[i
].int_value(),
6532 out_attr
[i
].int_value());
6534 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
6535 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6537 case elfcpp::Tag_ABI_enum_size
:
6538 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
6540 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
6541 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
6543 // The existing object is compatible with anything.
6544 // Use whatever requirements the new object has.
6545 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6547 // FIXME: Make it possible to turn off this warning.
6548 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
6549 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6551 unsigned int in_value
= in_attr
[i
].int_value();
6552 unsigned int out_value
= out_attr
[i
].int_value();
6553 gold_warning(_("%s uses %s enums yet the output is to use "
6554 "%s enums; use of enum values across objects "
6557 this->aeabi_enum_name(in_value
).c_str(),
6558 this->aeabi_enum_name(out_value
).c_str());
6562 case elfcpp::Tag_ABI_VFP_args
:
6565 case elfcpp::Tag_ABI_WMMX_args
:
6566 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6568 gold_error(_("%s uses iWMMXt register arguments, output does "
6573 case Object_attribute::Tag_compatibility
:
6574 // Merged in target-independent code.
6576 case elfcpp::Tag_ABI_HardFP_use
:
6577 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6578 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
6579 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
6580 out_attr
[i
].set_int_value(3);
6581 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6582 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6584 case elfcpp::Tag_ABI_FP_16bit_format
:
6585 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6587 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6588 gold_error(_("fp16 format mismatch between %s and output"),
6591 if (in_attr
[i
].int_value() != 0)
6592 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6595 case elfcpp::Tag_nodefaults
:
6596 // This tag is set if it exists, but the value is unused (and is
6597 // typically zero). We don't actually need to do anything here -
6598 // the merge happens automatically when the type flags are merged
6601 case elfcpp::Tag_also_compatible_with
:
6602 // Already done in Tag_CPU_arch.
6604 case elfcpp::Tag_conformance
:
6605 // Keep the attribute if it matches. Throw it away otherwise.
6606 // No attribute means no claim to conform.
6607 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
6608 out_attr
[i
].set_string_value("");
6613 const char* err_object
= NULL
;
6615 // The "known_obj_attributes" table does contain some undefined
6616 // attributes. Ensure that there are unused.
6617 if (out_attr
[i
].int_value() != 0
6618 || out_attr
[i
].string_value() != "")
6619 err_object
= "output";
6620 else if (in_attr
[i
].int_value() != 0
6621 || in_attr
[i
].string_value() != "")
6624 if (err_object
!= NULL
)
6626 // Attribute numbers >=64 (mod 128) can be safely ignored.
6628 gold_error(_("%s: unknown mandatory EABI object attribute "
6632 gold_warning(_("%s: unknown EABI object attribute %d"),
6636 // Only pass on attributes that match in both inputs.
6637 if (!in_attr
[i
].matches(out_attr
[i
]))
6639 out_attr
[i
].set_int_value(0);
6640 out_attr
[i
].set_string_value("");
6645 // If out_attr was copied from in_attr then it won't have a type yet.
6646 if (in_attr
[i
].type() && !out_attr
[i
].type())
6647 out_attr
[i
].set_type(in_attr
[i
].type());
6650 // Merge Tag_compatibility attributes and any common GNU ones.
6651 this->attributes_section_data_
->merge(name
, pasd
);
6653 // Check for any attributes not known on ARM.
6654 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
6655 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
6656 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
6657 Other_attributes
* out_other_attributes
=
6658 this->attributes_section_data_
->other_attributes(vendor
);
6659 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
6661 while (in_iter
!= in_other_attributes
->end()
6662 || out_iter
!= out_other_attributes
->end())
6664 const char* err_object
= NULL
;
6667 // The tags for each list are in numerical order.
6668 // If the tags are equal, then merge.
6669 if (out_iter
!= out_other_attributes
->end()
6670 && (in_iter
== in_other_attributes
->end()
6671 || in_iter
->first
> out_iter
->first
))
6673 // This attribute only exists in output. We can't merge, and we
6674 // don't know what the tag means, so delete it.
6675 err_object
= "output";
6676 err_tag
= out_iter
->first
;
6677 int saved_tag
= out_iter
->first
;
6678 delete out_iter
->second
;
6679 out_other_attributes
->erase(out_iter
);
6680 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6682 else if (in_iter
!= in_other_attributes
->end()
6683 && (out_iter
!= out_other_attributes
->end()
6684 || in_iter
->first
< out_iter
->first
))
6686 // This attribute only exists in input. We can't merge, and we
6687 // don't know what the tag means, so ignore it.
6689 err_tag
= in_iter
->first
;
6692 else // The tags are equal.
6694 // As present, all attributes in the list are unknown, and
6695 // therefore can't be merged meaningfully.
6696 err_object
= "output";
6697 err_tag
= out_iter
->first
;
6699 // Only pass on attributes that match in both inputs.
6700 if (!in_iter
->second
->matches(*(out_iter
->second
)))
6702 // No match. Delete the attribute.
6703 int saved_tag
= out_iter
->first
;
6704 delete out_iter
->second
;
6705 out_other_attributes
->erase(out_iter
);
6706 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6710 // Matched. Keep the attribute and move to the next.
6718 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6719 if ((err_tag
& 127) < 64)
6721 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6722 err_object
, err_tag
);
6726 gold_warning(_("%s: unknown EABI object attribute %d"),
6727 err_object
, err_tag
);
6733 // Return whether a relocation type used the LSB to distinguish THUMB
6735 template<bool big_endian
>
6737 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
6741 case elfcpp::R_ARM_PC24
:
6742 case elfcpp::R_ARM_ABS32
:
6743 case elfcpp::R_ARM_REL32
:
6744 case elfcpp::R_ARM_SBREL32
:
6745 case elfcpp::R_ARM_THM_CALL
:
6746 case elfcpp::R_ARM_GLOB_DAT
:
6747 case elfcpp::R_ARM_JUMP_SLOT
:
6748 case elfcpp::R_ARM_GOTOFF32
:
6749 case elfcpp::R_ARM_PLT32
:
6750 case elfcpp::R_ARM_CALL
:
6751 case elfcpp::R_ARM_JUMP24
:
6752 case elfcpp::R_ARM_THM_JUMP24
:
6753 case elfcpp::R_ARM_SBREL31
:
6754 case elfcpp::R_ARM_PREL31
:
6755 case elfcpp::R_ARM_MOVW_ABS_NC
:
6756 case elfcpp::R_ARM_MOVW_PREL_NC
:
6757 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6758 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6759 case elfcpp::R_ARM_THM_JUMP19
:
6760 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
6761 case elfcpp::R_ARM_ALU_PC_G0_NC
:
6762 case elfcpp::R_ARM_ALU_PC_G0
:
6763 case elfcpp::R_ARM_ALU_PC_G1_NC
:
6764 case elfcpp::R_ARM_ALU_PC_G1
:
6765 case elfcpp::R_ARM_ALU_PC_G2
:
6766 case elfcpp::R_ARM_ALU_SB_G0_NC
:
6767 case elfcpp::R_ARM_ALU_SB_G0
:
6768 case elfcpp::R_ARM_ALU_SB_G1_NC
:
6769 case elfcpp::R_ARM_ALU_SB_G1
:
6770 case elfcpp::R_ARM_ALU_SB_G2
:
6771 case elfcpp::R_ARM_MOVW_BREL_NC
:
6772 case elfcpp::R_ARM_MOVW_BREL
:
6773 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
6774 case elfcpp::R_ARM_THM_MOVW_BREL
:
6781 // Stub-generation methods for Target_arm.
6783 // Make a new Arm_input_section object.
6785 template<bool big_endian
>
6786 Arm_input_section
<big_endian
>*
6787 Target_arm
<big_endian
>::new_arm_input_section(
6791 Input_section_specifier
iss(relobj
, shndx
);
6793 Arm_input_section
<big_endian
>* arm_input_section
=
6794 new Arm_input_section
<big_endian
>(relobj
, shndx
);
6795 arm_input_section
->init();
6797 // Register new Arm_input_section in map for look-up.
6798 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
6799 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
6801 // Make sure that it we have not created another Arm_input_section
6802 // for this input section already.
6803 gold_assert(ins
.second
);
6805 return arm_input_section
;
6808 // Find the Arm_input_section object corresponding to the SHNDX-th input
6809 // section of RELOBJ.
6811 template<bool big_endian
>
6812 Arm_input_section
<big_endian
>*
6813 Target_arm
<big_endian
>::find_arm_input_section(
6815 unsigned int shndx
) const
6817 Input_section_specifier
iss(relobj
, shndx
);
6818 typename
Arm_input_section_map::const_iterator p
=
6819 this->arm_input_section_map_
.find(iss
);
6820 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
6823 // Make a new stub table.
6825 template<bool big_endian
>
6826 Stub_table
<big_endian
>*
6827 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
6829 Stub_table
<big_endian
>* stub_table
=
6830 new Stub_table
<big_endian
>(owner
);
6831 this->stub_tables_
.push_back(stub_table
);
6833 stub_table
->set_address(owner
->address() + owner
->data_size());
6834 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
6835 stub_table
->finalize_data_size();
6840 // Scan a relocation for stub generation.
6842 template<bool big_endian
>
6844 Target_arm
<big_endian
>::scan_reloc_for_stub(
6845 const Relocate_info
<32, big_endian
>* relinfo
,
6846 unsigned int r_type
,
6847 const Sized_symbol
<32>* gsym
,
6849 const Symbol_value
<32>* psymval
,
6850 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
6851 Arm_address address
)
6853 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
6855 const Arm_relobj
<big_endian
>* arm_relobj
=
6856 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6858 bool target_is_thumb
;
6859 Symbol_value
<32> symval
;
6862 // This is a global symbol. Determine if we use PLT and if the
6863 // final target is THUMB.
6864 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
6866 // This uses a PLT, change the symbol value.
6867 symval
.set_output_value(this->plt_section()->address()
6868 + gsym
->plt_offset());
6870 target_is_thumb
= false;
6872 else if (gsym
->is_undefined())
6873 // There is no need to generate a stub symbol is undefined.
6878 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6879 || (gsym
->type() == elfcpp::STT_FUNC
6880 && !gsym
->is_undefined()
6881 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
6886 // This is a local symbol. Determine if the final target is THUMB.
6887 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
6890 // Strip LSB if this points to a THUMB target.
6892 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6893 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
6895 Arm_address stripped_value
=
6896 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
6897 symval
.set_output_value(stripped_value
);
6901 // Get the symbol value.
6902 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
6904 // Owing to pipelining, the PC relative branches below actually skip
6905 // two instructions when the branch offset is 0.
6906 Arm_address destination
;
6909 case elfcpp::R_ARM_CALL
:
6910 case elfcpp::R_ARM_JUMP24
:
6911 case elfcpp::R_ARM_PLT32
:
6913 destination
= value
+ addend
+ 8;
6915 case elfcpp::R_ARM_THM_CALL
:
6916 case elfcpp::R_ARM_THM_XPC22
:
6917 case elfcpp::R_ARM_THM_JUMP24
:
6918 case elfcpp::R_ARM_THM_JUMP19
:
6920 destination
= value
+ addend
+ 4;
6926 Stub_type stub_type
=
6927 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
6930 // This reloc does not need a stub.
6931 if (stub_type
== arm_stub_none
)
6934 // Try looking up an existing stub from a stub table.
6935 Stub_table
<big_endian
>* stub_table
=
6936 arm_relobj
->stub_table(relinfo
->data_shndx
);
6937 gold_assert(stub_table
!= NULL
);
6939 // Locate stub by destination.
6940 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
6942 // Create a stub if there is not one already
6943 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
6946 // create a new stub and add it to stub table.
6947 stub
= this->stub_factory().make_reloc_stub(stub_type
);
6948 stub_table
->add_reloc_stub(stub
, stub_key
);
6951 // Record the destination address.
6952 stub
->set_destination_address(destination
6953 | (target_is_thumb
? 1 : 0));
6956 // This function scans a relocation sections for stub generation.
6957 // The template parameter Relocate must be a class type which provides
6958 // a single function, relocate(), which implements the machine
6959 // specific part of a relocation.
6961 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
6962 // SHT_REL or SHT_RELA.
6964 // PRELOCS points to the relocation data. RELOC_COUNT is the number
6965 // of relocs. OUTPUT_SECTION is the output section.
6966 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6967 // mapped to output offsets.
6969 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
6970 // VIEW_SIZE is the size. These refer to the input section, unless
6971 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6972 // the output section.
6974 template<bool big_endian
>
6975 template<int sh_type
>
6977 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
6978 const Relocate_info
<32, big_endian
>* relinfo
,
6979 const unsigned char* prelocs
,
6981 Output_section
* output_section
,
6982 bool needs_special_offset_handling
,
6983 const unsigned char* view
,
6984 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
6987 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
6988 const int reloc_size
=
6989 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
6991 Arm_relobj
<big_endian
>* arm_object
=
6992 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6993 unsigned int local_count
= arm_object
->local_symbol_count();
6995 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
6997 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6999 Reltype
reloc(prelocs
);
7001 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
7002 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
7003 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
7005 r_type
= this->get_real_reloc_type(r_type
);
7007 // Only a few relocation types need stubs.
7008 if ((r_type
!= elfcpp::R_ARM_CALL
)
7009 && (r_type
!= elfcpp::R_ARM_JUMP24
)
7010 && (r_type
!= elfcpp::R_ARM_PLT32
)
7011 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
7012 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
7013 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
7014 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
7017 section_offset_type offset
=
7018 convert_to_section_size_type(reloc
.get_r_offset());
7020 if (needs_special_offset_handling
)
7022 offset
= output_section
->output_offset(relinfo
->object
,
7023 relinfo
->data_shndx
,
7030 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
7031 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
7032 stub_addend_reader(r_type
, view
+ offset
, reloc
);
7034 const Sized_symbol
<32>* sym
;
7036 Symbol_value
<32> symval
;
7037 const Symbol_value
<32> *psymval
;
7038 if (r_sym
< local_count
)
7041 psymval
= arm_object
->local_symbol(r_sym
);
7043 // If the local symbol belongs to a section we are discarding,
7044 // and that section is a debug section, try to find the
7045 // corresponding kept section and map this symbol to its
7046 // counterpart in the kept section. The symbol must not
7047 // correspond to a section we are folding.
7049 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7051 && shndx
!= elfcpp::SHN_UNDEF
7052 && !arm_object
->is_section_included(shndx
)
7053 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
7055 if (comdat_behavior
== CB_UNDETERMINED
)
7058 arm_object
->section_name(relinfo
->data_shndx
);
7059 comdat_behavior
= get_comdat_behavior(name
.c_str());
7061 if (comdat_behavior
== CB_PRETEND
)
7064 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
7065 arm_object
->map_to_kept_section(shndx
, &found
);
7067 symval
.set_output_value(value
+ psymval
->input_value());
7069 symval
.set_output_value(0);
7073 symval
.set_output_value(0);
7075 symval
.set_no_output_symtab_entry();
7081 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
7082 gold_assert(gsym
!= NULL
);
7083 if (gsym
->is_forwarder())
7084 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7086 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
7087 if (sym
->has_symtab_index())
7088 symval
.set_output_symtab_index(sym
->symtab_index());
7090 symval
.set_no_output_symtab_entry();
7092 // We need to compute the would-be final value of this global
7094 const Symbol_table
* symtab
= relinfo
->symtab
;
7095 const Sized_symbol
<32>* sized_symbol
=
7096 symtab
->get_sized_symbol
<32>(gsym
);
7097 Symbol_table::Compute_final_value_status status
;
7099 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
7101 // Skip this if the symbol has not output section.
7102 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
7105 symval
.set_output_value(value
);
7109 // If symbol is a section symbol, we don't know the actual type of
7110 // destination. Give up.
7111 if (psymval
->is_section_symbol())
7114 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
7115 addend
, view_address
+ offset
);
7119 // Scan an input section for stub generation.
7121 template<bool big_endian
>
7123 Target_arm
<big_endian
>::scan_section_for_stubs(
7124 const Relocate_info
<32, big_endian
>* relinfo
,
7125 unsigned int sh_type
,
7126 const unsigned char* prelocs
,
7128 Output_section
* output_section
,
7129 bool needs_special_offset_handling
,
7130 const unsigned char* view
,
7131 Arm_address view_address
,
7132 section_size_type view_size
)
7134 if (sh_type
== elfcpp::SHT_REL
)
7135 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
7140 needs_special_offset_handling
,
7144 else if (sh_type
== elfcpp::SHT_RELA
)
7145 // We do not support RELA type relocations yet. This is provided for
7147 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
7152 needs_special_offset_handling
,
7160 // Group input sections for stub generation.
7162 // We goup input sections in an output sections so that the total size,
7163 // including any padding space due to alignment is smaller than GROUP_SIZE
7164 // unless the only input section in group is bigger than GROUP_SIZE already.
7165 // Then an ARM stub table is created to follow the last input section
7166 // in group. For each group an ARM stub table is created an is placed
7167 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7168 // extend the group after the stub table.
7170 template<bool big_endian
>
7172 Target_arm
<big_endian
>::group_sections(
7174 section_size_type group_size
,
7175 bool stubs_always_after_branch
)
7177 // Group input sections and insert stub table
7178 Layout::Section_list section_list
;
7179 layout
->get_allocated_sections(§ion_list
);
7180 for (Layout::Section_list::const_iterator p
= section_list
.begin();
7181 p
!= section_list
.end();
7184 Arm_output_section
<big_endian
>* output_section
=
7185 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
7186 output_section
->group_sections(group_size
, stubs_always_after_branch
,
7191 // Relaxation hook. This is where we do stub generation.
7193 template<bool big_endian
>
7195 Target_arm
<big_endian
>::do_relax(
7197 const Input_objects
* input_objects
,
7198 Symbol_table
* symtab
,
7201 // No need to generate stubs if this is a relocatable link.
7202 gold_assert(!parameters
->options().relocatable());
7204 // If this is the first pass, we need to group input sections into
7208 // Determine the stub group size. The group size is the absolute
7209 // value of the parameter --stub-group-size. If --stub-group-size
7210 // is passed a negative value, we restict stubs to be always after
7211 // the stubbed branches.
7212 int32_t stub_group_size_param
=
7213 parameters
->options().stub_group_size();
7214 bool stubs_always_after_branch
= stub_group_size_param
< 0;
7215 section_size_type stub_group_size
= abs(stub_group_size_param
);
7217 if (stub_group_size
== 1)
7220 // Thumb branch range is +-4MB has to be used as the default
7221 // maximum size (a given section can contain both ARM and Thumb
7222 // code, so the worst case has to be taken into account).
7224 // This value is 24K less than that, which allows for 2025
7225 // 12-byte stubs. If we exceed that, then we will fail to link.
7226 // The user will have to relink with an explicit group size
7228 stub_group_size
= 4170000;
7231 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
7234 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
7236 // scan relocs for stubs
7237 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
7238 op
!= input_objects
->relobj_end();
7241 Arm_relobj
<big_endian
>* arm_relobj
=
7242 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
7243 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
7246 // Check all stub tables to see if any of them have their data sizes
7247 // or addresses alignments changed. These are the only things that
7249 bool any_stub_table_changed
= false;
7250 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7251 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7254 if ((*sp
)->update_data_size_and_addralign())
7255 any_stub_table_changed
= true;
7258 // Finalize the stubs in the last relaxation pass.
7259 if (!any_stub_table_changed
)
7260 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7261 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7263 (*sp
)->finalize_stubs();
7265 return any_stub_table_changed
;
7270 template<bool big_endian
>
7272 Target_arm
<big_endian
>::relocate_stub(
7274 const Relocate_info
<32, big_endian
>* relinfo
,
7275 Output_section
* output_section
,
7276 unsigned char* view
,
7277 Arm_address address
,
7278 section_size_type view_size
)
7281 const Stub_template
* stub_template
= stub
->stub_template();
7282 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
7284 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
7285 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
7287 unsigned int r_type
= insn
->r_type();
7288 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
7289 section_size_type reloc_size
= insn
->size();
7290 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
7292 // This is the address of the stub destination.
7293 Arm_address target
= stub
->reloc_target(i
);
7294 Symbol_value
<32> symval
;
7295 symval
.set_output_value(target
);
7297 // Synthesize a fake reloc just in case. We don't have a symbol so
7299 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
7300 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
7301 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
7302 reloc_write
.put_r_offset(reloc_offset
);
7303 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
7304 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
7306 relocate
.relocate(relinfo
, this, output_section
,
7307 this->fake_relnum_for_stubs
, rel
, r_type
,
7308 NULL
, &symval
, view
+ reloc_offset
,
7309 address
+ reloc_offset
, reloc_size
);
7313 // Determine whether an object attribute tag takes an integer, a
7316 template<bool big_endian
>
7318 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
7320 if (tag
== Object_attribute::Tag_compatibility
)
7321 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7322 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
7323 else if (tag
== elfcpp::Tag_nodefaults
)
7324 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7325 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
7326 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
7327 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
7329 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
7331 return ((tag
& 1) != 0
7332 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7333 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
7336 // Reorder attributes.
7338 // The ABI defines that Tag_conformance should be emitted first, and that
7339 // Tag_nodefaults should be second (if either is defined). This sets those
7340 // two positions, and bumps up the position of all the remaining tags to
7343 template<bool big_endian
>
7345 Target_arm
<big_endian
>::do_attributes_order(int num
) const
7347 // Reorder the known object attributes in output. We want to move
7348 // Tag_conformance to position 4 and Tag_conformance to position 5
7349 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7351 return elfcpp::Tag_conformance
;
7353 return elfcpp::Tag_nodefaults
;
7354 if ((num
- 2) < elfcpp::Tag_nodefaults
)
7356 if ((num
- 1) < elfcpp::Tag_conformance
)
7361 template<bool big_endian
>
7362 class Target_selector_arm
: public Target_selector
7365 Target_selector_arm()
7366 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
7367 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
7371 do_instantiate_target()
7372 { return new Target_arm
<big_endian
>(); }
7375 Target_selector_arm
<false> target_selector_arm
;
7376 Target_selector_arm
<true> target_selector_armbe
;
7378 } // End anonymous namespace.