1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
35 #include "parameters.h"
42 #include "copy-relocs.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
55 template<bool big_endian
>
56 class Output_data_plt_arm
;
58 template<bool big_endian
>
61 template<bool big_endian
>
62 class Arm_input_section
;
64 template<bool big_endian
>
68 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
70 // Maximum branch offsets for ARM, THUMB and THUMB2.
71 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
72 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
73 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
74 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
75 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
76 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
78 // The arm target class.
80 // This is a very simple port of gold for ARM-EABI. It is intended for
81 // supporting Android only for the time being. Only these relocation types
110 // R_ARM_THM_MOVW_ABS_NC
111 // R_ARM_THM_MOVT_ABS
112 // R_ARM_MOVW_PREL_NC
114 // R_ARM_THM_MOVW_PREL_NC
115 // R_ARM_THM_MOVT_PREL
118 // - Generate various branch stubs.
119 // - Support interworking.
120 // - Define section symbols __exidx_start and __exidx_stop.
121 // - Support more relocation types as needed.
122 // - Make PLTs more flexible for different architecture features like
124 // There are probably a lot more.
126 // Instruction template class. This class is similar to the insn_sequence
127 // struct in bfd/elf32-arm.c.
132 // Types of instruction templates.
141 // Factory methods to create instrunction templates in different formats.
143 static const Insn_template
144 thumb16_insn(uint32_t data
)
145 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
147 // A bit of a hack. A Thumb conditional branch, in which the proper
148 // condition is inserted when we build the stub.
149 static const Insn_template
150 thumb16_bcond_insn(uint32_t data
)
151 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 1); }
153 static const Insn_template
154 thumb32_insn(uint32_t data
)
155 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
157 static const Insn_template
158 thumb32_b_insn(uint32_t data
, int reloc_addend
)
160 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
164 static const Insn_template
165 arm_insn(uint32_t data
)
166 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
168 static const Insn_template
169 arm_rel_insn(unsigned data
, int reloc_addend
)
170 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
172 static const Insn_template
173 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
174 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
176 // Accessors. This class is used for read-only objects so no modifiers
181 { return this->data_
; }
183 // Return the instruction sequence type of this.
186 { return this->type_
; }
188 // Return the ARM relocation type of this.
191 { return this->r_type_
; }
195 { return this->reloc_addend_
; }
197 // Return size of instrunction template in bytes.
201 // Return byte-alignment of instrunction template.
206 // We make the constructor private to ensure that only the factory
209 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
210 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
213 // Instruction specific data. This is used to store information like
214 // some of the instruction bits.
216 // Instruction template type.
218 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
219 unsigned int r_type_
;
220 // Relocation addend.
221 int32_t reloc_addend_
;
224 // Macro for generating code to stub types. One entry per long/short
228 DEF_STUB(long_branch_any_any) \
229 DEF_STUB(long_branch_v4t_arm_thumb) \
230 DEF_STUB(long_branch_thumb_only) \
231 DEF_STUB(long_branch_v4t_thumb_thumb) \
232 DEF_STUB(long_branch_v4t_thumb_arm) \
233 DEF_STUB(short_branch_v4t_thumb_arm) \
234 DEF_STUB(long_branch_any_arm_pic) \
235 DEF_STUB(long_branch_any_thumb_pic) \
236 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
237 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
238 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
239 DEF_STUB(long_branch_thumb_only_pic) \
240 DEF_STUB(a8_veneer_b_cond) \
241 DEF_STUB(a8_veneer_b) \
242 DEF_STUB(a8_veneer_bl) \
243 DEF_STUB(a8_veneer_blx)
247 #define DEF_STUB(x) arm_stub_##x,
253 // First reloc stub type.
254 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
255 // Last reloc stub type.
256 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
258 // First Cortex-A8 stub type.
259 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
260 // Last Cortex-A8 stub type.
261 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
264 arm_stub_type_last
= arm_stub_a8_veneer_blx
268 // Stub template class. Templates are meant to be read-only objects.
269 // A stub template for a stub type contains all read-only attributes
270 // common to all stubs of the same type.
275 Stub_template(Stub_type
, const Insn_template
*, size_t);
283 { return this->type_
; }
285 // Return an array of instruction templates.
288 { return this->insns_
; }
290 // Return size of template in number of instructions.
293 { return this->insn_count_
; }
295 // Return size of template in bytes.
298 { return this->size_
; }
300 // Return alignment of the stub template.
303 { return this->alignment_
; }
305 // Return whether entry point is in thumb mode.
307 entry_in_thumb_mode() const
308 { return this->entry_in_thumb_mode_
; }
310 // Return number of relocations in this template.
313 { return this->relocs_
.size(); }
315 // Return index of the I-th instruction with relocation.
317 reloc_insn_index(size_t i
) const
319 gold_assert(i
< this->relocs_
.size());
320 return this->relocs_
[i
].first
;
323 // Return the offset of the I-th instruction with relocation from the
324 // beginning of the stub.
326 reloc_offset(size_t i
) const
328 gold_assert(i
< this->relocs_
.size());
329 return this->relocs_
[i
].second
;
333 // This contains information about an instruction template with a relocation
334 // and its offset from start of stub.
335 typedef std::pair
<size_t, section_size_type
> Reloc
;
337 // A Stub_template may not be copied. We want to share templates as much
339 Stub_template(const Stub_template
&);
340 Stub_template
& operator=(const Stub_template
&);
344 // Points to an array of Insn_templates.
345 const Insn_template
* insns_
;
346 // Number of Insn_templates in insns_[].
348 // Size of templated instructions in bytes.
350 // Alignment of templated instructions.
352 // Flag to indicate if entry is in thumb mode.
353 bool entry_in_thumb_mode_
;
354 // A table of reloc instruction indices and offsets. We can find these by
355 // looking at the instruction templates but we pre-compute and then stash
356 // them here for speed.
357 std::vector
<Reloc
> relocs_
;
361 // A class for code stubs. This is a base class for different type of
362 // stubs used in the ARM target.
368 static const section_offset_type invalid_offset
=
369 static_cast<section_offset_type
>(-1);
372 Stub(const Stub_template
* stub_template
)
373 : stub_template_(stub_template
), offset_(invalid_offset
)
380 // Return the stub template.
382 stub_template() const
383 { return this->stub_template_
; }
385 // Return offset of code stub from beginning of its containing stub table.
389 gold_assert(this->offset_
!= invalid_offset
);
390 return this->offset_
;
393 // Set offset of code stub from beginning of its containing stub table.
395 set_offset(section_offset_type offset
)
396 { this->offset_
= offset
; }
398 // Return the relocation target address of the i-th relocation in the
399 // stub. This must be defined in a child class.
401 reloc_target(size_t i
)
402 { return this->do_reloc_target(i
); }
404 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
406 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
407 { this->do_write(view
, view_size
, big_endian
); }
410 // This must be defined in the child class.
412 do_reloc_target(size_t) = 0;
414 // This must be defined in the child class.
416 do_write(unsigned char*, section_size_type
, bool) = 0;
420 const Stub_template
* stub_template_
;
421 // Offset within the section of containing this stub.
422 section_offset_type offset_
;
425 // Reloc stub class. These are stubs we use to fix up relocation because
426 // of limited branch ranges.
428 class Reloc_stub
: public Stub
431 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
432 // We assume we never jump to this address.
433 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
435 // Return destination address.
437 destination_address() const
439 gold_assert(this->destination_address_
!= this->invalid_address
);
440 return this->destination_address_
;
443 // Set destination address.
445 set_destination_address(Arm_address address
)
447 gold_assert(address
!= this->invalid_address
);
448 this->destination_address_
= address
;
451 // Reset destination address.
453 reset_destination_address()
454 { this->destination_address_
= this->invalid_address
; }
456 // Determine stub type for a branch of a relocation of R_TYPE going
457 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
458 // the branch target is a thumb instruction. TARGET is used for look
459 // up ARM-specific linker settings.
461 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
462 Arm_address branch_target
, bool target_is_thumb
);
464 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
465 // and an addend. Since we treat global and local symbol differently, we
466 // use a Symbol object for a global symbol and a object-index pair for
471 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
472 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
473 // and R_SYM must not be invalid_index.
474 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
475 unsigned int r_sym
, int32_t addend
)
476 : stub_type_(stub_type
), addend_(addend
)
480 this->r_sym_
= Reloc_stub::invalid_index
;
481 this->u_
.symbol
= symbol
;
485 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
486 this->r_sym_
= r_sym
;
487 this->u_
.relobj
= relobj
;
494 // Accessors: Keys are meant to be read-only object so no modifiers are
500 { return this->stub_type_
; }
502 // Return the local symbol index or invalid_index.
505 { return this->r_sym_
; }
507 // Return the symbol if there is one.
510 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
512 // Return the relobj if there is one.
515 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
517 // Whether this equals to another key k.
519 eq(const Key
& k
) const
521 return ((this->stub_type_
== k
.stub_type_
)
522 && (this->r_sym_
== k
.r_sym_
)
523 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
524 ? (this->u_
.relobj
== k
.u_
.relobj
)
525 : (this->u_
.symbol
== k
.u_
.symbol
))
526 && (this->addend_
== k
.addend_
));
529 // Return a hash value.
533 return (this->stub_type_
535 ^ gold::string_hash
<char>(
536 (this->r_sym_
!= Reloc_stub::invalid_index
)
537 ? this->u_
.relobj
->name().c_str()
538 : this->u_
.symbol
->name())
542 // Functors for STL associative containers.
546 operator()(const Key
& k
) const
547 { return k
.hash_value(); }
553 operator()(const Key
& k1
, const Key
& k2
) const
554 { return k1
.eq(k2
); }
557 // Name of key. This is mainly for debugging.
563 Stub_type stub_type_
;
564 // If this is a local symbol, this is the index in the defining object.
565 // Otherwise, it is invalid_index for a global symbol.
567 // If r_sym_ is invalid index. This points to a global symbol.
568 // Otherwise, this points a relobj. We used the unsized and target
569 // independent Symbol and Relobj classes instead of Arm_symbol and
570 // Arm_relobj. This is done to avoid making the stub class a template
571 // as most of the stub machinery is endianity-neutral. However, it
572 // may require a bit of casting done by users of this class.
575 const Symbol
* symbol
;
576 const Relobj
* relobj
;
578 // Addend associated with a reloc.
583 // Reloc_stubs are created via a stub factory. So these are protected.
584 Reloc_stub(const Stub_template
* stub_template
)
585 : Stub(stub_template
), destination_address_(invalid_address
)
591 friend class Stub_factory
;
594 // Return the relocation target address of the i-th relocation in the
597 do_reloc_target(size_t i
)
599 // All reloc stub have only one relocation.
601 return this->destination_address_
;
604 // A template to implement do_write below.
605 template<bool big_endian
>
607 do_fixed_endian_write(unsigned char*, section_size_type
);
611 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
);
613 // Address of destination.
614 Arm_address destination_address_
;
617 // Stub factory class.
622 // Return the unique instance of this class.
623 static const Stub_factory
&
626 static Stub_factory singleton
;
630 // Make a relocation stub.
632 make_reloc_stub(Stub_type stub_type
) const
634 gold_assert(stub_type
>= arm_stub_reloc_first
635 && stub_type
<= arm_stub_reloc_last
);
636 return new Reloc_stub(this->stub_templates_
[stub_type
]);
640 // Constructor and destructor are protected since we only return a single
641 // instance created in Stub_factory::get_instance().
645 // A Stub_factory may not be copied since it is a singleton.
646 Stub_factory(const Stub_factory
&);
647 Stub_factory
& operator=(Stub_factory
&);
649 // Stub templates. These are initialized in the constructor.
650 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
653 // A class to hold stubs for the ARM target.
655 template<bool big_endian
>
656 class Stub_table
: public Output_data
659 Stub_table(Arm_input_section
<big_endian
>* owner
)
660 : Output_data(), addralign_(1), owner_(owner
), has_been_changed_(false),
667 // Owner of this stub table.
668 Arm_input_section
<big_endian
>*
670 { return this->owner_
; }
672 // Whether this stub table is empty.
675 { return this->reloc_stubs_
.empty(); }
677 // Whether this has been changed.
679 has_been_changed() const
680 { return this->has_been_changed_
; }
682 // Set the has-been-changed flag.
684 set_has_been_changed(bool value
)
685 { this->has_been_changed_
= value
; }
687 // Return the current data size.
689 current_data_size() const
690 { return this->current_data_size_for_child(); }
692 // Add a STUB with using KEY. Caller is reponsible for avoid adding
693 // if already a STUB with the same key has been added.
695 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
);
697 // Look up a relocation stub using KEY. Return NULL if there is none.
699 find_reloc_stub(const Reloc_stub::Key
& key
) const
701 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
702 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
705 // Relocate stubs in this stub table.
707 relocate_stubs(const Relocate_info
<32, big_endian
>*,
708 Target_arm
<big_endian
>*, Output_section
*,
709 unsigned char*, Arm_address
, section_size_type
);
712 // Write out section contents.
714 do_write(Output_file
*);
716 // Return the required alignment.
719 { return this->addralign_
; }
721 // Finalize data size.
723 set_final_data_size()
724 { this->set_data_size(this->current_data_size_for_child()); }
726 // Reset address and file offset.
728 do_reset_address_and_file_offset();
731 // Unordered map of stubs.
733 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
734 Reloc_stub::Key::equal_to
>
739 // Owner of this stub table.
740 Arm_input_section
<big_endian
>* owner_
;
741 // This is set to true during relaxiong if the size of the stub table
743 bool has_been_changed_
;
744 // The relocation stubs.
745 Reloc_stub_map reloc_stubs_
;
748 // A class to wrap an ordinary input section containing executable code.
750 template<bool big_endian
>
751 class Arm_input_section
: public Output_relaxed_input_section
754 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
755 : Output_relaxed_input_section(relobj
, shndx
, 1),
756 original_addralign_(1), original_size_(0), stub_table_(NULL
)
766 // Whether this is a stub table owner.
768 is_stub_table_owner() const
769 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
771 // Return the stub table.
772 Stub_table
<big_endian
>*
774 { return this->stub_table_
; }
776 // Set the stub_table.
778 set_stub_table(Stub_table
<big_endian
>* stub_table
)
779 { this->stub_table_
= stub_table
; }
782 // Write data to output file.
784 do_write(Output_file
*);
786 // Return required alignment of this.
790 if (this->is_stub_table_owner())
791 return std::max(this->stub_table_
->addralign(),
792 this->original_addralign_
);
794 return this->original_addralign_
;
797 // Finalize data size.
799 set_final_data_size();
801 // Reset address and file offset.
803 do_reset_address_and_file_offset();
807 do_output_offset(const Relobj
* object
, unsigned int shndx
,
808 section_offset_type offset
,
809 section_offset_type
* poutput
) const
811 if ((object
== this->relobj())
812 && (shndx
== this->shndx())
814 && (convert_types
<uint64_t, section_offset_type
>(offset
)
815 <= this->original_size_
))
825 // Copying is not allowed.
826 Arm_input_section(const Arm_input_section
&);
827 Arm_input_section
& operator=(const Arm_input_section
&);
829 // Address alignment of the original input section.
830 uint64_t original_addralign_
;
831 // Section size of the original input section.
832 uint64_t original_size_
;
834 Stub_table
<big_endian
>* stub_table_
;
837 // Utilities for manipulating integers of up to 32-bits
841 // Sign extend an n-bit unsigned integer stored in an uint32_t into
842 // an int32_t. NO_BITS must be between 1 to 32.
843 template<int no_bits
>
844 static inline int32_t
845 sign_extend(uint32_t bits
)
847 gold_assert(no_bits
>= 0 && no_bits
<= 32);
849 return static_cast<int32_t>(bits
);
850 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
852 uint32_t top_bit
= 1U << (no_bits
- 1);
853 int32_t as_signed
= static_cast<int32_t>(bits
);
854 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
857 // Detects overflow of an NO_BITS integer stored in a uint32_t.
858 template<int no_bits
>
860 has_overflow(uint32_t bits
)
862 gold_assert(no_bits
>= 0 && no_bits
<= 32);
865 int32_t max
= (1 << (no_bits
- 1)) - 1;
866 int32_t min
= -(1 << (no_bits
- 1));
867 int32_t as_signed
= static_cast<int32_t>(bits
);
868 return as_signed
> max
|| as_signed
< min
;
871 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
872 // fits in the given number of bits as either a signed or unsigned value.
873 // For example, has_signed_unsigned_overflow<8> would check
874 // -128 <= bits <= 255
875 template<int no_bits
>
877 has_signed_unsigned_overflow(uint32_t bits
)
879 gold_assert(no_bits
>= 2 && no_bits
<= 32);
882 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
883 int32_t min
= -(1 << (no_bits
- 1));
884 int32_t as_signed
= static_cast<int32_t>(bits
);
885 return as_signed
> max
|| as_signed
< min
;
888 // Select bits from A and B using bits in MASK. For each n in [0..31],
889 // the n-th bit in the result is chosen from the n-th bits of A and B.
890 // A zero selects A and a one selects B.
891 static inline uint32_t
892 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
893 { return (a
& ~mask
) | (b
& mask
); }
896 template<bool big_endian
>
897 class Target_arm
: public Sized_target
<32, big_endian
>
900 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
904 : Sized_target
<32, big_endian
>(&arm_info
),
905 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
906 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
),
907 may_use_blx_(true), should_force_pic_veneer_(false)
910 // Whether we can use BLX.
913 { return this->may_use_blx_
; }
917 set_may_use_blx(bool value
)
918 { this->may_use_blx_
= value
; }
920 // Whether we force PCI branch veneers.
922 should_force_pic_veneer() const
923 { return this->should_force_pic_veneer_
; }
925 // Set PIC veneer flag.
927 set_should_force_pic_veneer(bool value
)
928 { this->should_force_pic_veneer_
= value
; }
930 // Whether we use THUMB-2 instructions.
934 // FIXME: This should not hard-coded.
938 // Whether we use THUMB/THUMB-2 instructions only.
940 using_thumb_only() const
942 // FIXME: This should not hard-coded.
946 // Process the relocations to determine unreferenced sections for
947 // garbage collection.
949 gc_process_relocs(const General_options
& options
,
950 Symbol_table
* symtab
,
952 Sized_relobj
<32, big_endian
>* object
,
953 unsigned int data_shndx
,
954 unsigned int sh_type
,
955 const unsigned char* prelocs
,
957 Output_section
* output_section
,
958 bool needs_special_offset_handling
,
959 size_t local_symbol_count
,
960 const unsigned char* plocal_symbols
);
962 // Scan the relocations to look for symbol adjustments.
964 scan_relocs(const General_options
& options
,
965 Symbol_table
* symtab
,
967 Sized_relobj
<32, big_endian
>* object
,
968 unsigned int data_shndx
,
969 unsigned int sh_type
,
970 const unsigned char* prelocs
,
972 Output_section
* output_section
,
973 bool needs_special_offset_handling
,
974 size_t local_symbol_count
,
975 const unsigned char* plocal_symbols
);
977 // Finalize the sections.
979 do_finalize_sections(Layout
*);
981 // Return the value to use for a dynamic symbol which requires special
984 do_dynsym_value(const Symbol
*) const;
986 // Relocate a section.
988 relocate_section(const Relocate_info
<32, big_endian
>*,
989 unsigned int sh_type
,
990 const unsigned char* prelocs
,
992 Output_section
* output_section
,
993 bool needs_special_offset_handling
,
995 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
996 section_size_type view_size
,
997 const Reloc_symbol_changes
*);
999 // Scan the relocs during a relocatable link.
1001 scan_relocatable_relocs(const General_options
& options
,
1002 Symbol_table
* symtab
,
1004 Sized_relobj
<32, big_endian
>* object
,
1005 unsigned int data_shndx
,
1006 unsigned int sh_type
,
1007 const unsigned char* prelocs
,
1009 Output_section
* output_section
,
1010 bool needs_special_offset_handling
,
1011 size_t local_symbol_count
,
1012 const unsigned char* plocal_symbols
,
1013 Relocatable_relocs
*);
1015 // Relocate a section during a relocatable link.
1017 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1018 unsigned int sh_type
,
1019 const unsigned char* prelocs
,
1021 Output_section
* output_section
,
1022 off_t offset_in_output_section
,
1023 const Relocatable_relocs
*,
1024 unsigned char* view
,
1025 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1026 section_size_type view_size
,
1027 unsigned char* reloc_view
,
1028 section_size_type reloc_view_size
);
1030 // Return whether SYM is defined by the ABI.
1032 do_is_defined_by_abi(Symbol
* sym
) const
1033 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1035 // Return the size of the GOT section.
1039 gold_assert(this->got_
!= NULL
);
1040 return this->got_
->data_size();
1043 // Map platform-specific reloc types
1045 get_real_reloc_type (unsigned int r_type
);
1047 // Get the default ARM target.
1048 static const Target_arm
<big_endian
>&
1051 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1052 && parameters
->target().is_big_endian() == big_endian
);
1053 return static_cast<const Target_arm
<big_endian
>&>(parameters
->target());
1057 // The class which scans relocations.
1062 : issued_non_pic_error_(false)
1066 local(const General_options
& options
, Symbol_table
* symtab
,
1067 Layout
* layout
, Target_arm
* target
,
1068 Sized_relobj
<32, big_endian
>* object
,
1069 unsigned int data_shndx
,
1070 Output_section
* output_section
,
1071 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1072 const elfcpp::Sym
<32, big_endian
>& lsym
);
1075 global(const General_options
& options
, Symbol_table
* symtab
,
1076 Layout
* layout
, Target_arm
* target
,
1077 Sized_relobj
<32, big_endian
>* object
,
1078 unsigned int data_shndx
,
1079 Output_section
* output_section
,
1080 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1085 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1086 unsigned int r_type
);
1089 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1090 unsigned int r_type
, Symbol
*);
1093 check_non_pic(Relobj
*, unsigned int r_type
);
1095 // Almost identical to Symbol::needs_plt_entry except that it also
1096 // handles STT_ARM_TFUNC.
1098 symbol_needs_plt_entry(const Symbol
* sym
)
1100 // An undefined symbol from an executable does not need a PLT entry.
1101 if (sym
->is_undefined() && !parameters
->options().shared())
1104 return (!parameters
->doing_static_link()
1105 && (sym
->type() == elfcpp::STT_FUNC
1106 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1107 && (sym
->is_from_dynobj()
1108 || sym
->is_undefined()
1109 || sym
->is_preemptible()));
1112 // Whether we have issued an error about a non-PIC compilation.
1113 bool issued_non_pic_error_
;
1116 // The class which implements relocation.
1126 // Return whether the static relocation needs to be applied.
1128 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1131 Output_section
* output_section
);
1133 // Do a relocation. Return false if the caller should not issue
1134 // any warnings about this relocation.
1136 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1137 Output_section
*, size_t relnum
,
1138 const elfcpp::Rel
<32, big_endian
>&,
1139 unsigned int r_type
, const Sized_symbol
<32>*,
1140 const Symbol_value
<32>*,
1141 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
1144 // Return whether we want to pass flag NON_PIC_REF for this
1147 reloc_is_non_pic (unsigned int r_type
)
1151 case elfcpp::R_ARM_REL32
:
1152 case elfcpp::R_ARM_THM_CALL
:
1153 case elfcpp::R_ARM_CALL
:
1154 case elfcpp::R_ARM_JUMP24
:
1155 case elfcpp::R_ARM_PREL31
:
1156 case elfcpp::R_ARM_THM_ABS5
:
1157 case elfcpp::R_ARM_ABS8
:
1158 case elfcpp::R_ARM_ABS12
:
1159 case elfcpp::R_ARM_ABS16
:
1160 case elfcpp::R_ARM_BASE_ABS
:
1168 // A class which returns the size required for a relocation type,
1169 // used while scanning relocs during a relocatable link.
1170 class Relocatable_size_for_reloc
1174 get_size_for_reloc(unsigned int, Relobj
*);
1177 // Get the GOT section, creating it if necessary.
1178 Output_data_got
<32, big_endian
>*
1179 got_section(Symbol_table
*, Layout
*);
1181 // Get the GOT PLT section.
1183 got_plt_section() const
1185 gold_assert(this->got_plt_
!= NULL
);
1186 return this->got_plt_
;
1189 // Create a PLT entry for a global symbol.
1191 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1193 // Get the PLT section.
1194 const Output_data_plt_arm
<big_endian
>*
1197 gold_assert(this->plt_
!= NULL
);
1201 // Get the dynamic reloc section, creating it if necessary.
1203 rel_dyn_section(Layout
*);
1205 // Return true if the symbol may need a COPY relocation.
1206 // References from an executable object to non-function symbols
1207 // defined in a dynamic object may need a COPY relocation.
1209 may_need_copy_reloc(Symbol
* gsym
)
1211 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1212 && gsym
->may_need_copy_reloc());
1215 // Add a potential copy relocation.
1217 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1218 Sized_relobj
<32, big_endian
>* object
,
1219 unsigned int shndx
, Output_section
* output_section
,
1220 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1222 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1223 symtab
->get_sized_symbol
<32>(sym
),
1224 object
, shndx
, output_section
, reloc
,
1225 this->rel_dyn_section(layout
));
1228 // Information about this specific target which we pass to the
1229 // general Target structure.
1230 static const Target::Target_info arm_info
;
1232 // The types of GOT entries needed for this platform.
1235 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1239 Output_data_got
<32, big_endian
>* got_
;
1241 Output_data_plt_arm
<big_endian
>* plt_
;
1242 // The GOT PLT section.
1243 Output_data_space
* got_plt_
;
1244 // The dynamic reloc section.
1245 Reloc_section
* rel_dyn_
;
1246 // Relocs saved to avoid a COPY reloc.
1247 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1248 // Space for variables copied with a COPY reloc.
1249 Output_data_space
* dynbss_
;
1250 // Whether we can use BLX.
1252 // Whether we force PIC branch veneers.
1253 bool should_force_pic_veneer_
;
1256 template<bool big_endian
>
1257 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1260 big_endian
, // is_big_endian
1261 elfcpp::EM_ARM
, // machine_code
1262 false, // has_make_symbol
1263 false, // has_resolve
1264 false, // has_code_fill
1265 true, // is_default_stack_executable
1267 "/usr/lib/libc.so.1", // dynamic_linker
1268 0x8000, // default_text_segment_address
1269 0x1000, // abi_pagesize (overridable by -z max-page-size)
1270 0x1000, // common_pagesize (overridable by -z common-page-size)
1271 elfcpp::SHN_UNDEF
, // small_common_shndx
1272 elfcpp::SHN_UNDEF
, // large_common_shndx
1273 0, // small_common_section_flags
1274 0 // large_common_section_flags
1277 // Arm relocate functions class
1280 template<bool big_endian
>
1281 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1286 STATUS_OKAY
, // No error during relocation.
1287 STATUS_OVERFLOW
, // Relocation oveflow.
1288 STATUS_BAD_RELOC
// Relocation cannot be applied.
1292 typedef Relocate_functions
<32, big_endian
> Base
;
1293 typedef Arm_relocate_functions
<big_endian
> This
;
1295 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1296 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1297 // to distinguish ARM and THUMB functions and it is treated specially.
1298 static inline Symbol_value
<32>::Value
1299 arm_symbol_value (const Sized_relobj
<32, big_endian
> *object
,
1300 const Symbol_value
<32>* psymval
,
1301 Symbol_value
<32>::Value addend
,
1304 typedef Symbol_value
<32>::Value Valtype
;
1308 Valtype raw
= psymval
->value(object
, 0);
1309 Valtype thumb_bit
= raw
& 1;
1310 return ((raw
& ~((Valtype
) 1)) + addend
) | thumb_bit
;
1313 return psymval
->value(object
, addend
);
1316 // Encoding of imm16 argument for movt and movw ARM instructions
1319 // imm16 := imm4 | imm12
1321 // 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
1322 // +-------+---------------+-------+-------+-----------------------+
1323 // | | |imm4 | |imm12 |
1324 // +-------+---------------+-------+-------+-----------------------+
1326 // Extract the relocation addend from VAL based on the ARM
1327 // instruction encoding described above.
1328 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1329 extract_arm_movw_movt_addend(
1330 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1332 // According to the Elf ABI for ARM Architecture the immediate
1333 // field is sign-extended to form the addend.
1334 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1337 // Insert X into VAL based on the ARM instruction encoding described
1339 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1340 insert_val_arm_movw_movt(
1341 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1342 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1346 val
|= (x
& 0xf000) << 4;
1350 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1353 // imm16 := imm4 | i | imm3 | imm8
1355 // 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
1356 // +---------+-+-----------+-------++-+-----+-------+---------------+
1357 // | |i| |imm4 || |imm3 | |imm8 |
1358 // +---------+-+-----------+-------++-+-----+-------+---------------+
1360 // Extract the relocation addend from VAL based on the Thumb2
1361 // instruction encoding described above.
1362 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1363 extract_thumb_movw_movt_addend(
1364 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1366 // According to the Elf ABI for ARM Architecture the immediate
1367 // field is sign-extended to form the addend.
1368 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1369 | ((val
>> 15) & 0x0800)
1370 | ((val
>> 4) & 0x0700)
1374 // Insert X into VAL based on the Thumb2 instruction encoding
1376 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1377 insert_val_thumb_movw_movt(
1378 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1379 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1382 val
|= (x
& 0xf000) << 4;
1383 val
|= (x
& 0x0800) << 15;
1384 val
|= (x
& 0x0700) << 4;
1385 val
|= (x
& 0x00ff);
1389 // FIXME: This probably only works for Android on ARM v5te. We should
1390 // following GNU ld for the general case.
1391 template<unsigned r_type
>
1392 static inline typename
This::Status
1393 arm_branch_common(unsigned char *view
,
1394 const Sized_relobj
<32, big_endian
>* object
,
1395 const Symbol_value
<32>* psymval
,
1396 elfcpp::Elf_types
<32>::Elf_Addr address
,
1399 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1400 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1401 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1403 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
1404 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
1405 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
1406 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
1407 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
1408 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
1409 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
1411 if (r_type
== elfcpp::R_ARM_CALL
)
1413 if (!insn_is_uncond_bl
&& !insn_is_blx
)
1414 return This::STATUS_BAD_RELOC
;
1416 else if (r_type
== elfcpp::R_ARM_JUMP24
)
1418 if (!insn_is_b
&& !insn_is_cond_bl
)
1419 return This::STATUS_BAD_RELOC
;
1421 else if (r_type
== elfcpp::R_ARM_PLT32
)
1423 if (!insn_is_any_branch
)
1424 return This::STATUS_BAD_RELOC
;
1429 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
1430 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1433 // If target has thumb bit set, we need to either turn the BL
1434 // into a BLX (for ARMv5 or above) or generate a stub.
1438 if (insn_is_uncond_bl
)
1439 val
= (val
& 0xffffff) | 0xfa000000 | ((x
& 2) << 23);
1441 return This::STATUS_BAD_RELOC
;
1444 gold_assert(!insn_is_blx
);
1446 val
= utils::bit_select(val
, (x
>> 2), 0xffffffUL
);
1447 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1448 return (utils::has_overflow
<26>(x
)
1449 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
1454 // R_ARM_ABS8: S + A
1455 static inline typename
This::Status
1456 abs8(unsigned char *view
,
1457 const Sized_relobj
<32, big_endian
>* object
,
1458 const Symbol_value
<32>* psymval
)
1460 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1461 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1462 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1463 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1464 Reltype addend
= utils::sign_extend
<8>(val
);
1465 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1466 val
= utils::bit_select(val
, x
, 0xffU
);
1467 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1468 return (utils::has_signed_unsigned_overflow
<8>(x
)
1469 ? This::STATUS_OVERFLOW
1470 : This::STATUS_OKAY
);
1473 // R_ARM_THM_ABS5: S + A
1474 static inline typename
This::Status
1475 thm_abs5(unsigned char *view
,
1476 const Sized_relobj
<32, big_endian
>* object
,
1477 const Symbol_value
<32>* psymval
)
1479 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1480 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1481 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1482 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1483 Reltype addend
= (val
& 0x7e0U
) >> 6;
1484 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1485 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1486 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1487 return (utils::has_overflow
<5>(x
)
1488 ? This::STATUS_OVERFLOW
1489 : This::STATUS_OKAY
);
1492 // R_ARM_ABS12: S + A
1493 static inline typename
This::Status
1494 abs12(unsigned char *view
,
1495 const Sized_relobj
<32, big_endian
>* object
,
1496 const Symbol_value
<32>* psymval
)
1498 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1499 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1500 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1501 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1502 Reltype addend
= val
& 0x0fffU
;
1503 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1504 val
= utils::bit_select(val
, x
, 0x0fffU
);
1505 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1506 return (utils::has_overflow
<12>(x
)
1507 ? This::STATUS_OVERFLOW
1508 : This::STATUS_OKAY
);
1511 // R_ARM_ABS16: S + A
1512 static inline typename
This::Status
1513 abs16(unsigned char *view
,
1514 const Sized_relobj
<32, big_endian
>* object
,
1515 const Symbol_value
<32>* psymval
)
1517 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1518 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1519 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1520 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1521 Reltype addend
= utils::sign_extend
<16>(val
);
1522 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1523 val
= utils::bit_select(val
, x
, 0xffffU
);
1524 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1525 return (utils::has_signed_unsigned_overflow
<16>(x
)
1526 ? This::STATUS_OVERFLOW
1527 : This::STATUS_OKAY
);
1530 // R_ARM_ABS32: (S + A) | T
1531 static inline typename
This::Status
1532 abs32(unsigned char *view
,
1533 const Sized_relobj
<32, big_endian
>* object
,
1534 const Symbol_value
<32>* psymval
,
1537 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1538 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1539 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1540 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
1541 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1542 return This::STATUS_OKAY
;
1545 // R_ARM_REL32: (S + A) | T - P
1546 static inline typename
This::Status
1547 rel32(unsigned char *view
,
1548 const Sized_relobj
<32, big_endian
>* object
,
1549 const Symbol_value
<32>* psymval
,
1550 elfcpp::Elf_types
<32>::Elf_Addr address
,
1553 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1554 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1555 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1556 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1558 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1559 return This::STATUS_OKAY
;
1562 // R_ARM_THM_CALL: (S + A) | T - P
1563 static inline typename
This::Status
1564 thm_call(unsigned char *view
,
1565 const Sized_relobj
<32, big_endian
>* object
,
1566 const Symbol_value
<32>* psymval
,
1567 elfcpp::Elf_types
<32>::Elf_Addr address
,
1570 // A thumb call consists of two instructions.
1571 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1572 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1573 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1574 Valtype hi
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1575 Valtype lo
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
1576 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1577 gold_assert((lo
& 0xf800) == 0xf800);
1578 Reltype addend
= utils::sign_extend
<23>(((hi
& 0x7ff) << 12)
1579 | ((lo
& 0x7ff) << 1));
1580 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1583 // If target has no thumb bit set, we need to either turn the BL
1584 // into a BLX (for ARMv5 or above) or generate a stub.
1587 // This only works for ARMv5 and above with interworking enabled.
1590 hi
= utils::bit_select(hi
, (x
>> 12), 0x7ffU
);
1591 lo
= utils::bit_select(lo
, (x
>> 1), 0x7ffU
);
1592 elfcpp::Swap
<16, big_endian
>::writeval(wv
, hi
);
1593 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lo
);
1594 return (utils::has_overflow
<23>(x
)
1595 ? This::STATUS_OVERFLOW
1596 : This::STATUS_OKAY
);
1599 // R_ARM_BASE_PREL: B(S) + A - P
1600 static inline typename
This::Status
1601 base_prel(unsigned char* view
,
1602 elfcpp::Elf_types
<32>::Elf_Addr origin
,
1603 elfcpp::Elf_types
<32>::Elf_Addr address
)
1605 Base::rel32(view
, origin
- address
);
1609 // R_ARM_BASE_ABS: B(S) + A
1610 static inline typename
This::Status
1611 base_abs(unsigned char* view
,
1612 elfcpp::Elf_types
<32>::Elf_Addr origin
)
1614 Base::rel32(view
, origin
);
1618 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1619 static inline typename
This::Status
1620 got_brel(unsigned char* view
,
1621 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
1623 Base::rel32(view
, got_offset
);
1624 return This::STATUS_OKAY
;
1627 // R_ARM_GOT_PREL: GOT(S) + A – P
1628 static inline typename
This::Status
1629 got_prel(unsigned char* view
,
1630 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
,
1631 elfcpp::Elf_types
<32>::Elf_Addr address
)
1633 Base::rel32(view
, got_offset
- address
);
1634 return This::STATUS_OKAY
;
1637 // R_ARM_PLT32: (S + A) | T - P
1638 static inline typename
This::Status
1639 plt32(unsigned char *view
,
1640 const Sized_relobj
<32, big_endian
>* object
,
1641 const Symbol_value
<32>* psymval
,
1642 elfcpp::Elf_types
<32>::Elf_Addr address
,
1645 return arm_branch_common
<elfcpp::R_ARM_PLT32
>(view
, object
, psymval
,
1646 address
, has_thumb_bit
);
1649 // R_ARM_CALL: (S + A) | T - P
1650 static inline typename
This::Status
1651 call(unsigned char *view
,
1652 const Sized_relobj
<32, big_endian
>* object
,
1653 const Symbol_value
<32>* psymval
,
1654 elfcpp::Elf_types
<32>::Elf_Addr address
,
1657 return arm_branch_common
<elfcpp::R_ARM_CALL
>(view
, object
, psymval
,
1658 address
, has_thumb_bit
);
1661 // R_ARM_JUMP24: (S + A) | T - P
1662 static inline typename
This::Status
1663 jump24(unsigned char *view
,
1664 const Sized_relobj
<32, big_endian
>* object
,
1665 const Symbol_value
<32>* psymval
,
1666 elfcpp::Elf_types
<32>::Elf_Addr address
,
1669 return arm_branch_common
<elfcpp::R_ARM_JUMP24
>(view
, object
, psymval
,
1670 address
, has_thumb_bit
);
1673 // R_ARM_PREL: (S + A) | T - P
1674 static inline typename
This::Status
1675 prel31(unsigned char *view
,
1676 const Sized_relobj
<32, big_endian
>* object
,
1677 const Symbol_value
<32>* psymval
,
1678 elfcpp::Elf_types
<32>::Elf_Addr address
,
1681 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1682 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1683 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1684 Valtype addend
= utils::sign_extend
<31>(val
);
1685 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1687 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
1688 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1689 return (utils::has_overflow
<31>(x
) ?
1690 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
1693 // R_ARM_MOVW_ABS_NC: (S + A) | T
1694 static inline typename
This::Status
1695 movw_abs_nc(unsigned char *view
,
1696 const Sized_relobj
<32, big_endian
>* object
,
1697 const Symbol_value
<32>* psymval
,
1700 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1701 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1702 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1703 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1704 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
1705 val
= This::insert_val_arm_movw_movt(val
, x
);
1706 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1707 return This::STATUS_OKAY
;
1710 // R_ARM_MOVT_ABS: S + A
1711 static inline typename
This::Status
1712 movt_abs(unsigned char *view
,
1713 const Sized_relobj
<32, big_endian
>* object
,
1714 const Symbol_value
<32>* psymval
)
1716 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1717 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1718 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1719 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1720 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, 0) >> 16;
1721 val
= This::insert_val_arm_movw_movt(val
, x
);
1722 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1723 return This::STATUS_OKAY
;
1726 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1727 static inline typename
This::Status
1728 thm_movw_abs_nc(unsigned char *view
,
1729 const Sized_relobj
<32, big_endian
>* object
,
1730 const Symbol_value
<32>* psymval
,
1733 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1734 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1735 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1736 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
1737 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
1738 Reltype addend
= extract_thumb_movw_movt_addend(val
);
1739 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
1740 val
= This::insert_val_thumb_movw_movt(val
, x
);
1741 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
1742 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
1743 return This::STATUS_OKAY
;
1746 // R_ARM_THM_MOVT_ABS: S + A
1747 static inline typename
This::Status
1748 thm_movt_abs(unsigned char *view
,
1749 const Sized_relobj
<32, big_endian
>* object
,
1750 const Symbol_value
<32>* psymval
)
1752 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1753 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1754 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1755 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
1756 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
1757 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
1758 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, 0) >> 16;
1759 val
= This::insert_val_thumb_movw_movt(val
, x
);
1760 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
1761 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
1762 return This::STATUS_OKAY
;
1765 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1766 static inline typename
This::Status
1767 movw_prel_nc(unsigned char *view
,
1768 const Sized_relobj
<32, big_endian
>* object
,
1769 const Symbol_value
<32>* psymval
,
1770 elfcpp::Elf_types
<32>::Elf_Addr address
,
1773 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1774 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1775 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1776 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1777 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1779 val
= This::insert_val_arm_movw_movt(val
, x
);
1780 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1781 return This::STATUS_OKAY
;
1784 // R_ARM_MOVT_PREL: S + A - P
1785 static inline typename
This::Status
1786 movt_prel(unsigned char *view
,
1787 const Sized_relobj
<32, big_endian
>* object
,
1788 const Symbol_value
<32>* psymval
,
1789 elfcpp::Elf_types
<32>::Elf_Addr address
)
1791 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1792 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1793 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1794 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1795 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, 0)
1797 val
= This::insert_val_arm_movw_movt(val
, x
);
1798 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1799 return This::STATUS_OKAY
;
1802 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1803 static inline typename
This::Status
1804 thm_movw_prel_nc(unsigned char *view
,
1805 const Sized_relobj
<32, big_endian
>* object
,
1806 const Symbol_value
<32>* psymval
,
1807 elfcpp::Elf_types
<32>::Elf_Addr address
,
1810 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1811 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1812 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1813 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
1814 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
1815 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
1816 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1818 val
= This::insert_val_thumb_movw_movt(val
, x
);
1819 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
1820 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
1821 return This::STATUS_OKAY
;
1824 // R_ARM_THM_MOVT_PREL: S + A - P
1825 static inline typename
This::Status
1826 thm_movt_prel(unsigned char *view
,
1827 const Sized_relobj
<32, big_endian
>* object
,
1828 const Symbol_value
<32>* psymval
,
1829 elfcpp::Elf_types
<32>::Elf_Addr address
)
1831 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1832 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1833 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1834 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
1835 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
1836 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
1837 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, 0)
1839 val
= This::insert_val_thumb_movw_movt(val
, x
);
1840 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
1841 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
1842 return This::STATUS_OKAY
;
1846 // Get the GOT section, creating it if necessary.
1848 template<bool big_endian
>
1849 Output_data_got
<32, big_endian
>*
1850 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1852 if (this->got_
== NULL
)
1854 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1856 this->got_
= new Output_data_got
<32, big_endian
>();
1859 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1861 | elfcpp::SHF_WRITE
),
1865 // The old GNU linker creates a .got.plt section. We just
1866 // create another set of data in the .got section. Note that we
1867 // always create a PLT if we create a GOT, although the PLT
1869 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
1870 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1872 | elfcpp::SHF_WRITE
),
1876 // The first three entries are reserved.
1877 this->got_plt_
->set_current_data_size(3 * 4);
1879 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1880 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1882 0, 0, elfcpp::STT_OBJECT
,
1884 elfcpp::STV_HIDDEN
, 0,
1890 // Get the dynamic reloc section, creating it if necessary.
1892 template<bool big_endian
>
1893 typename Target_arm
<big_endian
>::Reloc_section
*
1894 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
1896 if (this->rel_dyn_
== NULL
)
1898 gold_assert(layout
!= NULL
);
1899 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
1900 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
1901 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
1903 return this->rel_dyn_
;
1906 // Insn_template methods.
1908 // Return byte size of an instruction template.
1911 Insn_template::size() const
1913 switch (this->type())
1926 // Return alignment of an instruction template.
1929 Insn_template::alignment() const
1931 switch (this->type())
1944 // Stub_template methods.
1946 Stub_template::Stub_template(
1947 Stub_type type
, const Insn_template
* insns
,
1949 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
1950 entry_in_thumb_mode_(false), relocs_()
1954 // Compute byte size and alignment of stub template.
1955 for (size_t i
= 0; i
< insn_count
; i
++)
1957 unsigned insn_alignment
= insns
[i
].alignment();
1958 size_t insn_size
= insns
[i
].size();
1959 gold_assert((offset
& (insn_alignment
- 1)) == 0);
1960 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
1961 switch (insns
[i
].type())
1963 case Insn_template::THUMB16_TYPE
:
1965 this->entry_in_thumb_mode_
= true;
1968 case Insn_template::THUMB32_TYPE
:
1969 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
1970 this->relocs_
.push_back(Reloc(i
, offset
));
1972 this->entry_in_thumb_mode_
= true;
1975 case Insn_template::ARM_TYPE
:
1976 // Handle cases where the target is encoded within the
1978 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
1979 this->relocs_
.push_back(Reloc(i
, offset
));
1982 case Insn_template::DATA_TYPE
:
1983 // Entry point cannot be data.
1984 gold_assert(i
!= 0);
1985 this->relocs_
.push_back(Reloc(i
, offset
));
1991 offset
+= insn_size
;
1993 this->size_
= offset
;
1996 // Reloc_stub::Key methods.
1998 // Dump a Key as a string for debugging.
2001 Reloc_stub::Key::name() const
2003 if (this->r_sym_
== invalid_index
)
2005 // Global symbol key name
2006 // <stub-type>:<symbol name>:<addend>.
2007 const std::string sym_name
= this->u_
.symbol
->name();
2008 // We need to print two hex number and two colons. So just add 100 bytes
2009 // to the symbol name size.
2010 size_t len
= sym_name
.size() + 100;
2011 char* buffer
= new char[len
];
2012 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2013 sym_name
.c_str(), this->addend_
);
2014 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2016 return std::string(buffer
);
2020 // local symbol key name
2021 // <stub-type>:<object>:<r_sym>:<addend>.
2022 const size_t len
= 200;
2024 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2025 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2026 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2027 return std::string(buffer
);
2031 // Reloc_stub methods.
2033 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2034 // LOCATION to DESTINATION.
2035 // This code is based on the arm_type_of_stub function in
2036 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2040 Reloc_stub::stub_type_for_reloc(
2041 unsigned int r_type
,
2042 Arm_address location
,
2043 Arm_address destination
,
2044 bool target_is_thumb
)
2046 Stub_type stub_type
= arm_stub_none
;
2048 // This is a bit ugly but we want to avoid using a templated class for
2049 // big and little endianities.
2051 bool should_force_pic_veneer
;
2054 if (parameters
->target().is_big_endian())
2056 const Target_arm
<true>& big_endian_target
=
2057 Target_arm
<true>::default_target();
2058 may_use_blx
= big_endian_target
.may_use_blx();
2059 should_force_pic_veneer
= big_endian_target
.should_force_pic_veneer();
2060 thumb2
= big_endian_target
.using_thumb2();
2061 thumb_only
= big_endian_target
.using_thumb_only();
2065 const Target_arm
<false>& little_endian_target
=
2066 Target_arm
<false>::default_target();
2067 may_use_blx
= little_endian_target
.may_use_blx();
2068 should_force_pic_veneer
= little_endian_target
.should_force_pic_veneer();
2069 thumb2
= little_endian_target
.using_thumb2();
2070 thumb_only
= little_endian_target
.using_thumb_only();
2073 int64_t branch_offset
= (int64_t)destination
- location
;
2075 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2077 // Handle cases where:
2078 // - this call goes too far (different Thumb/Thumb2 max
2080 // - it's a Thumb->Arm call and blx is not available, or it's a
2081 // Thumb->Arm branch (not bl). A stub is needed in this case.
2083 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2084 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2086 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2087 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2088 || ((!target_is_thumb
)
2089 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2090 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2092 if (target_is_thumb
)
2097 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2100 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2101 // V5T and above. Stub starts with ARM code, so
2102 // we must be able to switch mode before
2103 // reaching it, which is only possible for 'bl'
2104 // (ie R_ARM_THM_CALL relocation).
2105 ? arm_stub_long_branch_any_thumb_pic
2106 // On V4T, use Thumb code only.
2107 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2111 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2112 ? arm_stub_long_branch_any_any
// V5T and above.
2113 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2117 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2118 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2119 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2126 // FIXME: We should check that the input section is from an
2127 // object that has interwork enabled.
2129 stub_type
= (parameters
->options().shared()
2130 || should_force_pic_veneer
)
2133 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2134 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2135 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2139 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2140 ? arm_stub_long_branch_any_any
// V5T and above.
2141 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2143 // Handle v4t short branches.
2144 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2145 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2146 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2147 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2151 else if (r_type
== elfcpp::R_ARM_CALL
2152 || r_type
== elfcpp::R_ARM_JUMP24
2153 || r_type
== elfcpp::R_ARM_PLT32
)
2155 if (target_is_thumb
)
2159 // FIXME: We should check that the input section is from an
2160 // object that has interwork enabled.
2162 // We have an extra 2-bytes reach because of
2163 // the mode change (bit 24 (H) of BLX encoding).
2164 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2165 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2166 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2167 || (r_type
== elfcpp::R_ARM_JUMP24
)
2168 || (r_type
== elfcpp::R_ARM_PLT32
))
2170 stub_type
= (parameters
->options().shared()
2171 || should_force_pic_veneer
)
2174 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2175 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2179 ? arm_stub_long_branch_any_any
// V5T and above.
2180 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2186 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2187 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2189 stub_type
= (parameters
->options().shared()
2190 || should_force_pic_veneer
)
2191 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2192 : arm_stub_long_branch_any_any
; /// non-PIC.
2200 // Template to implement do_write for a specific target endianity.
2202 template<bool big_endian
>
2204 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2205 section_size_type view_size
)
2207 const Stub_template
* stub_template
= this->stub_template();
2208 const Insn_template
* insns
= stub_template
->insns();
2210 // FIXME: We do not handle BE8 encoding yet.
2211 unsigned char* pov
= view
;
2212 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2214 switch (insns
[i
].type())
2216 case Insn_template::THUMB16_TYPE
:
2217 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2218 gold_assert(insns
[i
].reloc_addend() == 0);
2219 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2221 case Insn_template::THUMB32_TYPE
:
2223 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2224 uint32_t lo
= insns
[i
].data() & 0xffff;
2225 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2226 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2229 case Insn_template::ARM_TYPE
:
2230 case Insn_template::DATA_TYPE
:
2231 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2236 pov
+= insns
[i
].size();
2238 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2241 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2244 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2248 this->do_fixed_endian_write
<true>(view
, view_size
);
2250 this->do_fixed_endian_write
<false>(view
, view_size
);
2253 // Stub_factory methods.
2255 Stub_factory::Stub_factory()
2257 // The instruction template sequences are declared as static
2258 // objects and initialized first time the constructor runs.
2260 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2261 // to reach the stub if necessary.
2262 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2264 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2265 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2266 // dcd R_ARM_ABS32(X)
2269 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2271 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
2273 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2274 Insn_template::arm_insn(0xe12fff1c), // bx ip
2275 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2276 // dcd R_ARM_ABS32(X)
2279 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2280 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
2282 Insn_template::thumb16_insn(0xb401), // push {r0}
2283 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2284 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2285 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2286 Insn_template::thumb16_insn(0x4760), // bx ip
2287 Insn_template::thumb16_insn(0xbf00), // nop
2288 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2289 // dcd R_ARM_ABS32(X)
2292 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2294 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
2296 Insn_template::thumb16_insn(0x4778), // bx pc
2297 Insn_template::thumb16_insn(0x46c0), // nop
2298 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2299 Insn_template::arm_insn(0xe12fff1c), // bx ip
2300 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2301 // dcd R_ARM_ABS32(X)
2304 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2306 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
2308 Insn_template::thumb16_insn(0x4778), // bx pc
2309 Insn_template::thumb16_insn(0x46c0), // nop
2310 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2311 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2312 // dcd R_ARM_ABS32(X)
2315 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2316 // one, when the destination is close enough.
2317 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
2319 Insn_template::thumb16_insn(0x4778), // bx pc
2320 Insn_template::thumb16_insn(0x46c0), // nop
2321 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2324 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2325 // blx to reach the stub if necessary.
2326 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
2328 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2329 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2330 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2331 // dcd R_ARM_REL32(X-4)
2334 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2335 // blx to reach the stub if necessary. We can not add into pc;
2336 // it is not guaranteed to mode switch (different in ARMv6 and
2338 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
2340 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2341 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2342 Insn_template::arm_insn(0xe12fff1c), // bx ip
2343 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2344 // dcd R_ARM_REL32(X)
2347 // V4T ARM -> ARM long branch stub, PIC.
2348 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
2350 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2351 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2352 Insn_template::arm_insn(0xe12fff1c), // bx ip
2353 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2354 // dcd R_ARM_REL32(X)
2357 // V4T Thumb -> ARM long branch stub, PIC.
2358 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
2360 Insn_template::thumb16_insn(0x4778), // bx pc
2361 Insn_template::thumb16_insn(0x46c0), // nop
2362 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2363 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2364 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2365 // dcd R_ARM_REL32(X)
2368 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2370 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
2372 Insn_template::thumb16_insn(0xb401), // push {r0}
2373 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2374 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2375 Insn_template::thumb16_insn(0x4484), // add ip, r0
2376 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2377 Insn_template::thumb16_insn(0x4760), // bx ip
2378 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
2379 // dcd R_ARM_REL32(X)
2382 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2384 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
2386 Insn_template::thumb16_insn(0x4778), // bx pc
2387 Insn_template::thumb16_insn(0x46c0), // nop
2388 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2389 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2390 Insn_template::arm_insn(0xe12fff1c), // bx ip
2391 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2392 // dcd R_ARM_REL32(X)
2395 // Cortex-A8 erratum-workaround stubs.
2397 // Stub used for conditional branches (which may be beyond +/-1MB away,
2398 // so we can't use a conditional branch to reach this stub).
2405 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
2407 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2408 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2409 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2413 // Stub used for b.w and bl.w instructions.
2415 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
2417 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2420 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
2422 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2425 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2426 // instruction (which switches to ARM mode) to point to this stub. Jump to
2427 // the real destination using an ARM-mode branch.
2428 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
2430 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2433 // Fill in the stub template look-up table. Stub templates are constructed
2434 // per instance of Stub_factory for fast look-up without locking
2435 // in a thread-enabled environment.
2437 this->stub_templates_
[arm_stub_none
] =
2438 new Stub_template(arm_stub_none
, NULL
, 0);
2440 #define DEF_STUB(x) \
2444 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2445 Stub_type type = arm_stub_##x; \
2446 this->stub_templates_[type] = \
2447 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2455 // Stub_table methods.
2457 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2458 // if already a STUB with the same key has been added.
2460 template<bool big_endian
>
2462 Stub_table
<big_endian
>::add_reloc_stub(
2464 const Reloc_stub::Key
& key
)
2466 const Stub_template
* stub_template
= stub
->stub_template();
2467 gold_assert(stub_template
->type() == key
.stub_type());
2468 this->reloc_stubs_
[key
] = stub
;
2469 if (this->addralign_
< stub_template
->alignment())
2470 this->addralign_
= stub_template
->alignment();
2471 this->has_been_changed_
= true;
2474 template<bool big_endian
>
2476 Stub_table
<big_endian
>::relocate_stubs(
2477 const Relocate_info
<32, big_endian
>* relinfo
,
2478 Target_arm
<big_endian
>* arm_target
,
2479 Output_section
* output_section
,
2480 unsigned char* view
,
2481 Arm_address address
,
2482 section_size_type view_size
)
2484 // If we are passed a view bigger than the stub table's. we need to
2486 gold_assert(address
== this->address()
2488 == static_cast<section_size_type
>(this->data_size())));
2490 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2491 p
!= this->reloc_stubs_
.end();
2494 Reloc_stub
* stub
= p
->second
;
2495 const Stub_template
* stub_template
= stub
->stub_template();
2496 if (stub_template
->reloc_count() != 0)
2498 // Adjust view to cover the stub only.
2499 section_size_type offset
= stub
->offset();
2500 section_size_type stub_size
= stub_template
->size();
2501 gold_assert(offset
+ stub_size
<= view_size
);
2503 arm_target
->relocate_stub(stub
, relinfo
, output_section
,
2504 view
+ offset
, address
+ offset
,
2510 // Reset address and file offset.
2512 template<bool big_endian
>
2514 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
2517 uint64_t max_addralign
= 1;
2518 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2519 p
!= this->reloc_stubs_
.end();
2522 Reloc_stub
* stub
= p
->second
;
2523 const Stub_template
* stub_template
= stub
->stub_template();
2524 uint64_t stub_addralign
= stub_template
->alignment();
2525 max_addralign
= std::max(max_addralign
, stub_addralign
);
2526 off
= align_address(off
, stub_addralign
);
2527 stub
->set_offset(off
);
2528 stub
->reset_destination_address();
2529 off
+= stub_template
->size();
2532 this->addralign_
= max_addralign
;
2533 this->set_current_data_size_for_child(off
);
2536 // Write out the stubs to file.
2538 template<bool big_endian
>
2540 Stub_table
<big_endian
>::do_write(Output_file
* of
)
2542 off_t offset
= this->offset();
2543 const section_size_type oview_size
=
2544 convert_to_section_size_type(this->data_size());
2545 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2547 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2548 p
!= this->reloc_stubs_
.end();
2551 Reloc_stub
* stub
= p
->second
;
2552 Arm_address address
= this->address() + stub
->offset();
2554 == align_address(address
,
2555 stub
->stub_template()->alignment()));
2556 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
2559 of
->write_output_view(this->offset(), oview_size
, oview
);
2562 // Arm_input_section methods.
2564 // Initialize an Arm_input_section.
2566 template<bool big_endian
>
2568 Arm_input_section
<big_endian
>::init()
2570 Relobj
* relobj
= this->relobj();
2571 unsigned int shndx
= this->shndx();
2573 // Cache these to speed up size and alignment queries. It is too slow
2574 // to call section_addraglin and section_size every time.
2575 this->original_addralign_
= relobj
->section_addralign(shndx
);
2576 this->original_size_
= relobj
->section_size(shndx
);
2578 // We want to make this look like the original input section after
2579 // output sections are finalized.
2580 Output_section
* os
= relobj
->output_section(shndx
);
2581 off_t offset
= relobj
->output_section_offset(shndx
);
2582 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
2583 this->set_address(os
->address() + offset
);
2584 this->set_file_offset(os
->offset() + offset
);
2586 this->set_current_data_size(this->original_size_
);
2587 this->finalize_data_size();
2590 template<bool big_endian
>
2592 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
2594 // We have to write out the original section content.
2595 section_size_type section_size
;
2596 const unsigned char* section_contents
=
2597 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
2598 of
->write(this->offset(), section_contents
, section_size
);
2600 // If this owns a stub table and it is not empty, write it.
2601 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
2602 this->stub_table_
->write(of
);
2605 // Finalize data size.
2607 template<bool big_endian
>
2609 Arm_input_section
<big_endian
>::set_final_data_size()
2611 // If this owns a stub table, finalize its data size as well.
2612 if (this->is_stub_table_owner())
2614 uint64_t address
= this->address();
2616 // The stub table comes after the original section contents.
2617 address
+= this->original_size_
;
2618 address
= align_address(address
, this->stub_table_
->addralign());
2619 off_t offset
= this->offset() + (address
- this->address());
2620 this->stub_table_
->set_address_and_file_offset(address
, offset
);
2621 address
+= this->stub_table_
->data_size();
2622 gold_assert(address
== this->address() + this->current_data_size());
2625 this->set_data_size(this->current_data_size());
2628 // Reset address and file offset.
2630 template<bool big_endian
>
2632 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
2634 // Size of the original input section contents.
2635 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2637 // If this is a stub table owner, account for the stub table size.
2638 if (this->is_stub_table_owner())
2640 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
2642 // Reset the stub table's address and file offset. The
2643 // current data size for child will be updated after that.
2644 stub_table_
->reset_address_and_file_offset();
2645 off
= align_address(off
, stub_table_
->addralign());
2646 off
+= stub_table
->current_data_size();
2649 this->set_current_data_size(off
);
2652 // A class to handle the PLT data.
2654 template<bool big_endian
>
2655 class Output_data_plt_arm
: public Output_section_data
2658 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
2661 Output_data_plt_arm(Layout
*, Output_data_space
*);
2663 // Add an entry to the PLT.
2665 add_entry(Symbol
* gsym
);
2667 // Return the .rel.plt section data.
2668 const Reloc_section
*
2670 { return this->rel_
; }
2674 do_adjust_output_section(Output_section
* os
);
2676 // Write to a map file.
2678 do_print_to_mapfile(Mapfile
* mapfile
) const
2679 { mapfile
->print_output_data(this, _("** PLT")); }
2682 // Template for the first PLT entry.
2683 static const uint32_t first_plt_entry
[5];
2685 // Template for subsequent PLT entries.
2686 static const uint32_t plt_entry
[3];
2688 // Set the final size.
2690 set_final_data_size()
2692 this->set_data_size(sizeof(first_plt_entry
)
2693 + this->count_
* sizeof(plt_entry
));
2696 // Write out the PLT data.
2698 do_write(Output_file
*);
2700 // The reloc section.
2701 Reloc_section
* rel_
;
2702 // The .got.plt section.
2703 Output_data_space
* got_plt_
;
2704 // The number of PLT entries.
2705 unsigned int count_
;
2708 // Create the PLT section. The ordinary .got section is an argument,
2709 // since we need to refer to the start. We also create our own .got
2710 // section just for PLT entries.
2712 template<bool big_endian
>
2713 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
2714 Output_data_space
* got_plt
)
2715 : Output_section_data(4), got_plt_(got_plt
), count_(0)
2717 this->rel_
= new Reloc_section(false);
2718 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
2719 elfcpp::SHF_ALLOC
, this->rel_
);
2722 template<bool big_endian
>
2724 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
2729 // Add an entry to the PLT.
2731 template<bool big_endian
>
2733 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
2735 gold_assert(!gsym
->has_plt_offset());
2737 // Note that when setting the PLT offset we skip the initial
2738 // reserved PLT entry.
2739 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
2740 + sizeof(first_plt_entry
));
2744 section_offset_type got_offset
= this->got_plt_
->current_data_size();
2746 // Every PLT entry needs a GOT entry which points back to the PLT
2747 // entry (this will be changed by the dynamic linker, normally
2748 // lazily when the function is called).
2749 this->got_plt_
->set_current_data_size(got_offset
+ 4);
2751 // Every PLT entry needs a reloc.
2752 gsym
->set_needs_dynsym_entry();
2753 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
2756 // Note that we don't need to save the symbol. The contents of the
2757 // PLT are independent of which symbols are used. The symbols only
2758 // appear in the relocations.
2762 // FIXME: This is not very flexible. Right now this has only been tested
2763 // on armv5te. If we are to support additional architecture features like
2764 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
2766 // The first entry in the PLT.
2767 template<bool big_endian
>
2768 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
2770 0xe52de004, // str lr, [sp, #-4]!
2771 0xe59fe004, // ldr lr, [pc, #4]
2772 0xe08fe00e, // add lr, pc, lr
2773 0xe5bef008, // ldr pc, [lr, #8]!
2774 0x00000000, // &GOT[0] - .
2777 // Subsequent entries in the PLT.
2779 template<bool big_endian
>
2780 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
2782 0xe28fc600, // add ip, pc, #0xNN00000
2783 0xe28cca00, // add ip, ip, #0xNN000
2784 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
2787 // Write out the PLT. This uses the hand-coded instructions above,
2788 // and adjusts them as needed. This is all specified by the arm ELF
2789 // Processor Supplement.
2791 template<bool big_endian
>
2793 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
2795 const off_t offset
= this->offset();
2796 const section_size_type oview_size
=
2797 convert_to_section_size_type(this->data_size());
2798 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2800 const off_t got_file_offset
= this->got_plt_
->offset();
2801 const section_size_type got_size
=
2802 convert_to_section_size_type(this->got_plt_
->data_size());
2803 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2805 unsigned char* pov
= oview
;
2807 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
2808 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
2810 // Write first PLT entry. All but the last word are constants.
2811 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
2812 / sizeof(plt_entry
[0]));
2813 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
2814 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
2815 // Last word in first PLT entry is &GOT[0] - .
2816 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
2817 got_address
- (plt_address
+ 16));
2818 pov
+= sizeof(first_plt_entry
);
2820 unsigned char* got_pov
= got_view
;
2822 memset(got_pov
, 0, 12);
2825 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
2826 unsigned int plt_offset
= sizeof(first_plt_entry
);
2827 unsigned int plt_rel_offset
= 0;
2828 unsigned int got_offset
= 12;
2829 const unsigned int count
= this->count_
;
2830 for (unsigned int i
= 0;
2833 pov
+= sizeof(plt_entry
),
2835 plt_offset
+= sizeof(plt_entry
),
2836 plt_rel_offset
+= rel_size
,
2839 // Set and adjust the PLT entry itself.
2840 int32_t offset
= ((got_address
+ got_offset
)
2841 - (plt_address
+ plt_offset
+ 8));
2843 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
2844 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
2845 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
2846 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
2847 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
2848 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
2849 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
2851 // Set the entry in the GOT.
2852 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
2855 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2856 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2858 of
->write_output_view(offset
, oview_size
, oview
);
2859 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2862 // Create a PLT entry for a global symbol.
2864 template<bool big_endian
>
2866 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
2869 if (gsym
->has_plt_offset())
2872 if (this->plt_
== NULL
)
2874 // Create the GOT sections first.
2875 this->got_section(symtab
, layout
);
2877 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
2878 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2880 | elfcpp::SHF_EXECINSTR
),
2883 this->plt_
->add_entry(gsym
);
2886 // Report an unsupported relocation against a local symbol.
2888 template<bool big_endian
>
2890 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
2891 Sized_relobj
<32, big_endian
>* object
,
2892 unsigned int r_type
)
2894 gold_error(_("%s: unsupported reloc %u against local symbol"),
2895 object
->name().c_str(), r_type
);
2898 // We are about to emit a dynamic relocation of type R_TYPE. If the
2899 // dynamic linker does not support it, issue an error. The GNU linker
2900 // only issues a non-PIC error for an allocated read-only section.
2901 // Here we know the section is allocated, but we don't know that it is
2902 // read-only. But we check for all the relocation types which the
2903 // glibc dynamic linker supports, so it seems appropriate to issue an
2904 // error even if the section is not read-only.
2906 template<bool big_endian
>
2908 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
2909 unsigned int r_type
)
2913 // These are the relocation types supported by glibc for ARM.
2914 case elfcpp::R_ARM_RELATIVE
:
2915 case elfcpp::R_ARM_COPY
:
2916 case elfcpp::R_ARM_GLOB_DAT
:
2917 case elfcpp::R_ARM_JUMP_SLOT
:
2918 case elfcpp::R_ARM_ABS32
:
2919 case elfcpp::R_ARM_ABS32_NOI
:
2920 case elfcpp::R_ARM_PC24
:
2921 // FIXME: The following 3 types are not supported by Android's dynamic
2923 case elfcpp::R_ARM_TLS_DTPMOD32
:
2924 case elfcpp::R_ARM_TLS_DTPOFF32
:
2925 case elfcpp::R_ARM_TLS_TPOFF32
:
2929 // This prevents us from issuing more than one error per reloc
2930 // section. But we can still wind up issuing more than one
2931 // error per object file.
2932 if (this->issued_non_pic_error_
)
2934 object
->error(_("requires unsupported dynamic reloc; "
2935 "recompile with -fPIC"));
2936 this->issued_non_pic_error_
= true;
2939 case elfcpp::R_ARM_NONE
:
2944 // Scan a relocation for a local symbol.
2945 // FIXME: This only handles a subset of relocation types used by Android
2946 // on ARM v5te devices.
2948 template<bool big_endian
>
2950 Target_arm
<big_endian
>::Scan::local(const General_options
&,
2951 Symbol_table
* symtab
,
2954 Sized_relobj
<32, big_endian
>* object
,
2955 unsigned int data_shndx
,
2956 Output_section
* output_section
,
2957 const elfcpp::Rel
<32, big_endian
>& reloc
,
2958 unsigned int r_type
,
2959 const elfcpp::Sym
<32, big_endian
>&)
2961 r_type
= get_real_reloc_type(r_type
);
2964 case elfcpp::R_ARM_NONE
:
2967 case elfcpp::R_ARM_ABS32
:
2968 case elfcpp::R_ARM_ABS32_NOI
:
2969 // If building a shared library (or a position-independent
2970 // executable), we need to create a dynamic relocation for
2971 // this location. The relocation applied at link time will
2972 // apply the link-time value, so we flag the location with
2973 // an R_ARM_RELATIVE relocation so the dynamic loader can
2974 // relocate it easily.
2975 if (parameters
->options().output_is_position_independent())
2977 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2978 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
2979 // If we are to add more other reloc types than R_ARM_ABS32,
2980 // we need to add check_non_pic(object, r_type) here.
2981 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
2982 output_section
, data_shndx
,
2983 reloc
.get_r_offset());
2987 case elfcpp::R_ARM_REL32
:
2988 case elfcpp::R_ARM_THM_CALL
:
2989 case elfcpp::R_ARM_CALL
:
2990 case elfcpp::R_ARM_PREL31
:
2991 case elfcpp::R_ARM_JUMP24
:
2992 case elfcpp::R_ARM_PLT32
:
2993 case elfcpp::R_ARM_THM_ABS5
:
2994 case elfcpp::R_ARM_ABS8
:
2995 case elfcpp::R_ARM_ABS12
:
2996 case elfcpp::R_ARM_ABS16
:
2997 case elfcpp::R_ARM_BASE_ABS
:
2998 case elfcpp::R_ARM_MOVW_ABS_NC
:
2999 case elfcpp::R_ARM_MOVT_ABS
:
3000 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3001 case elfcpp::R_ARM_THM_MOVT_ABS
:
3002 case elfcpp::R_ARM_MOVW_PREL_NC
:
3003 case elfcpp::R_ARM_MOVT_PREL
:
3004 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3005 case elfcpp::R_ARM_THM_MOVT_PREL
:
3008 case elfcpp::R_ARM_GOTOFF32
:
3009 // We need a GOT section:
3010 target
->got_section(symtab
, layout
);
3013 case elfcpp::R_ARM_BASE_PREL
:
3014 // FIXME: What about this?
3017 case elfcpp::R_ARM_GOT_BREL
:
3018 case elfcpp::R_ARM_GOT_PREL
:
3020 // The symbol requires a GOT entry.
3021 Output_data_got
<32, big_endian
>* got
=
3022 target
->got_section(symtab
, layout
);
3023 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3024 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
3026 // If we are generating a shared object, we need to add a
3027 // dynamic RELATIVE relocation for this symbol's GOT entry.
3028 if (parameters
->options().output_is_position_independent())
3030 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3031 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3032 rel_dyn
->add_local_relative(
3033 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
3034 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3040 case elfcpp::R_ARM_TARGET1
:
3041 // This should have been mapped to another type already.
3043 case elfcpp::R_ARM_COPY
:
3044 case elfcpp::R_ARM_GLOB_DAT
:
3045 case elfcpp::R_ARM_JUMP_SLOT
:
3046 case elfcpp::R_ARM_RELATIVE
:
3047 // These are relocations which should only be seen by the
3048 // dynamic linker, and should never be seen here.
3049 gold_error(_("%s: unexpected reloc %u in object file"),
3050 object
->name().c_str(), r_type
);
3054 unsupported_reloc_local(object
, r_type
);
3059 // Report an unsupported relocation against a global symbol.
3061 template<bool big_endian
>
3063 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
3064 Sized_relobj
<32, big_endian
>* object
,
3065 unsigned int r_type
,
3068 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3069 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3072 // Scan a relocation for a global symbol.
3073 // FIXME: This only handles a subset of relocation types used by Android
3074 // on ARM v5te devices.
3076 template<bool big_endian
>
3078 Target_arm
<big_endian
>::Scan::global(const General_options
&,
3079 Symbol_table
* symtab
,
3082 Sized_relobj
<32, big_endian
>* object
,
3083 unsigned int data_shndx
,
3084 Output_section
* output_section
,
3085 const elfcpp::Rel
<32, big_endian
>& reloc
,
3086 unsigned int r_type
,
3089 r_type
= get_real_reloc_type(r_type
);
3092 case elfcpp::R_ARM_NONE
:
3095 case elfcpp::R_ARM_ABS32
:
3096 case elfcpp::R_ARM_ABS32_NOI
:
3098 // Make a dynamic relocation if necessary.
3099 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
3101 if (target
->may_need_copy_reloc(gsym
))
3103 target
->copy_reloc(symtab
, layout
, object
,
3104 data_shndx
, output_section
, gsym
, reloc
);
3106 else if (gsym
->can_use_relative_reloc(false))
3108 // If we are to add more other reloc types than R_ARM_ABS32,
3109 // we need to add check_non_pic(object, r_type) here.
3110 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3111 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
3112 output_section
, object
,
3113 data_shndx
, reloc
.get_r_offset());
3117 // If we are to add more other reloc types than R_ARM_ABS32,
3118 // we need to add check_non_pic(object, r_type) here.
3119 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3120 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3121 data_shndx
, reloc
.get_r_offset());
3127 case elfcpp::R_ARM_MOVW_ABS_NC
:
3128 case elfcpp::R_ARM_MOVT_ABS
:
3129 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3130 case elfcpp::R_ARM_THM_MOVT_ABS
:
3131 case elfcpp::R_ARM_MOVW_PREL_NC
:
3132 case elfcpp::R_ARM_MOVT_PREL
:
3133 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3134 case elfcpp::R_ARM_THM_MOVT_PREL
:
3137 case elfcpp::R_ARM_THM_ABS5
:
3138 case elfcpp::R_ARM_ABS8
:
3139 case elfcpp::R_ARM_ABS12
:
3140 case elfcpp::R_ARM_ABS16
:
3141 case elfcpp::R_ARM_BASE_ABS
:
3143 // No dynamic relocs of this kinds.
3144 // Report the error in case of PIC.
3145 int flags
= Symbol::NON_PIC_REF
;
3146 if (gsym
->type() == elfcpp::STT_FUNC
3147 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
3148 flags
|= Symbol::FUNCTION_CALL
;
3149 if (gsym
->needs_dynamic_reloc(flags
))
3150 check_non_pic(object
, r_type
);
3154 case elfcpp::R_ARM_REL32
:
3155 case elfcpp::R_ARM_PREL31
:
3157 // Make a dynamic relocation if necessary.
3158 int flags
= Symbol::NON_PIC_REF
;
3159 if (gsym
->needs_dynamic_reloc(flags
))
3161 if (target
->may_need_copy_reloc(gsym
))
3163 target
->copy_reloc(symtab
, layout
, object
,
3164 data_shndx
, output_section
, gsym
, reloc
);
3168 check_non_pic(object
, r_type
);
3169 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3170 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3171 data_shndx
, reloc
.get_r_offset());
3177 case elfcpp::R_ARM_JUMP24
:
3178 case elfcpp::R_ARM_THM_CALL
:
3179 case elfcpp::R_ARM_CALL
:
3181 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
3182 target
->make_plt_entry(symtab
, layout
, gsym
);
3183 // Make a dynamic relocation if necessary.
3184 int flags
= Symbol::NON_PIC_REF
;
3185 if (gsym
->type() == elfcpp::STT_FUNC
3186 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
3187 flags
|= Symbol::FUNCTION_CALL
;
3188 if (gsym
->needs_dynamic_reloc(flags
))
3190 if (target
->may_need_copy_reloc(gsym
))
3192 target
->copy_reloc(symtab
, layout
, object
,
3193 data_shndx
, output_section
, gsym
,
3198 check_non_pic(object
, r_type
);
3199 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3200 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3201 data_shndx
, reloc
.get_r_offset());
3207 case elfcpp::R_ARM_PLT32
:
3208 // If the symbol is fully resolved, this is just a relative
3209 // local reloc. Otherwise we need a PLT entry.
3210 if (gsym
->final_value_is_known())
3212 // If building a shared library, we can also skip the PLT entry
3213 // if the symbol is defined in the output file and is protected
3215 if (gsym
->is_defined()
3216 && !gsym
->is_from_dynobj()
3217 && !gsym
->is_preemptible())
3219 target
->make_plt_entry(symtab
, layout
, gsym
);
3222 case elfcpp::R_ARM_GOTOFF32
:
3223 // We need a GOT section.
3224 target
->got_section(symtab
, layout
);
3227 case elfcpp::R_ARM_BASE_PREL
:
3228 // FIXME: What about this?
3231 case elfcpp::R_ARM_GOT_BREL
:
3232 case elfcpp::R_ARM_GOT_PREL
:
3234 // The symbol requires a GOT entry.
3235 Output_data_got
<32, big_endian
>* got
=
3236 target
->got_section(symtab
, layout
);
3237 if (gsym
->final_value_is_known())
3238 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3241 // If this symbol is not fully resolved, we need to add a
3242 // GOT entry with a dynamic relocation.
3243 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3244 if (gsym
->is_from_dynobj()
3245 || gsym
->is_undefined()
3246 || gsym
->is_preemptible())
3247 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
3248 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
3251 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
3252 rel_dyn
->add_global_relative(
3253 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
3254 gsym
->got_offset(GOT_TYPE_STANDARD
));
3260 case elfcpp::R_ARM_TARGET1
:
3261 // This should have been mapped to another type already.
3263 case elfcpp::R_ARM_COPY
:
3264 case elfcpp::R_ARM_GLOB_DAT
:
3265 case elfcpp::R_ARM_JUMP_SLOT
:
3266 case elfcpp::R_ARM_RELATIVE
:
3267 // These are relocations which should only be seen by the
3268 // dynamic linker, and should never be seen here.
3269 gold_error(_("%s: unexpected reloc %u in object file"),
3270 object
->name().c_str(), r_type
);
3274 unsupported_reloc_global(object
, r_type
, gsym
);
3279 // Process relocations for gc.
3281 template<bool big_endian
>
3283 Target_arm
<big_endian
>::gc_process_relocs(const General_options
& options
,
3284 Symbol_table
* symtab
,
3286 Sized_relobj
<32, big_endian
>* object
,
3287 unsigned int data_shndx
,
3289 const unsigned char* prelocs
,
3291 Output_section
* output_section
,
3292 bool needs_special_offset_handling
,
3293 size_t local_symbol_count
,
3294 const unsigned char* plocal_symbols
)
3296 typedef Target_arm
<big_endian
> Arm
;
3297 typedef typename Target_arm
<big_endian
>::Scan Scan
;
3299 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
3309 needs_special_offset_handling
,
3314 // Scan relocations for a section.
3316 template<bool big_endian
>
3318 Target_arm
<big_endian
>::scan_relocs(const General_options
& options
,
3319 Symbol_table
* symtab
,
3321 Sized_relobj
<32, big_endian
>* object
,
3322 unsigned int data_shndx
,
3323 unsigned int sh_type
,
3324 const unsigned char* prelocs
,
3326 Output_section
* output_section
,
3327 bool needs_special_offset_handling
,
3328 size_t local_symbol_count
,
3329 const unsigned char* plocal_symbols
)
3331 typedef typename Target_arm
<big_endian
>::Scan Scan
;
3332 if (sh_type
== elfcpp::SHT_RELA
)
3334 gold_error(_("%s: unsupported RELA reloc section"),
3335 object
->name().c_str());
3339 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
3349 needs_special_offset_handling
,
3354 // Finalize the sections.
3356 template<bool big_endian
>
3358 Target_arm
<big_endian
>::do_finalize_sections(Layout
* layout
)
3360 // Fill in some more dynamic tags.
3361 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3364 if (this->got_plt_
!= NULL
)
3365 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
3367 if (this->plt_
!= NULL
)
3369 const Output_data
* od
= this->plt_
->rel_plt();
3370 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
3371 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
3372 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
3375 if (this->rel_dyn_
!= NULL
)
3377 const Output_data
* od
= this->rel_dyn_
;
3378 odyn
->add_section_address(elfcpp::DT_REL
, od
);
3379 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
3380 odyn
->add_constant(elfcpp::DT_RELENT
,
3381 elfcpp::Elf_sizes
<32>::rel_size
);
3384 if (!parameters
->options().shared())
3386 // The value of the DT_DEBUG tag is filled in by the dynamic
3387 // linker at run time, and used by the debugger.
3388 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
3392 // Emit any relocs we saved in an attempt to avoid generating COPY
3394 if (this->copy_relocs_
.any_saved_relocs())
3395 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
3397 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3398 // the .ARM.exidx section.
3399 if (!layout
->script_options()->saw_phdrs_clause()
3400 && !parameters
->options().relocatable())
3402 Output_section
* exidx_section
=
3403 layout
->find_output_section(".ARM.exidx");
3405 if (exidx_section
!= NULL
3406 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
)
3408 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
3410 Output_segment
* exidx_segment
=
3411 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
3412 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
);
3417 // Return whether a direct absolute static relocation needs to be applied.
3418 // In cases where Scan::local() or Scan::global() has created
3419 // a dynamic relocation other than R_ARM_RELATIVE, the addend
3420 // of the relocation is carried in the data, and we must not
3421 // apply the static relocation.
3423 template<bool big_endian
>
3425 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
3426 const Sized_symbol
<32>* gsym
,
3429 Output_section
* output_section
)
3431 // If the output section is not allocated, then we didn't call
3432 // scan_relocs, we didn't create a dynamic reloc, and we must apply
3434 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
3437 // For local symbols, we will have created a non-RELATIVE dynamic
3438 // relocation only if (a) the output is position independent,
3439 // (b) the relocation is absolute (not pc- or segment-relative), and
3440 // (c) the relocation is not 32 bits wide.
3442 return !(parameters
->options().output_is_position_independent()
3443 && (ref_flags
& Symbol::ABSOLUTE_REF
)
3446 // For global symbols, we use the same helper routines used in the
3447 // scan pass. If we did not create a dynamic relocation, or if we
3448 // created a RELATIVE dynamic relocation, we should apply the static
3450 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
3451 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
3452 && gsym
->can_use_relative_reloc(ref_flags
3453 & Symbol::FUNCTION_CALL
);
3454 return !has_dyn
|| is_rel
;
3457 // Perform a relocation.
3459 template<bool big_endian
>
3461 Target_arm
<big_endian
>::Relocate::relocate(
3462 const Relocate_info
<32, big_endian
>* relinfo
,
3464 Output_section
*output_section
,
3466 const elfcpp::Rel
<32, big_endian
>& rel
,
3467 unsigned int r_type
,
3468 const Sized_symbol
<32>* gsym
,
3469 const Symbol_value
<32>* psymval
,
3470 unsigned char* view
,
3471 elfcpp::Elf_types
<32>::Elf_Addr address
,
3472 section_size_type
/* view_size */ )
3474 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
3476 r_type
= get_real_reloc_type(r_type
);
3478 // If this the symbol may be a Thumb function, set thumb bit to 1.
3479 bool has_thumb_bit
= ((gsym
!= NULL
)
3480 && (gsym
->type() == elfcpp::STT_FUNC
3481 || gsym
->type() == elfcpp::STT_ARM_TFUNC
));
3483 // Pick the value to use for symbols defined in shared objects.
3484 Symbol_value
<32> symval
;
3486 && gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
3488 symval
.set_output_value(target
->plt_section()->address()
3489 + gsym
->plt_offset());
3494 const Sized_relobj
<32, big_endian
>* object
= relinfo
->object
;
3496 // Get the GOT offset if needed.
3497 // The GOT pointer points to the end of the GOT section.
3498 // We need to subtract the size of the GOT section to get
3499 // the actual offset to use in the relocation.
3500 bool have_got_offset
= false;
3501 unsigned int got_offset
= 0;
3504 case elfcpp::R_ARM_GOT_BREL
:
3505 case elfcpp::R_ARM_GOT_PREL
:
3508 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3509 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
3510 - target
->got_size());
3514 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
3515 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3516 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3517 - target
->got_size());
3519 have_got_offset
= true;
3526 typename
Arm_relocate_functions::Status reloc_status
=
3527 Arm_relocate_functions::STATUS_OKAY
;
3530 case elfcpp::R_ARM_NONE
:
3533 case elfcpp::R_ARM_ABS8
:
3534 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
3536 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
3539 case elfcpp::R_ARM_ABS12
:
3540 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
3542 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
3545 case elfcpp::R_ARM_ABS16
:
3546 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
3548 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
3551 case elfcpp::R_ARM_ABS32
:
3552 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3554 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
3558 case elfcpp::R_ARM_ABS32_NOI
:
3559 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3561 // No thumb bit for this relocation: (S + A)
3562 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
3566 case elfcpp::R_ARM_MOVW_ABS_NC
:
3567 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3569 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
3573 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
3574 "a shared object; recompile with -fPIC"));
3577 case elfcpp::R_ARM_MOVT_ABS
:
3578 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3580 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
3582 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
3583 "a shared object; recompile with -fPIC"));
3586 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3587 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3589 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
3593 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
3594 "making a shared object; recompile with -fPIC"));
3597 case elfcpp::R_ARM_THM_MOVT_ABS
:
3598 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3600 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
3603 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
3604 "making a shared object; recompile with -fPIC"));
3607 case elfcpp::R_ARM_MOVW_PREL_NC
:
3608 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
3613 case elfcpp::R_ARM_MOVT_PREL
:
3614 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
3618 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3619 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
3624 case elfcpp::R_ARM_THM_MOVT_PREL
:
3625 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
3629 case elfcpp::R_ARM_REL32
:
3630 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
3631 address
, has_thumb_bit
);
3634 case elfcpp::R_ARM_THM_ABS5
:
3635 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
3637 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
3640 case elfcpp::R_ARM_THM_CALL
:
3641 reloc_status
= Arm_relocate_functions::thm_call(view
, object
, psymval
,
3642 address
, has_thumb_bit
);
3645 case elfcpp::R_ARM_GOTOFF32
:
3647 elfcpp::Elf_types
<32>::Elf_Addr got_origin
;
3648 got_origin
= target
->got_plt_section()->address();
3649 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
3650 got_origin
, has_thumb_bit
);
3654 case elfcpp::R_ARM_BASE_PREL
:
3657 // Get the addressing origin of the output segment defining the
3658 // symbol gsym (AAELF 4.6.1.2 Relocation types)
3659 gold_assert(gsym
!= NULL
);
3660 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
3661 origin
= gsym
->output_segment()->vaddr();
3662 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
3663 origin
= gsym
->output_data()->address();
3666 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
3667 _("cannot find origin of R_ARM_BASE_PREL"));
3670 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
3674 case elfcpp::R_ARM_BASE_ABS
:
3676 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
3681 // Get the addressing origin of the output segment defining
3682 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
3684 // R_ARM_BASE_ABS with the NULL symbol will give the
3685 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
3686 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
3687 origin
= target
->got_plt_section()->address();
3688 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
3689 origin
= gsym
->output_segment()->vaddr();
3690 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
3691 origin
= gsym
->output_data()->address();
3694 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
3695 _("cannot find origin of R_ARM_BASE_ABS"));
3699 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
3703 case elfcpp::R_ARM_GOT_BREL
:
3704 gold_assert(have_got_offset
);
3705 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
3708 case elfcpp::R_ARM_GOT_PREL
:
3709 gold_assert(have_got_offset
);
3710 // Get the address origin for GOT PLT, which is allocated right
3711 // after the GOT section, to calculate an absolute address of
3712 // the symbol GOT entry (got_origin + got_offset).
3713 elfcpp::Elf_types
<32>::Elf_Addr got_origin
;
3714 got_origin
= target
->got_plt_section()->address();
3715 reloc_status
= Arm_relocate_functions::got_prel(view
,
3716 got_origin
+ got_offset
,
3720 case elfcpp::R_ARM_PLT32
:
3721 gold_assert(gsym
== NULL
3722 || gsym
->has_plt_offset()
3723 || gsym
->final_value_is_known()
3724 || (gsym
->is_defined()
3725 && !gsym
->is_from_dynobj()
3726 && !gsym
->is_preemptible()));
3727 reloc_status
= Arm_relocate_functions::plt32(view
, object
, psymval
,
3728 address
, has_thumb_bit
);
3731 case elfcpp::R_ARM_CALL
:
3732 reloc_status
= Arm_relocate_functions::call(view
, object
, psymval
,
3733 address
, has_thumb_bit
);
3736 case elfcpp::R_ARM_JUMP24
:
3737 reloc_status
= Arm_relocate_functions::jump24(view
, object
, psymval
,
3738 address
, has_thumb_bit
);
3741 case elfcpp::R_ARM_PREL31
:
3742 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
3743 address
, has_thumb_bit
);
3746 case elfcpp::R_ARM_TARGET1
:
3747 // This should have been mapped to another type already.
3749 case elfcpp::R_ARM_COPY
:
3750 case elfcpp::R_ARM_GLOB_DAT
:
3751 case elfcpp::R_ARM_JUMP_SLOT
:
3752 case elfcpp::R_ARM_RELATIVE
:
3753 // These are relocations which should only be seen by the
3754 // dynamic linker, and should never be seen here.
3755 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
3756 _("unexpected reloc %u in object file"),
3761 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
3762 _("unsupported reloc %u"),
3767 // Report any errors.
3768 switch (reloc_status
)
3770 case Arm_relocate_functions::STATUS_OKAY
:
3772 case Arm_relocate_functions::STATUS_OVERFLOW
:
3773 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
3774 _("relocation overflow in relocation %u"),
3777 case Arm_relocate_functions::STATUS_BAD_RELOC
:
3778 gold_error_at_location(
3782 _("unexpected opcode while processing relocation %u"),
3792 // Relocate section data.
3794 template<bool big_endian
>
3796 Target_arm
<big_endian
>::relocate_section(
3797 const Relocate_info
<32, big_endian
>* relinfo
,
3798 unsigned int sh_type
,
3799 const unsigned char* prelocs
,
3801 Output_section
* output_section
,
3802 bool needs_special_offset_handling
,
3803 unsigned char* view
,
3804 elfcpp::Elf_types
<32>::Elf_Addr address
,
3805 section_size_type view_size
,
3806 const Reloc_symbol_changes
* reloc_symbol_changes
)
3808 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
3809 gold_assert(sh_type
== elfcpp::SHT_REL
);
3811 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
3818 needs_special_offset_handling
,
3822 reloc_symbol_changes
);
3825 // Return the size of a relocation while scanning during a relocatable
3828 template<bool big_endian
>
3830 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
3831 unsigned int r_type
,
3834 r_type
= get_real_reloc_type(r_type
);
3837 case elfcpp::R_ARM_NONE
:
3840 case elfcpp::R_ARM_ABS8
:
3843 case elfcpp::R_ARM_ABS16
:
3844 case elfcpp::R_ARM_THM_ABS5
:
3847 case elfcpp::R_ARM_ABS32
:
3848 case elfcpp::R_ARM_ABS32_NOI
:
3849 case elfcpp::R_ARM_ABS12
:
3850 case elfcpp::R_ARM_BASE_ABS
:
3851 case elfcpp::R_ARM_REL32
:
3852 case elfcpp::R_ARM_THM_CALL
:
3853 case elfcpp::R_ARM_GOTOFF32
:
3854 case elfcpp::R_ARM_BASE_PREL
:
3855 case elfcpp::R_ARM_GOT_BREL
:
3856 case elfcpp::R_ARM_GOT_PREL
:
3857 case elfcpp::R_ARM_PLT32
:
3858 case elfcpp::R_ARM_CALL
:
3859 case elfcpp::R_ARM_JUMP24
:
3860 case elfcpp::R_ARM_PREL31
:
3861 case elfcpp::R_ARM_MOVW_ABS_NC
:
3862 case elfcpp::R_ARM_MOVT_ABS
:
3863 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3864 case elfcpp::R_ARM_THM_MOVT_ABS
:
3865 case elfcpp::R_ARM_MOVW_PREL_NC
:
3866 case elfcpp::R_ARM_MOVT_PREL
:
3867 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3868 case elfcpp::R_ARM_THM_MOVT_PREL
:
3871 case elfcpp::R_ARM_TARGET1
:
3872 // This should have been mapped to another type already.
3874 case elfcpp::R_ARM_COPY
:
3875 case elfcpp::R_ARM_GLOB_DAT
:
3876 case elfcpp::R_ARM_JUMP_SLOT
:
3877 case elfcpp::R_ARM_RELATIVE
:
3878 // These are relocations which should only be seen by the
3879 // dynamic linker, and should never be seen here.
3880 gold_error(_("%s: unexpected reloc %u in object file"),
3881 object
->name().c_str(), r_type
);
3885 object
->error(_("unsupported reloc %u in object file"), r_type
);
3890 // Scan the relocs during a relocatable link.
3892 template<bool big_endian
>
3894 Target_arm
<big_endian
>::scan_relocatable_relocs(
3895 const General_options
& options
,
3896 Symbol_table
* symtab
,
3898 Sized_relobj
<32, big_endian
>* object
,
3899 unsigned int data_shndx
,
3900 unsigned int sh_type
,
3901 const unsigned char* prelocs
,
3903 Output_section
* output_section
,
3904 bool needs_special_offset_handling
,
3905 size_t local_symbol_count
,
3906 const unsigned char* plocal_symbols
,
3907 Relocatable_relocs
* rr
)
3909 gold_assert(sh_type
== elfcpp::SHT_REL
);
3911 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
3912 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3914 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
3915 Scan_relocatable_relocs
>(
3924 needs_special_offset_handling
,
3930 // Relocate a section during a relocatable link.
3932 template<bool big_endian
>
3934 Target_arm
<big_endian
>::relocate_for_relocatable(
3935 const Relocate_info
<32, big_endian
>* relinfo
,
3936 unsigned int sh_type
,
3937 const unsigned char* prelocs
,
3939 Output_section
* output_section
,
3940 off_t offset_in_output_section
,
3941 const Relocatable_relocs
* rr
,
3942 unsigned char* view
,
3943 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
3944 section_size_type view_size
,
3945 unsigned char* reloc_view
,
3946 section_size_type reloc_view_size
)
3948 gold_assert(sh_type
== elfcpp::SHT_REL
);
3950 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
3955 offset_in_output_section
,
3964 // Return the value to use for a dynamic symbol which requires special
3965 // treatment. This is how we support equality comparisons of function
3966 // pointers across shared library boundaries, as described in the
3967 // processor specific ABI supplement.
3969 template<bool big_endian
>
3971 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
3973 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3974 return this->plt_section()->address() + gsym
->plt_offset();
3977 // Map platform-specific relocs to real relocs
3979 template<bool big_endian
>
3981 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
3985 case elfcpp::R_ARM_TARGET1
:
3986 // This is either R_ARM_ABS32 or R_ARM_REL32;
3987 return elfcpp::R_ARM_ABS32
;
3989 case elfcpp::R_ARM_TARGET2
:
3990 // This can be any reloc type but ususally is R_ARM_GOT_PREL
3991 return elfcpp::R_ARM_GOT_PREL
;
3998 // The selector for arm object files.
4000 template<bool big_endian
>
4001 class Target_selector_arm
: public Target_selector
4004 Target_selector_arm()
4005 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
4006 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
4010 do_instantiate_target()
4011 { return new Target_arm
<big_endian
>(); }
4014 Target_selector_arm
<false> target_selector_arm
;
4015 Target_selector_arm
<true> target_selector_armbe
;
4017 } // End anonymous namespace.