1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
37 #include "parameters.h"
44 #include "copy-relocs.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
51 #include "attributes.h"
58 template<bool big_endian
>
59 class Output_data_plt_arm
;
61 template<bool big_endian
>
64 template<bool big_endian
>
65 class Arm_input_section
;
67 class Arm_exidx_cantunwind
;
69 class Arm_exidx_merged_section
;
71 class Arm_exidx_fixup
;
73 template<bool big_endian
>
74 class Arm_output_section
;
76 class Arm_exidx_input_section
;
78 template<bool big_endian
>
81 template<bool big_endian
>
85 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
87 // Maximum branch offsets for ARM, THUMB and THUMB2.
88 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
89 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
90 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
91 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
92 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
93 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
95 // The arm target class.
97 // This is a very simple port of gold for ARM-EABI. It is intended for
98 // supporting Android only for the time being. Only these relocation types
127 // R_ARM_THM_MOVW_ABS_NC
128 // R_ARM_THM_MOVT_ABS
129 // R_ARM_MOVW_PREL_NC
131 // R_ARM_THM_MOVW_PREL_NC
132 // R_ARM_THM_MOVT_PREL
139 // - Support more relocation types as needed.
140 // - Make PLTs more flexible for different architecture features like
142 // There are probably a lot more.
144 // Instruction template class. This class is similar to the insn_sequence
145 // struct in bfd/elf32-arm.c.
150 // Types of instruction templates.
154 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
155 // templates with class-specific semantics. Currently this is used
156 // only by the Cortex_a8_stub class for handling condition codes in
157 // conditional branches.
158 THUMB16_SPECIAL_TYPE
,
164 // Factory methods to create instruction templates in different formats.
166 static const Insn_template
167 thumb16_insn(uint32_t data
)
168 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
170 // A Thumb conditional branch, in which the proper condition is inserted
171 // when we build the stub.
172 static const Insn_template
173 thumb16_bcond_insn(uint32_t data
)
174 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
176 static const Insn_template
177 thumb32_insn(uint32_t data
)
178 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
180 static const Insn_template
181 thumb32_b_insn(uint32_t data
, int reloc_addend
)
183 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
187 static const Insn_template
188 arm_insn(uint32_t data
)
189 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
191 static const Insn_template
192 arm_rel_insn(unsigned data
, int reloc_addend
)
193 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
195 static const Insn_template
196 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
197 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
199 // Accessors. This class is used for read-only objects so no modifiers
204 { return this->data_
; }
206 // Return the instruction sequence type of this.
209 { return this->type_
; }
211 // Return the ARM relocation type of this.
214 { return this->r_type_
; }
218 { return this->reloc_addend_
; }
220 // Return size of instruction template in bytes.
224 // Return byte-alignment of instruction template.
229 // We make the constructor private to ensure that only the factory
232 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
233 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
236 // Instruction specific data. This is used to store information like
237 // some of the instruction bits.
239 // Instruction template type.
241 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
242 unsigned int r_type_
;
243 // Relocation addend.
244 int32_t reloc_addend_
;
247 // Macro for generating code to stub types. One entry per long/short
251 DEF_STUB(long_branch_any_any) \
252 DEF_STUB(long_branch_v4t_arm_thumb) \
253 DEF_STUB(long_branch_thumb_only) \
254 DEF_STUB(long_branch_v4t_thumb_thumb) \
255 DEF_STUB(long_branch_v4t_thumb_arm) \
256 DEF_STUB(short_branch_v4t_thumb_arm) \
257 DEF_STUB(long_branch_any_arm_pic) \
258 DEF_STUB(long_branch_any_thumb_pic) \
259 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
260 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
261 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
262 DEF_STUB(long_branch_thumb_only_pic) \
263 DEF_STUB(a8_veneer_b_cond) \
264 DEF_STUB(a8_veneer_b) \
265 DEF_STUB(a8_veneer_bl) \
266 DEF_STUB(a8_veneer_blx) \
267 DEF_STUB(v4_veneer_bx)
271 #define DEF_STUB(x) arm_stub_##x,
277 // First reloc stub type.
278 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
279 // Last reloc stub type.
280 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
282 // First Cortex-A8 stub type.
283 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
284 // Last Cortex-A8 stub type.
285 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
288 arm_stub_type_last
= arm_stub_v4_veneer_bx
292 // Stub template class. Templates are meant to be read-only objects.
293 // A stub template for a stub type contains all read-only attributes
294 // common to all stubs of the same type.
299 Stub_template(Stub_type
, const Insn_template
*, size_t);
307 { return this->type_
; }
309 // Return an array of instruction templates.
312 { return this->insns_
; }
314 // Return size of template in number of instructions.
317 { return this->insn_count_
; }
319 // Return size of template in bytes.
322 { return this->size_
; }
324 // Return alignment of the stub template.
327 { return this->alignment_
; }
329 // Return whether entry point is in thumb mode.
331 entry_in_thumb_mode() const
332 { return this->entry_in_thumb_mode_
; }
334 // Return number of relocations in this template.
337 { return this->relocs_
.size(); }
339 // Return index of the I-th instruction with relocation.
341 reloc_insn_index(size_t i
) const
343 gold_assert(i
< this->relocs_
.size());
344 return this->relocs_
[i
].first
;
347 // Return the offset of the I-th instruction with relocation from the
348 // beginning of the stub.
350 reloc_offset(size_t i
) const
352 gold_assert(i
< this->relocs_
.size());
353 return this->relocs_
[i
].second
;
357 // This contains information about an instruction template with a relocation
358 // and its offset from start of stub.
359 typedef std::pair
<size_t, section_size_type
> Reloc
;
361 // A Stub_template may not be copied. We want to share templates as much
363 Stub_template(const Stub_template
&);
364 Stub_template
& operator=(const Stub_template
&);
368 // Points to an array of Insn_templates.
369 const Insn_template
* insns_
;
370 // Number of Insn_templates in insns_[].
372 // Size of templated instructions in bytes.
374 // Alignment of templated instructions.
376 // Flag to indicate if entry is in thumb mode.
377 bool entry_in_thumb_mode_
;
378 // A table of reloc instruction indices and offsets. We can find these by
379 // looking at the instruction templates but we pre-compute and then stash
380 // them here for speed.
381 std::vector
<Reloc
> relocs_
;
385 // A class for code stubs. This is a base class for different type of
386 // stubs used in the ARM target.
392 static const section_offset_type invalid_offset
=
393 static_cast<section_offset_type
>(-1);
396 Stub(const Stub_template
* stub_template
)
397 : stub_template_(stub_template
), offset_(invalid_offset
)
404 // Return the stub template.
406 stub_template() const
407 { return this->stub_template_
; }
409 // Return offset of code stub from beginning of its containing stub table.
413 gold_assert(this->offset_
!= invalid_offset
);
414 return this->offset_
;
417 // Set offset of code stub from beginning of its containing stub table.
419 set_offset(section_offset_type offset
)
420 { this->offset_
= offset
; }
422 // Return the relocation target address of the i-th relocation in the
423 // stub. This must be defined in a child class.
425 reloc_target(size_t i
)
426 { return this->do_reloc_target(i
); }
428 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
430 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
431 { this->do_write(view
, view_size
, big_endian
); }
433 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
434 // for the i-th instruction.
436 thumb16_special(size_t i
)
437 { return this->do_thumb16_special(i
); }
440 // This must be defined in the child class.
442 do_reloc_target(size_t) = 0;
444 // This may be overridden in the child class.
446 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
449 this->do_fixed_endian_write
<true>(view
, view_size
);
451 this->do_fixed_endian_write
<false>(view
, view_size
);
454 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
455 // instruction template.
457 do_thumb16_special(size_t)
458 { gold_unreachable(); }
461 // A template to implement do_write.
462 template<bool big_endian
>
464 do_fixed_endian_write(unsigned char*, section_size_type
);
467 const Stub_template
* stub_template_
;
468 // Offset within the section of containing this stub.
469 section_offset_type offset_
;
472 // Reloc stub class. These are stubs we use to fix up relocation because
473 // of limited branch ranges.
475 class Reloc_stub
: public Stub
478 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
479 // We assume we never jump to this address.
480 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
482 // Return destination address.
484 destination_address() const
486 gold_assert(this->destination_address_
!= this->invalid_address
);
487 return this->destination_address_
;
490 // Set destination address.
492 set_destination_address(Arm_address address
)
494 gold_assert(address
!= this->invalid_address
);
495 this->destination_address_
= address
;
498 // Reset destination address.
500 reset_destination_address()
501 { this->destination_address_
= this->invalid_address
; }
503 // Determine stub type for a branch of a relocation of R_TYPE going
504 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
505 // the branch target is a thumb instruction. TARGET is used for look
506 // up ARM-specific linker settings.
508 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
509 Arm_address branch_target
, bool target_is_thumb
);
511 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
512 // and an addend. Since we treat global and local symbol differently, we
513 // use a Symbol object for a global symbol and a object-index pair for
518 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
519 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
520 // and R_SYM must not be invalid_index.
521 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
522 unsigned int r_sym
, int32_t addend
)
523 : stub_type_(stub_type
), addend_(addend
)
527 this->r_sym_
= Reloc_stub::invalid_index
;
528 this->u_
.symbol
= symbol
;
532 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
533 this->r_sym_
= r_sym
;
534 this->u_
.relobj
= relobj
;
541 // Accessors: Keys are meant to be read-only object so no modifiers are
547 { return this->stub_type_
; }
549 // Return the local symbol index or invalid_index.
552 { return this->r_sym_
; }
554 // Return the symbol if there is one.
557 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
559 // Return the relobj if there is one.
562 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
564 // Whether this equals to another key k.
566 eq(const Key
& k
) const
568 return ((this->stub_type_
== k
.stub_type_
)
569 && (this->r_sym_
== k
.r_sym_
)
570 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
571 ? (this->u_
.relobj
== k
.u_
.relobj
)
572 : (this->u_
.symbol
== k
.u_
.symbol
))
573 && (this->addend_
== k
.addend_
));
576 // Return a hash value.
580 return (this->stub_type_
582 ^ gold::string_hash
<char>(
583 (this->r_sym_
!= Reloc_stub::invalid_index
)
584 ? this->u_
.relobj
->name().c_str()
585 : this->u_
.symbol
->name())
589 // Functors for STL associative containers.
593 operator()(const Key
& k
) const
594 { return k
.hash_value(); }
600 operator()(const Key
& k1
, const Key
& k2
) const
601 { return k1
.eq(k2
); }
604 // Name of key. This is mainly for debugging.
610 Stub_type stub_type_
;
611 // If this is a local symbol, this is the index in the defining object.
612 // Otherwise, it is invalid_index for a global symbol.
614 // If r_sym_ is invalid index. This points to a global symbol.
615 // Otherwise, this points a relobj. We used the unsized and target
616 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
617 // Arm_relobj. This is done to avoid making the stub class a template
618 // as most of the stub machinery is endianity-neutral. However, it
619 // may require a bit of casting done by users of this class.
622 const Symbol
* symbol
;
623 const Relobj
* relobj
;
625 // Addend associated with a reloc.
630 // Reloc_stubs are created via a stub factory. So these are protected.
631 Reloc_stub(const Stub_template
* stub_template
)
632 : Stub(stub_template
), destination_address_(invalid_address
)
638 friend class Stub_factory
;
640 // Return the relocation target address of the i-th relocation in the
643 do_reloc_target(size_t i
)
645 // All reloc stub have only one relocation.
647 return this->destination_address_
;
651 // Address of destination.
652 Arm_address destination_address_
;
655 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
656 // THUMB branch that meets the following conditions:
658 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
659 // branch address is 0xffe.
660 // 2. The branch target address is in the same page as the first word of the
662 // 3. The branch follows a 32-bit instruction which is not a branch.
664 // To do the fix up, we need to store the address of the branch instruction
665 // and its target at least. We also need to store the original branch
666 // instruction bits for the condition code in a conditional branch. The
667 // condition code is used in a special instruction template. We also want
668 // to identify input sections needing Cortex-A8 workaround quickly. We store
669 // extra information about object and section index of the code section
670 // containing a branch being fixed up. The information is used to mark
671 // the code section when we finalize the Cortex-A8 stubs.
674 class Cortex_a8_stub
: public Stub
680 // Return the object of the code section containing the branch being fixed
684 { return this->relobj_
; }
686 // Return the section index of the code section containing the branch being
690 { return this->shndx_
; }
692 // Return the source address of stub. This is the address of the original
693 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
696 source_address() const
697 { return this->source_address_
; }
699 // Return the destination address of the stub. This is the branch taken
700 // address of the original branch instruction. LSB is 1 if it is a THUMB
701 // instruction address.
703 destination_address() const
704 { return this->destination_address_
; }
706 // Return the instruction being fixed up.
708 original_insn() const
709 { return this->original_insn_
; }
712 // Cortex_a8_stubs are created via a stub factory. So these are protected.
713 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
714 unsigned int shndx
, Arm_address source_address
,
715 Arm_address destination_address
, uint32_t original_insn
)
716 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
717 source_address_(source_address
| 1U),
718 destination_address_(destination_address
),
719 original_insn_(original_insn
)
722 friend class Stub_factory
;
724 // Return the relocation target address of the i-th relocation in the
727 do_reloc_target(size_t i
)
729 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
731 // The conditional branch veneer has two relocations.
733 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
737 // All other Cortex-A8 stubs have only one relocation.
739 return this->destination_address_
;
743 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
745 do_thumb16_special(size_t);
748 // Object of the code section containing the branch being fixed up.
750 // Section index of the code section containing the branch begin fixed up.
752 // Source address of original branch.
753 Arm_address source_address_
;
754 // Destination address of the original branch.
755 Arm_address destination_address_
;
756 // Original branch instruction. This is needed for copying the condition
757 // code from a condition branch to its stub.
758 uint32_t original_insn_
;
761 // ARMv4 BX Rx branch relocation stub class.
762 class Arm_v4bx_stub
: public Stub
768 // Return the associated register.
771 { return this->reg_
; }
774 // Arm V4BX stubs are created via a stub factory. So these are protected.
775 Arm_v4bx_stub(const Stub_template
* stub_template
, const uint32_t reg
)
776 : Stub(stub_template
), reg_(reg
)
779 friend class Stub_factory
;
781 // Return the relocation target address of the i-th relocation in the
784 do_reloc_target(size_t)
785 { gold_unreachable(); }
787 // This may be overridden in the child class.
789 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
792 this->do_fixed_endian_v4bx_write
<true>(view
, view_size
);
794 this->do_fixed_endian_v4bx_write
<false>(view
, view_size
);
798 // A template to implement do_write.
799 template<bool big_endian
>
801 do_fixed_endian_v4bx_write(unsigned char* view
, section_size_type
)
803 const Insn_template
* insns
= this->stub_template()->insns();
804 elfcpp::Swap
<32, big_endian
>::writeval(view
,
806 + (this->reg_
<< 16)));
807 view
+= insns
[0].size();
808 elfcpp::Swap
<32, big_endian
>::writeval(view
,
809 (insns
[1].data() + this->reg_
));
810 view
+= insns
[1].size();
811 elfcpp::Swap
<32, big_endian
>::writeval(view
,
812 (insns
[2].data() + this->reg_
));
815 // A register index (r0-r14), which is associated with the stub.
819 // Stub factory class.
824 // Return the unique instance of this class.
825 static const Stub_factory
&
828 static Stub_factory singleton
;
832 // Make a relocation stub.
834 make_reloc_stub(Stub_type stub_type
) const
836 gold_assert(stub_type
>= arm_stub_reloc_first
837 && stub_type
<= arm_stub_reloc_last
);
838 return new Reloc_stub(this->stub_templates_
[stub_type
]);
841 // Make a Cortex-A8 stub.
843 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
844 Arm_address source
, Arm_address destination
,
845 uint32_t original_insn
) const
847 gold_assert(stub_type
>= arm_stub_cortex_a8_first
848 && stub_type
<= arm_stub_cortex_a8_last
);
849 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
850 source
, destination
, original_insn
);
853 // Make an ARM V4BX relocation stub.
854 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
856 make_arm_v4bx_stub(uint32_t reg
) const
858 gold_assert(reg
< 0xf);
859 return new Arm_v4bx_stub(this->stub_templates_
[arm_stub_v4_veneer_bx
],
864 // Constructor and destructor are protected since we only return a single
865 // instance created in Stub_factory::get_instance().
869 // A Stub_factory may not be copied since it is a singleton.
870 Stub_factory(const Stub_factory
&);
871 Stub_factory
& operator=(Stub_factory
&);
873 // Stub templates. These are initialized in the constructor.
874 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
877 // A class to hold stubs for the ARM target.
879 template<bool big_endian
>
880 class Stub_table
: public Output_data
883 Stub_table(Arm_input_section
<big_endian
>* owner
)
884 : Output_data(), owner_(owner
), reloc_stubs_(), cortex_a8_stubs_(),
885 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
891 // Owner of this stub table.
892 Arm_input_section
<big_endian
>*
894 { return this->owner_
; }
896 // Whether this stub table is empty.
900 return (this->reloc_stubs_
.empty()
901 && this->cortex_a8_stubs_
.empty()
902 && this->arm_v4bx_stubs_
.empty());
905 // Return the current data size.
907 current_data_size() const
908 { return this->current_data_size_for_child(); }
910 // Add a STUB with using KEY. Caller is reponsible for avoid adding
911 // if already a STUB with the same key has been added.
913 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
915 const Stub_template
* stub_template
= stub
->stub_template();
916 gold_assert(stub_template
->type() == key
.stub_type());
917 this->reloc_stubs_
[key
] = stub
;
920 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
921 // Caller is reponsible for avoid adding if already a STUB with the same
922 // address has been added.
924 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
926 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
927 this->cortex_a8_stubs_
.insert(value
);
930 // Add an ARM V4BX relocation stub. A register index will be retrieved
933 add_arm_v4bx_stub(Arm_v4bx_stub
* stub
)
935 gold_assert(stub
!= NULL
&& this->arm_v4bx_stubs_
[stub
->reg()] == NULL
);
936 this->arm_v4bx_stubs_
[stub
->reg()] = stub
;
939 // Remove all Cortex-A8 stubs.
941 remove_all_cortex_a8_stubs();
943 // Look up a relocation stub using KEY. Return NULL if there is none.
945 find_reloc_stub(const Reloc_stub::Key
& key
) const
947 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
948 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
951 // Look up an arm v4bx relocation stub using the register index.
952 // Return NULL if there is none.
954 find_arm_v4bx_stub(const uint32_t reg
) const
956 gold_assert(reg
< 0xf);
957 return this->arm_v4bx_stubs_
[reg
];
960 // Relocate stubs in this stub table.
962 relocate_stubs(const Relocate_info
<32, big_endian
>*,
963 Target_arm
<big_endian
>*, Output_section
*,
964 unsigned char*, Arm_address
, section_size_type
);
966 // Update data size and alignment at the end of a relaxation pass. Return
967 // true if either data size or alignment is different from that of the
968 // previous relaxation pass.
970 update_data_size_and_addralign();
972 // Finalize stubs. Set the offsets of all stubs and mark input sections
973 // needing the Cortex-A8 workaround.
977 // Apply Cortex-A8 workaround to an address range.
979 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
980 unsigned char*, Arm_address
,
984 // Write out section contents.
986 do_write(Output_file
*);
988 // Return the required alignment.
991 { return this->prev_addralign_
; }
993 // Reset address and file offset.
995 do_reset_address_and_file_offset()
996 { this->set_current_data_size_for_child(this->prev_data_size_
); }
998 // Set final data size.
1000 set_final_data_size()
1001 { this->set_data_size(this->current_data_size()); }
1004 // Relocate one stub.
1006 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1007 Target_arm
<big_endian
>*, Output_section
*,
1008 unsigned char*, Arm_address
, section_size_type
);
1010 // Unordered map of relocation stubs.
1012 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
1013 Reloc_stub::Key::equal_to
>
1016 // List of Cortex-A8 stubs ordered by addresses of branches being
1017 // fixed up in output.
1018 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
1019 // List of Arm V4BX relocation stubs ordered by associated registers.
1020 typedef std::vector
<Arm_v4bx_stub
*> Arm_v4bx_stub_list
;
1022 // Owner of this stub table.
1023 Arm_input_section
<big_endian
>* owner_
;
1024 // The relocation stubs.
1025 Reloc_stub_map reloc_stubs_
;
1026 // The cortex_a8_stubs.
1027 Cortex_a8_stub_list cortex_a8_stubs_
;
1028 // The Arm V4BX relocation stubs.
1029 Arm_v4bx_stub_list arm_v4bx_stubs_
;
1030 // data size of this in the previous pass.
1031 off_t prev_data_size_
;
1032 // address alignment of this in the previous pass.
1033 uint64_t prev_addralign_
;
1036 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1037 // we add to the end of an EXIDX input section that goes into the output.
1039 class Arm_exidx_cantunwind
: public Output_section_data
1042 Arm_exidx_cantunwind(Relobj
* relobj
, unsigned int shndx
)
1043 : Output_section_data(8, 4, true), relobj_(relobj
), shndx_(shndx
)
1046 // Return the object containing the section pointed by this.
1049 { return this->relobj_
; }
1051 // Return the section index of the section pointed by this.
1054 { return this->shndx_
; }
1058 do_write(Output_file
* of
)
1060 if (parameters
->target().is_big_endian())
1061 this->do_fixed_endian_write
<true>(of
);
1063 this->do_fixed_endian_write
<false>(of
);
1067 // Implement do_write for a given endianity.
1068 template<bool big_endian
>
1070 do_fixed_endian_write(Output_file
*);
1072 // The object containing the section pointed by this.
1074 // The section index of the section pointed by this.
1075 unsigned int shndx_
;
1078 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1079 // Offset map is used to map input section offset within the EXIDX section
1080 // to the output offset from the start of this EXIDX section.
1082 typedef std::map
<section_offset_type
, section_offset_type
>
1083 Arm_exidx_section_offset_map
;
1085 // Arm_exidx_merged_section class. This represents an EXIDX input section
1086 // with some of its entries merged.
1088 class Arm_exidx_merged_section
: public Output_relaxed_input_section
1091 // Constructor for Arm_exidx_merged_section.
1092 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1093 // SECTION_OFFSET_MAP points to a section offset map describing how
1094 // parts of the input section are mapped to output. DELETED_BYTES is
1095 // the number of bytes deleted from the EXIDX input section.
1096 Arm_exidx_merged_section(
1097 const Arm_exidx_input_section
& exidx_input_section
,
1098 const Arm_exidx_section_offset_map
& section_offset_map
,
1099 uint32_t deleted_bytes
);
1101 // Return the original EXIDX input section.
1102 const Arm_exidx_input_section
&
1103 exidx_input_section() const
1104 { return this->exidx_input_section_
; }
1106 // Return the section offset map.
1107 const Arm_exidx_section_offset_map
&
1108 section_offset_map() const
1109 { return this->section_offset_map_
; }
1112 // Write merged section into file OF.
1114 do_write(Output_file
* of
);
1117 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
1118 section_offset_type
*) const;
1121 // Original EXIDX input section.
1122 const Arm_exidx_input_section
& exidx_input_section_
;
1123 // Section offset map.
1124 const Arm_exidx_section_offset_map
& section_offset_map_
;
1127 // A class to wrap an ordinary input section containing executable code.
1129 template<bool big_endian
>
1130 class Arm_input_section
: public Output_relaxed_input_section
1133 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
1134 : Output_relaxed_input_section(relobj
, shndx
, 1),
1135 original_addralign_(1), original_size_(0), stub_table_(NULL
)
1138 ~Arm_input_section()
1145 // Whether this is a stub table owner.
1147 is_stub_table_owner() const
1148 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
1150 // Return the stub table.
1151 Stub_table
<big_endian
>*
1153 { return this->stub_table_
; }
1155 // Set the stub_table.
1157 set_stub_table(Stub_table
<big_endian
>* stub_table
)
1158 { this->stub_table_
= stub_table
; }
1160 // Downcast a base pointer to an Arm_input_section pointer. This is
1161 // not type-safe but we only use Arm_input_section not the base class.
1162 static Arm_input_section
<big_endian
>*
1163 as_arm_input_section(Output_relaxed_input_section
* poris
)
1164 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
1167 // Write data to output file.
1169 do_write(Output_file
*);
1171 // Return required alignment of this.
1173 do_addralign() const
1175 if (this->is_stub_table_owner())
1176 return std::max(this->stub_table_
->addralign(),
1177 this->original_addralign_
);
1179 return this->original_addralign_
;
1182 // Finalize data size.
1184 set_final_data_size();
1186 // Reset address and file offset.
1188 do_reset_address_and_file_offset();
1192 do_output_offset(const Relobj
* object
, unsigned int shndx
,
1193 section_offset_type offset
,
1194 section_offset_type
* poutput
) const
1196 if ((object
== this->relobj())
1197 && (shndx
== this->shndx())
1199 && (convert_types
<uint64_t, section_offset_type
>(offset
)
1200 <= this->original_size_
))
1210 // Copying is not allowed.
1211 Arm_input_section(const Arm_input_section
&);
1212 Arm_input_section
& operator=(const Arm_input_section
&);
1214 // Address alignment of the original input section.
1215 uint64_t original_addralign_
;
1216 // Section size of the original input section.
1217 uint64_t original_size_
;
1219 Stub_table
<big_endian
>* stub_table_
;
1222 // Arm_exidx_fixup class. This is used to define a number of methods
1223 // and keep states for fixing up EXIDX coverage.
1225 class Arm_exidx_fixup
1228 Arm_exidx_fixup(Output_section
* exidx_output_section
)
1229 : exidx_output_section_(exidx_output_section
), last_unwind_type_(UT_NONE
),
1230 last_inlined_entry_(0), last_input_section_(NULL
),
1231 section_offset_map_(NULL
)
1235 { delete this->section_offset_map_
; }
1237 // Process an EXIDX section for entry merging. Return number of bytes to
1238 // be deleted in output. If parts of the input EXIDX section are merged
1239 // a heap allocated Arm_exidx_section_offset_map is store in the located
1240 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1242 template<bool big_endian
>
1244 process_exidx_section(const Arm_exidx_input_section
* exidx_input_section
,
1245 Arm_exidx_section_offset_map
** psection_offset_map
);
1247 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1248 // input section, if there is not one already.
1250 add_exidx_cantunwind_as_needed();
1253 // Copying is not allowed.
1254 Arm_exidx_fixup(const Arm_exidx_fixup
&);
1255 Arm_exidx_fixup
& operator=(const Arm_exidx_fixup
&);
1257 // Type of EXIDX unwind entry.
1262 // EXIDX_CANTUNWIND.
1263 UT_EXIDX_CANTUNWIND
,
1270 // Process an EXIDX entry. We only care about the second word of the
1271 // entry. Return true if the entry can be deleted.
1273 process_exidx_entry(uint32_t second_word
);
1275 // Update the current section offset map during EXIDX section fix-up.
1276 // If there is no map, create one. INPUT_OFFSET is the offset of a
1277 // reference point, DELETED_BYTES is the number of deleted by in the
1278 // section so far. If DELETE_ENTRY is true, the reference point and
1279 // all offsets after the previous reference point are discarded.
1281 update_offset_map(section_offset_type input_offset
,
1282 section_size_type deleted_bytes
, bool delete_entry
);
1284 // EXIDX output section.
1285 Output_section
* exidx_output_section_
;
1286 // Unwind type of the last EXIDX entry processed.
1287 Unwind_type last_unwind_type_
;
1288 // Last seen inlined EXIDX entry.
1289 uint32_t last_inlined_entry_
;
1290 // Last processed EXIDX input section.
1291 Arm_exidx_input_section
* last_input_section_
;
1292 // Section offset map created in process_exidx_section.
1293 Arm_exidx_section_offset_map
* section_offset_map_
;
1296 // Arm output section class. This is defined mainly to add a number of
1297 // stub generation methods.
1299 template<bool big_endian
>
1300 class Arm_output_section
: public Output_section
1303 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1304 elfcpp::Elf_Xword flags
)
1305 : Output_section(name
, type
, flags
)
1308 ~Arm_output_section()
1311 // Group input sections for stub generation.
1313 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1315 // Downcast a base pointer to an Arm_output_section pointer. This is
1316 // not type-safe but we only use Arm_output_section not the base class.
1317 static Arm_output_section
<big_endian
>*
1318 as_arm_output_section(Output_section
* os
)
1319 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1323 typedef Output_section::Input_section Input_section
;
1324 typedef Output_section::Input_section_list Input_section_list
;
1326 // Create a stub group.
1327 void create_stub_group(Input_section_list::const_iterator
,
1328 Input_section_list::const_iterator
,
1329 Input_section_list::const_iterator
,
1330 Target_arm
<big_endian
>*,
1331 std::vector
<Output_relaxed_input_section
*>*);
1334 // Arm_exidx_input_section class. This represents an EXIDX input section.
1336 class Arm_exidx_input_section
1339 static const section_offset_type invalid_offset
=
1340 static_cast<section_offset_type
>(-1);
1342 Arm_exidx_input_section(Relobj
* relobj
, unsigned int shndx
,
1343 unsigned int link
, uint32_t size
, uint32_t addralign
)
1344 : relobj_(relobj
), shndx_(shndx
), link_(link
), size_(size
),
1345 addralign_(addralign
)
1348 ~Arm_exidx_input_section()
1351 // Accessors: This is a read-only class.
1353 // Return the object containing this EXIDX input section.
1356 { return this->relobj_
; }
1358 // Return the section index of this EXIDX input section.
1361 { return this->shndx_
; }
1363 // Return the section index of linked text section in the same object.
1366 { return this->link_
; }
1368 // Return size of the EXIDX input section.
1371 { return this->size_
; }
1373 // Reutnr address alignment of EXIDX input section.
1376 { return this->addralign_
; }
1379 // Object containing this.
1381 // Section index of this.
1382 unsigned int shndx_
;
1383 // text section linked to this in the same object.
1385 // Size of this. For ARM 32-bit is sufficient.
1387 // Address alignment of this. For ARM 32-bit is sufficient.
1388 uint32_t addralign_
;
1391 // Arm_relobj class.
1393 template<bool big_endian
>
1394 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1397 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1399 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1400 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1401 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1402 stub_tables_(), local_symbol_is_thumb_function_(),
1403 attributes_section_data_(NULL
), mapping_symbols_info_(),
1404 section_has_cortex_a8_workaround_(NULL
)
1408 { delete this->attributes_section_data_
; }
1410 // Return the stub table of the SHNDX-th section if there is one.
1411 Stub_table
<big_endian
>*
1412 stub_table(unsigned int shndx
) const
1414 gold_assert(shndx
< this->stub_tables_
.size());
1415 return this->stub_tables_
[shndx
];
1418 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1420 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1422 gold_assert(shndx
< this->stub_tables_
.size());
1423 this->stub_tables_
[shndx
] = stub_table
;
1426 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1427 // index. This is only valid after do_count_local_symbol is called.
1429 local_symbol_is_thumb_function(unsigned int r_sym
) const
1431 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1432 return this->local_symbol_is_thumb_function_
[r_sym
];
1435 // Scan all relocation sections for stub generation.
1437 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1440 // Convert regular input section with index SHNDX to a relaxed section.
1442 convert_input_section_to_relaxed_section(unsigned shndx
)
1444 // The stubs have relocations and we need to process them after writing
1445 // out the stubs. So relocation now must follow section write.
1446 this->invalidate_section_offset(shndx
);
1447 this->set_relocs_must_follow_section_writes();
1450 // Downcast a base pointer to an Arm_relobj pointer. This is
1451 // not type-safe but we only use Arm_relobj not the base class.
1452 static Arm_relobj
<big_endian
>*
1453 as_arm_relobj(Relobj
* relobj
)
1454 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1456 // Processor-specific flags in ELF file header. This is valid only after
1459 processor_specific_flags() const
1460 { return this->processor_specific_flags_
; }
1462 // Attribute section data This is the contents of the .ARM.attribute section
1464 const Attributes_section_data
*
1465 attributes_section_data() const
1466 { return this->attributes_section_data_
; }
1468 // Mapping symbol location.
1469 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1471 // Functor for STL container.
1472 struct Mapping_symbol_position_less
1475 operator()(const Mapping_symbol_position
& p1
,
1476 const Mapping_symbol_position
& p2
) const
1478 return (p1
.first
< p2
.first
1479 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1483 // We only care about the first character of a mapping symbol, so
1484 // we only store that instead of the whole symbol name.
1485 typedef std::map
<Mapping_symbol_position
, char,
1486 Mapping_symbol_position_less
> Mapping_symbols_info
;
1488 // Whether a section contains any Cortex-A8 workaround.
1490 section_has_cortex_a8_workaround(unsigned int shndx
) const
1492 return (this->section_has_cortex_a8_workaround_
!= NULL
1493 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1496 // Mark a section that has Cortex-A8 workaround.
1498 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1500 if (this->section_has_cortex_a8_workaround_
== NULL
)
1501 this->section_has_cortex_a8_workaround_
=
1502 new std::vector
<bool>(this->shnum(), false);
1503 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1506 // Return the EXIDX section of an text section with index SHNDX or NULL
1507 // if the text section has no associated EXIDX section.
1508 const Arm_exidx_input_section
*
1509 exidx_input_section_by_link(unsigned int shndx
) const
1511 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1512 return ((p
!= this->exidx_section_map_
.end()
1513 && p
->second
->link() == shndx
)
1518 // Return the EXIDX section with index SHNDX or NULL if there is none.
1519 const Arm_exidx_input_section
*
1520 exidx_input_section_by_shndx(unsigned shndx
) const
1522 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1523 return ((p
!= this->exidx_section_map_
.end()
1524 && p
->second
->shndx() == shndx
)
1530 // Post constructor setup.
1534 // Call parent's setup method.
1535 Sized_relobj
<32, big_endian
>::do_setup();
1537 // Initialize look-up tables.
1538 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1539 this->stub_tables_
.swap(empty_stub_table_list
);
1542 // Count the local symbols.
1544 do_count_local_symbols(Stringpool_template
<char>*,
1545 Stringpool_template
<char>*);
1548 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1549 const unsigned char* pshdrs
,
1550 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1552 // Read the symbol information.
1554 do_read_symbols(Read_symbols_data
* sd
);
1556 // Process relocs for garbage collection.
1558 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1562 // Whether a section needs to be scanned for relocation stubs.
1564 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1565 const Relobj::Output_sections
&,
1566 const Symbol_table
*);
1568 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1570 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1571 unsigned int, Output_section
*,
1572 const Symbol_table
*);
1574 // Scan a section for the Cortex-A8 erratum.
1576 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1577 unsigned int, Output_section
*,
1578 Target_arm
<big_endian
>*);
1580 // Make a new Arm_exidx_input_section object for EXIDX section with
1581 // index SHNDX and section header SHDR.
1583 make_exidx_input_section(unsigned int shndx
,
1584 const elfcpp::Shdr
<32, big_endian
>& shdr
);
1586 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1587 typedef Unordered_map
<unsigned int, const Arm_exidx_input_section
*>
1590 // List of stub tables.
1591 Stub_table_list stub_tables_
;
1592 // Bit vector to tell if a local symbol is a thumb function or not.
1593 // This is only valid after do_count_local_symbol is called.
1594 std::vector
<bool> local_symbol_is_thumb_function_
;
1595 // processor-specific flags in ELF file header.
1596 elfcpp::Elf_Word processor_specific_flags_
;
1597 // Object attributes if there is an .ARM.attributes section or NULL.
1598 Attributes_section_data
* attributes_section_data_
;
1599 // Mapping symbols information.
1600 Mapping_symbols_info mapping_symbols_info_
;
1601 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1602 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1603 // Map a text section to its associated .ARM.exidx section, if there is one.
1604 Exidx_section_map exidx_section_map_
;
1607 // Arm_dynobj class.
1609 template<bool big_endian
>
1610 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1613 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1614 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1615 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1616 processor_specific_flags_(0), attributes_section_data_(NULL
)
1620 { delete this->attributes_section_data_
; }
1622 // Downcast a base pointer to an Arm_relobj pointer. This is
1623 // not type-safe but we only use Arm_relobj not the base class.
1624 static Arm_dynobj
<big_endian
>*
1625 as_arm_dynobj(Dynobj
* dynobj
)
1626 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1628 // Processor-specific flags in ELF file header. This is valid only after
1631 processor_specific_flags() const
1632 { return this->processor_specific_flags_
; }
1634 // Attributes section data.
1635 const Attributes_section_data
*
1636 attributes_section_data() const
1637 { return this->attributes_section_data_
; }
1640 // Read the symbol information.
1642 do_read_symbols(Read_symbols_data
* sd
);
1645 // processor-specific flags in ELF file header.
1646 elfcpp::Elf_Word processor_specific_flags_
;
1647 // Object attributes if there is an .ARM.attributes section or NULL.
1648 Attributes_section_data
* attributes_section_data_
;
1651 // Functor to read reloc addends during stub generation.
1653 template<int sh_type
, bool big_endian
>
1654 struct Stub_addend_reader
1656 // Return the addend for a relocation of a particular type. Depending
1657 // on whether this is a REL or RELA relocation, read the addend from a
1658 // view or from a Reloc object.
1659 elfcpp::Elf_types
<32>::Elf_Swxword
1661 unsigned int /* r_type */,
1662 const unsigned char* /* view */,
1663 const typename Reloc_types
<sh_type
,
1664 32, big_endian
>::Reloc
& /* reloc */) const;
1667 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1669 template<bool big_endian
>
1670 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1672 elfcpp::Elf_types
<32>::Elf_Swxword
1675 const unsigned char*,
1676 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1679 // Specialized Stub_addend_reader for RELA type relocation sections.
1680 // We currently do not handle RELA type relocation sections but it is trivial
1681 // to implement the addend reader. This is provided for completeness and to
1682 // make it easier to add support for RELA relocation sections in the future.
1684 template<bool big_endian
>
1685 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1687 elfcpp::Elf_types
<32>::Elf_Swxword
1690 const unsigned char*,
1691 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1692 big_endian
>::Reloc
& reloc
) const
1693 { return reloc
.get_r_addend(); }
1696 // Cortex_a8_reloc class. We keep record of relocation that may need
1697 // the Cortex-A8 erratum workaround.
1699 class Cortex_a8_reloc
1702 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1703 Arm_address destination
)
1704 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1710 // Accessors: This is a read-only class.
1712 // Return the relocation stub associated with this relocation if there is
1716 { return this->reloc_stub_
; }
1718 // Return the relocation type.
1721 { return this->r_type_
; }
1723 // Return the destination address of the relocation. LSB stores the THUMB
1727 { return this->destination_
; }
1730 // Associated relocation stub if there is one, or NULL.
1731 const Reloc_stub
* reloc_stub_
;
1733 unsigned int r_type_
;
1734 // Destination address of this relocation. LSB is used to distinguish
1736 Arm_address destination_
;
1739 // Utilities for manipulating integers of up to 32-bits
1743 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1744 // an int32_t. NO_BITS must be between 1 to 32.
1745 template<int no_bits
>
1746 static inline int32_t
1747 sign_extend(uint32_t bits
)
1749 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1751 return static_cast<int32_t>(bits
);
1752 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1754 uint32_t top_bit
= 1U << (no_bits
- 1);
1755 int32_t as_signed
= static_cast<int32_t>(bits
);
1756 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1759 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1760 template<int no_bits
>
1762 has_overflow(uint32_t bits
)
1764 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1767 int32_t max
= (1 << (no_bits
- 1)) - 1;
1768 int32_t min
= -(1 << (no_bits
- 1));
1769 int32_t as_signed
= static_cast<int32_t>(bits
);
1770 return as_signed
> max
|| as_signed
< min
;
1773 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1774 // fits in the given number of bits as either a signed or unsigned value.
1775 // For example, has_signed_unsigned_overflow<8> would check
1776 // -128 <= bits <= 255
1777 template<int no_bits
>
1779 has_signed_unsigned_overflow(uint32_t bits
)
1781 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1784 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1785 int32_t min
= -(1 << (no_bits
- 1));
1786 int32_t as_signed
= static_cast<int32_t>(bits
);
1787 return as_signed
> max
|| as_signed
< min
;
1790 // Select bits from A and B using bits in MASK. For each n in [0..31],
1791 // the n-th bit in the result is chosen from the n-th bits of A and B.
1792 // A zero selects A and a one selects B.
1793 static inline uint32_t
1794 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1795 { return (a
& ~mask
) | (b
& mask
); }
1798 template<bool big_endian
>
1799 class Target_arm
: public Sized_target
<32, big_endian
>
1802 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1805 // When were are relocating a stub, we pass this as the relocation number.
1806 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1809 : Sized_target
<32, big_endian
>(&arm_info
),
1810 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1811 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1812 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1813 should_force_pic_veneer_(false), arm_input_section_map_(),
1814 attributes_section_data_(NULL
), fix_cortex_a8_(false),
1815 cortex_a8_relocs_info_()
1818 // Whether we can use BLX.
1821 { return this->may_use_blx_
; }
1823 // Set use-BLX flag.
1825 set_may_use_blx(bool value
)
1826 { this->may_use_blx_
= value
; }
1828 // Whether we force PCI branch veneers.
1830 should_force_pic_veneer() const
1831 { return this->should_force_pic_veneer_
; }
1833 // Set PIC veneer flag.
1835 set_should_force_pic_veneer(bool value
)
1836 { this->should_force_pic_veneer_
= value
; }
1838 // Whether we use THUMB-2 instructions.
1840 using_thumb2() const
1842 Object_attribute
* attr
=
1843 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1844 int arch
= attr
->int_value();
1845 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1848 // Whether we use THUMB/THUMB-2 instructions only.
1850 using_thumb_only() const
1852 Object_attribute
* attr
=
1853 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1854 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1855 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1857 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1858 return attr
->int_value() == 'M';
1861 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1863 may_use_arm_nop() const
1865 Object_attribute
* attr
=
1866 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1867 int arch
= attr
->int_value();
1868 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1869 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1870 || arch
== elfcpp::TAG_CPU_ARCH_V7
1871 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1874 // Whether we have THUMB-2 NOP.W instruction.
1876 may_use_thumb2_nop() const
1878 Object_attribute
* attr
=
1879 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1880 int arch
= attr
->int_value();
1881 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1882 || arch
== elfcpp::TAG_CPU_ARCH_V7
1883 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1886 // Process the relocations to determine unreferenced sections for
1887 // garbage collection.
1889 gc_process_relocs(Symbol_table
* symtab
,
1891 Sized_relobj
<32, big_endian
>* object
,
1892 unsigned int data_shndx
,
1893 unsigned int sh_type
,
1894 const unsigned char* prelocs
,
1896 Output_section
* output_section
,
1897 bool needs_special_offset_handling
,
1898 size_t local_symbol_count
,
1899 const unsigned char* plocal_symbols
);
1901 // Scan the relocations to look for symbol adjustments.
1903 scan_relocs(Symbol_table
* symtab
,
1905 Sized_relobj
<32, big_endian
>* object
,
1906 unsigned int data_shndx
,
1907 unsigned int sh_type
,
1908 const unsigned char* prelocs
,
1910 Output_section
* output_section
,
1911 bool needs_special_offset_handling
,
1912 size_t local_symbol_count
,
1913 const unsigned char* plocal_symbols
);
1915 // Finalize the sections.
1917 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1919 // Return the value to use for a dynamic symbol which requires special
1922 do_dynsym_value(const Symbol
*) const;
1924 // Relocate a section.
1926 relocate_section(const Relocate_info
<32, big_endian
>*,
1927 unsigned int sh_type
,
1928 const unsigned char* prelocs
,
1930 Output_section
* output_section
,
1931 bool needs_special_offset_handling
,
1932 unsigned char* view
,
1933 Arm_address view_address
,
1934 section_size_type view_size
,
1935 const Reloc_symbol_changes
*);
1937 // Scan the relocs during a relocatable link.
1939 scan_relocatable_relocs(Symbol_table
* symtab
,
1941 Sized_relobj
<32, big_endian
>* object
,
1942 unsigned int data_shndx
,
1943 unsigned int sh_type
,
1944 const unsigned char* prelocs
,
1946 Output_section
* output_section
,
1947 bool needs_special_offset_handling
,
1948 size_t local_symbol_count
,
1949 const unsigned char* plocal_symbols
,
1950 Relocatable_relocs
*);
1952 // Relocate a section during a relocatable link.
1954 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1955 unsigned int sh_type
,
1956 const unsigned char* prelocs
,
1958 Output_section
* output_section
,
1959 off_t offset_in_output_section
,
1960 const Relocatable_relocs
*,
1961 unsigned char* view
,
1962 Arm_address view_address
,
1963 section_size_type view_size
,
1964 unsigned char* reloc_view
,
1965 section_size_type reloc_view_size
);
1967 // Return whether SYM is defined by the ABI.
1969 do_is_defined_by_abi(Symbol
* sym
) const
1970 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1972 // Return the size of the GOT section.
1976 gold_assert(this->got_
!= NULL
);
1977 return this->got_
->data_size();
1980 // Map platform-specific reloc types
1982 get_real_reloc_type (unsigned int r_type
);
1985 // Methods to support stub-generations.
1988 // Return the stub factory
1990 stub_factory() const
1991 { return this->stub_factory_
; }
1993 // Make a new Arm_input_section object.
1994 Arm_input_section
<big_endian
>*
1995 new_arm_input_section(Relobj
*, unsigned int);
1997 // Find the Arm_input_section object corresponding to the SHNDX-th input
1998 // section of RELOBJ.
1999 Arm_input_section
<big_endian
>*
2000 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
2002 // Make a new Stub_table
2003 Stub_table
<big_endian
>*
2004 new_stub_table(Arm_input_section
<big_endian
>*);
2006 // Scan a section for stub generation.
2008 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
2009 const unsigned char*, size_t, Output_section
*,
2010 bool, const unsigned char*, Arm_address
,
2015 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
2016 Output_section
*, unsigned char*, Arm_address
,
2019 // Get the default ARM target.
2020 static Target_arm
<big_endian
>*
2023 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
2024 && parameters
->target().is_big_endian() == big_endian
);
2025 return static_cast<Target_arm
<big_endian
>*>(
2026 parameters
->sized_target
<32, big_endian
>());
2029 // Whether relocation type uses LSB to distinguish THUMB addresses.
2031 reloc_uses_thumb_bit(unsigned int r_type
);
2033 // Whether NAME belongs to a mapping symbol.
2035 is_mapping_symbol_name(const char* name
)
2039 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
2040 && (name
[2] == '\0' || name
[2] == '.'));
2043 // Whether we work around the Cortex-A8 erratum.
2045 fix_cortex_a8() const
2046 { return this->fix_cortex_a8_
; }
2048 // Whether we fix R_ARM_V4BX relocation.
2050 // 1 - replace with MOV instruction (armv4 target)
2051 // 2 - make interworking veneer (>= armv4t targets only)
2052 General_options::Fix_v4bx
2054 { return parameters
->options().fix_v4bx(); }
2056 // Scan a span of THUMB code section for Cortex-A8 erratum.
2058 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
2059 section_size_type
, section_size_type
,
2060 const unsigned char*, Arm_address
);
2062 // Apply Cortex-A8 workaround to a branch.
2064 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
2065 unsigned char*, Arm_address
);
2068 // Make an ELF object.
2070 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2071 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
2074 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2075 const elfcpp::Ehdr
<32, !big_endian
>&)
2076 { gold_unreachable(); }
2079 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2080 const elfcpp::Ehdr
<64, false>&)
2081 { gold_unreachable(); }
2084 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2085 const elfcpp::Ehdr
<64, true>&)
2086 { gold_unreachable(); }
2088 // Make an output section.
2090 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
2091 elfcpp::Elf_Xword flags
)
2092 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
2095 do_adjust_elf_header(unsigned char* view
, int len
) const;
2097 // We only need to generate stubs, and hence perform relaxation if we are
2098 // not doing relocatable linking.
2100 do_may_relax() const
2101 { return !parameters
->options().relocatable(); }
2104 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
2106 // Determine whether an object attribute tag takes an integer, a
2109 do_attribute_arg_type(int tag
) const;
2111 // Reorder tags during output.
2113 do_attributes_order(int num
) const;
2116 // The class which scans relocations.
2121 : issued_non_pic_error_(false)
2125 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2126 Sized_relobj
<32, big_endian
>* object
,
2127 unsigned int data_shndx
,
2128 Output_section
* output_section
,
2129 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2130 const elfcpp::Sym
<32, big_endian
>& lsym
);
2133 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2134 Sized_relobj
<32, big_endian
>* object
,
2135 unsigned int data_shndx
,
2136 Output_section
* output_section
,
2137 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2142 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
2143 unsigned int r_type
);
2146 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
2147 unsigned int r_type
, Symbol
*);
2150 check_non_pic(Relobj
*, unsigned int r_type
);
2152 // Almost identical to Symbol::needs_plt_entry except that it also
2153 // handles STT_ARM_TFUNC.
2155 symbol_needs_plt_entry(const Symbol
* sym
)
2157 // An undefined symbol from an executable does not need a PLT entry.
2158 if (sym
->is_undefined() && !parameters
->options().shared())
2161 return (!parameters
->doing_static_link()
2162 && (sym
->type() == elfcpp::STT_FUNC
2163 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
2164 && (sym
->is_from_dynobj()
2165 || sym
->is_undefined()
2166 || sym
->is_preemptible()));
2169 // Whether we have issued an error about a non-PIC compilation.
2170 bool issued_non_pic_error_
;
2173 // The class which implements relocation.
2183 // Return whether the static relocation needs to be applied.
2185 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2188 Output_section
* output_section
);
2190 // Do a relocation. Return false if the caller should not issue
2191 // any warnings about this relocation.
2193 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
2194 Output_section
*, size_t relnum
,
2195 const elfcpp::Rel
<32, big_endian
>&,
2196 unsigned int r_type
, const Sized_symbol
<32>*,
2197 const Symbol_value
<32>*,
2198 unsigned char*, Arm_address
,
2201 // Return whether we want to pass flag NON_PIC_REF for this
2202 // reloc. This means the relocation type accesses a symbol not via
2205 reloc_is_non_pic (unsigned int r_type
)
2209 // These relocation types reference GOT or PLT entries explicitly.
2210 case elfcpp::R_ARM_GOT_BREL
:
2211 case elfcpp::R_ARM_GOT_ABS
:
2212 case elfcpp::R_ARM_GOT_PREL
:
2213 case elfcpp::R_ARM_GOT_BREL12
:
2214 case elfcpp::R_ARM_PLT32_ABS
:
2215 case elfcpp::R_ARM_TLS_GD32
:
2216 case elfcpp::R_ARM_TLS_LDM32
:
2217 case elfcpp::R_ARM_TLS_IE32
:
2218 case elfcpp::R_ARM_TLS_IE12GP
:
2220 // These relocate types may use PLT entries.
2221 case elfcpp::R_ARM_CALL
:
2222 case elfcpp::R_ARM_THM_CALL
:
2223 case elfcpp::R_ARM_JUMP24
:
2224 case elfcpp::R_ARM_THM_JUMP24
:
2225 case elfcpp::R_ARM_THM_JUMP19
:
2226 case elfcpp::R_ARM_PLT32
:
2227 case elfcpp::R_ARM_THM_XPC22
:
2236 // A class which returns the size required for a relocation type,
2237 // used while scanning relocs during a relocatable link.
2238 class Relocatable_size_for_reloc
2242 get_size_for_reloc(unsigned int, Relobj
*);
2245 // Get the GOT section, creating it if necessary.
2246 Output_data_got
<32, big_endian
>*
2247 got_section(Symbol_table
*, Layout
*);
2249 // Get the GOT PLT section.
2251 got_plt_section() const
2253 gold_assert(this->got_plt_
!= NULL
);
2254 return this->got_plt_
;
2257 // Create a PLT entry for a global symbol.
2259 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
2261 // Get the PLT section.
2262 const Output_data_plt_arm
<big_endian
>*
2265 gold_assert(this->plt_
!= NULL
);
2269 // Get the dynamic reloc section, creating it if necessary.
2271 rel_dyn_section(Layout
*);
2273 // Return true if the symbol may need a COPY relocation.
2274 // References from an executable object to non-function symbols
2275 // defined in a dynamic object may need a COPY relocation.
2277 may_need_copy_reloc(Symbol
* gsym
)
2279 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
2280 && gsym
->may_need_copy_reloc());
2283 // Add a potential copy relocation.
2285 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
2286 Sized_relobj
<32, big_endian
>* object
,
2287 unsigned int shndx
, Output_section
* output_section
,
2288 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
2290 this->copy_relocs_
.copy_reloc(symtab
, layout
,
2291 symtab
->get_sized_symbol
<32>(sym
),
2292 object
, shndx
, output_section
, reloc
,
2293 this->rel_dyn_section(layout
));
2296 // Whether two EABI versions are compatible.
2298 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
2300 // Merge processor-specific flags from input object and those in the ELF
2301 // header of the output.
2303 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
2305 // Get the secondary compatible architecture.
2307 get_secondary_compatible_arch(const Attributes_section_data
*);
2309 // Set the secondary compatible architecture.
2311 set_secondary_compatible_arch(Attributes_section_data
*, int);
2314 tag_cpu_arch_combine(const char*, int, int*, int, int);
2316 // Helper to print AEABI enum tag value.
2318 aeabi_enum_name(unsigned int);
2320 // Return string value for TAG_CPU_name.
2322 tag_cpu_name_value(unsigned int);
2324 // Merge object attributes from input object and those in the output.
2326 merge_object_attributes(const char*, const Attributes_section_data
*);
2328 // Helper to get an AEABI object attribute
2330 get_aeabi_object_attribute(int tag
) const
2332 Attributes_section_data
* pasd
= this->attributes_section_data_
;
2333 gold_assert(pasd
!= NULL
);
2334 Object_attribute
* attr
=
2335 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
2336 gold_assert(attr
!= NULL
);
2341 // Methods to support stub-generations.
2344 // Group input sections for stub generation.
2346 group_sections(Layout
*, section_size_type
, bool);
2348 // Scan a relocation for stub generation.
2350 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
2351 const Sized_symbol
<32>*, unsigned int,
2352 const Symbol_value
<32>*,
2353 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
2355 // Scan a relocation section for stub.
2356 template<int sh_type
>
2358 scan_reloc_section_for_stubs(
2359 const Relocate_info
<32, big_endian
>* relinfo
,
2360 const unsigned char* prelocs
,
2362 Output_section
* output_section
,
2363 bool needs_special_offset_handling
,
2364 const unsigned char* view
,
2365 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2368 // Information about this specific target which we pass to the
2369 // general Target structure.
2370 static const Target::Target_info arm_info
;
2372 // The types of GOT entries needed for this platform.
2375 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
2378 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2380 // Map input section to Arm_input_section.
2381 typedef Unordered_map
<Section_id
,
2382 Arm_input_section
<big_endian
>*,
2384 Arm_input_section_map
;
2386 // Map output addresses to relocs for Cortex-A8 erratum.
2387 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2388 Cortex_a8_relocs_info
;
2391 Output_data_got
<32, big_endian
>* got_
;
2393 Output_data_plt_arm
<big_endian
>* plt_
;
2394 // The GOT PLT section.
2395 Output_data_space
* got_plt_
;
2396 // The dynamic reloc section.
2397 Reloc_section
* rel_dyn_
;
2398 // Relocs saved to avoid a COPY reloc.
2399 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2400 // Space for variables copied with a COPY reloc.
2401 Output_data_space
* dynbss_
;
2402 // Vector of Stub_tables created.
2403 Stub_table_list stub_tables_
;
2405 const Stub_factory
&stub_factory_
;
2406 // Whether we can use BLX.
2408 // Whether we force PIC branch veneers.
2409 bool should_force_pic_veneer_
;
2410 // Map for locating Arm_input_sections.
2411 Arm_input_section_map arm_input_section_map_
;
2412 // Attributes section data in output.
2413 Attributes_section_data
* attributes_section_data_
;
2414 // Whether we want to fix code for Cortex-A8 erratum.
2415 bool fix_cortex_a8_
;
2416 // Map addresses to relocs for Cortex-A8 erratum.
2417 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2420 template<bool big_endian
>
2421 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2424 big_endian
, // is_big_endian
2425 elfcpp::EM_ARM
, // machine_code
2426 false, // has_make_symbol
2427 false, // has_resolve
2428 false, // has_code_fill
2429 true, // is_default_stack_executable
2431 "/usr/lib/libc.so.1", // dynamic_linker
2432 0x8000, // default_text_segment_address
2433 0x1000, // abi_pagesize (overridable by -z max-page-size)
2434 0x1000, // common_pagesize (overridable by -z common-page-size)
2435 elfcpp::SHN_UNDEF
, // small_common_shndx
2436 elfcpp::SHN_UNDEF
, // large_common_shndx
2437 0, // small_common_section_flags
2438 0, // large_common_section_flags
2439 ".ARM.attributes", // attributes_section
2440 "aeabi" // attributes_vendor
2443 // Arm relocate functions class
2446 template<bool big_endian
>
2447 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2452 STATUS_OKAY
, // No error during relocation.
2453 STATUS_OVERFLOW
, // Relocation oveflow.
2454 STATUS_BAD_RELOC
// Relocation cannot be applied.
2458 typedef Relocate_functions
<32, big_endian
> Base
;
2459 typedef Arm_relocate_functions
<big_endian
> This
;
2461 // Encoding of imm16 argument for movt and movw ARM instructions
2464 // imm16 := imm4 | imm12
2466 // 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
2467 // +-------+---------------+-------+-------+-----------------------+
2468 // | | |imm4 | |imm12 |
2469 // +-------+---------------+-------+-------+-----------------------+
2471 // Extract the relocation addend from VAL based on the ARM
2472 // instruction encoding described above.
2473 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2474 extract_arm_movw_movt_addend(
2475 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2477 // According to the Elf ABI for ARM Architecture the immediate
2478 // field is sign-extended to form the addend.
2479 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2482 // Insert X into VAL based on the ARM instruction encoding described
2484 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2485 insert_val_arm_movw_movt(
2486 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2487 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2491 val
|= (x
& 0xf000) << 4;
2495 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2498 // imm16 := imm4 | i | imm3 | imm8
2500 // 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
2501 // +---------+-+-----------+-------++-+-----+-------+---------------+
2502 // | |i| |imm4 || |imm3 | |imm8 |
2503 // +---------+-+-----------+-------++-+-----+-------+---------------+
2505 // Extract the relocation addend from VAL based on the Thumb2
2506 // instruction encoding described above.
2507 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2508 extract_thumb_movw_movt_addend(
2509 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2511 // According to the Elf ABI for ARM Architecture the immediate
2512 // field is sign-extended to form the addend.
2513 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2514 | ((val
>> 15) & 0x0800)
2515 | ((val
>> 4) & 0x0700)
2519 // Insert X into VAL based on the Thumb2 instruction encoding
2521 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2522 insert_val_thumb_movw_movt(
2523 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2524 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2527 val
|= (x
& 0xf000) << 4;
2528 val
|= (x
& 0x0800) << 15;
2529 val
|= (x
& 0x0700) << 4;
2530 val
|= (x
& 0x00ff);
2534 // Handle ARM long branches.
2535 static typename
This::Status
2536 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2537 unsigned char *, const Sized_symbol
<32>*,
2538 const Arm_relobj
<big_endian
>*, unsigned int,
2539 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2541 // Handle THUMB long branches.
2542 static typename
This::Status
2543 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2544 unsigned char *, const Sized_symbol
<32>*,
2545 const Arm_relobj
<big_endian
>*, unsigned int,
2546 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2550 // Return the branch offset of a 32-bit THUMB branch.
2551 static inline int32_t
2552 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2554 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2555 // involving the J1 and J2 bits.
2556 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2557 uint32_t upper
= upper_insn
& 0x3ffU
;
2558 uint32_t lower
= lower_insn
& 0x7ffU
;
2559 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2560 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2561 uint32_t i1
= j1
^ s
? 0 : 1;
2562 uint32_t i2
= j2
^ s
? 0 : 1;
2564 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2565 | (upper
<< 12) | (lower
<< 1));
2568 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2569 // UPPER_INSN is the original upper instruction of the branch. Caller is
2570 // responsible for overflow checking and BLX offset adjustment.
2571 static inline uint16_t
2572 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2574 uint32_t s
= offset
< 0 ? 1 : 0;
2575 uint32_t bits
= static_cast<uint32_t>(offset
);
2576 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2579 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2580 // LOWER_INSN is the original lower instruction of the branch. Caller is
2581 // responsible for overflow checking and BLX offset adjustment.
2582 static inline uint16_t
2583 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2585 uint32_t s
= offset
< 0 ? 1 : 0;
2586 uint32_t bits
= static_cast<uint32_t>(offset
);
2587 return ((lower_insn
& ~0x2fffU
)
2588 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2589 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2590 | ((bits
>> 1) & 0x7ffU
));
2593 // Return the branch offset of a 32-bit THUMB conditional branch.
2594 static inline int32_t
2595 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2597 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2598 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2599 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2600 uint32_t lower
= (lower_insn
& 0x07ffU
);
2601 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2603 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2606 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2607 // instruction. UPPER_INSN is the original upper instruction of the branch.
2608 // Caller is responsible for overflow checking.
2609 static inline uint16_t
2610 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2612 uint32_t s
= offset
< 0 ? 1 : 0;
2613 uint32_t bits
= static_cast<uint32_t>(offset
);
2614 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2617 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2618 // instruction. LOWER_INSN is the original lower instruction of the branch.
2619 // Caller is reponsible for overflow checking.
2620 static inline uint16_t
2621 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2623 uint32_t bits
= static_cast<uint32_t>(offset
);
2624 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2625 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2626 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2628 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
2631 // R_ARM_ABS8: S + A
2632 static inline typename
This::Status
2633 abs8(unsigned char *view
,
2634 const Sized_relobj
<32, big_endian
>* object
,
2635 const Symbol_value
<32>* psymval
)
2637 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
2638 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2639 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2640 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
2641 Reltype addend
= utils::sign_extend
<8>(val
);
2642 Reltype x
= psymval
->value(object
, addend
);
2643 val
= utils::bit_select(val
, x
, 0xffU
);
2644 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
2645 return (utils::has_signed_unsigned_overflow
<8>(x
)
2646 ? This::STATUS_OVERFLOW
2647 : This::STATUS_OKAY
);
2650 // R_ARM_THM_ABS5: S + A
2651 static inline typename
This::Status
2652 thm_abs5(unsigned char *view
,
2653 const Sized_relobj
<32, big_endian
>* object
,
2654 const Symbol_value
<32>* psymval
)
2656 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2657 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2658 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2659 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2660 Reltype addend
= (val
& 0x7e0U
) >> 6;
2661 Reltype x
= psymval
->value(object
, addend
);
2662 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
2663 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2664 return (utils::has_overflow
<5>(x
)
2665 ? This::STATUS_OVERFLOW
2666 : This::STATUS_OKAY
);
2669 // R_ARM_ABS12: S + A
2670 static inline typename
This::Status
2671 abs12(unsigned char *view
,
2672 const Sized_relobj
<32, big_endian
>* object
,
2673 const Symbol_value
<32>* psymval
)
2675 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2676 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2677 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2678 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2679 Reltype addend
= val
& 0x0fffU
;
2680 Reltype x
= psymval
->value(object
, addend
);
2681 val
= utils::bit_select(val
, x
, 0x0fffU
);
2682 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2683 return (utils::has_overflow
<12>(x
)
2684 ? This::STATUS_OVERFLOW
2685 : This::STATUS_OKAY
);
2688 // R_ARM_ABS16: S + A
2689 static inline typename
This::Status
2690 abs16(unsigned char *view
,
2691 const Sized_relobj
<32, big_endian
>* object
,
2692 const Symbol_value
<32>* psymval
)
2694 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2695 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2696 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2697 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2698 Reltype addend
= utils::sign_extend
<16>(val
);
2699 Reltype x
= psymval
->value(object
, addend
);
2700 val
= utils::bit_select(val
, x
, 0xffffU
);
2701 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2702 return (utils::has_signed_unsigned_overflow
<16>(x
)
2703 ? This::STATUS_OVERFLOW
2704 : This::STATUS_OKAY
);
2707 // R_ARM_ABS32: (S + A) | T
2708 static inline typename
This::Status
2709 abs32(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 addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2717 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2718 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2719 return This::STATUS_OKAY
;
2722 // R_ARM_REL32: (S + A) | T - P
2723 static inline typename
This::Status
2724 rel32(unsigned char *view
,
2725 const Sized_relobj
<32, big_endian
>* object
,
2726 const Symbol_value
<32>* psymval
,
2727 Arm_address address
,
2728 Arm_address thumb_bit
)
2730 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2731 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2732 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2733 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2734 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
2735 return This::STATUS_OKAY
;
2738 // R_ARM_THM_CALL: (S + A) | T - P
2739 static inline typename
This::Status
2740 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2741 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2742 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2743 Arm_address address
, Arm_address thumb_bit
,
2744 bool is_weakly_undefined_without_plt
)
2746 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
2747 object
, r_sym
, psymval
, address
, thumb_bit
,
2748 is_weakly_undefined_without_plt
);
2751 // R_ARM_THM_JUMP24: (S + A) | T - P
2752 static inline typename
This::Status
2753 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2754 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2755 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2756 Arm_address address
, Arm_address thumb_bit
,
2757 bool is_weakly_undefined_without_plt
)
2759 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
2760 object
, r_sym
, psymval
, address
, thumb_bit
,
2761 is_weakly_undefined_without_plt
);
2764 // R_ARM_THM_JUMP24: (S + A) | T - P
2765 static typename
This::Status
2766 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
2767 const Symbol_value
<32>* psymval
, Arm_address address
,
2768 Arm_address thumb_bit
);
2770 // R_ARM_THM_XPC22: (S + A) | T - P
2771 static inline typename
This::Status
2772 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
2773 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
2774 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
2775 Arm_address address
, Arm_address thumb_bit
,
2776 bool is_weakly_undefined_without_plt
)
2778 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2779 object
, r_sym
, psymval
, address
, thumb_bit
,
2780 is_weakly_undefined_without_plt
);
2783 // R_ARM_THM_JUMP6: S + A – P
2784 static inline typename
This::Status
2785 thm_jump6(unsigned char *view
,
2786 const Sized_relobj
<32, big_endian
>* object
,
2787 const Symbol_value
<32>* psymval
,
2788 Arm_address address
)
2790 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2791 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2792 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2793 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2794 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2795 Reltype addend
= (((val
& 0x0200) >> 3) | ((val
& 0x00f8) >> 2));
2796 Reltype x
= (psymval
->value(object
, addend
) - address
);
2797 val
= (val
& 0xfd07) | ((x
& 0x0040) << 3) | ((val
& 0x003e) << 2);
2798 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
2799 // CZB does only forward jumps.
2800 return ((x
> 0x007e)
2801 ? This::STATUS_OVERFLOW
2802 : This::STATUS_OKAY
);
2805 // R_ARM_THM_JUMP8: S + A – P
2806 static inline typename
This::Status
2807 thm_jump8(unsigned char *view
,
2808 const Sized_relobj
<32, big_endian
>* object
,
2809 const Symbol_value
<32>* psymval
,
2810 Arm_address address
)
2812 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2813 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2814 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2815 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2816 Reltype addend
= utils::sign_extend
<8>((val
& 0x00ff) << 1);
2817 Reltype x
= (psymval
->value(object
, addend
) - address
);
2818 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xff00) | ((x
& 0x01fe) >> 1));
2819 return (utils::has_overflow
<8>(x
)
2820 ? This::STATUS_OVERFLOW
2821 : This::STATUS_OKAY
);
2824 // R_ARM_THM_JUMP11: S + A – P
2825 static inline typename
This::Status
2826 thm_jump11(unsigned char *view
,
2827 const Sized_relobj
<32, big_endian
>* object
,
2828 const Symbol_value
<32>* psymval
,
2829 Arm_address address
)
2831 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2832 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
2833 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2834 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2835 Reltype addend
= utils::sign_extend
<11>((val
& 0x07ff) << 1);
2836 Reltype x
= (psymval
->value(object
, addend
) - address
);
2837 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xf800) | ((x
& 0x0ffe) >> 1));
2838 return (utils::has_overflow
<11>(x
)
2839 ? This::STATUS_OVERFLOW
2840 : This::STATUS_OKAY
);
2843 // R_ARM_BASE_PREL: B(S) + A - P
2844 static inline typename
This::Status
2845 base_prel(unsigned char* view
,
2847 Arm_address address
)
2849 Base::rel32(view
, origin
- address
);
2853 // R_ARM_BASE_ABS: B(S) + A
2854 static inline typename
This::Status
2855 base_abs(unsigned char* view
,
2858 Base::rel32(view
, origin
);
2862 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2863 static inline typename
This::Status
2864 got_brel(unsigned char* view
,
2865 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2867 Base::rel32(view
, got_offset
);
2868 return This::STATUS_OKAY
;
2871 // R_ARM_GOT_PREL: GOT(S) + A - P
2872 static inline typename
This::Status
2873 got_prel(unsigned char *view
,
2874 Arm_address got_entry
,
2875 Arm_address address
)
2877 Base::rel32(view
, got_entry
- address
);
2878 return This::STATUS_OKAY
;
2881 // R_ARM_PLT32: (S + A) | T - P
2882 static inline typename
This::Status
2883 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2884 unsigned char *view
,
2885 const Sized_symbol
<32>* gsym
,
2886 const Arm_relobj
<big_endian
>* object
,
2888 const Symbol_value
<32>* psymval
,
2889 Arm_address address
,
2890 Arm_address thumb_bit
,
2891 bool is_weakly_undefined_without_plt
)
2893 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2894 object
, r_sym
, psymval
, address
, thumb_bit
,
2895 is_weakly_undefined_without_plt
);
2898 // R_ARM_XPC25: (S + A) | T - P
2899 static inline typename
This::Status
2900 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2901 unsigned char *view
,
2902 const Sized_symbol
<32>* gsym
,
2903 const Arm_relobj
<big_endian
>* object
,
2905 const Symbol_value
<32>* psymval
,
2906 Arm_address address
,
2907 Arm_address thumb_bit
,
2908 bool is_weakly_undefined_without_plt
)
2910 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2911 object
, r_sym
, psymval
, address
, thumb_bit
,
2912 is_weakly_undefined_without_plt
);
2915 // R_ARM_CALL: (S + A) | T - P
2916 static inline typename
This::Status
2917 call(const Relocate_info
<32, big_endian
>* relinfo
,
2918 unsigned char *view
,
2919 const Sized_symbol
<32>* gsym
,
2920 const Arm_relobj
<big_endian
>* object
,
2922 const Symbol_value
<32>* psymval
,
2923 Arm_address address
,
2924 Arm_address thumb_bit
,
2925 bool is_weakly_undefined_without_plt
)
2927 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2928 object
, r_sym
, psymval
, address
, thumb_bit
,
2929 is_weakly_undefined_without_plt
);
2932 // R_ARM_JUMP24: (S + A) | T - P
2933 static inline typename
This::Status
2934 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2935 unsigned char *view
,
2936 const Sized_symbol
<32>* gsym
,
2937 const Arm_relobj
<big_endian
>* object
,
2939 const Symbol_value
<32>* psymval
,
2940 Arm_address address
,
2941 Arm_address thumb_bit
,
2942 bool is_weakly_undefined_without_plt
)
2944 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2945 object
, r_sym
, psymval
, address
, thumb_bit
,
2946 is_weakly_undefined_without_plt
);
2949 // R_ARM_PREL: (S + A) | T - P
2950 static inline typename
This::Status
2951 prel31(unsigned char *view
,
2952 const Sized_relobj
<32, big_endian
>* object
,
2953 const Symbol_value
<32>* psymval
,
2954 Arm_address address
,
2955 Arm_address thumb_bit
)
2957 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2958 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2959 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2960 Valtype addend
= utils::sign_extend
<31>(val
);
2961 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2962 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2963 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2964 return (utils::has_overflow
<31>(x
) ?
2965 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2968 // R_ARM_MOVW_ABS_NC: (S + A) | T
2969 static inline typename
This::Status
2970 movw_abs_nc(unsigned char *view
,
2971 const Sized_relobj
<32, big_endian
>* object
,
2972 const Symbol_value
<32>* psymval
,
2973 Arm_address thumb_bit
)
2975 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2976 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2977 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2978 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2979 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2980 val
= This::insert_val_arm_movw_movt(val
, x
);
2981 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2982 return This::STATUS_OKAY
;
2985 // R_ARM_MOVT_ABS: S + A
2986 static inline typename
This::Status
2987 movt_abs(unsigned char *view
,
2988 const Sized_relobj
<32, big_endian
>* object
,
2989 const Symbol_value
<32>* psymval
)
2991 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2992 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2993 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2994 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2995 Valtype x
= psymval
->value(object
, addend
) >> 16;
2996 val
= This::insert_val_arm_movw_movt(val
, x
);
2997 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2998 return This::STATUS_OKAY
;
3001 // R_ARM_THM_MOVW_ABS_NC: S + A | T
3002 static inline typename
This::Status
3003 thm_movw_abs_nc(unsigned char *view
,
3004 const Sized_relobj
<32, big_endian
>* object
,
3005 const Symbol_value
<32>* psymval
,
3006 Arm_address thumb_bit
)
3008 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3009 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3010 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3011 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3012 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
3013 Reltype addend
= extract_thumb_movw_movt_addend(val
);
3014 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
3015 val
= This::insert_val_thumb_movw_movt(val
, x
);
3016 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3017 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3018 return This::STATUS_OKAY
;
3021 // R_ARM_THM_MOVT_ABS: S + A
3022 static inline typename
This::Status
3023 thm_movt_abs(unsigned char *view
,
3024 const Sized_relobj
<32, big_endian
>* object
,
3025 const Symbol_value
<32>* psymval
)
3027 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3028 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3029 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3030 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3031 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
3032 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3033 Reltype x
= psymval
->value(object
, addend
) >> 16;
3034 val
= This::insert_val_thumb_movw_movt(val
, x
);
3035 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3036 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3037 return This::STATUS_OKAY
;
3040 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3041 static inline typename
This::Status
3042 movw_prel_nc(unsigned char *view
,
3043 const Sized_relobj
<32, big_endian
>* object
,
3044 const Symbol_value
<32>* psymval
,
3045 Arm_address address
,
3046 Arm_address thumb_bit
)
3048 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3049 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3050 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3051 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3052 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3053 val
= This::insert_val_arm_movw_movt(val
, x
);
3054 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3055 return This::STATUS_OKAY
;
3058 // R_ARM_MOVT_PREL: S + A - P
3059 static inline typename
This::Status
3060 movt_prel(unsigned char *view
,
3061 const Sized_relobj
<32, big_endian
>* object
,
3062 const Symbol_value
<32>* psymval
,
3063 Arm_address address
)
3065 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3066 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3067 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3068 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3069 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
3070 val
= This::insert_val_arm_movw_movt(val
, x
);
3071 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3072 return This::STATUS_OKAY
;
3075 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3076 static inline typename
This::Status
3077 thm_movw_prel_nc(unsigned char *view
,
3078 const Sized_relobj
<32, big_endian
>* object
,
3079 const Symbol_value
<32>* psymval
,
3080 Arm_address address
,
3081 Arm_address thumb_bit
)
3083 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3084 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3085 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3086 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3087 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3088 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3089 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3090 val
= This::insert_val_thumb_movw_movt(val
, x
);
3091 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3092 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3093 return This::STATUS_OKAY
;
3096 // R_ARM_THM_MOVT_PREL: S + A - P
3097 static inline typename
This::Status
3098 thm_movt_prel(unsigned char *view
,
3099 const Sized_relobj
<32, big_endian
>* object
,
3100 const Symbol_value
<32>* psymval
,
3101 Arm_address address
)
3103 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3104 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3105 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3106 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3107 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3108 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3109 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
3110 val
= This::insert_val_thumb_movw_movt(val
, x
);
3111 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3112 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3113 return This::STATUS_OKAY
;
3117 static inline typename
This::Status
3118 v4bx(const Relocate_info
<32, big_endian
>* relinfo
,
3119 unsigned char *view
,
3120 const Arm_relobj
<big_endian
>* object
,
3121 const Arm_address address
,
3122 const bool is_interworking
)
3125 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3126 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3127 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3129 // Ensure that we have a BX instruction.
3130 gold_assert((val
& 0x0ffffff0) == 0x012fff10);
3131 const uint32_t reg
= (val
& 0xf);
3132 if (is_interworking
&& reg
!= 0xf)
3134 Stub_table
<big_endian
>* stub_table
=
3135 object
->stub_table(relinfo
->data_shndx
);
3136 gold_assert(stub_table
!= NULL
);
3138 Arm_v4bx_stub
* stub
= stub_table
->find_arm_v4bx_stub(reg
);
3139 gold_assert(stub
!= NULL
);
3141 int32_t veneer_address
=
3142 stub_table
->address() + stub
->offset() - 8 - address
;
3143 gold_assert((veneer_address
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3144 && (veneer_address
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3145 // Replace with a branch to veneer (B <addr>)
3146 val
= (val
& 0xf0000000) | 0x0a000000
3147 | ((veneer_address
>> 2) & 0x00ffffff);
3151 // Preserve Rm (lowest four bits) and the condition code
3152 // (highest four bits). Other bits encode MOV PC,Rm.
3153 val
= (val
& 0xf000000f) | 0x01a0f000;
3155 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3156 return This::STATUS_OKAY
;
3160 // Relocate ARM long branches. This handles relocation types
3161 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3162 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3163 // undefined and we do not use PLT in this relocation. In such a case,
3164 // the branch is converted into an NOP.
3166 template<bool big_endian
>
3167 typename Arm_relocate_functions
<big_endian
>::Status
3168 Arm_relocate_functions
<big_endian
>::arm_branch_common(
3169 unsigned int r_type
,
3170 const Relocate_info
<32, big_endian
>* relinfo
,
3171 unsigned char *view
,
3172 const Sized_symbol
<32>* gsym
,
3173 const Arm_relobj
<big_endian
>* object
,
3175 const Symbol_value
<32>* psymval
,
3176 Arm_address address
,
3177 Arm_address thumb_bit
,
3178 bool is_weakly_undefined_without_plt
)
3180 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3181 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3182 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3184 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
3185 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
3186 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
3187 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
3188 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
3189 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
3190 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
3192 // Check that the instruction is valid.
3193 if (r_type
== elfcpp::R_ARM_CALL
)
3195 if (!insn_is_uncond_bl
&& !insn_is_blx
)
3196 return This::STATUS_BAD_RELOC
;
3198 else if (r_type
== elfcpp::R_ARM_JUMP24
)
3200 if (!insn_is_b
&& !insn_is_cond_bl
)
3201 return This::STATUS_BAD_RELOC
;
3203 else if (r_type
== elfcpp::R_ARM_PLT32
)
3205 if (!insn_is_any_branch
)
3206 return This::STATUS_BAD_RELOC
;
3208 else if (r_type
== elfcpp::R_ARM_XPC25
)
3210 // FIXME: AAELF document IH0044C does not say much about it other
3211 // than it being obsolete.
3212 if (!insn_is_any_branch
)
3213 return This::STATUS_BAD_RELOC
;
3218 // A branch to an undefined weak symbol is turned into a jump to
3219 // the next instruction unless a PLT entry will be created.
3220 // Do the same for local undefined symbols.
3221 // The jump to the next instruction is optimized as a NOP depending
3222 // on the architecture.
3223 const Target_arm
<big_endian
>* arm_target
=
3224 Target_arm
<big_endian
>::default_target();
3225 if (is_weakly_undefined_without_plt
)
3227 Valtype cond
= val
& 0xf0000000U
;
3228 if (arm_target
->may_use_arm_nop())
3229 val
= cond
| 0x0320f000;
3231 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3232 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3233 return This::STATUS_OKAY
;
3236 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
3237 Valtype branch_target
= psymval
->value(object
, addend
);
3238 int32_t branch_offset
= branch_target
- address
;
3240 // We need a stub if the branch offset is too large or if we need
3242 bool may_use_blx
= arm_target
->may_use_blx();
3243 Reloc_stub
* stub
= NULL
;
3244 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
3245 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3246 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
3248 Stub_type stub_type
=
3249 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
3251 if (stub_type
!= arm_stub_none
)
3253 Stub_table
<big_endian
>* stub_table
=
3254 object
->stub_table(relinfo
->data_shndx
);
3255 gold_assert(stub_table
!= NULL
);
3257 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3258 stub
= stub_table
->find_reloc_stub(stub_key
);
3259 gold_assert(stub
!= NULL
);
3260 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3261 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3262 branch_offset
= branch_target
- address
;
3263 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3264 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3268 // At this point, if we still need to switch mode, the instruction
3269 // must either be a BLX or a BL that can be converted to a BLX.
3273 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
3274 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
3277 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
3278 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3279 return (utils::has_overflow
<26>(branch_offset
)
3280 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3283 // Relocate THUMB long branches. This handles relocation types
3284 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3285 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3286 // undefined and we do not use PLT in this relocation. In such a case,
3287 // the branch is converted into an NOP.
3289 template<bool big_endian
>
3290 typename Arm_relocate_functions
<big_endian
>::Status
3291 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
3292 unsigned int r_type
,
3293 const Relocate_info
<32, big_endian
>* relinfo
,
3294 unsigned char *view
,
3295 const Sized_symbol
<32>* gsym
,
3296 const Arm_relobj
<big_endian
>* object
,
3298 const Symbol_value
<32>* psymval
,
3299 Arm_address address
,
3300 Arm_address thumb_bit
,
3301 bool is_weakly_undefined_without_plt
)
3303 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3304 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3305 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3306 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3308 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3310 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
3311 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
3313 // Check that the instruction is valid.
3314 if (r_type
== elfcpp::R_ARM_THM_CALL
)
3316 if (!is_bl_insn
&& !is_blx_insn
)
3317 return This::STATUS_BAD_RELOC
;
3319 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
3321 // This cannot be a BLX.
3323 return This::STATUS_BAD_RELOC
;
3325 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
3327 // Check for Thumb to Thumb call.
3329 return This::STATUS_BAD_RELOC
;
3332 gold_warning(_("%s: Thumb BLX instruction targets "
3333 "thumb function '%s'."),
3334 object
->name().c_str(),
3335 (gsym
? gsym
->name() : "(local)"));
3336 // Convert BLX to BL.
3337 lower_insn
|= 0x1000U
;
3343 // A branch to an undefined weak symbol is turned into a jump to
3344 // the next instruction unless a PLT entry will be created.
3345 // The jump to the next instruction is optimized as a NOP.W for
3346 // Thumb-2 enabled architectures.
3347 const Target_arm
<big_endian
>* arm_target
=
3348 Target_arm
<big_endian
>::default_target();
3349 if (is_weakly_undefined_without_plt
)
3351 if (arm_target
->may_use_thumb2_nop())
3353 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
3354 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
3358 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
3359 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
3361 return This::STATUS_OKAY
;
3364 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
3365 Arm_address branch_target
= psymval
->value(object
, addend
);
3366 int32_t branch_offset
= branch_target
- address
;
3368 // We need a stub if the branch offset is too large or if we need
3370 bool may_use_blx
= arm_target
->may_use_blx();
3371 bool thumb2
= arm_target
->using_thumb2();
3373 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3374 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3376 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3377 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3378 || ((thumb_bit
== 0)
3379 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3380 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
3382 Stub_type stub_type
=
3383 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
3385 if (stub_type
!= arm_stub_none
)
3387 Stub_table
<big_endian
>* stub_table
=
3388 object
->stub_table(relinfo
->data_shndx
);
3389 gold_assert(stub_table
!= NULL
);
3391 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3392 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
3393 gold_assert(stub
!= NULL
);
3394 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3395 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3396 branch_offset
= branch_target
- address
;
3400 // At this point, if we still need to switch mode, the instruction
3401 // must either be a BLX or a BL that can be converted to a BLX.
3404 gold_assert(may_use_blx
3405 && (r_type
== elfcpp::R_ARM_THM_CALL
3406 || r_type
== elfcpp::R_ARM_THM_XPC22
));
3407 // Make sure this is a BLX.
3408 lower_insn
&= ~0x1000U
;
3412 // Make sure this is a BL.
3413 lower_insn
|= 0x1000U
;
3416 if ((lower_insn
& 0x5000U
) == 0x4000U
)
3417 // For a BLX instruction, make sure that the relocation is rounded up
3418 // to a word boundary. This follows the semantics of the instruction
3419 // which specifies that bit 1 of the target address will come from bit
3420 // 1 of the base address.
3421 branch_offset
= (branch_offset
+ 2) & ~3;
3423 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3424 // We use the Thumb-2 encoding, which is safe even if dealing with
3425 // a Thumb-1 instruction by virtue of our overflow check above. */
3426 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
3427 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
3429 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3430 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3433 ? utils::has_overflow
<25>(branch_offset
)
3434 : utils::has_overflow
<23>(branch_offset
))
3435 ? This::STATUS_OVERFLOW
3436 : This::STATUS_OKAY
);
3439 // Relocate THUMB-2 long conditional branches.
3440 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3441 // undefined and we do not use PLT in this relocation. In such a case,
3442 // the branch is converted into an NOP.
3444 template<bool big_endian
>
3445 typename Arm_relocate_functions
<big_endian
>::Status
3446 Arm_relocate_functions
<big_endian
>::thm_jump19(
3447 unsigned char *view
,
3448 const Arm_relobj
<big_endian
>* object
,
3449 const Symbol_value
<32>* psymval
,
3450 Arm_address address
,
3451 Arm_address thumb_bit
)
3453 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3454 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3455 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3456 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3457 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
3459 Arm_address branch_target
= psymval
->value(object
, addend
);
3460 int32_t branch_offset
= branch_target
- address
;
3462 // ??? Should handle interworking? GCC might someday try to
3463 // use this for tail calls.
3464 // FIXME: We do support thumb entry to PLT yet.
3467 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3468 return This::STATUS_BAD_RELOC
;
3471 // Put RELOCATION back into the insn.
3472 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
3473 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
3475 // Put the relocated value back in the object file:
3476 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3477 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3479 return (utils::has_overflow
<21>(branch_offset
)
3480 ? This::STATUS_OVERFLOW
3481 : This::STATUS_OKAY
);
3484 // Get the GOT section, creating it if necessary.
3486 template<bool big_endian
>
3487 Output_data_got
<32, big_endian
>*
3488 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
3490 if (this->got_
== NULL
)
3492 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3494 this->got_
= new Output_data_got
<32, big_endian
>();
3497 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3499 | elfcpp::SHF_WRITE
),
3500 this->got_
, false, true, true,
3503 // The old GNU linker creates a .got.plt section. We just
3504 // create another set of data in the .got section. Note that we
3505 // always create a PLT if we create a GOT, although the PLT
3507 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
3508 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3510 | elfcpp::SHF_WRITE
),
3511 this->got_plt_
, false, false,
3514 // The first three entries are reserved.
3515 this->got_plt_
->set_current_data_size(3 * 4);
3517 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3518 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3519 Symbol_table::PREDEFINED
,
3521 0, 0, elfcpp::STT_OBJECT
,
3523 elfcpp::STV_HIDDEN
, 0,
3529 // Get the dynamic reloc section, creating it if necessary.
3531 template<bool big_endian
>
3532 typename Target_arm
<big_endian
>::Reloc_section
*
3533 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
3535 if (this->rel_dyn_
== NULL
)
3537 gold_assert(layout
!= NULL
);
3538 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
3539 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
3540 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
3541 false, false, false);
3543 return this->rel_dyn_
;
3546 // Insn_template methods.
3548 // Return byte size of an instruction template.
3551 Insn_template::size() const
3553 switch (this->type())
3556 case THUMB16_SPECIAL_TYPE
:
3567 // Return alignment of an instruction template.
3570 Insn_template::alignment() const
3572 switch (this->type())
3575 case THUMB16_SPECIAL_TYPE
:
3586 // Stub_template methods.
3588 Stub_template::Stub_template(
3589 Stub_type type
, const Insn_template
* insns
,
3591 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
3592 entry_in_thumb_mode_(false), relocs_()
3596 // Compute byte size and alignment of stub template.
3597 for (size_t i
= 0; i
< insn_count
; i
++)
3599 unsigned insn_alignment
= insns
[i
].alignment();
3600 size_t insn_size
= insns
[i
].size();
3601 gold_assert((offset
& (insn_alignment
- 1)) == 0);
3602 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
3603 switch (insns
[i
].type())
3605 case Insn_template::THUMB16_TYPE
:
3606 case Insn_template::THUMB16_SPECIAL_TYPE
:
3608 this->entry_in_thumb_mode_
= true;
3611 case Insn_template::THUMB32_TYPE
:
3612 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
3613 this->relocs_
.push_back(Reloc(i
, offset
));
3615 this->entry_in_thumb_mode_
= true;
3618 case Insn_template::ARM_TYPE
:
3619 // Handle cases where the target is encoded within the
3621 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
3622 this->relocs_
.push_back(Reloc(i
, offset
));
3625 case Insn_template::DATA_TYPE
:
3626 // Entry point cannot be data.
3627 gold_assert(i
!= 0);
3628 this->relocs_
.push_back(Reloc(i
, offset
));
3634 offset
+= insn_size
;
3636 this->size_
= offset
;
3641 // Template to implement do_write for a specific target endianity.
3643 template<bool big_endian
>
3645 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
3647 const Stub_template
* stub_template
= this->stub_template();
3648 const Insn_template
* insns
= stub_template
->insns();
3650 // FIXME: We do not handle BE8 encoding yet.
3651 unsigned char* pov
= view
;
3652 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
3654 switch (insns
[i
].type())
3656 case Insn_template::THUMB16_TYPE
:
3657 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
3659 case Insn_template::THUMB16_SPECIAL_TYPE
:
3660 elfcpp::Swap
<16, big_endian
>::writeval(
3662 this->thumb16_special(i
));
3664 case Insn_template::THUMB32_TYPE
:
3666 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
3667 uint32_t lo
= insns
[i
].data() & 0xffff;
3668 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
3669 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
3672 case Insn_template::ARM_TYPE
:
3673 case Insn_template::DATA_TYPE
:
3674 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
3679 pov
+= insns
[i
].size();
3681 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
3684 // Reloc_stub::Key methods.
3686 // Dump a Key as a string for debugging.
3689 Reloc_stub::Key::name() const
3691 if (this->r_sym_
== invalid_index
)
3693 // Global symbol key name
3694 // <stub-type>:<symbol name>:<addend>.
3695 const std::string sym_name
= this->u_
.symbol
->name();
3696 // We need to print two hex number and two colons. So just add 100 bytes
3697 // to the symbol name size.
3698 size_t len
= sym_name
.size() + 100;
3699 char* buffer
= new char[len
];
3700 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
3701 sym_name
.c_str(), this->addend_
);
3702 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3704 return std::string(buffer
);
3708 // local symbol key name
3709 // <stub-type>:<object>:<r_sym>:<addend>.
3710 const size_t len
= 200;
3712 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
3713 this->u_
.relobj
, this->r_sym_
, this->addend_
);
3714 gold_assert(c
> 0 && c
< static_cast<int>(len
));
3715 return std::string(buffer
);
3719 // Reloc_stub methods.
3721 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3722 // LOCATION to DESTINATION.
3723 // This code is based on the arm_type_of_stub function in
3724 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3728 Reloc_stub::stub_type_for_reloc(
3729 unsigned int r_type
,
3730 Arm_address location
,
3731 Arm_address destination
,
3732 bool target_is_thumb
)
3734 Stub_type stub_type
= arm_stub_none
;
3736 // This is a bit ugly but we want to avoid using a templated class for
3737 // big and little endianities.
3739 bool should_force_pic_veneer
;
3742 if (parameters
->target().is_big_endian())
3744 const Target_arm
<true>* big_endian_target
=
3745 Target_arm
<true>::default_target();
3746 may_use_blx
= big_endian_target
->may_use_blx();
3747 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
3748 thumb2
= big_endian_target
->using_thumb2();
3749 thumb_only
= big_endian_target
->using_thumb_only();
3753 const Target_arm
<false>* little_endian_target
=
3754 Target_arm
<false>::default_target();
3755 may_use_blx
= little_endian_target
->may_use_blx();
3756 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
3757 thumb2
= little_endian_target
->using_thumb2();
3758 thumb_only
= little_endian_target
->using_thumb_only();
3761 int64_t branch_offset
= (int64_t)destination
- location
;
3763 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
3765 // Handle cases where:
3766 // - this call goes too far (different Thumb/Thumb2 max
3768 // - it's a Thumb->Arm call and blx is not available, or it's a
3769 // Thumb->Arm branch (not bl). A stub is needed in this case.
3771 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
3772 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
3774 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
3775 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
3776 || ((!target_is_thumb
)
3777 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3778 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
3780 if (target_is_thumb
)
3785 stub_type
= (parameters
->options().shared()
3786 || should_force_pic_veneer
)
3789 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3790 // V5T and above. Stub starts with ARM code, so
3791 // we must be able to switch mode before
3792 // reaching it, which is only possible for 'bl'
3793 // (ie R_ARM_THM_CALL relocation).
3794 ? arm_stub_long_branch_any_thumb_pic
3795 // On V4T, use Thumb code only.
3796 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
3800 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3801 ? arm_stub_long_branch_any_any
// V5T and above.
3802 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
3806 stub_type
= (parameters
->options().shared()
3807 || should_force_pic_veneer
)
3808 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
3809 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
3816 // FIXME: We should check that the input section is from an
3817 // object that has interwork enabled.
3819 stub_type
= (parameters
->options().shared()
3820 || should_force_pic_veneer
)
3823 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3824 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
3825 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
3829 && (r_type
== elfcpp::R_ARM_THM_CALL
))
3830 ? arm_stub_long_branch_any_any
// V5T and above.
3831 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
3833 // Handle v4t short branches.
3834 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
3835 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
3836 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
3837 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
3841 else if (r_type
== elfcpp::R_ARM_CALL
3842 || r_type
== elfcpp::R_ARM_JUMP24
3843 || r_type
== elfcpp::R_ARM_PLT32
)
3845 if (target_is_thumb
)
3849 // FIXME: We should check that the input section is from an
3850 // object that has interwork enabled.
3852 // We have an extra 2-bytes reach because of
3853 // the mode change (bit 24 (H) of BLX encoding).
3854 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
3855 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
3856 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
3857 || (r_type
== elfcpp::R_ARM_JUMP24
)
3858 || (r_type
== elfcpp::R_ARM_PLT32
))
3860 stub_type
= (parameters
->options().shared()
3861 || should_force_pic_veneer
)
3864 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
3865 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
3869 ? arm_stub_long_branch_any_any
// V5T and above.
3870 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
3876 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
3877 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
3879 stub_type
= (parameters
->options().shared()
3880 || should_force_pic_veneer
)
3881 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
3882 : arm_stub_long_branch_any_any
; /// non-PIC.
3890 // Cortex_a8_stub methods.
3892 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3893 // I is the position of the instruction template in the stub template.
3896 Cortex_a8_stub::do_thumb16_special(size_t i
)
3898 // The only use of this is to copy condition code from a conditional
3899 // branch being worked around to the corresponding conditional branch in
3901 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3903 uint16_t data
= this->stub_template()->insns()[i
].data();
3904 gold_assert((data
& 0xff00U
) == 0xd000U
);
3905 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
3909 // Stub_factory methods.
3911 Stub_factory::Stub_factory()
3913 // The instruction template sequences are declared as static
3914 // objects and initialized first time the constructor runs.
3916 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3917 // to reach the stub if necessary.
3918 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
3920 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3921 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3922 // dcd R_ARM_ABS32(X)
3925 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3927 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3929 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3930 Insn_template::arm_insn(0xe12fff1c), // bx ip
3931 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3932 // dcd R_ARM_ABS32(X)
3935 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3936 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3938 Insn_template::thumb16_insn(0xb401), // push {r0}
3939 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3940 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3941 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3942 Insn_template::thumb16_insn(0x4760), // bx ip
3943 Insn_template::thumb16_insn(0xbf00), // nop
3944 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3945 // dcd R_ARM_ABS32(X)
3948 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3950 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3952 Insn_template::thumb16_insn(0x4778), // bx pc
3953 Insn_template::thumb16_insn(0x46c0), // nop
3954 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3955 Insn_template::arm_insn(0xe12fff1c), // bx ip
3956 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3957 // dcd R_ARM_ABS32(X)
3960 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3962 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3964 Insn_template::thumb16_insn(0x4778), // bx pc
3965 Insn_template::thumb16_insn(0x46c0), // nop
3966 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3967 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3968 // dcd R_ARM_ABS32(X)
3971 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3972 // one, when the destination is close enough.
3973 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3975 Insn_template::thumb16_insn(0x4778), // bx pc
3976 Insn_template::thumb16_insn(0x46c0), // nop
3977 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3980 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3981 // blx to reach the stub if necessary.
3982 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3984 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3985 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3986 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3987 // dcd R_ARM_REL32(X-4)
3990 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3991 // blx to reach the stub if necessary. We can not add into pc;
3992 // it is not guaranteed to mode switch (different in ARMv6 and
3994 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3996 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3997 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3998 Insn_template::arm_insn(0xe12fff1c), // bx ip
3999 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4000 // dcd R_ARM_REL32(X)
4003 // V4T ARM -> ARM long branch stub, PIC.
4004 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
4006 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4007 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4008 Insn_template::arm_insn(0xe12fff1c), // bx ip
4009 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4010 // dcd R_ARM_REL32(X)
4013 // V4T Thumb -> ARM long branch stub, PIC.
4014 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
4016 Insn_template::thumb16_insn(0x4778), // bx pc
4017 Insn_template::thumb16_insn(0x46c0), // nop
4018 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4019 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4020 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
4021 // dcd R_ARM_REL32(X)
4024 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4026 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
4028 Insn_template::thumb16_insn(0xb401), // push {r0}
4029 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4030 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4031 Insn_template::thumb16_insn(0x4484), // add ip, r0
4032 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4033 Insn_template::thumb16_insn(0x4760), // bx ip
4034 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
4035 // dcd R_ARM_REL32(X)
4038 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4040 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
4042 Insn_template::thumb16_insn(0x4778), // bx pc
4043 Insn_template::thumb16_insn(0x46c0), // nop
4044 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4045 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4046 Insn_template::arm_insn(0xe12fff1c), // bx ip
4047 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4048 // dcd R_ARM_REL32(X)
4051 // Cortex-A8 erratum-workaround stubs.
4053 // Stub used for conditional branches (which may be beyond +/-1MB away,
4054 // so we can't use a conditional branch to reach this stub).
4061 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
4063 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4064 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4065 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4069 // Stub used for b.w and bl.w instructions.
4071 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
4073 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4076 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
4078 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4081 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4082 // instruction (which switches to ARM mode) to point to this stub. Jump to
4083 // the real destination using an ARM-mode branch.
4084 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
4086 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4089 // Stub used to provide an interworking for R_ARM_V4BX relocation
4090 // (bx r[n] instruction).
4091 static const Insn_template elf32_arm_stub_v4_veneer_bx
[] =
4093 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4094 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4095 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4098 // Fill in the stub template look-up table. Stub templates are constructed
4099 // per instance of Stub_factory for fast look-up without locking
4100 // in a thread-enabled environment.
4102 this->stub_templates_
[arm_stub_none
] =
4103 new Stub_template(arm_stub_none
, NULL
, 0);
4105 #define DEF_STUB(x) \
4109 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4110 Stub_type type = arm_stub_##x; \
4111 this->stub_templates_[type] = \
4112 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4120 // Stub_table methods.
4122 // Removel all Cortex-A8 stub.
4124 template<bool big_endian
>
4126 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
4128 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4129 p
!= this->cortex_a8_stubs_
.end();
4132 this->cortex_a8_stubs_
.clear();
4135 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4137 template<bool big_endian
>
4139 Stub_table
<big_endian
>::relocate_stub(
4141 const Relocate_info
<32, big_endian
>* relinfo
,
4142 Target_arm
<big_endian
>* arm_target
,
4143 Output_section
* output_section
,
4144 unsigned char* view
,
4145 Arm_address address
,
4146 section_size_type view_size
)
4148 const Stub_template
* stub_template
= stub
->stub_template();
4149 if (stub_template
->reloc_count() != 0)
4151 // Adjust view to cover the stub only.
4152 section_size_type offset
= stub
->offset();
4153 section_size_type stub_size
= stub_template
->size();
4154 gold_assert(offset
+ stub_size
<= view_size
);
4156 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
4157 address
+ offset
, stub_size
);
4161 // Relocate all stubs in this stub table.
4163 template<bool big_endian
>
4165 Stub_table
<big_endian
>::relocate_stubs(
4166 const Relocate_info
<32, big_endian
>* relinfo
,
4167 Target_arm
<big_endian
>* arm_target
,
4168 Output_section
* output_section
,
4169 unsigned char* view
,
4170 Arm_address address
,
4171 section_size_type view_size
)
4173 // If we are passed a view bigger than the stub table's. we need to
4175 gold_assert(address
== this->address()
4177 == static_cast<section_size_type
>(this->data_size())));
4179 // Relocate all relocation stubs.
4180 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4181 p
!= this->reloc_stubs_
.end();
4183 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4184 address
, view_size
);
4186 // Relocate all Cortex-A8 stubs.
4187 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4188 p
!= this->cortex_a8_stubs_
.end();
4190 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4191 address
, view_size
);
4193 // Relocate all ARM V4BX stubs.
4194 for (Arm_v4bx_stub_list::iterator p
= this->arm_v4bx_stubs_
.begin();
4195 p
!= this->arm_v4bx_stubs_
.end();
4199 this->relocate_stub(*p
, relinfo
, arm_target
, output_section
, view
,
4200 address
, view_size
);
4204 // Write out the stubs to file.
4206 template<bool big_endian
>
4208 Stub_table
<big_endian
>::do_write(Output_file
* of
)
4210 off_t offset
= this->offset();
4211 const section_size_type oview_size
=
4212 convert_to_section_size_type(this->data_size());
4213 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4215 // Write relocation stubs.
4216 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4217 p
!= this->reloc_stubs_
.end();
4220 Reloc_stub
* stub
= p
->second
;
4221 Arm_address address
= this->address() + stub
->offset();
4223 == align_address(address
,
4224 stub
->stub_template()->alignment()));
4225 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4229 // Write Cortex-A8 stubs.
4230 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4231 p
!= this->cortex_a8_stubs_
.end();
4234 Cortex_a8_stub
* stub
= p
->second
;
4235 Arm_address address
= this->address() + stub
->offset();
4237 == align_address(address
,
4238 stub
->stub_template()->alignment()));
4239 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4243 // Write ARM V4BX relocation stubs.
4244 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4245 p
!= this->arm_v4bx_stubs_
.end();
4251 Arm_address address
= this->address() + (*p
)->offset();
4253 == align_address(address
,
4254 (*p
)->stub_template()->alignment()));
4255 (*p
)->write(oview
+ (*p
)->offset(), (*p
)->stub_template()->size(),
4259 of
->write_output_view(this->offset(), oview_size
, oview
);
4262 // Update the data size and address alignment of the stub table at the end
4263 // of a relaxation pass. Return true if either the data size or the
4264 // alignment changed in this relaxation pass.
4266 template<bool big_endian
>
4268 Stub_table
<big_endian
>::update_data_size_and_addralign()
4271 unsigned addralign
= 1;
4273 // Go over all stubs in table to compute data size and address alignment.
4275 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4276 p
!= this->reloc_stubs_
.end();
4279 const Stub_template
* stub_template
= p
->second
->stub_template();
4280 addralign
= std::max(addralign
, stub_template
->alignment());
4281 size
= (align_address(size
, stub_template
->alignment())
4282 + stub_template
->size());
4285 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4286 p
!= this->cortex_a8_stubs_
.end();
4289 const Stub_template
* stub_template
= p
->second
->stub_template();
4290 addralign
= std::max(addralign
, stub_template
->alignment());
4291 size
= (align_address(size
, stub_template
->alignment())
4292 + stub_template
->size());
4295 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4296 p
!= this->arm_v4bx_stubs_
.end();
4302 const Stub_template
* stub_template
= (*p
)->stub_template();
4303 addralign
= std::max(addralign
, stub_template
->alignment());
4304 size
= (align_address(size
, stub_template
->alignment())
4305 + stub_template
->size());
4308 // Check if either data size or alignment changed in this pass.
4309 // Update prev_data_size_ and prev_addralign_. These will be used
4310 // as the current data size and address alignment for the next pass.
4311 bool changed
= size
!= this->prev_data_size_
;
4312 this->prev_data_size_
= size
;
4314 if (addralign
!= this->prev_addralign_
)
4316 this->prev_addralign_
= addralign
;
4321 // Finalize the stubs. This sets the offsets of the stubs within the stub
4322 // table. It also marks all input sections needing Cortex-A8 workaround.
4324 template<bool big_endian
>
4326 Stub_table
<big_endian
>::finalize_stubs()
4329 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4330 p
!= this->reloc_stubs_
.end();
4333 Reloc_stub
* stub
= p
->second
;
4334 const Stub_template
* stub_template
= stub
->stub_template();
4335 uint64_t stub_addralign
= stub_template
->alignment();
4336 off
= align_address(off
, stub_addralign
);
4337 stub
->set_offset(off
);
4338 off
+= stub_template
->size();
4341 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4342 p
!= this->cortex_a8_stubs_
.end();
4345 Cortex_a8_stub
* stub
= p
->second
;
4346 const Stub_template
* stub_template
= stub
->stub_template();
4347 uint64_t stub_addralign
= stub_template
->alignment();
4348 off
= align_address(off
, stub_addralign
);
4349 stub
->set_offset(off
);
4350 off
+= stub_template
->size();
4352 // Mark input section so that we can determine later if a code section
4353 // needs the Cortex-A8 workaround quickly.
4354 Arm_relobj
<big_endian
>* arm_relobj
=
4355 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
4356 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
4359 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4360 p
!= this->arm_v4bx_stubs_
.end();
4366 const Stub_template
* stub_template
= (*p
)->stub_template();
4367 uint64_t stub_addralign
= stub_template
->alignment();
4368 off
= align_address(off
, stub_addralign
);
4369 (*p
)->set_offset(off
);
4370 off
+= stub_template
->size();
4373 gold_assert(off
<= this->prev_data_size_
);
4376 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4377 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4378 // of the address range seen by the linker.
4380 template<bool big_endian
>
4382 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
4383 Target_arm
<big_endian
>* arm_target
,
4384 unsigned char* view
,
4385 Arm_address view_address
,
4386 section_size_type view_size
)
4388 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4389 for (Cortex_a8_stub_list::const_iterator p
=
4390 this->cortex_a8_stubs_
.lower_bound(view_address
);
4391 ((p
!= this->cortex_a8_stubs_
.end())
4392 && (p
->first
< (view_address
+ view_size
)));
4395 // We do not store the THUMB bit in the LSB of either the branch address
4396 // or the stub offset. There is no need to strip the LSB.
4397 Arm_address branch_address
= p
->first
;
4398 const Cortex_a8_stub
* stub
= p
->second
;
4399 Arm_address stub_address
= this->address() + stub
->offset();
4401 // Offset of the branch instruction relative to this view.
4402 section_size_type offset
=
4403 convert_to_section_size_type(branch_address
- view_address
);
4404 gold_assert((offset
+ 4) <= view_size
);
4406 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
4407 view
+ offset
, branch_address
);
4411 // Arm_input_section methods.
4413 // Initialize an Arm_input_section.
4415 template<bool big_endian
>
4417 Arm_input_section
<big_endian
>::init()
4419 Relobj
* relobj
= this->relobj();
4420 unsigned int shndx
= this->shndx();
4422 // Cache these to speed up size and alignment queries. It is too slow
4423 // to call section_addraglin and section_size every time.
4424 this->original_addralign_
= relobj
->section_addralign(shndx
);
4425 this->original_size_
= relobj
->section_size(shndx
);
4427 // We want to make this look like the original input section after
4428 // output sections are finalized.
4429 Output_section
* os
= relobj
->output_section(shndx
);
4430 off_t offset
= relobj
->output_section_offset(shndx
);
4431 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
4432 this->set_address(os
->address() + offset
);
4433 this->set_file_offset(os
->offset() + offset
);
4435 this->set_current_data_size(this->original_size_
);
4436 this->finalize_data_size();
4439 template<bool big_endian
>
4441 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
4443 // We have to write out the original section content.
4444 section_size_type section_size
;
4445 const unsigned char* section_contents
=
4446 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4447 of
->write(this->offset(), section_contents
, section_size
);
4449 // If this owns a stub table and it is not empty, write it.
4450 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
4451 this->stub_table_
->write(of
);
4454 // Finalize data size.
4456 template<bool big_endian
>
4458 Arm_input_section
<big_endian
>::set_final_data_size()
4460 // If this owns a stub table, finalize its data size as well.
4461 if (this->is_stub_table_owner())
4463 uint64_t address
= this->address();
4465 // The stub table comes after the original section contents.
4466 address
+= this->original_size_
;
4467 address
= align_address(address
, this->stub_table_
->addralign());
4468 off_t offset
= this->offset() + (address
- this->address());
4469 this->stub_table_
->set_address_and_file_offset(address
, offset
);
4470 address
+= this->stub_table_
->data_size();
4471 gold_assert(address
== this->address() + this->current_data_size());
4474 this->set_data_size(this->current_data_size());
4477 // Reset address and file offset.
4479 template<bool big_endian
>
4481 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
4483 // Size of the original input section contents.
4484 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
4486 // If this is a stub table owner, account for the stub table size.
4487 if (this->is_stub_table_owner())
4489 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
4491 // Reset the stub table's address and file offset. The
4492 // current data size for child will be updated after that.
4493 stub_table_
->reset_address_and_file_offset();
4494 off
= align_address(off
, stub_table_
->addralign());
4495 off
+= stub_table
->current_data_size();
4498 this->set_current_data_size(off
);
4501 // Arm_exidx_cantunwind methods.
4503 // Write this to Output file OF for a fixed endianity.
4505 template<bool big_endian
>
4507 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file
* of
)
4509 off_t offset
= this->offset();
4510 const section_size_type oview_size
= 8;
4511 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4513 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4514 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
);
4516 Output_section
* os
= this->relobj_
->output_section(this->shndx_
);
4517 gold_assert(os
!= NULL
);
4519 Arm_relobj
<big_endian
>* arm_relobj
=
4520 Arm_relobj
<big_endian
>::as_arm_relobj(this->relobj_
);
4521 Arm_address output_offset
=
4522 arm_relobj
->get_output_section_offset(this->shndx_
);
4523 Arm_address section_start
;
4524 if(output_offset
!= Arm_relobj
<big_endian
>::invalid_address
)
4525 section_start
= os
->address() + output_offset
;
4528 // Currently this only happens for a relaxed section.
4529 const Output_relaxed_input_section
* poris
=
4530 os
->find_relaxed_input_section(this->relobj_
, this->shndx_
);
4531 gold_assert(poris
!= NULL
);
4532 section_start
= poris
->address();
4535 // We always append this to the end of an EXIDX section.
4536 Arm_address output_address
=
4537 section_start
+ this->relobj_
->section_size(this->shndx_
);
4539 // Write out the entry. The first word either points to the beginning
4540 // or after the end of a text section. The second word is the special
4541 // EXIDX_CANTUNWIND value.
4542 elfcpp::Swap
<32, big_endian
>::writeval(wv
, output_address
);
4543 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, elfcpp::EXIDX_CANTUNWIND
);
4545 of
->write_output_view(this->offset(), oview_size
, oview
);
4548 // Arm_exidx_merged_section methods.
4550 // Constructor for Arm_exidx_merged_section.
4551 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4552 // SECTION_OFFSET_MAP points to a section offset map describing how
4553 // parts of the input section are mapped to output. DELETED_BYTES is
4554 // the number of bytes deleted from the EXIDX input section.
4556 Arm_exidx_merged_section::Arm_exidx_merged_section(
4557 const Arm_exidx_input_section
& exidx_input_section
,
4558 const Arm_exidx_section_offset_map
& section_offset_map
,
4559 uint32_t deleted_bytes
)
4560 : Output_relaxed_input_section(exidx_input_section
.relobj(),
4561 exidx_input_section
.shndx(),
4562 exidx_input_section
.addralign()),
4563 exidx_input_section_(exidx_input_section
),
4564 section_offset_map_(section_offset_map
)
4566 // Fix size here so that we do not need to implement set_final_data_size.
4567 this->set_data_size(exidx_input_section
.size() - deleted_bytes
);
4568 this->fix_data_size();
4571 // Given an input OBJECT, an input section index SHNDX within that
4572 // object, and an OFFSET relative to the start of that input
4573 // section, return whether or not the corresponding offset within
4574 // the output section is known. If this function returns true, it
4575 // sets *POUTPUT to the output offset. The value -1 indicates that
4576 // this input offset is being discarded.
4579 Arm_exidx_merged_section::do_output_offset(
4580 const Relobj
* relobj
,
4582 section_offset_type offset
,
4583 section_offset_type
* poutput
) const
4585 // We only handle offsets for the original EXIDX input section.
4586 if (relobj
!= this->exidx_input_section_
.relobj()
4587 || shndx
!= this->exidx_input_section_
.shndx())
4590 section_offset_type section_size
=
4591 convert_types
<section_offset_type
>(this->exidx_input_section_
.size());
4592 if (offset
< 0 || offset
>= section_size
)
4593 // Input offset is out of valid range.
4597 // We need to look up the section offset map to determine the output
4598 // offset. Find the reference point in map that is first offset
4599 // bigger than or equal to this offset.
4600 Arm_exidx_section_offset_map::const_iterator p
=
4601 this->section_offset_map_
.lower_bound(offset
);
4603 // The section offset maps are build such that this should not happen if
4604 // input offset is in the valid range.
4605 gold_assert(p
!= this->section_offset_map_
.end());
4607 // We need to check if this is dropped.
4608 section_offset_type ref
= p
->first
;
4609 section_offset_type mapped_ref
= p
->second
;
4611 if (mapped_ref
!= Arm_exidx_input_section::invalid_offset
)
4612 // Offset is present in output.
4613 *poutput
= mapped_ref
+ (offset
- ref
);
4615 // Offset is discarded owing to EXIDX entry merging.
4622 // Write this to output file OF.
4625 Arm_exidx_merged_section::do_write(Output_file
* of
)
4627 // If we retain or discard the whole EXIDX input section, we would
4629 gold_assert(this->data_size() != this->exidx_input_section_
.size()
4630 && this->data_size() != 0);
4632 off_t offset
= this->offset();
4633 const section_size_type oview_size
= this->data_size();
4634 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4636 Output_section
* os
= this->relobj()->output_section(this->shndx());
4637 gold_assert(os
!= NULL
);
4639 // Get contents of EXIDX input section.
4640 section_size_type section_size
;
4641 const unsigned char* section_contents
=
4642 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4643 gold_assert(section_size
== this->exidx_input_section_
.size());
4645 // Go over spans of input offsets and write only those that are not
4647 section_offset_type in_start
= 0;
4648 section_offset_type out_start
= 0;
4649 for(Arm_exidx_section_offset_map::const_iterator p
=
4650 this->section_offset_map_
.begin();
4651 p
!= this->section_offset_map_
.end();
4654 section_offset_type in_end
= p
->first
;
4655 gold_assert(in_end
>= in_start
);
4656 section_offset_type out_end
= p
->second
;
4657 size_t in_chunk_size
= convert_types
<size_t>(in_end
- in_start
+ 1);
4660 size_t out_chunk_size
=
4661 convert_types
<size_t>(out_end
- out_start
+ 1);
4662 gold_assert(out_chunk_size
== in_chunk_size
);
4663 memcpy(oview
+ out_start
, section_contents
+ in_start
,
4665 out_start
+= out_chunk_size
;
4667 in_start
+= in_chunk_size
;
4670 gold_assert(convert_to_section_size_type(out_start
) == oview_size
);
4671 of
->write_output_view(this->offset(), oview_size
, oview
);
4674 // Arm_exidx_fixup methods.
4676 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
4677 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
4678 // points to the end of the last seen EXIDX section.
4681 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
4683 if (this->last_unwind_type_
!= UT_EXIDX_CANTUNWIND
4684 && this->last_input_section_
!= NULL
)
4686 Relobj
* relobj
= this->last_input_section_
->relobj();
4687 unsigned int shndx
= this->last_input_section_
->shndx();
4688 Arm_exidx_cantunwind
* cantunwind
=
4689 new Arm_exidx_cantunwind(relobj
, shndx
);
4690 this->exidx_output_section_
->add_output_section_data(cantunwind
);
4691 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
4695 // Process an EXIDX section entry in input. Return whether this entry
4696 // can be deleted in the output. SECOND_WORD in the second word of the
4700 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word
)
4703 if (second_word
== elfcpp::EXIDX_CANTUNWIND
)
4705 // Merge if previous entry is also an EXIDX_CANTUNWIND.
4706 delete_entry
= this->last_unwind_type_
== UT_EXIDX_CANTUNWIND
;
4707 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
4709 else if ((second_word
& 0x80000000) != 0)
4711 // Inlined unwinding data. Merge if equal to previous.
4712 delete_entry
= (this->last_unwind_type_
== UT_INLINED_ENTRY
4713 && this->last_inlined_entry_
== second_word
);
4714 this->last_unwind_type_
= UT_INLINED_ENTRY
;
4715 this->last_inlined_entry_
= second_word
;
4719 // Normal table entry. In theory we could merge these too,
4720 // but duplicate entries are likely to be much less common.
4721 delete_entry
= false;
4722 this->last_unwind_type_
= UT_NORMAL_ENTRY
;
4724 return delete_entry
;
4727 // Update the current section offset map during EXIDX section fix-up.
4728 // If there is no map, create one. INPUT_OFFSET is the offset of a
4729 // reference point, DELETED_BYTES is the number of deleted by in the
4730 // section so far. If DELETE_ENTRY is true, the reference point and
4731 // all offsets after the previous reference point are discarded.
4734 Arm_exidx_fixup::update_offset_map(
4735 section_offset_type input_offset
,
4736 section_size_type deleted_bytes
,
4739 if (this->section_offset_map_
== NULL
)
4740 this->section_offset_map_
= new Arm_exidx_section_offset_map();
4741 section_offset_type output_offset
= (delete_entry
4743 : input_offset
- deleted_bytes
);
4744 (*this->section_offset_map_
)[input_offset
] = output_offset
;
4747 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
4748 // bytes deleted. If some entries are merged, also store a pointer to a newly
4749 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
4750 // caller owns the map and is responsible for releasing it after use.
4752 template<bool big_endian
>
4754 Arm_exidx_fixup::process_exidx_section(
4755 const Arm_exidx_input_section
* exidx_input_section
,
4756 Arm_exidx_section_offset_map
** psection_offset_map
)
4758 Relobj
* relobj
= exidx_input_section
->relobj();
4759 unsigned shndx
= exidx_input_section
->shndx();
4760 section_size_type section_size
;
4761 const unsigned char* section_contents
=
4762 relobj
->section_contents(shndx
, §ion_size
, false);
4764 if ((section_size
% 8) != 0)
4766 // Something is wrong with this section. Better not touch it.
4767 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
4768 relobj
->name().c_str(), shndx
);
4769 this->last_input_section_
= exidx_input_section
;
4770 this->last_unwind_type_
= UT_NONE
;
4774 uint32_t deleted_bytes
= 0;
4775 bool prev_delete_entry
= false;
4776 gold_assert(this->section_offset_map_
== NULL
);
4778 for (section_size_type i
= 0; i
< section_size
; i
+= 8)
4780 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4782 reinterpret_cast<const Valtype
*>(section_contents
+ i
+ 4);
4783 uint32_t second_word
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4785 bool delete_entry
= this->process_exidx_entry(second_word
);
4787 // Entry deletion causes changes in output offsets. We use a std::map
4788 // to record these. And entry (x, y) means input offset x
4789 // is mapped to output offset y. If y is invalid_offset, then x is
4790 // dropped in the output. Because of the way std::map::lower_bound
4791 // works, we record the last offset in a region w.r.t to keeping or
4792 // dropping. If there is no entry (x0, y0) for an input offset x0,
4793 // the output offset y0 of it is determined by the output offset y1 of
4794 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
4795 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
4797 if (delete_entry
!= prev_delete_entry
&& i
!= 0)
4798 this->update_offset_map(i
- 1, deleted_bytes
, prev_delete_entry
);
4800 // Update total deleted bytes for this entry.
4804 prev_delete_entry
= delete_entry
;
4807 // If section offset map is not NULL, make an entry for the end of
4809 if (this->section_offset_map_
!= NULL
)
4810 update_offset_map(section_size
- 1, deleted_bytes
, prev_delete_entry
);
4812 *psection_offset_map
= this->section_offset_map_
;
4813 this->section_offset_map_
= NULL
;
4814 this->last_input_section_
= exidx_input_section
;
4816 return deleted_bytes
;
4819 // Arm_output_section methods.
4821 // Create a stub group for input sections from BEGIN to END. OWNER
4822 // points to the input section to be the owner a new stub table.
4824 template<bool big_endian
>
4826 Arm_output_section
<big_endian
>::create_stub_group(
4827 Input_section_list::const_iterator begin
,
4828 Input_section_list::const_iterator end
,
4829 Input_section_list::const_iterator owner
,
4830 Target_arm
<big_endian
>* target
,
4831 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
4833 // Currently we convert ordinary input sections into relaxed sections only
4834 // at this point but we may want to support creating relaxed input section
4835 // very early. So we check here to see if owner is already a relaxed
4838 Arm_input_section
<big_endian
>* arm_input_section
;
4839 if (owner
->is_relaxed_input_section())
4842 Arm_input_section
<big_endian
>::as_arm_input_section(
4843 owner
->relaxed_input_section());
4847 gold_assert(owner
->is_input_section());
4848 // Create a new relaxed input section.
4850 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
4851 new_relaxed_sections
->push_back(arm_input_section
);
4854 // Create a stub table.
4855 Stub_table
<big_endian
>* stub_table
=
4856 target
->new_stub_table(arm_input_section
);
4858 arm_input_section
->set_stub_table(stub_table
);
4860 Input_section_list::const_iterator p
= begin
;
4861 Input_section_list::const_iterator prev_p
;
4863 // Look for input sections or relaxed input sections in [begin ... end].
4866 if (p
->is_input_section() || p
->is_relaxed_input_section())
4868 // The stub table information for input sections live
4869 // in their objects.
4870 Arm_relobj
<big_endian
>* arm_relobj
=
4871 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
4872 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
4876 while (prev_p
!= end
);
4879 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4880 // of stub groups. We grow a stub group by adding input section until the
4881 // size is just below GROUP_SIZE. The last input section will be converted
4882 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4883 // input section after the stub table, effectively double the group size.
4885 // This is similar to the group_sections() function in elf32-arm.c but is
4886 // implemented differently.
4888 template<bool big_endian
>
4890 Arm_output_section
<big_endian
>::group_sections(
4891 section_size_type group_size
,
4892 bool stubs_always_after_branch
,
4893 Target_arm
<big_endian
>* target
)
4895 // We only care about sections containing code.
4896 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
4899 // States for grouping.
4902 // No group is being built.
4904 // A group is being built but the stub table is not found yet.
4905 // We keep group a stub group until the size is just under GROUP_SIZE.
4906 // The last input section in the group will be used as the stub table.
4907 FINDING_STUB_SECTION
,
4908 // A group is being built and we have already found a stub table.
4909 // We enter this state to grow a stub group by adding input section
4910 // after the stub table. This effectively doubles the group size.
4914 // Any newly created relaxed sections are stored here.
4915 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
4917 State state
= NO_GROUP
;
4918 section_size_type off
= 0;
4919 section_size_type group_begin_offset
= 0;
4920 section_size_type group_end_offset
= 0;
4921 section_size_type stub_table_end_offset
= 0;
4922 Input_section_list::const_iterator group_begin
=
4923 this->input_sections().end();
4924 Input_section_list::const_iterator stub_table
=
4925 this->input_sections().end();
4926 Input_section_list::const_iterator group_end
= this->input_sections().end();
4927 for (Input_section_list::const_iterator p
= this->input_sections().begin();
4928 p
!= this->input_sections().end();
4931 section_size_type section_begin_offset
=
4932 align_address(off
, p
->addralign());
4933 section_size_type section_end_offset
=
4934 section_begin_offset
+ p
->data_size();
4936 // Check to see if we should group the previously seens sections.
4942 case FINDING_STUB_SECTION
:
4943 // Adding this section makes the group larger than GROUP_SIZE.
4944 if (section_end_offset
- group_begin_offset
>= group_size
)
4946 if (stubs_always_after_branch
)
4948 gold_assert(group_end
!= this->input_sections().end());
4949 this->create_stub_group(group_begin
, group_end
, group_end
,
4950 target
, &new_relaxed_sections
);
4955 // But wait, there's more! Input sections up to
4956 // stub_group_size bytes after the stub table can be
4957 // handled by it too.
4958 state
= HAS_STUB_SECTION
;
4959 stub_table
= group_end
;
4960 stub_table_end_offset
= group_end_offset
;
4965 case HAS_STUB_SECTION
:
4966 // Adding this section makes the post stub-section group larger
4968 if (section_end_offset
- stub_table_end_offset
>= group_size
)
4970 gold_assert(group_end
!= this->input_sections().end());
4971 this->create_stub_group(group_begin
, group_end
, stub_table
,
4972 target
, &new_relaxed_sections
);
4981 // If we see an input section and currently there is no group, start
4982 // a new one. Skip any empty sections.
4983 if ((p
->is_input_section() || p
->is_relaxed_input_section())
4984 && (p
->relobj()->section_size(p
->shndx()) != 0))
4986 if (state
== NO_GROUP
)
4988 state
= FINDING_STUB_SECTION
;
4990 group_begin_offset
= section_begin_offset
;
4993 // Keep track of the last input section seen.
4995 group_end_offset
= section_end_offset
;
4998 off
= section_end_offset
;
5001 // Create a stub group for any ungrouped sections.
5002 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
5004 gold_assert(group_end
!= this->input_sections().end());
5005 this->create_stub_group(group_begin
, group_end
,
5006 (state
== FINDING_STUB_SECTION
5009 target
, &new_relaxed_sections
);
5012 // Convert input section into relaxed input section in a batch.
5013 if (!new_relaxed_sections
.empty())
5014 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
5016 // Update the section offsets
5017 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
5019 Arm_relobj
<big_endian
>* arm_relobj
=
5020 Arm_relobj
<big_endian
>::as_arm_relobj(
5021 new_relaxed_sections
[i
]->relobj());
5022 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
5023 // Tell Arm_relobj that this input section is converted.
5024 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
5028 // Arm_relobj methods.
5030 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5031 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5033 template<bool big_endian
>
5035 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
5036 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5037 const Relobj::Output_sections
& out_sections
,
5038 const Symbol_table
*symtab
)
5040 unsigned int sh_type
= shdr
.get_sh_type();
5041 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
5044 // Ignore empty section.
5045 off_t sh_size
= shdr
.get_sh_size();
5049 // Ignore reloc section with bad info. This error will be
5050 // reported in the final link.
5051 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
5052 if (index
>= this->shnum())
5055 // This relocation section is against a section which we
5056 // discarded or if the section is folded into another
5057 // section due to ICF.
5058 if (out_sections
[index
] == NULL
|| symtab
->is_section_folded(this, index
))
5061 // Ignore reloc section with unexpected symbol table. The
5062 // error will be reported in the final link.
5063 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
5066 unsigned int reloc_size
;
5067 if (sh_type
== elfcpp::SHT_REL
)
5068 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5070 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
5072 // Ignore reloc section with unexpected entsize or uneven size.
5073 // The error will be reported in the final link.
5074 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
5080 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5081 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5083 template<bool big_endian
>
5085 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
5086 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5089 const Symbol_table
* symtab
)
5091 // We only scan non-empty code sections.
5092 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) == 0
5093 || shdr
.get_sh_size() == 0)
5096 // Ignore discarded or ICF'ed sections.
5097 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
5100 // Find output address of section.
5101 Arm_address address
= os
->output_address(this, shndx
, 0);
5103 // If the section does not cross any 4K-boundaries, it does not need to
5105 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
5111 // Scan a section for Cortex-A8 workaround.
5113 template<bool big_endian
>
5115 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
5116 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5119 Target_arm
<big_endian
>* arm_target
)
5121 Arm_address output_address
= os
->output_address(this, shndx
, 0);
5123 // Get the section contents.
5124 section_size_type input_view_size
= 0;
5125 const unsigned char* input_view
=
5126 this->section_contents(shndx
, &input_view_size
, false);
5128 // We need to go through the mapping symbols to determine what to
5129 // scan. There are two reasons. First, we should look at THUMB code and
5130 // THUMB code only. Second, we only want to look at the 4K-page boundary
5131 // to speed up the scanning.
5133 // Look for the first mapping symbol in this section. It should be
5135 Mapping_symbol_position
section_start(shndx
, 0);
5136 typename
Mapping_symbols_info::const_iterator p
=
5137 this->mapping_symbols_info_
.lower_bound(section_start
);
5139 if (p
== this->mapping_symbols_info_
.end()
5140 || p
->first
!= section_start
)
5142 gold_warning(_("Cortex-A8 erratum scanning failed because there "
5143 "is no mapping symbols for section %u of %s"),
5144 shndx
, this->name().c_str());
5148 while (p
!= this->mapping_symbols_info_
.end()
5149 && p
->first
.first
== shndx
)
5151 typename
Mapping_symbols_info::const_iterator next
=
5152 this->mapping_symbols_info_
.upper_bound(p
->first
);
5154 // Only scan part of a section with THUMB code.
5155 if (p
->second
== 't')
5157 // Determine the end of this range.
5158 section_size_type span_start
=
5159 convert_to_section_size_type(p
->first
.second
);
5160 section_size_type span_end
;
5161 if (next
!= this->mapping_symbols_info_
.end()
5162 && next
->first
.first
== shndx
)
5163 span_end
= convert_to_section_size_type(next
->first
.second
);
5165 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
5167 if (((span_start
+ output_address
) & ~0xfffUL
)
5168 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
5170 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
5171 span_start
, span_end
,
5181 // Scan relocations for stub generation.
5183 template<bool big_endian
>
5185 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
5186 Target_arm
<big_endian
>* arm_target
,
5187 const Symbol_table
* symtab
,
5188 const Layout
* layout
)
5190 unsigned int shnum
= this->shnum();
5191 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5193 // Read the section headers.
5194 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
5198 // To speed up processing, we set up hash tables for fast lookup of
5199 // input offsets to output addresses.
5200 this->initialize_input_to_output_maps();
5202 const Relobj::Output_sections
& out_sections(this->output_sections());
5204 Relocate_info
<32, big_endian
> relinfo
;
5205 relinfo
.symtab
= symtab
;
5206 relinfo
.layout
= layout
;
5207 relinfo
.object
= this;
5209 // Do relocation stubs scanning.
5210 const unsigned char* p
= pshdrs
+ shdr_size
;
5211 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
5213 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
5214 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
))
5216 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
5217 Arm_address output_offset
= this->get_output_section_offset(index
);
5218 Arm_address output_address
;
5219 if(output_offset
!= invalid_address
)
5220 output_address
= out_sections
[index
]->address() + output_offset
;
5223 // Currently this only happens for a relaxed section.
5224 const Output_relaxed_input_section
* poris
=
5225 out_sections
[index
]->find_relaxed_input_section(this, index
);
5226 gold_assert(poris
!= NULL
);
5227 output_address
= poris
->address();
5230 // Get the relocations.
5231 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
5235 // Get the section contents. This does work for the case in which
5236 // we modify the contents of an input section. We need to pass the
5237 // output view under such circumstances.
5238 section_size_type input_view_size
= 0;
5239 const unsigned char* input_view
=
5240 this->section_contents(index
, &input_view_size
, false);
5242 relinfo
.reloc_shndx
= i
;
5243 relinfo
.data_shndx
= index
;
5244 unsigned int sh_type
= shdr
.get_sh_type();
5245 unsigned int reloc_size
;
5246 if (sh_type
== elfcpp::SHT_REL
)
5247 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5249 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
5251 Output_section
* os
= out_sections
[index
];
5252 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
5253 shdr
.get_sh_size() / reloc_size
,
5255 output_offset
== invalid_address
,
5256 input_view
, output_address
,
5261 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5262 // after its relocation section, if there is one, is processed for
5263 // relocation stubs. Merging this loop with the one above would have been
5264 // complicated since we would have had to make sure that relocation stub
5265 // scanning is done first.
5266 if (arm_target
->fix_cortex_a8())
5268 const unsigned char* p
= pshdrs
+ shdr_size
;
5269 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
5271 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
5272 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
5275 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
5280 // After we've done the relocations, we release the hash tables,
5281 // since we no longer need them.
5282 this->free_input_to_output_maps();
5285 // Count the local symbols. The ARM backend needs to know if a symbol
5286 // is a THUMB function or not. For global symbols, it is easy because
5287 // the Symbol object keeps the ELF symbol type. For local symbol it is
5288 // harder because we cannot access this information. So we override the
5289 // do_count_local_symbol in parent and scan local symbols to mark
5290 // THUMB functions. This is not the most efficient way but I do not want to
5291 // slow down other ports by calling a per symbol targer hook inside
5292 // Sized_relobj<size, big_endian>::do_count_local_symbols.
5294 template<bool big_endian
>
5296 Arm_relobj
<big_endian
>::do_count_local_symbols(
5297 Stringpool_template
<char>* pool
,
5298 Stringpool_template
<char>* dynpool
)
5300 // We need to fix-up the values of any local symbols whose type are
5303 // Ask parent to count the local symbols.
5304 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
5305 const unsigned int loccount
= this->local_symbol_count();
5309 // Intialize the thumb function bit-vector.
5310 std::vector
<bool> empty_vector(loccount
, false);
5311 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
5313 // Read the symbol table section header.
5314 const unsigned int symtab_shndx
= this->symtab_shndx();
5315 elfcpp::Shdr
<32, big_endian
>
5316 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
5317 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
5319 // Read the local symbols.
5320 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
5321 gold_assert(loccount
== symtabshdr
.get_sh_info());
5322 off_t locsize
= loccount
* sym_size
;
5323 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
5324 locsize
, true, true);
5326 // For mapping symbol processing, we need to read the symbol names.
5327 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
5328 if (strtab_shndx
>= this->shnum())
5330 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
5334 elfcpp::Shdr
<32, big_endian
>
5335 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
5336 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
5338 this->error(_("symbol table name section has wrong type: %u"),
5339 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
5342 const char* pnames
=
5343 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
5344 strtabshdr
.get_sh_size(),
5347 // Loop over the local symbols and mark any local symbols pointing
5348 // to THUMB functions.
5350 // Skip the first dummy symbol.
5352 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
5353 this->local_values();
5354 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
5356 elfcpp::Sym
<32, big_endian
> sym(psyms
);
5357 elfcpp::STT st_type
= sym
.get_st_type();
5358 Symbol_value
<32>& lv((*plocal_values
)[i
]);
5359 Arm_address input_value
= lv
.input_value();
5361 // Check to see if this is a mapping symbol.
5362 const char* sym_name
= pnames
+ sym
.get_st_name();
5363 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
5365 unsigned int input_shndx
= sym
.get_st_shndx();
5367 // Strip of LSB in case this is a THUMB symbol.
5368 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
5369 this->mapping_symbols_info_
[msp
] = sym_name
[1];
5372 if (st_type
== elfcpp::STT_ARM_TFUNC
5373 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
5375 // This is a THUMB function. Mark this and canonicalize the
5376 // symbol value by setting LSB.
5377 this->local_symbol_is_thumb_function_
[i
] = true;
5378 if ((input_value
& 1) == 0)
5379 lv
.set_input_value(input_value
| 1);
5384 // Relocate sections.
5385 template<bool big_endian
>
5387 Arm_relobj
<big_endian
>::do_relocate_sections(
5388 const Symbol_table
* symtab
,
5389 const Layout
* layout
,
5390 const unsigned char* pshdrs
,
5391 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
5393 // Call parent to relocate sections.
5394 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
5397 // We do not generate stubs if doing a relocatable link.
5398 if (parameters
->options().relocatable())
5401 // Relocate stub tables.
5402 unsigned int shnum
= this->shnum();
5404 Target_arm
<big_endian
>* arm_target
=
5405 Target_arm
<big_endian
>::default_target();
5407 Relocate_info
<32, big_endian
> relinfo
;
5408 relinfo
.symtab
= symtab
;
5409 relinfo
.layout
= layout
;
5410 relinfo
.object
= this;
5412 for (unsigned int i
= 1; i
< shnum
; ++i
)
5414 Arm_input_section
<big_endian
>* arm_input_section
=
5415 arm_target
->find_arm_input_section(this, i
);
5417 if (arm_input_section
!= NULL
5418 && arm_input_section
->is_stub_table_owner()
5419 && !arm_input_section
->stub_table()->empty())
5421 // We cannot discard a section if it owns a stub table.
5422 Output_section
* os
= this->output_section(i
);
5423 gold_assert(os
!= NULL
);
5425 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
5426 relinfo
.reloc_shdr
= NULL
;
5427 relinfo
.data_shndx
= i
;
5428 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
5430 gold_assert((*pviews
)[i
].view
!= NULL
);
5432 // We are passed the output section view. Adjust it to cover the
5434 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
5435 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
5436 && ((stub_table
->address() + stub_table
->data_size())
5437 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
5439 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
5440 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
5441 Arm_address address
= stub_table
->address();
5442 section_size_type view_size
= stub_table
->data_size();
5444 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
5448 // Apply Cortex A8 workaround if applicable.
5449 if (this->section_has_cortex_a8_workaround(i
))
5451 unsigned char* view
= (*pviews
)[i
].view
;
5452 Arm_address view_address
= (*pviews
)[i
].address
;
5453 section_size_type view_size
= (*pviews
)[i
].view_size
;
5454 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
5456 // Adjust view to cover section.
5457 Output_section
* os
= this->output_section(i
);
5458 gold_assert(os
!= NULL
);
5459 Arm_address section_address
= os
->output_address(this, i
, 0);
5460 uint64_t section_size
= this->section_size(i
);
5462 gold_assert(section_address
>= view_address
5463 && ((section_address
+ section_size
)
5464 <= (view_address
+ view_size
)));
5466 unsigned char* section_view
= view
+ (section_address
- view_address
);
5468 // Apply the Cortex-A8 workaround to the output address range
5469 // corresponding to this input section.
5470 stub_table
->apply_cortex_a8_workaround_to_address_range(
5479 // Create a new EXIDX input section object for EXIDX section SHNDX with
5482 template<bool big_endian
>
5484 Arm_relobj
<big_endian
>::make_exidx_input_section(
5486 const elfcpp::Shdr
<32, big_endian
>& shdr
)
5488 // Link .text section to its .ARM.exidx section in the same object.
5489 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
5491 // Issue an error and ignore this EXIDX section if it does not point
5492 // to any text section.
5493 if (text_shndx
== elfcpp::SHN_UNDEF
)
5495 gold_error(_("EXIDX section %u in %s has no linked text section"),
5496 shndx
, this->name().c_str());
5500 // Issue an error and ignore this EXIDX section if it points to a text
5501 // section already has an EXIDX section.
5502 if (this->exidx_section_map_
[text_shndx
] != NULL
)
5504 gold_error(_("EXIDX sections %u and %u both link to text section %u "
5506 shndx
, this->exidx_section_map_
[text_shndx
]->shndx(),
5507 text_shndx
, this->name().c_str());
5511 // Create an Arm_exidx_input_section object for this EXIDX section.
5512 Arm_exidx_input_section
* exidx_input_section
=
5513 new Arm_exidx_input_section(this, shndx
, text_shndx
, shdr
.get_sh_size(),
5514 shdr
.get_sh_addralign());
5515 this->exidx_section_map_
[text_shndx
] = exidx_input_section
;
5517 // Also map the EXIDX section index to this.
5518 gold_assert(this->exidx_section_map_
[shndx
] == NULL
);
5519 this->exidx_section_map_
[shndx
] = exidx_input_section
;
5522 // Read the symbol information.
5524 template<bool big_endian
>
5526 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
5528 // Call parent class to read symbol information.
5529 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
5531 // Read processor-specific flags in ELF file header.
5532 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
5533 elfcpp::Elf_sizes
<32>::ehdr_size
,
5535 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
5536 this->processor_specific_flags_
= ehdr
.get_e_flags();
5538 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
5540 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5541 const unsigned char *ps
=
5542 sd
->section_headers
->data() + shdr_size
;
5543 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
5545 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5546 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
5548 gold_assert(this->attributes_section_data_
== NULL
);
5549 section_offset_type section_offset
= shdr
.get_sh_offset();
5550 section_size_type section_size
=
5551 convert_to_section_size_type(shdr
.get_sh_size());
5552 File_view
* view
= this->get_lasting_view(section_offset
,
5553 section_size
, true, false);
5554 this->attributes_section_data_
=
5555 new Attributes_section_data(view
->data(), section_size
);
5557 else if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
5558 this->make_exidx_input_section(i
, shdr
);
5562 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
5563 // sections for unwinding. These sections are referenced implicitly by
5564 // text sections linked in the section headers. If we ignore these implict
5565 // references, the .ARM.exidx sections and any .ARM.extab sections they use
5566 // will be garbage-collected incorrectly. Hence we override the same function
5567 // in the base class to handle these implicit references.
5569 template<bool big_endian
>
5571 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
5573 Read_relocs_data
* rd
)
5575 // First, call base class method to process relocations in this object.
5576 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
5578 unsigned int shnum
= this->shnum();
5579 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5580 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
5584 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
5585 // to these from the linked text sections.
5586 const unsigned char* ps
= pshdrs
+ shdr_size
;
5587 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
5589 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5590 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
5592 // Found an .ARM.exidx section, add it to the set of reachable
5593 // sections from its linked text section.
5594 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
5595 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
5600 // Arm_dynobj methods.
5602 // Read the symbol information.
5604 template<bool big_endian
>
5606 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
5608 // Call parent class to read symbol information.
5609 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
5611 // Read processor-specific flags in ELF file header.
5612 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
5613 elfcpp::Elf_sizes
<32>::ehdr_size
,
5615 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
5616 this->processor_specific_flags_
= ehdr
.get_e_flags();
5618 // Read the attributes section if there is one.
5619 // We read from the end because gas seems to put it near the end of
5620 // the section headers.
5621 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5622 const unsigned char *ps
=
5623 sd
->section_headers
->data() + shdr_size
* (this->shnum() - 1);
5624 for (unsigned int i
= this->shnum(); i
> 0; --i
, ps
-= shdr_size
)
5626 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
5627 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
5629 section_offset_type section_offset
= shdr
.get_sh_offset();
5630 section_size_type section_size
=
5631 convert_to_section_size_type(shdr
.get_sh_size());
5632 File_view
* view
= this->get_lasting_view(section_offset
,
5633 section_size
, true, false);
5634 this->attributes_section_data_
=
5635 new Attributes_section_data(view
->data(), section_size
);
5641 // Stub_addend_reader methods.
5643 // Read the addend of a REL relocation of type R_TYPE at VIEW.
5645 template<bool big_endian
>
5646 elfcpp::Elf_types
<32>::Elf_Swxword
5647 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
5648 unsigned int r_type
,
5649 const unsigned char* view
,
5650 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
5652 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
5656 case elfcpp::R_ARM_CALL
:
5657 case elfcpp::R_ARM_JUMP24
:
5658 case elfcpp::R_ARM_PLT32
:
5660 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5661 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5662 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5663 return utils::sign_extend
<26>(val
<< 2);
5666 case elfcpp::R_ARM_THM_CALL
:
5667 case elfcpp::R_ARM_THM_JUMP24
:
5668 case elfcpp::R_ARM_THM_XPC22
:
5670 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5671 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5672 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5673 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5674 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
5677 case elfcpp::R_ARM_THM_JUMP19
:
5679 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
5680 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
5681 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
5682 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
5683 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
5691 // A class to handle the PLT data.
5693 template<bool big_endian
>
5694 class Output_data_plt_arm
: public Output_section_data
5697 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
5700 Output_data_plt_arm(Layout
*, Output_data_space
*);
5702 // Add an entry to the PLT.
5704 add_entry(Symbol
* gsym
);
5706 // Return the .rel.plt section data.
5707 const Reloc_section
*
5709 { return this->rel_
; }
5713 do_adjust_output_section(Output_section
* os
);
5715 // Write to a map file.
5717 do_print_to_mapfile(Mapfile
* mapfile
) const
5718 { mapfile
->print_output_data(this, _("** PLT")); }
5721 // Template for the first PLT entry.
5722 static const uint32_t first_plt_entry
[5];
5724 // Template for subsequent PLT entries.
5725 static const uint32_t plt_entry
[3];
5727 // Set the final size.
5729 set_final_data_size()
5731 this->set_data_size(sizeof(first_plt_entry
)
5732 + this->count_
* sizeof(plt_entry
));
5735 // Write out the PLT data.
5737 do_write(Output_file
*);
5739 // The reloc section.
5740 Reloc_section
* rel_
;
5741 // The .got.plt section.
5742 Output_data_space
* got_plt_
;
5743 // The number of PLT entries.
5744 unsigned int count_
;
5747 // Create the PLT section. The ordinary .got section is an argument,
5748 // since we need to refer to the start. We also create our own .got
5749 // section just for PLT entries.
5751 template<bool big_endian
>
5752 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
5753 Output_data_space
* got_plt
)
5754 : Output_section_data(4), got_plt_(got_plt
), count_(0)
5756 this->rel_
= new Reloc_section(false);
5757 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
5758 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
5762 template<bool big_endian
>
5764 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
5769 // Add an entry to the PLT.
5771 template<bool big_endian
>
5773 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
5775 gold_assert(!gsym
->has_plt_offset());
5777 // Note that when setting the PLT offset we skip the initial
5778 // reserved PLT entry.
5779 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
5780 + sizeof(first_plt_entry
));
5784 section_offset_type got_offset
= this->got_plt_
->current_data_size();
5786 // Every PLT entry needs a GOT entry which points back to the PLT
5787 // entry (this will be changed by the dynamic linker, normally
5788 // lazily when the function is called).
5789 this->got_plt_
->set_current_data_size(got_offset
+ 4);
5791 // Every PLT entry needs a reloc.
5792 gsym
->set_needs_dynsym_entry();
5793 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
5796 // Note that we don't need to save the symbol. The contents of the
5797 // PLT are independent of which symbols are used. The symbols only
5798 // appear in the relocations.
5802 // FIXME: This is not very flexible. Right now this has only been tested
5803 // on armv5te. If we are to support additional architecture features like
5804 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
5806 // The first entry in the PLT.
5807 template<bool big_endian
>
5808 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
5810 0xe52de004, // str lr, [sp, #-4]!
5811 0xe59fe004, // ldr lr, [pc, #4]
5812 0xe08fe00e, // add lr, pc, lr
5813 0xe5bef008, // ldr pc, [lr, #8]!
5814 0x00000000, // &GOT[0] - .
5817 // Subsequent entries in the PLT.
5819 template<bool big_endian
>
5820 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
5822 0xe28fc600, // add ip, pc, #0xNN00000
5823 0xe28cca00, // add ip, ip, #0xNN000
5824 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
5827 // Write out the PLT. This uses the hand-coded instructions above,
5828 // and adjusts them as needed. This is all specified by the arm ELF
5829 // Processor Supplement.
5831 template<bool big_endian
>
5833 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
5835 const off_t offset
= this->offset();
5836 const section_size_type oview_size
=
5837 convert_to_section_size_type(this->data_size());
5838 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5840 const off_t got_file_offset
= this->got_plt_
->offset();
5841 const section_size_type got_size
=
5842 convert_to_section_size_type(this->got_plt_
->data_size());
5843 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
5845 unsigned char* pov
= oview
;
5847 Arm_address plt_address
= this->address();
5848 Arm_address got_address
= this->got_plt_
->address();
5850 // Write first PLT entry. All but the last word are constants.
5851 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
5852 / sizeof(plt_entry
[0]));
5853 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
5854 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
5855 // Last word in first PLT entry is &GOT[0] - .
5856 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
5857 got_address
- (plt_address
+ 16));
5858 pov
+= sizeof(first_plt_entry
);
5860 unsigned char* got_pov
= got_view
;
5862 memset(got_pov
, 0, 12);
5865 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5866 unsigned int plt_offset
= sizeof(first_plt_entry
);
5867 unsigned int plt_rel_offset
= 0;
5868 unsigned int got_offset
= 12;
5869 const unsigned int count
= this->count_
;
5870 for (unsigned int i
= 0;
5873 pov
+= sizeof(plt_entry
),
5875 plt_offset
+= sizeof(plt_entry
),
5876 plt_rel_offset
+= rel_size
,
5879 // Set and adjust the PLT entry itself.
5880 int32_t offset
= ((got_address
+ got_offset
)
5881 - (plt_address
+ plt_offset
+ 8));
5883 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
5884 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
5885 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
5886 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
5887 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
5888 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
5889 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
5891 // Set the entry in the GOT.
5892 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
5895 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
5896 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
5898 of
->write_output_view(offset
, oview_size
, oview
);
5899 of
->write_output_view(got_file_offset
, got_size
, got_view
);
5902 // Create a PLT entry for a global symbol.
5904 template<bool big_endian
>
5906 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
5909 if (gsym
->has_plt_offset())
5912 if (this->plt_
== NULL
)
5914 // Create the GOT sections first.
5915 this->got_section(symtab
, layout
);
5917 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
5918 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
5920 | elfcpp::SHF_EXECINSTR
),
5921 this->plt_
, false, false, false, false);
5923 this->plt_
->add_entry(gsym
);
5926 // Report an unsupported relocation against a local symbol.
5928 template<bool big_endian
>
5930 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
5931 Sized_relobj
<32, big_endian
>* object
,
5932 unsigned int r_type
)
5934 gold_error(_("%s: unsupported reloc %u against local symbol"),
5935 object
->name().c_str(), r_type
);
5938 // We are about to emit a dynamic relocation of type R_TYPE. If the
5939 // dynamic linker does not support it, issue an error. The GNU linker
5940 // only issues a non-PIC error for an allocated read-only section.
5941 // Here we know the section is allocated, but we don't know that it is
5942 // read-only. But we check for all the relocation types which the
5943 // glibc dynamic linker supports, so it seems appropriate to issue an
5944 // error even if the section is not read-only.
5946 template<bool big_endian
>
5948 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
5949 unsigned int r_type
)
5953 // These are the relocation types supported by glibc for ARM.
5954 case elfcpp::R_ARM_RELATIVE
:
5955 case elfcpp::R_ARM_COPY
:
5956 case elfcpp::R_ARM_GLOB_DAT
:
5957 case elfcpp::R_ARM_JUMP_SLOT
:
5958 case elfcpp::R_ARM_ABS32
:
5959 case elfcpp::R_ARM_ABS32_NOI
:
5960 case elfcpp::R_ARM_PC24
:
5961 // FIXME: The following 3 types are not supported by Android's dynamic
5963 case elfcpp::R_ARM_TLS_DTPMOD32
:
5964 case elfcpp::R_ARM_TLS_DTPOFF32
:
5965 case elfcpp::R_ARM_TLS_TPOFF32
:
5969 // This prevents us from issuing more than one error per reloc
5970 // section. But we can still wind up issuing more than one
5971 // error per object file.
5972 if (this->issued_non_pic_error_
)
5974 object
->error(_("requires unsupported dynamic reloc; "
5975 "recompile with -fPIC"));
5976 this->issued_non_pic_error_
= true;
5979 case elfcpp::R_ARM_NONE
:
5984 // Scan a relocation for a local symbol.
5985 // FIXME: This only handles a subset of relocation types used by Android
5986 // on ARM v5te devices.
5988 template<bool big_endian
>
5990 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
5993 Sized_relobj
<32, big_endian
>* object
,
5994 unsigned int data_shndx
,
5995 Output_section
* output_section
,
5996 const elfcpp::Rel
<32, big_endian
>& reloc
,
5997 unsigned int r_type
,
5998 const elfcpp::Sym
<32, big_endian
>&)
6000 r_type
= get_real_reloc_type(r_type
);
6003 case elfcpp::R_ARM_NONE
:
6006 case elfcpp::R_ARM_ABS32
:
6007 case elfcpp::R_ARM_ABS32_NOI
:
6008 // If building a shared library (or a position-independent
6009 // executable), we need to create a dynamic relocation for
6010 // this location. The relocation applied at link time will
6011 // apply the link-time value, so we flag the location with
6012 // an R_ARM_RELATIVE relocation so the dynamic loader can
6013 // relocate it easily.
6014 if (parameters
->options().output_is_position_independent())
6016 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6017 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
6018 // If we are to add more other reloc types than R_ARM_ABS32,
6019 // we need to add check_non_pic(object, r_type) here.
6020 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
6021 output_section
, data_shndx
,
6022 reloc
.get_r_offset());
6026 case elfcpp::R_ARM_REL32
:
6027 case elfcpp::R_ARM_THM_CALL
:
6028 case elfcpp::R_ARM_CALL
:
6029 case elfcpp::R_ARM_PREL31
:
6030 case elfcpp::R_ARM_JUMP24
:
6031 case elfcpp::R_ARM_THM_JUMP24
:
6032 case elfcpp::R_ARM_THM_JUMP19
:
6033 case elfcpp::R_ARM_PLT32
:
6034 case elfcpp::R_ARM_THM_ABS5
:
6035 case elfcpp::R_ARM_ABS8
:
6036 case elfcpp::R_ARM_ABS12
:
6037 case elfcpp::R_ARM_ABS16
:
6038 case elfcpp::R_ARM_BASE_ABS
:
6039 case elfcpp::R_ARM_MOVW_ABS_NC
:
6040 case elfcpp::R_ARM_MOVT_ABS
:
6041 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6042 case elfcpp::R_ARM_THM_MOVT_ABS
:
6043 case elfcpp::R_ARM_MOVW_PREL_NC
:
6044 case elfcpp::R_ARM_MOVT_PREL
:
6045 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6046 case elfcpp::R_ARM_THM_MOVT_PREL
:
6047 case elfcpp::R_ARM_THM_JUMP6
:
6048 case elfcpp::R_ARM_THM_JUMP8
:
6049 case elfcpp::R_ARM_THM_JUMP11
:
6050 case elfcpp::R_ARM_V4BX
:
6053 case elfcpp::R_ARM_GOTOFF32
:
6054 // We need a GOT section:
6055 target
->got_section(symtab
, layout
);
6058 case elfcpp::R_ARM_BASE_PREL
:
6059 // FIXME: What about this?
6062 case elfcpp::R_ARM_GOT_BREL
:
6063 case elfcpp::R_ARM_GOT_PREL
:
6065 // The symbol requires a GOT entry.
6066 Output_data_got
<32, big_endian
>* got
=
6067 target
->got_section(symtab
, layout
);
6068 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
6069 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
6071 // If we are generating a shared object, we need to add a
6072 // dynamic RELATIVE relocation for this symbol's GOT entry.
6073 if (parameters
->options().output_is_position_independent())
6075 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6076 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
6077 rel_dyn
->add_local_relative(
6078 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
6079 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6085 case elfcpp::R_ARM_TARGET1
:
6086 // This should have been mapped to another type already.
6088 case elfcpp::R_ARM_COPY
:
6089 case elfcpp::R_ARM_GLOB_DAT
:
6090 case elfcpp::R_ARM_JUMP_SLOT
:
6091 case elfcpp::R_ARM_RELATIVE
:
6092 // These are relocations which should only be seen by the
6093 // dynamic linker, and should never be seen here.
6094 gold_error(_("%s: unexpected reloc %u in object file"),
6095 object
->name().c_str(), r_type
);
6099 unsupported_reloc_local(object
, r_type
);
6104 // Report an unsupported relocation against a global symbol.
6106 template<bool big_endian
>
6108 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
6109 Sized_relobj
<32, big_endian
>* object
,
6110 unsigned int r_type
,
6113 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6114 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6117 // Scan a relocation for a global symbol.
6118 // FIXME: This only handles a subset of relocation types used by Android
6119 // on ARM v5te devices.
6121 template<bool big_endian
>
6123 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
6126 Sized_relobj
<32, big_endian
>* object
,
6127 unsigned int data_shndx
,
6128 Output_section
* output_section
,
6129 const elfcpp::Rel
<32, big_endian
>& reloc
,
6130 unsigned int r_type
,
6133 r_type
= get_real_reloc_type(r_type
);
6136 case elfcpp::R_ARM_NONE
:
6139 case elfcpp::R_ARM_ABS32
:
6140 case elfcpp::R_ARM_ABS32_NOI
:
6142 // Make a dynamic relocation if necessary.
6143 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
6145 if (target
->may_need_copy_reloc(gsym
))
6147 target
->copy_reloc(symtab
, layout
, object
,
6148 data_shndx
, output_section
, gsym
, reloc
);
6150 else if (gsym
->can_use_relative_reloc(false))
6152 // If we are to add more other reloc types than R_ARM_ABS32,
6153 // we need to add check_non_pic(object, r_type) here.
6154 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6155 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
6156 output_section
, object
,
6157 data_shndx
, reloc
.get_r_offset());
6161 // If we are to add more other reloc types than R_ARM_ABS32,
6162 // we need to add check_non_pic(object, r_type) here.
6163 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6164 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6165 data_shndx
, reloc
.get_r_offset());
6171 case elfcpp::R_ARM_MOVW_ABS_NC
:
6172 case elfcpp::R_ARM_MOVT_ABS
:
6173 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6174 case elfcpp::R_ARM_THM_MOVT_ABS
:
6175 case elfcpp::R_ARM_MOVW_PREL_NC
:
6176 case elfcpp::R_ARM_MOVT_PREL
:
6177 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6178 case elfcpp::R_ARM_THM_MOVT_PREL
:
6179 case elfcpp::R_ARM_THM_JUMP6
:
6180 case elfcpp::R_ARM_THM_JUMP8
:
6181 case elfcpp::R_ARM_THM_JUMP11
:
6182 case elfcpp::R_ARM_V4BX
:
6185 case elfcpp::R_ARM_THM_ABS5
:
6186 case elfcpp::R_ARM_ABS8
:
6187 case elfcpp::R_ARM_ABS12
:
6188 case elfcpp::R_ARM_ABS16
:
6189 case elfcpp::R_ARM_BASE_ABS
:
6191 // No dynamic relocs of this kinds.
6192 // Report the error in case of PIC.
6193 int flags
= Symbol::NON_PIC_REF
;
6194 if (gsym
->type() == elfcpp::STT_FUNC
6195 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6196 flags
|= Symbol::FUNCTION_CALL
;
6197 if (gsym
->needs_dynamic_reloc(flags
))
6198 check_non_pic(object
, r_type
);
6202 case elfcpp::R_ARM_REL32
:
6203 case elfcpp::R_ARM_PREL31
:
6205 // Make a dynamic relocation if necessary.
6206 int flags
= Symbol::NON_PIC_REF
;
6207 if (gsym
->needs_dynamic_reloc(flags
))
6209 if (target
->may_need_copy_reloc(gsym
))
6211 target
->copy_reloc(symtab
, layout
, object
,
6212 data_shndx
, output_section
, gsym
, reloc
);
6216 check_non_pic(object
, r_type
);
6217 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6218 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6219 data_shndx
, reloc
.get_r_offset());
6225 case elfcpp::R_ARM_JUMP24
:
6226 case elfcpp::R_ARM_THM_JUMP24
:
6227 case elfcpp::R_ARM_THM_JUMP19
:
6228 case elfcpp::R_ARM_CALL
:
6229 case elfcpp::R_ARM_THM_CALL
:
6231 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
6232 target
->make_plt_entry(symtab
, layout
, gsym
);
6235 // Check to see if this is a function that would need a PLT
6236 // but does not get one because the function symbol is untyped.
6237 // This happens in assembly code missing a proper .type directive.
6238 if ((!gsym
->is_undefined() || parameters
->options().shared())
6239 && !parameters
->doing_static_link()
6240 && gsym
->type() == elfcpp::STT_NOTYPE
6241 && (gsym
->is_from_dynobj()
6242 || gsym
->is_undefined()
6243 || gsym
->is_preemptible()))
6244 gold_error(_("%s is not a function."),
6245 gsym
->demangled_name().c_str());
6249 case elfcpp::R_ARM_PLT32
:
6250 // If the symbol is fully resolved, this is just a relative
6251 // local reloc. Otherwise we need a PLT entry.
6252 if (gsym
->final_value_is_known())
6254 // If building a shared library, we can also skip the PLT entry
6255 // if the symbol is defined in the output file and is protected
6257 if (gsym
->is_defined()
6258 && !gsym
->is_from_dynobj()
6259 && !gsym
->is_preemptible())
6261 target
->make_plt_entry(symtab
, layout
, gsym
);
6264 case elfcpp::R_ARM_GOTOFF32
:
6265 // We need a GOT section.
6266 target
->got_section(symtab
, layout
);
6269 case elfcpp::R_ARM_BASE_PREL
:
6270 // FIXME: What about this?
6273 case elfcpp::R_ARM_GOT_BREL
:
6274 case elfcpp::R_ARM_GOT_PREL
:
6276 // The symbol requires a GOT entry.
6277 Output_data_got
<32, big_endian
>* got
=
6278 target
->got_section(symtab
, layout
);
6279 if (gsym
->final_value_is_known())
6280 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6283 // If this symbol is not fully resolved, we need to add a
6284 // GOT entry with a dynamic relocation.
6285 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
6286 if (gsym
->is_from_dynobj()
6287 || gsym
->is_undefined()
6288 || gsym
->is_preemptible())
6289 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
6290 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
6293 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
6294 rel_dyn
->add_global_relative(
6295 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
6296 gsym
->got_offset(GOT_TYPE_STANDARD
));
6302 case elfcpp::R_ARM_TARGET1
:
6303 // This should have been mapped to another type already.
6305 case elfcpp::R_ARM_COPY
:
6306 case elfcpp::R_ARM_GLOB_DAT
:
6307 case elfcpp::R_ARM_JUMP_SLOT
:
6308 case elfcpp::R_ARM_RELATIVE
:
6309 // These are relocations which should only be seen by the
6310 // dynamic linker, and should never be seen here.
6311 gold_error(_("%s: unexpected reloc %u in object file"),
6312 object
->name().c_str(), r_type
);
6316 unsupported_reloc_global(object
, r_type
, gsym
);
6321 // Process relocations for gc.
6323 template<bool big_endian
>
6325 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
6327 Sized_relobj
<32, big_endian
>* object
,
6328 unsigned int data_shndx
,
6330 const unsigned char* prelocs
,
6332 Output_section
* output_section
,
6333 bool needs_special_offset_handling
,
6334 size_t local_symbol_count
,
6335 const unsigned char* plocal_symbols
)
6337 typedef Target_arm
<big_endian
> Arm
;
6338 typedef typename Target_arm
<big_endian
>::Scan Scan
;
6340 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
6349 needs_special_offset_handling
,
6354 // Scan relocations for a section.
6356 template<bool big_endian
>
6358 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
6360 Sized_relobj
<32, big_endian
>* object
,
6361 unsigned int data_shndx
,
6362 unsigned int sh_type
,
6363 const unsigned char* prelocs
,
6365 Output_section
* output_section
,
6366 bool needs_special_offset_handling
,
6367 size_t local_symbol_count
,
6368 const unsigned char* plocal_symbols
)
6370 typedef typename Target_arm
<big_endian
>::Scan Scan
;
6371 if (sh_type
== elfcpp::SHT_RELA
)
6373 gold_error(_("%s: unsupported RELA reloc section"),
6374 object
->name().c_str());
6378 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
6387 needs_special_offset_handling
,
6392 // Finalize the sections.
6394 template<bool big_endian
>
6396 Target_arm
<big_endian
>::do_finalize_sections(
6398 const Input_objects
* input_objects
,
6399 Symbol_table
* symtab
)
6401 // Merge processor-specific flags.
6402 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
6403 p
!= input_objects
->relobj_end();
6406 Arm_relobj
<big_endian
>* arm_relobj
=
6407 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
6408 this->merge_processor_specific_flags(
6410 arm_relobj
->processor_specific_flags());
6411 this->merge_object_attributes(arm_relobj
->name().c_str(),
6412 arm_relobj
->attributes_section_data());
6416 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
6417 p
!= input_objects
->dynobj_end();
6420 Arm_dynobj
<big_endian
>* arm_dynobj
=
6421 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
6422 this->merge_processor_specific_flags(
6424 arm_dynobj
->processor_specific_flags());
6425 this->merge_object_attributes(arm_dynobj
->name().c_str(),
6426 arm_dynobj
->attributes_section_data());
6430 const Object_attribute
* cpu_arch_attr
=
6431 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
6432 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
6433 this->set_may_use_blx(true);
6435 // Check if we need to use Cortex-A8 workaround.
6436 if (parameters
->options().user_set_fix_cortex_a8())
6437 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
6440 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
6441 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
6443 const Object_attribute
* cpu_arch_profile_attr
=
6444 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
6445 this->fix_cortex_a8_
=
6446 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
6447 && (cpu_arch_profile_attr
->int_value() == 'A'
6448 || cpu_arch_profile_attr
->int_value() == 0));
6451 // Check if we can use V4BX interworking.
6452 // The V4BX interworking stub contains BX instruction,
6453 // which is not specified for some profiles.
6454 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
6455 && !this->may_use_blx())
6456 gold_error(_("unable to provide V4BX reloc interworking fix up; "
6457 "the target profile does not support BX instruction"));
6459 // Fill in some more dynamic tags.
6460 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6462 : this->plt_
->rel_plt());
6463 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
6464 this->rel_dyn_
, true);
6466 // Emit any relocs we saved in an attempt to avoid generating COPY
6468 if (this->copy_relocs_
.any_saved_relocs())
6469 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
6471 // Handle the .ARM.exidx section.
6472 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
6473 if (exidx_section
!= NULL
6474 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
6475 && !parameters
->options().relocatable())
6477 // Create __exidx_start and __exdix_end symbols.
6478 symtab
->define_in_output_data("__exidx_start", NULL
,
6479 Symbol_table::PREDEFINED
,
6480 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
6481 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
6483 symtab
->define_in_output_data("__exidx_end", NULL
,
6484 Symbol_table::PREDEFINED
,
6485 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
6486 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
6489 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
6490 // the .ARM.exidx section.
6491 if (!layout
->script_options()->saw_phdrs_clause())
6493 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
6495 Output_segment
* exidx_segment
=
6496 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
6497 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
6502 // Create an .ARM.attributes section if there is not one already.
6503 Output_attributes_section_data
* attributes_section
=
6504 new Output_attributes_section_data(*this->attributes_section_data_
);
6505 layout
->add_output_section_data(".ARM.attributes",
6506 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
6507 attributes_section
, false, false, false,
6511 // Return whether a direct absolute static relocation needs to be applied.
6512 // In cases where Scan::local() or Scan::global() has created
6513 // a dynamic relocation other than R_ARM_RELATIVE, the addend
6514 // of the relocation is carried in the data, and we must not
6515 // apply the static relocation.
6517 template<bool big_endian
>
6519 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
6520 const Sized_symbol
<32>* gsym
,
6523 Output_section
* output_section
)
6525 // If the output section is not allocated, then we didn't call
6526 // scan_relocs, we didn't create a dynamic reloc, and we must apply
6528 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
6531 // For local symbols, we will have created a non-RELATIVE dynamic
6532 // relocation only if (a) the output is position independent,
6533 // (b) the relocation is absolute (not pc- or segment-relative), and
6534 // (c) the relocation is not 32 bits wide.
6536 return !(parameters
->options().output_is_position_independent()
6537 && (ref_flags
& Symbol::ABSOLUTE_REF
)
6540 // For global symbols, we use the same helper routines used in the
6541 // scan pass. If we did not create a dynamic relocation, or if we
6542 // created a RELATIVE dynamic relocation, we should apply the static
6544 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
6545 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
6546 && gsym
->can_use_relative_reloc(ref_flags
6547 & Symbol::FUNCTION_CALL
);
6548 return !has_dyn
|| is_rel
;
6551 // Perform a relocation.
6553 template<bool big_endian
>
6555 Target_arm
<big_endian
>::Relocate::relocate(
6556 const Relocate_info
<32, big_endian
>* relinfo
,
6558 Output_section
*output_section
,
6560 const elfcpp::Rel
<32, big_endian
>& rel
,
6561 unsigned int r_type
,
6562 const Sized_symbol
<32>* gsym
,
6563 const Symbol_value
<32>* psymval
,
6564 unsigned char* view
,
6565 Arm_address address
,
6566 section_size_type
/* view_size */ )
6568 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
6570 r_type
= get_real_reloc_type(r_type
);
6572 const Arm_relobj
<big_endian
>* object
=
6573 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6575 // If the final branch target of a relocation is THUMB instruction, this
6576 // is 1. Otherwise it is 0.
6577 Arm_address thumb_bit
= 0;
6578 Symbol_value
<32> symval
;
6579 bool is_weakly_undefined_without_plt
= false;
6580 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
6584 // This is a global symbol. Determine if we use PLT and if the
6585 // final target is THUMB.
6586 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
6588 // This uses a PLT, change the symbol value.
6589 symval
.set_output_value(target
->plt_section()->address()
6590 + gsym
->plt_offset());
6593 else if (gsym
->is_weak_undefined())
6595 // This is a weakly undefined symbol and we do not use PLT
6596 // for this relocation. A branch targeting this symbol will
6597 // be converted into an NOP.
6598 is_weakly_undefined_without_plt
= true;
6602 // Set thumb bit if symbol:
6603 // -Has type STT_ARM_TFUNC or
6604 // -Has type STT_FUNC, is defined and with LSB in value set.
6606 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6607 || (gsym
->type() == elfcpp::STT_FUNC
6608 && !gsym
->is_undefined()
6609 && ((psymval
->value(object
, 0) & 1) != 0)))
6616 // This is a local symbol. Determine if the final target is THUMB.
6617 // We saved this information when all the local symbols were read.
6618 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
6619 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6620 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
6625 // This is a fake relocation synthesized for a stub. It does not have
6626 // a real symbol. We just look at the LSB of the symbol value to
6627 // determine if the target is THUMB or not.
6628 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
6631 // Strip LSB if this points to a THUMB target.
6633 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6634 && ((psymval
->value(object
, 0) & 1) != 0))
6636 Arm_address stripped_value
=
6637 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
6638 symval
.set_output_value(stripped_value
);
6642 // Get the GOT offset if needed.
6643 // The GOT pointer points to the end of the GOT section.
6644 // We need to subtract the size of the GOT section to get
6645 // the actual offset to use in the relocation.
6646 bool have_got_offset
= false;
6647 unsigned int got_offset
= 0;
6650 case elfcpp::R_ARM_GOT_BREL
:
6651 case elfcpp::R_ARM_GOT_PREL
:
6654 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6655 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
6656 - target
->got_size());
6660 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6661 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6662 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
6663 - target
->got_size());
6665 have_got_offset
= true;
6672 // To look up relocation stubs, we need to pass the symbol table index of
6674 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
6676 typename
Arm_relocate_functions::Status reloc_status
=
6677 Arm_relocate_functions::STATUS_OKAY
;
6680 case elfcpp::R_ARM_NONE
:
6683 case elfcpp::R_ARM_ABS8
:
6684 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6686 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
6689 case elfcpp::R_ARM_ABS12
:
6690 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6692 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
6695 case elfcpp::R_ARM_ABS16
:
6696 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6698 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
6701 case elfcpp::R_ARM_ABS32
:
6702 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6704 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6708 case elfcpp::R_ARM_ABS32_NOI
:
6709 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6711 // No thumb bit for this relocation: (S + A)
6712 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
6716 case elfcpp::R_ARM_MOVW_ABS_NC
:
6717 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6719 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
6723 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6724 "a shared object; recompile with -fPIC"));
6727 case elfcpp::R_ARM_MOVT_ABS
:
6728 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6730 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
6732 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6733 "a shared object; recompile with -fPIC"));
6736 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6737 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6739 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
6743 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6744 "making a shared object; recompile with -fPIC"));
6747 case elfcpp::R_ARM_THM_MOVT_ABS
:
6748 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6750 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
6753 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6754 "making a shared object; recompile with -fPIC"));
6757 case elfcpp::R_ARM_MOVW_PREL_NC
:
6758 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
6763 case elfcpp::R_ARM_MOVT_PREL
:
6764 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
6768 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6769 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
6774 case elfcpp::R_ARM_THM_MOVT_PREL
:
6775 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
6779 case elfcpp::R_ARM_REL32
:
6780 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6781 address
, thumb_bit
);
6784 case elfcpp::R_ARM_THM_ABS5
:
6785 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
6787 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
6790 case elfcpp::R_ARM_THM_CALL
:
6792 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
6793 psymval
, address
, thumb_bit
,
6794 is_weakly_undefined_without_plt
);
6797 case elfcpp::R_ARM_XPC25
:
6799 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
6800 psymval
, address
, thumb_bit
,
6801 is_weakly_undefined_without_plt
);
6804 case elfcpp::R_ARM_THM_XPC22
:
6806 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
6807 psymval
, address
, thumb_bit
,
6808 is_weakly_undefined_without_plt
);
6811 case elfcpp::R_ARM_GOTOFF32
:
6813 Arm_address got_origin
;
6814 got_origin
= target
->got_plt_section()->address();
6815 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
6816 got_origin
, thumb_bit
);
6820 case elfcpp::R_ARM_BASE_PREL
:
6823 // Get the addressing origin of the output segment defining the
6824 // symbol gsym (AAELF 4.6.1.2 Relocation types)
6825 gold_assert(gsym
!= NULL
);
6826 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6827 origin
= gsym
->output_segment()->vaddr();
6828 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6829 origin
= gsym
->output_data()->address();
6832 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6833 _("cannot find origin of R_ARM_BASE_PREL"));
6836 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
6840 case elfcpp::R_ARM_BASE_ABS
:
6842 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
6847 // Get the addressing origin of the output segment defining
6848 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
6850 // R_ARM_BASE_ABS with the NULL symbol will give the
6851 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
6852 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6853 origin
= target
->got_plt_section()->address();
6854 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
6855 origin
= gsym
->output_segment()->vaddr();
6856 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
6857 origin
= gsym
->output_data()->address();
6860 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6861 _("cannot find origin of R_ARM_BASE_ABS"));
6865 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
6869 case elfcpp::R_ARM_GOT_BREL
:
6870 gold_assert(have_got_offset
);
6871 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
6874 case elfcpp::R_ARM_GOT_PREL
:
6875 gold_assert(have_got_offset
);
6876 // Get the address origin for GOT PLT, which is allocated right
6877 // after the GOT section, to calculate an absolute address of
6878 // the symbol GOT entry (got_origin + got_offset).
6879 Arm_address got_origin
;
6880 got_origin
= target
->got_plt_section()->address();
6881 reloc_status
= Arm_relocate_functions::got_prel(view
,
6882 got_origin
+ got_offset
,
6886 case elfcpp::R_ARM_PLT32
:
6887 gold_assert(gsym
== NULL
6888 || gsym
->has_plt_offset()
6889 || gsym
->final_value_is_known()
6890 || (gsym
->is_defined()
6891 && !gsym
->is_from_dynobj()
6892 && !gsym
->is_preemptible()));
6894 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
6895 psymval
, address
, thumb_bit
,
6896 is_weakly_undefined_without_plt
);
6899 case elfcpp::R_ARM_CALL
:
6901 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
6902 psymval
, address
, thumb_bit
,
6903 is_weakly_undefined_without_plt
);
6906 case elfcpp::R_ARM_JUMP24
:
6908 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
6909 psymval
, address
, thumb_bit
,
6910 is_weakly_undefined_without_plt
);
6913 case elfcpp::R_ARM_THM_JUMP24
:
6915 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
6916 psymval
, address
, thumb_bit
,
6917 is_weakly_undefined_without_plt
);
6920 case elfcpp::R_ARM_THM_JUMP19
:
6922 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
6926 case elfcpp::R_ARM_THM_JUMP6
:
6928 Arm_relocate_functions::thm_jump6(view
, object
, psymval
, address
);
6931 case elfcpp::R_ARM_THM_JUMP8
:
6933 Arm_relocate_functions::thm_jump8(view
, object
, psymval
, address
);
6936 case elfcpp::R_ARM_THM_JUMP11
:
6938 Arm_relocate_functions::thm_jump11(view
, object
, psymval
, address
);
6941 case elfcpp::R_ARM_PREL31
:
6942 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
6943 address
, thumb_bit
);
6946 case elfcpp::R_ARM_V4BX
:
6947 if (target
->fix_v4bx() > General_options::FIX_V4BX_NONE
)
6949 const bool is_v4bx_interworking
=
6950 (target
->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
);
6952 Arm_relocate_functions::v4bx(relinfo
, view
, object
, address
,
6953 is_v4bx_interworking
);
6957 case elfcpp::R_ARM_TARGET1
:
6958 // This should have been mapped to another type already.
6960 case elfcpp::R_ARM_COPY
:
6961 case elfcpp::R_ARM_GLOB_DAT
:
6962 case elfcpp::R_ARM_JUMP_SLOT
:
6963 case elfcpp::R_ARM_RELATIVE
:
6964 // These are relocations which should only be seen by the
6965 // dynamic linker, and should never be seen here.
6966 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6967 _("unexpected reloc %u in object file"),
6972 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6973 _("unsupported reloc %u"),
6978 // Report any errors.
6979 switch (reloc_status
)
6981 case Arm_relocate_functions::STATUS_OKAY
:
6983 case Arm_relocate_functions::STATUS_OVERFLOW
:
6984 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
6985 _("relocation overflow in relocation %u"),
6988 case Arm_relocate_functions::STATUS_BAD_RELOC
:
6989 gold_error_at_location(
6993 _("unexpected opcode while processing relocation %u"),
7003 // Relocate section data.
7005 template<bool big_endian
>
7007 Target_arm
<big_endian
>::relocate_section(
7008 const Relocate_info
<32, big_endian
>* relinfo
,
7009 unsigned int sh_type
,
7010 const unsigned char* prelocs
,
7012 Output_section
* output_section
,
7013 bool needs_special_offset_handling
,
7014 unsigned char* view
,
7015 Arm_address address
,
7016 section_size_type view_size
,
7017 const Reloc_symbol_changes
* reloc_symbol_changes
)
7019 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
7020 gold_assert(sh_type
== elfcpp::SHT_REL
);
7022 Arm_input_section
<big_endian
>* arm_input_section
=
7023 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
7025 // This is an ARM input section and the view covers the whole output
7027 if (arm_input_section
!= NULL
)
7029 gold_assert(needs_special_offset_handling
);
7030 Arm_address section_address
= arm_input_section
->address();
7031 section_size_type section_size
= arm_input_section
->data_size();
7033 gold_assert((arm_input_section
->address() >= address
)
7034 && ((arm_input_section
->address()
7035 + arm_input_section
->data_size())
7036 <= (address
+ view_size
)));
7038 off_t offset
= section_address
- address
;
7041 view_size
= section_size
;
7044 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
7051 needs_special_offset_handling
,
7055 reloc_symbol_changes
);
7058 // Return the size of a relocation while scanning during a relocatable
7061 template<bool big_endian
>
7063 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
7064 unsigned int r_type
,
7067 r_type
= get_real_reloc_type(r_type
);
7070 case elfcpp::R_ARM_NONE
:
7073 case elfcpp::R_ARM_ABS8
:
7076 case elfcpp::R_ARM_ABS16
:
7077 case elfcpp::R_ARM_THM_ABS5
:
7078 case elfcpp::R_ARM_THM_JUMP6
:
7079 case elfcpp::R_ARM_THM_JUMP8
:
7080 case elfcpp::R_ARM_THM_JUMP11
:
7083 case elfcpp::R_ARM_ABS32
:
7084 case elfcpp::R_ARM_ABS32_NOI
:
7085 case elfcpp::R_ARM_ABS12
:
7086 case elfcpp::R_ARM_BASE_ABS
:
7087 case elfcpp::R_ARM_REL32
:
7088 case elfcpp::R_ARM_THM_CALL
:
7089 case elfcpp::R_ARM_GOTOFF32
:
7090 case elfcpp::R_ARM_BASE_PREL
:
7091 case elfcpp::R_ARM_GOT_BREL
:
7092 case elfcpp::R_ARM_GOT_PREL
:
7093 case elfcpp::R_ARM_PLT32
:
7094 case elfcpp::R_ARM_CALL
:
7095 case elfcpp::R_ARM_JUMP24
:
7096 case elfcpp::R_ARM_PREL31
:
7097 case elfcpp::R_ARM_MOVW_ABS_NC
:
7098 case elfcpp::R_ARM_MOVT_ABS
:
7099 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7100 case elfcpp::R_ARM_THM_MOVT_ABS
:
7101 case elfcpp::R_ARM_MOVW_PREL_NC
:
7102 case elfcpp::R_ARM_MOVT_PREL
:
7103 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7104 case elfcpp::R_ARM_THM_MOVT_PREL
:
7105 case elfcpp::R_ARM_V4BX
:
7108 case elfcpp::R_ARM_TARGET1
:
7109 // This should have been mapped to another type already.
7111 case elfcpp::R_ARM_COPY
:
7112 case elfcpp::R_ARM_GLOB_DAT
:
7113 case elfcpp::R_ARM_JUMP_SLOT
:
7114 case elfcpp::R_ARM_RELATIVE
:
7115 // These are relocations which should only be seen by the
7116 // dynamic linker, and should never be seen here.
7117 gold_error(_("%s: unexpected reloc %u in object file"),
7118 object
->name().c_str(), r_type
);
7122 object
->error(_("unsupported reloc %u in object file"), r_type
);
7127 // Scan the relocs during a relocatable link.
7129 template<bool big_endian
>
7131 Target_arm
<big_endian
>::scan_relocatable_relocs(
7132 Symbol_table
* symtab
,
7134 Sized_relobj
<32, big_endian
>* object
,
7135 unsigned int data_shndx
,
7136 unsigned int sh_type
,
7137 const unsigned char* prelocs
,
7139 Output_section
* output_section
,
7140 bool needs_special_offset_handling
,
7141 size_t local_symbol_count
,
7142 const unsigned char* plocal_symbols
,
7143 Relocatable_relocs
* rr
)
7145 gold_assert(sh_type
== elfcpp::SHT_REL
);
7147 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
7148 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
7150 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
7151 Scan_relocatable_relocs
>(
7159 needs_special_offset_handling
,
7165 // Relocate a section during a relocatable link.
7167 template<bool big_endian
>
7169 Target_arm
<big_endian
>::relocate_for_relocatable(
7170 const Relocate_info
<32, big_endian
>* relinfo
,
7171 unsigned int sh_type
,
7172 const unsigned char* prelocs
,
7174 Output_section
* output_section
,
7175 off_t offset_in_output_section
,
7176 const Relocatable_relocs
* rr
,
7177 unsigned char* view
,
7178 Arm_address view_address
,
7179 section_size_type view_size
,
7180 unsigned char* reloc_view
,
7181 section_size_type reloc_view_size
)
7183 gold_assert(sh_type
== elfcpp::SHT_REL
);
7185 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
7190 offset_in_output_section
,
7199 // Return the value to use for a dynamic symbol which requires special
7200 // treatment. This is how we support equality comparisons of function
7201 // pointers across shared library boundaries, as described in the
7202 // processor specific ABI supplement.
7204 template<bool big_endian
>
7206 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
7208 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
7209 return this->plt_section()->address() + gsym
->plt_offset();
7212 // Map platform-specific relocs to real relocs
7214 template<bool big_endian
>
7216 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
7220 case elfcpp::R_ARM_TARGET1
:
7221 // This is either R_ARM_ABS32 or R_ARM_REL32;
7222 return elfcpp::R_ARM_ABS32
;
7224 case elfcpp::R_ARM_TARGET2
:
7225 // This can be any reloc type but ususally is R_ARM_GOT_PREL
7226 return elfcpp::R_ARM_GOT_PREL
;
7233 // Whether if two EABI versions V1 and V2 are compatible.
7235 template<bool big_endian
>
7237 Target_arm
<big_endian
>::are_eabi_versions_compatible(
7238 elfcpp::Elf_Word v1
,
7239 elfcpp::Elf_Word v2
)
7241 // v4 and v5 are the same spec before and after it was released,
7242 // so allow mixing them.
7243 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
7244 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
7250 // Combine FLAGS from an input object called NAME and the processor-specific
7251 // flags in the ELF header of the output. Much of this is adapted from the
7252 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
7253 // in bfd/elf32-arm.c.
7255 template<bool big_endian
>
7257 Target_arm
<big_endian
>::merge_processor_specific_flags(
7258 const std::string
& name
,
7259 elfcpp::Elf_Word flags
)
7261 if (this->are_processor_specific_flags_set())
7263 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
7265 // Nothing to merge if flags equal to those in output.
7266 if (flags
== out_flags
)
7269 // Complain about various flag mismatches.
7270 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
7271 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
7272 if (!this->are_eabi_versions_compatible(version1
, version2
))
7273 gold_error(_("Source object %s has EABI version %d but output has "
7274 "EABI version %d."),
7276 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
7277 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
7281 // If the input is the default architecture and had the default
7282 // flags then do not bother setting the flags for the output
7283 // architecture, instead allow future merges to do this. If no
7284 // future merges ever set these flags then they will retain their
7285 // uninitialised values, which surprise surprise, correspond
7286 // to the default values.
7290 // This is the first time, just copy the flags.
7291 // We only copy the EABI version for now.
7292 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
7296 // Adjust ELF file header.
7297 template<bool big_endian
>
7299 Target_arm
<big_endian
>::do_adjust_elf_header(
7300 unsigned char* view
,
7303 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
7305 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
7306 unsigned char e_ident
[elfcpp::EI_NIDENT
];
7307 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
7309 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
7310 == elfcpp::EF_ARM_EABI_UNKNOWN
)
7311 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
7313 e_ident
[elfcpp::EI_OSABI
] = 0;
7314 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
7316 // FIXME: Do EF_ARM_BE8 adjustment.
7318 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
7319 oehdr
.put_e_ident(e_ident
);
7322 // do_make_elf_object to override the same function in the base class.
7323 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
7324 // to store ARM specific information. Hence we need to have our own
7325 // ELF object creation.
7327 template<bool big_endian
>
7329 Target_arm
<big_endian
>::do_make_elf_object(
7330 const std::string
& name
,
7331 Input_file
* input_file
,
7332 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
7334 int et
= ehdr
.get_e_type();
7335 if (et
== elfcpp::ET_REL
)
7337 Arm_relobj
<big_endian
>* obj
=
7338 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
7342 else if (et
== elfcpp::ET_DYN
)
7344 Sized_dynobj
<32, big_endian
>* obj
=
7345 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
7351 gold_error(_("%s: unsupported ELF file type %d"),
7357 // Read the architecture from the Tag_also_compatible_with attribute, if any.
7358 // Returns -1 if no architecture could be read.
7359 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
7361 template<bool big_endian
>
7363 Target_arm
<big_endian
>::get_secondary_compatible_arch(
7364 const Attributes_section_data
* pasd
)
7366 const Object_attribute
*known_attributes
=
7367 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
7369 // Note: the tag and its argument below are uleb128 values, though
7370 // currently-defined values fit in one byte for each.
7371 const std::string
& sv
=
7372 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
7374 && sv
.data()[0] == elfcpp::Tag_CPU_arch
7375 && (sv
.data()[1] & 128) != 128)
7376 return sv
.data()[1];
7378 // This tag is "safely ignorable", so don't complain if it looks funny.
7382 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
7383 // The tag is removed if ARCH is -1.
7384 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
7386 template<bool big_endian
>
7388 Target_arm
<big_endian
>::set_secondary_compatible_arch(
7389 Attributes_section_data
* pasd
,
7392 Object_attribute
*known_attributes
=
7393 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
7397 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
7401 // Note: the tag and its argument below are uleb128 values, though
7402 // currently-defined values fit in one byte for each.
7404 sv
[0] = elfcpp::Tag_CPU_arch
;
7405 gold_assert(arch
!= 0);
7409 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
7412 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
7414 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
7416 template<bool big_endian
>
7418 Target_arm
<big_endian
>::tag_cpu_arch_combine(
7421 int* secondary_compat_out
,
7423 int secondary_compat
)
7425 #define T(X) elfcpp::TAG_CPU_ARCH_##X
7426 static const int v6t2
[] =
7438 static const int v6k
[] =
7451 static const int v7
[] =
7465 static const int v6_m
[] =
7480 static const int v6s_m
[] =
7496 static const int v7e_m
[] =
7513 static const int v4t_plus_v6_m
[] =
7529 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
7531 static const int *comb
[] =
7539 // Pseudo-architecture.
7543 // Check we've not got a higher architecture than we know about.
7545 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
7547 gold_error(_("%s: unknown CPU architecture"), name
);
7551 // Override old tag if we have a Tag_also_compatible_with on the output.
7553 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
7554 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
7555 oldtag
= T(V4T_PLUS_V6_M
);
7557 // And override the new tag if we have a Tag_also_compatible_with on the
7560 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
7561 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
7562 newtag
= T(V4T_PLUS_V6_M
);
7564 // Architectures before V6KZ add features monotonically.
7565 int tagh
= std::max(oldtag
, newtag
);
7566 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
7569 int tagl
= std::min(oldtag
, newtag
);
7570 int result
= comb
[tagh
- T(V6T2
)][tagl
];
7572 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
7573 // as the canonical version.
7574 if (result
== T(V4T_PLUS_V6_M
))
7577 *secondary_compat_out
= T(V6_M
);
7580 *secondary_compat_out
= -1;
7584 gold_error(_("%s: conflicting CPU architectures %d/%d"),
7585 name
, oldtag
, newtag
);
7593 // Helper to print AEABI enum tag value.
7595 template<bool big_endian
>
7597 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
7599 static const char *aeabi_enum_names
[] =
7600 { "", "variable-size", "32-bit", "" };
7601 const size_t aeabi_enum_names_size
=
7602 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
7604 if (value
< aeabi_enum_names_size
)
7605 return std::string(aeabi_enum_names
[value
]);
7609 sprintf(buffer
, "<unknown value %u>", value
);
7610 return std::string(buffer
);
7614 // Return the string value to store in TAG_CPU_name.
7616 template<bool big_endian
>
7618 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
7620 static const char *name_table
[] = {
7621 // These aren't real CPU names, but we can't guess
7622 // that from the architecture version alone.
7638 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
7640 if (value
< name_table_size
)
7641 return std::string(name_table
[value
]);
7645 sprintf(buffer
, "<unknown CPU value %u>", value
);
7646 return std::string(buffer
);
7650 // Merge object attributes from input file called NAME with those of the
7651 // output. The input object attributes are in the object pointed by PASD.
7653 template<bool big_endian
>
7655 Target_arm
<big_endian
>::merge_object_attributes(
7657 const Attributes_section_data
* pasd
)
7659 // Return if there is no attributes section data.
7663 // If output has no object attributes, just copy.
7664 if (this->attributes_section_data_
== NULL
)
7666 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
7670 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
7671 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
7672 Object_attribute
* out_attr
=
7673 this->attributes_section_data_
->known_attributes(vendor
);
7675 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7676 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
7677 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
7679 // Ignore mismatches if the object doesn't use floating point. */
7680 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
7681 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
7682 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
7683 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
7684 gold_error(_("%s uses VFP register arguments, output does not"),
7688 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
7690 // Merge this attribute with existing attributes.
7693 case elfcpp::Tag_CPU_raw_name
:
7694 case elfcpp::Tag_CPU_name
:
7695 // These are merged after Tag_CPU_arch.
7698 case elfcpp::Tag_ABI_optimization_goals
:
7699 case elfcpp::Tag_ABI_FP_optimization_goals
:
7700 // Use the first value seen.
7703 case elfcpp::Tag_CPU_arch
:
7705 unsigned int saved_out_attr
= out_attr
->int_value();
7706 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7707 int secondary_compat
=
7708 this->get_secondary_compatible_arch(pasd
);
7709 int secondary_compat_out
=
7710 this->get_secondary_compatible_arch(
7711 this->attributes_section_data_
);
7712 out_attr
[i
].set_int_value(
7713 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
7714 &secondary_compat_out
,
7715 in_attr
[i
].int_value(),
7717 this->set_secondary_compatible_arch(this->attributes_section_data_
,
7718 secondary_compat_out
);
7720 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7721 if (out_attr
[i
].int_value() == saved_out_attr
)
7722 ; // Leave the names alone.
7723 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
7725 // The output architecture has been changed to match the
7726 // input architecture. Use the input names.
7727 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
7728 in_attr
[elfcpp::Tag_CPU_name
].string_value());
7729 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
7730 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
7734 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
7735 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
7738 // If we still don't have a value for Tag_CPU_name,
7739 // make one up now. Tag_CPU_raw_name remains blank.
7740 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
7742 const std::string cpu_name
=
7743 this->tag_cpu_name_value(out_attr
[i
].int_value());
7744 // FIXME: If we see an unknown CPU, this will be set
7745 // to "<unknown CPU n>", where n is the attribute value.
7746 // This is different from BFD, which leaves the name alone.
7747 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
7752 case elfcpp::Tag_ARM_ISA_use
:
7753 case elfcpp::Tag_THUMB_ISA_use
:
7754 case elfcpp::Tag_WMMX_arch
:
7755 case elfcpp::Tag_Advanced_SIMD_arch
:
7756 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7757 case elfcpp::Tag_ABI_FP_rounding
:
7758 case elfcpp::Tag_ABI_FP_exceptions
:
7759 case elfcpp::Tag_ABI_FP_user_exceptions
:
7760 case elfcpp::Tag_ABI_FP_number_model
:
7761 case elfcpp::Tag_VFP_HP_extension
:
7762 case elfcpp::Tag_CPU_unaligned_access
:
7763 case elfcpp::Tag_T2EE_use
:
7764 case elfcpp::Tag_Virtualization_use
:
7765 case elfcpp::Tag_MPextension_use
:
7766 // Use the largest value specified.
7767 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7768 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7771 case elfcpp::Tag_ABI_align8_preserved
:
7772 case elfcpp::Tag_ABI_PCS_RO_data
:
7773 // Use the smallest value specified.
7774 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7775 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7778 case elfcpp::Tag_ABI_align8_needed
:
7779 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
7780 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
7781 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
7784 // This error message should be enabled once all non-conformant
7785 // binaries in the toolchain have had the attributes set
7787 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7791 case elfcpp::Tag_ABI_FP_denormal
:
7792 case elfcpp::Tag_ABI_PCS_GOT_use
:
7794 // These tags have 0 = don't care, 1 = strong requirement,
7795 // 2 = weak requirement.
7796 static const int order_021
[3] = {0, 2, 1};
7798 // Use the "greatest" from the sequence 0, 2, 1, or the largest
7799 // value if greater than 2 (for future-proofing).
7800 if ((in_attr
[i
].int_value() > 2
7801 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7802 || (in_attr
[i
].int_value() <= 2
7803 && out_attr
[i
].int_value() <= 2
7804 && (order_021
[in_attr
[i
].int_value()]
7805 > order_021
[out_attr
[i
].int_value()])))
7806 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7810 case elfcpp::Tag_CPU_arch_profile
:
7811 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
7813 // 0 will merge with anything.
7814 // 'A' and 'S' merge to 'A'.
7815 // 'R' and 'S' merge to 'R'.
7816 // 'M' and 'A|R|S' is an error.
7817 if (out_attr
[i
].int_value() == 0
7818 || (out_attr
[i
].int_value() == 'S'
7819 && (in_attr
[i
].int_value() == 'A'
7820 || in_attr
[i
].int_value() == 'R')))
7821 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7822 else if (in_attr
[i
].int_value() == 0
7823 || (in_attr
[i
].int_value() == 'S'
7824 && (out_attr
[i
].int_value() == 'A'
7825 || out_attr
[i
].int_value() == 'R')))
7830 (_("conflicting architecture profiles %c/%c"),
7831 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
7832 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
7836 case elfcpp::Tag_VFP_arch
:
7853 // Values greater than 6 aren't defined, so just pick the
7855 if (in_attr
[i
].int_value() > 6
7856 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
7858 *out_attr
= *in_attr
;
7861 // The output uses the superset of input features
7862 // (ISA version) and registers.
7863 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
7864 vfp_versions
[out_attr
[i
].int_value()].ver
);
7865 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
7866 vfp_versions
[out_attr
[i
].int_value()].regs
);
7867 // This assumes all possible supersets are also a valid
7870 for (newval
= 6; newval
> 0; newval
--)
7872 if (regs
== vfp_versions
[newval
].regs
7873 && ver
== vfp_versions
[newval
].ver
)
7876 out_attr
[i
].set_int_value(newval
);
7879 case elfcpp::Tag_PCS_config
:
7880 if (out_attr
[i
].int_value() == 0)
7881 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7882 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7884 // It's sometimes ok to mix different configs, so this is only
7886 gold_warning(_("%s: conflicting platform configuration"), name
);
7889 case elfcpp::Tag_ABI_PCS_R9_use
:
7890 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
7891 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
7892 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
7894 gold_error(_("%s: conflicting use of R9"), name
);
7896 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
7897 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7899 case elfcpp::Tag_ABI_PCS_RW_data
:
7900 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7901 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7902 != elfcpp::AEABI_R9_SB
)
7903 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
7904 != elfcpp::AEABI_R9_unused
))
7906 gold_error(_("%s: SB relative addressing conflicts with use "
7910 // Use the smallest value specified.
7911 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
7912 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7914 case elfcpp::Tag_ABI_PCS_wchar_t
:
7915 // FIXME: Make it possible to turn off this warning.
7916 if (out_attr
[i
].int_value()
7917 && in_attr
[i
].int_value()
7918 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7920 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7921 "use %u-byte wchar_t; use of wchar_t values "
7922 "across objects may fail"),
7923 name
, in_attr
[i
].int_value(),
7924 out_attr
[i
].int_value());
7926 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
7927 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7929 case elfcpp::Tag_ABI_enum_size
:
7930 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
7932 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
7933 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
7935 // The existing object is compatible with anything.
7936 // Use whatever requirements the new object has.
7937 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7939 // FIXME: Make it possible to turn off this warning.
7940 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
7941 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
7943 unsigned int in_value
= in_attr
[i
].int_value();
7944 unsigned int out_value
= out_attr
[i
].int_value();
7945 gold_warning(_("%s uses %s enums yet the output is to use "
7946 "%s enums; use of enum values across objects "
7949 this->aeabi_enum_name(in_value
).c_str(),
7950 this->aeabi_enum_name(out_value
).c_str());
7954 case elfcpp::Tag_ABI_VFP_args
:
7957 case elfcpp::Tag_ABI_WMMX_args
:
7958 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7960 gold_error(_("%s uses iWMMXt register arguments, output does "
7965 case Object_attribute::Tag_compatibility
:
7966 // Merged in target-independent code.
7968 case elfcpp::Tag_ABI_HardFP_use
:
7969 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7970 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
7971 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
7972 out_attr
[i
].set_int_value(3);
7973 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
7974 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7976 case elfcpp::Tag_ABI_FP_16bit_format
:
7977 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
7979 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
7980 gold_error(_("fp16 format mismatch between %s and output"),
7983 if (in_attr
[i
].int_value() != 0)
7984 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
7987 case elfcpp::Tag_nodefaults
:
7988 // This tag is set if it exists, but the value is unused (and is
7989 // typically zero). We don't actually need to do anything here -
7990 // the merge happens automatically when the type flags are merged
7993 case elfcpp::Tag_also_compatible_with
:
7994 // Already done in Tag_CPU_arch.
7996 case elfcpp::Tag_conformance
:
7997 // Keep the attribute if it matches. Throw it away otherwise.
7998 // No attribute means no claim to conform.
7999 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
8000 out_attr
[i
].set_string_value("");
8005 const char* err_object
= NULL
;
8007 // The "known_obj_attributes" table does contain some undefined
8008 // attributes. Ensure that there are unused.
8009 if (out_attr
[i
].int_value() != 0
8010 || out_attr
[i
].string_value() != "")
8011 err_object
= "output";
8012 else if (in_attr
[i
].int_value() != 0
8013 || in_attr
[i
].string_value() != "")
8016 if (err_object
!= NULL
)
8018 // Attribute numbers >=64 (mod 128) can be safely ignored.
8020 gold_error(_("%s: unknown mandatory EABI object attribute "
8024 gold_warning(_("%s: unknown EABI object attribute %d"),
8028 // Only pass on attributes that match in both inputs.
8029 if (!in_attr
[i
].matches(out_attr
[i
]))
8031 out_attr
[i
].set_int_value(0);
8032 out_attr
[i
].set_string_value("");
8037 // If out_attr was copied from in_attr then it won't have a type yet.
8038 if (in_attr
[i
].type() && !out_attr
[i
].type())
8039 out_attr
[i
].set_type(in_attr
[i
].type());
8042 // Merge Tag_compatibility attributes and any common GNU ones.
8043 this->attributes_section_data_
->merge(name
, pasd
);
8045 // Check for any attributes not known on ARM.
8046 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
8047 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
8048 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
8049 Other_attributes
* out_other_attributes
=
8050 this->attributes_section_data_
->other_attributes(vendor
);
8051 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
8053 while (in_iter
!= in_other_attributes
->end()
8054 || out_iter
!= out_other_attributes
->end())
8056 const char* err_object
= NULL
;
8059 // The tags for each list are in numerical order.
8060 // If the tags are equal, then merge.
8061 if (out_iter
!= out_other_attributes
->end()
8062 && (in_iter
== in_other_attributes
->end()
8063 || in_iter
->first
> out_iter
->first
))
8065 // This attribute only exists in output. We can't merge, and we
8066 // don't know what the tag means, so delete it.
8067 err_object
= "output";
8068 err_tag
= out_iter
->first
;
8069 int saved_tag
= out_iter
->first
;
8070 delete out_iter
->second
;
8071 out_other_attributes
->erase(out_iter
);
8072 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
8074 else if (in_iter
!= in_other_attributes
->end()
8075 && (out_iter
!= out_other_attributes
->end()
8076 || in_iter
->first
< out_iter
->first
))
8078 // This attribute only exists in input. We can't merge, and we
8079 // don't know what the tag means, so ignore it.
8081 err_tag
= in_iter
->first
;
8084 else // The tags are equal.
8086 // As present, all attributes in the list are unknown, and
8087 // therefore can't be merged meaningfully.
8088 err_object
= "output";
8089 err_tag
= out_iter
->first
;
8091 // Only pass on attributes that match in both inputs.
8092 if (!in_iter
->second
->matches(*(out_iter
->second
)))
8094 // No match. Delete the attribute.
8095 int saved_tag
= out_iter
->first
;
8096 delete out_iter
->second
;
8097 out_other_attributes
->erase(out_iter
);
8098 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
8102 // Matched. Keep the attribute and move to the next.
8110 // Attribute numbers >=64 (mod 128) can be safely ignored. */
8111 if ((err_tag
& 127) < 64)
8113 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
8114 err_object
, err_tag
);
8118 gold_warning(_("%s: unknown EABI object attribute %d"),
8119 err_object
, err_tag
);
8125 // Return whether a relocation type used the LSB to distinguish THUMB
8127 template<bool big_endian
>
8129 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
8133 case elfcpp::R_ARM_PC24
:
8134 case elfcpp::R_ARM_ABS32
:
8135 case elfcpp::R_ARM_REL32
:
8136 case elfcpp::R_ARM_SBREL32
:
8137 case elfcpp::R_ARM_THM_CALL
:
8138 case elfcpp::R_ARM_GLOB_DAT
:
8139 case elfcpp::R_ARM_JUMP_SLOT
:
8140 case elfcpp::R_ARM_GOTOFF32
:
8141 case elfcpp::R_ARM_PLT32
:
8142 case elfcpp::R_ARM_CALL
:
8143 case elfcpp::R_ARM_JUMP24
:
8144 case elfcpp::R_ARM_THM_JUMP24
:
8145 case elfcpp::R_ARM_SBREL31
:
8146 case elfcpp::R_ARM_PREL31
:
8147 case elfcpp::R_ARM_MOVW_ABS_NC
:
8148 case elfcpp::R_ARM_MOVW_PREL_NC
:
8149 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
8150 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
8151 case elfcpp::R_ARM_THM_JUMP19
:
8152 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
8153 case elfcpp::R_ARM_ALU_PC_G0_NC
:
8154 case elfcpp::R_ARM_ALU_PC_G0
:
8155 case elfcpp::R_ARM_ALU_PC_G1_NC
:
8156 case elfcpp::R_ARM_ALU_PC_G1
:
8157 case elfcpp::R_ARM_ALU_PC_G2
:
8158 case elfcpp::R_ARM_ALU_SB_G0_NC
:
8159 case elfcpp::R_ARM_ALU_SB_G0
:
8160 case elfcpp::R_ARM_ALU_SB_G1_NC
:
8161 case elfcpp::R_ARM_ALU_SB_G1
:
8162 case elfcpp::R_ARM_ALU_SB_G2
:
8163 case elfcpp::R_ARM_MOVW_BREL_NC
:
8164 case elfcpp::R_ARM_MOVW_BREL
:
8165 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
8166 case elfcpp::R_ARM_THM_MOVW_BREL
:
8173 // Stub-generation methods for Target_arm.
8175 // Make a new Arm_input_section object.
8177 template<bool big_endian
>
8178 Arm_input_section
<big_endian
>*
8179 Target_arm
<big_endian
>::new_arm_input_section(
8183 Section_id
sid(relobj
, shndx
);
8185 Arm_input_section
<big_endian
>* arm_input_section
=
8186 new Arm_input_section
<big_endian
>(relobj
, shndx
);
8187 arm_input_section
->init();
8189 // Register new Arm_input_section in map for look-up.
8190 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
8191 this->arm_input_section_map_
.insert(std::make_pair(sid
, arm_input_section
));
8193 // Make sure that it we have not created another Arm_input_section
8194 // for this input section already.
8195 gold_assert(ins
.second
);
8197 return arm_input_section
;
8200 // Find the Arm_input_section object corresponding to the SHNDX-th input
8201 // section of RELOBJ.
8203 template<bool big_endian
>
8204 Arm_input_section
<big_endian
>*
8205 Target_arm
<big_endian
>::find_arm_input_section(
8207 unsigned int shndx
) const
8209 Section_id
sid(relobj
, shndx
);
8210 typename
Arm_input_section_map::const_iterator p
=
8211 this->arm_input_section_map_
.find(sid
);
8212 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
8215 // Make a new stub table.
8217 template<bool big_endian
>
8218 Stub_table
<big_endian
>*
8219 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
8221 Stub_table
<big_endian
>* stub_table
=
8222 new Stub_table
<big_endian
>(owner
);
8223 this->stub_tables_
.push_back(stub_table
);
8225 stub_table
->set_address(owner
->address() + owner
->data_size());
8226 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
8227 stub_table
->finalize_data_size();
8232 // Scan a relocation for stub generation.
8234 template<bool big_endian
>
8236 Target_arm
<big_endian
>::scan_reloc_for_stub(
8237 const Relocate_info
<32, big_endian
>* relinfo
,
8238 unsigned int r_type
,
8239 const Sized_symbol
<32>* gsym
,
8241 const Symbol_value
<32>* psymval
,
8242 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
8243 Arm_address address
)
8245 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
8247 const Arm_relobj
<big_endian
>* arm_relobj
=
8248 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8250 if (r_type
== elfcpp::R_ARM_V4BX
)
8252 const uint32_t reg
= (addend
& 0xf);
8253 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8256 // Try looking up an existing stub from a stub table.
8257 Stub_table
<big_endian
>* stub_table
=
8258 arm_relobj
->stub_table(relinfo
->data_shndx
);
8259 gold_assert(stub_table
!= NULL
);
8261 if (stub_table
->find_arm_v4bx_stub(reg
) == NULL
)
8263 // create a new stub and add it to stub table.
8264 Arm_v4bx_stub
* stub
=
8265 this->stub_factory().make_arm_v4bx_stub(reg
);
8266 gold_assert(stub
!= NULL
);
8267 stub_table
->add_arm_v4bx_stub(stub
);
8274 bool target_is_thumb
;
8275 Symbol_value
<32> symval
;
8278 // This is a global symbol. Determine if we use PLT and if the
8279 // final target is THUMB.
8280 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
8282 // This uses a PLT, change the symbol value.
8283 symval
.set_output_value(this->plt_section()->address()
8284 + gsym
->plt_offset());
8286 target_is_thumb
= false;
8288 else if (gsym
->is_undefined())
8289 // There is no need to generate a stub symbol is undefined.
8294 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
8295 || (gsym
->type() == elfcpp::STT_FUNC
8296 && !gsym
->is_undefined()
8297 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
8302 // This is a local symbol. Determine if the final target is THUMB.
8303 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
8306 // Strip LSB if this points to a THUMB target.
8308 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
8309 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
8311 Arm_address stripped_value
=
8312 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
8313 symval
.set_output_value(stripped_value
);
8317 // Get the symbol value.
8318 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
8320 // Owing to pipelining, the PC relative branches below actually skip
8321 // two instructions when the branch offset is 0.
8322 Arm_address destination
;
8325 case elfcpp::R_ARM_CALL
:
8326 case elfcpp::R_ARM_JUMP24
:
8327 case elfcpp::R_ARM_PLT32
:
8329 destination
= value
+ addend
+ 8;
8331 case elfcpp::R_ARM_THM_CALL
:
8332 case elfcpp::R_ARM_THM_XPC22
:
8333 case elfcpp::R_ARM_THM_JUMP24
:
8334 case elfcpp::R_ARM_THM_JUMP19
:
8336 destination
= value
+ addend
+ 4;
8342 Reloc_stub
* stub
= NULL
;
8343 Stub_type stub_type
=
8344 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
8346 if (stub_type
!= arm_stub_none
)
8348 // Try looking up an existing stub from a stub table.
8349 Stub_table
<big_endian
>* stub_table
=
8350 arm_relobj
->stub_table(relinfo
->data_shndx
);
8351 gold_assert(stub_table
!= NULL
);
8353 // Locate stub by destination.
8354 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
8356 // Create a stub if there is not one already
8357 stub
= stub_table
->find_reloc_stub(stub_key
);
8360 // create a new stub and add it to stub table.
8361 stub
= this->stub_factory().make_reloc_stub(stub_type
);
8362 stub_table
->add_reloc_stub(stub
, stub_key
);
8365 // Record the destination address.
8366 stub
->set_destination_address(destination
8367 | (target_is_thumb
? 1 : 0));
8370 // For Cortex-A8, we need to record a relocation at 4K page boundary.
8371 if (this->fix_cortex_a8_
8372 && (r_type
== elfcpp::R_ARM_THM_JUMP24
8373 || r_type
== elfcpp::R_ARM_THM_JUMP19
8374 || r_type
== elfcpp::R_ARM_THM_CALL
8375 || r_type
== elfcpp::R_ARM_THM_XPC22
)
8376 && (address
& 0xfffU
) == 0xffeU
)
8378 // Found a candidate. Note we haven't checked the destination is
8379 // within 4K here: if we do so (and don't create a record) we can't
8380 // tell that a branch should have been relocated when scanning later.
8381 this->cortex_a8_relocs_info_
[address
] =
8382 new Cortex_a8_reloc(stub
, r_type
,
8383 destination
| (target_is_thumb
? 1 : 0));
8387 // This function scans a relocation sections for stub generation.
8388 // The template parameter Relocate must be a class type which provides
8389 // a single function, relocate(), which implements the machine
8390 // specific part of a relocation.
8392 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
8393 // SHT_REL or SHT_RELA.
8395 // PRELOCS points to the relocation data. RELOC_COUNT is the number
8396 // of relocs. OUTPUT_SECTION is the output section.
8397 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
8398 // mapped to output offsets.
8400 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
8401 // VIEW_SIZE is the size. These refer to the input section, unless
8402 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
8403 // the output section.
8405 template<bool big_endian
>
8406 template<int sh_type
>
8408 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
8409 const Relocate_info
<32, big_endian
>* relinfo
,
8410 const unsigned char* prelocs
,
8412 Output_section
* output_section
,
8413 bool needs_special_offset_handling
,
8414 const unsigned char* view
,
8415 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
8418 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
8419 const int reloc_size
=
8420 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
8422 Arm_relobj
<big_endian
>* arm_object
=
8423 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8424 unsigned int local_count
= arm_object
->local_symbol_count();
8426 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
8428 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
8430 Reltype
reloc(prelocs
);
8432 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
8433 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
8434 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
8436 r_type
= this->get_real_reloc_type(r_type
);
8438 // Only a few relocation types need stubs.
8439 if ((r_type
!= elfcpp::R_ARM_CALL
)
8440 && (r_type
!= elfcpp::R_ARM_JUMP24
)
8441 && (r_type
!= elfcpp::R_ARM_PLT32
)
8442 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
8443 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
8444 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
8445 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
)
8446 && (r_type
!= elfcpp::R_ARM_V4BX
))
8449 section_offset_type offset
=
8450 convert_to_section_size_type(reloc
.get_r_offset());
8452 if (needs_special_offset_handling
)
8454 offset
= output_section
->output_offset(relinfo
->object
,
8455 relinfo
->data_shndx
,
8461 if (r_type
== elfcpp::R_ARM_V4BX
)
8463 // Get the BX instruction.
8464 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8465 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ offset
);
8466 elfcpp::Elf_types
<32>::Elf_Swxword insn
=
8467 elfcpp::Swap
<32, big_endian
>::readval(wv
);
8468 this->scan_reloc_for_stub(relinfo
, r_type
, NULL
, 0, NULL
,
8474 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
8475 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
8476 stub_addend_reader(r_type
, view
+ offset
, reloc
);
8478 const Sized_symbol
<32>* sym
;
8480 Symbol_value
<32> symval
;
8481 const Symbol_value
<32> *psymval
;
8482 if (r_sym
< local_count
)
8485 psymval
= arm_object
->local_symbol(r_sym
);
8487 // If the local symbol belongs to a section we are discarding,
8488 // and that section is a debug section, try to find the
8489 // corresponding kept section and map this symbol to its
8490 // counterpart in the kept section. The symbol must not
8491 // correspond to a section we are folding.
8493 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
8495 && shndx
!= elfcpp::SHN_UNDEF
8496 && !arm_object
->is_section_included(shndx
)
8497 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
8499 if (comdat_behavior
== CB_UNDETERMINED
)
8502 arm_object
->section_name(relinfo
->data_shndx
);
8503 comdat_behavior
= get_comdat_behavior(name
.c_str());
8505 if (comdat_behavior
== CB_PRETEND
)
8508 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
8509 arm_object
->map_to_kept_section(shndx
, &found
);
8511 symval
.set_output_value(value
+ psymval
->input_value());
8513 symval
.set_output_value(0);
8517 symval
.set_output_value(0);
8519 symval
.set_no_output_symtab_entry();
8525 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
8526 gold_assert(gsym
!= NULL
);
8527 if (gsym
->is_forwarder())
8528 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
8530 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
8531 if (sym
->has_symtab_index())
8532 symval
.set_output_symtab_index(sym
->symtab_index());
8534 symval
.set_no_output_symtab_entry();
8536 // We need to compute the would-be final value of this global
8538 const Symbol_table
* symtab
= relinfo
->symtab
;
8539 const Sized_symbol
<32>* sized_symbol
=
8540 symtab
->get_sized_symbol
<32>(gsym
);
8541 Symbol_table::Compute_final_value_status status
;
8543 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
8545 // Skip this if the symbol has not output section.
8546 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
8549 symval
.set_output_value(value
);
8553 // If symbol is a section symbol, we don't know the actual type of
8554 // destination. Give up.
8555 if (psymval
->is_section_symbol())
8558 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
8559 addend
, view_address
+ offset
);
8563 // Scan an input section for stub generation.
8565 template<bool big_endian
>
8567 Target_arm
<big_endian
>::scan_section_for_stubs(
8568 const Relocate_info
<32, big_endian
>* relinfo
,
8569 unsigned int sh_type
,
8570 const unsigned char* prelocs
,
8572 Output_section
* output_section
,
8573 bool needs_special_offset_handling
,
8574 const unsigned char* view
,
8575 Arm_address view_address
,
8576 section_size_type view_size
)
8578 if (sh_type
== elfcpp::SHT_REL
)
8579 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
8584 needs_special_offset_handling
,
8588 else if (sh_type
== elfcpp::SHT_RELA
)
8589 // We do not support RELA type relocations yet. This is provided for
8591 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
8596 needs_special_offset_handling
,
8604 // Group input sections for stub generation.
8606 // We goup input sections in an output sections so that the total size,
8607 // including any padding space due to alignment is smaller than GROUP_SIZE
8608 // unless the only input section in group is bigger than GROUP_SIZE already.
8609 // Then an ARM stub table is created to follow the last input section
8610 // in group. For each group an ARM stub table is created an is placed
8611 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
8612 // extend the group after the stub table.
8614 template<bool big_endian
>
8616 Target_arm
<big_endian
>::group_sections(
8618 section_size_type group_size
,
8619 bool stubs_always_after_branch
)
8621 // Group input sections and insert stub table
8622 Layout::Section_list section_list
;
8623 layout
->get_allocated_sections(§ion_list
);
8624 for (Layout::Section_list::const_iterator p
= section_list
.begin();
8625 p
!= section_list
.end();
8628 Arm_output_section
<big_endian
>* output_section
=
8629 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
8630 output_section
->group_sections(group_size
, stubs_always_after_branch
,
8635 // Relaxation hook. This is where we do stub generation.
8637 template<bool big_endian
>
8639 Target_arm
<big_endian
>::do_relax(
8641 const Input_objects
* input_objects
,
8642 Symbol_table
* symtab
,
8645 // No need to generate stubs if this is a relocatable link.
8646 gold_assert(!parameters
->options().relocatable());
8648 // If this is the first pass, we need to group input sections into
8652 // Determine the stub group size. The group size is the absolute
8653 // value of the parameter --stub-group-size. If --stub-group-size
8654 // is passed a negative value, we restict stubs to be always after
8655 // the stubbed branches.
8656 int32_t stub_group_size_param
=
8657 parameters
->options().stub_group_size();
8658 bool stubs_always_after_branch
= stub_group_size_param
< 0;
8659 section_size_type stub_group_size
= abs(stub_group_size_param
);
8661 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8662 // page as the first half of a 32-bit branch straddling two 4K pages.
8663 // This is a crude way of enforcing that.
8664 if (this->fix_cortex_a8_
)
8665 stubs_always_after_branch
= true;
8667 if (stub_group_size
== 1)
8670 // Thumb branch range is +-4MB has to be used as the default
8671 // maximum size (a given section can contain both ARM and Thumb
8672 // code, so the worst case has to be taken into account).
8674 // This value is 24K less than that, which allows for 2025
8675 // 12-byte stubs. If we exceed that, then we will fail to link.
8676 // The user will have to relink with an explicit group size
8678 stub_group_size
= 4170000;
8681 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
8684 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8685 // beginning of each relaxation pass, just blow away all the stubs.
8686 // Alternatively, we could selectively remove only the stubs and reloc
8687 // information for code sections that have moved since the last pass.
8688 // That would require more book-keeping.
8689 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
8690 if (this->fix_cortex_a8_
)
8692 // Clear all Cortex-A8 reloc information.
8693 for (typename
Cortex_a8_relocs_info::const_iterator p
=
8694 this->cortex_a8_relocs_info_
.begin();
8695 p
!= this->cortex_a8_relocs_info_
.end();
8698 this->cortex_a8_relocs_info_
.clear();
8700 // Remove all Cortex-A8 stubs.
8701 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8702 sp
!= this->stub_tables_
.end();
8704 (*sp
)->remove_all_cortex_a8_stubs();
8707 // Scan relocs for relocation stubs
8708 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
8709 op
!= input_objects
->relobj_end();
8712 Arm_relobj
<big_endian
>* arm_relobj
=
8713 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
8714 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
8717 // Check all stub tables to see if any of them have their data sizes
8718 // or addresses alignments changed. These are the only things that
8720 bool any_stub_table_changed
= false;
8721 Unordered_set
<const Output_section
*> sections_needing_adjustment
;
8722 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8723 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8726 if ((*sp
)->update_data_size_and_addralign())
8728 // Update data size of stub table owner.
8729 Arm_input_section
<big_endian
>* owner
= (*sp
)->owner();
8730 uint64_t address
= owner
->address();
8731 off_t offset
= owner
->offset();
8732 owner
->reset_address_and_file_offset();
8733 owner
->set_address_and_file_offset(address
, offset
);
8735 sections_needing_adjustment
.insert(owner
->output_section());
8736 any_stub_table_changed
= true;
8740 // Output_section_data::output_section() returns a const pointer but we
8741 // need to update output sections, so we record all output sections needing
8742 // update above and scan the sections here to find out what sections need
8744 for(Layout::Section_list::const_iterator p
= layout
->section_list().begin();
8745 p
!= layout
->section_list().end();
8748 if (sections_needing_adjustment
.find(*p
)
8749 != sections_needing_adjustment
.end())
8750 (*p
)->set_section_offsets_need_adjustment();
8753 // Finalize the stubs in the last relaxation pass.
8754 if (!any_stub_table_changed
)
8755 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
8756 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
8758 (*sp
)->finalize_stubs();
8760 return any_stub_table_changed
;
8765 template<bool big_endian
>
8767 Target_arm
<big_endian
>::relocate_stub(
8769 const Relocate_info
<32, big_endian
>* relinfo
,
8770 Output_section
* output_section
,
8771 unsigned char* view
,
8772 Arm_address address
,
8773 section_size_type view_size
)
8776 const Stub_template
* stub_template
= stub
->stub_template();
8777 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
8779 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
8780 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
8782 unsigned int r_type
= insn
->r_type();
8783 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
8784 section_size_type reloc_size
= insn
->size();
8785 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
8787 // This is the address of the stub destination.
8788 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
8789 Symbol_value
<32> symval
;
8790 symval
.set_output_value(target
);
8792 // Synthesize a fake reloc just in case. We don't have a symbol so
8794 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
8795 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
8796 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
8797 reloc_write
.put_r_offset(reloc_offset
);
8798 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
8799 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
8801 relocate
.relocate(relinfo
, this, output_section
,
8802 this->fake_relnum_for_stubs
, rel
, r_type
,
8803 NULL
, &symval
, view
+ reloc_offset
,
8804 address
+ reloc_offset
, reloc_size
);
8808 // Determine whether an object attribute tag takes an integer, a
8811 template<bool big_endian
>
8813 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
8815 if (tag
== Object_attribute::Tag_compatibility
)
8816 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8817 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
8818 else if (tag
== elfcpp::Tag_nodefaults
)
8819 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8820 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
8821 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
8822 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
8824 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
8826 return ((tag
& 1) != 0
8827 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
8828 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8831 // Reorder attributes.
8833 // The ABI defines that Tag_conformance should be emitted first, and that
8834 // Tag_nodefaults should be second (if either is defined). This sets those
8835 // two positions, and bumps up the position of all the remaining tags to
8838 template<bool big_endian
>
8840 Target_arm
<big_endian
>::do_attributes_order(int num
) const
8842 // Reorder the known object attributes in output. We want to move
8843 // Tag_conformance to position 4 and Tag_conformance to position 5
8844 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
8846 return elfcpp::Tag_conformance
;
8848 return elfcpp::Tag_nodefaults
;
8849 if ((num
- 2) < elfcpp::Tag_nodefaults
)
8851 if ((num
- 1) < elfcpp::Tag_conformance
)
8856 // Scan a span of THUMB code for Cortex-A8 erratum.
8858 template<bool big_endian
>
8860 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
8861 Arm_relobj
<big_endian
>* arm_relobj
,
8863 section_size_type span_start
,
8864 section_size_type span_end
,
8865 const unsigned char* view
,
8866 Arm_address address
)
8868 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
8870 // The opcode is BLX.W, BL.W, B.W, Bcc.W
8871 // The branch target is in the same 4KB region as the
8872 // first half of the branch.
8873 // The instruction before the branch is a 32-bit
8874 // length non-branch instruction.
8875 section_size_type i
= span_start
;
8876 bool last_was_32bit
= false;
8877 bool last_was_branch
= false;
8878 while (i
< span_end
)
8880 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
8881 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
8882 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
8883 bool is_blx
= false, is_b
= false;
8884 bool is_bl
= false, is_bcc
= false;
8886 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
8889 // Load the rest of the insn (in manual-friendly order).
8890 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
8892 // Encoding T4: B<c>.W.
8893 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
8894 // Encoding T1: BL<c>.W.
8895 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
8896 // Encoding T2: BLX<c>.W.
8897 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
8898 // Encoding T3: B<c>.W (not permitted in IT block).
8899 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
8900 && (insn
& 0x07f00000U
) != 0x03800000U
);
8903 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
8905 // If this instruction is a 32-bit THUMB branch that crosses a 4K
8906 // page boundary and it follows 32-bit non-branch instruction,
8907 // we need to work around.
8909 && ((address
+ i
) & 0xfffU
) == 0xffeU
8911 && !last_was_branch
)
8913 // Check to see if there is a relocation stub for this branch.
8914 bool force_target_arm
= false;
8915 bool force_target_thumb
= false;
8916 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
8917 Cortex_a8_relocs_info::const_iterator p
=
8918 this->cortex_a8_relocs_info_
.find(address
+ i
);
8920 if (p
!= this->cortex_a8_relocs_info_
.end())
8922 cortex_a8_reloc
= p
->second
;
8923 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
8925 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8926 && !target_is_thumb
)
8927 force_target_arm
= true;
8928 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
8930 force_target_thumb
= true;
8934 Stub_type stub_type
= arm_stub_none
;
8936 // Check if we have an offending branch instruction.
8937 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
8938 uint16_t lower_insn
= insn
& 0xffffU
;
8939 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
8941 if (cortex_a8_reloc
!= NULL
8942 && cortex_a8_reloc
->reloc_stub() != NULL
)
8943 // We've already made a stub for this instruction, e.g.
8944 // it's a long branch or a Thumb->ARM stub. Assume that
8945 // stub will suffice to work around the A8 erratum (see
8946 // setting of always_after_branch above).
8950 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
8952 stub_type
= arm_stub_a8_veneer_b_cond
;
8954 else if (is_b
|| is_bl
|| is_blx
)
8956 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
8962 ? arm_stub_a8_veneer_blx
8964 ? arm_stub_a8_veneer_bl
8965 : arm_stub_a8_veneer_b
));
8968 if (stub_type
!= arm_stub_none
)
8970 Arm_address pc_for_insn
= address
+ i
+ 4;
8972 // The original instruction is a BL, but the target is
8973 // an ARM instruction. If we were not making a stub,
8974 // the BL would have been converted to a BLX. Use the
8975 // BLX stub instead in that case.
8976 if (this->may_use_blx() && force_target_arm
8977 && stub_type
== arm_stub_a8_veneer_bl
)
8979 stub_type
= arm_stub_a8_veneer_blx
;
8983 // Conversely, if the original instruction was
8984 // BLX but the target is Thumb mode, use the BL stub.
8985 else if (force_target_thumb
8986 && stub_type
== arm_stub_a8_veneer_blx
)
8988 stub_type
= arm_stub_a8_veneer_bl
;
8996 // If we found a relocation, use the proper destination,
8997 // not the offset in the (unrelocated) instruction.
8998 // Note this is always done if we switched the stub type above.
8999 if (cortex_a8_reloc
!= NULL
)
9000 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
9002 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
9004 // Add a new stub if destination address in in the same page.
9005 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
9007 Cortex_a8_stub
* stub
=
9008 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
9012 Stub_table
<big_endian
>* stub_table
=
9013 arm_relobj
->stub_table(shndx
);
9014 gold_assert(stub_table
!= NULL
);
9015 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
9020 i
+= insn_32bit
? 4 : 2;
9021 last_was_32bit
= insn_32bit
;
9022 last_was_branch
= is_32bit_branch
;
9026 // Apply the Cortex-A8 workaround.
9028 template<bool big_endian
>
9030 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
9031 const Cortex_a8_stub
* stub
,
9032 Arm_address stub_address
,
9033 unsigned char* insn_view
,
9034 Arm_address insn_address
)
9036 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
9037 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
9038 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
9039 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
9040 off_t branch_offset
= stub_address
- (insn_address
+ 4);
9042 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
9043 switch (stub
->stub_template()->type())
9045 case arm_stub_a8_veneer_b_cond
:
9046 gold_assert(!utils::has_overflow
<21>(branch_offset
));
9047 upper_insn
= RelocFuncs::thumb32_cond_branch_upper(upper_insn
,
9049 lower_insn
= RelocFuncs::thumb32_cond_branch_lower(lower_insn
,
9053 case arm_stub_a8_veneer_b
:
9054 case arm_stub_a8_veneer_bl
:
9055 case arm_stub_a8_veneer_blx
:
9056 if ((lower_insn
& 0x5000U
) == 0x4000U
)
9057 // For a BLX instruction, make sure that the relocation is
9058 // rounded up to a word boundary. This follows the semantics of
9059 // the instruction which specifies that bit 1 of the target
9060 // address will come from bit 1 of the base address.
9061 branch_offset
= (branch_offset
+ 2) & ~3;
9063 // Put BRANCH_OFFSET back into the insn.
9064 gold_assert(!utils::has_overflow
<25>(branch_offset
));
9065 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
9066 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
9073 // Put the relocated value back in the object file:
9074 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
9075 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
9078 template<bool big_endian
>
9079 class Target_selector_arm
: public Target_selector
9082 Target_selector_arm()
9083 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
9084 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
9088 do_instantiate_target()
9089 { return new Target_arm
<big_endian
>(); }
9092 Target_selector_arm
<false> target_selector_arm
;
9093 Target_selector_arm
<true> target_selector_armbe
;
9095 } // End anonymous namespace.