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
);
1068 // Implement do_write for a given endianness.
1069 template<bool big_endian
>
1071 do_fixed_endian_write(Output_file
*);
1073 // The object containing the section pointed by this.
1075 // The section index of the section pointed by this.
1076 unsigned int shndx_
;
1079 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1080 // Offset map is used to map input section offset within the EXIDX section
1081 // to the output offset from the start of this EXIDX section.
1083 typedef std::map
<section_offset_type
, section_offset_type
>
1084 Arm_exidx_section_offset_map
;
1086 // Arm_exidx_merged_section class. This represents an EXIDX input section
1087 // with some of its entries merged.
1089 class Arm_exidx_merged_section
: public Output_relaxed_input_section
1092 // Constructor for Arm_exidx_merged_section.
1093 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1094 // SECTION_OFFSET_MAP points to a section offset map describing how
1095 // parts of the input section are mapped to output. DELETED_BYTES is
1096 // the number of bytes deleted from the EXIDX input section.
1097 Arm_exidx_merged_section(
1098 const Arm_exidx_input_section
& exidx_input_section
,
1099 const Arm_exidx_section_offset_map
& section_offset_map
,
1100 uint32_t deleted_bytes
);
1102 // Return the original EXIDX input section.
1103 const Arm_exidx_input_section
&
1104 exidx_input_section() const
1105 { return this->exidx_input_section_
; }
1107 // Return the section offset map.
1108 const Arm_exidx_section_offset_map
&
1109 section_offset_map() const
1110 { return this->section_offset_map_
; }
1113 // Write merged section into file OF.
1115 do_write(Output_file
* of
);
1118 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
1119 section_offset_type
*) const;
1122 // Original EXIDX input section.
1123 const Arm_exidx_input_section
& exidx_input_section_
;
1124 // Section offset map.
1125 const Arm_exidx_section_offset_map
& section_offset_map_
;
1128 // A class to wrap an ordinary input section containing executable code.
1130 template<bool big_endian
>
1131 class Arm_input_section
: public Output_relaxed_input_section
1134 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
1135 : Output_relaxed_input_section(relobj
, shndx
, 1),
1136 original_addralign_(1), original_size_(0), stub_table_(NULL
)
1139 ~Arm_input_section()
1146 // Whether this is a stub table owner.
1148 is_stub_table_owner() const
1149 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
1151 // Return the stub table.
1152 Stub_table
<big_endian
>*
1154 { return this->stub_table_
; }
1156 // Set the stub_table.
1158 set_stub_table(Stub_table
<big_endian
>* stub_table
)
1159 { this->stub_table_
= stub_table
; }
1161 // Downcast a base pointer to an Arm_input_section pointer. This is
1162 // not type-safe but we only use Arm_input_section not the base class.
1163 static Arm_input_section
<big_endian
>*
1164 as_arm_input_section(Output_relaxed_input_section
* poris
)
1165 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
1167 // Return the original size of the section.
1169 original_size() const
1170 { return this->original_size_
; }
1173 // Write data to output file.
1175 do_write(Output_file
*);
1177 // Return required alignment of this.
1179 do_addralign() const
1181 if (this->is_stub_table_owner())
1182 return std::max(this->stub_table_
->addralign(),
1183 static_cast<uint64_t>(this->original_addralign_
));
1185 return this->original_addralign_
;
1188 // Finalize data size.
1190 set_final_data_size();
1192 // Reset address and file offset.
1194 do_reset_address_and_file_offset();
1198 do_output_offset(const Relobj
* object
, unsigned int shndx
,
1199 section_offset_type offset
,
1200 section_offset_type
* poutput
) const
1202 if ((object
== this->relobj())
1203 && (shndx
== this->shndx())
1205 && (offset
<= this->original_size_
))
1215 // Copying is not allowed.
1216 Arm_input_section(const Arm_input_section
&);
1217 Arm_input_section
& operator=(const Arm_input_section
&);
1219 // Address alignment of the original input section.
1220 uint32_t original_addralign_
;
1221 // Section size of the original input section.
1222 uint32_t original_size_
;
1224 Stub_table
<big_endian
>* stub_table_
;
1227 // Arm_exidx_fixup class. This is used to define a number of methods
1228 // and keep states for fixing up EXIDX coverage.
1230 class Arm_exidx_fixup
1233 Arm_exidx_fixup(Output_section
* exidx_output_section
,
1234 bool merge_exidx_entries
= true)
1235 : exidx_output_section_(exidx_output_section
), last_unwind_type_(UT_NONE
),
1236 last_inlined_entry_(0), last_input_section_(NULL
),
1237 section_offset_map_(NULL
), first_output_text_section_(NULL
),
1238 merge_exidx_entries_(merge_exidx_entries
)
1242 { delete this->section_offset_map_
; }
1244 // Process an EXIDX section for entry merging. Return number of bytes to
1245 // be deleted in output. If parts of the input EXIDX section are merged
1246 // a heap allocated Arm_exidx_section_offset_map is store in the located
1247 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1249 template<bool big_endian
>
1251 process_exidx_section(const Arm_exidx_input_section
* exidx_input_section
,
1252 Arm_exidx_section_offset_map
** psection_offset_map
);
1254 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1255 // input section, if there is not one already.
1257 add_exidx_cantunwind_as_needed();
1259 // Return the output section for the text section which is linked to the
1260 // first exidx input in output.
1262 first_output_text_section() const
1263 { return this->first_output_text_section_
; }
1266 // Copying is not allowed.
1267 Arm_exidx_fixup(const Arm_exidx_fixup
&);
1268 Arm_exidx_fixup
& operator=(const Arm_exidx_fixup
&);
1270 // Type of EXIDX unwind entry.
1275 // EXIDX_CANTUNWIND.
1276 UT_EXIDX_CANTUNWIND
,
1283 // Process an EXIDX entry. We only care about the second word of the
1284 // entry. Return true if the entry can be deleted.
1286 process_exidx_entry(uint32_t second_word
);
1288 // Update the current section offset map during EXIDX section fix-up.
1289 // If there is no map, create one. INPUT_OFFSET is the offset of a
1290 // reference point, DELETED_BYTES is the number of deleted by in the
1291 // section so far. If DELETE_ENTRY is true, the reference point and
1292 // all offsets after the previous reference point are discarded.
1294 update_offset_map(section_offset_type input_offset
,
1295 section_size_type deleted_bytes
, bool delete_entry
);
1297 // EXIDX output section.
1298 Output_section
* exidx_output_section_
;
1299 // Unwind type of the last EXIDX entry processed.
1300 Unwind_type last_unwind_type_
;
1301 // Last seen inlined EXIDX entry.
1302 uint32_t last_inlined_entry_
;
1303 // Last processed EXIDX input section.
1304 const Arm_exidx_input_section
* last_input_section_
;
1305 // Section offset map created in process_exidx_section.
1306 Arm_exidx_section_offset_map
* section_offset_map_
;
1307 // Output section for the text section which is linked to the first exidx
1309 Output_section
* first_output_text_section_
;
1311 bool merge_exidx_entries_
;
1314 // Arm output section class. This is defined mainly to add a number of
1315 // stub generation methods.
1317 template<bool big_endian
>
1318 class Arm_output_section
: public Output_section
1321 typedef std::vector
<std::pair
<Relobj
*, unsigned int> > Text_section_list
;
1323 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
1324 elfcpp::Elf_Xword flags
)
1325 : Output_section(name
, type
, flags
)
1328 ~Arm_output_section()
1331 // Group input sections for stub generation.
1333 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
1335 // Downcast a base pointer to an Arm_output_section pointer. This is
1336 // not type-safe but we only use Arm_output_section not the base class.
1337 static Arm_output_section
<big_endian
>*
1338 as_arm_output_section(Output_section
* os
)
1339 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
1341 // Append all input text sections in this into LIST.
1343 append_text_sections_to_list(Text_section_list
* list
);
1345 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1346 // is a list of text input sections sorted in ascending order of their
1347 // output addresses.
1349 fix_exidx_coverage(Layout
* layout
,
1350 const Text_section_list
& sorted_text_section
,
1351 Symbol_table
* symtab
,
1352 bool merge_exidx_entries
);
1356 typedef Output_section::Input_section Input_section
;
1357 typedef Output_section::Input_section_list Input_section_list
;
1359 // Create a stub group.
1360 void create_stub_group(Input_section_list::const_iterator
,
1361 Input_section_list::const_iterator
,
1362 Input_section_list::const_iterator
,
1363 Target_arm
<big_endian
>*,
1364 std::vector
<Output_relaxed_input_section
*>*);
1367 // Arm_exidx_input_section class. This represents an EXIDX input section.
1369 class Arm_exidx_input_section
1372 static const section_offset_type invalid_offset
=
1373 static_cast<section_offset_type
>(-1);
1375 Arm_exidx_input_section(Relobj
* relobj
, unsigned int shndx
,
1376 unsigned int link
, uint32_t size
, uint32_t addralign
)
1377 : relobj_(relobj
), shndx_(shndx
), link_(link
), size_(size
),
1378 addralign_(addralign
)
1381 ~Arm_exidx_input_section()
1384 // Accessors: This is a read-only class.
1386 // Return the object containing this EXIDX input section.
1389 { return this->relobj_
; }
1391 // Return the section index of this EXIDX input section.
1394 { return this->shndx_
; }
1396 // Return the section index of linked text section in the same object.
1399 { return this->link_
; }
1401 // Return size of the EXIDX input section.
1404 { return this->size_
; }
1406 // Reutnr address alignment of EXIDX input section.
1409 { return this->addralign_
; }
1412 // Object containing this.
1414 // Section index of this.
1415 unsigned int shndx_
;
1416 // text section linked to this in the same object.
1418 // Size of this. For ARM 32-bit is sufficient.
1420 // Address alignment of this. For ARM 32-bit is sufficient.
1421 uint32_t addralign_
;
1424 // Arm_relobj class.
1426 template<bool big_endian
>
1427 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
1430 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
1432 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1433 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1434 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1435 stub_tables_(), local_symbol_is_thumb_function_(),
1436 attributes_section_data_(NULL
), mapping_symbols_info_(),
1437 section_has_cortex_a8_workaround_(NULL
), exidx_section_map_(),
1438 output_local_symbol_count_needs_update_(false),
1439 merge_flags_and_attributes_(true)
1443 { delete this->attributes_section_data_
; }
1445 // Return the stub table of the SHNDX-th section if there is one.
1446 Stub_table
<big_endian
>*
1447 stub_table(unsigned int shndx
) const
1449 gold_assert(shndx
< this->stub_tables_
.size());
1450 return this->stub_tables_
[shndx
];
1453 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1455 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
1457 gold_assert(shndx
< this->stub_tables_
.size());
1458 this->stub_tables_
[shndx
] = stub_table
;
1461 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1462 // index. This is only valid after do_count_local_symbol is called.
1464 local_symbol_is_thumb_function(unsigned int r_sym
) const
1466 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
1467 return this->local_symbol_is_thumb_function_
[r_sym
];
1470 // Scan all relocation sections for stub generation.
1472 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
1475 // Convert regular input section with index SHNDX to a relaxed section.
1477 convert_input_section_to_relaxed_section(unsigned shndx
)
1479 // The stubs have relocations and we need to process them after writing
1480 // out the stubs. So relocation now must follow section write.
1481 this->set_section_offset(shndx
, -1ULL);
1482 this->set_relocs_must_follow_section_writes();
1485 // Downcast a base pointer to an Arm_relobj pointer. This is
1486 // not type-safe but we only use Arm_relobj not the base class.
1487 static Arm_relobj
<big_endian
>*
1488 as_arm_relobj(Relobj
* relobj
)
1489 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
1491 // Processor-specific flags in ELF file header. This is valid only after
1494 processor_specific_flags() const
1495 { return this->processor_specific_flags_
; }
1497 // Attribute section data This is the contents of the .ARM.attribute section
1499 const Attributes_section_data
*
1500 attributes_section_data() const
1501 { return this->attributes_section_data_
; }
1503 // Mapping symbol location.
1504 typedef std::pair
<unsigned int, Arm_address
> Mapping_symbol_position
;
1506 // Functor for STL container.
1507 struct Mapping_symbol_position_less
1510 operator()(const Mapping_symbol_position
& p1
,
1511 const Mapping_symbol_position
& p2
) const
1513 return (p1
.first
< p2
.first
1514 || (p1
.first
== p2
.first
&& p1
.second
< p2
.second
));
1518 // We only care about the first character of a mapping symbol, so
1519 // we only store that instead of the whole symbol name.
1520 typedef std::map
<Mapping_symbol_position
, char,
1521 Mapping_symbol_position_less
> Mapping_symbols_info
;
1523 // Whether a section contains any Cortex-A8 workaround.
1525 section_has_cortex_a8_workaround(unsigned int shndx
) const
1527 return (this->section_has_cortex_a8_workaround_
!= NULL
1528 && (*this->section_has_cortex_a8_workaround_
)[shndx
]);
1531 // Mark a section that has Cortex-A8 workaround.
1533 mark_section_for_cortex_a8_workaround(unsigned int shndx
)
1535 if (this->section_has_cortex_a8_workaround_
== NULL
)
1536 this->section_has_cortex_a8_workaround_
=
1537 new std::vector
<bool>(this->shnum(), false);
1538 (*this->section_has_cortex_a8_workaround_
)[shndx
] = true;
1541 // Return the EXIDX section of an text section with index SHNDX or NULL
1542 // if the text section has no associated EXIDX section.
1543 const Arm_exidx_input_section
*
1544 exidx_input_section_by_link(unsigned int shndx
) const
1546 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1547 return ((p
!= this->exidx_section_map_
.end()
1548 && p
->second
->link() == shndx
)
1553 // Return the EXIDX section with index SHNDX or NULL if there is none.
1554 const Arm_exidx_input_section
*
1555 exidx_input_section_by_shndx(unsigned shndx
) const
1557 Exidx_section_map::const_iterator p
= this->exidx_section_map_
.find(shndx
);
1558 return ((p
!= this->exidx_section_map_
.end()
1559 && p
->second
->shndx() == shndx
)
1564 // Whether output local symbol count needs updating.
1566 output_local_symbol_count_needs_update() const
1567 { return this->output_local_symbol_count_needs_update_
; }
1569 // Set output_local_symbol_count_needs_update flag to be true.
1571 set_output_local_symbol_count_needs_update()
1572 { this->output_local_symbol_count_needs_update_
= true; }
1574 // Update output local symbol count at the end of relaxation.
1576 update_output_local_symbol_count();
1578 // Whether we want to merge processor-specific flags and attributes.
1580 merge_flags_and_attributes() const
1581 { return this->merge_flags_and_attributes_
; }
1584 // Post constructor setup.
1588 // Call parent's setup method.
1589 Sized_relobj
<32, big_endian
>::do_setup();
1591 // Initialize look-up tables.
1592 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
1593 this->stub_tables_
.swap(empty_stub_table_list
);
1596 // Count the local symbols.
1598 do_count_local_symbols(Stringpool_template
<char>*,
1599 Stringpool_template
<char>*);
1602 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
1603 const unsigned char* pshdrs
,
1604 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
1606 // Read the symbol information.
1608 do_read_symbols(Read_symbols_data
* sd
);
1610 // Process relocs for garbage collection.
1612 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
1616 // Whether a section needs to be scanned for relocation stubs.
1618 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1619 const Relobj::Output_sections
&,
1620 const Symbol_table
*, const unsigned char*);
1622 // Whether a section is a scannable text section.
1624 section_is_scannable(const elfcpp::Shdr
<32, big_endian
>&, unsigned int,
1625 const Output_section
*, const Symbol_table
*);
1627 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1629 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr
<32, big_endian
>&,
1630 unsigned int, Output_section
*,
1631 const Symbol_table
*);
1633 // Scan a section for the Cortex-A8 erratum.
1635 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr
<32, big_endian
>&,
1636 unsigned int, Output_section
*,
1637 Target_arm
<big_endian
>*);
1639 // Find the linked text section of an EXIDX section by looking at the
1640 // first reloction of the EXIDX section. PSHDR points to the section
1641 // headers of a relocation section and PSYMS points to the local symbols.
1642 // PSHNDX points to a location storing the text section index if found.
1643 // Return whether we can find the linked section.
1645 find_linked_text_section(const unsigned char* pshdr
,
1646 const unsigned char* psyms
, unsigned int* pshndx
);
1649 // Make a new Arm_exidx_input_section object for EXIDX section with
1650 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1651 // index of the linked text section.
1653 make_exidx_input_section(unsigned int shndx
,
1654 const elfcpp::Shdr
<32, big_endian
>& shdr
,
1655 unsigned int text_shndx
);
1657 // Return the output address of either a plain input section or a
1658 // relaxed input section. SHNDX is the section index.
1660 simple_input_section_output_address(unsigned int, Output_section
*);
1662 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1663 typedef Unordered_map
<unsigned int, const Arm_exidx_input_section
*>
1666 // List of stub tables.
1667 Stub_table_list stub_tables_
;
1668 // Bit vector to tell if a local symbol is a thumb function or not.
1669 // This is only valid after do_count_local_symbol is called.
1670 std::vector
<bool> local_symbol_is_thumb_function_
;
1671 // processor-specific flags in ELF file header.
1672 elfcpp::Elf_Word processor_specific_flags_
;
1673 // Object attributes if there is an .ARM.attributes section or NULL.
1674 Attributes_section_data
* attributes_section_data_
;
1675 // Mapping symbols information.
1676 Mapping_symbols_info mapping_symbols_info_
;
1677 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1678 std::vector
<bool>* section_has_cortex_a8_workaround_
;
1679 // Map a text section to its associated .ARM.exidx section, if there is one.
1680 Exidx_section_map exidx_section_map_
;
1681 // Whether output local symbol count needs updating.
1682 bool output_local_symbol_count_needs_update_
;
1683 // Whether we merge processor flags and attributes of this object to
1685 bool merge_flags_and_attributes_
;
1688 // Arm_dynobj class.
1690 template<bool big_endian
>
1691 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1694 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1695 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1696 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1697 processor_specific_flags_(0), attributes_section_data_(NULL
)
1701 { delete this->attributes_section_data_
; }
1703 // Downcast a base pointer to an Arm_relobj pointer. This is
1704 // not type-safe but we only use Arm_relobj not the base class.
1705 static Arm_dynobj
<big_endian
>*
1706 as_arm_dynobj(Dynobj
* dynobj
)
1707 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1709 // Processor-specific flags in ELF file header. This is valid only after
1712 processor_specific_flags() const
1713 { return this->processor_specific_flags_
; }
1715 // Attributes section data.
1716 const Attributes_section_data
*
1717 attributes_section_data() const
1718 { return this->attributes_section_data_
; }
1721 // Read the symbol information.
1723 do_read_symbols(Read_symbols_data
* sd
);
1726 // processor-specific flags in ELF file header.
1727 elfcpp::Elf_Word processor_specific_flags_
;
1728 // Object attributes if there is an .ARM.attributes section or NULL.
1729 Attributes_section_data
* attributes_section_data_
;
1732 // Functor to read reloc addends during stub generation.
1734 template<int sh_type
, bool big_endian
>
1735 struct Stub_addend_reader
1737 // Return the addend for a relocation of a particular type. Depending
1738 // on whether this is a REL or RELA relocation, read the addend from a
1739 // view or from a Reloc object.
1740 elfcpp::Elf_types
<32>::Elf_Swxword
1742 unsigned int /* r_type */,
1743 const unsigned char* /* view */,
1744 const typename Reloc_types
<sh_type
,
1745 32, big_endian
>::Reloc
& /* reloc */) const;
1748 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1750 template<bool big_endian
>
1751 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1753 elfcpp::Elf_types
<32>::Elf_Swxword
1756 const unsigned char*,
1757 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1760 // Specialized Stub_addend_reader for RELA type relocation sections.
1761 // We currently do not handle RELA type relocation sections but it is trivial
1762 // to implement the addend reader. This is provided for completeness and to
1763 // make it easier to add support for RELA relocation sections in the future.
1765 template<bool big_endian
>
1766 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1768 elfcpp::Elf_types
<32>::Elf_Swxword
1771 const unsigned char*,
1772 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1773 big_endian
>::Reloc
& reloc
) const
1774 { return reloc
.get_r_addend(); }
1777 // Cortex_a8_reloc class. We keep record of relocation that may need
1778 // the Cortex-A8 erratum workaround.
1780 class Cortex_a8_reloc
1783 Cortex_a8_reloc(Reloc_stub
* reloc_stub
, unsigned r_type
,
1784 Arm_address destination
)
1785 : reloc_stub_(reloc_stub
), r_type_(r_type
), destination_(destination
)
1791 // Accessors: This is a read-only class.
1793 // Return the relocation stub associated with this relocation if there is
1797 { return this->reloc_stub_
; }
1799 // Return the relocation type.
1802 { return this->r_type_
; }
1804 // Return the destination address of the relocation. LSB stores the THUMB
1808 { return this->destination_
; }
1811 // Associated relocation stub if there is one, or NULL.
1812 const Reloc_stub
* reloc_stub_
;
1814 unsigned int r_type_
;
1815 // Destination address of this relocation. LSB is used to distinguish
1817 Arm_address destination_
;
1820 // Arm_output_data_got class. We derive this from Output_data_got to add
1821 // extra methods to handle TLS relocations in a static link.
1823 template<bool big_endian
>
1824 class Arm_output_data_got
: public Output_data_got
<32, big_endian
>
1827 Arm_output_data_got(Symbol_table
* symtab
, Layout
* layout
)
1828 : Output_data_got
<32, big_endian
>(), symbol_table_(symtab
), layout_(layout
)
1831 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1832 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1833 // applied in a static link.
1835 add_static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
1836 { this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, gsym
)); }
1838 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1839 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1840 // relocation that needs to be applied in a static link.
1842 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
1843 Sized_relobj
<32, big_endian
>* relobj
, unsigned int index
)
1845 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
1849 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1850 // The first one is initialized to be 1, which is the module index for
1851 // the main executable and the second one 0. A reloc of the type
1852 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1853 // be applied by gold. GSYM is a global symbol.
1855 add_tls_gd32_with_static_reloc(unsigned int got_type
, Symbol
* gsym
);
1857 // Same as the above but for a local symbol in OBJECT with INDEX.
1859 add_tls_gd32_with_static_reloc(unsigned int got_type
,
1860 Sized_relobj
<32, big_endian
>* object
,
1861 unsigned int index
);
1864 // Write out the GOT table.
1866 do_write(Output_file
*);
1869 // This class represent dynamic relocations that need to be applied by
1870 // gold because we are using TLS relocations in a static link.
1874 Static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
1875 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(true)
1876 { this->u_
.global
.symbol
= gsym
; }
1878 Static_reloc(unsigned int got_offset
, unsigned int r_type
,
1879 Sized_relobj
<32, big_endian
>* relobj
, unsigned int index
)
1880 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(false)
1882 this->u_
.local
.relobj
= relobj
;
1883 this->u_
.local
.index
= index
;
1886 // Return the GOT offset.
1889 { return this->got_offset_
; }
1894 { return this->r_type_
; }
1896 // Whether the symbol is global or not.
1898 symbol_is_global() const
1899 { return this->symbol_is_global_
; }
1901 // For a relocation against a global symbol, the global symbol.
1905 gold_assert(this->symbol_is_global_
);
1906 return this->u_
.global
.symbol
;
1909 // For a relocation against a local symbol, the defining object.
1910 Sized_relobj
<32, big_endian
>*
1913 gold_assert(!this->symbol_is_global_
);
1914 return this->u_
.local
.relobj
;
1917 // For a relocation against a local symbol, the local symbol index.
1921 gold_assert(!this->symbol_is_global_
);
1922 return this->u_
.local
.index
;
1926 // GOT offset of the entry to which this relocation is applied.
1927 unsigned int got_offset_
;
1928 // Type of relocation.
1929 unsigned int r_type_
;
1930 // Whether this relocation is against a global symbol.
1931 bool symbol_is_global_
;
1932 // A global or local symbol.
1937 // For a global symbol, the symbol itself.
1942 // For a local symbol, the object defining object.
1943 Sized_relobj
<32, big_endian
>* relobj
;
1944 // For a local symbol, the symbol index.
1950 // Symbol table of the output object.
1951 Symbol_table
* symbol_table_
;
1952 // Layout of the output object.
1954 // Static relocs to be applied to the GOT.
1955 std::vector
<Static_reloc
> static_relocs_
;
1958 // Utilities for manipulating integers of up to 32-bits
1962 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1963 // an int32_t. NO_BITS must be between 1 to 32.
1964 template<int no_bits
>
1965 static inline int32_t
1966 sign_extend(uint32_t bits
)
1968 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1970 return static_cast<int32_t>(bits
);
1971 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1973 uint32_t top_bit
= 1U << (no_bits
- 1);
1974 int32_t as_signed
= static_cast<int32_t>(bits
);
1975 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1978 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1979 template<int no_bits
>
1981 has_overflow(uint32_t bits
)
1983 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1986 int32_t max
= (1 << (no_bits
- 1)) - 1;
1987 int32_t min
= -(1 << (no_bits
- 1));
1988 int32_t as_signed
= static_cast<int32_t>(bits
);
1989 return as_signed
> max
|| as_signed
< min
;
1992 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1993 // fits in the given number of bits as either a signed or unsigned value.
1994 // For example, has_signed_unsigned_overflow<8> would check
1995 // -128 <= bits <= 255
1996 template<int no_bits
>
1998 has_signed_unsigned_overflow(uint32_t bits
)
2000 gold_assert(no_bits
>= 2 && no_bits
<= 32);
2003 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
2004 int32_t min
= -(1 << (no_bits
- 1));
2005 int32_t as_signed
= static_cast<int32_t>(bits
);
2006 return as_signed
> max
|| as_signed
< min
;
2009 // Select bits from A and B using bits in MASK. For each n in [0..31],
2010 // the n-th bit in the result is chosen from the n-th bits of A and B.
2011 // A zero selects A and a one selects B.
2012 static inline uint32_t
2013 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
2014 { return (a
& ~mask
) | (b
& mask
); }
2017 template<bool big_endian
>
2018 class Target_arm
: public Sized_target
<32, big_endian
>
2021 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
2024 // When were are relocating a stub, we pass this as the relocation number.
2025 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
2028 : Sized_target
<32, big_endian
>(&arm_info
),
2029 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
2030 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
),
2031 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2032 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2033 may_use_blx_(false), should_force_pic_veneer_(false),
2034 arm_input_section_map_(), attributes_section_data_(NULL
),
2035 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2038 // Whether we can use BLX.
2041 { return this->may_use_blx_
; }
2043 // Set use-BLX flag.
2045 set_may_use_blx(bool value
)
2046 { this->may_use_blx_
= value
; }
2048 // Whether we force PCI branch veneers.
2050 should_force_pic_veneer() const
2051 { return this->should_force_pic_veneer_
; }
2053 // Set PIC veneer flag.
2055 set_should_force_pic_veneer(bool value
)
2056 { this->should_force_pic_veneer_
= value
; }
2058 // Whether we use THUMB-2 instructions.
2060 using_thumb2() const
2062 Object_attribute
* attr
=
2063 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2064 int arch
= attr
->int_value();
2065 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
2068 // Whether we use THUMB/THUMB-2 instructions only.
2070 using_thumb_only() const
2072 Object_attribute
* attr
=
2073 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2075 if (attr
->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2076 || attr
->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M
)
2078 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
2079 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
2081 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
2082 return attr
->int_value() == 'M';
2085 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2087 may_use_arm_nop() const
2089 Object_attribute
* attr
=
2090 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2091 int arch
= attr
->int_value();
2092 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
2093 || arch
== elfcpp::TAG_CPU_ARCH_V6K
2094 || arch
== elfcpp::TAG_CPU_ARCH_V7
2095 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
2098 // Whether we have THUMB-2 NOP.W instruction.
2100 may_use_thumb2_nop() const
2102 Object_attribute
* attr
=
2103 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
2104 int arch
= attr
->int_value();
2105 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
2106 || arch
== elfcpp::TAG_CPU_ARCH_V7
2107 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
2110 // Process the relocations to determine unreferenced sections for
2111 // garbage collection.
2113 gc_process_relocs(Symbol_table
* symtab
,
2115 Sized_relobj
<32, big_endian
>* object
,
2116 unsigned int data_shndx
,
2117 unsigned int sh_type
,
2118 const unsigned char* prelocs
,
2120 Output_section
* output_section
,
2121 bool needs_special_offset_handling
,
2122 size_t local_symbol_count
,
2123 const unsigned char* plocal_symbols
);
2125 // Scan the relocations to look for symbol adjustments.
2127 scan_relocs(Symbol_table
* symtab
,
2129 Sized_relobj
<32, big_endian
>* object
,
2130 unsigned int data_shndx
,
2131 unsigned int sh_type
,
2132 const unsigned char* prelocs
,
2134 Output_section
* output_section
,
2135 bool needs_special_offset_handling
,
2136 size_t local_symbol_count
,
2137 const unsigned char* plocal_symbols
);
2139 // Finalize the sections.
2141 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
2143 // Return the value to use for a dynamic symbol which requires special
2146 do_dynsym_value(const Symbol
*) const;
2148 // Relocate a section.
2150 relocate_section(const Relocate_info
<32, big_endian
>*,
2151 unsigned int sh_type
,
2152 const unsigned char* prelocs
,
2154 Output_section
* output_section
,
2155 bool needs_special_offset_handling
,
2156 unsigned char* view
,
2157 Arm_address view_address
,
2158 section_size_type view_size
,
2159 const Reloc_symbol_changes
*);
2161 // Scan the relocs during a relocatable link.
2163 scan_relocatable_relocs(Symbol_table
* symtab
,
2165 Sized_relobj
<32, big_endian
>* object
,
2166 unsigned int data_shndx
,
2167 unsigned int sh_type
,
2168 const unsigned char* prelocs
,
2170 Output_section
* output_section
,
2171 bool needs_special_offset_handling
,
2172 size_t local_symbol_count
,
2173 const unsigned char* plocal_symbols
,
2174 Relocatable_relocs
*);
2176 // Relocate a section during a relocatable link.
2178 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
2179 unsigned int sh_type
,
2180 const unsigned char* prelocs
,
2182 Output_section
* output_section
,
2183 off_t offset_in_output_section
,
2184 const Relocatable_relocs
*,
2185 unsigned char* view
,
2186 Arm_address view_address
,
2187 section_size_type view_size
,
2188 unsigned char* reloc_view
,
2189 section_size_type reloc_view_size
);
2191 // Return whether SYM is defined by the ABI.
2193 do_is_defined_by_abi(Symbol
* sym
) const
2194 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
2196 // Return whether there is a GOT section.
2198 has_got_section() const
2199 { return this->got_
!= NULL
; }
2201 // Return the size of the GOT section.
2205 gold_assert(this->got_
!= NULL
);
2206 return this->got_
->data_size();
2209 // Map platform-specific reloc types
2211 get_real_reloc_type (unsigned int r_type
);
2214 // Methods to support stub-generations.
2217 // Return the stub factory
2219 stub_factory() const
2220 { return this->stub_factory_
; }
2222 // Make a new Arm_input_section object.
2223 Arm_input_section
<big_endian
>*
2224 new_arm_input_section(Relobj
*, unsigned int);
2226 // Find the Arm_input_section object corresponding to the SHNDX-th input
2227 // section of RELOBJ.
2228 Arm_input_section
<big_endian
>*
2229 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
2231 // Make a new Stub_table
2232 Stub_table
<big_endian
>*
2233 new_stub_table(Arm_input_section
<big_endian
>*);
2235 // Scan a section for stub generation.
2237 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
2238 const unsigned char*, size_t, Output_section
*,
2239 bool, const unsigned char*, Arm_address
,
2244 relocate_stub(Stub
*, const Relocate_info
<32, big_endian
>*,
2245 Output_section
*, unsigned char*, Arm_address
,
2248 // Get the default ARM target.
2249 static Target_arm
<big_endian
>*
2252 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
2253 && parameters
->target().is_big_endian() == big_endian
);
2254 return static_cast<Target_arm
<big_endian
>*>(
2255 parameters
->sized_target
<32, big_endian
>());
2258 // Whether NAME belongs to a mapping symbol.
2260 is_mapping_symbol_name(const char* name
)
2264 && (name
[1] == 'a' || name
[1] == 't' || name
[1] == 'd')
2265 && (name
[2] == '\0' || name
[2] == '.'));
2268 // Whether we work around the Cortex-A8 erratum.
2270 fix_cortex_a8() const
2271 { return this->fix_cortex_a8_
; }
2273 // Whether we merge exidx entries in debuginfo.
2275 merge_exidx_entries() const
2276 { return parameters
->options().merge_exidx_entries(); }
2278 // Whether we fix R_ARM_V4BX relocation.
2280 // 1 - replace with MOV instruction (armv4 target)
2281 // 2 - make interworking veneer (>= armv4t targets only)
2282 General_options::Fix_v4bx
2284 { return parameters
->options().fix_v4bx(); }
2286 // Scan a span of THUMB code section for Cortex-A8 erratum.
2288 scan_span_for_cortex_a8_erratum(Arm_relobj
<big_endian
>*, unsigned int,
2289 section_size_type
, section_size_type
,
2290 const unsigned char*, Arm_address
);
2292 // Apply Cortex-A8 workaround to a branch.
2294 apply_cortex_a8_workaround(const Cortex_a8_stub
*, Arm_address
,
2295 unsigned char*, Arm_address
);
2298 // Make an ELF object.
2300 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2301 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
2304 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2305 const elfcpp::Ehdr
<32, !big_endian
>&)
2306 { gold_unreachable(); }
2309 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2310 const elfcpp::Ehdr
<64, false>&)
2311 { gold_unreachable(); }
2314 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2315 const elfcpp::Ehdr
<64, true>&)
2316 { gold_unreachable(); }
2318 // Make an output section.
2320 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
2321 elfcpp::Elf_Xword flags
)
2322 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
2325 do_adjust_elf_header(unsigned char* view
, int len
) const;
2327 // We only need to generate stubs, and hence perform relaxation if we are
2328 // not doing relocatable linking.
2330 do_may_relax() const
2331 { return !parameters
->options().relocatable(); }
2334 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
2336 // Determine whether an object attribute tag takes an integer, a
2339 do_attribute_arg_type(int tag
) const;
2341 // Reorder tags during output.
2343 do_attributes_order(int num
) const;
2345 // This is called when the target is selected as the default.
2347 do_select_as_default_target()
2349 // No locking is required since there should only be one default target.
2350 // We cannot have both the big-endian and little-endian ARM targets
2352 gold_assert(arm_reloc_property_table
== NULL
);
2353 arm_reloc_property_table
= new Arm_reloc_property_table();
2357 // The class which scans relocations.
2362 : issued_non_pic_error_(false)
2366 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2367 Sized_relobj
<32, big_endian
>* object
,
2368 unsigned int data_shndx
,
2369 Output_section
* output_section
,
2370 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2371 const elfcpp::Sym
<32, big_endian
>& lsym
);
2374 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
2375 Sized_relobj
<32, big_endian
>* object
,
2376 unsigned int data_shndx
,
2377 Output_section
* output_section
,
2378 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
2382 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* , Target_arm
* ,
2383 Sized_relobj
<32, big_endian
>* ,
2386 const elfcpp::Rel
<32, big_endian
>& ,
2388 const elfcpp::Sym
<32, big_endian
>&)
2392 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* , Target_arm
* ,
2393 Sized_relobj
<32, big_endian
>* ,
2396 const elfcpp::Rel
<32, big_endian
>& ,
2397 unsigned int , Symbol
*)
2402 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
2403 unsigned int r_type
);
2406 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
2407 unsigned int r_type
, Symbol
*);
2410 check_non_pic(Relobj
*, unsigned int r_type
);
2412 // Almost identical to Symbol::needs_plt_entry except that it also
2413 // handles STT_ARM_TFUNC.
2415 symbol_needs_plt_entry(const Symbol
* sym
)
2417 // An undefined symbol from an executable does not need a PLT entry.
2418 if (sym
->is_undefined() && !parameters
->options().shared())
2421 return (!parameters
->doing_static_link()
2422 && (sym
->type() == elfcpp::STT_FUNC
2423 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
2424 && (sym
->is_from_dynobj()
2425 || sym
->is_undefined()
2426 || sym
->is_preemptible()));
2429 // Whether we have issued an error about a non-PIC compilation.
2430 bool issued_non_pic_error_
;
2433 // The class which implements relocation.
2443 // Return whether the static relocation needs to be applied.
2445 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2448 Output_section
* output_section
);
2450 // Do a relocation. Return false if the caller should not issue
2451 // any warnings about this relocation.
2453 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
2454 Output_section
*, size_t relnum
,
2455 const elfcpp::Rel
<32, big_endian
>&,
2456 unsigned int r_type
, const Sized_symbol
<32>*,
2457 const Symbol_value
<32>*,
2458 unsigned char*, Arm_address
,
2461 // Return whether we want to pass flag NON_PIC_REF for this
2462 // reloc. This means the relocation type accesses a symbol not via
2465 reloc_is_non_pic (unsigned int r_type
)
2469 // These relocation types reference GOT or PLT entries explicitly.
2470 case elfcpp::R_ARM_GOT_BREL
:
2471 case elfcpp::R_ARM_GOT_ABS
:
2472 case elfcpp::R_ARM_GOT_PREL
:
2473 case elfcpp::R_ARM_GOT_BREL12
:
2474 case elfcpp::R_ARM_PLT32_ABS
:
2475 case elfcpp::R_ARM_TLS_GD32
:
2476 case elfcpp::R_ARM_TLS_LDM32
:
2477 case elfcpp::R_ARM_TLS_IE32
:
2478 case elfcpp::R_ARM_TLS_IE12GP
:
2480 // These relocate types may use PLT entries.
2481 case elfcpp::R_ARM_CALL
:
2482 case elfcpp::R_ARM_THM_CALL
:
2483 case elfcpp::R_ARM_JUMP24
:
2484 case elfcpp::R_ARM_THM_JUMP24
:
2485 case elfcpp::R_ARM_THM_JUMP19
:
2486 case elfcpp::R_ARM_PLT32
:
2487 case elfcpp::R_ARM_THM_XPC22
:
2488 case elfcpp::R_ARM_PREL31
:
2489 case elfcpp::R_ARM_SBREL31
:
2498 // Do a TLS relocation.
2499 inline typename Arm_relocate_functions
<big_endian
>::Status
2500 relocate_tls(const Relocate_info
<32, big_endian
>*, Target_arm
<big_endian
>*,
2501 size_t, const elfcpp::Rel
<32, big_endian
>&, unsigned int,
2502 const Sized_symbol
<32>*, const Symbol_value
<32>*,
2503 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
2508 // A class which returns the size required for a relocation type,
2509 // used while scanning relocs during a relocatable link.
2510 class Relocatable_size_for_reloc
2514 get_size_for_reloc(unsigned int, Relobj
*);
2517 // Adjust TLS relocation type based on the options and whether this
2518 // is a local symbol.
2519 static tls::Tls_optimization
2520 optimize_tls_reloc(bool is_final
, int r_type
);
2522 // Get the GOT section, creating it if necessary.
2523 Arm_output_data_got
<big_endian
>*
2524 got_section(Symbol_table
*, Layout
*);
2526 // Get the GOT PLT section.
2528 got_plt_section() const
2530 gold_assert(this->got_plt_
!= NULL
);
2531 return this->got_plt_
;
2534 // Create a PLT entry for a global symbol.
2536 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
2538 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2540 define_tls_base_symbol(Symbol_table
*, Layout
*);
2542 // Create a GOT entry for the TLS module index.
2544 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2545 Sized_relobj
<32, big_endian
>* object
);
2547 // Get the PLT section.
2548 const Output_data_plt_arm
<big_endian
>*
2551 gold_assert(this->plt_
!= NULL
);
2555 // Get the dynamic reloc section, creating it if necessary.
2557 rel_dyn_section(Layout
*);
2559 // Get the section to use for TLS_DESC relocations.
2561 rel_tls_desc_section(Layout
*) const;
2563 // Return true if the symbol may need a COPY relocation.
2564 // References from an executable object to non-function symbols
2565 // defined in a dynamic object may need a COPY relocation.
2567 may_need_copy_reloc(Symbol
* gsym
)
2569 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
2570 && gsym
->may_need_copy_reloc());
2573 // Add a potential copy relocation.
2575 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
2576 Sized_relobj
<32, big_endian
>* object
,
2577 unsigned int shndx
, Output_section
* output_section
,
2578 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
2580 this->copy_relocs_
.copy_reloc(symtab
, layout
,
2581 symtab
->get_sized_symbol
<32>(sym
),
2582 object
, shndx
, output_section
, reloc
,
2583 this->rel_dyn_section(layout
));
2586 // Whether two EABI versions are compatible.
2588 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
2590 // Merge processor-specific flags from input object and those in the ELF
2591 // header of the output.
2593 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
2595 // Get the secondary compatible architecture.
2597 get_secondary_compatible_arch(const Attributes_section_data
*);
2599 // Set the secondary compatible architecture.
2601 set_secondary_compatible_arch(Attributes_section_data
*, int);
2604 tag_cpu_arch_combine(const char*, int, int*, int, int);
2606 // Helper to print AEABI enum tag value.
2608 aeabi_enum_name(unsigned int);
2610 // Return string value for TAG_CPU_name.
2612 tag_cpu_name_value(unsigned int);
2614 // Merge object attributes from input object and those in the output.
2616 merge_object_attributes(const char*, const Attributes_section_data
*);
2618 // Helper to get an AEABI object attribute
2620 get_aeabi_object_attribute(int tag
) const
2622 Attributes_section_data
* pasd
= this->attributes_section_data_
;
2623 gold_assert(pasd
!= NULL
);
2624 Object_attribute
* attr
=
2625 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
2626 gold_assert(attr
!= NULL
);
2631 // Methods to support stub-generations.
2634 // Group input sections for stub generation.
2636 group_sections(Layout
*, section_size_type
, bool);
2638 // Scan a relocation for stub generation.
2640 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
2641 const Sized_symbol
<32>*, unsigned int,
2642 const Symbol_value
<32>*,
2643 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
2645 // Scan a relocation section for stub.
2646 template<int sh_type
>
2648 scan_reloc_section_for_stubs(
2649 const Relocate_info
<32, big_endian
>* relinfo
,
2650 const unsigned char* prelocs
,
2652 Output_section
* output_section
,
2653 bool needs_special_offset_handling
,
2654 const unsigned char* view
,
2655 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2658 // Fix .ARM.exidx section coverage.
2660 fix_exidx_coverage(Layout
*, Arm_output_section
<big_endian
>*, Symbol_table
*);
2662 // Functors for STL set.
2663 struct output_section_address_less_than
2666 operator()(const Output_section
* s1
, const Output_section
* s2
) const
2667 { return s1
->address() < s2
->address(); }
2670 // Information about this specific target which we pass to the
2671 // general Target structure.
2672 static const Target::Target_info arm_info
;
2674 // The types of GOT entries needed for this platform.
2677 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
2678 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
2679 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
2680 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
2681 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
2684 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
2686 // Map input section to Arm_input_section.
2687 typedef Unordered_map
<Section_id
,
2688 Arm_input_section
<big_endian
>*,
2690 Arm_input_section_map
;
2692 // Map output addresses to relocs for Cortex-A8 erratum.
2693 typedef Unordered_map
<Arm_address
, const Cortex_a8_reloc
*>
2694 Cortex_a8_relocs_info
;
2697 Arm_output_data_got
<big_endian
>* got_
;
2699 Output_data_plt_arm
<big_endian
>* plt_
;
2700 // The GOT PLT section.
2701 Output_data_space
* got_plt_
;
2702 // The dynamic reloc section.
2703 Reloc_section
* rel_dyn_
;
2704 // Relocs saved to avoid a COPY reloc.
2705 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
2706 // Space for variables copied with a COPY reloc.
2707 Output_data_space
* dynbss_
;
2708 // Offset of the GOT entry for the TLS module index.
2709 unsigned int got_mod_index_offset_
;
2710 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2711 bool tls_base_symbol_defined_
;
2712 // Vector of Stub_tables created.
2713 Stub_table_list stub_tables_
;
2715 const Stub_factory
&stub_factory_
;
2716 // Whether we can use BLX.
2718 // Whether we force PIC branch veneers.
2719 bool should_force_pic_veneer_
;
2720 // Map for locating Arm_input_sections.
2721 Arm_input_section_map arm_input_section_map_
;
2722 // Attributes section data in output.
2723 Attributes_section_data
* attributes_section_data_
;
2724 // Whether we want to fix code for Cortex-A8 erratum.
2725 bool fix_cortex_a8_
;
2726 // Map addresses to relocs for Cortex-A8 erratum.
2727 Cortex_a8_relocs_info cortex_a8_relocs_info_
;
2730 template<bool big_endian
>
2731 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
2734 big_endian
, // is_big_endian
2735 elfcpp::EM_ARM
, // machine_code
2736 false, // has_make_symbol
2737 false, // has_resolve
2738 false, // has_code_fill
2739 true, // is_default_stack_executable
2741 "/usr/lib/libc.so.1", // dynamic_linker
2742 0x8000, // default_text_segment_address
2743 0x1000, // abi_pagesize (overridable by -z max-page-size)
2744 0x1000, // common_pagesize (overridable by -z common-page-size)
2745 elfcpp::SHN_UNDEF
, // small_common_shndx
2746 elfcpp::SHN_UNDEF
, // large_common_shndx
2747 0, // small_common_section_flags
2748 0, // large_common_section_flags
2749 ".ARM.attributes", // attributes_section
2750 "aeabi" // attributes_vendor
2753 // Arm relocate functions class
2756 template<bool big_endian
>
2757 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
2762 STATUS_OKAY
, // No error during relocation.
2763 STATUS_OVERFLOW
, // Relocation oveflow.
2764 STATUS_BAD_RELOC
// Relocation cannot be applied.
2768 typedef Relocate_functions
<32, big_endian
> Base
;
2769 typedef Arm_relocate_functions
<big_endian
> This
;
2771 // Encoding of imm16 argument for movt and movw ARM instructions
2774 // imm16 := imm4 | imm12
2776 // 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
2777 // +-------+---------------+-------+-------+-----------------------+
2778 // | | |imm4 | |imm12 |
2779 // +-------+---------------+-------+-------+-----------------------+
2781 // Extract the relocation addend from VAL based on the ARM
2782 // instruction encoding described above.
2783 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2784 extract_arm_movw_movt_addend(
2785 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2787 // According to the Elf ABI for ARM Architecture the immediate
2788 // field is sign-extended to form the addend.
2789 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
2792 // Insert X into VAL based on the ARM instruction encoding described
2794 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2795 insert_val_arm_movw_movt(
2796 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2797 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2801 val
|= (x
& 0xf000) << 4;
2805 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2808 // imm16 := imm4 | i | imm3 | imm8
2810 // 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
2811 // +---------+-+-----------+-------++-+-----+-------+---------------+
2812 // | |i| |imm4 || |imm3 | |imm8 |
2813 // +---------+-+-----------+-------++-+-----+-------+---------------+
2815 // Extract the relocation addend from VAL based on the Thumb2
2816 // instruction encoding described above.
2817 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2818 extract_thumb_movw_movt_addend(
2819 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
2821 // According to the Elf ABI for ARM Architecture the immediate
2822 // field is sign-extended to form the addend.
2823 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
2824 | ((val
>> 15) & 0x0800)
2825 | ((val
>> 4) & 0x0700)
2829 // Insert X into VAL based on the Thumb2 instruction encoding
2831 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
2832 insert_val_thumb_movw_movt(
2833 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
2834 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
2837 val
|= (x
& 0xf000) << 4;
2838 val
|= (x
& 0x0800) << 15;
2839 val
|= (x
& 0x0700) << 4;
2840 val
|= (x
& 0x00ff);
2844 // Calculate the smallest constant Kn for the specified residual.
2845 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2847 calc_grp_kn(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
)
2853 // Determine the most significant bit in the residual and
2854 // align the resulting value to a 2-bit boundary.
2855 for (msb
= 30; (msb
>= 0) && !(residual
& (3 << msb
)); msb
-= 2)
2857 // The desired shift is now (msb - 6), or zero, whichever
2859 return (((msb
- 6) < 0) ? 0 : (msb
- 6));
2862 // Calculate the final residual for the specified group index.
2863 // If the passed group index is less than zero, the method will return
2864 // the value of the specified residual without any change.
2865 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2866 static typename
elfcpp::Swap
<32, big_endian
>::Valtype
2867 calc_grp_residual(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
,
2870 for (int n
= 0; n
<= group
; n
++)
2872 // Calculate which part of the value to mask.
2873 uint32_t shift
= calc_grp_kn(residual
);
2874 // Calculate the residual for the next time around.
2875 residual
&= ~(residual
& (0xff << shift
));
2881 // Calculate the value of Gn for the specified group index.
2882 // We return it in the form of an encoded constant-and-rotation.
2883 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2884 static typename
elfcpp::Swap
<32, big_endian
>::Valtype
2885 calc_grp_gn(typename
elfcpp::Swap
<32, big_endian
>::Valtype residual
,
2888 typename
elfcpp::Swap
<32, big_endian
>::Valtype gn
= 0;
2891 for (int n
= 0; n
<= group
; n
++)
2893 // Calculate which part of the value to mask.
2894 shift
= calc_grp_kn(residual
);
2895 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2896 gn
= residual
& (0xff << shift
);
2897 // Calculate the residual for the next time around.
2900 // Return Gn in the form of an encoded constant-and-rotation.
2901 return ((gn
>> shift
) | ((gn
<= 0xff ? 0 : (32 - shift
) / 2) << 8));
2905 // Handle ARM long branches.
2906 static typename
This::Status
2907 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2908 unsigned char *, const Sized_symbol
<32>*,
2909 const Arm_relobj
<big_endian
>*, unsigned int,
2910 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2912 // Handle THUMB long branches.
2913 static typename
This::Status
2914 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
2915 unsigned char *, const Sized_symbol
<32>*,
2916 const Arm_relobj
<big_endian
>*, unsigned int,
2917 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
2920 // Return the branch offset of a 32-bit THUMB branch.
2921 static inline int32_t
2922 thumb32_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2924 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2925 // involving the J1 and J2 bits.
2926 uint32_t s
= (upper_insn
& (1U << 10)) >> 10;
2927 uint32_t upper
= upper_insn
& 0x3ffU
;
2928 uint32_t lower
= lower_insn
& 0x7ffU
;
2929 uint32_t j1
= (lower_insn
& (1U << 13)) >> 13;
2930 uint32_t j2
= (lower_insn
& (1U << 11)) >> 11;
2931 uint32_t i1
= j1
^ s
? 0 : 1;
2932 uint32_t i2
= j2
^ s
? 0 : 1;
2934 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
2935 | (upper
<< 12) | (lower
<< 1));
2938 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2939 // UPPER_INSN is the original upper instruction of the branch. Caller is
2940 // responsible for overflow checking and BLX offset adjustment.
2941 static inline uint16_t
2942 thumb32_branch_upper(uint16_t upper_insn
, int32_t offset
)
2944 uint32_t s
= offset
< 0 ? 1 : 0;
2945 uint32_t bits
= static_cast<uint32_t>(offset
);
2946 return (upper_insn
& ~0x7ffU
) | ((bits
>> 12) & 0x3ffU
) | (s
<< 10);
2949 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2950 // LOWER_INSN is the original lower instruction of the branch. Caller is
2951 // responsible for overflow checking and BLX offset adjustment.
2952 static inline uint16_t
2953 thumb32_branch_lower(uint16_t lower_insn
, int32_t offset
)
2955 uint32_t s
= offset
< 0 ? 1 : 0;
2956 uint32_t bits
= static_cast<uint32_t>(offset
);
2957 return ((lower_insn
& ~0x2fffU
)
2958 | ((((bits
>> 23) & 1) ^ !s
) << 13)
2959 | ((((bits
>> 22) & 1) ^ !s
) << 11)
2960 | ((bits
>> 1) & 0x7ffU
));
2963 // Return the branch offset of a 32-bit THUMB conditional branch.
2964 static inline int32_t
2965 thumb32_cond_branch_offset(uint16_t upper_insn
, uint16_t lower_insn
)
2967 uint32_t s
= (upper_insn
& 0x0400U
) >> 10;
2968 uint32_t j1
= (lower_insn
& 0x2000U
) >> 13;
2969 uint32_t j2
= (lower_insn
& 0x0800U
) >> 11;
2970 uint32_t lower
= (lower_insn
& 0x07ffU
);
2971 uint32_t upper
= (s
<< 8) | (j2
<< 7) | (j1
<< 6) | (upper_insn
& 0x003fU
);
2973 return utils::sign_extend
<21>((upper
<< 12) | (lower
<< 1));
2976 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2977 // instruction. UPPER_INSN is the original upper instruction of the branch.
2978 // Caller is responsible for overflow checking.
2979 static inline uint16_t
2980 thumb32_cond_branch_upper(uint16_t upper_insn
, int32_t offset
)
2982 uint32_t s
= offset
< 0 ? 1 : 0;
2983 uint32_t bits
= static_cast<uint32_t>(offset
);
2984 return (upper_insn
& 0xfbc0U
) | (s
<< 10) | ((bits
& 0x0003f000U
) >> 12);
2987 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2988 // instruction. LOWER_INSN is the original lower instruction of the branch.
2989 // Caller is reponsible for overflow checking.
2990 static inline uint16_t
2991 thumb32_cond_branch_lower(uint16_t lower_insn
, int32_t offset
)
2993 uint32_t bits
= static_cast<uint32_t>(offset
);
2994 uint32_t j2
= (bits
& 0x00080000U
) >> 19;
2995 uint32_t j1
= (bits
& 0x00040000U
) >> 18;
2996 uint32_t lo
= (bits
& 0x00000ffeU
) >> 1;
2998 return (lower_insn
& 0xd000U
) | (j1
<< 13) | (j2
<< 11) | lo
;
3001 // R_ARM_ABS8: S + A
3002 static inline typename
This::Status
3003 abs8(unsigned char *view
,
3004 const Sized_relobj
<32, big_endian
>* object
,
3005 const Symbol_value
<32>* psymval
)
3007 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
3008 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3009 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3010 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
3011 Reltype addend
= utils::sign_extend
<8>(val
);
3012 Reltype x
= psymval
->value(object
, addend
);
3013 val
= utils::bit_select(val
, x
, 0xffU
);
3014 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
3016 // R_ARM_ABS8 permits signed or unsigned results.
3017 int signed_x
= static_cast<int32_t>(x
);
3018 return ((signed_x
< -128 || signed_x
> 255)
3019 ? This::STATUS_OVERFLOW
3020 : This::STATUS_OKAY
);
3023 // R_ARM_THM_ABS5: S + A
3024 static inline typename
This::Status
3025 thm_abs5(unsigned char *view
,
3026 const Sized_relobj
<32, big_endian
>* object
,
3027 const Symbol_value
<32>* psymval
)
3029 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3030 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3031 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3032 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3033 Reltype addend
= (val
& 0x7e0U
) >> 6;
3034 Reltype x
= psymval
->value(object
, addend
);
3035 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
3036 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3038 // R_ARM_ABS16 permits signed or unsigned results.
3039 int signed_x
= static_cast<int32_t>(x
);
3040 return ((signed_x
< -32768 || signed_x
> 65535)
3041 ? This::STATUS_OVERFLOW
3042 : This::STATUS_OKAY
);
3045 // R_ARM_ABS12: S + A
3046 static inline typename
This::Status
3047 abs12(unsigned char *view
,
3048 const Sized_relobj
<32, big_endian
>* object
,
3049 const Symbol_value
<32>* psymval
)
3051 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3052 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3053 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3054 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3055 Reltype addend
= val
& 0x0fffU
;
3056 Reltype x
= psymval
->value(object
, addend
);
3057 val
= utils::bit_select(val
, x
, 0x0fffU
);
3058 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3059 return (utils::has_overflow
<12>(x
)
3060 ? This::STATUS_OVERFLOW
3061 : This::STATUS_OKAY
);
3064 // R_ARM_ABS16: S + A
3065 static inline typename
This::Status
3066 abs16(unsigned char *view
,
3067 const Sized_relobj
<32, big_endian
>* object
,
3068 const Symbol_value
<32>* psymval
)
3070 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3071 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3072 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3073 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3074 Reltype addend
= utils::sign_extend
<16>(val
);
3075 Reltype x
= psymval
->value(object
, addend
);
3076 val
= utils::bit_select(val
, x
, 0xffffU
);
3077 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3078 return (utils::has_signed_unsigned_overflow
<16>(x
)
3079 ? This::STATUS_OVERFLOW
3080 : This::STATUS_OKAY
);
3083 // R_ARM_ABS32: (S + A) | T
3084 static inline typename
This::Status
3085 abs32(unsigned char *view
,
3086 const Sized_relobj
<32, big_endian
>* object
,
3087 const Symbol_value
<32>* psymval
,
3088 Arm_address thumb_bit
)
3090 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3091 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3092 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3093 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
3094 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
3095 return This::STATUS_OKAY
;
3098 // R_ARM_REL32: (S + A) | T - P
3099 static inline typename
This::Status
3100 rel32(unsigned char *view
,
3101 const Sized_relobj
<32, big_endian
>* object
,
3102 const Symbol_value
<32>* psymval
,
3103 Arm_address address
,
3104 Arm_address thumb_bit
)
3106 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3107 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3108 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3109 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3110 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
3111 return This::STATUS_OKAY
;
3114 // R_ARM_THM_JUMP24: (S + A) | T - P
3115 static typename
This::Status
3116 thm_jump19(unsigned char *view
, const Arm_relobj
<big_endian
>* object
,
3117 const Symbol_value
<32>* psymval
, Arm_address address
,
3118 Arm_address thumb_bit
);
3120 // R_ARM_THM_JUMP6: S + A – P
3121 static inline typename
This::Status
3122 thm_jump6(unsigned char *view
,
3123 const Sized_relobj
<32, big_endian
>* object
,
3124 const Symbol_value
<32>* psymval
,
3125 Arm_address address
)
3127 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3128 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3129 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3130 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3131 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3132 Reltype addend
= (((val
& 0x0200) >> 3) | ((val
& 0x00f8) >> 2));
3133 Reltype x
= (psymval
->value(object
, addend
) - address
);
3134 val
= (val
& 0xfd07) | ((x
& 0x0040) << 3) | ((val
& 0x003e) << 2);
3135 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
3136 // CZB does only forward jumps.
3137 return ((x
> 0x007e)
3138 ? This::STATUS_OVERFLOW
3139 : This::STATUS_OKAY
);
3142 // R_ARM_THM_JUMP8: S + A – P
3143 static inline typename
This::Status
3144 thm_jump8(unsigned char *view
,
3145 const Sized_relobj
<32, big_endian
>* object
,
3146 const Symbol_value
<32>* psymval
,
3147 Arm_address address
)
3149 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3150 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3151 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3152 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3153 Reltype addend
= utils::sign_extend
<8>((val
& 0x00ff) << 1);
3154 Reltype x
= (psymval
->value(object
, addend
) - address
);
3155 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xff00) | ((x
& 0x01fe) >> 1));
3156 return (utils::has_overflow
<8>(x
)
3157 ? This::STATUS_OVERFLOW
3158 : This::STATUS_OKAY
);
3161 // R_ARM_THM_JUMP11: S + A – P
3162 static inline typename
This::Status
3163 thm_jump11(unsigned char *view
,
3164 const Sized_relobj
<32, big_endian
>* object
,
3165 const Symbol_value
<32>* psymval
,
3166 Arm_address address
)
3168 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3169 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3170 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3171 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3172 Reltype addend
= utils::sign_extend
<11>((val
& 0x07ff) << 1);
3173 Reltype x
= (psymval
->value(object
, addend
) - address
);
3174 elfcpp::Swap
<16, big_endian
>::writeval(wv
, (val
& 0xf800) | ((x
& 0x0ffe) >> 1));
3175 return (utils::has_overflow
<11>(x
)
3176 ? This::STATUS_OVERFLOW
3177 : This::STATUS_OKAY
);
3180 // R_ARM_BASE_PREL: B(S) + A - P
3181 static inline typename
This::Status
3182 base_prel(unsigned char* view
,
3184 Arm_address address
)
3186 Base::rel32(view
, origin
- address
);
3190 // R_ARM_BASE_ABS: B(S) + A
3191 static inline typename
This::Status
3192 base_abs(unsigned char* view
,
3195 Base::rel32(view
, origin
);
3199 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3200 static inline typename
This::Status
3201 got_brel(unsigned char* view
,
3202 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
3204 Base::rel32(view
, got_offset
);
3205 return This::STATUS_OKAY
;
3208 // R_ARM_GOT_PREL: GOT(S) + A - P
3209 static inline typename
This::Status
3210 got_prel(unsigned char *view
,
3211 Arm_address got_entry
,
3212 Arm_address address
)
3214 Base::rel32(view
, got_entry
- address
);
3215 return This::STATUS_OKAY
;
3218 // R_ARM_PREL: (S + A) | T - P
3219 static inline typename
This::Status
3220 prel31(unsigned char *view
,
3221 const Sized_relobj
<32, big_endian
>* object
,
3222 const Symbol_value
<32>* psymval
,
3223 Arm_address address
,
3224 Arm_address thumb_bit
)
3226 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3227 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3228 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3229 Valtype addend
= utils::sign_extend
<31>(val
);
3230 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
3231 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
3232 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3233 return (utils::has_overflow
<31>(x
) ?
3234 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3237 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3238 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3239 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3240 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3241 static inline typename
This::Status
3242 movw(unsigned char* view
,
3243 const Sized_relobj
<32, big_endian
>* object
,
3244 const Symbol_value
<32>* psymval
,
3245 Arm_address relative_address_base
,
3246 Arm_address thumb_bit
,
3247 bool check_overflow
)
3249 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3250 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3251 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3252 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3253 Valtype x
= ((psymval
->value(object
, addend
) | thumb_bit
)
3254 - relative_address_base
);
3255 val
= This::insert_val_arm_movw_movt(val
, x
);
3256 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3257 return ((check_overflow
&& utils::has_overflow
<16>(x
))
3258 ? This::STATUS_OVERFLOW
3259 : This::STATUS_OKAY
);
3262 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3263 // R_ARM_MOVT_PREL: S + A - P
3264 // R_ARM_MOVT_BREL: S + A - B(S)
3265 static inline typename
This::Status
3266 movt(unsigned char* view
,
3267 const Sized_relobj
<32, big_endian
>* object
,
3268 const Symbol_value
<32>* psymval
,
3269 Arm_address relative_address_base
)
3271 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3272 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3273 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3274 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
3275 Valtype x
= (psymval
->value(object
, addend
) - relative_address_base
) >> 16;
3276 val
= This::insert_val_arm_movw_movt(val
, x
);
3277 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3278 // FIXME: IHI0044D says that we should check for overflow.
3279 return This::STATUS_OKAY
;
3282 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3283 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3284 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3285 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3286 static inline typename
This::Status
3287 thm_movw(unsigned char *view
,
3288 const Sized_relobj
<32, big_endian
>* object
,
3289 const Symbol_value
<32>* psymval
,
3290 Arm_address relative_address_base
,
3291 Arm_address thumb_bit
,
3292 bool check_overflow
)
3294 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3295 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3296 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3297 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3298 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3299 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3301 (psymval
->value(object
, addend
) | thumb_bit
) - relative_address_base
;
3302 val
= This::insert_val_thumb_movw_movt(val
, x
);
3303 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3304 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3305 return ((check_overflow
&& utils::has_overflow
<16>(x
))
3306 ? This::STATUS_OVERFLOW
3307 : This::STATUS_OKAY
);
3310 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3311 // R_ARM_THM_MOVT_PREL: S + A - P
3312 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3313 static inline typename
This::Status
3314 thm_movt(unsigned char* view
,
3315 const Sized_relobj
<32, big_endian
>* object
,
3316 const Symbol_value
<32>* psymval
,
3317 Arm_address relative_address_base
)
3319 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3320 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3321 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3322 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3323 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3324 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
3325 Reltype x
= (psymval
->value(object
, addend
) - relative_address_base
) >> 16;
3326 val
= This::insert_val_thumb_movw_movt(val
, x
);
3327 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
3328 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
3329 return This::STATUS_OKAY
;
3332 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3333 static inline typename
This::Status
3334 thm_alu11(unsigned char* view
,
3335 const Sized_relobj
<32, big_endian
>* object
,
3336 const Symbol_value
<32>* psymval
,
3337 Arm_address address
,
3338 Arm_address thumb_bit
)
3340 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3341 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3342 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3343 Reltype insn
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3344 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3346 // 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
3347 // -----------------------------------------------------------------------
3348 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3349 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3350 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3351 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3352 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3353 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3355 // Determine a sign for the addend.
3356 const int sign
= ((insn
& 0xf8ef0000) == 0xf0ad0000
3357 || (insn
& 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3358 // Thumb2 addend encoding:
3359 // imm12 := i | imm3 | imm8
3360 int32_t addend
= (insn
& 0xff)
3361 | ((insn
& 0x00007000) >> 4)
3362 | ((insn
& 0x04000000) >> 15);
3363 // Apply a sign to the added.
3366 int32_t x
= (psymval
->value(object
, addend
) | thumb_bit
)
3367 - (address
& 0xfffffffc);
3368 Reltype val
= abs(x
);
3369 // Mask out the value and a distinct part of the ADD/SUB opcode
3370 // (bits 7:5 of opword).
3371 insn
= (insn
& 0xfb0f8f00)
3373 | ((val
& 0x700) << 4)
3374 | ((val
& 0x800) << 15);
3375 // Set the opcode according to whether the value to go in the
3376 // place is negative.
3380 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
>> 16);
3381 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, insn
& 0xffff);
3382 return ((val
> 0xfff) ?
3383 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3386 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3387 static inline typename
This::Status
3388 thm_pc8(unsigned char* view
,
3389 const Sized_relobj
<32, big_endian
>* object
,
3390 const Symbol_value
<32>* psymval
,
3391 Arm_address address
)
3393 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3394 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Reltype
;
3395 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3396 Valtype insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3397 Reltype addend
= ((insn
& 0x00ff) << 2);
3398 int32_t x
= (psymval
->value(object
, addend
) - (address
& 0xfffffffc));
3399 Reltype val
= abs(x
);
3400 insn
= (insn
& 0xff00) | ((val
& 0x03fc) >> 2);
3402 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
);
3403 return ((val
> 0x03fc)
3404 ? This::STATUS_OVERFLOW
3405 : This::STATUS_OKAY
);
3408 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3409 static inline typename
This::Status
3410 thm_pc12(unsigned char* view
,
3411 const Sized_relobj
<32, big_endian
>* object
,
3412 const Symbol_value
<32>* psymval
,
3413 Arm_address address
)
3415 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3416 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
3417 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3418 Reltype insn
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
3419 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3420 // Determine a sign for the addend (positive if the U bit is 1).
3421 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3422 int32_t addend
= (insn
& 0xfff);
3423 // Apply a sign to the added.
3426 int32_t x
= (psymval
->value(object
, addend
) - (address
& 0xfffffffc));
3427 Reltype val
= abs(x
);
3428 // Mask out and apply the value and the U bit.
3429 insn
= (insn
& 0xff7ff000) | (val
& 0xfff);
3430 // Set the U bit according to whether the value to go in the
3431 // place is positive.
3435 elfcpp::Swap
<16, big_endian
>::writeval(wv
, insn
>> 16);
3436 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, insn
& 0xffff);
3437 return ((val
> 0xfff) ?
3438 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3442 static inline typename
This::Status
3443 v4bx(const Relocate_info
<32, big_endian
>* relinfo
,
3444 unsigned char *view
,
3445 const Arm_relobj
<big_endian
>* object
,
3446 const Arm_address address
,
3447 const bool is_interworking
)
3450 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3451 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3452 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3454 // Ensure that we have a BX instruction.
3455 gold_assert((val
& 0x0ffffff0) == 0x012fff10);
3456 const uint32_t reg
= (val
& 0xf);
3457 if (is_interworking
&& reg
!= 0xf)
3459 Stub_table
<big_endian
>* stub_table
=
3460 object
->stub_table(relinfo
->data_shndx
);
3461 gold_assert(stub_table
!= NULL
);
3463 Arm_v4bx_stub
* stub
= stub_table
->find_arm_v4bx_stub(reg
);
3464 gold_assert(stub
!= NULL
);
3466 int32_t veneer_address
=
3467 stub_table
->address() + stub
->offset() - 8 - address
;
3468 gold_assert((veneer_address
<= ARM_MAX_FWD_BRANCH_OFFSET
)
3469 && (veneer_address
>= ARM_MAX_BWD_BRANCH_OFFSET
));
3470 // Replace with a branch to veneer (B <addr>)
3471 val
= (val
& 0xf0000000) | 0x0a000000
3472 | ((veneer_address
>> 2) & 0x00ffffff);
3476 // Preserve Rm (lowest four bits) and the condition code
3477 // (highest four bits). Other bits encode MOV PC,Rm.
3478 val
= (val
& 0xf000000f) | 0x01a0f000;
3480 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3481 return This::STATUS_OKAY
;
3484 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3485 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3486 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3487 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3488 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3489 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3490 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3491 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3492 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3493 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3494 static inline typename
This::Status
3495 arm_grp_alu(unsigned char* view
,
3496 const Sized_relobj
<32, big_endian
>* object
,
3497 const Symbol_value
<32>* psymval
,
3499 Arm_address address
,
3500 Arm_address thumb_bit
,
3501 bool check_overflow
)
3503 gold_assert(group
>= 0 && group
< 3);
3504 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3505 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3506 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3508 // ALU group relocations are allowed only for the ADD/SUB instructions.
3509 // (0x00800000 - ADD, 0x00400000 - SUB)
3510 const Valtype opcode
= insn
& 0x01e00000;
3511 if (opcode
!= 0x00800000 && opcode
!= 0x00400000)
3512 return This::STATUS_BAD_RELOC
;
3514 // Determine a sign for the addend.
3515 const int sign
= (opcode
== 0x00800000) ? 1 : -1;
3516 // shifter = rotate_imm * 2
3517 const uint32_t shifter
= (insn
& 0xf00) >> 7;
3518 // Initial addend value.
3519 int32_t addend
= insn
& 0xff;
3520 // Rotate addend right by shifter.
3521 addend
= (addend
>> shifter
) | (addend
<< (32 - shifter
));
3522 // Apply a sign to the added.
3525 int32_t x
= ((psymval
->value(object
, addend
) | thumb_bit
) - address
);
3526 Valtype gn
= Arm_relocate_functions::calc_grp_gn(abs(x
), group
);
3527 // Check for overflow if required
3529 && (Arm_relocate_functions::calc_grp_residual(abs(x
), group
) != 0))
3530 return This::STATUS_OVERFLOW
;
3532 // Mask out the value and the ADD/SUB part of the opcode; take care
3533 // not to destroy the S bit.
3535 // Set the opcode according to whether the value to go in the
3536 // place is negative.
3537 insn
|= ((x
< 0) ? 0x00400000 : 0x00800000);
3538 // Encode the offset (encoded Gn).
3541 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3542 return This::STATUS_OKAY
;
3545 // R_ARM_LDR_PC_G0: S + A - P
3546 // R_ARM_LDR_PC_G1: S + A - P
3547 // R_ARM_LDR_PC_G2: S + A - P
3548 // R_ARM_LDR_SB_G0: S + A - B(S)
3549 // R_ARM_LDR_SB_G1: S + A - B(S)
3550 // R_ARM_LDR_SB_G2: S + A - B(S)
3551 static inline typename
This::Status
3552 arm_grp_ldr(unsigned char* view
,
3553 const Sized_relobj
<32, big_endian
>* object
,
3554 const Symbol_value
<32>* psymval
,
3556 Arm_address address
)
3558 gold_assert(group
>= 0 && group
< 3);
3559 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3560 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3561 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3563 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3564 int32_t addend
= (insn
& 0xfff) * sign
;
3565 int32_t x
= (psymval
->value(object
, addend
) - address
);
3566 // Calculate the relevant G(n-1) value to obtain this stage residual.
3568 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3569 if (residual
>= 0x1000)
3570 return This::STATUS_OVERFLOW
;
3572 // Mask out the value and U bit.
3574 // Set the U bit for non-negative values.
3579 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3580 return This::STATUS_OKAY
;
3583 // R_ARM_LDRS_PC_G0: S + A - P
3584 // R_ARM_LDRS_PC_G1: S + A - P
3585 // R_ARM_LDRS_PC_G2: S + A - P
3586 // R_ARM_LDRS_SB_G0: S + A - B(S)
3587 // R_ARM_LDRS_SB_G1: S + A - B(S)
3588 // R_ARM_LDRS_SB_G2: S + A - B(S)
3589 static inline typename
This::Status
3590 arm_grp_ldrs(unsigned char* view
,
3591 const Sized_relobj
<32, big_endian
>* object
,
3592 const Symbol_value
<32>* psymval
,
3594 Arm_address address
)
3596 gold_assert(group
>= 0 && group
< 3);
3597 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3598 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3599 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3601 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3602 int32_t addend
= (((insn
& 0xf00) >> 4) + (insn
& 0xf)) * sign
;
3603 int32_t x
= (psymval
->value(object
, addend
) - address
);
3604 // Calculate the relevant G(n-1) value to obtain this stage residual.
3606 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3607 if (residual
>= 0x100)
3608 return This::STATUS_OVERFLOW
;
3610 // Mask out the value and U bit.
3612 // Set the U bit for non-negative values.
3615 insn
|= ((residual
& 0xf0) << 4) | (residual
& 0xf);
3617 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3618 return This::STATUS_OKAY
;
3621 // R_ARM_LDC_PC_G0: S + A - P
3622 // R_ARM_LDC_PC_G1: S + A - P
3623 // R_ARM_LDC_PC_G2: S + A - P
3624 // R_ARM_LDC_SB_G0: S + A - B(S)
3625 // R_ARM_LDC_SB_G1: S + A - B(S)
3626 // R_ARM_LDC_SB_G2: S + A - B(S)
3627 static inline typename
This::Status
3628 arm_grp_ldc(unsigned char* view
,
3629 const Sized_relobj
<32, big_endian
>* object
,
3630 const Symbol_value
<32>* psymval
,
3632 Arm_address address
)
3634 gold_assert(group
>= 0 && group
< 3);
3635 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3636 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3637 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3639 const int sign
= (insn
& 0x00800000) ? 1 : -1;
3640 int32_t addend
= ((insn
& 0xff) << 2) * sign
;
3641 int32_t x
= (psymval
->value(object
, addend
) - address
);
3642 // Calculate the relevant G(n-1) value to obtain this stage residual.
3644 Arm_relocate_functions::calc_grp_residual(abs(x
), group
- 1);
3645 if ((residual
& 0x3) != 0 || residual
>= 0x400)
3646 return This::STATUS_OVERFLOW
;
3648 // Mask out the value and U bit.
3650 // Set the U bit for non-negative values.
3653 insn
|= (residual
>> 2);
3655 elfcpp::Swap
<32, big_endian
>::writeval(wv
, insn
);
3656 return This::STATUS_OKAY
;
3660 // Relocate ARM long branches. This handles relocation types
3661 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3662 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3663 // undefined and we do not use PLT in this relocation. In such a case,
3664 // the branch is converted into an NOP.
3666 template<bool big_endian
>
3667 typename Arm_relocate_functions
<big_endian
>::Status
3668 Arm_relocate_functions
<big_endian
>::arm_branch_common(
3669 unsigned int r_type
,
3670 const Relocate_info
<32, big_endian
>* relinfo
,
3671 unsigned char *view
,
3672 const Sized_symbol
<32>* gsym
,
3673 const Arm_relobj
<big_endian
>* object
,
3675 const Symbol_value
<32>* psymval
,
3676 Arm_address address
,
3677 Arm_address thumb_bit
,
3678 bool is_weakly_undefined_without_plt
)
3680 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3681 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3682 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3684 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
3685 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
3686 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
3687 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
3688 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
3689 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
3690 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
3692 // Check that the instruction is valid.
3693 if (r_type
== elfcpp::R_ARM_CALL
)
3695 if (!insn_is_uncond_bl
&& !insn_is_blx
)
3696 return This::STATUS_BAD_RELOC
;
3698 else if (r_type
== elfcpp::R_ARM_JUMP24
)
3700 if (!insn_is_b
&& !insn_is_cond_bl
)
3701 return This::STATUS_BAD_RELOC
;
3703 else if (r_type
== elfcpp::R_ARM_PLT32
)
3705 if (!insn_is_any_branch
)
3706 return This::STATUS_BAD_RELOC
;
3708 else if (r_type
== elfcpp::R_ARM_XPC25
)
3710 // FIXME: AAELF document IH0044C does not say much about it other
3711 // than it being obsolete.
3712 if (!insn_is_any_branch
)
3713 return This::STATUS_BAD_RELOC
;
3718 // A branch to an undefined weak symbol is turned into a jump to
3719 // the next instruction unless a PLT entry will be created.
3720 // Do the same for local undefined symbols.
3721 // The jump to the next instruction is optimized as a NOP depending
3722 // on the architecture.
3723 const Target_arm
<big_endian
>* arm_target
=
3724 Target_arm
<big_endian
>::default_target();
3725 if (is_weakly_undefined_without_plt
)
3727 Valtype cond
= val
& 0xf0000000U
;
3728 if (arm_target
->may_use_arm_nop())
3729 val
= cond
| 0x0320f000;
3731 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3732 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3733 return This::STATUS_OKAY
;
3736 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
3737 Valtype branch_target
= psymval
->value(object
, addend
);
3738 int32_t branch_offset
= branch_target
- address
;
3740 // We need a stub if the branch offset is too large or if we need
3742 bool may_use_blx
= arm_target
->may_use_blx();
3743 Reloc_stub
* stub
= NULL
;
3744 if (utils::has_overflow
<26>(branch_offset
)
3745 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
3747 Valtype unadjusted_branch_target
= psymval
->value(object
, 0);
3749 Stub_type stub_type
=
3750 Reloc_stub::stub_type_for_reloc(r_type
, address
,
3751 unadjusted_branch_target
,
3753 if (stub_type
!= arm_stub_none
)
3755 Stub_table
<big_endian
>* stub_table
=
3756 object
->stub_table(relinfo
->data_shndx
);
3757 gold_assert(stub_table
!= NULL
);
3759 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3760 stub
= stub_table
->find_reloc_stub(stub_key
);
3761 gold_assert(stub
!= NULL
);
3762 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3763 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3764 branch_offset
= branch_target
- address
;
3765 gold_assert(!utils::has_overflow
<26>(branch_offset
));
3769 // At this point, if we still need to switch mode, the instruction
3770 // must either be a BLX or a BL that can be converted to a BLX.
3774 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
3775 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
3778 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
3779 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
3780 return (utils::has_overflow
<26>(branch_offset
)
3781 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
3784 // Relocate THUMB long branches. This handles relocation types
3785 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3786 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3787 // undefined and we do not use PLT in this relocation. In such a case,
3788 // the branch is converted into an NOP.
3790 template<bool big_endian
>
3791 typename Arm_relocate_functions
<big_endian
>::Status
3792 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
3793 unsigned int r_type
,
3794 const Relocate_info
<32, big_endian
>* relinfo
,
3795 unsigned char *view
,
3796 const Sized_symbol
<32>* gsym
,
3797 const Arm_relobj
<big_endian
>* object
,
3799 const Symbol_value
<32>* psymval
,
3800 Arm_address address
,
3801 Arm_address thumb_bit
,
3802 bool is_weakly_undefined_without_plt
)
3804 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3805 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3806 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3807 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3809 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3811 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
3812 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
3814 // Check that the instruction is valid.
3815 if (r_type
== elfcpp::R_ARM_THM_CALL
)
3817 if (!is_bl_insn
&& !is_blx_insn
)
3818 return This::STATUS_BAD_RELOC
;
3820 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
3822 // This cannot be a BLX.
3824 return This::STATUS_BAD_RELOC
;
3826 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
3828 // Check for Thumb to Thumb call.
3830 return This::STATUS_BAD_RELOC
;
3833 gold_warning(_("%s: Thumb BLX instruction targets "
3834 "thumb function '%s'."),
3835 object
->name().c_str(),
3836 (gsym
? gsym
->name() : "(local)"));
3837 // Convert BLX to BL.
3838 lower_insn
|= 0x1000U
;
3844 // A branch to an undefined weak symbol is turned into a jump to
3845 // the next instruction unless a PLT entry will be created.
3846 // The jump to the next instruction is optimized as a NOP.W for
3847 // Thumb-2 enabled architectures.
3848 const Target_arm
<big_endian
>* arm_target
=
3849 Target_arm
<big_endian
>::default_target();
3850 if (is_weakly_undefined_without_plt
)
3852 if (arm_target
->may_use_thumb2_nop())
3854 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
3855 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
3859 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
3860 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
3862 return This::STATUS_OKAY
;
3865 int32_t addend
= This::thumb32_branch_offset(upper_insn
, lower_insn
);
3866 Arm_address branch_target
= psymval
->value(object
, addend
);
3868 // For BLX, bit 1 of target address comes from bit 1 of base address.
3869 bool may_use_blx
= arm_target
->may_use_blx();
3870 if (thumb_bit
== 0 && may_use_blx
)
3871 branch_target
= utils::bit_select(branch_target
, address
, 0x2);
3873 int32_t branch_offset
= branch_target
- address
;
3875 // We need a stub if the branch offset is too large or if we need
3877 bool thumb2
= arm_target
->using_thumb2();
3878 if ((!thumb2
&& utils::has_overflow
<23>(branch_offset
))
3879 || (thumb2
&& utils::has_overflow
<25>(branch_offset
))
3880 || ((thumb_bit
== 0)
3881 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
3882 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
3884 Arm_address unadjusted_branch_target
= psymval
->value(object
, 0);
3886 Stub_type stub_type
=
3887 Reloc_stub::stub_type_for_reloc(r_type
, address
,
3888 unadjusted_branch_target
,
3891 if (stub_type
!= arm_stub_none
)
3893 Stub_table
<big_endian
>* stub_table
=
3894 object
->stub_table(relinfo
->data_shndx
);
3895 gold_assert(stub_table
!= NULL
);
3897 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
3898 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
3899 gold_assert(stub
!= NULL
);
3900 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3901 branch_target
= stub_table
->address() + stub
->offset() + addend
;
3902 if (thumb_bit
== 0 && may_use_blx
)
3903 branch_target
= utils::bit_select(branch_target
, address
, 0x2);
3904 branch_offset
= branch_target
- address
;
3908 // At this point, if we still need to switch mode, the instruction
3909 // must either be a BLX or a BL that can be converted to a BLX.
3912 gold_assert(may_use_blx
3913 && (r_type
== elfcpp::R_ARM_THM_CALL
3914 || r_type
== elfcpp::R_ARM_THM_XPC22
));
3915 // Make sure this is a BLX.
3916 lower_insn
&= ~0x1000U
;
3920 // Make sure this is a BL.
3921 lower_insn
|= 0x1000U
;
3924 // For a BLX instruction, make sure that the relocation is rounded up
3925 // to a word boundary. This follows the semantics of the instruction
3926 // which specifies that bit 1 of the target address will come from bit
3927 // 1 of the base address.
3928 if ((lower_insn
& 0x5000U
) == 0x4000U
)
3929 gold_assert((branch_offset
& 3) == 0);
3931 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3932 // We use the Thumb-2 encoding, which is safe even if dealing with
3933 // a Thumb-1 instruction by virtue of our overflow check above. */
3934 upper_insn
= This::thumb32_branch_upper(upper_insn
, branch_offset
);
3935 lower_insn
= This::thumb32_branch_lower(lower_insn
, branch_offset
);
3937 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3938 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3940 gold_assert(!utils::has_overflow
<25>(branch_offset
));
3943 ? utils::has_overflow
<25>(branch_offset
)
3944 : utils::has_overflow
<23>(branch_offset
))
3945 ? This::STATUS_OVERFLOW
3946 : This::STATUS_OKAY
);
3949 // Relocate THUMB-2 long conditional branches.
3950 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3951 // undefined and we do not use PLT in this relocation. In such a case,
3952 // the branch is converted into an NOP.
3954 template<bool big_endian
>
3955 typename Arm_relocate_functions
<big_endian
>::Status
3956 Arm_relocate_functions
<big_endian
>::thm_jump19(
3957 unsigned char *view
,
3958 const Arm_relobj
<big_endian
>* object
,
3959 const Symbol_value
<32>* psymval
,
3960 Arm_address address
,
3961 Arm_address thumb_bit
)
3963 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3964 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3965 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3966 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3967 int32_t addend
= This::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
3969 Arm_address branch_target
= psymval
->value(object
, addend
);
3970 int32_t branch_offset
= branch_target
- address
;
3972 // ??? Should handle interworking? GCC might someday try to
3973 // use this for tail calls.
3974 // FIXME: We do support thumb entry to PLT yet.
3977 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3978 return This::STATUS_BAD_RELOC
;
3981 // Put RELOCATION back into the insn.
3982 upper_insn
= This::thumb32_cond_branch_upper(upper_insn
, branch_offset
);
3983 lower_insn
= This::thumb32_cond_branch_lower(lower_insn
, branch_offset
);
3985 // Put the relocated value back in the object file:
3986 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
3987 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
3989 return (utils::has_overflow
<21>(branch_offset
)
3990 ? This::STATUS_OVERFLOW
3991 : This::STATUS_OKAY
);
3994 // Get the GOT section, creating it if necessary.
3996 template<bool big_endian
>
3997 Arm_output_data_got
<big_endian
>*
3998 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
4000 if (this->got_
== NULL
)
4002 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
4004 this->got_
= new Arm_output_data_got
<big_endian
>(symtab
, layout
);
4007 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
4009 | elfcpp::SHF_WRITE
),
4010 this->got_
, false, false, false,
4012 // The old GNU linker creates a .got.plt section. We just
4013 // create another set of data in the .got section. Note that we
4014 // always create a PLT if we create a GOT, although the PLT
4016 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
4017 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
4019 | elfcpp::SHF_WRITE
),
4020 this->got_plt_
, false, false,
4023 // The first three entries are reserved.
4024 this->got_plt_
->set_current_data_size(3 * 4);
4026 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4027 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
4028 Symbol_table::PREDEFINED
,
4030 0, 0, elfcpp::STT_OBJECT
,
4032 elfcpp::STV_HIDDEN
, 0,
4038 // Get the dynamic reloc section, creating it if necessary.
4040 template<bool big_endian
>
4041 typename Target_arm
<big_endian
>::Reloc_section
*
4042 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
4044 if (this->rel_dyn_
== NULL
)
4046 gold_assert(layout
!= NULL
);
4047 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
4048 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
4049 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
4050 false, false, false);
4052 return this->rel_dyn_
;
4055 // Insn_template methods.
4057 // Return byte size of an instruction template.
4060 Insn_template::size() const
4062 switch (this->type())
4065 case THUMB16_SPECIAL_TYPE
:
4076 // Return alignment of an instruction template.
4079 Insn_template::alignment() const
4081 switch (this->type())
4084 case THUMB16_SPECIAL_TYPE
:
4095 // Stub_template methods.
4097 Stub_template::Stub_template(
4098 Stub_type type
, const Insn_template
* insns
,
4100 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
4101 entry_in_thumb_mode_(false), relocs_()
4105 // Compute byte size and alignment of stub template.
4106 for (size_t i
= 0; i
< insn_count
; i
++)
4108 unsigned insn_alignment
= insns
[i
].alignment();
4109 size_t insn_size
= insns
[i
].size();
4110 gold_assert((offset
& (insn_alignment
- 1)) == 0);
4111 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
4112 switch (insns
[i
].type())
4114 case Insn_template::THUMB16_TYPE
:
4115 case Insn_template::THUMB16_SPECIAL_TYPE
:
4117 this->entry_in_thumb_mode_
= true;
4120 case Insn_template::THUMB32_TYPE
:
4121 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
4122 this->relocs_
.push_back(Reloc(i
, offset
));
4124 this->entry_in_thumb_mode_
= true;
4127 case Insn_template::ARM_TYPE
:
4128 // Handle cases where the target is encoded within the
4130 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
4131 this->relocs_
.push_back(Reloc(i
, offset
));
4134 case Insn_template::DATA_TYPE
:
4135 // Entry point cannot be data.
4136 gold_assert(i
!= 0);
4137 this->relocs_
.push_back(Reloc(i
, offset
));
4143 offset
+= insn_size
;
4145 this->size_
= offset
;
4150 // Template to implement do_write for a specific target endianness.
4152 template<bool big_endian
>
4154 Stub::do_fixed_endian_write(unsigned char* view
, section_size_type view_size
)
4156 const Stub_template
* stub_template
= this->stub_template();
4157 const Insn_template
* insns
= stub_template
->insns();
4159 // FIXME: We do not handle BE8 encoding yet.
4160 unsigned char* pov
= view
;
4161 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
4163 switch (insns
[i
].type())
4165 case Insn_template::THUMB16_TYPE
:
4166 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
4168 case Insn_template::THUMB16_SPECIAL_TYPE
:
4169 elfcpp::Swap
<16, big_endian
>::writeval(
4171 this->thumb16_special(i
));
4173 case Insn_template::THUMB32_TYPE
:
4175 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
4176 uint32_t lo
= insns
[i
].data() & 0xffff;
4177 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
4178 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
4181 case Insn_template::ARM_TYPE
:
4182 case Insn_template::DATA_TYPE
:
4183 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
4188 pov
+= insns
[i
].size();
4190 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
4193 // Reloc_stub::Key methods.
4195 // Dump a Key as a string for debugging.
4198 Reloc_stub::Key::name() const
4200 if (this->r_sym_
== invalid_index
)
4202 // Global symbol key name
4203 // <stub-type>:<symbol name>:<addend>.
4204 const std::string sym_name
= this->u_
.symbol
->name();
4205 // We need to print two hex number and two colons. So just add 100 bytes
4206 // to the symbol name size.
4207 size_t len
= sym_name
.size() + 100;
4208 char* buffer
= new char[len
];
4209 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
4210 sym_name
.c_str(), this->addend_
);
4211 gold_assert(c
> 0 && c
< static_cast<int>(len
));
4213 return std::string(buffer
);
4217 // local symbol key name
4218 // <stub-type>:<object>:<r_sym>:<addend>.
4219 const size_t len
= 200;
4221 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
4222 this->u_
.relobj
, this->r_sym_
, this->addend_
);
4223 gold_assert(c
> 0 && c
< static_cast<int>(len
));
4224 return std::string(buffer
);
4228 // Reloc_stub methods.
4230 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4231 // LOCATION to DESTINATION.
4232 // This code is based on the arm_type_of_stub function in
4233 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4237 Reloc_stub::stub_type_for_reloc(
4238 unsigned int r_type
,
4239 Arm_address location
,
4240 Arm_address destination
,
4241 bool target_is_thumb
)
4243 Stub_type stub_type
= arm_stub_none
;
4245 // This is a bit ugly but we want to avoid using a templated class for
4246 // big and little endianities.
4248 bool should_force_pic_veneer
;
4251 if (parameters
->target().is_big_endian())
4253 const Target_arm
<true>* big_endian_target
=
4254 Target_arm
<true>::default_target();
4255 may_use_blx
= big_endian_target
->may_use_blx();
4256 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
4257 thumb2
= big_endian_target
->using_thumb2();
4258 thumb_only
= big_endian_target
->using_thumb_only();
4262 const Target_arm
<false>* little_endian_target
=
4263 Target_arm
<false>::default_target();
4264 may_use_blx
= little_endian_target
->may_use_blx();
4265 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
4266 thumb2
= little_endian_target
->using_thumb2();
4267 thumb_only
= little_endian_target
->using_thumb_only();
4270 int64_t branch_offset
;
4271 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
4273 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4274 // base address (instruction address + 4).
4275 if ((r_type
== elfcpp::R_ARM_THM_CALL
) && may_use_blx
&& !target_is_thumb
)
4276 destination
= utils::bit_select(destination
, location
, 0x2);
4277 branch_offset
= static_cast<int64_t>(destination
) - location
;
4279 // Handle cases where:
4280 // - this call goes too far (different Thumb/Thumb2 max
4282 // - it's a Thumb->Arm call and blx is not available, or it's a
4283 // Thumb->Arm branch (not bl). A stub is needed in this case.
4285 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
4286 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
4288 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
4289 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
4290 || ((!target_is_thumb
)
4291 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
4292 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
4294 if (target_is_thumb
)
4299 stub_type
= (parameters
->options().shared()
4300 || should_force_pic_veneer
)
4303 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4304 // V5T and above. Stub starts with ARM code, so
4305 // we must be able to switch mode before
4306 // reaching it, which is only possible for 'bl'
4307 // (ie R_ARM_THM_CALL relocation).
4308 ? arm_stub_long_branch_any_thumb_pic
4309 // On V4T, use Thumb code only.
4310 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
4314 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4315 ? arm_stub_long_branch_any_any
// V5T and above.
4316 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
4320 stub_type
= (parameters
->options().shared()
4321 || should_force_pic_veneer
)
4322 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
4323 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
4330 // FIXME: We should check that the input section is from an
4331 // object that has interwork enabled.
4333 stub_type
= (parameters
->options().shared()
4334 || should_force_pic_veneer
)
4337 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4338 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
4339 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
4343 && (r_type
== elfcpp::R_ARM_THM_CALL
))
4344 ? arm_stub_long_branch_any_any
// V5T and above.
4345 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
4347 // Handle v4t short branches.
4348 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
4349 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
4350 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
4351 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
4355 else if (r_type
== elfcpp::R_ARM_CALL
4356 || r_type
== elfcpp::R_ARM_JUMP24
4357 || r_type
== elfcpp::R_ARM_PLT32
)
4359 branch_offset
= static_cast<int64_t>(destination
) - location
;
4360 if (target_is_thumb
)
4364 // FIXME: We should check that the input section is from an
4365 // object that has interwork enabled.
4367 // We have an extra 2-bytes reach because of
4368 // the mode change (bit 24 (H) of BLX encoding).
4369 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
4370 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
4371 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
4372 || (r_type
== elfcpp::R_ARM_JUMP24
)
4373 || (r_type
== elfcpp::R_ARM_PLT32
))
4375 stub_type
= (parameters
->options().shared()
4376 || should_force_pic_veneer
)
4379 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
4380 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
4384 ? arm_stub_long_branch_any_any
// V5T and above.
4385 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
4391 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
4392 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
4394 stub_type
= (parameters
->options().shared()
4395 || should_force_pic_veneer
)
4396 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
4397 : arm_stub_long_branch_any_any
; /// non-PIC.
4405 // Cortex_a8_stub methods.
4407 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4408 // I is the position of the instruction template in the stub template.
4411 Cortex_a8_stub::do_thumb16_special(size_t i
)
4413 // The only use of this is to copy condition code from a conditional
4414 // branch being worked around to the corresponding conditional branch in
4416 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4418 uint16_t data
= this->stub_template()->insns()[i
].data();
4419 gold_assert((data
& 0xff00U
) == 0xd000U
);
4420 data
|= ((this->original_insn_
>> 22) & 0xf) << 8;
4424 // Stub_factory methods.
4426 Stub_factory::Stub_factory()
4428 // The instruction template sequences are declared as static
4429 // objects and initialized first time the constructor runs.
4431 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4432 // to reach the stub if necessary.
4433 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
4435 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4436 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4437 // dcd R_ARM_ABS32(X)
4440 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4442 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
4444 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4445 Insn_template::arm_insn(0xe12fff1c), // bx ip
4446 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4447 // dcd R_ARM_ABS32(X)
4450 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4451 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
4453 Insn_template::thumb16_insn(0xb401), // push {r0}
4454 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4455 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4456 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4457 Insn_template::thumb16_insn(0x4760), // bx ip
4458 Insn_template::thumb16_insn(0xbf00), // nop
4459 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4460 // dcd R_ARM_ABS32(X)
4463 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4465 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
4467 Insn_template::thumb16_insn(0x4778), // bx pc
4468 Insn_template::thumb16_insn(0x46c0), // nop
4469 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4470 Insn_template::arm_insn(0xe12fff1c), // bx ip
4471 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4472 // dcd R_ARM_ABS32(X)
4475 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4477 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
4479 Insn_template::thumb16_insn(0x4778), // bx pc
4480 Insn_template::thumb16_insn(0x46c0), // nop
4481 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4482 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
4483 // dcd R_ARM_ABS32(X)
4486 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4487 // one, when the destination is close enough.
4488 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
4490 Insn_template::thumb16_insn(0x4778), // bx pc
4491 Insn_template::thumb16_insn(0x46c0), // nop
4492 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4495 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4496 // blx to reach the stub if necessary.
4497 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
4499 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4500 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4501 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
4502 // dcd R_ARM_REL32(X-4)
4505 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4506 // blx to reach the stub if necessary. We can not add into pc;
4507 // it is not guaranteed to mode switch (different in ARMv6 and
4509 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
4511 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4512 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4513 Insn_template::arm_insn(0xe12fff1c), // bx ip
4514 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4515 // dcd R_ARM_REL32(X)
4518 // V4T ARM -> ARM long branch stub, PIC.
4519 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
4521 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4522 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4523 Insn_template::arm_insn(0xe12fff1c), // bx ip
4524 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4525 // dcd R_ARM_REL32(X)
4528 // V4T Thumb -> ARM long branch stub, PIC.
4529 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
4531 Insn_template::thumb16_insn(0x4778), // bx pc
4532 Insn_template::thumb16_insn(0x46c0), // nop
4533 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4534 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4535 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
4536 // dcd R_ARM_REL32(X)
4539 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4541 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
4543 Insn_template::thumb16_insn(0xb401), // push {r0}
4544 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4545 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4546 Insn_template::thumb16_insn(0x4484), // add ip, r0
4547 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4548 Insn_template::thumb16_insn(0x4760), // bx ip
4549 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
4550 // dcd R_ARM_REL32(X)
4553 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4555 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
4557 Insn_template::thumb16_insn(0x4778), // bx pc
4558 Insn_template::thumb16_insn(0x46c0), // nop
4559 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4560 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4561 Insn_template::arm_insn(0xe12fff1c), // bx ip
4562 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
4563 // dcd R_ARM_REL32(X)
4566 // Cortex-A8 erratum-workaround stubs.
4568 // Stub used for conditional branches (which may be beyond +/-1MB away,
4569 // so we can't use a conditional branch to reach this stub).
4576 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
4578 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4579 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4580 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4584 // Stub used for b.w and bl.w instructions.
4586 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
4588 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4591 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
4593 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4596 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4597 // instruction (which switches to ARM mode) to point to this stub. Jump to
4598 // the real destination using an ARM-mode branch.
4599 static const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
4601 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4604 // Stub used to provide an interworking for R_ARM_V4BX relocation
4605 // (bx r[n] instruction).
4606 static const Insn_template elf32_arm_stub_v4_veneer_bx
[] =
4608 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4609 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4610 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4613 // Fill in the stub template look-up table. Stub templates are constructed
4614 // per instance of Stub_factory for fast look-up without locking
4615 // in a thread-enabled environment.
4617 this->stub_templates_
[arm_stub_none
] =
4618 new Stub_template(arm_stub_none
, NULL
, 0);
4620 #define DEF_STUB(x) \
4624 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4625 Stub_type type = arm_stub_##x; \
4626 this->stub_templates_[type] = \
4627 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4635 // Stub_table methods.
4637 // Removel all Cortex-A8 stub.
4639 template<bool big_endian
>
4641 Stub_table
<big_endian
>::remove_all_cortex_a8_stubs()
4643 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4644 p
!= this->cortex_a8_stubs_
.end();
4647 this->cortex_a8_stubs_
.clear();
4650 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4652 template<bool big_endian
>
4654 Stub_table
<big_endian
>::relocate_stub(
4656 const Relocate_info
<32, big_endian
>* relinfo
,
4657 Target_arm
<big_endian
>* arm_target
,
4658 Output_section
* output_section
,
4659 unsigned char* view
,
4660 Arm_address address
,
4661 section_size_type view_size
)
4663 const Stub_template
* stub_template
= stub
->stub_template();
4664 if (stub_template
->reloc_count() != 0)
4666 // Adjust view to cover the stub only.
4667 section_size_type offset
= stub
->offset();
4668 section_size_type stub_size
= stub_template
->size();
4669 gold_assert(offset
+ stub_size
<= view_size
);
4671 arm_target
->relocate_stub(stub
, relinfo
, output_section
, view
+ offset
,
4672 address
+ offset
, stub_size
);
4676 // Relocate all stubs in this stub table.
4678 template<bool big_endian
>
4680 Stub_table
<big_endian
>::relocate_stubs(
4681 const Relocate_info
<32, big_endian
>* relinfo
,
4682 Target_arm
<big_endian
>* arm_target
,
4683 Output_section
* output_section
,
4684 unsigned char* view
,
4685 Arm_address address
,
4686 section_size_type view_size
)
4688 // If we are passed a view bigger than the stub table's. we need to
4690 gold_assert(address
== this->address()
4692 == static_cast<section_size_type
>(this->data_size())));
4694 // Relocate all relocation stubs.
4695 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4696 p
!= this->reloc_stubs_
.end();
4698 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4699 address
, view_size
);
4701 // Relocate all Cortex-A8 stubs.
4702 for (Cortex_a8_stub_list::iterator p
= this->cortex_a8_stubs_
.begin();
4703 p
!= this->cortex_a8_stubs_
.end();
4705 this->relocate_stub(p
->second
, relinfo
, arm_target
, output_section
, view
,
4706 address
, view_size
);
4708 // Relocate all ARM V4BX stubs.
4709 for (Arm_v4bx_stub_list::iterator p
= this->arm_v4bx_stubs_
.begin();
4710 p
!= this->arm_v4bx_stubs_
.end();
4714 this->relocate_stub(*p
, relinfo
, arm_target
, output_section
, view
,
4715 address
, view_size
);
4719 // Write out the stubs to file.
4721 template<bool big_endian
>
4723 Stub_table
<big_endian
>::do_write(Output_file
* of
)
4725 off_t offset
= this->offset();
4726 const section_size_type oview_size
=
4727 convert_to_section_size_type(this->data_size());
4728 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4730 // Write relocation stubs.
4731 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
4732 p
!= this->reloc_stubs_
.end();
4735 Reloc_stub
* stub
= p
->second
;
4736 Arm_address address
= this->address() + stub
->offset();
4738 == align_address(address
,
4739 stub
->stub_template()->alignment()));
4740 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4744 // Write Cortex-A8 stubs.
4745 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4746 p
!= this->cortex_a8_stubs_
.end();
4749 Cortex_a8_stub
* stub
= p
->second
;
4750 Arm_address address
= this->address() + stub
->offset();
4752 == align_address(address
,
4753 stub
->stub_template()->alignment()));
4754 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
4758 // Write ARM V4BX relocation stubs.
4759 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4760 p
!= this->arm_v4bx_stubs_
.end();
4766 Arm_address address
= this->address() + (*p
)->offset();
4768 == align_address(address
,
4769 (*p
)->stub_template()->alignment()));
4770 (*p
)->write(oview
+ (*p
)->offset(), (*p
)->stub_template()->size(),
4774 of
->write_output_view(this->offset(), oview_size
, oview
);
4777 // Update the data size and address alignment of the stub table at the end
4778 // of a relaxation pass. Return true if either the data size or the
4779 // alignment changed in this relaxation pass.
4781 template<bool big_endian
>
4783 Stub_table
<big_endian
>::update_data_size_and_addralign()
4785 // Go over all stubs in table to compute data size and address alignment.
4786 off_t size
= this->reloc_stubs_size_
;
4787 unsigned addralign
= this->reloc_stubs_addralign_
;
4789 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4790 p
!= this->cortex_a8_stubs_
.end();
4793 const Stub_template
* stub_template
= p
->second
->stub_template();
4794 addralign
= std::max(addralign
, stub_template
->alignment());
4795 size
= (align_address(size
, stub_template
->alignment())
4796 + stub_template
->size());
4799 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4800 p
!= this->arm_v4bx_stubs_
.end();
4806 const Stub_template
* stub_template
= (*p
)->stub_template();
4807 addralign
= std::max(addralign
, stub_template
->alignment());
4808 size
= (align_address(size
, stub_template
->alignment())
4809 + stub_template
->size());
4812 // Check if either data size or alignment changed in this pass.
4813 // Update prev_data_size_ and prev_addralign_. These will be used
4814 // as the current data size and address alignment for the next pass.
4815 bool changed
= size
!= this->prev_data_size_
;
4816 this->prev_data_size_
= size
;
4818 if (addralign
!= this->prev_addralign_
)
4820 this->prev_addralign_
= addralign
;
4825 // Finalize the stubs. This sets the offsets of the stubs within the stub
4826 // table. It also marks all input sections needing Cortex-A8 workaround.
4828 template<bool big_endian
>
4830 Stub_table
<big_endian
>::finalize_stubs()
4832 off_t off
= this->reloc_stubs_size_
;
4833 for (Cortex_a8_stub_list::const_iterator p
= this->cortex_a8_stubs_
.begin();
4834 p
!= this->cortex_a8_stubs_
.end();
4837 Cortex_a8_stub
* stub
= p
->second
;
4838 const Stub_template
* stub_template
= stub
->stub_template();
4839 uint64_t stub_addralign
= stub_template
->alignment();
4840 off
= align_address(off
, stub_addralign
);
4841 stub
->set_offset(off
);
4842 off
+= stub_template
->size();
4844 // Mark input section so that we can determine later if a code section
4845 // needs the Cortex-A8 workaround quickly.
4846 Arm_relobj
<big_endian
>* arm_relobj
=
4847 Arm_relobj
<big_endian
>::as_arm_relobj(stub
->relobj());
4848 arm_relobj
->mark_section_for_cortex_a8_workaround(stub
->shndx());
4851 for (Arm_v4bx_stub_list::const_iterator p
= this->arm_v4bx_stubs_
.begin();
4852 p
!= this->arm_v4bx_stubs_
.end();
4858 const Stub_template
* stub_template
= (*p
)->stub_template();
4859 uint64_t stub_addralign
= stub_template
->alignment();
4860 off
= align_address(off
, stub_addralign
);
4861 (*p
)->set_offset(off
);
4862 off
+= stub_template
->size();
4865 gold_assert(off
<= this->prev_data_size_
);
4868 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4869 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4870 // of the address range seen by the linker.
4872 template<bool big_endian
>
4874 Stub_table
<big_endian
>::apply_cortex_a8_workaround_to_address_range(
4875 Target_arm
<big_endian
>* arm_target
,
4876 unsigned char* view
,
4877 Arm_address view_address
,
4878 section_size_type view_size
)
4880 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4881 for (Cortex_a8_stub_list::const_iterator p
=
4882 this->cortex_a8_stubs_
.lower_bound(view_address
);
4883 ((p
!= this->cortex_a8_stubs_
.end())
4884 && (p
->first
< (view_address
+ view_size
)));
4887 // We do not store the THUMB bit in the LSB of either the branch address
4888 // or the stub offset. There is no need to strip the LSB.
4889 Arm_address branch_address
= p
->first
;
4890 const Cortex_a8_stub
* stub
= p
->second
;
4891 Arm_address stub_address
= this->address() + stub
->offset();
4893 // Offset of the branch instruction relative to this view.
4894 section_size_type offset
=
4895 convert_to_section_size_type(branch_address
- view_address
);
4896 gold_assert((offset
+ 4) <= view_size
);
4898 arm_target
->apply_cortex_a8_workaround(stub
, stub_address
,
4899 view
+ offset
, branch_address
);
4903 // Arm_input_section methods.
4905 // Initialize an Arm_input_section.
4907 template<bool big_endian
>
4909 Arm_input_section
<big_endian
>::init()
4911 Relobj
* relobj
= this->relobj();
4912 unsigned int shndx
= this->shndx();
4914 // Cache these to speed up size and alignment queries. It is too slow
4915 // to call section_addraglin and section_size every time.
4916 this->original_addralign_
=
4917 convert_types
<uint32_t, uint64_t>(relobj
->section_addralign(shndx
));
4918 this->original_size_
=
4919 convert_types
<uint32_t, uint64_t>(relobj
->section_size(shndx
));
4921 // We want to make this look like the original input section after
4922 // output sections are finalized.
4923 Output_section
* os
= relobj
->output_section(shndx
);
4924 off_t offset
= relobj
->output_section_offset(shndx
);
4925 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
4926 this->set_address(os
->address() + offset
);
4927 this->set_file_offset(os
->offset() + offset
);
4929 this->set_current_data_size(this->original_size_
);
4930 this->finalize_data_size();
4933 template<bool big_endian
>
4935 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
4937 // We have to write out the original section content.
4938 section_size_type section_size
;
4939 const unsigned char* section_contents
=
4940 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
4941 of
->write(this->offset(), section_contents
, section_size
);
4943 // If this owns a stub table and it is not empty, write it.
4944 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
4945 this->stub_table_
->write(of
);
4948 // Finalize data size.
4950 template<bool big_endian
>
4952 Arm_input_section
<big_endian
>::set_final_data_size()
4954 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
4956 if (this->is_stub_table_owner())
4958 this->stub_table_
->finalize_data_size();
4959 off
= align_address(off
, this->stub_table_
->addralign());
4960 off
+= this->stub_table_
->data_size();
4962 this->set_data_size(off
);
4965 // Reset address and file offset.
4967 template<bool big_endian
>
4969 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
4971 // Size of the original input section contents.
4972 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
4974 // If this is a stub table owner, account for the stub table size.
4975 if (this->is_stub_table_owner())
4977 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
4979 // Reset the stub table's address and file offset. The
4980 // current data size for child will be updated after that.
4981 stub_table_
->reset_address_and_file_offset();
4982 off
= align_address(off
, stub_table_
->addralign());
4983 off
+= stub_table
->current_data_size();
4986 this->set_current_data_size(off
);
4989 // Arm_exidx_cantunwind methods.
4991 // Write this to Output file OF for a fixed endianness.
4993 template<bool big_endian
>
4995 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file
* of
)
4997 off_t offset
= this->offset();
4998 const section_size_type oview_size
= 8;
4999 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5001 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5002 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
);
5004 Output_section
* os
= this->relobj_
->output_section(this->shndx_
);
5005 gold_assert(os
!= NULL
);
5007 Arm_relobj
<big_endian
>* arm_relobj
=
5008 Arm_relobj
<big_endian
>::as_arm_relobj(this->relobj_
);
5009 Arm_address output_offset
=
5010 arm_relobj
->get_output_section_offset(this->shndx_
);
5011 Arm_address section_start
;
5012 if (output_offset
!= Arm_relobj
<big_endian
>::invalid_address
)
5013 section_start
= os
->address() + output_offset
;
5016 // Currently this only happens for a relaxed section.
5017 const Output_relaxed_input_section
* poris
=
5018 os
->find_relaxed_input_section(this->relobj_
, this->shndx_
);
5019 gold_assert(poris
!= NULL
);
5020 section_start
= poris
->address();
5023 // We always append this to the end of an EXIDX section.
5024 Arm_address output_address
=
5025 section_start
+ this->relobj_
->section_size(this->shndx_
);
5027 // Write out the entry. The first word either points to the beginning
5028 // or after the end of a text section. The second word is the special
5029 // EXIDX_CANTUNWIND value.
5030 uint32_t prel31_offset
= output_address
- this->address();
5031 if (utils::has_overflow
<31>(offset
))
5032 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5033 elfcpp::Swap
<32, big_endian
>::writeval(wv
, prel31_offset
& 0x7fffffffU
);
5034 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, elfcpp::EXIDX_CANTUNWIND
);
5036 of
->write_output_view(this->offset(), oview_size
, oview
);
5039 // Arm_exidx_merged_section methods.
5041 // Constructor for Arm_exidx_merged_section.
5042 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5043 // SECTION_OFFSET_MAP points to a section offset map describing how
5044 // parts of the input section are mapped to output. DELETED_BYTES is
5045 // the number of bytes deleted from the EXIDX input section.
5047 Arm_exidx_merged_section::Arm_exidx_merged_section(
5048 const Arm_exidx_input_section
& exidx_input_section
,
5049 const Arm_exidx_section_offset_map
& section_offset_map
,
5050 uint32_t deleted_bytes
)
5051 : Output_relaxed_input_section(exidx_input_section
.relobj(),
5052 exidx_input_section
.shndx(),
5053 exidx_input_section
.addralign()),
5054 exidx_input_section_(exidx_input_section
),
5055 section_offset_map_(section_offset_map
)
5057 // Fix size here so that we do not need to implement set_final_data_size.
5058 this->set_data_size(exidx_input_section
.size() - deleted_bytes
);
5059 this->fix_data_size();
5062 // Given an input OBJECT, an input section index SHNDX within that
5063 // object, and an OFFSET relative to the start of that input
5064 // section, return whether or not the corresponding offset within
5065 // the output section is known. If this function returns true, it
5066 // sets *POUTPUT to the output offset. The value -1 indicates that
5067 // this input offset is being discarded.
5070 Arm_exidx_merged_section::do_output_offset(
5071 const Relobj
* relobj
,
5073 section_offset_type offset
,
5074 section_offset_type
* poutput
) const
5076 // We only handle offsets for the original EXIDX input section.
5077 if (relobj
!= this->exidx_input_section_
.relobj()
5078 || shndx
!= this->exidx_input_section_
.shndx())
5081 section_offset_type section_size
=
5082 convert_types
<section_offset_type
>(this->exidx_input_section_
.size());
5083 if (offset
< 0 || offset
>= section_size
)
5084 // Input offset is out of valid range.
5088 // We need to look up the section offset map to determine the output
5089 // offset. Find the reference point in map that is first offset
5090 // bigger than or equal to this offset.
5091 Arm_exidx_section_offset_map::const_iterator p
=
5092 this->section_offset_map_
.lower_bound(offset
);
5094 // The section offset maps are build such that this should not happen if
5095 // input offset is in the valid range.
5096 gold_assert(p
!= this->section_offset_map_
.end());
5098 // We need to check if this is dropped.
5099 section_offset_type ref
= p
->first
;
5100 section_offset_type mapped_ref
= p
->second
;
5102 if (mapped_ref
!= Arm_exidx_input_section::invalid_offset
)
5103 // Offset is present in output.
5104 *poutput
= mapped_ref
+ (offset
- ref
);
5106 // Offset is discarded owing to EXIDX entry merging.
5113 // Write this to output file OF.
5116 Arm_exidx_merged_section::do_write(Output_file
* of
)
5118 // If we retain or discard the whole EXIDX input section, we would
5120 gold_assert(this->data_size() != this->exidx_input_section_
.size()
5121 && this->data_size() != 0);
5123 off_t offset
= this->offset();
5124 const section_size_type oview_size
= this->data_size();
5125 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
5127 Output_section
* os
= this->relobj()->output_section(this->shndx());
5128 gold_assert(os
!= NULL
);
5130 // Get contents of EXIDX input section.
5131 section_size_type section_size
;
5132 const unsigned char* section_contents
=
5133 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
5134 gold_assert(section_size
== this->exidx_input_section_
.size());
5136 // Go over spans of input offsets and write only those that are not
5138 section_offset_type in_start
= 0;
5139 section_offset_type out_start
= 0;
5140 for(Arm_exidx_section_offset_map::const_iterator p
=
5141 this->section_offset_map_
.begin();
5142 p
!= this->section_offset_map_
.end();
5145 section_offset_type in_end
= p
->first
;
5146 gold_assert(in_end
>= in_start
);
5147 section_offset_type out_end
= p
->second
;
5148 size_t in_chunk_size
= convert_types
<size_t>(in_end
- in_start
+ 1);
5151 size_t out_chunk_size
=
5152 convert_types
<size_t>(out_end
- out_start
+ 1);
5153 gold_assert(out_chunk_size
== in_chunk_size
);
5154 memcpy(oview
+ out_start
, section_contents
+ in_start
,
5156 out_start
+= out_chunk_size
;
5158 in_start
+= in_chunk_size
;
5161 gold_assert(convert_to_section_size_type(out_start
) == oview_size
);
5162 of
->write_output_view(this->offset(), oview_size
, oview
);
5165 // Arm_exidx_fixup methods.
5167 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5168 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5169 // points to the end of the last seen EXIDX section.
5172 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5174 if (this->last_unwind_type_
!= UT_EXIDX_CANTUNWIND
5175 && this->last_input_section_
!= NULL
)
5177 Relobj
* relobj
= this->last_input_section_
->relobj();
5178 unsigned int text_shndx
= this->last_input_section_
->link();
5179 Arm_exidx_cantunwind
* cantunwind
=
5180 new Arm_exidx_cantunwind(relobj
, text_shndx
);
5181 this->exidx_output_section_
->add_output_section_data(cantunwind
);
5182 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
5186 // Process an EXIDX section entry in input. Return whether this entry
5187 // can be deleted in the output. SECOND_WORD in the second word of the
5191 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word
)
5194 if (second_word
== elfcpp::EXIDX_CANTUNWIND
)
5196 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5197 delete_entry
= this->last_unwind_type_
== UT_EXIDX_CANTUNWIND
;
5198 this->last_unwind_type_
= UT_EXIDX_CANTUNWIND
;
5200 else if ((second_word
& 0x80000000) != 0)
5202 // Inlined unwinding data. Merge if equal to previous.
5203 delete_entry
= (merge_exidx_entries_
5204 && this->last_unwind_type_
== UT_INLINED_ENTRY
5205 && this->last_inlined_entry_
== second_word
);
5206 this->last_unwind_type_
= UT_INLINED_ENTRY
;
5207 this->last_inlined_entry_
= second_word
;
5211 // Normal table entry. In theory we could merge these too,
5212 // but duplicate entries are likely to be much less common.
5213 delete_entry
= false;
5214 this->last_unwind_type_
= UT_NORMAL_ENTRY
;
5216 return delete_entry
;
5219 // Update the current section offset map during EXIDX section fix-up.
5220 // If there is no map, create one. INPUT_OFFSET is the offset of a
5221 // reference point, DELETED_BYTES is the number of deleted by in the
5222 // section so far. If DELETE_ENTRY is true, the reference point and
5223 // all offsets after the previous reference point are discarded.
5226 Arm_exidx_fixup::update_offset_map(
5227 section_offset_type input_offset
,
5228 section_size_type deleted_bytes
,
5231 if (this->section_offset_map_
== NULL
)
5232 this->section_offset_map_
= new Arm_exidx_section_offset_map();
5233 section_offset_type output_offset
;
5235 output_offset
= Arm_exidx_input_section::invalid_offset
;
5237 output_offset
= input_offset
- deleted_bytes
;
5238 (*this->section_offset_map_
)[input_offset
] = output_offset
;
5241 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5242 // bytes deleted. If some entries are merged, also store a pointer to a newly
5243 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5244 // caller owns the map and is responsible for releasing it after use.
5246 template<bool big_endian
>
5248 Arm_exidx_fixup::process_exidx_section(
5249 const Arm_exidx_input_section
* exidx_input_section
,
5250 Arm_exidx_section_offset_map
** psection_offset_map
)
5252 Relobj
* relobj
= exidx_input_section
->relobj();
5253 unsigned shndx
= exidx_input_section
->shndx();
5254 section_size_type section_size
;
5255 const unsigned char* section_contents
=
5256 relobj
->section_contents(shndx
, §ion_size
, false);
5258 if ((section_size
% 8) != 0)
5260 // Something is wrong with this section. Better not touch it.
5261 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5262 relobj
->name().c_str(), shndx
);
5263 this->last_input_section_
= exidx_input_section
;
5264 this->last_unwind_type_
= UT_NONE
;
5268 uint32_t deleted_bytes
= 0;
5269 bool prev_delete_entry
= false;
5270 gold_assert(this->section_offset_map_
== NULL
);
5272 for (section_size_type i
= 0; i
< section_size
; i
+= 8)
5274 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5276 reinterpret_cast<const Valtype
*>(section_contents
+ i
+ 4);
5277 uint32_t second_word
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5279 bool delete_entry
= this->process_exidx_entry(second_word
);
5281 // Entry deletion causes changes in output offsets. We use a std::map
5282 // to record these. And entry (x, y) means input offset x
5283 // is mapped to output offset y. If y is invalid_offset, then x is
5284 // dropped in the output. Because of the way std::map::lower_bound
5285 // works, we record the last offset in a region w.r.t to keeping or
5286 // dropping. If there is no entry (x0, y0) for an input offset x0,
5287 // the output offset y0 of it is determined by the output offset y1 of
5288 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5289 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5291 if (delete_entry
!= prev_delete_entry
&& i
!= 0)
5292 this->update_offset_map(i
- 1, deleted_bytes
, prev_delete_entry
);
5294 // Update total deleted bytes for this entry.
5298 prev_delete_entry
= delete_entry
;
5301 // If section offset map is not NULL, make an entry for the end of
5303 if (this->section_offset_map_
!= NULL
)
5304 update_offset_map(section_size
- 1, deleted_bytes
, prev_delete_entry
);
5306 *psection_offset_map
= this->section_offset_map_
;
5307 this->section_offset_map_
= NULL
;
5308 this->last_input_section_
= exidx_input_section
;
5310 // Set the first output text section so that we can link the EXIDX output
5311 // section to it. Ignore any EXIDX input section that is completely merged.
5312 if (this->first_output_text_section_
== NULL
5313 && deleted_bytes
!= section_size
)
5315 unsigned int link
= exidx_input_section
->link();
5316 Output_section
* os
= relobj
->output_section(link
);
5317 gold_assert(os
!= NULL
);
5318 this->first_output_text_section_
= os
;
5321 return deleted_bytes
;
5324 // Arm_output_section methods.
5326 // Create a stub group for input sections from BEGIN to END. OWNER
5327 // points to the input section to be the owner a new stub table.
5329 template<bool big_endian
>
5331 Arm_output_section
<big_endian
>::create_stub_group(
5332 Input_section_list::const_iterator begin
,
5333 Input_section_list::const_iterator end
,
5334 Input_section_list::const_iterator owner
,
5335 Target_arm
<big_endian
>* target
,
5336 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
5338 // We use a different kind of relaxed section in an EXIDX section.
5339 // The static casting from Output_relaxed_input_section to
5340 // Arm_input_section is invalid in an EXIDX section. We are okay
5341 // because we should not be calling this for an EXIDX section.
5342 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX
);
5344 // Currently we convert ordinary input sections into relaxed sections only
5345 // at this point but we may want to support creating relaxed input section
5346 // very early. So we check here to see if owner is already a relaxed
5349 Arm_input_section
<big_endian
>* arm_input_section
;
5350 if (owner
->is_relaxed_input_section())
5353 Arm_input_section
<big_endian
>::as_arm_input_section(
5354 owner
->relaxed_input_section());
5358 gold_assert(owner
->is_input_section());
5359 // Create a new relaxed input section.
5361 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
5362 new_relaxed_sections
->push_back(arm_input_section
);
5365 // Create a stub table.
5366 Stub_table
<big_endian
>* stub_table
=
5367 target
->new_stub_table(arm_input_section
);
5369 arm_input_section
->set_stub_table(stub_table
);
5371 Input_section_list::const_iterator p
= begin
;
5372 Input_section_list::const_iterator prev_p
;
5374 // Look for input sections or relaxed input sections in [begin ... end].
5377 if (p
->is_input_section() || p
->is_relaxed_input_section())
5379 // The stub table information for input sections live
5380 // in their objects.
5381 Arm_relobj
<big_endian
>* arm_relobj
=
5382 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
5383 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
5387 while (prev_p
!= end
);
5390 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5391 // of stub groups. We grow a stub group by adding input section until the
5392 // size is just below GROUP_SIZE. The last input section will be converted
5393 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5394 // input section after the stub table, effectively double the group size.
5396 // This is similar to the group_sections() function in elf32-arm.c but is
5397 // implemented differently.
5399 template<bool big_endian
>
5401 Arm_output_section
<big_endian
>::group_sections(
5402 section_size_type group_size
,
5403 bool stubs_always_after_branch
,
5404 Target_arm
<big_endian
>* target
)
5406 // We only care about sections containing code.
5407 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
5410 // States for grouping.
5413 // No group is being built.
5415 // A group is being built but the stub table is not found yet.
5416 // We keep group a stub group until the size is just under GROUP_SIZE.
5417 // The last input section in the group will be used as the stub table.
5418 FINDING_STUB_SECTION
,
5419 // A group is being built and we have already found a stub table.
5420 // We enter this state to grow a stub group by adding input section
5421 // after the stub table. This effectively doubles the group size.
5425 // Any newly created relaxed sections are stored here.
5426 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
5428 State state
= NO_GROUP
;
5429 section_size_type off
= 0;
5430 section_size_type group_begin_offset
= 0;
5431 section_size_type group_end_offset
= 0;
5432 section_size_type stub_table_end_offset
= 0;
5433 Input_section_list::const_iterator group_begin
=
5434 this->input_sections().end();
5435 Input_section_list::const_iterator stub_table
=
5436 this->input_sections().end();
5437 Input_section_list::const_iterator group_end
= this->input_sections().end();
5438 for (Input_section_list::const_iterator p
= this->input_sections().begin();
5439 p
!= this->input_sections().end();
5442 section_size_type section_begin_offset
=
5443 align_address(off
, p
->addralign());
5444 section_size_type section_end_offset
=
5445 section_begin_offset
+ p
->data_size();
5447 // Check to see if we should group the previously seens sections.
5453 case FINDING_STUB_SECTION
:
5454 // Adding this section makes the group larger than GROUP_SIZE.
5455 if (section_end_offset
- group_begin_offset
>= group_size
)
5457 if (stubs_always_after_branch
)
5459 gold_assert(group_end
!= this->input_sections().end());
5460 this->create_stub_group(group_begin
, group_end
, group_end
,
5461 target
, &new_relaxed_sections
);
5466 // But wait, there's more! Input sections up to
5467 // stub_group_size bytes after the stub table can be
5468 // handled by it too.
5469 state
= HAS_STUB_SECTION
;
5470 stub_table
= group_end
;
5471 stub_table_end_offset
= group_end_offset
;
5476 case HAS_STUB_SECTION
:
5477 // Adding this section makes the post stub-section group larger
5479 if (section_end_offset
- stub_table_end_offset
>= group_size
)
5481 gold_assert(group_end
!= this->input_sections().end());
5482 this->create_stub_group(group_begin
, group_end
, stub_table
,
5483 target
, &new_relaxed_sections
);
5492 // If we see an input section and currently there is no group, start
5493 // a new one. Skip any empty sections.
5494 if ((p
->is_input_section() || p
->is_relaxed_input_section())
5495 && (p
->relobj()->section_size(p
->shndx()) != 0))
5497 if (state
== NO_GROUP
)
5499 state
= FINDING_STUB_SECTION
;
5501 group_begin_offset
= section_begin_offset
;
5504 // Keep track of the last input section seen.
5506 group_end_offset
= section_end_offset
;
5509 off
= section_end_offset
;
5512 // Create a stub group for any ungrouped sections.
5513 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
5515 gold_assert(group_end
!= this->input_sections().end());
5516 this->create_stub_group(group_begin
, group_end
,
5517 (state
== FINDING_STUB_SECTION
5520 target
, &new_relaxed_sections
);
5523 // Convert input section into relaxed input section in a batch.
5524 if (!new_relaxed_sections
.empty())
5525 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
5527 // Update the section offsets
5528 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
5530 Arm_relobj
<big_endian
>* arm_relobj
=
5531 Arm_relobj
<big_endian
>::as_arm_relobj(
5532 new_relaxed_sections
[i
]->relobj());
5533 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
5534 // Tell Arm_relobj that this input section is converted.
5535 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
5539 // Append non empty text sections in this to LIST in ascending
5540 // order of their position in this.
5542 template<bool big_endian
>
5544 Arm_output_section
<big_endian
>::append_text_sections_to_list(
5545 Text_section_list
* list
)
5547 // We only care about text sections.
5548 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
5551 gold_assert((this->flags() & elfcpp::SHF_ALLOC
) != 0);
5553 for (Input_section_list::const_iterator p
= this->input_sections().begin();
5554 p
!= this->input_sections().end();
5557 // We only care about plain or relaxed input sections. We also
5558 // ignore any merged sections.
5559 if ((p
->is_input_section() || p
->is_relaxed_input_section())
5560 && p
->data_size() != 0)
5561 list
->push_back(Text_section_list::value_type(p
->relobj(),
5566 template<bool big_endian
>
5568 Arm_output_section
<big_endian
>::fix_exidx_coverage(
5570 const Text_section_list
& sorted_text_sections
,
5571 Symbol_table
* symtab
,
5572 bool merge_exidx_entries
)
5574 // We should only do this for the EXIDX output section.
5575 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX
);
5577 // We don't want the relaxation loop to undo these changes, so we discard
5578 // the current saved states and take another one after the fix-up.
5579 this->discard_states();
5581 // Remove all input sections.
5582 uint64_t address
= this->address();
5583 typedef std::list
<Output_section::Input_section
> Input_section_list
;
5584 Input_section_list input_sections
;
5585 this->reset_address_and_file_offset();
5586 this->get_input_sections(address
, std::string(""), &input_sections
);
5588 if (!this->input_sections().empty())
5589 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5591 // Go through all the known input sections and record them.
5592 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_id_set
;
5593 typedef Unordered_map
<Section_id
, const Output_section::Input_section
*,
5594 Section_id_hash
> Text_to_exidx_map
;
5595 Text_to_exidx_map text_to_exidx_map
;
5596 for (Input_section_list::const_iterator p
= input_sections
.begin();
5597 p
!= input_sections
.end();
5600 // This should never happen. At this point, we should only see
5601 // plain EXIDX input sections.
5602 gold_assert(!p
->is_relaxed_input_section());
5603 text_to_exidx_map
[Section_id(p
->relobj(), p
->shndx())] = &(*p
);
5606 Arm_exidx_fixup
exidx_fixup(this, merge_exidx_entries
);
5608 // Go over the sorted text sections.
5609 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_id_set
;
5610 Section_id_set processed_input_sections
;
5611 for (Text_section_list::const_iterator p
= sorted_text_sections
.begin();
5612 p
!= sorted_text_sections
.end();
5615 Relobj
* relobj
= p
->first
;
5616 unsigned int shndx
= p
->second
;
5618 Arm_relobj
<big_endian
>* arm_relobj
=
5619 Arm_relobj
<big_endian
>::as_arm_relobj(relobj
);
5620 const Arm_exidx_input_section
* exidx_input_section
=
5621 arm_relobj
->exidx_input_section_by_link(shndx
);
5623 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5624 // entry pointing to the end of the last seen EXIDX section.
5625 if (exidx_input_section
== NULL
)
5627 exidx_fixup
.add_exidx_cantunwind_as_needed();
5631 Relobj
* exidx_relobj
= exidx_input_section
->relobj();
5632 unsigned int exidx_shndx
= exidx_input_section
->shndx();
5633 Section_id
sid(exidx_relobj
, exidx_shndx
);
5634 Text_to_exidx_map::const_iterator iter
= text_to_exidx_map
.find(sid
);
5635 if (iter
== text_to_exidx_map
.end())
5637 // This is odd. We have not seen this EXIDX input section before.
5638 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5639 // issue a warning instead. We assume the user knows what he
5640 // or she is doing. Otherwise, this is an error.
5641 if (layout
->script_options()->saw_sections_clause())
5642 gold_warning(_("unwinding may not work because EXIDX input section"
5643 " %u of %s is not in EXIDX output section"),
5644 exidx_shndx
, exidx_relobj
->name().c_str());
5646 gold_error(_("unwinding may not work because EXIDX input section"
5647 " %u of %s is not in EXIDX output section"),
5648 exidx_shndx
, exidx_relobj
->name().c_str());
5650 exidx_fixup
.add_exidx_cantunwind_as_needed();
5654 // Fix up coverage and append input section to output data list.
5655 Arm_exidx_section_offset_map
* section_offset_map
= NULL
;
5656 uint32_t deleted_bytes
=
5657 exidx_fixup
.process_exidx_section
<big_endian
>(exidx_input_section
,
5658 §ion_offset_map
);
5660 if (deleted_bytes
== exidx_input_section
->size())
5662 // The whole EXIDX section got merged. Remove it from output.
5663 gold_assert(section_offset_map
== NULL
);
5664 exidx_relobj
->set_output_section(exidx_shndx
, NULL
);
5666 // All local symbols defined in this input section will be dropped.
5667 // We need to adjust output local symbol count.
5668 arm_relobj
->set_output_local_symbol_count_needs_update();
5670 else if (deleted_bytes
> 0)
5672 // Some entries are merged. We need to convert this EXIDX input
5673 // section into a relaxed section.
5674 gold_assert(section_offset_map
!= NULL
);
5675 Arm_exidx_merged_section
* merged_section
=
5676 new Arm_exidx_merged_section(*exidx_input_section
,
5677 *section_offset_map
, deleted_bytes
);
5678 this->add_relaxed_input_section(merged_section
);
5679 arm_relobj
->convert_input_section_to_relaxed_section(exidx_shndx
);
5681 // All local symbols defined in discarded portions of this input
5682 // section will be dropped. We need to adjust output local symbol
5684 arm_relobj
->set_output_local_symbol_count_needs_update();
5688 // Just add back the EXIDX input section.
5689 gold_assert(section_offset_map
== NULL
);
5690 const Output_section::Input_section
* pis
= iter
->second
;
5691 gold_assert(pis
->is_input_section());
5692 this->add_script_input_section(*pis
);
5695 processed_input_sections
.insert(Section_id(exidx_relobj
, exidx_shndx
));
5698 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5699 exidx_fixup
.add_exidx_cantunwind_as_needed();
5701 // Remove any known EXIDX input sections that are not processed.
5702 for (Input_section_list::const_iterator p
= input_sections
.begin();
5703 p
!= input_sections
.end();
5706 if (processed_input_sections
.find(Section_id(p
->relobj(), p
->shndx()))
5707 == processed_input_sections
.end())
5709 // We only discard a known EXIDX section because its linked
5710 // text section has been folded by ICF.
5711 Arm_relobj
<big_endian
>* arm_relobj
=
5712 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
5713 const Arm_exidx_input_section
* exidx_input_section
=
5714 arm_relobj
->exidx_input_section_by_shndx(p
->shndx());
5715 gold_assert(exidx_input_section
!= NULL
);
5716 unsigned int text_shndx
= exidx_input_section
->link();
5717 gold_assert(symtab
->is_section_folded(p
->relobj(), text_shndx
));
5719 // Remove this from link. We also need to recount the
5721 p
->relobj()->set_output_section(p
->shndx(), NULL
);
5722 arm_relobj
->set_output_local_symbol_count_needs_update();
5726 // Link exidx output section to the first seen output section and
5727 // set correct entry size.
5728 this->set_link_section(exidx_fixup
.first_output_text_section());
5729 this->set_entsize(8);
5731 // Make changes permanent.
5732 this->save_states();
5733 this->set_section_offsets_need_adjustment();
5736 // Arm_relobj methods.
5738 // Determine if an input section is scannable for stub processing. SHDR is
5739 // the header of the section and SHNDX is the section index. OS is the output
5740 // section for the input section and SYMTAB is the global symbol table used to
5741 // look up ICF information.
5743 template<bool big_endian
>
5745 Arm_relobj
<big_endian
>::section_is_scannable(
5746 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5748 const Output_section
* os
,
5749 const Symbol_table
*symtab
)
5751 // Skip any empty sections, unallocated sections or sections whose
5752 // type are not SHT_PROGBITS.
5753 if (shdr
.get_sh_size() == 0
5754 || (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0
5755 || shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
5758 // Skip any discarded or ICF'ed sections.
5759 if (os
== NULL
|| symtab
->is_section_folded(this, shndx
))
5762 // If this requires special offset handling, check to see if it is
5763 // a relaxed section. If this is not, then it is a merged section that
5764 // we cannot handle.
5765 if (this->is_output_section_offset_invalid(shndx
))
5767 const Output_relaxed_input_section
* poris
=
5768 os
->find_relaxed_input_section(this, shndx
);
5776 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5777 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5779 template<bool big_endian
>
5781 Arm_relobj
<big_endian
>::section_needs_reloc_stub_scanning(
5782 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5783 const Relobj::Output_sections
& out_sections
,
5784 const Symbol_table
*symtab
,
5785 const unsigned char* pshdrs
)
5787 unsigned int sh_type
= shdr
.get_sh_type();
5788 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
5791 // Ignore empty section.
5792 off_t sh_size
= shdr
.get_sh_size();
5796 // Ignore reloc section with unexpected symbol table. The
5797 // error will be reported in the final link.
5798 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
5801 unsigned int reloc_size
;
5802 if (sh_type
== elfcpp::SHT_REL
)
5803 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
5805 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
5807 // Ignore reloc section with unexpected entsize or uneven size.
5808 // The error will be reported in the final link.
5809 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
5812 // Ignore reloc section with bad info. This error will be
5813 // reported in the final link.
5814 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
5815 if (index
>= this->shnum())
5818 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5819 const elfcpp::Shdr
<32, big_endian
> text_shdr(pshdrs
+ index
* shdr_size
);
5820 return this->section_is_scannable(text_shdr
, index
,
5821 out_sections
[index
], symtab
);
5824 // Return the output address of either a plain input section or a relaxed
5825 // input section. SHNDX is the section index. We define and use this
5826 // instead of calling Output_section::output_address because that is slow
5827 // for large output.
5829 template<bool big_endian
>
5831 Arm_relobj
<big_endian
>::simple_input_section_output_address(
5835 if (this->is_output_section_offset_invalid(shndx
))
5837 const Output_relaxed_input_section
* poris
=
5838 os
->find_relaxed_input_section(this, shndx
);
5839 // We do not handle merged sections here.
5840 gold_assert(poris
!= NULL
);
5841 return poris
->address();
5844 return os
->address() + this->get_output_section_offset(shndx
);
5847 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5848 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5850 template<bool big_endian
>
5852 Arm_relobj
<big_endian
>::section_needs_cortex_a8_stub_scanning(
5853 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5856 const Symbol_table
* symtab
)
5858 if (!this->section_is_scannable(shdr
, shndx
, os
, symtab
))
5861 // If the section does not cross any 4K-boundaries, it does not need to
5863 Arm_address address
= this->simple_input_section_output_address(shndx
, os
);
5864 if ((address
& ~0xfffU
) == ((address
+ shdr
.get_sh_size() - 1) & ~0xfffU
))
5870 // Scan a section for Cortex-A8 workaround.
5872 template<bool big_endian
>
5874 Arm_relobj
<big_endian
>::scan_section_for_cortex_a8_erratum(
5875 const elfcpp::Shdr
<32, big_endian
>& shdr
,
5878 Target_arm
<big_endian
>* arm_target
)
5880 // Look for the first mapping symbol in this section. It should be
5882 Mapping_symbol_position
section_start(shndx
, 0);
5883 typename
Mapping_symbols_info::const_iterator p
=
5884 this->mapping_symbols_info_
.lower_bound(section_start
);
5886 // There are no mapping symbols for this section. Treat it as a data-only
5887 // section. Issue a warning if section is marked as containing
5889 if (p
== this->mapping_symbols_info_
.end() || p
->first
.first
!= shndx
)
5891 if ((this->section_flags(shndx
) & elfcpp::SHF_EXECINSTR
) != 0)
5892 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
5893 "erratum because it has no mapping symbols."),
5894 shndx
, this->name().c_str());
5898 Arm_address output_address
=
5899 this->simple_input_section_output_address(shndx
, os
);
5901 // Get the section contents.
5902 section_size_type input_view_size
= 0;
5903 const unsigned char* input_view
=
5904 this->section_contents(shndx
, &input_view_size
, false);
5906 // We need to go through the mapping symbols to determine what to
5907 // scan. There are two reasons. First, we should look at THUMB code and
5908 // THUMB code only. Second, we only want to look at the 4K-page boundary
5909 // to speed up the scanning.
5911 while (p
!= this->mapping_symbols_info_
.end()
5912 && p
->first
.first
== shndx
)
5914 typename
Mapping_symbols_info::const_iterator next
=
5915 this->mapping_symbols_info_
.upper_bound(p
->first
);
5917 // Only scan part of a section with THUMB code.
5918 if (p
->second
== 't')
5920 // Determine the end of this range.
5921 section_size_type span_start
=
5922 convert_to_section_size_type(p
->first
.second
);
5923 section_size_type span_end
;
5924 if (next
!= this->mapping_symbols_info_
.end()
5925 && next
->first
.first
== shndx
)
5926 span_end
= convert_to_section_size_type(next
->first
.second
);
5928 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
5930 if (((span_start
+ output_address
) & ~0xfffUL
)
5931 != ((span_end
+ output_address
- 1) & ~0xfffUL
))
5933 arm_target
->scan_span_for_cortex_a8_erratum(this, shndx
,
5934 span_start
, span_end
,
5944 // Scan relocations for stub generation.
5946 template<bool big_endian
>
5948 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
5949 Target_arm
<big_endian
>* arm_target
,
5950 const Symbol_table
* symtab
,
5951 const Layout
* layout
)
5953 unsigned int shnum
= this->shnum();
5954 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
5956 // Read the section headers.
5957 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
5961 // To speed up processing, we set up hash tables for fast lookup of
5962 // input offsets to output addresses.
5963 this->initialize_input_to_output_maps();
5965 const Relobj::Output_sections
& out_sections(this->output_sections());
5967 Relocate_info
<32, big_endian
> relinfo
;
5968 relinfo
.symtab
= symtab
;
5969 relinfo
.layout
= layout
;
5970 relinfo
.object
= this;
5972 // Do relocation stubs scanning.
5973 const unsigned char* p
= pshdrs
+ shdr_size
;
5974 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
5976 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
5977 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
,
5980 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
5981 Arm_address output_offset
= this->get_output_section_offset(index
);
5982 Arm_address output_address
;
5983 if (output_offset
!= invalid_address
)
5984 output_address
= out_sections
[index
]->address() + output_offset
;
5987 // Currently this only happens for a relaxed section.
5988 const Output_relaxed_input_section
* poris
=
5989 out_sections
[index
]->find_relaxed_input_section(this, index
);
5990 gold_assert(poris
!= NULL
);
5991 output_address
= poris
->address();
5994 // Get the relocations.
5995 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
5999 // Get the section contents. This does work for the case in which
6000 // we modify the contents of an input section. We need to pass the
6001 // output view under such circumstances.
6002 section_size_type input_view_size
= 0;
6003 const unsigned char* input_view
=
6004 this->section_contents(index
, &input_view_size
, false);
6006 relinfo
.reloc_shndx
= i
;
6007 relinfo
.data_shndx
= index
;
6008 unsigned int sh_type
= shdr
.get_sh_type();
6009 unsigned int reloc_size
;
6010 if (sh_type
== elfcpp::SHT_REL
)
6011 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
6013 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
6015 Output_section
* os
= out_sections
[index
];
6016 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
6017 shdr
.get_sh_size() / reloc_size
,
6019 output_offset
== invalid_address
,
6020 input_view
, output_address
,
6025 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6026 // after its relocation section, if there is one, is processed for
6027 // relocation stubs. Merging this loop with the one above would have been
6028 // complicated since we would have had to make sure that relocation stub
6029 // scanning is done first.
6030 if (arm_target
->fix_cortex_a8())
6032 const unsigned char* p
= pshdrs
+ shdr_size
;
6033 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
6035 const elfcpp::Shdr
<32, big_endian
> shdr(p
);
6036 if (this->section_needs_cortex_a8_stub_scanning(shdr
, i
,
6039 this->scan_section_for_cortex_a8_erratum(shdr
, i
, out_sections
[i
],
6044 // After we've done the relocations, we release the hash tables,
6045 // since we no longer need them.
6046 this->free_input_to_output_maps();
6049 // Count the local symbols. The ARM backend needs to know if a symbol
6050 // is a THUMB function or not. For global symbols, it is easy because
6051 // the Symbol object keeps the ELF symbol type. For local symbol it is
6052 // harder because we cannot access this information. So we override the
6053 // do_count_local_symbol in parent and scan local symbols to mark
6054 // THUMB functions. This is not the most efficient way but I do not want to
6055 // slow down other ports by calling a per symbol targer hook inside
6056 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6058 template<bool big_endian
>
6060 Arm_relobj
<big_endian
>::do_count_local_symbols(
6061 Stringpool_template
<char>* pool
,
6062 Stringpool_template
<char>* dynpool
)
6064 // We need to fix-up the values of any local symbols whose type are
6067 // Ask parent to count the local symbols.
6068 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
6069 const unsigned int loccount
= this->local_symbol_count();
6073 // Intialize the thumb function bit-vector.
6074 std::vector
<bool> empty_vector(loccount
, false);
6075 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
6077 // Read the symbol table section header.
6078 const unsigned int symtab_shndx
= this->symtab_shndx();
6079 elfcpp::Shdr
<32, big_endian
>
6080 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6081 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6083 // Read the local symbols.
6084 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
6085 gold_assert(loccount
== symtabshdr
.get_sh_info());
6086 off_t locsize
= loccount
* sym_size
;
6087 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6088 locsize
, true, true);
6090 // For mapping symbol processing, we need to read the symbol names.
6091 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
6092 if (strtab_shndx
>= this->shnum())
6094 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
6098 elfcpp::Shdr
<32, big_endian
>
6099 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
6100 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
6102 this->error(_("symbol table name section has wrong type: %u"),
6103 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
6106 const char* pnames
=
6107 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
6108 strtabshdr
.get_sh_size(),
6111 // Loop over the local symbols and mark any local symbols pointing
6112 // to THUMB functions.
6114 // Skip the first dummy symbol.
6116 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
6117 this->local_values();
6118 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
6120 elfcpp::Sym
<32, big_endian
> sym(psyms
);
6121 elfcpp::STT st_type
= sym
.get_st_type();
6122 Symbol_value
<32>& lv((*plocal_values
)[i
]);
6123 Arm_address input_value
= lv
.input_value();
6125 // Check to see if this is a mapping symbol.
6126 const char* sym_name
= pnames
+ sym
.get_st_name();
6127 if (Target_arm
<big_endian
>::is_mapping_symbol_name(sym_name
))
6130 unsigned int input_shndx
=
6131 this->adjust_sym_shndx(i
, sym
.get_st_shndx(), &is_ordinary
);
6132 gold_assert(is_ordinary
);
6134 // Strip of LSB in case this is a THUMB symbol.
6135 Mapping_symbol_position
msp(input_shndx
, input_value
& ~1U);
6136 this->mapping_symbols_info_
[msp
] = sym_name
[1];
6139 if (st_type
== elfcpp::STT_ARM_TFUNC
6140 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
6142 // This is a THUMB function. Mark this and canonicalize the
6143 // symbol value by setting LSB.
6144 this->local_symbol_is_thumb_function_
[i
] = true;
6145 if ((input_value
& 1) == 0)
6146 lv
.set_input_value(input_value
| 1);
6151 // Relocate sections.
6152 template<bool big_endian
>
6154 Arm_relobj
<big_endian
>::do_relocate_sections(
6155 const Symbol_table
* symtab
,
6156 const Layout
* layout
,
6157 const unsigned char* pshdrs
,
6158 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
6160 // Call parent to relocate sections.
6161 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
6164 // We do not generate stubs if doing a relocatable link.
6165 if (parameters
->options().relocatable())
6168 // Relocate stub tables.
6169 unsigned int shnum
= this->shnum();
6171 Target_arm
<big_endian
>* arm_target
=
6172 Target_arm
<big_endian
>::default_target();
6174 Relocate_info
<32, big_endian
> relinfo
;
6175 relinfo
.symtab
= symtab
;
6176 relinfo
.layout
= layout
;
6177 relinfo
.object
= this;
6179 for (unsigned int i
= 1; i
< shnum
; ++i
)
6181 Arm_input_section
<big_endian
>* arm_input_section
=
6182 arm_target
->find_arm_input_section(this, i
);
6184 if (arm_input_section
!= NULL
6185 && arm_input_section
->is_stub_table_owner()
6186 && !arm_input_section
->stub_table()->empty())
6188 // We cannot discard a section if it owns a stub table.
6189 Output_section
* os
= this->output_section(i
);
6190 gold_assert(os
!= NULL
);
6192 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
6193 relinfo
.reloc_shdr
= NULL
;
6194 relinfo
.data_shndx
= i
;
6195 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
6197 gold_assert((*pviews
)[i
].view
!= NULL
);
6199 // We are passed the output section view. Adjust it to cover the
6201 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
6202 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
6203 && ((stub_table
->address() + stub_table
->data_size())
6204 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
6206 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
6207 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
6208 Arm_address address
= stub_table
->address();
6209 section_size_type view_size
= stub_table
->data_size();
6211 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
6215 // Apply Cortex A8 workaround if applicable.
6216 if (this->section_has_cortex_a8_workaround(i
))
6218 unsigned char* view
= (*pviews
)[i
].view
;
6219 Arm_address view_address
= (*pviews
)[i
].address
;
6220 section_size_type view_size
= (*pviews
)[i
].view_size
;
6221 Stub_table
<big_endian
>* stub_table
= this->stub_tables_
[i
];
6223 // Adjust view to cover section.
6224 Output_section
* os
= this->output_section(i
);
6225 gold_assert(os
!= NULL
);
6226 Arm_address section_address
=
6227 this->simple_input_section_output_address(i
, os
);
6228 uint64_t section_size
= this->section_size(i
);
6230 gold_assert(section_address
>= view_address
6231 && ((section_address
+ section_size
)
6232 <= (view_address
+ view_size
)));
6234 unsigned char* section_view
= view
+ (section_address
- view_address
);
6236 // Apply the Cortex-A8 workaround to the output address range
6237 // corresponding to this input section.
6238 stub_table
->apply_cortex_a8_workaround_to_address_range(
6247 // Find the linked text section of an EXIDX section by looking the the first
6248 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6249 // must be linked to to its associated code section via the sh_link field of
6250 // its section header. However, some tools are broken and the link is not
6251 // always set. LD just drops such an EXIDX section silently, causing the
6252 // associated code not unwindabled. Here we try a little bit harder to
6253 // discover the linked code section.
6255 // PSHDR points to the section header of a relocation section of an EXIDX
6256 // section. If we can find a linked text section, return true and
6257 // store the text section index in the location PSHNDX. Otherwise
6260 template<bool big_endian
>
6262 Arm_relobj
<big_endian
>::find_linked_text_section(
6263 const unsigned char* pshdr
,
6264 const unsigned char* psyms
,
6265 unsigned int* pshndx
)
6267 elfcpp::Shdr
<32, big_endian
> shdr(pshdr
);
6269 // If there is no relocation, we cannot find the linked text section.
6271 if (shdr
.get_sh_type() == elfcpp::SHT_REL
)
6272 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
6274 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
6275 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
6277 // Get the relocations.
6278 const unsigned char* prelocs
=
6279 this->get_view(shdr
.get_sh_offset(), shdr
.get_sh_size(), true, false);
6281 // Find the REL31 relocation for the first word of the first EXIDX entry.
6282 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6284 Arm_address r_offset
;
6285 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
;
6286 if (shdr
.get_sh_type() == elfcpp::SHT_REL
)
6288 typename
elfcpp::Rel
<32, big_endian
> reloc(prelocs
);
6289 r_info
= reloc
.get_r_info();
6290 r_offset
= reloc
.get_r_offset();
6294 typename
elfcpp::Rela
<32, big_endian
> reloc(prelocs
);
6295 r_info
= reloc
.get_r_info();
6296 r_offset
= reloc
.get_r_offset();
6299 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
6300 if (r_type
!= elfcpp::R_ARM_PREL31
&& r_type
!= elfcpp::R_ARM_SBREL31
)
6303 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6305 || r_sym
>= this->local_symbol_count()
6309 // This is the relocation for the first word of the first EXIDX entry.
6310 // We expect to see a local section symbol.
6311 const int sym_size
= elfcpp::Elf_sizes
<32>::sym_size
;
6312 elfcpp::Sym
<32, big_endian
> sym(psyms
+ r_sym
* sym_size
);
6313 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
6317 this->adjust_sym_shndx(r_sym
, sym
.get_st_shndx(), &is_ordinary
);
6318 gold_assert(is_ordinary
);
6328 // Make an EXIDX input section object for an EXIDX section whose index is
6329 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6330 // is the section index of the linked text section.
6332 template<bool big_endian
>
6334 Arm_relobj
<big_endian
>::make_exidx_input_section(
6336 const elfcpp::Shdr
<32, big_endian
>& shdr
,
6337 unsigned int text_shndx
)
6339 // Issue an error and ignore this EXIDX section if it points to a text
6340 // section already has an EXIDX section.
6341 if (this->exidx_section_map_
[text_shndx
] != NULL
)
6343 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6345 shndx
, this->exidx_section_map_
[text_shndx
]->shndx(),
6346 text_shndx
, this->name().c_str());
6350 // Create an Arm_exidx_input_section object for this EXIDX section.
6351 Arm_exidx_input_section
* exidx_input_section
=
6352 new Arm_exidx_input_section(this, shndx
, text_shndx
, shdr
.get_sh_size(),
6353 shdr
.get_sh_addralign());
6354 this->exidx_section_map_
[text_shndx
] = exidx_input_section
;
6356 // Also map the EXIDX section index to this.
6357 gold_assert(this->exidx_section_map_
[shndx
] == NULL
);
6358 this->exidx_section_map_
[shndx
] = exidx_input_section
;
6361 // Read the symbol information.
6363 template<bool big_endian
>
6365 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
6367 // Call parent class to read symbol information.
6368 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
6370 // If this input file is a binary file, it has no processor
6371 // specific flags and attributes section.
6372 Input_file::Format format
= this->input_file()->format();
6373 if (format
!= Input_file::FORMAT_ELF
)
6375 gold_assert(format
== Input_file::FORMAT_BINARY
);
6376 this->merge_flags_and_attributes_
= false;
6380 // Read processor-specific flags in ELF file header.
6381 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
6382 elfcpp::Elf_sizes
<32>::ehdr_size
,
6384 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
6385 this->processor_specific_flags_
= ehdr
.get_e_flags();
6387 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6389 std::vector
<unsigned int> deferred_exidx_sections
;
6390 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6391 const unsigned char* pshdrs
= sd
->section_headers
->data();
6392 const unsigned char *ps
= pshdrs
+ shdr_size
;
6393 bool must_merge_flags_and_attributes
= false;
6394 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
6396 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6398 // Sometimes an object has no contents except the section name string
6399 // table and an empty symbol table with the undefined symbol. We
6400 // don't want to merge processor-specific flags from such an object.
6401 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
6403 // Symbol table is not empty.
6404 const elfcpp::Elf_types
<32>::Elf_WXword sym_size
=
6405 elfcpp::Elf_sizes
<32>::sym_size
;
6406 if (shdr
.get_sh_size() > sym_size
)
6407 must_merge_flags_and_attributes
= true;
6409 else if (shdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
6410 // If this is neither an empty symbol table nor a string table,
6412 must_merge_flags_and_attributes
= true;
6414 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
6416 gold_assert(this->attributes_section_data_
== NULL
);
6417 section_offset_type section_offset
= shdr
.get_sh_offset();
6418 section_size_type section_size
=
6419 convert_to_section_size_type(shdr
.get_sh_size());
6420 File_view
* view
= this->get_lasting_view(section_offset
,
6421 section_size
, true, false);
6422 this->attributes_section_data_
=
6423 new Attributes_section_data(view
->data(), section_size
);
6425 else if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
6427 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
6428 if (text_shndx
>= this->shnum())
6429 gold_error(_("EXIDX section %u linked to invalid section %u"),
6431 else if (text_shndx
== elfcpp::SHN_UNDEF
)
6432 deferred_exidx_sections
.push_back(i
);
6434 this->make_exidx_input_section(i
, shdr
, text_shndx
);
6439 if (!must_merge_flags_and_attributes
)
6441 this->merge_flags_and_attributes_
= false;
6445 // Some tools are broken and they do not set the link of EXIDX sections.
6446 // We look at the first relocation to figure out the linked sections.
6447 if (!deferred_exidx_sections
.empty())
6449 // We need to go over the section headers again to find the mapping
6450 // from sections being relocated to their relocation sections. This is
6451 // a bit inefficient as we could do that in the loop above. However,
6452 // we do not expect any deferred EXIDX sections normally. So we do not
6453 // want to slow down the most common path.
6454 typedef Unordered_map
<unsigned int, unsigned int> Reloc_map
;
6455 Reloc_map reloc_map
;
6456 ps
= pshdrs
+ shdr_size
;
6457 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
6459 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6460 elfcpp::Elf_Word sh_type
= shdr
.get_sh_type();
6461 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
6463 unsigned int info_shndx
= this->adjust_shndx(shdr
.get_sh_info());
6464 if (info_shndx
>= this->shnum())
6465 gold_error(_("relocation section %u has invalid info %u"),
6467 Reloc_map::value_type
value(info_shndx
, i
);
6468 std::pair
<Reloc_map::iterator
, bool> result
=
6469 reloc_map
.insert(value
);
6471 gold_error(_("section %u has multiple relocation sections "
6473 info_shndx
, i
, reloc_map
[info_shndx
]);
6477 // Read the symbol table section header.
6478 const unsigned int symtab_shndx
= this->symtab_shndx();
6479 elfcpp::Shdr
<32, big_endian
>
6480 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6481 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6483 // Read the local symbols.
6484 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
6485 const unsigned int loccount
= this->local_symbol_count();
6486 gold_assert(loccount
== symtabshdr
.get_sh_info());
6487 off_t locsize
= loccount
* sym_size
;
6488 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6489 locsize
, true, true);
6491 // Process the deferred EXIDX sections.
6492 for(unsigned int i
= 0; i
< deferred_exidx_sections
.size(); ++i
)
6494 unsigned int shndx
= deferred_exidx_sections
[i
];
6495 elfcpp::Shdr
<32, big_endian
> shdr(pshdrs
+ shndx
* shdr_size
);
6496 unsigned int text_shndx
;
6497 Reloc_map::const_iterator it
= reloc_map
.find(shndx
);
6498 if (it
!= reloc_map
.end()
6499 && find_linked_text_section(pshdrs
+ it
->second
* shdr_size
,
6500 psyms
, &text_shndx
))
6501 this->make_exidx_input_section(shndx
, shdr
, text_shndx
);
6503 gold_error(_("EXIDX section %u has no linked text section."),
6509 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6510 // sections for unwinding. These sections are referenced implicitly by
6511 // text sections linked in the section headers. If we ignore these implict
6512 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6513 // will be garbage-collected incorrectly. Hence we override the same function
6514 // in the base class to handle these implicit references.
6516 template<bool big_endian
>
6518 Arm_relobj
<big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
6520 Read_relocs_data
* rd
)
6522 // First, call base class method to process relocations in this object.
6523 Sized_relobj
<32, big_endian
>::do_gc_process_relocs(symtab
, layout
, rd
);
6525 // If --gc-sections is not specified, there is nothing more to do.
6526 // This happens when --icf is used but --gc-sections is not.
6527 if (!parameters
->options().gc_sections())
6530 unsigned int shnum
= this->shnum();
6531 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6532 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
6536 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6537 // to these from the linked text sections.
6538 const unsigned char* ps
= pshdrs
+ shdr_size
;
6539 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= shdr_size
)
6541 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6542 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_EXIDX
)
6544 // Found an .ARM.exidx section, add it to the set of reachable
6545 // sections from its linked text section.
6546 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_link());
6547 symtab
->gc()->add_reference(this, text_shndx
, this, i
);
6552 // Update output local symbol count. Owing to EXIDX entry merging, some local
6553 // symbols will be removed in output. Adjust output local symbol count
6554 // accordingly. We can only changed the static output local symbol count. It
6555 // is too late to change the dynamic symbols.
6557 template<bool big_endian
>
6559 Arm_relobj
<big_endian
>::update_output_local_symbol_count()
6561 // Caller should check that this needs updating. We want caller checking
6562 // because output_local_symbol_count_needs_update() is most likely inlined.
6563 gold_assert(this->output_local_symbol_count_needs_update_
);
6565 gold_assert(this->symtab_shndx() != -1U);
6566 if (this->symtab_shndx() == 0)
6568 // This object has no symbols. Weird but legal.
6572 // Read the symbol table section header.
6573 const unsigned int symtab_shndx
= this->symtab_shndx();
6574 elfcpp::Shdr
<32, big_endian
>
6575 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6576 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6578 // Read the local symbols.
6579 const int sym_size
= elfcpp::Elf_sizes
<32>::sym_size
;
6580 const unsigned int loccount
= this->local_symbol_count();
6581 gold_assert(loccount
== symtabshdr
.get_sh_info());
6582 off_t locsize
= loccount
* sym_size
;
6583 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6584 locsize
, true, true);
6586 // Loop over the local symbols.
6588 typedef typename Sized_relobj
<32, big_endian
>::Output_sections
6590 const Output_sections
& out_sections(this->output_sections());
6591 unsigned int shnum
= this->shnum();
6592 unsigned int count
= 0;
6593 // Skip the first, dummy, symbol.
6595 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
6597 elfcpp::Sym
<32, big_endian
> sym(psyms
);
6599 Symbol_value
<32>& lv((*this->local_values())[i
]);
6601 // This local symbol was already discarded by do_count_local_symbols.
6602 if (lv
.is_output_symtab_index_set() && !lv
.has_output_symtab_entry())
6606 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
6611 Output_section
* os
= out_sections
[shndx
];
6613 // This local symbol no longer has an output section. Discard it.
6616 lv
.set_no_output_symtab_entry();
6620 // Currently we only discard parts of EXIDX input sections.
6621 // We explicitly check for a merged EXIDX input section to avoid
6622 // calling Output_section_data::output_offset unless necessary.
6623 if ((this->get_output_section_offset(shndx
) == invalid_address
)
6624 && (this->exidx_input_section_by_shndx(shndx
) != NULL
))
6626 section_offset_type output_offset
=
6627 os
->output_offset(this, shndx
, lv
.input_value());
6628 if (output_offset
== -1)
6630 // This symbol is defined in a part of an EXIDX input section
6631 // that is discarded due to entry merging.
6632 lv
.set_no_output_symtab_entry();
6641 this->set_output_local_symbol_count(count
);
6642 this->output_local_symbol_count_needs_update_
= false;
6645 // Arm_dynobj methods.
6647 // Read the symbol information.
6649 template<bool big_endian
>
6651 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
6653 // Call parent class to read symbol information.
6654 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
6656 // Read processor-specific flags in ELF file header.
6657 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
6658 elfcpp::Elf_sizes
<32>::ehdr_size
,
6660 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
6661 this->processor_specific_flags_
= ehdr
.get_e_flags();
6663 // Read the attributes section if there is one.
6664 // We read from the end because gas seems to put it near the end of
6665 // the section headers.
6666 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
6667 const unsigned char *ps
=
6668 sd
->section_headers
->data() + shdr_size
* (this->shnum() - 1);
6669 for (unsigned int i
= this->shnum(); i
> 0; --i
, ps
-= shdr_size
)
6671 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
6672 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
6674 section_offset_type section_offset
= shdr
.get_sh_offset();
6675 section_size_type section_size
=
6676 convert_to_section_size_type(shdr
.get_sh_size());
6677 File_view
* view
= this->get_lasting_view(section_offset
,
6678 section_size
, true, false);
6679 this->attributes_section_data_
=
6680 new Attributes_section_data(view
->data(), section_size
);
6686 // Stub_addend_reader methods.
6688 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6690 template<bool big_endian
>
6691 elfcpp::Elf_types
<32>::Elf_Swxword
6692 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
6693 unsigned int r_type
,
6694 const unsigned char* view
,
6695 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
6697 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
6701 case elfcpp::R_ARM_CALL
:
6702 case elfcpp::R_ARM_JUMP24
:
6703 case elfcpp::R_ARM_PLT32
:
6705 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
6706 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6707 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
6708 return utils::sign_extend
<26>(val
<< 2);
6711 case elfcpp::R_ARM_THM_CALL
:
6712 case elfcpp::R_ARM_THM_JUMP24
:
6713 case elfcpp::R_ARM_THM_XPC22
:
6715 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
6716 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6717 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
6718 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
6719 return RelocFuncs::thumb32_branch_offset(upper_insn
, lower_insn
);
6722 case elfcpp::R_ARM_THM_JUMP19
:
6724 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
6725 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
6726 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
6727 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
6728 return RelocFuncs::thumb32_cond_branch_offset(upper_insn
, lower_insn
);
6736 // Arm_output_data_got methods.
6738 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6739 // The first one is initialized to be 1, which is the module index for
6740 // the main executable and the second one 0. A reloc of the type
6741 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6742 // be applied by gold. GSYM is a global symbol.
6744 template<bool big_endian
>
6746 Arm_output_data_got
<big_endian
>::add_tls_gd32_with_static_reloc(
6747 unsigned int got_type
,
6750 if (gsym
->has_got_offset(got_type
))
6753 // We are doing a static link. Just mark it as belong to module 1,
6755 unsigned int got_offset
= this->add_constant(1);
6756 gsym
->set_got_offset(got_type
, got_offset
);
6757 got_offset
= this->add_constant(0);
6758 this->static_relocs_
.push_back(Static_reloc(got_offset
,
6759 elfcpp::R_ARM_TLS_DTPOFF32
,
6763 // Same as the above but for a local symbol.
6765 template<bool big_endian
>
6767 Arm_output_data_got
<big_endian
>::add_tls_gd32_with_static_reloc(
6768 unsigned int got_type
,
6769 Sized_relobj
<32, big_endian
>* object
,
6772 if (object
->local_has_got_offset(index
, got_type
))
6775 // We are doing a static link. Just mark it as belong to module 1,
6777 unsigned int got_offset
= this->add_constant(1);
6778 object
->set_local_got_offset(index
, got_type
, got_offset
);
6779 got_offset
= this->add_constant(0);
6780 this->static_relocs_
.push_back(Static_reloc(got_offset
,
6781 elfcpp::R_ARM_TLS_DTPOFF32
,
6785 template<bool big_endian
>
6787 Arm_output_data_got
<big_endian
>::do_write(Output_file
* of
)
6789 // Call parent to write out GOT.
6790 Output_data_got
<32, big_endian
>::do_write(of
);
6792 // We are done if there is no fix up.
6793 if (this->static_relocs_
.empty())
6796 gold_assert(parameters
->doing_static_link());
6798 const off_t offset
= this->offset();
6799 const section_size_type oview_size
=
6800 convert_to_section_size_type(this->data_size());
6801 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
6803 Output_segment
* tls_segment
= this->layout_
->tls_segment();
6804 gold_assert(tls_segment
!= NULL
);
6806 // The thread pointer $tp points to the TCB, which is followed by the
6807 // TLS. So we need to adjust $tp relative addressing by this amount.
6808 Arm_address aligned_tcb_size
=
6809 align_address(ARM_TCB_SIZE
, tls_segment
->maximum_alignment());
6811 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
6813 Static_reloc
& reloc(this->static_relocs_
[i
]);
6816 if (!reloc
.symbol_is_global())
6818 Sized_relobj
<32, big_endian
>* object
= reloc
.relobj();
6819 const Symbol_value
<32>* psymval
=
6820 reloc
.relobj()->local_symbol(reloc
.index());
6822 // We are doing static linking. Issue an error and skip this
6823 // relocation if the symbol is undefined or in a discarded_section.
6825 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
6826 if ((shndx
== elfcpp::SHN_UNDEF
)
6828 && shndx
!= elfcpp::SHN_UNDEF
6829 && !object
->is_section_included(shndx
)
6830 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
6832 gold_error(_("undefined or discarded local symbol %u from "
6833 " object %s in GOT"),
6834 reloc
.index(), reloc
.relobj()->name().c_str());
6838 value
= psymval
->value(object
, 0);
6842 const Symbol
* gsym
= reloc
.symbol();
6843 gold_assert(gsym
!= NULL
);
6844 if (gsym
->is_forwarder())
6845 gsym
= this->symbol_table_
->resolve_forwards(gsym
);
6847 // We are doing static linking. Issue an error and skip this
6848 // relocation if the symbol is undefined or in a discarded_section
6849 // unless it is a weakly_undefined symbol.
6850 if ((gsym
->is_defined_in_discarded_section()
6851 || gsym
->is_undefined())
6852 && !gsym
->is_weak_undefined())
6854 gold_error(_("undefined or discarded symbol %s in GOT"),
6859 if (!gsym
->is_weak_undefined())
6861 const Sized_symbol
<32>* sym
=
6862 static_cast<const Sized_symbol
<32>*>(gsym
);
6863 value
= sym
->value();
6869 unsigned got_offset
= reloc
.got_offset();
6870 gold_assert(got_offset
< oview_size
);
6872 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
6873 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
6875 switch (reloc
.r_type())
6877 case elfcpp::R_ARM_TLS_DTPOFF32
:
6880 case elfcpp::R_ARM_TLS_TPOFF32
:
6881 x
= value
+ aligned_tcb_size
;
6886 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
6889 of
->write_output_view(offset
, oview_size
, oview
);
6892 // A class to handle the PLT data.
6894 template<bool big_endian
>
6895 class Output_data_plt_arm
: public Output_section_data
6898 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
6901 Output_data_plt_arm(Layout
*, Output_data_space
*);
6903 // Add an entry to the PLT.
6905 add_entry(Symbol
* gsym
);
6907 // Return the .rel.plt section data.
6908 const Reloc_section
*
6910 { return this->rel_
; }
6914 do_adjust_output_section(Output_section
* os
);
6916 // Write to a map file.
6918 do_print_to_mapfile(Mapfile
* mapfile
) const
6919 { mapfile
->print_output_data(this, _("** PLT")); }
6922 // Template for the first PLT entry.
6923 static const uint32_t first_plt_entry
[5];
6925 // Template for subsequent PLT entries.
6926 static const uint32_t plt_entry
[3];
6928 // Set the final size.
6930 set_final_data_size()
6932 this->set_data_size(sizeof(first_plt_entry
)
6933 + this->count_
* sizeof(plt_entry
));
6936 // Write out the PLT data.
6938 do_write(Output_file
*);
6940 // The reloc section.
6941 Reloc_section
* rel_
;
6942 // The .got.plt section.
6943 Output_data_space
* got_plt_
;
6944 // The number of PLT entries.
6945 unsigned int count_
;
6948 // Create the PLT section. The ordinary .got section is an argument,
6949 // since we need to refer to the start. We also create our own .got
6950 // section just for PLT entries.
6952 template<bool big_endian
>
6953 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
6954 Output_data_space
* got_plt
)
6955 : Output_section_data(4), got_plt_(got_plt
), count_(0)
6957 this->rel_
= new Reloc_section(false);
6958 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
6959 elfcpp::SHF_ALLOC
, this->rel_
, true, false,
6963 template<bool big_endian
>
6965 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
6970 // Add an entry to the PLT.
6972 template<bool big_endian
>
6974 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
6976 gold_assert(!gsym
->has_plt_offset());
6978 // Note that when setting the PLT offset we skip the initial
6979 // reserved PLT entry.
6980 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
6981 + sizeof(first_plt_entry
));
6985 section_offset_type got_offset
= this->got_plt_
->current_data_size();
6987 // Every PLT entry needs a GOT entry which points back to the PLT
6988 // entry (this will be changed by the dynamic linker, normally
6989 // lazily when the function is called).
6990 this->got_plt_
->set_current_data_size(got_offset
+ 4);
6992 // Every PLT entry needs a reloc.
6993 gsym
->set_needs_dynsym_entry();
6994 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
6997 // Note that we don't need to save the symbol. The contents of the
6998 // PLT are independent of which symbols are used. The symbols only
6999 // appear in the relocations.
7003 // FIXME: This is not very flexible. Right now this has only been tested
7004 // on armv5te. If we are to support additional architecture features like
7005 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7007 // The first entry in the PLT.
7008 template<bool big_endian
>
7009 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
7011 0xe52de004, // str lr, [sp, #-4]!
7012 0xe59fe004, // ldr lr, [pc, #4]
7013 0xe08fe00e, // add lr, pc, lr
7014 0xe5bef008, // ldr pc, [lr, #8]!
7015 0x00000000, // &GOT[0] - .
7018 // Subsequent entries in the PLT.
7020 template<bool big_endian
>
7021 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
7023 0xe28fc600, // add ip, pc, #0xNN00000
7024 0xe28cca00, // add ip, ip, #0xNN000
7025 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7028 // Write out the PLT. This uses the hand-coded instructions above,
7029 // and adjusts them as needed. This is all specified by the arm ELF
7030 // Processor Supplement.
7032 template<bool big_endian
>
7034 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
7036 const off_t offset
= this->offset();
7037 const section_size_type oview_size
=
7038 convert_to_section_size_type(this->data_size());
7039 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7041 const off_t got_file_offset
= this->got_plt_
->offset();
7042 const section_size_type got_size
=
7043 convert_to_section_size_type(this->got_plt_
->data_size());
7044 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
7046 unsigned char* pov
= oview
;
7048 Arm_address plt_address
= this->address();
7049 Arm_address got_address
= this->got_plt_
->address();
7051 // Write first PLT entry. All but the last word are constants.
7052 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
7053 / sizeof(plt_entry
[0]));
7054 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
7055 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
7056 // Last word in first PLT entry is &GOT[0] - .
7057 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
7058 got_address
- (plt_address
+ 16));
7059 pov
+= sizeof(first_plt_entry
);
7061 unsigned char* got_pov
= got_view
;
7063 memset(got_pov
, 0, 12);
7066 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
7067 unsigned int plt_offset
= sizeof(first_plt_entry
);
7068 unsigned int plt_rel_offset
= 0;
7069 unsigned int got_offset
= 12;
7070 const unsigned int count
= this->count_
;
7071 for (unsigned int i
= 0;
7074 pov
+= sizeof(plt_entry
),
7076 plt_offset
+= sizeof(plt_entry
),
7077 plt_rel_offset
+= rel_size
,
7080 // Set and adjust the PLT entry itself.
7081 int32_t offset
= ((got_address
+ got_offset
)
7082 - (plt_address
+ plt_offset
+ 8));
7084 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
7085 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
7086 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
7087 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
7088 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
7089 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
7090 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
7092 // Set the entry in the GOT.
7093 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
7096 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
7097 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
7099 of
->write_output_view(offset
, oview_size
, oview
);
7100 of
->write_output_view(got_file_offset
, got_size
, got_view
);
7103 // Create a PLT entry for a global symbol.
7105 template<bool big_endian
>
7107 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
7110 if (gsym
->has_plt_offset())
7113 if (this->plt_
== NULL
)
7115 // Create the GOT sections first.
7116 this->got_section(symtab
, layout
);
7118 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
7119 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
7121 | elfcpp::SHF_EXECINSTR
),
7122 this->plt_
, false, false, false, false);
7124 this->plt_
->add_entry(gsym
);
7127 // Get the section to use for TLS_DESC relocations.
7129 template<bool big_endian
>
7130 typename Target_arm
<big_endian
>::Reloc_section
*
7131 Target_arm
<big_endian
>::rel_tls_desc_section(Layout
* layout
) const
7133 return this->plt_section()->rel_tls_desc(layout
);
7136 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7138 template<bool big_endian
>
7140 Target_arm
<big_endian
>::define_tls_base_symbol(
7141 Symbol_table
* symtab
,
7144 if (this->tls_base_symbol_defined_
)
7147 Output_segment
* tls_segment
= layout
->tls_segment();
7148 if (tls_segment
!= NULL
)
7150 bool is_exec
= parameters
->options().output_is_executable();
7151 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
7152 Symbol_table::PREDEFINED
,
7156 elfcpp::STV_HIDDEN
, 0,
7158 ? Symbol::SEGMENT_END
7159 : Symbol::SEGMENT_START
),
7162 this->tls_base_symbol_defined_
= true;
7165 // Create a GOT entry for the TLS module index.
7167 template<bool big_endian
>
7169 Target_arm
<big_endian
>::got_mod_index_entry(
7170 Symbol_table
* symtab
,
7172 Sized_relobj
<32, big_endian
>* object
)
7174 if (this->got_mod_index_offset_
== -1U)
7176 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
7177 Arm_output_data_got
<big_endian
>* got
= this->got_section(symtab
, layout
);
7178 unsigned int got_offset
;
7179 if (!parameters
->doing_static_link())
7181 got_offset
= got
->add_constant(0);
7182 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
7183 rel_dyn
->add_local(object
, 0, elfcpp::R_ARM_TLS_DTPMOD32
, got
,
7188 // We are doing a static link. Just mark it as belong to module 1,
7190 got_offset
= got
->add_constant(1);
7193 got
->add_constant(0);
7194 this->got_mod_index_offset_
= got_offset
;
7196 return this->got_mod_index_offset_
;
7199 // Optimize the TLS relocation type based on what we know about the
7200 // symbol. IS_FINAL is true if the final address of this symbol is
7201 // known at link time.
7203 template<bool big_endian
>
7204 tls::Tls_optimization
7205 Target_arm
<big_endian
>::optimize_tls_reloc(bool, int)
7207 // FIXME: Currently we do not do any TLS optimization.
7208 return tls::TLSOPT_NONE
;
7211 // Report an unsupported relocation against a local symbol.
7213 template<bool big_endian
>
7215 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
7216 Sized_relobj
<32, big_endian
>* object
,
7217 unsigned int r_type
)
7219 gold_error(_("%s: unsupported reloc %u against local symbol"),
7220 object
->name().c_str(), r_type
);
7223 // We are about to emit a dynamic relocation of type R_TYPE. If the
7224 // dynamic linker does not support it, issue an error. The GNU linker
7225 // only issues a non-PIC error for an allocated read-only section.
7226 // Here we know the section is allocated, but we don't know that it is
7227 // read-only. But we check for all the relocation types which the
7228 // glibc dynamic linker supports, so it seems appropriate to issue an
7229 // error even if the section is not read-only.
7231 template<bool big_endian
>
7233 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
7234 unsigned int r_type
)
7238 // These are the relocation types supported by glibc for ARM.
7239 case elfcpp::R_ARM_RELATIVE
:
7240 case elfcpp::R_ARM_COPY
:
7241 case elfcpp::R_ARM_GLOB_DAT
:
7242 case elfcpp::R_ARM_JUMP_SLOT
:
7243 case elfcpp::R_ARM_ABS32
:
7244 case elfcpp::R_ARM_ABS32_NOI
:
7245 case elfcpp::R_ARM_PC24
:
7246 // FIXME: The following 3 types are not supported by Android's dynamic
7248 case elfcpp::R_ARM_TLS_DTPMOD32
:
7249 case elfcpp::R_ARM_TLS_DTPOFF32
:
7250 case elfcpp::R_ARM_TLS_TPOFF32
:
7255 // This prevents us from issuing more than one error per reloc
7256 // section. But we can still wind up issuing more than one
7257 // error per object file.
7258 if (this->issued_non_pic_error_
)
7260 const Arm_reloc_property
* reloc_property
=
7261 arm_reloc_property_table
->get_reloc_property(r_type
);
7262 gold_assert(reloc_property
!= NULL
);
7263 object
->error(_("requires unsupported dynamic reloc %s; "
7264 "recompile with -fPIC"),
7265 reloc_property
->name().c_str());
7266 this->issued_non_pic_error_
= true;
7270 case elfcpp::R_ARM_NONE
:
7275 // Scan a relocation for a local symbol.
7276 // FIXME: This only handles a subset of relocation types used by Android
7277 // on ARM v5te devices.
7279 template<bool big_endian
>
7281 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
7284 Sized_relobj
<32, big_endian
>* object
,
7285 unsigned int data_shndx
,
7286 Output_section
* output_section
,
7287 const elfcpp::Rel
<32, big_endian
>& reloc
,
7288 unsigned int r_type
,
7289 const elfcpp::Sym
<32, big_endian
>& lsym
)
7291 r_type
= get_real_reloc_type(r_type
);
7294 case elfcpp::R_ARM_NONE
:
7295 case elfcpp::R_ARM_V4BX
:
7296 case elfcpp::R_ARM_GNU_VTENTRY
:
7297 case elfcpp::R_ARM_GNU_VTINHERIT
:
7300 case elfcpp::R_ARM_ABS32
:
7301 case elfcpp::R_ARM_ABS32_NOI
:
7302 // If building a shared library (or a position-independent
7303 // executable), we need to create a dynamic relocation for
7304 // this location. The relocation applied at link time will
7305 // apply the link-time value, so we flag the location with
7306 // an R_ARM_RELATIVE relocation so the dynamic loader can
7307 // relocate it easily.
7308 if (parameters
->options().output_is_position_independent())
7310 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7311 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7312 // If we are to add more other reloc types than R_ARM_ABS32,
7313 // we need to add check_non_pic(object, r_type) here.
7314 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
7315 output_section
, data_shndx
,
7316 reloc
.get_r_offset());
7320 case elfcpp::R_ARM_ABS16
:
7321 case elfcpp::R_ARM_ABS12
:
7322 case elfcpp::R_ARM_THM_ABS5
:
7323 case elfcpp::R_ARM_ABS8
:
7324 case elfcpp::R_ARM_BASE_ABS
:
7325 case elfcpp::R_ARM_MOVW_ABS_NC
:
7326 case elfcpp::R_ARM_MOVT_ABS
:
7327 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7328 case elfcpp::R_ARM_THM_MOVT_ABS
:
7329 // If building a shared library (or a position-independent
7330 // executable), we need to create a dynamic relocation for
7331 // this location. Because the addend needs to remain in the
7332 // data section, we need to be careful not to apply this
7333 // relocation statically.
7334 if (parameters
->options().output_is_position_independent())
7336 check_non_pic(object
, r_type
);
7337 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7338 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7339 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
7340 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
7341 data_shndx
, reloc
.get_r_offset());
7344 gold_assert(lsym
.get_st_value() == 0);
7345 unsigned int shndx
= lsym
.get_st_shndx();
7347 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
7350 object
->error(_("section symbol %u has bad shndx %u"),
7353 rel_dyn
->add_local_section(object
, shndx
,
7354 r_type
, output_section
,
7355 data_shndx
, reloc
.get_r_offset());
7360 case elfcpp::R_ARM_PC24
:
7361 case elfcpp::R_ARM_REL32
:
7362 case elfcpp::R_ARM_LDR_PC_G0
:
7363 case elfcpp::R_ARM_SBREL32
:
7364 case elfcpp::R_ARM_THM_CALL
:
7365 case elfcpp::R_ARM_THM_PC8
:
7366 case elfcpp::R_ARM_BASE_PREL
:
7367 case elfcpp::R_ARM_PLT32
:
7368 case elfcpp::R_ARM_CALL
:
7369 case elfcpp::R_ARM_JUMP24
:
7370 case elfcpp::R_ARM_THM_JUMP24
:
7371 case elfcpp::R_ARM_LDR_SBREL_11_0_NC
:
7372 case elfcpp::R_ARM_ALU_SBREL_19_12_NC
:
7373 case elfcpp::R_ARM_ALU_SBREL_27_20_CK
:
7374 case elfcpp::R_ARM_SBREL31
:
7375 case elfcpp::R_ARM_PREL31
:
7376 case elfcpp::R_ARM_MOVW_PREL_NC
:
7377 case elfcpp::R_ARM_MOVT_PREL
:
7378 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7379 case elfcpp::R_ARM_THM_MOVT_PREL
:
7380 case elfcpp::R_ARM_THM_JUMP19
:
7381 case elfcpp::R_ARM_THM_JUMP6
:
7382 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7383 case elfcpp::R_ARM_THM_PC12
:
7384 case elfcpp::R_ARM_REL32_NOI
:
7385 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7386 case elfcpp::R_ARM_ALU_PC_G0
:
7387 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7388 case elfcpp::R_ARM_ALU_PC_G1
:
7389 case elfcpp::R_ARM_ALU_PC_G2
:
7390 case elfcpp::R_ARM_LDR_PC_G1
:
7391 case elfcpp::R_ARM_LDR_PC_G2
:
7392 case elfcpp::R_ARM_LDRS_PC_G0
:
7393 case elfcpp::R_ARM_LDRS_PC_G1
:
7394 case elfcpp::R_ARM_LDRS_PC_G2
:
7395 case elfcpp::R_ARM_LDC_PC_G0
:
7396 case elfcpp::R_ARM_LDC_PC_G1
:
7397 case elfcpp::R_ARM_LDC_PC_G2
:
7398 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7399 case elfcpp::R_ARM_ALU_SB_G0
:
7400 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7401 case elfcpp::R_ARM_ALU_SB_G1
:
7402 case elfcpp::R_ARM_ALU_SB_G2
:
7403 case elfcpp::R_ARM_LDR_SB_G0
:
7404 case elfcpp::R_ARM_LDR_SB_G1
:
7405 case elfcpp::R_ARM_LDR_SB_G2
:
7406 case elfcpp::R_ARM_LDRS_SB_G0
:
7407 case elfcpp::R_ARM_LDRS_SB_G1
:
7408 case elfcpp::R_ARM_LDRS_SB_G2
:
7409 case elfcpp::R_ARM_LDC_SB_G0
:
7410 case elfcpp::R_ARM_LDC_SB_G1
:
7411 case elfcpp::R_ARM_LDC_SB_G2
:
7412 case elfcpp::R_ARM_MOVW_BREL_NC
:
7413 case elfcpp::R_ARM_MOVT_BREL
:
7414 case elfcpp::R_ARM_MOVW_BREL
:
7415 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7416 case elfcpp::R_ARM_THM_MOVT_BREL
:
7417 case elfcpp::R_ARM_THM_MOVW_BREL
:
7418 case elfcpp::R_ARM_THM_JUMP11
:
7419 case elfcpp::R_ARM_THM_JUMP8
:
7420 // We don't need to do anything for a relative addressing relocation
7421 // against a local symbol if it does not reference the GOT.
7424 case elfcpp::R_ARM_GOTOFF32
:
7425 case elfcpp::R_ARM_GOTOFF12
:
7426 // We need a GOT section:
7427 target
->got_section(symtab
, layout
);
7430 case elfcpp::R_ARM_GOT_BREL
:
7431 case elfcpp::R_ARM_GOT_PREL
:
7433 // The symbol requires a GOT entry.
7434 Arm_output_data_got
<big_endian
>* got
=
7435 target
->got_section(symtab
, layout
);
7436 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7437 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
7439 // If we are generating a shared object, we need to add a
7440 // dynamic RELATIVE relocation for this symbol's GOT entry.
7441 if (parameters
->options().output_is_position_independent())
7443 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7444 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7445 rel_dyn
->add_local_relative(
7446 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
7447 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7453 case elfcpp::R_ARM_TARGET1
:
7454 case elfcpp::R_ARM_TARGET2
:
7455 // This should have been mapped to another type already.
7457 case elfcpp::R_ARM_COPY
:
7458 case elfcpp::R_ARM_GLOB_DAT
:
7459 case elfcpp::R_ARM_JUMP_SLOT
:
7460 case elfcpp::R_ARM_RELATIVE
:
7461 // These are relocations which should only be seen by the
7462 // dynamic linker, and should never be seen here.
7463 gold_error(_("%s: unexpected reloc %u in object file"),
7464 object
->name().c_str(), r_type
);
7468 // These are initial TLS relocs, which are expected when
7470 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7471 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7472 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7473 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7474 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7476 bool output_is_shared
= parameters
->options().shared();
7477 const tls::Tls_optimization optimized_type
7478 = Target_arm
<big_endian
>::optimize_tls_reloc(!output_is_shared
,
7482 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7483 if (optimized_type
== tls::TLSOPT_NONE
)
7485 // Create a pair of GOT entries for the module index and
7486 // dtv-relative offset.
7487 Arm_output_data_got
<big_endian
>* got
7488 = target
->got_section(symtab
, layout
);
7489 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7490 unsigned int shndx
= lsym
.get_st_shndx();
7492 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7495 object
->error(_("local symbol %u has bad shndx %u"),
7500 if (!parameters
->doing_static_link())
7501 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
7503 target
->rel_dyn_section(layout
),
7504 elfcpp::R_ARM_TLS_DTPMOD32
, 0);
7506 got
->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR
,
7510 // FIXME: TLS optimization not supported yet.
7514 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7515 if (optimized_type
== tls::TLSOPT_NONE
)
7517 // Create a GOT entry for the module index.
7518 target
->got_mod_index_entry(symtab
, layout
, object
);
7521 // FIXME: TLS optimization not supported yet.
7525 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7528 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7529 layout
->set_has_static_tls();
7530 if (optimized_type
== tls::TLSOPT_NONE
)
7532 // Create a GOT entry for the tp-relative offset.
7533 Arm_output_data_got
<big_endian
>* got
7534 = target
->got_section(symtab
, layout
);
7535 unsigned int r_sym
=
7536 elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7537 if (!parameters
->doing_static_link())
7538 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
7539 target
->rel_dyn_section(layout
),
7540 elfcpp::R_ARM_TLS_TPOFF32
);
7541 else if (!object
->local_has_got_offset(r_sym
,
7542 GOT_TYPE_TLS_OFFSET
))
7544 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_OFFSET
);
7545 unsigned int got_offset
=
7546 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
7547 got
->add_static_reloc(got_offset
,
7548 elfcpp::R_ARM_TLS_TPOFF32
, object
,
7553 // FIXME: TLS optimization not supported yet.
7557 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7558 layout
->set_has_static_tls();
7559 if (output_is_shared
)
7561 // We need to create a dynamic relocation.
7562 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
7563 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
7564 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7565 rel_dyn
->add_local(object
, r_sym
, elfcpp::R_ARM_TLS_TPOFF32
,
7566 output_section
, data_shndx
,
7567 reloc
.get_r_offset());
7578 unsupported_reloc_local(object
, r_type
);
7583 // Report an unsupported relocation against a global symbol.
7585 template<bool big_endian
>
7587 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
7588 Sized_relobj
<32, big_endian
>* object
,
7589 unsigned int r_type
,
7592 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7593 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
7596 // Scan a relocation for a global symbol.
7598 template<bool big_endian
>
7600 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
7603 Sized_relobj
<32, big_endian
>* object
,
7604 unsigned int data_shndx
,
7605 Output_section
* output_section
,
7606 const elfcpp::Rel
<32, big_endian
>& reloc
,
7607 unsigned int r_type
,
7610 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7611 // section. We check here to avoid creating a dynamic reloc against
7612 // _GLOBAL_OFFSET_TABLE_.
7613 if (!target
->has_got_section()
7614 && strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7615 target
->got_section(symtab
, layout
);
7617 r_type
= get_real_reloc_type(r_type
);
7620 case elfcpp::R_ARM_NONE
:
7621 case elfcpp::R_ARM_V4BX
:
7622 case elfcpp::R_ARM_GNU_VTENTRY
:
7623 case elfcpp::R_ARM_GNU_VTINHERIT
:
7626 case elfcpp::R_ARM_ABS32
:
7627 case elfcpp::R_ARM_ABS16
:
7628 case elfcpp::R_ARM_ABS12
:
7629 case elfcpp::R_ARM_THM_ABS5
:
7630 case elfcpp::R_ARM_ABS8
:
7631 case elfcpp::R_ARM_BASE_ABS
:
7632 case elfcpp::R_ARM_MOVW_ABS_NC
:
7633 case elfcpp::R_ARM_MOVT_ABS
:
7634 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
7635 case elfcpp::R_ARM_THM_MOVT_ABS
:
7636 case elfcpp::R_ARM_ABS32_NOI
:
7637 // Absolute addressing relocations.
7639 // Make a PLT entry if necessary.
7640 if (this->symbol_needs_plt_entry(gsym
))
7642 target
->make_plt_entry(symtab
, layout
, gsym
);
7643 // Since this is not a PC-relative relocation, we may be
7644 // taking the address of a function. In that case we need to
7645 // set the entry in the dynamic symbol table to the address of
7647 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
7648 gsym
->set_needs_dynsym_value();
7650 // Make a dynamic relocation if necessary.
7651 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
7653 if (gsym
->may_need_copy_reloc())
7655 target
->copy_reloc(symtab
, layout
, object
,
7656 data_shndx
, output_section
, gsym
, reloc
);
7658 else if ((r_type
== elfcpp::R_ARM_ABS32
7659 || r_type
== elfcpp::R_ARM_ABS32_NOI
)
7660 && gsym
->can_use_relative_reloc(false))
7662 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7663 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
7664 output_section
, object
,
7665 data_shndx
, reloc
.get_r_offset());
7669 check_non_pic(object
, r_type
);
7670 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7671 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
7672 data_shndx
, reloc
.get_r_offset());
7678 case elfcpp::R_ARM_GOTOFF32
:
7679 case elfcpp::R_ARM_GOTOFF12
:
7680 // We need a GOT section.
7681 target
->got_section(symtab
, layout
);
7684 case elfcpp::R_ARM_REL32
:
7685 case elfcpp::R_ARM_LDR_PC_G0
:
7686 case elfcpp::R_ARM_SBREL32
:
7687 case elfcpp::R_ARM_THM_PC8
:
7688 case elfcpp::R_ARM_BASE_PREL
:
7689 case elfcpp::R_ARM_LDR_SBREL_11_0_NC
:
7690 case elfcpp::R_ARM_ALU_SBREL_19_12_NC
:
7691 case elfcpp::R_ARM_ALU_SBREL_27_20_CK
:
7692 case elfcpp::R_ARM_MOVW_PREL_NC
:
7693 case elfcpp::R_ARM_MOVT_PREL
:
7694 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
7695 case elfcpp::R_ARM_THM_MOVT_PREL
:
7696 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
7697 case elfcpp::R_ARM_THM_PC12
:
7698 case elfcpp::R_ARM_REL32_NOI
:
7699 case elfcpp::R_ARM_ALU_PC_G0_NC
:
7700 case elfcpp::R_ARM_ALU_PC_G0
:
7701 case elfcpp::R_ARM_ALU_PC_G1_NC
:
7702 case elfcpp::R_ARM_ALU_PC_G1
:
7703 case elfcpp::R_ARM_ALU_PC_G2
:
7704 case elfcpp::R_ARM_LDR_PC_G1
:
7705 case elfcpp::R_ARM_LDR_PC_G2
:
7706 case elfcpp::R_ARM_LDRS_PC_G0
:
7707 case elfcpp::R_ARM_LDRS_PC_G1
:
7708 case elfcpp::R_ARM_LDRS_PC_G2
:
7709 case elfcpp::R_ARM_LDC_PC_G0
:
7710 case elfcpp::R_ARM_LDC_PC_G1
:
7711 case elfcpp::R_ARM_LDC_PC_G2
:
7712 case elfcpp::R_ARM_ALU_SB_G0_NC
:
7713 case elfcpp::R_ARM_ALU_SB_G0
:
7714 case elfcpp::R_ARM_ALU_SB_G1_NC
:
7715 case elfcpp::R_ARM_ALU_SB_G1
:
7716 case elfcpp::R_ARM_ALU_SB_G2
:
7717 case elfcpp::R_ARM_LDR_SB_G0
:
7718 case elfcpp::R_ARM_LDR_SB_G1
:
7719 case elfcpp::R_ARM_LDR_SB_G2
:
7720 case elfcpp::R_ARM_LDRS_SB_G0
:
7721 case elfcpp::R_ARM_LDRS_SB_G1
:
7722 case elfcpp::R_ARM_LDRS_SB_G2
:
7723 case elfcpp::R_ARM_LDC_SB_G0
:
7724 case elfcpp::R_ARM_LDC_SB_G1
:
7725 case elfcpp::R_ARM_LDC_SB_G2
:
7726 case elfcpp::R_ARM_MOVW_BREL_NC
:
7727 case elfcpp::R_ARM_MOVT_BREL
:
7728 case elfcpp::R_ARM_MOVW_BREL
:
7729 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
7730 case elfcpp::R_ARM_THM_MOVT_BREL
:
7731 case elfcpp::R_ARM_THM_MOVW_BREL
:
7732 // Relative addressing relocations.
7734 // Make a dynamic relocation if necessary.
7735 int flags
= Symbol::NON_PIC_REF
;
7736 if (gsym
->needs_dynamic_reloc(flags
))
7738 if (target
->may_need_copy_reloc(gsym
))
7740 target
->copy_reloc(symtab
, layout
, object
,
7741 data_shndx
, output_section
, gsym
, reloc
);
7745 check_non_pic(object
, r_type
);
7746 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7747 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
7748 data_shndx
, reloc
.get_r_offset());
7754 case elfcpp::R_ARM_PC24
:
7755 case elfcpp::R_ARM_THM_CALL
:
7756 case elfcpp::R_ARM_PLT32
:
7757 case elfcpp::R_ARM_CALL
:
7758 case elfcpp::R_ARM_JUMP24
:
7759 case elfcpp::R_ARM_THM_JUMP24
:
7760 case elfcpp::R_ARM_SBREL31
:
7761 case elfcpp::R_ARM_PREL31
:
7762 case elfcpp::R_ARM_THM_JUMP19
:
7763 case elfcpp::R_ARM_THM_JUMP6
:
7764 case elfcpp::R_ARM_THM_JUMP11
:
7765 case elfcpp::R_ARM_THM_JUMP8
:
7766 // All the relocation above are branches except for the PREL31 ones.
7767 // A PREL31 relocation can point to a personality function in a shared
7768 // library. In that case we want to use a PLT because we want to
7769 // call the personality routine and the dyanmic linkers we care about
7770 // do not support dynamic PREL31 relocations. An REL31 relocation may
7771 // point to a function whose unwinding behaviour is being described but
7772 // we will not mistakenly generate a PLT for that because we should use
7773 // a local section symbol.
7775 // If the symbol is fully resolved, this is just a relative
7776 // local reloc. Otherwise we need a PLT entry.
7777 if (gsym
->final_value_is_known())
7779 // If building a shared library, we can also skip the PLT entry
7780 // if the symbol is defined in the output file and is protected
7782 if (gsym
->is_defined()
7783 && !gsym
->is_from_dynobj()
7784 && !gsym
->is_preemptible())
7786 target
->make_plt_entry(symtab
, layout
, gsym
);
7789 case elfcpp::R_ARM_GOT_BREL
:
7790 case elfcpp::R_ARM_GOT_ABS
:
7791 case elfcpp::R_ARM_GOT_PREL
:
7793 // The symbol requires a GOT entry.
7794 Arm_output_data_got
<big_endian
>* got
=
7795 target
->got_section(symtab
, layout
);
7796 if (gsym
->final_value_is_known())
7797 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
7800 // If this symbol is not fully resolved, we need to add a
7801 // GOT entry with a dynamic relocation.
7802 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7803 if (gsym
->is_from_dynobj()
7804 || gsym
->is_undefined()
7805 || gsym
->is_preemptible())
7806 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
7807 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
7810 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
7811 rel_dyn
->add_global_relative(
7812 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
7813 gsym
->got_offset(GOT_TYPE_STANDARD
));
7819 case elfcpp::R_ARM_TARGET1
:
7820 case elfcpp::R_ARM_TARGET2
:
7821 // These should have been mapped to other types already.
7823 case elfcpp::R_ARM_COPY
:
7824 case elfcpp::R_ARM_GLOB_DAT
:
7825 case elfcpp::R_ARM_JUMP_SLOT
:
7826 case elfcpp::R_ARM_RELATIVE
:
7827 // These are relocations which should only be seen by the
7828 // dynamic linker, and should never be seen here.
7829 gold_error(_("%s: unexpected reloc %u in object file"),
7830 object
->name().c_str(), r_type
);
7833 // These are initial tls relocs, which are expected when
7835 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7836 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7837 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7838 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7839 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7841 const bool is_final
= gsym
->final_value_is_known();
7842 const tls::Tls_optimization optimized_type
7843 = Target_arm
<big_endian
>::optimize_tls_reloc(is_final
, r_type
);
7846 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
7847 if (optimized_type
== tls::TLSOPT_NONE
)
7849 // Create a pair of GOT entries for the module index and
7850 // dtv-relative offset.
7851 Arm_output_data_got
<big_endian
>* got
7852 = target
->got_section(symtab
, layout
);
7853 if (!parameters
->doing_static_link())
7854 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
7855 target
->rel_dyn_section(layout
),
7856 elfcpp::R_ARM_TLS_DTPMOD32
,
7857 elfcpp::R_ARM_TLS_DTPOFF32
);
7859 got
->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR
, gsym
);
7862 // FIXME: TLS optimization not supported yet.
7866 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
7867 if (optimized_type
== tls::TLSOPT_NONE
)
7869 // Create a GOT entry for the module index.
7870 target
->got_mod_index_entry(symtab
, layout
, object
);
7873 // FIXME: TLS optimization not supported yet.
7877 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
7880 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
7881 layout
->set_has_static_tls();
7882 if (optimized_type
== tls::TLSOPT_NONE
)
7884 // Create a GOT entry for the tp-relative offset.
7885 Arm_output_data_got
<big_endian
>* got
7886 = target
->got_section(symtab
, layout
);
7887 if (!parameters
->doing_static_link())
7888 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
7889 target
->rel_dyn_section(layout
),
7890 elfcpp::R_ARM_TLS_TPOFF32
);
7891 else if (!gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
))
7893 got
->add_global(gsym
, GOT_TYPE_TLS_OFFSET
);
7894 unsigned int got_offset
=
7895 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
7896 got
->add_static_reloc(got_offset
,
7897 elfcpp::R_ARM_TLS_TPOFF32
, gsym
);
7901 // FIXME: TLS optimization not supported yet.
7905 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
7906 layout
->set_has_static_tls();
7907 if (parameters
->options().shared())
7909 // We need to create a dynamic relocation.
7910 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
7911 rel_dyn
->add_global(gsym
, elfcpp::R_ARM_TLS_TPOFF32
,
7912 output_section
, object
,
7913 data_shndx
, reloc
.get_r_offset());
7924 unsupported_reloc_global(object
, r_type
, gsym
);
7929 // Process relocations for gc.
7931 template<bool big_endian
>
7933 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
7935 Sized_relobj
<32, big_endian
>* object
,
7936 unsigned int data_shndx
,
7938 const unsigned char* prelocs
,
7940 Output_section
* output_section
,
7941 bool needs_special_offset_handling
,
7942 size_t local_symbol_count
,
7943 const unsigned char* plocal_symbols
)
7945 typedef Target_arm
<big_endian
> Arm
;
7946 typedef typename Target_arm
<big_endian
>::Scan Scan
;
7948 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
7957 needs_special_offset_handling
,
7962 // Scan relocations for a section.
7964 template<bool big_endian
>
7966 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
7968 Sized_relobj
<32, big_endian
>* object
,
7969 unsigned int data_shndx
,
7970 unsigned int sh_type
,
7971 const unsigned char* prelocs
,
7973 Output_section
* output_section
,
7974 bool needs_special_offset_handling
,
7975 size_t local_symbol_count
,
7976 const unsigned char* plocal_symbols
)
7978 typedef typename Target_arm
<big_endian
>::Scan Scan
;
7979 if (sh_type
== elfcpp::SHT_RELA
)
7981 gold_error(_("%s: unsupported RELA reloc section"),
7982 object
->name().c_str());
7986 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
7995 needs_special_offset_handling
,
8000 // Finalize the sections.
8002 template<bool big_endian
>
8004 Target_arm
<big_endian
>::do_finalize_sections(
8006 const Input_objects
* input_objects
,
8007 Symbol_table
* symtab
)
8009 // Merge processor-specific flags.
8010 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
8011 p
!= input_objects
->relobj_end();
8014 Arm_relobj
<big_endian
>* arm_relobj
=
8015 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
8016 if (arm_relobj
->merge_flags_and_attributes())
8018 this->merge_processor_specific_flags(
8020 arm_relobj
->processor_specific_flags());
8021 this->merge_object_attributes(arm_relobj
->name().c_str(),
8022 arm_relobj
->attributes_section_data());
8026 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
8027 p
!= input_objects
->dynobj_end();
8030 Arm_dynobj
<big_endian
>* arm_dynobj
=
8031 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
8032 this->merge_processor_specific_flags(
8034 arm_dynobj
->processor_specific_flags());
8035 this->merge_object_attributes(arm_dynobj
->name().c_str(),
8036 arm_dynobj
->attributes_section_data());
8039 // Create an empty uninitialized attribute section if we still don't have it
8040 // at this moment. This happens if there is no attributes sections in all
8042 if (this->attributes_section_data_
== NULL
)
8043 this->attributes_section_data_
= new Attributes_section_data(NULL
, 0);
8046 const Object_attribute
* cpu_arch_attr
=
8047 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
8048 if (cpu_arch_attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
8049 this->set_may_use_blx(true);
8051 // Check if we need to use Cortex-A8 workaround.
8052 if (parameters
->options().user_set_fix_cortex_a8())
8053 this->fix_cortex_a8_
= parameters
->options().fix_cortex_a8();
8056 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8057 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8059 const Object_attribute
* cpu_arch_profile_attr
=
8060 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
8061 this->fix_cortex_a8_
=
8062 (cpu_arch_attr
->int_value() == elfcpp::TAG_CPU_ARCH_V7
8063 && (cpu_arch_profile_attr
->int_value() == 'A'
8064 || cpu_arch_profile_attr
->int_value() == 0));
8067 // Check if we can use V4BX interworking.
8068 // The V4BX interworking stub contains BX instruction,
8069 // which is not specified for some profiles.
8070 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8071 && !this->may_use_blx())
8072 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8073 "the target profile does not support BX instruction"));
8075 // Fill in some more dynamic tags.
8076 const Reloc_section
* rel_plt
= (this->plt_
== NULL
8078 : this->plt_
->rel_plt());
8079 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
8080 this->rel_dyn_
, true, false);
8082 // Emit any relocs we saved in an attempt to avoid generating COPY
8084 if (this->copy_relocs_
.any_saved_relocs())
8085 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
8087 // Handle the .ARM.exidx section.
8088 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
8089 if (exidx_section
!= NULL
8090 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
8091 && !parameters
->options().relocatable())
8093 // Create __exidx_start and __exdix_end symbols.
8094 symtab
->define_in_output_data("__exidx_start", NULL
,
8095 Symbol_table::PREDEFINED
,
8096 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
8097 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
8099 symtab
->define_in_output_data("__exidx_end", NULL
,
8100 Symbol_table::PREDEFINED
,
8101 exidx_section
, 0, 0, elfcpp::STT_OBJECT
,
8102 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
8105 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8106 // the .ARM.exidx section.
8107 if (!layout
->script_options()->saw_phdrs_clause())
8109 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
8111 Output_segment
* exidx_segment
=
8112 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
8113 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
8118 // Create an .ARM.attributes section unless we have no regular input
8119 // object. In that case the output will be empty.
8120 if (input_objects
->number_of_relobjs() != 0)
8122 Output_attributes_section_data
* attributes_section
=
8123 new Output_attributes_section_data(*this->attributes_section_data_
);
8124 layout
->add_output_section_data(".ARM.attributes",
8125 elfcpp::SHT_ARM_ATTRIBUTES
, 0,
8126 attributes_section
, false, false, false,
8131 // Return whether a direct absolute static relocation needs to be applied.
8132 // In cases where Scan::local() or Scan::global() has created
8133 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8134 // of the relocation is carried in the data, and we must not
8135 // apply the static relocation.
8137 template<bool big_endian
>
8139 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
8140 const Sized_symbol
<32>* gsym
,
8143 Output_section
* output_section
)
8145 // If the output section is not allocated, then we didn't call
8146 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8148 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
8151 // For local symbols, we will have created a non-RELATIVE dynamic
8152 // relocation only if (a) the output is position independent,
8153 // (b) the relocation is absolute (not pc- or segment-relative), and
8154 // (c) the relocation is not 32 bits wide.
8156 return !(parameters
->options().output_is_position_independent()
8157 && (ref_flags
& Symbol::ABSOLUTE_REF
)
8160 // For global symbols, we use the same helper routines used in the
8161 // scan pass. If we did not create a dynamic relocation, or if we
8162 // created a RELATIVE dynamic relocation, we should apply the static
8164 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
8165 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
8166 && gsym
->can_use_relative_reloc(ref_flags
8167 & Symbol::FUNCTION_CALL
);
8168 return !has_dyn
|| is_rel
;
8171 // Perform a relocation.
8173 template<bool big_endian
>
8175 Target_arm
<big_endian
>::Relocate::relocate(
8176 const Relocate_info
<32, big_endian
>* relinfo
,
8178 Output_section
*output_section
,
8180 const elfcpp::Rel
<32, big_endian
>& rel
,
8181 unsigned int r_type
,
8182 const Sized_symbol
<32>* gsym
,
8183 const Symbol_value
<32>* psymval
,
8184 unsigned char* view
,
8185 Arm_address address
,
8186 section_size_type view_size
)
8188 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
8190 r_type
= get_real_reloc_type(r_type
);
8191 const Arm_reloc_property
* reloc_property
=
8192 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
8193 if (reloc_property
== NULL
)
8195 std::string reloc_name
=
8196 arm_reloc_property_table
->reloc_name_in_error_message(r_type
);
8197 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
8198 _("cannot relocate %s in object file"),
8199 reloc_name
.c_str());
8203 const Arm_relobj
<big_endian
>* object
=
8204 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
8206 // If the final branch target of a relocation is THUMB instruction, this
8207 // is 1. Otherwise it is 0.
8208 Arm_address thumb_bit
= 0;
8209 Symbol_value
<32> symval
;
8210 bool is_weakly_undefined_without_plt
= false;
8211 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
8215 // This is a global symbol. Determine if we use PLT and if the
8216 // final target is THUMB.
8217 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
8219 // This uses a PLT, change the symbol value.
8220 symval
.set_output_value(target
->plt_section()->address()
8221 + gsym
->plt_offset());
8224 else if (gsym
->is_weak_undefined())
8226 // This is a weakly undefined symbol and we do not use PLT
8227 // for this relocation. A branch targeting this symbol will
8228 // be converted into an NOP.
8229 is_weakly_undefined_without_plt
= true;
8233 // Set thumb bit if symbol:
8234 // -Has type STT_ARM_TFUNC or
8235 // -Has type STT_FUNC, is defined and with LSB in value set.
8237 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
8238 || (gsym
->type() == elfcpp::STT_FUNC
8239 && !gsym
->is_undefined()
8240 && ((psymval
->value(object
, 0) & 1) != 0)))
8247 // This is a local symbol. Determine if the final target is THUMB.
8248 // We saved this information when all the local symbols were read.
8249 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
8250 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
8251 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
8256 // This is a fake relocation synthesized for a stub. It does not have
8257 // a real symbol. We just look at the LSB of the symbol value to
8258 // determine if the target is THUMB or not.
8259 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
8262 // Strip LSB if this points to a THUMB target.
8264 && reloc_property
->uses_thumb_bit()
8265 && ((psymval
->value(object
, 0) & 1) != 0))
8267 Arm_address stripped_value
=
8268 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
8269 symval
.set_output_value(stripped_value
);
8273 // Get the GOT offset if needed.
8274 // The GOT pointer points to the end of the GOT section.
8275 // We need to subtract the size of the GOT section to get
8276 // the actual offset to use in the relocation.
8277 bool have_got_offset
= false;
8278 unsigned int got_offset
= 0;
8281 case elfcpp::R_ARM_GOT_BREL
:
8282 case elfcpp::R_ARM_GOT_PREL
:
8285 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
8286 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
8287 - target
->got_size());
8291 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8292 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
8293 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
8294 - target
->got_size());
8296 have_got_offset
= true;
8303 // To look up relocation stubs, we need to pass the symbol table index of
8305 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8307 // Get the addressing origin of the output segment defining the
8308 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8309 Arm_address sym_origin
= 0;
8310 if (reloc_property
->uses_symbol_base())
8312 if (r_type
== elfcpp::R_ARM_BASE_ABS
&& gsym
== NULL
)
8313 // R_ARM_BASE_ABS with the NULL symbol will give the
8314 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8315 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8316 sym_origin
= target
->got_plt_section()->address();
8317 else if (gsym
== NULL
)
8319 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
8320 sym_origin
= gsym
->output_segment()->vaddr();
8321 else if (gsym
->source() == Symbol::IN_OUTPUT_DATA
)
8322 sym_origin
= gsym
->output_data()->address();
8324 // TODO: Assumes the segment base to be zero for the global symbols
8325 // till the proper support for the segment-base-relative addressing
8326 // will be implemented. This is consistent with GNU ld.
8329 // For relative addressing relocation, find out the relative address base.
8330 Arm_address relative_address_base
= 0;
8331 switch(reloc_property
->relative_address_base())
8333 case Arm_reloc_property::RAB_NONE
:
8334 // Relocations with relative address bases RAB_TLS and RAB_tp are
8335 // handled by relocate_tls. So we do not need to do anything here.
8336 case Arm_reloc_property::RAB_TLS
:
8337 case Arm_reloc_property::RAB_tp
:
8339 case Arm_reloc_property::RAB_B_S
:
8340 relative_address_base
= sym_origin
;
8342 case Arm_reloc_property::RAB_GOT_ORG
:
8343 relative_address_base
= target
->got_plt_section()->address();
8345 case Arm_reloc_property::RAB_P
:
8346 relative_address_base
= address
;
8348 case Arm_reloc_property::RAB_Pa
:
8349 relative_address_base
= address
& 0xfffffffcU
;
8355 typename
Arm_relocate_functions::Status reloc_status
=
8356 Arm_relocate_functions::STATUS_OKAY
;
8357 bool check_overflow
= reloc_property
->checks_overflow();
8360 case elfcpp::R_ARM_NONE
:
8363 case elfcpp::R_ARM_ABS8
:
8364 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8366 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
8369 case elfcpp::R_ARM_ABS12
:
8370 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8372 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
8375 case elfcpp::R_ARM_ABS16
:
8376 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8378 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
8381 case elfcpp::R_ARM_ABS32
:
8382 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
8384 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
8388 case elfcpp::R_ARM_ABS32_NOI
:
8389 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
8391 // No thumb bit for this relocation: (S + A)
8392 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
8396 case elfcpp::R_ARM_MOVW_ABS_NC
:
8397 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8399 reloc_status
= Arm_relocate_functions::movw(view
, object
, psymval
,
8404 case elfcpp::R_ARM_MOVT_ABS
:
8405 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8407 reloc_status
= Arm_relocate_functions::movt(view
, object
, psymval
, 0);
8410 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
8411 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8413 reloc_status
= Arm_relocate_functions::thm_movw(view
, object
, psymval
,
8414 0, thumb_bit
, false);
8417 case elfcpp::R_ARM_THM_MOVT_ABS
:
8418 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8420 reloc_status
= Arm_relocate_functions::thm_movt(view
, object
,
8424 case elfcpp::R_ARM_MOVW_PREL_NC
:
8425 case elfcpp::R_ARM_MOVW_BREL_NC
:
8426 case elfcpp::R_ARM_MOVW_BREL
:
8428 Arm_relocate_functions::movw(view
, object
, psymval
,
8429 relative_address_base
, thumb_bit
,
8433 case elfcpp::R_ARM_MOVT_PREL
:
8434 case elfcpp::R_ARM_MOVT_BREL
:
8436 Arm_relocate_functions::movt(view
, object
, psymval
,
8437 relative_address_base
);
8440 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
8441 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
8442 case elfcpp::R_ARM_THM_MOVW_BREL
:
8444 Arm_relocate_functions::thm_movw(view
, object
, psymval
,
8445 relative_address_base
,
8446 thumb_bit
, check_overflow
);
8449 case elfcpp::R_ARM_THM_MOVT_PREL
:
8450 case elfcpp::R_ARM_THM_MOVT_BREL
:
8452 Arm_relocate_functions::thm_movt(view
, object
, psymval
,
8453 relative_address_base
);
8456 case elfcpp::R_ARM_REL32
:
8457 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
8458 address
, thumb_bit
);
8461 case elfcpp::R_ARM_THM_ABS5
:
8462 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8464 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
8467 // Thumb long branches.
8468 case elfcpp::R_ARM_THM_CALL
:
8469 case elfcpp::R_ARM_THM_XPC22
:
8470 case elfcpp::R_ARM_THM_JUMP24
:
8472 Arm_relocate_functions::thumb_branch_common(
8473 r_type
, relinfo
, view
, gsym
, object
, r_sym
, psymval
, address
,
8474 thumb_bit
, is_weakly_undefined_without_plt
);
8477 case elfcpp::R_ARM_GOTOFF32
:
8479 Arm_address got_origin
;
8480 got_origin
= target
->got_plt_section()->address();
8481 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
8482 got_origin
, thumb_bit
);
8486 case elfcpp::R_ARM_BASE_PREL
:
8487 gold_assert(gsym
!= NULL
);
8489 Arm_relocate_functions::base_prel(view
, sym_origin
, address
);
8492 case elfcpp::R_ARM_BASE_ABS
:
8494 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
8498 reloc_status
= Arm_relocate_functions::base_abs(view
, sym_origin
);
8502 case elfcpp::R_ARM_GOT_BREL
:
8503 gold_assert(have_got_offset
);
8504 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
8507 case elfcpp::R_ARM_GOT_PREL
:
8508 gold_assert(have_got_offset
);
8509 // Get the address origin for GOT PLT, which is allocated right
8510 // after the GOT section, to calculate an absolute address of
8511 // the symbol GOT entry (got_origin + got_offset).
8512 Arm_address got_origin
;
8513 got_origin
= target
->got_plt_section()->address();
8514 reloc_status
= Arm_relocate_functions::got_prel(view
,
8515 got_origin
+ got_offset
,
8519 case elfcpp::R_ARM_PLT32
:
8520 case elfcpp::R_ARM_CALL
:
8521 case elfcpp::R_ARM_JUMP24
:
8522 case elfcpp::R_ARM_XPC25
:
8523 gold_assert(gsym
== NULL
8524 || gsym
->has_plt_offset()
8525 || gsym
->final_value_is_known()
8526 || (gsym
->is_defined()
8527 && !gsym
->is_from_dynobj()
8528 && !gsym
->is_preemptible()));
8530 Arm_relocate_functions::arm_branch_common(
8531 r_type
, relinfo
, view
, gsym
, object
, r_sym
, psymval
, address
,
8532 thumb_bit
, is_weakly_undefined_without_plt
);
8535 case elfcpp::R_ARM_THM_JUMP19
:
8537 Arm_relocate_functions::thm_jump19(view
, object
, psymval
, address
,
8541 case elfcpp::R_ARM_THM_JUMP6
:
8543 Arm_relocate_functions::thm_jump6(view
, object
, psymval
, address
);
8546 case elfcpp::R_ARM_THM_JUMP8
:
8548 Arm_relocate_functions::thm_jump8(view
, object
, psymval
, address
);
8551 case elfcpp::R_ARM_THM_JUMP11
:
8553 Arm_relocate_functions::thm_jump11(view
, object
, psymval
, address
);
8556 case elfcpp::R_ARM_PREL31
:
8557 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
8558 address
, thumb_bit
);
8561 case elfcpp::R_ARM_V4BX
:
8562 if (target
->fix_v4bx() > General_options::FIX_V4BX_NONE
)
8564 const bool is_v4bx_interworking
=
8565 (target
->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
);
8567 Arm_relocate_functions::v4bx(relinfo
, view
, object
, address
,
8568 is_v4bx_interworking
);
8572 case elfcpp::R_ARM_THM_PC8
:
8574 Arm_relocate_functions::thm_pc8(view
, object
, psymval
, address
);
8577 case elfcpp::R_ARM_THM_PC12
:
8579 Arm_relocate_functions::thm_pc12(view
, object
, psymval
, address
);
8582 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
8584 Arm_relocate_functions::thm_alu11(view
, object
, psymval
, address
,
8588 case elfcpp::R_ARM_ALU_PC_G0_NC
:
8589 case elfcpp::R_ARM_ALU_PC_G0
:
8590 case elfcpp::R_ARM_ALU_PC_G1_NC
:
8591 case elfcpp::R_ARM_ALU_PC_G1
:
8592 case elfcpp::R_ARM_ALU_PC_G2
:
8593 case elfcpp::R_ARM_ALU_SB_G0_NC
:
8594 case elfcpp::R_ARM_ALU_SB_G0
:
8595 case elfcpp::R_ARM_ALU_SB_G1_NC
:
8596 case elfcpp::R_ARM_ALU_SB_G1
:
8597 case elfcpp::R_ARM_ALU_SB_G2
:
8599 Arm_relocate_functions::arm_grp_alu(view
, object
, psymval
,
8600 reloc_property
->group_index(),
8601 relative_address_base
,
8602 thumb_bit
, check_overflow
);
8605 case elfcpp::R_ARM_LDR_PC_G0
:
8606 case elfcpp::R_ARM_LDR_PC_G1
:
8607 case elfcpp::R_ARM_LDR_PC_G2
:
8608 case elfcpp::R_ARM_LDR_SB_G0
:
8609 case elfcpp::R_ARM_LDR_SB_G1
:
8610 case elfcpp::R_ARM_LDR_SB_G2
:
8612 Arm_relocate_functions::arm_grp_ldr(view
, object
, psymval
,
8613 reloc_property
->group_index(),
8614 relative_address_base
);
8617 case elfcpp::R_ARM_LDRS_PC_G0
:
8618 case elfcpp::R_ARM_LDRS_PC_G1
:
8619 case elfcpp::R_ARM_LDRS_PC_G2
:
8620 case elfcpp::R_ARM_LDRS_SB_G0
:
8621 case elfcpp::R_ARM_LDRS_SB_G1
:
8622 case elfcpp::R_ARM_LDRS_SB_G2
:
8624 Arm_relocate_functions::arm_grp_ldrs(view
, object
, psymval
,
8625 reloc_property
->group_index(),
8626 relative_address_base
);
8629 case elfcpp::R_ARM_LDC_PC_G0
:
8630 case elfcpp::R_ARM_LDC_PC_G1
:
8631 case elfcpp::R_ARM_LDC_PC_G2
:
8632 case elfcpp::R_ARM_LDC_SB_G0
:
8633 case elfcpp::R_ARM_LDC_SB_G1
:
8634 case elfcpp::R_ARM_LDC_SB_G2
:
8636 Arm_relocate_functions::arm_grp_ldc(view
, object
, psymval
,
8637 reloc_property
->group_index(),
8638 relative_address_base
);
8641 // These are initial tls relocs, which are expected when
8643 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
8644 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
8645 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
8646 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
8647 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
8649 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
8650 view
, address
, view_size
);
8657 // Report any errors.
8658 switch (reloc_status
)
8660 case Arm_relocate_functions::STATUS_OKAY
:
8662 case Arm_relocate_functions::STATUS_OVERFLOW
:
8663 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
8664 _("relocation overflow in %s"),
8665 reloc_property
->name().c_str());
8667 case Arm_relocate_functions::STATUS_BAD_RELOC
:
8668 gold_error_at_location(
8672 _("unexpected opcode while processing relocation %s"),
8673 reloc_property
->name().c_str());
8682 // Perform a TLS relocation.
8684 template<bool big_endian
>
8685 inline typename Arm_relocate_functions
<big_endian
>::Status
8686 Target_arm
<big_endian
>::Relocate::relocate_tls(
8687 const Relocate_info
<32, big_endian
>* relinfo
,
8688 Target_arm
<big_endian
>* target
,
8690 const elfcpp::Rel
<32, big_endian
>& rel
,
8691 unsigned int r_type
,
8692 const Sized_symbol
<32>* gsym
,
8693 const Symbol_value
<32>* psymval
,
8694 unsigned char* view
,
8695 elfcpp::Elf_types
<32>::Elf_Addr address
,
8696 section_size_type
/*view_size*/ )
8698 typedef Arm_relocate_functions
<big_endian
> ArmRelocFuncs
;
8699 typedef Relocate_functions
<32, big_endian
> RelocFuncs
;
8700 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
8702 const Sized_relobj
<32, big_endian
>* object
= relinfo
->object
;
8704 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
8706 const bool is_final
= (gsym
== NULL
8707 ? !parameters
->options().shared()
8708 : gsym
->final_value_is_known());
8709 const tls::Tls_optimization optimized_type
8710 = Target_arm
<big_endian
>::optimize_tls_reloc(is_final
, r_type
);
8713 case elfcpp::R_ARM_TLS_GD32
: // Global-dynamic
8715 unsigned int got_type
= GOT_TYPE_TLS_PAIR
;
8716 unsigned int got_offset
;
8719 gold_assert(gsym
->has_got_offset(got_type
));
8720 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
8724 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8725 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8726 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
8727 - target
->got_size());
8729 if (optimized_type
== tls::TLSOPT_NONE
)
8731 Arm_address got_entry
=
8732 target
->got_plt_section()->address() + got_offset
;
8734 // Relocate the field with the PC relative offset of the pair of
8736 RelocFuncs::pcrel32(view
, got_entry
, address
);
8737 return ArmRelocFuncs::STATUS_OKAY
;
8742 case elfcpp::R_ARM_TLS_LDM32
: // Local-dynamic
8743 if (optimized_type
== tls::TLSOPT_NONE
)
8745 // Relocate the field with the offset of the GOT entry for
8746 // the module index.
8747 unsigned int got_offset
;
8748 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
8749 - target
->got_size());
8750 Arm_address got_entry
=
8751 target
->got_plt_section()->address() + got_offset
;
8753 // Relocate the field with the PC relative offset of the pair of
8755 RelocFuncs::pcrel32(view
, got_entry
, address
);
8756 return ArmRelocFuncs::STATUS_OKAY
;
8760 case elfcpp::R_ARM_TLS_LDO32
: // Alternate local-dynamic
8761 RelocFuncs::rel32(view
, value
);
8762 return ArmRelocFuncs::STATUS_OKAY
;
8764 case elfcpp::R_ARM_TLS_IE32
: // Initial-exec
8765 if (optimized_type
== tls::TLSOPT_NONE
)
8767 // Relocate the field with the offset of the GOT entry for
8768 // the tp-relative offset of the symbol.
8769 unsigned int got_type
= GOT_TYPE_TLS_OFFSET
;
8770 unsigned int got_offset
;
8773 gold_assert(gsym
->has_got_offset(got_type
));
8774 got_offset
= gsym
->got_offset(got_type
);
8778 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
8779 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8780 got_offset
= object
->local_got_offset(r_sym
, got_type
);
8783 // All GOT offsets are relative to the end of the GOT.
8784 got_offset
-= target
->got_size();
8786 Arm_address got_entry
=
8787 target
->got_plt_section()->address() + got_offset
;
8789 // Relocate the field with the PC relative offset of the GOT entry.
8790 RelocFuncs::pcrel32(view
, got_entry
, address
);
8791 return ArmRelocFuncs::STATUS_OKAY
;
8795 case elfcpp::R_ARM_TLS_LE32
: // Local-exec
8796 // If we're creating a shared library, a dynamic relocation will
8797 // have been created for this location, so do not apply it now.
8798 if (!parameters
->options().shared())
8800 gold_assert(tls_segment
!= NULL
);
8802 // $tp points to the TCB, which is followed by the TLS, so we
8803 // need to add TCB size to the offset.
8804 Arm_address aligned_tcb_size
=
8805 align_address(ARM_TCB_SIZE
, tls_segment
->maximum_alignment());
8806 RelocFuncs::rel32(view
, value
+ aligned_tcb_size
);
8809 return ArmRelocFuncs::STATUS_OKAY
;
8815 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
8816 _("unsupported reloc %u"),
8818 return ArmRelocFuncs::STATUS_BAD_RELOC
;
8821 // Relocate section data.
8823 template<bool big_endian
>
8825 Target_arm
<big_endian
>::relocate_section(
8826 const Relocate_info
<32, big_endian
>* relinfo
,
8827 unsigned int sh_type
,
8828 const unsigned char* prelocs
,
8830 Output_section
* output_section
,
8831 bool needs_special_offset_handling
,
8832 unsigned char* view
,
8833 Arm_address address
,
8834 section_size_type view_size
,
8835 const Reloc_symbol_changes
* reloc_symbol_changes
)
8837 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
8838 gold_assert(sh_type
== elfcpp::SHT_REL
);
8840 // See if we are relocating a relaxed input section. If so, the view
8841 // covers the whole output section and we need to adjust accordingly.
8842 if (needs_special_offset_handling
)
8844 const Output_relaxed_input_section
* poris
=
8845 output_section
->find_relaxed_input_section(relinfo
->object
,
8846 relinfo
->data_shndx
);
8849 Arm_address section_address
= poris
->address();
8850 section_size_type section_size
= poris
->data_size();
8852 gold_assert((section_address
>= address
)
8853 && ((section_address
+ section_size
)
8854 <= (address
+ view_size
)));
8856 off_t offset
= section_address
- address
;
8859 view_size
= section_size
;
8863 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
8870 needs_special_offset_handling
,
8874 reloc_symbol_changes
);
8877 // Return the size of a relocation while scanning during a relocatable
8880 template<bool big_endian
>
8882 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
8883 unsigned int r_type
,
8886 r_type
= get_real_reloc_type(r_type
);
8887 const Arm_reloc_property
* arp
=
8888 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
8893 std::string reloc_name
=
8894 arm_reloc_property_table
->reloc_name_in_error_message(r_type
);
8895 gold_error(_("%s: unexpected %s in object file"),
8896 object
->name().c_str(), reloc_name
.c_str());
8901 // Scan the relocs during a relocatable link.
8903 template<bool big_endian
>
8905 Target_arm
<big_endian
>::scan_relocatable_relocs(
8906 Symbol_table
* symtab
,
8908 Sized_relobj
<32, big_endian
>* object
,
8909 unsigned int data_shndx
,
8910 unsigned int sh_type
,
8911 const unsigned char* prelocs
,
8913 Output_section
* output_section
,
8914 bool needs_special_offset_handling
,
8915 size_t local_symbol_count
,
8916 const unsigned char* plocal_symbols
,
8917 Relocatable_relocs
* rr
)
8919 gold_assert(sh_type
== elfcpp::SHT_REL
);
8921 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
8922 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
8924 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
8925 Scan_relocatable_relocs
>(
8933 needs_special_offset_handling
,
8939 // Relocate a section during a relocatable link.
8941 template<bool big_endian
>
8943 Target_arm
<big_endian
>::relocate_for_relocatable(
8944 const Relocate_info
<32, big_endian
>* relinfo
,
8945 unsigned int sh_type
,
8946 const unsigned char* prelocs
,
8948 Output_section
* output_section
,
8949 off_t offset_in_output_section
,
8950 const Relocatable_relocs
* rr
,
8951 unsigned char* view
,
8952 Arm_address view_address
,
8953 section_size_type view_size
,
8954 unsigned char* reloc_view
,
8955 section_size_type reloc_view_size
)
8957 gold_assert(sh_type
== elfcpp::SHT_REL
);
8959 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
8964 offset_in_output_section
,
8973 // Return the value to use for a dynamic symbol which requires special
8974 // treatment. This is how we support equality comparisons of function
8975 // pointers across shared library boundaries, as described in the
8976 // processor specific ABI supplement.
8978 template<bool big_endian
>
8980 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
8982 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
8983 return this->plt_section()->address() + gsym
->plt_offset();
8986 // Map platform-specific relocs to real relocs
8988 template<bool big_endian
>
8990 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
8994 case elfcpp::R_ARM_TARGET1
:
8995 // This is either R_ARM_ABS32 or R_ARM_REL32;
8996 return elfcpp::R_ARM_ABS32
;
8998 case elfcpp::R_ARM_TARGET2
:
8999 // This can be any reloc type but ususally is R_ARM_GOT_PREL
9000 return elfcpp::R_ARM_GOT_PREL
;
9007 // Whether if two EABI versions V1 and V2 are compatible.
9009 template<bool big_endian
>
9011 Target_arm
<big_endian
>::are_eabi_versions_compatible(
9012 elfcpp::Elf_Word v1
,
9013 elfcpp::Elf_Word v2
)
9015 // v4 and v5 are the same spec before and after it was released,
9016 // so allow mixing them.
9017 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
9018 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
9024 // Combine FLAGS from an input object called NAME and the processor-specific
9025 // flags in the ELF header of the output. Much of this is adapted from the
9026 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9027 // in bfd/elf32-arm.c.
9029 template<bool big_endian
>
9031 Target_arm
<big_endian
>::merge_processor_specific_flags(
9032 const std::string
& name
,
9033 elfcpp::Elf_Word flags
)
9035 if (this->are_processor_specific_flags_set())
9037 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
9039 // Nothing to merge if flags equal to those in output.
9040 if (flags
== out_flags
)
9043 // Complain about various flag mismatches.
9044 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
9045 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
9046 if (!this->are_eabi_versions_compatible(version1
, version2
)
9047 && parameters
->options().warn_mismatch())
9048 gold_error(_("Source object %s has EABI version %d but output has "
9049 "EABI version %d."),
9051 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
9052 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
9056 // If the input is the default architecture and had the default
9057 // flags then do not bother setting the flags for the output
9058 // architecture, instead allow future merges to do this. If no
9059 // future merges ever set these flags then they will retain their
9060 // uninitialised values, which surprise surprise, correspond
9061 // to the default values.
9065 // This is the first time, just copy the flags.
9066 // We only copy the EABI version for now.
9067 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
9071 // Adjust ELF file header.
9072 template<bool big_endian
>
9074 Target_arm
<big_endian
>::do_adjust_elf_header(
9075 unsigned char* view
,
9078 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
9080 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
9081 unsigned char e_ident
[elfcpp::EI_NIDENT
];
9082 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
9084 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9085 == elfcpp::EF_ARM_EABI_UNKNOWN
)
9086 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
9088 e_ident
[elfcpp::EI_OSABI
] = 0;
9089 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
9091 // FIXME: Do EF_ARM_BE8 adjustment.
9093 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
9094 oehdr
.put_e_ident(e_ident
);
9097 // do_make_elf_object to override the same function in the base class.
9098 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9099 // to store ARM specific information. Hence we need to have our own
9100 // ELF object creation.
9102 template<bool big_endian
>
9104 Target_arm
<big_endian
>::do_make_elf_object(
9105 const std::string
& name
,
9106 Input_file
* input_file
,
9107 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
9109 int et
= ehdr
.get_e_type();
9110 if (et
== elfcpp::ET_REL
)
9112 Arm_relobj
<big_endian
>* obj
=
9113 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
9117 else if (et
== elfcpp::ET_DYN
)
9119 Sized_dynobj
<32, big_endian
>* obj
=
9120 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
9126 gold_error(_("%s: unsupported ELF file type %d"),
9132 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9133 // Returns -1 if no architecture could be read.
9134 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9136 template<bool big_endian
>
9138 Target_arm
<big_endian
>::get_secondary_compatible_arch(
9139 const Attributes_section_data
* pasd
)
9141 const Object_attribute
*known_attributes
=
9142 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
9144 // Note: the tag and its argument below are uleb128 values, though
9145 // currently-defined values fit in one byte for each.
9146 const std::string
& sv
=
9147 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
9149 && sv
.data()[0] == elfcpp::Tag_CPU_arch
9150 && (sv
.data()[1] & 128) != 128)
9151 return sv
.data()[1];
9153 // This tag is "safely ignorable", so don't complain if it looks funny.
9157 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9158 // The tag is removed if ARCH is -1.
9159 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9161 template<bool big_endian
>
9163 Target_arm
<big_endian
>::set_secondary_compatible_arch(
9164 Attributes_section_data
* pasd
,
9167 Object_attribute
*known_attributes
=
9168 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
9172 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
9176 // Note: the tag and its argument below are uleb128 values, though
9177 // currently-defined values fit in one byte for each.
9179 sv
[0] = elfcpp::Tag_CPU_arch
;
9180 gold_assert(arch
!= 0);
9184 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
9187 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9189 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9191 template<bool big_endian
>
9193 Target_arm
<big_endian
>::tag_cpu_arch_combine(
9196 int* secondary_compat_out
,
9198 int secondary_compat
)
9200 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9201 static const int v6t2
[] =
9213 static const int v6k
[] =
9226 static const int v7
[] =
9240 static const int v6_m
[] =
9255 static const int v6s_m
[] =
9271 static const int v7e_m
[] =
9288 static const int v4t_plus_v6_m
[] =
9304 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
9306 static const int *comb
[] =
9314 // Pseudo-architecture.
9318 // Check we've not got a higher architecture than we know about.
9320 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
9322 gold_error(_("%s: unknown CPU architecture"), name
);
9326 // Override old tag if we have a Tag_also_compatible_with on the output.
9328 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
9329 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
9330 oldtag
= T(V4T_PLUS_V6_M
);
9332 // And override the new tag if we have a Tag_also_compatible_with on the
9335 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
9336 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
9337 newtag
= T(V4T_PLUS_V6_M
);
9339 // Architectures before V6KZ add features monotonically.
9340 int tagh
= std::max(oldtag
, newtag
);
9341 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
9344 int tagl
= std::min(oldtag
, newtag
);
9345 int result
= comb
[tagh
- T(V6T2
)][tagl
];
9347 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9348 // as the canonical version.
9349 if (result
== T(V4T_PLUS_V6_M
))
9352 *secondary_compat_out
= T(V6_M
);
9355 *secondary_compat_out
= -1;
9359 gold_error(_("%s: conflicting CPU architectures %d/%d"),
9360 name
, oldtag
, newtag
);
9368 // Helper to print AEABI enum tag value.
9370 template<bool big_endian
>
9372 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
9374 static const char *aeabi_enum_names
[] =
9375 { "", "variable-size", "32-bit", "" };
9376 const size_t aeabi_enum_names_size
=
9377 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
9379 if (value
< aeabi_enum_names_size
)
9380 return std::string(aeabi_enum_names
[value
]);
9384 sprintf(buffer
, "<unknown value %u>", value
);
9385 return std::string(buffer
);
9389 // Return the string value to store in TAG_CPU_name.
9391 template<bool big_endian
>
9393 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
9395 static const char *name_table
[] = {
9396 // These aren't real CPU names, but we can't guess
9397 // that from the architecture version alone.
9413 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
9415 if (value
< name_table_size
)
9416 return std::string(name_table
[value
]);
9420 sprintf(buffer
, "<unknown CPU value %u>", value
);
9421 return std::string(buffer
);
9425 // Merge object attributes from input file called NAME with those of the
9426 // output. The input object attributes are in the object pointed by PASD.
9428 template<bool big_endian
>
9430 Target_arm
<big_endian
>::merge_object_attributes(
9432 const Attributes_section_data
* pasd
)
9434 // Return if there is no attributes section data.
9438 // If output has no object attributes, just copy.
9439 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
9440 if (this->attributes_section_data_
== NULL
)
9442 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
9443 Object_attribute
* out_attr
=
9444 this->attributes_section_data_
->known_attributes(vendor
);
9446 // We do not output objects with Tag_MPextension_use_legacy - we move
9447 // the attribute's value to Tag_MPextension_use. */
9448 if (out_attr
[elfcpp::Tag_MPextension_use_legacy
].int_value() != 0)
9450 if (out_attr
[elfcpp::Tag_MPextension_use
].int_value() != 0
9451 && out_attr
[elfcpp::Tag_MPextension_use_legacy
].int_value()
9452 != out_attr
[elfcpp::Tag_MPextension_use
].int_value())
9454 gold_error(_("%s has both the current and legacy "
9455 "Tag_MPextension_use attributes"),
9459 out_attr
[elfcpp::Tag_MPextension_use
] =
9460 out_attr
[elfcpp::Tag_MPextension_use_legacy
];
9461 out_attr
[elfcpp::Tag_MPextension_use_legacy
].set_type(0);
9462 out_attr
[elfcpp::Tag_MPextension_use_legacy
].set_int_value(0);
9468 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
9469 Object_attribute
* out_attr
=
9470 this->attributes_section_data_
->known_attributes(vendor
);
9472 // This needs to happen before Tag_ABI_FP_number_model is merged. */
9473 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
9474 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
9476 // Ignore mismatches if the object doesn't use floating point. */
9477 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
9478 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
9479 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
9480 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0
9481 && parameters
->options().warn_mismatch())
9482 gold_error(_("%s uses VFP register arguments, output does not"),
9486 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
9488 // Merge this attribute with existing attributes.
9491 case elfcpp::Tag_CPU_raw_name
:
9492 case elfcpp::Tag_CPU_name
:
9493 // These are merged after Tag_CPU_arch.
9496 case elfcpp::Tag_ABI_optimization_goals
:
9497 case elfcpp::Tag_ABI_FP_optimization_goals
:
9498 // Use the first value seen.
9501 case elfcpp::Tag_CPU_arch
:
9503 unsigned int saved_out_attr
= out_attr
->int_value();
9504 // Merge Tag_CPU_arch and Tag_also_compatible_with.
9505 int secondary_compat
=
9506 this->get_secondary_compatible_arch(pasd
);
9507 int secondary_compat_out
=
9508 this->get_secondary_compatible_arch(
9509 this->attributes_section_data_
);
9510 out_attr
[i
].set_int_value(
9511 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
9512 &secondary_compat_out
,
9513 in_attr
[i
].int_value(),
9515 this->set_secondary_compatible_arch(this->attributes_section_data_
,
9516 secondary_compat_out
);
9518 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9519 if (out_attr
[i
].int_value() == saved_out_attr
)
9520 ; // Leave the names alone.
9521 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
9523 // The output architecture has been changed to match the
9524 // input architecture. Use the input names.
9525 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
9526 in_attr
[elfcpp::Tag_CPU_name
].string_value());
9527 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
9528 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
9532 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
9533 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
9536 // If we still don't have a value for Tag_CPU_name,
9537 // make one up now. Tag_CPU_raw_name remains blank.
9538 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
9540 const std::string cpu_name
=
9541 this->tag_cpu_name_value(out_attr
[i
].int_value());
9542 // FIXME: If we see an unknown CPU, this will be set
9543 // to "<unknown CPU n>", where n is the attribute value.
9544 // This is different from BFD, which leaves the name alone.
9545 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
9550 case elfcpp::Tag_ARM_ISA_use
:
9551 case elfcpp::Tag_THUMB_ISA_use
:
9552 case elfcpp::Tag_WMMX_arch
:
9553 case elfcpp::Tag_Advanced_SIMD_arch
:
9554 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9555 case elfcpp::Tag_ABI_FP_rounding
:
9556 case elfcpp::Tag_ABI_FP_exceptions
:
9557 case elfcpp::Tag_ABI_FP_user_exceptions
:
9558 case elfcpp::Tag_ABI_FP_number_model
:
9559 case elfcpp::Tag_VFP_HP_extension
:
9560 case elfcpp::Tag_CPU_unaligned_access
:
9561 case elfcpp::Tag_T2EE_use
:
9562 case elfcpp::Tag_Virtualization_use
:
9563 case elfcpp::Tag_MPextension_use
:
9564 // Use the largest value specified.
9565 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
9566 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9569 case elfcpp::Tag_ABI_align8_preserved
:
9570 case elfcpp::Tag_ABI_PCS_RO_data
:
9571 // Use the smallest value specified.
9572 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
9573 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9576 case elfcpp::Tag_ABI_align8_needed
:
9577 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
9578 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
9579 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
9582 // This error message should be enabled once all non-conformant
9583 // binaries in the toolchain have had the attributes set
9585 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9589 case elfcpp::Tag_ABI_FP_denormal
:
9590 case elfcpp::Tag_ABI_PCS_GOT_use
:
9592 // These tags have 0 = don't care, 1 = strong requirement,
9593 // 2 = weak requirement.
9594 static const int order_021
[3] = {0, 2, 1};
9596 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9597 // value if greater than 2 (for future-proofing).
9598 if ((in_attr
[i
].int_value() > 2
9599 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
9600 || (in_attr
[i
].int_value() <= 2
9601 && out_attr
[i
].int_value() <= 2
9602 && (order_021
[in_attr
[i
].int_value()]
9603 > order_021
[out_attr
[i
].int_value()])))
9604 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9608 case elfcpp::Tag_CPU_arch_profile
:
9609 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
9611 // 0 will merge with anything.
9612 // 'A' and 'S' merge to 'A'.
9613 // 'R' and 'S' merge to 'R'.
9614 // 'M' and 'A|R|S' is an error.
9615 if (out_attr
[i
].int_value() == 0
9616 || (out_attr
[i
].int_value() == 'S'
9617 && (in_attr
[i
].int_value() == 'A'
9618 || in_attr
[i
].int_value() == 'R')))
9619 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9620 else if (in_attr
[i
].int_value() == 0
9621 || (in_attr
[i
].int_value() == 'S'
9622 && (out_attr
[i
].int_value() == 'A'
9623 || out_attr
[i
].int_value() == 'R')))
9625 else if (parameters
->options().warn_mismatch())
9628 (_("conflicting architecture profiles %c/%c"),
9629 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
9630 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
9634 case elfcpp::Tag_VFP_arch
:
9651 // Values greater than 6 aren't defined, so just pick the
9653 if (in_attr
[i
].int_value() > 6
9654 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
9656 *out_attr
= *in_attr
;
9659 // The output uses the superset of input features
9660 // (ISA version) and registers.
9661 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
9662 vfp_versions
[out_attr
[i
].int_value()].ver
);
9663 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
9664 vfp_versions
[out_attr
[i
].int_value()].regs
);
9665 // This assumes all possible supersets are also a valid
9668 for (newval
= 6; newval
> 0; newval
--)
9670 if (regs
== vfp_versions
[newval
].regs
9671 && ver
== vfp_versions
[newval
].ver
)
9674 out_attr
[i
].set_int_value(newval
);
9677 case elfcpp::Tag_PCS_config
:
9678 if (out_attr
[i
].int_value() == 0)
9679 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9680 else if (in_attr
[i
].int_value() != 0
9681 && out_attr
[i
].int_value() != 0
9682 && parameters
->options().warn_mismatch())
9684 // It's sometimes ok to mix different configs, so this is only
9686 gold_warning(_("%s: conflicting platform configuration"), name
);
9689 case elfcpp::Tag_ABI_PCS_R9_use
:
9690 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
9691 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
9692 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
9693 && parameters
->options().warn_mismatch())
9695 gold_error(_("%s: conflicting use of R9"), name
);
9697 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
9698 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9700 case elfcpp::Tag_ABI_PCS_RW_data
:
9701 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9702 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
9703 != elfcpp::AEABI_R9_SB
)
9704 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
9705 != elfcpp::AEABI_R9_unused
)
9706 && parameters
->options().warn_mismatch())
9708 gold_error(_("%s: SB relative addressing conflicts with use "
9712 // Use the smallest value specified.
9713 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
9714 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9716 case elfcpp::Tag_ABI_PCS_wchar_t
:
9717 // FIXME: Make it possible to turn off this warning.
9718 if (out_attr
[i
].int_value()
9719 && in_attr
[i
].int_value()
9720 && out_attr
[i
].int_value() != in_attr
[i
].int_value()
9721 && parameters
->options().warn_mismatch())
9723 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9724 "use %u-byte wchar_t; use of wchar_t values "
9725 "across objects may fail"),
9726 name
, in_attr
[i
].int_value(),
9727 out_attr
[i
].int_value());
9729 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
9730 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9732 case elfcpp::Tag_ABI_enum_size
:
9733 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
9735 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
9736 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
9738 // The existing object is compatible with anything.
9739 // Use whatever requirements the new object has.
9740 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9742 // FIXME: Make it possible to turn off this warning.
9743 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
9744 && out_attr
[i
].int_value() != in_attr
[i
].int_value()
9745 && parameters
->options().warn_mismatch())
9747 unsigned int in_value
= in_attr
[i
].int_value();
9748 unsigned int out_value
= out_attr
[i
].int_value();
9749 gold_warning(_("%s uses %s enums yet the output is to use "
9750 "%s enums; use of enum values across objects "
9753 this->aeabi_enum_name(in_value
).c_str(),
9754 this->aeabi_enum_name(out_value
).c_str());
9758 case elfcpp::Tag_ABI_VFP_args
:
9761 case elfcpp::Tag_ABI_WMMX_args
:
9762 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
9763 && parameters
->options().warn_mismatch())
9765 gold_error(_("%s uses iWMMXt register arguments, output does "
9770 case Object_attribute::Tag_compatibility
:
9771 // Merged in target-independent code.
9773 case elfcpp::Tag_ABI_HardFP_use
:
9774 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9775 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
9776 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
9777 out_attr
[i
].set_int_value(3);
9778 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
9779 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9781 case elfcpp::Tag_ABI_FP_16bit_format
:
9782 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
9784 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
9785 && parameters
->options().warn_mismatch())
9786 gold_error(_("fp16 format mismatch between %s and output"),
9789 if (in_attr
[i
].int_value() != 0)
9790 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9793 case elfcpp::Tag_DIV_use
:
9794 // This tag is set to zero if we can use UDIV and SDIV in Thumb
9795 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
9796 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
9797 // CPU. We will merge as follows: If the input attribute's value
9798 // is one then the output attribute's value remains unchanged. If
9799 // the input attribute's value is zero or two then if the output
9800 // attribute's value is one the output value is set to the input
9801 // value, otherwise the output value must be the same as the
9803 if (in_attr
[i
].int_value() != 1 && out_attr
[i
].int_value() != 1)
9805 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
9807 gold_error(_("DIV usage mismatch between %s and output"),
9812 if (in_attr
[i
].int_value() != 1)
9813 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
9817 case elfcpp::Tag_MPextension_use_legacy
:
9818 // We don't output objects with Tag_MPextension_use_legacy - we
9819 // move the value to Tag_MPextension_use.
9820 if (in_attr
[i
].int_value() != 0
9821 && in_attr
[elfcpp::Tag_MPextension_use
].int_value() != 0)
9823 if (in_attr
[elfcpp::Tag_MPextension_use
].int_value()
9824 != in_attr
[i
].int_value())
9826 gold_error(_("%s has has both the current and legacy "
9827 "Tag_MPextension_use attributes"),
9832 if (in_attr
[i
].int_value()
9833 > out_attr
[elfcpp::Tag_MPextension_use
].int_value())
9834 out_attr
[elfcpp::Tag_MPextension_use
] = in_attr
[i
];
9838 case elfcpp::Tag_nodefaults
:
9839 // This tag is set if it exists, but the value is unused (and is
9840 // typically zero). We don't actually need to do anything here -
9841 // the merge happens automatically when the type flags are merged
9844 case elfcpp::Tag_also_compatible_with
:
9845 // Already done in Tag_CPU_arch.
9847 case elfcpp::Tag_conformance
:
9848 // Keep the attribute if it matches. Throw it away otherwise.
9849 // No attribute means no claim to conform.
9850 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
9851 out_attr
[i
].set_string_value("");
9856 const char* err_object
= NULL
;
9858 // The "known_obj_attributes" table does contain some undefined
9859 // attributes. Ensure that there are unused.
9860 if (out_attr
[i
].int_value() != 0
9861 || out_attr
[i
].string_value() != "")
9862 err_object
= "output";
9863 else if (in_attr
[i
].int_value() != 0
9864 || in_attr
[i
].string_value() != "")
9867 if (err_object
!= NULL
9868 && parameters
->options().warn_mismatch())
9870 // Attribute numbers >=64 (mod 128) can be safely ignored.
9872 gold_error(_("%s: unknown mandatory EABI object attribute "
9876 gold_warning(_("%s: unknown EABI object attribute %d"),
9880 // Only pass on attributes that match in both inputs.
9881 if (!in_attr
[i
].matches(out_attr
[i
]))
9883 out_attr
[i
].set_int_value(0);
9884 out_attr
[i
].set_string_value("");
9889 // If out_attr was copied from in_attr then it won't have a type yet.
9890 if (in_attr
[i
].type() && !out_attr
[i
].type())
9891 out_attr
[i
].set_type(in_attr
[i
].type());
9894 // Merge Tag_compatibility attributes and any common GNU ones.
9895 this->attributes_section_data_
->merge(name
, pasd
);
9897 // Check for any attributes not known on ARM.
9898 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
9899 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
9900 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
9901 Other_attributes
* out_other_attributes
=
9902 this->attributes_section_data_
->other_attributes(vendor
);
9903 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
9905 while (in_iter
!= in_other_attributes
->end()
9906 || out_iter
!= out_other_attributes
->end())
9908 const char* err_object
= NULL
;
9911 // The tags for each list are in numerical order.
9912 // If the tags are equal, then merge.
9913 if (out_iter
!= out_other_attributes
->end()
9914 && (in_iter
== in_other_attributes
->end()
9915 || in_iter
->first
> out_iter
->first
))
9917 // This attribute only exists in output. We can't merge, and we
9918 // don't know what the tag means, so delete it.
9919 err_object
= "output";
9920 err_tag
= out_iter
->first
;
9921 int saved_tag
= out_iter
->first
;
9922 delete out_iter
->second
;
9923 out_other_attributes
->erase(out_iter
);
9924 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
9926 else if (in_iter
!= in_other_attributes
->end()
9927 && (out_iter
!= out_other_attributes
->end()
9928 || in_iter
->first
< out_iter
->first
))
9930 // This attribute only exists in input. We can't merge, and we
9931 // don't know what the tag means, so ignore it.
9933 err_tag
= in_iter
->first
;
9936 else // The tags are equal.
9938 // As present, all attributes in the list are unknown, and
9939 // therefore can't be merged meaningfully.
9940 err_object
= "output";
9941 err_tag
= out_iter
->first
;
9943 // Only pass on attributes that match in both inputs.
9944 if (!in_iter
->second
->matches(*(out_iter
->second
)))
9946 // No match. Delete the attribute.
9947 int saved_tag
= out_iter
->first
;
9948 delete out_iter
->second
;
9949 out_other_attributes
->erase(out_iter
);
9950 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
9954 // Matched. Keep the attribute and move to the next.
9960 if (err_object
&& parameters
->options().warn_mismatch())
9962 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9963 if ((err_tag
& 127) < 64)
9965 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9966 err_object
, err_tag
);
9970 gold_warning(_("%s: unknown EABI object attribute %d"),
9971 err_object
, err_tag
);
9977 // Stub-generation methods for Target_arm.
9979 // Make a new Arm_input_section object.
9981 template<bool big_endian
>
9982 Arm_input_section
<big_endian
>*
9983 Target_arm
<big_endian
>::new_arm_input_section(
9987 Section_id
sid(relobj
, shndx
);
9989 Arm_input_section
<big_endian
>* arm_input_section
=
9990 new Arm_input_section
<big_endian
>(relobj
, shndx
);
9991 arm_input_section
->init();
9993 // Register new Arm_input_section in map for look-up.
9994 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
9995 this->arm_input_section_map_
.insert(std::make_pair(sid
, arm_input_section
));
9997 // Make sure that it we have not created another Arm_input_section
9998 // for this input section already.
9999 gold_assert(ins
.second
);
10001 return arm_input_section
;
10004 // Find the Arm_input_section object corresponding to the SHNDX-th input
10005 // section of RELOBJ.
10007 template<bool big_endian
>
10008 Arm_input_section
<big_endian
>*
10009 Target_arm
<big_endian
>::find_arm_input_section(
10011 unsigned int shndx
) const
10013 Section_id
sid(relobj
, shndx
);
10014 typename
Arm_input_section_map::const_iterator p
=
10015 this->arm_input_section_map_
.find(sid
);
10016 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
10019 // Make a new stub table.
10021 template<bool big_endian
>
10022 Stub_table
<big_endian
>*
10023 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
10025 Stub_table
<big_endian
>* stub_table
=
10026 new Stub_table
<big_endian
>(owner
);
10027 this->stub_tables_
.push_back(stub_table
);
10029 stub_table
->set_address(owner
->address() + owner
->data_size());
10030 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
10031 stub_table
->finalize_data_size();
10036 // Scan a relocation for stub generation.
10038 template<bool big_endian
>
10040 Target_arm
<big_endian
>::scan_reloc_for_stub(
10041 const Relocate_info
<32, big_endian
>* relinfo
,
10042 unsigned int r_type
,
10043 const Sized_symbol
<32>* gsym
,
10044 unsigned int r_sym
,
10045 const Symbol_value
<32>* psymval
,
10046 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
10047 Arm_address address
)
10049 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
10051 const Arm_relobj
<big_endian
>* arm_relobj
=
10052 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
10054 bool target_is_thumb
;
10055 Symbol_value
<32> symval
;
10058 // This is a global symbol. Determine if we use PLT and if the
10059 // final target is THUMB.
10060 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
10062 // This uses a PLT, change the symbol value.
10063 symval
.set_output_value(this->plt_section()->address()
10064 + gsym
->plt_offset());
10066 target_is_thumb
= false;
10068 else if (gsym
->is_undefined())
10069 // There is no need to generate a stub symbol is undefined.
10074 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
10075 || (gsym
->type() == elfcpp::STT_FUNC
10076 && !gsym
->is_undefined()
10077 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
10082 // This is a local symbol. Determine if the final target is THUMB.
10083 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
10086 // Strip LSB if this points to a THUMB target.
10087 const Arm_reloc_property
* reloc_property
=
10088 arm_reloc_property_table
->get_implemented_static_reloc_property(r_type
);
10089 gold_assert(reloc_property
!= NULL
);
10090 if (target_is_thumb
10091 && reloc_property
->uses_thumb_bit()
10092 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
10094 Arm_address stripped_value
=
10095 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
10096 symval
.set_output_value(stripped_value
);
10100 // Get the symbol value.
10101 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
10103 // Owing to pipelining, the PC relative branches below actually skip
10104 // two instructions when the branch offset is 0.
10105 Arm_address destination
;
10108 case elfcpp::R_ARM_CALL
:
10109 case elfcpp::R_ARM_JUMP24
:
10110 case elfcpp::R_ARM_PLT32
:
10112 destination
= value
+ addend
+ 8;
10114 case elfcpp::R_ARM_THM_CALL
:
10115 case elfcpp::R_ARM_THM_XPC22
:
10116 case elfcpp::R_ARM_THM_JUMP24
:
10117 case elfcpp::R_ARM_THM_JUMP19
:
10119 destination
= value
+ addend
+ 4;
10122 gold_unreachable();
10125 Reloc_stub
* stub
= NULL
;
10126 Stub_type stub_type
=
10127 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
10129 if (stub_type
!= arm_stub_none
)
10131 // Try looking up an existing stub from a stub table.
10132 Stub_table
<big_endian
>* stub_table
=
10133 arm_relobj
->stub_table(relinfo
->data_shndx
);
10134 gold_assert(stub_table
!= NULL
);
10136 // Locate stub by destination.
10137 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
10139 // Create a stub if there is not one already
10140 stub
= stub_table
->find_reloc_stub(stub_key
);
10143 // create a new stub and add it to stub table.
10144 stub
= this->stub_factory().make_reloc_stub(stub_type
);
10145 stub_table
->add_reloc_stub(stub
, stub_key
);
10148 // Record the destination address.
10149 stub
->set_destination_address(destination
10150 | (target_is_thumb
? 1 : 0));
10153 // For Cortex-A8, we need to record a relocation at 4K page boundary.
10154 if (this->fix_cortex_a8_
10155 && (r_type
== elfcpp::R_ARM_THM_JUMP24
10156 || r_type
== elfcpp::R_ARM_THM_JUMP19
10157 || r_type
== elfcpp::R_ARM_THM_CALL
10158 || r_type
== elfcpp::R_ARM_THM_XPC22
)
10159 && (address
& 0xfffU
) == 0xffeU
)
10161 // Found a candidate. Note we haven't checked the destination is
10162 // within 4K here: if we do so (and don't create a record) we can't
10163 // tell that a branch should have been relocated when scanning later.
10164 this->cortex_a8_relocs_info_
[address
] =
10165 new Cortex_a8_reloc(stub
, r_type
,
10166 destination
| (target_is_thumb
? 1 : 0));
10170 // This function scans a relocation sections for stub generation.
10171 // The template parameter Relocate must be a class type which provides
10172 // a single function, relocate(), which implements the machine
10173 // specific part of a relocation.
10175 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10176 // SHT_REL or SHT_RELA.
10178 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10179 // of relocs. OUTPUT_SECTION is the output section.
10180 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10181 // mapped to output offsets.
10183 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10184 // VIEW_SIZE is the size. These refer to the input section, unless
10185 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10186 // the output section.
10188 template<bool big_endian
>
10189 template<int sh_type
>
10191 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
10192 const Relocate_info
<32, big_endian
>* relinfo
,
10193 const unsigned char* prelocs
,
10194 size_t reloc_count
,
10195 Output_section
* output_section
,
10196 bool needs_special_offset_handling
,
10197 const unsigned char* view
,
10198 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
10201 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
10202 const int reloc_size
=
10203 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
10205 Arm_relobj
<big_endian
>* arm_object
=
10206 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
10207 unsigned int local_count
= arm_object
->local_symbol_count();
10209 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
10211 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
10213 Reltype
reloc(prelocs
);
10215 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
10216 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
10217 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
10219 r_type
= this->get_real_reloc_type(r_type
);
10221 // Only a few relocation types need stubs.
10222 if ((r_type
!= elfcpp::R_ARM_CALL
)
10223 && (r_type
!= elfcpp::R_ARM_JUMP24
)
10224 && (r_type
!= elfcpp::R_ARM_PLT32
)
10225 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
10226 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
10227 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
10228 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
)
10229 && (r_type
!= elfcpp::R_ARM_V4BX
))
10232 section_offset_type offset
=
10233 convert_to_section_size_type(reloc
.get_r_offset());
10235 if (needs_special_offset_handling
)
10237 offset
= output_section
->output_offset(relinfo
->object
,
10238 relinfo
->data_shndx
,
10244 // Create a v4bx stub if --fix-v4bx-interworking is used.
10245 if (r_type
== elfcpp::R_ARM_V4BX
)
10247 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
)
10249 // Get the BX instruction.
10250 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
10251 const Valtype
* wv
=
10252 reinterpret_cast<const Valtype
*>(view
+ offset
);
10253 elfcpp::Elf_types
<32>::Elf_Swxword insn
=
10254 elfcpp::Swap
<32, big_endian
>::readval(wv
);
10255 const uint32_t reg
= (insn
& 0xf);
10259 // Try looking up an existing stub from a stub table.
10260 Stub_table
<big_endian
>* stub_table
=
10261 arm_object
->stub_table(relinfo
->data_shndx
);
10262 gold_assert(stub_table
!= NULL
);
10264 if (stub_table
->find_arm_v4bx_stub(reg
) == NULL
)
10266 // create a new stub and add it to stub table.
10267 Arm_v4bx_stub
* stub
=
10268 this->stub_factory().make_arm_v4bx_stub(reg
);
10269 gold_assert(stub
!= NULL
);
10270 stub_table
->add_arm_v4bx_stub(stub
);
10278 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
10279 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
10280 stub_addend_reader(r_type
, view
+ offset
, reloc
);
10282 const Sized_symbol
<32>* sym
;
10284 Symbol_value
<32> symval
;
10285 const Symbol_value
<32> *psymval
;
10286 if (r_sym
< local_count
)
10289 psymval
= arm_object
->local_symbol(r_sym
);
10291 // If the local symbol belongs to a section we are discarding,
10292 // and that section is a debug section, try to find the
10293 // corresponding kept section and map this symbol to its
10294 // counterpart in the kept section. The symbol must not
10295 // correspond to a section we are folding.
10297 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
10299 && shndx
!= elfcpp::SHN_UNDEF
10300 && !arm_object
->is_section_included(shndx
)
10301 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
10303 if (comdat_behavior
== CB_UNDETERMINED
)
10306 arm_object
->section_name(relinfo
->data_shndx
);
10307 comdat_behavior
= get_comdat_behavior(name
.c_str());
10309 if (comdat_behavior
== CB_PRETEND
)
10312 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
10313 arm_object
->map_to_kept_section(shndx
, &found
);
10315 symval
.set_output_value(value
+ psymval
->input_value());
10317 symval
.set_output_value(0);
10321 symval
.set_output_value(0);
10323 symval
.set_no_output_symtab_entry();
10329 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
10330 gold_assert(gsym
!= NULL
);
10331 if (gsym
->is_forwarder())
10332 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
10334 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
10335 if (sym
->has_symtab_index())
10336 symval
.set_output_symtab_index(sym
->symtab_index());
10338 symval
.set_no_output_symtab_entry();
10340 // We need to compute the would-be final value of this global
10342 const Symbol_table
* symtab
= relinfo
->symtab
;
10343 const Sized_symbol
<32>* sized_symbol
=
10344 symtab
->get_sized_symbol
<32>(gsym
);
10345 Symbol_table::Compute_final_value_status status
;
10346 Arm_address value
=
10347 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
10349 // Skip this if the symbol has not output section.
10350 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
10353 symval
.set_output_value(value
);
10357 // If symbol is a section symbol, we don't know the actual type of
10358 // destination. Give up.
10359 if (psymval
->is_section_symbol())
10362 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
10363 addend
, view_address
+ offset
);
10367 // Scan an input section for stub generation.
10369 template<bool big_endian
>
10371 Target_arm
<big_endian
>::scan_section_for_stubs(
10372 const Relocate_info
<32, big_endian
>* relinfo
,
10373 unsigned int sh_type
,
10374 const unsigned char* prelocs
,
10375 size_t reloc_count
,
10376 Output_section
* output_section
,
10377 bool needs_special_offset_handling
,
10378 const unsigned char* view
,
10379 Arm_address view_address
,
10380 section_size_type view_size
)
10382 if (sh_type
== elfcpp::SHT_REL
)
10383 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
10388 needs_special_offset_handling
,
10392 else if (sh_type
== elfcpp::SHT_RELA
)
10393 // We do not support RELA type relocations yet. This is provided for
10395 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
10400 needs_special_offset_handling
,
10405 gold_unreachable();
10408 // Group input sections for stub generation.
10410 // We goup input sections in an output sections so that the total size,
10411 // including any padding space due to alignment is smaller than GROUP_SIZE
10412 // unless the only input section in group is bigger than GROUP_SIZE already.
10413 // Then an ARM stub table is created to follow the last input section
10414 // in group. For each group an ARM stub table is created an is placed
10415 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
10416 // extend the group after the stub table.
10418 template<bool big_endian
>
10420 Target_arm
<big_endian
>::group_sections(
10422 section_size_type group_size
,
10423 bool stubs_always_after_branch
)
10425 // Group input sections and insert stub table
10426 Layout::Section_list section_list
;
10427 layout
->get_allocated_sections(§ion_list
);
10428 for (Layout::Section_list::const_iterator p
= section_list
.begin();
10429 p
!= section_list
.end();
10432 Arm_output_section
<big_endian
>* output_section
=
10433 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
10434 output_section
->group_sections(group_size
, stubs_always_after_branch
,
10439 // Relaxation hook. This is where we do stub generation.
10441 template<bool big_endian
>
10443 Target_arm
<big_endian
>::do_relax(
10445 const Input_objects
* input_objects
,
10446 Symbol_table
* symtab
,
10449 // No need to generate stubs if this is a relocatable link.
10450 gold_assert(!parameters
->options().relocatable());
10452 // If this is the first pass, we need to group input sections into
10454 bool done_exidx_fixup
= false;
10455 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
10458 // Determine the stub group size. The group size is the absolute
10459 // value of the parameter --stub-group-size. If --stub-group-size
10460 // is passed a negative value, we restict stubs to be always after
10461 // the stubbed branches.
10462 int32_t stub_group_size_param
=
10463 parameters
->options().stub_group_size();
10464 bool stubs_always_after_branch
= stub_group_size_param
< 0;
10465 section_size_type stub_group_size
= abs(stub_group_size_param
);
10467 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
10468 // page as the first half of a 32-bit branch straddling two 4K pages.
10469 // This is a crude way of enforcing that.
10470 if (this->fix_cortex_a8_
)
10471 stubs_always_after_branch
= true;
10473 if (stub_group_size
== 1)
10476 // Thumb branch range is +-4MB has to be used as the default
10477 // maximum size (a given section can contain both ARM and Thumb
10478 // code, so the worst case has to be taken into account). If we are
10479 // fixing cortex-a8 errata, the branch range has to be even smaller,
10480 // since wide conditional branch has a range of +-1MB only.
10482 // This value is 24K less than that, which allows for 2025
10483 // 12-byte stubs. If we exceed that, then we will fail to link.
10484 // The user will have to relink with an explicit group size
10486 if (this->fix_cortex_a8_
)
10487 stub_group_size
= 1024276;
10489 stub_group_size
= 4170000;
10492 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
10494 // Also fix .ARM.exidx section coverage.
10495 Output_section
* os
= layout
->find_output_section(".ARM.exidx");
10496 if (os
!= NULL
&& os
->type() == elfcpp::SHT_ARM_EXIDX
)
10498 Arm_output_section
<big_endian
>* exidx_output_section
=
10499 Arm_output_section
<big_endian
>::as_arm_output_section(os
);
10500 this->fix_exidx_coverage(layout
, exidx_output_section
, symtab
);
10501 done_exidx_fixup
= true;
10506 // If this is not the first pass, addresses and file offsets have
10507 // been reset at this point, set them here.
10508 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
10509 sp
!= this->stub_tables_
.end();
10512 Arm_input_section
<big_endian
>* owner
= (*sp
)->owner();
10513 off_t off
= align_address(owner
->original_size(),
10514 (*sp
)->addralign());
10515 (*sp
)->set_address_and_file_offset(owner
->address() + off
,
10516 owner
->offset() + off
);
10520 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
10521 // beginning of each relaxation pass, just blow away all the stubs.
10522 // Alternatively, we could selectively remove only the stubs and reloc
10523 // information for code sections that have moved since the last pass.
10524 // That would require more book-keeping.
10525 if (this->fix_cortex_a8_
)
10527 // Clear all Cortex-A8 reloc information.
10528 for (typename
Cortex_a8_relocs_info::const_iterator p
=
10529 this->cortex_a8_relocs_info_
.begin();
10530 p
!= this->cortex_a8_relocs_info_
.end();
10533 this->cortex_a8_relocs_info_
.clear();
10535 // Remove all Cortex-A8 stubs.
10536 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
10537 sp
!= this->stub_tables_
.end();
10539 (*sp
)->remove_all_cortex_a8_stubs();
10542 // Scan relocs for relocation stubs
10543 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
10544 op
!= input_objects
->relobj_end();
10547 Arm_relobj
<big_endian
>* arm_relobj
=
10548 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
10549 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
10552 // Check all stub tables to see if any of them have their data sizes
10553 // or addresses alignments changed. These are the only things that
10555 bool any_stub_table_changed
= false;
10556 Unordered_set
<const Output_section
*> sections_needing_adjustment
;
10557 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
10558 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
10561 if ((*sp
)->update_data_size_and_addralign())
10563 // Update data size of stub table owner.
10564 Arm_input_section
<big_endian
>* owner
= (*sp
)->owner();
10565 uint64_t address
= owner
->address();
10566 off_t offset
= owner
->offset();
10567 owner
->reset_address_and_file_offset();
10568 owner
->set_address_and_file_offset(address
, offset
);
10570 sections_needing_adjustment
.insert(owner
->output_section());
10571 any_stub_table_changed
= true;
10575 // Output_section_data::output_section() returns a const pointer but we
10576 // need to update output sections, so we record all output sections needing
10577 // update above and scan the sections here to find out what sections need
10579 for(Layout::Section_list::const_iterator p
= layout
->section_list().begin();
10580 p
!= layout
->section_list().end();
10583 if (sections_needing_adjustment
.find(*p
)
10584 != sections_needing_adjustment
.end())
10585 (*p
)->set_section_offsets_need_adjustment();
10588 // Stop relaxation if no EXIDX fix-up and no stub table change.
10589 bool continue_relaxation
= done_exidx_fixup
|| any_stub_table_changed
;
10591 // Finalize the stubs in the last relaxation pass.
10592 if (!continue_relaxation
)
10594 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
10595 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
10597 (*sp
)->finalize_stubs();
10599 // Update output local symbol counts of objects if necessary.
10600 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
10601 op
!= input_objects
->relobj_end();
10604 Arm_relobj
<big_endian
>* arm_relobj
=
10605 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
10607 // Update output local symbol counts. We need to discard local
10608 // symbols defined in parts of input sections that are discarded by
10610 if (arm_relobj
->output_local_symbol_count_needs_update())
10611 arm_relobj
->update_output_local_symbol_count();
10615 return continue_relaxation
;
10618 // Relocate a stub.
10620 template<bool big_endian
>
10622 Target_arm
<big_endian
>::relocate_stub(
10624 const Relocate_info
<32, big_endian
>* relinfo
,
10625 Output_section
* output_section
,
10626 unsigned char* view
,
10627 Arm_address address
,
10628 section_size_type view_size
)
10631 const Stub_template
* stub_template
= stub
->stub_template();
10632 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
10634 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
10635 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
10637 unsigned int r_type
= insn
->r_type();
10638 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
10639 section_size_type reloc_size
= insn
->size();
10640 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
10642 // This is the address of the stub destination.
10643 Arm_address target
= stub
->reloc_target(i
) + insn
->reloc_addend();
10644 Symbol_value
<32> symval
;
10645 symval
.set_output_value(target
);
10647 // Synthesize a fake reloc just in case. We don't have a symbol so
10649 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
10650 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
10651 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
10652 reloc_write
.put_r_offset(reloc_offset
);
10653 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
10654 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
10656 relocate
.relocate(relinfo
, this, output_section
,
10657 this->fake_relnum_for_stubs
, rel
, r_type
,
10658 NULL
, &symval
, view
+ reloc_offset
,
10659 address
+ reloc_offset
, reloc_size
);
10663 // Determine whether an object attribute tag takes an integer, a
10666 template<bool big_endian
>
10668 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
10670 if (tag
== Object_attribute::Tag_compatibility
)
10671 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10672 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
10673 else if (tag
== elfcpp::Tag_nodefaults
)
10674 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10675 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
10676 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
10677 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
10679 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
10681 return ((tag
& 1) != 0
10682 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10683 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
10686 // Reorder attributes.
10688 // The ABI defines that Tag_conformance should be emitted first, and that
10689 // Tag_nodefaults should be second (if either is defined). This sets those
10690 // two positions, and bumps up the position of all the remaining tags to
10693 template<bool big_endian
>
10695 Target_arm
<big_endian
>::do_attributes_order(int num
) const
10697 // Reorder the known object attributes in output. We want to move
10698 // Tag_conformance to position 4 and Tag_conformance to position 5
10699 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10701 return elfcpp::Tag_conformance
;
10703 return elfcpp::Tag_nodefaults
;
10704 if ((num
- 2) < elfcpp::Tag_nodefaults
)
10706 if ((num
- 1) < elfcpp::Tag_conformance
)
10711 // Scan a span of THUMB code for Cortex-A8 erratum.
10713 template<bool big_endian
>
10715 Target_arm
<big_endian
>::scan_span_for_cortex_a8_erratum(
10716 Arm_relobj
<big_endian
>* arm_relobj
,
10717 unsigned int shndx
,
10718 section_size_type span_start
,
10719 section_size_type span_end
,
10720 const unsigned char* view
,
10721 Arm_address address
)
10723 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10725 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10726 // The branch target is in the same 4KB region as the
10727 // first half of the branch.
10728 // The instruction before the branch is a 32-bit
10729 // length non-branch instruction.
10730 section_size_type i
= span_start
;
10731 bool last_was_32bit
= false;
10732 bool last_was_branch
= false;
10733 while (i
< span_end
)
10735 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
10736 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
+ i
);
10737 uint32_t insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
10738 bool is_blx
= false, is_b
= false;
10739 bool is_bl
= false, is_bcc
= false;
10741 bool insn_32bit
= (insn
& 0xe000) == 0xe000 && (insn
& 0x1800) != 0x0000;
10744 // Load the rest of the insn (in manual-friendly order).
10745 insn
= (insn
<< 16) | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
10747 // Encoding T4: B<c>.W.
10748 is_b
= (insn
& 0xf800d000U
) == 0xf0009000U
;
10749 // Encoding T1: BL<c>.W.
10750 is_bl
= (insn
& 0xf800d000U
) == 0xf000d000U
;
10751 // Encoding T2: BLX<c>.W.
10752 is_blx
= (insn
& 0xf800d000U
) == 0xf000c000U
;
10753 // Encoding T3: B<c>.W (not permitted in IT block).
10754 is_bcc
= ((insn
& 0xf800d000U
) == 0xf0008000U
10755 && (insn
& 0x07f00000U
) != 0x03800000U
);
10758 bool is_32bit_branch
= is_b
|| is_bl
|| is_blx
|| is_bcc
;
10760 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10761 // page boundary and it follows 32-bit non-branch instruction,
10762 // we need to work around.
10763 if (is_32bit_branch
10764 && ((address
+ i
) & 0xfffU
) == 0xffeU
10766 && !last_was_branch
)
10768 // Check to see if there is a relocation stub for this branch.
10769 bool force_target_arm
= false;
10770 bool force_target_thumb
= false;
10771 const Cortex_a8_reloc
* cortex_a8_reloc
= NULL
;
10772 Cortex_a8_relocs_info::const_iterator p
=
10773 this->cortex_a8_relocs_info_
.find(address
+ i
);
10775 if (p
!= this->cortex_a8_relocs_info_
.end())
10777 cortex_a8_reloc
= p
->second
;
10778 bool target_is_thumb
= (cortex_a8_reloc
->destination() & 1) != 0;
10780 if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
10781 && !target_is_thumb
)
10782 force_target_arm
= true;
10783 else if (cortex_a8_reloc
->r_type() == elfcpp::R_ARM_THM_CALL
10784 && target_is_thumb
)
10785 force_target_thumb
= true;
10789 Stub_type stub_type
= arm_stub_none
;
10791 // Check if we have an offending branch instruction.
10792 uint16_t upper_insn
= (insn
>> 16) & 0xffffU
;
10793 uint16_t lower_insn
= insn
& 0xffffU
;
10794 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
10796 if (cortex_a8_reloc
!= NULL
10797 && cortex_a8_reloc
->reloc_stub() != NULL
)
10798 // We've already made a stub for this instruction, e.g.
10799 // it's a long branch or a Thumb->ARM stub. Assume that
10800 // stub will suffice to work around the A8 erratum (see
10801 // setting of always_after_branch above).
10805 offset
= RelocFuncs::thumb32_cond_branch_offset(upper_insn
,
10807 stub_type
= arm_stub_a8_veneer_b_cond
;
10809 else if (is_b
|| is_bl
|| is_blx
)
10811 offset
= RelocFuncs::thumb32_branch_offset(upper_insn
,
10816 stub_type
= (is_blx
10817 ? arm_stub_a8_veneer_blx
10819 ? arm_stub_a8_veneer_bl
10820 : arm_stub_a8_veneer_b
));
10823 if (stub_type
!= arm_stub_none
)
10825 Arm_address pc_for_insn
= address
+ i
+ 4;
10827 // The original instruction is a BL, but the target is
10828 // an ARM instruction. If we were not making a stub,
10829 // the BL would have been converted to a BLX. Use the
10830 // BLX stub instead in that case.
10831 if (this->may_use_blx() && force_target_arm
10832 && stub_type
== arm_stub_a8_veneer_bl
)
10834 stub_type
= arm_stub_a8_veneer_blx
;
10838 // Conversely, if the original instruction was
10839 // BLX but the target is Thumb mode, use the BL stub.
10840 else if (force_target_thumb
10841 && stub_type
== arm_stub_a8_veneer_blx
)
10843 stub_type
= arm_stub_a8_veneer_bl
;
10851 // If we found a relocation, use the proper destination,
10852 // not the offset in the (unrelocated) instruction.
10853 // Note this is always done if we switched the stub type above.
10854 if (cortex_a8_reloc
!= NULL
)
10855 offset
= (off_t
) (cortex_a8_reloc
->destination() - pc_for_insn
);
10857 Arm_address target
= (pc_for_insn
+ offset
) | (is_blx
? 0 : 1);
10859 // Add a new stub if destination address in in the same page.
10860 if (((address
+ i
) & ~0xfffU
) == (target
& ~0xfffU
))
10862 Cortex_a8_stub
* stub
=
10863 this->stub_factory_
.make_cortex_a8_stub(stub_type
,
10867 Stub_table
<big_endian
>* stub_table
=
10868 arm_relobj
->stub_table(shndx
);
10869 gold_assert(stub_table
!= NULL
);
10870 stub_table
->add_cortex_a8_stub(address
+ i
, stub
);
10875 i
+= insn_32bit
? 4 : 2;
10876 last_was_32bit
= insn_32bit
;
10877 last_was_branch
= is_32bit_branch
;
10881 // Apply the Cortex-A8 workaround.
10883 template<bool big_endian
>
10885 Target_arm
<big_endian
>::apply_cortex_a8_workaround(
10886 const Cortex_a8_stub
* stub
,
10887 Arm_address stub_address
,
10888 unsigned char* insn_view
,
10889 Arm_address insn_address
)
10891 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
10892 Valtype
* wv
= reinterpret_cast<Valtype
*>(insn_view
);
10893 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
10894 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
10895 off_t branch_offset
= stub_address
- (insn_address
+ 4);
10897 typedef struct Arm_relocate_functions
<big_endian
> RelocFuncs
;
10898 switch (stub
->stub_template()->type())
10900 case arm_stub_a8_veneer_b_cond
:
10901 gold_assert(!utils::has_overflow
<21>(branch_offset
));
10902 upper_insn
= RelocFuncs::thumb32_cond_branch_upper(upper_insn
,
10904 lower_insn
= RelocFuncs::thumb32_cond_branch_lower(lower_insn
,
10908 case arm_stub_a8_veneer_b
:
10909 case arm_stub_a8_veneer_bl
:
10910 case arm_stub_a8_veneer_blx
:
10911 if ((lower_insn
& 0x5000U
) == 0x4000U
)
10912 // For a BLX instruction, make sure that the relocation is
10913 // rounded up to a word boundary. This follows the semantics of
10914 // the instruction which specifies that bit 1 of the target
10915 // address will come from bit 1 of the base address.
10916 branch_offset
= (branch_offset
+ 2) & ~3;
10918 // Put BRANCH_OFFSET back into the insn.
10919 gold_assert(!utils::has_overflow
<25>(branch_offset
));
10920 upper_insn
= RelocFuncs::thumb32_branch_upper(upper_insn
, branch_offset
);
10921 lower_insn
= RelocFuncs::thumb32_branch_lower(lower_insn
, branch_offset
);
10925 gold_unreachable();
10928 // Put the relocated value back in the object file:
10929 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
10930 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
10933 template<bool big_endian
>
10934 class Target_selector_arm
: public Target_selector
10937 Target_selector_arm()
10938 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
10939 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
10943 do_instantiate_target()
10944 { return new Target_arm
<big_endian
>(); }
10947 // Fix .ARM.exidx section coverage.
10949 template<bool big_endian
>
10951 Target_arm
<big_endian
>::fix_exidx_coverage(
10953 Arm_output_section
<big_endian
>* exidx_section
,
10954 Symbol_table
* symtab
)
10956 // We need to look at all the input sections in output in ascending
10957 // order of of output address. We do that by building a sorted list
10958 // of output sections by addresses. Then we looks at the output sections
10959 // in order. The input sections in an output section are already sorted
10960 // by addresses within the output section.
10962 typedef std::set
<Output_section
*, output_section_address_less_than
>
10963 Sorted_output_section_list
;
10964 Sorted_output_section_list sorted_output_sections
;
10965 Layout::Section_list section_list
;
10966 layout
->get_allocated_sections(§ion_list
);
10967 for (Layout::Section_list::const_iterator p
= section_list
.begin();
10968 p
!= section_list
.end();
10971 // We only care about output sections that contain executable code.
10972 if (((*p
)->flags() & elfcpp::SHF_EXECINSTR
) != 0)
10973 sorted_output_sections
.insert(*p
);
10976 // Go over the output sections in ascending order of output addresses.
10977 typedef typename Arm_output_section
<big_endian
>::Text_section_list
10979 Text_section_list sorted_text_sections
;
10980 for(typename
Sorted_output_section_list::iterator p
=
10981 sorted_output_sections
.begin();
10982 p
!= sorted_output_sections
.end();
10985 Arm_output_section
<big_endian
>* arm_output_section
=
10986 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
10987 arm_output_section
->append_text_sections_to_list(&sorted_text_sections
);
10990 exidx_section
->fix_exidx_coverage(layout
, sorted_text_sections
, symtab
,
10991 merge_exidx_entries());
10994 Target_selector_arm
<false> target_selector_arm
;
10995 Target_selector_arm
<true> target_selector_armbe
;
10997 } // End anonymous namespace.