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
);
1182 // List of stub tables.
1183 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1184 Stub_table_list stub_tables_
;
1185 // Bit vector to tell if a local symbol is a thumb function or not.
1186 // This is only valid after do_count_local_symbol is called.
1187 std::vector
<bool> local_symbol_is_thumb_function_
;
1188 // processor-specific flags in ELF file header.
1189 elfcpp::Elf_Word processor_specific_flags_
;
1190 // Object attributes if there is an .ARM.attributes section or NULL.
1191 Attributes_section_data
* attributes_section_data_
;
1192 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1193 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1196 // Arm_dynobj class.
1198 template<bool big_endian
>
1199 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1202 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1203 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1204 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1205 processor_specific_flags_(0), attributes_section_data_(NULL
)
1209 { delete this->attributes_section_data_
; }
1211 // Downcast a base pointer to an Arm_relobj pointer. This is
1212 // not type-safe but we only use Arm_relobj not the base class.
1213 static Arm_dynobj
<big_endian
>*
1214 as_arm_dynobj(Dynobj
* dynobj
)
1215 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1217 // Processor-specific flags in ELF file header. This is valid only after
1220 processor_specific_flags() const
1221 { return this->processor_specific_flags_
; }
1223 // Attributes section data.
1224 const Attributes_section_data
*
1225 attributes_section_data() const
1226 { return this->attributes_section_data_
; }
1229 // Read the symbol information.
1231 do_read_symbols(Read_symbols_data
* sd
);
1234 // processor-specific flags in ELF file header.
1235 elfcpp::Elf_Word processor_specific_flags_
;
1236 // Object attributes if there is an .ARM.attributes section or NULL.
1237 Attributes_section_data
* attributes_section_data_
;
1240 // Functor to read reloc addends during stub generation.
1242 template<int sh_type
, bool big_endian
>
1243 struct Stub_addend_reader
1245 // Return the addend for a relocation of a particular type. Depending
1246 // on whether this is a REL or RELA relocation, read the addend from a
1247 // view or from a Reloc object.
1248 elfcpp::Elf_types
<32>::Elf_Swxword
1250 unsigned int /* r_type */,
1251 const unsigned char* /* view */,
1252 const typename Reloc_types
<sh_type
,
1253 32, big_endian
>::Reloc
& /* reloc */) const;
1256 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1258 template<bool big_endian
>
1259 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1261 elfcpp::Elf_types
<32>::Elf_Swxword
1264 const unsigned char*,
1265 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1268 // Specialized Stub_addend_reader for RELA type relocation sections.
1269 // We currently do not handle RELA type relocation sections but it is trivial
1270 // to implement the addend reader. This is provided for completeness and to
1271 // make it easier to add support for RELA relocation sections in the future.
1273 template<bool big_endian
>
1274 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1276 elfcpp::Elf_types
<32>::Elf_Swxword
1279 const unsigned char*,
1280 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1281 big_endian
>::Reloc
& reloc
) const
1282 { return reloc
.get_r_addend(); }
1285 // Utilities for manipulating integers of up to 32-bits
1289 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1290 // an int32_t. NO_BITS must be between 1 to 32.
1291 template<int no_bits
>
1292 static inline int32_t
1293 sign_extend(uint32_t bits
)
1295 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1297 return static_cast<int32_t>(bits
);
1298 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1300 uint32_t top_bit
= 1U << (no_bits
- 1);
1301 int32_t as_signed
= static_cast<int32_t>(bits
);
1302 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1305 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1306 template<int no_bits
>
1308 has_overflow(uint32_t bits
)
1310 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1313 int32_t max
= (1 << (no_bits
- 1)) - 1;
1314 int32_t min
= -(1 << (no_bits
- 1));
1315 int32_t as_signed
= static_cast<int32_t>(bits
);
1316 return as_signed
> max
|| as_signed
< min
;
1319 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1320 // fits in the given number of bits as either a signed or unsigned value.
1321 // For example, has_signed_unsigned_overflow<8> would check
1322 // -128 <= bits <= 255
1323 template<int no_bits
>
1325 has_signed_unsigned_overflow(uint32_t bits
)
1327 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1330 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1331 int32_t min
= -(1 << (no_bits
- 1));
1332 int32_t as_signed
= static_cast<int32_t>(bits
);
1333 return as_signed
> max
|| as_signed
< min
;
1336 // Select bits from A and B using bits in MASK. For each n in [0..31],
1337 // the n-th bit in the result is chosen from the n-th bits of A and B.
1338 // A zero selects A and a one selects B.
1339 static inline uint32_t
1340 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1341 { return (a
& ~mask
) | (b
& mask
); }
1344 template<bool big_endian
>
1345 class Target_arm
: public Sized_target
<32, big_endian
>
1348 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1351 // When were are relocating a stub, we pass this as the relocation number.
1352 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1355 : Sized_target
<32, big_endian
>(&arm_info
),
1356 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1357 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1358 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1359 should_force_pic_veneer_(false), arm_input_section_map_(),
1360 attributes_section_data_(NULL
)
1363 // Whether we can use BLX.
1366 { return this->may_use_blx_
; }
1368 // Set use-BLX flag.
1370 set_may_use_blx(bool value
)
1371 { this->may_use_blx_
= value
; }
1373 // Whether we force PCI branch veneers.
1375 should_force_pic_veneer() const
1376 { return this->should_force_pic_veneer_
; }
1378 // Set PIC veneer flag.
1380 set_should_force_pic_veneer(bool value
)
1381 { this->should_force_pic_veneer_
= value
; }
1383 // Whether we use THUMB-2 instructions.
1385 using_thumb2() const
1387 Object_attribute
* attr
=
1388 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1389 int arch
= attr
->int_value();
1390 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1393 // Whether we use THUMB/THUMB-2 instructions only.
1395 using_thumb_only() const
1397 Object_attribute
* attr
=
1398 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1399 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1400 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1402 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1403 return attr
->int_value() == 'M';
1406 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1408 may_use_arm_nop() const
1410 Object_attribute
* attr
=
1411 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1412 int arch
= attr
->int_value();
1413 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1414 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1415 || arch
== elfcpp::TAG_CPU_ARCH_V7
1416 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1419 // Whether we have THUMB-2 NOP.W instruction.
1421 may_use_thumb2_nop() const
1423 Object_attribute
* attr
=
1424 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1425 int arch
= attr
->int_value();
1426 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1427 || arch
== elfcpp::TAG_CPU_ARCH_V7
1428 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1431 // Process the relocations to determine unreferenced sections for
1432 // garbage collection.
1434 gc_process_relocs(Symbol_table
* symtab
,
1436 Sized_relobj
<32, big_endian
>* object
,
1437 unsigned int data_shndx
,
1438 unsigned int sh_type
,
1439 const unsigned char* prelocs
,
1441 Output_section
* output_section
,
1442 bool needs_special_offset_handling
,
1443 size_t local_symbol_count
,
1444 const unsigned char* plocal_symbols
);
1446 // Scan the relocations to look for symbol adjustments.
1448 scan_relocs(Symbol_table
* symtab
,
1450 Sized_relobj
<32, big_endian
>* object
,
1451 unsigned int data_shndx
,
1452 unsigned int sh_type
,
1453 const unsigned char* prelocs
,
1455 Output_section
* output_section
,
1456 bool needs_special_offset_handling
,
1457 size_t local_symbol_count
,
1458 const unsigned char* plocal_symbols
);
1460 // Finalize the sections.
1462 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1464 // Return the value to use for a dynamic symbol which requires special
1467 do_dynsym_value(const Symbol
*) const;
1469 // Relocate a section.
1471 relocate_section(const Relocate_info
<32, big_endian
>*,
1472 unsigned int sh_type
,
1473 const unsigned char* prelocs
,
1475 Output_section
* output_section
,
1476 bool needs_special_offset_handling
,
1477 unsigned char* view
,
1478 Arm_address view_address
,
1479 section_size_type view_size
,
1480 const Reloc_symbol_changes
*);
1482 // Scan the relocs during a relocatable link.
1484 scan_relocatable_relocs(Symbol_table
* symtab
,
1486 Sized_relobj
<32, big_endian
>* object
,
1487 unsigned int data_shndx
,
1488 unsigned int sh_type
,
1489 const unsigned char* prelocs
,
1491 Output_section
* output_section
,
1492 bool needs_special_offset_handling
,
1493 size_t local_symbol_count
,
1494 const unsigned char* plocal_symbols
,
1495 Relocatable_relocs
*);
1497 // Relocate a section during a relocatable link.
1499 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1500 unsigned int sh_type
,
1501 const unsigned char* prelocs
,
1503 Output_section
* output_section
,
1504 off_t offset_in_output_section
,
1505 const Relocatable_relocs
*,
1506 unsigned char* view
,
1507 Arm_address view_address
,
1508 section_size_type view_size
,
1509 unsigned char* reloc_view
,
1510 section_size_type reloc_view_size
);
1512 // Return whether SYM is defined by the ABI.
1514 do_is_defined_by_abi(Symbol
* sym
) const
1515 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1517 // Return the size of the GOT section.
1521 gold_assert(this->got_
!= NULL
);
1522 return this->got_
->data_size();
1525 // Map platform-specific reloc types
1527 get_real_reloc_type (unsigned int r_type
);
1530 // Methods to support stub-generations.
1533 // Return the stub factory
1535 stub_factory() const
1536 { return this->stub_factory_
; }
1538 // Make a new Arm_input_section object.
1539 Arm_input_section
<big_endian
>*
1540 new_arm_input_section(Relobj
*, unsigned int);
1542 // Find the Arm_input_section object corresponding to the SHNDX-th input
1543 // section of RELOBJ.
1544 Arm_input_section
<big_endian
>*
1545 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1547 // Make a new Stub_table
1548 Stub_table
<big_endian
>*
1549 new_stub_table(Arm_input_section
<big_endian
>*);
1551 // Scan a section for stub generation.
1553 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1554 const unsigned char*, size_t, Output_section
*,
1555 bool, const unsigned char*, Arm_address
,
1560 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1561 Output_section
*, unsigned char*, Arm_address
,
1564 // Get the default ARM target.
1565 static Target_arm
<big_endian
>*
1568 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1569 && parameters
->target().is_big_endian() == big_endian
);
1570 return static_cast<Target_arm
<big_endian
>*>(
1571 parameters
->sized_target
<32, big_endian
>());
1574 // Whether relocation type uses LSB to distinguish THUMB addresses.
1576 reloc_uses_thumb_bit(unsigned int r_type
);
1579 // Make an ELF object.
1581 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1582 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1585 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1586 const elfcpp::Ehdr
<32, !big_endian
>&)
1587 { gold_unreachable(); }
1590 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1591 const elfcpp::Ehdr
<64, false>&)
1592 { gold_unreachable(); }
1595 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1596 const elfcpp::Ehdr
<64, true>&)
1597 { gold_unreachable(); }
1599 // Make an output section.
1601 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1602 elfcpp::Elf_Xword flags
)
1603 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1606 do_adjust_elf_header(unsigned char* view
, int len
) const;
1608 // We only need to generate stubs, and hence perform relaxation if we are
1609 // not doing relocatable linking.
1611 do_may_relax() const
1612 { return !parameters
->options().relocatable(); }
1615 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1617 // Determine whether an object attribute tag takes an integer, a
1620 do_attribute_arg_type(int tag
) const;
1622 // Reorder tags during output.
1624 do_attributes_order(int num
) const;
1627 // The class which scans relocations.
1632 : issued_non_pic_error_(false)
1636 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1637 Sized_relobj
<32, big_endian
>* object
,
1638 unsigned int data_shndx
,
1639 Output_section
* output_section
,
1640 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1641 const elfcpp::Sym
<32, big_endian
>& lsym
);
1644 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1645 Sized_relobj
<32, big_endian
>* object
,
1646 unsigned int data_shndx
,
1647 Output_section
* output_section
,
1648 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1653 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1654 unsigned int r_type
);
1657 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1658 unsigned int r_type
, Symbol
*);
1661 check_non_pic(Relobj
*, unsigned int r_type
);
1663 // Almost identical to Symbol::needs_plt_entry except that it also
1664 // handles STT_ARM_TFUNC.
1666 symbol_needs_plt_entry(const Symbol
* sym
)
1668 // An undefined symbol from an executable does not need a PLT entry.
1669 if (sym
->is_undefined() && !parameters
->options().shared())
1672 return (!parameters
->doing_static_link()
1673 && (sym
->type() == elfcpp::STT_FUNC
1674 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1675 && (sym
->is_from_dynobj()
1676 || sym
->is_undefined()
1677 || sym
->is_preemptible()));
1680 // Whether we have issued an error about a non-PIC compilation.
1681 bool issued_non_pic_error_
;
1684 // The class which implements relocation.
1694 // Return whether the static relocation needs to be applied.
1696 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1699 Output_section
* output_section
);
1701 // Do a relocation. Return false if the caller should not issue
1702 // any warnings about this relocation.
1704 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1705 Output_section
*, size_t relnum
,
1706 const elfcpp::Rel
<32, big_endian
>&,
1707 unsigned int r_type
, const Sized_symbol
<32>*,
1708 const Symbol_value
<32>*,
1709 unsigned char*, Arm_address
,
1712 // Return whether we want to pass flag NON_PIC_REF for this
1713 // reloc. This means the relocation type accesses a symbol not via
1716 reloc_is_non_pic (unsigned int r_type
)
1720 // These relocation types reference GOT or PLT entries explicitly.
1721 case elfcpp::R_ARM_GOT_BREL
:
1722 case elfcpp::R_ARM_GOT_ABS
:
1723 case elfcpp::R_ARM_GOT_PREL
:
1724 case elfcpp::R_ARM_GOT_BREL12
:
1725 case elfcpp::R_ARM_PLT32_ABS
:
1726 case elfcpp::R_ARM_TLS_GD32
:
1727 case elfcpp::R_ARM_TLS_LDM32
:
1728 case elfcpp::R_ARM_TLS_IE32
:
1729 case elfcpp::R_ARM_TLS_IE12GP
:
1731 // These relocate types may use PLT entries.
1732 case elfcpp::R_ARM_CALL
:
1733 case elfcpp::R_ARM_THM_CALL
:
1734 case elfcpp::R_ARM_JUMP24
:
1735 case elfcpp::R_ARM_THM_JUMP24
:
1736 case elfcpp::R_ARM_THM_JUMP19
:
1737 case elfcpp::R_ARM_PLT32
:
1738 case elfcpp::R_ARM_THM_XPC22
:
1747 // A class which returns the size required for a relocation type,
1748 // used while scanning relocs during a relocatable link.
1749 class Relocatable_size_for_reloc
1753 get_size_for_reloc(unsigned int, Relobj
*);
1756 // Get the GOT section, creating it if necessary.
1757 Output_data_got
<32, big_endian
>*
1758 got_section(Symbol_table
*, Layout
*);
1760 // Get the GOT PLT section.
1762 got_plt_section() const
1764 gold_assert(this->got_plt_
!= NULL
);
1765 return this->got_plt_
;
1768 // Create a PLT entry for a global symbol.
1770 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1772 // Get the PLT section.
1773 const Output_data_plt_arm
<big_endian
>*
1776 gold_assert(this->plt_
!= NULL
);
1780 // Get the dynamic reloc section, creating it if necessary.
1782 rel_dyn_section(Layout
*);
1784 // Return true if the symbol may need a COPY relocation.
1785 // References from an executable object to non-function symbols
1786 // defined in a dynamic object may need a COPY relocation.
1788 may_need_copy_reloc(Symbol
* gsym
)
1790 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1791 && gsym
->may_need_copy_reloc());
1794 // Add a potential copy relocation.
1796 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1797 Sized_relobj
<32, big_endian
>* object
,
1798 unsigned int shndx
, Output_section
* output_section
,
1799 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1801 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1802 symtab
->get_sized_symbol
<32>(sym
),
1803 object
, shndx
, output_section
, reloc
,
1804 this->rel_dyn_section(layout
));
1807 // Whether two EABI versions are compatible.
1809 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1811 // Merge processor-specific flags from input object and those in the ELF
1812 // header of the output.
1814 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1816 // Get the secondary compatible architecture.
1818 get_secondary_compatible_arch(const Attributes_section_data
*);
1820 // Set the secondary compatible architecture.
1822 set_secondary_compatible_arch(Attributes_section_data
*, int);
1825 tag_cpu_arch_combine(const char*, int, int*, int, int);
1827 // Helper to print AEABI enum tag value.
1829 aeabi_enum_name(unsigned int);
1831 // Return string value for TAG_CPU_name.
1833 tag_cpu_name_value(unsigned int);
1835 // Merge object attributes from input object and those in the output.
1837 merge_object_attributes(const char*, const Attributes_section_data
*);
1839 // Helper to get an AEABI object attribute
1841 get_aeabi_object_attribute(int tag
) const
1843 Attributes_section_data
* pasd
= this->attributes_section_data_
;
1844 gold_assert(pasd
!= NULL
);
1845 Object_attribute
* attr
=
1846 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
1847 gold_assert(attr
!= NULL
);
1852 // Methods to support stub-generations.
1855 // Group input sections for stub generation.
1857 group_sections(Layout
*, section_size_type
, bool);
1859 // Scan a relocation for stub generation.
1861 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1862 const Sized_symbol
<32>*, unsigned int,
1863 const Symbol_value
<32>*,
1864 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1866 // Scan a relocation section for stub.
1867 template<int sh_type
>
1869 scan_reloc_section_for_stubs(
1870 const Relocate_info
<32, big_endian
>* relinfo
,
1871 const unsigned char* prelocs
,
1873 Output_section
* output_section
,
1874 bool needs_special_offset_handling
,
1875 const unsigned char* view
,
1876 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1879 // Information about this specific target which we pass to the
1880 // general Target structure.
1881 static const Target::Target_info arm_info
;
1883 // The types of GOT entries needed for this platform.
1886 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1889 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1891 // Map input section to Arm_input_section.
1892 typedef Unordered_map
<Input_section_specifier
,
1893 Arm_input_section
<big_endian
>*,
1894 Input_section_specifier::hash
,
1895 Input_section_specifier::equal_to
>
1896 Arm_input_section_map
;
1899 Output_data_got
<32, big_endian
>* got_
;
1901 Output_data_plt_arm
<big_endian
>* plt_
;
1902 // The GOT PLT section.
1903 Output_data_space
* got_plt_
;
1904 // The dynamic reloc section.
1905 Reloc_section
* rel_dyn_
;
1906 // Relocs saved to avoid a COPY reloc.
1907 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1908 // Space for variables copied with a COPY reloc.
1909 Output_data_space
* dynbss_
;
1910 // Vector of Stub_tables created.
1911 Stub_table_list stub_tables_
;
1913 const Stub_factory
&stub_factory_
;
1914 // Whether we can use BLX.
1916 // Whether we force PIC branch veneers.
1917 bool should_force_pic_veneer_
;
1918 // Map for locating Arm_input_sections.
1919 Arm_input_section_map arm_input_section_map_
;
1920 // Attributes section data in output.
1921 Attributes_section_data
* attributes_section_data_
;
1924 template<bool big_endian
>
1925 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1928 big_endian
, // is_big_endian
1929 elfcpp::EM_ARM
, // machine_code
1930 false, // has_make_symbol
1931 false, // has_resolve
1932 false, // has_code_fill
1933 true, // is_default_stack_executable
1935 "/usr/lib/libc.so.1", // dynamic_linker
1936 0x8000, // default_text_segment_address
1937 0x1000, // abi_pagesize (overridable by -z max-page-size)
1938 0x1000, // common_pagesize (overridable by -z common-page-size)
1939 elfcpp::SHN_UNDEF
, // small_common_shndx
1940 elfcpp::SHN_UNDEF
, // large_common_shndx
1941 0, // small_common_section_flags
1942 0, // large_common_section_flags
1943 ".ARM.attributes", // attributes_section
1944 "aeabi" // attributes_vendor
1947 // Arm relocate functions class
1950 template<bool big_endian
>
1951 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1956 STATUS_OKAY
, // No error during relocation.
1957 STATUS_OVERFLOW
, // Relocation oveflow.
1958 STATUS_BAD_RELOC
// Relocation cannot be applied.
1962 typedef Relocate_functions
<32, big_endian
> Base
;
1963 typedef Arm_relocate_functions
<big_endian
> This
;
1965 // Encoding of imm16 argument for movt and movw ARM instructions
1968 // imm16 := imm4 | imm12
1970 // 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
1971 // +-------+---------------+-------+-------+-----------------------+
1972 // | | |imm4 | |imm12 |
1973 // +-------+---------------+-------+-------+-----------------------+
1975 // Extract the relocation addend from VAL based on the ARM
1976 // instruction encoding described above.
1977 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1978 extract_arm_movw_movt_addend(
1979 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1981 // According to the Elf ABI for ARM Architecture the immediate
1982 // field is sign-extended to form the addend.
1983 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1986 // Insert X into VAL based on the ARM instruction encoding described
1988 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1989 insert_val_arm_movw_movt(
1990 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1991 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1995 val
|= (x
& 0xf000) << 4;
1999 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2002 // imm16 := imm4 | i | imm3 | imm8
2004 // 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
2005 // +---------+-+-----------+-------++-+-----+-------+---------------+
2006 // | |i| |imm4 || |imm3 | |imm8 |
2007 // +---------+-+-----------+-------++-+-----+-------+---------------+
2009 // Extract the relocation addend from VAL based on the Thumb2
2010 // instruction encoding described above.
2011 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2012 extract_thumb_movw_movt_addend(
2013 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2015 // According to the Elf ABI for ARM Architecture the immediate
2016 // field is sign-extended to form the addend.
2017 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2018 | ((val
>> 15) & 0x0800)
2019 | ((val
>> 4) & 0x0700)
2023 // Insert X into VAL based on the Thumb2 instruction encoding
2025 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2026 insert_val_thumb_movw_movt(
2027 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2028 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2031 val
|= (x
& 0xf000) << 4;
2032 val
|= (x
& 0x0800) << 15;
2033 val
|= (x
& 0x0700) << 4;
2034 val
|= (x
& 0x00ff);
2038 // Handle ARM long branches.
2039 static typename
This::Status
2040 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2041 unsigned char *, const Sized_symbol
<32>*,
2042 const Arm_relobj
<big_endian
>*, unsigned int,
2043 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2045 // Handle THUMB long branches.
2046 static typename
This::Status
2047 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2048 unsigned char *, const Sized_symbol
<32>*,
2049 const Arm_relobj
<big_endian
>*, unsigned int,
2050 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2054 // R_ARM_ABS8: S + A
2055 static inline typename
This::Status
2056 abs8(unsigned char *view
,
2057 const Sized_relobj
<32, big_endian
>* object
,
2058 const Symbol_value
<32>* psymval
)
2060 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2061 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2062 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2063 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2064 Reltype addend
= utils::sign_extend
<8>(val
);
2065 Reltype x
= psymval
->value(object
, addend
);
2066 val
= utils::bit_select(val
, x
, 0xffU
);
2067 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2068 return (utils::has_signed_unsigned_overflow
<8>(x
)
2069 ? This::STATUS_OVERFLOW
2070 : This::STATUS_OKAY
);
2073 // R_ARM_THM_ABS5: S + A
2074 static inline typename
This::Status
2075 thm_abs5(unsigned char *view
,
2076 const Sized_relobj
<32, big_endian
>* object
,
2077 const Symbol_value
<32>* psymval
)
2079 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2080 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2081 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2082 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2083 Reltype addend
= (val
& 0x7e0U
) >> 6;
2084 Reltype x
= psymval
->value(object
, addend
);
2085 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2086 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2087 return (utils::has_overflow
<5>(x
)
2088 ? This::STATUS_OVERFLOW
2089 : This::STATUS_OKAY
);
2092 // R_ARM_ABS12: S + A
2093 static inline typename
This::Status
2094 abs12(unsigned char *view
,
2095 const Sized_relobj
<32, big_endian
>* object
,
2096 const Symbol_value
<32>* psymval
)
2098 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2099 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2100 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2101 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2102 Reltype addend
= val
& 0x0fffU
;
2103 Reltype x
= psymval
->value(object
, addend
);
2104 val
= utils::bit_select(val
, x
, 0x0fffU
);
2105 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2106 return (utils::has_overflow
<12>(x
)
2107 ? This::STATUS_OVERFLOW
2108 : This::STATUS_OKAY
);
2111 // R_ARM_ABS16: S + A
2112 static inline typename
This::Status
2113 abs16(unsigned char *view
,
2114 const Sized_relobj
<32, big_endian
>* object
,
2115 const Symbol_value
<32>* psymval
)
2117 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2118 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2119 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2120 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2121 Reltype addend
= utils::sign_extend
<16>(val
);
2122 Reltype x
= psymval
->value(object
, addend
);
2123 val
= utils::bit_select(val
, x
, 0xffffU
);
2124 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2125 return (utils::has_signed_unsigned_overflow
<16>(x
)
2126 ? This::STATUS_OVERFLOW
2127 : This::STATUS_OKAY
);
2130 // R_ARM_ABS32: (S + A) | T
2131 static inline typename
This::Status
2132 abs32(unsigned char *view
,
2133 const Sized_relobj
<32, big_endian
>* object
,
2134 const Symbol_value
<32>* psymval
,
2135 Arm_address thumb_bit
)
2137 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2138 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2139 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2140 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2141 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2142 return This::STATUS_OKAY
;
2145 // R_ARM_REL32: (S + A) | T - P
2146 static inline typename
This::Status
2147 rel32(unsigned char *view
,
2148 const Sized_relobj
<32, big_endian
>* object
,
2149 const Symbol_value
<32>* psymval
,
2150 Arm_address address
,
2151 Arm_address thumb_bit
)
2153 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2154 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2155 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2156 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2157 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2158 return This::STATUS_OKAY
;
2161 // R_ARM_THM_CALL: (S + A) | T - P
2162 static inline typename
This::Status
2163 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2164 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2165 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2166 Arm_address address
, Arm_address thumb_bit
,
2167 bool is_weakly_undefined_without_plt
)
2169 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2170 object
, r_sym
, psymval
, address
, thumb_bit
,
2171 is_weakly_undefined_without_plt
);
2174 // R_ARM_THM_JUMP24: (S + A) | T - P
2175 static inline typename
This::Status
2176 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2177 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2178 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2179 Arm_address address
, Arm_address thumb_bit
,
2180 bool is_weakly_undefined_without_plt
)
2182 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2183 object
, r_sym
, psymval
, address
, thumb_bit
,
2184 is_weakly_undefined_without_plt
);
2187 // R_ARM_THM_XPC22: (S + A) | T - P
2188 static inline typename
This::Status
2189 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2190 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2191 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2192 Arm_address address
, Arm_address thumb_bit
,
2193 bool is_weakly_undefined_without_plt
)
2195 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2196 object
, r_sym
, psymval
, address
, thumb_bit
,
2197 is_weakly_undefined_without_plt
);
2200 // R_ARM_BASE_PREL: B(S) + A - P
2201 static inline typename
This::Status
2202 base_prel(unsigned char* view
,
2204 Arm_address address
)
2206 Base::rel32(view
, origin
- address
);
2210 // R_ARM_BASE_ABS: B(S) + A
2211 static inline typename
This::Status
2212 base_abs(unsigned char* view
,
2215 Base::rel32(view
, origin
);
2219 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2220 static inline typename
This::Status
2221 got_brel(unsigned char* view
,
2222 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2224 Base::rel32(view
, got_offset
);
2225 return This::STATUS_OKAY
;
2228 // R_ARM_GOT_PREL: GOT(S) + A - P
2229 static inline typename
This::Status
2230 got_prel(unsigned char *view
,
2231 Arm_address got_entry
,
2232 Arm_address address
)
2234 Base::rel32(view
, got_entry
- address
);
2235 return This::STATUS_OKAY
;
2238 // R_ARM_PLT32: (S + A) | T - P
2239 static inline typename
This::Status
2240 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2241 unsigned char *view
,
2242 const Sized_symbol
<32>* gsym
,
2243 const Arm_relobj
<big_endian
>* object
,
2245 const Symbol_value
<32>* psymval
,
2246 Arm_address address
,
2247 Arm_address thumb_bit
,
2248 bool is_weakly_undefined_without_plt
)
2250 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2251 object
, r_sym
, psymval
, address
, thumb_bit
,
2252 is_weakly_undefined_without_plt
);
2255 // R_ARM_XPC25: (S + A) | T - P
2256 static inline typename
This::Status
2257 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2258 unsigned char *view
,
2259 const Sized_symbol
<32>* gsym
,
2260 const Arm_relobj
<big_endian
>* object
,
2262 const Symbol_value
<32>* psymval
,
2263 Arm_address address
,
2264 Arm_address thumb_bit
,
2265 bool is_weakly_undefined_without_plt
)
2267 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2268 object
, r_sym
, psymval
, address
, thumb_bit
,
2269 is_weakly_undefined_without_plt
);
2272 // R_ARM_CALL: (S + A) | T - P
2273 static inline typename
This::Status
2274 call(const Relocate_info
<32, big_endian
>* relinfo
,
2275 unsigned char *view
,
2276 const Sized_symbol
<32>* gsym
,
2277 const Arm_relobj
<big_endian
>* object
,
2279 const Symbol_value
<32>* psymval
,
2280 Arm_address address
,
2281 Arm_address thumb_bit
,
2282 bool is_weakly_undefined_without_plt
)
2284 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2285 object
, r_sym
, psymval
, address
, thumb_bit
,
2286 is_weakly_undefined_without_plt
);
2289 // R_ARM_JUMP24: (S + A) | T - P
2290 static inline typename
This::Status
2291 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2292 unsigned char *view
,
2293 const Sized_symbol
<32>* gsym
,
2294 const Arm_relobj
<big_endian
>* object
,
2296 const Symbol_value
<32>* psymval
,
2297 Arm_address address
,
2298 Arm_address thumb_bit
,
2299 bool is_weakly_undefined_without_plt
)
2301 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2302 object
, r_sym
, psymval
, address
, thumb_bit
,
2303 is_weakly_undefined_without_plt
);
2306 // R_ARM_PREL: (S + A) | T - P
2307 static inline typename
This::Status
2308 prel31(unsigned char *view
,
2309 const Sized_relobj
<32, big_endian
>* object
,
2310 const Symbol_value
<32>* psymval
,
2311 Arm_address address
,
2312 Arm_address thumb_bit
)
2314 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2315 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2316 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2317 Valtype addend
= utils::sign_extend
<31>(val
);
2318 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2319 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2320 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2321 return (utils::has_overflow
<31>(x
) ?
2322 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2325 // R_ARM_MOVW_ABS_NC: (S + A) | T
2326 static inline typename
This::Status
2327 movw_abs_nc(unsigned char *view
,
2328 const Sized_relobj
<32, big_endian
>* object
,
2329 const Symbol_value
<32>* psymval
,
2330 Arm_address thumb_bit
)
2332 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2333 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2334 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2335 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2336 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2337 val
= This::insert_val_arm_movw_movt(val
, x
);
2338 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2339 return This::STATUS_OKAY
;
2342 // R_ARM_MOVT_ABS: S + A
2343 static inline typename
This::Status
2344 movt_abs(unsigned char *view
,
2345 const Sized_relobj
<32, big_endian
>* object
,
2346 const Symbol_value
<32>* psymval
)
2348 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2349 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2350 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2351 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2352 Valtype x
= psymval
->value(object
, addend
) >> 16;
2353 val
= This::insert_val_arm_movw_movt(val
, x
);
2354 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2355 return This::STATUS_OKAY
;
2358 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2359 static inline typename
This::Status
2360 thm_movw_abs_nc(unsigned char *view
,
2361 const Sized_relobj
<32, big_endian
>* object
,
2362 const Symbol_value
<32>* psymval
,
2363 Arm_address thumb_bit
)
2365 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2366 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2367 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2368 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2369 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2370 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2371 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2372 val
= This::insert_val_thumb_movw_movt(val
, x
);
2373 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2374 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2375 return This::STATUS_OKAY
;
2378 // R_ARM_THM_MOVT_ABS: S + A
2379 static inline typename
This::Status
2380 thm_movt_abs(unsigned char *view
,
2381 const Sized_relobj
<32, big_endian
>* object
,
2382 const Symbol_value
<32>* psymval
)
2384 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2385 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2386 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2387 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2388 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2389 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2390 Reltype x
= psymval
->value(object
, addend
) >> 16;
2391 val
= This::insert_val_thumb_movw_movt(val
, x
);
2392 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2393 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2394 return This::STATUS_OKAY
;
2397 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2398 static inline typename
This::Status
2399 movw_prel_nc(unsigned char *view
,
2400 const Sized_relobj
<32, big_endian
>* object
,
2401 const Symbol_value
<32>* psymval
,
2402 Arm_address address
,
2403 Arm_address thumb_bit
)
2405 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2406 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2407 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2408 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2409 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2410 val
= This::insert_val_arm_movw_movt(val
, x
);
2411 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2412 return This::STATUS_OKAY
;
2415 // R_ARM_MOVT_PREL: S + A - P
2416 static inline typename
This::Status
2417 movt_prel(unsigned char *view
,
2418 const Sized_relobj
<32, big_endian
>* object
,
2419 const Symbol_value
<32>* psymval
,
2420 Arm_address address
)
2422 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2423 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2424 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2425 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2426 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2427 val
= This::insert_val_arm_movw_movt(val
, x
);
2428 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2429 return This::STATUS_OKAY
;
2432 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2433 static inline typename
This::Status
2434 thm_movw_prel_nc(unsigned char *view
,
2435 const Sized_relobj
<32, big_endian
>* object
,
2436 const Symbol_value
<32>* psymval
,
2437 Arm_address address
,
2438 Arm_address thumb_bit
)
2440 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2441 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2442 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2443 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2444 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2445 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2446 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2447 val
= This::insert_val_thumb_movw_movt(val
, x
);
2448 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2449 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2450 return This::STATUS_OKAY
;
2453 // R_ARM_THM_MOVT_PREL: S + A - P
2454 static inline typename
This::Status
2455 thm_movt_prel(unsigned char *view
,
2456 const Sized_relobj
<32, big_endian
>* object
,
2457 const Symbol_value
<32>* psymval
,
2458 Arm_address address
)
2460 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2461 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2462 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2463 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2464 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2465 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2466 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2467 val
= This::insert_val_thumb_movw_movt(val
, x
);
2468 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2469 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2470 return This::STATUS_OKAY
;
2474 // Relocate ARM long branches. This handles relocation types
2475 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2476 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2477 // undefined and we do not use PLT in this relocation. In such a case,
2478 // the branch is converted into an NOP.
2480 template<bool big_endian
>
2481 typename Arm_relocate_functions
<big_endian
>::Status
2482 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2483 unsigned int r_type
,
2484 const Relocate_info
<32, big_endian
>* relinfo
,
2485 unsigned char *view
,
2486 const Sized_symbol
<32>* gsym
,
2487 const Arm_relobj
<big_endian
>* object
,
2489 const Symbol_value
<32>* psymval
,
2490 Arm_address address
,
2491 Arm_address thumb_bit
,
2492 bool is_weakly_undefined_without_plt
)
2494 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2495 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2496 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2498 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2499 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2500 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2501 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2502 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2503 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2504 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2506 // Check that the instruction is valid.
2507 if (r_type
== elfcpp::R_ARM_CALL
)
2509 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2510 return This::STATUS_BAD_RELOC
;
2512 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2514 if (!insn_is_b
&& !insn_is_cond_bl
)
2515 return This::STATUS_BAD_RELOC
;
2517 else if (r_type
== elfcpp::R_ARM_PLT32
)
2519 if (!insn_is_any_branch
)
2520 return This::STATUS_BAD_RELOC
;
2522 else if (r_type
== elfcpp::R_ARM_XPC25
)
2524 // FIXME: AAELF document IH0044C does not say much about it other
2525 // than it being obsolete.
2526 if (!insn_is_any_branch
)
2527 return This::STATUS_BAD_RELOC
;
2532 // A branch to an undefined weak symbol is turned into a jump to
2533 // the next instruction unless a PLT entry will be created.
2534 // Do the same for local undefined symbols.
2535 // The jump to the next instruction is optimized as a NOP depending
2536 // on the architecture.
2537 const Target_arm
<big_endian
>* arm_target
=
2538 Target_arm
<big_endian
>::default_target();
2539 if (is_weakly_undefined_without_plt
)
2541 Valtype cond
= val
& 0xf0000000U
;
2542 if (arm_target
->may_use_arm_nop())
2543 val
= cond
| 0x0320f000;
2545 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2546 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2547 return This::STATUS_OKAY
;
2550 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2551 Valtype branch_target
= psymval
->value(object
, addend
);
2552 int32_t branch_offset
= branch_target
- address
;
2554 // We need a stub if the branch offset is too large or if we need
2556 bool may_use_blx
= arm_target
->may_use_blx();
2557 Reloc_stub
* stub
= NULL
;
2558 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2559 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2560 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2562 Stub_type stub_type
=
2563 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2565 if (stub_type
!= arm_stub_none
)
2567 Stub_table
<big_endian
>* stub_table
=
2568 object
->stub_table(relinfo
->data_shndx
);
2569 gold_assert(stub_table
!= NULL
);
2571 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2572 stub
= stub_table
->find_reloc_stub(stub_key
);
2573 gold_assert(stub
!= NULL
);
2574 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2575 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2576 branch_offset
= branch_target
- address
;
2577 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2578 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2582 // At this point, if we still need to switch mode, the instruction
2583 // must either be a BLX or a BL that can be converted to a BLX.
2587 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2588 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2591 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2592 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2593 return (utils::has_overflow
<26>(branch_offset
)
2594 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2597 // Relocate THUMB long branches. This handles relocation types
2598 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2599 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2600 // undefined and we do not use PLT in this relocation. In such a case,
2601 // the branch is converted into an NOP.
2603 template<bool big_endian
>
2604 typename Arm_relocate_functions
<big_endian
>::Status
2605 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2606 unsigned int r_type
,
2607 const Relocate_info
<32, big_endian
>* relinfo
,
2608 unsigned char *view
,
2609 const Sized_symbol
<32>* gsym
,
2610 const Arm_relobj
<big_endian
>* object
,
2612 const Symbol_value
<32>* psymval
,
2613 Arm_address address
,
2614 Arm_address thumb_bit
,
2615 bool is_weakly_undefined_without_plt
)
2617 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2618 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2619 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2620 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2622 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2624 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2625 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2627 // Check that the instruction is valid.
2628 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2630 if (!is_bl_insn
&& !is_blx_insn
)
2631 return This::STATUS_BAD_RELOC
;
2633 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2635 // This cannot be a BLX.
2637 return This::STATUS_BAD_RELOC
;
2639 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2641 // Check for Thumb to Thumb call.
2643 return This::STATUS_BAD_RELOC
;
2646 gold_warning(_("%s: Thumb BLX instruction targets "
2647 "thumb function '%s'."),
2648 object
->name().c_str(),
2649 (gsym
? gsym
->name() : "(local)"));
2650 // Convert BLX to BL.
2651 lower_insn
|= 0x1000U
;
2657 // A branch to an undefined weak symbol is turned into a jump to
2658 // the next instruction unless a PLT entry will be created.
2659 // The jump to the next instruction is optimized as a NOP.W for
2660 // Thumb-2 enabled architectures.
2661 const Target_arm
<big_endian
>* arm_target
=
2662 Target_arm
<big_endian
>::default_target();
2663 if (is_weakly_undefined_without_plt
)
2665 if (arm_target
->may_use_thumb2_nop())
2667 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2668 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2672 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2673 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2675 return This::STATUS_OKAY
;
2678 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2679 // with Thumb-1) involving the J1 and J2 bits.
2680 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
2681 uint32_t upper
= upper_insn
& 0x3ff;
2682 uint32_t lower
= lower_insn
& 0x7ff;
2683 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
2684 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
2685 uint32_t i1
= j1
^ s
? 0 : 1;
2686 uint32_t i2
= j2
^ s
? 0 : 1;
2688 int32_t addend
= (i1
<< 23) | (i2
<< 22) | (upper
<< 12) | (lower
<< 1);
2690 addend
= (addend
| ((s
? 0 : 1) << 24)) - (1 << 24);
2692 Arm_address branch_target
= psymval
->value(object
, addend
);
2693 int32_t branch_offset
= branch_target
- address
;
2695 // We need a stub if the branch offset is too large or if we need
2697 bool may_use_blx
= arm_target
->may_use_blx();
2698 bool thumb2
= arm_target
->using_thumb2();
2700 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2701 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2703 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2704 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2705 || ((thumb_bit
== 0)
2706 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2707 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2709 Stub_type stub_type
=
2710 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2712 if (stub_type
!= arm_stub_none
)
2714 Stub_table
<big_endian
>* stub_table
=
2715 object
->stub_table(relinfo
->data_shndx
);
2716 gold_assert(stub_table
!= NULL
);
2718 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2719 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2720 gold_assert(stub
!= NULL
);
2721 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2722 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2723 branch_offset
= branch_target
- address
;
2727 // At this point, if we still need to switch mode, the instruction
2728 // must either be a BLX or a BL that can be converted to a BLX.
2731 gold_assert(may_use_blx
2732 && (r_type
== elfcpp::R_ARM_THM_CALL
2733 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2734 // Make sure this is a BLX.
2735 lower_insn
&= ~0x1000U
;
2739 // Make sure this is a BL.
2740 lower_insn
|= 0x1000U
;
2743 uint32_t reloc_sign
= (branch_offset
< 0) ? 1 : 0;
2744 uint32_t relocation
= static_cast<uint32_t>(branch_offset
);
2746 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2747 // For a BLX instruction, make sure that the relocation is rounded up
2748 // to a word boundary. This follows the semantics of the instruction
2749 // which specifies that bit 1 of the target address will come from bit
2750 // 1 of the base address.
2751 relocation
= (relocation
+ 2U) & ~3U;
2753 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2754 // We use the Thumb-2 encoding, which is safe even if dealing with
2755 // a Thumb-1 instruction by virtue of our overflow check above. */
2756 upper_insn
= (upper_insn
& ~0x7ffU
)
2757 | ((relocation
>> 12) & 0x3ffU
)
2758 | (reloc_sign
<< 10);
2759 lower_insn
= (lower_insn
& ~0x2fffU
)
2760 | (((!((relocation
>> 23) & 1U)) ^ reloc_sign
) << 13)
2761 | (((!((relocation
>> 22) & 1U)) ^ reloc_sign
) << 11)
2762 | ((relocation
>> 1) & 0x7ffU
);
2764 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2765 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2768 ? utils::has_overflow
<25>(relocation
)
2769 : utils::has_overflow
<23>(relocation
))
2770 ? This::STATUS_OVERFLOW
2771 : This::STATUS_OKAY
);
2774 // Get the GOT section, creating it if necessary.
2776 template<bool big_endian
>
2777 Output_data_got
<32, big_endian
>*
2778 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2780 if (this->got_
== NULL
)
2782 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2784 this->got_
= new Output_data_got
<32, big_endian
>();
2787 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2789 | elfcpp::SHF_WRITE
),
2790 this->got_
, false, true, true,
2793 // The old GNU linker creates a .got.plt section. We just
2794 // create another set of data in the .got section. Note that we
2795 // always create a PLT if we create a GOT, although the PLT
2797 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2798 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2800 | elfcpp::SHF_WRITE
),
2801 this->got_plt_
, false, false,
2804 // The first three entries are reserved.
2805 this->got_plt_
->set_current_data_size(3 * 4);
2807 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2808 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2809 Symbol_table::PREDEFINED
,
2811 0, 0, elfcpp::STT_OBJECT
,
2813 elfcpp::STV_HIDDEN
, 0,
2819 // Get the dynamic reloc section, creating it if necessary.
2821 template<bool big_endian
>
2822 typename Target_arm
<big_endian
>::Reloc_section
*
2823 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2825 if (this->rel_dyn_
== NULL
)
2827 gold_assert(layout
!= NULL
);
2828 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2829 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2830 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
2831 false, false, false);
2833 return this->rel_dyn_
;
2836 // Insn_template methods.
2838 // Return byte size of an instruction template.
2841 Insn_template::size() const
2843 switch (this->type())
2846 case THUMB16_SPECIAL_TYPE
:
2857 // Return alignment of an instruction template.
2860 Insn_template::alignment() const
2862 switch (this->type())
2865 case THUMB16_SPECIAL_TYPE
:
2876 // Stub_template methods.
2878 Stub_template::Stub_template(
2879 Stub_type type
, const Insn_template
* insns
,
2881 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2882 entry_in_thumb_mode_(false), relocs_()
2886 // Compute byte size and alignment of stub template.
2887 for (size_t i
= 0; i
< insn_count
; i
++)
2889 unsigned insn_alignment
= insns
[i
].alignment();
2890 size_t insn_size
= insns
[i
].size();
2891 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2892 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2893 switch (insns
[i
].type())
2895 case Insn_template::THUMB16_TYPE
:
2897 this->entry_in_thumb_mode_
= true;
2900 case Insn_template::THUMB32_TYPE
:
2901 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2902 this->relocs_
.push_back(Reloc(i
, offset
));
2904 this->entry_in_thumb_mode_
= true;
2907 case Insn_template::ARM_TYPE
:
2908 // Handle cases where the target is encoded within the
2910 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2911 this->relocs_
.push_back(Reloc(i
, offset
));
2914 case Insn_template::DATA_TYPE
:
2915 // Entry point cannot be data.
2916 gold_assert(i
!= 0);
2917 this->relocs_
.push_back(Reloc(i
, offset
));
2923 offset
+= insn_size
;
2925 this->size_
= offset
;
2930 // Template to implement do_write for a specific target endianity.
2932 template<bool big_endian
>
2934 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
2936 const Stub_template
* stub_template
= this->stub_template();
2937 const Insn_template
* insns
= stub_template
->insns();
2939 // FIXME: We do not handle BE8 encoding yet.
2940 unsigned char* pov
= view
;
2941 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2943 switch (insns
[i
].type())
2945 case Insn_template::THUMB16_TYPE
:
2946 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2948 case Insn_template::THUMB16_SPECIAL_TYPE
:
2949 elfcpp::Swap
<16, big_endian
>::writeval(
2951 this->thumb16_special(i
));
2953 case Insn_template::THUMB32_TYPE
:
2955 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2956 uint32_t lo
= insns
[i
].data() & 0xffff;
2957 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2958 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2961 case Insn_template::ARM_TYPE
:
2962 case Insn_template::DATA_TYPE
:
2963 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2968 pov
+= insns
[i
].size();
2970 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2973 // Reloc_stub::Key methods.
2975 // Dump a Key as a string for debugging.
2978 Reloc_stub::Key::name() const
2980 if (this->r_sym_
== invalid_index
)
2982 // Global symbol key name
2983 // <stub-type>:<symbol name>:<addend>.
2984 const std::string sym_name
= this->u_
.symbol
->name();
2985 // We need to print two hex number and two colons. So just add 100 bytes
2986 // to the symbol name size.
2987 size_t len
= sym_name
.size() + 100;
2988 char* buffer
= new char[len
];
2989 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2990 sym_name
.c_str(), this->addend_
);
2991 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2993 return std::string(buffer
);
2997 // local symbol key name
2998 // <stub-type>:<object>:<r_sym>:<addend>.
2999 const size_t len
= 200;
3001 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3002 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3003 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3004 return std::string(buffer
);
3008 // Reloc_stub methods.
3010 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3011 // LOCATION to DESTINATION.
3012 // This code is based on the arm_type_of_stub function in
3013 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3017 Reloc_stub::stub_type_for_reloc(
3018 unsigned int r_type
,
3019 Arm_address location
,
3020 Arm_address destination
,
3021 bool target_is_thumb
)
3023 Stub_type stub_type
= arm_stub_none
;
3025 // This is a bit ugly but we want to avoid using a templated class for
3026 // big and little endianities.
3028 bool should_force_pic_veneer
;
3031 if (parameters
->target().is_big_endian())
3033 const Target_arm
<true>* big_endian_target
=
3034 Target_arm
<true>::default_target();
3035 may_use_blx
= big_endian_target
->may_use_blx();
3036 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3037 thumb2
= big_endian_target
->using_thumb2();
3038 thumb_only
= big_endian_target
->using_thumb_only();
3042 const Target_arm
<false>* little_endian_target
=
3043 Target_arm
<false>::default_target();
3044 may_use_blx
= little_endian_target
->may_use_blx();
3045 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3046 thumb2
= little_endian_target
->using_thumb2();
3047 thumb_only
= little_endian_target
->using_thumb_only();
3050 int64_t branch_offset
= (int64_t)destination
- location
;
3052 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3054 // Handle cases where:
3055 // - this call goes too far (different Thumb/Thumb2 max
3057 // - it's a Thumb->Arm call and blx is not available, or it's a
3058 // Thumb->Arm branch (not bl). A stub is needed in this case.
3060 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3061 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3063 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3064 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3065 || ((!target_is_thumb
)
3066 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3067 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3069 if (target_is_thumb
)
3074 stub_type
= (parameters
->options().shared()
3075 || should_force_pic_veneer
)
3078 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3079 // V5T and above. Stub starts with ARM code, so
3080 // we must be able to switch mode before
3081 // reaching it, which is only possible for 'bl'
3082 // (ie R_ARM_THM_CALL relocation).
3083 ? arm_stub_long_branch_any_thumb_pic
3084 // On V4T, use Thumb code only.
3085 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3089 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3090 ? arm_stub_long_branch_any_any
// V5T and above.
3091 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3095 stub_type
= (parameters
->options().shared()
3096 || should_force_pic_veneer
)
3097 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3098 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3105 // FIXME: We should check that the input section is from an
3106 // object that has interwork enabled.
3108 stub_type
= (parameters
->options().shared()
3109 || should_force_pic_veneer
)
3112 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3113 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3114 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3118 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3119 ? arm_stub_long_branch_any_any
// V5T and above.
3120 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3122 // Handle v4t short branches.
3123 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3124 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3125 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3126 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3130 else if (r_type
== elfcpp::R_ARM_CALL
3131 || r_type
== elfcpp::R_ARM_JUMP24
3132 || r_type
== elfcpp::R_ARM_PLT32
)
3134 if (target_is_thumb
)
3138 // FIXME: We should check that the input section is from an
3139 // object that has interwork enabled.
3141 // We have an extra 2-bytes reach because of
3142 // the mode change (bit 24 (H) of BLX encoding).
3143 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3144 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3145 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3146 || (r_type
== elfcpp::R_ARM_JUMP24
)
3147 || (r_type
== elfcpp::R_ARM_PLT32
))
3149 stub_type
= (parameters
->options().shared()
3150 || should_force_pic_veneer
)
3153 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3154 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3158 ? arm_stub_long_branch_any_any
// V5T and above.
3159 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3165 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3166 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3168 stub_type
= (parameters
->options().shared()
3169 || should_force_pic_veneer
)
3170 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3171 : arm_stub_long_branch_any_any
; /// non-PIC.
3179 // Cortex_a8_stub methods.
3181 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3182 // I is the position of the instruction template in the stub template.
3185 Cortex_a8_stub::do_thumb16_special(size_t i
)
3187 // The only use of this is to copy condition code from a conditional
3188 // branch being worked around to the corresponding conditional branch in
3190 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3192 uint16_t data
= this->stub_template()->insns()[i
].data();
3193 gold_assert((data
& 0xff00U
) == 0xd000U
);
3194 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3198 // Stub_factory methods.
3200 Stub_factory::Stub_factory()
3202 // The instruction template sequences are declared as static
3203 // objects and initialized first time the constructor runs.
3205 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3206 // to reach the stub if necessary.
3207 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3209 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3210 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3211 // dcd R_ARM_ABS32(X)
3214 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3216 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3218 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3219 Insn_template::arm_insn(0xe12fff1c), // bx ip
3220 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3221 // dcd R_ARM_ABS32(X)
3224 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3225 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3227 Insn_template::thumb16_insn(0xb401), // push {r0}
3228 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3229 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3230 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3231 Insn_template::thumb16_insn(0x4760), // bx ip
3232 Insn_template::thumb16_insn(0xbf00), // nop
3233 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3234 // dcd R_ARM_ABS32(X)
3237 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3239 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3241 Insn_template::thumb16_insn(0x4778), // bx pc
3242 Insn_template::thumb16_insn(0x46c0), // nop
3243 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3244 Insn_template::arm_insn(0xe12fff1c), // bx ip
3245 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3246 // dcd R_ARM_ABS32(X)
3249 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3251 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3253 Insn_template::thumb16_insn(0x4778), // bx pc
3254 Insn_template::thumb16_insn(0x46c0), // nop
3255 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3256 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3257 // dcd R_ARM_ABS32(X)
3260 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3261 // one, when the destination is close enough.
3262 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3264 Insn_template::thumb16_insn(0x4778), // bx pc
3265 Insn_template::thumb16_insn(0x46c0), // nop
3266 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3269 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3270 // blx to reach the stub if necessary.
3271 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3273 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3274 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3275 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3276 // dcd R_ARM_REL32(X-4)
3279 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3280 // blx to reach the stub if necessary. We can not add into pc;
3281 // it is not guaranteed to mode switch (different in ARMv6 and
3283 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3285 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3286 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3287 Insn_template::arm_insn(0xe12fff1c), // bx ip
3288 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3289 // dcd R_ARM_REL32(X)
3292 // V4T ARM -> ARM long branch stub, PIC.
3293 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3295 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3296 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3297 Insn_template::arm_insn(0xe12fff1c), // bx ip
3298 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3299 // dcd R_ARM_REL32(X)
3302 // V4T Thumb -> ARM long branch stub, PIC.
3303 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3305 Insn_template::thumb16_insn(0x4778), // bx pc
3306 Insn_template::thumb16_insn(0x46c0), // nop
3307 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3308 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3309 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3310 // dcd R_ARM_REL32(X)
3313 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3315 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3317 Insn_template::thumb16_insn(0xb401), // push {r0}
3318 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3319 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3320 Insn_template::thumb16_insn(0x4484), // add ip, r0
3321 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3322 Insn_template::thumb16_insn(0x4760), // bx ip
3323 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3324 // dcd R_ARM_REL32(X)
3327 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3329 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3331 Insn_template::thumb16_insn(0x4778), // bx pc
3332 Insn_template::thumb16_insn(0x46c0), // nop
3333 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3334 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3335 Insn_template::arm_insn(0xe12fff1c), // bx ip
3336 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3337 // dcd R_ARM_REL32(X)
3340 // Cortex-A8 erratum-workaround stubs.
3342 // Stub used for conditional branches (which may be beyond +/-1MB away,
3343 // so we can't use a conditional branch to reach this stub).
3350 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3352 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3353 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3354 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3358 // Stub used for b.w and bl.w instructions.
3360 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3362 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3365 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3367 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3370 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3371 // instruction (which switches to ARM mode) to point to this stub. Jump to
3372 // the real destination using an ARM-mode branch.
3373 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3375 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3378 // Fill in the stub template look-up table. Stub templates are constructed
3379 // per instance of Stub_factory for fast look-up without locking
3380 // in a thread-enabled environment.
3382 this->stub_templates_
[arm_stub_none
] =
3383 new Stub_template(arm_stub_none
, NULL
, 0);
3385 #define DEF_STUB(x) \
3389 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3390 Stub_type type = arm_stub_##x; \
3391 this->stub_templates_[type] = \
3392 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3400 // Stub_table methods.
3402 // Removel all Cortex-A8 stub.
3404 template<bool big_endian
>
3406 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
3408 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3409 p
!= this->cortex_a8_stubs_
.end();
3412 this->cortex_a8_stubs_
.clear();
3415 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3417 template<bool big_endian
>
3419 Stub_table
<big_endian
>::relocate_stub(
3421 const Relocate_info
<32, big_endian
>* relinfo
,
3422 Target_arm
<big_endian
>* arm_target
,
3423 Output_section
* output_section
,
3424 unsigned char* view
,
3425 Arm_address address
,
3426 section_size_type view_size
)
3428 const Stub_template
* stub_template
= stub
->stub_template();
3429 if (stub_template
->reloc_count() != 0)
3431 // Adjust view to cover the stub only.
3432 section_size_type offset
= stub
->offset();
3433 section_size_type stub_size
= stub_template
->size();
3434 gold_assert(offset
+ stub_size
<= view_size
);
3436 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
3437 address
+ offset
, stub_size
);
3441 // Relocate all stubs in this stub table.
3443 template<bool big_endian
>
3445 Stub_table
<big_endian
>::relocate_stubs(
3446 const Relocate_info
<32, big_endian
>* relinfo
,
3447 Target_arm
<big_endian
>* arm_target
,
3448 Output_section
* output_section
,
3449 unsigned char* view
,
3450 Arm_address address
,
3451 section_size_type view_size
)
3453 // If we are passed a view bigger than the stub table's. we need to
3455 gold_assert(address
== this->address()
3457 == static_cast<section_size_type
>(this->data_size())));
3459 // Relocate all relocation stubs.
3460 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3461 p
!= this->reloc_stubs_
.end();
3463 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3464 address
, view_size
);
3466 // Relocate all Cortex-A8 stubs.
3467 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3468 p
!= this->cortex_a8_stubs_
.end();
3470 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3471 address
, view_size
);
3474 // Write out the stubs to file.
3476 template<bool big_endian
>
3478 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3480 off_t offset
= this->offset();
3481 const section_size_type oview_size
=
3482 convert_to_section_size_type(this->data_size());
3483 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3485 // Write relocation stubs.
3486 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3487 p
!= this->reloc_stubs_
.end();
3490 Reloc_stub
* stub
= p
->second
;
3491 Arm_address address
= this->address() + stub
->offset();
3493 == align_address(address
,
3494 stub
->stub_template()->alignment()));
3495 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3499 // Write Cortex-A8 stubs.
3500 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3501 p
!= this->cortex_a8_stubs_
.end();
3504 Cortex_a8_stub
* stub
= p
->second
;
3505 Arm_address address
= this->address() + stub
->offset();
3507 == align_address(address
,
3508 stub
->stub_template()->alignment()));
3509 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3513 of
->write_output_view(this->offset(), oview_size
, oview
);
3516 // Update the data size and address alignment of the stub table at the end
3517 // of a relaxation pass. Return true if either the data size or the
3518 // alignment changed in this relaxation pass.
3520 template<bool big_endian
>
3522 Stub_table
<big_endian
>::update_data_size_and_addralign()
3525 unsigned addralign
= 1;
3527 // Go over all stubs in table to compute data size and address alignment.
3529 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3530 p
!= this->reloc_stubs_
.end();
3533 const Stub_template
* stub_template
= p
->second
->stub_template();
3534 addralign
= std::max(addralign
, stub_template
->alignment());
3535 size
= (align_address(size
, stub_template
->alignment())
3536 + stub_template
->size());
3539 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3540 p
!= this->cortex_a8_stubs_
.end();
3543 const Stub_template
* stub_template
= p
->second
->stub_template();
3544 addralign
= std::max(addralign
, stub_template
->alignment());
3545 size
= (align_address(size
, stub_template
->alignment())
3546 + stub_template
->size());
3549 // Check if either data size or alignment changed in this pass.
3550 // Update prev_data_size_ and prev_addralign_. These will be used
3551 // as the current data size and address alignment for the next pass.
3552 bool changed
= size
!= this->prev_data_size_
;
3553 this->prev_data_size_
= size
;
3555 if (addralign
!= this->prev_addralign_
)
3557 this->prev_addralign_
= addralign
;
3562 // Finalize the stubs. This sets the offsets of the stubs within the stub
3563 // table. It also marks all input sections needing Cortex-A8 workaround.
3565 template<bool big_endian
>
3567 Stub_table
<big_endian
>::finalize_stubs()
3570 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3571 p
!= this->reloc_stubs_
.end();
3574 Reloc_stub
* stub
= p
->second
;
3575 const Stub_template
* stub_template
= stub
->stub_template();
3576 uint64_t stub_addralign
= stub_template
->alignment();
3577 off
= align_address(off
, stub_addralign
);
3578 stub
->set_offset(off
);
3579 off
+= stub_template
->size();
3582 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3583 p
!= this->cortex_a8_stubs_
.end();
3586 Cortex_a8_stub
* stub
= p
->second
;
3587 const Stub_template
* stub_template
= stub
->stub_template();
3588 uint64_t stub_addralign
= stub_template
->alignment();
3589 off
= align_address(off
, stub_addralign
);
3590 stub
->set_offset(off
);
3591 off
+= stub_template
->size();
3593 // Mark input section so that we can determine later if a code section
3594 // needs the Cortex-A8 workaround quickly.
3595 Arm_relobj
<big_endian
>* arm_relobj
=
3596 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
3597 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
3600 gold_assert(off
<= this->prev_data_size_
);
3603 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3604 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3605 // of the address range seen by the linker.
3607 template<bool big_endian
>
3609 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
3610 Target_arm
<big_endian
>* arm_target
,
3611 unsigned char* view
,
3612 Arm_address view_address
,
3613 section_size_type view_size
)
3615 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3616 for (Cortex_a8_stub_list::const_iterator p
=
3617 this->cortex_a8_stubs_
.lower_bound(view_address
);
3618 ((p
!= this->cortex_a8_stubs_
.end())
3619 && (p
->first
< (view_address
+ view_size
)));
3622 // We do not store the THUMB bit in the LSB of either the branch address
3623 // or the stub offset. There is no need to strip the LSB.
3624 Arm_address branch_address
= p
->first
;
3625 const Cortex_a8_stub
* stub
= p
->second
;
3626 Arm_address stub_address
= this->address() + stub
->offset();
3628 // Offset of the branch instruction relative to this view.
3629 section_size_type offset
=
3630 convert_to_section_size_type(branch_address
- view_address
);
3631 gold_assert((offset
+ 4) <= view_size
);
3633 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
3634 view
+ offset
, branch_address
);
3638 // Arm_input_section methods.
3640 // Initialize an Arm_input_section.
3642 template<bool big_endian
>
3644 Arm_input_section
<big_endian
>::init()
3646 Relobj
* relobj
= this->relobj();
3647 unsigned int shndx
= this->shndx();
3649 // Cache these to speed up size and alignment queries. It is too slow
3650 // to call section_addraglin and section_size every time.
3651 this->original_addralign_
= relobj
->section_addralign(shndx
);
3652 this->original_size_
= relobj
->section_size(shndx
);
3654 // We want to make this look like the original input section after
3655 // output sections are finalized.
3656 Output_section
* os
= relobj
->output_section(shndx
);
3657 off_t offset
= relobj
->output_section_offset(shndx
);
3658 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3659 this->set_address(os
->address() + offset
);
3660 this->set_file_offset(os
->offset() + offset
);
3662 this->set_current_data_size(this->original_size_
);
3663 this->finalize_data_size();
3666 template<bool big_endian
>
3668 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3670 // We have to write out the original section content.
3671 section_size_type section_size
;
3672 const unsigned char* section_contents
=
3673 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3674 of
->write(this->offset(), section_contents
, section_size
);
3676 // If this owns a stub table and it is not empty, write it.
3677 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3678 this->stub_table_
->write(of
);
3681 // Finalize data size.
3683 template<bool big_endian
>
3685 Arm_input_section
<big_endian
>::set_final_data_size()
3687 // If this owns a stub table, finalize its data size as well.
3688 if (this->is_stub_table_owner())
3690 uint64_t address
= this->address();
3692 // The stub table comes after the original section contents.
3693 address
+= this->original_size_
;
3694 address
= align_address(address
, this->stub_table_
->addralign());
3695 off_t offset
= this->offset() + (address
- this->address());
3696 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3697 address
+= this->stub_table_
->data_size();
3698 gold_assert(address
== this->address() + this->current_data_size());
3701 this->set_data_size(this->current_data_size());
3704 // Reset address and file offset.
3706 template<bool big_endian
>
3708 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3710 // Size of the original input section contents.
3711 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3713 // If this is a stub table owner, account for the stub table size.
3714 if (this->is_stub_table_owner())
3716 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3718 // Reset the stub table's address and file offset. The
3719 // current data size for child will be updated after that.
3720 stub_table_
->reset_address_and_file_offset();
3721 off
= align_address(off
, stub_table_
->addralign());
3722 off
+= stub_table
->current_data_size();
3725 this->set_current_data_size(off
);
3728 // Arm_output_section methods.
3730 // Create a stub group for input sections from BEGIN to END. OWNER
3731 // points to the input section to be the owner a new stub table.
3733 template<bool big_endian
>
3735 Arm_output_section
<big_endian
>::create_stub_group(
3736 Input_section_list::const_iterator begin
,
3737 Input_section_list::const_iterator end
,
3738 Input_section_list::const_iterator owner
,
3739 Target_arm
<big_endian
>* target
,
3740 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3742 // Currently we convert ordinary input sections into relaxed sections only
3743 // at this point but we may want to support creating relaxed input section
3744 // very early. So we check here to see if owner is already a relaxed
3747 Arm_input_section
<big_endian
>* arm_input_section
;
3748 if (owner
->is_relaxed_input_section())
3751 Arm_input_section
<big_endian
>::as_arm_input_section(
3752 owner
->relaxed_input_section());
3756 gold_assert(owner
->is_input_section());
3757 // Create a new relaxed input section.
3759 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3760 new_relaxed_sections
->push_back(arm_input_section
);
3763 // Create a stub table.
3764 Stub_table
<big_endian
>* stub_table
=
3765 target
->new_stub_table(arm_input_section
);
3767 arm_input_section
->set_stub_table(stub_table
);
3769 Input_section_list::const_iterator p
= begin
;
3770 Input_section_list::const_iterator prev_p
;
3772 // Look for input sections or relaxed input sections in [begin ... end].
3775 if (p
->is_input_section() || p
->is_relaxed_input_section())
3777 // The stub table information for input sections live
3778 // in their objects.
3779 Arm_relobj
<big_endian
>* arm_relobj
=
3780 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3781 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3785 while (prev_p
!= end
);
3788 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3789 // of stub groups. We grow a stub group by adding input section until the
3790 // size is just below GROUP_SIZE. The last input section will be converted
3791 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3792 // input section after the stub table, effectively double the group size.
3794 // This is similar to the group_sections() function in elf32-arm.c but is
3795 // implemented differently.
3797 template<bool big_endian
>
3799 Arm_output_section
<big_endian
>::group_sections(
3800 section_size_type group_size
,
3801 bool stubs_always_after_branch
,
3802 Target_arm
<big_endian
>* target
)
3804 // We only care about sections containing code.
3805 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3808 // States for grouping.
3811 // No group is being built.
3813 // A group is being built but the stub table is not found yet.
3814 // We keep group a stub group until the size is just under GROUP_SIZE.
3815 // The last input section in the group will be used as the stub table.
3816 FINDING_STUB_SECTION
,
3817 // A group is being built and we have already found a stub table.
3818 // We enter this state to grow a stub group by adding input section
3819 // after the stub table. This effectively doubles the group size.
3823 // Any newly created relaxed sections are stored here.
3824 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3826 State state
= NO_GROUP
;
3827 section_size_type off
= 0;
3828 section_size_type group_begin_offset
= 0;
3829 section_size_type group_end_offset
= 0;
3830 section_size_type stub_table_end_offset
= 0;
3831 Input_section_list::const_iterator group_begin
=
3832 this->input_sections().end();
3833 Input_section_list::const_iterator stub_table
=
3834 this->input_sections().end();
3835 Input_section_list::const_iterator group_end
= this->input_sections().end();
3836 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3837 p
!= this->input_sections().end();
3840 section_size_type section_begin_offset
=
3841 align_address(off
, p
->addralign());
3842 section_size_type section_end_offset
=
3843 section_begin_offset
+ p
->data_size();
3845 // Check to see if we should group the previously seens sections.
3851 case FINDING_STUB_SECTION
:
3852 // Adding this section makes the group larger than GROUP_SIZE.
3853 if (section_end_offset
- group_begin_offset
>= group_size
)
3855 if (stubs_always_after_branch
)
3857 gold_assert(group_end
!= this->input_sections().end());
3858 this->create_stub_group(group_begin
, group_end
, group_end
,
3859 target
, &new_relaxed_sections
);
3864 // But wait, there's more! Input sections up to
3865 // stub_group_size bytes after the stub table can be
3866 // handled by it too.
3867 state
= HAS_STUB_SECTION
;
3868 stub_table
= group_end
;
3869 stub_table_end_offset
= group_end_offset
;
3874 case HAS_STUB_SECTION
:
3875 // Adding this section makes the post stub-section group larger
3877 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3879 gold_assert(group_end
!= this->input_sections().end());
3880 this->create_stub_group(group_begin
, group_end
, stub_table
,
3881 target
, &new_relaxed_sections
);
3890 // If we see an input section and currently there is no group, start
3891 // a new one. Skip any empty sections.
3892 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3893 && (p
->relobj()->section_size(p
->shndx()) != 0))
3895 if (state
== NO_GROUP
)
3897 state
= FINDING_STUB_SECTION
;
3899 group_begin_offset
= section_begin_offset
;
3902 // Keep track of the last input section seen.
3904 group_end_offset
= section_end_offset
;
3907 off
= section_end_offset
;
3910 // Create a stub group for any ungrouped sections.
3911 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3913 gold_assert(group_end
!= this->input_sections().end());
3914 this->create_stub_group(group_begin
, group_end
,
3915 (state
== FINDING_STUB_SECTION
3918 target
, &new_relaxed_sections
);
3921 // Convert input section into relaxed input section in a batch.
3922 if (!new_relaxed_sections
.empty())
3923 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3925 // Update the section offsets
3926 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3928 Arm_relobj
<big_endian
>* arm_relobj
=
3929 Arm_relobj
<big_endian
>::as_arm_relobj(
3930 new_relaxed_sections
[i
]->relobj());
3931 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3932 // Tell Arm_relobj that this input section is converted.
3933 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3937 // Arm_relobj methods.
3939 // Scan relocations for stub generation.
3941 template<bool big_endian
>
3943 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3944 Target_arm
<big_endian
>* arm_target
,
3945 const Symbol_table
* symtab
,
3946 const Layout
* layout
)
3948 unsigned int shnum
= this->shnum();
3949 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3951 // Read the section headers.
3952 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3956 // To speed up processing, we set up hash tables for fast lookup of
3957 // input offsets to output addresses.
3958 this->initialize_input_to_output_maps();
3960 const Relobj::Output_sections
& out_sections(this->output_sections());
3962 Relocate_info
<32, big_endian
> relinfo
;
3963 relinfo
.symtab
= symtab
;
3964 relinfo
.layout
= layout
;
3965 relinfo
.object
= this;
3967 const unsigned char* p
= pshdrs
+ shdr_size
;
3968 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3970 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3972 unsigned int sh_type
= shdr
.get_sh_type();
3973 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3976 off_t sh_size
= shdr
.get_sh_size();
3980 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3981 if (index
>= this->shnum())
3983 // Ignore reloc section with bad info. This error will be
3984 // reported in the final link.
3988 Output_section
* os
= out_sections
[index
];
3990 || symtab
->is_section_folded(this, index
))
3992 // This relocation section is against a section which we
3993 // discarded or if the section is folded into another
3994 // section due to ICF.
3997 Arm_address output_offset
= this->get_output_section_offset(index
);
3999 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4001 // Ignore reloc section with unexpected symbol table. The
4002 // error will be reported in the final link.
4006 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
4007 sh_size
, true, false);
4009 unsigned int reloc_size
;
4010 if (sh_type
== elfcpp::SHT_REL
)
4011 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4013 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4015 if (reloc_size
!= shdr
.get_sh_entsize())
4017 // Ignore reloc section with unexpected entsize. The error
4018 // will be reported in the final link.
4022 size_t reloc_count
= sh_size
/ reloc_size
;
4023 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
4025 // Ignore reloc section with uneven size. The error will be
4026 // reported in the final link.
4030 gold_assert(output_offset
!= invalid_address
4031 || this->relocs_must_follow_section_writes());
4033 // Get the section contents. This does work for the case in which
4034 // we modify the contents of an input section. We need to pass the
4035 // output view under such circumstances.
4036 section_size_type input_view_size
= 0;
4037 const unsigned char* input_view
=
4038 this->section_contents(index
, &input_view_size
, false);
4040 relinfo
.reloc_shndx
= i
;
4041 relinfo
.data_shndx
= index
;
4042 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
4044 output_offset
== invalid_address
,
4050 // After we've done the relocations, we release the hash tables,
4051 // since we no longer need them.
4052 this->free_input_to_output_maps();
4055 // Count the local symbols. The ARM backend needs to know if a symbol
4056 // is a THUMB function or not. For global symbols, it is easy because
4057 // the Symbol object keeps the ELF symbol type. For local symbol it is
4058 // harder because we cannot access this information. So we override the
4059 // do_count_local_symbol in parent and scan local symbols to mark
4060 // THUMB functions. This is not the most efficient way but I do not want to
4061 // slow down other ports by calling a per symbol targer hook inside
4062 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4064 template<bool big_endian
>
4066 Arm_relobj
<big_endian
>::do_count_local_symbols(
4067 Stringpool_template
<char>* pool
,
4068 Stringpool_template
<char>* dynpool
)
4070 // We need to fix-up the values of any local symbols whose type are
4073 // Ask parent to count the local symbols.
4074 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
4075 const unsigned int loccount
= this->local_symbol_count();
4079 // Intialize the thumb function bit-vector.
4080 std::vector
<bool> empty_vector(loccount
, false);
4081 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
4083 // Read the symbol table section header.
4084 const unsigned int symtab_shndx
= this->symtab_shndx();
4085 elfcpp::Shdr
<32, big_endian
>
4086 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
4087 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
4089 // Read the local symbols.
4090 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
4091 gold_assert(loccount
== symtabshdr
.get_sh_info());
4092 off_t locsize
= loccount
* sym_size
;
4093 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
4094 locsize
, true, true);
4096 // Loop over the local symbols and mark any local symbols pointing
4097 // to THUMB functions.
4099 // Skip the first dummy symbol.
4101 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
4102 this->local_values();
4103 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
4105 elfcpp::Sym
<32, big_endian
> sym(psyms
);
4106 elfcpp::STT st_type
= sym
.get_st_type();
4107 Symbol_value
<32>& lv((*plocal_values
)[i
]);
4108 Arm_address input_value
= lv
.input_value();
4110 if (st_type
== elfcpp::STT_ARM_TFUNC
4111 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
4113 // This is a THUMB function. Mark this and canonicalize the
4114 // symbol value by setting LSB.
4115 this->local_symbol_is_thumb_function_
[i
] = true;
4116 if ((input_value
& 1) == 0)
4117 lv
.set_input_value(input_value
| 1);
4122 // Relocate sections.
4123 template<bool big_endian
>
4125 Arm_relobj
<big_endian
>::do_relocate_sections(
4126 const Symbol_table
* symtab
,
4127 const Layout
* layout
,
4128 const unsigned char* pshdrs
,
4129 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
4131 // Call parent to relocate sections.
4132 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
4135 // We do not generate stubs if doing a relocatable link.
4136 if (parameters
->options().relocatable())
4139 // Relocate stub tables.
4140 unsigned int shnum
= this->shnum();
4142 Target_arm
<big_endian
>* arm_target
=
4143 Target_arm
<big_endian
>::default_target();
4145 Relocate_info
<32, big_endian
> relinfo
;
4146 relinfo
.symtab
= symtab
;
4147 relinfo
.layout
= layout
;
4148 relinfo
.object
= this;
4150 for (unsigned int i
= 1; i
< shnum
; ++i
)
4152 Arm_input_section
<big_endian
>* arm_input_section
=
4153 arm_target
->find_arm_input_section(this, i
);
4155 if (arm_input_section
== NULL
4156 || !arm_input_section
->is_stub_table_owner()
4157 || arm_input_section
->stub_table()->empty())
4160 // We cannot discard a section if it owns a stub table.
4161 Output_section
* os
= this->output_section(i
);
4162 gold_assert(os
!= NULL
);
4164 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
4165 relinfo
.reloc_shdr
= NULL
;
4166 relinfo
.data_shndx
= i
;
4167 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
4169 gold_assert((*pviews
)[i
].view
!= NULL
);
4171 // We are passed the output section view. Adjust it to cover the
4173 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
4174 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
4175 && ((stub_table
->address() + stub_table
->data_size())
4176 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
4178 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
4179 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
4180 Arm_address address
= stub_table
->address();
4181 section_size_type view_size
= stub_table
->data_size();
4183 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
4188 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4191 template<bool big_endian
>
4192 Attributes_section_data
*
4193 read_arm_attributes_section(
4195 Read_symbols_data
*sd
)
4197 // Read the attributes section if there is one.
4198 // We read from the end because gas seems to put it near the end of
4199 // the section headers.
4200 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4201 const unsigned char *ps
=
4202 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
4203 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
4205 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4206 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
4208 section_offset_type section_offset
= shdr
.get_sh_offset();
4209 section_size_type section_size
=
4210 convert_to_section_size_type(shdr
.get_sh_size());
4211 File_view
* view
= object
->get_lasting_view(section_offset
,
4212 section_size
, true, false);
4213 return new Attributes_section_data(view
->data(), section_size
);
4219 // Read the symbol information.
4221 template<bool big_endian
>
4223 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4225 // Call parent class to read symbol information.
4226 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
4228 // Read processor-specific flags in ELF file header.
4229 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4230 elfcpp::Elf_sizes
<32>::ehdr_size
,
4232 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4233 this->processor_specific_flags_
= ehdr
.get_e_flags();
4234 this->attributes_section_data_
=
4235 read_arm_attributes_section
<big_endian
>(this, sd
);
4238 // Arm_dynobj methods.
4240 // Read the symbol information.
4242 template<bool big_endian
>
4244 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4246 // Call parent class to read symbol information.
4247 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
4249 // Read processor-specific flags in ELF file header.
4250 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4251 elfcpp::Elf_sizes
<32>::ehdr_size
,
4253 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4254 this->processor_specific_flags_
= ehdr
.get_e_flags();
4255 this->attributes_section_data_
=
4256 read_arm_attributes_section
<big_endian
>(this, sd
);
4259 // Stub_addend_reader methods.
4261 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4263 template<bool big_endian
>
4264 elfcpp::Elf_types
<32>::Elf_Swxword
4265 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
4266 unsigned int r_type
,
4267 const unsigned char* view
,
4268 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
4272 case elfcpp::R_ARM_CALL
:
4273 case elfcpp::R_ARM_JUMP24
:
4274 case elfcpp::R_ARM_PLT32
:
4276 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4277 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4278 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4279 return utils::sign_extend
<26>(val
<< 2);
4282 case elfcpp::R_ARM_THM_CALL
:
4283 case elfcpp::R_ARM_THM_JUMP24
:
4284 case elfcpp::R_ARM_THM_XPC22
:
4286 // Fetch the addend. We use the Thumb-2 encoding (backwards
4287 // compatible with Thumb-1) involving the J1 and J2 bits.
4288 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4289 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4290 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4291 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4293 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
4294 uint32_t upper
= upper_insn
& 0x3ff;
4295 uint32_t lower
= lower_insn
& 0x7ff;
4296 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
4297 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
4298 uint32_t i1
= j1
^ s
? 0 : 1;
4299 uint32_t i2
= j2
^ s
? 0 : 1;
4301 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
4302 | (upper
<< 12) | (lower
<< 1));
4305 case elfcpp::R_ARM_THM_JUMP19
:
4307 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4308 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
4309 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4310 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4312 // Reconstruct the top three bits and squish the two 11 bit pieces
4314 uint32_t S
= (upper_insn
& 0x0400) >> 10;
4315 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
4316 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
4318 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
4319 uint32_t lower
= (lower_insn
& 0x07ff);
4320 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
4328 // A class to handle the PLT data.
4330 template<bool big_endian
>
4331 class Output_data_plt_arm
: public Output_section_data
4334 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
4337 Output_data_plt_arm(Layout
*, Output_data_space
*);
4339 // Add an entry to the PLT.
4341 add_entry(Symbol
* gsym
);
4343 // Return the .rel.plt section data.
4344 const Reloc_section
*
4346 { return this->rel_
; }
4350 do_adjust_output_section(Output_section
* os
);
4352 // Write to a map file.
4354 do_print_to_mapfile(Mapfile
* mapfile
) const
4355 { mapfile
->print_output_data(this, _("** PLT")); }
4358 // Template for the first PLT entry.
4359 static const uint32_t first_plt_entry
[5];
4361 // Template for subsequent PLT entries.
4362 static const uint32_t plt_entry
[3];
4364 // Set the final size.
4366 set_final_data_size()
4368 this->set_data_size(sizeof(first_plt_entry
)
4369 + this->count_
* sizeof(plt_entry
));
4372 // Write out the PLT data.
4374 do_write(Output_file
*);
4376 // The reloc section.
4377 Reloc_section
* rel_
;
4378 // The .got.plt section.
4379 Output_data_space
* got_plt_
;
4380 // The number of PLT entries.
4381 unsigned int count_
;
4384 // Create the PLT section. The ordinary .got section is an argument,
4385 // since we need to refer to the start. We also create our own .got
4386 // section just for PLT entries.
4388 template<bool big_endian
>
4389 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
4390 Output_data_space
* got_plt
)
4391 : Output_section_data(4), got_plt_(got_plt
), count_(0)
4393 this->rel_
= new Reloc_section(false);
4394 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
4395 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
4399 template<bool big_endian
>
4401 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
4406 // Add an entry to the PLT.
4408 template<bool big_endian
>
4410 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
4412 gold_assert(!gsym
->has_plt_offset());
4414 // Note that when setting the PLT offset we skip the initial
4415 // reserved PLT entry.
4416 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
4417 + sizeof(first_plt_entry
));
4421 section_offset_type got_offset
= this->got_plt_
->current_data_size();
4423 // Every PLT entry needs a GOT entry which points back to the PLT
4424 // entry (this will be changed by the dynamic linker, normally
4425 // lazily when the function is called).
4426 this->got_plt_
->set_current_data_size(got_offset
+ 4);
4428 // Every PLT entry needs a reloc.
4429 gsym
->set_needs_dynsym_entry();
4430 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
4433 // Note that we don't need to save the symbol. The contents of the
4434 // PLT are independent of which symbols are used. The symbols only
4435 // appear in the relocations.
4439 // FIXME: This is not very flexible. Right now this has only been tested
4440 // on armv5te. If we are to support additional architecture features like
4441 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4443 // The first entry in the PLT.
4444 template<bool big_endian
>
4445 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
4447 0xe52de004, // str lr, [sp, #-4]!
4448 0xe59fe004, // ldr lr, [pc, #4]
4449 0xe08fe00e, // add lr, pc, lr
4450 0xe5bef008, // ldr pc, [lr, #8]!
4451 0x00000000, // &GOT[0] - .
4454 // Subsequent entries in the PLT.
4456 template<bool big_endian
>
4457 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
4459 0xe28fc600, // add ip, pc, #0xNN00000
4460 0xe28cca00, // add ip, ip, #0xNN000
4461 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4464 // Write out the PLT. This uses the hand-coded instructions above,
4465 // and adjusts them as needed. This is all specified by the arm ELF
4466 // Processor Supplement.
4468 template<bool big_endian
>
4470 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4472 const off_t offset
= this->offset();
4473 const section_size_type oview_size
=
4474 convert_to_section_size_type(this->data_size());
4475 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4477 const off_t got_file_offset
= this->got_plt_
->offset();
4478 const section_size_type got_size
=
4479 convert_to_section_size_type(this->got_plt_
->data_size());
4480 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4482 unsigned char* pov
= oview
;
4484 Arm_address plt_address
= this->address();
4485 Arm_address got_address
= this->got_plt_
->address();
4487 // Write first PLT entry. All but the last word are constants.
4488 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4489 / sizeof(plt_entry
[0]));
4490 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4491 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4492 // Last word in first PLT entry is &GOT[0] - .
4493 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4494 got_address
- (plt_address
+ 16));
4495 pov
+= sizeof(first_plt_entry
);
4497 unsigned char* got_pov
= got_view
;
4499 memset(got_pov
, 0, 12);
4502 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4503 unsigned int plt_offset
= sizeof(first_plt_entry
);
4504 unsigned int plt_rel_offset
= 0;
4505 unsigned int got_offset
= 12;
4506 const unsigned int count
= this->count_
;
4507 for (unsigned int i
= 0;
4510 pov
+= sizeof(plt_entry
),
4512 plt_offset
+= sizeof(plt_entry
),
4513 plt_rel_offset
+= rel_size
,
4516 // Set and adjust the PLT entry itself.
4517 int32_t offset
= ((got_address
+ got_offset
)
4518 - (plt_address
+ plt_offset
+ 8));
4520 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4521 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4522 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4523 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4524 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4525 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4526 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4528 // Set the entry in the GOT.
4529 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4532 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4533 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4535 of
->write_output_view(offset
, oview_size
, oview
);
4536 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4539 // Create a PLT entry for a global symbol.
4541 template<bool big_endian
>
4543 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
4546 if (gsym
->has_plt_offset())
4549 if (this->plt_
== NULL
)
4551 // Create the GOT sections first.
4552 this->got_section(symtab
, layout
);
4554 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
4555 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4557 | elfcpp::SHF_EXECINSTR
),
4558 this->plt_
, false, false, false, false);
4560 this->plt_
->add_entry(gsym
);
4563 // Report an unsupported relocation against a local symbol.
4565 template<bool big_endian
>
4567 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4568 Sized_relobj
<32, big_endian
>* object
,
4569 unsigned int r_type
)
4571 gold_error(_("%s: unsupported reloc %u against local symbol"),
4572 object
->name().c_str(), r_type
);
4575 // We are about to emit a dynamic relocation of type R_TYPE. If the
4576 // dynamic linker does not support it, issue an error. The GNU linker
4577 // only issues a non-PIC error for an allocated read-only section.
4578 // Here we know the section is allocated, but we don't know that it is
4579 // read-only. But we check for all the relocation types which the
4580 // glibc dynamic linker supports, so it seems appropriate to issue an
4581 // error even if the section is not read-only.
4583 template<bool big_endian
>
4585 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4586 unsigned int r_type
)
4590 // These are the relocation types supported by glibc for ARM.
4591 case elfcpp::R_ARM_RELATIVE
:
4592 case elfcpp::R_ARM_COPY
:
4593 case elfcpp::R_ARM_GLOB_DAT
:
4594 case elfcpp::R_ARM_JUMP_SLOT
:
4595 case elfcpp::R_ARM_ABS32
:
4596 case elfcpp::R_ARM_ABS32_NOI
:
4597 case elfcpp::R_ARM_PC24
:
4598 // FIXME: The following 3 types are not supported by Android's dynamic
4600 case elfcpp::R_ARM_TLS_DTPMOD32
:
4601 case elfcpp::R_ARM_TLS_DTPOFF32
:
4602 case elfcpp::R_ARM_TLS_TPOFF32
:
4606 // This prevents us from issuing more than one error per reloc
4607 // section. But we can still wind up issuing more than one
4608 // error per object file.
4609 if (this->issued_non_pic_error_
)
4611 object
->error(_("requires unsupported dynamic reloc; "
4612 "recompile with -fPIC"));
4613 this->issued_non_pic_error_
= true;
4616 case elfcpp::R_ARM_NONE
:
4621 // Scan a relocation for a local symbol.
4622 // FIXME: This only handles a subset of relocation types used by Android
4623 // on ARM v5te devices.
4625 template<bool big_endian
>
4627 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4630 Sized_relobj
<32, big_endian
>* object
,
4631 unsigned int data_shndx
,
4632 Output_section
* output_section
,
4633 const elfcpp::Rel
<32, big_endian
>& reloc
,
4634 unsigned int r_type
,
4635 const elfcpp::Sym
<32, big_endian
>&)
4637 r_type
= get_real_reloc_type(r_type
);
4640 case elfcpp::R_ARM_NONE
:
4643 case elfcpp::R_ARM_ABS32
:
4644 case elfcpp::R_ARM_ABS32_NOI
:
4645 // If building a shared library (or a position-independent
4646 // executable), we need to create a dynamic relocation for
4647 // this location. The relocation applied at link time will
4648 // apply the link-time value, so we flag the location with
4649 // an R_ARM_RELATIVE relocation so the dynamic loader can
4650 // relocate it easily.
4651 if (parameters
->options().output_is_position_independent())
4653 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4654 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4655 // If we are to add more other reloc types than R_ARM_ABS32,
4656 // we need to add check_non_pic(object, r_type) here.
4657 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4658 output_section
, data_shndx
,
4659 reloc
.get_r_offset());
4663 case elfcpp::R_ARM_REL32
:
4664 case elfcpp::R_ARM_THM_CALL
:
4665 case elfcpp::R_ARM_CALL
:
4666 case elfcpp::R_ARM_PREL31
:
4667 case elfcpp::R_ARM_JUMP24
:
4668 case elfcpp::R_ARM_PLT32
:
4669 case elfcpp::R_ARM_THM_ABS5
:
4670 case elfcpp::R_ARM_ABS8
:
4671 case elfcpp::R_ARM_ABS12
:
4672 case elfcpp::R_ARM_ABS16
:
4673 case elfcpp::R_ARM_BASE_ABS
:
4674 case elfcpp::R_ARM_MOVW_ABS_NC
:
4675 case elfcpp::R_ARM_MOVT_ABS
:
4676 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4677 case elfcpp::R_ARM_THM_MOVT_ABS
:
4678 case elfcpp::R_ARM_MOVW_PREL_NC
:
4679 case elfcpp::R_ARM_MOVT_PREL
:
4680 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4681 case elfcpp::R_ARM_THM_MOVT_PREL
:
4684 case elfcpp::R_ARM_GOTOFF32
:
4685 // We need a GOT section:
4686 target
->got_section(symtab
, layout
);
4689 case elfcpp::R_ARM_BASE_PREL
:
4690 // FIXME: What about this?
4693 case elfcpp::R_ARM_GOT_BREL
:
4694 case elfcpp::R_ARM_GOT_PREL
:
4696 // The symbol requires a GOT entry.
4697 Output_data_got
<32, big_endian
>* got
=
4698 target
->got_section(symtab
, layout
);
4699 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4700 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4702 // If we are generating a shared object, we need to add a
4703 // dynamic RELATIVE relocation for this symbol's GOT entry.
4704 if (parameters
->options().output_is_position_independent())
4706 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4707 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4708 rel_dyn
->add_local_relative(
4709 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4710 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4716 case elfcpp::R_ARM_TARGET1
:
4717 // This should have been mapped to another type already.
4719 case elfcpp::R_ARM_COPY
:
4720 case elfcpp::R_ARM_GLOB_DAT
:
4721 case elfcpp::R_ARM_JUMP_SLOT
:
4722 case elfcpp::R_ARM_RELATIVE
:
4723 // These are relocations which should only be seen by the
4724 // dynamic linker, and should never be seen here.
4725 gold_error(_("%s: unexpected reloc %u in object file"),
4726 object
->name().c_str(), r_type
);
4730 unsupported_reloc_local(object
, r_type
);
4735 // Report an unsupported relocation against a global symbol.
4737 template<bool big_endian
>
4739 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4740 Sized_relobj
<32, big_endian
>* object
,
4741 unsigned int r_type
,
4744 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4745 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4748 // Scan a relocation for a global symbol.
4749 // FIXME: This only handles a subset of relocation types used by Android
4750 // on ARM v5te devices.
4752 template<bool big_endian
>
4754 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4757 Sized_relobj
<32, big_endian
>* object
,
4758 unsigned int data_shndx
,
4759 Output_section
* output_section
,
4760 const elfcpp::Rel
<32, big_endian
>& reloc
,
4761 unsigned int r_type
,
4764 r_type
= get_real_reloc_type(r_type
);
4767 case elfcpp::R_ARM_NONE
:
4770 case elfcpp::R_ARM_ABS32
:
4771 case elfcpp::R_ARM_ABS32_NOI
:
4773 // Make a dynamic relocation if necessary.
4774 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4776 if (target
->may_need_copy_reloc(gsym
))
4778 target
->copy_reloc(symtab
, layout
, object
,
4779 data_shndx
, output_section
, gsym
, reloc
);
4781 else if (gsym
->can_use_relative_reloc(false))
4783 // If we are to add more other reloc types than R_ARM_ABS32,
4784 // we need to add check_non_pic(object, r_type) here.
4785 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4786 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4787 output_section
, object
,
4788 data_shndx
, reloc
.get_r_offset());
4792 // If we are to add more other reloc types than R_ARM_ABS32,
4793 // we need to add check_non_pic(object, r_type) here.
4794 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4795 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4796 data_shndx
, reloc
.get_r_offset());
4802 case elfcpp::R_ARM_MOVW_ABS_NC
:
4803 case elfcpp::R_ARM_MOVT_ABS
:
4804 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4805 case elfcpp::R_ARM_THM_MOVT_ABS
:
4806 case elfcpp::R_ARM_MOVW_PREL_NC
:
4807 case elfcpp::R_ARM_MOVT_PREL
:
4808 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4809 case elfcpp::R_ARM_THM_MOVT_PREL
:
4812 case elfcpp::R_ARM_THM_ABS5
:
4813 case elfcpp::R_ARM_ABS8
:
4814 case elfcpp::R_ARM_ABS12
:
4815 case elfcpp::R_ARM_ABS16
:
4816 case elfcpp::R_ARM_BASE_ABS
:
4818 // No dynamic relocs of this kinds.
4819 // Report the error in case of PIC.
4820 int flags
= Symbol::NON_PIC_REF
;
4821 if (gsym
->type() == elfcpp::STT_FUNC
4822 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4823 flags
|= Symbol::FUNCTION_CALL
;
4824 if (gsym
->needs_dynamic_reloc(flags
))
4825 check_non_pic(object
, r_type
);
4829 case elfcpp::R_ARM_REL32
:
4830 case elfcpp::R_ARM_PREL31
:
4832 // Make a dynamic relocation if necessary.
4833 int flags
= Symbol::NON_PIC_REF
;
4834 if (gsym
->needs_dynamic_reloc(flags
))
4836 if (target
->may_need_copy_reloc(gsym
))
4838 target
->copy_reloc(symtab
, layout
, object
,
4839 data_shndx
, output_section
, gsym
, reloc
);
4843 check_non_pic(object
, r_type
);
4844 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4845 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4846 data_shndx
, reloc
.get_r_offset());
4852 case elfcpp::R_ARM_JUMP24
:
4853 case elfcpp::R_ARM_THM_JUMP24
:
4854 case elfcpp::R_ARM_CALL
:
4855 case elfcpp::R_ARM_THM_CALL
:
4857 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4858 target
->make_plt_entry(symtab
, layout
, gsym
);
4861 // Check to see if this is a function that would need a PLT
4862 // but does not get one because the function symbol is untyped.
4863 // This happens in assembly code missing a proper .type directive.
4864 if ((!gsym
->is_undefined() || parameters
->options().shared())
4865 && !parameters
->doing_static_link()
4866 && gsym
->type() == elfcpp::STT_NOTYPE
4867 && (gsym
->is_from_dynobj()
4868 || gsym
->is_undefined()
4869 || gsym
->is_preemptible()))
4870 gold_error(_("%s is not a function."),
4871 gsym
->demangled_name().c_str());
4875 case elfcpp::R_ARM_PLT32
:
4876 // If the symbol is fully resolved, this is just a relative
4877 // local reloc. Otherwise we need a PLT entry.
4878 if (gsym
->final_value_is_known())
4880 // If building a shared library, we can also skip the PLT entry
4881 // if the symbol is defined in the output file and is protected
4883 if (gsym
->is_defined()
4884 && !gsym
->is_from_dynobj()
4885 && !gsym
->is_preemptible())
4887 target
->make_plt_entry(symtab
, layout
, gsym
);
4890 case elfcpp::R_ARM_GOTOFF32
:
4891 // We need a GOT section.
4892 target
->got_section(symtab
, layout
);
4895 case elfcpp::R_ARM_BASE_PREL
:
4896 // FIXME: What about this?
4899 case elfcpp::R_ARM_GOT_BREL
:
4900 case elfcpp::R_ARM_GOT_PREL
:
4902 // The symbol requires a GOT entry.
4903 Output_data_got
<32, big_endian
>* got
=
4904 target
->got_section(symtab
, layout
);
4905 if (gsym
->final_value_is_known())
4906 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4909 // If this symbol is not fully resolved, we need to add a
4910 // GOT entry with a dynamic relocation.
4911 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4912 if (gsym
->is_from_dynobj()
4913 || gsym
->is_undefined()
4914 || gsym
->is_preemptible())
4915 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4916 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4919 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4920 rel_dyn
->add_global_relative(
4921 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4922 gsym
->got_offset(GOT_TYPE_STANDARD
));
4928 case elfcpp::R_ARM_TARGET1
:
4929 // This should have been mapped to another type already.
4931 case elfcpp::R_ARM_COPY
:
4932 case elfcpp::R_ARM_GLOB_DAT
:
4933 case elfcpp::R_ARM_JUMP_SLOT
:
4934 case elfcpp::R_ARM_RELATIVE
:
4935 // These are relocations which should only be seen by the
4936 // dynamic linker, and should never be seen here.
4937 gold_error(_("%s: unexpected reloc %u in object file"),
4938 object
->name().c_str(), r_type
);
4942 unsupported_reloc_global(object
, r_type
, gsym
);
4947 // Process relocations for gc.
4949 template<bool big_endian
>
4951 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4953 Sized_relobj
<32, big_endian
>* object
,
4954 unsigned int data_shndx
,
4956 const unsigned char* prelocs
,
4958 Output_section
* output_section
,
4959 bool needs_special_offset_handling
,
4960 size_t local_symbol_count
,
4961 const unsigned char* plocal_symbols
)
4963 typedef Target_arm
<big_endian
> Arm
;
4964 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4966 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
4975 needs_special_offset_handling
,
4980 // Scan relocations for a section.
4982 template<bool big_endian
>
4984 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4986 Sized_relobj
<32, big_endian
>* object
,
4987 unsigned int data_shndx
,
4988 unsigned int sh_type
,
4989 const unsigned char* prelocs
,
4991 Output_section
* output_section
,
4992 bool needs_special_offset_handling
,
4993 size_t local_symbol_count
,
4994 const unsigned char* plocal_symbols
)
4996 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4997 if (sh_type
== elfcpp::SHT_RELA
)
4999 gold_error(_("%s: unsupported RELA reloc section"),
5000 object
->name().c_str());
5004 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
5013 needs_special_offset_handling
,
5018 // Finalize the sections.
5020 template<bool big_endian
>
5022 Target_arm
<big_endian
>::do_finalize_sections(
5024 const Input_objects
* input_objects
,
5025 Symbol_table
* symtab
)
5027 // Merge processor-specific flags.
5028 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
5029 p
!= input_objects
->relobj_end();
5032 Arm_relobj
<big_endian
>* arm_relobj
=
5033 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
5034 this->merge_processor_specific_flags(
5036 arm_relobj
->processor_specific_flags());
5037 this->merge_object_attributes(arm_relobj
->name().c_str(),
5038 arm_relobj
->attributes_section_data());
5042 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
5043 p
!= input_objects
->dynobj_end();
5046 Arm_dynobj
<big_endian
>* arm_dynobj
=
5047 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
5048 this->merge_processor_specific_flags(
5050 arm_dynobj
->processor_specific_flags());
5051 this->merge_object_attributes(arm_dynobj
->name().c_str(),
5052 arm_dynobj
->attributes_section_data());
5056 Object_attribute
* attr
=
5057 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
5058 if (attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
5059 this->set_may_use_blx(true);
5061 // Fill in some more dynamic tags.
5062 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5064 : this->plt_
->rel_plt());
5065 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
5066 this->rel_dyn_
, true);
5068 // Emit any relocs we saved in an attempt to avoid generating COPY
5070 if (this->copy_relocs_
.any_saved_relocs())
5071 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
5073 // Handle the .ARM.exidx section.
5074 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
5075 if (exidx_section
!= NULL
5076 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
5077 && !parameters
->options().relocatable())
5079 // Create __exidx_start and __exdix_end symbols.
5080 symtab
->define_in_output_data("__exidx_start", NULL
,
5081 Symbol_table::PREDEFINED
,
5082 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5083 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5085 symtab
->define_in_output_data("__exidx_end", NULL
,
5086 Symbol_table::PREDEFINED
,
5087 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5088 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5091 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5092 // the .ARM.exidx section.
5093 if (!layout
->script_options()->saw_phdrs_clause())
5095 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
5097 Output_segment
* exidx_segment
=
5098 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
5099 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
5104 // Create an .ARM.attributes section if there is not one already.
5105 Output_attributes_section_data
* attributes_section
=
5106 new Output_attributes_section_data(*this->attributes_section_data_
);
5107 layout
->add_output_section_data(".ARM.attributes",
5108 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
5109 attributes_section
, false, false, false,
5113 // Return whether a direct absolute static relocation needs to be applied.
5114 // In cases where Scan::local() or Scan::global() has created
5115 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5116 // of the relocation is carried in the data, and we must not
5117 // apply the static relocation.
5119 template<bool big_endian
>
5121 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
5122 const Sized_symbol
<32>* gsym
,
5125 Output_section
* output_section
)
5127 // If the output section is not allocated, then we didn't call
5128 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5130 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
5133 // For local symbols, we will have created a non-RELATIVE dynamic
5134 // relocation only if (a) the output is position independent,
5135 // (b) the relocation is absolute (not pc- or segment-relative), and
5136 // (c) the relocation is not 32 bits wide.
5138 return !(parameters
->options().output_is_position_independent()
5139 && (ref_flags
& Symbol::ABSOLUTE_REF
)
5142 // For global symbols, we use the same helper routines used in the
5143 // scan pass. If we did not create a dynamic relocation, or if we
5144 // created a RELATIVE dynamic relocation, we should apply the static
5146 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
5147 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
5148 && gsym
->can_use_relative_reloc(ref_flags
5149 & Symbol::FUNCTION_CALL
);
5150 return !has_dyn
|| is_rel
;
5153 // Perform a relocation.
5155 template<bool big_endian
>
5157 Target_arm
<big_endian
>::Relocate::relocate(
5158 const Relocate_info
<32, big_endian
>* relinfo
,
5160 Output_section
*output_section
,
5162 const elfcpp::Rel
<32, big_endian
>& rel
,
5163 unsigned int r_type
,
5164 const Sized_symbol
<32>* gsym
,
5165 const Symbol_value
<32>* psymval
,
5166 unsigned char* view
,
5167 Arm_address address
,
5168 section_size_type
/* view_size */ )
5170 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
5172 r_type
= get_real_reloc_type(r_type
);
5174 const Arm_relobj
<big_endian
>* object
=
5175 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5177 // If the final branch target of a relocation is THUMB instruction, this
5178 // is 1. Otherwise it is 0.
5179 Arm_address thumb_bit
= 0;
5180 Symbol_value
<32> symval
;
5181 bool is_weakly_undefined_without_plt
= false;
5182 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
5186 // This is a global symbol. Determine if we use PLT and if the
5187 // final target is THUMB.
5188 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
5190 // This uses a PLT, change the symbol value.
5191 symval
.set_output_value(target
->plt_section()->address()
5192 + gsym
->plt_offset());
5195 else if (gsym
->is_weak_undefined())
5197 // This is a weakly undefined symbol and we do not use PLT
5198 // for this relocation. A branch targeting this symbol will
5199 // be converted into an NOP.
5200 is_weakly_undefined_without_plt
= true;
5204 // Set thumb bit if symbol:
5205 // -Has type STT_ARM_TFUNC or
5206 // -Has type STT_FUNC, is defined and with LSB in value set.
5208 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5209 || (gsym
->type() == elfcpp::STT_FUNC
5210 && !gsym
->is_undefined()
5211 && ((psymval
->value(object
, 0) & 1) != 0)))
5218 // This is a local symbol. Determine if the final target is THUMB.
5219 // We saved this information when all the local symbols were read.
5220 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
5221 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5222 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
5227 // This is a fake relocation synthesized for a stub. It does not have
5228 // a real symbol. We just look at the LSB of the symbol value to
5229 // determine if the target is THUMB or not.
5230 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
5233 // Strip LSB if this points to a THUMB target.
5235 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5236 && ((psymval
->value(object
, 0) & 1) != 0))
5238 Arm_address stripped_value
=
5239 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
5240 symval
.set_output_value(stripped_value
);
5244 // Get the GOT offset if needed.
5245 // The GOT pointer points to the end of the GOT section.
5246 // We need to subtract the size of the GOT section to get
5247 // the actual offset to use in the relocation.
5248 bool have_got_offset
= false;
5249 unsigned int got_offset
= 0;
5252 case elfcpp::R_ARM_GOT_BREL
:
5253 case elfcpp::R_ARM_GOT_PREL
:
5256 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5257 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
5258 - target
->got_size());
5262 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5263 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5264 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
5265 - target
->got_size());
5267 have_got_offset
= true;
5274 // To look up relocation stubs, we need to pass the symbol table index of
5276 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
5278 typename
Arm_relocate_functions::Status reloc_status
=
5279 Arm_relocate_functions::STATUS_OKAY
;
5282 case elfcpp::R_ARM_NONE
:
5285 case elfcpp::R_ARM_ABS8
:
5286 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5288 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
5291 case elfcpp::R_ARM_ABS12
:
5292 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5294 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
5297 case elfcpp::R_ARM_ABS16
:
5298 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5300 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
5303 case elfcpp::R_ARM_ABS32
:
5304 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5306 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5310 case elfcpp::R_ARM_ABS32_NOI
:
5311 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5313 // No thumb bit for this relocation: (S + A)
5314 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
5318 case elfcpp::R_ARM_MOVW_ABS_NC
:
5319 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5321 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
5325 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5326 "a shared object; recompile with -fPIC"));
5329 case elfcpp::R_ARM_MOVT_ABS
:
5330 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5332 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
5334 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5335 "a shared object; recompile with -fPIC"));
5338 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5339 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5341 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
5345 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5346 "making a shared object; recompile with -fPIC"));
5349 case elfcpp::R_ARM_THM_MOVT_ABS
:
5350 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5352 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
5355 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5356 "making a shared object; recompile with -fPIC"));
5359 case elfcpp::R_ARM_MOVW_PREL_NC
:
5360 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
5365 case elfcpp::R_ARM_MOVT_PREL
:
5366 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
5370 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5371 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
5376 case elfcpp::R_ARM_THM_MOVT_PREL
:
5377 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
5381 case elfcpp::R_ARM_REL32
:
5382 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5383 address
, thumb_bit
);
5386 case elfcpp::R_ARM_THM_ABS5
:
5387 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5389 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
5392 case elfcpp::R_ARM_THM_CALL
:
5394 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
5395 psymval
, address
, thumb_bit
,
5396 is_weakly_undefined_without_plt
);
5399 case elfcpp::R_ARM_XPC25
:
5401 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
5402 psymval
, address
, thumb_bit
,
5403 is_weakly_undefined_without_plt
);
5406 case elfcpp::R_ARM_THM_XPC22
:
5408 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
5409 psymval
, address
, thumb_bit
,
5410 is_weakly_undefined_without_plt
);
5413 case elfcpp::R_ARM_GOTOFF32
:
5415 Arm_address got_origin
;
5416 got_origin
= target
->got_plt_section()->address();
5417 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5418 got_origin
, thumb_bit
);
5422 case elfcpp::R_ARM_BASE_PREL
:
5425 // Get the addressing origin of the output segment defining the
5426 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5427 gold_assert(gsym
!= NULL
);
5428 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5429 origin
= gsym
->output_segment()->vaddr();
5430 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5431 origin
= gsym
->output_data()->address();
5434 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5435 _("cannot find origin of R_ARM_BASE_PREL"));
5438 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
5442 case elfcpp::R_ARM_BASE_ABS
:
5444 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5449 // Get the addressing origin of the output segment defining
5450 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5452 // R_ARM_BASE_ABS with the NULL symbol will give the
5453 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5454 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5455 origin
= target
->got_plt_section()->address();
5456 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5457 origin
= gsym
->output_segment()->vaddr();
5458 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5459 origin
= gsym
->output_data()->address();
5462 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5463 _("cannot find origin of R_ARM_BASE_ABS"));
5467 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5471 case elfcpp::R_ARM_GOT_BREL
:
5472 gold_assert(have_got_offset
);
5473 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5476 case elfcpp::R_ARM_GOT_PREL
:
5477 gold_assert(have_got_offset
);
5478 // Get the address origin for GOT PLT, which is allocated right
5479 // after the GOT section, to calculate an absolute address of
5480 // the symbol GOT entry (got_origin + got_offset).
5481 Arm_address got_origin
;
5482 got_origin
= target
->got_plt_section()->address();
5483 reloc_status
= Arm_relocate_functions::got_prel(view
,
5484 got_origin
+ got_offset
,
5488 case elfcpp::R_ARM_PLT32
:
5489 gold_assert(gsym
== NULL
5490 || gsym
->has_plt_offset()
5491 || gsym
->final_value_is_known()
5492 || (gsym
->is_defined()
5493 && !gsym
->is_from_dynobj()
5494 && !gsym
->is_preemptible()));
5496 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5497 psymval
, address
, thumb_bit
,
5498 is_weakly_undefined_without_plt
);
5501 case elfcpp::R_ARM_CALL
:
5503 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5504 psymval
, address
, thumb_bit
,
5505 is_weakly_undefined_without_plt
);
5508 case elfcpp::R_ARM_JUMP24
:
5510 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5511 psymval
, address
, thumb_bit
,
5512 is_weakly_undefined_without_plt
);
5515 case elfcpp::R_ARM_THM_JUMP24
:
5517 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5518 psymval
, address
, thumb_bit
,
5519 is_weakly_undefined_without_plt
);
5522 case elfcpp::R_ARM_PREL31
:
5523 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5524 address
, thumb_bit
);
5527 case elfcpp::R_ARM_TARGET1
:
5528 // This should have been mapped to another type already.
5530 case elfcpp::R_ARM_COPY
:
5531 case elfcpp::R_ARM_GLOB_DAT
:
5532 case elfcpp::R_ARM_JUMP_SLOT
:
5533 case elfcpp::R_ARM_RELATIVE
:
5534 // These are relocations which should only be seen by the
5535 // dynamic linker, and should never be seen here.
5536 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5537 _("unexpected reloc %u in object file"),
5542 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5543 _("unsupported reloc %u"),
5548 // Report any errors.
5549 switch (reloc_status
)
5551 case Arm_relocate_functions::STATUS_OKAY
:
5553 case Arm_relocate_functions::STATUS_OVERFLOW
:
5554 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5555 _("relocation overflow in relocation %u"),
5558 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5559 gold_error_at_location(
5563 _("unexpected opcode while processing relocation %u"),
5573 // Relocate section data.
5575 template<bool big_endian
>
5577 Target_arm
<big_endian
>::relocate_section(
5578 const Relocate_info
<32, big_endian
>* relinfo
,
5579 unsigned int sh_type
,
5580 const unsigned char* prelocs
,
5582 Output_section
* output_section
,
5583 bool needs_special_offset_handling
,
5584 unsigned char* view
,
5585 Arm_address address
,
5586 section_size_type view_size
,
5587 const Reloc_symbol_changes
* reloc_symbol_changes
)
5589 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5590 gold_assert(sh_type
== elfcpp::SHT_REL
);
5592 Arm_input_section
<big_endian
>* arm_input_section
=
5593 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5595 // This is an ARM input section and the view covers the whole output
5597 if (arm_input_section
!= NULL
)
5599 gold_assert(needs_special_offset_handling
);
5600 Arm_address section_address
= arm_input_section
->address();
5601 section_size_type section_size
= arm_input_section
->data_size();
5603 gold_assert((arm_input_section
->address() >= address
)
5604 && ((arm_input_section
->address()
5605 + arm_input_section
->data_size())
5606 <= (address
+ view_size
)));
5608 off_t offset
= section_address
- address
;
5611 view_size
= section_size
;
5614 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5621 needs_special_offset_handling
,
5625 reloc_symbol_changes
);
5628 // Return the size of a relocation while scanning during a relocatable
5631 template<bool big_endian
>
5633 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5634 unsigned int r_type
,
5637 r_type
= get_real_reloc_type(r_type
);
5640 case elfcpp::R_ARM_NONE
:
5643 case elfcpp::R_ARM_ABS8
:
5646 case elfcpp::R_ARM_ABS16
:
5647 case elfcpp::R_ARM_THM_ABS5
:
5650 case elfcpp::R_ARM_ABS32
:
5651 case elfcpp::R_ARM_ABS32_NOI
:
5652 case elfcpp::R_ARM_ABS12
:
5653 case elfcpp::R_ARM_BASE_ABS
:
5654 case elfcpp::R_ARM_REL32
:
5655 case elfcpp::R_ARM_THM_CALL
:
5656 case elfcpp::R_ARM_GOTOFF32
:
5657 case elfcpp::R_ARM_BASE_PREL
:
5658 case elfcpp::R_ARM_GOT_BREL
:
5659 case elfcpp::R_ARM_GOT_PREL
:
5660 case elfcpp::R_ARM_PLT32
:
5661 case elfcpp::R_ARM_CALL
:
5662 case elfcpp::R_ARM_JUMP24
:
5663 case elfcpp::R_ARM_PREL31
:
5664 case elfcpp::R_ARM_MOVW_ABS_NC
:
5665 case elfcpp::R_ARM_MOVT_ABS
:
5666 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5667 case elfcpp::R_ARM_THM_MOVT_ABS
:
5668 case elfcpp::R_ARM_MOVW_PREL_NC
:
5669 case elfcpp::R_ARM_MOVT_PREL
:
5670 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5671 case elfcpp::R_ARM_THM_MOVT_PREL
:
5674 case elfcpp::R_ARM_TARGET1
:
5675 // This should have been mapped to another type already.
5677 case elfcpp::R_ARM_COPY
:
5678 case elfcpp::R_ARM_GLOB_DAT
:
5679 case elfcpp::R_ARM_JUMP_SLOT
:
5680 case elfcpp::R_ARM_RELATIVE
:
5681 // These are relocations which should only be seen by the
5682 // dynamic linker, and should never be seen here.
5683 gold_error(_("%s: unexpected reloc %u in object file"),
5684 object
->name().c_str(), r_type
);
5688 object
->error(_("unsupported reloc %u in object file"), r_type
);
5693 // Scan the relocs during a relocatable link.
5695 template<bool big_endian
>
5697 Target_arm
<big_endian
>::scan_relocatable_relocs(
5698 Symbol_table
* symtab
,
5700 Sized_relobj
<32, big_endian
>* object
,
5701 unsigned int data_shndx
,
5702 unsigned int sh_type
,
5703 const unsigned char* prelocs
,
5705 Output_section
* output_section
,
5706 bool needs_special_offset_handling
,
5707 size_t local_symbol_count
,
5708 const unsigned char* plocal_symbols
,
5709 Relocatable_relocs
* rr
)
5711 gold_assert(sh_type
== elfcpp::SHT_REL
);
5713 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5714 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5716 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5717 Scan_relocatable_relocs
>(
5725 needs_special_offset_handling
,
5731 // Relocate a section during a relocatable link.
5733 template<bool big_endian
>
5735 Target_arm
<big_endian
>::relocate_for_relocatable(
5736 const Relocate_info
<32, big_endian
>* relinfo
,
5737 unsigned int sh_type
,
5738 const unsigned char* prelocs
,
5740 Output_section
* output_section
,
5741 off_t offset_in_output_section
,
5742 const Relocatable_relocs
* rr
,
5743 unsigned char* view
,
5744 Arm_address view_address
,
5745 section_size_type view_size
,
5746 unsigned char* reloc_view
,
5747 section_size_type reloc_view_size
)
5749 gold_assert(sh_type
== elfcpp::SHT_REL
);
5751 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5756 offset_in_output_section
,
5765 // Return the value to use for a dynamic symbol which requires special
5766 // treatment. This is how we support equality comparisons of function
5767 // pointers across shared library boundaries, as described in the
5768 // processor specific ABI supplement.
5770 template<bool big_endian
>
5772 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5774 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5775 return this->plt_section()->address() + gsym
->plt_offset();
5778 // Map platform-specific relocs to real relocs
5780 template<bool big_endian
>
5782 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5786 case elfcpp::R_ARM_TARGET1
:
5787 // This is either R_ARM_ABS32 or R_ARM_REL32;
5788 return elfcpp::R_ARM_ABS32
;
5790 case elfcpp::R_ARM_TARGET2
:
5791 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5792 return elfcpp::R_ARM_GOT_PREL
;
5799 // Whether if two EABI versions V1 and V2 are compatible.
5801 template<bool big_endian
>
5803 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5804 elfcpp::Elf_Word v1
,
5805 elfcpp::Elf_Word v2
)
5807 // v4 and v5 are the same spec before and after it was released,
5808 // so allow mixing them.
5809 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5810 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5816 // Combine FLAGS from an input object called NAME and the processor-specific
5817 // flags in the ELF header of the output. Much of this is adapted from the
5818 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5819 // in bfd/elf32-arm.c.
5821 template<bool big_endian
>
5823 Target_arm
<big_endian
>::merge_processor_specific_flags(
5824 const std::string
& name
,
5825 elfcpp::Elf_Word flags
)
5827 if (this->are_processor_specific_flags_set())
5829 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5831 // Nothing to merge if flags equal to those in output.
5832 if (flags
== out_flags
)
5835 // Complain about various flag mismatches.
5836 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5837 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5838 if (!this->are_eabi_versions_compatible(version1
, version2
))
5839 gold_error(_("Source object %s has EABI version %d but output has "
5840 "EABI version %d."),
5842 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5843 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5847 // If the input is the default architecture and had the default
5848 // flags then do not bother setting the flags for the output
5849 // architecture, instead allow future merges to do this. If no
5850 // future merges ever set these flags then they will retain their
5851 // uninitialised values, which surprise surprise, correspond
5852 // to the default values.
5856 // This is the first time, just copy the flags.
5857 // We only copy the EABI version for now.
5858 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5862 // Adjust ELF file header.
5863 template<bool big_endian
>
5865 Target_arm
<big_endian
>::do_adjust_elf_header(
5866 unsigned char* view
,
5869 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5871 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5872 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5873 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5875 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5876 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5877 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5879 e_ident
[elfcpp::EI_OSABI
] = 0;
5880 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5882 // FIXME: Do EF_ARM_BE8 adjustment.
5884 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5885 oehdr
.put_e_ident(e_ident
);
5888 // do_make_elf_object to override the same function in the base class.
5889 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5890 // to store ARM specific information. Hence we need to have our own
5891 // ELF object creation.
5893 template<bool big_endian
>
5895 Target_arm
<big_endian
>::do_make_elf_object(
5896 const std::string
& name
,
5897 Input_file
* input_file
,
5898 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5900 int et
= ehdr
.get_e_type();
5901 if (et
== elfcpp::ET_REL
)
5903 Arm_relobj
<big_endian
>* obj
=
5904 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5908 else if (et
== elfcpp::ET_DYN
)
5910 Sized_dynobj
<32, big_endian
>* obj
=
5911 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5917 gold_error(_("%s: unsupported ELF file type %d"),
5923 // Read the architecture from the Tag_also_compatible_with attribute, if any.
5924 // Returns -1 if no architecture could be read.
5925 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5927 template<bool big_endian
>
5929 Target_arm
<big_endian
>::get_secondary_compatible_arch(
5930 const Attributes_section_data
* pasd
)
5932 const Object_attribute
*known_attributes
=
5933 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
5935 // Note: the tag and its argument below are uleb128 values, though
5936 // currently-defined values fit in one byte for each.
5937 const std::string
& sv
=
5938 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
5940 && sv
.data()[0] == elfcpp::Tag_CPU_arch
5941 && (sv
.data()[1] & 128) != 128)
5942 return sv
.data()[1];
5944 // This tag is "safely ignorable", so don't complain if it looks funny.
5948 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5949 // The tag is removed if ARCH is -1.
5950 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5952 template<bool big_endian
>
5954 Target_arm
<big_endian
>::set_secondary_compatible_arch(
5955 Attributes_section_data
* pasd
,
5958 Object_attribute
*known_attributes
=
5959 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
5963 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
5967 // Note: the tag and its argument below are uleb128 values, though
5968 // currently-defined values fit in one byte for each.
5970 sv
[0] = elfcpp::Tag_CPU_arch
;
5971 gold_assert(arch
!= 0);
5975 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
5978 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
5980 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
5982 template<bool big_endian
>
5984 Target_arm
<big_endian
>::tag_cpu_arch_combine(
5987 int* secondary_compat_out
,
5989 int secondary_compat
)
5991 #define T(X) elfcpp::TAG_CPU_ARCH_##X
5992 static const int v6t2
[] =
6004 static const int v6k
[] =
6017 static const int v7
[] =
6031 static const int v6_m
[] =
6046 static const int v6s_m
[] =
6062 static const int v7e_m
[] =
6079 static const int v4t_plus_v6_m
[] =
6095 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
6097 static const int *comb
[] =
6105 // Pseudo-architecture.
6109 // Check we've not got a higher architecture than we know about.
6111 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
6113 gold_error(_("%s: unknown CPU architecture"), name
);
6117 // Override old tag if we have a Tag_also_compatible_with on the output.
6119 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
6120 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
6121 oldtag
= T(V4T_PLUS_V6_M
);
6123 // And override the new tag if we have a Tag_also_compatible_with on the
6126 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
6127 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
6128 newtag
= T(V4T_PLUS_V6_M
);
6130 // Architectures before V6KZ add features monotonically.
6131 int tagh
= std::max(oldtag
, newtag
);
6132 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
6135 int tagl
= std::min(oldtag
, newtag
);
6136 int result
= comb
[tagh
- T(V6T2
)][tagl
];
6138 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6139 // as the canonical version.
6140 if (result
== T(V4T_PLUS_V6_M
))
6143 *secondary_compat_out
= T(V6_M
);
6146 *secondary_compat_out
= -1;
6150 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6151 name
, oldtag
, newtag
);
6159 // Helper to print AEABI enum tag value.
6161 template<bool big_endian
>
6163 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
6165 static const char *aeabi_enum_names
[] =
6166 { "", "variable-size", "32-bit", "" };
6167 const size_t aeabi_enum_names_size
=
6168 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
6170 if (value
< aeabi_enum_names_size
)
6171 return std::string(aeabi_enum_names
[value
]);
6175 sprintf(buffer
, "<unknown value %u>", value
);
6176 return std::string(buffer
);
6180 // Return the string value to store in TAG_CPU_name.
6182 template<bool big_endian
>
6184 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
6186 static const char *name_table
[] = {
6187 // These aren't real CPU names, but we can't guess
6188 // that from the architecture version alone.
6204 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
6206 if (value
< name_table_size
)
6207 return std::string(name_table
[value
]);
6211 sprintf(buffer
, "<unknown CPU value %u>", value
);
6212 return std::string(buffer
);
6216 // Merge object attributes from input file called NAME with those of the
6217 // output. The input object attributes are in the object pointed by PASD.
6219 template<bool big_endian
>
6221 Target_arm
<big_endian
>::merge_object_attributes(
6223 const Attributes_section_data
* pasd
)
6225 // Return if there is no attributes section data.
6229 // If output has no object attributes, just copy.
6230 if (this->attributes_section_data_
== NULL
)
6232 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
6236 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
6237 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
6238 Object_attribute
* out_attr
=
6239 this->attributes_section_data_
->known_attributes(vendor
);
6241 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6242 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
6243 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
6245 // Ignore mismatches if the object doesn't use floating point. */
6246 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
6247 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
6248 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
6249 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
6250 gold_error(_("%s uses VFP register arguments, output does not"),
6254 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
6256 // Merge this attribute with existing attributes.
6259 case elfcpp::Tag_CPU_raw_name
:
6260 case elfcpp::Tag_CPU_name
:
6261 // These are merged after Tag_CPU_arch.
6264 case elfcpp::Tag_ABI_optimization_goals
:
6265 case elfcpp::Tag_ABI_FP_optimization_goals
:
6266 // Use the first value seen.
6269 case elfcpp::Tag_CPU_arch
:
6271 unsigned int saved_out_attr
= out_attr
->int_value();
6272 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6273 int secondary_compat
=
6274 this->get_secondary_compatible_arch(pasd
);
6275 int secondary_compat_out
=
6276 this->get_secondary_compatible_arch(
6277 this->attributes_section_data_
);
6278 out_attr
[i
].set_int_value(
6279 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
6280 &secondary_compat_out
,
6281 in_attr
[i
].int_value(),
6283 this->set_secondary_compatible_arch(this->attributes_section_data_
,
6284 secondary_compat_out
);
6286 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6287 if (out_attr
[i
].int_value() == saved_out_attr
)
6288 ; // Leave the names alone.
6289 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
6291 // The output architecture has been changed to match the
6292 // input architecture. Use the input names.
6293 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
6294 in_attr
[elfcpp::Tag_CPU_name
].string_value());
6295 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
6296 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
6300 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
6301 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
6304 // If we still don't have a value for Tag_CPU_name,
6305 // make one up now. Tag_CPU_raw_name remains blank.
6306 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
6308 const std::string cpu_name
=
6309 this->tag_cpu_name_value(out_attr
[i
].int_value());
6310 // FIXME: If we see an unknown CPU, this will be set
6311 // to "<unknown CPU n>", where n is the attribute value.
6312 // This is different from BFD, which leaves the name alone.
6313 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
6318 case elfcpp::Tag_ARM_ISA_use
:
6319 case elfcpp::Tag_THUMB_ISA_use
:
6320 case elfcpp::Tag_WMMX_arch
:
6321 case elfcpp::Tag_Advanced_SIMD_arch
:
6322 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6323 case elfcpp::Tag_ABI_FP_rounding
:
6324 case elfcpp::Tag_ABI_FP_exceptions
:
6325 case elfcpp::Tag_ABI_FP_user_exceptions
:
6326 case elfcpp::Tag_ABI_FP_number_model
:
6327 case elfcpp::Tag_VFP_HP_extension
:
6328 case elfcpp::Tag_CPU_unaligned_access
:
6329 case elfcpp::Tag_T2EE_use
:
6330 case elfcpp::Tag_Virtualization_use
:
6331 case elfcpp::Tag_MPextension_use
:
6332 // Use the largest value specified.
6333 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6334 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6337 case elfcpp::Tag_ABI_align8_preserved
:
6338 case elfcpp::Tag_ABI_PCS_RO_data
:
6339 // Use the smallest value specified.
6340 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6341 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6344 case elfcpp::Tag_ABI_align8_needed
:
6345 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
6346 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
6347 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
6350 // This error message should be enabled once all non-conformant
6351 // binaries in the toolchain have had the attributes set
6353 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6357 case elfcpp::Tag_ABI_FP_denormal
:
6358 case elfcpp::Tag_ABI_PCS_GOT_use
:
6360 // These tags have 0 = don't care, 1 = strong requirement,
6361 // 2 = weak requirement.
6362 static const int order_021
[3] = {0, 2, 1};
6364 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6365 // value if greater than 2 (for future-proofing).
6366 if ((in_attr
[i
].int_value() > 2
6367 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6368 || (in_attr
[i
].int_value() <= 2
6369 && out_attr
[i
].int_value() <= 2
6370 && (order_021
[in_attr
[i
].int_value()]
6371 > order_021
[out_attr
[i
].int_value()])))
6372 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6376 case elfcpp::Tag_CPU_arch_profile
:
6377 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
6379 // 0 will merge with anything.
6380 // 'A' and 'S' merge to 'A'.
6381 // 'R' and 'S' merge to 'R'.
6382 // 'M' and 'A|R|S' is an error.
6383 if (out_attr
[i
].int_value() == 0
6384 || (out_attr
[i
].int_value() == 'S'
6385 && (in_attr
[i
].int_value() == 'A'
6386 || in_attr
[i
].int_value() == 'R')))
6387 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6388 else if (in_attr
[i
].int_value() == 0
6389 || (in_attr
[i
].int_value() == 'S'
6390 && (out_attr
[i
].int_value() == 'A'
6391 || out_attr
[i
].int_value() == 'R')))
6396 (_("conflicting architecture profiles %c/%c"),
6397 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
6398 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
6402 case elfcpp::Tag_VFP_arch
:
6419 // Values greater than 6 aren't defined, so just pick the
6421 if (in_attr
[i
].int_value() > 6
6422 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6424 *out_attr
= *in_attr
;
6427 // The output uses the superset of input features
6428 // (ISA version) and registers.
6429 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
6430 vfp_versions
[out_attr
[i
].int_value()].ver
);
6431 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
6432 vfp_versions
[out_attr
[i
].int_value()].regs
);
6433 // This assumes all possible supersets are also a valid
6436 for (newval
= 6; newval
> 0; newval
--)
6438 if (regs
== vfp_versions
[newval
].regs
6439 && ver
== vfp_versions
[newval
].ver
)
6442 out_attr
[i
].set_int_value(newval
);
6445 case elfcpp::Tag_PCS_config
:
6446 if (out_attr
[i
].int_value() == 0)
6447 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6448 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6450 // It's sometimes ok to mix different configs, so this is only
6452 gold_warning(_("%s: conflicting platform configuration"), name
);
6455 case elfcpp::Tag_ABI_PCS_R9_use
:
6456 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
6457 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
6458 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
6460 gold_error(_("%s: conflicting use of R9"), name
);
6462 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
6463 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6465 case elfcpp::Tag_ABI_PCS_RW_data
:
6466 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6467 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6468 != elfcpp::AEABI_R9_SB
)
6469 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6470 != elfcpp::AEABI_R9_unused
))
6472 gold_error(_("%s: SB relative addressing conflicts with use "
6476 // Use the smallest value specified.
6477 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6478 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6480 case elfcpp::Tag_ABI_PCS_wchar_t
:
6481 // FIXME: Make it possible to turn off this warning.
6482 if (out_attr
[i
].int_value()
6483 && in_attr
[i
].int_value()
6484 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6486 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6487 "use %u-byte wchar_t; use of wchar_t values "
6488 "across objects may fail"),
6489 name
, in_attr
[i
].int_value(),
6490 out_attr
[i
].int_value());
6492 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
6493 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6495 case elfcpp::Tag_ABI_enum_size
:
6496 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
6498 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
6499 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
6501 // The existing object is compatible with anything.
6502 // Use whatever requirements the new object has.
6503 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6505 // FIXME: Make it possible to turn off this warning.
6506 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
6507 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6509 unsigned int in_value
= in_attr
[i
].int_value();
6510 unsigned int out_value
= out_attr
[i
].int_value();
6511 gold_warning(_("%s uses %s enums yet the output is to use "
6512 "%s enums; use of enum values across objects "
6515 this->aeabi_enum_name(in_value
).c_str(),
6516 this->aeabi_enum_name(out_value
).c_str());
6520 case elfcpp::Tag_ABI_VFP_args
:
6523 case elfcpp::Tag_ABI_WMMX_args
:
6524 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6526 gold_error(_("%s uses iWMMXt register arguments, output does "
6531 case Object_attribute::Tag_compatibility
:
6532 // Merged in target-independent code.
6534 case elfcpp::Tag_ABI_HardFP_use
:
6535 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6536 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
6537 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
6538 out_attr
[i
].set_int_value(3);
6539 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6540 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6542 case elfcpp::Tag_ABI_FP_16bit_format
:
6543 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6545 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6546 gold_error(_("fp16 format mismatch between %s and output"),
6549 if (in_attr
[i
].int_value() != 0)
6550 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6553 case elfcpp::Tag_nodefaults
:
6554 // This tag is set if it exists, but the value is unused (and is
6555 // typically zero). We don't actually need to do anything here -
6556 // the merge happens automatically when the type flags are merged
6559 case elfcpp::Tag_also_compatible_with
:
6560 // Already done in Tag_CPU_arch.
6562 case elfcpp::Tag_conformance
:
6563 // Keep the attribute if it matches. Throw it away otherwise.
6564 // No attribute means no claim to conform.
6565 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
6566 out_attr
[i
].set_string_value("");
6571 const char* err_object
= NULL
;
6573 // The "known_obj_attributes" table does contain some undefined
6574 // attributes. Ensure that there are unused.
6575 if (out_attr
[i
].int_value() != 0
6576 || out_attr
[i
].string_value() != "")
6577 err_object
= "output";
6578 else if (in_attr
[i
].int_value() != 0
6579 || in_attr
[i
].string_value() != "")
6582 if (err_object
!= NULL
)
6584 // Attribute numbers >=64 (mod 128) can be safely ignored.
6586 gold_error(_("%s: unknown mandatory EABI object attribute "
6590 gold_warning(_("%s: unknown EABI object attribute %d"),
6594 // Only pass on attributes that match in both inputs.
6595 if (!in_attr
[i
].matches(out_attr
[i
]))
6597 out_attr
[i
].set_int_value(0);
6598 out_attr
[i
].set_string_value("");
6603 // If out_attr was copied from in_attr then it won't have a type yet.
6604 if (in_attr
[i
].type() && !out_attr
[i
].type())
6605 out_attr
[i
].set_type(in_attr
[i
].type());
6608 // Merge Tag_compatibility attributes and any common GNU ones.
6609 this->attributes_section_data_
->merge(name
, pasd
);
6611 // Check for any attributes not known on ARM.
6612 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
6613 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
6614 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
6615 Other_attributes
* out_other_attributes
=
6616 this->attributes_section_data_
->other_attributes(vendor
);
6617 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
6619 while (in_iter
!= in_other_attributes
->end()
6620 || out_iter
!= out_other_attributes
->end())
6622 const char* err_object
= NULL
;
6625 // The tags for each list are in numerical order.
6626 // If the tags are equal, then merge.
6627 if (out_iter
!= out_other_attributes
->end()
6628 && (in_iter
== in_other_attributes
->end()
6629 || in_iter
->first
> out_iter
->first
))
6631 // This attribute only exists in output. We can't merge, and we
6632 // don't know what the tag means, so delete it.
6633 err_object
= "output";
6634 err_tag
= out_iter
->first
;
6635 int saved_tag
= out_iter
->first
;
6636 delete out_iter
->second
;
6637 out_other_attributes
->erase(out_iter
);
6638 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6640 else if (in_iter
!= in_other_attributes
->end()
6641 && (out_iter
!= out_other_attributes
->end()
6642 || in_iter
->first
< out_iter
->first
))
6644 // This attribute only exists in input. We can't merge, and we
6645 // don't know what the tag means, so ignore it.
6647 err_tag
= in_iter
->first
;
6650 else // The tags are equal.
6652 // As present, all attributes in the list are unknown, and
6653 // therefore can't be merged meaningfully.
6654 err_object
= "output";
6655 err_tag
= out_iter
->first
;
6657 // Only pass on attributes that match in both inputs.
6658 if (!in_iter
->second
->matches(*(out_iter
->second
)))
6660 // No match. Delete the attribute.
6661 int saved_tag
= out_iter
->first
;
6662 delete out_iter
->second
;
6663 out_other_attributes
->erase(out_iter
);
6664 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6668 // Matched. Keep the attribute and move to the next.
6676 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6677 if ((err_tag
& 127) < 64)
6679 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6680 err_object
, err_tag
);
6684 gold_warning(_("%s: unknown EABI object attribute %d"),
6685 err_object
, err_tag
);
6691 // Return whether a relocation type used the LSB to distinguish THUMB
6693 template<bool big_endian
>
6695 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
6699 case elfcpp::R_ARM_PC24
:
6700 case elfcpp::R_ARM_ABS32
:
6701 case elfcpp::R_ARM_REL32
:
6702 case elfcpp::R_ARM_SBREL32
:
6703 case elfcpp::R_ARM_THM_CALL
:
6704 case elfcpp::R_ARM_GLOB_DAT
:
6705 case elfcpp::R_ARM_JUMP_SLOT
:
6706 case elfcpp::R_ARM_GOTOFF32
:
6707 case elfcpp::R_ARM_PLT32
:
6708 case elfcpp::R_ARM_CALL
:
6709 case elfcpp::R_ARM_JUMP24
:
6710 case elfcpp::R_ARM_THM_JUMP24
:
6711 case elfcpp::R_ARM_SBREL31
:
6712 case elfcpp::R_ARM_PREL31
:
6713 case elfcpp::R_ARM_MOVW_ABS_NC
:
6714 case elfcpp::R_ARM_MOVW_PREL_NC
:
6715 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6716 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6717 case elfcpp::R_ARM_THM_JUMP19
:
6718 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
6719 case elfcpp::R_ARM_ALU_PC_G0_NC
:
6720 case elfcpp::R_ARM_ALU_PC_G0
:
6721 case elfcpp::R_ARM_ALU_PC_G1_NC
:
6722 case elfcpp::R_ARM_ALU_PC_G1
:
6723 case elfcpp::R_ARM_ALU_PC_G2
:
6724 case elfcpp::R_ARM_ALU_SB_G0_NC
:
6725 case elfcpp::R_ARM_ALU_SB_G0
:
6726 case elfcpp::R_ARM_ALU_SB_G1_NC
:
6727 case elfcpp::R_ARM_ALU_SB_G1
:
6728 case elfcpp::R_ARM_ALU_SB_G2
:
6729 case elfcpp::R_ARM_MOVW_BREL_NC
:
6730 case elfcpp::R_ARM_MOVW_BREL
:
6731 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
6732 case elfcpp::R_ARM_THM_MOVW_BREL
:
6739 // Stub-generation methods for Target_arm.
6741 // Make a new Arm_input_section object.
6743 template<bool big_endian
>
6744 Arm_input_section
<big_endian
>*
6745 Target_arm
<big_endian
>::new_arm_input_section(
6749 Input_section_specifier
iss(relobj
, shndx
);
6751 Arm_input_section
<big_endian
>* arm_input_section
=
6752 new Arm_input_section
<big_endian
>(relobj
, shndx
);
6753 arm_input_section
->init();
6755 // Register new Arm_input_section in map for look-up.
6756 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
6757 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
6759 // Make sure that it we have not created another Arm_input_section
6760 // for this input section already.
6761 gold_assert(ins
.second
);
6763 return arm_input_section
;
6766 // Find the Arm_input_section object corresponding to the SHNDX-th input
6767 // section of RELOBJ.
6769 template<bool big_endian
>
6770 Arm_input_section
<big_endian
>*
6771 Target_arm
<big_endian
>::find_arm_input_section(
6773 unsigned int shndx
) const
6775 Input_section_specifier
iss(relobj
, shndx
);
6776 typename
Arm_input_section_map::const_iterator p
=
6777 this->arm_input_section_map_
.find(iss
);
6778 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
6781 // Make a new stub table.
6783 template<bool big_endian
>
6784 Stub_table
<big_endian
>*
6785 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
6787 Stub_table
<big_endian
>* stub_table
=
6788 new Stub_table
<big_endian
>(owner
);
6789 this->stub_tables_
.push_back(stub_table
);
6791 stub_table
->set_address(owner
->address() + owner
->data_size());
6792 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
6793 stub_table
->finalize_data_size();
6798 // Scan a relocation for stub generation.
6800 template<bool big_endian
>
6802 Target_arm
<big_endian
>::scan_reloc_for_stub(
6803 const Relocate_info
<32, big_endian
>* relinfo
,
6804 unsigned int r_type
,
6805 const Sized_symbol
<32>* gsym
,
6807 const Symbol_value
<32>* psymval
,
6808 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
6809 Arm_address address
)
6811 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
6813 const Arm_relobj
<big_endian
>* arm_relobj
=
6814 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6816 bool target_is_thumb
;
6817 Symbol_value
<32> symval
;
6820 // This is a global symbol. Determine if we use PLT and if the
6821 // final target is THUMB.
6822 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
6824 // This uses a PLT, change the symbol value.
6825 symval
.set_output_value(this->plt_section()->address()
6826 + gsym
->plt_offset());
6828 target_is_thumb
= false;
6830 else if (gsym
->is_undefined())
6831 // There is no need to generate a stub symbol is undefined.
6836 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6837 || (gsym
->type() == elfcpp::STT_FUNC
6838 && !gsym
->is_undefined()
6839 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
6844 // This is a local symbol. Determine if the final target is THUMB.
6845 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
6848 // Strip LSB if this points to a THUMB target.
6850 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6851 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
6853 Arm_address stripped_value
=
6854 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
6855 symval
.set_output_value(stripped_value
);
6859 // Get the symbol value.
6860 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
6862 // Owing to pipelining, the PC relative branches below actually skip
6863 // two instructions when the branch offset is 0.
6864 Arm_address destination
;
6867 case elfcpp::R_ARM_CALL
:
6868 case elfcpp::R_ARM_JUMP24
:
6869 case elfcpp::R_ARM_PLT32
:
6871 destination
= value
+ addend
+ 8;
6873 case elfcpp::R_ARM_THM_CALL
:
6874 case elfcpp::R_ARM_THM_XPC22
:
6875 case elfcpp::R_ARM_THM_JUMP24
:
6876 case elfcpp::R_ARM_THM_JUMP19
:
6878 destination
= value
+ addend
+ 4;
6884 Stub_type stub_type
=
6885 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
6888 // This reloc does not need a stub.
6889 if (stub_type
== arm_stub_none
)
6892 // Try looking up an existing stub from a stub table.
6893 Stub_table
<big_endian
>* stub_table
=
6894 arm_relobj
->stub_table(relinfo
->data_shndx
);
6895 gold_assert(stub_table
!= NULL
);
6897 // Locate stub by destination.
6898 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
6900 // Create a stub if there is not one already
6901 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
6904 // create a new stub and add it to stub table.
6905 stub
= this->stub_factory().make_reloc_stub(stub_type
);
6906 stub_table
->add_reloc_stub(stub
, stub_key
);
6909 // Record the destination address.
6910 stub
->set_destination_address(destination
6911 | (target_is_thumb
? 1 : 0));
6914 // This function scans a relocation sections for stub generation.
6915 // The template parameter Relocate must be a class type which provides
6916 // a single function, relocate(), which implements the machine
6917 // specific part of a relocation.
6919 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
6920 // SHT_REL or SHT_RELA.
6922 // PRELOCS points to the relocation data. RELOC_COUNT is the number
6923 // of relocs. OUTPUT_SECTION is the output section.
6924 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6925 // mapped to output offsets.
6927 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
6928 // VIEW_SIZE is the size. These refer to the input section, unless
6929 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6930 // the output section.
6932 template<bool big_endian
>
6933 template<int sh_type
>
6935 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
6936 const Relocate_info
<32, big_endian
>* relinfo
,
6937 const unsigned char* prelocs
,
6939 Output_section
* output_section
,
6940 bool needs_special_offset_handling
,
6941 const unsigned char* view
,
6942 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
6945 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
6946 const int reloc_size
=
6947 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
6949 Arm_relobj
<big_endian
>* arm_object
=
6950 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6951 unsigned int local_count
= arm_object
->local_symbol_count();
6953 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
6955 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6957 Reltype
reloc(prelocs
);
6959 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
6960 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6961 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
6963 r_type
= this->get_real_reloc_type(r_type
);
6965 // Only a few relocation types need stubs.
6966 if ((r_type
!= elfcpp::R_ARM_CALL
)
6967 && (r_type
!= elfcpp::R_ARM_JUMP24
)
6968 && (r_type
!= elfcpp::R_ARM_PLT32
)
6969 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
6970 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
6971 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
6972 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
6975 section_offset_type offset
=
6976 convert_to_section_size_type(reloc
.get_r_offset());
6978 if (needs_special_offset_handling
)
6980 offset
= output_section
->output_offset(relinfo
->object
,
6981 relinfo
->data_shndx
,
6988 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
6989 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
6990 stub_addend_reader(r_type
, view
+ offset
, reloc
);
6992 const Sized_symbol
<32>* sym
;
6994 Symbol_value
<32> symval
;
6995 const Symbol_value
<32> *psymval
;
6996 if (r_sym
< local_count
)
6999 psymval
= arm_object
->local_symbol(r_sym
);
7001 // If the local symbol belongs to a section we are discarding,
7002 // and that section is a debug section, try to find the
7003 // corresponding kept section and map this symbol to its
7004 // counterpart in the kept section. The symbol must not
7005 // correspond to a section we are folding.
7007 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7009 && shndx
!= elfcpp::SHN_UNDEF
7010 && !arm_object
->is_section_included(shndx
)
7011 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
7013 if (comdat_behavior
== CB_UNDETERMINED
)
7016 arm_object
->section_name(relinfo
->data_shndx
);
7017 comdat_behavior
= get_comdat_behavior(name
.c_str());
7019 if (comdat_behavior
== CB_PRETEND
)
7022 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
7023 arm_object
->map_to_kept_section(shndx
, &found
);
7025 symval
.set_output_value(value
+ psymval
->input_value());
7027 symval
.set_output_value(0);
7031 symval
.set_output_value(0);
7033 symval
.set_no_output_symtab_entry();
7039 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
7040 gold_assert(gsym
!= NULL
);
7041 if (gsym
->is_forwarder())
7042 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7044 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
7045 if (sym
->has_symtab_index())
7046 symval
.set_output_symtab_index(sym
->symtab_index());
7048 symval
.set_no_output_symtab_entry();
7050 // We need to compute the would-be final value of this global
7052 const Symbol_table
* symtab
= relinfo
->symtab
;
7053 const Sized_symbol
<32>* sized_symbol
=
7054 symtab
->get_sized_symbol
<32>(gsym
);
7055 Symbol_table::Compute_final_value_status status
;
7057 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
7059 // Skip this if the symbol has not output section.
7060 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
7063 symval
.set_output_value(value
);
7067 // If symbol is a section symbol, we don't know the actual type of
7068 // destination. Give up.
7069 if (psymval
->is_section_symbol())
7072 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
7073 addend
, view_address
+ offset
);
7077 // Scan an input section for stub generation.
7079 template<bool big_endian
>
7081 Target_arm
<big_endian
>::scan_section_for_stubs(
7082 const Relocate_info
<32, big_endian
>* relinfo
,
7083 unsigned int sh_type
,
7084 const unsigned char* prelocs
,
7086 Output_section
* output_section
,
7087 bool needs_special_offset_handling
,
7088 const unsigned char* view
,
7089 Arm_address view_address
,
7090 section_size_type view_size
)
7092 if (sh_type
== elfcpp::SHT_REL
)
7093 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
7098 needs_special_offset_handling
,
7102 else if (sh_type
== elfcpp::SHT_RELA
)
7103 // We do not support RELA type relocations yet. This is provided for
7105 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
7110 needs_special_offset_handling
,
7118 // Group input sections for stub generation.
7120 // We goup input sections in an output sections so that the total size,
7121 // including any padding space due to alignment is smaller than GROUP_SIZE
7122 // unless the only input section in group is bigger than GROUP_SIZE already.
7123 // Then an ARM stub table is created to follow the last input section
7124 // in group. For each group an ARM stub table is created an is placed
7125 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7126 // extend the group after the stub table.
7128 template<bool big_endian
>
7130 Target_arm
<big_endian
>::group_sections(
7132 section_size_type group_size
,
7133 bool stubs_always_after_branch
)
7135 // Group input sections and insert stub table
7136 Layout::Section_list section_list
;
7137 layout
->get_allocated_sections(§ion_list
);
7138 for (Layout::Section_list::const_iterator p
= section_list
.begin();
7139 p
!= section_list
.end();
7142 Arm_output_section
<big_endian
>* output_section
=
7143 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
7144 output_section
->group_sections(group_size
, stubs_always_after_branch
,
7149 // Relaxation hook. This is where we do stub generation.
7151 template<bool big_endian
>
7153 Target_arm
<big_endian
>::do_relax(
7155 const Input_objects
* input_objects
,
7156 Symbol_table
* symtab
,
7159 // No need to generate stubs if this is a relocatable link.
7160 gold_assert(!parameters
->options().relocatable());
7162 // If this is the first pass, we need to group input sections into
7166 // Determine the stub group size. The group size is the absolute
7167 // value of the parameter --stub-group-size. If --stub-group-size
7168 // is passed a negative value, we restict stubs to be always after
7169 // the stubbed branches.
7170 int32_t stub_group_size_param
=
7171 parameters
->options().stub_group_size();
7172 bool stubs_always_after_branch
= stub_group_size_param
< 0;
7173 section_size_type stub_group_size
= abs(stub_group_size_param
);
7175 if (stub_group_size
== 1)
7178 // Thumb branch range is +-4MB has to be used as the default
7179 // maximum size (a given section can contain both ARM and Thumb
7180 // code, so the worst case has to be taken into account).
7182 // This value is 24K less than that, which allows for 2025
7183 // 12-byte stubs. If we exceed that, then we will fail to link.
7184 // The user will have to relink with an explicit group size
7186 stub_group_size
= 4170000;
7189 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
7192 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
7194 // scan relocs for stubs
7195 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
7196 op
!= input_objects
->relobj_end();
7199 Arm_relobj
<big_endian
>* arm_relobj
=
7200 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
7201 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
7204 // Check all stub tables to see if any of them have their data sizes
7205 // or addresses alignments changed. These are the only things that
7207 bool any_stub_table_changed
= false;
7208 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7209 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7212 if ((*sp
)->update_data_size_and_addralign())
7213 any_stub_table_changed
= true;
7216 // Finalize the stubs in the last relaxation pass.
7217 if (!any_stub_table_changed
)
7218 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
7219 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
7221 (*sp
)->finalize_stubs();
7223 return any_stub_table_changed
;
7228 template<bool big_endian
>
7230 Target_arm
<big_endian
>::relocate_stub(
7232 const Relocate_info
<32, big_endian
>* relinfo
,
7233 Output_section
* output_section
,
7234 unsigned char* view
,
7235 Arm_address address
,
7236 section_size_type view_size
)
7239 const Stub_template
* stub_template
= stub
->stub_template();
7240 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
7242 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
7243 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
7245 unsigned int r_type
= insn
->r_type();
7246 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
7247 section_size_type reloc_size
= insn
->size();
7248 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
7250 // This is the address of the stub destination.
7251 Arm_address target
= stub
->reloc_target(i
);
7252 Symbol_value
<32> symval
;
7253 symval
.set_output_value(target
);
7255 // Synthesize a fake reloc just in case. We don't have a symbol so
7257 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
7258 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
7259 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
7260 reloc_write
.put_r_offset(reloc_offset
);
7261 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
7262 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
7264 relocate
.relocate(relinfo
, this, output_section
,
7265 this->fake_relnum_for_stubs
, rel
, r_type
,
7266 NULL
, &symval
, view
+ reloc_offset
,
7267 address
+ reloc_offset
, reloc_size
);
7271 // Determine whether an object attribute tag takes an integer, a
7274 template<bool big_endian
>
7276 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
7278 if (tag
== Object_attribute::Tag_compatibility
)
7279 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7280 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
7281 else if (tag
== elfcpp::Tag_nodefaults
)
7282 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7283 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
7284 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
7285 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
7287 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
7289 return ((tag
& 1) != 0
7290 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7291 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
7294 // Reorder attributes.
7296 // The ABI defines that Tag_conformance should be emitted first, and that
7297 // Tag_nodefaults should be second (if either is defined). This sets those
7298 // two positions, and bumps up the position of all the remaining tags to
7301 template<bool big_endian
>
7303 Target_arm
<big_endian
>::do_attributes_order(int num
) const
7305 // Reorder the known object attributes in output. We want to move
7306 // Tag_conformance to position 4 and Tag_conformance to position 5
7307 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7309 return elfcpp::Tag_conformance
;
7311 return elfcpp::Tag_nodefaults
;
7312 if ((num
- 2) < elfcpp::Tag_nodefaults
)
7314 if ((num
- 1) < elfcpp::Tag_conformance
)
7319 template<bool big_endian
>
7320 class Target_selector_arm
: public Target_selector
7323 Target_selector_arm()
7324 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
7325 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
7329 do_instantiate_target()
7330 { return new Target_arm
<big_endian
>(); }
7333 Target_selector_arm
<false> target_selector_arm
;
7334 Target_selector_arm
<true> target_selector_armbe
;
7336 } // End anonymous namespace.