1 // arm.cc -- arm target support for gold.
3 // Copyright 2009, 2010 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.
38 #include "parameters.h"
45 #include "copy-relocs.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
60 template<bool big_endian
>
61 class Output_data_plt_arm
;
63 template<bool big_endian
>
66 template<bool big_endian
>
67 class Arm_input_section
;
69 class Arm_exidx_cantunwind
;
71 class Arm_exidx_merged_section
;
73 class Arm_exidx_fixup
;
75 template<bool big_endian
>
76 class Arm_output_section
;
78 class Arm_exidx_input_section
;
80 template<bool big_endian
>
83 template<bool big_endian
>
84 class Arm_relocate_functions
;
86 template<bool big_endian
>
87 class Arm_output_data_got
;
89 template<bool big_endian
>
93 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
95 // Maximum branch offsets for ARM, THUMB and THUMB2.
96 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
97 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
98 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
99 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
100 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
101 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE
= 8;
106 // The arm target class.
108 // This is a very simple port of gold for ARM-EABI. It is intended for
109 // supporting Android only for the time being.
112 // - Implement all static relocation types documented in arm-reloc.def.
113 // - Make PLTs more flexible for different architecture features like
115 // There are probably a lot more.
117 // Ideally we would like to avoid using global variables but this is used
118 // very in many places and sometimes in loops. If we use a function
119 // returning a static instance of Arm_reloc_property_table, it will very
120 // slow in an threaded environment since the static instance needs to be
121 // locked. The pointer is below initialized in the
122 // Target::do_select_as_default_target() hook so that we do not spend time
123 // building the table if we are not linking ARM objects.
125 // An alternative is to to process the information in arm-reloc.def in
126 // compilation time and generate a representation of it in PODs only. That
127 // way we can avoid initialization when the linker starts.
129 Arm_reloc_property_table
* arm_reloc_property_table
= NULL
;
131 // Instruction template class. This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
137 // Types of instruction templates.
141 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
142 // templates with class-specific semantics. Currently this is used
143 // only by the Cortex_a8_stub class for handling condition codes in
144 // conditional branches.
145 THUMB16_SPECIAL_TYPE
,
151 // Factory methods to create instruction templates in different formats.
153 static const Insn_template
154 thumb16_insn(uint32_t data
)
155 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
157 // A Thumb conditional branch, in which the proper condition is inserted
158 // when we build the stub.
159 static const Insn_template
160 thumb16_bcond_insn(uint32_t data
)
161 { return Insn_template(data
, THUMB16_SPECIAL_TYPE
, elfcpp::R_ARM_NONE
, 1); }
163 static const Insn_template
164 thumb32_insn(uint32_t data
)
165 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
167 static const Insn_template
168 thumb32_b_insn(uint32_t data
, int reloc_addend
)
170 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
174 static const Insn_template
175 arm_insn(uint32_t data
)
176 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
178 static const Insn_template
179 arm_rel_insn(unsigned data
, int reloc_addend
)
180 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
182 static const Insn_template
183 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
184 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
186 // Accessors. This class is used for read-only objects so no modifiers
191 { return this->data_
; }
193 // Return the instruction sequence type of this.
196 { return this->type_
; }
198 // Return the ARM relocation type of this.
201 { return this->r_type_
; }
205 { return this->reloc_addend_
; }
207 // Return size of instruction template in bytes.
211 // Return byte-alignment of instruction template.
216 // We make the constructor private to ensure that only the factory
219 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
220 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
223 // Instruction specific data. This is used to store information like
224 // some of the instruction bits.
226 // Instruction template type.
228 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
229 unsigned int r_type_
;
230 // Relocation addend.
231 int32_t reloc_addend_
;
234 // Macro for generating code to stub types. One entry per long/short
238 DEF_STUB(long_branch_any_any) \
239 DEF_STUB(long_branch_v4t_arm_thumb) \
240 DEF_STUB(long_branch_thumb_only) \
241 DEF_STUB(long_branch_v4t_thumb_thumb) \
242 DEF_STUB(long_branch_v4t_thumb_arm) \
243 DEF_STUB(short_branch_v4t_thumb_arm) \
244 DEF_STUB(long_branch_any_arm_pic) \
245 DEF_STUB(long_branch_any_thumb_pic) \
246 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
247 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
248 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
249 DEF_STUB(long_branch_thumb_only_pic) \
250 DEF_STUB(a8_veneer_b_cond) \
251 DEF_STUB(a8_veneer_b) \
252 DEF_STUB(a8_veneer_bl) \
253 DEF_STUB(a8_veneer_blx) \
254 DEF_STUB(v4_veneer_bx)
258 #define DEF_STUB(x) arm_stub_##x,
264 // First reloc stub type.
265 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
266 // Last reloc stub type.
267 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
269 // First Cortex-A8 stub type.
270 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
271 // Last Cortex-A8 stub type.
272 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
275 arm_stub_type_last
= arm_stub_v4_veneer_bx
279 // Stub template class. Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
286 Stub_template(Stub_type
, const Insn_template
*, size_t);
294 { return this->type_
; }
296 // Return an array of instruction templates.
299 { return this->insns_
; }
301 // Return size of template in number of instructions.
304 { return this->insn_count_
; }
306 // Return size of template in bytes.
309 { return this->size_
; }
311 // Return alignment of the stub template.
314 { return this->alignment_
; }
316 // Return whether entry point is in thumb mode.
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_
; }
321 // Return number of relocations in this template.
324 { return this->relocs_
.size(); }
326 // Return index of the I-th instruction with relocation.
328 reloc_insn_index(size_t i
) const
330 gold_assert(i
< this->relocs_
.size());
331 return this->relocs_
[i
].first
;
334 // Return the offset of the I-th instruction with relocation from the
335 // beginning of the stub.
337 reloc_offset(size_t i
) const
339 gold_assert(i
< this->relocs_
.size());
340 return this->relocs_
[i
].second
;
344 // This contains information about an instruction template with a relocation
345 // and its offset from start of stub.
346 typedef std::pair
<size_t, section_size_type
> Reloc
;
348 // A Stub_template may not be copied. We want to share templates as much
350 Stub_template(const Stub_template
&);
351 Stub_template
& operator=(const Stub_template
&);
355 // Points to an array of Insn_templates.
356 const Insn_template
* insns_
;
357 // Number of Insn_templates in insns_[].
359 // Size of templated instructions in bytes.
361 // Alignment of templated instructions.
363 // Flag to indicate if entry is in thumb mode.
364 bool entry_in_thumb_mode_
;
365 // A table of reloc instruction indices and offsets. We can find these by
366 // looking at the instruction templates but we pre-compute and then stash
367 // them here for speed.
368 std::vector
<Reloc
> relocs_
;
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
379 static const section_offset_type invalid_offset
=
380 static_cast<section_offset_type
>(-1);
383 Stub(const Stub_template
* stub_template
)
384 : stub_template_(stub_template
), offset_(invalid_offset
)
391 // Return the stub template.
393 stub_template() const
394 { return this->stub_template_
; }
396 // Return offset of code stub from beginning of its containing stub table.
400 gold_assert(this->offset_
!= invalid_offset
);
401 return this->offset_
;
404 // Set offset of code stub from beginning of its containing stub table.
406 set_offset(section_offset_type offset
)
407 { this->offset_
= offset
; }
409 // Return the relocation target address of the i-th relocation in the
410 // stub. This must be defined in a child class.
412 reloc_target(size_t i
)
413 { return this->do_reloc_target(i
); }
415 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
417 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
418 { this->do_write(view
, view_size
, big_endian
); }
420 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421 // for the i-th instruction.
423 thumb16_special(size_t i
)
424 { return this->do_thumb16_special(i
); }
427 // This must be defined in the child class.
429 do_reloc_target(size_t) = 0;
431 // This may be overridden in the child class.
433 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
436 this->do_fixed_endian_write
<true>(view
, view_size
);
438 this->do_fixed_endian_write
<false>(view
, view_size
);
441 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442 // instruction template.
444 do_thumb16_special(size_t)
445 { gold_unreachable(); }
448 // A template to implement do_write.
449 template<bool big_endian
>
451 do_fixed_endian_write(unsigned char*, section_size_type
);
454 const Stub_template
* stub_template_
;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_
;
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
462 class Reloc_stub
: public Stub
465 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
466 // We assume we never jump to this address.
467 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
469 // Return destination address.
471 destination_address() const
473 gold_assert(this->destination_address_
!= this->invalid_address
);
474 return this->destination_address_
;
477 // Set destination address.
479 set_destination_address(Arm_address address
)
481 gold_assert(address
!= this->invalid_address
);
482 this->destination_address_
= address
;
485 // Reset destination address.
487 reset_destination_address()
488 { this->destination_address_
= this->invalid_address
; }
490 // Determine stub type for a branch of a relocation of R_TYPE going
491 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
492 // the branch target is a thumb instruction. TARGET is used for look
493 // up ARM-specific linker settings.
495 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
496 Arm_address branch_target
, bool target_is_thumb
);
498 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
499 // and an addend. Since we treat global and local symbol differently, we
500 // use a Symbol object for a global symbol and a object-index pair for
505 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
507 // and R_SYM must not be invalid_index.
508 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
509 unsigned int r_sym
, int32_t addend
)
510 : stub_type_(stub_type
), addend_(addend
)
514 this->r_sym_
= Reloc_stub::invalid_index
;
515 this->u_
.symbol
= symbol
;
519 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
520 this->r_sym_
= r_sym
;
521 this->u_
.relobj
= relobj
;
528 // Accessors: Keys are meant to be read-only object so no modifiers are
534 { return this->stub_type_
; }
536 // Return the local symbol index or invalid_index.
539 { return this->r_sym_
; }
541 // Return the symbol if there is one.
544 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
546 // Return the relobj if there is one.
549 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
551 // Whether this equals to another key k.
553 eq(const Key
& k
) const
555 return ((this->stub_type_
== k
.stub_type_
)
556 && (this->r_sym_
== k
.r_sym_
)
557 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
558 ? (this->u_
.relobj
== k
.u_
.relobj
)
559 : (this->u_
.symbol
== k
.u_
.symbol
))
560 && (this->addend_
== k
.addend_
));
563 // Return a hash value.
567 return (this->stub_type_
569 ^ gold::string_hash
<char>(
570 (this->r_sym_
!= Reloc_stub::invalid_index
)
571 ? this->u_
.relobj
->name().c_str()
572 : this->u_
.symbol
->name())
576 // Functors for STL associative containers.
580 operator()(const Key
& k
) const
581 { return k
.hash_value(); }
587 operator()(const Key
& k1
, const Key
& k2
) const
588 { return k1
.eq(k2
); }
591 // Name of key. This is mainly for debugging.
597 Stub_type stub_type_
;
598 // If this is a local symbol, this is the index in the defining object.
599 // Otherwise, it is invalid_index for a global symbol.
601 // If r_sym_ is invalid index. This points to a global symbol.
602 // Otherwise, this points a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj. This is done to avoid making the stub class a template
605 // as most of the stub machinery is endianness-neutral. However, it
606 // may require a bit of casting done by users of this class.
609 const Symbol
* symbol
;
610 const Relobj
* relobj
;
612 // Addend associated with a reloc.
617 // Reloc_stubs are created via a stub factory. So these are protected.
618 Reloc_stub(const Stub_template
* stub_template
)
619 : Stub(stub_template
), destination_address_(invalid_address
)
625 friend class Stub_factory
;
627 // Return the relocation target address of the i-th relocation in the
630 do_reloc_target(size_t i
)
632 // All reloc stub have only one relocation.
634 return this->destination_address_
;
638 // Address of destination.
639 Arm_address destination_address_
;
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 // branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
649 // 3. The branch follows a 32-bit instruction which is not a branch.
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least. We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch. The
654 // condition code is used in a special instruction template. We also want
655 // to identify input sections needing Cortex-A8 workaround quickly. We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up. The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
661 class Cortex_a8_stub
: public Stub
667 // Return the object of the code section containing the branch being fixed
671 { return this->relobj_
; }
673 // Return the section index of the code section containing the branch being
677 { return this->shndx_
; }
679 // Return the source address of stub. This is the address of the original
680 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
683 source_address() const
684 { return this->source_address_
; }
686 // Return the destination address of the stub. This is the branch taken
687 // address of the original branch instruction. LSB is 1 if it is a THUMB
688 // instruction address.
690 destination_address() const
691 { return this->destination_address_
; }
693 // Return the instruction being fixed up.
695 original_insn() const
696 { return this->original_insn_
; }
699 // Cortex_a8_stubs are created via a stub factory. So these are protected.
700 Cortex_a8_stub(const Stub_template
* stub_template
, Relobj
* relobj
,
701 unsigned int shndx
, Arm_address source_address
,
702 Arm_address destination_address
, uint32_t original_insn
)
703 : Stub(stub_template
), relobj_(relobj
), shndx_(shndx
),
704 source_address_(source_address
| 1U),
705 destination_address_(destination_address
),
706 original_insn_(original_insn
)
709 friend class Stub_factory
;
711 // Return the relocation target address of the i-th relocation in the
714 do_reloc_target(size_t i
)
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond
)
718 // The conditional branch veneer has two relocations.
720 return i
== 0 ? this->source_address_
+ 4 : this->destination_address_
;
724 // All other Cortex-A8 stubs have only one relocation.
726 return this->destination_address_
;
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
732 do_thumb16_special(size_t);
735 // Object of the code section containing the branch being fixed up.
737 // Section index of the code section containing the branch begin fixed up.
739 // Source address of original branch.
740 Arm_address source_address_
;
741 // Destination address of the original branch.
742 Arm_address destination_address_
;
743 // Original branch instruction. This is needed for copying the condition
744 // code from a condition branch to its stub.
745 uint32_t original_insn_
;
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub
: public Stub
755 // Return the associated register.
758 { return this->reg_
; }
761 // Arm V4BX stubs are created via a stub factory. So these are protected.
762 Arm_v4bx_stub(const Stub_template
* stub_template
, const uint32_t reg
)
763 : Stub(stub_template
), reg_(reg
)
766 friend class Stub_factory
;
768 // Return the relocation target address of the i-th relocation in the
771 do_reloc_target(size_t)
772 { gold_unreachable(); }
774 // This may be overridden in the child class.
776 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
779 this->do_fixed_endian_v4bx_write
<true>(view
, view_size
);
781 this->do_fixed_endian_v4bx_write
<false>(view
, view_size
);
785 // A template to implement do_write.
786 template<bool big_endian
>
788 do_fixed_endian_v4bx_write(unsigned char* view
, section_size_type
)
790 const Insn_template
* insns
= this->stub_template()->insns();
791 elfcpp::Swap
<32, big_endian
>::writeval(view
,
793 + (this->reg_
<< 16)));
794 view
+= insns
[0].size();
795 elfcpp::Swap
<32, big_endian
>::writeval(view
,
796 (insns
[1].data() + this->reg_
));
797 view
+= insns
[1].size();
798 elfcpp::Swap
<32, big_endian
>::writeval(view
,
799 (insns
[2].data() + this->reg_
));
802 // A register index (r0-r14), which is associated with the stub.
806 // Stub factory class.
811 // Return the unique instance of this class.
812 static const Stub_factory
&
815 static Stub_factory singleton
;
819 // Make a relocation stub.
821 make_reloc_stub(Stub_type stub_type
) const
823 gold_assert(stub_type
>= arm_stub_reloc_first
824 && stub_type
<= arm_stub_reloc_last
);
825 return new Reloc_stub(this->stub_templates_
[stub_type
]);
828 // Make a Cortex-A8 stub.
830 make_cortex_a8_stub(Stub_type stub_type
, Relobj
* relobj
, unsigned int shndx
,
831 Arm_address source
, Arm_address destination
,
832 uint32_t original_insn
) const
834 gold_assert(stub_type
>= arm_stub_cortex_a8_first
835 && stub_type
<= arm_stub_cortex_a8_last
);
836 return new Cortex_a8_stub(this->stub_templates_
[stub_type
], relobj
, shndx
,
837 source
, destination
, original_insn
);
840 // Make an ARM V4BX relocation stub.
841 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
843 make_arm_v4bx_stub(uint32_t reg
) const
845 gold_assert(reg
< 0xf);
846 return new Arm_v4bx_stub(this->stub_templates_
[arm_stub_v4_veneer_bx
],
851 // Constructor and destructor are protected since we only return a single
852 // instance created in Stub_factory::get_instance().
856 // A Stub_factory may not be copied since it is a singleton.
857 Stub_factory(const Stub_factory
&);
858 Stub_factory
& operator=(Stub_factory
&);
860 // Stub templates. These are initialized in the constructor.
861 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
864 // A class to hold stubs for the ARM target.
866 template<bool big_endian
>
867 class Stub_table
: public Output_data
870 Stub_table(Arm_input_section
<big_endian
>* owner
)
871 : Output_data(), owner_(owner
), reloc_stubs_(), reloc_stubs_size_(0),
872 reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
873 prev_data_size_(0), prev_addralign_(1)
879 // Owner of this stub table.
880 Arm_input_section
<big_endian
>*
882 { return this->owner_
; }
884 // Whether this stub table is empty.
888 return (this->reloc_stubs_
.empty()
889 && this->cortex_a8_stubs_
.empty()
890 && this->arm_v4bx_stubs_
.empty());
893 // Return the current data size.
895 current_data_size() const
896 { return this->current_data_size_for_child(); }
898 // Add a STUB with using KEY. Caller is reponsible for avoid adding
899 // if already a STUB with the same key has been added.
901 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
)
903 const Stub_template
* stub_template
= stub
->stub_template();
904 gold_assert(stub_template
->type() == key
.stub_type());
905 this->reloc_stubs_
[key
] = stub
;
907 // Assign stub offset early. We can do this because we never remove
908 // reloc stubs and they are in the beginning of the stub table.
909 uint64_t align
= stub_template
->alignment();
910 this->reloc_stubs_size_
= align_address(this->reloc_stubs_size_
, align
);
911 stub
->set_offset(this->reloc_stubs_size_
);
912 this->reloc_stubs_size_
+= stub_template
->size();
913 this->reloc_stubs_addralign_
=
914 std::max(this->reloc_stubs_addralign_
, align
);
917 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
918 // Caller is reponsible for avoid adding if already a STUB with the same
919 // address has been added.
921 add_cortex_a8_stub(Arm_address address
, Cortex_a8_stub
* stub
)
923 std::pair
<Arm_address
, Cortex_a8_stub
*> value(address
, stub
);
924 this->cortex_a8_stubs_
.insert(value
);
927 // Add an ARM V4BX relocation stub. A register index will be retrieved
930 add_arm_v4bx_stub(Arm_v4bx_stub
* stub
)
932 gold_assert(stub
!= NULL
&& this->arm_v4bx_stubs_
[stub
->reg()] == NULL
);
933 this->arm_v4bx_stubs_
[stub
->reg()] = stub
;
936 // Remove all Cortex-A8 stubs.
938 remove_all_cortex_a8_stubs();
940 // Look up a relocation stub using KEY. Return NULL if there is none.
942 find_reloc_stub(const Reloc_stub::Key
& key
) const
944 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
945 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
948 // Look up an arm v4bx relocation stub using the register index.
949 // Return NULL if there is none.
951 find_arm_v4bx_stub(const uint32_t reg
) const
953 gold_assert(reg
< 0xf);
954 return this->arm_v4bx_stubs_
[reg
];
957 // Relocate stubs in this stub table.
959 relocate_stubs(const Relocate_info
<32, big_endian
>*,
960 Target_arm
<big_endian
>*, Output_section
*,
961 unsigned char*, Arm_address
, section_size_type
);
963 // Update data size and alignment at the end of a relaxation pass. Return
964 // true if either data size or alignment is different from that of the
965 // previous relaxation pass.
967 update_data_size_and_addralign();
969 // Finalize stubs. Set the offsets of all stubs and mark input sections
970 // needing the Cortex-A8 workaround.
974 // Apply Cortex-A8 workaround to an address range.
976 apply_cortex_a8_workaround_to_address_range(Target_arm
<big_endian
>*,
977 unsigned char*, Arm_address
,
981 // Write out section contents.
983 do_write(Output_file
*);
985 // Return the required alignment.
988 { return this->prev_addralign_
; }
990 // Reset address and file offset.
992 do_reset_address_and_file_offset()
993 { this->set_current_data_size_for_child(this->prev_data_size_
); }
995 // Set final data size.
997 set_final_data_size()
998 { this->set_data_size(this->current_data_size()); }
1001 // Relocate one stub.
1003 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
1004 Target_arm
<big_endian
>*, Output_section
*,
1005 unsigned char*, Arm_address
, section_size_type
);
1007 // Unordered map of relocation stubs.
1009 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
1010 Reloc_stub::Key::equal_to
>
1013 // List of Cortex-A8 stubs ordered by addresses of branches being
1014 // fixed up in output.
1015 typedef std::map
<Arm_address
, Cortex_a8_stub
*> Cortex_a8_stub_list
;
1016 // List of Arm V4BX relocation stubs ordered by associated registers.
1017 typedef std::vector
<Arm_v4bx_stub
*> Arm_v4bx_stub_list
;
1019 // Owner of this stub table.
1020 Arm_input_section
<big_endian
>* owner_
;
1021 // The relocation stubs.
1022 Reloc_stub_map reloc_stubs_
;
1023 // Size of reloc stubs.
1024 off_t reloc_stubs_size_
;
1025 // Maximum address alignment of reloc stubs.
1026 uint64_t reloc_stubs_addralign_
;
1027 // The cortex_a8_stubs.
1028 Cortex_a8_stub_list cortex_a8_stubs_
;
1029 // The Arm V4BX relocation stubs.
1030 Arm_v4bx_stub_list arm_v4bx_stubs_
;
1031 // data size of this in the previous pass.
1032 off_t prev_data_size_
;
1033 // address alignment of this in the previous pass.
1034 uint64_t prev_addralign_
;
1037 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1038 // we add to the end of an EXIDX input section that goes into the output.
1040 class Arm_exidx_cantunwind
: public Output_section_data
1043 Arm_exidx_cantunwind(Relobj
* relobj
, unsigned int shndx
)
1044 : Output_section_data(8, 4, true), relobj_(relobj
), shndx_(shndx
)
1047 // Return the object containing the section pointed by this.
1050 { return this->relobj_
; }
1052 // Return the section index of the section pointed by this.
1055 { return this->shndx_
; }
1059 do_write(Output_file
* of
)
1061 if (parameters
->target().is_big_endian())
1062 this->do_fixed_endian_write
<true>(of
);
1064 this->do_fixed_endian_write
<false>(of
);
1067 // Write to a map file.
1069 do_print_to_mapfile(Mapfile
* mapfile
) const
1070 { mapfile
->print_output_data(this, _("** ARM cantunwind")); }
1073 // Implement do_write for a given endianness.
1074 template<bool big_endian
>
1076 do_fixed_endian_write(Output_file
*);
1078 // The object containing the section pointed by this.
1080 // The section index of the section pointed by this.
1081 unsigned int shndx_
;
1084 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1085 // Offset map is used to map input section offset within the EXIDX section
1086 // to the output offset from the start of this EXIDX section.
1088 typedef std::map
<section_offset_type
, section_offset_type
>
1089 Arm_exidx_section_offset_map
;
1091 // Arm_exidx_merged_section class. This represents an EXIDX input section
1092 // with some of its entries merged.
1094 class Arm_exidx_merged_section
: public Output_relaxed_input_section
1097 // Constructor for Arm_exidx_merged_section.
1098 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1099 // SECTION_OFFSET_MAP points to a section offset map describing how
1100 // parts of the input section are mapped to output. DELETED_BYTES is
1101 // the number of bytes deleted from the EXIDX input section.
1102 Arm_exidx_merged_section(
1103 const Arm_exidx_input_section
& exidx_input_section
,
1104 const Arm_exidx_section_offset_map
& section_offset_map
,
1105 uint32_t deleted_bytes
);
1107 // Return the original EXIDX input section.
1108 const Arm_exidx_input_section
&
1109 exidx_input_section() const
1110 { return this->exidx_input_section_
; }
1112 // Return the section offset map.
1113 const Arm_exidx_section_offset_map
&
1114 section_offset_map() const
1115 { return this->section_offset_map_
; }
1118 // Write merged section into file OF.
1120 do_write(Output_file
* of
);
1123 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
1124 section_offset_type
*) const;
1127 // Original EXIDX input section.
1128 const Arm_exidx_input_section
& exidx_input_section_
;
1129 // Section offset map.
1130 const Arm_exidx_section_offset_map
& section_offset_map_
;
1133 // A class to wrap an ordinary input section containing executable code.
1135 template<bool big_endian
>
1136 class Arm_input_section
: public Output_relaxed_input_section
1139 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
1140 : Output_relaxed_input_section(relobj
, shndx
, 1),
1141 original_addralign_(1), original_size_(0), stub_table_(NULL
)
1144 ~Arm_input_section()
1151 // Whether this is a stub table owner.
1153 is_stub_table_owner() const
1154 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
1156 // Return the stub table.
1157 Stub_table
<big_endian
>*
1159 { return this->stub_table_
; }
1161 // Set the stub_table.
1163 set_stub_table(Stub_table
<big_endian
>* stub_table
)
1164 { this->stub_table_
= stub_table
; }
1166 // Downcast a base pointer to an Arm_input_section pointer. This is
1167 // not type-safe but we only use Arm_input_section not the base class.
1168 static Arm_input_section
<big_endian
>*
1169 as_arm_input_section(Output_relaxed_input_section
* poris
)
1170 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
1172 // Return the original size of the section.
1174 original_size() const
1175 { return this->original_size_
; }
1178 // Write data to output file.
1180 do_write(Output_file
*);
1182 // Return required alignment of this.
1184 do_addralign() const
1186 if (this->is_stub_table_owner())
1187 return std::max(this->stub_table_
->addralign(),
1188 static_cast<uint64_t>(this->original_addralign_
));
1190 return this->original_addralign_
;
1193 // Finalize data size.
1195 set_final_data_size();
1197 // Reset address and file offset.
1199 do_reset_address_and_file_offset();
1203 do_output_offset(const Relobj
* object
, unsigned int shndx
,
1204 section_offset_type offset
,
1205 section_offset_type
* poutput
) const
1207 if ((object
== this->relobj())
1208 && (shndx
== this->shndx())
1211 convert_types
<section_offset_type
, uint32_t>(this->original_size_
)))
1221 // Copying is not allowed.
1222 Arm_input_section(const Arm_input_section
&);
1223 Arm_input_section
& operator=(const Arm_input_section
&);
1225 // Address alignment of the original input section.
1226 uint32_t original_addralign_
;
1227 // Section size of the original input section.
1228 uint32_t original_size_
;
1230 Stub_table
<big_endian
>* stub_table_
;
1233 // Arm_exidx_fixup class. This is used to define a number of methods
1234 // and keep states for fixing up EXIDX coverage.
1236 class Arm_exidx_fixup
1239 Arm_exidx_fixup(Output_section
* exidx_output_section
,
1240 bool merge_exidx_entries
= true)
1241 : exidx_output_section_(exidx_output_section
), last_unwind_type_(UT_NONE
),
1242 last_inlined_entry_(0), last_input_section_(NULL
),
1243 section_offset_map_(NULL
), first_output_text_section_(NULL
),
1244 merge_exidx_entries_(merge_exidx_entries
)
1248 { delete this->section_offset_map_
; }
1250 // Process an EXIDX section for entry merging. Return number of bytes to
1251 // be deleted in output. If parts of the input EXIDX section are merged
1252 // a heap allocated Arm_exidx_section_offset_map is store in the located
1253 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1255 template<bool big_endian
>
1257 process_exidx_section(const Arm_exidx_input_section
* exidx_input_section
,
1258 Arm_exidx_section_offset_map
** psection_offset_map
);
1260 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1261 // input section, if there is not one already.
1263 add_exidx_cantunwind_as_needed();
1265 // Return the output section for the text section which is linked to the
1266 // first exidx input in output.
1268 first_output_text_section() const
1269 { return this->first_output_text_section_
; }
1272 // Copying is not allowed.
1273 Arm_exidx_fixup(const Arm_exidx_fixup
&);
1274 Arm_exidx_fixup
& operator=(const Arm_exidx_fixup
&);
1276 // Type of EXIDX unwind entry.
1281 // EXIDX_CANTUNWIND.
1282 UT_EXIDX_CANTUNWIND
,
1289 // Process an EXIDX entry. We only care about the second word of the
1290 // entry. Return true if the entry can be deleted.
1292 process_exidx_entry(uint32_t second_word
);
1294 // Update the current section offset map during EXIDX section fix-up.
1295 // If there is no map, create one. INPUT_OFFSET is the offset of a
1296 // reference point, DELETED_BYTES is the number of deleted by in the
1297 // section so far. If DELETE_ENTRY is true, the reference point and
1298 // all offsets after the previous reference point are discarded.
1300 update_offset_map(section_offset_type input_offset
,
1301 section_size_type deleted_bytes
, bool delete_entry
);
1303 // EXIDX output section.
1304 Output_section
* exidx_output_section_
;
1305 // Unwind type of the last EXIDX entry processed.
1306 Unwind_type last_unwind_type_
;
1307 // Last seen inlined EXIDX entry.
1308 uint32_t last_inlined_entry_
;
1309 // Last processed EXIDX input section.
1310 const Arm_exidx_input_section
* last_input_section_
;
1311 // Section offset map created in process_exidx_section.
1312 Arm_exidx_section_offset_map
* section_offset_map_
;
1313 // Output section for the text section which is linked to the first exidx
1315 Output_section
* first_output_text_section_
;
1317 bool merge_exidx_entries_
;
1320 // Arm output section class. This is defined mainly to add a number of
1321 // stub generation methods.
1323 template<bool big_endian
>
1324 class Arm_output_section
: public Output_section
1327 typedef std::vector
<std::pair
<Relobj
*, unsigned int> > Text_section_list
;
1329 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1330 elfcpp::Elf_Xword flags
)
1331 : Output_section(name
, type
, flags
)
1333 if (type
== elfcpp::SHT_ARM_EXIDX
)
1334 this->set_always_keeps_input_sections();
1337 ~Arm_output_section()
1340 // Group input sections for stub generation.
1342 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1344 // Downcast a base pointer to an Arm_output_section pointer. This is
1345 // not type-safe but we only use Arm_output_section not the base class.
1346 static Arm_output_section
<big_endian
>*
1347 as_arm_output_section(Output_section
* os
)
1348 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1350 // Append all input text sections in this into LIST.
1352 append_text_sections_to_list(Text_section_list
* list
);
1354 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1355 // is a list of text input sections sorted in ascending order of their
1356 // output addresses.
1358 fix_exidx_coverage(Layout
* layout
,
1359 const Text_section_list
& sorted_text_section
,
1360 Symbol_table
* symtab
,
1361 bool merge_exidx_entries
);
1363 // Link an EXIDX section into its corresponding text section.
1365 set_exidx_section_link();
1369 typedef Output_section::Input_section Input_section
;
1370 typedef Output_section::Input_section_list Input_section_list
;
1372 // Create a stub group.
1373 void create_stub_group(Input_section_list::const_iterator
,
1374 Input_section_list::const_iterator
,
1375 Input_section_list::const_iterator
,
1376 Target_arm
<big_endian
>*,
1377 std::vector
<Output_relaxed_input_section
*>*);
1380 // Arm_exidx_input_section class. This represents an EXIDX input section.
1382 class Arm_exidx_input_section
1385 static const section_offset_type invalid_offset
=
1386 static_cast<section_offset_type
>(-1);
1388 Arm_exidx_input_section(Relobj
* relobj
, unsigned int shndx
,
1389 unsigned int link
, uint32_t size
, uint32_t addralign
)
1390 : relobj_(relobj
), shndx_(shndx
), link_(link
), size_(size
),
1391 addralign_(addralign
), has_errors_(false)
1394 ~Arm_exidx_input_section()
1397 // Accessors: This is a read-only class.
1399 // Return the object containing this EXIDX input section.
1402 { return this->relobj_
; }
1404 // Return the section index of this EXIDX input section.
1407 { return this->shndx_
; }
1409 // Return the section index of linked text section in the same object.
1412 { return this->link_
; }
1414 // Return size of the EXIDX input section.
1417 { return this->size_
; }
1419 // Reutnr address alignment of EXIDX input section.
1422 { return this->addralign_
; }
1424 // Whether there are any errors in the EXIDX input section.
1427 { return this->has_errors_
; }
1429 // Set has-errors flag.
1432 { this->has_errors_
= true; }
1435 // Object containing this.
1437 // Section index of this.
1438 unsigned int shndx_
;
1439 // text section linked to this in the same object.
1441 // Size of this. For ARM 32-bit is sufficient.
1443 // Address alignment of this. For ARM 32-bit is sufficient.
1444 uint32_t addralign_
;
1445 // Whether this has any errors.
1449 // Arm_relobj class.
1451 template<bool big_endian
>
1452 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1455 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1457 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1458 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1459 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1460 stub_tables_(), local_symbol_is_thumb_function_(),
1461 attributes_section_data_(NULL
), mapping_symbols_info_(),
1462 section_has_cortex_a8_workaround_(NULL
), exidx_section_map_(),
1463 output_local_symbol_count_needs_update_(false),
1464 merge_flags_and_attributes_(true)
1468 { delete this->attributes_section_data_
; }
1470 // Return the stub table of the SHNDX-th section if there is one.
1471 Stub_table
<big_endian
>*
1472 stub_table(unsigned int shndx
) const
1474 gold_assert(shndx
< this->stub_tables_
.size());
1475 return this->stub_tables_
[shndx
];
1478 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1480 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1482 gold_assert(shndx
< this->stub_tables_
.size());
1483 this->stub_tables_
[shndx
] = stub_table
;
1486 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1487 // index. This is only valid after do_count_local_symbol is called.
1489 local_symbol_is_thumb_function(unsigned int r_sym
) const
1491 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1492 return this->local_symbol_is_thumb_function_
[r_sym
];
1495 // Scan all relocation sections for stub generation.
1497 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1500 // Convert regular input section with index SHNDX to a relaxed section.
1502 convert_input_section_to_relaxed_section(unsigned shndx
)
1504 // The stubs have relocations and we need to process them after writing
1505 // out the stubs. So relocation now must follow section write.
1506 this->set_section_offset(shndx
, -1ULL);
1507 this->set_relocs_must_follow_section_writes();
1510 // Downcast a base pointer to an Arm_relobj pointer. This is
1511 // not type-safe but we only use Arm_relobj not the base class.
1512 static Arm_relobj
<big_endian
>*
1513 as_arm_relobj(Relobj
* relobj
)
1514 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1516 // Processor-specific flags in ELF file header. This is valid only after
1519 processor_specific_flags() const
1520 { return this->processor_specific_flags_
; }
1522 // Attribute section data This is the contents of the .ARM.attribute section
1524 const Attributes_section_data
*
1525 attributes_section_data() const
1526 { return this->attributes_section_data_
; }
1528 // Mapping symbol location.
1529 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1531 // Functor for STL container.
1532 struct Mapping_symbol_position_less
1535 operator()(const Mapping_symbol_position
& p1
,
1536 const Mapping_symbol_position
& p2
) const
1538 return (p1
.first
< p2
.first
1539 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1543 // We only care about the first character of a mapping symbol, so
1544 // we only store that instead of the whole symbol name.
1545 typedef std::map
<Mapping_symbol_position
, char,
1546 Mapping_symbol_position_less
> Mapping_symbols_info
;
1548 // Whether a section contains any Cortex-A8 workaround.
1550 section_has_cortex_a8_workaround(unsigned int shndx
) const
1552 return (this->section_has_cortex_a8_workaround_
!= NULL
1553 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1556 // Mark a section that has Cortex-A8 workaround.
1558 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1560 if (this->section_has_cortex_a8_workaround_
== NULL
)
1561 this->section_has_cortex_a8_workaround_
=
1562 new std::vector
<bool>(this->shnum(), false);
1563 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1566 // Return the EXIDX section of an text section with index SHNDX or NULL
1567 // if the text section has no associated EXIDX section.
1568 const Arm_exidx_input_section
*
1569 exidx_input_section_by_link(unsigned int shndx
) const
1571 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1572 return ((p
!= this->exidx_section_map_
.end()
1573 && p
->second
->link() == shndx
)
1578 // Return the EXIDX section with index SHNDX or NULL if there is none.
1579 const Arm_exidx_input_section
*
1580 exidx_input_section_by_shndx(unsigned shndx
) const
1582 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1583 return ((p
!= this->exidx_section_map_
.end()
1584 && p
->second
->shndx() == shndx
)
1589 // Whether output local symbol count needs updating.
1591 output_local_symbol_count_needs_update() const
1592 { return this->output_local_symbol_count_needs_update_
; }
1594 // Set output_local_symbol_count_needs_update flag to be true.
1596 set_output_local_symbol_count_needs_update()
1597 { this->output_local_symbol_count_needs_update_
= true; }
1599 // Update output local symbol count at the end of relaxation.
1601 update_output_local_symbol_count();
1603 // Whether we want to merge processor-specific flags and attributes.
1605 merge_flags_and_attributes() const
1606 { return this->merge_flags_and_attributes_
; }
1608 // Export list of EXIDX section indices.
1610 get_exidx_shndx_list(std::vector
<unsigned int>* list
) const
1613 for (Exidx_section_map::const_iterator p
= this->exidx_section_map_
.begin();
1614 p
!= this->exidx_section_map_
.end();
1617 if (p
->second
->shndx() == p
->first
)
1618 list
->push_back(p
->first
);
1620 // Sort list to make result independent of implementation of map.
1621 std::sort(list
->begin(), list
->end());
1625 // Post constructor setup.
1629 // Call parent's setup method.
1630 Sized_relobj
<32, big_endian
>::do_setup();
1632 // Initialize look-up tables.
1633 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1634 this->stub_tables_
.swap(empty_stub_table_list
);
1637 // Count the local symbols.
1639 do_count_local_symbols(Stringpool_template
<char>*,
1640 Stringpool_template
<char>*);
1643 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1644 const unsigned char* pshdrs
, Output_file
* of
,
1645 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1647 // Read the symbol information.
1649 do_read_symbols(Read_symbols_data
* sd
);
1651 // Process relocs for garbage collection.
1653 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1657 // Whether a section needs to be scanned for relocation stubs.
1659 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1660 const Relobj::Output_sections
&,
1661 const Symbol_table
*, const unsigned char*);
1663 // Whether a section is a scannable text section.
1665 section_is_scannable(const elfcpp::Shdr
<32, big_endian
>&, unsigned int,
1666 const Output_section
*, const Symbol_table
*);
1668 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1670 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1671 unsigned int, Output_section
*,
1672 const Symbol_table
*);
1674 // Scan a section for the Cortex-A8 erratum.
1676 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1677 unsigned int, Output_section
*,
1678 Target_arm
<big_endian
>*);
1680 // Find the linked text section of an EXIDX section by looking at the
1681 // first reloction of the EXIDX section. PSHDR points to the section
1682 // headers of a relocation section and PSYMS points to the local symbols.
1683 // PSHNDX points to a location storing the text section index if found.
1684 // Return whether we can find the linked section.
1686 find_linked_text_section(const unsigned char* pshdr
,
1687 const unsigned char* psyms
, unsigned int* pshndx
);
1690 // Make a new Arm_exidx_input_section object for EXIDX section with
1691 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1692 // index of the linked text section.
1694 make_exidx_input_section(unsigned int shndx
,
1695 const elfcpp::Shdr
<32, big_endian
>& shdr
,
1696 unsigned int text_shndx
,
1697 const elfcpp::Shdr
<32, big_endian
>& text_shdr
);
1699 // Return the output address of either a plain input section or a
1700 // relaxed input section. SHNDX is the section index.
1702 simple_input_section_output_address(unsigned int, Output_section
*);
1704 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1705 typedef Unordered_map
<unsigned int, const Arm_exidx_input_section
*>
1708 // List of stub tables.
1709 Stub_table_list stub_tables_
;
1710 // Bit vector to tell if a local symbol is a thumb function or not.
1711 // This is only valid after do_count_local_symbol is called.
1712 std::vector
<bool> local_symbol_is_thumb_function_
;
1713 // processor-specific flags in ELF file header.
1714 elfcpp::Elf_Word processor_specific_flags_
;
1715 // Object attributes if there is an .ARM.attributes section or NULL.
1716 Attributes_section_data
* attributes_section_data_
;
1717 // Mapping symbols information.
1718 Mapping_symbols_info mapping_symbols_info_
;
1719 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1720 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1721 // Map a text section to its associated .ARM.exidx section, if there is one.
1722 Exidx_section_map exidx_section_map_
;
1723 // Whether output local symbol count needs updating.
1724 bool output_local_symbol_count_needs_update_
;
1725 // Whether we merge processor flags and attributes of this object to
1727 bool merge_flags_and_attributes_
;
1730 // Arm_dynobj class.
1732 template<bool big_endian
>
1733 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1736 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1737 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1738 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1739 processor_specific_flags_(0), attributes_section_data_(NULL
)
1743 { delete this->attributes_section_data_
; }
1745 // Downcast a base pointer to an Arm_relobj pointer. This is
1746 // not type-safe but we only use Arm_relobj not the base class.
1747 static Arm_dynobj
<big_endian
>*
1748 as_arm_dynobj(Dynobj
* dynobj
)
1749 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1751 // Processor-specific flags in ELF file header. This is valid only after
1754 processor_specific_flags() const
1755 { return this->processor_specific_flags_
; }
1757 // Attributes section data.
1758 const Attributes_section_data
*
1759 attributes_section_data() const
1760 { return this->attributes_section_data_
; }
1763 // Read the symbol information.
1765 do_read_symbols(Read_symbols_data
* sd
);
1768 // processor-specific flags in ELF file header.
1769 elfcpp::Elf_Word processor_specific_flags_
;
1770 // Object attributes if there is an .ARM.attributes section or NULL.
1771 Attributes_section_data
* attributes_section_data_
;
1774 // Functor to read reloc addends during stub generation.
1776 template<int sh_type
, bool big_endian
>
1777 struct Stub_addend_reader
1779 // Return the addend for a relocation of a particular type. Depending
1780 // on whether this is a REL or RELA relocation, read the addend from a
1781 // view or from a Reloc object.
1782 elfcpp::Elf_types
<32>::Elf_Swxword
1784 unsigned int /* r_type */,
1785 const unsigned char* /* view */,
1786 const typename Reloc_types
<sh_type
,
1787 32, big_endian
>::Reloc
& /* reloc */) const;
1790 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1792 template<bool big_endian
>
1793 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1795 elfcpp::Elf_types
<32>::Elf_Swxword
1798 const unsigned char*,
1799 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1802 // Specialized Stub_addend_reader for RELA type relocation sections.
1803 // We currently do not handle RELA type relocation sections but it is trivial
1804 // to implement the addend reader. This is provided for completeness and to
1805 // make it easier to add support for RELA relocation sections in the future.
1807 template<bool big_endian
>
1808 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1810 elfcpp::Elf_types
<32>::Elf_Swxword
1813 const unsigned char*,
1814 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1815 big_endian
>::Reloc
& reloc
) const
1816 { return reloc
.get_r_addend(); }
1819 // Cortex_a8_reloc class. We keep record of relocation that may need
1820 // the Cortex-A8 erratum workaround.
1822 class Cortex_a8_reloc
1825 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1826 Arm_address destination
)
1827 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1833 // Accessors: This is a read-only class.
1835 // Return the relocation stub associated with this relocation if there is
1839 { return this->reloc_stub_
; }
1841 // Return the relocation type.
1844 { return this->r_type_
; }
1846 // Return the destination address of the relocation. LSB stores the THUMB
1850 { return this->destination_
; }
1853 // Associated relocation stub if there is one, or NULL.
1854 const Reloc_stub
* reloc_stub_
;
1856 unsigned int r_type_
;
1857 // Destination address of this relocation. LSB is used to distinguish
1859 Arm_address destination_
;
1862 // Arm_output_data_got class. We derive this from Output_data_got to add
1863 // extra methods to handle TLS relocations in a static link.
1865 template<bool big_endian
>
1866 class Arm_output_data_got
: public Output_data_got
<32, big_endian
>
1869 Arm_output_data_got(Symbol_table
* symtab
, Layout
* layout
)
1870 : Output_data_got
<32, big_endian
>(), symbol_table_(symtab
), layout_(layout
)
1873 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1874 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1875 // applied in a static link.
1877 add_static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
1878 { this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, gsym
)); }
1880 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1881 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1882 // relocation that needs to be applied in a static link.
1884 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
1885 Sized_relobj
<32, big_endian
>* relobj
, unsigned int index
)
1887 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
1891 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1892 // The first one is initialized to be 1, which is the module index for
1893 // the main executable and the second one 0. A reloc of the type
1894 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1895 // be applied by gold. GSYM is a global symbol.
1897 add_tls_gd32_with_static_reloc(unsigned int got_type
, Symbol
* gsym
);
1899 // Same as the above but for a local symbol in OBJECT with INDEX.
1901 add_tls_gd32_with_static_reloc(unsigned int got_type
,
1902 Sized_relobj
<32, big_endian
>* object
,
1903 unsigned int index
);
1906 // Write out the GOT table.
1908 do_write(Output_file
*);
1911 // This class represent dynamic relocations that need to be applied by
1912 // gold because we are using TLS relocations in a static link.
1916 Static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
1917 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(true)
1918 { this->u_
.global
.symbol
= gsym
; }
1920 Static_reloc(unsigned int got_offset
, unsigned int r_type
,
1921 Sized_relobj
<32, big_endian
>* relobj
, unsigned int index
)
1922 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(false)
1924 this->u_
.local
.relobj
= relobj
;
1925 this->u_
.local
.index
= index
;
1928 // Return the GOT offset.
1931 { return this->got_offset_
; }
1936 { return this->r_type_
; }
1938 // Whether the symbol is global or not.
1940 symbol_is_global() const
1941 { return this->symbol_is_global_
; }
1943 // For a relocation against a global symbol, the global symbol.
1947 gold_assert(this->symbol_is_global_
);
1948 return this->u_
.global
.symbol
;
1951 // For a relocation against a local symbol, the defining object.
1952 Sized_relobj
<32, big_endian
>*
1955 gold_assert(!this->symbol_is_global_
);
1956 return this->u_
.local
.relobj
;
1959 // For a relocation against a local symbol, the local symbol index.
1963 gold_assert(!this->symbol_is_global_
);
1964 return this->u_
.local
.index
;
1968 // GOT offset of the entry to which this relocation is applied.
1969 unsigned int got_offset_
;
1970 // Type of relocation.
1971 unsigned int r_type_
;
1972 // Whether this relocation is against a global symbol.
1973 bool symbol_is_global_
;
1974 // A global or local symbol.
1979 // For a global symbol, the symbol itself.
1984 // For a local symbol, the object defining object.
1985 Sized_relobj
<32, big_endian
>* relobj
;
1986 // For a local symbol, the symbol index.
1992 // Symbol table of the output object.
1993 Symbol_table
* symbol_table_
;
1994 // Layout of the output object.
1996 // Static relocs to be applied to the GOT.
1997 std::vector
<Static_reloc
> static_relocs_
;
2000 // The ARM target has many relocation types with odd-sizes or incontigious
2001 // bits. The default handling of relocatable relocation cannot process these
2002 // relocations. So we have to extend the default code.
2004 template<bool big_endian
, int sh_type
, typename Classify_reloc
>
2005 class Arm_scan_relocatable_relocs
:
2006 public Default_scan_relocatable_relocs
<sh_type
, Classify_reloc
>
2009 // Return the strategy to use for a local symbol which is a section
2010 // symbol, given the relocation type.
2011 inline Relocatable_relocs::Reloc_strategy
2012 local_section_strategy(unsigned int r_type
, Relobj
*)
2014 if (sh_type
== elfcpp::SHT_RELA
)
2015 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
2018 if (r_type
== elfcpp::R_ARM_TARGET1
2019 || r_type
== elfcpp::R_ARM_TARGET2
)
2021 const Target_arm
<big_endian
>* arm_target
=
2022 Target_arm
<big_endian
>::default_target();
2023 r_type
= arm_target
->get_real_reloc_type(r_type
);
2028 // Relocations that write nothing. These exclude R_ARM_TARGET1
2029 // and R_ARM_TARGET2.
2030 case elfcpp::R_ARM_NONE
:
2031 case elfcpp::R_ARM_V4BX
:
2032 case elfcpp::R_ARM_TLS_GOTDESC
:
2033 case elfcpp::R_ARM_TLS_CALL
:
2034 case elfcpp::R_ARM_TLS_DESCSEQ
:
2035 case elfcpp::R_ARM_THM_TLS_CALL
:
2036 case elfcpp::R_ARM_GOTRELAX
:
2037 case elfcpp::R_ARM_GNU_VTENTRY
:
2038 case elfcpp::R_ARM_GNU_VTINHERIT
:
2039 case elfcpp::R_ARM_THM_TLS_DESCSEQ16
:
2040 case elfcpp::R_ARM_THM_TLS_DESCSEQ32
:
2041 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
2042 // These should have been converted to something else above.
2043 case elfcpp::R_ARM_TARGET1
:
2044 case elfcpp::R_ARM_TARGET2
:
2046 // Relocations that write full 32 bits.
2047 case elfcpp::R_ARM_ABS32
:
2048 case elfcpp::R_ARM_REL32
:
2049 case elfcpp::R_ARM_SBREL32
:
2050 case elfcpp::R_ARM_GOTOFF32
:
2051 case elfcpp::R_ARM_BASE_PREL
:
2052 case elfcpp::R_ARM_GOT_BREL
:
2053 case elfcpp::R_ARM_BASE_ABS
:
2054 case elfcpp::R_ARM_ABS32_NOI
:
2055 case elfcpp::R_ARM_REL32_NOI
:
2056 case elfcpp::R_ARM_PLT32_ABS
:
2057 case elfcpp::R_ARM_GOT_ABS
:
2058 case elfcpp::R_ARM_GOT_PREL
:
2059 case elfcpp::R_ARM_TLS_GD32
:
2060 case elfcpp::R_ARM_TLS_LDM32
:
2061 case elfcpp::R_ARM_TLS_LDO32
:
2062 case elfcpp::R_ARM_TLS_IE32
:
2063 case elfcpp::R_ARM_TLS_LE32
:
2064 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
2066 // For all other static relocations, return RELOC_SPECIAL.
2067 return Relocatable_relocs::RELOC_SPECIAL
;
2073 // Utilities for manipulating integers of up to 32-bits
2077 // Sign extend an n-bit unsigned integer stored in an uint32_t into
2078 // an int32_t. NO_BITS must be between 1 to 32.
2079 template<int no_bits
>
2080 static inline int32_t
2081 sign_extend(uint32_t bits
)
2083 gold_assert(no_bits
>= 0 && no_bits
<= 32);
2085 return static_cast<int32_t>(bits
);
2086 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
2088 uint32_t top_bit
= 1U << (no_bits
- 1);
2089 int32_t as_signed
= static_cast<int32_t>(bits
);
2090 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
2093 // Detects overflow of an NO_BITS integer stored in a uint32_t.
2094 template<int no_bits
>
2096 has_overflow(uint32_t bits
)
2098 gold_assert(no_bits
>= 0 && no_bits
<= 32);
2101 int32_t max
= (1 << (no_bits
- 1)) - 1;
2102 int32_t min
= -(1 << (no_bits
- 1));
2103 int32_t as_signed
= static_cast<int32_t>(bits
);
2104 return as_signed
> max
|| as_signed
< min
;
2107 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
2108 // fits in the given number of bits as either a signed or unsigned value.
2109 // For example, has_signed_unsigned_overflow<8> would check
2110 // -128 <= bits <= 255
2111 template<int no_bits
>
2113 has_signed_unsigned_overflow(uint32_t bits
)
2115 gold_assert(no_bits
>= 2 && no_bits
<= 32);
2118 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
2119 int32_t min
= -(1 << (no_bits
- 1));
2120 int32_t as_signed
= static_cast<int32_t>(bits
);
2121 return as_signed
> max
|| as_signed
< min
;
2124 // Select bits from A and B using bits in MASK. For each n in [0..31],
2125 // the n-th bit in the result is chosen from the n-th bits of A and B.
2126 // A zero selects A and a one selects B.
2127 static inline uint32_t
2128 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
2129 { return (a
& ~mask
) | (b
& mask
); }
2132 template<bool big_endian
>
2133 class Target_arm
: public Sized_target
<32, big_endian
>
2136 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
2139 // When were are relocating a stub, we pass this as the relocation number.
2140 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
2143 : Sized_target
<32, big_endian
>(&arm_info
),
2144 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
2145 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
),
2146 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2147 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2148 may_use_blx_(false), should_force_pic_veneer_(false),
2149 arm_input_section_map_(), attributes_section_data_(NULL
),
2150 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2153 // Virtual function which is set to return true by a target if
2154 // it can use relocation types to determine if a function's
2155 // pointer is taken.
2157 can_check_for_function_pointers() const
2160 // Whether a section called SECTION_NAME may have function pointers to
2161 // sections not eligible for safe ICF folding.
2163 section_may_have_icf_unsafe_pointers(const char* section_name
) const
2165 return (!is_prefix_of(".ARM.exidx", section_name
)
2166 && !is_prefix_of(".ARM.extab", section_name
)
2167 && Target::section_may_have_icf_unsafe_pointers(section_name
));
2170 // Whether we can use BLX.
2173 { return this->may_use_blx_
; }
2175 // Set use-BLX flag.
2177 set_may_use_blx(bool value
)
2178 { this->may_use_blx_
= value
; }
2180 // Whether we force PCI branch veneers.
2182 should_force_pic_veneer() const
2183 { return this->should_force_pic_veneer_
; }
2185 // Set PIC veneer flag.
2187 set_should_force_pic_veneer(bool value
)
2188 { this->should_force_pic_veneer_
= value
; }
2190 // Whether we use THUMB-2 instructions.
2192 using_thumb2() const
2194 Object_attribute
* attr
=
2195 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2196 int arch
= attr
->int_value();
2197 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
2200 // Whether we use THUMB/THUMB-2 instructions only.
2202 using_thumb_only() const
2204 Object_attribute
* attr
=
2205 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2207 if (attr
->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2208 || attr
->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M
)
2210 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
2211 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
2213 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
2214 return attr
->int_value() == 'M';
2217 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2219 may_use_arm_nop() const
2221 Object_attribute
* attr
=
2222 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2223 int arch
= attr
->int_value();
2224 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
2225 || arch
== elfcpp::TAG_CPU_ARCH_V6K
2226 || arch
== elfcpp::TAG_CPU_ARCH_V7
2227 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
2230 // Whether we have THUMB-2 NOP.W instruction.
2232 may_use_thumb2_nop() const
2234 Object_attribute
* attr
=
2235 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2236 int arch
= attr
->int_value();
2237 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
2238 || arch
== elfcpp::TAG_CPU_ARCH_V7
2239 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
2242 // Process the relocations to determine unreferenced sections for
2243 // garbage collection.
2245 gc_process_relocs(Symbol_table
* symtab
,
2247 Sized_relobj
<32, big_endian
>* object
,
2248 unsigned int data_shndx
,
2249 unsigned int sh_type
,
2250 const unsigned char* prelocs
,
2252 Output_section
* output_section
,
2253 bool needs_special_offset_handling
,
2254 size_t local_symbol_count
,
2255 const unsigned char* plocal_symbols
);
2257 // Scan the relocations to look for symbol adjustments.
2259 scan_relocs(Symbol_table
* symtab
,
2261 Sized_relobj
<32, big_endian
>* object
,
2262 unsigned int data_shndx
,
2263 unsigned int sh_type
,
2264 const unsigned char* prelocs
,
2266 Output_section
* output_section
,
2267 bool needs_special_offset_handling
,
2268 size_t local_symbol_count
,
2269 const unsigned char* plocal_symbols
);
2271 // Finalize the sections.
2273 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
2275 // Return the value to use for a dynamic symbol which requires special
2278 do_dynsym_value(const Symbol
*) const;
2280 // Relocate a section.
2282 relocate_section(const Relocate_info
<32, big_endian
>*,
2283 unsigned int sh_type
,
2284 const unsigned char* prelocs
,
2286 Output_section
* output_section
,
2287 bool needs_special_offset_handling
,
2288 unsigned char* view
,
2289 Arm_address view_address
,
2290 section_size_type view_size
,
2291 const Reloc_symbol_changes
*);
2293 // Scan the relocs during a relocatable link.
2295 scan_relocatable_relocs(Symbol_table
* symtab
,
2297 Sized_relobj
<32, big_endian
>* object
,
2298 unsigned int data_shndx
,
2299 unsigned int sh_type
,
2300 const unsigned char* prelocs
,
2302 Output_section
* output_section
,
2303 bool needs_special_offset_handling
,
2304 size_t local_symbol_count
,
2305 const unsigned char* plocal_symbols
,
2306 Relocatable_relocs
*);
2308 // Relocate a section during a relocatable link.
2310 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
2311 unsigned int sh_type
,
2312 const unsigned char* prelocs
,
2314 Output_section
* output_section
,
2315 off_t offset_in_output_section
,
2316 const Relocatable_relocs
*,
2317 unsigned char* view
,
2318 Arm_address view_address
,
2319 section_size_type view_size
,
2320 unsigned char* reloc_view
,
2321 section_size_type reloc_view_size
);
2323 // Perform target-specific processing in a relocatable link. This is
2324 // only used if we use the relocation strategy RELOC_SPECIAL.
2326 relocate_special_relocatable(const Relocate_info
<32, big_endian
>* relinfo
,
2327 unsigned int sh_type
,
2328 const unsigned char* preloc_in
,
2330 Output_section
* output_section
,
2331 off_t offset_in_output_section
,
2332 unsigned char* view
,
2333 typename
elfcpp::Elf_types
<32>::Elf_Addr
2335 section_size_type view_size
,
2336 unsigned char* preloc_out
);
2338 // Return whether SYM is defined by the ABI.
2340 do_is_defined_by_abi(Symbol
* sym
) const
2341 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
2343 // Return whether there is a GOT section.
2345 has_got_section() const
2346 { return this->got_
!= NULL
; }
2348 // Return the size of the GOT section.
2352 gold_assert(this->got_
!= NULL
);
2353 return this->got_
->data_size();
2356 // Return the number of entries in the GOT.
2358 got_entry_count() const
2360 if (!this->has_got_section())
2362 return this->got_size() / 4;
2365 // Return the number of entries in the PLT.
2367 plt_entry_count() const;
2369 // Return the offset of the first non-reserved PLT entry.
2371 first_plt_entry_offset() const;
2373 // Return the size of each PLT entry.
2375 plt_entry_size() const;
2377 // Map platform-specific reloc types
2379 get_real_reloc_type(unsigned int r_type
);
2382 // Methods to support stub-generations.
2385 // Return the stub factory
2387 stub_factory() const
2388 { return this->stub_factory_
; }
2390 // Make a new Arm_input_section object.
2391 Arm_input_section
<big_endian
>*
2392 new_arm_input_section(Relobj
*, unsigned int);
2394 // Find the Arm_input_section object corresponding to the SHNDX-th input
2395 // section of RELOBJ.
2396 Arm_input_section
<big_endian
>*
2397 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
2399 // Make a new Stub_table
2400 Stub_table
<big_endian
>*
2401 new_stub_table(Arm_input_section
<big_endian
>*);
2403 // Scan a section for stub generation.
2405 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
2406 const unsigned char*, size_t, Output_section
*,
2407 bool, const unsigned char*, Arm_address
,
2412 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
2413 Output_section
*, unsigned char*, Arm_address
,
2416 // Get the default ARM target.
2417 static Target_arm
<big_endian
>*
2420 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
2421 && parameters
->target().is_big_endian() == big_endian
);
2422 return static_cast<Target_arm
<big_endian
>*>(
2423 parameters
->sized_target
<32, big_endian
>());
2426 // Whether NAME belongs to a mapping symbol.
2428 is_mapping_symbol_name(const char* name
)
2432 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
2433 && (name
[2] == '\0' || name
[2] == '.'));
2436 // Whether we work around the Cortex-A8 erratum.
2438 fix_cortex_a8() const
2439 { return this->fix_cortex_a8_
; }
2441 // Whether we merge exidx entries in debuginfo.
2443 merge_exidx_entries() const
2444 { return parameters
->options().merge_exidx_entries(); }
2446 // Whether we fix R_ARM_V4BX relocation.
2448 // 1 - replace with MOV instruction (armv4 target)
2449 // 2 - make interworking veneer (>= armv4t targets only)
2450 General_options::Fix_v4bx
2452 { return parameters
->options().fix_v4bx(); }
2454 // Scan a span of THUMB code section for Cortex-A8 erratum.
2456 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
2457 section_size_type
, section_size_type
,
2458 const unsigned char*, Arm_address
);
2460 // Apply Cortex-A8 workaround to a branch.
2462 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
2463 unsigned char*, Arm_address
);
2466 // Make an ELF object.
2468 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2469 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
2472 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2473 const elfcpp::Ehdr
<32, !big_endian
>&)
2474 { gold_unreachable(); }
2477 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2478 const elfcpp::Ehdr
<64, false>&)
2479 { gold_unreachable(); }
2482 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2483 const elfcpp::Ehdr
<64, true>&)
2484 { gold_unreachable(); }
2486 // Make an output section.
2488 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
2489 elfcpp::Elf_Xword flags
)
2490 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
2493 do_adjust_elf_header(unsigned char* view
, int len
) const;
2495 // We only need to generate stubs, and hence perform relaxation if we are
2496 // not doing relocatable linking.
2498 do_may_relax() const
2499 { return !parameters
->options().relocatable(); }
2502 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
2504 // Determine whether an object attribute tag takes an integer, a
2507 do_attribute_arg_type(int tag
) const;
2509 // Reorder tags during output.
2511 do_attributes_order(int num
) const;
2513 // This is called when the target is selected as the default.
2515 do_select_as_default_target()
2517 // No locking is required since there should only be one default target.
2518 // We cannot have both the big-endian and little-endian ARM targets
2520 gold_assert(arm_reloc_property_table
== NULL
);
2521 arm_reloc_property_table
= new Arm_reloc_property_table();
2525 // The class which scans relocations.
2530 : issued_non_pic_error_(false)
2534 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2535 Sized_relobj
<32, big_endian
>* object
,
2536 unsigned int data_shndx
,
2537 Output_section
* output_section
,
2538 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2539 const elfcpp::Sym
<32, big_endian
>& lsym
);
2542 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2543 Sized_relobj
<32, big_endian
>* object
,
2544 unsigned int data_shndx
,
2545 Output_section
* output_section
,
2546 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2550 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* , Target_arm
* ,
2551 Sized_relobj
<32, big_endian
>* ,
2554 const elfcpp::Rel
<32, big_endian
>& ,
2556 const elfcpp::Sym
<32, big_endian
>&);
2559 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* , Target_arm
* ,
2560 Sized_relobj
<32, big_endian
>* ,
2563 const elfcpp::Rel
<32, big_endian
>& ,
2564 unsigned int , Symbol
*);
2568 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
2569 unsigned int r_type
);
2572 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
2573 unsigned int r_type
, Symbol
*);
2576 check_non_pic(Relobj
*, unsigned int r_type
);
2578 // Almost identical to Symbol::needs_plt_entry except that it also
2579 // handles STT_ARM_TFUNC.
2581 symbol_needs_plt_entry(const Symbol
* sym
)
2583 // An undefined symbol from an executable does not need a PLT entry.
2584 if (sym
->is_undefined() && !parameters
->options().shared())
2587 return (!parameters
->doing_static_link()
2588 && (sym
->type() == elfcpp::STT_FUNC
2589 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
2590 && (sym
->is_from_dynobj()
2591 || sym
->is_undefined()
2592 || sym
->is_preemptible()));
2596 possible_function_pointer_reloc(unsigned int r_type
);
2598 // Whether we have issued an error about a non-PIC compilation.
2599 bool issued_non_pic_error_
;
2602 // The class which implements relocation.
2612 // Return whether the static relocation needs to be applied.
2614 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2617 Output_section
* output_section
);
2619 // Do a relocation. Return false if the caller should not issue
2620 // any warnings about this relocation.
2622 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
2623 Output_section
*, size_t relnum
,
2624 const elfcpp::Rel
<32, big_endian
>&,
2625 unsigned int r_type
, const Sized_symbol
<32>*,
2626 const Symbol_value
<32>*,
2627 unsigned char*, Arm_address
,
2630 // Return whether we want to pass flag NON_PIC_REF for this
2631 // reloc. This means the relocation type accesses a symbol not via
2634 reloc_is_non_pic(unsigned int r_type
)
2638 // These relocation types reference GOT or PLT entries explicitly.
2639 case elfcpp::R_ARM_GOT_BREL
:
2640 case elfcpp::R_ARM_GOT_ABS
:
2641 case elfcpp::R_ARM_GOT_PREL
:
2642 case elfcpp::R_ARM_GOT_BREL12
:
2643 case elfcpp::R_ARM_PLT32_ABS
:
2644 case elfcpp::R_ARM_TLS_GD32
:
2645 case elfcpp::R_ARM_TLS_LDM32
:
2646 case elfcpp::R_ARM_TLS_IE32
:
2647 case elfcpp::R_ARM_TLS_IE12GP
:
2649 // These relocate types may use PLT entries.
2650 case elfcpp::R_ARM_CALL
:
2651 case elfcpp::R_ARM_THM_CALL
:
2652 case elfcpp::R_ARM_JUMP24
:
2653 case elfcpp::R_ARM_THM_JUMP24
:
2654 case elfcpp::R_ARM_THM_JUMP19
:
2655 case elfcpp::R_ARM_PLT32
:
2656 case elfcpp::R_ARM_THM_XPC22
:
2657 case elfcpp::R_ARM_PREL31
:
2658 case elfcpp::R_ARM_SBREL31
:
2667 // Do a TLS relocation.
2668 inline typename Arm_relocate_functions
<big_endian
>::Status
2669 relocate_tls(const Relocate_info
<32, big_endian
>*, Target_arm
<big_endian
>*,
2670 size_t, const elfcpp::Rel
<32, big_endian
>&, unsigned int,
2671 const Sized_symbol
<32>*, const Symbol_value
<32>*,
2672 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
2677 // A class which returns the size required for a relocation type,
2678 // used while scanning relocs during a relocatable link.
2679 class Relocatable_size_for_reloc
2683 get_size_for_reloc(unsigned int, Relobj
*);
2686 // Adjust TLS relocation type based on the options and whether this
2687 // is a local symbol.
2688 static tls::Tls_optimization
2689 optimize_tls_reloc(bool is_final
, int r_type
);
2691 // Get the GOT section, creating it if necessary.
2692 Arm_output_data_got
<big_endian
>*
2693 got_section(Symbol_table
*, Layout
*);
2695 // Get the GOT PLT section.
2697 got_plt_section() const
2699 gold_assert(this->got_plt_
!= NULL
);
2700 return this->got_plt_
;
2703 // Create a PLT entry for a global symbol.
2705 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
2707 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2709 define_tls_base_symbol(Symbol_table
*, Layout
*);
2711 // Create a GOT entry for the TLS module index.
2713 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2714 Sized_relobj
<32, big_endian
>* object
);
2716 // Get the PLT section.
2717 const Output_data_plt_arm
<big_endian
>*
2720 gold_assert(this->plt_
!= NULL
);
2724 // Get the dynamic reloc section, creating it if necessary.
2726 rel_dyn_section(Layout
*);
2728 // Get the section to use for TLS_DESC relocations.
2730 rel_tls_desc_section(Layout
*) const;
2732 // Return true if the symbol may need a COPY relocation.
2733 // References from an executable object to non-function symbols
2734 // defined in a dynamic object may need a COPY relocation.
2736 may_need_copy_reloc(Symbol
* gsym
)
2738 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
2739 && gsym
->may_need_copy_reloc());
2742 // Add a potential copy relocation.
2744 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
2745 Sized_relobj
<32, big_endian
>* object
,
2746 unsigned int shndx
, Output_section
* output_section
,
2747 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
2749 this->copy_relocs_
.copy_reloc(symtab
, layout
,
2750 symtab
->get_sized_symbol
<32>(sym
),
2751 object
, shndx
, output_section
, reloc
,
2752 this->rel_dyn_section(layout
));
2755 // Whether two EABI versions are compatible.
2757 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
2759 // Merge processor-specific flags from input object and those in the ELF
2760 // header of the output.
2762 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
2764 // Get the secondary compatible architecture.
2766 get_secondary_compatible_arch(const Attributes_section_data
*);
2768 // Set the secondary compatible architecture.
2770 set_secondary_compatible_arch(Attributes_section_data
*, int);
2773 tag_cpu_arch_combine(const char*, int, int*, int, int);
2775 // Helper to print AEABI enum tag value.
2777 aeabi_enum_name(unsigned int);
2779 // Return string value for TAG_CPU_name.
2781 tag_cpu_name_value(unsigned int);
2783 // Merge object attributes from input object and those in the output.
2785 merge_object_attributes(const char*, const Attributes_section_data
*);
2787 // Helper to get an AEABI object attribute
2789 get_aeabi_object_attribute(int tag
) const
2791 Attributes_section_data
* pasd
= this->attributes_section_data_
;
2792 gold_assert(pasd
!= NULL
);
2793 Object_attribute
* attr
=
2794 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
2795 gold_assert(attr
!= NULL
);
2800 // Methods to support stub-generations.
2803 // Group input sections for stub generation.
2805 group_sections(Layout
*, section_size_type
, bool);
2807 // Scan a relocation for stub generation.
2809 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
2810 const Sized_symbol
<32>*, unsigned int,
2811 const Symbol_value
<32>*,
2812 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
2814 // Scan a relocation section for stub.
2815 template<int sh_type
>
2817 scan_reloc_section_for_stubs(
2818 const Relocate_info
<32, big_endian
>* relinfo
,
2819 const unsigned char* prelocs
,
2821 Output_section
* output_section
,
2822 bool needs_special_offset_handling
,
2823 const unsigned char* view
,
2824 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2827 // Fix .ARM.exidx section coverage.
2829 fix_exidx_coverage(Layout
*, const Input_objects
*,
2830 Arm_output_section
<big_endian
>*, Symbol_table
*);
2832 // Functors for STL set.
2833 struct output_section_address_less_than
2836 operator()(const Output_section
* s1
, const Output_section
* s2
) const
2837 { return s1
->address() < s2
->address(); }
2840 // Information about this specific target which we pass to the
2841 // general Target structure.
2842 static const Target::Target_info arm_info
;
2844 // The types of GOT entries needed for this platform.
2845 // These values are exposed to the ABI in an incremental link.
2846 // Do not renumber existing values without changing the version
2847 // number of the .gnu_incremental_inputs section.
2850 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
2851 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
2852 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
2853 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
2854 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
2857 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2859 // Map input section to Arm_input_section.
2860 typedef Unordered_map
<Section_id
,
2861 Arm_input_section
<big_endian
>*,
2863 Arm_input_section_map
;
2865 // Map output addresses to relocs for Cortex-A8 erratum.
2866 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2867 Cortex_a8_relocs_info
;
2870 Arm_output_data_got
<big_endian
>* got_
;
2872 Output_data_plt_arm
<big_endian
>* plt_
;
2873 // The GOT PLT section.
2874 Output_data_space
* got_plt_
;
2875 // The dynamic reloc section.
2876 Reloc_section
* rel_dyn_
;
2877 // Relocs saved to avoid a COPY reloc.
2878 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2879 // Space for variables copied with a COPY reloc.
2880 Output_data_space
* dynbss_
;
2881 // Offset of the GOT entry for the TLS module index.
2882 unsigned int got_mod_index_offset_
;
2883 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2884 bool tls_base_symbol_defined_
;
2885 // Vector of Stub_tables created.
2886 Stub_table_list stub_tables_
;
2888 const Stub_factory
&stub_factory_
;
2889 // Whether we can use BLX.
2891 // Whether we force PIC branch veneers.
2892 bool should_force_pic_veneer_
;
2893 // Map for locating Arm_input_sections.
2894 Arm_input_section_map arm_input_section_map_
;
2895 // Attributes section data in output.
2896 Attributes_section_data
* attributes_section_data_
;
2897 // Whether we want to fix code for Cortex-A8 erratum.
2898 bool fix_cortex_a8_
;
2899 // Map addresses to relocs for Cortex-A8 erratum.
2900 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2903 template<bool big_endian
>
2904 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2907 big_endian
, // is_big_endian
2908 elfcpp::EM_ARM
, // machine_code
2909 false, // has_make_symbol
2910 false, // has_resolve
2911 false, // has_code_fill
2912 true, // is_default_stack_executable
2914 "/usr/lib/libc.so.1", // dynamic_linker
2915 0x8000, // default_text_segment_address
2916 0x1000, // abi_pagesize (overridable by -z max-page-size)
2917 0x1000, // common_pagesize (overridable by -z common-page-size)
2918 elfcpp::SHN_UNDEF
, // small_common_shndx
2919 elfcpp::SHN_UNDEF
, // large_common_shndx
2920 0, // small_common_section_flags
2921 0, // large_common_section_flags
2922 ".ARM.attributes", // attributes_section
2923 "aeabi" // attributes_vendor
2926 // Arm relocate functions class
2929 template<bool big_endian
>
2930 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2935 STATUS_OKAY
, // No error during relocation.
2936 STATUS_OVERFLOW
, // Relocation oveflow.
2937 STATUS_BAD_RELOC
// Relocation cannot be applied.
2941 typedef Relocate_functions
<32, big_endian
> Base
;
2942 typedef Arm_relocate_functions
<big_endian
> This
;
2944 // Encoding of imm16 argument for movt and movw ARM instructions
2947 // imm16 := imm4 | imm12
2949 // 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
2950 // +-------+---------------+-------+-------+-----------------------+
2951 // | | |imm4 | |imm12 |
2952 // +-------+---------------+-------+-------+-----------------------+
2954 // Extract the relocation addend from VAL based on the ARM
2955 // instruction encoding described above.
2956 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2957 extract_arm_movw_movt_addend(
2958 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2960 // According to the Elf ABI for ARM Architecture the immediate
2961 // field is sign-extended to form the addend.
2962 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2965 // Insert X into VAL based on the ARM instruction encoding described
2967 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2968 insert_val_arm_movw_movt(
2969 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2970 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2974 val
|= (x
& 0xf000) << 4;
2978 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2981 // imm16 := imm4 | i | imm3 | imm8
2983 // 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
2984 // +---------+-+-----------+-------++-+-----+-------+---------------+
2985 // | |i| |imm4 || |imm3 | |imm8 |
2986 // +---------+-+-----------+-------++-+-----+-------+---------------+
2988 // Extract the relocation addend from VAL based on the Thumb2
2989 // instruction encoding described above.
2990 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2991 extract_thumb_movw_movt_addend(
2992 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2994 // According to the Elf ABI for ARM Architecture the immediate
2995 // field is sign-extended to form the addend.
2996 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2997 | ((val
>> 15) & 0x0800)
2998 | ((val
>> 4) & 0x0700)
3002 // Insert X into VAL based on the Thumb2 instruction encoding
3004 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
3005 insert_val_thumb_movw_movt(
3006 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
3007 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
3010 val
|= (x
& 0xf000) << 4;
3011 val
|= (x
& 0x0800) << 15;
3012 val
|= (x
& 0x0700) << 4;
3013 val
|= (x
& 0x00ff);
3017 // Calculate the smallest constant Kn for the specified residual.
3018 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3020 calc_grp_kn(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
)
3026 // Determine the most significant bit in the residual and
3027 // align the resulting value to a 2-bit boundary.
3028 for (msb
= 30; (msb
>= 0) && !(residual
& (3 << msb
)); msb
-= 2)
3030 // The desired shift is now (msb - 6), or zero, whichever
3032 return (((msb
- 6) < 0) ? 0 : (msb
- 6));
3035 // Calculate the final residual for the specified group index.
3036 // If the passed group index is less than zero, the method will return
3037 // the value of the specified residual without any change.
3038 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3039 static typename
elfcpp::Swap
<32, big_endian
>::Valtype
3040 calc_grp_residual(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
,
3043 for (int n
= 0; n
<= group
; n
++)
3045 // Calculate which part of the value to mask.
3046 uint32_t shift
= calc_grp_kn(residual
);
3047 // Calculate the residual for the next time around.
3048 residual
&= ~(residual
& (0xff << shift
));
3054 // Calculate the value of Gn for the specified group index.
3055 // We return it in the form of an encoded constant-and-rotation.
3056 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3057 static typename
elfcpp::Swap
<32, big_endian
>::Valtype
3058 calc_grp_gn(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
,
3061 typename
elfcpp::Swap
<32, big_endian
>::Valtype gn
= 0;
3064 for (int n
= 0; n
<= group
; n
++)
3066 // Calculate which part of the value to mask.
3067 shift
= calc_grp_kn(residual
);
3068 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
3069 gn
= residual
& (0xff << shift
);
3070 // Calculate the residual for the next time around.
3073 // Return Gn in the form of an encoded constant-and-rotation.
3074 return ((gn
>> shift
) | ((gn
<= 0xff ? 0 : (32 - shift
) / 2) << 8));
3078 // Handle ARM long branches.
3079 static typename
This::Status
3080 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
3081 unsigned char*, const Sized_symbol
<32>*,
3082 const Arm_relobj
<big_endian
>*, unsigned int,
3083 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
3085 // Handle THUMB long branches.
3086 static typename
This::Status
3087 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
3088 unsigned char*, const Sized_symbol
<32>*,
3089 const Arm_relobj
<big_endian
>*, unsigned int,
3090 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
3093 // Return the branch offset of a 32-bit THUMB branch.
3094 static inline int32_t
3095 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
3097 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
3098 // involving the J1 and J2 bits.
3099 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
3100 uint32_t upper
= upper_insn
& 0x3ffU
;
3101 uint32_t lower
= lower_insn
& 0x7ffU
;
3102 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
3103 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
3104 uint32_t i1
= j1
^ s
? 0 : 1;
3105 uint32_t i2
= j2
^ s
? 0 : 1;
3107 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3108 | (upper
<< 12) | (lower
<< 1));
3111 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
3112 // UPPER_INSN is the original upper instruction of the branch. Caller is
3113 // responsible for overflow checking and BLX offset adjustment.
3114 static inline uint16_t
3115 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
3117 uint32_t s
= offset
< 0 ? 1 : 0;
3118 uint32_t bits
= static_cast<uint32_t>(offset
);
3119 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
3122 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
3123 // LOWER_INSN is the original lower instruction of the branch. Caller is
3124 // responsible for overflow checking and BLX offset adjustment.
3125 static inline uint16_t
3126 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
3128 uint32_t s
= offset
< 0 ? 1 : 0;
3129 uint32_t bits
= static_cast<uint32_t>(offset
);
3130 return ((lower_insn
& ~0x2fffU
)
3131 | ((((bits
>> 23) & 1) ^ !s
) << 13)
3132 | ((((bits
>> 22) & 1) ^ !s
) << 11)
3133 | ((bits
>> 1) & 0x7ffU
));
3136 // Return the branch offset of a 32-bit THUMB conditional branch.
3137 static inline int32_t
3138 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
3140 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
3141 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
3142 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
3143 uint32_t lower
= (lower_insn
& 0x07ffU
);
3144 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
3146 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
3149 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
3150 // instruction. UPPER_INSN is the original upper instruction of the branch.
3151 // Caller is responsible for overflow checking.
3152 static inline uint16_t
3153 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
3155 uint32_t s
= offset
< 0 ? 1 : 0;
3156 uint32_t bits
= static_cast<uint32_t>(offset
);
3157 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
3160 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
3161 // instruction. LOWER_INSN is the original lower instruction of the branch.
3162 // Caller is reponsible for overflow checking.
3163 static inline uint16_t
3164 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
3166 uint32_t bits
= static_cast<uint32_t>(offset
);
3167 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
3168 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
3169 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
3171 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
3174 // R_ARM_ABS8: S + A
3175 static inline typename
This::Status
3176 abs8(unsigned char* view
,
3177 const Sized_relobj
<32, big_endian
>* object
,
3178 const Symbol_value
<32>* psymval
)
3180 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
3181 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3182 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3183 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
3184 Reltype addend
= utils::sign_extend
<8>(val
);
3185 Reltype x
= psymval
->value(object
, addend
);
3186 val
= utils::bit_select(val
, x
, 0xffU
);
3187 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
3189 // R_ARM_ABS8 permits signed or unsigned results.
3190 int signed_x
= static_cast<int32_t>(x
);
3191 return ((signed_x
< -128 || signed_x
> 255)
3192 ? This::STATUS_OVERFLOW
3193 : This::STATUS_OKAY
);
3196 // R_ARM_THM_ABS5: S + A
3197 static inline typename
This::Status
3198 thm_abs5(unsigned char* view
,
3199 const Sized_relobj
<32, big_endian
>* object
,
3200 const Symbol_value
<32>* psymval
)
3202 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3203 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3204 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3205 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3206 Reltype addend
= (val
& 0x7e0U
) >> 6;
3207 Reltype x
= psymval
->value(object
, addend
);
3208 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
3209 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3211 // R_ARM_ABS16 permits signed or unsigned results.
3212 int signed_x
= static_cast<int32_t>(x
);
3213 return ((signed_x
< -32768 || signed_x
> 65535)
3214 ? This::STATUS_OVERFLOW
3215 : This::STATUS_OKAY
);
3218 // R_ARM_ABS12: S + A
3219 static inline typename
This::Status
3220 abs12(unsigned char* view
,
3221 const Sized_relobj
<32, big_endian
>* object
,
3222 const Symbol_value
<32>* psymval
)
3224 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3225 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3226 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3227 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3228 Reltype addend
= val
& 0x0fffU
;
3229 Reltype x
= psymval
->value(object
, addend
);
3230 val
= utils::bit_select(val
, x
, 0x0fffU
);
3231 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3232 return (utils::has_overflow
<12>(x
)
3233 ? This::STATUS_OVERFLOW
3234 : This::STATUS_OKAY
);
3237 // R_ARM_ABS16: S + A
3238 static inline typename
This::Status
3239 abs16(unsigned char* view
,
3240 const Sized_relobj
<32, big_endian
>* object
,
3241 const Symbol_value
<32>* psymval
)
3243 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3244 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3245 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3246 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3247 Reltype addend
= utils::sign_extend
<16>(val
);
3248 Reltype x
= psymval
->value(object
, addend
);
3249 val
= utils::bit_select(val
, x
, 0xffffU
);
3250 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3251 return (utils::has_signed_unsigned_overflow
<16>(x
)
3252 ? This::STATUS_OVERFLOW
3253 : This::STATUS_OKAY
);
3256 // R_ARM_ABS32: (S + A) | T
3257 static inline typename
This::Status
3258 abs32(unsigned char* view
,
3259 const Sized_relobj
<32, big_endian
>* object
,
3260 const Symbol_value
<32>* psymval
,
3261 Arm_address thumb_bit
)
3263 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3264 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3265 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3266 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
3267 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
3268 return This::STATUS_OKAY
;
3271 // R_ARM_REL32: (S + A) | T - P
3272 static inline typename
This::Status
3273 rel32(unsigned char* view
,
3274 const Sized_relobj
<32, big_endian
>* object
,
3275 const Symbol_value
<32>* psymval
,
3276 Arm_address address
,
3277 Arm_address thumb_bit
)
3279 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3280 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3281 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3282 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3283 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
3284 return This::STATUS_OKAY
;
3287 // R_ARM_THM_JUMP24: (S + A) | T - P
3288 static typename
This::Status
3289 thm_jump19(unsigned char* view
, const Arm_relobj
<big_endian
>* object
,
3290 const Symbol_value
<32>* psymval
, Arm_address address
,
3291 Arm_address thumb_bit
);
3293 // R_ARM_THM_JUMP6: S + A – P
3294 static inline typename
This::Status
3295 thm_jump6(unsigned char* view
,
3296 const Sized_relobj
<32, big_endian
>* object
,
3297 const Symbol_value
<32>* psymval
,
3298 Arm_address address
)
3300 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3301 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3302 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3303 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3304 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3305 Reltype addend
= (((val
& 0x0200) >> 3) | ((val
& 0x00f8) >> 2));
3306 Reltype x
= (psymval
->value(object
, addend
) - address
);
3307 val
= (val
& 0xfd07) | ((x
& 0x0040) << 3) | ((val
& 0x003e) << 2);
3308 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3309 // CZB does only forward jumps.
3310 return ((x
> 0x007e)
3311 ? This::STATUS_OVERFLOW
3312 : This::STATUS_OKAY
);
3315 // R_ARM_THM_JUMP8: S + A – P
3316 static inline typename
This::Status
3317 thm_jump8(unsigned char* view
,
3318 const Sized_relobj
<32, big_endian
>* object
,
3319 const Symbol_value
<32>* psymval
,
3320 Arm_address address
)
3322 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3323 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3324 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3325 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3326 Reltype addend
= utils::sign_extend
<8>((val
& 0x00ff) << 1);
3327 Reltype x
= (psymval
->value(object
, addend
) - address
);
3328 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xff00) | ((x
& 0x01fe) >> 1));
3329 return (utils::has_overflow
<8>(x
)
3330 ? This::STATUS_OVERFLOW
3331 : This::STATUS_OKAY
);
3334 // R_ARM_THM_JUMP11: S + A – P
3335 static inline typename
This::Status
3336 thm_jump11(unsigned char* view
,
3337 const Sized_relobj
<32, big_endian
>* object
,
3338 const Symbol_value
<32>* psymval
,
3339 Arm_address address
)
3341 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3342 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3343 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3344 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3345 Reltype addend
= utils::sign_extend
<11>((val
& 0x07ff) << 1);
3346 Reltype x
= (psymval
->value(object
, addend
) - address
);
3347 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xf800) | ((x
& 0x0ffe) >> 1));
3348 return (utils::has_overflow
<11>(x
)
3349 ? This::STATUS_OVERFLOW
3350 : This::STATUS_OKAY
);
3353 // R_ARM_BASE_PREL: B(S) + A - P
3354 static inline typename
This::Status
3355 base_prel(unsigned char* view
,
3357 Arm_address address
)
3359 Base::rel32(view
, origin
- address
);
3363 // R_ARM_BASE_ABS: B(S) + A
3364 static inline typename
This::Status
3365 base_abs(unsigned char* view
,
3368 Base::rel32(view
, origin
);
3372 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3373 static inline typename
This::Status
3374 got_brel(unsigned char* view
,
3375 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
3377 Base::rel32(view
, got_offset
);
3378 return This::STATUS_OKAY
;
3381 // R_ARM_GOT_PREL: GOT(S) + A - P
3382 static inline typename
This::Status
3383 got_prel(unsigned char* view
,
3384 Arm_address got_entry
,
3385 Arm_address address
)
3387 Base::rel32(view
, got_entry
- address
);
3388 return This::STATUS_OKAY
;
3391 // R_ARM_PREL: (S + A) | T - P
3392 static inline typename
This::Status
3393 prel31(unsigned char* view
,
3394 const Sized_relobj
<32, big_endian
>* object
,
3395 const Symbol_value
<32>* psymval
,
3396 Arm_address address
,
3397 Arm_address thumb_bit
)
3399 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3400 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3401 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3402 Valtype addend
= utils::sign_extend
<31>(val
);
3403 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3404 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
3405 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3406 return (utils::has_overflow
<31>(x
) ?
3407 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3410 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3411 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3412 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3413 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3414 static inline typename
This::Status
3415 movw(unsigned char* view
,
3416 const Sized_relobj
<32, big_endian
>* object
,
3417 const Symbol_value
<32>* psymval
,
3418 Arm_address relative_address_base
,
3419 Arm_address thumb_bit
,
3420 bool check_overflow
)
3422 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3423 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3424 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3425 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3426 Valtype x
= ((psymval
->value(object
, addend
) | thumb_bit
)
3427 - relative_address_base
);
3428 val
= This::insert_val_arm_movw_movt(val
, x
);
3429 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3430 return ((check_overflow
&& utils::has_overflow
<16>(x
))
3431 ? This::STATUS_OVERFLOW
3432 : This::STATUS_OKAY
);
3435 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3436 // R_ARM_MOVT_PREL: S + A - P
3437 // R_ARM_MOVT_BREL: S + A - B(S)
3438 static inline typename
This::Status
3439 movt(unsigned char* view
,
3440 const Sized_relobj
<32, big_endian
>* object
,
3441 const Symbol_value
<32>* psymval
,
3442 Arm_address relative_address_base
)
3444 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3445 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3446 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3447 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3448 Valtype x
= (psymval
->value(object
, addend
) - relative_address_base
) >> 16;
3449 val
= This::insert_val_arm_movw_movt(val
, x
);
3450 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3451 // FIXME: IHI0044D says that we should check for overflow.
3452 return This::STATUS_OKAY
;
3455 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3456 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3457 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3458 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3459 static inline typename
This::Status
3460 thm_movw(unsigned char* view
,
3461 const Sized_relobj
<32, big_endian
>* object
,
3462 const Symbol_value
<32>* psymval
,
3463 Arm_address relative_address_base
,
3464 Arm_address thumb_bit
,
3465 bool check_overflow
)
3467 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3468 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3469 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3470 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3471 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3472 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3474 (psymval
->value(object
, addend
) | thumb_bit
) - relative_address_base
;
3475 val
= This::insert_val_thumb_movw_movt(val
, x
);
3476 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3477 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3478 return ((check_overflow
&& utils::has_overflow
<16>(x
))
3479 ? This::STATUS_OVERFLOW
3480 : This::STATUS_OKAY
);
3483 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3484 // R_ARM_THM_MOVT_PREL: S + A - P
3485 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3486 static inline typename
This::Status
3487 thm_movt(unsigned char* view
,
3488 const Sized_relobj
<32, big_endian
>* object
,
3489 const Symbol_value
<32>* psymval
,
3490 Arm_address relative_address_base
)
3492 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3493 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3494 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3495 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3496 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3497 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3498 Reltype x
= (psymval
->value(object
, addend
) - relative_address_base
) >> 16;
3499 val
= This::insert_val_thumb_movw_movt(val
, x
);
3500 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3501 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3502 return This::STATUS_OKAY
;
3505 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3506 static inline typename
This::Status
3507 thm_alu11(unsigned char* view
,
3508 const Sized_relobj
<32, big_endian
>* object
,
3509 const Symbol_value
<32>* psymval
,
3510 Arm_address address
,
3511 Arm_address thumb_bit
)
3513 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3514 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3515 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3516 Reltype insn
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3517 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3519 // 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
3520 // -----------------------------------------------------------------------
3521 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3522 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3523 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3524 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3525 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3526 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3528 // Determine a sign for the addend.
3529 const int sign
= ((insn
& 0xf8ef0000) == 0xf0ad0000
3530 || (insn
& 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3531 // Thumb2 addend encoding:
3532 // imm12 := i | imm3 | imm8
3533 int32_t addend
= (insn
& 0xff)
3534 | ((insn
& 0x00007000) >> 4)
3535 | ((insn
& 0x04000000) >> 15);
3536 // Apply a sign to the added.
3539 int32_t x
= (psymval
->value(object
, addend
) | thumb_bit
)
3540 - (address
& 0xfffffffc);
3541 Reltype val
= abs(x
);
3542 // Mask out the value and a distinct part of the ADD/SUB opcode
3543 // (bits 7:5 of opword).
3544 insn
= (insn
& 0xfb0f8f00)
3546 | ((val
& 0x700) << 4)
3547 | ((val
& 0x800) << 15);
3548 // Set the opcode according to whether the value to go in the
3549 // place is negative.
3553 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
>> 16);
3554 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, insn
& 0xffff);
3555 return ((val
> 0xfff) ?
3556 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3559 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3560 static inline typename
This::Status
3561 thm_pc8(unsigned char* view
,
3562 const Sized_relobj
<32, big_endian
>* object
,
3563 const Symbol_value
<32>* psymval
,
3564 Arm_address address
)
3566 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3567 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3568 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3569 Valtype insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3570 Reltype addend
= ((insn
& 0x00ff) << 2);
3571 int32_t x
= (psymval
->value(object
, addend
) - (address
& 0xfffffffc));
3572 Reltype val
= abs(x
);
3573 insn
= (insn
& 0xff00) | ((val
& 0x03fc) >> 2);
3575 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
);
3576 return ((val
> 0x03fc)
3577 ? This::STATUS_OVERFLOW
3578 : This::STATUS_OKAY
);
3581 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3582 static inline typename
This::Status
3583 thm_pc12(unsigned char* view
,
3584 const Sized_relobj
<32, big_endian
>* object
,
3585 const Symbol_value
<32>* psymval
,
3586 Arm_address address
)
3588 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3589 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3590 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3591 Reltype insn
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3592 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3593 // Determine a sign for the addend (positive if the U bit is 1).
3594 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3595 int32_t addend
= (insn
& 0xfff);
3596 // Apply a sign to the added.
3599 int32_t x
= (psymval
->value(object
, addend
) - (address
& 0xfffffffc));
3600 Reltype val
= abs(x
);
3601 // Mask out and apply the value and the U bit.
3602 insn
= (insn
& 0xff7ff000) | (val
& 0xfff);
3603 // Set the U bit according to whether the value to go in the
3604 // place is positive.
3608 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
>> 16);
3609 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, insn
& 0xffff);
3610 return ((val
> 0xfff) ?
3611 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3615 static inline typename
This::Status
3616 v4bx(const Relocate_info
<32, big_endian
>* relinfo
,
3617 unsigned char* view
,
3618 const Arm_relobj
<big_endian
>* object
,
3619 const Arm_address address
,
3620 const bool is_interworking
)
3623 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3624 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3625 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3627 // Ensure that we have a BX instruction.
3628 gold_assert((val
& 0x0ffffff0) == 0x012fff10);
3629 const uint32_t reg
= (val
& 0xf);
3630 if (is_interworking
&& reg
!= 0xf)
3632 Stub_table
<big_endian
>* stub_table
=
3633 object
->stub_table(relinfo
->data_shndx
);
3634 gold_assert(stub_table
!= NULL
);
3636 Arm_v4bx_stub
* stub
= stub_table
->find_arm_v4bx_stub(reg
);
3637 gold_assert(stub
!= NULL
);
3639 int32_t veneer_address
=
3640 stub_table
->address() + stub
->offset() - 8 - address
;
3641 gold_assert((veneer_address
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3642 && (veneer_address
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3643 // Replace with a branch to veneer (B <addr>)
3644 val
= (val
& 0xf0000000) | 0x0a000000
3645 | ((veneer_address
>> 2) & 0x00ffffff);
3649 // Preserve Rm (lowest four bits) and the condition code
3650 // (highest four bits). Other bits encode MOV PC,Rm.
3651 val
= (val
& 0xf000000f) | 0x01a0f000;
3653 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3654 return This::STATUS_OKAY
;
3657 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3658 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3659 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3660 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3661 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3662 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3663 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3664 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3665 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3666 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3667 static inline typename
This::Status
3668 arm_grp_alu(unsigned char* view
,
3669 const Sized_relobj
<32, big_endian
>* object
,
3670 const Symbol_value
<32>* psymval
,
3672 Arm_address address
,
3673 Arm_address thumb_bit
,
3674 bool check_overflow
)
3676 gold_assert(group
>= 0 && group
< 3);
3677 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3678 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3679 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3681 // ALU group relocations are allowed only for the ADD/SUB instructions.
3682 // (0x00800000 - ADD, 0x00400000 - SUB)
3683 const Valtype opcode
= insn
& 0x01e00000;
3684 if (opcode
!= 0x00800000 && opcode
!= 0x00400000)
3685 return This::STATUS_BAD_RELOC
;
3687 // Determine a sign for the addend.
3688 const int sign
= (opcode
== 0x00800000) ? 1 : -1;
3689 // shifter = rotate_imm * 2
3690 const uint32_t shifter
= (insn
& 0xf00) >> 7;
3691 // Initial addend value.
3692 int32_t addend
= insn
& 0xff;
3693 // Rotate addend right by shifter.
3694 addend
= (addend
>> shifter
) | (addend
<< (32 - shifter
));
3695 // Apply a sign to the added.
3698 int32_t x
= ((psymval
->value(object
, addend
) | thumb_bit
) - address
);
3699 Valtype gn
= Arm_relocate_functions::calc_grp_gn(abs(x
), group
);
3700 // Check for overflow if required
3702 && (Arm_relocate_functions::calc_grp_residual(abs(x
), group
) != 0))
3703 return This::STATUS_OVERFLOW
;
3705 // Mask out the value and the ADD/SUB part of the opcode; take care
3706 // not to destroy the S bit.
3708 // Set the opcode according to whether the value to go in the
3709 // place is negative.
3710 insn
|= ((x
< 0) ? 0x00400000 : 0x00800000);
3711 // Encode the offset (encoded Gn).
3714 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3715 return This::STATUS_OKAY
;
3718 // R_ARM_LDR_PC_G0: S + A - P
3719 // R_ARM_LDR_PC_G1: S + A - P
3720 // R_ARM_LDR_PC_G2: S + A - P
3721 // R_ARM_LDR_SB_G0: S + A - B(S)
3722 // R_ARM_LDR_SB_G1: S + A - B(S)
3723 // R_ARM_LDR_SB_G2: S + A - B(S)
3724 static inline typename
This::Status
3725 arm_grp_ldr(unsigned char* view
,
3726 const Sized_relobj
<32, big_endian
>* object
,
3727 const Symbol_value
<32>* psymval
,
3729 Arm_address address
)
3731 gold_assert(group
>= 0 && group
< 3);
3732 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3733 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3734 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3736 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3737 int32_t addend
= (insn
& 0xfff) * sign
;
3738 int32_t x
= (psymval
->value(object
, addend
) - address
);
3739 // Calculate the relevant G(n-1) value to obtain this stage residual.
3741 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3742 if (residual
>= 0x1000)
3743 return This::STATUS_OVERFLOW
;
3745 // Mask out the value and U bit.
3747 // Set the U bit for non-negative values.
3752 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3753 return This::STATUS_OKAY
;
3756 // R_ARM_LDRS_PC_G0: S + A - P
3757 // R_ARM_LDRS_PC_G1: S + A - P
3758 // R_ARM_LDRS_PC_G2: S + A - P
3759 // R_ARM_LDRS_SB_G0: S + A - B(S)
3760 // R_ARM_LDRS_SB_G1: S + A - B(S)
3761 // R_ARM_LDRS_SB_G2: S + A - B(S)
3762 static inline typename
This::Status
3763 arm_grp_ldrs(unsigned char* view
,
3764 const Sized_relobj
<32, big_endian
>* object
,
3765 const Symbol_value
<32>* psymval
,
3767 Arm_address address
)
3769 gold_assert(group
>= 0 && group
< 3);
3770 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3771 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3772 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3774 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3775 int32_t addend
= (((insn
& 0xf00) >> 4) + (insn
& 0xf)) * sign
;
3776 int32_t x
= (psymval
->value(object
, addend
) - address
);
3777 // Calculate the relevant G(n-1) value to obtain this stage residual.
3779 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3780 if (residual
>= 0x100)
3781 return This::STATUS_OVERFLOW
;
3783 // Mask out the value and U bit.
3785 // Set the U bit for non-negative values.
3788 insn
|= ((residual
& 0xf0) << 4) | (residual
& 0xf);
3790 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3791 return This::STATUS_OKAY
;
3794 // R_ARM_LDC_PC_G0: S + A - P
3795 // R_ARM_LDC_PC_G1: S + A - P
3796 // R_ARM_LDC_PC_G2: S + A - P
3797 // R_ARM_LDC_SB_G0: S + A - B(S)
3798 // R_ARM_LDC_SB_G1: S + A - B(S)
3799 // R_ARM_LDC_SB_G2: S + A - B(S)
3800 static inline typename
This::Status
3801 arm_grp_ldc(unsigned char* view
,
3802 const Sized_relobj
<32, big_endian
>* object
,
3803 const Symbol_value
<32>* psymval
,
3805 Arm_address address
)
3807 gold_assert(group
>= 0 && group
< 3);
3808 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3809 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3810 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3812 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3813 int32_t addend
= ((insn
& 0xff) << 2) * sign
;
3814 int32_t x
= (psymval
->value(object
, addend
) - address
);
3815 // Calculate the relevant G(n-1) value to obtain this stage residual.
3817 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3818 if ((residual
& 0x3) != 0 || residual
>= 0x400)
3819 return This::STATUS_OVERFLOW
;
3821 // Mask out the value and U bit.
3823 // Set the U bit for non-negative values.
3826 insn
|= (residual
>> 2);
3828 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3829 return This::STATUS_OKAY
;
3833 // Relocate ARM long branches. This handles relocation types
3834 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3835 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3836 // undefined and we do not use PLT in this relocation. In such a case,
3837 // the branch is converted into an NOP.
3839 template<bool big_endian
>
3840 typename Arm_relocate_functions
<big_endian
>::Status
3841 Arm_relocate_functions
<big_endian
>::arm_branch_common(
3842 unsigned int r_type
,
3843 const Relocate_info
<32, big_endian
>* relinfo
,
3844 unsigned char* view
,
3845 const Sized_symbol
<32>* gsym
,
3846 const Arm_relobj
<big_endian
>* object
,
3848 const Symbol_value
<32>* psymval
,
3849 Arm_address address
,
3850 Arm_address thumb_bit
,
3851 bool is_weakly_undefined_without_plt
)
3853 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3854 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3855 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3857 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
3858 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
3859 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
3860 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
3861 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
3862 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
3863 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
3865 // Check that the instruction is valid.
3866 if (r_type
== elfcpp::R_ARM_CALL
)
3868 if (!insn_is_uncond_bl
&& !insn_is_blx
)
3869 return This::STATUS_BAD_RELOC
;
3871 else if (r_type
== elfcpp::R_ARM_JUMP24
)
3873 if (!insn_is_b
&& !insn_is_cond_bl
)
3874 return This::STATUS_BAD_RELOC
;
3876 else if (r_type
== elfcpp::R_ARM_PLT32
)
3878 if (!insn_is_any_branch
)
3879 return This::STATUS_BAD_RELOC
;
3881 else if (r_type
== elfcpp::R_ARM_XPC25
)
3883 // FIXME: AAELF document IH0044C does not say much about it other
3884 // than it being obsolete.
3885 if (!insn_is_any_branch
)
3886 return This::STATUS_BAD_RELOC
;
3891 // A branch to an undefined weak symbol is turned into a jump to
3892 // the next instruction unless a PLT entry will be created.
3893 // Do the same for local undefined symbols.
3894 // The jump to the next instruction is optimized as a NOP depending
3895 // on the architecture.
3896 const Target_arm
<big_endian
>* arm_target
=
3897 Target_arm
<big_endian
>::default_target();
3898 if (is_weakly_undefined_without_plt
)
3900 gold_assert(!parameters
->options().relocatable());
3901 Valtype cond
= val
& 0xf0000000U
;
3902 if (arm_target
->may_use_arm_nop())
3903 val
= cond
| 0x0320f000;
3905 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3906 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3907 return This::STATUS_OKAY
;
3910 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
3911 Valtype branch_target
= psymval
->value(object
, addend
);
3912 int32_t branch_offset
= branch_target
- address
;
3914 // We need a stub if the branch offset is too large or if we need
3916 bool may_use_blx
= arm_target
->may_use_blx();
3917 Reloc_stub
* stub
= NULL
;
3919 if (!parameters
->options().relocatable()
3920 && (utils::has_overflow
<26>(branch_offset
)
3921 || ((thumb_bit
!= 0)
3922 && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
))))
3924 Valtype unadjusted_branch_target
= psymval
->value(object
, 0);
3926 Stub_type stub_type
=
3927 Reloc_stub::stub_type_for_reloc(r_type
, address
,
3928 unadjusted_branch_target
,
3930 if (stub_type
!= arm_stub_none
)
3932 Stub_table
<big_endian
>* stub_table
=
3933 object
->stub_table(relinfo
->data_shndx
);
3934 gold_assert(stub_table
!= NULL
);
3936 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3937 stub
= stub_table
->find_reloc_stub(stub_key
);
3938 gold_assert(stub
!= NULL
);
3939 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3940 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3941 branch_offset
= branch_target
- address
;
3942 gold_assert(!utils::has_overflow
<26>(branch_offset
));
3946 // At this point, if we still need to switch mode, the instruction
3947 // must either be a BLX or a BL that can be converted to a BLX.
3951 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
3952 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
3955 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
3956 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3957 return (utils::has_overflow
<26>(branch_offset
)
3958 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3961 // Relocate THUMB long branches. This handles relocation types
3962 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3963 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3964 // undefined and we do not use PLT in this relocation. In such a case,
3965 // the branch is converted into an NOP.
3967 template<bool big_endian
>
3968 typename Arm_relocate_functions
<big_endian
>::Status
3969 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
3970 unsigned int r_type
,
3971 const Relocate_info
<32, big_endian
>* relinfo
,
3972 unsigned char* view
,
3973 const Sized_symbol
<32>* gsym
,
3974 const Arm_relobj
<big_endian
>* object
,
3976 const Symbol_value
<32>* psymval
,
3977 Arm_address address
,
3978 Arm_address thumb_bit
,
3979 bool is_weakly_undefined_without_plt
)
3981 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3982 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3983 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3984 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3986 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3988 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
3989 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
3991 // Check that the instruction is valid.
3992 if (r_type
== elfcpp::R_ARM_THM_CALL
)
3994 if (!is_bl_insn
&& !is_blx_insn
)
3995 return This::STATUS_BAD_RELOC
;
3997 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
3999 // This cannot be a BLX.
4001 return This::STATUS_BAD_RELOC
;
4003 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
4005 // Check for Thumb to Thumb call.
4007 return This::STATUS_BAD_RELOC
;
4010 gold_warning(_("%s: Thumb BLX instruction targets "
4011 "thumb function '%s'."),
4012 object
->name().c_str(),
4013 (gsym
? gsym
->name() : "(local)"));
4014 // Convert BLX to BL.
4015 lower_insn
|= 0x1000U
;
4021 // A branch to an undefined weak symbol is turned into a jump to
4022 // the next instruction unless a PLT entry will be created.
4023 // The jump to the next instruction is optimized as a NOP.W for
4024 // Thumb-2 enabled architectures.
4025 const Target_arm
<big_endian
>* arm_target
=
4026 Target_arm
<big_endian
>::default_target();
4027 if (is_weakly_undefined_without_plt
)
4029 gold_assert(!parameters
->options().relocatable());
4030 if (arm_target
->may_use_thumb2_nop())
4032 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
4033 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
4037 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
4038 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
4040 return This::STATUS_OKAY
;
4043 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
4044 Arm_address branch_target
= psymval
->value(object
, addend
);
4046 // For BLX, bit 1 of target address comes from bit 1 of base address.
4047 bool may_use_blx
= arm_target
->may_use_blx();
4048 if (thumb_bit
== 0 && may_use_blx
)
4049 branch_target
= utils::bit_select(branch_target
, address
, 0x2);
4051 int32_t branch_offset
= branch_target
- address
;
4053 // We need a stub if the branch offset is too large or if we need
4055 bool thumb2
= arm_target
->using_thumb2();
4056 if (!parameters
->options().relocatable()
4057 && ((!thumb2
&& utils::has_overflow
<23>(branch_offset
))
4058 || (thumb2
&& utils::has_overflow
<25>(branch_offset
))
4059 || ((thumb_bit
== 0)
4060 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
4061 || r_type
== elfcpp::R_ARM_THM_JUMP24
))))
4063 Arm_address unadjusted_branch_target
= psymval
->value(object
, 0);
4065 Stub_type stub_type
=
4066 Reloc_stub::stub_type_for_reloc(r_type
, address
,
4067 unadjusted_branch_target
,
4070 if (stub_type
!= arm_stub_none
)
4072 Stub_table
<big_endian
>* stub_table
=
4073 object
->stub_table(relinfo
->data_shndx
);
4074 gold_assert(stub_table
!= NULL
);
4076 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
4077 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
4078 gold_assert(stub
!= NULL
);
4079 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
4080 branch_target
= stub_table
->address() + stub
->offset() + addend
;
4081 if (thumb_bit
== 0 && may_use_blx
)
4082 branch_target
= utils::bit_select(branch_target
, address
, 0x2);
4083 branch_offset
= branch_target
- address
;
4087 // At this point, if we still need to switch mode, the instruction
4088 // must either be a BLX or a BL that can be converted to a BLX.
4091 gold_assert(may_use_blx
4092 && (r_type
== elfcpp::R_ARM_THM_CALL
4093 || r_type
== elfcpp::R_ARM_THM_XPC22
));
4094 // Make sure this is a BLX.
4095 lower_insn
&= ~0x1000U
;
4099 // Make sure this is a BL.
4100 lower_insn
|= 0x1000U
;
4103 // For a BLX instruction, make sure that the relocation is rounded up
4104 // to a word boundary. This follows the semantics of the instruction
4105 // which specifies that bit 1 of the target address will come from bit
4106 // 1 of the base address.
4107 if ((lower_insn
& 0x5000U
) == 0x4000U
)
4108 gold_assert((branch_offset
& 3) == 0);
4110 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
4111 // We use the Thumb-2 encoding, which is safe even if dealing with
4112 // a Thumb-1 instruction by virtue of our overflow check above. */
4113 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
4114 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
4116 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
4117 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
4119 gold_assert(!utils::has_overflow
<25>(branch_offset
));
4122 ? utils::has_overflow
<25>(branch_offset
)
4123 : utils::has_overflow
<23>(branch_offset
))
4124 ? This::STATUS_OVERFLOW
4125 : This::STATUS_OKAY
);
4128 // Relocate THUMB-2 long conditional branches.
4129 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
4130 // undefined and we do not use PLT in this relocation. In such a case,
4131 // the branch is converted into an NOP.
4133 template<bool big_endian
>
4134 typename Arm_relocate_functions
<big_endian
>::Status
4135 Arm_relocate_functions
<big_endian
>::thm_jump19(
4136 unsigned char* view
,
4137 const Arm_relobj
<big_endian
>* object
,
4138 const Symbol_value
<32>* psymval
,
4139 Arm_address address
,
4140 Arm_address thumb_bit
)
4142 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
4143 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4144 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4145 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
4146 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
4148 Arm_address branch_target
= psymval
->value(object
, addend
);
4149 int32_t branch_offset
= branch_target
- address
;
4151 // ??? Should handle interworking? GCC might someday try to
4152 // use this for tail calls.
4153 // FIXME: We do support thumb entry to PLT yet.
4156 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
4157 return This::STATUS_BAD_RELOC
;
4160 // Put RELOCATION back into the insn.
4161 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
4162 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
4164 // Put the relocated value back in the object file:
4165 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
4166 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
4168 return (utils::has_overflow
<21>(branch_offset
)
4169 ? This::STATUS_OVERFLOW
4170 : This::STATUS_OKAY
);
4173 // Get the GOT section, creating it if necessary.
4175 template<bool big_endian
>
4176 Arm_output_data_got
<big_endian
>*
4177 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
4179 if (this->got_
== NULL
)
4181 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
4183 this->got_
= new Arm_output_data_got
<big_endian
>(symtab
, layout
);
4185 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
4186 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
4187 this->got_
, ORDER_DATA
, false);
4189 // The old GNU linker creates a .got.plt section. We just
4190 // create another set of data in the .got section. Note that we
4191 // always create a PLT if we create a GOT, although the PLT
4193 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
4194 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
4195 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
4196 this->got_plt_
, ORDER_DATA
, false);
4198 // The first three entries are reserved.
4199 this->got_plt_
->set_current_data_size(3 * 4);
4201 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4202 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
4203 Symbol_table::PREDEFINED
,
4205 0, 0, elfcpp::STT_OBJECT
,
4207 elfcpp::STV_HIDDEN
, 0,
4213 // Get the dynamic reloc section, creating it if necessary.
4215 template<bool big_endian
>
4216 typename Target_arm
<big_endian
>::Reloc_section
*
4217 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
4219 if (this->rel_dyn_
== NULL
)
4221 gold_assert(layout
!= NULL
);
4222 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
4223 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
4224 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
4225 ORDER_DYNAMIC_RELOCS
, false);
4227 return this->rel_dyn_
;
4230 // Insn_template methods.
4232 // Return byte size of an instruction template.
4235 Insn_template::size() const
4237 switch (this->type())
4240 case THUMB16_SPECIAL_TYPE
:
4251 // Return alignment of an instruction template.
4254 Insn_template::alignment() const
4256 switch (this->type())
4259 case THUMB16_SPECIAL_TYPE
:
4270 // Stub_template methods.
4272 Stub_template::Stub_template(
4273 Stub_type type
, const Insn_template
* insns
,
4275 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
4276 entry_in_thumb_mode_(false), relocs_()
4280 // Compute byte size and alignment of stub template.
4281 for (size_t i
= 0; i
< insn_count
; i
++)
4283 unsigned insn_alignment
= insns
[i
].alignment();
4284 size_t insn_size
= insns
[i
].size();
4285 gold_assert((offset
& (insn_alignment
- 1)) == 0);
4286 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
4287 switch (insns
[i
].type())
4289 case Insn_template::THUMB16_TYPE
:
4290 case Insn_template::THUMB16_SPECIAL_TYPE
:
4292 this->entry_in_thumb_mode_
= true;
4295 case Insn_template::THUMB32_TYPE
:
4296 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
4297 this->relocs_
.push_back(Reloc(i
, offset
));
4299 this->entry_in_thumb_mode_
= true;
4302 case Insn_template::ARM_TYPE
:
4303 // Handle cases where the target is encoded within the
4305 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
4306 this->relocs_
.push_back(Reloc(i
, offset
));
4309 case Insn_template::DATA_TYPE
:
4310 // Entry point cannot be data.
4311 gold_assert(i
!= 0);
4312 this->relocs_
.push_back(Reloc(i
, offset
));
4318 offset
+= insn_size
;
4320 this->size_
= offset
;
4325 // Template to implement do_write for a specific target endianness.
4327 template<bool big_endian
>
4329 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
4331 const Stub_template
* stub_template
= this->stub_template();
4332 const Insn_template
* insns
= stub_template
->insns();
4334 // FIXME: We do not handle BE8 encoding yet.
4335 unsigned char* pov
= view
;
4336 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
4338 switch (insns
[i
].type())
4340 case Insn_template::THUMB16_TYPE
:
4341 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
4343 case Insn_template::THUMB16_SPECIAL_TYPE
:
4344 elfcpp::Swap
<16, big_endian
>::writeval(
4346 this->thumb16_special(i
));
4348 case Insn_template::THUMB32_TYPE
:
4350 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
4351 uint32_t lo
= insns
[i
].data() & 0xffff;
4352 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
4353 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
4356 case Insn_template::ARM_TYPE
:
4357 case Insn_template::DATA_TYPE
:
4358 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
4363 pov
+= insns
[i
].size();
4365 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
4368 // Reloc_stub::Key methods.
4370 // Dump a Key as a string for debugging.
4373 Reloc_stub::Key::name() const
4375 if (this->r_sym_
== invalid_index
)
4377 // Global symbol key name
4378 // <stub-type>:<symbol name>:<addend>.
4379 const std::string sym_name
= this->u_
.symbol
->name();
4380 // We need to print two hex number and two colons. So just add 100 bytes
4381 // to the symbol name size.
4382 size_t len
= sym_name
.size() + 100;
4383 char* buffer
= new char[len
];
4384 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
4385 sym_name
.c_str(), this->addend_
);
4386 gold_assert(c
> 0 && c
< static_cast<int>(len
));
4388 return std::string(buffer
);
4392 // local symbol key name
4393 // <stub-type>:<object>:<r_sym>:<addend>.
4394 const size_t len
= 200;
4396 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
4397 this->u_
.relobj
, this->r_sym_
, this->addend_
);
4398 gold_assert(c
> 0 && c
< static_cast<int>(len
));
4399 return std::string(buffer
);
4403 // Reloc_stub methods.
4405 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4406 // LOCATION to DESTINATION.
4407 // This code is based on the arm_type_of_stub function in
4408 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4412 Reloc_stub::stub_type_for_reloc(
4413 unsigned int r_type
,
4414 Arm_address location
,
4415 Arm_address destination
,
4416 bool target_is_thumb
)
4418 Stub_type stub_type
= arm_stub_none
;
4420 // This is a bit ugly but we want to avoid using a templated class for
4421 // big and little endianities.
4423 bool should_force_pic_veneer
;
4426 if (parameters
->target().is_big_endian())
4428 const Target_arm
<true>* big_endian_target
=
4429 Target_arm
<true>::default_target();
4430 may_use_blx
= big_endian_target
->may_use_blx();
4431 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
4432 thumb2
= big_endian_target
->using_thumb2();
4433 thumb_only
= big_endian_target
->using_thumb_only();
4437 const Target_arm
<false>* little_endian_target
=
4438 Target_arm
<false>::default_target();
4439 may_use_blx
= little_endian_target
->may_use_blx();
4440 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
4441 thumb2
= little_endian_target
->using_thumb2();
4442 thumb_only
= little_endian_target
->using_thumb_only();
4445 int64_t branch_offset
;
4446 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
4448 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4449 // base address (instruction address + 4).
4450 if ((r_type
== elfcpp::R_ARM_THM_CALL
) && may_use_blx
&& !target_is_thumb
)
4451 destination
= utils::bit_select(destination
, location
, 0x2);
4452 branch_offset
= static_cast<int64_t>(destination
) - location
;
4454 // Handle cases where:
4455 // - this call goes too far (different Thumb/Thumb2 max
4457 // - it's a Thumb->Arm call and blx is not available, or it's a
4458 // Thumb->Arm branch (not bl). A stub is needed in this case.
4460 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
4461 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
4463 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
4464 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
4465 || ((!target_is_thumb
)
4466 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
4467 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
4469 if (target_is_thumb
)
4474 stub_type
= (parameters
->options().shared()
4475 || should_force_pic_veneer
)
4478 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4479 // V5T and above. Stub starts with ARM code, so
4480 // we must be able to switch mode before
4481 // reaching it, which is only possible for 'bl'
4482 // (ie R_ARM_THM_CALL relocation).
4483 ? arm_stub_long_branch_any_thumb_pic
4484 // On V4T, use Thumb code only.
4485 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
4489 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4490 ? arm_stub_long_branch_any_any
// V5T and above.
4491 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
4495 stub_type
= (parameters
->options().shared()
4496 || should_force_pic_veneer
)
4497 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
4498 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
4505 // FIXME: We should check that the input section is from an
4506 // object that has interwork enabled.
4508 stub_type
= (parameters
->options().shared()
4509 || should_force_pic_veneer
)
4512 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4513 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
4514 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
4518 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4519 ? arm_stub_long_branch_any_any
// V5T and above.
4520 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
4522 // Handle v4t short branches.
4523 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
4524 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
4525 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
4526 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
4530 else if (r_type
== elfcpp::R_ARM_CALL
4531 || r_type
== elfcpp::R_ARM_JUMP24
4532 || r_type
== elfcpp::R_ARM_PLT32
)
4534 branch_offset
= static_cast<int64_t>(destination
) - location
;
4535 if (target_is_thumb
)
4539 // FIXME: We should check that the input section is from an
4540 // object that has interwork enabled.
4542 // We have an extra 2-bytes reach because of
4543 // the mode change (bit 24 (H) of BLX encoding).
4544 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
4545 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
4546 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
4547 || (r_type
== elfcpp::R_ARM_JUMP24
)
4548 || (r_type
== elfcpp::R_ARM_PLT32
))
4550 stub_type
= (parameters
->options().shared()
4551 || should_force_pic_veneer
)
4554 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
4555 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
4559 ? arm_stub_long_branch_any_any
// V5T and above.
4560 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
4566 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
4567 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
4569 stub_type
= (parameters
->options().shared()
4570 || should_force_pic_veneer
)
4571 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
4572 : arm_stub_long_branch_any_any
; /// non-PIC.
4580 // Cortex_a8_stub methods.
4582 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4583 // I is the position of the instruction template in the stub template.
4586 Cortex_a8_stub::do_thumb16_special(size_t i
)
4588 // The only use of this is to copy condition code from a conditional
4589 // branch being worked around to the corresponding conditional branch in
4591 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4593 uint16_t data
= this->stub_template()->insns()[i
].data();
4594 gold_assert((data
& 0xff00U
) == 0xd000U
);
4595 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
4599 // Stub_factory methods.
4601 Stub_factory::Stub_factory()
4603 // The instruction template sequences are declared as static
4604 // objects and initialized first time the constructor runs.
4606 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4607 // to reach the stub if necessary.
4608 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
4610 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4611 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4612 // dcd R_ARM_ABS32(X)
4615 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4617 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
4619 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4620 Insn_template::arm_insn(0xe12fff1c), // bx ip
4621 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4622 // dcd R_ARM_ABS32(X)
4625 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4626 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
4628 Insn_template::thumb16_insn(0xb401), // push {r0}
4629 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4630 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4631 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4632 Insn_template::thumb16_insn(0x4760), // bx ip
4633 Insn_template::thumb16_insn(0xbf00), // nop
4634 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4635 // dcd R_ARM_ABS32(X)
4638 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4640 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
4642 Insn_template::thumb16_insn(0x4778), // bx pc
4643 Insn_template::thumb16_insn(0x46c0), // nop
4644 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4645 Insn_template::arm_insn(0xe12fff1c), // bx ip
4646 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4647 // dcd R_ARM_ABS32(X)
4650 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4652 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
4654 Insn_template::thumb16_insn(0x4778), // bx pc
4655 Insn_template::thumb16_insn(0x46c0), // nop
4656 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4657 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4658 // dcd R_ARM_ABS32(X)
4661 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4662 // one, when the destination is close enough.
4663 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
4665 Insn_template::thumb16_insn(0x4778), // bx pc
4666 Insn_template::thumb16_insn(0x46c0), // nop
4667 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4670 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4671 // blx to reach the stub if necessary.
4672 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
4674 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4675 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4676 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
4677 // dcd R_ARM_REL32(X-4)
4680 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4681 // blx to reach the stub if necessary. We can not add into pc;
4682 // it is not guaranteed to mode switch (different in ARMv6 and
4684 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
4686 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4687 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4688 Insn_template::arm_insn(0xe12fff1c), // bx ip
4689 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4690 // dcd R_ARM_REL32(X)
4693 // V4T ARM -> ARM long branch stub, PIC.
4694 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
4696 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4697 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4698 Insn_template::arm_insn(0xe12fff1c), // bx ip
4699 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4700 // dcd R_ARM_REL32(X)
4703 // V4T Thumb -> ARM long branch stub, PIC.
4704 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
4706 Insn_template::thumb16_insn(0x4778), // bx pc
4707 Insn_template::thumb16_insn(0x46c0), // nop
4708 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4709 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4710 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
4711 // dcd R_ARM_REL32(X)
4714 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4716 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
4718 Insn_template::thumb16_insn(0xb401), // push {r0}
4719 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4720 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4721 Insn_template::thumb16_insn(0x4484), // add ip, r0
4722 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4723 Insn_template::thumb16_insn(0x4760), // bx ip
4724 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
4725 // dcd R_ARM_REL32(X)
4728 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4730 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
4732 Insn_template::thumb16_insn(0x4778), // bx pc
4733 Insn_template::thumb16_insn(0x46c0), // nop
4734 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4735 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4736 Insn_template::arm_insn(0xe12fff1c), // bx ip
4737 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4738 // dcd R_ARM_REL32(X)
4741 // Cortex-A8 erratum-workaround stubs.
4743 // Stub used for conditional branches (which may be beyond +/-1MB away,
4744 // so we can't use a conditional branch to reach this stub).
4751 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
4753 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4754 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4755 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4759 // Stub used for b.w and bl.w instructions.
4761 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
4763 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4766 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
4768 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4771 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4772 // instruction (which switches to ARM mode) to point to this stub. Jump to
4773 // the real destination using an ARM-mode branch.
4774 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
4776 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4779 // Stub used to provide an interworking for R_ARM_V4BX relocation
4780 // (bx r[n] instruction).
4781 static const Insn_template elf32_arm_stub_v4_veneer_bx
[] =
4783 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4784 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4785 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4788 // Fill in the stub template look-up table. Stub templates are constructed
4789 // per instance of Stub_factory for fast look-up without locking
4790 // in a thread-enabled environment.
4792 this->stub_templates_
[arm_stub_none
] =
4793 new Stub_template(arm_stub_none
, NULL
, 0);
4795 #define DEF_STUB(x) \
4799 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4800 Stub_type type = arm_stub_##x; \
4801 this->stub_templates_[type] = \
4802 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4810 // Stub_table methods.
4812 // Removel all Cortex-A8 stub.
4814 template<bool big_endian
>
4816 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
4818 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4819 p
!= this->cortex_a8_stubs_
.end();
4822 this->cortex_a8_stubs_
.clear();
4825 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4827 template<bool big_endian
>
4829 Stub_table
<big_endian
>::relocate_stub(
4831 const Relocate_info
<32, big_endian
>* relinfo
,
4832 Target_arm
<big_endian
>* arm_target
,
4833 Output_section
* output_section
,
4834 unsigned char* view
,
4835 Arm_address address
,
4836 section_size_type view_size
)
4838 const Stub_template
* stub_template
= stub
->stub_template();
4839 if (stub_template
->reloc_count() != 0)
4841 // Adjust view to cover the stub only.
4842 section_size_type offset
= stub
->offset();
4843 section_size_type stub_size
= stub_template
->size();
4844 gold_assert(offset
+ stub_size
<= view_size
);
4846 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
4847 address
+ offset
, stub_size
);
4851 // Relocate all stubs in this stub table.
4853 template<bool big_endian
>
4855 Stub_table
<big_endian
>::relocate_stubs(
4856 const Relocate_info
<32, big_endian
>* relinfo
,
4857 Target_arm
<big_endian
>* arm_target
,
4858 Output_section
* output_section
,
4859 unsigned char* view
,
4860 Arm_address address
,
4861 section_size_type view_size
)
4863 // If we are passed a view bigger than the stub table's. we need to
4865 gold_assert(address
== this->address()
4867 == static_cast<section_size_type
>(this->data_size())));
4869 // Relocate all relocation stubs.
4870 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4871 p
!= this->reloc_stubs_
.end();
4873 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4874 address
, view_size
);
4876 // Relocate all Cortex-A8 stubs.
4877 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4878 p
!= this->cortex_a8_stubs_
.end();
4880 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4881 address
, view_size
);
4883 // Relocate all ARM V4BX stubs.
4884 for (Arm_v4bx_stub_list::iterator p
= this->arm_v4bx_stubs_
.begin();
4885 p
!= this->arm_v4bx_stubs_
.end();
4889 this->relocate_stub(*p
, relinfo
, arm_target
, output_section
, view
,
4890 address
, view_size
);
4894 // Write out the stubs to file.
4896 template<bool big_endian
>
4898 Stub_table
<big_endian
>::do_write(Output_file
* of
)
4900 off_t offset
= this->offset();
4901 const section_size_type oview_size
=
4902 convert_to_section_size_type(this->data_size());
4903 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4905 // Write relocation stubs.
4906 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4907 p
!= this->reloc_stubs_
.end();
4910 Reloc_stub
* stub
= p
->second
;
4911 Arm_address address
= this->address() + stub
->offset();
4913 == align_address(address
,
4914 stub
->stub_template()->alignment()));
4915 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4919 // Write Cortex-A8 stubs.
4920 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4921 p
!= this->cortex_a8_stubs_
.end();
4924 Cortex_a8_stub
* stub
= p
->second
;
4925 Arm_address address
= this->address() + stub
->offset();
4927 == align_address(address
,
4928 stub
->stub_template()->alignment()));
4929 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4933 // Write ARM V4BX relocation stubs.
4934 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4935 p
!= this->arm_v4bx_stubs_
.end();
4941 Arm_address address
= this->address() + (*p
)->offset();
4943 == align_address(address
,
4944 (*p
)->stub_template()->alignment()));
4945 (*p
)->write(oview
+ (*p
)->offset(), (*p
)->stub_template()->size(),
4949 of
->write_output_view(this->offset(), oview_size
, oview
);
4952 // Update the data size and address alignment of the stub table at the end
4953 // of a relaxation pass. Return true if either the data size or the
4954 // alignment changed in this relaxation pass.
4956 template<bool big_endian
>
4958 Stub_table
<big_endian
>::update_data_size_and_addralign()
4960 // Go over all stubs in table to compute data size and address alignment.
4961 off_t size
= this->reloc_stubs_size_
;
4962 unsigned addralign
= this->reloc_stubs_addralign_
;
4964 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4965 p
!= this->cortex_a8_stubs_
.end();
4968 const Stub_template
* stub_template
= p
->second
->stub_template();
4969 addralign
= std::max(addralign
, stub_template
->alignment());
4970 size
= (align_address(size
, stub_template
->alignment())
4971 + stub_template
->size());
4974 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4975 p
!= this->arm_v4bx_stubs_
.end();
4981 const Stub_template
* stub_template
= (*p
)->stub_template();
4982 addralign
= std::max(addralign
, stub_template
->alignment());
4983 size
= (align_address(size
, stub_template
->alignment())
4984 + stub_template
->size());
4987 // Check if either data size or alignment changed in this pass.
4988 // Update prev_data_size_ and prev_addralign_. These will be used
4989 // as the current data size and address alignment for the next pass.
4990 bool changed
= size
!= this->prev_data_size_
;
4991 this->prev_data_size_
= size
;
4993 if (addralign
!= this->prev_addralign_
)
4995 this->prev_addralign_
= addralign
;
5000 // Finalize the stubs. This sets the offsets of the stubs within the stub
5001 // table. It also marks all input sections needing Cortex-A8 workaround.
5003 template<bool big_endian
>
5005 Stub_table
<big_endian
>::finalize_stubs()
5007 off_t off
= this->reloc_stubs_size_
;
5008 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
5009 p
!= this->cortex_a8_stubs_
.end();
5012 Cortex_a8_stub
* stub
= p
->second
;
5013 const Stub_template
* stub_template
= stub
->stub_template();
5014 uint64_t stub_addralign
= stub_template
->alignment();
5015 off
= align_address(off
, stub_addralign
);
5016 stub
->set_offset(off
);
5017 off
+= stub_template
->size();
5019 // Mark input section so that we can determine later if a code section
5020 // needs the Cortex-A8 workaround quickly.
5021 Arm_relobj
<big_endian
>* arm_relobj
=
5022 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
5023 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
5026 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
5027 p
!= this->arm_v4bx_stubs_
.end();
5033 const Stub_template
* stub_template
= (*p
)->stub_template();
5034 uint64_t stub_addralign
= stub_template
->alignment();
5035 off
= align_address(off
, stub_addralign
);
5036 (*p
)->set_offset(off
);
5037 off
+= stub_template
->size();
5040 gold_assert(off
<= this->prev_data_size_
);
5043 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
5044 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
5045 // of the address range seen by the linker.
5047 template<bool big_endian
>
5049 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
5050 Target_arm
<big_endian
>* arm_target
,
5051 unsigned char* view
,
5052 Arm_address view_address
,
5053 section_size_type view_size
)
5055 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
5056 for (Cortex_a8_stub_list::const_iterator p
=
5057 this->cortex_a8_stubs_
.lower_bound(view_address
);
5058 ((p
!= this->cortex_a8_stubs_
.end())
5059 && (p
->first
< (view_address
+ view_size
)));
5062 // We do not store the THUMB bit in the LSB of either the branch address
5063 // or the stub offset. There is no need to strip the LSB.
5064 Arm_address branch_address
= p
->first
;
5065 const Cortex_a8_stub
* stub
= p
->second
;
5066 Arm_address stub_address
= this->address() + stub
->offset();
5068 // Offset of the branch instruction relative to this view.
5069 section_size_type offset
=
5070 convert_to_section_size_type(branch_address
- view_address
);
5071 gold_assert((offset
+ 4) <= view_size
);
5073 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
5074 view
+ offset
, branch_address
);
5078 // Arm_input_section methods.
5080 // Initialize an Arm_input_section.
5082 template<bool big_endian
>
5084 Arm_input_section
<big_endian
>::init()
5086 Relobj
* relobj
= this->relobj();
5087 unsigned int shndx
= this->shndx();
5089 // Cache these to speed up size and alignment queries. It is too slow
5090 // to call section_addraglin and section_size every time.
5091 this->original_addralign_
=
5092 convert_types
<uint32_t, uint64_t>(relobj
->section_addralign(shndx
));
5093 this->original_size_
=
5094 convert_types
<uint32_t, uint64_t>(relobj
->section_size(shndx
));
5096 // We want to make this look like the original input section after
5097 // output sections are finalized.
5098 Output_section
* os
= relobj
->output_section(shndx
);
5099 off_t offset
= relobj
->output_section_offset(shndx
);
5100 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
5101 this->set_address(os
->address() + offset
);
5102 this->set_file_offset(os
->offset() + offset
);
5104 this->set_current_data_size(this->original_size_
);
5105 this->finalize_data_size();
5108 template<bool big_endian
>
5110 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
5112 // We have to write out the original section content.
5113 section_size_type section_size
;
5114 const unsigned char* section_contents
=
5115 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
5116 of
->write(this->offset(), section_contents
, section_size
);
5118 // If this owns a stub table and it is not empty, write it.
5119 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
5120 this->stub_table_
->write(of
);
5123 // Finalize data size.
5125 template<bool big_endian
>
5127 Arm_input_section
<big_endian
>::set_final_data_size()
5129 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
5131 if (this->is_stub_table_owner())
5133 this->stub_table_
->finalize_data_size();
5134 off
= align_address(off
, this->stub_table_
->addralign());
5135 off
+= this->stub_table_
->data_size();
5137 this->set_data_size(off
);
5140 // Reset address and file offset.
5142 template<bool big_endian
>
5144 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
5146 // Size of the original input section contents.
5147 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
5149 // If this is a stub table owner, account for the stub table size.
5150 if (this->is_stub_table_owner())
5152 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
5154 // Reset the stub table's address and file offset. The
5155 // current data size for child will be updated after that.
5156 stub_table_
->reset_address_and_file_offset();
5157 off
= align_address(off
, stub_table_
->addralign());
5158 off
+= stub_table
->current_data_size();
5161 this->set_current_data_size(off
);
5164 // Arm_exidx_cantunwind methods.
5166 // Write this to Output file OF for a fixed endianness.
5168 template<bool big_endian
>
5170 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file
* of
)
5172 off_t offset
= this->offset();
5173 const section_size_type oview_size
= 8;
5174 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5176 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5177 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
);
5179 Output_section
* os
= this->relobj_
->output_section(this->shndx_
);
5180 gold_assert(os
!= NULL
);
5182 Arm_relobj
<big_endian
>* arm_relobj
=
5183 Arm_relobj
<big_endian
>::as_arm_relobj(this->relobj_
);
5184 Arm_address output_offset
=
5185 arm_relobj
->get_output_section_offset(this->shndx_
);
5186 Arm_address section_start
;
5187 if (output_offset
!= Arm_relobj
<big_endian
>::invalid_address
)
5188 section_start
= os
->address() + output_offset
;
5191 // Currently this only happens for a relaxed section.
5192 const Output_relaxed_input_section
* poris
=
5193 os
->find_relaxed_input_section(this->relobj_
, this->shndx_
);
5194 gold_assert(poris
!= NULL
);
5195 section_start
= poris
->address();
5198 // We always append this to the end of an EXIDX section.
5199 Arm_address output_address
=
5200 section_start
+ this->relobj_
->section_size(this->shndx_
);
5202 // Write out the entry. The first word either points to the beginning
5203 // or after the end of a text section. The second word is the special
5204 // EXIDX_CANTUNWIND value.
5205 uint32_t prel31_offset
= output_address
- this->address();
5206 if (utils::has_overflow
<31>(offset
))
5207 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5208 elfcpp::Swap
<32, big_endian
>::writeval(wv
, prel31_offset
& 0x7fffffffU
);
5209 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, elfcpp::EXIDX_CANTUNWIND
);
5211 of
->write_output_view(this->offset(), oview_size
, oview
);
5214 // Arm_exidx_merged_section methods.
5216 // Constructor for Arm_exidx_merged_section.
5217 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5218 // SECTION_OFFSET_MAP points to a section offset map describing how
5219 // parts of the input section are mapped to output. DELETED_BYTES is
5220 // the number of bytes deleted from the EXIDX input section.
5222 Arm_exidx_merged_section::Arm_exidx_merged_section(
5223 const Arm_exidx_input_section
& exidx_input_section
,
5224 const Arm_exidx_section_offset_map
& section_offset_map
,
5225 uint32_t deleted_bytes
)
5226 : Output_relaxed_input_section(exidx_input_section
.relobj(),
5227 exidx_input_section
.shndx(),
5228 exidx_input_section
.addralign()),
5229 exidx_input_section_(exidx_input_section
),
5230 section_offset_map_(section_offset_map
)
5232 // Fix size here so that we do not need to implement set_final_data_size.
5233 this->set_data_size(exidx_input_section
.size() - deleted_bytes
);
5234 this->fix_data_size();
5237 // Given an input OBJECT, an input section index SHNDX within that
5238 // object, and an OFFSET relative to the start of that input
5239 // section, return whether or not the corresponding offset within
5240 // the output section is known. If this function returns true, it
5241 // sets *POUTPUT to the output offset. The value -1 indicates that
5242 // this input offset is being discarded.
5245 Arm_exidx_merged_section::do_output_offset(
5246 const Relobj
* relobj
,
5248 section_offset_type offset
,
5249 section_offset_type
* poutput
) const
5251 // We only handle offsets for the original EXIDX input section.
5252 if (relobj
!= this->exidx_input_section_
.relobj()
5253 || shndx
!= this->exidx_input_section_
.shndx())
5256 section_offset_type section_size
=
5257 convert_types
<section_offset_type
>(this->exidx_input_section_
.size());
5258 if (offset
< 0 || offset
>= section_size
)
5259 // Input offset is out of valid range.
5263 // We need to look up the section offset map to determine the output
5264 // offset. Find the reference point in map that is first offset
5265 // bigger than or equal to this offset.
5266 Arm_exidx_section_offset_map::const_iterator p
=
5267 this->section_offset_map_
.lower_bound(offset
);
5269 // The section offset maps are build such that this should not happen if
5270 // input offset is in the valid range.
5271 gold_assert(p
!= this->section_offset_map_
.end());
5273 // We need to check if this is dropped.
5274 section_offset_type ref
= p
->first
;
5275 section_offset_type mapped_ref
= p
->second
;
5277 if (mapped_ref
!= Arm_exidx_input_section::invalid_offset
)
5278 // Offset is present in output.
5279 *poutput
= mapped_ref
+ (offset
- ref
);
5281 // Offset is discarded owing to EXIDX entry merging.
5288 // Write this to output file OF.
5291 Arm_exidx_merged_section::do_write(Output_file
* of
)
5293 // If we retain or discard the whole EXIDX input section, we would
5295 gold_assert(this->data_size() != this->exidx_input_section_
.size()
5296 && this->data_size() != 0);
5298 off_t offset
= this->offset();
5299 const section_size_type oview_size
= this->data_size();
5300 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5302 Output_section
* os
= this->relobj()->output_section(this->shndx());
5303 gold_assert(os
!= NULL
);
5305 // Get contents of EXIDX input section.
5306 section_size_type section_size
;
5307 const unsigned char* section_contents
=
5308 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
5309 gold_assert(section_size
== this->exidx_input_section_
.size());
5311 // Go over spans of input offsets and write only those that are not
5313 section_offset_type in_start
= 0;
5314 section_offset_type out_start
= 0;
5315 for(Arm_exidx_section_offset_map::const_iterator p
=
5316 this->section_offset_map_
.begin();
5317 p
!= this->section_offset_map_
.end();
5320 section_offset_type in_end
= p
->first
;
5321 gold_assert(in_end
>= in_start
);
5322 section_offset_type out_end
= p
->second
;
5323 size_t in_chunk_size
= convert_types
<size_t>(in_end
- in_start
+ 1);
5326 size_t out_chunk_size
=
5327 convert_types
<size_t>(out_end
- out_start
+ 1);
5328 gold_assert(out_chunk_size
== in_chunk_size
);
5329 memcpy(oview
+ out_start
, section_contents
+ in_start
,
5331 out_start
+= out_chunk_size
;
5333 in_start
+= in_chunk_size
;
5336 gold_assert(convert_to_section_size_type(out_start
) == oview_size
);
5337 of
->write_output_view(this->offset(), oview_size
, oview
);
5340 // Arm_exidx_fixup methods.
5342 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5343 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5344 // points to the end of the last seen EXIDX section.
5347 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5349 if (this->last_unwind_type_
!= UT_EXIDX_CANTUNWIND
5350 && this->last_input_section_
!= NULL
)
5352 Relobj
* relobj
= this->last_input_section_
->relobj();
5353 unsigned int text_shndx
= this->last_input_section_
->link();
5354 Arm_exidx_cantunwind
* cantunwind
=
5355 new Arm_exidx_cantunwind(relobj
, text_shndx
);
5356 this->exidx_output_section_
->add_output_section_data(cantunwind
);
5357 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
5361 // Process an EXIDX section entry in input. Return whether this entry
5362 // can be deleted in the output. SECOND_WORD in the second word of the
5366 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word
)
5369 if (second_word
== elfcpp::EXIDX_CANTUNWIND
)
5371 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5372 delete_entry
= this->last_unwind_type_
== UT_EXIDX_CANTUNWIND
;
5373 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
5375 else if ((second_word
& 0x80000000) != 0)
5377 // Inlined unwinding data. Merge if equal to previous.
5378 delete_entry
= (merge_exidx_entries_
5379 && this->last_unwind_type_
== UT_INLINED_ENTRY
5380 && this->last_inlined_entry_
== second_word
);
5381 this->last_unwind_type_
= UT_INLINED_ENTRY
;
5382 this->last_inlined_entry_
= second_word
;
5386 // Normal table entry. In theory we could merge these too,
5387 // but duplicate entries are likely to be much less common.
5388 delete_entry
= false;
5389 this->last_unwind_type_
= UT_NORMAL_ENTRY
;
5391 return delete_entry
;
5394 // Update the current section offset map during EXIDX section fix-up.
5395 // If there is no map, create one. INPUT_OFFSET is the offset of a
5396 // reference point, DELETED_BYTES is the number of deleted by in the
5397 // section so far. If DELETE_ENTRY is true, the reference point and
5398 // all offsets after the previous reference point are discarded.
5401 Arm_exidx_fixup::update_offset_map(
5402 section_offset_type input_offset
,
5403 section_size_type deleted_bytes
,
5406 if (this->section_offset_map_
== NULL
)
5407 this->section_offset_map_
= new Arm_exidx_section_offset_map();
5408 section_offset_type output_offset
;
5410 output_offset
= Arm_exidx_input_section::invalid_offset
;
5412 output_offset
= input_offset
- deleted_bytes
;
5413 (*this->section_offset_map_
)[input_offset
] = output_offset
;
5416 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5417 // bytes deleted. If some entries are merged, also store a pointer to a newly
5418 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5419 // caller owns the map and is responsible for releasing it after use.
5421 template<bool big_endian
>
5423 Arm_exidx_fixup::process_exidx_section(
5424 const Arm_exidx_input_section
* exidx_input_section
,
5425 Arm_exidx_section_offset_map
** psection_offset_map
)
5427 Relobj
* relobj
= exidx_input_section
->relobj();
5428 unsigned shndx
= exidx_input_section
->shndx();
5429 section_size_type section_size
;
5430 const unsigned char* section_contents
=
5431 relobj
->section_contents(shndx
, §ion_size
, false);
5433 if ((section_size
% 8) != 0)
5435 // Something is wrong with this section. Better not touch it.
5436 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5437 relobj
->name().c_str(), shndx
);
5438 this->last_input_section_
= exidx_input_section
;
5439 this->last_unwind_type_
= UT_NONE
;
5443 uint32_t deleted_bytes
= 0;
5444 bool prev_delete_entry
= false;
5445 gold_assert(this->section_offset_map_
== NULL
);
5447 for (section_size_type i
= 0; i
< section_size
; i
+= 8)
5449 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5451 reinterpret_cast<const Valtype
*>(section_contents
+ i
+ 4);
5452 uint32_t second_word
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5454 bool delete_entry
= this->process_exidx_entry(second_word
);
5456 // Entry deletion causes changes in output offsets. We use a std::map
5457 // to record these. And entry (x, y) means input offset x
5458 // is mapped to output offset y. If y is invalid_offset, then x is
5459 // dropped in the output. Because of the way std::map::lower_bound
5460 // works, we record the last offset in a region w.r.t to keeping or
5461 // dropping. If there is no entry (x0, y0) for an input offset x0,
5462 // the output offset y0 of it is determined by the output offset y1 of
5463 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5464 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5466 if (delete_entry
!= prev_delete_entry
&& i
!= 0)
5467 this->update_offset_map(i
- 1, deleted_bytes
, prev_delete_entry
);
5469 // Update total deleted bytes for this entry.
5473 prev_delete_entry
= delete_entry
;
5476 // If section offset map is not NULL, make an entry for the end of
5478 if (this->section_offset_map_
!= NULL
)
5479 update_offset_map(section_size
- 1, deleted_bytes
, prev_delete_entry
);
5481 *psection_offset_map
= this->section_offset_map_
;
5482 this->section_offset_map_
= NULL
;
5483 this->last_input_section_
= exidx_input_section
;
5485 // Set the first output text section so that we can link the EXIDX output
5486 // section to it. Ignore any EXIDX input section that is completely merged.
5487 if (this->first_output_text_section_
== NULL
5488 && deleted_bytes
!= section_size
)
5490 unsigned int link
= exidx_input_section
->link();
5491 Output_section
* os
= relobj
->output_section(link
);
5492 gold_assert(os
!= NULL
);
5493 this->first_output_text_section_
= os
;
5496 return deleted_bytes
;
5499 // Arm_output_section methods.
5501 // Create a stub group for input sections from BEGIN to END. OWNER
5502 // points to the input section to be the owner a new stub table.
5504 template<bool big_endian
>
5506 Arm_output_section
<big_endian
>::create_stub_group(
5507 Input_section_list::const_iterator begin
,
5508 Input_section_list::const_iterator end
,
5509 Input_section_list::const_iterator owner
,
5510 Target_arm
<big_endian
>* target
,
5511 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
5513 // We use a different kind of relaxed section in an EXIDX section.
5514 // The static casting from Output_relaxed_input_section to
5515 // Arm_input_section is invalid in an EXIDX section. We are okay
5516 // because we should not be calling this for an EXIDX section.
5517 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX
);
5519 // Currently we convert ordinary input sections into relaxed sections only
5520 // at this point but we may want to support creating relaxed input section
5521 // very early. So we check here to see if owner is already a relaxed
5524 Arm_input_section
<big_endian
>* arm_input_section
;
5525 if (owner
->is_relaxed_input_section())
5528 Arm_input_section
<big_endian
>::as_arm_input_section(
5529 owner
->relaxed_input_section());
5533 gold_assert(owner
->is_input_section());
5534 // Create a new relaxed input section.
5536 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
5537 new_relaxed_sections
->push_back(arm_input_section
);
5540 // Create a stub table.
5541 Stub_table
<big_endian
>* stub_table
=
5542 target
->new_stub_table(arm_input_section
);
5544 arm_input_section
->set_stub_table(stub_table
);
5546 Input_section_list::const_iterator p
= begin
;
5547 Input_section_list::const_iterator prev_p
;
5549 // Look for input sections or relaxed input sections in [begin ... end].
5552 if (p
->is_input_section() || p
->is_relaxed_input_section())
5554 // The stub table information for input sections live
5555 // in their objects.
5556 Arm_relobj
<big_endian
>* arm_relobj
=
5557 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
5558 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
5562 while (prev_p
!= end
);
5565 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5566 // of stub groups. We grow a stub group by adding input section until the
5567 // size is just below GROUP_SIZE. The last input section will be converted
5568 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5569 // input section after the stub table, effectively double the group size.
5571 // This is similar to the group_sections() function in elf32-arm.c but is
5572 // implemented differently.
5574 template<bool big_endian
>
5576 Arm_output_section
<big_endian
>::group_sections(
5577 section_size_type group_size
,
5578 bool stubs_always_after_branch
,
5579 Target_arm
<big_endian
>* target
)
5581 // We only care about sections containing code.
5582 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
5585 // States for grouping.
5588 // No group is being built.
5590 // A group is being built but the stub table is not found yet.
5591 // We keep group a stub group until the size is just under GROUP_SIZE.
5592 // The last input section in the group will be used as the stub table.
5593 FINDING_STUB_SECTION
,
5594 // A group is being built and we have already found a stub table.
5595 // We enter this state to grow a stub group by adding input section
5596 // after the stub table. This effectively doubles the group size.
5600 // Any newly created relaxed sections are stored here.
5601 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
5603 State state
= NO_GROUP
;
5604 section_size_type off
= 0;
5605 section_size_type group_begin_offset
= 0;
5606 section_size_type group_end_offset
= 0;
5607 section_size_type stub_table_end_offset
= 0;
5608 Input_section_list::const_iterator group_begin
=
5609 this->input_sections().end();
5610 Input_section_list::const_iterator stub_table
=
5611 this->input_sections().end();
5612 Input_section_list::const_iterator group_end
= this->input_sections().end();
5613 for (Input_section_list::const_iterator p
= this->input_sections().begin();
5614 p
!= this->input_sections().end();
5617 section_size_type section_begin_offset
=
5618 align_address(off
, p
->addralign());
5619 section_size_type section_end_offset
=
5620 section_begin_offset
+ p
->data_size();
5622 // Check to see if we should group the previously seens sections.
5628 case FINDING_STUB_SECTION
:
5629 // Adding this section makes the group larger than GROUP_SIZE.
5630 if (section_end_offset
- group_begin_offset
>= group_size
)
5632 if (stubs_always_after_branch
)
5634 gold_assert(group_end
!= this->input_sections().end());
5635 this->create_stub_group(group_begin
, group_end
, group_end
,
5636 target
, &new_relaxed_sections
);
5641 // But wait, there's more! Input sections up to
5642 // stub_group_size bytes after the stub table can be
5643 // handled by it too.
5644 state
= HAS_STUB_SECTION
;
5645 stub_table
= group_end
;
5646 stub_table_end_offset
= group_end_offset
;
5651 case HAS_STUB_SECTION
:
5652 // Adding this section makes the post stub-section group larger
5654 if (section_end_offset
- stub_table_end_offset
>= group_size
)
5656 gold_assert(group_end
!= this->input_sections().end());
5657 this->create_stub_group(group_begin
, group_end
, stub_table
,
5658 target
, &new_relaxed_sections
);
5667 // If we see an input section and currently there is no group, start
5668 // a new one. Skip any empty sections.
5669 if ((p
->is_input_section() || p
->is_relaxed_input_section())
5670 && (p
->relobj()->section_size(p
->shndx()) != 0))
5672 if (state
== NO_GROUP
)
5674 state
= FINDING_STUB_SECTION
;
5676 group_begin_offset
= section_begin_offset
;
5679 // Keep track of the last input section seen.
5681 group_end_offset
= section_end_offset
;
5684 off
= section_end_offset
;
5687 // Create a stub group for any ungrouped sections.
5688 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
5690 gold_assert(group_end
!= this->input_sections().end());
5691 this->create_stub_group(group_begin
, group_end
,
5692 (state
== FINDING_STUB_SECTION
5695 target
, &new_relaxed_sections
);
5698 // Convert input section into relaxed input section in a batch.
5699 if (!new_relaxed_sections
.empty())
5700 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
5702 // Update the section offsets
5703 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
5705 Arm_relobj
<big_endian
>* arm_relobj
=
5706 Arm_relobj
<big_endian
>::as_arm_relobj(
5707 new_relaxed_sections
[i
]->relobj());
5708 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
5709 // Tell Arm_relobj that this input section is converted.
5710 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
5714 // Append non empty text sections in this to LIST in ascending
5715 // order of their position in this.
5717 template<bool big_endian
>
5719 Arm_output_section
<big_endian
>::append_text_sections_to_list(
5720 Text_section_list
* list
)
5722 gold_assert((this->flags() & elfcpp::SHF_ALLOC
) != 0);
5724 for (Input_section_list::const_iterator p
= this->input_sections().begin();
5725 p
!= this->input_sections().end();
5728 // We only care about plain or relaxed input sections. We also
5729 // ignore any merged sections.
5730 if ((p
->is_input_section() || p
->is_relaxed_input_section())
5731 && p
->data_size() != 0)
5732 list
->push_back(Text_section_list::value_type(p
->relobj(),
5737 template<bool big_endian
>
5739 Arm_output_section
<big_endian
>::fix_exidx_coverage(
5741 const Text_section_list
& sorted_text_sections
,
5742 Symbol_table
* symtab
,
5743 bool merge_exidx_entries
)
5745 // We should only do this for the EXIDX output section.
5746 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX
);
5748 // We don't want the relaxation loop to undo these changes, so we discard
5749 // the current saved states and take another one after the fix-up.
5750 this->discard_states();
5752 // Remove all input sections.
5753 uint64_t address
= this->address();
5754 typedef std::list
<Output_section::Input_section
> Input_section_list
;
5755 Input_section_list input_sections
;
5756 this->reset_address_and_file_offset();
5757 this->get_input_sections(address
, std::string(""), &input_sections
);
5759 if (!this->input_sections().empty())
5760 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5762 // Go through all the known input sections and record them.
5763 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_id_set
;
5764 typedef Unordered_map
<Section_id
, const Output_section::Input_section
*,
5765 Section_id_hash
> Text_to_exidx_map
;
5766 Text_to_exidx_map text_to_exidx_map
;
5767 for (Input_section_list::const_iterator p
= input_sections
.begin();
5768 p
!= input_sections
.end();
5771 // This should never happen. At this point, we should only see
5772 // plain EXIDX input sections.
5773 gold_assert(!p
->is_relaxed_input_section());
5774 text_to_exidx_map
[Section_id(p
->relobj(), p
->shndx())] = &(*p
);
5777 Arm_exidx_fixup
exidx_fixup(this, merge_exidx_entries
);
5779 // Go over the sorted text sections.
5780 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_id_set
;
5781 Section_id_set processed_input_sections
;
5782 for (Text_section_list::const_iterator p
= sorted_text_sections
.begin();
5783 p
!= sorted_text_sections
.end();
5786 Relobj
* relobj
= p
->first
;
5787 unsigned int shndx
= p
->second
;
5789 Arm_relobj
<big_endian
>* arm_relobj
=
5790 Arm_relobj
<big_endian
>::as_arm_relobj(relobj
);
5791 const Arm_exidx_input_section
* exidx_input_section
=
5792 arm_relobj
->exidx_input_section_by_link(shndx
);
5794 // If this text section has no EXIDX section or if the EXIDX section
5795 // has errors, force an EXIDX_CANTUNWIND entry pointing to the end
5796 // of the last seen EXIDX section.
5797 if (exidx_input_section
== NULL
|| exidx_input_section
->has_errors())
5799 exidx_fixup
.add_exidx_cantunwind_as_needed();
5803 Relobj
* exidx_relobj
= exidx_input_section
->relobj();
5804 unsigned int exidx_shndx
= exidx_input_section
->shndx();
5805 Section_id
sid(exidx_relobj
, exidx_shndx
);
5806 Text_to_exidx_map::const_iterator iter
= text_to_exidx_map
.find(sid
);
5807 if (iter
== text_to_exidx_map
.end())
5809 // This is odd. We have not seen this EXIDX input section before.
5810 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5811 // issue a warning instead. We assume the user knows what he
5812 // or she is doing. Otherwise, this is an error.
5813 if (layout
->script_options()->saw_sections_clause())
5814 gold_warning(_("unwinding may not work because EXIDX input section"
5815 " %u of %s is not in EXIDX output section"),
5816 exidx_shndx
, exidx_relobj
->name().c_str());
5818 gold_error(_("unwinding may not work because EXIDX input section"
5819 " %u of %s is not in EXIDX output section"),
5820 exidx_shndx
, exidx_relobj
->name().c_str());
5822 exidx_fixup
.add_exidx_cantunwind_as_needed();
5826 // Fix up coverage and append input section to output data list.
5827 Arm_exidx_section_offset_map
* section_offset_map
= NULL
;
5828 uint32_t deleted_bytes
=
5829 exidx_fixup
.process_exidx_section
<big_endian
>(exidx_input_section
,
5830 §ion_offset_map
);
5832 if (deleted_bytes
== exidx_input_section
->size())
5834 // The whole EXIDX section got merged. Remove it from output.
5835 gold_assert(section_offset_map
== NULL
);
5836 exidx_relobj
->set_output_section(exidx_shndx
, NULL
);
5838 // All local symbols defined in this input section will be dropped.
5839 // We need to adjust output local symbol count.
5840 arm_relobj
->set_output_local_symbol_count_needs_update();
5842 else if (deleted_bytes
> 0)
5844 // Some entries are merged. We need to convert this EXIDX input
5845 // section into a relaxed section.
5846 gold_assert(section_offset_map
!= NULL
);
5847 Arm_exidx_merged_section
* merged_section
=
5848 new Arm_exidx_merged_section(*exidx_input_section
,
5849 *section_offset_map
, deleted_bytes
);
5850 const std::string secname
= exidx_relobj
->section_name(exidx_shndx
);
5851 this->add_relaxed_input_section(layout
, merged_section
, secname
);
5852 arm_relobj
->convert_input_section_to_relaxed_section(exidx_shndx
);
5854 // All local symbols defined in discarded portions of this input
5855 // section will be dropped. We need to adjust output local symbol
5857 arm_relobj
->set_output_local_symbol_count_needs_update();
5861 // Just add back the EXIDX input section.
5862 gold_assert(section_offset_map
== NULL
);
5863 const Output_section::Input_section
* pis
= iter
->second
;
5864 gold_assert(pis
->is_input_section());
5865 this->add_script_input_section(*pis
);
5868 processed_input_sections
.insert(Section_id(exidx_relobj
, exidx_shndx
));
5871 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5872 exidx_fixup
.add_exidx_cantunwind_as_needed();
5874 // Remove any known EXIDX input sections that are not processed.
5875 for (Input_section_list::const_iterator p
= input_sections
.begin();
5876 p
!= input_sections
.end();
5879 if (processed_input_sections
.find(Section_id(p
->relobj(), p
->shndx()))
5880 == processed_input_sections
.end())
5882 // We discard a known EXIDX section because its linked
5883 // text section has been folded by ICF. We also discard an
5884 // EXIDX section with error, the output does not matter in this
5885 // case. We do this to avoid triggering asserts.
5886 Arm_relobj
<big_endian
>* arm_relobj
=
5887 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
5888 const Arm_exidx_input_section
* exidx_input_section
=
5889 arm_relobj
->exidx_input_section_by_shndx(p
->shndx());
5890 gold_assert(exidx_input_section
!= NULL
);
5891 if (!exidx_input_section
->has_errors())
5893 unsigned int text_shndx
= exidx_input_section
->link();
5894 gold_assert(symtab
->is_section_folded(p
->relobj(), text_shndx
));
5897 // Remove this from link. We also need to recount the
5899 p
->relobj()->set_output_section(p
->shndx(), NULL
);
5900 arm_relobj
->set_output_local_symbol_count_needs_update();
5904 // Link exidx output section to the first seen output section and
5905 // set correct entry size.
5906 this->set_link_section(exidx_fixup
.first_output_text_section());
5907 this->set_entsize(8);
5909 // Make changes permanent.
5910 this->save_states();
5911 this->set_section_offsets_need_adjustment();
5914 // Link EXIDX output sections to text output sections.
5916 template<bool big_endian
>
5918 Arm_output_section
<big_endian
>::set_exidx_section_link()
5920 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX
);
5921 if (!this->input_sections().empty())
5923 Input_section_list::const_iterator p
= this->input_sections().begin();
5924 Arm_relobj
<big_endian
>* arm_relobj
=
5925 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
5926 unsigned exidx_shndx
= p
->shndx();
5927 const Arm_exidx_input_section
* exidx_input_section
=
5928 arm_relobj
->exidx_input_section_by_shndx(exidx_shndx
);
5929 gold_assert(exidx_input_section
!= NULL
);
5930 unsigned int text_shndx
= exidx_input_section
->link();
5931 Output_section
* os
= arm_relobj
->output_section(text_shndx
);
5932 this->set_link_section(os
);
5936 // Arm_relobj methods.
5938 // Determine if an input section is scannable for stub processing. SHDR is
5939 // the header of the section and SHNDX is the section index. OS is the output
5940 // section for the input section and SYMTAB is the global symbol table used to
5941 // look up ICF information.
5943 template<bool big_endian
>
5945 Arm_relobj
<big_endian
>::section_is_scannable(
5946 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5948 const Output_section
* os
,
5949 const Symbol_table
* symtab
)
5951 // Skip any empty sections, unallocated sections or sections whose
5952 // type are not SHT_PROGBITS.
5953 if (shdr
.get_sh_size() == 0
5954 || (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0
5955 || shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
5958 // Skip any discarded or ICF'ed sections.
5959 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
5962 // If this requires special offset handling, check to see if it is
5963 // a relaxed section. If this is not, then it is a merged section that
5964 // we cannot handle.
5965 if (this->is_output_section_offset_invalid(shndx
))
5967 const Output_relaxed_input_section
* poris
=
5968 os
->find_relaxed_input_section(this, shndx
);
5976 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5977 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5979 template<bool big_endian
>
5981 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
5982 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5983 const Relobj::Output_sections
& out_sections
,
5984 const Symbol_table
* symtab
,
5985 const unsigned char* pshdrs
)
5987 unsigned int sh_type
= shdr
.get_sh_type();
5988 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
5991 // Ignore empty section.
5992 off_t sh_size
= shdr
.get_sh_size();
5996 // Ignore reloc section with unexpected symbol table. The
5997 // error will be reported in the final link.
5998 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
6001 unsigned int reloc_size
;
6002 if (sh_type
== elfcpp::SHT_REL
)
6003 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
6005 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
6007 // Ignore reloc section with unexpected entsize or uneven size.
6008 // The error will be reported in the final link.
6009 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
6012 // Ignore reloc section with bad info. This error will be
6013 // reported in the final link.
6014 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
6015 if (index
>= this->shnum())
6018 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6019 const elfcpp::Shdr
<32, big_endian
> text_shdr(pshdrs
+ index
* shdr_size
);
6020 return this->section_is_scannable(text_shdr
, index
,
6021 out_sections
[index
], symtab
);
6024 // Return the output address of either a plain input section or a relaxed
6025 // input section. SHNDX is the section index. We define and use this
6026 // instead of calling Output_section::output_address because that is slow
6027 // for large output.
6029 template<bool big_endian
>
6031 Arm_relobj
<big_endian
>::simple_input_section_output_address(
6035 if (this->is_output_section_offset_invalid(shndx
))
6037 const Output_relaxed_input_section
* poris
=
6038 os
->find_relaxed_input_section(this, shndx
);
6039 // We do not handle merged sections here.
6040 gold_assert(poris
!= NULL
);
6041 return poris
->address();
6044 return os
->address() + this->get_output_section_offset(shndx
);
6047 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
6048 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6050 template<bool big_endian
>
6052 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
6053 const elfcpp::Shdr
<32, big_endian
>& shdr
,
6056 const Symbol_table
* symtab
)
6058 if (!this->section_is_scannable(shdr
, shndx
, os
, symtab
))
6061 // If the section does not cross any 4K-boundaries, it does not need to
6063 Arm_address address
= this->simple_input_section_output_address(shndx
, os
);
6064 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
6070 // Scan a section for Cortex-A8 workaround.
6072 template<bool big_endian
>
6074 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
6075 const elfcpp::Shdr
<32, big_endian
>& shdr
,
6078 Target_arm
<big_endian
>* arm_target
)
6080 // Look for the first mapping symbol in this section. It should be
6082 Mapping_symbol_position
section_start(shndx
, 0);
6083 typename
Mapping_symbols_info::const_iterator p
=
6084 this->mapping_symbols_info_
.lower_bound(section_start
);
6086 // There are no mapping symbols for this section. Treat it as a data-only
6087 // section. Issue a warning if section is marked as containing
6089 if (p
== this->mapping_symbols_info_
.end() || p
->first
.first
!= shndx
)
6091 if ((this->section_flags(shndx
) & elfcpp::SHF_EXECINSTR
) != 0)
6092 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
6093 "erratum because it has no mapping symbols."),
6094 shndx
, this->name().c_str());
6098 Arm_address output_address
=
6099 this->simple_input_section_output_address(shndx
, os
);
6101 // Get the section contents.
6102 section_size_type input_view_size
= 0;
6103 const unsigned char* input_view
=
6104 this->section_contents(shndx
, &input_view_size
, false);
6106 // We need to go through the mapping symbols to determine what to
6107 // scan. There are two reasons. First, we should look at THUMB code and
6108 // THUMB code only. Second, we only want to look at the 4K-page boundary
6109 // to speed up the scanning.
6111 while (p
!= this->mapping_symbols_info_
.end()
6112 && p
->first
.first
== shndx
)
6114 typename
Mapping_symbols_info::const_iterator next
=
6115 this->mapping_symbols_info_
.upper_bound(p
->first
);
6117 // Only scan part of a section with THUMB code.
6118 if (p
->second
== 't')
6120 // Determine the end of this range.
6121 section_size_type span_start
=
6122 convert_to_section_size_type(p
->first
.second
);
6123 section_size_type span_end
;
6124 if (next
!= this->mapping_symbols_info_
.end()
6125 && next
->first
.first
== shndx
)
6126 span_end
= convert_to_section_size_type(next
->first
.second
);
6128 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
6130 if (((span_start
+ output_address
) & ~0xfffUL
)
6131 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
6133 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
6134 span_start
, span_end
,
6144 // Scan relocations for stub generation.
6146 template<bool big_endian
>
6148 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
6149 Target_arm
<big_endian
>* arm_target
,
6150 const Symbol_table
* symtab
,
6151 const Layout
* layout
)
6153 unsigned int shnum
= this->shnum();
6154 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6156 // Read the section headers.
6157 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
6161 // To speed up processing, we set up hash tables for fast lookup of
6162 // input offsets to output addresses.
6163 this->initialize_input_to_output_maps();
6165 const Relobj::Output_sections
& out_sections(this->output_sections());
6167 Relocate_info
<32, big_endian
> relinfo
;
6168 relinfo
.symtab
= symtab
;
6169 relinfo
.layout
= layout
;
6170 relinfo
.object
= this;
6172 // Do relocation stubs scanning.
6173 const unsigned char* p
= pshdrs
+ shdr_size
;
6174 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
6176 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
6177 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
,
6180 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
6181 Arm_address output_offset
= this->get_output_section_offset(index
);
6182 Arm_address output_address
;
6183 if (output_offset
!= invalid_address
)
6184 output_address
= out_sections
[index
]->address() + output_offset
;
6187 // Currently this only happens for a relaxed section.
6188 const Output_relaxed_input_section
* poris
=
6189 out_sections
[index
]->find_relaxed_input_section(this, index
);
6190 gold_assert(poris
!= NULL
);
6191 output_address
= poris
->address();
6194 // Get the relocations.
6195 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
6199 // Get the section contents. This does work for the case in which
6200 // we modify the contents of an input section. We need to pass the
6201 // output view under such circumstances.
6202 section_size_type input_view_size
= 0;
6203 const unsigned char* input_view
=
6204 this->section_contents(index
, &input_view_size
, false);
6206 relinfo
.reloc_shndx
= i
;
6207 relinfo
.data_shndx
= index
;
6208 unsigned int sh_type
= shdr
.get_sh_type();
6209 unsigned int reloc_size
;
6210 if (sh_type
== elfcpp::SHT_REL
)
6211 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
6213 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
6215 Output_section
* os
= out_sections
[index
];
6216 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
6217 shdr
.get_sh_size() / reloc_size
,
6219 output_offset
== invalid_address
,
6220 input_view
, output_address
,
6225 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6226 // after its relocation section, if there is one, is processed for
6227 // relocation stubs. Merging this loop with the one above would have been
6228 // complicated since we would have had to make sure that relocation stub
6229 // scanning is done first.
6230 if (arm_target
->fix_cortex_a8())
6232 const unsigned char* p
= pshdrs
+ shdr_size
;
6233 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
6235 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
6236 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
6239 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
6244 // After we've done the relocations, we release the hash tables,
6245 // since we no longer need them.
6246 this->free_input_to_output_maps();
6249 // Count the local symbols. The ARM backend needs to know if a symbol
6250 // is a THUMB function or not. For global symbols, it is easy because
6251 // the Symbol object keeps the ELF symbol type. For local symbol it is
6252 // harder because we cannot access this information. So we override the
6253 // do_count_local_symbol in parent and scan local symbols to mark
6254 // THUMB functions. This is not the most efficient way but I do not want to
6255 // slow down other ports by calling a per symbol targer hook inside
6256 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6258 template<bool big_endian
>
6260 Arm_relobj
<big_endian
>::do_count_local_symbols(
6261 Stringpool_template
<char>* pool
,
6262 Stringpool_template
<char>* dynpool
)
6264 // We need to fix-up the values of any local symbols whose type are
6267 // Ask parent to count the local symbols.
6268 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
6269 const unsigned int loccount
= this->local_symbol_count();
6273 // Intialize the thumb function bit-vector.
6274 std::vector
<bool> empty_vector(loccount
, false);
6275 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
6277 // Read the symbol table section header.
6278 const unsigned int symtab_shndx
= this->symtab_shndx();
6279 elfcpp::Shdr
<32, big_endian
>
6280 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6281 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6283 // Read the local symbols.
6284 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
6285 gold_assert(loccount
== symtabshdr
.get_sh_info());
6286 off_t locsize
= loccount
* sym_size
;
6287 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6288 locsize
, true, true);
6290 // For mapping symbol processing, we need to read the symbol names.
6291 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
6292 if (strtab_shndx
>= this->shnum())
6294 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
6298 elfcpp::Shdr
<32, big_endian
>
6299 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
6300 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
6302 this->error(_("symbol table name section has wrong type: %u"),
6303 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
6306 const char* pnames
=
6307 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
6308 strtabshdr
.get_sh_size(),
6311 // Loop over the local symbols and mark any local symbols pointing
6312 // to THUMB functions.
6314 // Skip the first dummy symbol.
6316 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
6317 this->local_values();
6318 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
6320 elfcpp::Sym
<32, big_endian
> sym(psyms
);
6321 elfcpp::STT st_type
= sym
.get_st_type();
6322 Symbol_value
<32>& lv((*plocal_values
)[i
]);
6323 Arm_address input_value
= lv
.input_value();
6325 // Check to see if this is a mapping symbol.
6326 const char* sym_name
= pnames
+ sym
.get_st_name();
6327 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
6330 unsigned int input_shndx
=
6331 this->adjust_sym_shndx(i
, sym
.get_st_shndx(), &is_ordinary
);
6332 gold_assert(is_ordinary
);
6334 // Strip of LSB in case this is a THUMB symbol.
6335 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
6336 this->mapping_symbols_info_
[msp
] = sym_name
[1];
6339 if (st_type
== elfcpp::STT_ARM_TFUNC
6340 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
6342 // This is a THUMB function. Mark this and canonicalize the
6343 // symbol value by setting LSB.
6344 this->local_symbol_is_thumb_function_
[i
] = true;
6345 if ((input_value
& 1) == 0)
6346 lv
.set_input_value(input_value
| 1);
6351 // Relocate sections.
6352 template<bool big_endian
>
6354 Arm_relobj
<big_endian
>::do_relocate_sections(
6355 const Symbol_table
* symtab
,
6356 const Layout
* layout
,
6357 const unsigned char* pshdrs
,
6359 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
6361 // Call parent to relocate sections.
6362 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
6365 // We do not generate stubs if doing a relocatable link.
6366 if (parameters
->options().relocatable())
6369 // Relocate stub tables.
6370 unsigned int shnum
= this->shnum();
6372 Target_arm
<big_endian
>* arm_target
=
6373 Target_arm
<big_endian
>::default_target();
6375 Relocate_info
<32, big_endian
> relinfo
;
6376 relinfo
.symtab
= symtab
;
6377 relinfo
.layout
= layout
;
6378 relinfo
.object
= this;
6380 for (unsigned int i
= 1; i
< shnum
; ++i
)
6382 Arm_input_section
<big_endian
>* arm_input_section
=
6383 arm_target
->find_arm_input_section(this, i
);
6385 if (arm_input_section
!= NULL
6386 && arm_input_section
->is_stub_table_owner()
6387 && !arm_input_section
->stub_table()->empty())
6389 // We cannot discard a section if it owns a stub table.
6390 Output_section
* os
= this->output_section(i
);
6391 gold_assert(os
!= NULL
);
6393 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
6394 relinfo
.reloc_shdr
= NULL
;
6395 relinfo
.data_shndx
= i
;
6396 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
6398 gold_assert((*pviews
)[i
].view
!= NULL
);
6400 // We are passed the output section view. Adjust it to cover the
6402 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
6403 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
6404 && ((stub_table
->address() + stub_table
->data_size())
6405 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
6407 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
6408 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
6409 Arm_address address
= stub_table
->address();
6410 section_size_type view_size
= stub_table
->data_size();
6412 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
6416 // Apply Cortex A8 workaround if applicable.
6417 if (this->section_has_cortex_a8_workaround(i
))
6419 unsigned char* view
= (*pviews
)[i
].view
;
6420 Arm_address view_address
= (*pviews
)[i
].address
;
6421 section_size_type view_size
= (*pviews
)[i
].view_size
;
6422 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
6424 // Adjust view to cover section.
6425 Output_section
* os
= this->output_section(i
);
6426 gold_assert(os
!= NULL
);
6427 Arm_address section_address
=
6428 this->simple_input_section_output_address(i
, os
);
6429 uint64_t section_size
= this->section_size(i
);
6431 gold_assert(section_address
>= view_address
6432 && ((section_address
+ section_size
)
6433 <= (view_address
+ view_size
)));
6435 unsigned char* section_view
= view
+ (section_address
- view_address
);
6437 // Apply the Cortex-A8 workaround to the output address range
6438 // corresponding to this input section.
6439 stub_table
->apply_cortex_a8_workaround_to_address_range(
6448 // Find the linked text section of an EXIDX section by looking the the first
6449 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6450 // must be linked to to its associated code section via the sh_link field of
6451 // its section header. However, some tools are broken and the link is not
6452 // always set. LD just drops such an EXIDX section silently, causing the
6453 // associated code not unwindabled. Here we try a little bit harder to
6454 // discover the linked code section.
6456 // PSHDR points to the section header of a relocation section of an EXIDX
6457 // section. If we can find a linked text section, return true and
6458 // store the text section index in the location PSHNDX. Otherwise
6461 template<bool big_endian
>
6463 Arm_relobj
<big_endian
>::find_linked_text_section(
6464 const unsigned char* pshdr
,
6465 const unsigned char* psyms
,
6466 unsigned int* pshndx
)
6468 elfcpp::Shdr
<32, big_endian
> shdr(pshdr
);
6470 // If there is no relocation, we cannot find the linked text section.
6472 if (shdr
.get_sh_type() == elfcpp::SHT_REL
)
6473 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
6475 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
6476 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
6478 // Get the relocations.
6479 const unsigned char* prelocs
=
6480 this->get_view(shdr
.get_sh_offset(), shdr
.get_sh_size(), true, false);
6482 // Find the REL31 relocation for the first word of the first EXIDX entry.
6483 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6485 Arm_address r_offset
;
6486 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
;
6487 if (shdr
.get_sh_type() == elfcpp::SHT_REL
)
6489 typename
elfcpp::Rel
<32, big_endian
> reloc(prelocs
);
6490 r_info
= reloc
.get_r_info();
6491 r_offset
= reloc
.get_r_offset();
6495 typename
elfcpp::Rela
<32, big_endian
> reloc(prelocs
);
6496 r_info
= reloc
.get_r_info();
6497 r_offset
= reloc
.get_r_offset();
6500 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
6501 if (r_type
!= elfcpp::R_ARM_PREL31
&& r_type
!= elfcpp::R_ARM_SBREL31
)
6504 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6506 || r_sym
>= this->local_symbol_count()
6510 // This is the relocation for the first word of the first EXIDX entry.
6511 // We expect to see a local section symbol.
6512 const int sym_size
= elfcpp::Elf_sizes
<32>::sym_size
;
6513 elfcpp::Sym
<32, big_endian
> sym(psyms
+ r_sym
* sym_size
);
6514 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
6518 this->adjust_sym_shndx(r_sym
, sym
.get_st_shndx(), &is_ordinary
);
6519 gold_assert(is_ordinary
);
6529 // Make an EXIDX input section object for an EXIDX section whose index is
6530 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6531 // is the section index of the linked text section.
6533 template<bool big_endian
>
6535 Arm_relobj
<big_endian
>::make_exidx_input_section(
6537 const elfcpp::Shdr
<32, big_endian
>& shdr
,
6538 unsigned int text_shndx
,
6539 const elfcpp::Shdr
<32, big_endian
>& text_shdr
)
6541 // Create an Arm_exidx_input_section object for this EXIDX section.
6542 Arm_exidx_input_section
* exidx_input_section
=
6543 new Arm_exidx_input_section(this, shndx
, text_shndx
, shdr
.get_sh_size(),
6544 shdr
.get_sh_addralign());
6546 gold_assert(this->exidx_section_map_
[shndx
] == NULL
);
6547 this->exidx_section_map_
[shndx
] = exidx_input_section
;
6549 if (text_shndx
== elfcpp::SHN_UNDEF
|| text_shndx
>= this->shnum())
6551 gold_error(_("EXIDX section %s(%u) links to invalid section %u in %s"),
6552 this->section_name(shndx
).c_str(), shndx
, text_shndx
,
6553 this->name().c_str());
6554 exidx_input_section
->set_has_errors();
6556 else if (this->exidx_section_map_
[text_shndx
] != NULL
)
6558 unsigned other_exidx_shndx
=
6559 this->exidx_section_map_
[text_shndx
]->shndx();
6560 gold_error(_("EXIDX sections %s(%u) and %s(%u) both link to text section"
6562 this->section_name(shndx
).c_str(), shndx
,
6563 this->section_name(other_exidx_shndx
).c_str(),
6564 other_exidx_shndx
, this->section_name(text_shndx
).c_str(),
6565 text_shndx
, this->name().c_str());
6566 exidx_input_section
->set_has_errors();
6569 this->exidx_section_map_
[text_shndx
] = exidx_input_section
;
6571 // Check section flags of text section.
6572 if ((text_shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
6574 gold_error(_("EXIDX section %s(%u) links to non-allocated section %s(%u) "
6576 this->section_name(shndx
).c_str(), shndx
,
6577 this->section_name(text_shndx
).c_str(), text_shndx
,
6578 this->name().c_str());
6579 exidx_input_section
->set_has_errors();
6581 else if ((text_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) == 0)
6582 // I would like to make this an error but currenlty ld just ignores
6584 gold_warning(_("EXIDX section %s(%u) links to non-executable section "
6586 this->section_name(shndx
).c_str(), shndx
,
6587 this->section_name(text_shndx
).c_str(), text_shndx
,
6588 this->name().c_str());
6591 // Read the symbol information.
6593 template<bool big_endian
>
6595 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
6597 // Call parent class to read symbol information.
6598 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
6600 // If this input file is a binary file, it has no processor
6601 // specific flags and attributes section.
6602 Input_file::Format format
= this->input_file()->format();
6603 if (format
!= Input_file::FORMAT_ELF
)
6605 gold_assert(format
== Input_file::FORMAT_BINARY
);
6606 this->merge_flags_and_attributes_
= false;
6610 // Read processor-specific flags in ELF file header.
6611 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
6612 elfcpp::Elf_sizes
<32>::ehdr_size
,
6614 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
6615 this->processor_specific_flags_
= ehdr
.get_e_flags();
6617 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6619 std::vector
<unsigned int> deferred_exidx_sections
;
6620 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6621 const unsigned char* pshdrs
= sd
->section_headers
->data();
6622 const unsigned char* ps
= pshdrs
+ shdr_size
;
6623 bool must_merge_flags_and_attributes
= false;
6624 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
6626 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6628 // Sometimes an object has no contents except the section name string
6629 // table and an empty symbol table with the undefined symbol. We
6630 // don't want to merge processor-specific flags from such an object.
6631 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
6633 // Symbol table is not empty.
6634 const elfcpp::Elf_types
<32>::Elf_WXword sym_size
=
6635 elfcpp::Elf_sizes
<32>::sym_size
;
6636 if (shdr
.get_sh_size() > sym_size
)
6637 must_merge_flags_and_attributes
= true;
6639 else if (shdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
6640 // If this is neither an empty symbol table nor a string table,
6642 must_merge_flags_and_attributes
= true;
6644 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
6646 gold_assert(this->attributes_section_data_
== NULL
);
6647 section_offset_type section_offset
= shdr
.get_sh_offset();
6648 section_size_type section_size
=
6649 convert_to_section_size_type(shdr
.get_sh_size());
6650 File_view
* view
= this->get_lasting_view(section_offset
,
6651 section_size
, true, false);
6652 this->attributes_section_data_
=
6653 new Attributes_section_data(view
->data(), section_size
);
6655 else if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
6657 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
6658 if (text_shndx
== elfcpp::SHN_UNDEF
)
6659 deferred_exidx_sections
.push_back(i
);
6662 elfcpp::Shdr
<32, big_endian
> text_shdr(pshdrs
6663 + text_shndx
* shdr_size
);
6664 this->make_exidx_input_section(i
, shdr
, text_shndx
, text_shdr
);
6666 // EHABI 4.4.1 requires that SHF_LINK_ORDER flag to be set.
6667 if ((shdr
.get_sh_flags() & elfcpp::SHF_LINK_ORDER
) == 0)
6668 gold_warning(_("SHF_LINK_ORDER not set in EXIDX section %s of %s"),
6669 this->section_name(i
).c_str(), this->name().c_str());
6674 if (!must_merge_flags_and_attributes
)
6676 gold_assert(deferred_exidx_sections
.empty());
6677 this->merge_flags_and_attributes_
= false;
6681 // Some tools are broken and they do not set the link of EXIDX sections.
6682 // We look at the first relocation to figure out the linked sections.
6683 if (!deferred_exidx_sections
.empty())
6685 // We need to go over the section headers again to find the mapping
6686 // from sections being relocated to their relocation sections. This is
6687 // a bit inefficient as we could do that in the loop above. However,
6688 // we do not expect any deferred EXIDX sections normally. So we do not
6689 // want to slow down the most common path.
6690 typedef Unordered_map
<unsigned int, unsigned int> Reloc_map
;
6691 Reloc_map reloc_map
;
6692 ps
= pshdrs
+ shdr_size
;
6693 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
6695 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6696 elfcpp::Elf_Word sh_type
= shdr
.get_sh_type();
6697 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
6699 unsigned int info_shndx
= this->adjust_shndx(shdr
.get_sh_info());
6700 if (info_shndx
>= this->shnum())
6701 gold_error(_("relocation section %u has invalid info %u"),
6703 Reloc_map::value_type
value(info_shndx
, i
);
6704 std::pair
<Reloc_map::iterator
, bool> result
=
6705 reloc_map
.insert(value
);
6707 gold_error(_("section %u has multiple relocation sections "
6709 info_shndx
, i
, reloc_map
[info_shndx
]);
6713 // Read the symbol table section header.
6714 const unsigned int symtab_shndx
= this->symtab_shndx();
6715 elfcpp::Shdr
<32, big_endian
>
6716 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6717 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6719 // Read the local symbols.
6720 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
6721 const unsigned int loccount
= this->local_symbol_count();
6722 gold_assert(loccount
== symtabshdr
.get_sh_info());
6723 off_t locsize
= loccount
* sym_size
;
6724 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6725 locsize
, true, true);
6727 // Process the deferred EXIDX sections.
6728 for(unsigned int i
= 0; i
< deferred_exidx_sections
.size(); ++i
)
6730 unsigned int shndx
= deferred_exidx_sections
[i
];
6731 elfcpp::Shdr
<32, big_endian
> shdr(pshdrs
+ shndx
* shdr_size
);
6732 unsigned int text_shndx
= elfcpp::SHN_UNDEF
;
6733 Reloc_map::const_iterator it
= reloc_map
.find(shndx
);
6734 if (it
!= reloc_map
.end())
6735 find_linked_text_section(pshdrs
+ it
->second
* shdr_size
,
6736 psyms
, &text_shndx
);
6737 elfcpp::Shdr
<32, big_endian
> text_shdr(pshdrs
6738 + text_shndx
* shdr_size
);
6739 this->make_exidx_input_section(shndx
, shdr
, text_shndx
, text_shdr
);
6744 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6745 // sections for unwinding. These sections are referenced implicitly by
6746 // text sections linked in the section headers. If we ignore these implict
6747 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6748 // will be garbage-collected incorrectly. Hence we override the same function
6749 // in the base class to handle these implicit references.
6751 template<bool big_endian
>
6753 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
6755 Read_relocs_data
* rd
)
6757 // First, call base class method to process relocations in this object.
6758 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
6760 // If --gc-sections is not specified, there is nothing more to do.
6761 // This happens when --icf is used but --gc-sections is not.
6762 if (!parameters
->options().gc_sections())
6765 unsigned int shnum
= this->shnum();
6766 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6767 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
6771 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6772 // to these from the linked text sections.
6773 const unsigned char* ps
= pshdrs
+ shdr_size
;
6774 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
6776 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6777 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
6779 // Found an .ARM.exidx section, add it to the set of reachable
6780 // sections from its linked text section.
6781 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
6782 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
6787 // Update output local symbol count. Owing to EXIDX entry merging, some local
6788 // symbols will be removed in output. Adjust output local symbol count
6789 // accordingly. We can only changed the static output local symbol count. It
6790 // is too late to change the dynamic symbols.
6792 template<bool big_endian
>
6794 Arm_relobj
<big_endian
>::update_output_local_symbol_count()
6796 // Caller should check that this needs updating. We want caller checking
6797 // because output_local_symbol_count_needs_update() is most likely inlined.
6798 gold_assert(this->output_local_symbol_count_needs_update_
);
6800 gold_assert(this->symtab_shndx() != -1U);
6801 if (this->symtab_shndx() == 0)
6803 // This object has no symbols. Weird but legal.
6807 // Read the symbol table section header.
6808 const unsigned int symtab_shndx
= this->symtab_shndx();
6809 elfcpp::Shdr
<32, big_endian
>
6810 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6811 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6813 // Read the local symbols.
6814 const int sym_size
= elfcpp::Elf_sizes
<32>::sym_size
;
6815 const unsigned int loccount
= this->local_symbol_count();
6816 gold_assert(loccount
== symtabshdr
.get_sh_info());
6817 off_t locsize
= loccount
* sym_size
;
6818 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6819 locsize
, true, true);
6821 // Loop over the local symbols.
6823 typedef typename Sized_relobj
<32, big_endian
>::Output_sections
6825 const Output_sections
& out_sections(this->output_sections());
6826 unsigned int shnum
= this->shnum();
6827 unsigned int count
= 0;
6828 // Skip the first, dummy, symbol.
6830 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
6832 elfcpp::Sym
<32, big_endian
> sym(psyms
);
6834 Symbol_value
<32>& lv((*this->local_values())[i
]);
6836 // This local symbol was already discarded by do_count_local_symbols.
6837 if (lv
.is_output_symtab_index_set() && !lv
.has_output_symtab_entry())
6841 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
6846 Output_section
* os
= out_sections
[shndx
];
6848 // This local symbol no longer has an output section. Discard it.
6851 lv
.set_no_output_symtab_entry();
6855 // Currently we only discard parts of EXIDX input sections.
6856 // We explicitly check for a merged EXIDX input section to avoid
6857 // calling Output_section_data::output_offset unless necessary.
6858 if ((this->get_output_section_offset(shndx
) == invalid_address
)
6859 && (this->exidx_input_section_by_shndx(shndx
) != NULL
))
6861 section_offset_type output_offset
=
6862 os
->output_offset(this, shndx
, lv
.input_value());
6863 if (output_offset
== -1)
6865 // This symbol is defined in a part of an EXIDX input section
6866 // that is discarded due to entry merging.
6867 lv
.set_no_output_symtab_entry();
6876 this->set_output_local_symbol_count(count
);
6877 this->output_local_symbol_count_needs_update_
= false;
6880 // Arm_dynobj methods.
6882 // Read the symbol information.
6884 template<bool big_endian
>
6886 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
6888 // Call parent class to read symbol information.
6889 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
6891 // Read processor-specific flags in ELF file header.
6892 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
6893 elfcpp::Elf_sizes
<32>::ehdr_size
,
6895 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
6896 this->processor_specific_flags_
= ehdr
.get_e_flags();
6898 // Read the attributes section if there is one.
6899 // We read from the end because gas seems to put it near the end of
6900 // the section headers.
6901 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6902 const unsigned char* ps
=
6903 sd
->section_headers
->data() + shdr_size
* (this->shnum() - 1);
6904 for (unsigned int i
= this->shnum(); i
> 0; --i
, ps
-= shdr_size
)
6906 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6907 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
6909 section_offset_type section_offset
= shdr
.get_sh_offset();
6910 section_size_type section_size
=
6911 convert_to_section_size_type(shdr
.get_sh_size());
6912 File_view
* view
= this->get_lasting_view(section_offset
,
6913 section_size
, true, false);
6914 this->attributes_section_data_
=
6915 new Attributes_section_data(view
->data(), section_size
);
6921 // Stub_addend_reader methods.
6923 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6925 template<bool big_endian
>
6926 elfcpp::Elf_types
<32>::Elf_Swxword
6927 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
6928 unsigned int r_type
,
6929 const unsigned char* view
,
6930 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
6932 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
6936 case elfcpp::R_ARM_CALL
:
6937 case elfcpp::R_ARM_JUMP24
:
6938 case elfcpp::R_ARM_PLT32
:
6940 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
6941 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6942 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
6943 return utils::sign_extend
<26>(val
<< 2);
6946 case elfcpp::R_ARM_THM_CALL
:
6947 case elfcpp::R_ARM_THM_JUMP24
:
6948 case elfcpp::R_ARM_THM_XPC22
:
6950 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
6951 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6952 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
6953 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
6954 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
6957 case elfcpp::R_ARM_THM_JUMP19
:
6959 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
6960 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6961 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
6962 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
6963 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
6971 // Arm_output_data_got methods.
6973 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6974 // The first one is initialized to be 1, which is the module index for
6975 // the main executable and the second one 0. A reloc of the type
6976 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6977 // be applied by gold. GSYM is a global symbol.
6979 template<bool big_endian
>
6981 Arm_output_data_got
<big_endian
>::add_tls_gd32_with_static_reloc(
6982 unsigned int got_type
,
6985 if (gsym
->has_got_offset(got_type
))
6988 // We are doing a static link. Just mark it as belong to module 1,
6990 unsigned int got_offset
= this->add_constant(1);
6991 gsym
->set_got_offset(got_type
, got_offset
);
6992 got_offset
= this->add_constant(0);
6993 this->static_relocs_
.push_back(Static_reloc(got_offset
,
6994 elfcpp::R_ARM_TLS_DTPOFF32
,
6998 // Same as the above but for a local symbol.
7000 template<bool big_endian
>
7002 Arm_output_data_got
<big_endian
>::add_tls_gd32_with_static_reloc(
7003 unsigned int got_type
,
7004 Sized_relobj
<32, big_endian
>* object
,
7007 if (object
->local_has_got_offset(index
, got_type
))
7010 // We are doing a static link. Just mark it as belong to module 1,
7012 unsigned int got_offset
= this->add_constant(1);
7013 object
->set_local_got_offset(index
, got_type
, got_offset
);
7014 got_offset
= this->add_constant(0);
7015 this->static_relocs_
.push_back(Static_reloc(got_offset
,
7016 elfcpp::R_ARM_TLS_DTPOFF32
,
7020 template<bool big_endian
>
7022 Arm_output_data_got
<big_endian
>::do_write(Output_file
* of
)
7024 // Call parent to write out GOT.
7025 Output_data_got
<32, big_endian
>::do_write(of
);
7027 // We are done if there is no fix up.
7028 if (this->static_relocs_
.empty())
7031 gold_assert(parameters
->doing_static_link());
7033 const off_t offset
= this->offset();
7034 const section_size_type oview_size
=
7035 convert_to_section_size_type(this->data_size());
7036 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7038 Output_segment
* tls_segment
= this->layout_
->tls_segment();
7039 gold_assert(tls_segment
!= NULL
);
7041 // The thread pointer $tp points to the TCB, which is followed by the
7042 // TLS. So we need to adjust $tp relative addressing by this amount.
7043 Arm_address aligned_tcb_size
=
7044 align_address(ARM_TCB_SIZE
, tls_segment
->maximum_alignment());
7046 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
7048 Static_reloc
& reloc(this->static_relocs_
[i
]);
7051 if (!reloc
.symbol_is_global())
7053 Sized_relobj
<32, big_endian
>* object
= reloc
.relobj();
7054 const Symbol_value
<32>* psymval
=
7055 reloc
.relobj()->local_symbol(reloc
.index());
7057 // We are doing static linking. Issue an error and skip this
7058 // relocation if the symbol is undefined or in a discarded_section.
7060 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
7061 if ((shndx
== elfcpp::SHN_UNDEF
)
7063 && shndx
!= elfcpp::SHN_UNDEF
7064 && !object
->is_section_included(shndx
)
7065 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
7067 gold_error(_("undefined or discarded local symbol %u from "
7068 " object %s in GOT"),
7069 reloc
.index(), reloc
.relobj()->name().c_str());
7073 value
= psymval
->value(object
, 0);
7077 const Symbol
* gsym
= reloc
.symbol();
7078 gold_assert(gsym
!= NULL
);
7079 if (gsym
->is_forwarder())
7080 gsym
= this->symbol_table_
->resolve_forwards(gsym
);
7082 // We are doing static linking. Issue an error and skip this
7083 // relocation if the symbol is undefined or in a discarded_section
7084 // unless it is a weakly_undefined symbol.
7085 if ((gsym
->is_defined_in_discarded_section()
7086 || gsym
->is_undefined())
7087 && !gsym
->is_weak_undefined())
7089 gold_error(_("undefined or discarded symbol %s in GOT"),
7094 if (!gsym
->is_weak_undefined())
7096 const Sized_symbol
<32>* sym
=
7097 static_cast<const Sized_symbol
<32>*>(gsym
);
7098 value
= sym
->value();
7104 unsigned got_offset
= reloc
.got_offset();
7105 gold_assert(got_offset
< oview_size
);
7107 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7108 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
7110 switch (reloc
.r_type())
7112 case elfcpp::R_ARM_TLS_DTPOFF32
:
7115 case elfcpp::R_ARM_TLS_TPOFF32
:
7116 x
= value
+ aligned_tcb_size
;
7121 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
7124 of
->write_output_view(offset
, oview_size
, oview
);
7127 // A class to handle the PLT data.
7129 template<bool big_endian
>
7130 class Output_data_plt_arm
: public Output_section_data
7133 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
7136 Output_data_plt_arm(Layout
*, Output_data_space
*);
7138 // Add an entry to the PLT.
7140 add_entry(Symbol
* gsym
);
7142 // Return the .rel.plt section data.
7143 const Reloc_section
*
7145 { return this->rel_
; }
7147 // Return the number of PLT entries.
7150 { return this->count_
; }
7152 // Return the offset of the first non-reserved PLT entry.
7154 first_plt_entry_offset()
7155 { return sizeof(first_plt_entry
); }
7157 // Return the size of a PLT entry.
7159 get_plt_entry_size()
7160 { return sizeof(plt_entry
); }
7164 do_adjust_output_section(Output_section
* os
);
7166 // Write to a map file.
7168 do_print_to_mapfile(Mapfile
* mapfile
) const
7169 { mapfile
->print_output_data(this, _("** PLT")); }
7172 // Template for the first PLT entry.
7173 static const uint32_t first_plt_entry
[5];
7175 // Template for subsequent PLT entries.
7176 static const uint32_t plt_entry
[3];
7178 // Set the final size.
7180 set_final_data_size()
7182 this->set_data_size(sizeof(first_plt_entry
)
7183 + this->count_
* sizeof(plt_entry
));
7186 // Write out the PLT data.
7188 do_write(Output_file
*);
7190 // The reloc section.
7191 Reloc_section
* rel_
;
7192 // The .got.plt section.
7193 Output_data_space
* got_plt_
;
7194 // The number of PLT entries.
7195 unsigned int count_
;
7198 // Create the PLT section. The ordinary .got section is an argument,
7199 // since we need to refer to the start. We also create our own .got
7200 // section just for PLT entries.
7202 template<bool big_endian
>
7203 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
7204 Output_data_space
* got_plt
)
7205 : Output_section_data(4), got_plt_(got_plt
), count_(0)
7207 this->rel_
= new Reloc_section(false);
7208 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
7209 elfcpp::SHF_ALLOC
, this->rel_
,
7210 ORDER_DYNAMIC_PLT_RELOCS
, false);
7213 template<bool big_endian
>
7215 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
7220 // Add an entry to the PLT.
7222 template<bool big_endian
>
7224 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
7226 gold_assert(!gsym
->has_plt_offset());
7228 // Note that when setting the PLT offset we skip the initial
7229 // reserved PLT entry.
7230 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
7231 + sizeof(first_plt_entry
));
7235 section_offset_type got_offset
= this->got_plt_
->current_data_size();
7237 // Every PLT entry needs a GOT entry which points back to the PLT
7238 // entry (this will be changed by the dynamic linker, normally
7239 // lazily when the function is called).
7240 this->got_plt_
->set_current_data_size(got_offset
+ 4);
7242 // Every PLT entry needs a reloc.
7243 gsym
->set_needs_dynsym_entry();
7244 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
7247 // Note that we don't need to save the symbol. The contents of the
7248 // PLT are independent of which symbols are used. The symbols only
7249 // appear in the relocations.
7253 // FIXME: This is not very flexible. Right now this has only been tested
7254 // on armv5te. If we are to support additional architecture features like
7255 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7257 // The first entry in the PLT.
7258 template<bool big_endian
>
7259 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
7261 0xe52de004, // str lr, [sp, #-4]!
7262 0xe59fe004, // ldr lr, [pc, #4]
7263 0xe08fe00e, // add lr, pc, lr
7264 0xe5bef008, // ldr pc, [lr, #8]!
7265 0x00000000, // &GOT[0] - .
7268 // Subsequent entries in the PLT.
7270 template<bool big_endian
>
7271 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
7273 0xe28fc600, // add ip, pc, #0xNN00000
7274 0xe28cca00, // add ip, ip, #0xNN000
7275 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7278 // Write out the PLT. This uses the hand-coded instructions above,
7279 // and adjusts them as needed. This is all specified by the arm ELF
7280 // Processor Supplement.
7282 template<bool big_endian
>
7284 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
7286 const off_t offset
= this->offset();
7287 const section_size_type oview_size
=
7288 convert_to_section_size_type(this->data_size());
7289 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7291 const off_t got_file_offset
= this->got_plt_
->offset();
7292 const section_size_type got_size
=
7293 convert_to_section_size_type(this->got_plt_
->data_size());
7294 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
7296 unsigned char* pov
= oview
;
7298 Arm_address plt_address
= this->address();
7299 Arm_address got_address
= this->got_plt_
->address();
7301 // Write first PLT entry. All but the last word are constants.
7302 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
7303 / sizeof(plt_entry
[0]));
7304 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
7305 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
7306 // Last word in first PLT entry is &GOT[0] - .
7307 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
7308 got_address
- (plt_address
+ 16));
7309 pov
+= sizeof(first_plt_entry
);
7311 unsigned char* got_pov
= got_view
;
7313 memset(got_pov
, 0, 12);
7316 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
7317 unsigned int plt_offset
= sizeof(first_plt_entry
);
7318 unsigned int plt_rel_offset
= 0;
7319 unsigned int got_offset
= 12;
7320 const unsigned int count
= this->count_
;
7321 for (unsigned int i
= 0;
7324 pov
+= sizeof(plt_entry
),
7326 plt_offset
+= sizeof(plt_entry
),
7327 plt_rel_offset
+= rel_size
,
7330 // Set and adjust the PLT entry itself.
7331 int32_t offset
= ((got_address
+ got_offset
)
7332 - (plt_address
+ plt_offset
+ 8));
7334 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
7335 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
7336 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
7337 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
7338 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
7339 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
7340 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
7342 // Set the entry in the GOT.
7343 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
7346 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
7347 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
7349 of
->write_output_view(offset
, oview_size
, oview
);
7350 of
->write_output_view(got_file_offset
, got_size
, got_view
);
7353 // Create a PLT entry for a global symbol.
7355 template<bool big_endian
>
7357 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
7360 if (gsym
->has_plt_offset())
7363 if (this->plt_
== NULL
)
7365 // Create the GOT sections first.
7366 this->got_section(symtab
, layout
);
7368 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
7369 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
7371 | elfcpp::SHF_EXECINSTR
),
7372 this->plt_
, ORDER_PLT
, false);
7374 this->plt_
->add_entry(gsym
);
7377 // Return the number of entries in the PLT.
7379 template<bool big_endian
>
7381 Target_arm
<big_endian
>::plt_entry_count() const
7383 if (this->plt_
== NULL
)
7385 return this->plt_
->entry_count();
7388 // Return the offset of the first non-reserved PLT entry.
7390 template<bool big_endian
>
7392 Target_arm
<big_endian
>::first_plt_entry_offset() const
7394 return Output_data_plt_arm
<big_endian
>::first_plt_entry_offset();
7397 // Return the size of each PLT entry.
7399 template<bool big_endian
>
7401 Target_arm
<big_endian
>::plt_entry_size() const
7403 return Output_data_plt_arm
<big_endian
>::get_plt_entry_size();
7406 // Get the section to use for TLS_DESC relocations.
7408 template<bool big_endian
>
7409 typename Target_arm
<big_endian
>::Reloc_section
*
7410 Target_arm
<big_endian
>::rel_tls_desc_section(Layout
* layout
) const
7412 return this->plt_section()->rel_tls_desc(layout
);
7415 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7417 template<bool big_endian
>
7419 Target_arm
<big_endian
>::define_tls_base_symbol(
7420 Symbol_table
* symtab
,
7423 if (this->tls_base_symbol_defined_
)
7426 Output_segment
* tls_segment
= layout
->tls_segment();
7427 if (tls_segment
!= NULL
)
7429 bool is_exec
= parameters
->options().output_is_executable();
7430 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
7431 Symbol_table::PREDEFINED
,
7435 elfcpp::STV_HIDDEN
, 0,
7437 ? Symbol::SEGMENT_END
7438 : Symbol::SEGMENT_START
),
7441 this->tls_base_symbol_defined_
= true;
7444 // Create a GOT entry for the TLS module index.
7446 template<bool big_endian
>
7448 Target_arm
<big_endian
>::got_mod_index_entry(
7449 Symbol_table
* symtab
,
7451 Sized_relobj
<32, big_endian
>* object
)
7453 if (this->got_mod_index_offset_
== -1U)
7455 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
7456 Arm_output_data_got
<big_endian
>* got
= this->got_section(symtab
, layout
);
7457 unsigned int got_offset
;
7458 if (!parameters
->doing_static_link())
7460 got_offset
= got
->add_constant(0);
7461 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
7462 rel_dyn
->add_local(object
, 0, elfcpp::R_ARM_TLS_DTPMOD32
, got
,
7467 // We are doing a static link. Just mark it as belong to module 1,
7469 got_offset
= got
->add_constant(1);
7472 got
->add_constant(0);
7473 this->got_mod_index_offset_
= got_offset
;
7475 return this->got_mod_index_offset_
;
7478 // Optimize the TLS relocation type based on what we know about the
7479 // symbol. IS_FINAL is true if the final address of this symbol is
7480 // known at link time.
7482 template<bool big_endian
>
7483 tls::Tls_optimization
7484 Target_arm
<big_endian
>::optimize_tls_reloc(bool, int)
7486 // FIXME: Currently we do not do any TLS optimization.
7487 return tls::TLSOPT_NONE
;
7490 // Report an unsupported relocation against a local symbol.
7492 template<bool big_endian
>
7494 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
7495 Sized_relobj
<32, big_endian
>* object
,
7496 unsigned int r_type
)
7498 gold_error(_("%s: unsupported reloc %u against local symbol"),
7499 object
->name().c_str(), r_type
);
7502 // We are about to emit a dynamic relocation of type R_TYPE. If the
7503 // dynamic linker does not support it, issue an error. The GNU linker
7504 // only issues a non-PIC error for an allocated read-only section.
7505 // Here we know the section is allocated, but we don't know that it is
7506 // read-only. But we check for all the relocation types which the
7507 // glibc dynamic linker supports, so it seems appropriate to issue an
7508 // error even if the section is not read-only.
7510 template<bool big_endian
>
7512 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
7513 unsigned int r_type
)
7517 // These are the relocation types supported by glibc for ARM.
7518 case elfcpp::R_ARM_RELATIVE
:
7519 case elfcpp::R_ARM_COPY
:
7520 case elfcpp::R_ARM_GLOB_DAT
:
7521 case elfcpp::R_ARM_JUMP_SLOT
:
7522 case elfcpp::R_ARM_ABS32
:
7523 case elfcpp::R_ARM_ABS32_NOI
:
7524 case elfcpp::R_ARM_PC24
:
7525 // FIXME: The following 3 types are not supported by Android's dynamic
7527 case elfcpp::R_ARM_TLS_DTPMOD32
:
7528 case elfcpp::R_ARM_TLS_DTPOFF32
:
7529 case elfcpp::R_ARM_TLS_TPOFF32
:
7534 // This prevents us from issuing more than one error per reloc
7535 // section. But we can still wind up issuing more than one
7536 // error per object file.
7537 if (this->issued_non_pic_error_
)
7539 const Arm_reloc_property
* reloc_property
=
7540 arm_reloc_property_table
->get_reloc_property(r_type
);
7541 gold_assert(reloc_property
!= NULL
);
7542 object
->error(_("requires unsupported dynamic reloc %s; "
7543 "recompile with -fPIC"),
7544 reloc_property
->name().c_str());
7545 this->issued_non_pic_error_
= true;
7549 case elfcpp::R_ARM_NONE
:
7554 // Scan a relocation for a local symbol.
7555 // FIXME: This only handles a subset of relocation types used by Android
7556 // on ARM v5te devices.
7558 template<bool big_endian
>
7560 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
7563 Sized_relobj
<32, big_endian
>* object
,
7564 unsigned int data_shndx
,
7565 Output_section
* output_section
,
7566 const elfcpp::Rel
<32, big_endian
>& reloc
,
7567 unsigned int r_type
,
7568 const elfcpp::Sym
<32, big_endian
>& lsym
)
7570 r_type
= get_real_reloc_type(r_type
);
7573 case elfcpp::R_ARM_NONE
:
7574 case elfcpp::R_ARM_V4BX
:
7575 case elfcpp::R_ARM_GNU_VTENTRY
:
7576 case elfcpp::R_ARM_GNU_VTINHERIT
:
7579 case elfcpp::R_ARM_ABS32
:
7580 case elfcpp::R_ARM_ABS32_NOI
:
7581 // If building a shared library (or a position-independent
7582 // executable), we need to create a dynamic relocation for
7583 // this location. The relocation applied at link time will
7584 // apply the link-time value, so we flag the location with
7585 // an R_ARM_RELATIVE relocation so the dynamic loader can
7586 // relocate it easily.
7587 if (parameters
->options().output_is_position_independent())
7589 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7590 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7591 // If we are to add more other reloc types than R_ARM_ABS32,
7592 // we need to add check_non_pic(object, r_type) here.
7593 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
7594 output_section
, data_shndx
,
7595 reloc
.get_r_offset());
7599 case elfcpp::R_ARM_ABS16
:
7600 case elfcpp::R_ARM_ABS12
:
7601 case elfcpp::R_ARM_THM_ABS5
:
7602 case elfcpp::R_ARM_ABS8
:
7603 case elfcpp::R_ARM_BASE_ABS
:
7604 case elfcpp::R_ARM_MOVW_ABS_NC
:
7605 case elfcpp::R_ARM_MOVT_ABS
:
7606 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7607 case elfcpp::R_ARM_THM_MOVT_ABS
:
7608 // If building a shared library (or a position-independent
7609 // executable), we need to create a dynamic relocation for
7610 // this location. Because the addend needs to remain in the
7611 // data section, we need to be careful not to apply this
7612 // relocation statically.
7613 if (parameters
->options().output_is_position_independent())
7615 check_non_pic(object
, r_type
);
7616 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7617 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7618 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
7619 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
7620 data_shndx
, reloc
.get_r_offset());
7623 gold_assert(lsym
.get_st_value() == 0);
7624 unsigned int shndx
= lsym
.get_st_shndx();
7626 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
7629 object
->error(_("section symbol %u has bad shndx %u"),
7632 rel_dyn
->add_local_section(object
, shndx
,
7633 r_type
, output_section
,
7634 data_shndx
, reloc
.get_r_offset());
7639 case elfcpp::R_ARM_REL32
:
7640 case elfcpp::R_ARM_LDR_PC_G0
:
7641 case elfcpp::R_ARM_SBREL32
:
7642 case elfcpp::R_ARM_THM_CALL
:
7643 case elfcpp::R_ARM_THM_PC8
:
7644 case elfcpp::R_ARM_BASE_PREL
:
7645 case elfcpp::R_ARM_PLT32
:
7646 case elfcpp::R_ARM_CALL
:
7647 case elfcpp::R_ARM_JUMP24
:
7648 case elfcpp::R_ARM_THM_JUMP24
:
7649 case elfcpp::R_ARM_SBREL31
:
7650 case elfcpp::R_ARM_PREL31
:
7651 case elfcpp::R_ARM_MOVW_PREL_NC
:
7652 case elfcpp::R_ARM_MOVT_PREL
:
7653 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7654 case elfcpp::R_ARM_THM_MOVT_PREL
:
7655 case elfcpp::R_ARM_THM_JUMP19
:
7656 case elfcpp::R_ARM_THM_JUMP6
:
7657 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7658 case elfcpp::R_ARM_THM_PC12
:
7659 case elfcpp::R_ARM_REL32_NOI
:
7660 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7661 case elfcpp::R_ARM_ALU_PC_G0
:
7662 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7663 case elfcpp::R_ARM_ALU_PC_G1
:
7664 case elfcpp::R_ARM_ALU_PC_G2
:
7665 case elfcpp::R_ARM_LDR_PC_G1
:
7666 case elfcpp::R_ARM_LDR_PC_G2
:
7667 case elfcpp::R_ARM_LDRS_PC_G0
:
7668 case elfcpp::R_ARM_LDRS_PC_G1
:
7669 case elfcpp::R_ARM_LDRS_PC_G2
:
7670 case elfcpp::R_ARM_LDC_PC_G0
:
7671 case elfcpp::R_ARM_LDC_PC_G1
:
7672 case elfcpp::R_ARM_LDC_PC_G2
:
7673 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7674 case elfcpp::R_ARM_ALU_SB_G0
:
7675 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7676 case elfcpp::R_ARM_ALU_SB_G1
:
7677 case elfcpp::R_ARM_ALU_SB_G2
:
7678 case elfcpp::R_ARM_LDR_SB_G0
:
7679 case elfcpp::R_ARM_LDR_SB_G1
:
7680 case elfcpp::R_ARM_LDR_SB_G2
:
7681 case elfcpp::R_ARM_LDRS_SB_G0
:
7682 case elfcpp::R_ARM_LDRS_SB_G1
:
7683 case elfcpp::R_ARM_LDRS_SB_G2
:
7684 case elfcpp::R_ARM_LDC_SB_G0
:
7685 case elfcpp::R_ARM_LDC_SB_G1
:
7686 case elfcpp::R_ARM_LDC_SB_G2
:
7687 case elfcpp::R_ARM_MOVW_BREL_NC
:
7688 case elfcpp::R_ARM_MOVT_BREL
:
7689 case elfcpp::R_ARM_MOVW_BREL
:
7690 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7691 case elfcpp::R_ARM_THM_MOVT_BREL
:
7692 case elfcpp::R_ARM_THM_MOVW_BREL
:
7693 case elfcpp::R_ARM_THM_JUMP11
:
7694 case elfcpp::R_ARM_THM_JUMP8
:
7695 // We don't need to do anything for a relative addressing relocation
7696 // against a local symbol if it does not reference the GOT.
7699 case elfcpp::R_ARM_GOTOFF32
:
7700 case elfcpp::R_ARM_GOTOFF12
:
7701 // We need a GOT section:
7702 target
->got_section(symtab
, layout
);
7705 case elfcpp::R_ARM_GOT_BREL
:
7706 case elfcpp::R_ARM_GOT_PREL
:
7708 // The symbol requires a GOT entry.
7709 Arm_output_data_got
<big_endian
>* got
=
7710 target
->got_section(symtab
, layout
);
7711 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7712 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
7714 // If we are generating a shared object, we need to add a
7715 // dynamic RELATIVE relocation for this symbol's GOT entry.
7716 if (parameters
->options().output_is_position_independent())
7718 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7719 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7720 rel_dyn
->add_local_relative(
7721 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
7722 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7728 case elfcpp::R_ARM_TARGET1
:
7729 case elfcpp::R_ARM_TARGET2
:
7730 // This should have been mapped to another type already.
7732 case elfcpp::R_ARM_COPY
:
7733 case elfcpp::R_ARM_GLOB_DAT
:
7734 case elfcpp::R_ARM_JUMP_SLOT
:
7735 case elfcpp::R_ARM_RELATIVE
:
7736 // These are relocations which should only be seen by the
7737 // dynamic linker, and should never be seen here.
7738 gold_error(_("%s: unexpected reloc %u in object file"),
7739 object
->name().c_str(), r_type
);
7743 // These are initial TLS relocs, which are expected when
7745 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7746 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7747 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7748 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7749 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7751 bool output_is_shared
= parameters
->options().shared();
7752 const tls::Tls_optimization optimized_type
7753 = Target_arm
<big_endian
>::optimize_tls_reloc(!output_is_shared
,
7757 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7758 if (optimized_type
== tls::TLSOPT_NONE
)
7760 // Create a pair of GOT entries for the module index and
7761 // dtv-relative offset.
7762 Arm_output_data_got
<big_endian
>* got
7763 = target
->got_section(symtab
, layout
);
7764 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7765 unsigned int shndx
= lsym
.get_st_shndx();
7767 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7770 object
->error(_("local symbol %u has bad shndx %u"),
7775 if (!parameters
->doing_static_link())
7776 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
7778 target
->rel_dyn_section(layout
),
7779 elfcpp::R_ARM_TLS_DTPMOD32
, 0);
7781 got
->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR
,
7785 // FIXME: TLS optimization not supported yet.
7789 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7790 if (optimized_type
== tls::TLSOPT_NONE
)
7792 // Create a GOT entry for the module index.
7793 target
->got_mod_index_entry(symtab
, layout
, object
);
7796 // FIXME: TLS optimization not supported yet.
7800 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7803 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7804 layout
->set_has_static_tls();
7805 if (optimized_type
== tls::TLSOPT_NONE
)
7807 // Create a GOT entry for the tp-relative offset.
7808 Arm_output_data_got
<big_endian
>* got
7809 = target
->got_section(symtab
, layout
);
7810 unsigned int r_sym
=
7811 elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7812 if (!parameters
->doing_static_link())
7813 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
7814 target
->rel_dyn_section(layout
),
7815 elfcpp::R_ARM_TLS_TPOFF32
);
7816 else if (!object
->local_has_got_offset(r_sym
,
7817 GOT_TYPE_TLS_OFFSET
))
7819 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_OFFSET
);
7820 unsigned int got_offset
=
7821 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
7822 got
->add_static_reloc(got_offset
,
7823 elfcpp::R_ARM_TLS_TPOFF32
, object
,
7828 // FIXME: TLS optimization not supported yet.
7832 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7833 layout
->set_has_static_tls();
7834 if (output_is_shared
)
7836 // We need to create a dynamic relocation.
7837 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
7838 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7839 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7840 rel_dyn
->add_local(object
, r_sym
, elfcpp::R_ARM_TLS_TPOFF32
,
7841 output_section
, data_shndx
,
7842 reloc
.get_r_offset());
7852 case elfcpp::R_ARM_PC24
:
7853 case elfcpp::R_ARM_LDR_SBREL_11_0_NC
:
7854 case elfcpp::R_ARM_ALU_SBREL_19_12_NC
:
7855 case elfcpp::R_ARM_ALU_SBREL_27_20_CK
:
7857 unsupported_reloc_local(object
, r_type
);
7862 // Report an unsupported relocation against a global symbol.
7864 template<bool big_endian
>
7866 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
7867 Sized_relobj
<32, big_endian
>* object
,
7868 unsigned int r_type
,
7871 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7872 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
7875 template<bool big_endian
>
7877 Target_arm
<big_endian
>::Scan::possible_function_pointer_reloc(
7878 unsigned int r_type
)
7882 case elfcpp::R_ARM_PC24
:
7883 case elfcpp::R_ARM_THM_CALL
:
7884 case elfcpp::R_ARM_PLT32
:
7885 case elfcpp::R_ARM_CALL
:
7886 case elfcpp::R_ARM_JUMP24
:
7887 case elfcpp::R_ARM_THM_JUMP24
:
7888 case elfcpp::R_ARM_SBREL31
:
7889 case elfcpp::R_ARM_PREL31
:
7890 case elfcpp::R_ARM_THM_JUMP19
:
7891 case elfcpp::R_ARM_THM_JUMP6
:
7892 case elfcpp::R_ARM_THM_JUMP11
:
7893 case elfcpp::R_ARM_THM_JUMP8
:
7894 // All the relocations above are branches except SBREL31 and PREL31.
7898 // Be conservative and assume this is a function pointer.
7903 template<bool big_endian
>
7905 Target_arm
<big_endian
>::Scan::local_reloc_may_be_function_pointer(
7908 Target_arm
<big_endian
>* target
,
7909 Sized_relobj
<32, big_endian
>*,
7912 const elfcpp::Rel
<32, big_endian
>&,
7913 unsigned int r_type
,
7914 const elfcpp::Sym
<32, big_endian
>&)
7916 r_type
= target
->get_real_reloc_type(r_type
);
7917 return possible_function_pointer_reloc(r_type
);
7920 template<bool big_endian
>
7922 Target_arm
<big_endian
>::Scan::global_reloc_may_be_function_pointer(
7925 Target_arm
<big_endian
>* target
,
7926 Sized_relobj
<32, big_endian
>*,
7929 const elfcpp::Rel
<32, big_endian
>&,
7930 unsigned int r_type
,
7933 // GOT is not a function.
7934 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7937 r_type
= target
->get_real_reloc_type(r_type
);
7938 return possible_function_pointer_reloc(r_type
);
7941 // Scan a relocation for a global symbol.
7943 template<bool big_endian
>
7945 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
7948 Sized_relobj
<32, big_endian
>* object
,
7949 unsigned int data_shndx
,
7950 Output_section
* output_section
,
7951 const elfcpp::Rel
<32, big_endian
>& reloc
,
7952 unsigned int r_type
,
7955 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7956 // section. We check here to avoid creating a dynamic reloc against
7957 // _GLOBAL_OFFSET_TABLE_.
7958 if (!target
->has_got_section()
7959 && strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7960 target
->got_section(symtab
, layout
);
7962 r_type
= get_real_reloc_type(r_type
);
7965 case elfcpp::R_ARM_NONE
:
7966 case elfcpp::R_ARM_V4BX
:
7967 case elfcpp::R_ARM_GNU_VTENTRY
:
7968 case elfcpp::R_ARM_GNU_VTINHERIT
:
7971 case elfcpp::R_ARM_ABS32
:
7972 case elfcpp::R_ARM_ABS16
:
7973 case elfcpp::R_ARM_ABS12
:
7974 case elfcpp::R_ARM_THM_ABS5
:
7975 case elfcpp::R_ARM_ABS8
:
7976 case elfcpp::R_ARM_BASE_ABS
:
7977 case elfcpp::R_ARM_MOVW_ABS_NC
:
7978 case elfcpp::R_ARM_MOVT_ABS
:
7979 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7980 case elfcpp::R_ARM_THM_MOVT_ABS
:
7981 case elfcpp::R_ARM_ABS32_NOI
:
7982 // Absolute addressing relocations.
7984 // Make a PLT entry if necessary.
7985 if (this->symbol_needs_plt_entry(gsym
))
7987 target
->make_plt_entry(symtab
, layout
, gsym
);
7988 // Since this is not a PC-relative relocation, we may be
7989 // taking the address of a function. In that case we need to
7990 // set the entry in the dynamic symbol table to the address of
7992 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
7993 gsym
->set_needs_dynsym_value();
7995 // Make a dynamic relocation if necessary.
7996 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
7998 if (gsym
->may_need_copy_reloc())
8000 target
->copy_reloc(symtab
, layout
, object
,
8001 data_shndx
, output_section
, gsym
, reloc
);
8003 else if ((r_type
== elfcpp::R_ARM_ABS32
8004 || r_type
== elfcpp::R_ARM_ABS32_NOI
)
8005 && gsym
->can_use_relative_reloc(false))
8007 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
8008 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
8009 output_section
, object
,
8010 data_shndx
, reloc
.get_r_offset());
8014 check_non_pic(object
, r_type
);
8015 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
8016 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
8017 data_shndx
, reloc
.get_r_offset());
8023 case elfcpp::R_ARM_GOTOFF32
:
8024 case elfcpp::R_ARM_GOTOFF12
:
8025 // We need a GOT section.
8026 target
->got_section(symtab
, layout
);
8029 case elfcpp::R_ARM_REL32
:
8030 case elfcpp::R_ARM_LDR_PC_G0
:
8031 case elfcpp::R_ARM_SBREL32
:
8032 case elfcpp::R_ARM_THM_PC8
:
8033 case elfcpp::R_ARM_BASE_PREL
:
8034 case elfcpp::R_ARM_MOVW_PREL_NC
:
8035 case elfcpp::R_ARM_MOVT_PREL
:
8036 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
8037 case elfcpp::R_ARM_THM_MOVT_PREL
:
8038 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
8039 case elfcpp::R_ARM_THM_PC12
:
8040 case elfcpp::R_ARM_REL32_NOI
:
8041 case elfcpp::R_ARM_ALU_PC_G0_NC
:
8042 case elfcpp::R_ARM_ALU_PC_G0
:
8043 case elfcpp::R_ARM_ALU_PC_G1_NC
:
8044 case elfcpp::R_ARM_ALU_PC_G1
:
8045 case elfcpp::R_ARM_ALU_PC_G2
:
8046 case elfcpp::R_ARM_LDR_PC_G1
:
8047 case elfcpp::R_ARM_LDR_PC_G2
:
8048 case elfcpp::R_ARM_LDRS_PC_G0
:
8049 case elfcpp::R_ARM_LDRS_PC_G1
:
8050 case elfcpp::R_ARM_LDRS_PC_G2
:
8051 case elfcpp::R_ARM_LDC_PC_G0
:
8052 case elfcpp::R_ARM_LDC_PC_G1
:
8053 case elfcpp::R_ARM_LDC_PC_G2
:
8054 case elfcpp::R_ARM_ALU_SB_G0_NC
:
8055 case elfcpp::R_ARM_ALU_SB_G0
:
8056 case elfcpp::R_ARM_ALU_SB_G1_NC
:
8057 case elfcpp::R_ARM_ALU_SB_G1
:
8058 case elfcpp::R_ARM_ALU_SB_G2
:
8059 case elfcpp::R_ARM_LDR_SB_G0
:
8060 case elfcpp::R_ARM_LDR_SB_G1
:
8061 case elfcpp::R_ARM_LDR_SB_G2
:
8062 case elfcpp::R_ARM_LDRS_SB_G0
:
8063 case elfcpp::R_ARM_LDRS_SB_G1
:
8064 case elfcpp::R_ARM_LDRS_SB_G2
:
8065 case elfcpp::R_ARM_LDC_SB_G0
:
8066 case elfcpp::R_ARM_LDC_SB_G1
:
8067 case elfcpp::R_ARM_LDC_SB_G2
:
8068 case elfcpp::R_ARM_MOVW_BREL_NC
:
8069 case elfcpp::R_ARM_MOVT_BREL
:
8070 case elfcpp::R_ARM_MOVW_BREL
:
8071 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
8072 case elfcpp::R_ARM_THM_MOVT_BREL
:
8073 case elfcpp::R_ARM_THM_MOVW_BREL
:
8074 // Relative addressing relocations.
8076 // Make a dynamic relocation if necessary.
8077 int flags
= Symbol::NON_PIC_REF
;
8078 if (gsym
->needs_dynamic_reloc(flags
))
8080 if (target
->may_need_copy_reloc(gsym
))
8082 target
->copy_reloc(symtab
, layout
, object
,
8083 data_shndx
, output_section
, gsym
, reloc
);
8087 check_non_pic(object
, r_type
);
8088 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
8089 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
8090 data_shndx
, reloc
.get_r_offset());
8096 case elfcpp::R_ARM_THM_CALL
:
8097 case elfcpp::R_ARM_PLT32
:
8098 case elfcpp::R_ARM_CALL
:
8099 case elfcpp::R_ARM_JUMP24
:
8100 case elfcpp::R_ARM_THM_JUMP24
:
8101 case elfcpp::R_ARM_SBREL31
:
8102 case elfcpp::R_ARM_PREL31
:
8103 case elfcpp::R_ARM_THM_JUMP19
:
8104 case elfcpp::R_ARM_THM_JUMP6
:
8105 case elfcpp::R_ARM_THM_JUMP11
:
8106 case elfcpp::R_ARM_THM_JUMP8
:
8107 // All the relocation above are branches except for the PREL31 ones.
8108 // A PREL31 relocation can point to a personality function in a shared
8109 // library. In that case we want to use a PLT because we want to
8110 // call the personality routine and the dyanmic linkers we care about
8111 // do not support dynamic PREL31 relocations. An REL31 relocation may
8112 // point to a function whose unwinding behaviour is being described but
8113 // we will not mistakenly generate a PLT for that because we should use
8114 // a local section symbol.
8116 // If the symbol is fully resolved, this is just a relative
8117 // local reloc. Otherwise we need a PLT entry.
8118 if (gsym
->final_value_is_known())
8120 // If building a shared library, we can also skip the PLT entry
8121 // if the symbol is defined in the output file and is protected
8123 if (gsym
->is_defined()
8124 && !gsym
->is_from_dynobj()
8125 && !gsym
->is_preemptible())
8127 target
->make_plt_entry(symtab
, layout
, gsym
);
8130 case elfcpp::R_ARM_GOT_BREL
:
8131 case elfcpp::R_ARM_GOT_ABS
:
8132 case elfcpp::R_ARM_GOT_PREL
:
8134 // The symbol requires a GOT entry.
8135 Arm_output_data_got
<big_endian
>* got
=
8136 target
->got_section(symtab
, layout
);
8137 if (gsym
->final_value_is_known())
8138 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
8141 // If this symbol is not fully resolved, we need to add a
8142 // GOT entry with a dynamic relocation.
8143 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
8144 if (gsym
->is_from_dynobj()
8145 || gsym
->is_undefined()
8146 || gsym
->is_preemptible())
8147 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
8148 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
8151 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
8152 rel_dyn
->add_global_relative(
8153 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
8154 gsym
->got_offset(GOT_TYPE_STANDARD
));
8160 case elfcpp::R_ARM_TARGET1
:
8161 case elfcpp::R_ARM_TARGET2
:
8162 // These should have been mapped to other types already.
8164 case elfcpp::R_ARM_COPY
:
8165 case elfcpp::R_ARM_GLOB_DAT
:
8166 case elfcpp::R_ARM_JUMP_SLOT
:
8167 case elfcpp::R_ARM_RELATIVE
:
8168 // These are relocations which should only be seen by the
8169 // dynamic linker, and should never be seen here.
8170 gold_error(_("%s: unexpected reloc %u in object file"),
8171 object
->name().c_str(), r_type
);
8174 // These are initial tls relocs, which are expected when
8176 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
8177 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
8178 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
8179 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
8180 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
8182 const bool is_final
= gsym
->final_value_is_known();
8183 const tls::Tls_optimization optimized_type
8184 = Target_arm
<big_endian
>::optimize_tls_reloc(is_final
, r_type
);
8187 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
8188 if (optimized_type
== tls::TLSOPT_NONE
)
8190 // Create a pair of GOT entries for the module index and
8191 // dtv-relative offset.
8192 Arm_output_data_got
<big_endian
>* got
8193 = target
->got_section(symtab
, layout
);
8194 if (!parameters
->doing_static_link())
8195 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
8196 target
->rel_dyn_section(layout
),
8197 elfcpp::R_ARM_TLS_DTPMOD32
,
8198 elfcpp::R_ARM_TLS_DTPOFF32
);
8200 got
->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR
, gsym
);
8203 // FIXME: TLS optimization not supported yet.
8207 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
8208 if (optimized_type
== tls::TLSOPT_NONE
)
8210 // Create a GOT entry for the module index.
8211 target
->got_mod_index_entry(symtab
, layout
, object
);
8214 // FIXME: TLS optimization not supported yet.
8218 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
8221 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
8222 layout
->set_has_static_tls();
8223 if (optimized_type
== tls::TLSOPT_NONE
)
8225 // Create a GOT entry for the tp-relative offset.
8226 Arm_output_data_got
<big_endian
>* got
8227 = target
->got_section(symtab
, layout
);
8228 if (!parameters
->doing_static_link())
8229 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
8230 target
->rel_dyn_section(layout
),
8231 elfcpp::R_ARM_TLS_TPOFF32
);
8232 else if (!gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
))
8234 got
->add_global(gsym
, GOT_TYPE_TLS_OFFSET
);
8235 unsigned int got_offset
=
8236 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
8237 got
->add_static_reloc(got_offset
,
8238 elfcpp::R_ARM_TLS_TPOFF32
, gsym
);
8242 // FIXME: TLS optimization not supported yet.
8246 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
8247 layout
->set_has_static_tls();
8248 if (parameters
->options().shared())
8250 // We need to create a dynamic relocation.
8251 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
8252 rel_dyn
->add_global(gsym
, elfcpp::R_ARM_TLS_TPOFF32
,
8253 output_section
, object
,
8254 data_shndx
, reloc
.get_r_offset());
8264 case elfcpp::R_ARM_PC24
:
8265 case elfcpp::R_ARM_LDR_SBREL_11_0_NC
:
8266 case elfcpp::R_ARM_ALU_SBREL_19_12_NC
:
8267 case elfcpp::R_ARM_ALU_SBREL_27_20_CK
:
8269 unsupported_reloc_global(object
, r_type
, gsym
);
8274 // Process relocations for gc.
8276 template<bool big_endian
>
8278 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
8280 Sized_relobj
<32, big_endian
>* object
,
8281 unsigned int data_shndx
,
8283 const unsigned char* prelocs
,
8285 Output_section
* output_section
,
8286 bool needs_special_offset_handling
,
8287 size_t local_symbol_count
,
8288 const unsigned char* plocal_symbols
)
8290 typedef Target_arm
<big_endian
> Arm
;
8291 typedef typename Target_arm
<big_endian
>::Scan Scan
;
8293 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
,
8294 typename
Target_arm::Relocatable_size_for_reloc
>(
8303 needs_special_offset_handling
,
8308 // Scan relocations for a section.
8310 template<bool big_endian
>
8312 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
8314 Sized_relobj
<32, big_endian
>* object
,
8315 unsigned int data_shndx
,
8316 unsigned int sh_type
,
8317 const unsigned char* prelocs
,
8319 Output_section
* output_section
,
8320 bool needs_special_offset_handling
,
8321 size_t local_symbol_count
,
8322 const unsigned char* plocal_symbols
)
8324 typedef typename Target_arm
<big_endian
>::Scan Scan
;
8325 if (sh_type
== elfcpp::SHT_RELA
)
8327 gold_error(_("%s: unsupported RELA reloc section"),
8328 object
->name().c_str());
8332 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
8341 needs_special_offset_handling
,
8346 // Finalize the sections.
8348 template<bool big_endian
>
8350 Target_arm
<big_endian
>::do_finalize_sections(
8352 const Input_objects
* input_objects
,
8353 Symbol_table
* symtab
)
8355 bool merged_any_attributes
= false;
8356 // Merge processor-specific flags.
8357 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
8358 p
!= input_objects
->relobj_end();
8361 Arm_relobj
<big_endian
>* arm_relobj
=
8362 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
8363 if (arm_relobj
->merge_flags_and_attributes())
8365 this->merge_processor_specific_flags(
8367 arm_relobj
->processor_specific_flags());
8368 this->merge_object_attributes(arm_relobj
->name().c_str(),
8369 arm_relobj
->attributes_section_data());
8370 merged_any_attributes
= true;
8374 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
8375 p
!= input_objects
->dynobj_end();
8378 Arm_dynobj
<big_endian
>* arm_dynobj
=
8379 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
8380 this->merge_processor_specific_flags(
8382 arm_dynobj
->processor_specific_flags());
8383 this->merge_object_attributes(arm_dynobj
->name().c_str(),
8384 arm_dynobj
->attributes_section_data());
8385 merged_any_attributes
= true;
8388 // Create an empty uninitialized attribute section if we still don't have it
8389 // at this moment. This happens if there is no attributes sections in all
8391 if (this->attributes_section_data_
== NULL
)
8392 this->attributes_section_data_
= new Attributes_section_data(NULL
, 0);
8395 const Object_attribute
* cpu_arch_attr
=
8396 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
8397 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
8398 this->set_may_use_blx(true);
8400 // Check if we need to use Cortex-A8 workaround.
8401 if (parameters
->options().user_set_fix_cortex_a8())
8402 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
8405 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8406 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8408 const Object_attribute
* cpu_arch_profile_attr
=
8409 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
8410 this->fix_cortex_a8_
=
8411 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
8412 && (cpu_arch_profile_attr
->int_value() == 'A'
8413 || cpu_arch_profile_attr
->int_value() == 0));
8416 // Check if we can use V4BX interworking.
8417 // The V4BX interworking stub contains BX instruction,
8418 // which is not specified for some profiles.
8419 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8420 && !this->may_use_blx())
8421 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8422 "the target profile does not support BX instruction"));
8424 // Fill in some more dynamic tags.
8425 const Reloc_section
* rel_plt
= (this->plt_
== NULL
8427 : this->plt_
->rel_plt());
8428 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
8429 this->rel_dyn_
, true, false);
8431 // Emit any relocs we saved in an attempt to avoid generating COPY
8433 if (this->copy_relocs_
.any_saved_relocs())
8434 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
8436 // Handle the .ARM.exidx section.
8437 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
8439 if (!parameters
->options().relocatable())
8441 if (exidx_section
!= NULL
8442 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
)
8444 // Create __exidx_start and __exdix_end symbols.
8445 symtab
->define_in_output_data("__exidx_start", NULL
,
8446 Symbol_table::PREDEFINED
,
8447 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
8448 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
8450 symtab
->define_in_output_data("__exidx_end", NULL
,
8451 Symbol_table::PREDEFINED
,
8452 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
8453 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
8456 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8457 // the .ARM.exidx section.
8458 if (!layout
->script_options()->saw_phdrs_clause())
8460 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0,
8463 Output_segment
* exidx_segment
=
8464 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
8465 exidx_segment
->add_output_section_to_nonload(exidx_section
,
8471 symtab
->define_as_constant("__exidx_start", NULL
,
8472 Symbol_table::PREDEFINED
,
8473 0, 0, elfcpp::STT_OBJECT
,
8474 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
8476 symtab
->define_as_constant("__exidx_end", NULL
,
8477 Symbol_table::PREDEFINED
,
8478 0, 0, elfcpp::STT_OBJECT
,
8479 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
8484 // Create an .ARM.attributes section if we have merged any attributes
8486 if (merged_any_attributes
)
8488 Output_attributes_section_data
* attributes_section
=
8489 new Output_attributes_section_data(*this->attributes_section_data_
);
8490 layout
->add_output_section_data(".ARM.attributes",
8491 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
8492 attributes_section
, ORDER_INVALID
,
8496 // Fix up links in section EXIDX headers.
8497 for (Layout::Section_list::const_iterator p
= layout
->section_list().begin();
8498 p
!= layout
->section_list().end();
8500 if ((*p
)->type() == elfcpp::SHT_ARM_EXIDX
)
8502 Arm_output_section
<big_endian
>* os
=
8503 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
8504 os
->set_exidx_section_link();
8508 // Return whether a direct absolute static relocation needs to be applied.
8509 // In cases where Scan::local() or Scan::global() has created
8510 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8511 // of the relocation is carried in the data, and we must not
8512 // apply the static relocation.
8514 template<bool big_endian
>
8516 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
8517 const Sized_symbol
<32>* gsym
,
8520 Output_section
* output_section
)
8522 // If the output section is not allocated, then we didn't call
8523 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8525 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
8528 // For local symbols, we will have created a non-RELATIVE dynamic
8529 // relocation only if (a) the output is position independent,
8530 // (b) the relocation is absolute (not pc- or segment-relative), and
8531 // (c) the relocation is not 32 bits wide.
8533 return !(parameters
->options().output_is_position_independent()
8534 && (ref_flags
& Symbol::ABSOLUTE_REF
)
8537 // For global symbols, we use the same helper routines used in the
8538 // scan pass. If we did not create a dynamic relocation, or if we
8539 // created a RELATIVE dynamic relocation, we should apply the static
8541 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
8542 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
8543 && gsym
->can_use_relative_reloc(ref_flags
8544 & Symbol::FUNCTION_CALL
);
8545 return !has_dyn
|| is_rel
;
8548 // Perform a relocation.
8550 template<bool big_endian
>
8552 Target_arm
<big_endian
>::Relocate::relocate(
8553 const Relocate_info
<32, big_endian
>* relinfo
,
8555 Output_section
* output_section
,
8557 const elfcpp::Rel
<32, big_endian
>& rel
,
8558 unsigned int r_type
,
8559 const Sized_symbol
<32>* gsym
,
8560 const Symbol_value
<32>* psymval
,
8561 unsigned char* view
,
8562 Arm_address address
,
8563 section_size_type view_size
)
8565 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
8567 r_type
= get_real_reloc_type(r_type
);
8568 const Arm_reloc_property
* reloc_property
=
8569 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
8570 if (reloc_property
== NULL
)
8572 std::string reloc_name
=
8573 arm_reloc_property_table
->reloc_name_in_error_message(r_type
);
8574 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
8575 _("cannot relocate %s in object file"),
8576 reloc_name
.c_str());
8580 const Arm_relobj
<big_endian
>* object
=
8581 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8583 // If the final branch target of a relocation is THUMB instruction, this
8584 // is 1. Otherwise it is 0.
8585 Arm_address thumb_bit
= 0;
8586 Symbol_value
<32> symval
;
8587 bool is_weakly_undefined_without_plt
= false;
8588 bool have_got_offset
= false;
8589 unsigned int got_offset
= 0;
8591 // If the relocation uses the GOT entry of a symbol instead of the symbol
8592 // itself, we don't care about whether the symbol is defined or what kind
8594 if (reloc_property
->uses_got_entry())
8596 // Get the GOT offset.
8597 // The GOT pointer points to the end of the GOT section.
8598 // We need to subtract the size of the GOT section to get
8599 // the actual offset to use in the relocation.
8600 // TODO: We should move GOT offset computing code in TLS relocations
8604 case elfcpp::R_ARM_GOT_BREL
:
8605 case elfcpp::R_ARM_GOT_PREL
:
8608 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
8609 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
8610 - target
->got_size());
8614 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8615 gold_assert(object
->local_has_got_offset(r_sym
,
8616 GOT_TYPE_STANDARD
));
8617 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
8618 - target
->got_size());
8620 have_got_offset
= true;
8627 else if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
8631 // This is a global symbol. Determine if we use PLT and if the
8632 // final target is THUMB.
8633 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
8635 // This uses a PLT, change the symbol value.
8636 symval
.set_output_value(target
->plt_section()->address()
8637 + gsym
->plt_offset());
8640 else if (gsym
->is_weak_undefined())
8642 // This is a weakly undefined symbol and we do not use PLT
8643 // for this relocation. A branch targeting this symbol will
8644 // be converted into an NOP.
8645 is_weakly_undefined_without_plt
= true;
8647 else if (gsym
->is_undefined() && reloc_property
->uses_symbol())
8649 // This relocation uses the symbol value but the symbol is
8650 // undefined. Exit early and have the caller reporting an
8656 // Set thumb bit if symbol:
8657 // -Has type STT_ARM_TFUNC or
8658 // -Has type STT_FUNC, is defined and with LSB in value set.
8660 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
8661 || (gsym
->type() == elfcpp::STT_FUNC
8662 && !gsym
->is_undefined()
8663 && ((psymval
->value(object
, 0) & 1) != 0)))
8670 // This is a local symbol. Determine if the final target is THUMB.
8671 // We saved this information when all the local symbols were read.
8672 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
8673 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
8674 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
8679 // This is a fake relocation synthesized for a stub. It does not have
8680 // a real symbol. We just look at the LSB of the symbol value to
8681 // determine if the target is THUMB or not.
8682 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
8685 // Strip LSB if this points to a THUMB target.
8687 && reloc_property
->uses_thumb_bit()
8688 && ((psymval
->value(object
, 0) & 1) != 0))
8690 Arm_address stripped_value
=
8691 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
8692 symval
.set_output_value(stripped_value
);
8696 // To look up relocation stubs, we need to pass the symbol table index of
8698 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8700 // Get the addressing origin of the output segment defining the
8701 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8702 Arm_address sym_origin
= 0;
8703 if (reloc_property
->uses_symbol_base())
8705 if (r_type
== elfcpp::R_ARM_BASE_ABS
&& gsym
== NULL
)
8706 // R_ARM_BASE_ABS with the NULL symbol will give the
8707 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8708 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8709 sym_origin
= target
->got_plt_section()->address();
8710 else if (gsym
== NULL
)
8712 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
8713 sym_origin
= gsym
->output_segment()->vaddr();
8714 else if (gsym
->source() == Symbol::IN_OUTPUT_DATA
)
8715 sym_origin
= gsym
->output_data()->address();
8717 // TODO: Assumes the segment base to be zero for the global symbols
8718 // till the proper support for the segment-base-relative addressing
8719 // will be implemented. This is consistent with GNU ld.
8722 // For relative addressing relocation, find out the relative address base.
8723 Arm_address relative_address_base
= 0;
8724 switch(reloc_property
->relative_address_base())
8726 case Arm_reloc_property::RAB_NONE
:
8727 // Relocations with relative address bases RAB_TLS and RAB_tp are
8728 // handled by relocate_tls. So we do not need to do anything here.
8729 case Arm_reloc_property::RAB_TLS
:
8730 case Arm_reloc_property::RAB_tp
:
8732 case Arm_reloc_property::RAB_B_S
:
8733 relative_address_base
= sym_origin
;
8735 case Arm_reloc_property::RAB_GOT_ORG
:
8736 relative_address_base
= target
->got_plt_section()->address();
8738 case Arm_reloc_property::RAB_P
:
8739 relative_address_base
= address
;
8741 case Arm_reloc_property::RAB_Pa
:
8742 relative_address_base
= address
& 0xfffffffcU
;
8748 typename
Arm_relocate_functions::Status reloc_status
=
8749 Arm_relocate_functions::STATUS_OKAY
;
8750 bool check_overflow
= reloc_property
->checks_overflow();
8753 case elfcpp::R_ARM_NONE
:
8756 case elfcpp::R_ARM_ABS8
:
8757 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8759 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
8762 case elfcpp::R_ARM_ABS12
:
8763 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8765 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
8768 case elfcpp::R_ARM_ABS16
:
8769 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8771 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
8774 case elfcpp::R_ARM_ABS32
:
8775 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
8777 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
8781 case elfcpp::R_ARM_ABS32_NOI
:
8782 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
8784 // No thumb bit for this relocation: (S + A)
8785 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
8789 case elfcpp::R_ARM_MOVW_ABS_NC
:
8790 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8792 reloc_status
= Arm_relocate_functions::movw(view
, object
, psymval
,
8797 case elfcpp::R_ARM_MOVT_ABS
:
8798 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8800 reloc_status
= Arm_relocate_functions::movt(view
, object
, psymval
, 0);
8803 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
8804 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8806 reloc_status
= Arm_relocate_functions::thm_movw(view
, object
, psymval
,
8807 0, thumb_bit
, false);
8810 case elfcpp::R_ARM_THM_MOVT_ABS
:
8811 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8813 reloc_status
= Arm_relocate_functions::thm_movt(view
, object
,
8817 case elfcpp::R_ARM_MOVW_PREL_NC
:
8818 case elfcpp::R_ARM_MOVW_BREL_NC
:
8819 case elfcpp::R_ARM_MOVW_BREL
:
8821 Arm_relocate_functions::movw(view
, object
, psymval
,
8822 relative_address_base
, thumb_bit
,
8826 case elfcpp::R_ARM_MOVT_PREL
:
8827 case elfcpp::R_ARM_MOVT_BREL
:
8829 Arm_relocate_functions::movt(view
, object
, psymval
,
8830 relative_address_base
);
8833 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
8834 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
8835 case elfcpp::R_ARM_THM_MOVW_BREL
:
8837 Arm_relocate_functions::thm_movw(view
, object
, psymval
,
8838 relative_address_base
,
8839 thumb_bit
, check_overflow
);
8842 case elfcpp::R_ARM_THM_MOVT_PREL
:
8843 case elfcpp::R_ARM_THM_MOVT_BREL
:
8845 Arm_relocate_functions::thm_movt(view
, object
, psymval
,
8846 relative_address_base
);
8849 case elfcpp::R_ARM_REL32
:
8850 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
8851 address
, thumb_bit
);
8854 case elfcpp::R_ARM_THM_ABS5
:
8855 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8857 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
8860 // Thumb long branches.
8861 case elfcpp::R_ARM_THM_CALL
:
8862 case elfcpp::R_ARM_THM_XPC22
:
8863 case elfcpp::R_ARM_THM_JUMP24
:
8865 Arm_relocate_functions::thumb_branch_common(
8866 r_type
, relinfo
, view
, gsym
, object
, r_sym
, psymval
, address
,
8867 thumb_bit
, is_weakly_undefined_without_plt
);
8870 case elfcpp::R_ARM_GOTOFF32
:
8872 Arm_address got_origin
;
8873 got_origin
= target
->got_plt_section()->address();
8874 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
8875 got_origin
, thumb_bit
);
8879 case elfcpp::R_ARM_BASE_PREL
:
8880 gold_assert(gsym
!= NULL
);
8882 Arm_relocate_functions::base_prel(view
, sym_origin
, address
);
8885 case elfcpp::R_ARM_BASE_ABS
:
8887 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8891 reloc_status
= Arm_relocate_functions::base_abs(view
, sym_origin
);
8895 case elfcpp::R_ARM_GOT_BREL
:
8896 gold_assert(have_got_offset
);
8897 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
8900 case elfcpp::R_ARM_GOT_PREL
:
8901 gold_assert(have_got_offset
);
8902 // Get the address origin for GOT PLT, which is allocated right
8903 // after the GOT section, to calculate an absolute address of
8904 // the symbol GOT entry (got_origin + got_offset).
8905 Arm_address got_origin
;
8906 got_origin
= target
->got_plt_section()->address();
8907 reloc_status
= Arm_relocate_functions::got_prel(view
,
8908 got_origin
+ got_offset
,
8912 case elfcpp::R_ARM_PLT32
:
8913 case elfcpp::R_ARM_CALL
:
8914 case elfcpp::R_ARM_JUMP24
:
8915 case elfcpp::R_ARM_XPC25
:
8916 gold_assert(gsym
== NULL
8917 || gsym
->has_plt_offset()
8918 || gsym
->final_value_is_known()
8919 || (gsym
->is_defined()
8920 && !gsym
->is_from_dynobj()
8921 && !gsym
->is_preemptible()));
8923 Arm_relocate_functions::arm_branch_common(
8924 r_type
, relinfo
, view
, gsym
, object
, r_sym
, psymval
, address
,
8925 thumb_bit
, is_weakly_undefined_without_plt
);
8928 case elfcpp::R_ARM_THM_JUMP19
:
8930 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
8934 case elfcpp::R_ARM_THM_JUMP6
:
8936 Arm_relocate_functions::thm_jump6(view
, object
, psymval
, address
);
8939 case elfcpp::R_ARM_THM_JUMP8
:
8941 Arm_relocate_functions::thm_jump8(view
, object
, psymval
, address
);
8944 case elfcpp::R_ARM_THM_JUMP11
:
8946 Arm_relocate_functions::thm_jump11(view
, object
, psymval
, address
);
8949 case elfcpp::R_ARM_PREL31
:
8950 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
8951 address
, thumb_bit
);
8954 case elfcpp::R_ARM_V4BX
:
8955 if (target
->fix_v4bx() > General_options::FIX_V4BX_NONE
)
8957 const bool is_v4bx_interworking
=
8958 (target
->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
);
8960 Arm_relocate_functions::v4bx(relinfo
, view
, object
, address
,
8961 is_v4bx_interworking
);
8965 case elfcpp::R_ARM_THM_PC8
:
8967 Arm_relocate_functions::thm_pc8(view
, object
, psymval
, address
);
8970 case elfcpp::R_ARM_THM_PC12
:
8972 Arm_relocate_functions::thm_pc12(view
, object
, psymval
, address
);
8975 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
8977 Arm_relocate_functions::thm_alu11(view
, object
, psymval
, address
,
8981 case elfcpp::R_ARM_ALU_PC_G0_NC
:
8982 case elfcpp::R_ARM_ALU_PC_G0
:
8983 case elfcpp::R_ARM_ALU_PC_G1_NC
:
8984 case elfcpp::R_ARM_ALU_PC_G1
:
8985 case elfcpp::R_ARM_ALU_PC_G2
:
8986 case elfcpp::R_ARM_ALU_SB_G0_NC
:
8987 case elfcpp::R_ARM_ALU_SB_G0
:
8988 case elfcpp::R_ARM_ALU_SB_G1_NC
:
8989 case elfcpp::R_ARM_ALU_SB_G1
:
8990 case elfcpp::R_ARM_ALU_SB_G2
:
8992 Arm_relocate_functions::arm_grp_alu(view
, object
, psymval
,
8993 reloc_property
->group_index(),
8994 relative_address_base
,
8995 thumb_bit
, check_overflow
);
8998 case elfcpp::R_ARM_LDR_PC_G0
:
8999 case elfcpp::R_ARM_LDR_PC_G1
:
9000 case elfcpp::R_ARM_LDR_PC_G2
:
9001 case elfcpp::R_ARM_LDR_SB_G0
:
9002 case elfcpp::R_ARM_LDR_SB_G1
:
9003 case elfcpp::R_ARM_LDR_SB_G2
:
9005 Arm_relocate_functions::arm_grp_ldr(view
, object
, psymval
,
9006 reloc_property
->group_index(),
9007 relative_address_base
);
9010 case elfcpp::R_ARM_LDRS_PC_G0
:
9011 case elfcpp::R_ARM_LDRS_PC_G1
:
9012 case elfcpp::R_ARM_LDRS_PC_G2
:
9013 case elfcpp::R_ARM_LDRS_SB_G0
:
9014 case elfcpp::R_ARM_LDRS_SB_G1
:
9015 case elfcpp::R_ARM_LDRS_SB_G2
:
9017 Arm_relocate_functions::arm_grp_ldrs(view
, object
, psymval
,
9018 reloc_property
->group_index(),
9019 relative_address_base
);
9022 case elfcpp::R_ARM_LDC_PC_G0
:
9023 case elfcpp::R_ARM_LDC_PC_G1
:
9024 case elfcpp::R_ARM_LDC_PC_G2
:
9025 case elfcpp::R_ARM_LDC_SB_G0
:
9026 case elfcpp::R_ARM_LDC_SB_G1
:
9027 case elfcpp::R_ARM_LDC_SB_G2
:
9029 Arm_relocate_functions::arm_grp_ldc(view
, object
, psymval
,
9030 reloc_property
->group_index(),
9031 relative_address_base
);
9034 // These are initial tls relocs, which are expected when
9036 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
9037 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
9038 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
9039 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
9040 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
9042 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
9043 view
, address
, view_size
);
9046 // The known and unknown unsupported and/or deprecated relocations.
9047 case elfcpp::R_ARM_PC24
:
9048 case elfcpp::R_ARM_LDR_SBREL_11_0_NC
:
9049 case elfcpp::R_ARM_ALU_SBREL_19_12_NC
:
9050 case elfcpp::R_ARM_ALU_SBREL_27_20_CK
:
9052 // Just silently leave the method. We should get an appropriate error
9053 // message in the scan methods.
9057 // Report any errors.
9058 switch (reloc_status
)
9060 case Arm_relocate_functions::STATUS_OKAY
:
9062 case Arm_relocate_functions::STATUS_OVERFLOW
:
9063 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
9064 _("relocation overflow in %s"),
9065 reloc_property
->name().c_str());
9067 case Arm_relocate_functions::STATUS_BAD_RELOC
:
9068 gold_error_at_location(
9072 _("unexpected opcode while processing relocation %s"),
9073 reloc_property
->name().c_str());
9082 // Perform a TLS relocation.
9084 template<bool big_endian
>
9085 inline typename Arm_relocate_functions
<big_endian
>::Status
9086 Target_arm
<big_endian
>::Relocate::relocate_tls(
9087 const Relocate_info
<32, big_endian
>* relinfo
,
9088 Target_arm
<big_endian
>* target
,
9090 const elfcpp::Rel
<32, big_endian
>& rel
,
9091 unsigned int r_type
,
9092 const Sized_symbol
<32>* gsym
,
9093 const Symbol_value
<32>* psymval
,
9094 unsigned char* view
,
9095 elfcpp::Elf_types
<32>::Elf_Addr address
,
9096 section_size_type
/*view_size*/ )
9098 typedef Arm_relocate_functions
<big_endian
> ArmRelocFuncs
;
9099 typedef Relocate_functions
<32, big_endian
> RelocFuncs
;
9100 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
9102 const Sized_relobj
<32, big_endian
>* object
= relinfo
->object
;
9104 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
9106 const bool is_final
= (gsym
== NULL
9107 ? !parameters
->options().shared()
9108 : gsym
->final_value_is_known());
9109 const tls::Tls_optimization optimized_type
9110 = Target_arm
<big_endian
>::optimize_tls_reloc(is_final
, r_type
);
9113 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
9115 unsigned int got_type
= GOT_TYPE_TLS_PAIR
;
9116 unsigned int got_offset
;
9119 gold_assert(gsym
->has_got_offset(got_type
));
9120 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
9124 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
9125 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
9126 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
9127 - target
->got_size());
9129 if (optimized_type
== tls::TLSOPT_NONE
)
9131 Arm_address got_entry
=
9132 target
->got_plt_section()->address() + got_offset
;
9134 // Relocate the field with the PC relative offset of the pair of
9136 RelocFuncs::pcrel32(view
, got_entry
, address
);
9137 return ArmRelocFuncs::STATUS_OKAY
;
9142 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
9143 if (optimized_type
== tls::TLSOPT_NONE
)
9145 // Relocate the field with the offset of the GOT entry for
9146 // the module index.
9147 unsigned int got_offset
;
9148 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
9149 - target
->got_size());
9150 Arm_address got_entry
=
9151 target
->got_plt_section()->address() + got_offset
;
9153 // Relocate the field with the PC relative offset of the pair of
9155 RelocFuncs::pcrel32(view
, got_entry
, address
);
9156 return ArmRelocFuncs::STATUS_OKAY
;
9160 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
9161 RelocFuncs::rel32(view
, value
);
9162 return ArmRelocFuncs::STATUS_OKAY
;
9164 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
9165 if (optimized_type
== tls::TLSOPT_NONE
)
9167 // Relocate the field with the offset of the GOT entry for
9168 // the tp-relative offset of the symbol.
9169 unsigned int got_type
= GOT_TYPE_TLS_OFFSET
;
9170 unsigned int got_offset
;
9173 gold_assert(gsym
->has_got_offset(got_type
));
9174 got_offset
= gsym
->got_offset(got_type
);
9178 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
9179 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
9180 got_offset
= object
->local_got_offset(r_sym
, got_type
);
9183 // All GOT offsets are relative to the end of the GOT.
9184 got_offset
-= target
->got_size();
9186 Arm_address got_entry
=
9187 target
->got_plt_section()->address() + got_offset
;
9189 // Relocate the field with the PC relative offset of the GOT entry.
9190 RelocFuncs::pcrel32(view
, got_entry
, address
);
9191 return ArmRelocFuncs::STATUS_OKAY
;
9195 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
9196 // If we're creating a shared library, a dynamic relocation will
9197 // have been created for this location, so do not apply it now.
9198 if (!parameters
->options().shared())
9200 gold_assert(tls_segment
!= NULL
);
9202 // $tp points to the TCB, which is followed by the TLS, so we
9203 // need to add TCB size to the offset.
9204 Arm_address aligned_tcb_size
=
9205 align_address(ARM_TCB_SIZE
, tls_segment
->maximum_alignment());
9206 RelocFuncs::rel32(view
, value
+ aligned_tcb_size
);
9209 return ArmRelocFuncs::STATUS_OKAY
;
9215 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
9216 _("unsupported reloc %u"),
9218 return ArmRelocFuncs::STATUS_BAD_RELOC
;
9221 // Relocate section data.
9223 template<bool big_endian
>
9225 Target_arm
<big_endian
>::relocate_section(
9226 const Relocate_info
<32, big_endian
>* relinfo
,
9227 unsigned int sh_type
,
9228 const unsigned char* prelocs
,
9230 Output_section
* output_section
,
9231 bool needs_special_offset_handling
,
9232 unsigned char* view
,
9233 Arm_address address
,
9234 section_size_type view_size
,
9235 const Reloc_symbol_changes
* reloc_symbol_changes
)
9237 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
9238 gold_assert(sh_type
== elfcpp::SHT_REL
);
9240 // See if we are relocating a relaxed input section. If so, the view
9241 // covers the whole output section and we need to adjust accordingly.
9242 if (needs_special_offset_handling
)
9244 const Output_relaxed_input_section
* poris
=
9245 output_section
->find_relaxed_input_section(relinfo
->object
,
9246 relinfo
->data_shndx
);
9249 Arm_address section_address
= poris
->address();
9250 section_size_type section_size
= poris
->data_size();
9252 gold_assert((section_address
>= address
)
9253 && ((section_address
+ section_size
)
9254 <= (address
+ view_size
)));
9256 off_t offset
= section_address
- address
;
9259 view_size
= section_size
;
9263 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
9270 needs_special_offset_handling
,
9274 reloc_symbol_changes
);
9277 // Return the size of a relocation while scanning during a relocatable
9280 template<bool big_endian
>
9282 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
9283 unsigned int r_type
,
9286 r_type
= get_real_reloc_type(r_type
);
9287 const Arm_reloc_property
* arp
=
9288 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
9293 std::string reloc_name
=
9294 arm_reloc_property_table
->reloc_name_in_error_message(r_type
);
9295 gold_error(_("%s: unexpected %s in object file"),
9296 object
->name().c_str(), reloc_name
.c_str());
9301 // Scan the relocs during a relocatable link.
9303 template<bool big_endian
>
9305 Target_arm
<big_endian
>::scan_relocatable_relocs(
9306 Symbol_table
* symtab
,
9308 Sized_relobj
<32, big_endian
>* object
,
9309 unsigned int data_shndx
,
9310 unsigned int sh_type
,
9311 const unsigned char* prelocs
,
9313 Output_section
* output_section
,
9314 bool needs_special_offset_handling
,
9315 size_t local_symbol_count
,
9316 const unsigned char* plocal_symbols
,
9317 Relocatable_relocs
* rr
)
9319 gold_assert(sh_type
== elfcpp::SHT_REL
);
9321 typedef Arm_scan_relocatable_relocs
<big_endian
, elfcpp::SHT_REL
,
9322 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
9324 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
9325 Scan_relocatable_relocs
>(
9333 needs_special_offset_handling
,
9339 // Relocate a section during a relocatable link.
9341 template<bool big_endian
>
9343 Target_arm
<big_endian
>::relocate_for_relocatable(
9344 const Relocate_info
<32, big_endian
>* relinfo
,
9345 unsigned int sh_type
,
9346 const unsigned char* prelocs
,
9348 Output_section
* output_section
,
9349 off_t offset_in_output_section
,
9350 const Relocatable_relocs
* rr
,
9351 unsigned char* view
,
9352 Arm_address view_address
,
9353 section_size_type view_size
,
9354 unsigned char* reloc_view
,
9355 section_size_type reloc_view_size
)
9357 gold_assert(sh_type
== elfcpp::SHT_REL
);
9359 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
9364 offset_in_output_section
,
9373 // Perform target-specific processing in a relocatable link. This is
9374 // only used if we use the relocation strategy RELOC_SPECIAL.
9376 template<bool big_endian
>
9378 Target_arm
<big_endian
>::relocate_special_relocatable(
9379 const Relocate_info
<32, big_endian
>* relinfo
,
9380 unsigned int sh_type
,
9381 const unsigned char* preloc_in
,
9383 Output_section
* output_section
,
9384 off_t offset_in_output_section
,
9385 unsigned char* view
,
9386 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
9388 unsigned char* preloc_out
)
9390 // We can only handle REL type relocation sections.
9391 gold_assert(sh_type
== elfcpp::SHT_REL
);
9393 typedef typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc Reltype
;
9394 typedef typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc_write
9396 const Arm_address invalid_address
= static_cast<Arm_address
>(0) - 1;
9398 const Arm_relobj
<big_endian
>* object
=
9399 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
9400 const unsigned int local_count
= object
->local_symbol_count();
9402 Reltype
reloc(preloc_in
);
9403 Reltype_write
reloc_write(preloc_out
);
9405 elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
9406 const unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
9407 const unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
9409 const Arm_reloc_property
* arp
=
9410 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
9411 gold_assert(arp
!= NULL
);
9413 // Get the new symbol index.
9414 // We only use RELOC_SPECIAL strategy in local relocations.
9415 gold_assert(r_sym
< local_count
);
9417 // We are adjusting a section symbol. We need to find
9418 // the symbol table index of the section symbol for
9419 // the output section corresponding to input section
9420 // in which this symbol is defined.
9422 unsigned int shndx
= object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9423 gold_assert(is_ordinary
);
9424 Output_section
* os
= object
->output_section(shndx
);
9425 gold_assert(os
!= NULL
);
9426 gold_assert(os
->needs_symtab_index());
9427 unsigned int new_symndx
= os
->symtab_index();
9429 // Get the new offset--the location in the output section where
9430 // this relocation should be applied.
9432 Arm_address offset
= reloc
.get_r_offset();
9433 Arm_address new_offset
;
9434 if (offset_in_output_section
!= invalid_address
)
9435 new_offset
= offset
+ offset_in_output_section
;
9438 section_offset_type sot_offset
=
9439 convert_types
<section_offset_type
, Arm_address
>(offset
);
9440 section_offset_type new_sot_offset
=
9441 output_section
->output_offset(object
, relinfo
->data_shndx
,
9443 gold_assert(new_sot_offset
!= -1);
9444 new_offset
= new_sot_offset
;
9447 // In an object file, r_offset is an offset within the section.
9448 // In an executable or dynamic object, generated by
9449 // --emit-relocs, r_offset is an absolute address.
9450 if (!parameters
->options().relocatable())
9452 new_offset
+= view_address
;
9453 if (offset_in_output_section
!= invalid_address
)
9454 new_offset
-= offset_in_output_section
;
9457 reloc_write
.put_r_offset(new_offset
);
9458 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(new_symndx
, r_type
));
9460 // Handle the reloc addend.
9461 // The relocation uses a section symbol in the input file.
9462 // We are adjusting it to use a section symbol in the output
9463 // file. The input section symbol refers to some address in
9464 // the input section. We need the relocation in the output
9465 // file to refer to that same address. This adjustment to
9466 // the addend is the same calculation we use for a simple
9467 // absolute relocation for the input section symbol.
9469 const Symbol_value
<32>* psymval
= object
->local_symbol(r_sym
);
9471 // Handle THUMB bit.
9472 Symbol_value
<32> symval
;
9473 Arm_address thumb_bit
=
9474 object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
9476 && arp
->uses_thumb_bit()
9477 && ((psymval
->value(object
, 0) & 1) != 0))
9479 Arm_address stripped_value
=
9480 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
9481 symval
.set_output_value(stripped_value
);
9485 unsigned char* paddend
= view
+ offset
;
9486 typename Arm_relocate_functions
<big_endian
>::Status reloc_status
=
9487 Arm_relocate_functions
<big_endian
>::STATUS_OKAY
;
9490 case elfcpp::R_ARM_ABS8
:
9491 reloc_status
= Arm_relocate_functions
<big_endian
>::abs8(paddend
, object
,
9495 case elfcpp::R_ARM_ABS12
:
9496 reloc_status
= Arm_relocate_functions
<big_endian
>::abs12(paddend
, object
,
9500 case elfcpp::R_ARM_ABS16
:
9501 reloc_status
= Arm_relocate_functions
<big_endian
>::abs16(paddend
, object
,
9505 case elfcpp::R_ARM_THM_ABS5
:
9506 reloc_status
= Arm_relocate_functions
<big_endian
>::thm_abs5(paddend
,
9511 case elfcpp::R_ARM_MOVW_ABS_NC
:
9512 case elfcpp::R_ARM_MOVW_PREL_NC
:
9513 case elfcpp::R_ARM_MOVW_BREL_NC
:
9514 case elfcpp::R_ARM_MOVW_BREL
:
9515 reloc_status
= Arm_relocate_functions
<big_endian
>::movw(
9516 paddend
, object
, psymval
, 0, thumb_bit
, arp
->checks_overflow());
9519 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
9520 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
9521 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
9522 case elfcpp::R_ARM_THM_MOVW_BREL
:
9523 reloc_status
= Arm_relocate_functions
<big_endian
>::thm_movw(
9524 paddend
, object
, psymval
, 0, thumb_bit
, arp
->checks_overflow());
9527 case elfcpp::R_ARM_THM_CALL
:
9528 case elfcpp::R_ARM_THM_XPC22
:
9529 case elfcpp::R_ARM_THM_JUMP24
:
9531 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
9532 r_type
, relinfo
, paddend
, NULL
, object
, 0, psymval
, 0, thumb_bit
,
9536 case elfcpp::R_ARM_PLT32
:
9537 case elfcpp::R_ARM_CALL
:
9538 case elfcpp::R_ARM_JUMP24
:
9539 case elfcpp::R_ARM_XPC25
:
9541 Arm_relocate_functions
<big_endian
>::arm_branch_common(
9542 r_type
, relinfo
, paddend
, NULL
, object
, 0, psymval
, 0, thumb_bit
,
9546 case elfcpp::R_ARM_THM_JUMP19
:
9548 Arm_relocate_functions
<big_endian
>::thm_jump19(paddend
, object
,
9549 psymval
, 0, thumb_bit
);
9552 case elfcpp::R_ARM_THM_JUMP6
:
9554 Arm_relocate_functions
<big_endian
>::thm_jump6(paddend
, object
, psymval
,
9558 case elfcpp::R_ARM_THM_JUMP8
:
9560 Arm_relocate_functions
<big_endian
>::thm_jump8(paddend
, object
, psymval
,
9564 case elfcpp::R_ARM_THM_JUMP11
:
9566 Arm_relocate_functions
<big_endian
>::thm_jump11(paddend
, object
, psymval
,
9570 case elfcpp::R_ARM_PREL31
:
9572 Arm_relocate_functions
<big_endian
>::prel31(paddend
, object
, psymval
, 0,
9576 case elfcpp::R_ARM_THM_PC8
:
9578 Arm_relocate_functions
<big_endian
>::thm_pc8(paddend
, object
, psymval
,
9582 case elfcpp::R_ARM_THM_PC12
:
9584 Arm_relocate_functions
<big_endian
>::thm_pc12(paddend
, object
, psymval
,
9588 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
9590 Arm_relocate_functions
<big_endian
>::thm_alu11(paddend
, object
, psymval
,
9594 // These relocation truncate relocation results so we cannot handle them
9595 // in a relocatable link.
9596 case elfcpp::R_ARM_MOVT_ABS
:
9597 case elfcpp::R_ARM_THM_MOVT_ABS
:
9598 case elfcpp::R_ARM_MOVT_PREL
:
9599 case elfcpp::R_ARM_MOVT_BREL
:
9600 case elfcpp::R_ARM_THM_MOVT_PREL
:
9601 case elfcpp::R_ARM_THM_MOVT_BREL
:
9602 case elfcpp::R_ARM_ALU_PC_G0_NC
:
9603 case elfcpp::R_ARM_ALU_PC_G0
:
9604 case elfcpp::R_ARM_ALU_PC_G1_NC
:
9605 case elfcpp::R_ARM_ALU_PC_G1
:
9606 case elfcpp::R_ARM_ALU_PC_G2
:
9607 case elfcpp::R_ARM_ALU_SB_G0_NC
:
9608 case elfcpp::R_ARM_ALU_SB_G0
:
9609 case elfcpp::R_ARM_ALU_SB_G1_NC
:
9610 case elfcpp::R_ARM_ALU_SB_G1
:
9611 case elfcpp::R_ARM_ALU_SB_G2
:
9612 case elfcpp::R_ARM_LDR_PC_G0
:
9613 case elfcpp::R_ARM_LDR_PC_G1
:
9614 case elfcpp::R_ARM_LDR_PC_G2
:
9615 case elfcpp::R_ARM_LDR_SB_G0
:
9616 case elfcpp::R_ARM_LDR_SB_G1
:
9617 case elfcpp::R_ARM_LDR_SB_G2
:
9618 case elfcpp::R_ARM_LDRS_PC_G0
:
9619 case elfcpp::R_ARM_LDRS_PC_G1
:
9620 case elfcpp::R_ARM_LDRS_PC_G2
:
9621 case elfcpp::R_ARM_LDRS_SB_G0
:
9622 case elfcpp::R_ARM_LDRS_SB_G1
:
9623 case elfcpp::R_ARM_LDRS_SB_G2
:
9624 case elfcpp::R_ARM_LDC_PC_G0
:
9625 case elfcpp::R_ARM_LDC_PC_G1
:
9626 case elfcpp::R_ARM_LDC_PC_G2
:
9627 case elfcpp::R_ARM_LDC_SB_G0
:
9628 case elfcpp::R_ARM_LDC_SB_G1
:
9629 case elfcpp::R_ARM_LDC_SB_G2
:
9630 gold_error(_("cannot handle %s in a relocatable link"),
9631 arp
->name().c_str());
9638 // Report any errors.
9639 switch (reloc_status
)
9641 case Arm_relocate_functions
<big_endian
>::STATUS_OKAY
:
9643 case Arm_relocate_functions
<big_endian
>::STATUS_OVERFLOW
:
9644 gold_error_at_location(relinfo
, relnum
, reloc
.get_r_offset(),
9645 _("relocation overflow in %s"),
9646 arp
->name().c_str());
9648 case Arm_relocate_functions
<big_endian
>::STATUS_BAD_RELOC
:
9649 gold_error_at_location(relinfo
, relnum
, reloc
.get_r_offset(),
9650 _("unexpected opcode while processing relocation %s"),
9651 arp
->name().c_str());
9658 // Return the value to use for a dynamic symbol which requires special
9659 // treatment. This is how we support equality comparisons of function
9660 // pointers across shared library boundaries, as described in the
9661 // processor specific ABI supplement.
9663 template<bool big_endian
>
9665 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
9667 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
9668 return this->plt_section()->address() + gsym
->plt_offset();
9671 // Map platform-specific relocs to real relocs
9673 template<bool big_endian
>
9675 Target_arm
<big_endian
>::get_real_reloc_type(unsigned int r_type
)
9679 case elfcpp::R_ARM_TARGET1
:
9680 // This is either R_ARM_ABS32 or R_ARM_REL32;
9681 return elfcpp::R_ARM_ABS32
;
9683 case elfcpp::R_ARM_TARGET2
:
9684 // This can be any reloc type but ususally is R_ARM_GOT_PREL
9685 return elfcpp::R_ARM_GOT_PREL
;
9692 // Whether if two EABI versions V1 and V2 are compatible.
9694 template<bool big_endian
>
9696 Target_arm
<big_endian
>::are_eabi_versions_compatible(
9697 elfcpp::Elf_Word v1
,
9698 elfcpp::Elf_Word v2
)
9700 // v4 and v5 are the same spec before and after it was released,
9701 // so allow mixing them.
9702 if ((v1
== elfcpp::EF_ARM_EABI_UNKNOWN
|| v2
== elfcpp::EF_ARM_EABI_UNKNOWN
)
9703 || (v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
9704 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
9710 // Combine FLAGS from an input object called NAME and the processor-specific
9711 // flags in the ELF header of the output. Much of this is adapted from the
9712 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9713 // in bfd/elf32-arm.c.
9715 template<bool big_endian
>
9717 Target_arm
<big_endian
>::merge_processor_specific_flags(
9718 const std::string
& name
,
9719 elfcpp::Elf_Word flags
)
9721 if (this->are_processor_specific_flags_set())
9723 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
9725 // Nothing to merge if flags equal to those in output.
9726 if (flags
== out_flags
)
9729 // Complain about various flag mismatches.
9730 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
9731 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
9732 if (!this->are_eabi_versions_compatible(version1
, version2
)
9733 && parameters
->options().warn_mismatch())
9734 gold_error(_("Source object %s has EABI version %d but output has "
9735 "EABI version %d."),
9737 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
9738 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
9742 // If the input is the default architecture and had the default
9743 // flags then do not bother setting the flags for the output
9744 // architecture, instead allow future merges to do this. If no
9745 // future merges ever set these flags then they will retain their
9746 // uninitialised values, which surprise surprise, correspond
9747 // to the default values.
9751 // This is the first time, just copy the flags.
9752 // We only copy the EABI version for now.
9753 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
9757 // Adjust ELF file header.
9758 template<bool big_endian
>
9760 Target_arm
<big_endian
>::do_adjust_elf_header(
9761 unsigned char* view
,
9764 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
9766 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
9767 unsigned char e_ident
[elfcpp::EI_NIDENT
];
9768 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
9770 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9771 == elfcpp::EF_ARM_EABI_UNKNOWN
)
9772 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
9774 e_ident
[elfcpp::EI_OSABI
] = 0;
9775 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
9777 // FIXME: Do EF_ARM_BE8 adjustment.
9779 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
9780 oehdr
.put_e_ident(e_ident
);
9783 // do_make_elf_object to override the same function in the base class.
9784 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9785 // to store ARM specific information. Hence we need to have our own
9786 // ELF object creation.
9788 template<bool big_endian
>
9790 Target_arm
<big_endian
>::do_make_elf_object(
9791 const std::string
& name
,
9792 Input_file
* input_file
,
9793 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
9795 int et
= ehdr
.get_e_type();
9796 if (et
== elfcpp::ET_REL
)
9798 Arm_relobj
<big_endian
>* obj
=
9799 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
9803 else if (et
== elfcpp::ET_DYN
)
9805 Sized_dynobj
<32, big_endian
>* obj
=
9806 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
9812 gold_error(_("%s: unsupported ELF file type %d"),
9818 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9819 // Returns -1 if no architecture could be read.
9820 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9822 template<bool big_endian
>
9824 Target_arm
<big_endian
>::get_secondary_compatible_arch(
9825 const Attributes_section_data
* pasd
)
9827 const Object_attribute
* known_attributes
=
9828 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
9830 // Note: the tag and its argument below are uleb128 values, though
9831 // currently-defined values fit in one byte for each.
9832 const std::string
& sv
=
9833 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
9835 && sv
.data()[0] == elfcpp::Tag_CPU_arch
9836 && (sv
.data()[1] & 128) != 128)
9837 return sv
.data()[1];
9839 // This tag is "safely ignorable", so don't complain if it looks funny.
9843 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9844 // The tag is removed if ARCH is -1.
9845 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9847 template<bool big_endian
>
9849 Target_arm
<big_endian
>::set_secondary_compatible_arch(
9850 Attributes_section_data
* pasd
,
9853 Object_attribute
* known_attributes
=
9854 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
9858 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
9862 // Note: the tag and its argument below are uleb128 values, though
9863 // currently-defined values fit in one byte for each.
9865 sv
[0] = elfcpp::Tag_CPU_arch
;
9866 gold_assert(arch
!= 0);
9870 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
9873 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9875 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9877 template<bool big_endian
>
9879 Target_arm
<big_endian
>::tag_cpu_arch_combine(
9882 int* secondary_compat_out
,
9884 int secondary_compat
)
9886 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9887 static const int v6t2
[] =
9899 static const int v6k
[] =
9912 static const int v7
[] =
9926 static const int v6_m
[] =
9941 static const int v6s_m
[] =
9957 static const int v7e_m
[] =
9974 static const int v4t_plus_v6_m
[] =
9990 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
9992 static const int* comb
[] =
10000 // Pseudo-architecture.
10004 // Check we've not got a higher architecture than we know about.
10006 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
10008 gold_error(_("%s: unknown CPU architecture"), name
);
10012 // Override old tag if we have a Tag_also_compatible_with on the output.
10014 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
10015 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
10016 oldtag
= T(V4T_PLUS_V6_M
);
10018 // And override the new tag if we have a Tag_also_compatible_with on the
10021 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
10022 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
10023 newtag
= T(V4T_PLUS_V6_M
);
10025 // Architectures before V6KZ add features monotonically.
10026 int tagh
= std::max(oldtag
, newtag
);
10027 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
10030 int tagl
= std::min(oldtag
, newtag
);
10031 int result
= comb
[tagh
- T(V6T2
)][tagl
];
10033 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
10034 // as the canonical version.
10035 if (result
== T(V4T_PLUS_V6_M
))
10038 *secondary_compat_out
= T(V6_M
);
10041 *secondary_compat_out
= -1;
10045 gold_error(_("%s: conflicting CPU architectures %d/%d"),
10046 name
, oldtag
, newtag
);
10054 // Helper to print AEABI enum tag value.
10056 template<bool big_endian
>
10058 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
10060 static const char* aeabi_enum_names
[] =
10061 { "", "variable-size", "32-bit", "" };
10062 const size_t aeabi_enum_names_size
=
10063 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
10065 if (value
< aeabi_enum_names_size
)
10066 return std::string(aeabi_enum_names
[value
]);
10070 sprintf(buffer
, "<unknown value %u>", value
);
10071 return std::string(buffer
);
10075 // Return the string value to store in TAG_CPU_name.
10077 template<bool big_endian
>
10079 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
10081 static const char* name_table
[] = {
10082 // These aren't real CPU names, but we can't guess
10083 // that from the architecture version alone.
10099 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
10101 if (value
< name_table_size
)
10102 return std::string(name_table
[value
]);
10106 sprintf(buffer
, "<unknown CPU value %u>", value
);
10107 return std::string(buffer
);
10111 // Merge object attributes from input file called NAME with those of the
10112 // output. The input object attributes are in the object pointed by PASD.
10114 template<bool big_endian
>
10116 Target_arm
<big_endian
>::merge_object_attributes(
10118 const Attributes_section_data
* pasd
)
10120 // Return if there is no attributes section data.
10124 // If output has no object attributes, just copy.
10125 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
10126 if (this->attributes_section_data_
== NULL
)
10128 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
10129 Object_attribute
* out_attr
=
10130 this->attributes_section_data_
->known_attributes(vendor
);
10132 // We do not output objects with Tag_MPextension_use_legacy - we move
10133 // the attribute's value to Tag_MPextension_use. */
10134 if (out_attr
[elfcpp::Tag_MPextension_use_legacy
].int_value() != 0)
10136 if (out_attr
[elfcpp::Tag_MPextension_use
].int_value() != 0
10137 && out_attr
[elfcpp::Tag_MPextension_use_legacy
].int_value()
10138 != out_attr
[elfcpp::Tag_MPextension_use
].int_value())
10140 gold_error(_("%s has both the current and legacy "
10141 "Tag_MPextension_use attributes"),
10145 out_attr
[elfcpp::Tag_MPextension_use
] =
10146 out_attr
[elfcpp::Tag_MPextension_use_legacy
];
10147 out_attr
[elfcpp::Tag_MPextension_use_legacy
].set_type(0);
10148 out_attr
[elfcpp::Tag_MPextension_use_legacy
].set_int_value(0);
10154 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
10155 Object_attribute
* out_attr
=
10156 this->attributes_section_data_
->known_attributes(vendor
);
10158 // This needs to happen before Tag_ABI_FP_number_model is merged. */
10159 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
10160 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
10162 // Ignore mismatches if the object doesn't use floating point. */
10163 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
10164 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
10165 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
10166 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0
10167 && parameters
->options().warn_mismatch())
10168 gold_error(_("%s uses VFP register arguments, output does not"),
10172 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
10174 // Merge this attribute with existing attributes.
10177 case elfcpp::Tag_CPU_raw_name
:
10178 case elfcpp::Tag_CPU_name
:
10179 // These are merged after Tag_CPU_arch.
10182 case elfcpp::Tag_ABI_optimization_goals
:
10183 case elfcpp::Tag_ABI_FP_optimization_goals
:
10184 // Use the first value seen.
10187 case elfcpp::Tag_CPU_arch
:
10189 unsigned int saved_out_attr
= out_attr
->int_value();
10190 // Merge Tag_CPU_arch and Tag_also_compatible_with.
10191 int secondary_compat
=
10192 this->get_secondary_compatible_arch(pasd
);
10193 int secondary_compat_out
=
10194 this->get_secondary_compatible_arch(
10195 this->attributes_section_data_
);
10196 out_attr
[i
].set_int_value(
10197 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
10198 &secondary_compat_out
,
10199 in_attr
[i
].int_value(),
10200 secondary_compat
));
10201 this->set_secondary_compatible_arch(this->attributes_section_data_
,
10202 secondary_compat_out
);
10204 // Merge Tag_CPU_name and Tag_CPU_raw_name.
10205 if (out_attr
[i
].int_value() == saved_out_attr
)
10206 ; // Leave the names alone.
10207 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
10209 // The output architecture has been changed to match the
10210 // input architecture. Use the input names.
10211 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
10212 in_attr
[elfcpp::Tag_CPU_name
].string_value());
10213 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
10214 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
10218 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
10219 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
10222 // If we still don't have a value for Tag_CPU_name,
10223 // make one up now. Tag_CPU_raw_name remains blank.
10224 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
10226 const std::string cpu_name
=
10227 this->tag_cpu_name_value(out_attr
[i
].int_value());
10228 // FIXME: If we see an unknown CPU, this will be set
10229 // to "<unknown CPU n>", where n is the attribute value.
10230 // This is different from BFD, which leaves the name alone.
10231 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
10236 case elfcpp::Tag_ARM_ISA_use
:
10237 case elfcpp::Tag_THUMB_ISA_use
:
10238 case elfcpp::Tag_WMMX_arch
:
10239 case elfcpp::Tag_Advanced_SIMD_arch
:
10240 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
10241 case elfcpp::Tag_ABI_FP_rounding
:
10242 case elfcpp::Tag_ABI_FP_exceptions
:
10243 case elfcpp::Tag_ABI_FP_user_exceptions
:
10244 case elfcpp::Tag_ABI_FP_number_model
:
10245 case elfcpp::Tag_VFP_HP_extension
:
10246 case elfcpp::Tag_CPU_unaligned_access
:
10247 case elfcpp::Tag_T2EE_use
:
10248 case elfcpp::Tag_Virtualization_use
:
10249 case elfcpp::Tag_MPextension_use
:
10250 // Use the largest value specified.
10251 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
10252 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10255 case elfcpp::Tag_ABI_align8_preserved
:
10256 case elfcpp::Tag_ABI_PCS_RO_data
:
10257 // Use the smallest value specified.
10258 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
10259 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10262 case elfcpp::Tag_ABI_align8_needed
:
10263 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
10264 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
10265 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
10268 // This error message should be enabled once all non-conformant
10269 // binaries in the toolchain have had the attributes set
10271 // gold_error(_("output 8-byte data alignment conflicts with %s"),
10275 case elfcpp::Tag_ABI_FP_denormal
:
10276 case elfcpp::Tag_ABI_PCS_GOT_use
:
10278 // These tags have 0 = don't care, 1 = strong requirement,
10279 // 2 = weak requirement.
10280 static const int order_021
[3] = {0, 2, 1};
10282 // Use the "greatest" from the sequence 0, 2, 1, or the largest
10283 // value if greater than 2 (for future-proofing).
10284 if ((in_attr
[i
].int_value() > 2
10285 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
10286 || (in_attr
[i
].int_value() <= 2
10287 && out_attr
[i
].int_value() <= 2
10288 && (order_021
[in_attr
[i
].int_value()]
10289 > order_021
[out_attr
[i
].int_value()])))
10290 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10294 case elfcpp::Tag_CPU_arch_profile
:
10295 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
10297 // 0 will merge with anything.
10298 // 'A' and 'S' merge to 'A'.
10299 // 'R' and 'S' merge to 'R'.
10300 // 'M' and 'A|R|S' is an error.
10301 if (out_attr
[i
].int_value() == 0
10302 || (out_attr
[i
].int_value() == 'S'
10303 && (in_attr
[i
].int_value() == 'A'
10304 || in_attr
[i
].int_value() == 'R')))
10305 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10306 else if (in_attr
[i
].int_value() == 0
10307 || (in_attr
[i
].int_value() == 'S'
10308 && (out_attr
[i
].int_value() == 'A'
10309 || out_attr
[i
].int_value() == 'R')))
10311 else if (parameters
->options().warn_mismatch())
10314 (_("conflicting architecture profiles %c/%c"),
10315 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
10316 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
10320 case elfcpp::Tag_VFP_arch
:
10322 static const struct
10326 } vfp_versions
[7] =
10337 // Values greater than 6 aren't defined, so just pick the
10339 if (in_attr
[i
].int_value() > 6
10340 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
10342 *out_attr
= *in_attr
;
10345 // The output uses the superset of input features
10346 // (ISA version) and registers.
10347 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
10348 vfp_versions
[out_attr
[i
].int_value()].ver
);
10349 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
10350 vfp_versions
[out_attr
[i
].int_value()].regs
);
10351 // This assumes all possible supersets are also a valid
10354 for (newval
= 6; newval
> 0; newval
--)
10356 if (regs
== vfp_versions
[newval
].regs
10357 && ver
== vfp_versions
[newval
].ver
)
10360 out_attr
[i
].set_int_value(newval
);
10363 case elfcpp::Tag_PCS_config
:
10364 if (out_attr
[i
].int_value() == 0)
10365 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10366 else if (in_attr
[i
].int_value() != 0
10367 && out_attr
[i
].int_value() != 0
10368 && parameters
->options().warn_mismatch())
10370 // It's sometimes ok to mix different configs, so this is only
10372 gold_warning(_("%s: conflicting platform configuration"), name
);
10375 case elfcpp::Tag_ABI_PCS_R9_use
:
10376 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
10377 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
10378 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
10379 && parameters
->options().warn_mismatch())
10381 gold_error(_("%s: conflicting use of R9"), name
);
10383 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
10384 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10386 case elfcpp::Tag_ABI_PCS_RW_data
:
10387 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
10388 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
10389 != elfcpp::AEABI_R9_SB
)
10390 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
10391 != elfcpp::AEABI_R9_unused
)
10392 && parameters
->options().warn_mismatch())
10394 gold_error(_("%s: SB relative addressing conflicts with use "
10398 // Use the smallest value specified.
10399 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
10400 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10402 case elfcpp::Tag_ABI_PCS_wchar_t
:
10403 if (out_attr
[i
].int_value()
10404 && in_attr
[i
].int_value()
10405 && out_attr
[i
].int_value() != in_attr
[i
].int_value()
10406 && parameters
->options().warn_mismatch()
10407 && parameters
->options().wchar_size_warning())
10409 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
10410 "use %u-byte wchar_t; use of wchar_t values "
10411 "across objects may fail"),
10412 name
, in_attr
[i
].int_value(),
10413 out_attr
[i
].int_value());
10415 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
10416 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10418 case elfcpp::Tag_ABI_enum_size
:
10419 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
10421 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
10422 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
10424 // The existing object is compatible with anything.
10425 // Use whatever requirements the new object has.
10426 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10428 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
10429 && out_attr
[i
].int_value() != in_attr
[i
].int_value()
10430 && parameters
->options().warn_mismatch()
10431 && parameters
->options().enum_size_warning())
10433 unsigned int in_value
= in_attr
[i
].int_value();
10434 unsigned int out_value
= out_attr
[i
].int_value();
10435 gold_warning(_("%s uses %s enums yet the output is to use "
10436 "%s enums; use of enum values across objects "
10439 this->aeabi_enum_name(in_value
).c_str(),
10440 this->aeabi_enum_name(out_value
).c_str());
10444 case elfcpp::Tag_ABI_VFP_args
:
10447 case elfcpp::Tag_ABI_WMMX_args
:
10448 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
10449 && parameters
->options().warn_mismatch())
10451 gold_error(_("%s uses iWMMXt register arguments, output does "
10456 case Object_attribute::Tag_compatibility
:
10457 // Merged in target-independent code.
10459 case elfcpp::Tag_ABI_HardFP_use
:
10460 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
10461 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
10462 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
10463 out_attr
[i
].set_int_value(3);
10464 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
10465 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10467 case elfcpp::Tag_ABI_FP_16bit_format
:
10468 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
10470 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
10471 && parameters
->options().warn_mismatch())
10472 gold_error(_("fp16 format mismatch between %s and output"),
10475 if (in_attr
[i
].int_value() != 0)
10476 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10479 case elfcpp::Tag_DIV_use
:
10480 // This tag is set to zero if we can use UDIV and SDIV in Thumb
10481 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
10482 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
10483 // CPU. We will merge as follows: If the input attribute's value
10484 // is one then the output attribute's value remains unchanged. If
10485 // the input attribute's value is zero or two then if the output
10486 // attribute's value is one the output value is set to the input
10487 // value, otherwise the output value must be the same as the
10489 if (in_attr
[i
].int_value() != 1 && out_attr
[i
].int_value() != 1)
10491 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
10493 gold_error(_("DIV usage mismatch between %s and output"),
10498 if (in_attr
[i
].int_value() != 1)
10499 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
10503 case elfcpp::Tag_MPextension_use_legacy
:
10504 // We don't output objects with Tag_MPextension_use_legacy - we
10505 // move the value to Tag_MPextension_use.
10506 if (in_attr
[i
].int_value() != 0
10507 && in_attr
[elfcpp::Tag_MPextension_use
].int_value() != 0)
10509 if (in_attr
[elfcpp::Tag_MPextension_use
].int_value()
10510 != in_attr
[i
].int_value())
10512 gold_error(_("%s has has both the current and legacy "
10513 "Tag_MPextension_use attributes"),
10518 if (in_attr
[i
].int_value()
10519 > out_attr
[elfcpp::Tag_MPextension_use
].int_value())
10520 out_attr
[elfcpp::Tag_MPextension_use
] = in_attr
[i
];
10524 case elfcpp::Tag_nodefaults
:
10525 // This tag is set if it exists, but the value is unused (and is
10526 // typically zero). We don't actually need to do anything here -
10527 // the merge happens automatically when the type flags are merged
10530 case elfcpp::Tag_also_compatible_with
:
10531 // Already done in Tag_CPU_arch.
10533 case elfcpp::Tag_conformance
:
10534 // Keep the attribute if it matches. Throw it away otherwise.
10535 // No attribute means no claim to conform.
10536 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
10537 out_attr
[i
].set_string_value("");
10542 const char* err_object
= NULL
;
10544 // The "known_obj_attributes" table does contain some undefined
10545 // attributes. Ensure that there are unused.
10546 if (out_attr
[i
].int_value() != 0
10547 || out_attr
[i
].string_value() != "")
10548 err_object
= "output";
10549 else if (in_attr
[i
].int_value() != 0
10550 || in_attr
[i
].string_value() != "")
10553 if (err_object
!= NULL
10554 && parameters
->options().warn_mismatch())
10556 // Attribute numbers >=64 (mod 128) can be safely ignored.
10557 if ((i
& 127) < 64)
10558 gold_error(_("%s: unknown mandatory EABI object attribute "
10562 gold_warning(_("%s: unknown EABI object attribute %d"),
10566 // Only pass on attributes that match in both inputs.
10567 if (!in_attr
[i
].matches(out_attr
[i
]))
10569 out_attr
[i
].set_int_value(0);
10570 out_attr
[i
].set_string_value("");
10575 // If out_attr was copied from in_attr then it won't have a type yet.
10576 if (in_attr
[i
].type() && !out_attr
[i
].type())
10577 out_attr
[i
].set_type(in_attr
[i
].type());
10580 // Merge Tag_compatibility attributes and any common GNU ones.
10581 this->attributes_section_data_
->merge(name
, pasd
);
10583 // Check for any attributes not known on ARM.
10584 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
10585 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
10586 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
10587 Other_attributes
* out_other_attributes
=
10588 this->attributes_section_data_
->other_attributes(vendor
);
10589 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
10591 while (in_iter
!= in_other_attributes
->end()
10592 || out_iter
!= out_other_attributes
->end())
10594 const char* err_object
= NULL
;
10597 // The tags for each list are in numerical order.
10598 // If the tags are equal, then merge.
10599 if (out_iter
!= out_other_attributes
->end()
10600 && (in_iter
== in_other_attributes
->end()
10601 || in_iter
->first
> out_iter
->first
))
10603 // This attribute only exists in output. We can't merge, and we
10604 // don't know what the tag means, so delete it.
10605 err_object
= "output";
10606 err_tag
= out_iter
->first
;
10607 int saved_tag
= out_iter
->first
;
10608 delete out_iter
->second
;
10609 out_other_attributes
->erase(out_iter
);
10610 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
10612 else if (in_iter
!= in_other_attributes
->end()
10613 && (out_iter
!= out_other_attributes
->end()
10614 || in_iter
->first
< out_iter
->first
))
10616 // This attribute only exists in input. We can't merge, and we
10617 // don't know what the tag means, so ignore it.
10619 err_tag
= in_iter
->first
;
10622 else // The tags are equal.
10624 // As present, all attributes in the list are unknown, and
10625 // therefore can't be merged meaningfully.
10626 err_object
= "output";
10627 err_tag
= out_iter
->first
;
10629 // Only pass on attributes that match in both inputs.
10630 if (!in_iter
->second
->matches(*(out_iter
->second
)))
10632 // No match. Delete the attribute.
10633 int saved_tag
= out_iter
->first
;
10634 delete out_iter
->second
;
10635 out_other_attributes
->erase(out_iter
);
10636 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
10640 // Matched. Keep the attribute and move to the next.
10646 if (err_object
&& parameters
->options().warn_mismatch())
10648 // Attribute numbers >=64 (mod 128) can be safely ignored. */
10649 if ((err_tag
& 127) < 64)
10651 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
10652 err_object
, err_tag
);
10656 gold_warning(_("%s: unknown EABI object attribute %d"),
10657 err_object
, err_tag
);
10663 // Stub-generation methods for Target_arm.
10665 // Make a new Arm_input_section object.
10667 template<bool big_endian
>
10668 Arm_input_section
<big_endian
>*
10669 Target_arm
<big_endian
>::new_arm_input_section(
10671 unsigned int shndx
)
10673 Section_id
sid(relobj
, shndx
);
10675 Arm_input_section
<big_endian
>* arm_input_section
=
10676 new Arm_input_section
<big_endian
>(relobj
, shndx
);
10677 arm_input_section
->init();
10679 // Register new Arm_input_section in map for look-up.
10680 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
10681 this->arm_input_section_map_
.insert(std::make_pair(sid
, arm_input_section
));
10683 // Make sure that it we have not created another Arm_input_section
10684 // for this input section already.
10685 gold_assert(ins
.second
);
10687 return arm_input_section
;
10690 // Find the Arm_input_section object corresponding to the SHNDX-th input
10691 // section of RELOBJ.
10693 template<bool big_endian
>
10694 Arm_input_section
<big_endian
>*
10695 Target_arm
<big_endian
>::find_arm_input_section(
10697 unsigned int shndx
) const
10699 Section_id
sid(relobj
, shndx
);
10700 typename
Arm_input_section_map::const_iterator p
=
10701 this->arm_input_section_map_
.find(sid
);
10702 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
10705 // Make a new stub table.
10707 template<bool big_endian
>
10708 Stub_table
<big_endian
>*
10709 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
10711 Stub_table
<big_endian
>* stub_table
=
10712 new Stub_table
<big_endian
>(owner
);
10713 this->stub_tables_
.push_back(stub_table
);
10715 stub_table
->set_address(owner
->address() + owner
->data_size());
10716 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
10717 stub_table
->finalize_data_size();
10722 // Scan a relocation for stub generation.
10724 template<bool big_endian
>
10726 Target_arm
<big_endian
>::scan_reloc_for_stub(
10727 const Relocate_info
<32, big_endian
>* relinfo
,
10728 unsigned int r_type
,
10729 const Sized_symbol
<32>* gsym
,
10730 unsigned int r_sym
,
10731 const Symbol_value
<32>* psymval
,
10732 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
10733 Arm_address address
)
10735 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
10737 const Arm_relobj
<big_endian
>* arm_relobj
=
10738 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
10740 bool target_is_thumb
;
10741 Symbol_value
<32> symval
;
10744 // This is a global symbol. Determine if we use PLT and if the
10745 // final target is THUMB.
10746 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
10748 // This uses a PLT, change the symbol value.
10749 symval
.set_output_value(this->plt_section()->address()
10750 + gsym
->plt_offset());
10752 target_is_thumb
= false;
10754 else if (gsym
->is_undefined())
10755 // There is no need to generate a stub symbol is undefined.
10760 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
10761 || (gsym
->type() == elfcpp::STT_FUNC
10762 && !gsym
->is_undefined()
10763 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
10768 // This is a local symbol. Determine if the final target is THUMB.
10769 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
10772 // Strip LSB if this points to a THUMB target.
10773 const Arm_reloc_property
* reloc_property
=
10774 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
10775 gold_assert(reloc_property
!= NULL
);
10776 if (target_is_thumb
10777 && reloc_property
->uses_thumb_bit()
10778 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
10780 Arm_address stripped_value
=
10781 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
10782 symval
.set_output_value(stripped_value
);
10786 // Get the symbol value.
10787 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
10789 // Owing to pipelining, the PC relative branches below actually skip
10790 // two instructions when the branch offset is 0.
10791 Arm_address destination
;
10794 case elfcpp::R_ARM_CALL
:
10795 case elfcpp::R_ARM_JUMP24
:
10796 case elfcpp::R_ARM_PLT32
:
10798 destination
= value
+ addend
+ 8;
10800 case elfcpp::R_ARM_THM_CALL
:
10801 case elfcpp::R_ARM_THM_XPC22
:
10802 case elfcpp::R_ARM_THM_JUMP24
:
10803 case elfcpp::R_ARM_THM_JUMP19
:
10805 destination
= value
+ addend
+ 4;
10808 gold_unreachable();
10811 Reloc_stub
* stub
= NULL
;
10812 Stub_type stub_type
=
10813 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
10815 if (stub_type
!= arm_stub_none
)
10817 // Try looking up an existing stub from a stub table.
10818 Stub_table
<big_endian
>* stub_table
=
10819 arm_relobj
->stub_table(relinfo
->data_shndx
);
10820 gold_assert(stub_table
!= NULL
);
10822 // Locate stub by destination.
10823 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
10825 // Create a stub if there is not one already
10826 stub
= stub_table
->find_reloc_stub(stub_key
);
10829 // create a new stub and add it to stub table.
10830 stub
= this->stub_factory().make_reloc_stub(stub_type
);
10831 stub_table
->add_reloc_stub(stub
, stub_key
);
10834 // Record the destination address.
10835 stub
->set_destination_address(destination
10836 | (target_is_thumb
? 1 : 0));
10839 // For Cortex-A8, we need to record a relocation at 4K page boundary.
10840 if (this->fix_cortex_a8_
10841 && (r_type
== elfcpp::R_ARM_THM_JUMP24
10842 || r_type
== elfcpp::R_ARM_THM_JUMP19
10843 || r_type
== elfcpp::R_ARM_THM_CALL
10844 || r_type
== elfcpp::R_ARM_THM_XPC22
)
10845 && (address
& 0xfffU
) == 0xffeU
)
10847 // Found a candidate. Note we haven't checked the destination is
10848 // within 4K here: if we do so (and don't create a record) we can't
10849 // tell that a branch should have been relocated when scanning later.
10850 this->cortex_a8_relocs_info_
[address
] =
10851 new Cortex_a8_reloc(stub
, r_type
,
10852 destination
| (target_is_thumb
? 1 : 0));
10856 // This function scans a relocation sections for stub generation.
10857 // The template parameter Relocate must be a class type which provides
10858 // a single function, relocate(), which implements the machine
10859 // specific part of a relocation.
10861 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10862 // SHT_REL or SHT_RELA.
10864 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10865 // of relocs. OUTPUT_SECTION is the output section.
10866 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10867 // mapped to output offsets.
10869 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10870 // VIEW_SIZE is the size. These refer to the input section, unless
10871 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10872 // the output section.
10874 template<bool big_endian
>
10875 template<int sh_type
>
10877 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
10878 const Relocate_info
<32, big_endian
>* relinfo
,
10879 const unsigned char* prelocs
,
10880 size_t reloc_count
,
10881 Output_section
* output_section
,
10882 bool needs_special_offset_handling
,
10883 const unsigned char* view
,
10884 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
10887 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
10888 const int reloc_size
=
10889 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
10891 Arm_relobj
<big_endian
>* arm_object
=
10892 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
10893 unsigned int local_count
= arm_object
->local_symbol_count();
10895 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
10897 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
10899 Reltype
reloc(prelocs
);
10901 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
10902 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
10903 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
10905 r_type
= this->get_real_reloc_type(r_type
);
10907 // Only a few relocation types need stubs.
10908 if ((r_type
!= elfcpp::R_ARM_CALL
)
10909 && (r_type
!= elfcpp::R_ARM_JUMP24
)
10910 && (r_type
!= elfcpp::R_ARM_PLT32
)
10911 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
10912 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
10913 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
10914 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
)
10915 && (r_type
!= elfcpp::R_ARM_V4BX
))
10918 section_offset_type offset
=
10919 convert_to_section_size_type(reloc
.get_r_offset());
10921 if (needs_special_offset_handling
)
10923 offset
= output_section
->output_offset(relinfo
->object
,
10924 relinfo
->data_shndx
,
10930 // Create a v4bx stub if --fix-v4bx-interworking is used.
10931 if (r_type
== elfcpp::R_ARM_V4BX
)
10933 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
)
10935 // Get the BX instruction.
10936 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
10937 const Valtype
* wv
=
10938 reinterpret_cast<const Valtype
*>(view
+ offset
);
10939 elfcpp::Elf_types
<32>::Elf_Swxword insn
=
10940 elfcpp::Swap
<32, big_endian
>::readval(wv
);
10941 const uint32_t reg
= (insn
& 0xf);
10945 // Try looking up an existing stub from a stub table.
10946 Stub_table
<big_endian
>* stub_table
=
10947 arm_object
->stub_table(relinfo
->data_shndx
);
10948 gold_assert(stub_table
!= NULL
);
10950 if (stub_table
->find_arm_v4bx_stub(reg
) == NULL
)
10952 // create a new stub and add it to stub table.
10953 Arm_v4bx_stub
* stub
=
10954 this->stub_factory().make_arm_v4bx_stub(reg
);
10955 gold_assert(stub
!= NULL
);
10956 stub_table
->add_arm_v4bx_stub(stub
);
10964 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
10965 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
10966 stub_addend_reader(r_type
, view
+ offset
, reloc
);
10968 const Sized_symbol
<32>* sym
;
10970 Symbol_value
<32> symval
;
10971 const Symbol_value
<32> *psymval
;
10972 bool is_defined_in_discarded_section
;
10973 unsigned int shndx
;
10974 if (r_sym
< local_count
)
10977 psymval
= arm_object
->local_symbol(r_sym
);
10979 // If the local symbol belongs to a section we are discarding,
10980 // and that section is a debug section, try to find the
10981 // corresponding kept section and map this symbol to its
10982 // counterpart in the kept section. The symbol must not
10983 // correspond to a section we are folding.
10985 shndx
= psymval
->input_shndx(&is_ordinary
);
10986 is_defined_in_discarded_section
=
10988 && shndx
!= elfcpp::SHN_UNDEF
10989 && !arm_object
->is_section_included(shndx
)
10990 && !relinfo
->symtab
->is_section_folded(arm_object
, shndx
));
10992 // We need to compute the would-be final value of this local
10994 if (!is_defined_in_discarded_section
)
10996 typedef Sized_relobj
<32, big_endian
> ObjType
;
10997 typename
ObjType::Compute_final_local_value_status status
=
10998 arm_object
->compute_final_local_value(r_sym
, psymval
, &symval
,
11000 if (status
== ObjType::CFLV_OK
)
11002 // Currently we cannot handle a branch to a target in
11003 // a merged section. If this is the case, issue an error
11004 // and also free the merge symbol value.
11005 if (!symval
.has_output_value())
11007 const std::string
& section_name
=
11008 arm_object
->section_name(shndx
);
11009 arm_object
->error(_("cannot handle branch to local %u "
11010 "in a merged section %s"),
11011 r_sym
, section_name
.c_str());
11017 // We cannot determine the final value.
11024 const Symbol
* gsym
;
11025 gsym
= arm_object
->global_symbol(r_sym
);
11026 gold_assert(gsym
!= NULL
);
11027 if (gsym
->is_forwarder())
11028 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
11030 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
11031 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
11032 symval
.set_output_symtab_index(sym
->symtab_index());
11034 symval
.set_no_output_symtab_entry();
11036 // We need to compute the would-be final value of this global
11038 const Symbol_table
* symtab
= relinfo
->symtab
;
11039 const Sized_symbol
<32>* sized_symbol
=
11040 symtab
->get_sized_symbol
<32>(gsym
);
11041 Symbol_table::Compute_final_value_status status
;
11042 Arm_address value
=
11043 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
11045 // Skip this if the symbol has not output section.
11046 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
11048 symval
.set_output_value(value
);
11050 if (gsym
->type() == elfcpp::STT_TLS
)
11051 symval
.set_is_tls_symbol();
11052 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
11053 symval
.set_is_ifunc_symbol();
11056 is_defined_in_discarded_section
=
11057 (gsym
->is_defined_in_discarded_section()
11058 && gsym
->is_undefined());
11062 Symbol_value
<32> symval2
;
11063 if (is_defined_in_discarded_section
)
11065 if (comdat_behavior
== CB_UNDETERMINED
)
11067 std::string name
= arm_object
->section_name(relinfo
->data_shndx
);
11068 comdat_behavior
= get_comdat_behavior(name
.c_str());
11070 if (comdat_behavior
== CB_PRETEND
)
11072 // FIXME: This case does not work for global symbols.
11073 // We have no place to store the original section index.
11074 // Fortunately this does not matter for comdat sections,
11075 // only for sections explicitly discarded by a linker
11078 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
11079 arm_object
->map_to_kept_section(shndx
, &found
);
11081 symval2
.set_output_value(value
+ psymval
->input_value());
11083 symval2
.set_output_value(0);
11087 if (comdat_behavior
== CB_WARNING
)
11088 gold_warning_at_location(relinfo
, i
, offset
,
11089 _("relocation refers to discarded "
11091 symval2
.set_output_value(0);
11093 symval2
.set_no_output_symtab_entry();
11094 psymval
= &symval2
;
11097 // If symbol is a section symbol, we don't know the actual type of
11098 // destination. Give up.
11099 if (psymval
->is_section_symbol())
11102 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
11103 addend
, view_address
+ offset
);
11107 // Scan an input section for stub generation.
11109 template<bool big_endian
>
11111 Target_arm
<big_endian
>::scan_section_for_stubs(
11112 const Relocate_info
<32, big_endian
>* relinfo
,
11113 unsigned int sh_type
,
11114 const unsigned char* prelocs
,
11115 size_t reloc_count
,
11116 Output_section
* output_section
,
11117 bool needs_special_offset_handling
,
11118 const unsigned char* view
,
11119 Arm_address view_address
,
11120 section_size_type view_size
)
11122 if (sh_type
== elfcpp::SHT_REL
)
11123 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
11128 needs_special_offset_handling
,
11132 else if (sh_type
== elfcpp::SHT_RELA
)
11133 // We do not support RELA type relocations yet. This is provided for
11135 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
11140 needs_special_offset_handling
,
11145 gold_unreachable();
11148 // Group input sections for stub generation.
11150 // We goup input sections in an output sections so that the total size,
11151 // including any padding space due to alignment is smaller than GROUP_SIZE
11152 // unless the only input section in group is bigger than GROUP_SIZE already.
11153 // Then an ARM stub table is created to follow the last input section
11154 // in group. For each group an ARM stub table is created an is placed
11155 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
11156 // extend the group after the stub table.
11158 template<bool big_endian
>
11160 Target_arm
<big_endian
>::group_sections(
11162 section_size_type group_size
,
11163 bool stubs_always_after_branch
)
11165 // Group input sections and insert stub table
11166 Layout::Section_list section_list
;
11167 layout
->get_allocated_sections(§ion_list
);
11168 for (Layout::Section_list::const_iterator p
= section_list
.begin();
11169 p
!= section_list
.end();
11172 Arm_output_section
<big_endian
>* output_section
=
11173 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
11174 output_section
->group_sections(group_size
, stubs_always_after_branch
,
11179 // Relaxation hook. This is where we do stub generation.
11181 template<bool big_endian
>
11183 Target_arm
<big_endian
>::do_relax(
11185 const Input_objects
* input_objects
,
11186 Symbol_table
* symtab
,
11189 // No need to generate stubs if this is a relocatable link.
11190 gold_assert(!parameters
->options().relocatable());
11192 // If this is the first pass, we need to group input sections into
11194 bool done_exidx_fixup
= false;
11195 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
11198 // Determine the stub group size. The group size is the absolute
11199 // value of the parameter --stub-group-size. If --stub-group-size
11200 // is passed a negative value, we restict stubs to be always after
11201 // the stubbed branches.
11202 int32_t stub_group_size_param
=
11203 parameters
->options().stub_group_size();
11204 bool stubs_always_after_branch
= stub_group_size_param
< 0;
11205 section_size_type stub_group_size
= abs(stub_group_size_param
);
11207 if (stub_group_size
== 1)
11210 // Thumb branch range is +-4MB has to be used as the default
11211 // maximum size (a given section can contain both ARM and Thumb
11212 // code, so the worst case has to be taken into account). If we are
11213 // fixing cortex-a8 errata, the branch range has to be even smaller,
11214 // since wide conditional branch has a range of +-1MB only.
11216 // This value is 48K less than that, which allows for 4096
11217 // 12-byte stubs. If we exceed that, then we will fail to link.
11218 // The user will have to relink with an explicit group size
11220 stub_group_size
= 4145152;
11223 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
11224 // page as the first half of a 32-bit branch straddling two 4K pages.
11225 // This is a crude way of enforcing that. In addition, long conditional
11226 // branches of THUMB-2 have a range of +-1M. If we are fixing cortex-A8
11227 // erratum, limit the group size to (1M - 12k) to avoid unreachable
11228 // cortex-A8 stubs from long conditional branches.
11229 if (this->fix_cortex_a8_
)
11231 stubs_always_after_branch
= true;
11232 const section_size_type cortex_a8_group_size
= 1024 * (1024 - 12);
11233 stub_group_size
= std::max(stub_group_size
, cortex_a8_group_size
);
11236 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
11238 // Also fix .ARM.exidx section coverage.
11239 Arm_output_section
<big_endian
>* exidx_output_section
= NULL
;
11240 for (Layout::Section_list::const_iterator p
=
11241 layout
->section_list().begin();
11242 p
!= layout
->section_list().end();
11244 if ((*p
)->type() == elfcpp::SHT_ARM_EXIDX
)
11246 if (exidx_output_section
== NULL
)
11247 exidx_output_section
=
11248 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
11250 // We cannot handle this now.
11251 gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
11252 "non-relocatable link"),
11253 exidx_output_section
->name(),
11257 if (exidx_output_section
!= NULL
)
11259 this->fix_exidx_coverage(layout
, input_objects
, exidx_output_section
,
11261 done_exidx_fixup
= true;
11266 // If this is not the first pass, addresses and file offsets have
11267 // been reset at this point, set them here.
11268 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
11269 sp
!= this->stub_tables_
.end();
11272 Arm_input_section
<big_endian
>* owner
= (*sp
)->owner();
11273 off_t off
= align_address(owner
->original_size(),
11274 (*sp
)->addralign());
11275 (*sp
)->set_address_and_file_offset(owner
->address() + off
,
11276 owner
->offset() + off
);
11280 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
11281 // beginning of each relaxation pass, just blow away all the stubs.
11282 // Alternatively, we could selectively remove only the stubs and reloc
11283 // information for code sections that have moved since the last pass.
11284 // That would require more book-keeping.
11285 if (this->fix_cortex_a8_
)
11287 // Clear all Cortex-A8 reloc information.
11288 for (typename
Cortex_a8_relocs_info::const_iterator p
=
11289 this->cortex_a8_relocs_info_
.begin();
11290 p
!= this->cortex_a8_relocs_info_
.end();
11293 this->cortex_a8_relocs_info_
.clear();
11295 // Remove all Cortex-A8 stubs.
11296 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
11297 sp
!= this->stub_tables_
.end();
11299 (*sp
)->remove_all_cortex_a8_stubs();
11302 // Scan relocs for relocation stubs
11303 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
11304 op
!= input_objects
->relobj_end();
11307 Arm_relobj
<big_endian
>* arm_relobj
=
11308 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
11309 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
11312 // Check all stub tables to see if any of them have their data sizes
11313 // or addresses alignments changed. These are the only things that
11315 bool any_stub_table_changed
= false;
11316 Unordered_set
<const Output_section
*> sections_needing_adjustment
;
11317 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
11318 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
11321 if ((*sp
)->update_data_size_and_addralign())
11323 // Update data size of stub table owner.
11324 Arm_input_section
<big_endian
>* owner
= (*sp
)->owner();
11325 uint64_t address
= owner
->address();
11326 off_t offset
= owner
->offset();
11327 owner
->reset_address_and_file_offset();
11328 owner
->set_address_and_file_offset(address
, offset
);
11330 sections_needing_adjustment
.insert(owner
->output_section());
11331 any_stub_table_changed
= true;
11335 // Output_section_data::output_section() returns a const pointer but we
11336 // need to update output sections, so we record all output sections needing
11337 // update above and scan the sections here to find out what sections need
11339 for(Layout::Section_list::const_iterator p
= layout
->section_list().begin();
11340 p
!= layout
->section_list().end();
11343 if (sections_needing_adjustment
.find(*p
)
11344 != sections_needing_adjustment
.end())
11345 (*p
)->set_section_offsets_need_adjustment();
11348 // Stop relaxation if no EXIDX fix-up and no stub table change.
11349 bool continue_relaxation
= done_exidx_fixup
|| any_stub_table_changed
;
11351 // Finalize the stubs in the last relaxation pass.
11352 if (!continue_relaxation
)
11354 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
11355 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
11357 (*sp
)->finalize_stubs();
11359 // Update output local symbol counts of objects if necessary.
11360 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
11361 op
!= input_objects
->relobj_end();
11364 Arm_relobj
<big_endian
>* arm_relobj
=
11365 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
11367 // Update output local symbol counts. We need to discard local
11368 // symbols defined in parts of input sections that are discarded by
11370 if (arm_relobj
->output_local_symbol_count_needs_update())
11371 arm_relobj
->update_output_local_symbol_count();
11375 return continue_relaxation
;
11378 // Relocate a stub.
11380 template<bool big_endian
>
11382 Target_arm
<big_endian
>::relocate_stub(
11384 const Relocate_info
<32, big_endian
>* relinfo
,
11385 Output_section
* output_section
,
11386 unsigned char* view
,
11387 Arm_address address
,
11388 section_size_type view_size
)
11391 const Stub_template
* stub_template
= stub
->stub_template();
11392 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
11394 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
11395 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
11397 unsigned int r_type
= insn
->r_type();
11398 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
11399 section_size_type reloc_size
= insn
->size();
11400 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
11402 // This is the address of the stub destination.
11403 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
11404 Symbol_value
<32> symval
;
11405 symval
.set_output_value(target
);
11407 // Synthesize a fake reloc just in case. We don't have a symbol so
11409 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
11410 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
11411 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
11412 reloc_write
.put_r_offset(reloc_offset
);
11413 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
11414 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
11416 relocate
.relocate(relinfo
, this, output_section
,
11417 this->fake_relnum_for_stubs
, rel
, r_type
,
11418 NULL
, &symval
, view
+ reloc_offset
,
11419 address
+ reloc_offset
, reloc_size
);
11423 // Determine whether an object attribute tag takes an integer, a
11426 template<bool big_endian
>
11428 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
11430 if (tag
== Object_attribute::Tag_compatibility
)
11431 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11432 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
11433 else if (tag
== elfcpp::Tag_nodefaults
)
11434 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11435 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
11436 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
11437 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
11439 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
11441 return ((tag
& 1) != 0
11442 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
11443 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
11446 // Reorder attributes.
11448 // The ABI defines that Tag_conformance should be emitted first, and that
11449 // Tag_nodefaults should be second (if either is defined). This sets those
11450 // two positions, and bumps up the position of all the remaining tags to
11453 template<bool big_endian
>
11455 Target_arm
<big_endian
>::do_attributes_order(int num
) const
11457 // Reorder the known object attributes in output. We want to move
11458 // Tag_conformance to position 4 and Tag_conformance to position 5
11459 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
11461 return elfcpp::Tag_conformance
;
11463 return elfcpp::Tag_nodefaults
;
11464 if ((num
- 2) < elfcpp::Tag_nodefaults
)
11466 if ((num
- 1) < elfcpp::Tag_conformance
)
11471 // Scan a span of THUMB code for Cortex-A8 erratum.
11473 template<bool big_endian
>
11475 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
11476 Arm_relobj
<big_endian
>* arm_relobj
,
11477 unsigned int shndx
,
11478 section_size_type span_start
,
11479 section_size_type span_end
,
11480 const unsigned char* view
,
11481 Arm_address address
)
11483 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
11485 // The opcode is BLX.W, BL.W, B.W, Bcc.W
11486 // The branch target is in the same 4KB region as the
11487 // first half of the branch.
11488 // The instruction before the branch is a 32-bit
11489 // length non-branch instruction.
11490 section_size_type i
= span_start
;
11491 bool last_was_32bit
= false;
11492 bool last_was_branch
= false;
11493 while (i
< span_end
)
11495 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
11496 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
11497 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
11498 bool is_blx
= false, is_b
= false;
11499 bool is_bl
= false, is_bcc
= false;
11501 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
11504 // Load the rest of the insn (in manual-friendly order).
11505 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
11507 // Encoding T4: B<c>.W.
11508 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
11509 // Encoding T1: BL<c>.W.
11510 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
11511 // Encoding T2: BLX<c>.W.
11512 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
11513 // Encoding T3: B<c>.W (not permitted in IT block).
11514 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
11515 && (insn
& 0x07f00000U
) != 0x03800000U
);
11518 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
11520 // If this instruction is a 32-bit THUMB branch that crosses a 4K
11521 // page boundary and it follows 32-bit non-branch instruction,
11522 // we need to work around.
11523 if (is_32bit_branch
11524 && ((address
+ i
) & 0xfffU
) == 0xffeU
11526 && !last_was_branch
)
11528 // Check to see if there is a relocation stub for this branch.
11529 bool force_target_arm
= false;
11530 bool force_target_thumb
= false;
11531 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
11532 Cortex_a8_relocs_info::const_iterator p
=
11533 this->cortex_a8_relocs_info_
.find(address
+ i
);
11535 if (p
!= this->cortex_a8_relocs_info_
.end())
11537 cortex_a8_reloc
= p
->second
;
11538 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
11540 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
11541 && !target_is_thumb
)
11542 force_target_arm
= true;
11543 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
11544 && target_is_thumb
)
11545 force_target_thumb
= true;
11549 Stub_type stub_type
= arm_stub_none
;
11551 // Check if we have an offending branch instruction.
11552 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
11553 uint16_t lower_insn
= insn
& 0xffffU
;
11554 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
11556 if (cortex_a8_reloc
!= NULL
11557 && cortex_a8_reloc
->reloc_stub() != NULL
)
11558 // We've already made a stub for this instruction, e.g.
11559 // it's a long branch or a Thumb->ARM stub. Assume that
11560 // stub will suffice to work around the A8 erratum (see
11561 // setting of always_after_branch above).
11565 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
11567 stub_type
= arm_stub_a8_veneer_b_cond
;
11569 else if (is_b
|| is_bl
|| is_blx
)
11571 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
11576 stub_type
= (is_blx
11577 ? arm_stub_a8_veneer_blx
11579 ? arm_stub_a8_veneer_bl
11580 : arm_stub_a8_veneer_b
));
11583 if (stub_type
!= arm_stub_none
)
11585 Arm_address pc_for_insn
= address
+ i
+ 4;
11587 // The original instruction is a BL, but the target is
11588 // an ARM instruction. If we were not making a stub,
11589 // the BL would have been converted to a BLX. Use the
11590 // BLX stub instead in that case.
11591 if (this->may_use_blx() && force_target_arm
11592 && stub_type
== arm_stub_a8_veneer_bl
)
11594 stub_type
= arm_stub_a8_veneer_blx
;
11598 // Conversely, if the original instruction was
11599 // BLX but the target is Thumb mode, use the BL stub.
11600 else if (force_target_thumb
11601 && stub_type
== arm_stub_a8_veneer_blx
)
11603 stub_type
= arm_stub_a8_veneer_bl
;
11611 // If we found a relocation, use the proper destination,
11612 // not the offset in the (unrelocated) instruction.
11613 // Note this is always done if we switched the stub type above.
11614 if (cortex_a8_reloc
!= NULL
)
11615 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
11617 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
11619 // Add a new stub if destination address in in the same page.
11620 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
11622 Cortex_a8_stub
* stub
=
11623 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
11627 Stub_table
<big_endian
>* stub_table
=
11628 arm_relobj
->stub_table(shndx
);
11629 gold_assert(stub_table
!= NULL
);
11630 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
11635 i
+= insn_32bit
? 4 : 2;
11636 last_was_32bit
= insn_32bit
;
11637 last_was_branch
= is_32bit_branch
;
11641 // Apply the Cortex-A8 workaround.
11643 template<bool big_endian
>
11645 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
11646 const Cortex_a8_stub
* stub
,
11647 Arm_address stub_address
,
11648 unsigned char* insn_view
,
11649 Arm_address insn_address
)
11651 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
11652 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
11653 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
11654 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
11655 off_t branch_offset
= stub_address
- (insn_address
+ 4);
11657 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
11658 switch (stub
->stub_template()->type())
11660 case arm_stub_a8_veneer_b_cond
:
11661 // For a conditional branch, we re-write it to be a uncondition
11662 // branch to the stub. We use the THUMB-2 encoding here.
11663 upper_insn
= 0xf000U
;
11664 lower_insn
= 0xb800U
;
11666 case arm_stub_a8_veneer_b
:
11667 case arm_stub_a8_veneer_bl
:
11668 case arm_stub_a8_veneer_blx
:
11669 if ((lower_insn
& 0x5000U
) == 0x4000U
)
11670 // For a BLX instruction, make sure that the relocation is
11671 // rounded up to a word boundary. This follows the semantics of
11672 // the instruction which specifies that bit 1 of the target
11673 // address will come from bit 1 of the base address.
11674 branch_offset
= (branch_offset
+ 2) & ~3;
11676 // Put BRANCH_OFFSET back into the insn.
11677 gold_assert(!utils::has_overflow
<25>(branch_offset
));
11678 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
11679 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
11683 gold_unreachable();
11686 // Put the relocated value back in the object file:
11687 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
11688 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
11691 template<bool big_endian
>
11692 class Target_selector_arm
: public Target_selector
11695 Target_selector_arm()
11696 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
11697 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
11701 do_instantiate_target()
11702 { return new Target_arm
<big_endian
>(); }
11705 // Fix .ARM.exidx section coverage.
11707 template<bool big_endian
>
11709 Target_arm
<big_endian
>::fix_exidx_coverage(
11711 const Input_objects
* input_objects
,
11712 Arm_output_section
<big_endian
>* exidx_section
,
11713 Symbol_table
* symtab
)
11715 // We need to look at all the input sections in output in ascending
11716 // order of of output address. We do that by building a sorted list
11717 // of output sections by addresses. Then we looks at the output sections
11718 // in order. The input sections in an output section are already sorted
11719 // by addresses within the output section.
11721 typedef std::set
<Output_section
*, output_section_address_less_than
>
11722 Sorted_output_section_list
;
11723 Sorted_output_section_list sorted_output_sections
;
11725 // Find out all the output sections of input sections pointed by
11726 // EXIDX input sections.
11727 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
11728 p
!= input_objects
->relobj_end();
11731 Arm_relobj
<big_endian
>* arm_relobj
=
11732 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
11733 std::vector
<unsigned int> shndx_list
;
11734 arm_relobj
->get_exidx_shndx_list(&shndx_list
);
11735 for (size_t i
= 0; i
< shndx_list
.size(); ++i
)
11737 const Arm_exidx_input_section
* exidx_input_section
=
11738 arm_relobj
->exidx_input_section_by_shndx(shndx_list
[i
]);
11739 gold_assert(exidx_input_section
!= NULL
);
11740 if (!exidx_input_section
->has_errors())
11742 unsigned int text_shndx
= exidx_input_section
->link();
11743 Output_section
* os
= arm_relobj
->output_section(text_shndx
);
11744 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
11745 sorted_output_sections
.insert(os
);
11750 // Go over the output sections in ascending order of output addresses.
11751 typedef typename Arm_output_section
<big_endian
>::Text_section_list
11753 Text_section_list sorted_text_sections
;
11754 for(typename
Sorted_output_section_list::iterator p
=
11755 sorted_output_sections
.begin();
11756 p
!= sorted_output_sections
.end();
11759 Arm_output_section
<big_endian
>* arm_output_section
=
11760 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
11761 arm_output_section
->append_text_sections_to_list(&sorted_text_sections
);
11764 exidx_section
->fix_exidx_coverage(layout
, sorted_text_sections
, symtab
,
11765 merge_exidx_entries());
11768 Target_selector_arm
<false> target_selector_arm
;
11769 Target_selector_arm
<true> target_selector_armbe
;
11771 } // End anonymous namespace.