1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
37 #include "parameters.h"
44 #include "copy-relocs.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
51 #include "attributes.h"
58 template<bool big_endian
>
59 class Output_data_plt_arm
;
61 template<bool big_endian
>
64 template<bool big_endian
>
65 class Arm_input_section
;
67 template<bool big_endian
>
68 class Arm_output_section
;
70 template<bool big_endian
>
73 template<bool big_endian
>
77 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
79 // Maximum branch offsets for ARM, THUMB and THUMB2.
80 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
81 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
82 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
83 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
84 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
85 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
87 // The arm target class.
89 // This is a very simple port of gold for ARM-EABI. It is intended for
90 // supporting Android only for the time being. Only these relocation types
119 // R_ARM_THM_MOVW_ABS_NC
120 // R_ARM_THM_MOVT_ABS
121 // R_ARM_MOVW_PREL_NC
123 // R_ARM_THM_MOVW_PREL_NC
124 // R_ARM_THM_MOVT_PREL
131 // - Support more relocation types as needed.
132 // - Make PLTs more flexible for different architecture features like
134 // There are probably a lot more.
136 // Instruction template class. This class is similar to the insn_sequence
137 // struct in bfd/elf32-arm.c.
142 // Types of instruction templates.
146 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
147 // templates with class-specific semantics. Currently this is used
148 // only by the Cortex_a8_stub class for handling condition codes in
149 // conditional branches.
150 THUMB16_SPECIAL_TYPE
,
156 // Factory methods to create instruction templates in different formats.
158 static const Insn_template
159 thumb16_insn(uint32_t data
)
160 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
162 // A Thumb conditional branch, in which the proper condition is inserted
163 // when we build the stub.
164 static const Insn_template
165 thumb16_bcond_insn(uint32_t data
)
166 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
168 static const Insn_template
169 thumb32_insn(uint32_t data
)
170 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
172 static const Insn_template
173 thumb32_b_insn(uint32_t data
, int reloc_addend
)
175 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
179 static const Insn_template
180 arm_insn(uint32_t data
)
181 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
183 static const Insn_template
184 arm_rel_insn(unsigned data
, int reloc_addend
)
185 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
187 static const Insn_template
188 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
189 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
191 // Accessors. This class is used for read-only objects so no modifiers
196 { return this->data_
; }
198 // Return the instruction sequence type of this.
201 { return this->type_
; }
203 // Return the ARM relocation type of this.
206 { return this->r_type_
; }
210 { return this->reloc_addend_
; }
212 // Return size of instruction template in bytes.
216 // Return byte-alignment of instruction template.
221 // We make the constructor private to ensure that only the factory
224 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
225 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
228 // Instruction specific data. This is used to store information like
229 // some of the instruction bits.
231 // Instruction template type.
233 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
234 unsigned int r_type_
;
235 // Relocation addend.
236 int32_t reloc_addend_
;
239 // Macro for generating code to stub types. One entry per long/short
243 DEF_STUB(long_branch_any_any) \
244 DEF_STUB(long_branch_v4t_arm_thumb) \
245 DEF_STUB(long_branch_thumb_only) \
246 DEF_STUB(long_branch_v4t_thumb_thumb) \
247 DEF_STUB(long_branch_v4t_thumb_arm) \
248 DEF_STUB(short_branch_v4t_thumb_arm) \
249 DEF_STUB(long_branch_any_arm_pic) \
250 DEF_STUB(long_branch_any_thumb_pic) \
251 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
252 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
253 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
254 DEF_STUB(long_branch_thumb_only_pic) \
255 DEF_STUB(a8_veneer_b_cond) \
256 DEF_STUB(a8_veneer_b) \
257 DEF_STUB(a8_veneer_bl) \
258 DEF_STUB(a8_veneer_blx) \
259 DEF_STUB(v4_veneer_bx)
263 #define DEF_STUB(x) arm_stub_##x,
269 // First reloc stub type.
270 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
271 // Last reloc stub type.
272 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
274 // First Cortex-A8 stub type.
275 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
276 // Last Cortex-A8 stub type.
277 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
280 arm_stub_type_last
= arm_stub_v4_veneer_bx
284 // Stub template class. Templates are meant to be read-only objects.
285 // A stub template for a stub type contains all read-only attributes
286 // common to all stubs of the same type.
291 Stub_template(Stub_type
, const Insn_template
*, size_t);
299 { return this->type_
; }
301 // Return an array of instruction templates.
304 { return this->insns_
; }
306 // Return size of template in number of instructions.
309 { return this->insn_count_
; }
311 // Return size of template in bytes.
314 { return this->size_
; }
316 // Return alignment of the stub template.
319 { return this->alignment_
; }
321 // Return whether entry point is in thumb mode.
323 entry_in_thumb_mode() const
324 { return this->entry_in_thumb_mode_
; }
326 // Return number of relocations in this template.
329 { return this->relocs_
.size(); }
331 // Return index of the I-th instruction with relocation.
333 reloc_insn_index(size_t i
) const
335 gold_assert(i
< this->relocs_
.size());
336 return this->relocs_
[i
].first
;
339 // Return the offset of the I-th instruction with relocation from the
340 // beginning of the stub.
342 reloc_offset(size_t i
) const
344 gold_assert(i
< this->relocs_
.size());
345 return this->relocs_
[i
].second
;
349 // This contains information about an instruction template with a relocation
350 // and its offset from start of stub.
351 typedef std::pair
<size_t, section_size_type
> Reloc
;
353 // A Stub_template may not be copied. We want to share templates as much
355 Stub_template(const Stub_template
&);
356 Stub_template
& operator=(const Stub_template
&);
360 // Points to an array of Insn_templates.
361 const Insn_template
* insns_
;
362 // Number of Insn_templates in insns_[].
364 // Size of templated instructions in bytes.
366 // Alignment of templated instructions.
368 // Flag to indicate if entry is in thumb mode.
369 bool entry_in_thumb_mode_
;
370 // A table of reloc instruction indices and offsets. We can find these by
371 // looking at the instruction templates but we pre-compute and then stash
372 // them here for speed.
373 std::vector
<Reloc
> relocs_
;
377 // A class for code stubs. This is a base class for different type of
378 // stubs used in the ARM target.
384 static const section_offset_type invalid_offset
=
385 static_cast<section_offset_type
>(-1);
388 Stub(const Stub_template
* stub_template
)
389 : stub_template_(stub_template
), offset_(invalid_offset
)
396 // Return the stub template.
398 stub_template() const
399 { return this->stub_template_
; }
401 // Return offset of code stub from beginning of its containing stub table.
405 gold_assert(this->offset_
!= invalid_offset
);
406 return this->offset_
;
409 // Set offset of code stub from beginning of its containing stub table.
411 set_offset(section_offset_type offset
)
412 { this->offset_
= offset
; }
414 // Return the relocation target address of the i-th relocation in the
415 // stub. This must be defined in a child class.
417 reloc_target(size_t i
)
418 { return this->do_reloc_target(i
); }
420 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
422 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
423 { this->do_write(view
, view_size
, big_endian
); }
425 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
426 // for the i-th instruction.
428 thumb16_special(size_t i
)
429 { return this->do_thumb16_special(i
); }
432 // This must be defined in the child class.
434 do_reloc_target(size_t) = 0;
436 // This may be overridden in the child class.
438 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
441 this->do_fixed_endian_write
<true>(view
, view_size
);
443 this->do_fixed_endian_write
<false>(view
, view_size
);
446 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
447 // instruction template.
449 do_thumb16_special(size_t)
450 { gold_unreachable(); }
453 // A template to implement do_write.
454 template<bool big_endian
>
456 do_fixed_endian_write(unsigned char*, section_size_type
);
459 const Stub_template
* stub_template_
;
460 // Offset within the section of containing this stub.
461 section_offset_type offset_
;
464 // Reloc stub class. These are stubs we use to fix up relocation because
465 // of limited branch ranges.
467 class Reloc_stub
: public Stub
470 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
471 // We assume we never jump to this address.
472 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
474 // Return destination address.
476 destination_address() const
478 gold_assert(this->destination_address_
!= this->invalid_address
);
479 return this->destination_address_
;
482 // Set destination address.
484 set_destination_address(Arm_address address
)
486 gold_assert(address
!= this->invalid_address
);
487 this->destination_address_
= address
;
490 // Reset destination address.
492 reset_destination_address()
493 { this->destination_address_
= this->invalid_address
; }
495 // Determine stub type for a branch of a relocation of R_TYPE going
496 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
497 // the branch target is a thumb instruction. TARGET is used for look
498 // up ARM-specific linker settings.
500 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
501 Arm_address branch_target
, bool target_is_thumb
);
503 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
504 // and an addend. Since we treat global and local symbol differently, we
505 // use a Symbol object for a global symbol and a object-index pair for
510 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
511 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
512 // and R_SYM must not be invalid_index.
513 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
514 unsigned int r_sym
, int32_t addend
)
515 : stub_type_(stub_type
), addend_(addend
)
519 this->r_sym_
= Reloc_stub::invalid_index
;
520 this->u_
.symbol
= symbol
;
524 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
525 this->r_sym_
= r_sym
;
526 this->u_
.relobj
= relobj
;
533 // Accessors: Keys are meant to be read-only object so no modifiers are
539 { return this->stub_type_
; }
541 // Return the local symbol index or invalid_index.
544 { return this->r_sym_
; }
546 // Return the symbol if there is one.
549 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
551 // Return the relobj if there is one.
554 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
556 // Whether this equals to another key k.
558 eq(const Key
& k
) const
560 return ((this->stub_type_
== k
.stub_type_
)
561 && (this->r_sym_
== k
.r_sym_
)
562 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
563 ? (this->u_
.relobj
== k
.u_
.relobj
)
564 : (this->u_
.symbol
== k
.u_
.symbol
))
565 && (this->addend_
== k
.addend_
));
568 // Return a hash value.
572 return (this->stub_type_
574 ^ gold::string_hash
<char>(
575 (this->r_sym_
!= Reloc_stub::invalid_index
)
576 ? this->u_
.relobj
->name().c_str()
577 : this->u_
.symbol
->name())
581 // Functors for STL associative containers.
585 operator()(const Key
& k
) const
586 { return k
.hash_value(); }
592 operator()(const Key
& k1
, const Key
& k2
) const
593 { return k1
.eq(k2
); }
596 // Name of key. This is mainly for debugging.
602 Stub_type stub_type_
;
603 // If this is a local symbol, this is the index in the defining object.
604 // Otherwise, it is invalid_index for a global symbol.
606 // If r_sym_ is invalid index. This points to a global symbol.
607 // Otherwise, this points a relobj. We used the unsized and target
608 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
609 // Arm_relobj. This is done to avoid making the stub class a template
610 // as most of the stub machinery is endianity-neutral. However, it
611 // may require a bit of casting done by users of this class.
614 const Symbol
* symbol
;
615 const Relobj
* relobj
;
617 // Addend associated with a reloc.
622 // Reloc_stubs are created via a stub factory. So these are protected.
623 Reloc_stub(const Stub_template
* stub_template
)
624 : Stub(stub_template
), destination_address_(invalid_address
)
630 friend class Stub_factory
;
632 // Return the relocation target address of the i-th relocation in the
635 do_reloc_target(size_t i
)
637 // All reloc stub have only one relocation.
639 return this->destination_address_
;
643 // Address of destination.
644 Arm_address destination_address_
;
647 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
648 // THUMB branch that meets the following conditions:
650 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
651 // branch address is 0xffe.
652 // 2. The branch target address is in the same page as the first word of the
654 // 3. The branch follows a 32-bit instruction which is not a branch.
656 // To do the fix up, we need to store the address of the branch instruction
657 // and its target at least. We also need to store the original branch
658 // instruction bits for the condition code in a conditional branch. The
659 // condition code is used in a special instruction template. We also want
660 // to identify input sections needing Cortex-A8 workaround quickly. We store
661 // extra information about object and section index of the code section
662 // containing a branch being fixed up. The information is used to mark
663 // the code section when we finalize the Cortex-A8 stubs.
666 class Cortex_a8_stub
: public Stub
672 // Return the object of the code section containing the branch being fixed
676 { return this->relobj_
; }
678 // Return the section index of the code section containing the branch being
682 { return this->shndx_
; }
684 // Return the source address of stub. This is the address of the original
685 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
688 source_address() const
689 { return this->source_address_
; }
691 // Return the destination address of the stub. This is the branch taken
692 // address of the original branch instruction. LSB is 1 if it is a THUMB
693 // instruction address.
695 destination_address() const
696 { return this->destination_address_
; }
698 // Return the instruction being fixed up.
700 original_insn() const
701 { return this->original_insn_
; }
704 // Cortex_a8_stubs are created via a stub factory. So these are protected.
705 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
706 unsigned int shndx
, Arm_address source_address
,
707 Arm_address destination_address
, uint32_t original_insn
)
708 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
709 source_address_(source_address
| 1U),
710 destination_address_(destination_address
),
711 original_insn_(original_insn
)
714 friend class Stub_factory
;
716 // Return the relocation target address of the i-th relocation in the
719 do_reloc_target(size_t i
)
721 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
723 // The conditional branch veneer has two relocations.
725 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
729 // All other Cortex-A8 stubs have only one relocation.
731 return this->destination_address_
;
735 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
737 do_thumb16_special(size_t);
740 // Object of the code section containing the branch being fixed up.
742 // Section index of the code section containing the branch begin fixed up.
744 // Source address of original branch.
745 Arm_address source_address_
;
746 // Destination address of the original branch.
747 Arm_address destination_address_
;
748 // Original branch instruction. This is needed for copying the condition
749 // code from a condition branch to its stub.
750 uint32_t original_insn_
;
753 // ARMv4 BX Rx branch relocation stub class.
754 class Arm_v4bx_stub
: public Stub
760 // Return the associated register.
763 { return this->reg_
; }
766 // Arm V4BX stubs are created via a stub factory. So these are protected.
767 Arm_v4bx_stub(const Stub_template
* stub_template
, const uint32_t reg
)
768 : Stub(stub_template
), reg_(reg
)
771 friend class Stub_factory
;
773 // Return the relocation target address of the i-th relocation in the
776 do_reloc_target(size_t)
777 { gold_unreachable(); }
779 // This may be overridden in the child class.
781 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
784 this->do_fixed_endian_v4bx_write
<true>(view
, view_size
);
786 this->do_fixed_endian_v4bx_write
<false>(view
, view_size
);
790 // A template to implement do_write.
791 template<bool big_endian
>
793 do_fixed_endian_v4bx_write(unsigned char* view
, section_size_type
)
795 const Insn_template
* insns
= this->stub_template()->insns();
796 elfcpp::Swap
<32, big_endian
>::writeval(view
,
798 + (this->reg_
<< 16)));
799 view
+= insns
[0].size();
800 elfcpp::Swap
<32, big_endian
>::writeval(view
,
801 (insns
[1].data() + this->reg_
));
802 view
+= insns
[1].size();
803 elfcpp::Swap
<32, big_endian
>::writeval(view
,
804 (insns
[2].data() + this->reg_
));
807 // A register index (r0-r14), which is associated with the stub.
811 // Stub factory class.
816 // Return the unique instance of this class.
817 static const Stub_factory
&
820 static Stub_factory singleton
;
824 // Make a relocation stub.
826 make_reloc_stub(Stub_type stub_type
) const
828 gold_assert(stub_type
>= arm_stub_reloc_first
829 && stub_type
<= arm_stub_reloc_last
);
830 return new Reloc_stub(this->stub_templates_
[stub_type
]);
833 // Make a Cortex-A8 stub.
835 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
836 Arm_address source
, Arm_address destination
,
837 uint32_t original_insn
) const
839 gold_assert(stub_type
>= arm_stub_cortex_a8_first
840 && stub_type
<= arm_stub_cortex_a8_last
);
841 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
842 source
, destination
, original_insn
);
845 // Make an ARM V4BX relocation stub.
846 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
848 make_arm_v4bx_stub(uint32_t reg
) const
850 gold_assert(reg
< 0xf);
851 return new Arm_v4bx_stub(this->stub_templates_
[arm_stub_v4_veneer_bx
],
856 // Constructor and destructor are protected since we only return a single
857 // instance created in Stub_factory::get_instance().
861 // A Stub_factory may not be copied since it is a singleton.
862 Stub_factory(const Stub_factory
&);
863 Stub_factory
& operator=(Stub_factory
&);
865 // Stub templates. These are initialized in the constructor.
866 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
869 // A class to hold stubs for the ARM target.
871 template<bool big_endian
>
872 class Stub_table
: public Output_data
875 Stub_table(Arm_input_section
<big_endian
>* owner
)
876 : Output_data(), owner_(owner
), reloc_stubs_(), cortex_a8_stubs_(),
877 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
883 // Owner of this stub table.
884 Arm_input_section
<big_endian
>*
886 { return this->owner_
; }
888 // Whether this stub table is empty.
892 return (this->reloc_stubs_
.empty()
893 && this->cortex_a8_stubs_
.empty()
894 && this->arm_v4bx_stubs_
.empty());
897 // Return the current data size.
899 current_data_size() const
900 { return this->current_data_size_for_child(); }
902 // Add a STUB with using KEY. Caller is reponsible for avoid adding
903 // if already a STUB with the same key has been added.
905 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
907 const Stub_template
* stub_template
= stub
->stub_template();
908 gold_assert(stub_template
->type() == key
.stub_type());
909 this->reloc_stubs_
[key
] = stub
;
912 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
913 // Caller is reponsible for avoid adding if already a STUB with the same
914 // address has been added.
916 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
918 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
919 this->cortex_a8_stubs_
.insert(value
);
922 // Add an ARM V4BX relocation stub. A register index will be retrieved
925 add_arm_v4bx_stub(Arm_v4bx_stub
* stub
)
927 gold_assert(stub
!= NULL
&& this->arm_v4bx_stubs_
[stub
->reg()] == NULL
);
928 this->arm_v4bx_stubs_
[stub
->reg()] = stub
;
931 // Remove all Cortex-A8 stubs.
933 remove_all_cortex_a8_stubs();
935 // Look up a relocation stub using KEY. Return NULL if there is none.
937 find_reloc_stub(const Reloc_stub::Key
& key
) const
939 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
940 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
943 // Look up an arm v4bx relocation stub using the register index.
944 // Return NULL if there is none.
946 find_arm_v4bx_stub(const uint32_t reg
) const
948 gold_assert(reg
< 0xf);
949 return this->arm_v4bx_stubs_
[reg
];
952 // Relocate stubs in this stub table.
954 relocate_stubs(const Relocate_info
<32, big_endian
>*,
955 Target_arm
<big_endian
>*, Output_section
*,
956 unsigned char*, Arm_address
, section_size_type
);
958 // Update data size and alignment at the end of a relaxation pass. Return
959 // true if either data size or alignment is different from that of the
960 // previous relaxation pass.
962 update_data_size_and_addralign();
964 // Finalize stubs. Set the offsets of all stubs and mark input sections
965 // needing the Cortex-A8 workaround.
969 // Apply Cortex-A8 workaround to an address range.
971 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
972 unsigned char*, Arm_address
,
976 // Write out section contents.
978 do_write(Output_file
*);
980 // Return the required alignment.
983 { return this->prev_addralign_
; }
985 // Reset address and file offset.
987 do_reset_address_and_file_offset()
988 { this->set_current_data_size_for_child(this->prev_data_size_
); }
990 // Set final data size.
992 set_final_data_size()
993 { this->set_data_size(this->current_data_size()); }
996 // Relocate one stub.
998 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
999 Target_arm
<big_endian
>*, Output_section
*,
1000 unsigned char*, Arm_address
, section_size_type
);
1002 // Unordered map of relocation stubs.
1004 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
1005 Reloc_stub::Key::equal_to
>
1008 // List of Cortex-A8 stubs ordered by addresses of branches being
1009 // fixed up in output.
1010 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
1011 // List of Arm V4BX relocation stubs ordered by associated registers.
1012 typedef std::vector
<Arm_v4bx_stub
*> Arm_v4bx_stub_list
;
1014 // Owner of this stub table.
1015 Arm_input_section
<big_endian
>* owner_
;
1016 // The relocation stubs.
1017 Reloc_stub_map reloc_stubs_
;
1018 // The cortex_a8_stubs.
1019 Cortex_a8_stub_list cortex_a8_stubs_
;
1020 // The Arm V4BX relocation stubs.
1021 Arm_v4bx_stub_list arm_v4bx_stubs_
;
1022 // data size of this in the previous pass.
1023 off_t prev_data_size_
;
1024 // address alignment of this in the previous pass.
1025 uint64_t prev_addralign_
;
1028 // A class to wrap an ordinary input section containing executable code.
1030 template<bool big_endian
>
1031 class Arm_input_section
: public Output_relaxed_input_section
1034 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
1035 : Output_relaxed_input_section(relobj
, shndx
, 1),
1036 original_addralign_(1), original_size_(0), stub_table_(NULL
)
1039 ~Arm_input_section()
1046 // Whether this is a stub table owner.
1048 is_stub_table_owner() const
1049 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
1051 // Return the stub table.
1052 Stub_table
<big_endian
>*
1054 { return this->stub_table_
; }
1056 // Set the stub_table.
1058 set_stub_table(Stub_table
<big_endian
>* stub_table
)
1059 { this->stub_table_
= stub_table
; }
1061 // Downcast a base pointer to an Arm_input_section pointer. This is
1062 // not type-safe but we only use Arm_input_section not the base class.
1063 static Arm_input_section
<big_endian
>*
1064 as_arm_input_section(Output_relaxed_input_section
* poris
)
1065 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
1068 // Write data to output file.
1070 do_write(Output_file
*);
1072 // Return required alignment of this.
1074 do_addralign() const
1076 if (this->is_stub_table_owner())
1077 return std::max(this->stub_table_
->addralign(),
1078 this->original_addralign_
);
1080 return this->original_addralign_
;
1083 // Finalize data size.
1085 set_final_data_size();
1087 // Reset address and file offset.
1089 do_reset_address_and_file_offset();
1093 do_output_offset(const Relobj
* object
, unsigned int shndx
,
1094 section_offset_type offset
,
1095 section_offset_type
* poutput
) const
1097 if ((object
== this->relobj())
1098 && (shndx
== this->shndx())
1100 && (convert_types
<uint64_t, section_offset_type
>(offset
)
1101 <= this->original_size_
))
1111 // Copying is not allowed.
1112 Arm_input_section(const Arm_input_section
&);
1113 Arm_input_section
& operator=(const Arm_input_section
&);
1115 // Address alignment of the original input section.
1116 uint64_t original_addralign_
;
1117 // Section size of the original input section.
1118 uint64_t original_size_
;
1120 Stub_table
<big_endian
>* stub_table_
;
1123 // Arm output section class. This is defined mainly to add a number of
1124 // stub generation methods.
1126 template<bool big_endian
>
1127 class Arm_output_section
: public Output_section
1130 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1131 elfcpp::Elf_Xword flags
)
1132 : Output_section(name
, type
, flags
)
1135 ~Arm_output_section()
1138 // Group input sections for stub generation.
1140 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1142 // Downcast a base pointer to an Arm_output_section pointer. This is
1143 // not type-safe but we only use Arm_output_section not the base class.
1144 static Arm_output_section
<big_endian
>*
1145 as_arm_output_section(Output_section
* os
)
1146 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1150 typedef Output_section::Input_section Input_section
;
1151 typedef Output_section::Input_section_list Input_section_list
;
1153 // Create a stub group.
1154 void create_stub_group(Input_section_list::const_iterator
,
1155 Input_section_list::const_iterator
,
1156 Input_section_list::const_iterator
,
1157 Target_arm
<big_endian
>*,
1158 std::vector
<Output_relaxed_input_section
*>*);
1161 // Arm_relobj class.
1163 template<bool big_endian
>
1164 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1167 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1169 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1170 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1171 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1172 stub_tables_(), local_symbol_is_thumb_function_(),
1173 attributes_section_data_(NULL
), mapping_symbols_info_(),
1174 section_has_cortex_a8_workaround_(NULL
)
1178 { delete this->attributes_section_data_
; }
1180 // Return the stub table of the SHNDX-th section if there is one.
1181 Stub_table
<big_endian
>*
1182 stub_table(unsigned int shndx
) const
1184 gold_assert(shndx
< this->stub_tables_
.size());
1185 return this->stub_tables_
[shndx
];
1188 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1190 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1192 gold_assert(shndx
< this->stub_tables_
.size());
1193 this->stub_tables_
[shndx
] = stub_table
;
1196 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1197 // index. This is only valid after do_count_local_symbol is called.
1199 local_symbol_is_thumb_function(unsigned int r_sym
) const
1201 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1202 return this->local_symbol_is_thumb_function_
[r_sym
];
1205 // Scan all relocation sections for stub generation.
1207 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1210 // Convert regular input section with index SHNDX to a relaxed section.
1212 convert_input_section_to_relaxed_section(unsigned shndx
)
1214 // The stubs have relocations and we need to process them after writing
1215 // out the stubs. So relocation now must follow section write.
1216 this->invalidate_section_offset(shndx
);
1217 this->set_relocs_must_follow_section_writes();
1220 // Downcast a base pointer to an Arm_relobj pointer. This is
1221 // not type-safe but we only use Arm_relobj not the base class.
1222 static Arm_relobj
<big_endian
>*
1223 as_arm_relobj(Relobj
* relobj
)
1224 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1226 // Processor-specific flags in ELF file header. This is valid only after
1229 processor_specific_flags() const
1230 { return this->processor_specific_flags_
; }
1232 // Attribute section data This is the contents of the .ARM.attribute section
1234 const Attributes_section_data
*
1235 attributes_section_data() const
1236 { return this->attributes_section_data_
; }
1238 // Mapping symbol location.
1239 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1241 // Functor for STL container.
1242 struct Mapping_symbol_position_less
1245 operator()(const Mapping_symbol_position
& p1
,
1246 const Mapping_symbol_position
& p2
) const
1248 return (p1
.first
< p2
.first
1249 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1253 // We only care about the first character of a mapping symbol, so
1254 // we only store that instead of the whole symbol name.
1255 typedef std::map
<Mapping_symbol_position
, char,
1256 Mapping_symbol_position_less
> Mapping_symbols_info
;
1258 // Whether a section contains any Cortex-A8 workaround.
1260 section_has_cortex_a8_workaround(unsigned int shndx
) const
1262 return (this->section_has_cortex_a8_workaround_
!= NULL
1263 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1266 // Mark a section that has Cortex-A8 workaround.
1268 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1270 if (this->section_has_cortex_a8_workaround_
== NULL
)
1271 this->section_has_cortex_a8_workaround_
=
1272 new std::vector
<bool>(this->shnum(), false);
1273 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1277 // Post constructor setup.
1281 // Call parent's setup method.
1282 Sized_relobj
<32, big_endian
>::do_setup();
1284 // Initialize look-up tables.
1285 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1286 this->stub_tables_
.swap(empty_stub_table_list
);
1289 // Count the local symbols.
1291 do_count_local_symbols(Stringpool_template
<char>*,
1292 Stringpool_template
<char>*);
1295 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1296 const unsigned char* pshdrs
,
1297 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1299 // Read the symbol information.
1301 do_read_symbols(Read_symbols_data
* sd
);
1303 // Process relocs for garbage collection.
1305 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1309 // Whether a section needs to be scanned for relocation stubs.
1311 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1312 const Relobj::Output_sections
&,
1313 const Symbol_table
*);
1315 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1317 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1318 unsigned int, Output_section
*,
1319 const Symbol_table
*);
1321 // Scan a section for the Cortex-A8 erratum.
1323 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1324 unsigned int, Output_section
*,
1325 Target_arm
<big_endian
>*);
1327 // List of stub tables.
1328 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1329 Stub_table_list stub_tables_
;
1330 // Bit vector to tell if a local symbol is a thumb function or not.
1331 // This is only valid after do_count_local_symbol is called.
1332 std::vector
<bool> local_symbol_is_thumb_function_
;
1333 // processor-specific flags in ELF file header.
1334 elfcpp::Elf_Word processor_specific_flags_
;
1335 // Object attributes if there is an .ARM.attributes section or NULL.
1336 Attributes_section_data
* attributes_section_data_
;
1337 // Mapping symbols information.
1338 Mapping_symbols_info mapping_symbols_info_
;
1339 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1340 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1343 // Arm_dynobj class.
1345 template<bool big_endian
>
1346 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1349 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1350 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1351 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1352 processor_specific_flags_(0), attributes_section_data_(NULL
)
1356 { delete this->attributes_section_data_
; }
1358 // Downcast a base pointer to an Arm_relobj pointer. This is
1359 // not type-safe but we only use Arm_relobj not the base class.
1360 static Arm_dynobj
<big_endian
>*
1361 as_arm_dynobj(Dynobj
* dynobj
)
1362 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1364 // Processor-specific flags in ELF file header. This is valid only after
1367 processor_specific_flags() const
1368 { return this->processor_specific_flags_
; }
1370 // Attributes section data.
1371 const Attributes_section_data
*
1372 attributes_section_data() const
1373 { return this->attributes_section_data_
; }
1376 // Read the symbol information.
1378 do_read_symbols(Read_symbols_data
* sd
);
1381 // processor-specific flags in ELF file header.
1382 elfcpp::Elf_Word processor_specific_flags_
;
1383 // Object attributes if there is an .ARM.attributes section or NULL.
1384 Attributes_section_data
* attributes_section_data_
;
1387 // Functor to read reloc addends during stub generation.
1389 template<int sh_type
, bool big_endian
>
1390 struct Stub_addend_reader
1392 // Return the addend for a relocation of a particular type. Depending
1393 // on whether this is a REL or RELA relocation, read the addend from a
1394 // view or from a Reloc object.
1395 elfcpp::Elf_types
<32>::Elf_Swxword
1397 unsigned int /* r_type */,
1398 const unsigned char* /* view */,
1399 const typename Reloc_types
<sh_type
,
1400 32, big_endian
>::Reloc
& /* reloc */) const;
1403 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1405 template<bool big_endian
>
1406 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1408 elfcpp::Elf_types
<32>::Elf_Swxword
1411 const unsigned char*,
1412 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1415 // Specialized Stub_addend_reader for RELA type relocation sections.
1416 // We currently do not handle RELA type relocation sections but it is trivial
1417 // to implement the addend reader. This is provided for completeness and to
1418 // make it easier to add support for RELA relocation sections in the future.
1420 template<bool big_endian
>
1421 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1423 elfcpp::Elf_types
<32>::Elf_Swxword
1426 const unsigned char*,
1427 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1428 big_endian
>::Reloc
& reloc
) const
1429 { return reloc
.get_r_addend(); }
1432 // Cortex_a8_reloc class. We keep record of relocation that may need
1433 // the Cortex-A8 erratum workaround.
1435 class Cortex_a8_reloc
1438 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1439 Arm_address destination
)
1440 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1446 // Accessors: This is a read-only class.
1448 // Return the relocation stub associated with this relocation if there is
1452 { return this->reloc_stub_
; }
1454 // Return the relocation type.
1457 { return this->r_type_
; }
1459 // Return the destination address of the relocation. LSB stores the THUMB
1463 { return this->destination_
; }
1466 // Associated relocation stub if there is one, or NULL.
1467 const Reloc_stub
* reloc_stub_
;
1469 unsigned int r_type_
;
1470 // Destination address of this relocation. LSB is used to distinguish
1472 Arm_address destination_
;
1475 // Utilities for manipulating integers of up to 32-bits
1479 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1480 // an int32_t. NO_BITS must be between 1 to 32.
1481 template<int no_bits
>
1482 static inline int32_t
1483 sign_extend(uint32_t bits
)
1485 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1487 return static_cast<int32_t>(bits
);
1488 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1490 uint32_t top_bit
= 1U << (no_bits
- 1);
1491 int32_t as_signed
= static_cast<int32_t>(bits
);
1492 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1495 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1496 template<int no_bits
>
1498 has_overflow(uint32_t bits
)
1500 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1503 int32_t max
= (1 << (no_bits
- 1)) - 1;
1504 int32_t min
= -(1 << (no_bits
- 1));
1505 int32_t as_signed
= static_cast<int32_t>(bits
);
1506 return as_signed
> max
|| as_signed
< min
;
1509 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1510 // fits in the given number of bits as either a signed or unsigned value.
1511 // For example, has_signed_unsigned_overflow<8> would check
1512 // -128 <= bits <= 255
1513 template<int no_bits
>
1515 has_signed_unsigned_overflow(uint32_t bits
)
1517 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1520 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1521 int32_t min
= -(1 << (no_bits
- 1));
1522 int32_t as_signed
= static_cast<int32_t>(bits
);
1523 return as_signed
> max
|| as_signed
< min
;
1526 // Select bits from A and B using bits in MASK. For each n in [0..31],
1527 // the n-th bit in the result is chosen from the n-th bits of A and B.
1528 // A zero selects A and a one selects B.
1529 static inline uint32_t
1530 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1531 { return (a
& ~mask
) | (b
& mask
); }
1534 template<bool big_endian
>
1535 class Target_arm
: public Sized_target
<32, big_endian
>
1538 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1541 // When were are relocating a stub, we pass this as the relocation number.
1542 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1545 : Sized_target
<32, big_endian
>(&arm_info
),
1546 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1547 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1548 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1549 should_force_pic_veneer_(false), arm_input_section_map_(),
1550 attributes_section_data_(NULL
), fix_cortex_a8_(false),
1551 cortex_a8_relocs_info_(), fix_v4bx_(0)
1554 // Whether we can use BLX.
1557 { return this->may_use_blx_
; }
1559 // Set use-BLX flag.
1561 set_may_use_blx(bool value
)
1562 { this->may_use_blx_
= value
; }
1564 // Whether we force PCI branch veneers.
1566 should_force_pic_veneer() const
1567 { return this->should_force_pic_veneer_
; }
1569 // Set PIC veneer flag.
1571 set_should_force_pic_veneer(bool value
)
1572 { this->should_force_pic_veneer_
= value
; }
1574 // Whether we use THUMB-2 instructions.
1576 using_thumb2() const
1578 Object_attribute
* attr
=
1579 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1580 int arch
= attr
->int_value();
1581 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1584 // Whether we use THUMB/THUMB-2 instructions only.
1586 using_thumb_only() const
1588 Object_attribute
* attr
=
1589 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1590 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1591 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1593 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1594 return attr
->int_value() == 'M';
1597 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1599 may_use_arm_nop() const
1601 Object_attribute
* attr
=
1602 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1603 int arch
= attr
->int_value();
1604 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1605 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1606 || arch
== elfcpp::TAG_CPU_ARCH_V7
1607 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1610 // Whether we have THUMB-2 NOP.W instruction.
1612 may_use_thumb2_nop() const
1614 Object_attribute
* attr
=
1615 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1616 int arch
= attr
->int_value();
1617 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1618 || arch
== elfcpp::TAG_CPU_ARCH_V7
1619 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1622 // Process the relocations to determine unreferenced sections for
1623 // garbage collection.
1625 gc_process_relocs(Symbol_table
* symtab
,
1627 Sized_relobj
<32, big_endian
>* object
,
1628 unsigned int data_shndx
,
1629 unsigned int sh_type
,
1630 const unsigned char* prelocs
,
1632 Output_section
* output_section
,
1633 bool needs_special_offset_handling
,
1634 size_t local_symbol_count
,
1635 const unsigned char* plocal_symbols
);
1637 // Scan the relocations to look for symbol adjustments.
1639 scan_relocs(Symbol_table
* symtab
,
1641 Sized_relobj
<32, big_endian
>* object
,
1642 unsigned int data_shndx
,
1643 unsigned int sh_type
,
1644 const unsigned char* prelocs
,
1646 Output_section
* output_section
,
1647 bool needs_special_offset_handling
,
1648 size_t local_symbol_count
,
1649 const unsigned char* plocal_symbols
);
1651 // Finalize the sections.
1653 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1655 // Return the value to use for a dynamic symbol which requires special
1658 do_dynsym_value(const Symbol
*) const;
1660 // Relocate a section.
1662 relocate_section(const Relocate_info
<32, big_endian
>*,
1663 unsigned int sh_type
,
1664 const unsigned char* prelocs
,
1666 Output_section
* output_section
,
1667 bool needs_special_offset_handling
,
1668 unsigned char* view
,
1669 Arm_address view_address
,
1670 section_size_type view_size
,
1671 const Reloc_symbol_changes
*);
1673 // Scan the relocs during a relocatable link.
1675 scan_relocatable_relocs(Symbol_table
* symtab
,
1677 Sized_relobj
<32, big_endian
>* object
,
1678 unsigned int data_shndx
,
1679 unsigned int sh_type
,
1680 const unsigned char* prelocs
,
1682 Output_section
* output_section
,
1683 bool needs_special_offset_handling
,
1684 size_t local_symbol_count
,
1685 const unsigned char* plocal_symbols
,
1686 Relocatable_relocs
*);
1688 // Relocate a section during a relocatable link.
1690 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1691 unsigned int sh_type
,
1692 const unsigned char* prelocs
,
1694 Output_section
* output_section
,
1695 off_t offset_in_output_section
,
1696 const Relocatable_relocs
*,
1697 unsigned char* view
,
1698 Arm_address view_address
,
1699 section_size_type view_size
,
1700 unsigned char* reloc_view
,
1701 section_size_type reloc_view_size
);
1703 // Return whether SYM is defined by the ABI.
1705 do_is_defined_by_abi(Symbol
* sym
) const
1706 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1708 // Return the size of the GOT section.
1712 gold_assert(this->got_
!= NULL
);
1713 return this->got_
->data_size();
1716 // Map platform-specific reloc types
1718 get_real_reloc_type (unsigned int r_type
);
1721 // Methods to support stub-generations.
1724 // Return the stub factory
1726 stub_factory() const
1727 { return this->stub_factory_
; }
1729 // Make a new Arm_input_section object.
1730 Arm_input_section
<big_endian
>*
1731 new_arm_input_section(Relobj
*, unsigned int);
1733 // Find the Arm_input_section object corresponding to the SHNDX-th input
1734 // section of RELOBJ.
1735 Arm_input_section
<big_endian
>*
1736 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1738 // Make a new Stub_table
1739 Stub_table
<big_endian
>*
1740 new_stub_table(Arm_input_section
<big_endian
>*);
1742 // Scan a section for stub generation.
1744 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1745 const unsigned char*, size_t, Output_section
*,
1746 bool, const unsigned char*, Arm_address
,
1751 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1752 Output_section
*, unsigned char*, Arm_address
,
1755 // Get the default ARM target.
1756 static Target_arm
<big_endian
>*
1759 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1760 && parameters
->target().is_big_endian() == big_endian
);
1761 return static_cast<Target_arm
<big_endian
>*>(
1762 parameters
->sized_target
<32, big_endian
>());
1765 // Whether relocation type uses LSB to distinguish THUMB addresses.
1767 reloc_uses_thumb_bit(unsigned int r_type
);
1769 // Whether NAME belongs to a mapping symbol.
1771 is_mapping_symbol_name(const char* name
)
1775 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
1776 && (name
[2] == '\0' || name
[2] == '.'));
1779 // Whether we work around the Cortex-A8 erratum.
1781 fix_cortex_a8() const
1782 { return this->fix_cortex_a8_
; }
1784 // Whether we fix R_ARM_V4BX relocation.
1786 // 1 - replace with MOV instruction (armv4 target)
1787 // 2 - make interworking veneer (>= armv4t targets only)
1790 { return this->fix_v4bx_
; }
1792 // Scan a span of THUMB code section for Cortex-A8 erratum.
1794 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
1795 section_size_type
, section_size_type
,
1796 const unsigned char*, Arm_address
);
1798 // Apply Cortex-A8 workaround to a branch.
1800 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
1801 unsigned char*, Arm_address
);
1804 // Make an ELF object.
1806 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1807 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1810 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1811 const elfcpp::Ehdr
<32, !big_endian
>&)
1812 { gold_unreachable(); }
1815 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1816 const elfcpp::Ehdr
<64, false>&)
1817 { gold_unreachable(); }
1820 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1821 const elfcpp::Ehdr
<64, true>&)
1822 { gold_unreachable(); }
1824 // Make an output section.
1826 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1827 elfcpp::Elf_Xword flags
)
1828 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1831 do_adjust_elf_header(unsigned char* view
, int len
) const;
1833 // We only need to generate stubs, and hence perform relaxation if we are
1834 // not doing relocatable linking.
1836 do_may_relax() const
1837 { return !parameters
->options().relocatable(); }
1840 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1842 // Determine whether an object attribute tag takes an integer, a
1845 do_attribute_arg_type(int tag
) const;
1847 // Reorder tags during output.
1849 do_attributes_order(int num
) const;
1852 // The class which scans relocations.
1857 : issued_non_pic_error_(false)
1861 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1862 Sized_relobj
<32, big_endian
>* object
,
1863 unsigned int data_shndx
,
1864 Output_section
* output_section
,
1865 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1866 const elfcpp::Sym
<32, big_endian
>& lsym
);
1869 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1870 Sized_relobj
<32, big_endian
>* object
,
1871 unsigned int data_shndx
,
1872 Output_section
* output_section
,
1873 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1878 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1879 unsigned int r_type
);
1882 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1883 unsigned int r_type
, Symbol
*);
1886 check_non_pic(Relobj
*, unsigned int r_type
);
1888 // Almost identical to Symbol::needs_plt_entry except that it also
1889 // handles STT_ARM_TFUNC.
1891 symbol_needs_plt_entry(const Symbol
* sym
)
1893 // An undefined symbol from an executable does not need a PLT entry.
1894 if (sym
->is_undefined() && !parameters
->options().shared())
1897 return (!parameters
->doing_static_link()
1898 && (sym
->type() == elfcpp::STT_FUNC
1899 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1900 && (sym
->is_from_dynobj()
1901 || sym
->is_undefined()
1902 || sym
->is_preemptible()));
1905 // Whether we have issued an error about a non-PIC compilation.
1906 bool issued_non_pic_error_
;
1909 // The class which implements relocation.
1919 // Return whether the static relocation needs to be applied.
1921 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1924 Output_section
* output_section
);
1926 // Do a relocation. Return false if the caller should not issue
1927 // any warnings about this relocation.
1929 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1930 Output_section
*, size_t relnum
,
1931 const elfcpp::Rel
<32, big_endian
>&,
1932 unsigned int r_type
, const Sized_symbol
<32>*,
1933 const Symbol_value
<32>*,
1934 unsigned char*, Arm_address
,
1937 // Return whether we want to pass flag NON_PIC_REF for this
1938 // reloc. This means the relocation type accesses a symbol not via
1941 reloc_is_non_pic (unsigned int r_type
)
1945 // These relocation types reference GOT or PLT entries explicitly.
1946 case elfcpp::R_ARM_GOT_BREL
:
1947 case elfcpp::R_ARM_GOT_ABS
:
1948 case elfcpp::R_ARM_GOT_PREL
:
1949 case elfcpp::R_ARM_GOT_BREL12
:
1950 case elfcpp::R_ARM_PLT32_ABS
:
1951 case elfcpp::R_ARM_TLS_GD32
:
1952 case elfcpp::R_ARM_TLS_LDM32
:
1953 case elfcpp::R_ARM_TLS_IE32
:
1954 case elfcpp::R_ARM_TLS_IE12GP
:
1956 // These relocate types may use PLT entries.
1957 case elfcpp::R_ARM_CALL
:
1958 case elfcpp::R_ARM_THM_CALL
:
1959 case elfcpp::R_ARM_JUMP24
:
1960 case elfcpp::R_ARM_THM_JUMP24
:
1961 case elfcpp::R_ARM_THM_JUMP19
:
1962 case elfcpp::R_ARM_PLT32
:
1963 case elfcpp::R_ARM_THM_XPC22
:
1972 // A class which returns the size required for a relocation type,
1973 // used while scanning relocs during a relocatable link.
1974 class Relocatable_size_for_reloc
1978 get_size_for_reloc(unsigned int, Relobj
*);
1981 // Get the GOT section, creating it if necessary.
1982 Output_data_got
<32, big_endian
>*
1983 got_section(Symbol_table
*, Layout
*);
1985 // Get the GOT PLT section.
1987 got_plt_section() const
1989 gold_assert(this->got_plt_
!= NULL
);
1990 return this->got_plt_
;
1993 // Create a PLT entry for a global symbol.
1995 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1997 // Get the PLT section.
1998 const Output_data_plt_arm
<big_endian
>*
2001 gold_assert(this->plt_
!= NULL
);
2005 // Get the dynamic reloc section, creating it if necessary.
2007 rel_dyn_section(Layout
*);
2009 // Return true if the symbol may need a COPY relocation.
2010 // References from an executable object to non-function symbols
2011 // defined in a dynamic object may need a COPY relocation.
2013 may_need_copy_reloc(Symbol
* gsym
)
2015 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
2016 && gsym
->may_need_copy_reloc());
2019 // Add a potential copy relocation.
2021 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
2022 Sized_relobj
<32, big_endian
>* object
,
2023 unsigned int shndx
, Output_section
* output_section
,
2024 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
2026 this->copy_relocs_
.copy_reloc(symtab
, layout
,
2027 symtab
->get_sized_symbol
<32>(sym
),
2028 object
, shndx
, output_section
, reloc
,
2029 this->rel_dyn_section(layout
));
2032 // Whether two EABI versions are compatible.
2034 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
2036 // Merge processor-specific flags from input object and those in the ELF
2037 // header of the output.
2039 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
2041 // Get the secondary compatible architecture.
2043 get_secondary_compatible_arch(const Attributes_section_data
*);
2045 // Set the secondary compatible architecture.
2047 set_secondary_compatible_arch(Attributes_section_data
*, int);
2050 tag_cpu_arch_combine(const char*, int, int*, int, int);
2052 // Helper to print AEABI enum tag value.
2054 aeabi_enum_name(unsigned int);
2056 // Return string value for TAG_CPU_name.
2058 tag_cpu_name_value(unsigned int);
2060 // Merge object attributes from input object and those in the output.
2062 merge_object_attributes(const char*, const Attributes_section_data
*);
2064 // Helper to get an AEABI object attribute
2066 get_aeabi_object_attribute(int tag
) const
2068 Attributes_section_data
* pasd
= this->attributes_section_data_
;
2069 gold_assert(pasd
!= NULL
);
2070 Object_attribute
* attr
=
2071 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
2072 gold_assert(attr
!= NULL
);
2077 // Methods to support stub-generations.
2080 // Group input sections for stub generation.
2082 group_sections(Layout
*, section_size_type
, bool);
2084 // Scan a relocation for stub generation.
2086 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
2087 const Sized_symbol
<32>*, unsigned int,
2088 const Symbol_value
<32>*,
2089 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
2091 // Scan a relocation section for stub.
2092 template<int sh_type
>
2094 scan_reloc_section_for_stubs(
2095 const Relocate_info
<32, big_endian
>* relinfo
,
2096 const unsigned char* prelocs
,
2098 Output_section
* output_section
,
2099 bool needs_special_offset_handling
,
2100 const unsigned char* view
,
2101 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2104 // Information about this specific target which we pass to the
2105 // general Target structure.
2106 static const Target::Target_info arm_info
;
2108 // The types of GOT entries needed for this platform.
2111 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
2114 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2116 // Map input section to Arm_input_section.
2117 typedef Unordered_map
<Input_section_specifier
,
2118 Arm_input_section
<big_endian
>*,
2119 Input_section_specifier::hash
,
2120 Input_section_specifier::equal_to
>
2121 Arm_input_section_map
;
2123 // Map output addresses to relocs for Cortex-A8 erratum.
2124 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2125 Cortex_a8_relocs_info
;
2128 Output_data_got
<32, big_endian
>* got_
;
2130 Output_data_plt_arm
<big_endian
>* plt_
;
2131 // The GOT PLT section.
2132 Output_data_space
* got_plt_
;
2133 // The dynamic reloc section.
2134 Reloc_section
* rel_dyn_
;
2135 // Relocs saved to avoid a COPY reloc.
2136 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2137 // Space for variables copied with a COPY reloc.
2138 Output_data_space
* dynbss_
;
2139 // Vector of Stub_tables created.
2140 Stub_table_list stub_tables_
;
2142 const Stub_factory
&stub_factory_
;
2143 // Whether we can use BLX.
2145 // Whether we force PIC branch veneers.
2146 bool should_force_pic_veneer_
;
2147 // Map for locating Arm_input_sections.
2148 Arm_input_section_map arm_input_section_map_
;
2149 // Attributes section data in output.
2150 Attributes_section_data
* attributes_section_data_
;
2151 // Whether we want to fix code for Cortex-A8 erratum.
2152 bool fix_cortex_a8_
;
2153 // Map addresses to relocs for Cortex-A8 erratum.
2154 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2155 // Whether we need to fix code for V4BX relocations.
2159 template<bool big_endian
>
2160 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2163 big_endian
, // is_big_endian
2164 elfcpp::EM_ARM
, // machine_code
2165 false, // has_make_symbol
2166 false, // has_resolve
2167 false, // has_code_fill
2168 true, // is_default_stack_executable
2170 "/usr/lib/libc.so.1", // dynamic_linker
2171 0x8000, // default_text_segment_address
2172 0x1000, // abi_pagesize (overridable by -z max-page-size)
2173 0x1000, // common_pagesize (overridable by -z common-page-size)
2174 elfcpp::SHN_UNDEF
, // small_common_shndx
2175 elfcpp::SHN_UNDEF
, // large_common_shndx
2176 0, // small_common_section_flags
2177 0, // large_common_section_flags
2178 ".ARM.attributes", // attributes_section
2179 "aeabi" // attributes_vendor
2182 // Arm relocate functions class
2185 template<bool big_endian
>
2186 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2191 STATUS_OKAY
, // No error during relocation.
2192 STATUS_OVERFLOW
, // Relocation oveflow.
2193 STATUS_BAD_RELOC
// Relocation cannot be applied.
2197 typedef Relocate_functions
<32, big_endian
> Base
;
2198 typedef Arm_relocate_functions
<big_endian
> This
;
2200 // Encoding of imm16 argument for movt and movw ARM instructions
2203 // imm16 := imm4 | imm12
2205 // 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
2206 // +-------+---------------+-------+-------+-----------------------+
2207 // | | |imm4 | |imm12 |
2208 // +-------+---------------+-------+-------+-----------------------+
2210 // Extract the relocation addend from VAL based on the ARM
2211 // instruction encoding described above.
2212 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2213 extract_arm_movw_movt_addend(
2214 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2216 // According to the Elf ABI for ARM Architecture the immediate
2217 // field is sign-extended to form the addend.
2218 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2221 // Insert X into VAL based on the ARM instruction encoding described
2223 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2224 insert_val_arm_movw_movt(
2225 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2226 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2230 val
|= (x
& 0xf000) << 4;
2234 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2237 // imm16 := imm4 | i | imm3 | imm8
2239 // 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
2240 // +---------+-+-----------+-------++-+-----+-------+---------------+
2241 // | |i| |imm4 || |imm3 | |imm8 |
2242 // +---------+-+-----------+-------++-+-----+-------+---------------+
2244 // Extract the relocation addend from VAL based on the Thumb2
2245 // instruction encoding described above.
2246 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2247 extract_thumb_movw_movt_addend(
2248 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2250 // According to the Elf ABI for ARM Architecture the immediate
2251 // field is sign-extended to form the addend.
2252 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2253 | ((val
>> 15) & 0x0800)
2254 | ((val
>> 4) & 0x0700)
2258 // Insert X into VAL based on the Thumb2 instruction encoding
2260 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2261 insert_val_thumb_movw_movt(
2262 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2263 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2266 val
|= (x
& 0xf000) << 4;
2267 val
|= (x
& 0x0800) << 15;
2268 val
|= (x
& 0x0700) << 4;
2269 val
|= (x
& 0x00ff);
2273 // Handle ARM long branches.
2274 static typename
This::Status
2275 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2276 unsigned char *, const Sized_symbol
<32>*,
2277 const Arm_relobj
<big_endian
>*, unsigned int,
2278 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2280 // Handle THUMB long branches.
2281 static typename
This::Status
2282 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2283 unsigned char *, const Sized_symbol
<32>*,
2284 const Arm_relobj
<big_endian
>*, unsigned int,
2285 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2289 // Return the branch offset of a 32-bit THUMB branch.
2290 static inline int32_t
2291 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2293 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2294 // involving the J1 and J2 bits.
2295 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2296 uint32_t upper
= upper_insn
& 0x3ffU
;
2297 uint32_t lower
= lower_insn
& 0x7ffU
;
2298 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2299 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2300 uint32_t i1
= j1
^ s
? 0 : 1;
2301 uint32_t i2
= j2
^ s
? 0 : 1;
2303 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2304 | (upper
<< 12) | (lower
<< 1));
2307 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2308 // UPPER_INSN is the original upper instruction of the branch. Caller is
2309 // responsible for overflow checking and BLX offset adjustment.
2310 static inline uint16_t
2311 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2313 uint32_t s
= offset
< 0 ? 1 : 0;
2314 uint32_t bits
= static_cast<uint32_t>(offset
);
2315 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2318 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2319 // LOWER_INSN is the original lower instruction of the branch. Caller is
2320 // responsible for overflow checking and BLX offset adjustment.
2321 static inline uint16_t
2322 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2324 uint32_t s
= offset
< 0 ? 1 : 0;
2325 uint32_t bits
= static_cast<uint32_t>(offset
);
2326 return ((lower_insn
& ~0x2fffU
)
2327 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2328 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2329 | ((bits
>> 1) & 0x7ffU
));
2332 // Return the branch offset of a 32-bit THUMB conditional branch.
2333 static inline int32_t
2334 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2336 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2337 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2338 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2339 uint32_t lower
= (lower_insn
& 0x07ffU
);
2340 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2342 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2345 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2346 // instruction. UPPER_INSN is the original upper instruction of the branch.
2347 // Caller is responsible for overflow checking.
2348 static inline uint16_t
2349 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2351 uint32_t s
= offset
< 0 ? 1 : 0;
2352 uint32_t bits
= static_cast<uint32_t>(offset
);
2353 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2356 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2357 // instruction. LOWER_INSN is the original lower instruction of the branch.
2358 // Caller is reponsible for overflow checking.
2359 static inline uint16_t
2360 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2362 uint32_t bits
= static_cast<uint32_t>(offset
);
2363 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2364 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2365 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2367 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
2370 // R_ARM_ABS8: S + A
2371 static inline typename
This::Status
2372 abs8(unsigned char *view
,
2373 const Sized_relobj
<32, big_endian
>* object
,
2374 const Symbol_value
<32>* psymval
)
2376 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2377 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2378 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2379 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2380 Reltype addend
= utils::sign_extend
<8>(val
);
2381 Reltype x
= psymval
->value(object
, addend
);
2382 val
= utils::bit_select(val
, x
, 0xffU
);
2383 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2384 return (utils::has_signed_unsigned_overflow
<8>(x
)
2385 ? This::STATUS_OVERFLOW
2386 : This::STATUS_OKAY
);
2389 // R_ARM_THM_ABS5: S + A
2390 static inline typename
This::Status
2391 thm_abs5(unsigned char *view
,
2392 const Sized_relobj
<32, big_endian
>* object
,
2393 const Symbol_value
<32>* psymval
)
2395 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2396 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2397 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2398 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2399 Reltype addend
= (val
& 0x7e0U
) >> 6;
2400 Reltype x
= psymval
->value(object
, addend
);
2401 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2402 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2403 return (utils::has_overflow
<5>(x
)
2404 ? This::STATUS_OVERFLOW
2405 : This::STATUS_OKAY
);
2408 // R_ARM_ABS12: S + A
2409 static inline typename
This::Status
2410 abs12(unsigned char *view
,
2411 const Sized_relobj
<32, big_endian
>* object
,
2412 const Symbol_value
<32>* psymval
)
2414 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2415 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2416 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2417 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2418 Reltype addend
= val
& 0x0fffU
;
2419 Reltype x
= psymval
->value(object
, addend
);
2420 val
= utils::bit_select(val
, x
, 0x0fffU
);
2421 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2422 return (utils::has_overflow
<12>(x
)
2423 ? This::STATUS_OVERFLOW
2424 : This::STATUS_OKAY
);
2427 // R_ARM_ABS16: S + A
2428 static inline typename
This::Status
2429 abs16(unsigned char *view
,
2430 const Sized_relobj
<32, big_endian
>* object
,
2431 const Symbol_value
<32>* psymval
)
2433 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2434 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2435 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2436 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2437 Reltype addend
= utils::sign_extend
<16>(val
);
2438 Reltype x
= psymval
->value(object
, addend
);
2439 val
= utils::bit_select(val
, x
, 0xffffU
);
2440 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2441 return (utils::has_signed_unsigned_overflow
<16>(x
)
2442 ? This::STATUS_OVERFLOW
2443 : This::STATUS_OKAY
);
2446 // R_ARM_ABS32: (S + A) | T
2447 static inline typename
This::Status
2448 abs32(unsigned char *view
,
2449 const Sized_relobj
<32, big_endian
>* object
,
2450 const Symbol_value
<32>* psymval
,
2451 Arm_address thumb_bit
)
2453 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2454 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2455 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2456 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2457 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2458 return This::STATUS_OKAY
;
2461 // R_ARM_REL32: (S + A) | T - P
2462 static inline typename
This::Status
2463 rel32(unsigned char *view
,
2464 const Sized_relobj
<32, big_endian
>* object
,
2465 const Symbol_value
<32>* psymval
,
2466 Arm_address address
,
2467 Arm_address thumb_bit
)
2469 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2470 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2471 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2472 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2473 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2474 return This::STATUS_OKAY
;
2477 // R_ARM_THM_CALL: (S + A) | T - P
2478 static inline typename
This::Status
2479 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2480 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2481 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2482 Arm_address address
, Arm_address thumb_bit
,
2483 bool is_weakly_undefined_without_plt
)
2485 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2486 object
, r_sym
, psymval
, address
, thumb_bit
,
2487 is_weakly_undefined_without_plt
);
2490 // R_ARM_THM_JUMP24: (S + A) | T - P
2491 static inline typename
This::Status
2492 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2493 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2494 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2495 Arm_address address
, Arm_address thumb_bit
,
2496 bool is_weakly_undefined_without_plt
)
2498 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2499 object
, r_sym
, psymval
, address
, thumb_bit
,
2500 is_weakly_undefined_without_plt
);
2503 // R_ARM_THM_JUMP24: (S + A) | T - P
2504 static typename
This::Status
2505 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
2506 const Symbol_value
<32>* psymval
, Arm_address address
,
2507 Arm_address thumb_bit
);
2509 // R_ARM_THM_XPC22: (S + A) | T - P
2510 static inline typename
This::Status
2511 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2512 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2513 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2514 Arm_address address
, Arm_address thumb_bit
,
2515 bool is_weakly_undefined_without_plt
)
2517 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2518 object
, r_sym
, psymval
, address
, thumb_bit
,
2519 is_weakly_undefined_without_plt
);
2522 // R_ARM_THM_JUMP6: S + A – P
2523 static inline typename
This::Status
2524 thm_jump6(unsigned char *view
,
2525 const Sized_relobj
<32, big_endian
>* object
,
2526 const Symbol_value
<32>* psymval
,
2527 Arm_address address
)
2529 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2530 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2531 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2532 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2533 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2534 Reltype addend
= (((val
& 0x0200) >> 3) | ((val
& 0x00f8) >> 2));
2535 Reltype x
= (psymval
->value(object
, addend
) - address
);
2536 val
= (val
& 0xfd07) | ((x
& 0x0040) << 3) | ((val
& 0x003e) << 2);
2537 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2538 // CZB does only forward jumps.
2539 return ((x
> 0x007e)
2540 ? This::STATUS_OVERFLOW
2541 : This::STATUS_OKAY
);
2544 // R_ARM_THM_JUMP8: S + A – P
2545 static inline typename
This::Status
2546 thm_jump8(unsigned char *view
,
2547 const Sized_relobj
<32, big_endian
>* object
,
2548 const Symbol_value
<32>* psymval
,
2549 Arm_address address
)
2551 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2552 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2553 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2554 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2555 Reltype addend
= utils::sign_extend
<8>((val
& 0x00ff) << 1);
2556 Reltype x
= (psymval
->value(object
, addend
) - address
);
2557 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xff00) | ((x
& 0x01fe) >> 1));
2558 return (utils::has_overflow
<8>(x
)
2559 ? This::STATUS_OVERFLOW
2560 : This::STATUS_OKAY
);
2563 // R_ARM_THM_JUMP11: S + A – P
2564 static inline typename
This::Status
2565 thm_jump11(unsigned char *view
,
2566 const Sized_relobj
<32, big_endian
>* object
,
2567 const Symbol_value
<32>* psymval
,
2568 Arm_address address
)
2570 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2571 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2572 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2573 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2574 Reltype addend
= utils::sign_extend
<11>((val
& 0x07ff) << 1);
2575 Reltype x
= (psymval
->value(object
, addend
) - address
);
2576 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xf800) | ((x
& 0x0ffe) >> 1));
2577 return (utils::has_overflow
<11>(x
)
2578 ? This::STATUS_OVERFLOW
2579 : This::STATUS_OKAY
);
2582 // R_ARM_BASE_PREL: B(S) + A - P
2583 static inline typename
This::Status
2584 base_prel(unsigned char* view
,
2586 Arm_address address
)
2588 Base::rel32(view
, origin
- address
);
2592 // R_ARM_BASE_ABS: B(S) + A
2593 static inline typename
This::Status
2594 base_abs(unsigned char* view
,
2597 Base::rel32(view
, origin
);
2601 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2602 static inline typename
This::Status
2603 got_brel(unsigned char* view
,
2604 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2606 Base::rel32(view
, got_offset
);
2607 return This::STATUS_OKAY
;
2610 // R_ARM_GOT_PREL: GOT(S) + A - P
2611 static inline typename
This::Status
2612 got_prel(unsigned char *view
,
2613 Arm_address got_entry
,
2614 Arm_address address
)
2616 Base::rel32(view
, got_entry
- address
);
2617 return This::STATUS_OKAY
;
2620 // R_ARM_PLT32: (S + A) | T - P
2621 static inline typename
This::Status
2622 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2623 unsigned char *view
,
2624 const Sized_symbol
<32>* gsym
,
2625 const Arm_relobj
<big_endian
>* object
,
2627 const Symbol_value
<32>* psymval
,
2628 Arm_address address
,
2629 Arm_address thumb_bit
,
2630 bool is_weakly_undefined_without_plt
)
2632 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2633 object
, r_sym
, psymval
, address
, thumb_bit
,
2634 is_weakly_undefined_without_plt
);
2637 // R_ARM_XPC25: (S + A) | T - P
2638 static inline typename
This::Status
2639 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2640 unsigned char *view
,
2641 const Sized_symbol
<32>* gsym
,
2642 const Arm_relobj
<big_endian
>* object
,
2644 const Symbol_value
<32>* psymval
,
2645 Arm_address address
,
2646 Arm_address thumb_bit
,
2647 bool is_weakly_undefined_without_plt
)
2649 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2650 object
, r_sym
, psymval
, address
, thumb_bit
,
2651 is_weakly_undefined_without_plt
);
2654 // R_ARM_CALL: (S + A) | T - P
2655 static inline typename
This::Status
2656 call(const Relocate_info
<32, big_endian
>* relinfo
,
2657 unsigned char *view
,
2658 const Sized_symbol
<32>* gsym
,
2659 const Arm_relobj
<big_endian
>* object
,
2661 const Symbol_value
<32>* psymval
,
2662 Arm_address address
,
2663 Arm_address thumb_bit
,
2664 bool is_weakly_undefined_without_plt
)
2666 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2667 object
, r_sym
, psymval
, address
, thumb_bit
,
2668 is_weakly_undefined_without_plt
);
2671 // R_ARM_JUMP24: (S + A) | T - P
2672 static inline typename
This::Status
2673 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2674 unsigned char *view
,
2675 const Sized_symbol
<32>* gsym
,
2676 const Arm_relobj
<big_endian
>* object
,
2678 const Symbol_value
<32>* psymval
,
2679 Arm_address address
,
2680 Arm_address thumb_bit
,
2681 bool is_weakly_undefined_without_plt
)
2683 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2684 object
, r_sym
, psymval
, address
, thumb_bit
,
2685 is_weakly_undefined_without_plt
);
2688 // R_ARM_PREL: (S + A) | T - P
2689 static inline typename
This::Status
2690 prel31(unsigned char *view
,
2691 const Sized_relobj
<32, big_endian
>* object
,
2692 const Symbol_value
<32>* psymval
,
2693 Arm_address address
,
2694 Arm_address thumb_bit
)
2696 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2697 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2698 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2699 Valtype addend
= utils::sign_extend
<31>(val
);
2700 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2701 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2702 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2703 return (utils::has_overflow
<31>(x
) ?
2704 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2707 // R_ARM_MOVW_ABS_NC: (S + A) | T
2708 static inline typename
This::Status
2709 movw_abs_nc(unsigned char *view
,
2710 const Sized_relobj
<32, big_endian
>* object
,
2711 const Symbol_value
<32>* psymval
,
2712 Arm_address thumb_bit
)
2714 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2715 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2716 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2717 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2718 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2719 val
= This::insert_val_arm_movw_movt(val
, x
);
2720 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2721 return This::STATUS_OKAY
;
2724 // R_ARM_MOVT_ABS: S + A
2725 static inline typename
This::Status
2726 movt_abs(unsigned char *view
,
2727 const Sized_relobj
<32, big_endian
>* object
,
2728 const Symbol_value
<32>* psymval
)
2730 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2731 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2732 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2733 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2734 Valtype x
= psymval
->value(object
, addend
) >> 16;
2735 val
= This::insert_val_arm_movw_movt(val
, x
);
2736 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2737 return This::STATUS_OKAY
;
2740 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2741 static inline typename
This::Status
2742 thm_movw_abs_nc(unsigned char *view
,
2743 const Sized_relobj
<32, big_endian
>* object
,
2744 const Symbol_value
<32>* psymval
,
2745 Arm_address thumb_bit
)
2747 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2748 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2749 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2750 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2751 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2752 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2753 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2754 val
= This::insert_val_thumb_movw_movt(val
, x
);
2755 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2756 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2757 return This::STATUS_OKAY
;
2760 // R_ARM_THM_MOVT_ABS: S + A
2761 static inline typename
This::Status
2762 thm_movt_abs(unsigned char *view
,
2763 const Sized_relobj
<32, big_endian
>* object
,
2764 const Symbol_value
<32>* psymval
)
2766 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2767 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2768 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2769 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2770 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2771 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2772 Reltype x
= psymval
->value(object
, addend
) >> 16;
2773 val
= This::insert_val_thumb_movw_movt(val
, x
);
2774 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2775 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2776 return This::STATUS_OKAY
;
2779 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2780 static inline typename
This::Status
2781 movw_prel_nc(unsigned char *view
,
2782 const Sized_relobj
<32, big_endian
>* object
,
2783 const Symbol_value
<32>* psymval
,
2784 Arm_address address
,
2785 Arm_address thumb_bit
)
2787 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2788 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2789 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2790 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2791 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2792 val
= This::insert_val_arm_movw_movt(val
, x
);
2793 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2794 return This::STATUS_OKAY
;
2797 // R_ARM_MOVT_PREL: S + A - P
2798 static inline typename
This::Status
2799 movt_prel(unsigned char *view
,
2800 const Sized_relobj
<32, big_endian
>* object
,
2801 const Symbol_value
<32>* psymval
,
2802 Arm_address address
)
2804 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2805 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2806 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2807 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2808 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2809 val
= This::insert_val_arm_movw_movt(val
, x
);
2810 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2811 return This::STATUS_OKAY
;
2814 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2815 static inline typename
This::Status
2816 thm_movw_prel_nc(unsigned char *view
,
2817 const Sized_relobj
<32, big_endian
>* object
,
2818 const Symbol_value
<32>* psymval
,
2819 Arm_address address
,
2820 Arm_address thumb_bit
)
2822 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2823 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2824 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2825 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2826 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2827 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2828 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2829 val
= This::insert_val_thumb_movw_movt(val
, x
);
2830 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2831 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2832 return This::STATUS_OKAY
;
2835 // R_ARM_THM_MOVT_PREL: S + A - P
2836 static inline typename
This::Status
2837 thm_movt_prel(unsigned char *view
,
2838 const Sized_relobj
<32, big_endian
>* object
,
2839 const Symbol_value
<32>* psymval
,
2840 Arm_address address
)
2842 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2843 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2844 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2845 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2846 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2847 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2848 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2849 val
= This::insert_val_thumb_movw_movt(val
, x
);
2850 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2851 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2852 return This::STATUS_OKAY
;
2856 static inline typename
This::Status
2857 v4bx(const Relocate_info
<32, big_endian
>* relinfo
,
2858 unsigned char *view
,
2859 const Arm_relobj
<big_endian
>* object
,
2860 const Arm_address address
,
2861 const bool is_interworking
)
2864 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2865 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2866 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2868 // Ensure that we have a BX instruction.
2869 gold_assert((val
& 0x0ffffff0) == 0x012fff10);
2870 const uint32_t reg
= (val
& 0xf);
2871 if (is_interworking
&& reg
!= 0xf)
2873 Stub_table
<big_endian
>* stub_table
=
2874 object
->stub_table(relinfo
->data_shndx
);
2875 gold_assert(stub_table
!= NULL
);
2877 Arm_v4bx_stub
* stub
= stub_table
->find_arm_v4bx_stub(reg
);
2878 gold_assert(stub
!= NULL
);
2880 int32_t veneer_address
=
2881 stub_table
->address() + stub
->offset() - 8 - address
;
2882 gold_assert((veneer_address
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2883 && (veneer_address
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2884 // Replace with a branch to veneer (B <addr>)
2885 val
= (val
& 0xf0000000) | 0x0a000000
2886 | ((veneer_address
>> 2) & 0x00ffffff);
2890 // Preserve Rm (lowest four bits) and the condition code
2891 // (highest four bits). Other bits encode MOV PC,Rm.
2892 val
= (val
& 0xf000000f) | 0x01a0f000;
2894 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2895 return This::STATUS_OKAY
;
2899 // Relocate ARM long branches. This handles relocation types
2900 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2901 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2902 // undefined and we do not use PLT in this relocation. In such a case,
2903 // the branch is converted into an NOP.
2905 template<bool big_endian
>
2906 typename Arm_relocate_functions
<big_endian
>::Status
2907 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2908 unsigned int r_type
,
2909 const Relocate_info
<32, big_endian
>* relinfo
,
2910 unsigned char *view
,
2911 const Sized_symbol
<32>* gsym
,
2912 const Arm_relobj
<big_endian
>* object
,
2914 const Symbol_value
<32>* psymval
,
2915 Arm_address address
,
2916 Arm_address thumb_bit
,
2917 bool is_weakly_undefined_without_plt
)
2919 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2920 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2921 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2923 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2924 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2925 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2926 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2927 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2928 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2929 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2931 // Check that the instruction is valid.
2932 if (r_type
== elfcpp::R_ARM_CALL
)
2934 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2935 return This::STATUS_BAD_RELOC
;
2937 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2939 if (!insn_is_b
&& !insn_is_cond_bl
)
2940 return This::STATUS_BAD_RELOC
;
2942 else if (r_type
== elfcpp::R_ARM_PLT32
)
2944 if (!insn_is_any_branch
)
2945 return This::STATUS_BAD_RELOC
;
2947 else if (r_type
== elfcpp::R_ARM_XPC25
)
2949 // FIXME: AAELF document IH0044C does not say much about it other
2950 // than it being obsolete.
2951 if (!insn_is_any_branch
)
2952 return This::STATUS_BAD_RELOC
;
2957 // A branch to an undefined weak symbol is turned into a jump to
2958 // the next instruction unless a PLT entry will be created.
2959 // Do the same for local undefined symbols.
2960 // The jump to the next instruction is optimized as a NOP depending
2961 // on the architecture.
2962 const Target_arm
<big_endian
>* arm_target
=
2963 Target_arm
<big_endian
>::default_target();
2964 if (is_weakly_undefined_without_plt
)
2966 Valtype cond
= val
& 0xf0000000U
;
2967 if (arm_target
->may_use_arm_nop())
2968 val
= cond
| 0x0320f000;
2970 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2971 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2972 return This::STATUS_OKAY
;
2975 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2976 Valtype branch_target
= psymval
->value(object
, addend
);
2977 int32_t branch_offset
= branch_target
- address
;
2979 // We need a stub if the branch offset is too large or if we need
2981 bool may_use_blx
= arm_target
->may_use_blx();
2982 Reloc_stub
* stub
= NULL
;
2983 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2984 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2985 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2987 Stub_type stub_type
=
2988 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2990 if (stub_type
!= arm_stub_none
)
2992 Stub_table
<big_endian
>* stub_table
=
2993 object
->stub_table(relinfo
->data_shndx
);
2994 gold_assert(stub_table
!= NULL
);
2996 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2997 stub
= stub_table
->find_reloc_stub(stub_key
);
2998 gold_assert(stub
!= NULL
);
2999 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3000 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3001 branch_offset
= branch_target
- address
;
3002 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3003 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3007 // At this point, if we still need to switch mode, the instruction
3008 // must either be a BLX or a BL that can be converted to a BLX.
3012 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
3013 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
3016 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
3017 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3018 return (utils::has_overflow
<26>(branch_offset
)
3019 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3022 // Relocate THUMB long branches. This handles relocation types
3023 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3024 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3025 // undefined and we do not use PLT in this relocation. In such a case,
3026 // the branch is converted into an NOP.
3028 template<bool big_endian
>
3029 typename Arm_relocate_functions
<big_endian
>::Status
3030 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
3031 unsigned int r_type
,
3032 const Relocate_info
<32, big_endian
>* relinfo
,
3033 unsigned char *view
,
3034 const Sized_symbol
<32>* gsym
,
3035 const Arm_relobj
<big_endian
>* object
,
3037 const Symbol_value
<32>* psymval
,
3038 Arm_address address
,
3039 Arm_address thumb_bit
,
3040 bool is_weakly_undefined_without_plt
)
3042 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3043 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3044 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3045 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3047 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3049 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
3050 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
3052 // Check that the instruction is valid.
3053 if (r_type
== elfcpp::R_ARM_THM_CALL
)
3055 if (!is_bl_insn
&& !is_blx_insn
)
3056 return This::STATUS_BAD_RELOC
;
3058 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
3060 // This cannot be a BLX.
3062 return This::STATUS_BAD_RELOC
;
3064 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
3066 // Check for Thumb to Thumb call.
3068 return This::STATUS_BAD_RELOC
;
3071 gold_warning(_("%s: Thumb BLX instruction targets "
3072 "thumb function '%s'."),
3073 object
->name().c_str(),
3074 (gsym
? gsym
->name() : "(local)"));
3075 // Convert BLX to BL.
3076 lower_insn
|= 0x1000U
;
3082 // A branch to an undefined weak symbol is turned into a jump to
3083 // the next instruction unless a PLT entry will be created.
3084 // The jump to the next instruction is optimized as a NOP.W for
3085 // Thumb-2 enabled architectures.
3086 const Target_arm
<big_endian
>* arm_target
=
3087 Target_arm
<big_endian
>::default_target();
3088 if (is_weakly_undefined_without_plt
)
3090 if (arm_target
->may_use_thumb2_nop())
3092 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
3093 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
3097 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
3098 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
3100 return This::STATUS_OKAY
;
3103 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
3104 Arm_address branch_target
= psymval
->value(object
, addend
);
3105 int32_t branch_offset
= branch_target
- address
;
3107 // We need a stub if the branch offset is too large or if we need
3109 bool may_use_blx
= arm_target
->may_use_blx();
3110 bool thumb2
= arm_target
->using_thumb2();
3112 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3113 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3115 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3116 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3117 || ((thumb_bit
== 0)
3118 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3119 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
3121 Stub_type stub_type
=
3122 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
3124 if (stub_type
!= arm_stub_none
)
3126 Stub_table
<big_endian
>* stub_table
=
3127 object
->stub_table(relinfo
->data_shndx
);
3128 gold_assert(stub_table
!= NULL
);
3130 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3131 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
3132 gold_assert(stub
!= NULL
);
3133 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3134 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3135 branch_offset
= branch_target
- address
;
3139 // At this point, if we still need to switch mode, the instruction
3140 // must either be a BLX or a BL that can be converted to a BLX.
3143 gold_assert(may_use_blx
3144 && (r_type
== elfcpp::R_ARM_THM_CALL
3145 || r_type
== elfcpp::R_ARM_THM_XPC22
));
3146 // Make sure this is a BLX.
3147 lower_insn
&= ~0x1000U
;
3151 // Make sure this is a BL.
3152 lower_insn
|= 0x1000U
;
3155 if ((lower_insn
& 0x5000U
) == 0x4000U
)
3156 // For a BLX instruction, make sure that the relocation is rounded up
3157 // to a word boundary. This follows the semantics of the instruction
3158 // which specifies that bit 1 of the target address will come from bit
3159 // 1 of the base address.
3160 branch_offset
= (branch_offset
+ 2) & ~3;
3162 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3163 // We use the Thumb-2 encoding, which is safe even if dealing with
3164 // a Thumb-1 instruction by virtue of our overflow check above. */
3165 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
3166 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
3168 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3169 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3172 ? utils::has_overflow
<25>(branch_offset
)
3173 : utils::has_overflow
<23>(branch_offset
))
3174 ? This::STATUS_OVERFLOW
3175 : This::STATUS_OKAY
);
3178 // Relocate THUMB-2 long conditional branches.
3179 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3180 // undefined and we do not use PLT in this relocation. In such a case,
3181 // the branch is converted into an NOP.
3183 template<bool big_endian
>
3184 typename Arm_relocate_functions
<big_endian
>::Status
3185 Arm_relocate_functions
<big_endian
>::thm_jump19(
3186 unsigned char *view
,
3187 const Arm_relobj
<big_endian
>* object
,
3188 const Symbol_value
<32>* psymval
,
3189 Arm_address address
,
3190 Arm_address thumb_bit
)
3192 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3193 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3194 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3195 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3196 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
3198 Arm_address branch_target
= psymval
->value(object
, addend
);
3199 int32_t branch_offset
= branch_target
- address
;
3201 // ??? Should handle interworking? GCC might someday try to
3202 // use this for tail calls.
3203 // FIXME: We do support thumb entry to PLT yet.
3206 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3207 return This::STATUS_BAD_RELOC
;
3210 // Put RELOCATION back into the insn.
3211 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
3212 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
3214 // Put the relocated value back in the object file:
3215 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3216 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3218 return (utils::has_overflow
<21>(branch_offset
)
3219 ? This::STATUS_OVERFLOW
3220 : This::STATUS_OKAY
);
3223 // Get the GOT section, creating it if necessary.
3225 template<bool big_endian
>
3226 Output_data_got
<32, big_endian
>*
3227 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
3229 if (this->got_
== NULL
)
3231 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3233 this->got_
= new Output_data_got
<32, big_endian
>();
3236 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3238 | elfcpp::SHF_WRITE
),
3239 this->got_
, false, true, true,
3242 // The old GNU linker creates a .got.plt section. We just
3243 // create another set of data in the .got section. Note that we
3244 // always create a PLT if we create a GOT, although the PLT
3246 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
3247 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3249 | elfcpp::SHF_WRITE
),
3250 this->got_plt_
, false, false,
3253 // The first three entries are reserved.
3254 this->got_plt_
->set_current_data_size(3 * 4);
3256 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3257 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3258 Symbol_table::PREDEFINED
,
3260 0, 0, elfcpp::STT_OBJECT
,
3262 elfcpp::STV_HIDDEN
, 0,
3268 // Get the dynamic reloc section, creating it if necessary.
3270 template<bool big_endian
>
3271 typename Target_arm
<big_endian
>::Reloc_section
*
3272 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
3274 if (this->rel_dyn_
== NULL
)
3276 gold_assert(layout
!= NULL
);
3277 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
3278 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
3279 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
3280 false, false, false);
3282 return this->rel_dyn_
;
3285 // Insn_template methods.
3287 // Return byte size of an instruction template.
3290 Insn_template::size() const
3292 switch (this->type())
3295 case THUMB16_SPECIAL_TYPE
:
3306 // Return alignment of an instruction template.
3309 Insn_template::alignment() const
3311 switch (this->type())
3314 case THUMB16_SPECIAL_TYPE
:
3325 // Stub_template methods.
3327 Stub_template::Stub_template(
3328 Stub_type type
, const Insn_template
* insns
,
3330 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
3331 entry_in_thumb_mode_(false), relocs_()
3335 // Compute byte size and alignment of stub template.
3336 for (size_t i
= 0; i
< insn_count
; i
++)
3338 unsigned insn_alignment
= insns
[i
].alignment();
3339 size_t insn_size
= insns
[i
].size();
3340 gold_assert((offset
& (insn_alignment
- 1)) == 0);
3341 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
3342 switch (insns
[i
].type())
3344 case Insn_template::THUMB16_TYPE
:
3345 case Insn_template::THUMB16_SPECIAL_TYPE
:
3347 this->entry_in_thumb_mode_
= true;
3350 case Insn_template::THUMB32_TYPE
:
3351 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
3352 this->relocs_
.push_back(Reloc(i
, offset
));
3354 this->entry_in_thumb_mode_
= true;
3357 case Insn_template::ARM_TYPE
:
3358 // Handle cases where the target is encoded within the
3360 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
3361 this->relocs_
.push_back(Reloc(i
, offset
));
3364 case Insn_template::DATA_TYPE
:
3365 // Entry point cannot be data.
3366 gold_assert(i
!= 0);
3367 this->relocs_
.push_back(Reloc(i
, offset
));
3373 offset
+= insn_size
;
3375 this->size_
= offset
;
3380 // Template to implement do_write for a specific target endianity.
3382 template<bool big_endian
>
3384 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
3386 const Stub_template
* stub_template
= this->stub_template();
3387 const Insn_template
* insns
= stub_template
->insns();
3389 // FIXME: We do not handle BE8 encoding yet.
3390 unsigned char* pov
= view
;
3391 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
3393 switch (insns
[i
].type())
3395 case Insn_template::THUMB16_TYPE
:
3396 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
3398 case Insn_template::THUMB16_SPECIAL_TYPE
:
3399 elfcpp::Swap
<16, big_endian
>::writeval(
3401 this->thumb16_special(i
));
3403 case Insn_template::THUMB32_TYPE
:
3405 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
3406 uint32_t lo
= insns
[i
].data() & 0xffff;
3407 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
3408 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
3411 case Insn_template::ARM_TYPE
:
3412 case Insn_template::DATA_TYPE
:
3413 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
3418 pov
+= insns
[i
].size();
3420 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
3423 // Reloc_stub::Key methods.
3425 // Dump a Key as a string for debugging.
3428 Reloc_stub::Key::name() const
3430 if (this->r_sym_
== invalid_index
)
3432 // Global symbol key name
3433 // <stub-type>:<symbol name>:<addend>.
3434 const std::string sym_name
= this->u_
.symbol
->name();
3435 // We need to print two hex number and two colons. So just add 100 bytes
3436 // to the symbol name size.
3437 size_t len
= sym_name
.size() + 100;
3438 char* buffer
= new char[len
];
3439 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
3440 sym_name
.c_str(), this->addend_
);
3441 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3443 return std::string(buffer
);
3447 // local symbol key name
3448 // <stub-type>:<object>:<r_sym>:<addend>.
3449 const size_t len
= 200;
3451 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3452 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3453 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3454 return std::string(buffer
);
3458 // Reloc_stub methods.
3460 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3461 // LOCATION to DESTINATION.
3462 // This code is based on the arm_type_of_stub function in
3463 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3467 Reloc_stub::stub_type_for_reloc(
3468 unsigned int r_type
,
3469 Arm_address location
,
3470 Arm_address destination
,
3471 bool target_is_thumb
)
3473 Stub_type stub_type
= arm_stub_none
;
3475 // This is a bit ugly but we want to avoid using a templated class for
3476 // big and little endianities.
3478 bool should_force_pic_veneer
;
3481 if (parameters
->target().is_big_endian())
3483 const Target_arm
<true>* big_endian_target
=
3484 Target_arm
<true>::default_target();
3485 may_use_blx
= big_endian_target
->may_use_blx();
3486 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3487 thumb2
= big_endian_target
->using_thumb2();
3488 thumb_only
= big_endian_target
->using_thumb_only();
3492 const Target_arm
<false>* little_endian_target
=
3493 Target_arm
<false>::default_target();
3494 may_use_blx
= little_endian_target
->may_use_blx();
3495 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3496 thumb2
= little_endian_target
->using_thumb2();
3497 thumb_only
= little_endian_target
->using_thumb_only();
3500 int64_t branch_offset
= (int64_t)destination
- location
;
3502 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3504 // Handle cases where:
3505 // - this call goes too far (different Thumb/Thumb2 max
3507 // - it's a Thumb->Arm call and blx is not available, or it's a
3508 // Thumb->Arm branch (not bl). A stub is needed in this case.
3510 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3511 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3513 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3514 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3515 || ((!target_is_thumb
)
3516 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3517 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3519 if (target_is_thumb
)
3524 stub_type
= (parameters
->options().shared()
3525 || should_force_pic_veneer
)
3528 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3529 // V5T and above. Stub starts with ARM code, so
3530 // we must be able to switch mode before
3531 // reaching it, which is only possible for 'bl'
3532 // (ie R_ARM_THM_CALL relocation).
3533 ? arm_stub_long_branch_any_thumb_pic
3534 // On V4T, use Thumb code only.
3535 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3539 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3540 ? arm_stub_long_branch_any_any
// V5T and above.
3541 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3545 stub_type
= (parameters
->options().shared()
3546 || should_force_pic_veneer
)
3547 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3548 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3555 // FIXME: We should check that the input section is from an
3556 // object that has interwork enabled.
3558 stub_type
= (parameters
->options().shared()
3559 || should_force_pic_veneer
)
3562 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3563 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3564 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3568 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3569 ? arm_stub_long_branch_any_any
// V5T and above.
3570 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3572 // Handle v4t short branches.
3573 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3574 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3575 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3576 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3580 else if (r_type
== elfcpp::R_ARM_CALL
3581 || r_type
== elfcpp::R_ARM_JUMP24
3582 || r_type
== elfcpp::R_ARM_PLT32
)
3584 if (target_is_thumb
)
3588 // FIXME: We should check that the input section is from an
3589 // object that has interwork enabled.
3591 // We have an extra 2-bytes reach because of
3592 // the mode change (bit 24 (H) of BLX encoding).
3593 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3594 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3595 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3596 || (r_type
== elfcpp::R_ARM_JUMP24
)
3597 || (r_type
== elfcpp::R_ARM_PLT32
))
3599 stub_type
= (parameters
->options().shared()
3600 || should_force_pic_veneer
)
3603 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3604 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3608 ? arm_stub_long_branch_any_any
// V5T and above.
3609 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3615 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3616 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3618 stub_type
= (parameters
->options().shared()
3619 || should_force_pic_veneer
)
3620 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3621 : arm_stub_long_branch_any_any
; /// non-PIC.
3629 // Cortex_a8_stub methods.
3631 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3632 // I is the position of the instruction template in the stub template.
3635 Cortex_a8_stub::do_thumb16_special(size_t i
)
3637 // The only use of this is to copy condition code from a conditional
3638 // branch being worked around to the corresponding conditional branch in
3640 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3642 uint16_t data
= this->stub_template()->insns()[i
].data();
3643 gold_assert((data
& 0xff00U
) == 0xd000U
);
3644 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3648 // Stub_factory methods.
3650 Stub_factory::Stub_factory()
3652 // The instruction template sequences are declared as static
3653 // objects and initialized first time the constructor runs.
3655 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3656 // to reach the stub if necessary.
3657 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3659 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3660 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3661 // dcd R_ARM_ABS32(X)
3664 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3666 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3668 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3669 Insn_template::arm_insn(0xe12fff1c), // bx ip
3670 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3671 // dcd R_ARM_ABS32(X)
3674 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3675 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3677 Insn_template::thumb16_insn(0xb401), // push {r0}
3678 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3679 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3680 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3681 Insn_template::thumb16_insn(0x4760), // bx ip
3682 Insn_template::thumb16_insn(0xbf00), // nop
3683 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3684 // dcd R_ARM_ABS32(X)
3687 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3689 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3691 Insn_template::thumb16_insn(0x4778), // bx pc
3692 Insn_template::thumb16_insn(0x46c0), // nop
3693 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3694 Insn_template::arm_insn(0xe12fff1c), // bx ip
3695 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3696 // dcd R_ARM_ABS32(X)
3699 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3701 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3703 Insn_template::thumb16_insn(0x4778), // bx pc
3704 Insn_template::thumb16_insn(0x46c0), // nop
3705 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3706 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3707 // dcd R_ARM_ABS32(X)
3710 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3711 // one, when the destination is close enough.
3712 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3714 Insn_template::thumb16_insn(0x4778), // bx pc
3715 Insn_template::thumb16_insn(0x46c0), // nop
3716 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3719 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3720 // blx to reach the stub if necessary.
3721 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3723 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3724 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3725 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3726 // dcd R_ARM_REL32(X-4)
3729 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3730 // blx to reach the stub if necessary. We can not add into pc;
3731 // it is not guaranteed to mode switch (different in ARMv6 and
3733 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3735 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3736 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3737 Insn_template::arm_insn(0xe12fff1c), // bx ip
3738 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3739 // dcd R_ARM_REL32(X)
3742 // V4T ARM -> ARM long branch stub, PIC.
3743 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3745 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3746 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3747 Insn_template::arm_insn(0xe12fff1c), // bx ip
3748 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3749 // dcd R_ARM_REL32(X)
3752 // V4T Thumb -> ARM long branch stub, PIC.
3753 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3755 Insn_template::thumb16_insn(0x4778), // bx pc
3756 Insn_template::thumb16_insn(0x46c0), // nop
3757 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3758 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3759 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3760 // dcd R_ARM_REL32(X)
3763 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3765 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3767 Insn_template::thumb16_insn(0xb401), // push {r0}
3768 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3769 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3770 Insn_template::thumb16_insn(0x4484), // add ip, r0
3771 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3772 Insn_template::thumb16_insn(0x4760), // bx ip
3773 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3774 // dcd R_ARM_REL32(X)
3777 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3779 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3781 Insn_template::thumb16_insn(0x4778), // bx pc
3782 Insn_template::thumb16_insn(0x46c0), // nop
3783 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3784 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3785 Insn_template::arm_insn(0xe12fff1c), // bx ip
3786 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3787 // dcd R_ARM_REL32(X)
3790 // Cortex-A8 erratum-workaround stubs.
3792 // Stub used for conditional branches (which may be beyond +/-1MB away,
3793 // so we can't use a conditional branch to reach this stub).
3800 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3802 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3803 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3804 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3808 // Stub used for b.w and bl.w instructions.
3810 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3812 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3815 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3817 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3820 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3821 // instruction (which switches to ARM mode) to point to this stub. Jump to
3822 // the real destination using an ARM-mode branch.
3823 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3825 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3828 // Stub used to provide an interworking for R_ARM_V4BX relocation
3829 // (bx r[n] instruction).
3830 static const Insn_template elf32_arm_stub_v4_veneer_bx
[] =
3832 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
3833 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
3834 Insn_template::arm_insn(0xe12fff10) // bx r<n>
3837 // Fill in the stub template look-up table. Stub templates are constructed
3838 // per instance of Stub_factory for fast look-up without locking
3839 // in a thread-enabled environment.
3841 this->stub_templates_
[arm_stub_none
] =
3842 new Stub_template(arm_stub_none
, NULL
, 0);
3844 #define DEF_STUB(x) \
3848 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3849 Stub_type type = arm_stub_##x; \
3850 this->stub_templates_[type] = \
3851 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3859 // Stub_table methods.
3861 // Removel all Cortex-A8 stub.
3863 template<bool big_endian
>
3865 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
3867 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3868 p
!= this->cortex_a8_stubs_
.end();
3871 this->cortex_a8_stubs_
.clear();
3874 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3876 template<bool big_endian
>
3878 Stub_table
<big_endian
>::relocate_stub(
3880 const Relocate_info
<32, big_endian
>* relinfo
,
3881 Target_arm
<big_endian
>* arm_target
,
3882 Output_section
* output_section
,
3883 unsigned char* view
,
3884 Arm_address address
,
3885 section_size_type view_size
)
3887 const Stub_template
* stub_template
= stub
->stub_template();
3888 if (stub_template
->reloc_count() != 0)
3890 // Adjust view to cover the stub only.
3891 section_size_type offset
= stub
->offset();
3892 section_size_type stub_size
= stub_template
->size();
3893 gold_assert(offset
+ stub_size
<= view_size
);
3895 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
3896 address
+ offset
, stub_size
);
3900 // Relocate all stubs in this stub table.
3902 template<bool big_endian
>
3904 Stub_table
<big_endian
>::relocate_stubs(
3905 const Relocate_info
<32, big_endian
>* relinfo
,
3906 Target_arm
<big_endian
>* arm_target
,
3907 Output_section
* output_section
,
3908 unsigned char* view
,
3909 Arm_address address
,
3910 section_size_type view_size
)
3912 // If we are passed a view bigger than the stub table's. we need to
3914 gold_assert(address
== this->address()
3916 == static_cast<section_size_type
>(this->data_size())));
3918 // Relocate all relocation stubs.
3919 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3920 p
!= this->reloc_stubs_
.end();
3922 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3923 address
, view_size
);
3925 // Relocate all Cortex-A8 stubs.
3926 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
3927 p
!= this->cortex_a8_stubs_
.end();
3929 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
3930 address
, view_size
);
3932 // Relocate all ARM V4BX stubs.
3933 for (Arm_v4bx_stub_list::iterator p
= this->arm_v4bx_stubs_
.begin();
3934 p
!= this->arm_v4bx_stubs_
.end();
3938 this->relocate_stub(*p
, relinfo
, arm_target
, output_section
, view
,
3939 address
, view_size
);
3943 // Write out the stubs to file.
3945 template<bool big_endian
>
3947 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3949 off_t offset
= this->offset();
3950 const section_size_type oview_size
=
3951 convert_to_section_size_type(this->data_size());
3952 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3954 // Write relocation stubs.
3955 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3956 p
!= this->reloc_stubs_
.end();
3959 Reloc_stub
* stub
= p
->second
;
3960 Arm_address address
= this->address() + stub
->offset();
3962 == align_address(address
,
3963 stub
->stub_template()->alignment()));
3964 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3968 // Write Cortex-A8 stubs.
3969 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
3970 p
!= this->cortex_a8_stubs_
.end();
3973 Cortex_a8_stub
* stub
= p
->second
;
3974 Arm_address address
= this->address() + stub
->offset();
3976 == align_address(address
,
3977 stub
->stub_template()->alignment()));
3978 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3982 // Write ARM V4BX relocation stubs.
3983 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
3984 p
!= this->arm_v4bx_stubs_
.end();
3990 Arm_address address
= this->address() + (*p
)->offset();
3992 == align_address(address
,
3993 (*p
)->stub_template()->alignment()));
3994 (*p
)->write(oview
+ (*p
)->offset(), (*p
)->stub_template()->size(),
3998 of
->write_output_view(this->offset(), oview_size
, oview
);
4001 // Update the data size and address alignment of the stub table at the end
4002 // of a relaxation pass. Return true if either the data size or the
4003 // alignment changed in this relaxation pass.
4005 template<bool big_endian
>
4007 Stub_table
<big_endian
>::update_data_size_and_addralign()
4010 unsigned addralign
= 1;
4012 // Go over all stubs in table to compute data size and address alignment.
4014 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4015 p
!= this->reloc_stubs_
.end();
4018 const Stub_template
* stub_template
= p
->second
->stub_template();
4019 addralign
= std::max(addralign
, stub_template
->alignment());
4020 size
= (align_address(size
, stub_template
->alignment())
4021 + stub_template
->size());
4024 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4025 p
!= this->cortex_a8_stubs_
.end();
4028 const Stub_template
* stub_template
= p
->second
->stub_template();
4029 addralign
= std::max(addralign
, stub_template
->alignment());
4030 size
= (align_address(size
, stub_template
->alignment())
4031 + stub_template
->size());
4034 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4035 p
!= this->arm_v4bx_stubs_
.end();
4041 const Stub_template
* stub_template
= (*p
)->stub_template();
4042 addralign
= std::max(addralign
, stub_template
->alignment());
4043 size
= (align_address(size
, stub_template
->alignment())
4044 + stub_template
->size());
4047 // Check if either data size or alignment changed in this pass.
4048 // Update prev_data_size_ and prev_addralign_. These will be used
4049 // as the current data size and address alignment for the next pass.
4050 bool changed
= size
!= this->prev_data_size_
;
4051 this->prev_data_size_
= size
;
4053 if (addralign
!= this->prev_addralign_
)
4055 this->prev_addralign_
= addralign
;
4060 // Finalize the stubs. This sets the offsets of the stubs within the stub
4061 // table. It also marks all input sections needing Cortex-A8 workaround.
4063 template<bool big_endian
>
4065 Stub_table
<big_endian
>::finalize_stubs()
4068 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4069 p
!= this->reloc_stubs_
.end();
4072 Reloc_stub
* stub
= p
->second
;
4073 const Stub_template
* stub_template
= stub
->stub_template();
4074 uint64_t stub_addralign
= stub_template
->alignment();
4075 off
= align_address(off
, stub_addralign
);
4076 stub
->set_offset(off
);
4077 off
+= stub_template
->size();
4080 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4081 p
!= this->cortex_a8_stubs_
.end();
4084 Cortex_a8_stub
* stub
= p
->second
;
4085 const Stub_template
* stub_template
= stub
->stub_template();
4086 uint64_t stub_addralign
= stub_template
->alignment();
4087 off
= align_address(off
, stub_addralign
);
4088 stub
->set_offset(off
);
4089 off
+= stub_template
->size();
4091 // Mark input section so that we can determine later if a code section
4092 // needs the Cortex-A8 workaround quickly.
4093 Arm_relobj
<big_endian
>* arm_relobj
=
4094 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
4095 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
4098 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4099 p
!= this->arm_v4bx_stubs_
.end();
4105 const Stub_template
* stub_template
= (*p
)->stub_template();
4106 uint64_t stub_addralign
= stub_template
->alignment();
4107 off
= align_address(off
, stub_addralign
);
4108 (*p
)->set_offset(off
);
4109 off
+= stub_template
->size();
4112 gold_assert(off
<= this->prev_data_size_
);
4115 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4116 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4117 // of the address range seen by the linker.
4119 template<bool big_endian
>
4121 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
4122 Target_arm
<big_endian
>* arm_target
,
4123 unsigned char* view
,
4124 Arm_address view_address
,
4125 section_size_type view_size
)
4127 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4128 for (Cortex_a8_stub_list::const_iterator p
=
4129 this->cortex_a8_stubs_
.lower_bound(view_address
);
4130 ((p
!= this->cortex_a8_stubs_
.end())
4131 && (p
->first
< (view_address
+ view_size
)));
4134 // We do not store the THUMB bit in the LSB of either the branch address
4135 // or the stub offset. There is no need to strip the LSB.
4136 Arm_address branch_address
= p
->first
;
4137 const Cortex_a8_stub
* stub
= p
->second
;
4138 Arm_address stub_address
= this->address() + stub
->offset();
4140 // Offset of the branch instruction relative to this view.
4141 section_size_type offset
=
4142 convert_to_section_size_type(branch_address
- view_address
);
4143 gold_assert((offset
+ 4) <= view_size
);
4145 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
4146 view
+ offset
, branch_address
);
4150 // Arm_input_section methods.
4152 // Initialize an Arm_input_section.
4154 template<bool big_endian
>
4156 Arm_input_section
<big_endian
>::init()
4158 Relobj
* relobj
= this->relobj();
4159 unsigned int shndx
= this->shndx();
4161 // Cache these to speed up size and alignment queries. It is too slow
4162 // to call section_addraglin and section_size every time.
4163 this->original_addralign_
= relobj
->section_addralign(shndx
);
4164 this->original_size_
= relobj
->section_size(shndx
);
4166 // We want to make this look like the original input section after
4167 // output sections are finalized.
4168 Output_section
* os
= relobj
->output_section(shndx
);
4169 off_t offset
= relobj
->output_section_offset(shndx
);
4170 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
4171 this->set_address(os
->address() + offset
);
4172 this->set_file_offset(os
->offset() + offset
);
4174 this->set_current_data_size(this->original_size_
);
4175 this->finalize_data_size();
4178 template<bool big_endian
>
4180 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
4182 // We have to write out the original section content.
4183 section_size_type section_size
;
4184 const unsigned char* section_contents
=
4185 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4186 of
->write(this->offset(), section_contents
, section_size
);
4188 // If this owns a stub table and it is not empty, write it.
4189 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
4190 this->stub_table_
->write(of
);
4193 // Finalize data size.
4195 template<bool big_endian
>
4197 Arm_input_section
<big_endian
>::set_final_data_size()
4199 // If this owns a stub table, finalize its data size as well.
4200 if (this->is_stub_table_owner())
4202 uint64_t address
= this->address();
4204 // The stub table comes after the original section contents.
4205 address
+= this->original_size_
;
4206 address
= align_address(address
, this->stub_table_
->addralign());
4207 off_t offset
= this->offset() + (address
- this->address());
4208 this->stub_table_
->set_address_and_file_offset(address
, offset
);
4209 address
+= this->stub_table_
->data_size();
4210 gold_assert(address
== this->address() + this->current_data_size());
4213 this->set_data_size(this->current_data_size());
4216 // Reset address and file offset.
4218 template<bool big_endian
>
4220 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
4222 // Size of the original input section contents.
4223 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
4225 // If this is a stub table owner, account for the stub table size.
4226 if (this->is_stub_table_owner())
4228 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
4230 // Reset the stub table's address and file offset. The
4231 // current data size for child will be updated after that.
4232 stub_table_
->reset_address_and_file_offset();
4233 off
= align_address(off
, stub_table_
->addralign());
4234 off
+= stub_table
->current_data_size();
4237 this->set_current_data_size(off
);
4240 // Arm_output_section methods.
4242 // Create a stub group for input sections from BEGIN to END. OWNER
4243 // points to the input section to be the owner a new stub table.
4245 template<bool big_endian
>
4247 Arm_output_section
<big_endian
>::create_stub_group(
4248 Input_section_list::const_iterator begin
,
4249 Input_section_list::const_iterator end
,
4250 Input_section_list::const_iterator owner
,
4251 Target_arm
<big_endian
>* target
,
4252 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
4254 // Currently we convert ordinary input sections into relaxed sections only
4255 // at this point but we may want to support creating relaxed input section
4256 // very early. So we check here to see if owner is already a relaxed
4259 Arm_input_section
<big_endian
>* arm_input_section
;
4260 if (owner
->is_relaxed_input_section())
4263 Arm_input_section
<big_endian
>::as_arm_input_section(
4264 owner
->relaxed_input_section());
4268 gold_assert(owner
->is_input_section());
4269 // Create a new relaxed input section.
4271 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
4272 new_relaxed_sections
->push_back(arm_input_section
);
4275 // Create a stub table.
4276 Stub_table
<big_endian
>* stub_table
=
4277 target
->new_stub_table(arm_input_section
);
4279 arm_input_section
->set_stub_table(stub_table
);
4281 Input_section_list::const_iterator p
= begin
;
4282 Input_section_list::const_iterator prev_p
;
4284 // Look for input sections or relaxed input sections in [begin ... end].
4287 if (p
->is_input_section() || p
->is_relaxed_input_section())
4289 // The stub table information for input sections live
4290 // in their objects.
4291 Arm_relobj
<big_endian
>* arm_relobj
=
4292 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
4293 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
4297 while (prev_p
!= end
);
4300 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4301 // of stub groups. We grow a stub group by adding input section until the
4302 // size is just below GROUP_SIZE. The last input section will be converted
4303 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4304 // input section after the stub table, effectively double the group size.
4306 // This is similar to the group_sections() function in elf32-arm.c but is
4307 // implemented differently.
4309 template<bool big_endian
>
4311 Arm_output_section
<big_endian
>::group_sections(
4312 section_size_type group_size
,
4313 bool stubs_always_after_branch
,
4314 Target_arm
<big_endian
>* target
)
4316 // We only care about sections containing code.
4317 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
4320 // States for grouping.
4323 // No group is being built.
4325 // A group is being built but the stub table is not found yet.
4326 // We keep group a stub group until the size is just under GROUP_SIZE.
4327 // The last input section in the group will be used as the stub table.
4328 FINDING_STUB_SECTION
,
4329 // A group is being built and we have already found a stub table.
4330 // We enter this state to grow a stub group by adding input section
4331 // after the stub table. This effectively doubles the group size.
4335 // Any newly created relaxed sections are stored here.
4336 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
4338 State state
= NO_GROUP
;
4339 section_size_type off
= 0;
4340 section_size_type group_begin_offset
= 0;
4341 section_size_type group_end_offset
= 0;
4342 section_size_type stub_table_end_offset
= 0;
4343 Input_section_list::const_iterator group_begin
=
4344 this->input_sections().end();
4345 Input_section_list::const_iterator stub_table
=
4346 this->input_sections().end();
4347 Input_section_list::const_iterator group_end
= this->input_sections().end();
4348 for (Input_section_list::const_iterator p
= this->input_sections().begin();
4349 p
!= this->input_sections().end();
4352 section_size_type section_begin_offset
=
4353 align_address(off
, p
->addralign());
4354 section_size_type section_end_offset
=
4355 section_begin_offset
+ p
->data_size();
4357 // Check to see if we should group the previously seens sections.
4363 case FINDING_STUB_SECTION
:
4364 // Adding this section makes the group larger than GROUP_SIZE.
4365 if (section_end_offset
- group_begin_offset
>= group_size
)
4367 if (stubs_always_after_branch
)
4369 gold_assert(group_end
!= this->input_sections().end());
4370 this->create_stub_group(group_begin
, group_end
, group_end
,
4371 target
, &new_relaxed_sections
);
4376 // But wait, there's more! Input sections up to
4377 // stub_group_size bytes after the stub table can be
4378 // handled by it too.
4379 state
= HAS_STUB_SECTION
;
4380 stub_table
= group_end
;
4381 stub_table_end_offset
= group_end_offset
;
4386 case HAS_STUB_SECTION
:
4387 // Adding this section makes the post stub-section group larger
4389 if (section_end_offset
- stub_table_end_offset
>= group_size
)
4391 gold_assert(group_end
!= this->input_sections().end());
4392 this->create_stub_group(group_begin
, group_end
, stub_table
,
4393 target
, &new_relaxed_sections
);
4402 // If we see an input section and currently there is no group, start
4403 // a new one. Skip any empty sections.
4404 if ((p
->is_input_section() || p
->is_relaxed_input_section())
4405 && (p
->relobj()->section_size(p
->shndx()) != 0))
4407 if (state
== NO_GROUP
)
4409 state
= FINDING_STUB_SECTION
;
4411 group_begin_offset
= section_begin_offset
;
4414 // Keep track of the last input section seen.
4416 group_end_offset
= section_end_offset
;
4419 off
= section_end_offset
;
4422 // Create a stub group for any ungrouped sections.
4423 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
4425 gold_assert(group_end
!= this->input_sections().end());
4426 this->create_stub_group(group_begin
, group_end
,
4427 (state
== FINDING_STUB_SECTION
4430 target
, &new_relaxed_sections
);
4433 // Convert input section into relaxed input section in a batch.
4434 if (!new_relaxed_sections
.empty())
4435 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
4437 // Update the section offsets
4438 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
4440 Arm_relobj
<big_endian
>* arm_relobj
=
4441 Arm_relobj
<big_endian
>::as_arm_relobj(
4442 new_relaxed_sections
[i
]->relobj());
4443 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
4444 // Tell Arm_relobj that this input section is converted.
4445 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
4449 // Arm_relobj methods.
4451 // Determine if we want to scan the SHNDX-th section for relocation stubs.
4452 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4454 template<bool big_endian
>
4456 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
4457 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4458 const Relobj::Output_sections
& out_sections
,
4459 const Symbol_table
*symtab
)
4461 unsigned int sh_type
= shdr
.get_sh_type();
4462 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
4465 // Ignore empty section.
4466 off_t sh_size
= shdr
.get_sh_size();
4470 // Ignore reloc section with bad info. This error will be
4471 // reported in the final link.
4472 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4473 if (index
>= this->shnum())
4476 // This relocation section is against a section which we
4477 // discarded or if the section is folded into another
4478 // section due to ICF.
4479 if (out_sections
[index
] == NULL
|| symtab
->is_section_folded(this, index
))
4482 // Ignore reloc section with unexpected symbol table. The
4483 // error will be reported in the final link.
4484 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
4487 unsigned int reloc_size
;
4488 if (sh_type
== elfcpp::SHT_REL
)
4489 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4491 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4493 // Ignore reloc section with unexpected entsize or uneven size.
4494 // The error will be reported in the final link.
4495 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
4501 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4502 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4504 template<bool big_endian
>
4506 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
4507 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4510 const Symbol_table
* symtab
)
4512 // We only scan non-empty code sections.
4513 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) == 0
4514 || shdr
.get_sh_size() == 0)
4517 // Ignore discarded or ICF'ed sections.
4518 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
4521 // Find output address of section.
4522 Arm_address address
= os
->output_address(this, shndx
, 0);
4524 // If the section does not cross any 4K-boundaries, it does not need to
4526 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
4532 // Scan a section for Cortex-A8 workaround.
4534 template<bool big_endian
>
4536 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
4537 const elfcpp::Shdr
<32, big_endian
>& shdr
,
4540 Target_arm
<big_endian
>* arm_target
)
4542 Arm_address output_address
= os
->output_address(this, shndx
, 0);
4544 // Get the section contents.
4545 section_size_type input_view_size
= 0;
4546 const unsigned char* input_view
=
4547 this->section_contents(shndx
, &input_view_size
, false);
4549 // We need to go through the mapping symbols to determine what to
4550 // scan. There are two reasons. First, we should look at THUMB code and
4551 // THUMB code only. Second, we only want to look at the 4K-page boundary
4552 // to speed up the scanning.
4554 // Look for the first mapping symbol in this section. It should be
4556 Mapping_symbol_position
section_start(shndx
, 0);
4557 typename
Mapping_symbols_info::const_iterator p
=
4558 this->mapping_symbols_info_
.lower_bound(section_start
);
4560 if (p
== this->mapping_symbols_info_
.end()
4561 || p
->first
!= section_start
)
4563 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4564 "is no mapping symbols for section %u of %s"),
4565 shndx
, this->name().c_str());
4569 while (p
!= this->mapping_symbols_info_
.end()
4570 && p
->first
.first
== shndx
)
4572 typename
Mapping_symbols_info::const_iterator next
=
4573 this->mapping_symbols_info_
.upper_bound(p
->first
);
4575 // Only scan part of a section with THUMB code.
4576 if (p
->second
== 't')
4578 // Determine the end of this range.
4579 section_size_type span_start
=
4580 convert_to_section_size_type(p
->first
.second
);
4581 section_size_type span_end
;
4582 if (next
!= this->mapping_symbols_info_
.end()
4583 && next
->first
.first
== shndx
)
4584 span_end
= convert_to_section_size_type(next
->first
.second
);
4586 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
4588 if (((span_start
+ output_address
) & ~0xfffUL
)
4589 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
4591 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
4592 span_start
, span_end
,
4602 // Scan relocations for stub generation.
4604 template<bool big_endian
>
4606 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
4607 Target_arm
<big_endian
>* arm_target
,
4608 const Symbol_table
* symtab
,
4609 const Layout
* layout
)
4611 unsigned int shnum
= this->shnum();
4612 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4614 // Read the section headers.
4615 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4619 // To speed up processing, we set up hash tables for fast lookup of
4620 // input offsets to output addresses.
4621 this->initialize_input_to_output_maps();
4623 const Relobj::Output_sections
& out_sections(this->output_sections());
4625 Relocate_info
<32, big_endian
> relinfo
;
4626 relinfo
.symtab
= symtab
;
4627 relinfo
.layout
= layout
;
4628 relinfo
.object
= this;
4630 // Do relocation stubs scanning.
4631 const unsigned char* p
= pshdrs
+ shdr_size
;
4632 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4634 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
4635 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
))
4637 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
4638 Arm_address output_offset
= this->get_output_section_offset(index
);
4639 Arm_address output_address
;
4640 if(output_offset
!= invalid_address
)
4641 output_address
= out_sections
[index
]->address() + output_offset
;
4644 // Currently this only happens for a relaxed section.
4645 const Output_relaxed_input_section
* poris
=
4646 out_sections
[index
]->find_relaxed_input_section(this, index
);
4647 gold_assert(poris
!= NULL
);
4648 output_address
= poris
->address();
4651 // Get the relocations.
4652 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
4656 // Get the section contents. This does work for the case in which
4657 // we modify the contents of an input section. We need to pass the
4658 // output view under such circumstances.
4659 section_size_type input_view_size
= 0;
4660 const unsigned char* input_view
=
4661 this->section_contents(index
, &input_view_size
, false);
4663 relinfo
.reloc_shndx
= i
;
4664 relinfo
.data_shndx
= index
;
4665 unsigned int sh_type
= shdr
.get_sh_type();
4666 unsigned int reloc_size
;
4667 if (sh_type
== elfcpp::SHT_REL
)
4668 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4670 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
4672 Output_section
* os
= out_sections
[index
];
4673 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
4674 shdr
.get_sh_size() / reloc_size
,
4676 output_offset
== invalid_address
,
4677 input_view
, output_address
,
4682 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
4683 // after its relocation section, if there is one, is processed for
4684 // relocation stubs. Merging this loop with the one above would have been
4685 // complicated since we would have had to make sure that relocation stub
4686 // scanning is done first.
4687 if (arm_target
->fix_cortex_a8())
4689 const unsigned char* p
= pshdrs
+ shdr_size
;
4690 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
4692 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
4693 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
4696 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
4701 // After we've done the relocations, we release the hash tables,
4702 // since we no longer need them.
4703 this->free_input_to_output_maps();
4706 // Count the local symbols. The ARM backend needs to know if a symbol
4707 // is a THUMB function or not. For global symbols, it is easy because
4708 // the Symbol object keeps the ELF symbol type. For local symbol it is
4709 // harder because we cannot access this information. So we override the
4710 // do_count_local_symbol in parent and scan local symbols to mark
4711 // THUMB functions. This is not the most efficient way but I do not want to
4712 // slow down other ports by calling a per symbol targer hook inside
4713 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4715 template<bool big_endian
>
4717 Arm_relobj
<big_endian
>::do_count_local_symbols(
4718 Stringpool_template
<char>* pool
,
4719 Stringpool_template
<char>* dynpool
)
4721 // We need to fix-up the values of any local symbols whose type are
4724 // Ask parent to count the local symbols.
4725 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
4726 const unsigned int loccount
= this->local_symbol_count();
4730 // Intialize the thumb function bit-vector.
4731 std::vector
<bool> empty_vector(loccount
, false);
4732 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
4734 // Read the symbol table section header.
4735 const unsigned int symtab_shndx
= this->symtab_shndx();
4736 elfcpp::Shdr
<32, big_endian
>
4737 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
4738 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
4740 // Read the local symbols.
4741 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
4742 gold_assert(loccount
== symtabshdr
.get_sh_info());
4743 off_t locsize
= loccount
* sym_size
;
4744 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
4745 locsize
, true, true);
4747 // For mapping symbol processing, we need to read the symbol names.
4748 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
4749 if (strtab_shndx
>= this->shnum())
4751 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
4755 elfcpp::Shdr
<32, big_endian
>
4756 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
4757 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
4759 this->error(_("symbol table name section has wrong type: %u"),
4760 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
4763 const char* pnames
=
4764 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
4765 strtabshdr
.get_sh_size(),
4768 // Loop over the local symbols and mark any local symbols pointing
4769 // to THUMB functions.
4771 // Skip the first dummy symbol.
4773 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
4774 this->local_values();
4775 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
4777 elfcpp::Sym
<32, big_endian
> sym(psyms
);
4778 elfcpp::STT st_type
= sym
.get_st_type();
4779 Symbol_value
<32>& lv((*plocal_values
)[i
]);
4780 Arm_address input_value
= lv
.input_value();
4782 // Check to see if this is a mapping symbol.
4783 const char* sym_name
= pnames
+ sym
.get_st_name();
4784 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
4786 unsigned int input_shndx
= sym
.get_st_shndx();
4788 // Strip of LSB in case this is a THUMB symbol.
4789 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
4790 this->mapping_symbols_info_
[msp
] = sym_name
[1];
4793 if (st_type
== elfcpp::STT_ARM_TFUNC
4794 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
4796 // This is a THUMB function. Mark this and canonicalize the
4797 // symbol value by setting LSB.
4798 this->local_symbol_is_thumb_function_
[i
] = true;
4799 if ((input_value
& 1) == 0)
4800 lv
.set_input_value(input_value
| 1);
4805 // Relocate sections.
4806 template<bool big_endian
>
4808 Arm_relobj
<big_endian
>::do_relocate_sections(
4809 const Symbol_table
* symtab
,
4810 const Layout
* layout
,
4811 const unsigned char* pshdrs
,
4812 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
4814 // Call parent to relocate sections.
4815 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
4818 // We do not generate stubs if doing a relocatable link.
4819 if (parameters
->options().relocatable())
4822 // Relocate stub tables.
4823 unsigned int shnum
= this->shnum();
4825 Target_arm
<big_endian
>* arm_target
=
4826 Target_arm
<big_endian
>::default_target();
4828 Relocate_info
<32, big_endian
> relinfo
;
4829 relinfo
.symtab
= symtab
;
4830 relinfo
.layout
= layout
;
4831 relinfo
.object
= this;
4833 for (unsigned int i
= 1; i
< shnum
; ++i
)
4835 Arm_input_section
<big_endian
>* arm_input_section
=
4836 arm_target
->find_arm_input_section(this, i
);
4838 if (arm_input_section
!= NULL
4839 && arm_input_section
->is_stub_table_owner()
4840 && !arm_input_section
->stub_table()->empty())
4842 // We cannot discard a section if it owns a stub table.
4843 Output_section
* os
= this->output_section(i
);
4844 gold_assert(os
!= NULL
);
4846 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
4847 relinfo
.reloc_shdr
= NULL
;
4848 relinfo
.data_shndx
= i
;
4849 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
4851 gold_assert((*pviews
)[i
].view
!= NULL
);
4853 // We are passed the output section view. Adjust it to cover the
4855 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
4856 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
4857 && ((stub_table
->address() + stub_table
->data_size())
4858 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
4860 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
4861 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
4862 Arm_address address
= stub_table
->address();
4863 section_size_type view_size
= stub_table
->data_size();
4865 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
4869 // Apply Cortex A8 workaround if applicable.
4870 if (this->section_has_cortex_a8_workaround(i
))
4872 unsigned char* view
= (*pviews
)[i
].view
;
4873 Arm_address view_address
= (*pviews
)[i
].address
;
4874 section_size_type view_size
= (*pviews
)[i
].view_size
;
4875 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
4877 // Adjust view to cover section.
4878 Output_section
* os
= this->output_section(i
);
4879 gold_assert(os
!= NULL
);
4880 Arm_address section_address
= os
->output_address(this, i
, 0);
4881 uint64_t section_size
= this->section_size(i
);
4883 gold_assert(section_address
>= view_address
4884 && ((section_address
+ section_size
)
4885 <= (view_address
+ view_size
)));
4887 unsigned char* section_view
= view
+ (section_address
- view_address
);
4889 // Apply the Cortex-A8 workaround to the output address range
4890 // corresponding to this input section.
4891 stub_table
->apply_cortex_a8_workaround_to_address_range(
4900 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4903 template<bool big_endian
>
4904 Attributes_section_data
*
4905 read_arm_attributes_section(
4907 Read_symbols_data
*sd
)
4909 // Read the attributes section if there is one.
4910 // We read from the end because gas seems to put it near the end of
4911 // the section headers.
4912 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4913 const unsigned char *ps
=
4914 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
4915 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
4917 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4918 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
4920 section_offset_type section_offset
= shdr
.get_sh_offset();
4921 section_size_type section_size
=
4922 convert_to_section_size_type(shdr
.get_sh_size());
4923 File_view
* view
= object
->get_lasting_view(section_offset
,
4924 section_size
, true, false);
4925 return new Attributes_section_data(view
->data(), section_size
);
4931 // Read the symbol information.
4933 template<bool big_endian
>
4935 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4937 // Call parent class to read symbol information.
4938 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
4940 // Read processor-specific flags in ELF file header.
4941 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
4942 elfcpp::Elf_sizes
<32>::ehdr_size
,
4944 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
4945 this->processor_specific_flags_
= ehdr
.get_e_flags();
4946 this->attributes_section_data_
=
4947 read_arm_attributes_section
<big_endian
>(this, sd
);
4950 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4951 // sections for unwinding. These sections are referenced implicitly by
4952 // text sections linked in the section headers. If we ignore these implict
4953 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4954 // will be garbage-collected incorrectly. Hence we override the same function
4955 // in the base class to handle these implicit references.
4957 template<bool big_endian
>
4959 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
4961 Read_relocs_data
* rd
)
4963 // First, call base class method to process relocations in this object.
4964 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
4966 unsigned int shnum
= this->shnum();
4967 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
4968 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
4972 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4973 // to these from the linked text sections.
4974 const unsigned char* ps
= pshdrs
+ shdr_size
;
4975 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
4977 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
4978 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
4980 // Found an .ARM.exidx section, add it to the set of reachable
4981 // sections from its linked text section.
4982 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
4983 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
4988 // Arm_dynobj methods.
4990 // Read the symbol information.
4992 template<bool big_endian
>
4994 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
4996 // Call parent class to read symbol information.
4997 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
4999 // Read processor-specific flags in ELF file header.
5000 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
5001 elfcpp::Elf_sizes
<32>::ehdr_size
,
5003 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
5004 this->processor_specific_flags_
= ehdr
.get_e_flags();
5005 this->attributes_section_data_
=
5006 read_arm_attributes_section
<big_endian
>(this, sd
);
5009 // Stub_addend_reader methods.
5011 // Read the addend of a REL relocation of type R_TYPE at VIEW.
5013 template<bool big_endian
>
5014 elfcpp::Elf_types
<32>::Elf_Swxword
5015 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
5016 unsigned int r_type
,
5017 const unsigned char* view
,
5018 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
5020 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
5024 case elfcpp::R_ARM_CALL
:
5025 case elfcpp::R_ARM_JUMP24
:
5026 case elfcpp::R_ARM_PLT32
:
5028 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5029 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5030 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5031 return utils::sign_extend
<26>(val
<< 2);
5034 case elfcpp::R_ARM_THM_CALL
:
5035 case elfcpp::R_ARM_THM_JUMP24
:
5036 case elfcpp::R_ARM_THM_XPC22
:
5038 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5039 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5040 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5041 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5042 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
5045 case elfcpp::R_ARM_THM_JUMP19
:
5047 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5048 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5049 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5050 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5051 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
5059 // A class to handle the PLT data.
5061 template<bool big_endian
>
5062 class Output_data_plt_arm
: public Output_section_data
5065 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
5068 Output_data_plt_arm(Layout
*, Output_data_space
*);
5070 // Add an entry to the PLT.
5072 add_entry(Symbol
* gsym
);
5074 // Return the .rel.plt section data.
5075 const Reloc_section
*
5077 { return this->rel_
; }
5081 do_adjust_output_section(Output_section
* os
);
5083 // Write to a map file.
5085 do_print_to_mapfile(Mapfile
* mapfile
) const
5086 { mapfile
->print_output_data(this, _("** PLT")); }
5089 // Template for the first PLT entry.
5090 static const uint32_t first_plt_entry
[5];
5092 // Template for subsequent PLT entries.
5093 static const uint32_t plt_entry
[3];
5095 // Set the final size.
5097 set_final_data_size()
5099 this->set_data_size(sizeof(first_plt_entry
)
5100 + this->count_
* sizeof(plt_entry
));
5103 // Write out the PLT data.
5105 do_write(Output_file
*);
5107 // The reloc section.
5108 Reloc_section
* rel_
;
5109 // The .got.plt section.
5110 Output_data_space
* got_plt_
;
5111 // The number of PLT entries.
5112 unsigned int count_
;
5115 // Create the PLT section. The ordinary .got section is an argument,
5116 // since we need to refer to the start. We also create our own .got
5117 // section just for PLT entries.
5119 template<bool big_endian
>
5120 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
5121 Output_data_space
* got_plt
)
5122 : Output_section_data(4), got_plt_(got_plt
), count_(0)
5124 this->rel_
= new Reloc_section(false);
5125 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
5126 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
5130 template<bool big_endian
>
5132 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
5137 // Add an entry to the PLT.
5139 template<bool big_endian
>
5141 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
5143 gold_assert(!gsym
->has_plt_offset());
5145 // Note that when setting the PLT offset we skip the initial
5146 // reserved PLT entry.
5147 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
5148 + sizeof(first_plt_entry
));
5152 section_offset_type got_offset
= this->got_plt_
->current_data_size();
5154 // Every PLT entry needs a GOT entry which points back to the PLT
5155 // entry (this will be changed by the dynamic linker, normally
5156 // lazily when the function is called).
5157 this->got_plt_
->set_current_data_size(got_offset
+ 4);
5159 // Every PLT entry needs a reloc.
5160 gsym
->set_needs_dynsym_entry();
5161 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
5164 // Note that we don't need to save the symbol. The contents of the
5165 // PLT are independent of which symbols are used. The symbols only
5166 // appear in the relocations.
5170 // FIXME: This is not very flexible. Right now this has only been tested
5171 // on armv5te. If we are to support additional architecture features like
5172 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
5174 // The first entry in the PLT.
5175 template<bool big_endian
>
5176 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
5178 0xe52de004, // str lr, [sp, #-4]!
5179 0xe59fe004, // ldr lr, [pc, #4]
5180 0xe08fe00e, // add lr, pc, lr
5181 0xe5bef008, // ldr pc, [lr, #8]!
5182 0x00000000, // &GOT[0] - .
5185 // Subsequent entries in the PLT.
5187 template<bool big_endian
>
5188 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
5190 0xe28fc600, // add ip, pc, #0xNN00000
5191 0xe28cca00, // add ip, ip, #0xNN000
5192 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
5195 // Write out the PLT. This uses the hand-coded instructions above,
5196 // and adjusts them as needed. This is all specified by the arm ELF
5197 // Processor Supplement.
5199 template<bool big_endian
>
5201 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
5203 const off_t offset
= this->offset();
5204 const section_size_type oview_size
=
5205 convert_to_section_size_type(this->data_size());
5206 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5208 const off_t got_file_offset
= this->got_plt_
->offset();
5209 const section_size_type got_size
=
5210 convert_to_section_size_type(this->got_plt_
->data_size());
5211 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
5213 unsigned char* pov
= oview
;
5215 Arm_address plt_address
= this->address();
5216 Arm_address got_address
= this->got_plt_
->address();
5218 // Write first PLT entry. All but the last word are constants.
5219 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
5220 / sizeof(plt_entry
[0]));
5221 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
5222 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
5223 // Last word in first PLT entry is &GOT[0] - .
5224 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
5225 got_address
- (plt_address
+ 16));
5226 pov
+= sizeof(first_plt_entry
);
5228 unsigned char* got_pov
= got_view
;
5230 memset(got_pov
, 0, 12);
5233 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5234 unsigned int plt_offset
= sizeof(first_plt_entry
);
5235 unsigned int plt_rel_offset
= 0;
5236 unsigned int got_offset
= 12;
5237 const unsigned int count
= this->count_
;
5238 for (unsigned int i
= 0;
5241 pov
+= sizeof(plt_entry
),
5243 plt_offset
+= sizeof(plt_entry
),
5244 plt_rel_offset
+= rel_size
,
5247 // Set and adjust the PLT entry itself.
5248 int32_t offset
= ((got_address
+ got_offset
)
5249 - (plt_address
+ plt_offset
+ 8));
5251 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
5252 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
5253 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
5254 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
5255 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
5256 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
5257 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
5259 // Set the entry in the GOT.
5260 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
5263 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
5264 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
5266 of
->write_output_view(offset
, oview_size
, oview
);
5267 of
->write_output_view(got_file_offset
, got_size
, got_view
);
5270 // Create a PLT entry for a global symbol.
5272 template<bool big_endian
>
5274 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
5277 if (gsym
->has_plt_offset())
5280 if (this->plt_
== NULL
)
5282 // Create the GOT sections first.
5283 this->got_section(symtab
, layout
);
5285 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
5286 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
5288 | elfcpp::SHF_EXECINSTR
),
5289 this->plt_
, false, false, false, false);
5291 this->plt_
->add_entry(gsym
);
5294 // Report an unsupported relocation against a local symbol.
5296 template<bool big_endian
>
5298 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
5299 Sized_relobj
<32, big_endian
>* object
,
5300 unsigned int r_type
)
5302 gold_error(_("%s: unsupported reloc %u against local symbol"),
5303 object
->name().c_str(), r_type
);
5306 // We are about to emit a dynamic relocation of type R_TYPE. If the
5307 // dynamic linker does not support it, issue an error. The GNU linker
5308 // only issues a non-PIC error for an allocated read-only section.
5309 // Here we know the section is allocated, but we don't know that it is
5310 // read-only. But we check for all the relocation types which the
5311 // glibc dynamic linker supports, so it seems appropriate to issue an
5312 // error even if the section is not read-only.
5314 template<bool big_endian
>
5316 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
5317 unsigned int r_type
)
5321 // These are the relocation types supported by glibc for ARM.
5322 case elfcpp::R_ARM_RELATIVE
:
5323 case elfcpp::R_ARM_COPY
:
5324 case elfcpp::R_ARM_GLOB_DAT
:
5325 case elfcpp::R_ARM_JUMP_SLOT
:
5326 case elfcpp::R_ARM_ABS32
:
5327 case elfcpp::R_ARM_ABS32_NOI
:
5328 case elfcpp::R_ARM_PC24
:
5329 // FIXME: The following 3 types are not supported by Android's dynamic
5331 case elfcpp::R_ARM_TLS_DTPMOD32
:
5332 case elfcpp::R_ARM_TLS_DTPOFF32
:
5333 case elfcpp::R_ARM_TLS_TPOFF32
:
5337 // This prevents us from issuing more than one error per reloc
5338 // section. But we can still wind up issuing more than one
5339 // error per object file.
5340 if (this->issued_non_pic_error_
)
5342 object
->error(_("requires unsupported dynamic reloc; "
5343 "recompile with -fPIC"));
5344 this->issued_non_pic_error_
= true;
5347 case elfcpp::R_ARM_NONE
:
5352 // Scan a relocation for a local symbol.
5353 // FIXME: This only handles a subset of relocation types used by Android
5354 // on ARM v5te devices.
5356 template<bool big_endian
>
5358 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
5361 Sized_relobj
<32, big_endian
>* object
,
5362 unsigned int data_shndx
,
5363 Output_section
* output_section
,
5364 const elfcpp::Rel
<32, big_endian
>& reloc
,
5365 unsigned int r_type
,
5366 const elfcpp::Sym
<32, big_endian
>&)
5368 r_type
= get_real_reloc_type(r_type
);
5371 case elfcpp::R_ARM_NONE
:
5374 case elfcpp::R_ARM_ABS32
:
5375 case elfcpp::R_ARM_ABS32_NOI
:
5376 // If building a shared library (or a position-independent
5377 // executable), we need to create a dynamic relocation for
5378 // this location. The relocation applied at link time will
5379 // apply the link-time value, so we flag the location with
5380 // an R_ARM_RELATIVE relocation so the dynamic loader can
5381 // relocate it easily.
5382 if (parameters
->options().output_is_position_independent())
5384 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5385 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5386 // If we are to add more other reloc types than R_ARM_ABS32,
5387 // we need to add check_non_pic(object, r_type) here.
5388 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
5389 output_section
, data_shndx
,
5390 reloc
.get_r_offset());
5394 case elfcpp::R_ARM_REL32
:
5395 case elfcpp::R_ARM_THM_CALL
:
5396 case elfcpp::R_ARM_CALL
:
5397 case elfcpp::R_ARM_PREL31
:
5398 case elfcpp::R_ARM_JUMP24
:
5399 case elfcpp::R_ARM_THM_JUMP24
:
5400 case elfcpp::R_ARM_THM_JUMP19
:
5401 case elfcpp::R_ARM_PLT32
:
5402 case elfcpp::R_ARM_THM_ABS5
:
5403 case elfcpp::R_ARM_ABS8
:
5404 case elfcpp::R_ARM_ABS12
:
5405 case elfcpp::R_ARM_ABS16
:
5406 case elfcpp::R_ARM_BASE_ABS
:
5407 case elfcpp::R_ARM_MOVW_ABS_NC
:
5408 case elfcpp::R_ARM_MOVT_ABS
:
5409 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5410 case elfcpp::R_ARM_THM_MOVT_ABS
:
5411 case elfcpp::R_ARM_MOVW_PREL_NC
:
5412 case elfcpp::R_ARM_MOVT_PREL
:
5413 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5414 case elfcpp::R_ARM_THM_MOVT_PREL
:
5415 case elfcpp::R_ARM_THM_JUMP6
:
5416 case elfcpp::R_ARM_THM_JUMP8
:
5417 case elfcpp::R_ARM_THM_JUMP11
:
5418 case elfcpp::R_ARM_V4BX
:
5421 case elfcpp::R_ARM_GOTOFF32
:
5422 // We need a GOT section:
5423 target
->got_section(symtab
, layout
);
5426 case elfcpp::R_ARM_BASE_PREL
:
5427 // FIXME: What about this?
5430 case elfcpp::R_ARM_GOT_BREL
:
5431 case elfcpp::R_ARM_GOT_PREL
:
5433 // The symbol requires a GOT entry.
5434 Output_data_got
<32, big_endian
>* got
=
5435 target
->got_section(symtab
, layout
);
5436 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5437 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
5439 // If we are generating a shared object, we need to add a
5440 // dynamic RELATIVE relocation for this symbol's GOT entry.
5441 if (parameters
->options().output_is_position_independent())
5443 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5444 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
5445 rel_dyn
->add_local_relative(
5446 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
5447 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5453 case elfcpp::R_ARM_TARGET1
:
5454 // This should have been mapped to another type already.
5456 case elfcpp::R_ARM_COPY
:
5457 case elfcpp::R_ARM_GLOB_DAT
:
5458 case elfcpp::R_ARM_JUMP_SLOT
:
5459 case elfcpp::R_ARM_RELATIVE
:
5460 // These are relocations which should only be seen by the
5461 // dynamic linker, and should never be seen here.
5462 gold_error(_("%s: unexpected reloc %u in object file"),
5463 object
->name().c_str(), r_type
);
5467 unsupported_reloc_local(object
, r_type
);
5472 // Report an unsupported relocation against a global symbol.
5474 template<bool big_endian
>
5476 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
5477 Sized_relobj
<32, big_endian
>* object
,
5478 unsigned int r_type
,
5481 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5482 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5485 // Scan a relocation for a global symbol.
5486 // FIXME: This only handles a subset of relocation types used by Android
5487 // on ARM v5te devices.
5489 template<bool big_endian
>
5491 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
5494 Sized_relobj
<32, big_endian
>* object
,
5495 unsigned int data_shndx
,
5496 Output_section
* output_section
,
5497 const elfcpp::Rel
<32, big_endian
>& reloc
,
5498 unsigned int r_type
,
5501 r_type
= get_real_reloc_type(r_type
);
5504 case elfcpp::R_ARM_NONE
:
5507 case elfcpp::R_ARM_ABS32
:
5508 case elfcpp::R_ARM_ABS32_NOI
:
5510 // Make a dynamic relocation if necessary.
5511 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
5513 if (target
->may_need_copy_reloc(gsym
))
5515 target
->copy_reloc(symtab
, layout
, object
,
5516 data_shndx
, output_section
, gsym
, reloc
);
5518 else if (gsym
->can_use_relative_reloc(false))
5520 // If we are to add more other reloc types than R_ARM_ABS32,
5521 // we need to add check_non_pic(object, r_type) here.
5522 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5523 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
5524 output_section
, object
,
5525 data_shndx
, reloc
.get_r_offset());
5529 // If we are to add more other reloc types than R_ARM_ABS32,
5530 // we need to add check_non_pic(object, r_type) here.
5531 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5532 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5533 data_shndx
, reloc
.get_r_offset());
5539 case elfcpp::R_ARM_MOVW_ABS_NC
:
5540 case elfcpp::R_ARM_MOVT_ABS
:
5541 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5542 case elfcpp::R_ARM_THM_MOVT_ABS
:
5543 case elfcpp::R_ARM_MOVW_PREL_NC
:
5544 case elfcpp::R_ARM_MOVT_PREL
:
5545 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5546 case elfcpp::R_ARM_THM_MOVT_PREL
:
5547 case elfcpp::R_ARM_THM_JUMP6
:
5548 case elfcpp::R_ARM_THM_JUMP8
:
5549 case elfcpp::R_ARM_THM_JUMP11
:
5550 case elfcpp::R_ARM_V4BX
:
5553 case elfcpp::R_ARM_THM_ABS5
:
5554 case elfcpp::R_ARM_ABS8
:
5555 case elfcpp::R_ARM_ABS12
:
5556 case elfcpp::R_ARM_ABS16
:
5557 case elfcpp::R_ARM_BASE_ABS
:
5559 // No dynamic relocs of this kinds.
5560 // Report the error in case of PIC.
5561 int flags
= Symbol::NON_PIC_REF
;
5562 if (gsym
->type() == elfcpp::STT_FUNC
5563 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5564 flags
|= Symbol::FUNCTION_CALL
;
5565 if (gsym
->needs_dynamic_reloc(flags
))
5566 check_non_pic(object
, r_type
);
5570 case elfcpp::R_ARM_REL32
:
5571 case elfcpp::R_ARM_PREL31
:
5573 // Make a dynamic relocation if necessary.
5574 int flags
= Symbol::NON_PIC_REF
;
5575 if (gsym
->needs_dynamic_reloc(flags
))
5577 if (target
->may_need_copy_reloc(gsym
))
5579 target
->copy_reloc(symtab
, layout
, object
,
5580 data_shndx
, output_section
, gsym
, reloc
);
5584 check_non_pic(object
, r_type
);
5585 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5586 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5587 data_shndx
, reloc
.get_r_offset());
5593 case elfcpp::R_ARM_JUMP24
:
5594 case elfcpp::R_ARM_THM_JUMP24
:
5595 case elfcpp::R_ARM_THM_JUMP19
:
5596 case elfcpp::R_ARM_CALL
:
5597 case elfcpp::R_ARM_THM_CALL
:
5599 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
5600 target
->make_plt_entry(symtab
, layout
, gsym
);
5603 // Check to see if this is a function that would need a PLT
5604 // but does not get one because the function symbol is untyped.
5605 // This happens in assembly code missing a proper .type directive.
5606 if ((!gsym
->is_undefined() || parameters
->options().shared())
5607 && !parameters
->doing_static_link()
5608 && gsym
->type() == elfcpp::STT_NOTYPE
5609 && (gsym
->is_from_dynobj()
5610 || gsym
->is_undefined()
5611 || gsym
->is_preemptible()))
5612 gold_error(_("%s is not a function."),
5613 gsym
->demangled_name().c_str());
5617 case elfcpp::R_ARM_PLT32
:
5618 // If the symbol is fully resolved, this is just a relative
5619 // local reloc. Otherwise we need a PLT entry.
5620 if (gsym
->final_value_is_known())
5622 // If building a shared library, we can also skip the PLT entry
5623 // if the symbol is defined in the output file and is protected
5625 if (gsym
->is_defined()
5626 && !gsym
->is_from_dynobj()
5627 && !gsym
->is_preemptible())
5629 target
->make_plt_entry(symtab
, layout
, gsym
);
5632 case elfcpp::R_ARM_GOTOFF32
:
5633 // We need a GOT section.
5634 target
->got_section(symtab
, layout
);
5637 case elfcpp::R_ARM_BASE_PREL
:
5638 // FIXME: What about this?
5641 case elfcpp::R_ARM_GOT_BREL
:
5642 case elfcpp::R_ARM_GOT_PREL
:
5644 // The symbol requires a GOT entry.
5645 Output_data_got
<32, big_endian
>* got
=
5646 target
->got_section(symtab
, layout
);
5647 if (gsym
->final_value_is_known())
5648 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5651 // If this symbol is not fully resolved, we need to add a
5652 // GOT entry with a dynamic relocation.
5653 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
5654 if (gsym
->is_from_dynobj()
5655 || gsym
->is_undefined()
5656 || gsym
->is_preemptible())
5657 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
5658 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
5661 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
5662 rel_dyn
->add_global_relative(
5663 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
5664 gsym
->got_offset(GOT_TYPE_STANDARD
));
5670 case elfcpp::R_ARM_TARGET1
:
5671 // This should have been mapped to another type already.
5673 case elfcpp::R_ARM_COPY
:
5674 case elfcpp::R_ARM_GLOB_DAT
:
5675 case elfcpp::R_ARM_JUMP_SLOT
:
5676 case elfcpp::R_ARM_RELATIVE
:
5677 // These are relocations which should only be seen by the
5678 // dynamic linker, and should never be seen here.
5679 gold_error(_("%s: unexpected reloc %u in object file"),
5680 object
->name().c_str(), r_type
);
5684 unsupported_reloc_global(object
, r_type
, gsym
);
5689 // Process relocations for gc.
5691 template<bool big_endian
>
5693 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
5695 Sized_relobj
<32, big_endian
>* object
,
5696 unsigned int data_shndx
,
5698 const unsigned char* prelocs
,
5700 Output_section
* output_section
,
5701 bool needs_special_offset_handling
,
5702 size_t local_symbol_count
,
5703 const unsigned char* plocal_symbols
)
5705 typedef Target_arm
<big_endian
> Arm
;
5706 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5708 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
5717 needs_special_offset_handling
,
5722 // Scan relocations for a section.
5724 template<bool big_endian
>
5726 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
5728 Sized_relobj
<32, big_endian
>* object
,
5729 unsigned int data_shndx
,
5730 unsigned int sh_type
,
5731 const unsigned char* prelocs
,
5733 Output_section
* output_section
,
5734 bool needs_special_offset_handling
,
5735 size_t local_symbol_count
,
5736 const unsigned char* plocal_symbols
)
5738 typedef typename Target_arm
<big_endian
>::Scan Scan
;
5739 if (sh_type
== elfcpp::SHT_RELA
)
5741 gold_error(_("%s: unsupported RELA reloc section"),
5742 object
->name().c_str());
5746 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
5755 needs_special_offset_handling
,
5760 // Finalize the sections.
5762 template<bool big_endian
>
5764 Target_arm
<big_endian
>::do_finalize_sections(
5766 const Input_objects
* input_objects
,
5767 Symbol_table
* symtab
)
5769 // Merge processor-specific flags.
5770 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
5771 p
!= input_objects
->relobj_end();
5774 Arm_relobj
<big_endian
>* arm_relobj
=
5775 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
5776 this->merge_processor_specific_flags(
5778 arm_relobj
->processor_specific_flags());
5779 this->merge_object_attributes(arm_relobj
->name().c_str(),
5780 arm_relobj
->attributes_section_data());
5784 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
5785 p
!= input_objects
->dynobj_end();
5788 Arm_dynobj
<big_endian
>* arm_dynobj
=
5789 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
5790 this->merge_processor_specific_flags(
5792 arm_dynobj
->processor_specific_flags());
5793 this->merge_object_attributes(arm_dynobj
->name().c_str(),
5794 arm_dynobj
->attributes_section_data());
5798 const Object_attribute
* cpu_arch_attr
=
5799 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
5800 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
5801 this->set_may_use_blx(true);
5803 // Check if we need to use Cortex-A8 workaround.
5804 if (parameters
->options().user_set_fix_cortex_a8())
5805 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
5808 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
5809 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
5811 const Object_attribute
* cpu_arch_profile_attr
=
5812 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
5813 this->fix_cortex_a8_
=
5814 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
5815 && (cpu_arch_profile_attr
->int_value() == 'A'
5816 || cpu_arch_profile_attr
->int_value() == 0));
5819 // Check if we can use V4BX interworking.
5820 // The V4BX interworking stub contains BX instruction,
5821 // which is not specified for some profiles.
5822 if (this->fix_v4bx() == 2 && !this->may_use_blx())
5823 gold_error(_("unable to provide V4BX reloc interworking fix up; "
5824 "the target profile does not support BX instruction"));
5826 // Fill in some more dynamic tags.
5827 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5829 : this->plt_
->rel_plt());
5830 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
5831 this->rel_dyn_
, true);
5833 // Emit any relocs we saved in an attempt to avoid generating COPY
5835 if (this->copy_relocs_
.any_saved_relocs())
5836 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
5838 // Handle the .ARM.exidx section.
5839 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
5840 if (exidx_section
!= NULL
5841 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
5842 && !parameters
->options().relocatable())
5844 // Create __exidx_start and __exdix_end symbols.
5845 symtab
->define_in_output_data("__exidx_start", NULL
,
5846 Symbol_table::PREDEFINED
,
5847 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5848 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5850 symtab
->define_in_output_data("__exidx_end", NULL
,
5851 Symbol_table::PREDEFINED
,
5852 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
5853 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
5856 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5857 // the .ARM.exidx section.
5858 if (!layout
->script_options()->saw_phdrs_clause())
5860 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
5862 Output_segment
* exidx_segment
=
5863 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
5864 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
5869 // Create an .ARM.attributes section if there is not one already.
5870 Output_attributes_section_data
* attributes_section
=
5871 new Output_attributes_section_data(*this->attributes_section_data_
);
5872 layout
->add_output_section_data(".ARM.attributes",
5873 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
5874 attributes_section
, false, false, false,
5878 // Return whether a direct absolute static relocation needs to be applied.
5879 // In cases where Scan::local() or Scan::global() has created
5880 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5881 // of the relocation is carried in the data, and we must not
5882 // apply the static relocation.
5884 template<bool big_endian
>
5886 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
5887 const Sized_symbol
<32>* gsym
,
5890 Output_section
* output_section
)
5892 // If the output section is not allocated, then we didn't call
5893 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5895 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
5898 // For local symbols, we will have created a non-RELATIVE dynamic
5899 // relocation only if (a) the output is position independent,
5900 // (b) the relocation is absolute (not pc- or segment-relative), and
5901 // (c) the relocation is not 32 bits wide.
5903 return !(parameters
->options().output_is_position_independent()
5904 && (ref_flags
& Symbol::ABSOLUTE_REF
)
5907 // For global symbols, we use the same helper routines used in the
5908 // scan pass. If we did not create a dynamic relocation, or if we
5909 // created a RELATIVE dynamic relocation, we should apply the static
5911 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
5912 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
5913 && gsym
->can_use_relative_reloc(ref_flags
5914 & Symbol::FUNCTION_CALL
);
5915 return !has_dyn
|| is_rel
;
5918 // Perform a relocation.
5920 template<bool big_endian
>
5922 Target_arm
<big_endian
>::Relocate::relocate(
5923 const Relocate_info
<32, big_endian
>* relinfo
,
5925 Output_section
*output_section
,
5927 const elfcpp::Rel
<32, big_endian
>& rel
,
5928 unsigned int r_type
,
5929 const Sized_symbol
<32>* gsym
,
5930 const Symbol_value
<32>* psymval
,
5931 unsigned char* view
,
5932 Arm_address address
,
5933 section_size_type
/* view_size */ )
5935 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
5937 r_type
= get_real_reloc_type(r_type
);
5939 const Arm_relobj
<big_endian
>* object
=
5940 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5942 // If the final branch target of a relocation is THUMB instruction, this
5943 // is 1. Otherwise it is 0.
5944 Arm_address thumb_bit
= 0;
5945 Symbol_value
<32> symval
;
5946 bool is_weakly_undefined_without_plt
= false;
5947 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
5951 // This is a global symbol. Determine if we use PLT and if the
5952 // final target is THUMB.
5953 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
5955 // This uses a PLT, change the symbol value.
5956 symval
.set_output_value(target
->plt_section()->address()
5957 + gsym
->plt_offset());
5960 else if (gsym
->is_weak_undefined())
5962 // This is a weakly undefined symbol and we do not use PLT
5963 // for this relocation. A branch targeting this symbol will
5964 // be converted into an NOP.
5965 is_weakly_undefined_without_plt
= true;
5969 // Set thumb bit if symbol:
5970 // -Has type STT_ARM_TFUNC or
5971 // -Has type STT_FUNC, is defined and with LSB in value set.
5973 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5974 || (gsym
->type() == elfcpp::STT_FUNC
5975 && !gsym
->is_undefined()
5976 && ((psymval
->value(object
, 0) & 1) != 0)))
5983 // This is a local symbol. Determine if the final target is THUMB.
5984 // We saved this information when all the local symbols were read.
5985 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
5986 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5987 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
5992 // This is a fake relocation synthesized for a stub. It does not have
5993 // a real symbol. We just look at the LSB of the symbol value to
5994 // determine if the target is THUMB or not.
5995 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
5998 // Strip LSB if this points to a THUMB target.
6000 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6001 && ((psymval
->value(object
, 0) & 1) != 0))
6003 Arm_address stripped_value
=
6004 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
6005 symval
.set_output_value(stripped_value
);
6009 // Get the GOT offset if needed.
6010 // The GOT pointer points to the end of the GOT section.
6011 // We need to subtract the size of the GOT section to get
6012 // the actual offset to use in the relocation.
6013 bool have_got_offset
= false;
6014 unsigned int got_offset
= 0;
6017 case elfcpp::R_ARM_GOT_BREL
:
6018 case elfcpp::R_ARM_GOT_PREL
:
6021 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6022 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
6023 - target
->got_size());
6027 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6028 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6029 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
6030 - target
->got_size());
6032 have_got_offset
= true;
6039 // To look up relocation stubs, we need to pass the symbol table index of
6041 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6043 typename
Arm_relocate_functions::Status reloc_status
=
6044 Arm_relocate_functions::STATUS_OKAY
;
6047 case elfcpp::R_ARM_NONE
:
6050 case elfcpp::R_ARM_ABS8
:
6051 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6053 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
6056 case elfcpp::R_ARM_ABS12
:
6057 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6059 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
6062 case elfcpp::R_ARM_ABS16
:
6063 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6065 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
6068 case elfcpp::R_ARM_ABS32
:
6069 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6071 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6075 case elfcpp::R_ARM_ABS32_NOI
:
6076 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6078 // No thumb bit for this relocation: (S + A)
6079 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6083 case elfcpp::R_ARM_MOVW_ABS_NC
:
6084 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6086 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
6090 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6091 "a shared object; recompile with -fPIC"));
6094 case elfcpp::R_ARM_MOVT_ABS
:
6095 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6097 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
6099 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6100 "a shared object; recompile with -fPIC"));
6103 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6104 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6106 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
6110 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6111 "making a shared object; recompile with -fPIC"));
6114 case elfcpp::R_ARM_THM_MOVT_ABS
:
6115 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6117 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
6120 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6121 "making a shared object; recompile with -fPIC"));
6124 case elfcpp::R_ARM_MOVW_PREL_NC
:
6125 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
6130 case elfcpp::R_ARM_MOVT_PREL
:
6131 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
6135 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6136 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
6141 case elfcpp::R_ARM_THM_MOVT_PREL
:
6142 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
6146 case elfcpp::R_ARM_REL32
:
6147 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6148 address
, thumb_bit
);
6151 case elfcpp::R_ARM_THM_ABS5
:
6152 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6154 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
6157 case elfcpp::R_ARM_THM_CALL
:
6159 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
6160 psymval
, address
, thumb_bit
,
6161 is_weakly_undefined_without_plt
);
6164 case elfcpp::R_ARM_XPC25
:
6166 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
6167 psymval
, address
, thumb_bit
,
6168 is_weakly_undefined_without_plt
);
6171 case elfcpp::R_ARM_THM_XPC22
:
6173 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
6174 psymval
, address
, thumb_bit
,
6175 is_weakly_undefined_without_plt
);
6178 case elfcpp::R_ARM_GOTOFF32
:
6180 Arm_address got_origin
;
6181 got_origin
= target
->got_plt_section()->address();
6182 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6183 got_origin
, thumb_bit
);
6187 case elfcpp::R_ARM_BASE_PREL
:
6190 // Get the addressing origin of the output segment defining the
6191 // symbol gsym (AAELF 4.6.1.2 Relocation types)
6192 gold_assert(gsym
!= NULL
);
6193 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6194 origin
= gsym
->output_segment()->vaddr();
6195 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6196 origin
= gsym
->output_data()->address();
6199 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6200 _("cannot find origin of R_ARM_BASE_PREL"));
6203 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
6207 case elfcpp::R_ARM_BASE_ABS
:
6209 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6214 // Get the addressing origin of the output segment defining
6215 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
6217 // R_ARM_BASE_ABS with the NULL symbol will give the
6218 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
6219 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6220 origin
= target
->got_plt_section()->address();
6221 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6222 origin
= gsym
->output_segment()->vaddr();
6223 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6224 origin
= gsym
->output_data()->address();
6227 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6228 _("cannot find origin of R_ARM_BASE_ABS"));
6232 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
6236 case elfcpp::R_ARM_GOT_BREL
:
6237 gold_assert(have_got_offset
);
6238 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
6241 case elfcpp::R_ARM_GOT_PREL
:
6242 gold_assert(have_got_offset
);
6243 // Get the address origin for GOT PLT, which is allocated right
6244 // after the GOT section, to calculate an absolute address of
6245 // the symbol GOT entry (got_origin + got_offset).
6246 Arm_address got_origin
;
6247 got_origin
= target
->got_plt_section()->address();
6248 reloc_status
= Arm_relocate_functions::got_prel(view
,
6249 got_origin
+ got_offset
,
6253 case elfcpp::R_ARM_PLT32
:
6254 gold_assert(gsym
== NULL
6255 || gsym
->has_plt_offset()
6256 || gsym
->final_value_is_known()
6257 || (gsym
->is_defined()
6258 && !gsym
->is_from_dynobj()
6259 && !gsym
->is_preemptible()));
6261 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
6262 psymval
, address
, thumb_bit
,
6263 is_weakly_undefined_without_plt
);
6266 case elfcpp::R_ARM_CALL
:
6268 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
6269 psymval
, address
, thumb_bit
,
6270 is_weakly_undefined_without_plt
);
6273 case elfcpp::R_ARM_JUMP24
:
6275 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
6276 psymval
, address
, thumb_bit
,
6277 is_weakly_undefined_without_plt
);
6280 case elfcpp::R_ARM_THM_JUMP24
:
6282 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
6283 psymval
, address
, thumb_bit
,
6284 is_weakly_undefined_without_plt
);
6287 case elfcpp::R_ARM_THM_JUMP19
:
6289 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
6293 case elfcpp::R_ARM_THM_JUMP6
:
6295 Arm_relocate_functions::thm_jump6(view
, object
, psymval
, address
);
6298 case elfcpp::R_ARM_THM_JUMP8
:
6300 Arm_relocate_functions::thm_jump8(view
, object
, psymval
, address
);
6303 case elfcpp::R_ARM_THM_JUMP11
:
6305 Arm_relocate_functions::thm_jump11(view
, object
, psymval
, address
);
6308 case elfcpp::R_ARM_PREL31
:
6309 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
6310 address
, thumb_bit
);
6313 case elfcpp::R_ARM_V4BX
:
6314 if (target
->fix_v4bx() > 0)
6316 Arm_relocate_functions::v4bx(relinfo
, view
, object
, address
,
6317 (target
->fix_v4bx() == 2));
6320 case elfcpp::R_ARM_TARGET1
:
6321 // This should have been mapped to another type already.
6323 case elfcpp::R_ARM_COPY
:
6324 case elfcpp::R_ARM_GLOB_DAT
:
6325 case elfcpp::R_ARM_JUMP_SLOT
:
6326 case elfcpp::R_ARM_RELATIVE
:
6327 // These are relocations which should only be seen by the
6328 // dynamic linker, and should never be seen here.
6329 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6330 _("unexpected reloc %u in object file"),
6335 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6336 _("unsupported reloc %u"),
6341 // Report any errors.
6342 switch (reloc_status
)
6344 case Arm_relocate_functions::STATUS_OKAY
:
6346 case Arm_relocate_functions::STATUS_OVERFLOW
:
6347 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6348 _("relocation overflow in relocation %u"),
6351 case Arm_relocate_functions::STATUS_BAD_RELOC
:
6352 gold_error_at_location(
6356 _("unexpected opcode while processing relocation %u"),
6366 // Relocate section data.
6368 template<bool big_endian
>
6370 Target_arm
<big_endian
>::relocate_section(
6371 const Relocate_info
<32, big_endian
>* relinfo
,
6372 unsigned int sh_type
,
6373 const unsigned char* prelocs
,
6375 Output_section
* output_section
,
6376 bool needs_special_offset_handling
,
6377 unsigned char* view
,
6378 Arm_address address
,
6379 section_size_type view_size
,
6380 const Reloc_symbol_changes
* reloc_symbol_changes
)
6382 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
6383 gold_assert(sh_type
== elfcpp::SHT_REL
);
6385 Arm_input_section
<big_endian
>* arm_input_section
=
6386 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
6388 // This is an ARM input section and the view covers the whole output
6390 if (arm_input_section
!= NULL
)
6392 gold_assert(needs_special_offset_handling
);
6393 Arm_address section_address
= arm_input_section
->address();
6394 section_size_type section_size
= arm_input_section
->data_size();
6396 gold_assert((arm_input_section
->address() >= address
)
6397 && ((arm_input_section
->address()
6398 + arm_input_section
->data_size())
6399 <= (address
+ view_size
)));
6401 off_t offset
= section_address
- address
;
6404 view_size
= section_size
;
6407 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
6414 needs_special_offset_handling
,
6418 reloc_symbol_changes
);
6421 // Return the size of a relocation while scanning during a relocatable
6424 template<bool big_endian
>
6426 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
6427 unsigned int r_type
,
6430 r_type
= get_real_reloc_type(r_type
);
6433 case elfcpp::R_ARM_NONE
:
6436 case elfcpp::R_ARM_ABS8
:
6439 case elfcpp::R_ARM_ABS16
:
6440 case elfcpp::R_ARM_THM_ABS5
:
6441 case elfcpp::R_ARM_THM_JUMP6
:
6442 case elfcpp::R_ARM_THM_JUMP8
:
6443 case elfcpp::R_ARM_THM_JUMP11
:
6446 case elfcpp::R_ARM_ABS32
:
6447 case elfcpp::R_ARM_ABS32_NOI
:
6448 case elfcpp::R_ARM_ABS12
:
6449 case elfcpp::R_ARM_BASE_ABS
:
6450 case elfcpp::R_ARM_REL32
:
6451 case elfcpp::R_ARM_THM_CALL
:
6452 case elfcpp::R_ARM_GOTOFF32
:
6453 case elfcpp::R_ARM_BASE_PREL
:
6454 case elfcpp::R_ARM_GOT_BREL
:
6455 case elfcpp::R_ARM_GOT_PREL
:
6456 case elfcpp::R_ARM_PLT32
:
6457 case elfcpp::R_ARM_CALL
:
6458 case elfcpp::R_ARM_JUMP24
:
6459 case elfcpp::R_ARM_PREL31
:
6460 case elfcpp::R_ARM_MOVW_ABS_NC
:
6461 case elfcpp::R_ARM_MOVT_ABS
:
6462 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6463 case elfcpp::R_ARM_THM_MOVT_ABS
:
6464 case elfcpp::R_ARM_MOVW_PREL_NC
:
6465 case elfcpp::R_ARM_MOVT_PREL
:
6466 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6467 case elfcpp::R_ARM_THM_MOVT_PREL
:
6468 case elfcpp::R_ARM_V4BX
:
6471 case elfcpp::R_ARM_TARGET1
:
6472 // This should have been mapped to another type already.
6474 case elfcpp::R_ARM_COPY
:
6475 case elfcpp::R_ARM_GLOB_DAT
:
6476 case elfcpp::R_ARM_JUMP_SLOT
:
6477 case elfcpp::R_ARM_RELATIVE
:
6478 // These are relocations which should only be seen by the
6479 // dynamic linker, and should never be seen here.
6480 gold_error(_("%s: unexpected reloc %u in object file"),
6481 object
->name().c_str(), r_type
);
6485 object
->error(_("unsupported reloc %u in object file"), r_type
);
6490 // Scan the relocs during a relocatable link.
6492 template<bool big_endian
>
6494 Target_arm
<big_endian
>::scan_relocatable_relocs(
6495 Symbol_table
* symtab
,
6497 Sized_relobj
<32, big_endian
>* object
,
6498 unsigned int data_shndx
,
6499 unsigned int sh_type
,
6500 const unsigned char* prelocs
,
6502 Output_section
* output_section
,
6503 bool needs_special_offset_handling
,
6504 size_t local_symbol_count
,
6505 const unsigned char* plocal_symbols
,
6506 Relocatable_relocs
* rr
)
6508 gold_assert(sh_type
== elfcpp::SHT_REL
);
6510 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
6511 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
6513 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
6514 Scan_relocatable_relocs
>(
6522 needs_special_offset_handling
,
6528 // Relocate a section during a relocatable link.
6530 template<bool big_endian
>
6532 Target_arm
<big_endian
>::relocate_for_relocatable(
6533 const Relocate_info
<32, big_endian
>* relinfo
,
6534 unsigned int sh_type
,
6535 const unsigned char* prelocs
,
6537 Output_section
* output_section
,
6538 off_t offset_in_output_section
,
6539 const Relocatable_relocs
* rr
,
6540 unsigned char* view
,
6541 Arm_address view_address
,
6542 section_size_type view_size
,
6543 unsigned char* reloc_view
,
6544 section_size_type reloc_view_size
)
6546 gold_assert(sh_type
== elfcpp::SHT_REL
);
6548 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
6553 offset_in_output_section
,
6562 // Return the value to use for a dynamic symbol which requires special
6563 // treatment. This is how we support equality comparisons of function
6564 // pointers across shared library boundaries, as described in the
6565 // processor specific ABI supplement.
6567 template<bool big_endian
>
6569 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6571 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6572 return this->plt_section()->address() + gsym
->plt_offset();
6575 // Map platform-specific relocs to real relocs
6577 template<bool big_endian
>
6579 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
6583 case elfcpp::R_ARM_TARGET1
:
6584 // This is either R_ARM_ABS32 or R_ARM_REL32;
6585 return elfcpp::R_ARM_ABS32
;
6587 case elfcpp::R_ARM_TARGET2
:
6588 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6589 return elfcpp::R_ARM_GOT_PREL
;
6596 // Whether if two EABI versions V1 and V2 are compatible.
6598 template<bool big_endian
>
6600 Target_arm
<big_endian
>::are_eabi_versions_compatible(
6601 elfcpp::Elf_Word v1
,
6602 elfcpp::Elf_Word v2
)
6604 // v4 and v5 are the same spec before and after it was released,
6605 // so allow mixing them.
6606 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
6607 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
6613 // Combine FLAGS from an input object called NAME and the processor-specific
6614 // flags in the ELF header of the output. Much of this is adapted from the
6615 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6616 // in bfd/elf32-arm.c.
6618 template<bool big_endian
>
6620 Target_arm
<big_endian
>::merge_processor_specific_flags(
6621 const std::string
& name
,
6622 elfcpp::Elf_Word flags
)
6624 if (this->are_processor_specific_flags_set())
6626 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
6628 // Nothing to merge if flags equal to those in output.
6629 if (flags
== out_flags
)
6632 // Complain about various flag mismatches.
6633 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
6634 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
6635 if (!this->are_eabi_versions_compatible(version1
, version2
))
6636 gold_error(_("Source object %s has EABI version %d but output has "
6637 "EABI version %d."),
6639 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
6640 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
6644 // If the input is the default architecture and had the default
6645 // flags then do not bother setting the flags for the output
6646 // architecture, instead allow future merges to do this. If no
6647 // future merges ever set these flags then they will retain their
6648 // uninitialised values, which surprise surprise, correspond
6649 // to the default values.
6653 // This is the first time, just copy the flags.
6654 // We only copy the EABI version for now.
6655 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
6659 // Adjust ELF file header.
6660 template<bool big_endian
>
6662 Target_arm
<big_endian
>::do_adjust_elf_header(
6663 unsigned char* view
,
6666 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
6668 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
6669 unsigned char e_ident
[elfcpp::EI_NIDENT
];
6670 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
6672 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6673 == elfcpp::EF_ARM_EABI_UNKNOWN
)
6674 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
6676 e_ident
[elfcpp::EI_OSABI
] = 0;
6677 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
6679 // FIXME: Do EF_ARM_BE8 adjustment.
6681 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
6682 oehdr
.put_e_ident(e_ident
);
6685 // do_make_elf_object to override the same function in the base class.
6686 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6687 // to store ARM specific information. Hence we need to have our own
6688 // ELF object creation.
6690 template<bool big_endian
>
6692 Target_arm
<big_endian
>::do_make_elf_object(
6693 const std::string
& name
,
6694 Input_file
* input_file
,
6695 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
6697 int et
= ehdr
.get_e_type();
6698 if (et
== elfcpp::ET_REL
)
6700 Arm_relobj
<big_endian
>* obj
=
6701 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6705 else if (et
== elfcpp::ET_DYN
)
6707 Sized_dynobj
<32, big_endian
>* obj
=
6708 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
6714 gold_error(_("%s: unsupported ELF file type %d"),
6720 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6721 // Returns -1 if no architecture could be read.
6722 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6724 template<bool big_endian
>
6726 Target_arm
<big_endian
>::get_secondary_compatible_arch(
6727 const Attributes_section_data
* pasd
)
6729 const Object_attribute
*known_attributes
=
6730 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6732 // Note: the tag and its argument below are uleb128 values, though
6733 // currently-defined values fit in one byte for each.
6734 const std::string
& sv
=
6735 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
6737 && sv
.data()[0] == elfcpp::Tag_CPU_arch
6738 && (sv
.data()[1] & 128) != 128)
6739 return sv
.data()[1];
6741 // This tag is "safely ignorable", so don't complain if it looks funny.
6745 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6746 // The tag is removed if ARCH is -1.
6747 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6749 template<bool big_endian
>
6751 Target_arm
<big_endian
>::set_secondary_compatible_arch(
6752 Attributes_section_data
* pasd
,
6755 Object_attribute
*known_attributes
=
6756 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
6760 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
6764 // Note: the tag and its argument below are uleb128 values, though
6765 // currently-defined values fit in one byte for each.
6767 sv
[0] = elfcpp::Tag_CPU_arch
;
6768 gold_assert(arch
!= 0);
6772 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
6775 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6777 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6779 template<bool big_endian
>
6781 Target_arm
<big_endian
>::tag_cpu_arch_combine(
6784 int* secondary_compat_out
,
6786 int secondary_compat
)
6788 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6789 static const int v6t2
[] =
6801 static const int v6k
[] =
6814 static const int v7
[] =
6828 static const int v6_m
[] =
6843 static const int v6s_m
[] =
6859 static const int v7e_m
[] =
6876 static const int v4t_plus_v6_m
[] =
6892 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
6894 static const int *comb
[] =
6902 // Pseudo-architecture.
6906 // Check we've not got a higher architecture than we know about.
6908 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
6910 gold_error(_("%s: unknown CPU architecture"), name
);
6914 // Override old tag if we have a Tag_also_compatible_with on the output.
6916 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
6917 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
6918 oldtag
= T(V4T_PLUS_V6_M
);
6920 // And override the new tag if we have a Tag_also_compatible_with on the
6923 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
6924 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
6925 newtag
= T(V4T_PLUS_V6_M
);
6927 // Architectures before V6KZ add features monotonically.
6928 int tagh
= std::max(oldtag
, newtag
);
6929 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
6932 int tagl
= std::min(oldtag
, newtag
);
6933 int result
= comb
[tagh
- T(V6T2
)][tagl
];
6935 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6936 // as the canonical version.
6937 if (result
== T(V4T_PLUS_V6_M
))
6940 *secondary_compat_out
= T(V6_M
);
6943 *secondary_compat_out
= -1;
6947 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6948 name
, oldtag
, newtag
);
6956 // Helper to print AEABI enum tag value.
6958 template<bool big_endian
>
6960 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
6962 static const char *aeabi_enum_names
[] =
6963 { "", "variable-size", "32-bit", "" };
6964 const size_t aeabi_enum_names_size
=
6965 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
6967 if (value
< aeabi_enum_names_size
)
6968 return std::string(aeabi_enum_names
[value
]);
6972 sprintf(buffer
, "<unknown value %u>", value
);
6973 return std::string(buffer
);
6977 // Return the string value to store in TAG_CPU_name.
6979 template<bool big_endian
>
6981 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
6983 static const char *name_table
[] = {
6984 // These aren't real CPU names, but we can't guess
6985 // that from the architecture version alone.
7001 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
7003 if (value
< name_table_size
)
7004 return std::string(name_table
[value
]);
7008 sprintf(buffer
, "<unknown CPU value %u>", value
);
7009 return std::string(buffer
);
7013 // Merge object attributes from input file called NAME with those of the
7014 // output. The input object attributes are in the object pointed by PASD.
7016 template<bool big_endian
>
7018 Target_arm
<big_endian
>::merge_object_attributes(
7020 const Attributes_section_data
* pasd
)
7022 // Return if there is no attributes section data.
7026 // If output has no object attributes, just copy.
7027 if (this->attributes_section_data_
== NULL
)
7029 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
7033 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
7034 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
7035 Object_attribute
* out_attr
=
7036 this->attributes_section_data_
->known_attributes(vendor
);
7038 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7039 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
7040 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
7042 // Ignore mismatches if the object doesn't use floating point. */
7043 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
7044 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
7045 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
7046 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
7047 gold_error(_("%s uses VFP register arguments, output does not"),
7051 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
7053 // Merge this attribute with existing attributes.
7056 case elfcpp::Tag_CPU_raw_name
:
7057 case elfcpp::Tag_CPU_name
:
7058 // These are merged after Tag_CPU_arch.
7061 case elfcpp::Tag_ABI_optimization_goals
:
7062 case elfcpp::Tag_ABI_FP_optimization_goals
:
7063 // Use the first value seen.
7066 case elfcpp::Tag_CPU_arch
:
7068 unsigned int saved_out_attr
= out_attr
->int_value();
7069 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7070 int secondary_compat
=
7071 this->get_secondary_compatible_arch(pasd
);
7072 int secondary_compat_out
=
7073 this->get_secondary_compatible_arch(
7074 this->attributes_section_data_
);
7075 out_attr
[i
].set_int_value(
7076 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
7077 &secondary_compat_out
,
7078 in_attr
[i
].int_value(),
7080 this->set_secondary_compatible_arch(this->attributes_section_data_
,
7081 secondary_compat_out
);
7083 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7084 if (out_attr
[i
].int_value() == saved_out_attr
)
7085 ; // Leave the names alone.
7086 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
7088 // The output architecture has been changed to match the
7089 // input architecture. Use the input names.
7090 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
7091 in_attr
[elfcpp::Tag_CPU_name
].string_value());
7092 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
7093 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
7097 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
7098 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
7101 // If we still don't have a value for Tag_CPU_name,
7102 // make one up now. Tag_CPU_raw_name remains blank.
7103 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
7105 const std::string cpu_name
=
7106 this->tag_cpu_name_value(out_attr
[i
].int_value());
7107 // FIXME: If we see an unknown CPU, this will be set
7108 // to "<unknown CPU n>", where n is the attribute value.
7109 // This is different from BFD, which leaves the name alone.
7110 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
7115 case elfcpp::Tag_ARM_ISA_use
:
7116 case elfcpp::Tag_THUMB_ISA_use
:
7117 case elfcpp::Tag_WMMX_arch
:
7118 case elfcpp::Tag_Advanced_SIMD_arch
:
7119 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7120 case elfcpp::Tag_ABI_FP_rounding
:
7121 case elfcpp::Tag_ABI_FP_exceptions
:
7122 case elfcpp::Tag_ABI_FP_user_exceptions
:
7123 case elfcpp::Tag_ABI_FP_number_model
:
7124 case elfcpp::Tag_VFP_HP_extension
:
7125 case elfcpp::Tag_CPU_unaligned_access
:
7126 case elfcpp::Tag_T2EE_use
:
7127 case elfcpp::Tag_Virtualization_use
:
7128 case elfcpp::Tag_MPextension_use
:
7129 // Use the largest value specified.
7130 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7131 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7134 case elfcpp::Tag_ABI_align8_preserved
:
7135 case elfcpp::Tag_ABI_PCS_RO_data
:
7136 // Use the smallest value specified.
7137 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7138 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7141 case elfcpp::Tag_ABI_align8_needed
:
7142 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
7143 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
7144 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
7147 // This error message should be enabled once all non-conformant
7148 // binaries in the toolchain have had the attributes set
7150 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7154 case elfcpp::Tag_ABI_FP_denormal
:
7155 case elfcpp::Tag_ABI_PCS_GOT_use
:
7157 // These tags have 0 = don't care, 1 = strong requirement,
7158 // 2 = weak requirement.
7159 static const int order_021
[3] = {0, 2, 1};
7161 // Use the "greatest" from the sequence 0, 2, 1, or the largest
7162 // value if greater than 2 (for future-proofing).
7163 if ((in_attr
[i
].int_value() > 2
7164 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7165 || (in_attr
[i
].int_value() <= 2
7166 && out_attr
[i
].int_value() <= 2
7167 && (order_021
[in_attr
[i
].int_value()]
7168 > order_021
[out_attr
[i
].int_value()])))
7169 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7173 case elfcpp::Tag_CPU_arch_profile
:
7174 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
7176 // 0 will merge with anything.
7177 // 'A' and 'S' merge to 'A'.
7178 // 'R' and 'S' merge to 'R'.
7179 // 'M' and 'A|R|S' is an error.
7180 if (out_attr
[i
].int_value() == 0
7181 || (out_attr
[i
].int_value() == 'S'
7182 && (in_attr
[i
].int_value() == 'A'
7183 || in_attr
[i
].int_value() == 'R')))
7184 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7185 else if (in_attr
[i
].int_value() == 0
7186 || (in_attr
[i
].int_value() == 'S'
7187 && (out_attr
[i
].int_value() == 'A'
7188 || out_attr
[i
].int_value() == 'R')))
7193 (_("conflicting architecture profiles %c/%c"),
7194 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
7195 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
7199 case elfcpp::Tag_VFP_arch
:
7216 // Values greater than 6 aren't defined, so just pick the
7218 if (in_attr
[i
].int_value() > 6
7219 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7221 *out_attr
= *in_attr
;
7224 // The output uses the superset of input features
7225 // (ISA version) and registers.
7226 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
7227 vfp_versions
[out_attr
[i
].int_value()].ver
);
7228 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
7229 vfp_versions
[out_attr
[i
].int_value()].regs
);
7230 // This assumes all possible supersets are also a valid
7233 for (newval
= 6; newval
> 0; newval
--)
7235 if (regs
== vfp_versions
[newval
].regs
7236 && ver
== vfp_versions
[newval
].ver
)
7239 out_attr
[i
].set_int_value(newval
);
7242 case elfcpp::Tag_PCS_config
:
7243 if (out_attr
[i
].int_value() == 0)
7244 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7245 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7247 // It's sometimes ok to mix different configs, so this is only
7249 gold_warning(_("%s: conflicting platform configuration"), name
);
7252 case elfcpp::Tag_ABI_PCS_R9_use
:
7253 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
7254 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
7255 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
7257 gold_error(_("%s: conflicting use of R9"), name
);
7259 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
7260 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7262 case elfcpp::Tag_ABI_PCS_RW_data
:
7263 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7264 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7265 != elfcpp::AEABI_R9_SB
)
7266 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7267 != elfcpp::AEABI_R9_unused
))
7269 gold_error(_("%s: SB relative addressing conflicts with use "
7273 // Use the smallest value specified.
7274 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7275 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7277 case elfcpp::Tag_ABI_PCS_wchar_t
:
7278 // FIXME: Make it possible to turn off this warning.
7279 if (out_attr
[i
].int_value()
7280 && in_attr
[i
].int_value()
7281 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7283 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7284 "use %u-byte wchar_t; use of wchar_t values "
7285 "across objects may fail"),
7286 name
, in_attr
[i
].int_value(),
7287 out_attr
[i
].int_value());
7289 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
7290 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7292 case elfcpp::Tag_ABI_enum_size
:
7293 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
7295 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
7296 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
7298 // The existing object is compatible with anything.
7299 // Use whatever requirements the new object has.
7300 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7302 // FIXME: Make it possible to turn off this warning.
7303 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
7304 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7306 unsigned int in_value
= in_attr
[i
].int_value();
7307 unsigned int out_value
= out_attr
[i
].int_value();
7308 gold_warning(_("%s uses %s enums yet the output is to use "
7309 "%s enums; use of enum values across objects "
7312 this->aeabi_enum_name(in_value
).c_str(),
7313 this->aeabi_enum_name(out_value
).c_str());
7317 case elfcpp::Tag_ABI_VFP_args
:
7320 case elfcpp::Tag_ABI_WMMX_args
:
7321 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7323 gold_error(_("%s uses iWMMXt register arguments, output does "
7328 case Object_attribute::Tag_compatibility
:
7329 // Merged in target-independent code.
7331 case elfcpp::Tag_ABI_HardFP_use
:
7332 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7333 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
7334 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
7335 out_attr
[i
].set_int_value(3);
7336 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7337 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7339 case elfcpp::Tag_ABI_FP_16bit_format
:
7340 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7342 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7343 gold_error(_("fp16 format mismatch between %s and output"),
7346 if (in_attr
[i
].int_value() != 0)
7347 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7350 case elfcpp::Tag_nodefaults
:
7351 // This tag is set if it exists, but the value is unused (and is
7352 // typically zero). We don't actually need to do anything here -
7353 // the merge happens automatically when the type flags are merged
7356 case elfcpp::Tag_also_compatible_with
:
7357 // Already done in Tag_CPU_arch.
7359 case elfcpp::Tag_conformance
:
7360 // Keep the attribute if it matches. Throw it away otherwise.
7361 // No attribute means no claim to conform.
7362 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
7363 out_attr
[i
].set_string_value("");
7368 const char* err_object
= NULL
;
7370 // The "known_obj_attributes" table does contain some undefined
7371 // attributes. Ensure that there are unused.
7372 if (out_attr
[i
].int_value() != 0
7373 || out_attr
[i
].string_value() != "")
7374 err_object
= "output";
7375 else if (in_attr
[i
].int_value() != 0
7376 || in_attr
[i
].string_value() != "")
7379 if (err_object
!= NULL
)
7381 // Attribute numbers >=64 (mod 128) can be safely ignored.
7383 gold_error(_("%s: unknown mandatory EABI object attribute "
7387 gold_warning(_("%s: unknown EABI object attribute %d"),
7391 // Only pass on attributes that match in both inputs.
7392 if (!in_attr
[i
].matches(out_attr
[i
]))
7394 out_attr
[i
].set_int_value(0);
7395 out_attr
[i
].set_string_value("");
7400 // If out_attr was copied from in_attr then it won't have a type yet.
7401 if (in_attr
[i
].type() && !out_attr
[i
].type())
7402 out_attr
[i
].set_type(in_attr
[i
].type());
7405 // Merge Tag_compatibility attributes and any common GNU ones.
7406 this->attributes_section_data_
->merge(name
, pasd
);
7408 // Check for any attributes not known on ARM.
7409 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
7410 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
7411 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
7412 Other_attributes
* out_other_attributes
=
7413 this->attributes_section_data_
->other_attributes(vendor
);
7414 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
7416 while (in_iter
!= in_other_attributes
->end()
7417 || out_iter
!= out_other_attributes
->end())
7419 const char* err_object
= NULL
;
7422 // The tags for each list are in numerical order.
7423 // If the tags are equal, then merge.
7424 if (out_iter
!= out_other_attributes
->end()
7425 && (in_iter
== in_other_attributes
->end()
7426 || in_iter
->first
> out_iter
->first
))
7428 // This attribute only exists in output. We can't merge, and we
7429 // don't know what the tag means, so delete it.
7430 err_object
= "output";
7431 err_tag
= out_iter
->first
;
7432 int saved_tag
= out_iter
->first
;
7433 delete out_iter
->second
;
7434 out_other_attributes
->erase(out_iter
);
7435 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7437 else if (in_iter
!= in_other_attributes
->end()
7438 && (out_iter
!= out_other_attributes
->end()
7439 || in_iter
->first
< out_iter
->first
))
7441 // This attribute only exists in input. We can't merge, and we
7442 // don't know what the tag means, so ignore it.
7444 err_tag
= in_iter
->first
;
7447 else // The tags are equal.
7449 // As present, all attributes in the list are unknown, and
7450 // therefore can't be merged meaningfully.
7451 err_object
= "output";
7452 err_tag
= out_iter
->first
;
7454 // Only pass on attributes that match in both inputs.
7455 if (!in_iter
->second
->matches(*(out_iter
->second
)))
7457 // No match. Delete the attribute.
7458 int saved_tag
= out_iter
->first
;
7459 delete out_iter
->second
;
7460 out_other_attributes
->erase(out_iter
);
7461 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
7465 // Matched. Keep the attribute and move to the next.
7473 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7474 if ((err_tag
& 127) < 64)
7476 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7477 err_object
, err_tag
);
7481 gold_warning(_("%s: unknown EABI object attribute %d"),
7482 err_object
, err_tag
);
7488 // Return whether a relocation type used the LSB to distinguish THUMB
7490 template<bool big_endian
>
7492 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
7496 case elfcpp::R_ARM_PC24
:
7497 case elfcpp::R_ARM_ABS32
:
7498 case elfcpp::R_ARM_REL32
:
7499 case elfcpp::R_ARM_SBREL32
:
7500 case elfcpp::R_ARM_THM_CALL
:
7501 case elfcpp::R_ARM_GLOB_DAT
:
7502 case elfcpp::R_ARM_JUMP_SLOT
:
7503 case elfcpp::R_ARM_GOTOFF32
:
7504 case elfcpp::R_ARM_PLT32
:
7505 case elfcpp::R_ARM_CALL
:
7506 case elfcpp::R_ARM_JUMP24
:
7507 case elfcpp::R_ARM_THM_JUMP24
:
7508 case elfcpp::R_ARM_SBREL31
:
7509 case elfcpp::R_ARM_PREL31
:
7510 case elfcpp::R_ARM_MOVW_ABS_NC
:
7511 case elfcpp::R_ARM_MOVW_PREL_NC
:
7512 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7513 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7514 case elfcpp::R_ARM_THM_JUMP19
:
7515 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7516 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7517 case elfcpp::R_ARM_ALU_PC_G0
:
7518 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7519 case elfcpp::R_ARM_ALU_PC_G1
:
7520 case elfcpp::R_ARM_ALU_PC_G2
:
7521 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7522 case elfcpp::R_ARM_ALU_SB_G0
:
7523 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7524 case elfcpp::R_ARM_ALU_SB_G1
:
7525 case elfcpp::R_ARM_ALU_SB_G2
:
7526 case elfcpp::R_ARM_MOVW_BREL_NC
:
7527 case elfcpp::R_ARM_MOVW_BREL
:
7528 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7529 case elfcpp::R_ARM_THM_MOVW_BREL
:
7536 // Stub-generation methods for Target_arm.
7538 // Make a new Arm_input_section object.
7540 template<bool big_endian
>
7541 Arm_input_section
<big_endian
>*
7542 Target_arm
<big_endian
>::new_arm_input_section(
7546 Input_section_specifier
iss(relobj
, shndx
);
7548 Arm_input_section
<big_endian
>* arm_input_section
=
7549 new Arm_input_section
<big_endian
>(relobj
, shndx
);
7550 arm_input_section
->init();
7552 // Register new Arm_input_section in map for look-up.
7553 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
7554 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
7556 // Make sure that it we have not created another Arm_input_section
7557 // for this input section already.
7558 gold_assert(ins
.second
);
7560 return arm_input_section
;
7563 // Find the Arm_input_section object corresponding to the SHNDX-th input
7564 // section of RELOBJ.
7566 template<bool big_endian
>
7567 Arm_input_section
<big_endian
>*
7568 Target_arm
<big_endian
>::find_arm_input_section(
7570 unsigned int shndx
) const
7572 Input_section_specifier
iss(relobj
, shndx
);
7573 typename
Arm_input_section_map::const_iterator p
=
7574 this->arm_input_section_map_
.find(iss
);
7575 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
7578 // Make a new stub table.
7580 template<bool big_endian
>
7581 Stub_table
<big_endian
>*
7582 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
7584 Stub_table
<big_endian
>* stub_table
=
7585 new Stub_table
<big_endian
>(owner
);
7586 this->stub_tables_
.push_back(stub_table
);
7588 stub_table
->set_address(owner
->address() + owner
->data_size());
7589 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
7590 stub_table
->finalize_data_size();
7595 // Scan a relocation for stub generation.
7597 template<bool big_endian
>
7599 Target_arm
<big_endian
>::scan_reloc_for_stub(
7600 const Relocate_info
<32, big_endian
>* relinfo
,
7601 unsigned int r_type
,
7602 const Sized_symbol
<32>* gsym
,
7604 const Symbol_value
<32>* psymval
,
7605 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
7606 Arm_address address
)
7608 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
7610 const Arm_relobj
<big_endian
>* arm_relobj
=
7611 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7613 if (r_type
== elfcpp::R_ARM_V4BX
)
7615 const uint32_t reg
= (addend
& 0xf);
7616 if (this->fix_v4bx() == 2 && reg
< 0xf)
7618 // Try looking up an existing stub from a stub table.
7619 Stub_table
<big_endian
>* stub_table
=
7620 arm_relobj
->stub_table(relinfo
->data_shndx
);
7621 gold_assert(stub_table
!= NULL
);
7623 if (stub_table
->find_arm_v4bx_stub(reg
) == NULL
)
7625 // create a new stub and add it to stub table.
7626 Arm_v4bx_stub
* stub
=
7627 this->stub_factory().make_arm_v4bx_stub(reg
);
7628 gold_assert(stub
!= NULL
);
7629 stub_table
->add_arm_v4bx_stub(stub
);
7636 bool target_is_thumb
;
7637 Symbol_value
<32> symval
;
7640 // This is a global symbol. Determine if we use PLT and if the
7641 // final target is THUMB.
7642 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
7644 // This uses a PLT, change the symbol value.
7645 symval
.set_output_value(this->plt_section()->address()
7646 + gsym
->plt_offset());
7648 target_is_thumb
= false;
7650 else if (gsym
->is_undefined())
7651 // There is no need to generate a stub symbol is undefined.
7656 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
7657 || (gsym
->type() == elfcpp::STT_FUNC
7658 && !gsym
->is_undefined()
7659 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
7664 // This is a local symbol. Determine if the final target is THUMB.
7665 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
7668 // Strip LSB if this points to a THUMB target.
7670 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
7671 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
7673 Arm_address stripped_value
=
7674 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
7675 symval
.set_output_value(stripped_value
);
7679 // Get the symbol value.
7680 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
7682 // Owing to pipelining, the PC relative branches below actually skip
7683 // two instructions when the branch offset is 0.
7684 Arm_address destination
;
7687 case elfcpp::R_ARM_CALL
:
7688 case elfcpp::R_ARM_JUMP24
:
7689 case elfcpp::R_ARM_PLT32
:
7691 destination
= value
+ addend
+ 8;
7693 case elfcpp::R_ARM_THM_CALL
:
7694 case elfcpp::R_ARM_THM_XPC22
:
7695 case elfcpp::R_ARM_THM_JUMP24
:
7696 case elfcpp::R_ARM_THM_JUMP19
:
7698 destination
= value
+ addend
+ 4;
7704 Reloc_stub
* stub
= NULL
;
7705 Stub_type stub_type
=
7706 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
7708 if (stub_type
!= arm_stub_none
)
7710 // Try looking up an existing stub from a stub table.
7711 Stub_table
<big_endian
>* stub_table
=
7712 arm_relobj
->stub_table(relinfo
->data_shndx
);
7713 gold_assert(stub_table
!= NULL
);
7715 // Locate stub by destination.
7716 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
7718 // Create a stub if there is not one already
7719 stub
= stub_table
->find_reloc_stub(stub_key
);
7722 // create a new stub and add it to stub table.
7723 stub
= this->stub_factory().make_reloc_stub(stub_type
);
7724 stub_table
->add_reloc_stub(stub
, stub_key
);
7727 // Record the destination address.
7728 stub
->set_destination_address(destination
7729 | (target_is_thumb
? 1 : 0));
7732 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7733 if (this->fix_cortex_a8_
7734 && (r_type
== elfcpp::R_ARM_THM_JUMP24
7735 || r_type
== elfcpp::R_ARM_THM_JUMP19
7736 || r_type
== elfcpp::R_ARM_THM_CALL
7737 || r_type
== elfcpp::R_ARM_THM_XPC22
)
7738 && (address
& 0xfffU
) == 0xffeU
)
7740 // Found a candidate. Note we haven't checked the destination is
7741 // within 4K here: if we do so (and don't create a record) we can't
7742 // tell that a branch should have been relocated when scanning later.
7743 this->cortex_a8_relocs_info_
[address
] =
7744 new Cortex_a8_reloc(stub
, r_type
,
7745 destination
| (target_is_thumb
? 1 : 0));
7749 // This function scans a relocation sections for stub generation.
7750 // The template parameter Relocate must be a class type which provides
7751 // a single function, relocate(), which implements the machine
7752 // specific part of a relocation.
7754 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7755 // SHT_REL or SHT_RELA.
7757 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7758 // of relocs. OUTPUT_SECTION is the output section.
7759 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7760 // mapped to output offsets.
7762 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7763 // VIEW_SIZE is the size. These refer to the input section, unless
7764 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7765 // the output section.
7767 template<bool big_endian
>
7768 template<int sh_type
>
7770 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
7771 const Relocate_info
<32, big_endian
>* relinfo
,
7772 const unsigned char* prelocs
,
7774 Output_section
* output_section
,
7775 bool needs_special_offset_handling
,
7776 const unsigned char* view
,
7777 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
7780 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
7781 const int reloc_size
=
7782 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
7784 Arm_relobj
<big_endian
>* arm_object
=
7785 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
7786 unsigned int local_count
= arm_object
->local_symbol_count();
7788 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
7790 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
7792 Reltype
reloc(prelocs
);
7794 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
7795 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
7796 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
7798 r_type
= this->get_real_reloc_type(r_type
);
7800 // Only a few relocation types need stubs.
7801 if ((r_type
!= elfcpp::R_ARM_CALL
)
7802 && (r_type
!= elfcpp::R_ARM_JUMP24
)
7803 && (r_type
!= elfcpp::R_ARM_PLT32
)
7804 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
7805 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
7806 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
7807 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
)
7808 && (r_type
!= elfcpp::R_ARM_V4BX
))
7811 section_offset_type offset
=
7812 convert_to_section_size_type(reloc
.get_r_offset());
7814 if (needs_special_offset_handling
)
7816 offset
= output_section
->output_offset(relinfo
->object
,
7817 relinfo
->data_shndx
,
7823 if (r_type
== elfcpp::R_ARM_V4BX
)
7825 // Get the BX instruction.
7826 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7827 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ offset
);
7828 elfcpp::Elf_types
<32>::Elf_Swxword insn
=
7829 elfcpp::Swap
<32, big_endian
>::readval(wv
);
7830 this->scan_reloc_for_stub(relinfo
, r_type
, NULL
, 0, NULL
,
7836 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
7837 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
7838 stub_addend_reader(r_type
, view
+ offset
, reloc
);
7840 const Sized_symbol
<32>* sym
;
7842 Symbol_value
<32> symval
;
7843 const Symbol_value
<32> *psymval
;
7844 if (r_sym
< local_count
)
7847 psymval
= arm_object
->local_symbol(r_sym
);
7849 // If the local symbol belongs to a section we are discarding,
7850 // and that section is a debug section, try to find the
7851 // corresponding kept section and map this symbol to its
7852 // counterpart in the kept section. The symbol must not
7853 // correspond to a section we are folding.
7855 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7857 && shndx
!= elfcpp::SHN_UNDEF
7858 && !arm_object
->is_section_included(shndx
)
7859 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
7861 if (comdat_behavior
== CB_UNDETERMINED
)
7864 arm_object
->section_name(relinfo
->data_shndx
);
7865 comdat_behavior
= get_comdat_behavior(name
.c_str());
7867 if (comdat_behavior
== CB_PRETEND
)
7870 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
7871 arm_object
->map_to_kept_section(shndx
, &found
);
7873 symval
.set_output_value(value
+ psymval
->input_value());
7875 symval
.set_output_value(0);
7879 symval
.set_output_value(0);
7881 symval
.set_no_output_symtab_entry();
7887 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
7888 gold_assert(gsym
!= NULL
);
7889 if (gsym
->is_forwarder())
7890 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7892 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
7893 if (sym
->has_symtab_index())
7894 symval
.set_output_symtab_index(sym
->symtab_index());
7896 symval
.set_no_output_symtab_entry();
7898 // We need to compute the would-be final value of this global
7900 const Symbol_table
* symtab
= relinfo
->symtab
;
7901 const Sized_symbol
<32>* sized_symbol
=
7902 symtab
->get_sized_symbol
<32>(gsym
);
7903 Symbol_table::Compute_final_value_status status
;
7905 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
7907 // Skip this if the symbol has not output section.
7908 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
7911 symval
.set_output_value(value
);
7915 // If symbol is a section symbol, we don't know the actual type of
7916 // destination. Give up.
7917 if (psymval
->is_section_symbol())
7920 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
7921 addend
, view_address
+ offset
);
7925 // Scan an input section for stub generation.
7927 template<bool big_endian
>
7929 Target_arm
<big_endian
>::scan_section_for_stubs(
7930 const Relocate_info
<32, big_endian
>* relinfo
,
7931 unsigned int sh_type
,
7932 const unsigned char* prelocs
,
7934 Output_section
* output_section
,
7935 bool needs_special_offset_handling
,
7936 const unsigned char* view
,
7937 Arm_address view_address
,
7938 section_size_type view_size
)
7940 if (sh_type
== elfcpp::SHT_REL
)
7941 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
7946 needs_special_offset_handling
,
7950 else if (sh_type
== elfcpp::SHT_RELA
)
7951 // We do not support RELA type relocations yet. This is provided for
7953 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
7958 needs_special_offset_handling
,
7966 // Group input sections for stub generation.
7968 // We goup input sections in an output sections so that the total size,
7969 // including any padding space due to alignment is smaller than GROUP_SIZE
7970 // unless the only input section in group is bigger than GROUP_SIZE already.
7971 // Then an ARM stub table is created to follow the last input section
7972 // in group. For each group an ARM stub table is created an is placed
7973 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7974 // extend the group after the stub table.
7976 template<bool big_endian
>
7978 Target_arm
<big_endian
>::group_sections(
7980 section_size_type group_size
,
7981 bool stubs_always_after_branch
)
7983 // Group input sections and insert stub table
7984 Layout::Section_list section_list
;
7985 layout
->get_allocated_sections(§ion_list
);
7986 for (Layout::Section_list::const_iterator p
= section_list
.begin();
7987 p
!= section_list
.end();
7990 Arm_output_section
<big_endian
>* output_section
=
7991 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
7992 output_section
->group_sections(group_size
, stubs_always_after_branch
,
7997 // Relaxation hook. This is where we do stub generation.
7999 template<bool big_endian
>
8001 Target_arm
<big_endian
>::do_relax(
8003 const Input_objects
* input_objects
,
8004 Symbol_table
* symtab
,
8007 // No need to generate stubs if this is a relocatable link.
8008 gold_assert(!parameters
->options().relocatable());
8010 // If this is the first pass, we need to group input sections into
8014 // Determine the stub group size. The group size is the absolute
8015 // value of the parameter --stub-group-size. If --stub-group-size
8016 // is passed a negative value, we restict stubs to be always after
8017 // the stubbed branches.
8018 int32_t stub_group_size_param
=
8019 parameters
->options().stub_group_size();
8020 bool stubs_always_after_branch
= stub_group_size_param
< 0;
8021 section_size_type stub_group_size
= abs(stub_group_size_param
);
8023 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8024 // page as the first half of a 32-bit branch straddling two 4K pages.
8025 // This is a crude way of enforcing that.
8026 if (this->fix_cortex_a8_
)
8027 stubs_always_after_branch
= true;
8029 if (stub_group_size
== 1)
8032 // Thumb branch range is +-4MB has to be used as the default
8033 // maximum size (a given section can contain both ARM and Thumb
8034 // code, so the worst case has to be taken into account).
8036 // This value is 24K less than that, which allows for 2025
8037 // 12-byte stubs. If we exceed that, then we will fail to link.
8038 // The user will have to relink with an explicit group size
8040 stub_group_size
= 4170000;
8043 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
8046 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8047 // beginning of each relaxation pass, just blow away all the stubs.
8048 // Alternatively, we could selectively remove only the stubs and reloc
8049 // information for code sections that have moved since the last pass.
8050 // That would require more book-keeping.
8051 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
8052 if (this->fix_cortex_a8_
)
8054 // Clear all Cortex-A8 reloc information.
8055 for (typename
Cortex_a8_relocs_info::const_iterator p
=
8056 this->cortex_a8_relocs_info_
.begin();
8057 p
!= this->cortex_a8_relocs_info_
.end();
8060 this->cortex_a8_relocs_info_
.clear();
8062 // Remove all Cortex-A8 stubs.
8063 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8064 sp
!= this->stub_tables_
.end();
8066 (*sp
)->remove_all_cortex_a8_stubs();
8069 // Scan relocs for relocation stubs
8070 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
8071 op
!= input_objects
->relobj_end();
8074 Arm_relobj
<big_endian
>* arm_relobj
=
8075 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
8076 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
8079 // Check all stub tables to see if any of them have their data sizes
8080 // or addresses alignments changed. These are the only things that
8082 bool any_stub_table_changed
= false;
8083 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8084 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8087 if ((*sp
)->update_data_size_and_addralign())
8088 any_stub_table_changed
= true;
8091 // Finalize the stubs in the last relaxation pass.
8092 if (!any_stub_table_changed
)
8093 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8094 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8096 (*sp
)->finalize_stubs();
8098 return any_stub_table_changed
;
8103 template<bool big_endian
>
8105 Target_arm
<big_endian
>::relocate_stub(
8107 const Relocate_info
<32, big_endian
>* relinfo
,
8108 Output_section
* output_section
,
8109 unsigned char* view
,
8110 Arm_address address
,
8111 section_size_type view_size
)
8114 const Stub_template
* stub_template
= stub
->stub_template();
8115 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
8117 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
8118 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
8120 unsigned int r_type
= insn
->r_type();
8121 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
8122 section_size_type reloc_size
= insn
->size();
8123 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
8125 // This is the address of the stub destination.
8126 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
8127 Symbol_value
<32> symval
;
8128 symval
.set_output_value(target
);
8130 // Synthesize a fake reloc just in case. We don't have a symbol so
8132 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
8133 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
8134 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
8135 reloc_write
.put_r_offset(reloc_offset
);
8136 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
8137 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
8139 relocate
.relocate(relinfo
, this, output_section
,
8140 this->fake_relnum_for_stubs
, rel
, r_type
,
8141 NULL
, &symval
, view
+ reloc_offset
,
8142 address
+ reloc_offset
, reloc_size
);
8146 // Determine whether an object attribute tag takes an integer, a
8149 template<bool big_endian
>
8151 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
8153 if (tag
== Object_attribute::Tag_compatibility
)
8154 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8155 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
8156 else if (tag
== elfcpp::Tag_nodefaults
)
8157 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8158 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
8159 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
8160 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
8162 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
8164 return ((tag
& 1) != 0
8165 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
8166 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8169 // Reorder attributes.
8171 // The ABI defines that Tag_conformance should be emitted first, and that
8172 // Tag_nodefaults should be second (if either is defined). This sets those
8173 // two positions, and bumps up the position of all the remaining tags to
8176 template<bool big_endian
>
8178 Target_arm
<big_endian
>::do_attributes_order(int num
) const
8180 // Reorder the known object attributes in output. We want to move
8181 // Tag_conformance to position 4 and Tag_conformance to position 5
8182 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
8184 return elfcpp::Tag_conformance
;
8186 return elfcpp::Tag_nodefaults
;
8187 if ((num
- 2) < elfcpp::Tag_nodefaults
)
8189 if ((num
- 1) < elfcpp::Tag_conformance
)
8194 // Scan a span of THUMB code for Cortex-A8 erratum.
8196 template<bool big_endian
>
8198 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
8199 Arm_relobj
<big_endian
>* arm_relobj
,
8201 section_size_type span_start
,
8202 section_size_type span_end
,
8203 const unsigned char* view
,
8204 Arm_address address
)
8206 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
8208 // The opcode is BLX.W, BL.W, B.W, Bcc.W
8209 // The branch target is in the same 4KB region as the
8210 // first half of the branch.
8211 // The instruction before the branch is a 32-bit
8212 // length non-branch instruction.
8213 section_size_type i
= span_start
;
8214 bool last_was_32bit
= false;
8215 bool last_was_branch
= false;
8216 while (i
< span_end
)
8218 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8219 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
8220 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8221 bool is_blx
= false, is_b
= false;
8222 bool is_bl
= false, is_bcc
= false;
8224 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
8227 // Load the rest of the insn (in manual-friendly order).
8228 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8230 // Encoding T4: B<c>.W.
8231 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
8232 // Encoding T1: BL<c>.W.
8233 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
8234 // Encoding T2: BLX<c>.W.
8235 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
8236 // Encoding T3: B<c>.W (not permitted in IT block).
8237 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
8238 && (insn
& 0x07f00000U
) != 0x03800000U
);
8241 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
8243 // If this instruction is a 32-bit THUMB branch that crosses a 4K
8244 // page boundary and it follows 32-bit non-branch instruction,
8245 // we need to work around.
8247 && ((address
+ i
) & 0xfffU
) == 0xffeU
8249 && !last_was_branch
)
8251 // Check to see if there is a relocation stub for this branch.
8252 bool force_target_arm
= false;
8253 bool force_target_thumb
= false;
8254 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
8255 Cortex_a8_relocs_info::const_iterator p
=
8256 this->cortex_a8_relocs_info_
.find(address
+ i
);
8258 if (p
!= this->cortex_a8_relocs_info_
.end())
8260 cortex_a8_reloc
= p
->second
;
8261 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
8263 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8264 && !target_is_thumb
)
8265 force_target_arm
= true;
8266 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8268 force_target_thumb
= true;
8272 Stub_type stub_type
= arm_stub_none
;
8274 // Check if we have an offending branch instruction.
8275 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
8276 uint16_t lower_insn
= insn
& 0xffffU
;
8277 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8279 if (cortex_a8_reloc
!= NULL
8280 && cortex_a8_reloc
->reloc_stub() != NULL
)
8281 // We've already made a stub for this instruction, e.g.
8282 // it's a long branch or a Thumb->ARM stub. Assume that
8283 // stub will suffice to work around the A8 erratum (see
8284 // setting of always_after_branch above).
8288 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
8290 stub_type
= arm_stub_a8_veneer_b_cond
;
8292 else if (is_b
|| is_bl
|| is_blx
)
8294 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
8300 ? arm_stub_a8_veneer_blx
8302 ? arm_stub_a8_veneer_bl
8303 : arm_stub_a8_veneer_b
));
8306 if (stub_type
!= arm_stub_none
)
8308 Arm_address pc_for_insn
= address
+ i
+ 4;
8310 // The original instruction is a BL, but the target is
8311 // an ARM instruction. If we were not making a stub,
8312 // the BL would have been converted to a BLX. Use the
8313 // BLX stub instead in that case.
8314 if (this->may_use_blx() && force_target_arm
8315 && stub_type
== arm_stub_a8_veneer_bl
)
8317 stub_type
= arm_stub_a8_veneer_blx
;
8321 // Conversely, if the original instruction was
8322 // BLX but the target is Thumb mode, use the BL stub.
8323 else if (force_target_thumb
8324 && stub_type
== arm_stub_a8_veneer_blx
)
8326 stub_type
= arm_stub_a8_veneer_bl
;
8334 // If we found a relocation, use the proper destination,
8335 // not the offset in the (unrelocated) instruction.
8336 // Note this is always done if we switched the stub type above.
8337 if (cortex_a8_reloc
!= NULL
)
8338 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
8340 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
8342 // Add a new stub if destination address in in the same page.
8343 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
8345 Cortex_a8_stub
* stub
=
8346 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
8350 Stub_table
<big_endian
>* stub_table
=
8351 arm_relobj
->stub_table(shndx
);
8352 gold_assert(stub_table
!= NULL
);
8353 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
8358 i
+= insn_32bit
? 4 : 2;
8359 last_was_32bit
= insn_32bit
;
8360 last_was_branch
= is_32bit_branch
;
8364 // Apply the Cortex-A8 workaround.
8366 template<bool big_endian
>
8368 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
8369 const Cortex_a8_stub
* stub
,
8370 Arm_address stub_address
,
8371 unsigned char* insn_view
,
8372 Arm_address insn_address
)
8374 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8375 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
8376 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8377 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8378 off_t branch_offset
= stub_address
- (insn_address
+ 4);
8380 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8381 switch (stub
->stub_template()->type())
8383 case arm_stub_a8_veneer_b_cond
:
8384 gold_assert(!utils::has_overflow
<21>(branch_offset
));
8385 upper_insn
= RelocFuncs::thumb32_cond_branch_upper(upper_insn
,
8387 lower_insn
= RelocFuncs::thumb32_cond_branch_lower(lower_insn
,
8391 case arm_stub_a8_veneer_b
:
8392 case arm_stub_a8_veneer_bl
:
8393 case arm_stub_a8_veneer_blx
:
8394 if ((lower_insn
& 0x5000U
) == 0x4000U
)
8395 // For a BLX instruction, make sure that the relocation is
8396 // rounded up to a word boundary. This follows the semantics of
8397 // the instruction which specifies that bit 1 of the target
8398 // address will come from bit 1 of the base address.
8399 branch_offset
= (branch_offset
+ 2) & ~3;
8401 // Put BRANCH_OFFSET back into the insn.
8402 gold_assert(!utils::has_overflow
<25>(branch_offset
));
8403 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
8404 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
8411 // Put the relocated value back in the object file:
8412 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
8413 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
8416 template<bool big_endian
>
8417 class Target_selector_arm
: public Target_selector
8420 Target_selector_arm()
8421 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
8422 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
8426 do_instantiate_target()
8427 { return new Target_arm
<big_endian
>(); }
8430 Target_selector_arm
<false> target_selector_arm
;
8431 Target_selector_arm
<true> target_selector_armbe
;
8433 } // End anonymous namespace.