1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
37 #include "parameters.h"
44 #include "copy-relocs.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
51 #include "attributes.h"
58 template<bool big_endian
>
59 class Output_data_plt_arm
;
61 template<bool big_endian
>
64 template<bool big_endian
>
65 class Arm_input_section
;
67 class Arm_exidx_cantunwind
;
69 class Arm_exidx_merged_section
;
71 template<bool big_endian
>
72 class Arm_output_section
;
74 class Arm_exidx_input_section
;
76 template<bool big_endian
>
79 template<bool big_endian
>
83 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
85 // Maximum branch offsets for ARM, THUMB and THUMB2.
86 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
87 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
88 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
89 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
90 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
91 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
93 // The arm target class.
95 // This is a very simple port of gold for ARM-EABI. It is intended for
96 // supporting Android only for the time being. Only these relocation types
125 // R_ARM_THM_MOVW_ABS_NC
126 // R_ARM_THM_MOVT_ABS
127 // R_ARM_MOVW_PREL_NC
129 // R_ARM_THM_MOVW_PREL_NC
130 // R_ARM_THM_MOVT_PREL
137 // - Support more relocation types as needed.
138 // - Make PLTs more flexible for different architecture features like
140 // There are probably a lot more.
142 // Instruction template class. This class is similar to the insn_sequence
143 // struct in bfd/elf32-arm.c.
148 // Types of instruction templates.
152 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
153 // templates with class-specific semantics. Currently this is used
154 // only by the Cortex_a8_stub class for handling condition codes in
155 // conditional branches.
156 THUMB16_SPECIAL_TYPE
,
162 // Factory methods to create instruction templates in different formats.
164 static const Insn_template
165 thumb16_insn(uint32_t data
)
166 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
168 // A Thumb conditional branch, in which the proper condition is inserted
169 // when we build the stub.
170 static const Insn_template
171 thumb16_bcond_insn(uint32_t data
)
172 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
174 static const Insn_template
175 thumb32_insn(uint32_t data
)
176 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
178 static const Insn_template
179 thumb32_b_insn(uint32_t data
, int reloc_addend
)
181 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
185 static const Insn_template
186 arm_insn(uint32_t data
)
187 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
189 static const Insn_template
190 arm_rel_insn(unsigned data
, int reloc_addend
)
191 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
193 static const Insn_template
194 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
195 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
197 // Accessors. This class is used for read-only objects so no modifiers
202 { return this->data_
; }
204 // Return the instruction sequence type of this.
207 { return this->type_
; }
209 // Return the ARM relocation type of this.
212 { return this->r_type_
; }
216 { return this->reloc_addend_
; }
218 // Return size of instruction template in bytes.
222 // Return byte-alignment of instruction template.
227 // We make the constructor private to ensure that only the factory
230 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
231 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
234 // Instruction specific data. This is used to store information like
235 // some of the instruction bits.
237 // Instruction template type.
239 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
240 unsigned int r_type_
;
241 // Relocation addend.
242 int32_t reloc_addend_
;
245 // Macro for generating code to stub types. One entry per long/short
249 DEF_STUB(long_branch_any_any) \
250 DEF_STUB(long_branch_v4t_arm_thumb) \
251 DEF_STUB(long_branch_thumb_only) \
252 DEF_STUB(long_branch_v4t_thumb_thumb) \
253 DEF_STUB(long_branch_v4t_thumb_arm) \
254 DEF_STUB(short_branch_v4t_thumb_arm) \
255 DEF_STUB(long_branch_any_arm_pic) \
256 DEF_STUB(long_branch_any_thumb_pic) \
257 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
258 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
259 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
260 DEF_STUB(long_branch_thumb_only_pic) \
261 DEF_STUB(a8_veneer_b_cond) \
262 DEF_STUB(a8_veneer_b) \
263 DEF_STUB(a8_veneer_bl) \
264 DEF_STUB(a8_veneer_blx) \
265 DEF_STUB(v4_veneer_bx)
269 #define DEF_STUB(x) arm_stub_##x,
275 // First reloc stub type.
276 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
277 // Last reloc stub type.
278 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
280 // First Cortex-A8 stub type.
281 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
282 // Last Cortex-A8 stub type.
283 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
286 arm_stub_type_last
= arm_stub_v4_veneer_bx
290 // Stub template class. Templates are meant to be read-only objects.
291 // A stub template for a stub type contains all read-only attributes
292 // common to all stubs of the same type.
297 Stub_template(Stub_type
, const Insn_template
*, size_t);
305 { return this->type_
; }
307 // Return an array of instruction templates.
310 { return this->insns_
; }
312 // Return size of template in number of instructions.
315 { return this->insn_count_
; }
317 // Return size of template in bytes.
320 { return this->size_
; }
322 // Return alignment of the stub template.
325 { return this->alignment_
; }
327 // Return whether entry point is in thumb mode.
329 entry_in_thumb_mode() const
330 { return this->entry_in_thumb_mode_
; }
332 // Return number of relocations in this template.
335 { return this->relocs_
.size(); }
337 // Return index of the I-th instruction with relocation.
339 reloc_insn_index(size_t i
) const
341 gold_assert(i
< this->relocs_
.size());
342 return this->relocs_
[i
].first
;
345 // Return the offset of the I-th instruction with relocation from the
346 // beginning of the stub.
348 reloc_offset(size_t i
) const
350 gold_assert(i
< this->relocs_
.size());
351 return this->relocs_
[i
].second
;
355 // This contains information about an instruction template with a relocation
356 // and its offset from start of stub.
357 typedef std::pair
<size_t, section_size_type
> Reloc
;
359 // A Stub_template may not be copied. We want to share templates as much
361 Stub_template(const Stub_template
&);
362 Stub_template
& operator=(const Stub_template
&);
366 // Points to an array of Insn_templates.
367 const Insn_template
* insns_
;
368 // Number of Insn_templates in insns_[].
370 // Size of templated instructions in bytes.
372 // Alignment of templated instructions.
374 // Flag to indicate if entry is in thumb mode.
375 bool entry_in_thumb_mode_
;
376 // A table of reloc instruction indices and offsets. We can find these by
377 // looking at the instruction templates but we pre-compute and then stash
378 // them here for speed.
379 std::vector
<Reloc
> relocs_
;
383 // A class for code stubs. This is a base class for different type of
384 // stubs used in the ARM target.
390 static const section_offset_type invalid_offset
=
391 static_cast<section_offset_type
>(-1);
394 Stub(const Stub_template
* stub_template
)
395 : stub_template_(stub_template
), offset_(invalid_offset
)
402 // Return the stub template.
404 stub_template() const
405 { return this->stub_template_
; }
407 // Return offset of code stub from beginning of its containing stub table.
411 gold_assert(this->offset_
!= invalid_offset
);
412 return this->offset_
;
415 // Set offset of code stub from beginning of its containing stub table.
417 set_offset(section_offset_type offset
)
418 { this->offset_
= offset
; }
420 // Return the relocation target address of the i-th relocation in the
421 // stub. This must be defined in a child class.
423 reloc_target(size_t i
)
424 { return this->do_reloc_target(i
); }
426 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
428 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
429 { this->do_write(view
, view_size
, big_endian
); }
431 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
432 // for the i-th instruction.
434 thumb16_special(size_t i
)
435 { return this->do_thumb16_special(i
); }
438 // This must be defined in the child class.
440 do_reloc_target(size_t) = 0;
442 // This may be overridden in the child class.
444 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
447 this->do_fixed_endian_write
<true>(view
, view_size
);
449 this->do_fixed_endian_write
<false>(view
, view_size
);
452 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
453 // instruction template.
455 do_thumb16_special(size_t)
456 { gold_unreachable(); }
459 // A template to implement do_write.
460 template<bool big_endian
>
462 do_fixed_endian_write(unsigned char*, section_size_type
);
465 const Stub_template
* stub_template_
;
466 // Offset within the section of containing this stub.
467 section_offset_type offset_
;
470 // Reloc stub class. These are stubs we use to fix up relocation because
471 // of limited branch ranges.
473 class Reloc_stub
: public Stub
476 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
477 // We assume we never jump to this address.
478 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
480 // Return destination address.
482 destination_address() const
484 gold_assert(this->destination_address_
!= this->invalid_address
);
485 return this->destination_address_
;
488 // Set destination address.
490 set_destination_address(Arm_address address
)
492 gold_assert(address
!= this->invalid_address
);
493 this->destination_address_
= address
;
496 // Reset destination address.
498 reset_destination_address()
499 { this->destination_address_
= this->invalid_address
; }
501 // Determine stub type for a branch of a relocation of R_TYPE going
502 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
503 // the branch target is a thumb instruction. TARGET is used for look
504 // up ARM-specific linker settings.
506 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
507 Arm_address branch_target
, bool target_is_thumb
);
509 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
510 // and an addend. Since we treat global and local symbol differently, we
511 // use a Symbol object for a global symbol and a object-index pair for
516 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
517 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
518 // and R_SYM must not be invalid_index.
519 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
520 unsigned int r_sym
, int32_t addend
)
521 : stub_type_(stub_type
), addend_(addend
)
525 this->r_sym_
= Reloc_stub::invalid_index
;
526 this->u_
.symbol
= symbol
;
530 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
531 this->r_sym_
= r_sym
;
532 this->u_
.relobj
= relobj
;
539 // Accessors: Keys are meant to be read-only object so no modifiers are
545 { return this->stub_type_
; }
547 // Return the local symbol index or invalid_index.
550 { return this->r_sym_
; }
552 // Return the symbol if there is one.
555 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
557 // Return the relobj if there is one.
560 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
562 // Whether this equals to another key k.
564 eq(const Key
& k
) const
566 return ((this->stub_type_
== k
.stub_type_
)
567 && (this->r_sym_
== k
.r_sym_
)
568 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
569 ? (this->u_
.relobj
== k
.u_
.relobj
)
570 : (this->u_
.symbol
== k
.u_
.symbol
))
571 && (this->addend_
== k
.addend_
));
574 // Return a hash value.
578 return (this->stub_type_
580 ^ gold::string_hash
<char>(
581 (this->r_sym_
!= Reloc_stub::invalid_index
)
582 ? this->u_
.relobj
->name().c_str()
583 : this->u_
.symbol
->name())
587 // Functors for STL associative containers.
591 operator()(const Key
& k
) const
592 { return k
.hash_value(); }
598 operator()(const Key
& k1
, const Key
& k2
) const
599 { return k1
.eq(k2
); }
602 // Name of key. This is mainly for debugging.
608 Stub_type stub_type_
;
609 // If this is a local symbol, this is the index in the defining object.
610 // Otherwise, it is invalid_index for a global symbol.
612 // If r_sym_ is invalid index. This points to a global symbol.
613 // Otherwise, this points a relobj. We used the unsized and target
614 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
615 // Arm_relobj. This is done to avoid making the stub class a template
616 // as most of the stub machinery is endianity-neutral. However, it
617 // may require a bit of casting done by users of this class.
620 const Symbol
* symbol
;
621 const Relobj
* relobj
;
623 // Addend associated with a reloc.
628 // Reloc_stubs are created via a stub factory. So these are protected.
629 Reloc_stub(const Stub_template
* stub_template
)
630 : Stub(stub_template
), destination_address_(invalid_address
)
636 friend class Stub_factory
;
638 // Return the relocation target address of the i-th relocation in the
641 do_reloc_target(size_t i
)
643 // All reloc stub have only one relocation.
645 return this->destination_address_
;
649 // Address of destination.
650 Arm_address destination_address_
;
653 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
654 // THUMB branch that meets the following conditions:
656 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
657 // branch address is 0xffe.
658 // 2. The branch target address is in the same page as the first word of the
660 // 3. The branch follows a 32-bit instruction which is not a branch.
662 // To do the fix up, we need to store the address of the branch instruction
663 // and its target at least. We also need to store the original branch
664 // instruction bits for the condition code in a conditional branch. The
665 // condition code is used in a special instruction template. We also want
666 // to identify input sections needing Cortex-A8 workaround quickly. We store
667 // extra information about object and section index of the code section
668 // containing a branch being fixed up. The information is used to mark
669 // the code section when we finalize the Cortex-A8 stubs.
672 class Cortex_a8_stub
: public Stub
678 // Return the object of the code section containing the branch being fixed
682 { return this->relobj_
; }
684 // Return the section index of the code section containing the branch being
688 { return this->shndx_
; }
690 // Return the source address of stub. This is the address of the original
691 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
694 source_address() const
695 { return this->source_address_
; }
697 // Return the destination address of the stub. This is the branch taken
698 // address of the original branch instruction. LSB is 1 if it is a THUMB
699 // instruction address.
701 destination_address() const
702 { return this->destination_address_
; }
704 // Return the instruction being fixed up.
706 original_insn() const
707 { return this->original_insn_
; }
710 // Cortex_a8_stubs are created via a stub factory. So these are protected.
711 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
712 unsigned int shndx
, Arm_address source_address
,
713 Arm_address destination_address
, uint32_t original_insn
)
714 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
715 source_address_(source_address
| 1U),
716 destination_address_(destination_address
),
717 original_insn_(original_insn
)
720 friend class Stub_factory
;
722 // Return the relocation target address of the i-th relocation in the
725 do_reloc_target(size_t i
)
727 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
729 // The conditional branch veneer has two relocations.
731 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
735 // All other Cortex-A8 stubs have only one relocation.
737 return this->destination_address_
;
741 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
743 do_thumb16_special(size_t);
746 // Object of the code section containing the branch being fixed up.
748 // Section index of the code section containing the branch begin fixed up.
750 // Source address of original branch.
751 Arm_address source_address_
;
752 // Destination address of the original branch.
753 Arm_address destination_address_
;
754 // Original branch instruction. This is needed for copying the condition
755 // code from a condition branch to its stub.
756 uint32_t original_insn_
;
759 // ARMv4 BX Rx branch relocation stub class.
760 class Arm_v4bx_stub
: public Stub
766 // Return the associated register.
769 { return this->reg_
; }
772 // Arm V4BX stubs are created via a stub factory. So these are protected.
773 Arm_v4bx_stub(const Stub_template
* stub_template
, const uint32_t reg
)
774 : Stub(stub_template
), reg_(reg
)
777 friend class Stub_factory
;
779 // Return the relocation target address of the i-th relocation in the
782 do_reloc_target(size_t)
783 { gold_unreachable(); }
785 // This may be overridden in the child class.
787 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
790 this->do_fixed_endian_v4bx_write
<true>(view
, view_size
);
792 this->do_fixed_endian_v4bx_write
<false>(view
, view_size
);
796 // A template to implement do_write.
797 template<bool big_endian
>
799 do_fixed_endian_v4bx_write(unsigned char* view
, section_size_type
)
801 const Insn_template
* insns
= this->stub_template()->insns();
802 elfcpp::Swap
<32, big_endian
>::writeval(view
,
804 + (this->reg_
<< 16)));
805 view
+= insns
[0].size();
806 elfcpp::Swap
<32, big_endian
>::writeval(view
,
807 (insns
[1].data() + this->reg_
));
808 view
+= insns
[1].size();
809 elfcpp::Swap
<32, big_endian
>::writeval(view
,
810 (insns
[2].data() + this->reg_
));
813 // A register index (r0-r14), which is associated with the stub.
817 // Stub factory class.
822 // Return the unique instance of this class.
823 static const Stub_factory
&
826 static Stub_factory singleton
;
830 // Make a relocation stub.
832 make_reloc_stub(Stub_type stub_type
) const
834 gold_assert(stub_type
>= arm_stub_reloc_first
835 && stub_type
<= arm_stub_reloc_last
);
836 return new Reloc_stub(this->stub_templates_
[stub_type
]);
839 // Make a Cortex-A8 stub.
841 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
842 Arm_address source
, Arm_address destination
,
843 uint32_t original_insn
) const
845 gold_assert(stub_type
>= arm_stub_cortex_a8_first
846 && stub_type
<= arm_stub_cortex_a8_last
);
847 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
848 source
, destination
, original_insn
);
851 // Make an ARM V4BX relocation stub.
852 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
854 make_arm_v4bx_stub(uint32_t reg
) const
856 gold_assert(reg
< 0xf);
857 return new Arm_v4bx_stub(this->stub_templates_
[arm_stub_v4_veneer_bx
],
862 // Constructor and destructor are protected since we only return a single
863 // instance created in Stub_factory::get_instance().
867 // A Stub_factory may not be copied since it is a singleton.
868 Stub_factory(const Stub_factory
&);
869 Stub_factory
& operator=(Stub_factory
&);
871 // Stub templates. These are initialized in the constructor.
872 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
875 // A class to hold stubs for the ARM target.
877 template<bool big_endian
>
878 class Stub_table
: public Output_data
881 Stub_table(Arm_input_section
<big_endian
>* owner
)
882 : Output_data(), owner_(owner
), reloc_stubs_(), cortex_a8_stubs_(),
883 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
889 // Owner of this stub table.
890 Arm_input_section
<big_endian
>*
892 { return this->owner_
; }
894 // Whether this stub table is empty.
898 return (this->reloc_stubs_
.empty()
899 && this->cortex_a8_stubs_
.empty()
900 && this->arm_v4bx_stubs_
.empty());
903 // Return the current data size.
905 current_data_size() const
906 { return this->current_data_size_for_child(); }
908 // Add a STUB with using KEY. Caller is reponsible for avoid adding
909 // if already a STUB with the same key has been added.
911 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
913 const Stub_template
* stub_template
= stub
->stub_template();
914 gold_assert(stub_template
->type() == key
.stub_type());
915 this->reloc_stubs_
[key
] = stub
;
918 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
919 // Caller is reponsible for avoid adding if already a STUB with the same
920 // address has been added.
922 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
924 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
925 this->cortex_a8_stubs_
.insert(value
);
928 // Add an ARM V4BX relocation stub. A register index will be retrieved
931 add_arm_v4bx_stub(Arm_v4bx_stub
* stub
)
933 gold_assert(stub
!= NULL
&& this->arm_v4bx_stubs_
[stub
->reg()] == NULL
);
934 this->arm_v4bx_stubs_
[stub
->reg()] = stub
;
937 // Remove all Cortex-A8 stubs.
939 remove_all_cortex_a8_stubs();
941 // Look up a relocation stub using KEY. Return NULL if there is none.
943 find_reloc_stub(const Reloc_stub::Key
& key
) const
945 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
946 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
949 // Look up an arm v4bx relocation stub using the register index.
950 // Return NULL if there is none.
952 find_arm_v4bx_stub(const uint32_t reg
) const
954 gold_assert(reg
< 0xf);
955 return this->arm_v4bx_stubs_
[reg
];
958 // Relocate stubs in this stub table.
960 relocate_stubs(const Relocate_info
<32, big_endian
>*,
961 Target_arm
<big_endian
>*, Output_section
*,
962 unsigned char*, Arm_address
, section_size_type
);
964 // Update data size and alignment at the end of a relaxation pass. Return
965 // true if either data size or alignment is different from that of the
966 // previous relaxation pass.
968 update_data_size_and_addralign();
970 // Finalize stubs. Set the offsets of all stubs and mark input sections
971 // needing the Cortex-A8 workaround.
975 // Apply Cortex-A8 workaround to an address range.
977 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
978 unsigned char*, Arm_address
,
982 // Write out section contents.
984 do_write(Output_file
*);
986 // Return the required alignment.
989 { return this->prev_addralign_
; }
991 // Reset address and file offset.
993 do_reset_address_and_file_offset()
994 { this->set_current_data_size_for_child(this->prev_data_size_
); }
996 // Set final data size.
998 set_final_data_size()
999 { this->set_data_size(this->current_data_size()); }
1002 // Relocate one stub.
1004 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1005 Target_arm
<big_endian
>*, Output_section
*,
1006 unsigned char*, Arm_address
, section_size_type
);
1008 // Unordered map of relocation stubs.
1010 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
1011 Reloc_stub::Key::equal_to
>
1014 // List of Cortex-A8 stubs ordered by addresses of branches being
1015 // fixed up in output.
1016 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
1017 // List of Arm V4BX relocation stubs ordered by associated registers.
1018 typedef std::vector
<Arm_v4bx_stub
*> Arm_v4bx_stub_list
;
1020 // Owner of this stub table.
1021 Arm_input_section
<big_endian
>* owner_
;
1022 // The relocation stubs.
1023 Reloc_stub_map reloc_stubs_
;
1024 // The cortex_a8_stubs.
1025 Cortex_a8_stub_list cortex_a8_stubs_
;
1026 // The Arm V4BX relocation stubs.
1027 Arm_v4bx_stub_list arm_v4bx_stubs_
;
1028 // data size of this in the previous pass.
1029 off_t prev_data_size_
;
1030 // address alignment of this in the previous pass.
1031 uint64_t prev_addralign_
;
1034 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1035 // we add to the end of an EXIDX input section that goes into the output.
1037 class Arm_exidx_cantunwind
: public Output_section_data
1040 Arm_exidx_cantunwind(Relobj
* relobj
, unsigned int shndx
)
1041 : Output_section_data(8, 4, true), relobj_(relobj
), shndx_(shndx
)
1044 // Return the object containing the section pointed by this.
1047 { return this->relobj_
; }
1049 // Return the section index of the section pointed by this.
1052 { return this->shndx_
; }
1056 do_write(Output_file
* of
)
1058 if (parameters
->target().is_big_endian())
1059 this->do_fixed_endian_write
<true>(of
);
1061 this->do_fixed_endian_write
<false>(of
);
1065 // Implement do_write for a given endianity.
1066 template<bool big_endian
>
1068 do_fixed_endian_write(Output_file
*);
1070 // The object containing the section pointed by this.
1072 // The section index of the section pointed by this.
1073 unsigned int shndx_
;
1076 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1077 // Offset map is used to map input section offset within the EXIDX section
1078 // to the output offset from the start of this EXIDX section.
1080 typedef std::map
<section_offset_type
, section_offset_type
>
1081 Arm_exidx_section_offset_map
;
1083 // Arm_exidx_merged_section class. This represents an EXIDX input section
1084 // with some of its entries merged.
1086 class Arm_exidx_merged_section
: public Output_relaxed_input_section
1089 // Constructor for Arm_exidx_merged_section.
1090 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1091 // SECTION_OFFSET_MAP points to a section offset map describing how
1092 // parts of the input section are mapped to output. DELETED_BYTES is
1093 // the number of bytes deleted from the EXIDX input section.
1094 Arm_exidx_merged_section(
1095 const Arm_exidx_input_section
& exidx_input_section
,
1096 const Arm_exidx_section_offset_map
& section_offset_map
,
1097 uint32_t deleted_bytes
);
1099 // Return the original EXIDX input section.
1100 const Arm_exidx_input_section
&
1101 exidx_input_section() const
1102 { return this->exidx_input_section_
; }
1104 // Return the section offset map.
1105 const Arm_exidx_section_offset_map
&
1106 section_offset_map() const
1107 { return this->section_offset_map_
; }
1110 // Write merged section into file OF.
1112 do_write(Output_file
* of
);
1115 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
1116 section_offset_type
*) const;
1119 // Original EXIDX input section.
1120 const Arm_exidx_input_section
& exidx_input_section_
;
1121 // Section offset map.
1122 const Arm_exidx_section_offset_map
& section_offset_map_
;
1125 // A class to wrap an ordinary input section containing executable code.
1127 template<bool big_endian
>
1128 class Arm_input_section
: public Output_relaxed_input_section
1131 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
1132 : Output_relaxed_input_section(relobj
, shndx
, 1),
1133 original_addralign_(1), original_size_(0), stub_table_(NULL
)
1136 ~Arm_input_section()
1143 // Whether this is a stub table owner.
1145 is_stub_table_owner() const
1146 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
1148 // Return the stub table.
1149 Stub_table
<big_endian
>*
1151 { return this->stub_table_
; }
1153 // Set the stub_table.
1155 set_stub_table(Stub_table
<big_endian
>* stub_table
)
1156 { this->stub_table_
= stub_table
; }
1158 // Downcast a base pointer to an Arm_input_section pointer. This is
1159 // not type-safe but we only use Arm_input_section not the base class.
1160 static Arm_input_section
<big_endian
>*
1161 as_arm_input_section(Output_relaxed_input_section
* poris
)
1162 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
1165 // Write data to output file.
1167 do_write(Output_file
*);
1169 // Return required alignment of this.
1171 do_addralign() const
1173 if (this->is_stub_table_owner())
1174 return std::max(this->stub_table_
->addralign(),
1175 this->original_addralign_
);
1177 return this->original_addralign_
;
1180 // Finalize data size.
1182 set_final_data_size();
1184 // Reset address and file offset.
1186 do_reset_address_and_file_offset();
1190 do_output_offset(const Relobj
* object
, unsigned int shndx
,
1191 section_offset_type offset
,
1192 section_offset_type
* poutput
) const
1194 if ((object
== this->relobj())
1195 && (shndx
== this->shndx())
1197 && (convert_types
<uint64_t, section_offset_type
>(offset
)
1198 <= this->original_size_
))
1208 // Copying is not allowed.
1209 Arm_input_section(const Arm_input_section
&);
1210 Arm_input_section
& operator=(const Arm_input_section
&);
1212 // Address alignment of the original input section.
1213 uint64_t original_addralign_
;
1214 // Section size of the original input section.
1215 uint64_t original_size_
;
1217 Stub_table
<big_endian
>* stub_table_
;
1220 // Arm output section class. This is defined mainly to add a number of
1221 // stub generation methods.
1223 template<bool big_endian
>
1224 class Arm_output_section
: public Output_section
1227 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1228 elfcpp::Elf_Xword flags
)
1229 : Output_section(name
, type
, flags
)
1232 ~Arm_output_section()
1235 // Group input sections for stub generation.
1237 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1239 // Downcast a base pointer to an Arm_output_section pointer. This is
1240 // not type-safe but we only use Arm_output_section not the base class.
1241 static Arm_output_section
<big_endian
>*
1242 as_arm_output_section(Output_section
* os
)
1243 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1247 typedef Output_section::Input_section Input_section
;
1248 typedef Output_section::Input_section_list Input_section_list
;
1250 // Create a stub group.
1251 void create_stub_group(Input_section_list::const_iterator
,
1252 Input_section_list::const_iterator
,
1253 Input_section_list::const_iterator
,
1254 Target_arm
<big_endian
>*,
1255 std::vector
<Output_relaxed_input_section
*>*);
1258 // Arm_exidx_input_section class. This represents an EXIDX input section.
1260 class Arm_exidx_input_section
1263 static const section_offset_type invalid_offset
=
1264 static_cast<section_offset_type
>(-1);
1266 Arm_exidx_input_section(Relobj
* relobj
, unsigned int shndx
,
1267 unsigned int link
, uint32_t size
, uint32_t addralign
)
1268 : relobj_(relobj
), shndx_(shndx
), link_(link
), size_(size
),
1269 addralign_(addralign
)
1272 ~Arm_exidx_input_section()
1275 // Accessors: This is a read-only class.
1277 // Return the object containing this EXIDX input section.
1280 { return this->relobj_
; }
1282 // Return the section index of this EXIDX input section.
1285 { return this->shndx_
; }
1287 // Return the section index of linked text section in the same object.
1290 { return this->link_
; }
1292 // Return size of the EXIDX input section.
1295 { return this->size_
; }
1297 // Reutnr address alignment of EXIDX input section.
1300 { return this->addralign_
; }
1303 // Object containing this.
1305 // Section index of this.
1306 unsigned int shndx_
;
1307 // text section linked to this in the same object.
1309 // Size of this. For ARM 32-bit is sufficient.
1311 // Address alignment of this. For ARM 32-bit is sufficient.
1312 uint32_t addralign_
;
1315 // Arm_relobj class.
1317 template<bool big_endian
>
1318 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1321 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1323 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1324 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1325 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1326 stub_tables_(), local_symbol_is_thumb_function_(),
1327 attributes_section_data_(NULL
), mapping_symbols_info_(),
1328 section_has_cortex_a8_workaround_(NULL
)
1332 { delete this->attributes_section_data_
; }
1334 // Return the stub table of the SHNDX-th section if there is one.
1335 Stub_table
<big_endian
>*
1336 stub_table(unsigned int shndx
) const
1338 gold_assert(shndx
< this->stub_tables_
.size());
1339 return this->stub_tables_
[shndx
];
1342 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1344 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1346 gold_assert(shndx
< this->stub_tables_
.size());
1347 this->stub_tables_
[shndx
] = stub_table
;
1350 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1351 // index. This is only valid after do_count_local_symbol is called.
1353 local_symbol_is_thumb_function(unsigned int r_sym
) const
1355 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1356 return this->local_symbol_is_thumb_function_
[r_sym
];
1359 // Scan all relocation sections for stub generation.
1361 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1364 // Convert regular input section with index SHNDX to a relaxed section.
1366 convert_input_section_to_relaxed_section(unsigned shndx
)
1368 // The stubs have relocations and we need to process them after writing
1369 // out the stubs. So relocation now must follow section write.
1370 this->invalidate_section_offset(shndx
);
1371 this->set_relocs_must_follow_section_writes();
1374 // Downcast a base pointer to an Arm_relobj pointer. This is
1375 // not type-safe but we only use Arm_relobj not the base class.
1376 static Arm_relobj
<big_endian
>*
1377 as_arm_relobj(Relobj
* relobj
)
1378 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1380 // Processor-specific flags in ELF file header. This is valid only after
1383 processor_specific_flags() const
1384 { return this->processor_specific_flags_
; }
1386 // Attribute section data This is the contents of the .ARM.attribute section
1388 const Attributes_section_data
*
1389 attributes_section_data() const
1390 { return this->attributes_section_data_
; }
1392 // Mapping symbol location.
1393 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1395 // Functor for STL container.
1396 struct Mapping_symbol_position_less
1399 operator()(const Mapping_symbol_position
& p1
,
1400 const Mapping_symbol_position
& p2
) const
1402 return (p1
.first
< p2
.first
1403 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1407 // We only care about the first character of a mapping symbol, so
1408 // we only store that instead of the whole symbol name.
1409 typedef std::map
<Mapping_symbol_position
, char,
1410 Mapping_symbol_position_less
> Mapping_symbols_info
;
1412 // Whether a section contains any Cortex-A8 workaround.
1414 section_has_cortex_a8_workaround(unsigned int shndx
) const
1416 return (this->section_has_cortex_a8_workaround_
!= NULL
1417 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1420 // Mark a section that has Cortex-A8 workaround.
1422 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1424 if (this->section_has_cortex_a8_workaround_
== NULL
)
1425 this->section_has_cortex_a8_workaround_
=
1426 new std::vector
<bool>(this->shnum(), false);
1427 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1430 // Return the EXIDX section of an text section with index SHNDX or NULL
1431 // if the text section has no associated EXIDX section.
1432 const Arm_exidx_input_section
*
1433 exidx_input_section_by_link(unsigned int shndx
) const
1435 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1436 return ((p
!= this->exidx_section_map_
.end()
1437 && p
->second
->link() == shndx
)
1442 // Return the EXIDX section with index SHNDX or NULL if there is none.
1443 const Arm_exidx_input_section
*
1444 exidx_input_section_by_shndx(unsigned shndx
) const
1446 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1447 return ((p
!= this->exidx_section_map_
.end()
1448 && p
->second
->shndx() == shndx
)
1454 // Post constructor setup.
1458 // Call parent's setup method.
1459 Sized_relobj
<32, big_endian
>::do_setup();
1461 // Initialize look-up tables.
1462 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1463 this->stub_tables_
.swap(empty_stub_table_list
);
1466 // Count the local symbols.
1468 do_count_local_symbols(Stringpool_template
<char>*,
1469 Stringpool_template
<char>*);
1472 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1473 const unsigned char* pshdrs
,
1474 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1476 // Read the symbol information.
1478 do_read_symbols(Read_symbols_data
* sd
);
1480 // Process relocs for garbage collection.
1482 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1486 // Whether a section needs to be scanned for relocation stubs.
1488 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1489 const Relobj::Output_sections
&,
1490 const Symbol_table
*);
1492 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1494 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1495 unsigned int, Output_section
*,
1496 const Symbol_table
*);
1498 // Scan a section for the Cortex-A8 erratum.
1500 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1501 unsigned int, Output_section
*,
1502 Target_arm
<big_endian
>*);
1504 // Make a new Arm_exidx_input_section object for EXIDX section with
1505 // index SHNDX and section header SHDR.
1507 make_exidx_input_section(unsigned int shndx
,
1508 const elfcpp::Shdr
<32, big_endian
>& shdr
);
1510 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1511 typedef Unordered_map
<unsigned int, const Arm_exidx_input_section
*>
1514 // List of stub tables.
1515 Stub_table_list stub_tables_
;
1516 // Bit vector to tell if a local symbol is a thumb function or not.
1517 // This is only valid after do_count_local_symbol is called.
1518 std::vector
<bool> local_symbol_is_thumb_function_
;
1519 // processor-specific flags in ELF file header.
1520 elfcpp::Elf_Word processor_specific_flags_
;
1521 // Object attributes if there is an .ARM.attributes section or NULL.
1522 Attributes_section_data
* attributes_section_data_
;
1523 // Mapping symbols information.
1524 Mapping_symbols_info mapping_symbols_info_
;
1525 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1526 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1527 // Map a text section to its associated .ARM.exidx section, if there is one.
1528 Exidx_section_map exidx_section_map_
;
1531 // Arm_dynobj class.
1533 template<bool big_endian
>
1534 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1537 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1538 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1539 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1540 processor_specific_flags_(0), attributes_section_data_(NULL
)
1544 { delete this->attributes_section_data_
; }
1546 // Downcast a base pointer to an Arm_relobj pointer. This is
1547 // not type-safe but we only use Arm_relobj not the base class.
1548 static Arm_dynobj
<big_endian
>*
1549 as_arm_dynobj(Dynobj
* dynobj
)
1550 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1552 // Processor-specific flags in ELF file header. This is valid only after
1555 processor_specific_flags() const
1556 { return this->processor_specific_flags_
; }
1558 // Attributes section data.
1559 const Attributes_section_data
*
1560 attributes_section_data() const
1561 { return this->attributes_section_data_
; }
1564 // Read the symbol information.
1566 do_read_symbols(Read_symbols_data
* sd
);
1569 // processor-specific flags in ELF file header.
1570 elfcpp::Elf_Word processor_specific_flags_
;
1571 // Object attributes if there is an .ARM.attributes section or NULL.
1572 Attributes_section_data
* attributes_section_data_
;
1575 // Functor to read reloc addends during stub generation.
1577 template<int sh_type
, bool big_endian
>
1578 struct Stub_addend_reader
1580 // Return the addend for a relocation of a particular type. Depending
1581 // on whether this is a REL or RELA relocation, read the addend from a
1582 // view or from a Reloc object.
1583 elfcpp::Elf_types
<32>::Elf_Swxword
1585 unsigned int /* r_type */,
1586 const unsigned char* /* view */,
1587 const typename Reloc_types
<sh_type
,
1588 32, big_endian
>::Reloc
& /* reloc */) const;
1591 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1593 template<bool big_endian
>
1594 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1596 elfcpp::Elf_types
<32>::Elf_Swxword
1599 const unsigned char*,
1600 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1603 // Specialized Stub_addend_reader for RELA type relocation sections.
1604 // We currently do not handle RELA type relocation sections but it is trivial
1605 // to implement the addend reader. This is provided for completeness and to
1606 // make it easier to add support for RELA relocation sections in the future.
1608 template<bool big_endian
>
1609 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1611 elfcpp::Elf_types
<32>::Elf_Swxword
1614 const unsigned char*,
1615 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1616 big_endian
>::Reloc
& reloc
) const
1617 { return reloc
.get_r_addend(); }
1620 // Cortex_a8_reloc class. We keep record of relocation that may need
1621 // the Cortex-A8 erratum workaround.
1623 class Cortex_a8_reloc
1626 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1627 Arm_address destination
)
1628 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1634 // Accessors: This is a read-only class.
1636 // Return the relocation stub associated with this relocation if there is
1640 { return this->reloc_stub_
; }
1642 // Return the relocation type.
1645 { return this->r_type_
; }
1647 // Return the destination address of the relocation. LSB stores the THUMB
1651 { return this->destination_
; }
1654 // Associated relocation stub if there is one, or NULL.
1655 const Reloc_stub
* reloc_stub_
;
1657 unsigned int r_type_
;
1658 // Destination address of this relocation. LSB is used to distinguish
1660 Arm_address destination_
;
1663 // Utilities for manipulating integers of up to 32-bits
1667 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1668 // an int32_t. NO_BITS must be between 1 to 32.
1669 template<int no_bits
>
1670 static inline int32_t
1671 sign_extend(uint32_t bits
)
1673 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1675 return static_cast<int32_t>(bits
);
1676 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1678 uint32_t top_bit
= 1U << (no_bits
- 1);
1679 int32_t as_signed
= static_cast<int32_t>(bits
);
1680 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1683 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1684 template<int no_bits
>
1686 has_overflow(uint32_t bits
)
1688 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1691 int32_t max
= (1 << (no_bits
- 1)) - 1;
1692 int32_t min
= -(1 << (no_bits
- 1));
1693 int32_t as_signed
= static_cast<int32_t>(bits
);
1694 return as_signed
> max
|| as_signed
< min
;
1697 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1698 // fits in the given number of bits as either a signed or unsigned value.
1699 // For example, has_signed_unsigned_overflow<8> would check
1700 // -128 <= bits <= 255
1701 template<int no_bits
>
1703 has_signed_unsigned_overflow(uint32_t bits
)
1705 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1708 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1709 int32_t min
= -(1 << (no_bits
- 1));
1710 int32_t as_signed
= static_cast<int32_t>(bits
);
1711 return as_signed
> max
|| as_signed
< min
;
1714 // Select bits from A and B using bits in MASK. For each n in [0..31],
1715 // the n-th bit in the result is chosen from the n-th bits of A and B.
1716 // A zero selects A and a one selects B.
1717 static inline uint32_t
1718 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1719 { return (a
& ~mask
) | (b
& mask
); }
1722 template<bool big_endian
>
1723 class Target_arm
: public Sized_target
<32, big_endian
>
1726 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1729 // When were are relocating a stub, we pass this as the relocation number.
1730 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1733 : Sized_target
<32, big_endian
>(&arm_info
),
1734 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1735 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1736 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1737 should_force_pic_veneer_(false), arm_input_section_map_(),
1738 attributes_section_data_(NULL
), fix_cortex_a8_(false),
1739 cortex_a8_relocs_info_(), fix_v4bx_(0)
1742 // Whether we can use BLX.
1745 { return this->may_use_blx_
; }
1747 // Set use-BLX flag.
1749 set_may_use_blx(bool value
)
1750 { this->may_use_blx_
= value
; }
1752 // Whether we force PCI branch veneers.
1754 should_force_pic_veneer() const
1755 { return this->should_force_pic_veneer_
; }
1757 // Set PIC veneer flag.
1759 set_should_force_pic_veneer(bool value
)
1760 { this->should_force_pic_veneer_
= value
; }
1762 // Whether we use THUMB-2 instructions.
1764 using_thumb2() const
1766 Object_attribute
* attr
=
1767 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1768 int arch
= attr
->int_value();
1769 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1772 // Whether we use THUMB/THUMB-2 instructions only.
1774 using_thumb_only() const
1776 Object_attribute
* attr
=
1777 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1778 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1779 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1781 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1782 return attr
->int_value() == 'M';
1785 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1787 may_use_arm_nop() const
1789 Object_attribute
* attr
=
1790 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1791 int arch
= attr
->int_value();
1792 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1793 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1794 || arch
== elfcpp::TAG_CPU_ARCH_V7
1795 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1798 // Whether we have THUMB-2 NOP.W instruction.
1800 may_use_thumb2_nop() const
1802 Object_attribute
* attr
=
1803 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1804 int arch
= attr
->int_value();
1805 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1806 || arch
== elfcpp::TAG_CPU_ARCH_V7
1807 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1810 // Process the relocations to determine unreferenced sections for
1811 // garbage collection.
1813 gc_process_relocs(Symbol_table
* symtab
,
1815 Sized_relobj
<32, big_endian
>* object
,
1816 unsigned int data_shndx
,
1817 unsigned int sh_type
,
1818 const unsigned char* prelocs
,
1820 Output_section
* output_section
,
1821 bool needs_special_offset_handling
,
1822 size_t local_symbol_count
,
1823 const unsigned char* plocal_symbols
);
1825 // Scan the relocations to look for symbol adjustments.
1827 scan_relocs(Symbol_table
* symtab
,
1829 Sized_relobj
<32, big_endian
>* object
,
1830 unsigned int data_shndx
,
1831 unsigned int sh_type
,
1832 const unsigned char* prelocs
,
1834 Output_section
* output_section
,
1835 bool needs_special_offset_handling
,
1836 size_t local_symbol_count
,
1837 const unsigned char* plocal_symbols
);
1839 // Finalize the sections.
1841 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1843 // Return the value to use for a dynamic symbol which requires special
1846 do_dynsym_value(const Symbol
*) const;
1848 // Relocate a section.
1850 relocate_section(const Relocate_info
<32, big_endian
>*,
1851 unsigned int sh_type
,
1852 const unsigned char* prelocs
,
1854 Output_section
* output_section
,
1855 bool needs_special_offset_handling
,
1856 unsigned char* view
,
1857 Arm_address view_address
,
1858 section_size_type view_size
,
1859 const Reloc_symbol_changes
*);
1861 // Scan the relocs during a relocatable link.
1863 scan_relocatable_relocs(Symbol_table
* symtab
,
1865 Sized_relobj
<32, big_endian
>* object
,
1866 unsigned int data_shndx
,
1867 unsigned int sh_type
,
1868 const unsigned char* prelocs
,
1870 Output_section
* output_section
,
1871 bool needs_special_offset_handling
,
1872 size_t local_symbol_count
,
1873 const unsigned char* plocal_symbols
,
1874 Relocatable_relocs
*);
1876 // Relocate a section during a relocatable link.
1878 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1879 unsigned int sh_type
,
1880 const unsigned char* prelocs
,
1882 Output_section
* output_section
,
1883 off_t offset_in_output_section
,
1884 const Relocatable_relocs
*,
1885 unsigned char* view
,
1886 Arm_address view_address
,
1887 section_size_type view_size
,
1888 unsigned char* reloc_view
,
1889 section_size_type reloc_view_size
);
1891 // Return whether SYM is defined by the ABI.
1893 do_is_defined_by_abi(Symbol
* sym
) const
1894 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1896 // Return the size of the GOT section.
1900 gold_assert(this->got_
!= NULL
);
1901 return this->got_
->data_size();
1904 // Map platform-specific reloc types
1906 get_real_reloc_type (unsigned int r_type
);
1909 // Methods to support stub-generations.
1912 // Return the stub factory
1914 stub_factory() const
1915 { return this->stub_factory_
; }
1917 // Make a new Arm_input_section object.
1918 Arm_input_section
<big_endian
>*
1919 new_arm_input_section(Relobj
*, unsigned int);
1921 // Find the Arm_input_section object corresponding to the SHNDX-th input
1922 // section of RELOBJ.
1923 Arm_input_section
<big_endian
>*
1924 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1926 // Make a new Stub_table
1927 Stub_table
<big_endian
>*
1928 new_stub_table(Arm_input_section
<big_endian
>*);
1930 // Scan a section for stub generation.
1932 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1933 const unsigned char*, size_t, Output_section
*,
1934 bool, const unsigned char*, Arm_address
,
1939 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1940 Output_section
*, unsigned char*, Arm_address
,
1943 // Get the default ARM target.
1944 static Target_arm
<big_endian
>*
1947 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1948 && parameters
->target().is_big_endian() == big_endian
);
1949 return static_cast<Target_arm
<big_endian
>*>(
1950 parameters
->sized_target
<32, big_endian
>());
1953 // Whether relocation type uses LSB to distinguish THUMB addresses.
1955 reloc_uses_thumb_bit(unsigned int r_type
);
1957 // Whether NAME belongs to a mapping symbol.
1959 is_mapping_symbol_name(const char* name
)
1963 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
1964 && (name
[2] == '\0' || name
[2] == '.'));
1967 // Whether we work around the Cortex-A8 erratum.
1969 fix_cortex_a8() const
1970 { return this->fix_cortex_a8_
; }
1972 // Whether we fix R_ARM_V4BX relocation.
1974 // 1 - replace with MOV instruction (armv4 target)
1975 // 2 - make interworking veneer (>= armv4t targets only)
1978 { return this->fix_v4bx_
; }
1980 // Scan a span of THUMB code section for Cortex-A8 erratum.
1982 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
1983 section_size_type
, section_size_type
,
1984 const unsigned char*, Arm_address
);
1986 // Apply Cortex-A8 workaround to a branch.
1988 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
1989 unsigned char*, Arm_address
);
1992 // Make an ELF object.
1994 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1995 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1998 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1999 const elfcpp::Ehdr
<32, !big_endian
>&)
2000 { gold_unreachable(); }
2003 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2004 const elfcpp::Ehdr
<64, false>&)
2005 { gold_unreachable(); }
2008 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2009 const elfcpp::Ehdr
<64, true>&)
2010 { gold_unreachable(); }
2012 // Make an output section.
2014 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
2015 elfcpp::Elf_Xword flags
)
2016 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
2019 do_adjust_elf_header(unsigned char* view
, int len
) const;
2021 // We only need to generate stubs, and hence perform relaxation if we are
2022 // not doing relocatable linking.
2024 do_may_relax() const
2025 { return !parameters
->options().relocatable(); }
2028 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
2030 // Determine whether an object attribute tag takes an integer, a
2033 do_attribute_arg_type(int tag
) const;
2035 // Reorder tags during output.
2037 do_attributes_order(int num
) const;
2040 // The class which scans relocations.
2045 : issued_non_pic_error_(false)
2049 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2050 Sized_relobj
<32, big_endian
>* object
,
2051 unsigned int data_shndx
,
2052 Output_section
* output_section
,
2053 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2054 const elfcpp::Sym
<32, big_endian
>& lsym
);
2057 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2058 Sized_relobj
<32, big_endian
>* object
,
2059 unsigned int data_shndx
,
2060 Output_section
* output_section
,
2061 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2066 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
2067 unsigned int r_type
);
2070 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
2071 unsigned int r_type
, Symbol
*);
2074 check_non_pic(Relobj
*, unsigned int r_type
);
2076 // Almost identical to Symbol::needs_plt_entry except that it also
2077 // handles STT_ARM_TFUNC.
2079 symbol_needs_plt_entry(const Symbol
* sym
)
2081 // An undefined symbol from an executable does not need a PLT entry.
2082 if (sym
->is_undefined() && !parameters
->options().shared())
2085 return (!parameters
->doing_static_link()
2086 && (sym
->type() == elfcpp::STT_FUNC
2087 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
2088 && (sym
->is_from_dynobj()
2089 || sym
->is_undefined()
2090 || sym
->is_preemptible()));
2093 // Whether we have issued an error about a non-PIC compilation.
2094 bool issued_non_pic_error_
;
2097 // The class which implements relocation.
2107 // Return whether the static relocation needs to be applied.
2109 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2112 Output_section
* output_section
);
2114 // Do a relocation. Return false if the caller should not issue
2115 // any warnings about this relocation.
2117 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
2118 Output_section
*, size_t relnum
,
2119 const elfcpp::Rel
<32, big_endian
>&,
2120 unsigned int r_type
, const Sized_symbol
<32>*,
2121 const Symbol_value
<32>*,
2122 unsigned char*, Arm_address
,
2125 // Return whether we want to pass flag NON_PIC_REF for this
2126 // reloc. This means the relocation type accesses a symbol not via
2129 reloc_is_non_pic (unsigned int r_type
)
2133 // These relocation types reference GOT or PLT entries explicitly.
2134 case elfcpp::R_ARM_GOT_BREL
:
2135 case elfcpp::R_ARM_GOT_ABS
:
2136 case elfcpp::R_ARM_GOT_PREL
:
2137 case elfcpp::R_ARM_GOT_BREL12
:
2138 case elfcpp::R_ARM_PLT32_ABS
:
2139 case elfcpp::R_ARM_TLS_GD32
:
2140 case elfcpp::R_ARM_TLS_LDM32
:
2141 case elfcpp::R_ARM_TLS_IE32
:
2142 case elfcpp::R_ARM_TLS_IE12GP
:
2144 // These relocate types may use PLT entries.
2145 case elfcpp::R_ARM_CALL
:
2146 case elfcpp::R_ARM_THM_CALL
:
2147 case elfcpp::R_ARM_JUMP24
:
2148 case elfcpp::R_ARM_THM_JUMP24
:
2149 case elfcpp::R_ARM_THM_JUMP19
:
2150 case elfcpp::R_ARM_PLT32
:
2151 case elfcpp::R_ARM_THM_XPC22
:
2160 // A class which returns the size required for a relocation type,
2161 // used while scanning relocs during a relocatable link.
2162 class Relocatable_size_for_reloc
2166 get_size_for_reloc(unsigned int, Relobj
*);
2169 // Get the GOT section, creating it if necessary.
2170 Output_data_got
<32, big_endian
>*
2171 got_section(Symbol_table
*, Layout
*);
2173 // Get the GOT PLT section.
2175 got_plt_section() const
2177 gold_assert(this->got_plt_
!= NULL
);
2178 return this->got_plt_
;
2181 // Create a PLT entry for a global symbol.
2183 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
2185 // Get the PLT section.
2186 const Output_data_plt_arm
<big_endian
>*
2189 gold_assert(this->plt_
!= NULL
);
2193 // Get the dynamic reloc section, creating it if necessary.
2195 rel_dyn_section(Layout
*);
2197 // Return true if the symbol may need a COPY relocation.
2198 // References from an executable object to non-function symbols
2199 // defined in a dynamic object may need a COPY relocation.
2201 may_need_copy_reloc(Symbol
* gsym
)
2203 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
2204 && gsym
->may_need_copy_reloc());
2207 // Add a potential copy relocation.
2209 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
2210 Sized_relobj
<32, big_endian
>* object
,
2211 unsigned int shndx
, Output_section
* output_section
,
2212 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
2214 this->copy_relocs_
.copy_reloc(symtab
, layout
,
2215 symtab
->get_sized_symbol
<32>(sym
),
2216 object
, shndx
, output_section
, reloc
,
2217 this->rel_dyn_section(layout
));
2220 // Whether two EABI versions are compatible.
2222 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
2224 // Merge processor-specific flags from input object and those in the ELF
2225 // header of the output.
2227 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
2229 // Get the secondary compatible architecture.
2231 get_secondary_compatible_arch(const Attributes_section_data
*);
2233 // Set the secondary compatible architecture.
2235 set_secondary_compatible_arch(Attributes_section_data
*, int);
2238 tag_cpu_arch_combine(const char*, int, int*, int, int);
2240 // Helper to print AEABI enum tag value.
2242 aeabi_enum_name(unsigned int);
2244 // Return string value for TAG_CPU_name.
2246 tag_cpu_name_value(unsigned int);
2248 // Merge object attributes from input object and those in the output.
2250 merge_object_attributes(const char*, const Attributes_section_data
*);
2252 // Helper to get an AEABI object attribute
2254 get_aeabi_object_attribute(int tag
) const
2256 Attributes_section_data
* pasd
= this->attributes_section_data_
;
2257 gold_assert(pasd
!= NULL
);
2258 Object_attribute
* attr
=
2259 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
2260 gold_assert(attr
!= NULL
);
2265 // Methods to support stub-generations.
2268 // Group input sections for stub generation.
2270 group_sections(Layout
*, section_size_type
, bool);
2272 // Scan a relocation for stub generation.
2274 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
2275 const Sized_symbol
<32>*, unsigned int,
2276 const Symbol_value
<32>*,
2277 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
2279 // Scan a relocation section for stub.
2280 template<int sh_type
>
2282 scan_reloc_section_for_stubs(
2283 const Relocate_info
<32, big_endian
>* relinfo
,
2284 const unsigned char* prelocs
,
2286 Output_section
* output_section
,
2287 bool needs_special_offset_handling
,
2288 const unsigned char* view
,
2289 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2292 // Information about this specific target which we pass to the
2293 // general Target structure.
2294 static const Target::Target_info arm_info
;
2296 // The types of GOT entries needed for this platform.
2299 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
2302 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2304 // Map input section to Arm_input_section.
2305 typedef Unordered_map
<Section_id
,
2306 Arm_input_section
<big_endian
>*,
2308 Arm_input_section_map
;
2310 // Map output addresses to relocs for Cortex-A8 erratum.
2311 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2312 Cortex_a8_relocs_info
;
2315 Output_data_got
<32, big_endian
>* got_
;
2317 Output_data_plt_arm
<big_endian
>* plt_
;
2318 // The GOT PLT section.
2319 Output_data_space
* got_plt_
;
2320 // The dynamic reloc section.
2321 Reloc_section
* rel_dyn_
;
2322 // Relocs saved to avoid a COPY reloc.
2323 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2324 // Space for variables copied with a COPY reloc.
2325 Output_data_space
* dynbss_
;
2326 // Vector of Stub_tables created.
2327 Stub_table_list stub_tables_
;
2329 const Stub_factory
&stub_factory_
;
2330 // Whether we can use BLX.
2332 // Whether we force PIC branch veneers.
2333 bool should_force_pic_veneer_
;
2334 // Map for locating Arm_input_sections.
2335 Arm_input_section_map arm_input_section_map_
;
2336 // Attributes section data in output.
2337 Attributes_section_data
* attributes_section_data_
;
2338 // Whether we want to fix code for Cortex-A8 erratum.
2339 bool fix_cortex_a8_
;
2340 // Map addresses to relocs for Cortex-A8 erratum.
2341 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2342 // Whether we need to fix code for V4BX relocations.
2346 template<bool big_endian
>
2347 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2350 big_endian
, // is_big_endian
2351 elfcpp::EM_ARM
, // machine_code
2352 false, // has_make_symbol
2353 false, // has_resolve
2354 false, // has_code_fill
2355 true, // is_default_stack_executable
2357 "/usr/lib/libc.so.1", // dynamic_linker
2358 0x8000, // default_text_segment_address
2359 0x1000, // abi_pagesize (overridable by -z max-page-size)
2360 0x1000, // common_pagesize (overridable by -z common-page-size)
2361 elfcpp::SHN_UNDEF
, // small_common_shndx
2362 elfcpp::SHN_UNDEF
, // large_common_shndx
2363 0, // small_common_section_flags
2364 0, // large_common_section_flags
2365 ".ARM.attributes", // attributes_section
2366 "aeabi" // attributes_vendor
2369 // Arm relocate functions class
2372 template<bool big_endian
>
2373 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2378 STATUS_OKAY
, // No error during relocation.
2379 STATUS_OVERFLOW
, // Relocation oveflow.
2380 STATUS_BAD_RELOC
// Relocation cannot be applied.
2384 typedef Relocate_functions
<32, big_endian
> Base
;
2385 typedef Arm_relocate_functions
<big_endian
> This
;
2387 // Encoding of imm16 argument for movt and movw ARM instructions
2390 // imm16 := imm4 | imm12
2392 // 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
2393 // +-------+---------------+-------+-------+-----------------------+
2394 // | | |imm4 | |imm12 |
2395 // +-------+---------------+-------+-------+-----------------------+
2397 // Extract the relocation addend from VAL based on the ARM
2398 // instruction encoding described above.
2399 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2400 extract_arm_movw_movt_addend(
2401 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2403 // According to the Elf ABI for ARM Architecture the immediate
2404 // field is sign-extended to form the addend.
2405 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2408 // Insert X into VAL based on the ARM instruction encoding described
2410 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2411 insert_val_arm_movw_movt(
2412 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2413 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2417 val
|= (x
& 0xf000) << 4;
2421 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2424 // imm16 := imm4 | i | imm3 | imm8
2426 // 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
2427 // +---------+-+-----------+-------++-+-----+-------+---------------+
2428 // | |i| |imm4 || |imm3 | |imm8 |
2429 // +---------+-+-----------+-------++-+-----+-------+---------------+
2431 // Extract the relocation addend from VAL based on the Thumb2
2432 // instruction encoding described above.
2433 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2434 extract_thumb_movw_movt_addend(
2435 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2437 // According to the Elf ABI for ARM Architecture the immediate
2438 // field is sign-extended to form the addend.
2439 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2440 | ((val
>> 15) & 0x0800)
2441 | ((val
>> 4) & 0x0700)
2445 // Insert X into VAL based on the Thumb2 instruction encoding
2447 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2448 insert_val_thumb_movw_movt(
2449 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2450 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2453 val
|= (x
& 0xf000) << 4;
2454 val
|= (x
& 0x0800) << 15;
2455 val
|= (x
& 0x0700) << 4;
2456 val
|= (x
& 0x00ff);
2460 // Handle ARM long branches.
2461 static typename
This::Status
2462 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2463 unsigned char *, const Sized_symbol
<32>*,
2464 const Arm_relobj
<big_endian
>*, unsigned int,
2465 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2467 // Handle THUMB long branches.
2468 static typename
This::Status
2469 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2470 unsigned char *, const Sized_symbol
<32>*,
2471 const Arm_relobj
<big_endian
>*, unsigned int,
2472 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2476 // Return the branch offset of a 32-bit THUMB branch.
2477 static inline int32_t
2478 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2480 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2481 // involving the J1 and J2 bits.
2482 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2483 uint32_t upper
= upper_insn
& 0x3ffU
;
2484 uint32_t lower
= lower_insn
& 0x7ffU
;
2485 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2486 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2487 uint32_t i1
= j1
^ s
? 0 : 1;
2488 uint32_t i2
= j2
^ s
? 0 : 1;
2490 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2491 | (upper
<< 12) | (lower
<< 1));
2494 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2495 // UPPER_INSN is the original upper instruction of the branch. Caller is
2496 // responsible for overflow checking and BLX offset adjustment.
2497 static inline uint16_t
2498 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2500 uint32_t s
= offset
< 0 ? 1 : 0;
2501 uint32_t bits
= static_cast<uint32_t>(offset
);
2502 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2505 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2506 // LOWER_INSN is the original lower instruction of the branch. Caller is
2507 // responsible for overflow checking and BLX offset adjustment.
2508 static inline uint16_t
2509 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2511 uint32_t s
= offset
< 0 ? 1 : 0;
2512 uint32_t bits
= static_cast<uint32_t>(offset
);
2513 return ((lower_insn
& ~0x2fffU
)
2514 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2515 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2516 | ((bits
>> 1) & 0x7ffU
));
2519 // Return the branch offset of a 32-bit THUMB conditional branch.
2520 static inline int32_t
2521 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2523 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2524 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2525 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2526 uint32_t lower
= (lower_insn
& 0x07ffU
);
2527 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2529 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2532 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2533 // instruction. UPPER_INSN is the original upper instruction of the branch.
2534 // Caller is responsible for overflow checking.
2535 static inline uint16_t
2536 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2538 uint32_t s
= offset
< 0 ? 1 : 0;
2539 uint32_t bits
= static_cast<uint32_t>(offset
);
2540 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2543 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2544 // instruction. LOWER_INSN is the original lower instruction of the branch.
2545 // Caller is reponsible for overflow checking.
2546 static inline uint16_t
2547 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2549 uint32_t bits
= static_cast<uint32_t>(offset
);
2550 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2551 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2552 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2554 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
2557 // R_ARM_ABS8: S + A
2558 static inline typename
This::Status
2559 abs8(unsigned char *view
,
2560 const Sized_relobj
<32, big_endian
>* object
,
2561 const Symbol_value
<32>* psymval
)
2563 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2564 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2565 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2566 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2567 Reltype addend
= utils::sign_extend
<8>(val
);
2568 Reltype x
= psymval
->value(object
, addend
);
2569 val
= utils::bit_select(val
, x
, 0xffU
);
2570 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2571 return (utils::has_signed_unsigned_overflow
<8>(x
)
2572 ? This::STATUS_OVERFLOW
2573 : This::STATUS_OKAY
);
2576 // R_ARM_THM_ABS5: S + A
2577 static inline typename
This::Status
2578 thm_abs5(unsigned char *view
,
2579 const Sized_relobj
<32, big_endian
>* object
,
2580 const Symbol_value
<32>* psymval
)
2582 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2583 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2584 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2585 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2586 Reltype addend
= (val
& 0x7e0U
) >> 6;
2587 Reltype x
= psymval
->value(object
, addend
);
2588 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2589 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2590 return (utils::has_overflow
<5>(x
)
2591 ? This::STATUS_OVERFLOW
2592 : This::STATUS_OKAY
);
2595 // R_ARM_ABS12: S + A
2596 static inline typename
This::Status
2597 abs12(unsigned char *view
,
2598 const Sized_relobj
<32, big_endian
>* object
,
2599 const Symbol_value
<32>* psymval
)
2601 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2602 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2603 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2604 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2605 Reltype addend
= val
& 0x0fffU
;
2606 Reltype x
= psymval
->value(object
, addend
);
2607 val
= utils::bit_select(val
, x
, 0x0fffU
);
2608 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2609 return (utils::has_overflow
<12>(x
)
2610 ? This::STATUS_OVERFLOW
2611 : This::STATUS_OKAY
);
2614 // R_ARM_ABS16: S + A
2615 static inline typename
This::Status
2616 abs16(unsigned char *view
,
2617 const Sized_relobj
<32, big_endian
>* object
,
2618 const Symbol_value
<32>* psymval
)
2620 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2621 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2622 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2623 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2624 Reltype addend
= utils::sign_extend
<16>(val
);
2625 Reltype x
= psymval
->value(object
, addend
);
2626 val
= utils::bit_select(val
, x
, 0xffffU
);
2627 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2628 return (utils::has_signed_unsigned_overflow
<16>(x
)
2629 ? This::STATUS_OVERFLOW
2630 : This::STATUS_OKAY
);
2633 // R_ARM_ABS32: (S + A) | T
2634 static inline typename
This::Status
2635 abs32(unsigned char *view
,
2636 const Sized_relobj
<32, big_endian
>* object
,
2637 const Symbol_value
<32>* psymval
,
2638 Arm_address thumb_bit
)
2640 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2641 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2642 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2643 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2644 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2645 return This::STATUS_OKAY
;
2648 // R_ARM_REL32: (S + A) | T - P
2649 static inline typename
This::Status
2650 rel32(unsigned char *view
,
2651 const Sized_relobj
<32, big_endian
>* object
,
2652 const Symbol_value
<32>* psymval
,
2653 Arm_address address
,
2654 Arm_address thumb_bit
)
2656 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2657 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2658 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2659 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2660 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2661 return This::STATUS_OKAY
;
2664 // R_ARM_THM_CALL: (S + A) | T - P
2665 static inline typename
This::Status
2666 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2667 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2668 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2669 Arm_address address
, Arm_address thumb_bit
,
2670 bool is_weakly_undefined_without_plt
)
2672 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2673 object
, r_sym
, psymval
, address
, thumb_bit
,
2674 is_weakly_undefined_without_plt
);
2677 // R_ARM_THM_JUMP24: (S + A) | T - P
2678 static inline typename
This::Status
2679 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2680 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2681 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2682 Arm_address address
, Arm_address thumb_bit
,
2683 bool is_weakly_undefined_without_plt
)
2685 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2686 object
, r_sym
, psymval
, address
, thumb_bit
,
2687 is_weakly_undefined_without_plt
);
2690 // R_ARM_THM_JUMP24: (S + A) | T - P
2691 static typename
This::Status
2692 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
2693 const Symbol_value
<32>* psymval
, Arm_address address
,
2694 Arm_address thumb_bit
);
2696 // R_ARM_THM_XPC22: (S + A) | T - P
2697 static inline typename
This::Status
2698 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2699 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2700 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2701 Arm_address address
, Arm_address thumb_bit
,
2702 bool is_weakly_undefined_without_plt
)
2704 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2705 object
, r_sym
, psymval
, address
, thumb_bit
,
2706 is_weakly_undefined_without_plt
);
2709 // R_ARM_THM_JUMP6: S + A – P
2710 static inline typename
This::Status
2711 thm_jump6(unsigned char *view
,
2712 const Sized_relobj
<32, big_endian
>* object
,
2713 const Symbol_value
<32>* psymval
,
2714 Arm_address address
)
2716 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2717 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2718 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2719 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2720 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2721 Reltype addend
= (((val
& 0x0200) >> 3) | ((val
& 0x00f8) >> 2));
2722 Reltype x
= (psymval
->value(object
, addend
) - address
);
2723 val
= (val
& 0xfd07) | ((x
& 0x0040) << 3) | ((val
& 0x003e) << 2);
2724 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2725 // CZB does only forward jumps.
2726 return ((x
> 0x007e)
2727 ? This::STATUS_OVERFLOW
2728 : This::STATUS_OKAY
);
2731 // R_ARM_THM_JUMP8: S + A – P
2732 static inline typename
This::Status
2733 thm_jump8(unsigned char *view
,
2734 const Sized_relobj
<32, big_endian
>* object
,
2735 const Symbol_value
<32>* psymval
,
2736 Arm_address address
)
2738 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2739 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2740 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2741 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2742 Reltype addend
= utils::sign_extend
<8>((val
& 0x00ff) << 1);
2743 Reltype x
= (psymval
->value(object
, addend
) - address
);
2744 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xff00) | ((x
& 0x01fe) >> 1));
2745 return (utils::has_overflow
<8>(x
)
2746 ? This::STATUS_OVERFLOW
2747 : This::STATUS_OKAY
);
2750 // R_ARM_THM_JUMP11: S + A – P
2751 static inline typename
This::Status
2752 thm_jump11(unsigned char *view
,
2753 const Sized_relobj
<32, big_endian
>* object
,
2754 const Symbol_value
<32>* psymval
,
2755 Arm_address address
)
2757 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2758 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2759 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2760 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2761 Reltype addend
= utils::sign_extend
<11>((val
& 0x07ff) << 1);
2762 Reltype x
= (psymval
->value(object
, addend
) - address
);
2763 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xf800) | ((x
& 0x0ffe) >> 1));
2764 return (utils::has_overflow
<11>(x
)
2765 ? This::STATUS_OVERFLOW
2766 : This::STATUS_OKAY
);
2769 // R_ARM_BASE_PREL: B(S) + A - P
2770 static inline typename
This::Status
2771 base_prel(unsigned char* view
,
2773 Arm_address address
)
2775 Base::rel32(view
, origin
- address
);
2779 // R_ARM_BASE_ABS: B(S) + A
2780 static inline typename
This::Status
2781 base_abs(unsigned char* view
,
2784 Base::rel32(view
, origin
);
2788 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2789 static inline typename
This::Status
2790 got_brel(unsigned char* view
,
2791 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2793 Base::rel32(view
, got_offset
);
2794 return This::STATUS_OKAY
;
2797 // R_ARM_GOT_PREL: GOT(S) + A - P
2798 static inline typename
This::Status
2799 got_prel(unsigned char *view
,
2800 Arm_address got_entry
,
2801 Arm_address address
)
2803 Base::rel32(view
, got_entry
- address
);
2804 return This::STATUS_OKAY
;
2807 // R_ARM_PLT32: (S + A) | T - P
2808 static inline typename
This::Status
2809 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2810 unsigned char *view
,
2811 const Sized_symbol
<32>* gsym
,
2812 const Arm_relobj
<big_endian
>* object
,
2814 const Symbol_value
<32>* psymval
,
2815 Arm_address address
,
2816 Arm_address thumb_bit
,
2817 bool is_weakly_undefined_without_plt
)
2819 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2820 object
, r_sym
, psymval
, address
, thumb_bit
,
2821 is_weakly_undefined_without_plt
);
2824 // R_ARM_XPC25: (S + A) | T - P
2825 static inline typename
This::Status
2826 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2827 unsigned char *view
,
2828 const Sized_symbol
<32>* gsym
,
2829 const Arm_relobj
<big_endian
>* object
,
2831 const Symbol_value
<32>* psymval
,
2832 Arm_address address
,
2833 Arm_address thumb_bit
,
2834 bool is_weakly_undefined_without_plt
)
2836 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2837 object
, r_sym
, psymval
, address
, thumb_bit
,
2838 is_weakly_undefined_without_plt
);
2841 // R_ARM_CALL: (S + A) | T - P
2842 static inline typename
This::Status
2843 call(const Relocate_info
<32, big_endian
>* relinfo
,
2844 unsigned char *view
,
2845 const Sized_symbol
<32>* gsym
,
2846 const Arm_relobj
<big_endian
>* object
,
2848 const Symbol_value
<32>* psymval
,
2849 Arm_address address
,
2850 Arm_address thumb_bit
,
2851 bool is_weakly_undefined_without_plt
)
2853 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2854 object
, r_sym
, psymval
, address
, thumb_bit
,
2855 is_weakly_undefined_without_plt
);
2858 // R_ARM_JUMP24: (S + A) | T - P
2859 static inline typename
This::Status
2860 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2861 unsigned char *view
,
2862 const Sized_symbol
<32>* gsym
,
2863 const Arm_relobj
<big_endian
>* object
,
2865 const Symbol_value
<32>* psymval
,
2866 Arm_address address
,
2867 Arm_address thumb_bit
,
2868 bool is_weakly_undefined_without_plt
)
2870 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2871 object
, r_sym
, psymval
, address
, thumb_bit
,
2872 is_weakly_undefined_without_plt
);
2875 // R_ARM_PREL: (S + A) | T - P
2876 static inline typename
This::Status
2877 prel31(unsigned char *view
,
2878 const Sized_relobj
<32, big_endian
>* object
,
2879 const Symbol_value
<32>* psymval
,
2880 Arm_address address
,
2881 Arm_address thumb_bit
)
2883 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2884 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2885 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2886 Valtype addend
= utils::sign_extend
<31>(val
);
2887 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2888 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2889 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2890 return (utils::has_overflow
<31>(x
) ?
2891 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2894 // R_ARM_MOVW_ABS_NC: (S + A) | T
2895 static inline typename
This::Status
2896 movw_abs_nc(unsigned char *view
,
2897 const Sized_relobj
<32, big_endian
>* object
,
2898 const Symbol_value
<32>* psymval
,
2899 Arm_address thumb_bit
)
2901 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2902 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2903 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2904 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2905 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2906 val
= This::insert_val_arm_movw_movt(val
, x
);
2907 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2908 return This::STATUS_OKAY
;
2911 // R_ARM_MOVT_ABS: S + A
2912 static inline typename
This::Status
2913 movt_abs(unsigned char *view
,
2914 const Sized_relobj
<32, big_endian
>* object
,
2915 const Symbol_value
<32>* psymval
)
2917 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2918 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2919 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2920 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2921 Valtype x
= psymval
->value(object
, addend
) >> 16;
2922 val
= This::insert_val_arm_movw_movt(val
, x
);
2923 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2924 return This::STATUS_OKAY
;
2927 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2928 static inline typename
This::Status
2929 thm_movw_abs_nc(unsigned char *view
,
2930 const Sized_relobj
<32, big_endian
>* object
,
2931 const Symbol_value
<32>* psymval
,
2932 Arm_address thumb_bit
)
2934 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2935 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2936 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2937 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2938 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2939 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2940 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2941 val
= This::insert_val_thumb_movw_movt(val
, x
);
2942 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2943 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2944 return This::STATUS_OKAY
;
2947 // R_ARM_THM_MOVT_ABS: S + A
2948 static inline typename
This::Status
2949 thm_movt_abs(unsigned char *view
,
2950 const Sized_relobj
<32, big_endian
>* object
,
2951 const Symbol_value
<32>* psymval
)
2953 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2954 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2955 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2956 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2957 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2958 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2959 Reltype x
= psymval
->value(object
, addend
) >> 16;
2960 val
= This::insert_val_thumb_movw_movt(val
, x
);
2961 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2962 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2963 return This::STATUS_OKAY
;
2966 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2967 static inline typename
This::Status
2968 movw_prel_nc(unsigned char *view
,
2969 const Sized_relobj
<32, big_endian
>* object
,
2970 const Symbol_value
<32>* psymval
,
2971 Arm_address address
,
2972 Arm_address thumb_bit
)
2974 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2975 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2976 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2977 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2978 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2979 val
= This::insert_val_arm_movw_movt(val
, x
);
2980 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2981 return This::STATUS_OKAY
;
2984 // R_ARM_MOVT_PREL: S + A - P
2985 static inline typename
This::Status
2986 movt_prel(unsigned char *view
,
2987 const Sized_relobj
<32, big_endian
>* object
,
2988 const Symbol_value
<32>* psymval
,
2989 Arm_address address
)
2991 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2992 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2993 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2994 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2995 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2996 val
= This::insert_val_arm_movw_movt(val
, x
);
2997 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2998 return This::STATUS_OKAY
;
3001 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3002 static inline typename
This::Status
3003 thm_movw_prel_nc(unsigned char *view
,
3004 const Sized_relobj
<32, big_endian
>* object
,
3005 const Symbol_value
<32>* psymval
,
3006 Arm_address address
,
3007 Arm_address thumb_bit
)
3009 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3010 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3011 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3012 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3013 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3014 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3015 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3016 val
= This::insert_val_thumb_movw_movt(val
, x
);
3017 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3018 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3019 return This::STATUS_OKAY
;
3022 // R_ARM_THM_MOVT_PREL: S + A - P
3023 static inline typename
This::Status
3024 thm_movt_prel(unsigned char *view
,
3025 const Sized_relobj
<32, big_endian
>* object
,
3026 const Symbol_value
<32>* psymval
,
3027 Arm_address address
)
3029 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3030 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3031 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3032 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3033 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3034 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3035 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
3036 val
= This::insert_val_thumb_movw_movt(val
, x
);
3037 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3038 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3039 return This::STATUS_OKAY
;
3043 static inline typename
This::Status
3044 v4bx(const Relocate_info
<32, big_endian
>* relinfo
,
3045 unsigned char *view
,
3046 const Arm_relobj
<big_endian
>* object
,
3047 const Arm_address address
,
3048 const bool is_interworking
)
3051 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3052 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3053 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3055 // Ensure that we have a BX instruction.
3056 gold_assert((val
& 0x0ffffff0) == 0x012fff10);
3057 const uint32_t reg
= (val
& 0xf);
3058 if (is_interworking
&& reg
!= 0xf)
3060 Stub_table
<big_endian
>* stub_table
=
3061 object
->stub_table(relinfo
->data_shndx
);
3062 gold_assert(stub_table
!= NULL
);
3064 Arm_v4bx_stub
* stub
= stub_table
->find_arm_v4bx_stub(reg
);
3065 gold_assert(stub
!= NULL
);
3067 int32_t veneer_address
=
3068 stub_table
->address() + stub
->offset() - 8 - address
;
3069 gold_assert((veneer_address
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3070 && (veneer_address
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3071 // Replace with a branch to veneer (B <addr>)
3072 val
= (val
& 0xf0000000) | 0x0a000000
3073 | ((veneer_address
>> 2) & 0x00ffffff);
3077 // Preserve Rm (lowest four bits) and the condition code
3078 // (highest four bits). Other bits encode MOV PC,Rm.
3079 val
= (val
& 0xf000000f) | 0x01a0f000;
3081 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3082 return This::STATUS_OKAY
;
3086 // Relocate ARM long branches. This handles relocation types
3087 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3088 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3089 // undefined and we do not use PLT in this relocation. In such a case,
3090 // the branch is converted into an NOP.
3092 template<bool big_endian
>
3093 typename Arm_relocate_functions
<big_endian
>::Status
3094 Arm_relocate_functions
<big_endian
>::arm_branch_common(
3095 unsigned int r_type
,
3096 const Relocate_info
<32, big_endian
>* relinfo
,
3097 unsigned char *view
,
3098 const Sized_symbol
<32>* gsym
,
3099 const Arm_relobj
<big_endian
>* object
,
3101 const Symbol_value
<32>* psymval
,
3102 Arm_address address
,
3103 Arm_address thumb_bit
,
3104 bool is_weakly_undefined_without_plt
)
3106 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3107 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3108 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3110 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
3111 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
3112 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
3113 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
3114 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
3115 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
3116 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
3118 // Check that the instruction is valid.
3119 if (r_type
== elfcpp::R_ARM_CALL
)
3121 if (!insn_is_uncond_bl
&& !insn_is_blx
)
3122 return This::STATUS_BAD_RELOC
;
3124 else if (r_type
== elfcpp::R_ARM_JUMP24
)
3126 if (!insn_is_b
&& !insn_is_cond_bl
)
3127 return This::STATUS_BAD_RELOC
;
3129 else if (r_type
== elfcpp::R_ARM_PLT32
)
3131 if (!insn_is_any_branch
)
3132 return This::STATUS_BAD_RELOC
;
3134 else if (r_type
== elfcpp::R_ARM_XPC25
)
3136 // FIXME: AAELF document IH0044C does not say much about it other
3137 // than it being obsolete.
3138 if (!insn_is_any_branch
)
3139 return This::STATUS_BAD_RELOC
;
3144 // A branch to an undefined weak symbol is turned into a jump to
3145 // the next instruction unless a PLT entry will be created.
3146 // Do the same for local undefined symbols.
3147 // The jump to the next instruction is optimized as a NOP depending
3148 // on the architecture.
3149 const Target_arm
<big_endian
>* arm_target
=
3150 Target_arm
<big_endian
>::default_target();
3151 if (is_weakly_undefined_without_plt
)
3153 Valtype cond
= val
& 0xf0000000U
;
3154 if (arm_target
->may_use_arm_nop())
3155 val
= cond
| 0x0320f000;
3157 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3158 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3159 return This::STATUS_OKAY
;
3162 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
3163 Valtype branch_target
= psymval
->value(object
, addend
);
3164 int32_t branch_offset
= branch_target
- address
;
3166 // We need a stub if the branch offset is too large or if we need
3168 bool may_use_blx
= arm_target
->may_use_blx();
3169 Reloc_stub
* stub
= NULL
;
3170 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
3171 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3172 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
3174 Stub_type stub_type
=
3175 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
3177 if (stub_type
!= arm_stub_none
)
3179 Stub_table
<big_endian
>* stub_table
=
3180 object
->stub_table(relinfo
->data_shndx
);
3181 gold_assert(stub_table
!= NULL
);
3183 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3184 stub
= stub_table
->find_reloc_stub(stub_key
);
3185 gold_assert(stub
!= NULL
);
3186 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3187 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3188 branch_offset
= branch_target
- address
;
3189 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3190 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3194 // At this point, if we still need to switch mode, the instruction
3195 // must either be a BLX or a BL that can be converted to a BLX.
3199 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
3200 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
3203 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
3204 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3205 return (utils::has_overflow
<26>(branch_offset
)
3206 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3209 // Relocate THUMB long branches. This handles relocation types
3210 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3211 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3212 // undefined and we do not use PLT in this relocation. In such a case,
3213 // the branch is converted into an NOP.
3215 template<bool big_endian
>
3216 typename Arm_relocate_functions
<big_endian
>::Status
3217 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
3218 unsigned int r_type
,
3219 const Relocate_info
<32, big_endian
>* relinfo
,
3220 unsigned char *view
,
3221 const Sized_symbol
<32>* gsym
,
3222 const Arm_relobj
<big_endian
>* object
,
3224 const Symbol_value
<32>* psymval
,
3225 Arm_address address
,
3226 Arm_address thumb_bit
,
3227 bool is_weakly_undefined_without_plt
)
3229 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3230 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3231 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3232 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3234 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3236 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
3237 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
3239 // Check that the instruction is valid.
3240 if (r_type
== elfcpp::R_ARM_THM_CALL
)
3242 if (!is_bl_insn
&& !is_blx_insn
)
3243 return This::STATUS_BAD_RELOC
;
3245 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
3247 // This cannot be a BLX.
3249 return This::STATUS_BAD_RELOC
;
3251 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
3253 // Check for Thumb to Thumb call.
3255 return This::STATUS_BAD_RELOC
;
3258 gold_warning(_("%s: Thumb BLX instruction targets "
3259 "thumb function '%s'."),
3260 object
->name().c_str(),
3261 (gsym
? gsym
->name() : "(local)"));
3262 // Convert BLX to BL.
3263 lower_insn
|= 0x1000U
;
3269 // A branch to an undefined weak symbol is turned into a jump to
3270 // the next instruction unless a PLT entry will be created.
3271 // The jump to the next instruction is optimized as a NOP.W for
3272 // Thumb-2 enabled architectures.
3273 const Target_arm
<big_endian
>* arm_target
=
3274 Target_arm
<big_endian
>::default_target();
3275 if (is_weakly_undefined_without_plt
)
3277 if (arm_target
->may_use_thumb2_nop())
3279 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
3280 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
3284 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
3285 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
3287 return This::STATUS_OKAY
;
3290 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
3291 Arm_address branch_target
= psymval
->value(object
, addend
);
3292 int32_t branch_offset
= branch_target
- address
;
3294 // We need a stub if the branch offset is too large or if we need
3296 bool may_use_blx
= arm_target
->may_use_blx();
3297 bool thumb2
= arm_target
->using_thumb2();
3299 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3300 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3302 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3303 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3304 || ((thumb_bit
== 0)
3305 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3306 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
3308 Stub_type stub_type
=
3309 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
3311 if (stub_type
!= arm_stub_none
)
3313 Stub_table
<big_endian
>* stub_table
=
3314 object
->stub_table(relinfo
->data_shndx
);
3315 gold_assert(stub_table
!= NULL
);
3317 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3318 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
3319 gold_assert(stub
!= NULL
);
3320 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3321 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3322 branch_offset
= branch_target
- address
;
3326 // At this point, if we still need to switch mode, the instruction
3327 // must either be a BLX or a BL that can be converted to a BLX.
3330 gold_assert(may_use_blx
3331 && (r_type
== elfcpp::R_ARM_THM_CALL
3332 || r_type
== elfcpp::R_ARM_THM_XPC22
));
3333 // Make sure this is a BLX.
3334 lower_insn
&= ~0x1000U
;
3338 // Make sure this is a BL.
3339 lower_insn
|= 0x1000U
;
3342 if ((lower_insn
& 0x5000U
) == 0x4000U
)
3343 // For a BLX instruction, make sure that the relocation is rounded up
3344 // to a word boundary. This follows the semantics of the instruction
3345 // which specifies that bit 1 of the target address will come from bit
3346 // 1 of the base address.
3347 branch_offset
= (branch_offset
+ 2) & ~3;
3349 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3350 // We use the Thumb-2 encoding, which is safe even if dealing with
3351 // a Thumb-1 instruction by virtue of our overflow check above. */
3352 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
3353 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
3355 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3356 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3359 ? utils::has_overflow
<25>(branch_offset
)
3360 : utils::has_overflow
<23>(branch_offset
))
3361 ? This::STATUS_OVERFLOW
3362 : This::STATUS_OKAY
);
3365 // Relocate THUMB-2 long conditional branches.
3366 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3367 // undefined and we do not use PLT in this relocation. In such a case,
3368 // the branch is converted into an NOP.
3370 template<bool big_endian
>
3371 typename Arm_relocate_functions
<big_endian
>::Status
3372 Arm_relocate_functions
<big_endian
>::thm_jump19(
3373 unsigned char *view
,
3374 const Arm_relobj
<big_endian
>* object
,
3375 const Symbol_value
<32>* psymval
,
3376 Arm_address address
,
3377 Arm_address thumb_bit
)
3379 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3380 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3381 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3382 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3383 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
3385 Arm_address branch_target
= psymval
->value(object
, addend
);
3386 int32_t branch_offset
= branch_target
- address
;
3388 // ??? Should handle interworking? GCC might someday try to
3389 // use this for tail calls.
3390 // FIXME: We do support thumb entry to PLT yet.
3393 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3394 return This::STATUS_BAD_RELOC
;
3397 // Put RELOCATION back into the insn.
3398 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
3399 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
3401 // Put the relocated value back in the object file:
3402 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3403 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3405 return (utils::has_overflow
<21>(branch_offset
)
3406 ? This::STATUS_OVERFLOW
3407 : This::STATUS_OKAY
);
3410 // Get the GOT section, creating it if necessary.
3412 template<bool big_endian
>
3413 Output_data_got
<32, big_endian
>*
3414 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
3416 if (this->got_
== NULL
)
3418 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3420 this->got_
= new Output_data_got
<32, big_endian
>();
3423 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3425 | elfcpp::SHF_WRITE
),
3426 this->got_
, false, true, true,
3429 // The old GNU linker creates a .got.plt section. We just
3430 // create another set of data in the .got section. Note that we
3431 // always create a PLT if we create a GOT, although the PLT
3433 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
3434 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3436 | elfcpp::SHF_WRITE
),
3437 this->got_plt_
, false, false,
3440 // The first three entries are reserved.
3441 this->got_plt_
->set_current_data_size(3 * 4);
3443 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3444 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3445 Symbol_table::PREDEFINED
,
3447 0, 0, elfcpp::STT_OBJECT
,
3449 elfcpp::STV_HIDDEN
, 0,
3455 // Get the dynamic reloc section, creating it if necessary.
3457 template<bool big_endian
>
3458 typename Target_arm
<big_endian
>::Reloc_section
*
3459 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
3461 if (this->rel_dyn_
== NULL
)
3463 gold_assert(layout
!= NULL
);
3464 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
3465 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
3466 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
3467 false, false, false);
3469 return this->rel_dyn_
;
3472 // Insn_template methods.
3474 // Return byte size of an instruction template.
3477 Insn_template::size() const
3479 switch (this->type())
3482 case THUMB16_SPECIAL_TYPE
:
3493 // Return alignment of an instruction template.
3496 Insn_template::alignment() const
3498 switch (this->type())
3501 case THUMB16_SPECIAL_TYPE
:
3512 // Stub_template methods.
3514 Stub_template::Stub_template(
3515 Stub_type type
, const Insn_template
* insns
,
3517 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
3518 entry_in_thumb_mode_(false), relocs_()
3522 // Compute byte size and alignment of stub template.
3523 for (size_t i
= 0; i
< insn_count
; i
++)
3525 unsigned insn_alignment
= insns
[i
].alignment();
3526 size_t insn_size
= insns
[i
].size();
3527 gold_assert((offset
& (insn_alignment
- 1)) == 0);
3528 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
3529 switch (insns
[i
].type())
3531 case Insn_template::THUMB16_TYPE
:
3532 case Insn_template::THUMB16_SPECIAL_TYPE
:
3534 this->entry_in_thumb_mode_
= true;
3537 case Insn_template::THUMB32_TYPE
:
3538 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
3539 this->relocs_
.push_back(Reloc(i
, offset
));
3541 this->entry_in_thumb_mode_
= true;
3544 case Insn_template::ARM_TYPE
:
3545 // Handle cases where the target is encoded within the
3547 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
3548 this->relocs_
.push_back(Reloc(i
, offset
));
3551 case Insn_template::DATA_TYPE
:
3552 // Entry point cannot be data.
3553 gold_assert(i
!= 0);
3554 this->relocs_
.push_back(Reloc(i
, offset
));
3560 offset
+= insn_size
;
3562 this->size_
= offset
;
3567 // Template to implement do_write for a specific target endianity.
3569 template<bool big_endian
>
3571 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
3573 const Stub_template
* stub_template
= this->stub_template();
3574 const Insn_template
* insns
= stub_template
->insns();
3576 // FIXME: We do not handle BE8 encoding yet.
3577 unsigned char* pov
= view
;
3578 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
3580 switch (insns
[i
].type())
3582 case Insn_template::THUMB16_TYPE
:
3583 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
3585 case Insn_template::THUMB16_SPECIAL_TYPE
:
3586 elfcpp::Swap
<16, big_endian
>::writeval(
3588 this->thumb16_special(i
));
3590 case Insn_template::THUMB32_TYPE
:
3592 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
3593 uint32_t lo
= insns
[i
].data() & 0xffff;
3594 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
3595 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
3598 case Insn_template::ARM_TYPE
:
3599 case Insn_template::DATA_TYPE
:
3600 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
3605 pov
+= insns
[i
].size();
3607 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
3610 // Reloc_stub::Key methods.
3612 // Dump a Key as a string for debugging.
3615 Reloc_stub::Key::name() const
3617 if (this->r_sym_
== invalid_index
)
3619 // Global symbol key name
3620 // <stub-type>:<symbol name>:<addend>.
3621 const std::string sym_name
= this->u_
.symbol
->name();
3622 // We need to print two hex number and two colons. So just add 100 bytes
3623 // to the symbol name size.
3624 size_t len
= sym_name
.size() + 100;
3625 char* buffer
= new char[len
];
3626 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
3627 sym_name
.c_str(), this->addend_
);
3628 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3630 return std::string(buffer
);
3634 // local symbol key name
3635 // <stub-type>:<object>:<r_sym>:<addend>.
3636 const size_t len
= 200;
3638 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3639 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3640 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3641 return std::string(buffer
);
3645 // Reloc_stub methods.
3647 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3648 // LOCATION to DESTINATION.
3649 // This code is based on the arm_type_of_stub function in
3650 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3654 Reloc_stub::stub_type_for_reloc(
3655 unsigned int r_type
,
3656 Arm_address location
,
3657 Arm_address destination
,
3658 bool target_is_thumb
)
3660 Stub_type stub_type
= arm_stub_none
;
3662 // This is a bit ugly but we want to avoid using a templated class for
3663 // big and little endianities.
3665 bool should_force_pic_veneer
;
3668 if (parameters
->target().is_big_endian())
3670 const Target_arm
<true>* big_endian_target
=
3671 Target_arm
<true>::default_target();
3672 may_use_blx
= big_endian_target
->may_use_blx();
3673 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3674 thumb2
= big_endian_target
->using_thumb2();
3675 thumb_only
= big_endian_target
->using_thumb_only();
3679 const Target_arm
<false>* little_endian_target
=
3680 Target_arm
<false>::default_target();
3681 may_use_blx
= little_endian_target
->may_use_blx();
3682 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3683 thumb2
= little_endian_target
->using_thumb2();
3684 thumb_only
= little_endian_target
->using_thumb_only();
3687 int64_t branch_offset
= (int64_t)destination
- location
;
3689 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3691 // Handle cases where:
3692 // - this call goes too far (different Thumb/Thumb2 max
3694 // - it's a Thumb->Arm call and blx is not available, or it's a
3695 // Thumb->Arm branch (not bl). A stub is needed in this case.
3697 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3698 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3700 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3701 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3702 || ((!target_is_thumb
)
3703 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3704 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3706 if (target_is_thumb
)
3711 stub_type
= (parameters
->options().shared()
3712 || should_force_pic_veneer
)
3715 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3716 // V5T and above. Stub starts with ARM code, so
3717 // we must be able to switch mode before
3718 // reaching it, which is only possible for 'bl'
3719 // (ie R_ARM_THM_CALL relocation).
3720 ? arm_stub_long_branch_any_thumb_pic
3721 // On V4T, use Thumb code only.
3722 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3726 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3727 ? arm_stub_long_branch_any_any
// V5T and above.
3728 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3732 stub_type
= (parameters
->options().shared()
3733 || should_force_pic_veneer
)
3734 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3735 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3742 // FIXME: We should check that the input section is from an
3743 // object that has interwork enabled.
3745 stub_type
= (parameters
->options().shared()
3746 || should_force_pic_veneer
)
3749 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3750 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3751 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3755 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3756 ? arm_stub_long_branch_any_any
// V5T and above.
3757 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3759 // Handle v4t short branches.
3760 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3761 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3762 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3763 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3767 else if (r_type
== elfcpp::R_ARM_CALL
3768 || r_type
== elfcpp::R_ARM_JUMP24
3769 || r_type
== elfcpp::R_ARM_PLT32
)
3771 if (target_is_thumb
)
3775 // FIXME: We should check that the input section is from an
3776 // object that has interwork enabled.
3778 // We have an extra 2-bytes reach because of
3779 // the mode change (bit 24 (H) of BLX encoding).
3780 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3781 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3782 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3783 || (r_type
== elfcpp::R_ARM_JUMP24
)
3784 || (r_type
== elfcpp::R_ARM_PLT32
))
3786 stub_type
= (parameters
->options().shared()
3787 || should_force_pic_veneer
)
3790 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3791 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3795 ? arm_stub_long_branch_any_any
// V5T and above.
3796 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3802 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3803 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3805 stub_type
= (parameters
->options().shared()
3806 || should_force_pic_veneer
)
3807 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3808 : arm_stub_long_branch_any_any
; /// non-PIC.
3816 // Cortex_a8_stub methods.
3818 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3819 // I is the position of the instruction template in the stub template.
3822 Cortex_a8_stub::do_thumb16_special(size_t i
)
3824 // The only use of this is to copy condition code from a conditional
3825 // branch being worked around to the corresponding conditional branch in
3827 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3829 uint16_t data
= this->stub_template()->insns()[i
].data();
3830 gold_assert((data
& 0xff00U
) == 0xd000U
);
3831 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3835 // Stub_factory methods.
3837 Stub_factory::Stub_factory()
3839 // The instruction template sequences are declared as static
3840 // objects and initialized first time the constructor runs.
3842 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3843 // to reach the stub if necessary.
3844 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3846 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3847 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3848 // dcd R_ARM_ABS32(X)
3851 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3853 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3855 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3856 Insn_template::arm_insn(0xe12fff1c), // bx ip
3857 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3858 // dcd R_ARM_ABS32(X)
3861 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3862 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3864 Insn_template::thumb16_insn(0xb401), // push {r0}
3865 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3866 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3867 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3868 Insn_template::thumb16_insn(0x4760), // bx ip
3869 Insn_template::thumb16_insn(0xbf00), // nop
3870 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3871 // dcd R_ARM_ABS32(X)
3874 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3876 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3878 Insn_template::thumb16_insn(0x4778), // bx pc
3879 Insn_template::thumb16_insn(0x46c0), // nop
3880 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3881 Insn_template::arm_insn(0xe12fff1c), // bx ip
3882 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3883 // dcd R_ARM_ABS32(X)
3886 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3888 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3890 Insn_template::thumb16_insn(0x4778), // bx pc
3891 Insn_template::thumb16_insn(0x46c0), // nop
3892 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3893 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3894 // dcd R_ARM_ABS32(X)
3897 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3898 // one, when the destination is close enough.
3899 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3901 Insn_template::thumb16_insn(0x4778), // bx pc
3902 Insn_template::thumb16_insn(0x46c0), // nop
3903 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3906 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3907 // blx to reach the stub if necessary.
3908 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3910 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3911 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3912 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3913 // dcd R_ARM_REL32(X-4)
3916 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3917 // blx to reach the stub if necessary. We can not add into pc;
3918 // it is not guaranteed to mode switch (different in ARMv6 and
3920 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3922 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3923 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3924 Insn_template::arm_insn(0xe12fff1c), // bx ip
3925 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3926 // dcd R_ARM_REL32(X)
3929 // V4T ARM -> ARM long branch stub, PIC.
3930 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3932 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3933 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3934 Insn_template::arm_insn(0xe12fff1c), // bx ip
3935 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3936 // dcd R_ARM_REL32(X)
3939 // V4T Thumb -> ARM long branch stub, PIC.
3940 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3942 Insn_template::thumb16_insn(0x4778), // bx pc
3943 Insn_template::thumb16_insn(0x46c0), // nop
3944 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3945 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3946 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3947 // dcd R_ARM_REL32(X)
3950 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3952 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3954 Insn_template::thumb16_insn(0xb401), // push {r0}
3955 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3956 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3957 Insn_template::thumb16_insn(0x4484), // add ip, r0
3958 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3959 Insn_template::thumb16_insn(0x4760), // bx ip
3960 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3961 // dcd R_ARM_REL32(X)
3964 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3966 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3968 Insn_template::thumb16_insn(0x4778), // bx pc
3969 Insn_template::thumb16_insn(0x46c0), // nop
3970 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3971 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3972 Insn_template::arm_insn(0xe12fff1c), // bx ip
3973 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3974 // dcd R_ARM_REL32(X)
3977 // Cortex-A8 erratum-workaround stubs.
3979 // Stub used for conditional branches (which may be beyond +/-1MB away,
3980 // so we can't use a conditional branch to reach this stub).
3987 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3989 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3990 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3991 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3995 // Stub used for b.w and bl.w instructions.
3997 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3999 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4002 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
4004 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4007 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4008 // instruction (which switches to ARM mode) to point to this stub. Jump to
4009 // the real destination using an ARM-mode branch.
4010 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
4012 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4015 // Stub used to provide an interworking for R_ARM_V4BX relocation
4016 // (bx r[n] instruction).
4017 static const Insn_template elf32_arm_stub_v4_veneer_bx
[] =
4019 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4020 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4021 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4024 // Fill in the stub template look-up table. Stub templates are constructed
4025 // per instance of Stub_factory for fast look-up without locking
4026 // in a thread-enabled environment.
4028 this->stub_templates_
[arm_stub_none
] =
4029 new Stub_template(arm_stub_none
, NULL
, 0);
4031 #define DEF_STUB(x) \
4035 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4036 Stub_type type = arm_stub_##x; \
4037 this->stub_templates_[type] = \
4038 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4046 // Stub_table methods.
4048 // Removel all Cortex-A8 stub.
4050 template<bool big_endian
>
4052 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
4054 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4055 p
!= this->cortex_a8_stubs_
.end();
4058 this->cortex_a8_stubs_
.clear();
4061 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4063 template<bool big_endian
>
4065 Stub_table
<big_endian
>::relocate_stub(
4067 const Relocate_info
<32, big_endian
>* relinfo
,
4068 Target_arm
<big_endian
>* arm_target
,
4069 Output_section
* output_section
,
4070 unsigned char* view
,
4071 Arm_address address
,
4072 section_size_type view_size
)
4074 const Stub_template
* stub_template
= stub
->stub_template();
4075 if (stub_template
->reloc_count() != 0)
4077 // Adjust view to cover the stub only.
4078 section_size_type offset
= stub
->offset();
4079 section_size_type stub_size
= stub_template
->size();
4080 gold_assert(offset
+ stub_size
<= view_size
);
4082 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
4083 address
+ offset
, stub_size
);
4087 // Relocate all stubs in this stub table.
4089 template<bool big_endian
>
4091 Stub_table
<big_endian
>::relocate_stubs(
4092 const Relocate_info
<32, big_endian
>* relinfo
,
4093 Target_arm
<big_endian
>* arm_target
,
4094 Output_section
* output_section
,
4095 unsigned char* view
,
4096 Arm_address address
,
4097 section_size_type view_size
)
4099 // If we are passed a view bigger than the stub table's. we need to
4101 gold_assert(address
== this->address()
4103 == static_cast<section_size_type
>(this->data_size())));
4105 // Relocate all relocation stubs.
4106 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4107 p
!= this->reloc_stubs_
.end();
4109 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4110 address
, view_size
);
4112 // Relocate all Cortex-A8 stubs.
4113 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4114 p
!= this->cortex_a8_stubs_
.end();
4116 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4117 address
, view_size
);
4119 // Relocate all ARM V4BX stubs.
4120 for (Arm_v4bx_stub_list::iterator p
= this->arm_v4bx_stubs_
.begin();
4121 p
!= this->arm_v4bx_stubs_
.end();
4125 this->relocate_stub(*p
, relinfo
, arm_target
, output_section
, view
,
4126 address
, view_size
);
4130 // Write out the stubs to file.
4132 template<bool big_endian
>
4134 Stub_table
<big_endian
>::do_write(Output_file
* of
)
4136 off_t offset
= this->offset();
4137 const section_size_type oview_size
=
4138 convert_to_section_size_type(this->data_size());
4139 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4141 // Write relocation stubs.
4142 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4143 p
!= this->reloc_stubs_
.end();
4146 Reloc_stub
* stub
= p
->second
;
4147 Arm_address address
= this->address() + stub
->offset();
4149 == align_address(address
,
4150 stub
->stub_template()->alignment()));
4151 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4155 // Write Cortex-A8 stubs.
4156 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4157 p
!= this->cortex_a8_stubs_
.end();
4160 Cortex_a8_stub
* stub
= p
->second
;
4161 Arm_address address
= this->address() + stub
->offset();
4163 == align_address(address
,
4164 stub
->stub_template()->alignment()));
4165 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4169 // Write ARM V4BX relocation stubs.
4170 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4171 p
!= this->arm_v4bx_stubs_
.end();
4177 Arm_address address
= this->address() + (*p
)->offset();
4179 == align_address(address
,
4180 (*p
)->stub_template()->alignment()));
4181 (*p
)->write(oview
+ (*p
)->offset(), (*p
)->stub_template()->size(),
4185 of
->write_output_view(this->offset(), oview_size
, oview
);
4188 // Update the data size and address alignment of the stub table at the end
4189 // of a relaxation pass. Return true if either the data size or the
4190 // alignment changed in this relaxation pass.
4192 template<bool big_endian
>
4194 Stub_table
<big_endian
>::update_data_size_and_addralign()
4197 unsigned addralign
= 1;
4199 // Go over all stubs in table to compute data size and address alignment.
4201 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4202 p
!= this->reloc_stubs_
.end();
4205 const Stub_template
* stub_template
= p
->second
->stub_template();
4206 addralign
= std::max(addralign
, stub_template
->alignment());
4207 size
= (align_address(size
, stub_template
->alignment())
4208 + stub_template
->size());
4211 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4212 p
!= this->cortex_a8_stubs_
.end();
4215 const Stub_template
* stub_template
= p
->second
->stub_template();
4216 addralign
= std::max(addralign
, stub_template
->alignment());
4217 size
= (align_address(size
, stub_template
->alignment())
4218 + stub_template
->size());
4221 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4222 p
!= this->arm_v4bx_stubs_
.end();
4228 const Stub_template
* stub_template
= (*p
)->stub_template();
4229 addralign
= std::max(addralign
, stub_template
->alignment());
4230 size
= (align_address(size
, stub_template
->alignment())
4231 + stub_template
->size());
4234 // Check if either data size or alignment changed in this pass.
4235 // Update prev_data_size_ and prev_addralign_. These will be used
4236 // as the current data size and address alignment for the next pass.
4237 bool changed
= size
!= this->prev_data_size_
;
4238 this->prev_data_size_
= size
;
4240 if (addralign
!= this->prev_addralign_
)
4242 this->prev_addralign_
= addralign
;
4247 // Finalize the stubs. This sets the offsets of the stubs within the stub
4248 // table. It also marks all input sections needing Cortex-A8 workaround.
4250 template<bool big_endian
>
4252 Stub_table
<big_endian
>::finalize_stubs()
4255 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4256 p
!= this->reloc_stubs_
.end();
4259 Reloc_stub
* stub
= p
->second
;
4260 const Stub_template
* stub_template
= stub
->stub_template();
4261 uint64_t stub_addralign
= stub_template
->alignment();
4262 off
= align_address(off
, stub_addralign
);
4263 stub
->set_offset(off
);
4264 off
+= stub_template
->size();
4267 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4268 p
!= this->cortex_a8_stubs_
.end();
4271 Cortex_a8_stub
* stub
= p
->second
;
4272 const Stub_template
* stub_template
= stub
->stub_template();
4273 uint64_t stub_addralign
= stub_template
->alignment();
4274 off
= align_address(off
, stub_addralign
);
4275 stub
->set_offset(off
);
4276 off
+= stub_template
->size();
4278 // Mark input section so that we can determine later if a code section
4279 // needs the Cortex-A8 workaround quickly.
4280 Arm_relobj
<big_endian
>* arm_relobj
=
4281 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
4282 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
4285 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4286 p
!= this->arm_v4bx_stubs_
.end();
4292 const Stub_template
* stub_template
= (*p
)->stub_template();
4293 uint64_t stub_addralign
= stub_template
->alignment();
4294 off
= align_address(off
, stub_addralign
);
4295 (*p
)->set_offset(off
);
4296 off
+= stub_template
->size();
4299 gold_assert(off
<= this->prev_data_size_
);
4302 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4303 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4304 // of the address range seen by the linker.
4306 template<bool big_endian
>
4308 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
4309 Target_arm
<big_endian
>* arm_target
,
4310 unsigned char* view
,
4311 Arm_address view_address
,
4312 section_size_type view_size
)
4314 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4315 for (Cortex_a8_stub_list::const_iterator p
=
4316 this->cortex_a8_stubs_
.lower_bound(view_address
);
4317 ((p
!= this->cortex_a8_stubs_
.end())
4318 && (p
->first
< (view_address
+ view_size
)));
4321 // We do not store the THUMB bit in the LSB of either the branch address
4322 // or the stub offset. There is no need to strip the LSB.
4323 Arm_address branch_address
= p
->first
;
4324 const Cortex_a8_stub
* stub
= p
->second
;
4325 Arm_address stub_address
= this->address() + stub
->offset();
4327 // Offset of the branch instruction relative to this view.
4328 section_size_type offset
=
4329 convert_to_section_size_type(branch_address
- view_address
);
4330 gold_assert((offset
+ 4) <= view_size
);
4332 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
4333 view
+ offset
, branch_address
);
4337 // Arm_input_section methods.
4339 // Initialize an Arm_input_section.
4341 template<bool big_endian
>
4343 Arm_input_section
<big_endian
>::init()
4345 Relobj
* relobj
= this->relobj();
4346 unsigned int shndx
= this->shndx();
4348 // Cache these to speed up size and alignment queries. It is too slow
4349 // to call section_addraglin and section_size every time.
4350 this->original_addralign_
= relobj
->section_addralign(shndx
);
4351 this->original_size_
= relobj
->section_size(shndx
);
4353 // We want to make this look like the original input section after
4354 // output sections are finalized.
4355 Output_section
* os
= relobj
->output_section(shndx
);
4356 off_t offset
= relobj
->output_section_offset(shndx
);
4357 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
4358 this->set_address(os
->address() + offset
);
4359 this->set_file_offset(os
->offset() + offset
);
4361 this->set_current_data_size(this->original_size_
);
4362 this->finalize_data_size();
4365 template<bool big_endian
>
4367 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
4369 // We have to write out the original section content.
4370 section_size_type section_size
;
4371 const unsigned char* section_contents
=
4372 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4373 of
->write(this->offset(), section_contents
, section_size
);
4375 // If this owns a stub table and it is not empty, write it.
4376 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
4377 this->stub_table_
->write(of
);
4380 // Finalize data size.
4382 template<bool big_endian
>
4384 Arm_input_section
<big_endian
>::set_final_data_size()
4386 // If this owns a stub table, finalize its data size as well.
4387 if (this->is_stub_table_owner())
4389 uint64_t address
= this->address();
4391 // The stub table comes after the original section contents.
4392 address
+= this->original_size_
;
4393 address
= align_address(address
, this->stub_table_
->addralign());
4394 off_t offset
= this->offset() + (address
- this->address());
4395 this->stub_table_
->set_address_and_file_offset(address
, offset
);
4396 address
+= this->stub_table_
->data_size();
4397 gold_assert(address
== this->address() + this->current_data_size());
4400 this->set_data_size(this->current_data_size());
4403 // Reset address and file offset.
4405 template<bool big_endian
>
4407 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
4409 // Size of the original input section contents.
4410 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
4412 // If this is a stub table owner, account for the stub table size.
4413 if (this->is_stub_table_owner())
4415 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
4417 // Reset the stub table's address and file offset. The
4418 // current data size for child will be updated after that.
4419 stub_table_
->reset_address_and_file_offset();
4420 off
= align_address(off
, stub_table_
->addralign());
4421 off
+= stub_table
->current_data_size();
4424 this->set_current_data_size(off
);
4427 // Arm_exidx_cantunwind methods.
4429 // Write this to Output file OF for a fixed endianity.
4431 template<bool big_endian
>
4433 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file
* of
)
4435 off_t offset
= this->offset();
4436 const section_size_type oview_size
= 8;
4437 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4439 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4440 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
);
4442 Output_section
* os
= this->relobj_
->output_section(this->shndx_
);
4443 gold_assert(os
!= NULL
);
4445 Arm_relobj
<big_endian
>* arm_relobj
=
4446 Arm_relobj
<big_endian
>::as_arm_relobj(this->relobj_
);
4447 Arm_address output_offset
=
4448 arm_relobj
->get_output_section_offset(this->shndx_
);
4449 Arm_address section_start
;
4450 if(output_offset
!= Arm_relobj
<big_endian
>::invalid_address
)
4451 section_start
= os
->address() + output_offset
;
4454 // Currently this only happens for a relaxed section.
4455 const Output_relaxed_input_section
* poris
=
4456 os
->find_relaxed_input_section(this->relobj_
, this->shndx_
);
4457 gold_assert(poris
!= NULL
);
4458 section_start
= poris
->address();
4461 // We always append this to the end of an EXIDX section.
4462 Arm_address output_address
=
4463 section_start
+ this->relobj_
->section_size(this->shndx_
);
4465 // Write out the entry. The first word either points to the beginning
4466 // or after the end of a text section. The second word is the special
4467 // EXIDX_CANTUNWIND value.
4468 elfcpp::Swap
<32, big_endian
>::writeval(wv
, output_address
);
4469 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, elfcpp::EXIDX_CANTUNWIND
);
4471 of
->write_output_view(this->offset(), oview_size
, oview
);
4474 // Arm_exidx_merged_section methods.
4476 // Constructor for Arm_exidx_merged_section.
4477 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4478 // SECTION_OFFSET_MAP points to a section offset map describing how
4479 // parts of the input section are mapped to output. DELETED_BYTES is
4480 // the number of bytes deleted from the EXIDX input section.
4482 Arm_exidx_merged_section::Arm_exidx_merged_section(
4483 const Arm_exidx_input_section
& exidx_input_section
,
4484 const Arm_exidx_section_offset_map
& section_offset_map
,
4485 uint32_t deleted_bytes
)
4486 : Output_relaxed_input_section(exidx_input_section
.relobj(),
4487 exidx_input_section
.shndx(),
4488 exidx_input_section
.addralign()),
4489 exidx_input_section_(exidx_input_section
),
4490 section_offset_map_(section_offset_map
)
4492 // Fix size here so that we do not need to implement set_final_data_size.
4493 this->set_data_size(exidx_input_section
.size() - deleted_bytes
);
4494 this->fix_data_size();
4497 // Given an input OBJECT, an input section index SHNDX within that
4498 // object, and an OFFSET relative to the start of that input
4499 // section, return whether or not the corresponding offset within
4500 // the output section is known. If this function returns true, it
4501 // sets *POUTPUT to the output offset. The value -1 indicates that
4502 // this input offset is being discarded.
4505 Arm_exidx_merged_section::do_output_offset(
4506 const Relobj
* relobj
,
4508 section_offset_type offset
,
4509 section_offset_type
* poutput
) const
4511 // We only handle offsets for the original EXIDX input section.
4512 if (relobj
!= this->exidx_input_section_
.relobj()
4513 || shndx
!= this->exidx_input_section_
.shndx())
4516 if (offset
< 0 || offset
>= this->exidx_input_section_
.size())
4517 // Input offset is out of valid range.
4521 // We need to look up the section offset map to determine the output
4522 // offset. Find the reference point in map that is first offset
4523 // bigger than or equal to this offset.
4524 Arm_exidx_section_offset_map::const_iterator p
=
4525 this->section_offset_map_
.lower_bound(offset
);
4527 // The section offset maps are build such that this should not happen if
4528 // input offset is in the valid range.
4529 gold_assert(p
!= this->section_offset_map_
.end());
4531 // We need to check if this is dropped.
4532 section_offset_type ref
= p
->first
;
4533 section_offset_type mapped_ref
= p
->second
;
4535 if (mapped_ref
!= Arm_exidx_input_section::invalid_offset
)
4536 // Offset is present in output.
4537 *poutput
= mapped_ref
+ (offset
- ref
);
4539 // Offset is discarded owing to EXIDX entry merging.
4546 // Write this to output file OF.
4549 Arm_exidx_merged_section::do_write(Output_file
* of
)
4551 // If we retain or discard the whole EXIDX input section, we would
4553 gold_assert(this->data_size() != this->exidx_input_section_
.size()
4554 && this->data_size() != 0);
4556 off_t offset
= this->offset();
4557 const section_size_type oview_size
= this->data_size();
4558 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4560 Output_section
* os
= this->relobj()->output_section(this->shndx());
4561 gold_assert(os
!= NULL
);
4563 // Get contents of EXIDX input section.
4564 section_size_type section_size
;
4565 const unsigned char* section_contents
=
4566 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4567 gold_assert(section_size
== this->exidx_input_section_
.size());
4569 // Go over spans of input offsets and write only those that are not
4571 section_offset_type in_start
= 0;
4572 section_offset_type out_start
= 0;
4573 for(Arm_exidx_section_offset_map::const_iterator p
=
4574 this->section_offset_map_
.begin();
4575 p
!= this->section_offset_map_
.end();
4578 section_offset_type in_end
= p
->first
;
4579 gold_assert(in_end
>= in_start
);
4580 section_offset_type out_end
= p
->second
;
4581 size_t in_chunk_size
= convert_types
<size_t>(in_end
- in_start
+ 1);
4584 size_t out_chunk_size
=
4585 convert_types
<size_t>(out_end
- out_start
+ 1);
4586 gold_assert(out_chunk_size
== in_chunk_size
);
4587 memcpy(oview
+ out_start
, section_contents
+ in_start
,
4589 out_start
+= out_chunk_size
;
4591 in_start
+= in_chunk_size
;
4594 gold_assert(convert_to_section_size_type(out_start
) == oview_size
);
4595 of
->write_output_view(this->offset(), oview_size
, oview
);
4598 // Arm_output_section methods.
4600 // Create a stub group for input sections from BEGIN to END. OWNER
4601 // points to the input section to be the owner a new stub table.
4603 template<bool big_endian
>
4605 Arm_output_section
<big_endian
>::create_stub_group(
4606 Input_section_list::const_iterator begin
,
4607 Input_section_list::const_iterator end
,
4608 Input_section_list::const_iterator owner
,
4609 Target_arm
<big_endian
>* target
,
4610 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
4612 // Currently we convert ordinary input sections into relaxed sections only
4613 // at this point but we may want to support creating relaxed input section
4614 // very early. So we check here to see if owner is already a relaxed
4617 Arm_input_section
<big_endian
>* arm_input_section
;
4618 if (owner
->is_relaxed_input_section())
4621 Arm_input_section
<big_endian
>::as_arm_input_section(
4622 owner
->relaxed_input_section());
4626 gold_assert(owner
->is_input_section());
4627 // Create a new relaxed input section.
4629 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
4630 new_relaxed_sections
->push_back(arm_input_section
);
4633 // Create a stub table.
4634 Stub_table
<big_endian
>* stub_table
=
4635 target
->new_stub_table(arm_input_section
);
4637 arm_input_section
->set_stub_table(stub_table
);
4639 Input_section_list::const_iterator p
= begin
;
4640 Input_section_list::const_iterator prev_p
;
4642 // Look for input sections or relaxed input sections in [begin ... end].
4645 if (p
->is_input_section() || p
->is_relaxed_input_section())
4647 // The stub table information for input sections live
4648 // in their objects.
4649 Arm_relobj
<big_endian
>* arm_relobj
=
4650 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
4651 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
4655 while (prev_p
!= end
);
4658 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4659 // of stub groups. We grow a stub group by adding input section until the
4660 // size is just below GROUP_SIZE. The last input section will be converted
4661 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4662 // input section after the stub table, effectively double the group size.
4664 // This is similar to the group_sections() function in elf32-arm.c but is
4665 // implemented differently.
4667 template<bool big_endian
>
4669 Arm_output_section
<big_endian
>::group_sections(
4670 section_size_type group_size
,
4671 bool stubs_always_after_branch
,
4672 Target_arm
<big_endian
>* target
)
4674 // We only care about sections containing code.
4675 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
4678 // States for grouping.
4681 // No group is being built.
4683 // A group is being built but the stub table is not found yet.
4684 // We keep group a stub group until the size is just under GROUP_SIZE.
4685 // The last input section in the group will be used as the stub table.
4686 FINDING_STUB_SECTION
,
4687 // A group is being built and we have already found a stub table.
4688 // We enter this state to grow a stub group by adding input section
4689 // after the stub table. This effectively doubles the group size.
4693 // Any newly created relaxed sections are stored here.
4694 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
4696 State state
= NO_GROUP
;
4697 section_size_type off
= 0;
4698 section_size_type group_begin_offset
= 0;
4699 section_size_type group_end_offset
= 0;
4700 section_size_type stub_table_end_offset
= 0;
4701 Input_section_list::const_iterator group_begin
=
4702 this->input_sections().end();
4703 Input_section_list::const_iterator stub_table
=
4704 this->input_sections().end();
4705 Input_section_list::const_iterator group_end
= this->input_sections().end();
4706 for (Input_section_list::const_iterator p
= this->input_sections().begin();
4707 p
!= this->input_sections().end();
4710 section_size_type section_begin_offset
=
4711 align_address(off
, p
->addralign());
4712 section_size_type section_end_offset
=
4713 section_begin_offset
+ p
->data_size();
4715 // Check to see if we should group the previously seens sections.
4721 case FINDING_STUB_SECTION
:
4722 // Adding this section makes the group larger than GROUP_SIZE.
4723 if (section_end_offset
- group_begin_offset
>= group_size
)
4725 if (stubs_always_after_branch
)
4727 gold_assert(group_end
!= this->input_sections().end());
4728 this->create_stub_group(group_begin
, group_end
, group_end
,
4729 target
, &new_relaxed_sections
);
4734 // But wait, there's more! Input sections up to
4735 // stub_group_size bytes after the stub table can be
4736 // handled by it too.
4737 state
= HAS_STUB_SECTION
;
4738 stub_table
= group_end
;
4739 stub_table_end_offset
= group_end_offset
;
4744 case HAS_STUB_SECTION
:
4745 // Adding this section makes the post stub-section group larger
4747 if (section_end_offset
- stub_table_end_offset
>= group_size
)
4749 gold_assert(group_end
!= this->input_sections().end());
4750 this->create_stub_group(group_begin
, group_end
, stub_table
,
4751 target
, &new_relaxed_sections
);
4760 // If we see an input section and currently there is no group, start
4761 // a new one. Skip any empty sections.
4762 if ((p
->is_input_section() || p
->is_relaxed_input_section())
4763 && (p
->relobj()->section_size(p
->shndx()) != 0))
4765 if (state
== NO_GROUP
)
4767 state
= FINDING_STUB_SECTION
;
4769 group_begin_offset
= section_begin_offset
;
4772 // Keep track of the last input section seen.
4774 group_end_offset
= section_end_offset
;
4777 off
= section_end_offset
;
4780 // Create a stub group for any ungrouped sections.
4781 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
4783 gold_assert(group_end
!= this->input_sections().end());
4784 this->create_stub_group(group_begin
, group_end
,
4785 (state
== FINDING_STUB_SECTION
4788 target
, &new_relaxed_sections
);
4791 // Convert input section into relaxed input section in a batch.
4792 if (!new_relaxed_sections
.empty())
4793 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
4795 // Update the section offsets
4796 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
4798 Arm_relobj
<big_endian
>* arm_relobj
=
4799 Arm_relobj
<big_endian
>::as_arm_relobj(
4800 new_relaxed_sections
[i
]->relobj());
4801 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
4802 // Tell Arm_relobj that this input section is converted.
4803 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
4807 // Arm_relobj methods.
4809 // Determine if we want to scan the SHNDX-th section for relocation stubs.
4810 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4812 template<bool big_endian
>
4814 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
4815 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4816 const Relobj::Output_sections
& out_sections
,
4817 const Symbol_table
*symtab
)
4819 unsigned int sh_type
= shdr
.get_sh_type();
4820 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
4823 // Ignore empty section.
4824 off_t sh_size
= shdr
.get_sh_size();
4828 // Ignore reloc section with bad info. This error will be
4829 // reported in the final link.
4830 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4831 if (index
>= this->shnum())
4834 // This relocation section is against a section which we
4835 // discarded or if the section is folded into another
4836 // section due to ICF.
4837 if (out_sections
[index
] == NULL
|| symtab
->is_section_folded(this, index
))
4840 // Ignore reloc section with unexpected symbol table. The
4841 // error will be reported in the final link.
4842 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4845 unsigned int reloc_size
;
4846 if (sh_type
== elfcpp::SHT_REL
)
4847 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4849 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4851 // Ignore reloc section with unexpected entsize or uneven size.
4852 // The error will be reported in the final link.
4853 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
4859 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4860 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4862 template<bool big_endian
>
4864 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
4865 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4868 const Symbol_table
* symtab
)
4870 // We only scan non-empty code sections.
4871 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) == 0
4872 || shdr
.get_sh_size() == 0)
4875 // Ignore discarded or ICF'ed sections.
4876 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
4879 // Find output address of section.
4880 Arm_address address
= os
->output_address(this, shndx
, 0);
4882 // If the section does not cross any 4K-boundaries, it does not need to
4884 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
4890 // Scan a section for Cortex-A8 workaround.
4892 template<bool big_endian
>
4894 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
4895 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4898 Target_arm
<big_endian
>* arm_target
)
4900 Arm_address output_address
= os
->output_address(this, shndx
, 0);
4902 // Get the section contents.
4903 section_size_type input_view_size
= 0;
4904 const unsigned char* input_view
=
4905 this->section_contents(shndx
, &input_view_size
, false);
4907 // We need to go through the mapping symbols to determine what to
4908 // scan. There are two reasons. First, we should look at THUMB code and
4909 // THUMB code only. Second, we only want to look at the 4K-page boundary
4910 // to speed up the scanning.
4912 // Look for the first mapping symbol in this section. It should be
4914 Mapping_symbol_position
section_start(shndx
, 0);
4915 typename
Mapping_symbols_info::const_iterator p
=
4916 this->mapping_symbols_info_
.lower_bound(section_start
);
4918 if (p
== this->mapping_symbols_info_
.end()
4919 || p
->first
!= section_start
)
4921 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4922 "is no mapping symbols for section %u of %s"),
4923 shndx
, this->name().c_str());
4927 while (p
!= this->mapping_symbols_info_
.end()
4928 && p
->first
.first
== shndx
)
4930 typename
Mapping_symbols_info::const_iterator next
=
4931 this->mapping_symbols_info_
.upper_bound(p
->first
);
4933 // Only scan part of a section with THUMB code.
4934 if (p
->second
== 't')
4936 // Determine the end of this range.
4937 section_size_type span_start
=
4938 convert_to_section_size_type(p
->first
.second
);
4939 section_size_type span_end
;
4940 if (next
!= this->mapping_symbols_info_
.end()
4941 && next
->first
.first
== shndx
)
4942 span_end
= convert_to_section_size_type(next
->first
.second
);
4944 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
4946 if (((span_start
+ output_address
) & ~0xfffUL
)
4947 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
4949 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
4950 span_start
, span_end
,
4960 // Scan relocations for stub generation.
4962 template<bool big_endian
>
4964 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
4965 Target_arm
<big_endian
>* arm_target
,
4966 const Symbol_table
* symtab
,
4967 const Layout
* layout
)
4969 unsigned int shnum
= this->shnum();
4970 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4972 // Read the section headers.
4973 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4977 // To speed up processing, we set up hash tables for fast lookup of
4978 // input offsets to output addresses.
4979 this->initialize_input_to_output_maps();
4981 const Relobj::Output_sections
& out_sections(this->output_sections());
4983 Relocate_info
<32, big_endian
> relinfo
;
4984 relinfo
.symtab
= symtab
;
4985 relinfo
.layout
= layout
;
4986 relinfo
.object
= this;
4988 // Do relocation stubs scanning.
4989 const unsigned char* p
= pshdrs
+ shdr_size
;
4990 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4992 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
4993 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
))
4995 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4996 Arm_address output_offset
= this->get_output_section_offset(index
);
4997 Arm_address output_address
;
4998 if(output_offset
!= invalid_address
)
4999 output_address
= out_sections
[index
]->address() + output_offset
;
5002 // Currently this only happens for a relaxed section.
5003 const Output_relaxed_input_section
* poris
=
5004 out_sections
[index
]->find_relaxed_input_section(this, index
);
5005 gold_assert(poris
!= NULL
);
5006 output_address
= poris
->address();
5009 // Get the relocations.
5010 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
5014 // Get the section contents. This does work for the case in which
5015 // we modify the contents of an input section. We need to pass the
5016 // output view under such circumstances.
5017 section_size_type input_view_size
= 0;
5018 const unsigned char* input_view
=
5019 this->section_contents(index
, &input_view_size
, false);
5021 relinfo
.reloc_shndx
= i
;
5022 relinfo
.data_shndx
= index
;
5023 unsigned int sh_type
= shdr
.get_sh_type();
5024 unsigned int reloc_size
;
5025 if (sh_type
== elfcpp::SHT_REL
)
5026 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5028 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
5030 Output_section
* os
= out_sections
[index
];
5031 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
5032 shdr
.get_sh_size() / reloc_size
,
5034 output_offset
== invalid_address
,
5035 input_view
, output_address
,
5040 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5041 // after its relocation section, if there is one, is processed for
5042 // relocation stubs. Merging this loop with the one above would have been
5043 // complicated since we would have had to make sure that relocation stub
5044 // scanning is done first.
5045 if (arm_target
->fix_cortex_a8())
5047 const unsigned char* p
= pshdrs
+ shdr_size
;
5048 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
5050 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
5051 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
5054 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
5059 // After we've done the relocations, we release the hash tables,
5060 // since we no longer need them.
5061 this->free_input_to_output_maps();
5064 // Count the local symbols. The ARM backend needs to know if a symbol
5065 // is a THUMB function or not. For global symbols, it is easy because
5066 // the Symbol object keeps the ELF symbol type. For local symbol it is
5067 // harder because we cannot access this information. So we override the
5068 // do_count_local_symbol in parent and scan local symbols to mark
5069 // THUMB functions. This is not the most efficient way but I do not want to
5070 // slow down other ports by calling a per symbol targer hook inside
5071 // Sized_relobj<size, big_endian>::do_count_local_symbols.
5073 template<bool big_endian
>
5075 Arm_relobj
<big_endian
>::do_count_local_symbols(
5076 Stringpool_template
<char>* pool
,
5077 Stringpool_template
<char>* dynpool
)
5079 // We need to fix-up the values of any local symbols whose type are
5082 // Ask parent to count the local symbols.
5083 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
5084 const unsigned int loccount
= this->local_symbol_count();
5088 // Intialize the thumb function bit-vector.
5089 std::vector
<bool> empty_vector(loccount
, false);
5090 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
5092 // Read the symbol table section header.
5093 const unsigned int symtab_shndx
= this->symtab_shndx();
5094 elfcpp::Shdr
<32, big_endian
>
5095 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
5096 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
5098 // Read the local symbols.
5099 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
5100 gold_assert(loccount
== symtabshdr
.get_sh_info());
5101 off_t locsize
= loccount
* sym_size
;
5102 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
5103 locsize
, true, true);
5105 // For mapping symbol processing, we need to read the symbol names.
5106 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
5107 if (strtab_shndx
>= this->shnum())
5109 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
5113 elfcpp::Shdr
<32, big_endian
>
5114 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
5115 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
5117 this->error(_("symbol table name section has wrong type: %u"),
5118 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
5121 const char* pnames
=
5122 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
5123 strtabshdr
.get_sh_size(),
5126 // Loop over the local symbols and mark any local symbols pointing
5127 // to THUMB functions.
5129 // Skip the first dummy symbol.
5131 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
5132 this->local_values();
5133 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
5135 elfcpp::Sym
<32, big_endian
> sym(psyms
);
5136 elfcpp::STT st_type
= sym
.get_st_type();
5137 Symbol_value
<32>& lv((*plocal_values
)[i
]);
5138 Arm_address input_value
= lv
.input_value();
5140 // Check to see if this is a mapping symbol.
5141 const char* sym_name
= pnames
+ sym
.get_st_name();
5142 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
5144 unsigned int input_shndx
= sym
.get_st_shndx();
5146 // Strip of LSB in case this is a THUMB symbol.
5147 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
5148 this->mapping_symbols_info_
[msp
] = sym_name
[1];
5151 if (st_type
== elfcpp::STT_ARM_TFUNC
5152 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
5154 // This is a THUMB function. Mark this and canonicalize the
5155 // symbol value by setting LSB.
5156 this->local_symbol_is_thumb_function_
[i
] = true;
5157 if ((input_value
& 1) == 0)
5158 lv
.set_input_value(input_value
| 1);
5163 // Relocate sections.
5164 template<bool big_endian
>
5166 Arm_relobj
<big_endian
>::do_relocate_sections(
5167 const Symbol_table
* symtab
,
5168 const Layout
* layout
,
5169 const unsigned char* pshdrs
,
5170 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
5172 // Call parent to relocate sections.
5173 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
5176 // We do not generate stubs if doing a relocatable link.
5177 if (parameters
->options().relocatable())
5180 // Relocate stub tables.
5181 unsigned int shnum
= this->shnum();
5183 Target_arm
<big_endian
>* arm_target
=
5184 Target_arm
<big_endian
>::default_target();
5186 Relocate_info
<32, big_endian
> relinfo
;
5187 relinfo
.symtab
= symtab
;
5188 relinfo
.layout
= layout
;
5189 relinfo
.object
= this;
5191 for (unsigned int i
= 1; i
< shnum
; ++i
)
5193 Arm_input_section
<big_endian
>* arm_input_section
=
5194 arm_target
->find_arm_input_section(this, i
);
5196 if (arm_input_section
!= NULL
5197 && arm_input_section
->is_stub_table_owner()
5198 && !arm_input_section
->stub_table()->empty())
5200 // We cannot discard a section if it owns a stub table.
5201 Output_section
* os
= this->output_section(i
);
5202 gold_assert(os
!= NULL
);
5204 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
5205 relinfo
.reloc_shdr
= NULL
;
5206 relinfo
.data_shndx
= i
;
5207 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
5209 gold_assert((*pviews
)[i
].view
!= NULL
);
5211 // We are passed the output section view. Adjust it to cover the
5213 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
5214 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
5215 && ((stub_table
->address() + stub_table
->data_size())
5216 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
5218 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
5219 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
5220 Arm_address address
= stub_table
->address();
5221 section_size_type view_size
= stub_table
->data_size();
5223 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
5227 // Apply Cortex A8 workaround if applicable.
5228 if (this->section_has_cortex_a8_workaround(i
))
5230 unsigned char* view
= (*pviews
)[i
].view
;
5231 Arm_address view_address
= (*pviews
)[i
].address
;
5232 section_size_type view_size
= (*pviews
)[i
].view_size
;
5233 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
5235 // Adjust view to cover section.
5236 Output_section
* os
= this->output_section(i
);
5237 gold_assert(os
!= NULL
);
5238 Arm_address section_address
= os
->output_address(this, i
, 0);
5239 uint64_t section_size
= this->section_size(i
);
5241 gold_assert(section_address
>= view_address
5242 && ((section_address
+ section_size
)
5243 <= (view_address
+ view_size
)));
5245 unsigned char* section_view
= view
+ (section_address
- view_address
);
5247 // Apply the Cortex-A8 workaround to the output address range
5248 // corresponding to this input section.
5249 stub_table
->apply_cortex_a8_workaround_to_address_range(
5258 // Create a new EXIDX input section object for EXIDX section SHNDX with
5261 template<bool big_endian
>
5263 Arm_relobj
<big_endian
>::make_exidx_input_section(
5265 const elfcpp::Shdr
<32, big_endian
>& shdr
)
5267 // Link .text section to its .ARM.exidx section in the same object.
5268 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
5270 // Issue an error and ignore this EXIDX section if it does not point
5271 // to any text section.
5272 if (text_shndx
== elfcpp::SHN_UNDEF
)
5274 gold_error(_("EXIDX section %u in %s has no linked text section"),
5275 shndx
, this->name().c_str());
5279 // Issue an error and ignore this EXIDX section if it points to a text
5280 // section already has an EXIDX section.
5281 if (this->exidx_section_map_
[text_shndx
] != NULL
)
5283 gold_error(_("EXIDX sections %u and %u both link to text section %u "
5285 shndx
, this->exidx_section_map_
[text_shndx
]->shndx(),
5286 text_shndx
, this->name().c_str());
5290 // Create an Arm_exidx_input_section object for this EXIDX section.
5291 Arm_exidx_input_section
* exidx_input_section
=
5292 new Arm_exidx_input_section(this, shndx
, text_shndx
, shdr
.get_sh_size(),
5293 shdr
.get_sh_addralign());
5294 this->exidx_section_map_
[text_shndx
] = exidx_input_section
;
5296 // Also map the EXIDX section index to this.
5297 gold_assert(this->exidx_section_map_
[shndx
] == NULL
);
5298 this->exidx_section_map_
[shndx
] = exidx_input_section
;
5301 // Read the symbol information.
5303 template<bool big_endian
>
5305 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
5307 // Call parent class to read symbol information.
5308 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
5310 // Read processor-specific flags in ELF file header.
5311 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
5312 elfcpp::Elf_sizes
<32>::ehdr_size
,
5314 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
5315 this->processor_specific_flags_
= ehdr
.get_e_flags();
5317 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
5319 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5320 const unsigned char *ps
=
5321 sd
->section_headers
->data() + shdr_size
;
5322 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
5324 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5325 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
5327 gold_assert(this->attributes_section_data_
== NULL
);
5328 section_offset_type section_offset
= shdr
.get_sh_offset();
5329 section_size_type section_size
=
5330 convert_to_section_size_type(shdr
.get_sh_size());
5331 File_view
* view
= this->get_lasting_view(section_offset
,
5332 section_size
, true, false);
5333 this->attributes_section_data_
=
5334 new Attributes_section_data(view
->data(), section_size
);
5336 else if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
5337 this->make_exidx_input_section(i
, shdr
);
5341 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
5342 // sections for unwinding. These sections are referenced implicitly by
5343 // text sections linked in the section headers. If we ignore these implict
5344 // references, the .ARM.exidx sections and any .ARM.extab sections they use
5345 // will be garbage-collected incorrectly. Hence we override the same function
5346 // in the base class to handle these implicit references.
5348 template<bool big_endian
>
5350 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
5352 Read_relocs_data
* rd
)
5354 // First, call base class method to process relocations in this object.
5355 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
5357 unsigned int shnum
= this->shnum();
5358 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5359 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
5363 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
5364 // to these from the linked text sections.
5365 const unsigned char* ps
= pshdrs
+ shdr_size
;
5366 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
5368 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5369 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
5371 // Found an .ARM.exidx section, add it to the set of reachable
5372 // sections from its linked text section.
5373 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
5374 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
5379 // Arm_dynobj methods.
5381 // Read the symbol information.
5383 template<bool big_endian
>
5385 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
5387 // Call parent class to read symbol information.
5388 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
5390 // Read processor-specific flags in ELF file header.
5391 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
5392 elfcpp::Elf_sizes
<32>::ehdr_size
,
5394 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
5395 this->processor_specific_flags_
= ehdr
.get_e_flags();
5397 // Read the attributes section if there is one.
5398 // We read from the end because gas seems to put it near the end of
5399 // the section headers.
5400 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5401 const unsigned char *ps
=
5402 sd
->section_headers
->data() + shdr_size
* (this->shnum() - 1);
5403 for (unsigned int i
= this->shnum(); i
> 0; --i
, ps
-= shdr_size
)
5405 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5406 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
5408 section_offset_type section_offset
= shdr
.get_sh_offset();
5409 section_size_type section_size
=
5410 convert_to_section_size_type(shdr
.get_sh_size());
5411 File_view
* view
= this->get_lasting_view(section_offset
,
5412 section_size
, true, false);
5413 this->attributes_section_data_
=
5414 new Attributes_section_data(view
->data(), section_size
);
5420 // Stub_addend_reader methods.
5422 // Read the addend of a REL relocation of type R_TYPE at VIEW.
5424 template<bool big_endian
>
5425 elfcpp::Elf_types
<32>::Elf_Swxword
5426 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
5427 unsigned int r_type
,
5428 const unsigned char* view
,
5429 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
5431 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
5435 case elfcpp::R_ARM_CALL
:
5436 case elfcpp::R_ARM_JUMP24
:
5437 case elfcpp::R_ARM_PLT32
:
5439 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5440 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5441 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5442 return utils::sign_extend
<26>(val
<< 2);
5445 case elfcpp::R_ARM_THM_CALL
:
5446 case elfcpp::R_ARM_THM_JUMP24
:
5447 case elfcpp::R_ARM_THM_XPC22
:
5449 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5450 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5451 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5452 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5453 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
5456 case elfcpp::R_ARM_THM_JUMP19
:
5458 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5459 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5460 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5461 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5462 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
5470 // A class to handle the PLT data.
5472 template<bool big_endian
>
5473 class Output_data_plt_arm
: public Output_section_data
5476 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
5479 Output_data_plt_arm(Layout
*, Output_data_space
*);
5481 // Add an entry to the PLT.
5483 add_entry(Symbol
* gsym
);
5485 // Return the .rel.plt section data.
5486 const Reloc_section
*
5488 { return this->rel_
; }
5492 do_adjust_output_section(Output_section
* os
);
5494 // Write to a map file.
5496 do_print_to_mapfile(Mapfile
* mapfile
) const
5497 { mapfile
->print_output_data(this, _("** PLT")); }
5500 // Template for the first PLT entry.
5501 static const uint32_t first_plt_entry
[5];
5503 // Template for subsequent PLT entries.
5504 static const uint32_t plt_entry
[3];
5506 // Set the final size.
5508 set_final_data_size()
5510 this->set_data_size(sizeof(first_plt_entry
)
5511 + this->count_
* sizeof(plt_entry
));
5514 // Write out the PLT data.
5516 do_write(Output_file
*);
5518 // The reloc section.
5519 Reloc_section
* rel_
;
5520 // The .got.plt section.
5521 Output_data_space
* got_plt_
;
5522 // The number of PLT entries.
5523 unsigned int count_
;
5526 // Create the PLT section. The ordinary .got section is an argument,
5527 // since we need to refer to the start. We also create our own .got
5528 // section just for PLT entries.
5530 template<bool big_endian
>
5531 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
5532 Output_data_space
* got_plt
)
5533 : Output_section_data(4), got_plt_(got_plt
), count_(0)
5535 this->rel_
= new Reloc_section(false);
5536 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
5537 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
5541 template<bool big_endian
>
5543 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
5548 // Add an entry to the PLT.
5550 template<bool big_endian
>
5552 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
5554 gold_assert(!gsym
->has_plt_offset());
5556 // Note that when setting the PLT offset we skip the initial
5557 // reserved PLT entry.
5558 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
5559 + sizeof(first_plt_entry
));
5563 section_offset_type got_offset
= this->got_plt_
->current_data_size();
5565 // Every PLT entry needs a GOT entry which points back to the PLT
5566 // entry (this will be changed by the dynamic linker, normally
5567 // lazily when the function is called).
5568 this->got_plt_
->set_current_data_size(got_offset
+ 4);
5570 // Every PLT entry needs a reloc.
5571 gsym
->set_needs_dynsym_entry();
5572 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
5575 // Note that we don't need to save the symbol. The contents of the
5576 // PLT are independent of which symbols are used. The symbols only
5577 // appear in the relocations.
5581 // FIXME: This is not very flexible. Right now this has only been tested
5582 // on armv5te. If we are to support additional architecture features like
5583 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
5585 // The first entry in the PLT.
5586 template<bool big_endian
>
5587 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
5589 0xe52de004, // str lr, [sp, #-4]!
5590 0xe59fe004, // ldr lr, [pc, #4]
5591 0xe08fe00e, // add lr, pc, lr
5592 0xe5bef008, // ldr pc, [lr, #8]!
5593 0x00000000, // &GOT[0] - .
5596 // Subsequent entries in the PLT.
5598 template<bool big_endian
>
5599 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
5601 0xe28fc600, // add ip, pc, #0xNN00000
5602 0xe28cca00, // add ip, ip, #0xNN000
5603 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
5606 // Write out the PLT. This uses the hand-coded instructions above,
5607 // and adjusts them as needed. This is all specified by the arm ELF
5608 // Processor Supplement.
5610 template<bool big_endian
>
5612 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
5614 const off_t offset
= this->offset();
5615 const section_size_type oview_size
=
5616 convert_to_section_size_type(this->data_size());
5617 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5619 const off_t got_file_offset
= this->got_plt_
->offset();
5620 const section_size_type got_size
=
5621 convert_to_section_size_type(this->got_plt_
->data_size());
5622 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
5624 unsigned char* pov
= oview
;
5626 Arm_address plt_address
= this->address();
5627 Arm_address got_address
= this->got_plt_
->address();
5629 // Write first PLT entry. All but the last word are constants.
5630 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
5631 / sizeof(plt_entry
[0]));
5632 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
5633 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
5634 // Last word in first PLT entry is &GOT[0] - .
5635 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
5636 got_address
- (plt_address
+ 16));
5637 pov
+= sizeof(first_plt_entry
);
5639 unsigned char* got_pov
= got_view
;
5641 memset(got_pov
, 0, 12);
5644 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5645 unsigned int plt_offset
= sizeof(first_plt_entry
);
5646 unsigned int plt_rel_offset
= 0;
5647 unsigned int got_offset
= 12;
5648 const unsigned int count
= this->count_
;
5649 for (unsigned int i
= 0;
5652 pov
+= sizeof(plt_entry
),
5654 plt_offset
+= sizeof(plt_entry
),
5655 plt_rel_offset
+= rel_size
,
5658 // Set and adjust the PLT entry itself.
5659 int32_t offset
= ((got_address
+ got_offset
)
5660 - (plt_address
+ plt_offset
+ 8));
5662 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
5663 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
5664 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
5665 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
5666 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
5667 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
5668 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
5670 // Set the entry in the GOT.
5671 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
5674 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
5675 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
5677 of
->write_output_view(offset
, oview_size
, oview
);
5678 of
->write_output_view(got_file_offset
, got_size
, got_view
);
5681 // Create a PLT entry for a global symbol.
5683 template<bool big_endian
>
5685 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
5688 if (gsym
->has_plt_offset())
5691 if (this->plt_
== NULL
)
5693 // Create the GOT sections first.
5694 this->got_section(symtab
, layout
);
5696 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
5697 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
5699 | elfcpp::SHF_EXECINSTR
),
5700 this->plt_
, false, false, false, false);
5702 this->plt_
->add_entry(gsym
);
5705 // Report an unsupported relocation against a local symbol.
5707 template<bool big_endian
>
5709 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
5710 Sized_relobj
<32, big_endian
>* object
,
5711 unsigned int r_type
)
5713 gold_error(_("%s: unsupported reloc %u against local symbol"),
5714 object
->name().c_str(), r_type
);
5717 // We are about to emit a dynamic relocation of type R_TYPE. If the
5718 // dynamic linker does not support it, issue an error. The GNU linker
5719 // only issues a non-PIC error for an allocated read-only section.
5720 // Here we know the section is allocated, but we don't know that it is
5721 // read-only. But we check for all the relocation types which the
5722 // glibc dynamic linker supports, so it seems appropriate to issue an
5723 // error even if the section is not read-only.
5725 template<bool big_endian
>
5727 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
5728 unsigned int r_type
)
5732 // These are the relocation types supported by glibc for ARM.
5733 case elfcpp::R_ARM_RELATIVE
:
5734 case elfcpp::R_ARM_COPY
:
5735 case elfcpp::R_ARM_GLOB_DAT
:
5736 case elfcpp::R_ARM_JUMP_SLOT
:
5737 case elfcpp::R_ARM_ABS32
:
5738 case elfcpp::R_ARM_ABS32_NOI
:
5739 case elfcpp::R_ARM_PC24
:
5740 // FIXME: The following 3 types are not supported by Android's dynamic
5742 case elfcpp::R_ARM_TLS_DTPMOD32
:
5743 case elfcpp::R_ARM_TLS_DTPOFF32
:
5744 case elfcpp::R_ARM_TLS_TPOFF32
:
5748 // This prevents us from issuing more than one error per reloc
5749 // section. But we can still wind up issuing more than one
5750 // error per object file.
5751 if (this->issued_non_pic_error_
)
5753 object
->error(_("requires unsupported dynamic reloc; "
5754 "recompile with -fPIC"));
5755 this->issued_non_pic_error_
= true;
5758 case elfcpp::R_ARM_NONE
:
5763 // Scan a relocation for a local symbol.
5764 // FIXME: This only handles a subset of relocation types used by Android
5765 // on ARM v5te devices.
5767 template<bool big_endian
>
5769 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
5772 Sized_relobj
<32, big_endian
>* object
,
5773 unsigned int data_shndx
,
5774 Output_section
* output_section
,
5775 const elfcpp::Rel
<32, big_endian
>& reloc
,
5776 unsigned int r_type
,
5777 const elfcpp::Sym
<32, big_endian
>&)
5779 r_type
= get_real_reloc_type(r_type
);
5782 case elfcpp::R_ARM_NONE
:
5785 case elfcpp::R_ARM_ABS32
:
5786 case elfcpp::R_ARM_ABS32_NOI
:
5787 // If building a shared library (or a position-independent
5788 // executable), we need to create a dynamic relocation for
5789 // this location. The relocation applied at link time will
5790 // apply the link-time value, so we flag the location with
5791 // an R_ARM_RELATIVE relocation so the dynamic loader can
5792 // relocate it easily.
5793 if (parameters
->options().output_is_position_independent())
5795 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5796 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5797 // If we are to add more other reloc types than R_ARM_ABS32,
5798 // we need to add check_non_pic(object, r_type) here.
5799 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
5800 output_section
, data_shndx
,
5801 reloc
.get_r_offset());
5805 case elfcpp::R_ARM_REL32
:
5806 case elfcpp::R_ARM_THM_CALL
:
5807 case elfcpp::R_ARM_CALL
:
5808 case elfcpp::R_ARM_PREL31
:
5809 case elfcpp::R_ARM_JUMP24
:
5810 case elfcpp::R_ARM_THM_JUMP24
:
5811 case elfcpp::R_ARM_THM_JUMP19
:
5812 case elfcpp::R_ARM_PLT32
:
5813 case elfcpp::R_ARM_THM_ABS5
:
5814 case elfcpp::R_ARM_ABS8
:
5815 case elfcpp::R_ARM_ABS12
:
5816 case elfcpp::R_ARM_ABS16
:
5817 case elfcpp::R_ARM_BASE_ABS
:
5818 case elfcpp::R_ARM_MOVW_ABS_NC
:
5819 case elfcpp::R_ARM_MOVT_ABS
:
5820 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5821 case elfcpp::R_ARM_THM_MOVT_ABS
:
5822 case elfcpp::R_ARM_MOVW_PREL_NC
:
5823 case elfcpp::R_ARM_MOVT_PREL
:
5824 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5825 case elfcpp::R_ARM_THM_MOVT_PREL
:
5826 case elfcpp::R_ARM_THM_JUMP6
:
5827 case elfcpp::R_ARM_THM_JUMP8
:
5828 case elfcpp::R_ARM_THM_JUMP11
:
5829 case elfcpp::R_ARM_V4BX
:
5832 case elfcpp::R_ARM_GOTOFF32
:
5833 // We need a GOT section:
5834 target
->got_section(symtab
, layout
);
5837 case elfcpp::R_ARM_BASE_PREL
:
5838 // FIXME: What about this?
5841 case elfcpp::R_ARM_GOT_BREL
:
5842 case elfcpp::R_ARM_GOT_PREL
:
5844 // The symbol requires a GOT entry.
5845 Output_data_got
<32, big_endian
>* got
=
5846 target
->got_section(symtab
, layout
);
5847 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5848 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
5850 // If we are generating a shared object, we need to add a
5851 // dynamic RELATIVE relocation for this symbol's GOT entry.
5852 if (parameters
->options().output_is_position_independent())
5854 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5855 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5856 rel_dyn
->add_local_relative(
5857 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
5858 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5864 case elfcpp::R_ARM_TARGET1
:
5865 // This should have been mapped to another type already.
5867 case elfcpp::R_ARM_COPY
:
5868 case elfcpp::R_ARM_GLOB_DAT
:
5869 case elfcpp::R_ARM_JUMP_SLOT
:
5870 case elfcpp::R_ARM_RELATIVE
:
5871 // These are relocations which should only be seen by the
5872 // dynamic linker, and should never be seen here.
5873 gold_error(_("%s: unexpected reloc %u in object file"),
5874 object
->name().c_str(), r_type
);
5878 unsupported_reloc_local(object
, r_type
);
5883 // Report an unsupported relocation against a global symbol.
5885 template<bool big_endian
>
5887 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
5888 Sized_relobj
<32, big_endian
>* object
,
5889 unsigned int r_type
,
5892 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5893 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5896 // Scan a relocation for a global symbol.
5897 // FIXME: This only handles a subset of relocation types used by Android
5898 // on ARM v5te devices.
5900 template<bool big_endian
>
5902 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
5905 Sized_relobj
<32, big_endian
>* object
,
5906 unsigned int data_shndx
,
5907 Output_section
* output_section
,
5908 const elfcpp::Rel
<32, big_endian
>& reloc
,
5909 unsigned int r_type
,
5912 r_type
= get_real_reloc_type(r_type
);
5915 case elfcpp::R_ARM_NONE
:
5918 case elfcpp::R_ARM_ABS32
:
5919 case elfcpp::R_ARM_ABS32_NOI
:
5921 // Make a dynamic relocation if necessary.
5922 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
5924 if (target
->may_need_copy_reloc(gsym
))
5926 target
->copy_reloc(symtab
, layout
, object
,
5927 data_shndx
, output_section
, gsym
, reloc
);
5929 else if (gsym
->can_use_relative_reloc(false))
5931 // If we are to add more other reloc types than R_ARM_ABS32,
5932 // we need to add check_non_pic(object, r_type) here.
5933 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5934 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
5935 output_section
, object
,
5936 data_shndx
, reloc
.get_r_offset());
5940 // If we are to add more other reloc types than R_ARM_ABS32,
5941 // we need to add check_non_pic(object, r_type) here.
5942 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5943 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5944 data_shndx
, reloc
.get_r_offset());
5950 case elfcpp::R_ARM_MOVW_ABS_NC
:
5951 case elfcpp::R_ARM_MOVT_ABS
:
5952 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5953 case elfcpp::R_ARM_THM_MOVT_ABS
:
5954 case elfcpp::R_ARM_MOVW_PREL_NC
:
5955 case elfcpp::R_ARM_MOVT_PREL
:
5956 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5957 case elfcpp::R_ARM_THM_MOVT_PREL
:
5958 case elfcpp::R_ARM_THM_JUMP6
:
5959 case elfcpp::R_ARM_THM_JUMP8
:
5960 case elfcpp::R_ARM_THM_JUMP11
:
5961 case elfcpp::R_ARM_V4BX
:
5964 case elfcpp::R_ARM_THM_ABS5
:
5965 case elfcpp::R_ARM_ABS8
:
5966 case elfcpp::R_ARM_ABS12
:
5967 case elfcpp::R_ARM_ABS16
:
5968 case elfcpp::R_ARM_BASE_ABS
:
5970 // No dynamic relocs of this kinds.
5971 // Report the error in case of PIC.
5972 int flags
= Symbol::NON_PIC_REF
;
5973 if (gsym
->type() == elfcpp::STT_FUNC
5974 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5975 flags
|= Symbol::FUNCTION_CALL
;
5976 if (gsym
->needs_dynamic_reloc(flags
))
5977 check_non_pic(object
, r_type
);
5981 case elfcpp::R_ARM_REL32
:
5982 case elfcpp::R_ARM_PREL31
:
5984 // Make a dynamic relocation if necessary.
5985 int flags
= Symbol::NON_PIC_REF
;
5986 if (gsym
->needs_dynamic_reloc(flags
))
5988 if (target
->may_need_copy_reloc(gsym
))
5990 target
->copy_reloc(symtab
, layout
, object
,
5991 data_shndx
, output_section
, gsym
, reloc
);
5995 check_non_pic(object
, r_type
);
5996 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5997 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5998 data_shndx
, reloc
.get_r_offset());
6004 case elfcpp::R_ARM_JUMP24
:
6005 case elfcpp::R_ARM_THM_JUMP24
:
6006 case elfcpp::R_ARM_THM_JUMP19
:
6007 case elfcpp::R_ARM_CALL
:
6008 case elfcpp::R_ARM_THM_CALL
:
6010 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
6011 target
->make_plt_entry(symtab
, layout
, gsym
);
6014 // Check to see if this is a function that would need a PLT
6015 // but does not get one because the function symbol is untyped.
6016 // This happens in assembly code missing a proper .type directive.
6017 if ((!gsym
->is_undefined() || parameters
->options().shared())
6018 && !parameters
->doing_static_link()
6019 && gsym
->type() == elfcpp::STT_NOTYPE
6020 && (gsym
->is_from_dynobj()
6021 || gsym
->is_undefined()
6022 || gsym
->is_preemptible()))
6023 gold_error(_("%s is not a function."),
6024 gsym
->demangled_name().c_str());
6028 case elfcpp::R_ARM_PLT32
:
6029 // If the symbol is fully resolved, this is just a relative
6030 // local reloc. Otherwise we need a PLT entry.
6031 if (gsym
->final_value_is_known())
6033 // If building a shared library, we can also skip the PLT entry
6034 // if the symbol is defined in the output file and is protected
6036 if (gsym
->is_defined()
6037 && !gsym
->is_from_dynobj()
6038 && !gsym
->is_preemptible())
6040 target
->make_plt_entry(symtab
, layout
, gsym
);
6043 case elfcpp::R_ARM_GOTOFF32
:
6044 // We need a GOT section.
6045 target
->got_section(symtab
, layout
);
6048 case elfcpp::R_ARM_BASE_PREL
:
6049 // FIXME: What about this?
6052 case elfcpp::R_ARM_GOT_BREL
:
6053 case elfcpp::R_ARM_GOT_PREL
:
6055 // The symbol requires a GOT entry.
6056 Output_data_got
<32, big_endian
>* got
=
6057 target
->got_section(symtab
, layout
);
6058 if (gsym
->final_value_is_known())
6059 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6062 // If this symbol is not fully resolved, we need to add a
6063 // GOT entry with a dynamic relocation.
6064 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6065 if (gsym
->is_from_dynobj()
6066 || gsym
->is_undefined()
6067 || gsym
->is_preemptible())
6068 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
6069 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
6072 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
6073 rel_dyn
->add_global_relative(
6074 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
6075 gsym
->got_offset(GOT_TYPE_STANDARD
));
6081 case elfcpp::R_ARM_TARGET1
:
6082 // This should have been mapped to another type already.
6084 case elfcpp::R_ARM_COPY
:
6085 case elfcpp::R_ARM_GLOB_DAT
:
6086 case elfcpp::R_ARM_JUMP_SLOT
:
6087 case elfcpp::R_ARM_RELATIVE
:
6088 // These are relocations which should only be seen by the
6089 // dynamic linker, and should never be seen here.
6090 gold_error(_("%s: unexpected reloc %u in object file"),
6091 object
->name().c_str(), r_type
);
6095 unsupported_reloc_global(object
, r_type
, gsym
);
6100 // Process relocations for gc.
6102 template<bool big_endian
>
6104 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
6106 Sized_relobj
<32, big_endian
>* object
,
6107 unsigned int data_shndx
,
6109 const unsigned char* prelocs
,
6111 Output_section
* output_section
,
6112 bool needs_special_offset_handling
,
6113 size_t local_symbol_count
,
6114 const unsigned char* plocal_symbols
)
6116 typedef Target_arm
<big_endian
> Arm
;
6117 typedef typename Target_arm
<big_endian
>::Scan Scan
;
6119 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
6128 needs_special_offset_handling
,
6133 // Scan relocations for a section.
6135 template<bool big_endian
>
6137 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
6139 Sized_relobj
<32, big_endian
>* object
,
6140 unsigned int data_shndx
,
6141 unsigned int sh_type
,
6142 const unsigned char* prelocs
,
6144 Output_section
* output_section
,
6145 bool needs_special_offset_handling
,
6146 size_t local_symbol_count
,
6147 const unsigned char* plocal_symbols
)
6149 typedef typename Target_arm
<big_endian
>::Scan Scan
;
6150 if (sh_type
== elfcpp::SHT_RELA
)
6152 gold_error(_("%s: unsupported RELA reloc section"),
6153 object
->name().c_str());
6157 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
6166 needs_special_offset_handling
,
6171 // Finalize the sections.
6173 template<bool big_endian
>
6175 Target_arm
<big_endian
>::do_finalize_sections(
6177 const Input_objects
* input_objects
,
6178 Symbol_table
* symtab
)
6180 // Merge processor-specific flags.
6181 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
6182 p
!= input_objects
->relobj_end();
6185 Arm_relobj
<big_endian
>* arm_relobj
=
6186 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
6187 this->merge_processor_specific_flags(
6189 arm_relobj
->processor_specific_flags());
6190 this->merge_object_attributes(arm_relobj
->name().c_str(),
6191 arm_relobj
->attributes_section_data());
6195 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
6196 p
!= input_objects
->dynobj_end();
6199 Arm_dynobj
<big_endian
>* arm_dynobj
=
6200 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
6201 this->merge_processor_specific_flags(
6203 arm_dynobj
->processor_specific_flags());
6204 this->merge_object_attributes(arm_dynobj
->name().c_str(),
6205 arm_dynobj
->attributes_section_data());
6209 const Object_attribute
* cpu_arch_attr
=
6210 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
6211 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
6212 this->set_may_use_blx(true);
6214 // Check if we need to use Cortex-A8 workaround.
6215 if (parameters
->options().user_set_fix_cortex_a8())
6216 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
6219 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
6220 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
6222 const Object_attribute
* cpu_arch_profile_attr
=
6223 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
6224 this->fix_cortex_a8_
=
6225 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
6226 && (cpu_arch_profile_attr
->int_value() == 'A'
6227 || cpu_arch_profile_attr
->int_value() == 0));
6230 // Check if we can use V4BX interworking.
6231 // The V4BX interworking stub contains BX instruction,
6232 // which is not specified for some profiles.
6233 if (this->fix_v4bx() == 2 && !this->may_use_blx())
6234 gold_error(_("unable to provide V4BX reloc interworking fix up; "
6235 "the target profile does not support BX instruction"));
6237 // Fill in some more dynamic tags.
6238 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6240 : this->plt_
->rel_plt());
6241 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
6242 this->rel_dyn_
, true);
6244 // Emit any relocs we saved in an attempt to avoid generating COPY
6246 if (this->copy_relocs_
.any_saved_relocs())
6247 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
6249 // Handle the .ARM.exidx section.
6250 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
6251 if (exidx_section
!= NULL
6252 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
6253 && !parameters
->options().relocatable())
6255 // Create __exidx_start and __exdix_end symbols.
6256 symtab
->define_in_output_data("__exidx_start", NULL
,
6257 Symbol_table::PREDEFINED
,
6258 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
6259 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
6261 symtab
->define_in_output_data("__exidx_end", NULL
,
6262 Symbol_table::PREDEFINED
,
6263 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
6264 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
6267 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
6268 // the .ARM.exidx section.
6269 if (!layout
->script_options()->saw_phdrs_clause())
6271 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
6273 Output_segment
* exidx_segment
=
6274 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
6275 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
6280 // Create an .ARM.attributes section if there is not one already.
6281 Output_attributes_section_data
* attributes_section
=
6282 new Output_attributes_section_data(*this->attributes_section_data_
);
6283 layout
->add_output_section_data(".ARM.attributes",
6284 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
6285 attributes_section
, false, false, false,
6289 // Return whether a direct absolute static relocation needs to be applied.
6290 // In cases where Scan::local() or Scan::global() has created
6291 // a dynamic relocation other than R_ARM_RELATIVE, the addend
6292 // of the relocation is carried in the data, and we must not
6293 // apply the static relocation.
6295 template<bool big_endian
>
6297 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
6298 const Sized_symbol
<32>* gsym
,
6301 Output_section
* output_section
)
6303 // If the output section is not allocated, then we didn't call
6304 // scan_relocs, we didn't create a dynamic reloc, and we must apply
6306 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
6309 // For local symbols, we will have created a non-RELATIVE dynamic
6310 // relocation only if (a) the output is position independent,
6311 // (b) the relocation is absolute (not pc- or segment-relative), and
6312 // (c) the relocation is not 32 bits wide.
6314 return !(parameters
->options().output_is_position_independent()
6315 && (ref_flags
& Symbol::ABSOLUTE_REF
)
6318 // For global symbols, we use the same helper routines used in the
6319 // scan pass. If we did not create a dynamic relocation, or if we
6320 // created a RELATIVE dynamic relocation, we should apply the static
6322 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
6323 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
6324 && gsym
->can_use_relative_reloc(ref_flags
6325 & Symbol::FUNCTION_CALL
);
6326 return !has_dyn
|| is_rel
;
6329 // Perform a relocation.
6331 template<bool big_endian
>
6333 Target_arm
<big_endian
>::Relocate::relocate(
6334 const Relocate_info
<32, big_endian
>* relinfo
,
6336 Output_section
*output_section
,
6338 const elfcpp::Rel
<32, big_endian
>& rel
,
6339 unsigned int r_type
,
6340 const Sized_symbol
<32>* gsym
,
6341 const Symbol_value
<32>* psymval
,
6342 unsigned char* view
,
6343 Arm_address address
,
6344 section_size_type
/* view_size */ )
6346 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
6348 r_type
= get_real_reloc_type(r_type
);
6350 const Arm_relobj
<big_endian
>* object
=
6351 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6353 // If the final branch target of a relocation is THUMB instruction, this
6354 // is 1. Otherwise it is 0.
6355 Arm_address thumb_bit
= 0;
6356 Symbol_value
<32> symval
;
6357 bool is_weakly_undefined_without_plt
= false;
6358 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
6362 // This is a global symbol. Determine if we use PLT and if the
6363 // final target is THUMB.
6364 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
6366 // This uses a PLT, change the symbol value.
6367 symval
.set_output_value(target
->plt_section()->address()
6368 + gsym
->plt_offset());
6371 else if (gsym
->is_weak_undefined())
6373 // This is a weakly undefined symbol and we do not use PLT
6374 // for this relocation. A branch targeting this symbol will
6375 // be converted into an NOP.
6376 is_weakly_undefined_without_plt
= true;
6380 // Set thumb bit if symbol:
6381 // -Has type STT_ARM_TFUNC or
6382 // -Has type STT_FUNC, is defined and with LSB in value set.
6384 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6385 || (gsym
->type() == elfcpp::STT_FUNC
6386 && !gsym
->is_undefined()
6387 && ((psymval
->value(object
, 0) & 1) != 0)))
6394 // This is a local symbol. Determine if the final target is THUMB.
6395 // We saved this information when all the local symbols were read.
6396 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
6397 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6398 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
6403 // This is a fake relocation synthesized for a stub. It does not have
6404 // a real symbol. We just look at the LSB of the symbol value to
6405 // determine if the target is THUMB or not.
6406 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
6409 // Strip LSB if this points to a THUMB target.
6411 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6412 && ((psymval
->value(object
, 0) & 1) != 0))
6414 Arm_address stripped_value
=
6415 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
6416 symval
.set_output_value(stripped_value
);
6420 // Get the GOT offset if needed.
6421 // The GOT pointer points to the end of the GOT section.
6422 // We need to subtract the size of the GOT section to get
6423 // the actual offset to use in the relocation.
6424 bool have_got_offset
= false;
6425 unsigned int got_offset
= 0;
6428 case elfcpp::R_ARM_GOT_BREL
:
6429 case elfcpp::R_ARM_GOT_PREL
:
6432 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6433 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
6434 - target
->got_size());
6438 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6439 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6440 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
6441 - target
->got_size());
6443 have_got_offset
= true;
6450 // To look up relocation stubs, we need to pass the symbol table index of
6452 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6454 typename
Arm_relocate_functions::Status reloc_status
=
6455 Arm_relocate_functions::STATUS_OKAY
;
6458 case elfcpp::R_ARM_NONE
:
6461 case elfcpp::R_ARM_ABS8
:
6462 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6464 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
6467 case elfcpp::R_ARM_ABS12
:
6468 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6470 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
6473 case elfcpp::R_ARM_ABS16
:
6474 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6476 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
6479 case elfcpp::R_ARM_ABS32
:
6480 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6482 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6486 case elfcpp::R_ARM_ABS32_NOI
:
6487 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6489 // No thumb bit for this relocation: (S + A)
6490 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6494 case elfcpp::R_ARM_MOVW_ABS_NC
:
6495 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6497 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
6501 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6502 "a shared object; recompile with -fPIC"));
6505 case elfcpp::R_ARM_MOVT_ABS
:
6506 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6508 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
6510 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6511 "a shared object; recompile with -fPIC"));
6514 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6515 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6517 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
6521 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6522 "making a shared object; recompile with -fPIC"));
6525 case elfcpp::R_ARM_THM_MOVT_ABS
:
6526 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6528 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
6531 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6532 "making a shared object; recompile with -fPIC"));
6535 case elfcpp::R_ARM_MOVW_PREL_NC
:
6536 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
6541 case elfcpp::R_ARM_MOVT_PREL
:
6542 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
6546 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6547 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
6552 case elfcpp::R_ARM_THM_MOVT_PREL
:
6553 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
6557 case elfcpp::R_ARM_REL32
:
6558 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6559 address
, thumb_bit
);
6562 case elfcpp::R_ARM_THM_ABS5
:
6563 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6565 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
6568 case elfcpp::R_ARM_THM_CALL
:
6570 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
6571 psymval
, address
, thumb_bit
,
6572 is_weakly_undefined_without_plt
);
6575 case elfcpp::R_ARM_XPC25
:
6577 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
6578 psymval
, address
, thumb_bit
,
6579 is_weakly_undefined_without_plt
);
6582 case elfcpp::R_ARM_THM_XPC22
:
6584 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
6585 psymval
, address
, thumb_bit
,
6586 is_weakly_undefined_without_plt
);
6589 case elfcpp::R_ARM_GOTOFF32
:
6591 Arm_address got_origin
;
6592 got_origin
= target
->got_plt_section()->address();
6593 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6594 got_origin
, thumb_bit
);
6598 case elfcpp::R_ARM_BASE_PREL
:
6601 // Get the addressing origin of the output segment defining the
6602 // symbol gsym (AAELF 4.6.1.2 Relocation types)
6603 gold_assert(gsym
!= NULL
);
6604 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6605 origin
= gsym
->output_segment()->vaddr();
6606 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6607 origin
= gsym
->output_data()->address();
6610 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6611 _("cannot find origin of R_ARM_BASE_PREL"));
6614 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
6618 case elfcpp::R_ARM_BASE_ABS
:
6620 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6625 // Get the addressing origin of the output segment defining
6626 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
6628 // R_ARM_BASE_ABS with the NULL symbol will give the
6629 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
6630 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6631 origin
= target
->got_plt_section()->address();
6632 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6633 origin
= gsym
->output_segment()->vaddr();
6634 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6635 origin
= gsym
->output_data()->address();
6638 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6639 _("cannot find origin of R_ARM_BASE_ABS"));
6643 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
6647 case elfcpp::R_ARM_GOT_BREL
:
6648 gold_assert(have_got_offset
);
6649 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
6652 case elfcpp::R_ARM_GOT_PREL
:
6653 gold_assert(have_got_offset
);
6654 // Get the address origin for GOT PLT, which is allocated right
6655 // after the GOT section, to calculate an absolute address of
6656 // the symbol GOT entry (got_origin + got_offset).
6657 Arm_address got_origin
;
6658 got_origin
= target
->got_plt_section()->address();
6659 reloc_status
= Arm_relocate_functions::got_prel(view
,
6660 got_origin
+ got_offset
,
6664 case elfcpp::R_ARM_PLT32
:
6665 gold_assert(gsym
== NULL
6666 || gsym
->has_plt_offset()
6667 || gsym
->final_value_is_known()
6668 || (gsym
->is_defined()
6669 && !gsym
->is_from_dynobj()
6670 && !gsym
->is_preemptible()));
6672 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
6673 psymval
, address
, thumb_bit
,
6674 is_weakly_undefined_without_plt
);
6677 case elfcpp::R_ARM_CALL
:
6679 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
6680 psymval
, address
, thumb_bit
,
6681 is_weakly_undefined_without_plt
);
6684 case elfcpp::R_ARM_JUMP24
:
6686 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
6687 psymval
, address
, thumb_bit
,
6688 is_weakly_undefined_without_plt
);
6691 case elfcpp::R_ARM_THM_JUMP24
:
6693 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
6694 psymval
, address
, thumb_bit
,
6695 is_weakly_undefined_without_plt
);
6698 case elfcpp::R_ARM_THM_JUMP19
:
6700 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
6704 case elfcpp::R_ARM_THM_JUMP6
:
6706 Arm_relocate_functions::thm_jump6(view
, object
, psymval
, address
);
6709 case elfcpp::R_ARM_THM_JUMP8
:
6711 Arm_relocate_functions::thm_jump8(view
, object
, psymval
, address
);
6714 case elfcpp::R_ARM_THM_JUMP11
:
6716 Arm_relocate_functions::thm_jump11(view
, object
, psymval
, address
);
6719 case elfcpp::R_ARM_PREL31
:
6720 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
6721 address
, thumb_bit
);
6724 case elfcpp::R_ARM_V4BX
:
6725 if (target
->fix_v4bx() > 0)
6727 Arm_relocate_functions::v4bx(relinfo
, view
, object
, address
,
6728 (target
->fix_v4bx() == 2));
6731 case elfcpp::R_ARM_TARGET1
:
6732 // This should have been mapped to another type already.
6734 case elfcpp::R_ARM_COPY
:
6735 case elfcpp::R_ARM_GLOB_DAT
:
6736 case elfcpp::R_ARM_JUMP_SLOT
:
6737 case elfcpp::R_ARM_RELATIVE
:
6738 // These are relocations which should only be seen by the
6739 // dynamic linker, and should never be seen here.
6740 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6741 _("unexpected reloc %u in object file"),
6746 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6747 _("unsupported reloc %u"),
6752 // Report any errors.
6753 switch (reloc_status
)
6755 case Arm_relocate_functions::STATUS_OKAY
:
6757 case Arm_relocate_functions::STATUS_OVERFLOW
:
6758 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6759 _("relocation overflow in relocation %u"),
6762 case Arm_relocate_functions::STATUS_BAD_RELOC
:
6763 gold_error_at_location(
6767 _("unexpected opcode while processing relocation %u"),
6777 // Relocate section data.
6779 template<bool big_endian
>
6781 Target_arm
<big_endian
>::relocate_section(
6782 const Relocate_info
<32, big_endian
>* relinfo
,
6783 unsigned int sh_type
,
6784 const unsigned char* prelocs
,
6786 Output_section
* output_section
,
6787 bool needs_special_offset_handling
,
6788 unsigned char* view
,
6789 Arm_address address
,
6790 section_size_type view_size
,
6791 const Reloc_symbol_changes
* reloc_symbol_changes
)
6793 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
6794 gold_assert(sh_type
== elfcpp::SHT_REL
);
6796 Arm_input_section
<big_endian
>* arm_input_section
=
6797 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
6799 // This is an ARM input section and the view covers the whole output
6801 if (arm_input_section
!= NULL
)
6803 gold_assert(needs_special_offset_handling
);
6804 Arm_address section_address
= arm_input_section
->address();
6805 section_size_type section_size
= arm_input_section
->data_size();
6807 gold_assert((arm_input_section
->address() >= address
)
6808 && ((arm_input_section
->address()
6809 + arm_input_section
->data_size())
6810 <= (address
+ view_size
)));
6812 off_t offset
= section_address
- address
;
6815 view_size
= section_size
;
6818 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
6825 needs_special_offset_handling
,
6829 reloc_symbol_changes
);
6832 // Return the size of a relocation while scanning during a relocatable
6835 template<bool big_endian
>
6837 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
6838 unsigned int r_type
,
6841 r_type
= get_real_reloc_type(r_type
);
6844 case elfcpp::R_ARM_NONE
:
6847 case elfcpp::R_ARM_ABS8
:
6850 case elfcpp::R_ARM_ABS16
:
6851 case elfcpp::R_ARM_THM_ABS5
:
6852 case elfcpp::R_ARM_THM_JUMP6
:
6853 case elfcpp::R_ARM_THM_JUMP8
:
6854 case elfcpp::R_ARM_THM_JUMP11
:
6857 case elfcpp::R_ARM_ABS32
:
6858 case elfcpp::R_ARM_ABS32_NOI
:
6859 case elfcpp::R_ARM_ABS12
:
6860 case elfcpp::R_ARM_BASE_ABS
:
6861 case elfcpp::R_ARM_REL32
:
6862 case elfcpp::R_ARM_THM_CALL
:
6863 case elfcpp::R_ARM_GOTOFF32
:
6864 case elfcpp::R_ARM_BASE_PREL
:
6865 case elfcpp::R_ARM_GOT_BREL
:
6866 case elfcpp::R_ARM_GOT_PREL
:
6867 case elfcpp::R_ARM_PLT32
:
6868 case elfcpp::R_ARM_CALL
:
6869 case elfcpp::R_ARM_JUMP24
:
6870 case elfcpp::R_ARM_PREL31
:
6871 case elfcpp::R_ARM_MOVW_ABS_NC
:
6872 case elfcpp::R_ARM_MOVT_ABS
:
6873 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6874 case elfcpp::R_ARM_THM_MOVT_ABS
:
6875 case elfcpp::R_ARM_MOVW_PREL_NC
:
6876 case elfcpp::R_ARM_MOVT_PREL
:
6877 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6878 case elfcpp::R_ARM_THM_MOVT_PREL
:
6879 case elfcpp::R_ARM_V4BX
:
6882 case elfcpp::R_ARM_TARGET1
:
6883 // This should have been mapped to another type already.
6885 case elfcpp::R_ARM_COPY
:
6886 case elfcpp::R_ARM_GLOB_DAT
:
6887 case elfcpp::R_ARM_JUMP_SLOT
:
6888 case elfcpp::R_ARM_RELATIVE
:
6889 // These are relocations which should only be seen by the
6890 // dynamic linker, and should never be seen here.
6891 gold_error(_("%s: unexpected reloc %u in object file"),
6892 object
->name().c_str(), r_type
);
6896 object
->error(_("unsupported reloc %u in object file"), r_type
);
6901 // Scan the relocs during a relocatable link.
6903 template<bool big_endian
>
6905 Target_arm
<big_endian
>::scan_relocatable_relocs(
6906 Symbol_table
* symtab
,
6908 Sized_relobj
<32, big_endian
>* object
,
6909 unsigned int data_shndx
,
6910 unsigned int sh_type
,
6911 const unsigned char* prelocs
,
6913 Output_section
* output_section
,
6914 bool needs_special_offset_handling
,
6915 size_t local_symbol_count
,
6916 const unsigned char* plocal_symbols
,
6917 Relocatable_relocs
* rr
)
6919 gold_assert(sh_type
== elfcpp::SHT_REL
);
6921 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
6922 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
6924 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
6925 Scan_relocatable_relocs
>(
6933 needs_special_offset_handling
,
6939 // Relocate a section during a relocatable link.
6941 template<bool big_endian
>
6943 Target_arm
<big_endian
>::relocate_for_relocatable(
6944 const Relocate_info
<32, big_endian
>* relinfo
,
6945 unsigned int sh_type
,
6946 const unsigned char* prelocs
,
6948 Output_section
* output_section
,
6949 off_t offset_in_output_section
,
6950 const Relocatable_relocs
* rr
,
6951 unsigned char* view
,
6952 Arm_address view_address
,
6953 section_size_type view_size
,
6954 unsigned char* reloc_view
,
6955 section_size_type reloc_view_size
)
6957 gold_assert(sh_type
== elfcpp::SHT_REL
);
6959 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
6964 offset_in_output_section
,
6973 // Return the value to use for a dynamic symbol which requires special
6974 // treatment. This is how we support equality comparisons of function
6975 // pointers across shared library boundaries, as described in the
6976 // processor specific ABI supplement.
6978 template<bool big_endian
>
6980 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6982 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6983 return this->plt_section()->address() + gsym
->plt_offset();
6986 // Map platform-specific relocs to real relocs
6988 template<bool big_endian
>
6990 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
6994 case elfcpp::R_ARM_TARGET1
:
6995 // This is either R_ARM_ABS32 or R_ARM_REL32;
6996 return elfcpp::R_ARM_ABS32
;
6998 case elfcpp::R_ARM_TARGET2
:
6999 // This can be any reloc type but ususally is R_ARM_GOT_PREL
7000 return elfcpp::R_ARM_GOT_PREL
;
7007 // Whether if two EABI versions V1 and V2 are compatible.
7009 template<bool big_endian
>
7011 Target_arm
<big_endian
>::are_eabi_versions_compatible(
7012 elfcpp::Elf_Word v1
,
7013 elfcpp::Elf_Word v2
)
7015 // v4 and v5 are the same spec before and after it was released,
7016 // so allow mixing them.
7017 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
7018 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
7024 // Combine FLAGS from an input object called NAME and the processor-specific
7025 // flags in the ELF header of the output. Much of this is adapted from the
7026 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
7027 // in bfd/elf32-arm.c.
7029 template<bool big_endian
>
7031 Target_arm
<big_endian
>::merge_processor_specific_flags(
7032 const std::string
& name
,
7033 elfcpp::Elf_Word flags
)
7035 if (this->are_processor_specific_flags_set())
7037 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
7039 // Nothing to merge if flags equal to those in output.
7040 if (flags
== out_flags
)
7043 // Complain about various flag mismatches.
7044 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
7045 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
7046 if (!this->are_eabi_versions_compatible(version1
, version2
))
7047 gold_error(_("Source object %s has EABI version %d but output has "
7048 "EABI version %d."),
7050 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
7051 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
7055 // If the input is the default architecture and had the default
7056 // flags then do not bother setting the flags for the output
7057 // architecture, instead allow future merges to do this. If no
7058 // future merges ever set these flags then they will retain their
7059 // uninitialised values, which surprise surprise, correspond
7060 // to the default values.
7064 // This is the first time, just copy the flags.
7065 // We only copy the EABI version for now.
7066 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
7070 // Adjust ELF file header.
7071 template<bool big_endian
>
7073 Target_arm
<big_endian
>::do_adjust_elf_header(
7074 unsigned char* view
,
7077 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
7079 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
7080 unsigned char e_ident
[elfcpp::EI_NIDENT
];
7081 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
7083 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
7084 == elfcpp::EF_ARM_EABI_UNKNOWN
)
7085 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
7087 e_ident
[elfcpp::EI_OSABI
] = 0;
7088 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
7090 // FIXME: Do EF_ARM_BE8 adjustment.
7092 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
7093 oehdr
.put_e_ident(e_ident
);
7096 // do_make_elf_object to override the same function in the base class.
7097 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
7098 // to store ARM specific information. Hence we need to have our own
7099 // ELF object creation.
7101 template<bool big_endian
>
7103 Target_arm
<big_endian
>::do_make_elf_object(
7104 const std::string
& name
,
7105 Input_file
* input_file
,
7106 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
7108 int et
= ehdr
.get_e_type();
7109 if (et
== elfcpp::ET_REL
)
7111 Arm_relobj
<big_endian
>* obj
=
7112 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
7116 else if (et
== elfcpp::ET_DYN
)
7118 Sized_dynobj
<32, big_endian
>* obj
=
7119 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
7125 gold_error(_("%s: unsupported ELF file type %d"),
7131 // Read the architecture from the Tag_also_compatible_with attribute, if any.
7132 // Returns -1 if no architecture could be read.
7133 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
7135 template<bool big_endian
>
7137 Target_arm
<big_endian
>::get_secondary_compatible_arch(
7138 const Attributes_section_data
* pasd
)
7140 const Object_attribute
*known_attributes
=
7141 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
7143 // Note: the tag and its argument below are uleb128 values, though
7144 // currently-defined values fit in one byte for each.
7145 const std::string
& sv
=
7146 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
7148 && sv
.data()[0] == elfcpp::Tag_CPU_arch
7149 && (sv
.data()[1] & 128) != 128)
7150 return sv
.data()[1];
7152 // This tag is "safely ignorable", so don't complain if it looks funny.
7156 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
7157 // The tag is removed if ARCH is -1.
7158 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
7160 template<bool big_endian
>
7162 Target_arm
<big_endian
>::set_secondary_compatible_arch(
7163 Attributes_section_data
* pasd
,
7166 Object_attribute
*known_attributes
=
7167 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
7171 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
7175 // Note: the tag and its argument below are uleb128 values, though
7176 // currently-defined values fit in one byte for each.
7178 sv
[0] = elfcpp::Tag_CPU_arch
;
7179 gold_assert(arch
!= 0);
7183 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
7186 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
7188 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
7190 template<bool big_endian
>
7192 Target_arm
<big_endian
>::tag_cpu_arch_combine(
7195 int* secondary_compat_out
,
7197 int secondary_compat
)
7199 #define T(X) elfcpp::TAG_CPU_ARCH_##X
7200 static const int v6t2
[] =
7212 static const int v6k
[] =
7225 static const int v7
[] =
7239 static const int v6_m
[] =
7254 static const int v6s_m
[] =
7270 static const int v7e_m
[] =
7287 static const int v4t_plus_v6_m
[] =
7303 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
7305 static const int *comb
[] =
7313 // Pseudo-architecture.
7317 // Check we've not got a higher architecture than we know about.
7319 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
7321 gold_error(_("%s: unknown CPU architecture"), name
);
7325 // Override old tag if we have a Tag_also_compatible_with on the output.
7327 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
7328 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
7329 oldtag
= T(V4T_PLUS_V6_M
);
7331 // And override the new tag if we have a Tag_also_compatible_with on the
7334 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
7335 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
7336 newtag
= T(V4T_PLUS_V6_M
);
7338 // Architectures before V6KZ add features monotonically.
7339 int tagh
= std::max(oldtag
, newtag
);
7340 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
7343 int tagl
= std::min(oldtag
, newtag
);
7344 int result
= comb
[tagh
- T(V6T2
)][tagl
];
7346 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
7347 // as the canonical version.
7348 if (result
== T(V4T_PLUS_V6_M
))
7351 *secondary_compat_out
= T(V6_M
);
7354 *secondary_compat_out
= -1;
7358 gold_error(_("%s: conflicting CPU architectures %d/%d"),
7359 name
, oldtag
, newtag
);
7367 // Helper to print AEABI enum tag value.
7369 template<bool big_endian
>
7371 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
7373 static const char *aeabi_enum_names
[] =
7374 { "", "variable-size", "32-bit", "" };
7375 const size_t aeabi_enum_names_size
=
7376 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
7378 if (value
< aeabi_enum_names_size
)
7379 return std::string(aeabi_enum_names
[value
]);
7383 sprintf(buffer
, "<unknown value %u>", value
);
7384 return std::string(buffer
);
7388 // Return the string value to store in TAG_CPU_name.
7390 template<bool big_endian
>
7392 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
7394 static const char *name_table
[] = {
7395 // These aren't real CPU names, but we can't guess
7396 // that from the architecture version alone.
7412 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
7414 if (value
< name_table_size
)
7415 return std::string(name_table
[value
]);
7419 sprintf(buffer
, "<unknown CPU value %u>", value
);
7420 return std::string(buffer
);
7424 // Merge object attributes from input file called NAME with those of the
7425 // output. The input object attributes are in the object pointed by PASD.
7427 template<bool big_endian
>
7429 Target_arm
<big_endian
>::merge_object_attributes(
7431 const Attributes_section_data
* pasd
)
7433 // Return if there is no attributes section data.
7437 // If output has no object attributes, just copy.
7438 if (this->attributes_section_data_
== NULL
)
7440 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
7444 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
7445 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
7446 Object_attribute
* out_attr
=
7447 this->attributes_section_data_
->known_attributes(vendor
);
7449 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7450 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
7451 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
7453 // Ignore mismatches if the object doesn't use floating point. */
7454 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
7455 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
7456 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
7457 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
7458 gold_error(_("%s uses VFP register arguments, output does not"),
7462 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
7464 // Merge this attribute with existing attributes.
7467 case elfcpp::Tag_CPU_raw_name
:
7468 case elfcpp::Tag_CPU_name
:
7469 // These are merged after Tag_CPU_arch.
7472 case elfcpp::Tag_ABI_optimization_goals
:
7473 case elfcpp::Tag_ABI_FP_optimization_goals
:
7474 // Use the first value seen.
7477 case elfcpp::Tag_CPU_arch
:
7479 unsigned int saved_out_attr
= out_attr
->int_value();
7480 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7481 int secondary_compat
=
7482 this->get_secondary_compatible_arch(pasd
);
7483 int secondary_compat_out
=
7484 this->get_secondary_compatible_arch(
7485 this->attributes_section_data_
);
7486 out_attr
[i
].set_int_value(
7487 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
7488 &secondary_compat_out
,
7489 in_attr
[i
].int_value(),
7491 this->set_secondary_compatible_arch(this->attributes_section_data_
,
7492 secondary_compat_out
);
7494 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7495 if (out_attr
[i
].int_value() == saved_out_attr
)
7496 ; // Leave the names alone.
7497 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
7499 // The output architecture has been changed to match the
7500 // input architecture. Use the input names.
7501 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
7502 in_attr
[elfcpp::Tag_CPU_name
].string_value());
7503 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
7504 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
7508 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
7509 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
7512 // If we still don't have a value for Tag_CPU_name,
7513 // make one up now. Tag_CPU_raw_name remains blank.
7514 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
7516 const std::string cpu_name
=
7517 this->tag_cpu_name_value(out_attr
[i
].int_value());
7518 // FIXME: If we see an unknown CPU, this will be set
7519 // to "<unknown CPU n>", where n is the attribute value.
7520 // This is different from BFD, which leaves the name alone.
7521 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
7526 case elfcpp::Tag_ARM_ISA_use
:
7527 case elfcpp::Tag_THUMB_ISA_use
:
7528 case elfcpp::Tag_WMMX_arch
:
7529 case elfcpp::Tag_Advanced_SIMD_arch
:
7530 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7531 case elfcpp::Tag_ABI_FP_rounding
:
7532 case elfcpp::Tag_ABI_FP_exceptions
:
7533 case elfcpp::Tag_ABI_FP_user_exceptions
:
7534 case elfcpp::Tag_ABI_FP_number_model
:
7535 case elfcpp::Tag_VFP_HP_extension
:
7536 case elfcpp::Tag_CPU_unaligned_access
:
7537 case elfcpp::Tag_T2EE_use
:
7538 case elfcpp::Tag_Virtualization_use
:
7539 case elfcpp::Tag_MPextension_use
:
7540 // Use the largest value specified.
7541 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7542 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7545 case elfcpp::Tag_ABI_align8_preserved
:
7546 case elfcpp::Tag_ABI_PCS_RO_data
:
7547 // Use the smallest value specified.
7548 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7549 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7552 case elfcpp::Tag_ABI_align8_needed
:
7553 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
7554 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
7555 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
7558 // This error message should be enabled once all non-conformant
7559 // binaries in the toolchain have had the attributes set
7561 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7565 case elfcpp::Tag_ABI_FP_denormal
:
7566 case elfcpp::Tag_ABI_PCS_GOT_use
:
7568 // These tags have 0 = don't care, 1 = strong requirement,
7569 // 2 = weak requirement.
7570 static const int order_021
[3] = {0, 2, 1};
7572 // Use the "greatest" from the sequence 0, 2, 1, or the largest
7573 // value if greater than 2 (for future-proofing).
7574 if ((in_attr
[i
].int_value() > 2
7575 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7576 || (in_attr
[i
].int_value() <= 2
7577 && out_attr
[i
].int_value() <= 2
7578 && (order_021
[in_attr
[i
].int_value()]
7579 > order_021
[out_attr
[i
].int_value()])))
7580 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7584 case elfcpp::Tag_CPU_arch_profile
:
7585 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
7587 // 0 will merge with anything.
7588 // 'A' and 'S' merge to 'A'.
7589 // 'R' and 'S' merge to 'R'.
7590 // 'M' and 'A|R|S' is an error.
7591 if (out_attr
[i
].int_value() == 0
7592 || (out_attr
[i
].int_value() == 'S'
7593 && (in_attr
[i
].int_value() == 'A'
7594 || in_attr
[i
].int_value() == 'R')))
7595 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7596 else if (in_attr
[i
].int_value() == 0
7597 || (in_attr
[i
].int_value() == 'S'
7598 && (out_attr
[i
].int_value() == 'A'
7599 || out_attr
[i
].int_value() == 'R')))
7604 (_("conflicting architecture profiles %c/%c"),
7605 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
7606 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
7610 case elfcpp::Tag_VFP_arch
:
7627 // Values greater than 6 aren't defined, so just pick the
7629 if (in_attr
[i
].int_value() > 6
7630 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7632 *out_attr
= *in_attr
;
7635 // The output uses the superset of input features
7636 // (ISA version) and registers.
7637 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
7638 vfp_versions
[out_attr
[i
].int_value()].ver
);
7639 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
7640 vfp_versions
[out_attr
[i
].int_value()].regs
);
7641 // This assumes all possible supersets are also a valid
7644 for (newval
= 6; newval
> 0; newval
--)
7646 if (regs
== vfp_versions
[newval
].regs
7647 && ver
== vfp_versions
[newval
].ver
)
7650 out_attr
[i
].set_int_value(newval
);
7653 case elfcpp::Tag_PCS_config
:
7654 if (out_attr
[i
].int_value() == 0)
7655 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7656 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7658 // It's sometimes ok to mix different configs, so this is only
7660 gold_warning(_("%s: conflicting platform configuration"), name
);
7663 case elfcpp::Tag_ABI_PCS_R9_use
:
7664 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
7665 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
7666 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
7668 gold_error(_("%s: conflicting use of R9"), name
);
7670 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
7671 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7673 case elfcpp::Tag_ABI_PCS_RW_data
:
7674 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7675 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7676 != elfcpp::AEABI_R9_SB
)
7677 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7678 != elfcpp::AEABI_R9_unused
))
7680 gold_error(_("%s: SB relative addressing conflicts with use "
7684 // Use the smallest value specified.
7685 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7686 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7688 case elfcpp::Tag_ABI_PCS_wchar_t
:
7689 // FIXME: Make it possible to turn off this warning.
7690 if (out_attr
[i
].int_value()
7691 && in_attr
[i
].int_value()
7692 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7694 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7695 "use %u-byte wchar_t; use of wchar_t values "
7696 "across objects may fail"),
7697 name
, in_attr
[i
].int_value(),
7698 out_attr
[i
].int_value());
7700 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
7701 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7703 case elfcpp::Tag_ABI_enum_size
:
7704 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
7706 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
7707 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
7709 // The existing object is compatible with anything.
7710 // Use whatever requirements the new object has.
7711 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7713 // FIXME: Make it possible to turn off this warning.
7714 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
7715 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7717 unsigned int in_value
= in_attr
[i
].int_value();
7718 unsigned int out_value
= out_attr
[i
].int_value();
7719 gold_warning(_("%s uses %s enums yet the output is to use "
7720 "%s enums; use of enum values across objects "
7723 this->aeabi_enum_name(in_value
).c_str(),
7724 this->aeabi_enum_name(out_value
).c_str());
7728 case elfcpp::Tag_ABI_VFP_args
:
7731 case elfcpp::Tag_ABI_WMMX_args
:
7732 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7734 gold_error(_("%s uses iWMMXt register arguments, output does "
7739 case Object_attribute::Tag_compatibility
:
7740 // Merged in target-independent code.
7742 case elfcpp::Tag_ABI_HardFP_use
:
7743 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7744 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
7745 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
7746 out_attr
[i
].set_int_value(3);
7747 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7748 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7750 case elfcpp::Tag_ABI_FP_16bit_format
:
7751 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7753 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7754 gold_error(_("fp16 format mismatch between %s and output"),
7757 if (in_attr
[i
].int_value() != 0)
7758 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7761 case elfcpp::Tag_nodefaults
:
7762 // This tag is set if it exists, but the value is unused (and is
7763 // typically zero). We don't actually need to do anything here -
7764 // the merge happens automatically when the type flags are merged
7767 case elfcpp::Tag_also_compatible_with
:
7768 // Already done in Tag_CPU_arch.
7770 case elfcpp::Tag_conformance
:
7771 // Keep the attribute if it matches. Throw it away otherwise.
7772 // No attribute means no claim to conform.
7773 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
7774 out_attr
[i
].set_string_value("");
7779 const char* err_object
= NULL
;
7781 // The "known_obj_attributes" table does contain some undefined
7782 // attributes. Ensure that there are unused.
7783 if (out_attr
[i
].int_value() != 0
7784 || out_attr
[i
].string_value() != "")
7785 err_object
= "output";
7786 else if (in_attr
[i
].int_value() != 0
7787 || in_attr
[i
].string_value() != "")
7790 if (err_object
!= NULL
)
7792 // Attribute numbers >=64 (mod 128) can be safely ignored.
7794 gold_error(_("%s: unknown mandatory EABI object attribute "
7798 gold_warning(_("%s: unknown EABI object attribute %d"),
7802 // Only pass on attributes that match in both inputs.
7803 if (!in_attr
[i
].matches(out_attr
[i
]))
7805 out_attr
[i
].set_int_value(0);
7806 out_attr
[i
].set_string_value("");
7811 // If out_attr was copied from in_attr then it won't have a type yet.
7812 if (in_attr
[i
].type() && !out_attr
[i
].type())
7813 out_attr
[i
].set_type(in_attr
[i
].type());
7816 // Merge Tag_compatibility attributes and any common GNU ones.
7817 this->attributes_section_data_
->merge(name
, pasd
);
7819 // Check for any attributes not known on ARM.
7820 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
7821 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
7822 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
7823 Other_attributes
* out_other_attributes
=
7824 this->attributes_section_data_
->other_attributes(vendor
);
7825 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
7827 while (in_iter
!= in_other_attributes
->end()
7828 || out_iter
!= out_other_attributes
->end())
7830 const char* err_object
= NULL
;
7833 // The tags for each list are in numerical order.
7834 // If the tags are equal, then merge.
7835 if (out_iter
!= out_other_attributes
->end()
7836 && (in_iter
== in_other_attributes
->end()
7837 || in_iter
->first
> out_iter
->first
))
7839 // This attribute only exists in output. We can't merge, and we
7840 // don't know what the tag means, so delete it.
7841 err_object
= "output";
7842 err_tag
= out_iter
->first
;
7843 int saved_tag
= out_iter
->first
;
7844 delete out_iter
->second
;
7845 out_other_attributes
->erase(out_iter
);
7846 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7848 else if (in_iter
!= in_other_attributes
->end()
7849 && (out_iter
!= out_other_attributes
->end()
7850 || in_iter
->first
< out_iter
->first
))
7852 // This attribute only exists in input. We can't merge, and we
7853 // don't know what the tag means, so ignore it.
7855 err_tag
= in_iter
->first
;
7858 else // The tags are equal.
7860 // As present, all attributes in the list are unknown, and
7861 // therefore can't be merged meaningfully.
7862 err_object
= "output";
7863 err_tag
= out_iter
->first
;
7865 // Only pass on attributes that match in both inputs.
7866 if (!in_iter
->second
->matches(*(out_iter
->second
)))
7868 // No match. Delete the attribute.
7869 int saved_tag
= out_iter
->first
;
7870 delete out_iter
->second
;
7871 out_other_attributes
->erase(out_iter
);
7872 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7876 // Matched. Keep the attribute and move to the next.
7884 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7885 if ((err_tag
& 127) < 64)
7887 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7888 err_object
, err_tag
);
7892 gold_warning(_("%s: unknown EABI object attribute %d"),
7893 err_object
, err_tag
);
7899 // Return whether a relocation type used the LSB to distinguish THUMB
7901 template<bool big_endian
>
7903 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
7907 case elfcpp::R_ARM_PC24
:
7908 case elfcpp::R_ARM_ABS32
:
7909 case elfcpp::R_ARM_REL32
:
7910 case elfcpp::R_ARM_SBREL32
:
7911 case elfcpp::R_ARM_THM_CALL
:
7912 case elfcpp::R_ARM_GLOB_DAT
:
7913 case elfcpp::R_ARM_JUMP_SLOT
:
7914 case elfcpp::R_ARM_GOTOFF32
:
7915 case elfcpp::R_ARM_PLT32
:
7916 case elfcpp::R_ARM_CALL
:
7917 case elfcpp::R_ARM_JUMP24
:
7918 case elfcpp::R_ARM_THM_JUMP24
:
7919 case elfcpp::R_ARM_SBREL31
:
7920 case elfcpp::R_ARM_PREL31
:
7921 case elfcpp::R_ARM_MOVW_ABS_NC
:
7922 case elfcpp::R_ARM_MOVW_PREL_NC
:
7923 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7924 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7925 case elfcpp::R_ARM_THM_JUMP19
:
7926 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7927 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7928 case elfcpp::R_ARM_ALU_PC_G0
:
7929 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7930 case elfcpp::R_ARM_ALU_PC_G1
:
7931 case elfcpp::R_ARM_ALU_PC_G2
:
7932 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7933 case elfcpp::R_ARM_ALU_SB_G0
:
7934 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7935 case elfcpp::R_ARM_ALU_SB_G1
:
7936 case elfcpp::R_ARM_ALU_SB_G2
:
7937 case elfcpp::R_ARM_MOVW_BREL_NC
:
7938 case elfcpp::R_ARM_MOVW_BREL
:
7939 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7940 case elfcpp::R_ARM_THM_MOVW_BREL
:
7947 // Stub-generation methods for Target_arm.
7949 // Make a new Arm_input_section object.
7951 template<bool big_endian
>
7952 Arm_input_section
<big_endian
>*
7953 Target_arm
<big_endian
>::new_arm_input_section(
7957 Section_id
sid(relobj
, shndx
);
7959 Arm_input_section
<big_endian
>* arm_input_section
=
7960 new Arm_input_section
<big_endian
>(relobj
, shndx
);
7961 arm_input_section
->init();
7963 // Register new Arm_input_section in map for look-up.
7964 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
7965 this->arm_input_section_map_
.insert(std::make_pair(sid
, arm_input_section
));
7967 // Make sure that it we have not created another Arm_input_section
7968 // for this input section already.
7969 gold_assert(ins
.second
);
7971 return arm_input_section
;
7974 // Find the Arm_input_section object corresponding to the SHNDX-th input
7975 // section of RELOBJ.
7977 template<bool big_endian
>
7978 Arm_input_section
<big_endian
>*
7979 Target_arm
<big_endian
>::find_arm_input_section(
7981 unsigned int shndx
) const
7983 Section_id
sid(relobj
, shndx
);
7984 typename
Arm_input_section_map::const_iterator p
=
7985 this->arm_input_section_map_
.find(sid
);
7986 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
7989 // Make a new stub table.
7991 template<bool big_endian
>
7992 Stub_table
<big_endian
>*
7993 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
7995 Stub_table
<big_endian
>* stub_table
=
7996 new Stub_table
<big_endian
>(owner
);
7997 this->stub_tables_
.push_back(stub_table
);
7999 stub_table
->set_address(owner
->address() + owner
->data_size());
8000 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
8001 stub_table
->finalize_data_size();
8006 // Scan a relocation for stub generation.
8008 template<bool big_endian
>
8010 Target_arm
<big_endian
>::scan_reloc_for_stub(
8011 const Relocate_info
<32, big_endian
>* relinfo
,
8012 unsigned int r_type
,
8013 const Sized_symbol
<32>* gsym
,
8015 const Symbol_value
<32>* psymval
,
8016 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
8017 Arm_address address
)
8019 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
8021 const Arm_relobj
<big_endian
>* arm_relobj
=
8022 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8024 if (r_type
== elfcpp::R_ARM_V4BX
)
8026 const uint32_t reg
= (addend
& 0xf);
8027 if (this->fix_v4bx() == 2 && reg
< 0xf)
8029 // Try looking up an existing stub from a stub table.
8030 Stub_table
<big_endian
>* stub_table
=
8031 arm_relobj
->stub_table(relinfo
->data_shndx
);
8032 gold_assert(stub_table
!= NULL
);
8034 if (stub_table
->find_arm_v4bx_stub(reg
) == NULL
)
8036 // create a new stub and add it to stub table.
8037 Arm_v4bx_stub
* stub
=
8038 this->stub_factory().make_arm_v4bx_stub(reg
);
8039 gold_assert(stub
!= NULL
);
8040 stub_table
->add_arm_v4bx_stub(stub
);
8047 bool target_is_thumb
;
8048 Symbol_value
<32> symval
;
8051 // This is a global symbol. Determine if we use PLT and if the
8052 // final target is THUMB.
8053 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
8055 // This uses a PLT, change the symbol value.
8056 symval
.set_output_value(this->plt_section()->address()
8057 + gsym
->plt_offset());
8059 target_is_thumb
= false;
8061 else if (gsym
->is_undefined())
8062 // There is no need to generate a stub symbol is undefined.
8067 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
8068 || (gsym
->type() == elfcpp::STT_FUNC
8069 && !gsym
->is_undefined()
8070 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
8075 // This is a local symbol. Determine if the final target is THUMB.
8076 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
8079 // Strip LSB if this points to a THUMB target.
8081 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
8082 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
8084 Arm_address stripped_value
=
8085 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
8086 symval
.set_output_value(stripped_value
);
8090 // Get the symbol value.
8091 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
8093 // Owing to pipelining, the PC relative branches below actually skip
8094 // two instructions when the branch offset is 0.
8095 Arm_address destination
;
8098 case elfcpp::R_ARM_CALL
:
8099 case elfcpp::R_ARM_JUMP24
:
8100 case elfcpp::R_ARM_PLT32
:
8102 destination
= value
+ addend
+ 8;
8104 case elfcpp::R_ARM_THM_CALL
:
8105 case elfcpp::R_ARM_THM_XPC22
:
8106 case elfcpp::R_ARM_THM_JUMP24
:
8107 case elfcpp::R_ARM_THM_JUMP19
:
8109 destination
= value
+ addend
+ 4;
8115 Reloc_stub
* stub
= NULL
;
8116 Stub_type stub_type
=
8117 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
8119 if (stub_type
!= arm_stub_none
)
8121 // Try looking up an existing stub from a stub table.
8122 Stub_table
<big_endian
>* stub_table
=
8123 arm_relobj
->stub_table(relinfo
->data_shndx
);
8124 gold_assert(stub_table
!= NULL
);
8126 // Locate stub by destination.
8127 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
8129 // Create a stub if there is not one already
8130 stub
= stub_table
->find_reloc_stub(stub_key
);
8133 // create a new stub and add it to stub table.
8134 stub
= this->stub_factory().make_reloc_stub(stub_type
);
8135 stub_table
->add_reloc_stub(stub
, stub_key
);
8138 // Record the destination address.
8139 stub
->set_destination_address(destination
8140 | (target_is_thumb
? 1 : 0));
8143 // For Cortex-A8, we need to record a relocation at 4K page boundary.
8144 if (this->fix_cortex_a8_
8145 && (r_type
== elfcpp::R_ARM_THM_JUMP24
8146 || r_type
== elfcpp::R_ARM_THM_JUMP19
8147 || r_type
== elfcpp::R_ARM_THM_CALL
8148 || r_type
== elfcpp::R_ARM_THM_XPC22
)
8149 && (address
& 0xfffU
) == 0xffeU
)
8151 // Found a candidate. Note we haven't checked the destination is
8152 // within 4K here: if we do so (and don't create a record) we can't
8153 // tell that a branch should have been relocated when scanning later.
8154 this->cortex_a8_relocs_info_
[address
] =
8155 new Cortex_a8_reloc(stub
, r_type
,
8156 destination
| (target_is_thumb
? 1 : 0));
8160 // This function scans a relocation sections for stub generation.
8161 // The template parameter Relocate must be a class type which provides
8162 // a single function, relocate(), which implements the machine
8163 // specific part of a relocation.
8165 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
8166 // SHT_REL or SHT_RELA.
8168 // PRELOCS points to the relocation data. RELOC_COUNT is the number
8169 // of relocs. OUTPUT_SECTION is the output section.
8170 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
8171 // mapped to output offsets.
8173 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
8174 // VIEW_SIZE is the size. These refer to the input section, unless
8175 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
8176 // the output section.
8178 template<bool big_endian
>
8179 template<int sh_type
>
8181 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
8182 const Relocate_info
<32, big_endian
>* relinfo
,
8183 const unsigned char* prelocs
,
8185 Output_section
* output_section
,
8186 bool needs_special_offset_handling
,
8187 const unsigned char* view
,
8188 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
8191 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
8192 const int reloc_size
=
8193 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
8195 Arm_relobj
<big_endian
>* arm_object
=
8196 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8197 unsigned int local_count
= arm_object
->local_symbol_count();
8199 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
8201 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
8203 Reltype
reloc(prelocs
);
8205 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
8206 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
8207 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
8209 r_type
= this->get_real_reloc_type(r_type
);
8211 // Only a few relocation types need stubs.
8212 if ((r_type
!= elfcpp::R_ARM_CALL
)
8213 && (r_type
!= elfcpp::R_ARM_JUMP24
)
8214 && (r_type
!= elfcpp::R_ARM_PLT32
)
8215 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
8216 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
8217 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
8218 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
)
8219 && (r_type
!= elfcpp::R_ARM_V4BX
))
8222 section_offset_type offset
=
8223 convert_to_section_size_type(reloc
.get_r_offset());
8225 if (needs_special_offset_handling
)
8227 offset
= output_section
->output_offset(relinfo
->object
,
8228 relinfo
->data_shndx
,
8234 if (r_type
== elfcpp::R_ARM_V4BX
)
8236 // Get the BX instruction.
8237 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8238 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ offset
);
8239 elfcpp::Elf_types
<32>::Elf_Swxword insn
=
8240 elfcpp::Swap
<32, big_endian
>::readval(wv
);
8241 this->scan_reloc_for_stub(relinfo
, r_type
, NULL
, 0, NULL
,
8247 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
8248 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
8249 stub_addend_reader(r_type
, view
+ offset
, reloc
);
8251 const Sized_symbol
<32>* sym
;
8253 Symbol_value
<32> symval
;
8254 const Symbol_value
<32> *psymval
;
8255 if (r_sym
< local_count
)
8258 psymval
= arm_object
->local_symbol(r_sym
);
8260 // If the local symbol belongs to a section we are discarding,
8261 // and that section is a debug section, try to find the
8262 // corresponding kept section and map this symbol to its
8263 // counterpart in the kept section. The symbol must not
8264 // correspond to a section we are folding.
8266 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
8268 && shndx
!= elfcpp::SHN_UNDEF
8269 && !arm_object
->is_section_included(shndx
)
8270 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
8272 if (comdat_behavior
== CB_UNDETERMINED
)
8275 arm_object
->section_name(relinfo
->data_shndx
);
8276 comdat_behavior
= get_comdat_behavior(name
.c_str());
8278 if (comdat_behavior
== CB_PRETEND
)
8281 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
8282 arm_object
->map_to_kept_section(shndx
, &found
);
8284 symval
.set_output_value(value
+ psymval
->input_value());
8286 symval
.set_output_value(0);
8290 symval
.set_output_value(0);
8292 symval
.set_no_output_symtab_entry();
8298 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
8299 gold_assert(gsym
!= NULL
);
8300 if (gsym
->is_forwarder())
8301 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
8303 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
8304 if (sym
->has_symtab_index())
8305 symval
.set_output_symtab_index(sym
->symtab_index());
8307 symval
.set_no_output_symtab_entry();
8309 // We need to compute the would-be final value of this global
8311 const Symbol_table
* symtab
= relinfo
->symtab
;
8312 const Sized_symbol
<32>* sized_symbol
=
8313 symtab
->get_sized_symbol
<32>(gsym
);
8314 Symbol_table::Compute_final_value_status status
;
8316 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
8318 // Skip this if the symbol has not output section.
8319 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
8322 symval
.set_output_value(value
);
8326 // If symbol is a section symbol, we don't know the actual type of
8327 // destination. Give up.
8328 if (psymval
->is_section_symbol())
8331 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
8332 addend
, view_address
+ offset
);
8336 // Scan an input section for stub generation.
8338 template<bool big_endian
>
8340 Target_arm
<big_endian
>::scan_section_for_stubs(
8341 const Relocate_info
<32, big_endian
>* relinfo
,
8342 unsigned int sh_type
,
8343 const unsigned char* prelocs
,
8345 Output_section
* output_section
,
8346 bool needs_special_offset_handling
,
8347 const unsigned char* view
,
8348 Arm_address view_address
,
8349 section_size_type view_size
)
8351 if (sh_type
== elfcpp::SHT_REL
)
8352 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
8357 needs_special_offset_handling
,
8361 else if (sh_type
== elfcpp::SHT_RELA
)
8362 // We do not support RELA type relocations yet. This is provided for
8364 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
8369 needs_special_offset_handling
,
8377 // Group input sections for stub generation.
8379 // We goup input sections in an output sections so that the total size,
8380 // including any padding space due to alignment is smaller than GROUP_SIZE
8381 // unless the only input section in group is bigger than GROUP_SIZE already.
8382 // Then an ARM stub table is created to follow the last input section
8383 // in group. For each group an ARM stub table is created an is placed
8384 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
8385 // extend the group after the stub table.
8387 template<bool big_endian
>
8389 Target_arm
<big_endian
>::group_sections(
8391 section_size_type group_size
,
8392 bool stubs_always_after_branch
)
8394 // Group input sections and insert stub table
8395 Layout::Section_list section_list
;
8396 layout
->get_allocated_sections(§ion_list
);
8397 for (Layout::Section_list::const_iterator p
= section_list
.begin();
8398 p
!= section_list
.end();
8401 Arm_output_section
<big_endian
>* output_section
=
8402 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
8403 output_section
->group_sections(group_size
, stubs_always_after_branch
,
8408 // Relaxation hook. This is where we do stub generation.
8410 template<bool big_endian
>
8412 Target_arm
<big_endian
>::do_relax(
8414 const Input_objects
* input_objects
,
8415 Symbol_table
* symtab
,
8418 // No need to generate stubs if this is a relocatable link.
8419 gold_assert(!parameters
->options().relocatable());
8421 // If this is the first pass, we need to group input sections into
8425 // Determine the stub group size. The group size is the absolute
8426 // value of the parameter --stub-group-size. If --stub-group-size
8427 // is passed a negative value, we restict stubs to be always after
8428 // the stubbed branches.
8429 int32_t stub_group_size_param
=
8430 parameters
->options().stub_group_size();
8431 bool stubs_always_after_branch
= stub_group_size_param
< 0;
8432 section_size_type stub_group_size
= abs(stub_group_size_param
);
8434 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8435 // page as the first half of a 32-bit branch straddling two 4K pages.
8436 // This is a crude way of enforcing that.
8437 if (this->fix_cortex_a8_
)
8438 stubs_always_after_branch
= true;
8440 if (stub_group_size
== 1)
8443 // Thumb branch range is +-4MB has to be used as the default
8444 // maximum size (a given section can contain both ARM and Thumb
8445 // code, so the worst case has to be taken into account).
8447 // This value is 24K less than that, which allows for 2025
8448 // 12-byte stubs. If we exceed that, then we will fail to link.
8449 // The user will have to relink with an explicit group size
8451 stub_group_size
= 4170000;
8454 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
8457 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8458 // beginning of each relaxation pass, just blow away all the stubs.
8459 // Alternatively, we could selectively remove only the stubs and reloc
8460 // information for code sections that have moved since the last pass.
8461 // That would require more book-keeping.
8462 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
8463 if (this->fix_cortex_a8_
)
8465 // Clear all Cortex-A8 reloc information.
8466 for (typename
Cortex_a8_relocs_info::const_iterator p
=
8467 this->cortex_a8_relocs_info_
.begin();
8468 p
!= this->cortex_a8_relocs_info_
.end();
8471 this->cortex_a8_relocs_info_
.clear();
8473 // Remove all Cortex-A8 stubs.
8474 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8475 sp
!= this->stub_tables_
.end();
8477 (*sp
)->remove_all_cortex_a8_stubs();
8480 // Scan relocs for relocation stubs
8481 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
8482 op
!= input_objects
->relobj_end();
8485 Arm_relobj
<big_endian
>* arm_relobj
=
8486 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
8487 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
8490 // Check all stub tables to see if any of them have their data sizes
8491 // or addresses alignments changed. These are the only things that
8493 bool any_stub_table_changed
= false;
8494 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8495 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8498 if ((*sp
)->update_data_size_and_addralign())
8499 any_stub_table_changed
= true;
8502 // Finalize the stubs in the last relaxation pass.
8503 if (!any_stub_table_changed
)
8504 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8505 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8507 (*sp
)->finalize_stubs();
8509 return any_stub_table_changed
;
8514 template<bool big_endian
>
8516 Target_arm
<big_endian
>::relocate_stub(
8518 const Relocate_info
<32, big_endian
>* relinfo
,
8519 Output_section
* output_section
,
8520 unsigned char* view
,
8521 Arm_address address
,
8522 section_size_type view_size
)
8525 const Stub_template
* stub_template
= stub
->stub_template();
8526 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
8528 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
8529 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
8531 unsigned int r_type
= insn
->r_type();
8532 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
8533 section_size_type reloc_size
= insn
->size();
8534 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
8536 // This is the address of the stub destination.
8537 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
8538 Symbol_value
<32> symval
;
8539 symval
.set_output_value(target
);
8541 // Synthesize a fake reloc just in case. We don't have a symbol so
8543 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
8544 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
8545 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
8546 reloc_write
.put_r_offset(reloc_offset
);
8547 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
8548 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
8550 relocate
.relocate(relinfo
, this, output_section
,
8551 this->fake_relnum_for_stubs
, rel
, r_type
,
8552 NULL
, &symval
, view
+ reloc_offset
,
8553 address
+ reloc_offset
, reloc_size
);
8557 // Determine whether an object attribute tag takes an integer, a
8560 template<bool big_endian
>
8562 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
8564 if (tag
== Object_attribute::Tag_compatibility
)
8565 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8566 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
8567 else if (tag
== elfcpp::Tag_nodefaults
)
8568 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8569 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
8570 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
8571 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
8573 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
8575 return ((tag
& 1) != 0
8576 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
8577 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8580 // Reorder attributes.
8582 // The ABI defines that Tag_conformance should be emitted first, and that
8583 // Tag_nodefaults should be second (if either is defined). This sets those
8584 // two positions, and bumps up the position of all the remaining tags to
8587 template<bool big_endian
>
8589 Target_arm
<big_endian
>::do_attributes_order(int num
) const
8591 // Reorder the known object attributes in output. We want to move
8592 // Tag_conformance to position 4 and Tag_conformance to position 5
8593 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
8595 return elfcpp::Tag_conformance
;
8597 return elfcpp::Tag_nodefaults
;
8598 if ((num
- 2) < elfcpp::Tag_nodefaults
)
8600 if ((num
- 1) < elfcpp::Tag_conformance
)
8605 // Scan a span of THUMB code for Cortex-A8 erratum.
8607 template<bool big_endian
>
8609 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
8610 Arm_relobj
<big_endian
>* arm_relobj
,
8612 section_size_type span_start
,
8613 section_size_type span_end
,
8614 const unsigned char* view
,
8615 Arm_address address
)
8617 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
8619 // The opcode is BLX.W, BL.W, B.W, Bcc.W
8620 // The branch target is in the same 4KB region as the
8621 // first half of the branch.
8622 // The instruction before the branch is a 32-bit
8623 // length non-branch instruction.
8624 section_size_type i
= span_start
;
8625 bool last_was_32bit
= false;
8626 bool last_was_branch
= false;
8627 while (i
< span_end
)
8629 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8630 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
8631 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8632 bool is_blx
= false, is_b
= false;
8633 bool is_bl
= false, is_bcc
= false;
8635 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
8638 // Load the rest of the insn (in manual-friendly order).
8639 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8641 // Encoding T4: B<c>.W.
8642 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
8643 // Encoding T1: BL<c>.W.
8644 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
8645 // Encoding T2: BLX<c>.W.
8646 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
8647 // Encoding T3: B<c>.W (not permitted in IT block).
8648 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
8649 && (insn
& 0x07f00000U
) != 0x03800000U
);
8652 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
8654 // If this instruction is a 32-bit THUMB branch that crosses a 4K
8655 // page boundary and it follows 32-bit non-branch instruction,
8656 // we need to work around.
8658 && ((address
+ i
) & 0xfffU
) == 0xffeU
8660 && !last_was_branch
)
8662 // Check to see if there is a relocation stub for this branch.
8663 bool force_target_arm
= false;
8664 bool force_target_thumb
= false;
8665 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
8666 Cortex_a8_relocs_info::const_iterator p
=
8667 this->cortex_a8_relocs_info_
.find(address
+ i
);
8669 if (p
!= this->cortex_a8_relocs_info_
.end())
8671 cortex_a8_reloc
= p
->second
;
8672 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
8674 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8675 && !target_is_thumb
)
8676 force_target_arm
= true;
8677 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8679 force_target_thumb
= true;
8683 Stub_type stub_type
= arm_stub_none
;
8685 // Check if we have an offending branch instruction.
8686 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
8687 uint16_t lower_insn
= insn
& 0xffffU
;
8688 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8690 if (cortex_a8_reloc
!= NULL
8691 && cortex_a8_reloc
->reloc_stub() != NULL
)
8692 // We've already made a stub for this instruction, e.g.
8693 // it's a long branch or a Thumb->ARM stub. Assume that
8694 // stub will suffice to work around the A8 erratum (see
8695 // setting of always_after_branch above).
8699 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
8701 stub_type
= arm_stub_a8_veneer_b_cond
;
8703 else if (is_b
|| is_bl
|| is_blx
)
8705 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
8711 ? arm_stub_a8_veneer_blx
8713 ? arm_stub_a8_veneer_bl
8714 : arm_stub_a8_veneer_b
));
8717 if (stub_type
!= arm_stub_none
)
8719 Arm_address pc_for_insn
= address
+ i
+ 4;
8721 // The original instruction is a BL, but the target is
8722 // an ARM instruction. If we were not making a stub,
8723 // the BL would have been converted to a BLX. Use the
8724 // BLX stub instead in that case.
8725 if (this->may_use_blx() && force_target_arm
8726 && stub_type
== arm_stub_a8_veneer_bl
)
8728 stub_type
= arm_stub_a8_veneer_blx
;
8732 // Conversely, if the original instruction was
8733 // BLX but the target is Thumb mode, use the BL stub.
8734 else if (force_target_thumb
8735 && stub_type
== arm_stub_a8_veneer_blx
)
8737 stub_type
= arm_stub_a8_veneer_bl
;
8745 // If we found a relocation, use the proper destination,
8746 // not the offset in the (unrelocated) instruction.
8747 // Note this is always done if we switched the stub type above.
8748 if (cortex_a8_reloc
!= NULL
)
8749 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
8751 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
8753 // Add a new stub if destination address in in the same page.
8754 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
8756 Cortex_a8_stub
* stub
=
8757 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
8761 Stub_table
<big_endian
>* stub_table
=
8762 arm_relobj
->stub_table(shndx
);
8763 gold_assert(stub_table
!= NULL
);
8764 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
8769 i
+= insn_32bit
? 4 : 2;
8770 last_was_32bit
= insn_32bit
;
8771 last_was_branch
= is_32bit_branch
;
8775 // Apply the Cortex-A8 workaround.
8777 template<bool big_endian
>
8779 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
8780 const Cortex_a8_stub
* stub
,
8781 Arm_address stub_address
,
8782 unsigned char* insn_view
,
8783 Arm_address insn_address
)
8785 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8786 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
8787 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8788 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8789 off_t branch_offset
= stub_address
- (insn_address
+ 4);
8791 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8792 switch (stub
->stub_template()->type())
8794 case arm_stub_a8_veneer_b_cond
:
8795 gold_assert(!utils::has_overflow
<21>(branch_offset
));
8796 upper_insn
= RelocFuncs::thumb32_cond_branch_upper(upper_insn
,
8798 lower_insn
= RelocFuncs::thumb32_cond_branch_lower(lower_insn
,
8802 case arm_stub_a8_veneer_b
:
8803 case arm_stub_a8_veneer_bl
:
8804 case arm_stub_a8_veneer_blx
:
8805 if ((lower_insn
& 0x5000U
) == 0x4000U
)
8806 // For a BLX instruction, make sure that the relocation is
8807 // rounded up to a word boundary. This follows the semantics of
8808 // the instruction which specifies that bit 1 of the target
8809 // address will come from bit 1 of the base address.
8810 branch_offset
= (branch_offset
+ 2) & ~3;
8812 // Put BRANCH_OFFSET back into the insn.
8813 gold_assert(!utils::has_overflow
<25>(branch_offset
));
8814 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
8815 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
8822 // Put the relocated value back in the object file:
8823 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
8824 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
8827 template<bool big_endian
>
8828 class Target_selector_arm
: public Target_selector
8831 Target_selector_arm()
8832 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
8833 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
8837 do_instantiate_target()
8838 { return new Target_arm
<big_endian
>(); }
8841 Target_selector_arm
<false> target_selector_arm
;
8842 Target_selector_arm
<true> target_selector_armbe
;
8844 } // End anonymous namespace.