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
>
65 class Arm_output_section
;
67 template<bool big_endian
>
70 template<bool big_endian
>
74 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
84 // The arm target class.
86 // This is a very simple port of gold for ARM-EABI. It is intended for
87 // supporting Android only for the time being. Only these relocation types
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
124 // - Support more relocation types as needed.
125 // - Make PLTs more flexible for different architecture features like
127 // There are probably a lot more.
129 // Instruction template class. This class is similar to the insn_sequence
130 // struct in bfd/elf32-arm.c.
135 // Types of instruction templates.
144 // Factory methods to create instrunction templates in different formats.
146 static const Insn_template
147 thumb16_insn(uint32_t data
)
148 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
150 // A bit of a hack. A Thumb conditional branch, in which the proper
151 // condition is inserted when we build the stub.
152 static const Insn_template
153 thumb16_bcond_insn(uint32_t data
)
154 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 1); }
156 static const Insn_template
157 thumb32_insn(uint32_t data
)
158 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
160 static const Insn_template
161 thumb32_b_insn(uint32_t data
, int reloc_addend
)
163 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
167 static const Insn_template
168 arm_insn(uint32_t data
)
169 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
171 static const Insn_template
172 arm_rel_insn(unsigned data
, int reloc_addend
)
173 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
175 static const Insn_template
176 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
177 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
179 // Accessors. This class is used for read-only objects so no modifiers
184 { return this->data_
; }
186 // Return the instruction sequence type of this.
189 { return this->type_
; }
191 // Return the ARM relocation type of this.
194 { return this->r_type_
; }
198 { return this->reloc_addend_
; }
200 // Return size of instrunction template in bytes.
204 // Return byte-alignment of instrunction template.
209 // We make the constructor private to ensure that only the factory
212 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
213 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
216 // Instruction specific data. This is used to store information like
217 // some of the instruction bits.
219 // Instruction template type.
221 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
222 unsigned int r_type_
;
223 // Relocation addend.
224 int32_t reloc_addend_
;
227 // Macro for generating code to stub types. One entry per long/short
231 DEF_STUB(long_branch_any_any) \
232 DEF_STUB(long_branch_v4t_arm_thumb) \
233 DEF_STUB(long_branch_thumb_only) \
234 DEF_STUB(long_branch_v4t_thumb_thumb) \
235 DEF_STUB(long_branch_v4t_thumb_arm) \
236 DEF_STUB(short_branch_v4t_thumb_arm) \
237 DEF_STUB(long_branch_any_arm_pic) \
238 DEF_STUB(long_branch_any_thumb_pic) \
239 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
240 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
241 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
242 DEF_STUB(long_branch_thumb_only_pic) \
243 DEF_STUB(a8_veneer_b_cond) \
244 DEF_STUB(a8_veneer_b) \
245 DEF_STUB(a8_veneer_bl) \
246 DEF_STUB(a8_veneer_blx)
250 #define DEF_STUB(x) arm_stub_##x,
256 // First reloc stub type.
257 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
258 // Last reloc stub type.
259 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
261 // First Cortex-A8 stub type.
262 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
263 // Last Cortex-A8 stub type.
264 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
267 arm_stub_type_last
= arm_stub_a8_veneer_blx
271 // Stub template class. Templates are meant to be read-only objects.
272 // A stub template for a stub type contains all read-only attributes
273 // common to all stubs of the same type.
278 Stub_template(Stub_type
, const Insn_template
*, size_t);
286 { return this->type_
; }
288 // Return an array of instruction templates.
291 { return this->insns_
; }
293 // Return size of template in number of instructions.
296 { return this->insn_count_
; }
298 // Return size of template in bytes.
301 { return this->size_
; }
303 // Return alignment of the stub template.
306 { return this->alignment_
; }
308 // Return whether entry point is in thumb mode.
310 entry_in_thumb_mode() const
311 { return this->entry_in_thumb_mode_
; }
313 // Return number of relocations in this template.
316 { return this->relocs_
.size(); }
318 // Return index of the I-th instruction with relocation.
320 reloc_insn_index(size_t i
) const
322 gold_assert(i
< this->relocs_
.size());
323 return this->relocs_
[i
].first
;
326 // Return the offset of the I-th instruction with relocation from the
327 // beginning of the stub.
329 reloc_offset(size_t i
) const
331 gold_assert(i
< this->relocs_
.size());
332 return this->relocs_
[i
].second
;
336 // This contains information about an instruction template with a relocation
337 // and its offset from start of stub.
338 typedef std::pair
<size_t, section_size_type
> Reloc
;
340 // A Stub_template may not be copied. We want to share templates as much
342 Stub_template(const Stub_template
&);
343 Stub_template
& operator=(const Stub_template
&);
347 // Points to an array of Insn_templates.
348 const Insn_template
* insns_
;
349 // Number of Insn_templates in insns_[].
351 // Size of templated instructions in bytes.
353 // Alignment of templated instructions.
355 // Flag to indicate if entry is in thumb mode.
356 bool entry_in_thumb_mode_
;
357 // A table of reloc instruction indices and offsets. We can find these by
358 // looking at the instruction templates but we pre-compute and then stash
359 // them here for speed.
360 std::vector
<Reloc
> relocs_
;
364 // A class for code stubs. This is a base class for different type of
365 // stubs used in the ARM target.
371 static const section_offset_type invalid_offset
=
372 static_cast<section_offset_type
>(-1);
375 Stub(const Stub_template
* stub_template
)
376 : stub_template_(stub_template
), offset_(invalid_offset
)
383 // Return the stub template.
385 stub_template() const
386 { return this->stub_template_
; }
388 // Return offset of code stub from beginning of its containing stub table.
392 gold_assert(this->offset_
!= invalid_offset
);
393 return this->offset_
;
396 // Set offset of code stub from beginning of its containing stub table.
398 set_offset(section_offset_type offset
)
399 { this->offset_
= offset
; }
401 // Return the relocation target address of the i-th relocation in the
402 // stub. This must be defined in a child class.
404 reloc_target(size_t i
)
405 { return this->do_reloc_target(i
); }
407 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
409 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
410 { this->do_write(view
, view_size
, big_endian
); }
413 // This must be defined in the child class.
415 do_reloc_target(size_t) = 0;
417 // This must be defined in the child class.
419 do_write(unsigned char*, section_size_type
, bool) = 0;
423 const Stub_template
* stub_template_
;
424 // Offset within the section of containing this stub.
425 section_offset_type offset_
;
428 // Reloc stub class. These are stubs we use to fix up relocation because
429 // of limited branch ranges.
431 class Reloc_stub
: public Stub
434 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
435 // We assume we never jump to this address.
436 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
438 // Return destination address.
440 destination_address() const
442 gold_assert(this->destination_address_
!= this->invalid_address
);
443 return this->destination_address_
;
446 // Set destination address.
448 set_destination_address(Arm_address address
)
450 gold_assert(address
!= this->invalid_address
);
451 this->destination_address_
= address
;
454 // Reset destination address.
456 reset_destination_address()
457 { this->destination_address_
= this->invalid_address
; }
459 // Determine stub type for a branch of a relocation of R_TYPE going
460 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
461 // the branch target is a thumb instruction. TARGET is used for look
462 // up ARM-specific linker settings.
464 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
465 Arm_address branch_target
, bool target_is_thumb
);
467 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
468 // and an addend. Since we treat global and local symbol differently, we
469 // use a Symbol object for a global symbol and a object-index pair for
474 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
475 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
476 // and R_SYM must not be invalid_index.
477 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
478 unsigned int r_sym
, int32_t addend
)
479 : stub_type_(stub_type
), addend_(addend
)
483 this->r_sym_
= Reloc_stub::invalid_index
;
484 this->u_
.symbol
= symbol
;
488 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
489 this->r_sym_
= r_sym
;
490 this->u_
.relobj
= relobj
;
497 // Accessors: Keys are meant to be read-only object so no modifiers are
503 { return this->stub_type_
; }
505 // Return the local symbol index or invalid_index.
508 { return this->r_sym_
; }
510 // Return the symbol if there is one.
513 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
515 // Return the relobj if there is one.
518 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
520 // Whether this equals to another key k.
522 eq(const Key
& k
) const
524 return ((this->stub_type_
== k
.stub_type_
)
525 && (this->r_sym_
== k
.r_sym_
)
526 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
527 ? (this->u_
.relobj
== k
.u_
.relobj
)
528 : (this->u_
.symbol
== k
.u_
.symbol
))
529 && (this->addend_
== k
.addend_
));
532 // Return a hash value.
536 return (this->stub_type_
538 ^ gold::string_hash
<char>(
539 (this->r_sym_
!= Reloc_stub::invalid_index
)
540 ? this->u_
.relobj
->name().c_str()
541 : this->u_
.symbol
->name())
545 // Functors for STL associative containers.
549 operator()(const Key
& k
) const
550 { return k
.hash_value(); }
556 operator()(const Key
& k1
, const Key
& k2
) const
557 { return k1
.eq(k2
); }
560 // Name of key. This is mainly for debugging.
566 Stub_type stub_type_
;
567 // If this is a local symbol, this is the index in the defining object.
568 // Otherwise, it is invalid_index for a global symbol.
570 // If r_sym_ is invalid index. This points to a global symbol.
571 // Otherwise, this points a relobj. We used the unsized and target
572 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
573 // Arm_relobj. This is done to avoid making the stub class a template
574 // as most of the stub machinery is endianity-neutral. However, it
575 // may require a bit of casting done by users of this class.
578 const Symbol
* symbol
;
579 const Relobj
* relobj
;
581 // Addend associated with a reloc.
586 // Reloc_stubs are created via a stub factory. So these are protected.
587 Reloc_stub(const Stub_template
* stub_template
)
588 : Stub(stub_template
), destination_address_(invalid_address
)
594 friend class Stub_factory
;
597 // Return the relocation target address of the i-th relocation in the
600 do_reloc_target(size_t i
)
602 // All reloc stub have only one relocation.
604 return this->destination_address_
;
607 // A template to implement do_write below.
608 template<bool big_endian
>
610 do_fixed_endian_write(unsigned char*, section_size_type
);
614 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
);
616 // Address of destination.
617 Arm_address destination_address_
;
620 // Stub factory class.
625 // Return the unique instance of this class.
626 static const Stub_factory
&
629 static Stub_factory singleton
;
633 // Make a relocation stub.
635 make_reloc_stub(Stub_type stub_type
) const
637 gold_assert(stub_type
>= arm_stub_reloc_first
638 && stub_type
<= arm_stub_reloc_last
);
639 return new Reloc_stub(this->stub_templates_
[stub_type
]);
643 // Constructor and destructor are protected since we only return a single
644 // instance created in Stub_factory::get_instance().
648 // A Stub_factory may not be copied since it is a singleton.
649 Stub_factory(const Stub_factory
&);
650 Stub_factory
& operator=(Stub_factory
&);
652 // Stub templates. These are initialized in the constructor.
653 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
656 // A class to hold stubs for the ARM target.
658 template<bool big_endian
>
659 class Stub_table
: public Output_data
662 Stub_table(Arm_input_section
<big_endian
>* owner
)
663 : Output_data(), addralign_(1), owner_(owner
), has_been_changed_(false),
670 // Owner of this stub table.
671 Arm_input_section
<big_endian
>*
673 { return this->owner_
; }
675 // Whether this stub table is empty.
678 { return this->reloc_stubs_
.empty(); }
680 // Whether this has been changed.
682 has_been_changed() const
683 { return this->has_been_changed_
; }
685 // Set the has-been-changed flag.
687 set_has_been_changed(bool value
)
688 { this->has_been_changed_
= value
; }
690 // Return the current data size.
692 current_data_size() const
693 { return this->current_data_size_for_child(); }
695 // Add a STUB with using KEY. Caller is reponsible for avoid adding
696 // if already a STUB with the same key has been added.
698 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
);
700 // Look up a relocation stub using KEY. Return NULL if there is none.
702 find_reloc_stub(const Reloc_stub::Key
& key
) const
704 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
705 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
708 // Relocate stubs in this stub table.
710 relocate_stubs(const Relocate_info
<32, big_endian
>*,
711 Target_arm
<big_endian
>*, Output_section
*,
712 unsigned char*, Arm_address
, section_size_type
);
715 // Write out section contents.
717 do_write(Output_file
*);
719 // Return the required alignment.
722 { return this->addralign_
; }
724 // Finalize data size.
726 set_final_data_size()
727 { this->set_data_size(this->current_data_size_for_child()); }
729 // Reset address and file offset.
731 do_reset_address_and_file_offset();
734 // Unordered map of stubs.
736 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
737 Reloc_stub::Key::equal_to
>
742 // Owner of this stub table.
743 Arm_input_section
<big_endian
>* owner_
;
744 // This is set to true during relaxiong if the size of the stub table
746 bool has_been_changed_
;
747 // The relocation stubs.
748 Reloc_stub_map reloc_stubs_
;
751 // A class to wrap an ordinary input section containing executable code.
753 template<bool big_endian
>
754 class Arm_input_section
: public Output_relaxed_input_section
757 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
758 : Output_relaxed_input_section(relobj
, shndx
, 1),
759 original_addralign_(1), original_size_(0), stub_table_(NULL
)
769 // Whether this is a stub table owner.
771 is_stub_table_owner() const
772 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
774 // Return the stub table.
775 Stub_table
<big_endian
>*
777 { return this->stub_table_
; }
779 // Set the stub_table.
781 set_stub_table(Stub_table
<big_endian
>* stub_table
)
782 { this->stub_table_
= stub_table
; }
784 // Downcast a base pointer to an Arm_input_section pointer. This is
785 // not type-safe but we only use Arm_input_section not the base class.
786 static Arm_input_section
<big_endian
>*
787 as_arm_input_section(Output_relaxed_input_section
* poris
)
788 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
791 // Write data to output file.
793 do_write(Output_file
*);
795 // Return required alignment of this.
799 if (this->is_stub_table_owner())
800 return std::max(this->stub_table_
->addralign(),
801 this->original_addralign_
);
803 return this->original_addralign_
;
806 // Finalize data size.
808 set_final_data_size();
810 // Reset address and file offset.
812 do_reset_address_and_file_offset();
816 do_output_offset(const Relobj
* object
, unsigned int shndx
,
817 section_offset_type offset
,
818 section_offset_type
* poutput
) const
820 if ((object
== this->relobj())
821 && (shndx
== this->shndx())
823 && (convert_types
<uint64_t, section_offset_type
>(offset
)
824 <= this->original_size_
))
834 // Copying is not allowed.
835 Arm_input_section(const Arm_input_section
&);
836 Arm_input_section
& operator=(const Arm_input_section
&);
838 // Address alignment of the original input section.
839 uint64_t original_addralign_
;
840 // Section size of the original input section.
841 uint64_t original_size_
;
843 Stub_table
<big_endian
>* stub_table_
;
846 // Arm output section class. This is defined mainly to add a number of
847 // stub generation methods.
849 template<bool big_endian
>
850 class Arm_output_section
: public Output_section
853 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
854 elfcpp::Elf_Xword flags
)
855 : Output_section(name
, type
, flags
)
858 ~Arm_output_section()
861 // Group input sections for stub generation.
863 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
865 // Downcast a base pointer to an Arm_output_section pointer. This is
866 // not type-safe but we only use Arm_output_section not the base class.
867 static Arm_output_section
<big_endian
>*
868 as_arm_output_section(Output_section
* os
)
869 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
873 typedef Output_section::Input_section Input_section
;
874 typedef Output_section::Input_section_list Input_section_list
;
876 // Create a stub group.
877 void create_stub_group(Input_section_list::const_iterator
,
878 Input_section_list::const_iterator
,
879 Input_section_list::const_iterator
,
880 Target_arm
<big_endian
>*,
881 std::vector
<Output_relaxed_input_section
*>*);
886 template<bool big_endian
>
887 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
890 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
892 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
893 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
894 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
895 stub_tables_(), local_symbol_is_thumb_function_()
901 // Return the stub table of the SHNDX-th section if there is one.
902 Stub_table
<big_endian
>*
903 stub_table(unsigned int shndx
) const
905 gold_assert(shndx
< this->stub_tables_
.size());
906 return this->stub_tables_
[shndx
];
909 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
911 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
913 gold_assert(shndx
< this->stub_tables_
.size());
914 this->stub_tables_
[shndx
] = stub_table
;
917 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
918 // index. This is only valid after do_count_local_symbol is called.
920 local_symbol_is_thumb_function(unsigned int r_sym
) const
922 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
923 return this->local_symbol_is_thumb_function_
[r_sym
];
926 // Scan all relocation sections for stub generation.
928 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
931 // Convert regular input section with index SHNDX to a relaxed section.
933 convert_input_section_to_relaxed_section(unsigned shndx
)
935 // The stubs have relocations and we need to process them after writing
936 // out the stubs. So relocation now must follow section write.
937 this->invalidate_section_offset(shndx
);
938 this->set_relocs_must_follow_section_writes();
941 // Downcast a base pointer to an Arm_relobj pointer. This is
942 // not type-safe but we only use Arm_relobj not the base class.
943 static Arm_relobj
<big_endian
>*
944 as_arm_relobj(Relobj
* relobj
)
945 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
947 // Processor-specific flags in ELF file header. This is valid only after
950 processor_specific_flags() const
951 { return this->processor_specific_flags_
; }
954 // Post constructor setup.
958 // Call parent's setup method.
959 Sized_relobj
<32, big_endian
>::do_setup();
961 // Initialize look-up tables.
962 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
963 this->stub_tables_
.swap(empty_stub_table_list
);
966 // Count the local symbols.
968 do_count_local_symbols(Stringpool_template
<char>*,
969 Stringpool_template
<char>*);
972 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
973 const unsigned char* pshdrs
,
974 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
976 // Read the symbol information.
978 do_read_symbols(Read_symbols_data
* sd
);
981 // List of stub tables.
982 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
983 Stub_table_list stub_tables_
;
984 // Bit vector to tell if a local symbol is a thumb function or not.
985 // This is only valid after do_count_local_symbol is called.
986 std::vector
<bool> local_symbol_is_thumb_function_
;
987 // processor-specific flags in ELF file header.
988 elfcpp::Elf_Word processor_specific_flags_
;
993 template<bool big_endian
>
994 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
997 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
998 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
999 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1000 processor_specific_flags_(0)
1006 // Downcast a base pointer to an Arm_relobj pointer. This is
1007 // not type-safe but we only use Arm_relobj not the base class.
1008 static Arm_dynobj
<big_endian
>*
1009 as_arm_dynobj(Dynobj
* dynobj
)
1010 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1012 // Processor-specific flags in ELF file header. This is valid only after
1015 processor_specific_flags() const
1016 { return this->processor_specific_flags_
; }
1019 // Read the symbol information.
1021 do_read_symbols(Read_symbols_data
* sd
);
1024 // processor-specific flags in ELF file header.
1025 elfcpp::Elf_Word processor_specific_flags_
;
1028 // Functor to read reloc addends during stub generation.
1030 template<int sh_type
, bool big_endian
>
1031 struct Stub_addend_reader
1033 // Return the addend for a relocation of a particular type. Depending
1034 // on whether this is a REL or RELA relocation, read the addend from a
1035 // view or from a Reloc object.
1036 elfcpp::Elf_types
<32>::Elf_Swxword
1038 unsigned int /* r_type */,
1039 const unsigned char* /* view */,
1040 const typename Reloc_types
<sh_type
,
1041 32, big_endian
>::Reloc
& /* reloc */) const;
1044 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1046 template<bool big_endian
>
1047 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1049 elfcpp::Elf_types
<32>::Elf_Swxword
1052 const unsigned char*,
1053 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1056 // Specialized Stub_addend_reader for RELA type relocation sections.
1057 // We currently do not handle RELA type relocation sections but it is trivial
1058 // to implement the addend reader. This is provided for completeness and to
1059 // make it easier to add support for RELA relocation sections in the future.
1061 template<bool big_endian
>
1062 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1064 elfcpp::Elf_types
<32>::Elf_Swxword
1067 const unsigned char*,
1068 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1069 big_endian
>::Reloc
& reloc
) const
1070 { return reloc
.get_r_addend(); }
1073 // Utilities for manipulating integers of up to 32-bits
1077 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1078 // an int32_t. NO_BITS must be between 1 to 32.
1079 template<int no_bits
>
1080 static inline int32_t
1081 sign_extend(uint32_t bits
)
1083 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1085 return static_cast<int32_t>(bits
);
1086 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1088 uint32_t top_bit
= 1U << (no_bits
- 1);
1089 int32_t as_signed
= static_cast<int32_t>(bits
);
1090 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1093 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1094 template<int no_bits
>
1096 has_overflow(uint32_t bits
)
1098 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1101 int32_t max
= (1 << (no_bits
- 1)) - 1;
1102 int32_t min
= -(1 << (no_bits
- 1));
1103 int32_t as_signed
= static_cast<int32_t>(bits
);
1104 return as_signed
> max
|| as_signed
< min
;
1107 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1108 // fits in the given number of bits as either a signed or unsigned value.
1109 // For example, has_signed_unsigned_overflow<8> would check
1110 // -128 <= bits <= 255
1111 template<int no_bits
>
1113 has_signed_unsigned_overflow(uint32_t bits
)
1115 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1118 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1119 int32_t min
= -(1 << (no_bits
- 1));
1120 int32_t as_signed
= static_cast<int32_t>(bits
);
1121 return as_signed
> max
|| as_signed
< min
;
1124 // Select bits from A and B using bits in MASK. For each n in [0..31],
1125 // the n-th bit in the result is chosen from the n-th bits of A and B.
1126 // A zero selects A and a one selects B.
1127 static inline uint32_t
1128 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1129 { return (a
& ~mask
) | (b
& mask
); }
1132 template<bool big_endian
>
1133 class Target_arm
: public Sized_target
<32, big_endian
>
1136 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1139 // When were are relocating a stub, we pass this as the relocation number.
1140 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1143 : Sized_target
<32, big_endian
>(&arm_info
),
1144 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1145 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1146 stub_factory_(Stub_factory::get_instance()),
1147 may_use_blx_(true), should_force_pic_veneer_(false),
1148 arm_input_section_map_()
1151 // Whether we can use BLX.
1154 { return this->may_use_blx_
; }
1156 // Set use-BLX flag.
1158 set_may_use_blx(bool value
)
1159 { this->may_use_blx_
= value
; }
1161 // Whether we force PCI branch veneers.
1163 should_force_pic_veneer() const
1164 { return this->should_force_pic_veneer_
; }
1166 // Set PIC veneer flag.
1168 set_should_force_pic_veneer(bool value
)
1169 { this->should_force_pic_veneer_
= value
; }
1171 // Whether we use THUMB-2 instructions.
1173 using_thumb2() const
1175 // FIXME: This should not hard-coded.
1179 // Whether we use THUMB/THUMB-2 instructions only.
1181 using_thumb_only() const
1183 // FIXME: This should not hard-coded.
1187 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1189 may_use_arm_nop() const
1191 // FIXME: This should not hard-coded.
1195 // Whether we have THUMB-2 NOP.W instruction.
1197 may_use_thumb2_nop() const
1199 // FIXME: This should not hard-coded.
1203 // Process the relocations to determine unreferenced sections for
1204 // garbage collection.
1206 gc_process_relocs(Symbol_table
* symtab
,
1208 Sized_relobj
<32, big_endian
>* object
,
1209 unsigned int data_shndx
,
1210 unsigned int sh_type
,
1211 const unsigned char* prelocs
,
1213 Output_section
* output_section
,
1214 bool needs_special_offset_handling
,
1215 size_t local_symbol_count
,
1216 const unsigned char* plocal_symbols
);
1218 // Scan the relocations to look for symbol adjustments.
1220 scan_relocs(Symbol_table
* symtab
,
1222 Sized_relobj
<32, big_endian
>* object
,
1223 unsigned int data_shndx
,
1224 unsigned int sh_type
,
1225 const unsigned char* prelocs
,
1227 Output_section
* output_section
,
1228 bool needs_special_offset_handling
,
1229 size_t local_symbol_count
,
1230 const unsigned char* plocal_symbols
);
1232 // Finalize the sections.
1234 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1236 // Return the value to use for a dynamic symbol which requires special
1239 do_dynsym_value(const Symbol
*) const;
1241 // Relocate a section.
1243 relocate_section(const Relocate_info
<32, big_endian
>*,
1244 unsigned int sh_type
,
1245 const unsigned char* prelocs
,
1247 Output_section
* output_section
,
1248 bool needs_special_offset_handling
,
1249 unsigned char* view
,
1250 Arm_address view_address
,
1251 section_size_type view_size
,
1252 const Reloc_symbol_changes
*);
1254 // Scan the relocs during a relocatable link.
1256 scan_relocatable_relocs(Symbol_table
* symtab
,
1258 Sized_relobj
<32, big_endian
>* object
,
1259 unsigned int data_shndx
,
1260 unsigned int sh_type
,
1261 const unsigned char* prelocs
,
1263 Output_section
* output_section
,
1264 bool needs_special_offset_handling
,
1265 size_t local_symbol_count
,
1266 const unsigned char* plocal_symbols
,
1267 Relocatable_relocs
*);
1269 // Relocate a section during a relocatable link.
1271 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1272 unsigned int sh_type
,
1273 const unsigned char* prelocs
,
1275 Output_section
* output_section
,
1276 off_t offset_in_output_section
,
1277 const Relocatable_relocs
*,
1278 unsigned char* view
,
1279 Arm_address view_address
,
1280 section_size_type view_size
,
1281 unsigned char* reloc_view
,
1282 section_size_type reloc_view_size
);
1284 // Return whether SYM is defined by the ABI.
1286 do_is_defined_by_abi(Symbol
* sym
) const
1287 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1289 // Return the size of the GOT section.
1293 gold_assert(this->got_
!= NULL
);
1294 return this->got_
->data_size();
1297 // Map platform-specific reloc types
1299 get_real_reloc_type (unsigned int r_type
);
1302 // Methods to support stub-generations.
1305 // Return the stub factory
1307 stub_factory() const
1308 { return this->stub_factory_
; }
1310 // Make a new Arm_input_section object.
1311 Arm_input_section
<big_endian
>*
1312 new_arm_input_section(Relobj
*, unsigned int);
1314 // Find the Arm_input_section object corresponding to the SHNDX-th input
1315 // section of RELOBJ.
1316 Arm_input_section
<big_endian
>*
1317 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1319 // Make a new Stub_table
1320 Stub_table
<big_endian
>*
1321 new_stub_table(Arm_input_section
<big_endian
>*);
1323 // Scan a section for stub generation.
1325 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1326 const unsigned char*, size_t, Output_section
*,
1327 bool, const unsigned char*, Arm_address
,
1332 relocate_stub(Reloc_stub
*, const Relocate_info
<32, big_endian
>*,
1333 Output_section
*, unsigned char*, Arm_address
,
1336 // Get the default ARM target.
1337 static Target_arm
<big_endian
>*
1340 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1341 && parameters
->target().is_big_endian() == big_endian
);
1342 return static_cast<Target_arm
<big_endian
>*>(
1343 parameters
->sized_target
<32, big_endian
>());
1346 // Whether relocation type uses LSB to distinguish THUMB addresses.
1348 reloc_uses_thumb_bit(unsigned int r_type
);
1351 // Make an ELF object.
1353 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1354 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1357 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1358 const elfcpp::Ehdr
<32, !big_endian
>&)
1359 { gold_unreachable(); }
1362 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1363 const elfcpp::Ehdr
<64, false>&)
1364 { gold_unreachable(); }
1367 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1368 const elfcpp::Ehdr
<64, true>&)
1369 { gold_unreachable(); }
1371 // Make an output section.
1373 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1374 elfcpp::Elf_Xword flags
)
1375 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1378 do_adjust_elf_header(unsigned char* view
, int len
) const;
1380 // We only need to generate stubs, and hence perform relaxation if we are
1381 // not doing relocatable linking.
1383 do_may_relax() const
1384 { return !parameters
->options().relocatable(); }
1387 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1390 // The class which scans relocations.
1395 : issued_non_pic_error_(false)
1399 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1400 Sized_relobj
<32, big_endian
>* object
,
1401 unsigned int data_shndx
,
1402 Output_section
* output_section
,
1403 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1404 const elfcpp::Sym
<32, big_endian
>& lsym
);
1407 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1408 Sized_relobj
<32, big_endian
>* object
,
1409 unsigned int data_shndx
,
1410 Output_section
* output_section
,
1411 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1416 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1417 unsigned int r_type
);
1420 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1421 unsigned int r_type
, Symbol
*);
1424 check_non_pic(Relobj
*, unsigned int r_type
);
1426 // Almost identical to Symbol::needs_plt_entry except that it also
1427 // handles STT_ARM_TFUNC.
1429 symbol_needs_plt_entry(const Symbol
* sym
)
1431 // An undefined symbol from an executable does not need a PLT entry.
1432 if (sym
->is_undefined() && !parameters
->options().shared())
1435 return (!parameters
->doing_static_link()
1436 && (sym
->type() == elfcpp::STT_FUNC
1437 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1438 && (sym
->is_from_dynobj()
1439 || sym
->is_undefined()
1440 || sym
->is_preemptible()));
1443 // Whether we have issued an error about a non-PIC compilation.
1444 bool issued_non_pic_error_
;
1447 // The class which implements relocation.
1457 // Return whether the static relocation needs to be applied.
1459 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1462 Output_section
* output_section
);
1464 // Do a relocation. Return false if the caller should not issue
1465 // any warnings about this relocation.
1467 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1468 Output_section
*, size_t relnum
,
1469 const elfcpp::Rel
<32, big_endian
>&,
1470 unsigned int r_type
, const Sized_symbol
<32>*,
1471 const Symbol_value
<32>*,
1472 unsigned char*, Arm_address
,
1475 // Return whether we want to pass flag NON_PIC_REF for this
1476 // reloc. This means the relocation type accesses a symbol not via
1479 reloc_is_non_pic (unsigned int r_type
)
1483 // These relocation types reference GOT or PLT entries explicitly.
1484 case elfcpp::R_ARM_GOT_BREL
:
1485 case elfcpp::R_ARM_GOT_ABS
:
1486 case elfcpp::R_ARM_GOT_PREL
:
1487 case elfcpp::R_ARM_GOT_BREL12
:
1488 case elfcpp::R_ARM_PLT32_ABS
:
1489 case elfcpp::R_ARM_TLS_GD32
:
1490 case elfcpp::R_ARM_TLS_LDM32
:
1491 case elfcpp::R_ARM_TLS_IE32
:
1492 case elfcpp::R_ARM_TLS_IE12GP
:
1494 // These relocate types may use PLT entries.
1495 case elfcpp::R_ARM_CALL
:
1496 case elfcpp::R_ARM_THM_CALL
:
1497 case elfcpp::R_ARM_JUMP24
:
1498 case elfcpp::R_ARM_THM_JUMP24
:
1499 case elfcpp::R_ARM_THM_JUMP19
:
1500 case elfcpp::R_ARM_PLT32
:
1501 case elfcpp::R_ARM_THM_XPC22
:
1510 // A class which returns the size required for a relocation type,
1511 // used while scanning relocs during a relocatable link.
1512 class Relocatable_size_for_reloc
1516 get_size_for_reloc(unsigned int, Relobj
*);
1519 // Get the GOT section, creating it if necessary.
1520 Output_data_got
<32, big_endian
>*
1521 got_section(Symbol_table
*, Layout
*);
1523 // Get the GOT PLT section.
1525 got_plt_section() const
1527 gold_assert(this->got_plt_
!= NULL
);
1528 return this->got_plt_
;
1531 // Create a PLT entry for a global symbol.
1533 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1535 // Get the PLT section.
1536 const Output_data_plt_arm
<big_endian
>*
1539 gold_assert(this->plt_
!= NULL
);
1543 // Get the dynamic reloc section, creating it if necessary.
1545 rel_dyn_section(Layout
*);
1547 // Return true if the symbol may need a COPY relocation.
1548 // References from an executable object to non-function symbols
1549 // defined in a dynamic object may need a COPY relocation.
1551 may_need_copy_reloc(Symbol
* gsym
)
1553 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1554 && gsym
->may_need_copy_reloc());
1557 // Add a potential copy relocation.
1559 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1560 Sized_relobj
<32, big_endian
>* object
,
1561 unsigned int shndx
, Output_section
* output_section
,
1562 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1564 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1565 symtab
->get_sized_symbol
<32>(sym
),
1566 object
, shndx
, output_section
, reloc
,
1567 this->rel_dyn_section(layout
));
1570 // Whether two EABI versions are compatible.
1572 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1574 // Merge processor-specific flags from input object and those in the ELF
1575 // header of the output.
1577 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1580 // Methods to support stub-generations.
1583 // Group input sections for stub generation.
1585 group_sections(Layout
*, section_size_type
, bool);
1587 // Scan a relocation for stub generation.
1589 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1590 const Sized_symbol
<32>*, unsigned int,
1591 const Symbol_value
<32>*,
1592 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1594 // Scan a relocation section for stub.
1595 template<int sh_type
>
1597 scan_reloc_section_for_stubs(
1598 const Relocate_info
<32, big_endian
>* relinfo
,
1599 const unsigned char* prelocs
,
1601 Output_section
* output_section
,
1602 bool needs_special_offset_handling
,
1603 const unsigned char* view
,
1604 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1607 // Information about this specific target which we pass to the
1608 // general Target structure.
1609 static const Target::Target_info arm_info
;
1611 // The types of GOT entries needed for this platform.
1614 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1617 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1619 // Map input section to Arm_input_section.
1620 typedef Unordered_map
<Input_section_specifier
,
1621 Arm_input_section
<big_endian
>*,
1622 Input_section_specifier::hash
,
1623 Input_section_specifier::equal_to
>
1624 Arm_input_section_map
;
1627 Output_data_got
<32, big_endian
>* got_
;
1629 Output_data_plt_arm
<big_endian
>* plt_
;
1630 // The GOT PLT section.
1631 Output_data_space
* got_plt_
;
1632 // The dynamic reloc section.
1633 Reloc_section
* rel_dyn_
;
1634 // Relocs saved to avoid a COPY reloc.
1635 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1636 // Space for variables copied with a COPY reloc.
1637 Output_data_space
* dynbss_
;
1638 // Vector of Stub_tables created.
1639 Stub_table_list stub_tables_
;
1641 const Stub_factory
&stub_factory_
;
1642 // Whether we can use BLX.
1644 // Whether we force PIC branch veneers.
1645 bool should_force_pic_veneer_
;
1646 // Map for locating Arm_input_sections.
1647 Arm_input_section_map arm_input_section_map_
;
1650 template<bool big_endian
>
1651 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1654 big_endian
, // is_big_endian
1655 elfcpp::EM_ARM
, // machine_code
1656 false, // has_make_symbol
1657 false, // has_resolve
1658 false, // has_code_fill
1659 true, // is_default_stack_executable
1661 "/usr/lib/libc.so.1", // dynamic_linker
1662 0x8000, // default_text_segment_address
1663 0x1000, // abi_pagesize (overridable by -z max-page-size)
1664 0x1000, // common_pagesize (overridable by -z common-page-size)
1665 elfcpp::SHN_UNDEF
, // small_common_shndx
1666 elfcpp::SHN_UNDEF
, // large_common_shndx
1667 0, // small_common_section_flags
1668 0 // large_common_section_flags
1671 // Arm relocate functions class
1674 template<bool big_endian
>
1675 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1680 STATUS_OKAY
, // No error during relocation.
1681 STATUS_OVERFLOW
, // Relocation oveflow.
1682 STATUS_BAD_RELOC
// Relocation cannot be applied.
1686 typedef Relocate_functions
<32, big_endian
> Base
;
1687 typedef Arm_relocate_functions
<big_endian
> This
;
1689 // Encoding of imm16 argument for movt and movw ARM instructions
1692 // imm16 := imm4 | imm12
1694 // 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
1695 // +-------+---------------+-------+-------+-----------------------+
1696 // | | |imm4 | |imm12 |
1697 // +-------+---------------+-------+-------+-----------------------+
1699 // Extract the relocation addend from VAL based on the ARM
1700 // instruction encoding described above.
1701 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1702 extract_arm_movw_movt_addend(
1703 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1705 // According to the Elf ABI for ARM Architecture the immediate
1706 // field is sign-extended to form the addend.
1707 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1710 // Insert X into VAL based on the ARM instruction encoding described
1712 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1713 insert_val_arm_movw_movt(
1714 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1715 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1719 val
|= (x
& 0xf000) << 4;
1723 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1726 // imm16 := imm4 | i | imm3 | imm8
1728 // 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
1729 // +---------+-+-----------+-------++-+-----+-------+---------------+
1730 // | |i| |imm4 || |imm3 | |imm8 |
1731 // +---------+-+-----------+-------++-+-----+-------+---------------+
1733 // Extract the relocation addend from VAL based on the Thumb2
1734 // instruction encoding described above.
1735 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1736 extract_thumb_movw_movt_addend(
1737 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1739 // According to the Elf ABI for ARM Architecture the immediate
1740 // field is sign-extended to form the addend.
1741 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1742 | ((val
>> 15) & 0x0800)
1743 | ((val
>> 4) & 0x0700)
1747 // Insert X into VAL based on the Thumb2 instruction encoding
1749 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1750 insert_val_thumb_movw_movt(
1751 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1752 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1755 val
|= (x
& 0xf000) << 4;
1756 val
|= (x
& 0x0800) << 15;
1757 val
|= (x
& 0x0700) << 4;
1758 val
|= (x
& 0x00ff);
1762 // Handle ARM long branches.
1763 static typename
This::Status
1764 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1765 unsigned char *, const Sized_symbol
<32>*,
1766 const Arm_relobj
<big_endian
>*, unsigned int,
1767 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1769 // Handle THUMB long branches.
1770 static typename
This::Status
1771 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1772 unsigned char *, const Sized_symbol
<32>*,
1773 const Arm_relobj
<big_endian
>*, unsigned int,
1774 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1778 // R_ARM_ABS8: S + A
1779 static inline typename
This::Status
1780 abs8(unsigned char *view
,
1781 const Sized_relobj
<32, big_endian
>* object
,
1782 const Symbol_value
<32>* psymval
)
1784 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1785 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1786 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1787 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1788 Reltype addend
= utils::sign_extend
<8>(val
);
1789 Reltype x
= psymval
->value(object
, addend
);
1790 val
= utils::bit_select(val
, x
, 0xffU
);
1791 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1792 return (utils::has_signed_unsigned_overflow
<8>(x
)
1793 ? This::STATUS_OVERFLOW
1794 : This::STATUS_OKAY
);
1797 // R_ARM_THM_ABS5: S + A
1798 static inline typename
This::Status
1799 thm_abs5(unsigned char *view
,
1800 const Sized_relobj
<32, big_endian
>* object
,
1801 const Symbol_value
<32>* psymval
)
1803 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1804 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1805 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1806 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1807 Reltype addend
= (val
& 0x7e0U
) >> 6;
1808 Reltype x
= psymval
->value(object
, addend
);
1809 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1810 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1811 return (utils::has_overflow
<5>(x
)
1812 ? This::STATUS_OVERFLOW
1813 : This::STATUS_OKAY
);
1816 // R_ARM_ABS12: S + A
1817 static inline typename
This::Status
1818 abs12(unsigned char *view
,
1819 const Sized_relobj
<32, big_endian
>* object
,
1820 const Symbol_value
<32>* psymval
)
1822 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1823 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1824 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1825 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1826 Reltype addend
= val
& 0x0fffU
;
1827 Reltype x
= psymval
->value(object
, addend
);
1828 val
= utils::bit_select(val
, x
, 0x0fffU
);
1829 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1830 return (utils::has_overflow
<12>(x
)
1831 ? This::STATUS_OVERFLOW
1832 : This::STATUS_OKAY
);
1835 // R_ARM_ABS16: S + A
1836 static inline typename
This::Status
1837 abs16(unsigned char *view
,
1838 const Sized_relobj
<32, big_endian
>* object
,
1839 const Symbol_value
<32>* psymval
)
1841 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1842 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1843 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1844 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1845 Reltype addend
= utils::sign_extend
<16>(val
);
1846 Reltype x
= psymval
->value(object
, addend
);
1847 val
= utils::bit_select(val
, x
, 0xffffU
);
1848 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1849 return (utils::has_signed_unsigned_overflow
<16>(x
)
1850 ? This::STATUS_OVERFLOW
1851 : This::STATUS_OKAY
);
1854 // R_ARM_ABS32: (S + A) | T
1855 static inline typename
This::Status
1856 abs32(unsigned char *view
,
1857 const Sized_relobj
<32, big_endian
>* object
,
1858 const Symbol_value
<32>* psymval
,
1859 Arm_address thumb_bit
)
1861 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1862 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1863 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1864 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
1865 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1866 return This::STATUS_OKAY
;
1869 // R_ARM_REL32: (S + A) | T - P
1870 static inline typename
This::Status
1871 rel32(unsigned char *view
,
1872 const Sized_relobj
<32, big_endian
>* object
,
1873 const Symbol_value
<32>* psymval
,
1874 Arm_address address
,
1875 Arm_address thumb_bit
)
1877 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1878 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1879 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1880 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
1881 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1882 return This::STATUS_OKAY
;
1885 // R_ARM_THM_CALL: (S + A) | T - P
1886 static inline typename
This::Status
1887 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1888 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1889 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1890 Arm_address address
, Arm_address thumb_bit
,
1891 bool is_weakly_undefined_without_plt
)
1893 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
1894 object
, r_sym
, psymval
, address
, thumb_bit
,
1895 is_weakly_undefined_without_plt
);
1898 // R_ARM_THM_JUMP24: (S + A) | T - P
1899 static inline typename
This::Status
1900 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1901 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1902 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1903 Arm_address address
, Arm_address thumb_bit
,
1904 bool is_weakly_undefined_without_plt
)
1906 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
1907 object
, r_sym
, psymval
, address
, thumb_bit
,
1908 is_weakly_undefined_without_plt
);
1911 // R_ARM_THM_XPC22: (S + A) | T - P
1912 static inline typename
This::Status
1913 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1914 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1915 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1916 Arm_address address
, Arm_address thumb_bit
,
1917 bool is_weakly_undefined_without_plt
)
1919 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
1920 object
, r_sym
, psymval
, address
, thumb_bit
,
1921 is_weakly_undefined_without_plt
);
1924 // R_ARM_BASE_PREL: B(S) + A - P
1925 static inline typename
This::Status
1926 base_prel(unsigned char* view
,
1928 Arm_address address
)
1930 Base::rel32(view
, origin
- address
);
1934 // R_ARM_BASE_ABS: B(S) + A
1935 static inline typename
This::Status
1936 base_abs(unsigned char* view
,
1939 Base::rel32(view
, origin
);
1943 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1944 static inline typename
This::Status
1945 got_brel(unsigned char* view
,
1946 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
1948 Base::rel32(view
, got_offset
);
1949 return This::STATUS_OKAY
;
1952 // R_ARM_GOT_PREL: GOT(S) + A - P
1953 static inline typename
This::Status
1954 got_prel(unsigned char *view
,
1955 Arm_address got_entry
,
1956 Arm_address address
)
1958 Base::rel32(view
, got_entry
- address
);
1959 return This::STATUS_OKAY
;
1962 // R_ARM_PLT32: (S + A) | T - P
1963 static inline typename
This::Status
1964 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
1965 unsigned char *view
,
1966 const Sized_symbol
<32>* gsym
,
1967 const Arm_relobj
<big_endian
>* object
,
1969 const Symbol_value
<32>* psymval
,
1970 Arm_address address
,
1971 Arm_address thumb_bit
,
1972 bool is_weakly_undefined_without_plt
)
1974 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
1975 object
, r_sym
, psymval
, address
, thumb_bit
,
1976 is_weakly_undefined_without_plt
);
1979 // R_ARM_XPC25: (S + A) | T - P
1980 static inline typename
This::Status
1981 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
1982 unsigned char *view
,
1983 const Sized_symbol
<32>* gsym
,
1984 const Arm_relobj
<big_endian
>* object
,
1986 const Symbol_value
<32>* psymval
,
1987 Arm_address address
,
1988 Arm_address thumb_bit
,
1989 bool is_weakly_undefined_without_plt
)
1991 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
1992 object
, r_sym
, psymval
, address
, thumb_bit
,
1993 is_weakly_undefined_without_plt
);
1996 // R_ARM_CALL: (S + A) | T - P
1997 static inline typename
This::Status
1998 call(const Relocate_info
<32, big_endian
>* relinfo
,
1999 unsigned char *view
,
2000 const Sized_symbol
<32>* gsym
,
2001 const Arm_relobj
<big_endian
>* object
,
2003 const Symbol_value
<32>* psymval
,
2004 Arm_address address
,
2005 Arm_address thumb_bit
,
2006 bool is_weakly_undefined_without_plt
)
2008 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2009 object
, r_sym
, psymval
, address
, thumb_bit
,
2010 is_weakly_undefined_without_plt
);
2013 // R_ARM_JUMP24: (S + A) | T - P
2014 static inline typename
This::Status
2015 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2016 unsigned char *view
,
2017 const Sized_symbol
<32>* gsym
,
2018 const Arm_relobj
<big_endian
>* object
,
2020 const Symbol_value
<32>* psymval
,
2021 Arm_address address
,
2022 Arm_address thumb_bit
,
2023 bool is_weakly_undefined_without_plt
)
2025 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2026 object
, r_sym
, psymval
, address
, thumb_bit
,
2027 is_weakly_undefined_without_plt
);
2030 // R_ARM_PREL: (S + A) | T - P
2031 static inline typename
This::Status
2032 prel31(unsigned char *view
,
2033 const Sized_relobj
<32, big_endian
>* object
,
2034 const Symbol_value
<32>* psymval
,
2035 Arm_address address
,
2036 Arm_address thumb_bit
)
2038 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2039 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2040 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2041 Valtype addend
= utils::sign_extend
<31>(val
);
2042 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2043 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2044 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2045 return (utils::has_overflow
<31>(x
) ?
2046 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2049 // R_ARM_MOVW_ABS_NC: (S + A) | T
2050 static inline typename
This::Status
2051 movw_abs_nc(unsigned char *view
,
2052 const Sized_relobj
<32, big_endian
>* object
,
2053 const Symbol_value
<32>* psymval
,
2054 Arm_address thumb_bit
)
2056 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2057 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2058 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2059 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2060 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2061 val
= This::insert_val_arm_movw_movt(val
, x
);
2062 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2063 return This::STATUS_OKAY
;
2066 // R_ARM_MOVT_ABS: S + A
2067 static inline typename
This::Status
2068 movt_abs(unsigned char *view
,
2069 const Sized_relobj
<32, big_endian
>* object
,
2070 const Symbol_value
<32>* psymval
)
2072 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2073 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2074 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2075 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2076 Valtype x
= psymval
->value(object
, addend
) >> 16;
2077 val
= This::insert_val_arm_movw_movt(val
, x
);
2078 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2079 return This::STATUS_OKAY
;
2082 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2083 static inline typename
This::Status
2084 thm_movw_abs_nc(unsigned char *view
,
2085 const Sized_relobj
<32, big_endian
>* object
,
2086 const Symbol_value
<32>* psymval
,
2087 Arm_address thumb_bit
)
2089 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2090 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2091 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2092 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2093 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2094 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2095 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2096 val
= This::insert_val_thumb_movw_movt(val
, x
);
2097 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2098 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2099 return This::STATUS_OKAY
;
2102 // R_ARM_THM_MOVT_ABS: S + A
2103 static inline typename
This::Status
2104 thm_movt_abs(unsigned char *view
,
2105 const Sized_relobj
<32, big_endian
>* object
,
2106 const Symbol_value
<32>* psymval
)
2108 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2109 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2110 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2111 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2112 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2113 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2114 Reltype x
= psymval
->value(object
, addend
) >> 16;
2115 val
= This::insert_val_thumb_movw_movt(val
, x
);
2116 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2117 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2118 return This::STATUS_OKAY
;
2121 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2122 static inline typename
This::Status
2123 movw_prel_nc(unsigned char *view
,
2124 const Sized_relobj
<32, big_endian
>* object
,
2125 const Symbol_value
<32>* psymval
,
2126 Arm_address address
,
2127 Arm_address thumb_bit
)
2129 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2130 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2131 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2132 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2133 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2134 val
= This::insert_val_arm_movw_movt(val
, x
);
2135 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2136 return This::STATUS_OKAY
;
2139 // R_ARM_MOVT_PREL: S + A - P
2140 static inline typename
This::Status
2141 movt_prel(unsigned char *view
,
2142 const Sized_relobj
<32, big_endian
>* object
,
2143 const Symbol_value
<32>* psymval
,
2144 Arm_address address
)
2146 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2147 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2148 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2149 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2150 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2151 val
= This::insert_val_arm_movw_movt(val
, x
);
2152 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2153 return This::STATUS_OKAY
;
2156 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2157 static inline typename
This::Status
2158 thm_movw_prel_nc(unsigned char *view
,
2159 const Sized_relobj
<32, big_endian
>* object
,
2160 const Symbol_value
<32>* psymval
,
2161 Arm_address address
,
2162 Arm_address thumb_bit
)
2164 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2165 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2166 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2167 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2168 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2169 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2170 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2171 val
= This::insert_val_thumb_movw_movt(val
, x
);
2172 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2173 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2174 return This::STATUS_OKAY
;
2177 // R_ARM_THM_MOVT_PREL: S + A - P
2178 static inline typename
This::Status
2179 thm_movt_prel(unsigned char *view
,
2180 const Sized_relobj
<32, big_endian
>* object
,
2181 const Symbol_value
<32>* psymval
,
2182 Arm_address address
)
2184 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2185 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2186 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2187 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2188 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2189 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2190 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2191 val
= This::insert_val_thumb_movw_movt(val
, x
);
2192 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2193 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2194 return This::STATUS_OKAY
;
2198 // Relocate ARM long branches. This handles relocation types
2199 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2200 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2201 // undefined and we do not use PLT in this relocation. In such a case,
2202 // the branch is converted into an NOP.
2204 template<bool big_endian
>
2205 typename Arm_relocate_functions
<big_endian
>::Status
2206 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2207 unsigned int r_type
,
2208 const Relocate_info
<32, big_endian
>* relinfo
,
2209 unsigned char *view
,
2210 const Sized_symbol
<32>* gsym
,
2211 const Arm_relobj
<big_endian
>* object
,
2213 const Symbol_value
<32>* psymval
,
2214 Arm_address address
,
2215 Arm_address thumb_bit
,
2216 bool is_weakly_undefined_without_plt
)
2218 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2219 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2220 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2222 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2223 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2224 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2225 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2226 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2227 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2228 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2230 // Check that the instruction is valid.
2231 if (r_type
== elfcpp::R_ARM_CALL
)
2233 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2234 return This::STATUS_BAD_RELOC
;
2236 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2238 if (!insn_is_b
&& !insn_is_cond_bl
)
2239 return This::STATUS_BAD_RELOC
;
2241 else if (r_type
== elfcpp::R_ARM_PLT32
)
2243 if (!insn_is_any_branch
)
2244 return This::STATUS_BAD_RELOC
;
2246 else if (r_type
== elfcpp::R_ARM_XPC25
)
2248 // FIXME: AAELF document IH0044C does not say much about it other
2249 // than it being obsolete.
2250 if (!insn_is_any_branch
)
2251 return This::STATUS_BAD_RELOC
;
2256 // A branch to an undefined weak symbol is turned into a jump to
2257 // the next instruction unless a PLT entry will be created.
2258 // Do the same for local undefined symbols.
2259 // The jump to the next instruction is optimized as a NOP depending
2260 // on the architecture.
2261 const Target_arm
<big_endian
>* arm_target
=
2262 Target_arm
<big_endian
>::default_target();
2263 if (is_weakly_undefined_without_plt
)
2265 Valtype cond
= val
& 0xf0000000U
;
2266 if (arm_target
->may_use_arm_nop())
2267 val
= cond
| 0x0320f000;
2269 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2270 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2271 return This::STATUS_OKAY
;
2274 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2275 Valtype branch_target
= psymval
->value(object
, addend
);
2276 int32_t branch_offset
= branch_target
- address
;
2278 // We need a stub if the branch offset is too large or if we need
2280 bool may_use_blx
= arm_target
->may_use_blx();
2281 Reloc_stub
* stub
= NULL
;
2282 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2283 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2284 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2286 Stub_type stub_type
=
2287 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2289 if (stub_type
!= arm_stub_none
)
2291 Stub_table
<big_endian
>* stub_table
=
2292 object
->stub_table(relinfo
->data_shndx
);
2293 gold_assert(stub_table
!= NULL
);
2295 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2296 stub
= stub_table
->find_reloc_stub(stub_key
);
2297 gold_assert(stub
!= NULL
);
2298 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2299 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2300 branch_offset
= branch_target
- address
;
2301 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2302 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2306 // At this point, if we still need to switch mode, the instruction
2307 // must either be a BLX or a BL that can be converted to a BLX.
2311 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2312 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2315 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2316 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2317 return (utils::has_overflow
<26>(branch_offset
)
2318 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2321 // Relocate THUMB long branches. This handles relocation types
2322 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2323 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2324 // undefined and we do not use PLT in this relocation. In such a case,
2325 // the branch is converted into an NOP.
2327 template<bool big_endian
>
2328 typename Arm_relocate_functions
<big_endian
>::Status
2329 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2330 unsigned int r_type
,
2331 const Relocate_info
<32, big_endian
>* relinfo
,
2332 unsigned char *view
,
2333 const Sized_symbol
<32>* gsym
,
2334 const Arm_relobj
<big_endian
>* object
,
2336 const Symbol_value
<32>* psymval
,
2337 Arm_address address
,
2338 Arm_address thumb_bit
,
2339 bool is_weakly_undefined_without_plt
)
2341 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2342 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2343 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2344 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2346 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2348 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2349 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2351 // Check that the instruction is valid.
2352 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2354 if (!is_bl_insn
&& !is_blx_insn
)
2355 return This::STATUS_BAD_RELOC
;
2357 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2359 // This cannot be a BLX.
2361 return This::STATUS_BAD_RELOC
;
2363 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2365 // Check for Thumb to Thumb call.
2367 return This::STATUS_BAD_RELOC
;
2370 gold_warning(_("%s: Thumb BLX instruction targets "
2371 "thumb function '%s'."),
2372 object
->name().c_str(),
2373 (gsym
? gsym
->name() : "(local)"));
2374 // Convert BLX to BL.
2375 lower_insn
|= 0x1000U
;
2381 // A branch to an undefined weak symbol is turned into a jump to
2382 // the next instruction unless a PLT entry will be created.
2383 // The jump to the next instruction is optimized as a NOP.W for
2384 // Thumb-2 enabled architectures.
2385 const Target_arm
<big_endian
>* arm_target
=
2386 Target_arm
<big_endian
>::default_target();
2387 if (is_weakly_undefined_without_plt
)
2389 if (arm_target
->may_use_thumb2_nop())
2391 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2392 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2396 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2397 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2399 return This::STATUS_OKAY
;
2402 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2403 // with Thumb-1) involving the J1 and J2 bits.
2404 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
2405 uint32_t upper
= upper_insn
& 0x3ff;
2406 uint32_t lower
= lower_insn
& 0x7ff;
2407 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
2408 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
2409 uint32_t i1
= j1
^ s
? 0 : 1;
2410 uint32_t i2
= j2
^ s
? 0 : 1;
2412 int32_t addend
= (i1
<< 23) | (i2
<< 22) | (upper
<< 12) | (lower
<< 1);
2414 addend
= (addend
| ((s
? 0 : 1) << 24)) - (1 << 24);
2416 Arm_address branch_target
= psymval
->value(object
, addend
);
2417 int32_t branch_offset
= branch_target
- address
;
2419 // We need a stub if the branch offset is too large or if we need
2421 bool may_use_blx
= arm_target
->may_use_blx();
2422 bool thumb2
= arm_target
->using_thumb2();
2424 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2425 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2427 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2428 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2429 || ((thumb_bit
== 0)
2430 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2431 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2433 Stub_type stub_type
=
2434 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2436 if (stub_type
!= arm_stub_none
)
2438 Stub_table
<big_endian
>* stub_table
=
2439 object
->stub_table(relinfo
->data_shndx
);
2440 gold_assert(stub_table
!= NULL
);
2442 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2443 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2444 gold_assert(stub
!= NULL
);
2445 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2446 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2447 branch_offset
= branch_target
- address
;
2451 // At this point, if we still need to switch mode, the instruction
2452 // must either be a BLX or a BL that can be converted to a BLX.
2455 gold_assert(may_use_blx
2456 && (r_type
== elfcpp::R_ARM_THM_CALL
2457 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2458 // Make sure this is a BLX.
2459 lower_insn
&= ~0x1000U
;
2463 // Make sure this is a BL.
2464 lower_insn
|= 0x1000U
;
2467 uint32_t reloc_sign
= (branch_offset
< 0) ? 1 : 0;
2468 uint32_t relocation
= static_cast<uint32_t>(branch_offset
);
2470 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2471 // For a BLX instruction, make sure that the relocation is rounded up
2472 // to a word boundary. This follows the semantics of the instruction
2473 // which specifies that bit 1 of the target address will come from bit
2474 // 1 of the base address.
2475 relocation
= (relocation
+ 2U) & ~3U;
2477 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2478 // We use the Thumb-2 encoding, which is safe even if dealing with
2479 // a Thumb-1 instruction by virtue of our overflow check above. */
2480 upper_insn
= (upper_insn
& ~0x7ffU
)
2481 | ((relocation
>> 12) & 0x3ffU
)
2482 | (reloc_sign
<< 10);
2483 lower_insn
= (lower_insn
& ~0x2fffU
)
2484 | (((!((relocation
>> 23) & 1U)) ^ reloc_sign
) << 13)
2485 | (((!((relocation
>> 22) & 1U)) ^ reloc_sign
) << 11)
2486 | ((relocation
>> 1) & 0x7ffU
);
2488 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2489 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2492 ? utils::has_overflow
<25>(relocation
)
2493 : utils::has_overflow
<23>(relocation
))
2494 ? This::STATUS_OVERFLOW
2495 : This::STATUS_OKAY
);
2498 // Get the GOT section, creating it if necessary.
2500 template<bool big_endian
>
2501 Output_data_got
<32, big_endian
>*
2502 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2504 if (this->got_
== NULL
)
2506 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2508 this->got_
= new Output_data_got
<32, big_endian
>();
2511 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2513 | elfcpp::SHF_WRITE
),
2517 // The old GNU linker creates a .got.plt section. We just
2518 // create another set of data in the .got section. Note that we
2519 // always create a PLT if we create a GOT, although the PLT
2521 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2522 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2524 | elfcpp::SHF_WRITE
),
2525 this->got_plt_
, false);
2528 // The first three entries are reserved.
2529 this->got_plt_
->set_current_data_size(3 * 4);
2531 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2532 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2534 0, 0, elfcpp::STT_OBJECT
,
2536 elfcpp::STV_HIDDEN
, 0,
2542 // Get the dynamic reloc section, creating it if necessary.
2544 template<bool big_endian
>
2545 typename Target_arm
<big_endian
>::Reloc_section
*
2546 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2548 if (this->rel_dyn_
== NULL
)
2550 gold_assert(layout
!= NULL
);
2551 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2552 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2553 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true);
2555 return this->rel_dyn_
;
2558 // Insn_template methods.
2560 // Return byte size of an instruction template.
2563 Insn_template::size() const
2565 switch (this->type())
2578 // Return alignment of an instruction template.
2581 Insn_template::alignment() const
2583 switch (this->type())
2596 // Stub_template methods.
2598 Stub_template::Stub_template(
2599 Stub_type type
, const Insn_template
* insns
,
2601 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2602 entry_in_thumb_mode_(false), relocs_()
2606 // Compute byte size and alignment of stub template.
2607 for (size_t i
= 0; i
< insn_count
; i
++)
2609 unsigned insn_alignment
= insns
[i
].alignment();
2610 size_t insn_size
= insns
[i
].size();
2611 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2612 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2613 switch (insns
[i
].type())
2615 case Insn_template::THUMB16_TYPE
:
2617 this->entry_in_thumb_mode_
= true;
2620 case Insn_template::THUMB32_TYPE
:
2621 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2622 this->relocs_
.push_back(Reloc(i
, offset
));
2624 this->entry_in_thumb_mode_
= true;
2627 case Insn_template::ARM_TYPE
:
2628 // Handle cases where the target is encoded within the
2630 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2631 this->relocs_
.push_back(Reloc(i
, offset
));
2634 case Insn_template::DATA_TYPE
:
2635 // Entry point cannot be data.
2636 gold_assert(i
!= 0);
2637 this->relocs_
.push_back(Reloc(i
, offset
));
2643 offset
+= insn_size
;
2645 this->size_
= offset
;
2648 // Reloc_stub::Key methods.
2650 // Dump a Key as a string for debugging.
2653 Reloc_stub::Key::name() const
2655 if (this->r_sym_
== invalid_index
)
2657 // Global symbol key name
2658 // <stub-type>:<symbol name>:<addend>.
2659 const std::string sym_name
= this->u_
.symbol
->name();
2660 // We need to print two hex number and two colons. So just add 100 bytes
2661 // to the symbol name size.
2662 size_t len
= sym_name
.size() + 100;
2663 char* buffer
= new char[len
];
2664 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2665 sym_name
.c_str(), this->addend_
);
2666 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2668 return std::string(buffer
);
2672 // local symbol key name
2673 // <stub-type>:<object>:<r_sym>:<addend>.
2674 const size_t len
= 200;
2676 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2677 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2678 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2679 return std::string(buffer
);
2683 // Reloc_stub methods.
2685 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2686 // LOCATION to DESTINATION.
2687 // This code is based on the arm_type_of_stub function in
2688 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2692 Reloc_stub::stub_type_for_reloc(
2693 unsigned int r_type
,
2694 Arm_address location
,
2695 Arm_address destination
,
2696 bool target_is_thumb
)
2698 Stub_type stub_type
= arm_stub_none
;
2700 // This is a bit ugly but we want to avoid using a templated class for
2701 // big and little endianities.
2703 bool should_force_pic_veneer
;
2706 if (parameters
->target().is_big_endian())
2708 const Target_arm
<true>* big_endian_target
=
2709 Target_arm
<true>::default_target();
2710 may_use_blx
= big_endian_target
->may_use_blx();
2711 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
2712 thumb2
= big_endian_target
->using_thumb2();
2713 thumb_only
= big_endian_target
->using_thumb_only();
2717 const Target_arm
<false>* little_endian_target
=
2718 Target_arm
<false>::default_target();
2719 may_use_blx
= little_endian_target
->may_use_blx();
2720 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
2721 thumb2
= little_endian_target
->using_thumb2();
2722 thumb_only
= little_endian_target
->using_thumb_only();
2725 int64_t branch_offset
= (int64_t)destination
- location
;
2727 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2729 // Handle cases where:
2730 // - this call goes too far (different Thumb/Thumb2 max
2732 // - it's a Thumb->Arm call and blx is not available, or it's a
2733 // Thumb->Arm branch (not bl). A stub is needed in this case.
2735 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2736 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2738 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2739 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2740 || ((!target_is_thumb
)
2741 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2742 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2744 if (target_is_thumb
)
2749 stub_type
= (parameters
->options().shared()
2750 || should_force_pic_veneer
)
2753 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2754 // V5T and above. Stub starts with ARM code, so
2755 // we must be able to switch mode before
2756 // reaching it, which is only possible for 'bl'
2757 // (ie R_ARM_THM_CALL relocation).
2758 ? arm_stub_long_branch_any_thumb_pic
2759 // On V4T, use Thumb code only.
2760 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2764 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2765 ? arm_stub_long_branch_any_any
// V5T and above.
2766 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2770 stub_type
= (parameters
->options().shared()
2771 || should_force_pic_veneer
)
2772 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2773 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2780 // FIXME: We should check that the input section is from an
2781 // object that has interwork enabled.
2783 stub_type
= (parameters
->options().shared()
2784 || should_force_pic_veneer
)
2787 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2788 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2789 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2793 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2794 ? arm_stub_long_branch_any_any
// V5T and above.
2795 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2797 // Handle v4t short branches.
2798 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2799 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2800 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2801 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2805 else if (r_type
== elfcpp::R_ARM_CALL
2806 || r_type
== elfcpp::R_ARM_JUMP24
2807 || r_type
== elfcpp::R_ARM_PLT32
)
2809 if (target_is_thumb
)
2813 // FIXME: We should check that the input section is from an
2814 // object that has interwork enabled.
2816 // We have an extra 2-bytes reach because of
2817 // the mode change (bit 24 (H) of BLX encoding).
2818 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2819 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2820 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2821 || (r_type
== elfcpp::R_ARM_JUMP24
)
2822 || (r_type
== elfcpp::R_ARM_PLT32
))
2824 stub_type
= (parameters
->options().shared()
2825 || should_force_pic_veneer
)
2828 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2829 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2833 ? arm_stub_long_branch_any_any
// V5T and above.
2834 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2840 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2841 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2843 stub_type
= (parameters
->options().shared()
2844 || should_force_pic_veneer
)
2845 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2846 : arm_stub_long_branch_any_any
; /// non-PIC.
2854 // Template to implement do_write for a specific target endianity.
2856 template<bool big_endian
>
2858 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2859 section_size_type view_size
)
2861 const Stub_template
* stub_template
= this->stub_template();
2862 const Insn_template
* insns
= stub_template
->insns();
2864 // FIXME: We do not handle BE8 encoding yet.
2865 unsigned char* pov
= view
;
2866 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2868 switch (insns
[i
].type())
2870 case Insn_template::THUMB16_TYPE
:
2871 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2872 gold_assert(insns
[i
].reloc_addend() == 0);
2873 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2875 case Insn_template::THUMB32_TYPE
:
2877 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2878 uint32_t lo
= insns
[i
].data() & 0xffff;
2879 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2880 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2883 case Insn_template::ARM_TYPE
:
2884 case Insn_template::DATA_TYPE
:
2885 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2890 pov
+= insns
[i
].size();
2892 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2895 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2898 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2902 this->do_fixed_endian_write
<true>(view
, view_size
);
2904 this->do_fixed_endian_write
<false>(view
, view_size
);
2907 // Stub_factory methods.
2909 Stub_factory::Stub_factory()
2911 // The instruction template sequences are declared as static
2912 // objects and initialized first time the constructor runs.
2914 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2915 // to reach the stub if necessary.
2916 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2918 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2919 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2920 // dcd R_ARM_ABS32(X)
2923 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2925 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
2927 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2928 Insn_template::arm_insn(0xe12fff1c), // bx ip
2929 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2930 // dcd R_ARM_ABS32(X)
2933 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2934 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
2936 Insn_template::thumb16_insn(0xb401), // push {r0}
2937 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2938 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2939 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2940 Insn_template::thumb16_insn(0x4760), // bx ip
2941 Insn_template::thumb16_insn(0xbf00), // nop
2942 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2943 // dcd R_ARM_ABS32(X)
2946 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2948 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
2950 Insn_template::thumb16_insn(0x4778), // bx pc
2951 Insn_template::thumb16_insn(0x46c0), // nop
2952 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2953 Insn_template::arm_insn(0xe12fff1c), // bx ip
2954 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2955 // dcd R_ARM_ABS32(X)
2958 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2960 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
2962 Insn_template::thumb16_insn(0x4778), // bx pc
2963 Insn_template::thumb16_insn(0x46c0), // nop
2964 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2965 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2966 // dcd R_ARM_ABS32(X)
2969 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2970 // one, when the destination is close enough.
2971 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
2973 Insn_template::thumb16_insn(0x4778), // bx pc
2974 Insn_template::thumb16_insn(0x46c0), // nop
2975 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2978 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2979 // blx to reach the stub if necessary.
2980 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
2982 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2983 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2984 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2985 // dcd R_ARM_REL32(X-4)
2988 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2989 // blx to reach the stub if necessary. We can not add into pc;
2990 // it is not guaranteed to mode switch (different in ARMv6 and
2992 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
2994 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2995 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2996 Insn_template::arm_insn(0xe12fff1c), // bx ip
2997 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2998 // dcd R_ARM_REL32(X)
3001 // V4T ARM -> ARM long branch stub, PIC.
3002 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3004 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3005 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3006 Insn_template::arm_insn(0xe12fff1c), // bx ip
3007 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3008 // dcd R_ARM_REL32(X)
3011 // V4T Thumb -> ARM long branch stub, PIC.
3012 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3014 Insn_template::thumb16_insn(0x4778), // bx pc
3015 Insn_template::thumb16_insn(0x46c0), // nop
3016 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3017 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3018 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3019 // dcd R_ARM_REL32(X)
3022 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3024 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3026 Insn_template::thumb16_insn(0xb401), // push {r0}
3027 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3028 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3029 Insn_template::thumb16_insn(0x4484), // add ip, r0
3030 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3031 Insn_template::thumb16_insn(0x4760), // bx ip
3032 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3033 // dcd R_ARM_REL32(X)
3036 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3038 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3040 Insn_template::thumb16_insn(0x4778), // bx pc
3041 Insn_template::thumb16_insn(0x46c0), // nop
3042 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3043 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3044 Insn_template::arm_insn(0xe12fff1c), // bx ip
3045 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3046 // dcd R_ARM_REL32(X)
3049 // Cortex-A8 erratum-workaround stubs.
3051 // Stub used for conditional branches (which may be beyond +/-1MB away,
3052 // so we can't use a conditional branch to reach this stub).
3059 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3061 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3062 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3063 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3067 // Stub used for b.w and bl.w instructions.
3069 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3071 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3074 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3076 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3079 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3080 // instruction (which switches to ARM mode) to point to this stub. Jump to
3081 // the real destination using an ARM-mode branch.
3082 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3084 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3087 // Fill in the stub template look-up table. Stub templates are constructed
3088 // per instance of Stub_factory for fast look-up without locking
3089 // in a thread-enabled environment.
3091 this->stub_templates_
[arm_stub_none
] =
3092 new Stub_template(arm_stub_none
, NULL
, 0);
3094 #define DEF_STUB(x) \
3098 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3099 Stub_type type = arm_stub_##x; \
3100 this->stub_templates_[type] = \
3101 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3109 // Stub_table methods.
3111 // Add a STUB with using KEY. Caller is reponsible for avoid adding
3112 // if already a STUB with the same key has been added.
3114 template<bool big_endian
>
3116 Stub_table
<big_endian
>::add_reloc_stub(
3118 const Reloc_stub::Key
& key
)
3120 const Stub_template
* stub_template
= stub
->stub_template();
3121 gold_assert(stub_template
->type() == key
.stub_type());
3122 this->reloc_stubs_
[key
] = stub
;
3123 if (this->addralign_
< stub_template
->alignment())
3124 this->addralign_
= stub_template
->alignment();
3125 this->has_been_changed_
= true;
3128 template<bool big_endian
>
3130 Stub_table
<big_endian
>::relocate_stubs(
3131 const Relocate_info
<32, big_endian
>* relinfo
,
3132 Target_arm
<big_endian
>* arm_target
,
3133 Output_section
* output_section
,
3134 unsigned char* view
,
3135 Arm_address address
,
3136 section_size_type view_size
)
3138 // If we are passed a view bigger than the stub table's. we need to
3140 gold_assert(address
== this->address()
3142 == static_cast<section_size_type
>(this->data_size())));
3144 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3145 p
!= this->reloc_stubs_
.end();
3148 Reloc_stub
* stub
= p
->second
;
3149 const Stub_template
* stub_template
= stub
->stub_template();
3150 if (stub_template
->reloc_count() != 0)
3152 // Adjust view to cover the stub only.
3153 section_size_type offset
= stub
->offset();
3154 section_size_type stub_size
= stub_template
->size();
3155 gold_assert(offset
+ stub_size
<= view_size
);
3157 arm_target
->relocate_stub(stub
, relinfo
, output_section
,
3158 view
+ offset
, address
+ offset
,
3164 // Reset address and file offset.
3166 template<bool big_endian
>
3168 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
3171 uint64_t max_addralign
= 1;
3172 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3173 p
!= this->reloc_stubs_
.end();
3176 Reloc_stub
* stub
= p
->second
;
3177 const Stub_template
* stub_template
= stub
->stub_template();
3178 uint64_t stub_addralign
= stub_template
->alignment();
3179 max_addralign
= std::max(max_addralign
, stub_addralign
);
3180 off
= align_address(off
, stub_addralign
);
3181 stub
->set_offset(off
);
3182 stub
->reset_destination_address();
3183 off
+= stub_template
->size();
3186 this->addralign_
= max_addralign
;
3187 this->set_current_data_size_for_child(off
);
3190 // Write out the stubs to file.
3192 template<bool big_endian
>
3194 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3196 off_t offset
= this->offset();
3197 const section_size_type oview_size
=
3198 convert_to_section_size_type(this->data_size());
3199 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3201 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3202 p
!= this->reloc_stubs_
.end();
3205 Reloc_stub
* stub
= p
->second
;
3206 Arm_address address
= this->address() + stub
->offset();
3208 == align_address(address
,
3209 stub
->stub_template()->alignment()));
3210 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3213 of
->write_output_view(this->offset(), oview_size
, oview
);
3216 // Arm_input_section methods.
3218 // Initialize an Arm_input_section.
3220 template<bool big_endian
>
3222 Arm_input_section
<big_endian
>::init()
3224 Relobj
* relobj
= this->relobj();
3225 unsigned int shndx
= this->shndx();
3227 // Cache these to speed up size and alignment queries. It is too slow
3228 // to call section_addraglin and section_size every time.
3229 this->original_addralign_
= relobj
->section_addralign(shndx
);
3230 this->original_size_
= relobj
->section_size(shndx
);
3232 // We want to make this look like the original input section after
3233 // output sections are finalized.
3234 Output_section
* os
= relobj
->output_section(shndx
);
3235 off_t offset
= relobj
->output_section_offset(shndx
);
3236 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3237 this->set_address(os
->address() + offset
);
3238 this->set_file_offset(os
->offset() + offset
);
3240 this->set_current_data_size(this->original_size_
);
3241 this->finalize_data_size();
3244 template<bool big_endian
>
3246 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3248 // We have to write out the original section content.
3249 section_size_type section_size
;
3250 const unsigned char* section_contents
=
3251 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3252 of
->write(this->offset(), section_contents
, section_size
);
3254 // If this owns a stub table and it is not empty, write it.
3255 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3256 this->stub_table_
->write(of
);
3259 // Finalize data size.
3261 template<bool big_endian
>
3263 Arm_input_section
<big_endian
>::set_final_data_size()
3265 // If this owns a stub table, finalize its data size as well.
3266 if (this->is_stub_table_owner())
3268 uint64_t address
= this->address();
3270 // The stub table comes after the original section contents.
3271 address
+= this->original_size_
;
3272 address
= align_address(address
, this->stub_table_
->addralign());
3273 off_t offset
= this->offset() + (address
- this->address());
3274 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3275 address
+= this->stub_table_
->data_size();
3276 gold_assert(address
== this->address() + this->current_data_size());
3279 this->set_data_size(this->current_data_size());
3282 // Reset address and file offset.
3284 template<bool big_endian
>
3286 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3288 // Size of the original input section contents.
3289 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3291 // If this is a stub table owner, account for the stub table size.
3292 if (this->is_stub_table_owner())
3294 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3296 // Reset the stub table's address and file offset. The
3297 // current data size for child will be updated after that.
3298 stub_table_
->reset_address_and_file_offset();
3299 off
= align_address(off
, stub_table_
->addralign());
3300 off
+= stub_table
->current_data_size();
3303 this->set_current_data_size(off
);
3306 // Arm_output_section methods.
3308 // Create a stub group for input sections from BEGIN to END. OWNER
3309 // points to the input section to be the owner a new stub table.
3311 template<bool big_endian
>
3313 Arm_output_section
<big_endian
>::create_stub_group(
3314 Input_section_list::const_iterator begin
,
3315 Input_section_list::const_iterator end
,
3316 Input_section_list::const_iterator owner
,
3317 Target_arm
<big_endian
>* target
,
3318 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3320 // Currently we convert ordinary input sections into relaxed sections only
3321 // at this point but we may want to support creating relaxed input section
3322 // very early. So we check here to see if owner is already a relaxed
3325 Arm_input_section
<big_endian
>* arm_input_section
;
3326 if (owner
->is_relaxed_input_section())
3329 Arm_input_section
<big_endian
>::as_arm_input_section(
3330 owner
->relaxed_input_section());
3334 gold_assert(owner
->is_input_section());
3335 // Create a new relaxed input section.
3337 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3338 new_relaxed_sections
->push_back(arm_input_section
);
3341 // Create a stub table.
3342 Stub_table
<big_endian
>* stub_table
=
3343 target
->new_stub_table(arm_input_section
);
3345 arm_input_section
->set_stub_table(stub_table
);
3347 Input_section_list::const_iterator p
= begin
;
3348 Input_section_list::const_iterator prev_p
;
3350 // Look for input sections or relaxed input sections in [begin ... end].
3353 if (p
->is_input_section() || p
->is_relaxed_input_section())
3355 // The stub table information for input sections live
3356 // in their objects.
3357 Arm_relobj
<big_endian
>* arm_relobj
=
3358 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3359 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3363 while (prev_p
!= end
);
3366 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3367 // of stub groups. We grow a stub group by adding input section until the
3368 // size is just below GROUP_SIZE. The last input section will be converted
3369 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3370 // input section after the stub table, effectively double the group size.
3372 // This is similar to the group_sections() function in elf32-arm.c but is
3373 // implemented differently.
3375 template<bool big_endian
>
3377 Arm_output_section
<big_endian
>::group_sections(
3378 section_size_type group_size
,
3379 bool stubs_always_after_branch
,
3380 Target_arm
<big_endian
>* target
)
3382 // We only care about sections containing code.
3383 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3386 // States for grouping.
3389 // No group is being built.
3391 // A group is being built but the stub table is not found yet.
3392 // We keep group a stub group until the size is just under GROUP_SIZE.
3393 // The last input section in the group will be used as the stub table.
3394 FINDING_STUB_SECTION
,
3395 // A group is being built and we have already found a stub table.
3396 // We enter this state to grow a stub group by adding input section
3397 // after the stub table. This effectively doubles the group size.
3401 // Any newly created relaxed sections are stored here.
3402 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3404 State state
= NO_GROUP
;
3405 section_size_type off
= 0;
3406 section_size_type group_begin_offset
= 0;
3407 section_size_type group_end_offset
= 0;
3408 section_size_type stub_table_end_offset
= 0;
3409 Input_section_list::const_iterator group_begin
=
3410 this->input_sections().end();
3411 Input_section_list::const_iterator stub_table
=
3412 this->input_sections().end();
3413 Input_section_list::const_iterator group_end
= this->input_sections().end();
3414 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3415 p
!= this->input_sections().end();
3418 section_size_type section_begin_offset
=
3419 align_address(off
, p
->addralign());
3420 section_size_type section_end_offset
=
3421 section_begin_offset
+ p
->data_size();
3423 // Check to see if we should group the previously seens sections.
3429 case FINDING_STUB_SECTION
:
3430 // Adding this section makes the group larger than GROUP_SIZE.
3431 if (section_end_offset
- group_begin_offset
>= group_size
)
3433 if (stubs_always_after_branch
)
3435 gold_assert(group_end
!= this->input_sections().end());
3436 this->create_stub_group(group_begin
, group_end
, group_end
,
3437 target
, &new_relaxed_sections
);
3442 // But wait, there's more! Input sections up to
3443 // stub_group_size bytes after the stub table can be
3444 // handled by it too.
3445 state
= HAS_STUB_SECTION
;
3446 stub_table
= group_end
;
3447 stub_table_end_offset
= group_end_offset
;
3452 case HAS_STUB_SECTION
:
3453 // Adding this section makes the post stub-section group larger
3455 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3457 gold_assert(group_end
!= this->input_sections().end());
3458 this->create_stub_group(group_begin
, group_end
, stub_table
,
3459 target
, &new_relaxed_sections
);
3468 // If we see an input section and currently there is no group, start
3469 // a new one. Skip any empty sections.
3470 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3471 && (p
->relobj()->section_size(p
->shndx()) != 0))
3473 if (state
== NO_GROUP
)
3475 state
= FINDING_STUB_SECTION
;
3477 group_begin_offset
= section_begin_offset
;
3480 // Keep track of the last input section seen.
3482 group_end_offset
= section_end_offset
;
3485 off
= section_end_offset
;
3488 // Create a stub group for any ungrouped sections.
3489 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3491 gold_assert(group_end
!= this->input_sections().end());
3492 this->create_stub_group(group_begin
, group_end
,
3493 (state
== FINDING_STUB_SECTION
3496 target
, &new_relaxed_sections
);
3499 // Convert input section into relaxed input section in a batch.
3500 if (!new_relaxed_sections
.empty())
3501 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3503 // Update the section offsets
3504 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3506 Arm_relobj
<big_endian
>* arm_relobj
=
3507 Arm_relobj
<big_endian
>::as_arm_relobj(
3508 new_relaxed_sections
[i
]->relobj());
3509 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3510 // Tell Arm_relobj that this input section is converted.
3511 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3515 // Arm_relobj methods.
3517 // Scan relocations for stub generation.
3519 template<bool big_endian
>
3521 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3522 Target_arm
<big_endian
>* arm_target
,
3523 const Symbol_table
* symtab
,
3524 const Layout
* layout
)
3526 unsigned int shnum
= this->shnum();
3527 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3529 // Read the section headers.
3530 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3534 // To speed up processing, we set up hash tables for fast lookup of
3535 // input offsets to output addresses.
3536 this->initialize_input_to_output_maps();
3538 const Relobj::Output_sections
& out_sections(this->output_sections());
3540 Relocate_info
<32, big_endian
> relinfo
;
3541 relinfo
.symtab
= symtab
;
3542 relinfo
.layout
= layout
;
3543 relinfo
.object
= this;
3545 const unsigned char* p
= pshdrs
+ shdr_size
;
3546 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3548 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3550 unsigned int sh_type
= shdr
.get_sh_type();
3551 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3554 off_t sh_size
= shdr
.get_sh_size();
3558 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3559 if (index
>= this->shnum())
3561 // Ignore reloc section with bad info. This error will be
3562 // reported in the final link.
3566 Output_section
* os
= out_sections
[index
];
3569 // This relocation section is against a section which we
3573 Arm_address output_offset
= this->get_output_section_offset(index
);
3575 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
3577 // Ignore reloc section with unexpected symbol table. The
3578 // error will be reported in the final link.
3582 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
3583 sh_size
, true, false);
3585 unsigned int reloc_size
;
3586 if (sh_type
== elfcpp::SHT_REL
)
3587 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3589 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
3591 if (reloc_size
!= shdr
.get_sh_entsize())
3593 // Ignore reloc section with unexpected entsize. The error
3594 // will be reported in the final link.
3598 size_t reloc_count
= sh_size
/ reloc_size
;
3599 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
3601 // Ignore reloc section with uneven size. The error will be
3602 // reported in the final link.
3606 gold_assert(output_offset
!= invalid_address
3607 || this->relocs_must_follow_section_writes());
3609 // Get the section contents. This does work for the case in which
3610 // we modify the contents of an input section. We need to pass the
3611 // output view under such circumstances.
3612 section_size_type input_view_size
= 0;
3613 const unsigned char* input_view
=
3614 this->section_contents(index
, &input_view_size
, false);
3616 relinfo
.reloc_shndx
= i
;
3617 relinfo
.data_shndx
= index
;
3618 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
3620 output_offset
== invalid_address
,
3626 // After we've done the relocations, we release the hash tables,
3627 // since we no longer need them.
3628 this->free_input_to_output_maps();
3631 // Count the local symbols. The ARM backend needs to know if a symbol
3632 // is a THUMB function or not. For global symbols, it is easy because
3633 // the Symbol object keeps the ELF symbol type. For local symbol it is
3634 // harder because we cannot access this information. So we override the
3635 // do_count_local_symbol in parent and scan local symbols to mark
3636 // THUMB functions. This is not the most efficient way but I do not want to
3637 // slow down other ports by calling a per symbol targer hook inside
3638 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3640 template<bool big_endian
>
3642 Arm_relobj
<big_endian
>::do_count_local_symbols(
3643 Stringpool_template
<char>* pool
,
3644 Stringpool_template
<char>* dynpool
)
3646 // We need to fix-up the values of any local symbols whose type are
3649 // Ask parent to count the local symbols.
3650 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
3651 const unsigned int loccount
= this->local_symbol_count();
3655 // Intialize the thumb function bit-vector.
3656 std::vector
<bool> empty_vector(loccount
, false);
3657 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
3659 // Read the symbol table section header.
3660 const unsigned int symtab_shndx
= this->symtab_shndx();
3661 elfcpp::Shdr
<32, big_endian
>
3662 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
3663 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
3665 // Read the local symbols.
3666 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
3667 gold_assert(loccount
== symtabshdr
.get_sh_info());
3668 off_t locsize
= loccount
* sym_size
;
3669 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
3670 locsize
, true, true);
3672 // Loop over the local symbols and mark any local symbols pointing
3673 // to THUMB functions.
3675 // Skip the first dummy symbol.
3677 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
3678 this->local_values();
3679 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
3681 elfcpp::Sym
<32, big_endian
> sym(psyms
);
3682 elfcpp::STT st_type
= sym
.get_st_type();
3683 Symbol_value
<32>& lv((*plocal_values
)[i
]);
3684 Arm_address input_value
= lv
.input_value();
3686 if (st_type
== elfcpp::STT_ARM_TFUNC
3687 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
3689 // This is a THUMB function. Mark this and canonicalize the
3690 // symbol value by setting LSB.
3691 this->local_symbol_is_thumb_function_
[i
] = true;
3692 if ((input_value
& 1) == 0)
3693 lv
.set_input_value(input_value
| 1);
3698 // Relocate sections.
3699 template<bool big_endian
>
3701 Arm_relobj
<big_endian
>::do_relocate_sections(
3702 const Symbol_table
* symtab
,
3703 const Layout
* layout
,
3704 const unsigned char* pshdrs
,
3705 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
3707 // Call parent to relocate sections.
3708 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
3711 // We do not generate stubs if doing a relocatable link.
3712 if (parameters
->options().relocatable())
3715 // Relocate stub tables.
3716 unsigned int shnum
= this->shnum();
3718 Target_arm
<big_endian
>* arm_target
=
3719 Target_arm
<big_endian
>::default_target();
3721 Relocate_info
<32, big_endian
> relinfo
;
3722 relinfo
.symtab
= symtab
;
3723 relinfo
.layout
= layout
;
3724 relinfo
.object
= this;
3726 for (unsigned int i
= 1; i
< shnum
; ++i
)
3728 Arm_input_section
<big_endian
>* arm_input_section
=
3729 arm_target
->find_arm_input_section(this, i
);
3731 if (arm_input_section
== NULL
3732 || !arm_input_section
->is_stub_table_owner()
3733 || arm_input_section
->stub_table()->empty())
3736 // We cannot discard a section if it owns a stub table.
3737 Output_section
* os
= this->output_section(i
);
3738 gold_assert(os
!= NULL
);
3740 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
3741 relinfo
.reloc_shdr
= NULL
;
3742 relinfo
.data_shndx
= i
;
3743 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
3745 gold_assert((*pviews
)[i
].view
!= NULL
);
3747 // We are passed the output section view. Adjust it to cover the
3749 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
3750 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
3751 && ((stub_table
->address() + stub_table
->data_size())
3752 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
3754 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
3755 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
3756 Arm_address address
= stub_table
->address();
3757 section_size_type view_size
= stub_table
->data_size();
3759 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
3764 // Read the symbol information.
3766 template<bool big_endian
>
3768 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3770 // Call parent class to read symbol information.
3771 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
3773 // Read processor-specific flags in ELF file header.
3774 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3775 elfcpp::Elf_sizes
<32>::ehdr_size
,
3777 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3778 this->processor_specific_flags_
= ehdr
.get_e_flags();
3781 // Arm_dynobj methods.
3783 // Read the symbol information.
3785 template<bool big_endian
>
3787 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3789 // Call parent class to read symbol information.
3790 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
3792 // Read processor-specific flags in ELF file header.
3793 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3794 elfcpp::Elf_sizes
<32>::ehdr_size
,
3796 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3797 this->processor_specific_flags_
= ehdr
.get_e_flags();
3800 // Stub_addend_reader methods.
3802 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3804 template<bool big_endian
>
3805 elfcpp::Elf_types
<32>::Elf_Swxword
3806 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
3807 unsigned int r_type
,
3808 const unsigned char* view
,
3809 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
3813 case elfcpp::R_ARM_CALL
:
3814 case elfcpp::R_ARM_JUMP24
:
3815 case elfcpp::R_ARM_PLT32
:
3817 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3818 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3819 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3820 return utils::sign_extend
<26>(val
<< 2);
3823 case elfcpp::R_ARM_THM_CALL
:
3824 case elfcpp::R_ARM_THM_JUMP24
:
3825 case elfcpp::R_ARM_THM_XPC22
:
3827 // Fetch the addend. We use the Thumb-2 encoding (backwards
3828 // compatible with Thumb-1) involving the J1 and J2 bits.
3829 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3830 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3831 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3832 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3834 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
3835 uint32_t upper
= upper_insn
& 0x3ff;
3836 uint32_t lower
= lower_insn
& 0x7ff;
3837 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
3838 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
3839 uint32_t i1
= j1
^ s
? 0 : 1;
3840 uint32_t i2
= j2
^ s
? 0 : 1;
3842 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3843 | (upper
<< 12) | (lower
<< 1));
3846 case elfcpp::R_ARM_THM_JUMP19
:
3848 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3849 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3850 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3851 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3853 // Reconstruct the top three bits and squish the two 11 bit pieces
3855 uint32_t S
= (upper_insn
& 0x0400) >> 10;
3856 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
3857 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
3859 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
3860 uint32_t lower
= (lower_insn
& 0x07ff);
3861 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
3869 // A class to handle the PLT data.
3871 template<bool big_endian
>
3872 class Output_data_plt_arm
: public Output_section_data
3875 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
3878 Output_data_plt_arm(Layout
*, Output_data_space
*);
3880 // Add an entry to the PLT.
3882 add_entry(Symbol
* gsym
);
3884 // Return the .rel.plt section data.
3885 const Reloc_section
*
3887 { return this->rel_
; }
3891 do_adjust_output_section(Output_section
* os
);
3893 // Write to a map file.
3895 do_print_to_mapfile(Mapfile
* mapfile
) const
3896 { mapfile
->print_output_data(this, _("** PLT")); }
3899 // Template for the first PLT entry.
3900 static const uint32_t first_plt_entry
[5];
3902 // Template for subsequent PLT entries.
3903 static const uint32_t plt_entry
[3];
3905 // Set the final size.
3907 set_final_data_size()
3909 this->set_data_size(sizeof(first_plt_entry
)
3910 + this->count_
* sizeof(plt_entry
));
3913 // Write out the PLT data.
3915 do_write(Output_file
*);
3917 // The reloc section.
3918 Reloc_section
* rel_
;
3919 // The .got.plt section.
3920 Output_data_space
* got_plt_
;
3921 // The number of PLT entries.
3922 unsigned int count_
;
3925 // Create the PLT section. The ordinary .got section is an argument,
3926 // since we need to refer to the start. We also create our own .got
3927 // section just for PLT entries.
3929 template<bool big_endian
>
3930 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
3931 Output_data_space
* got_plt
)
3932 : Output_section_data(4), got_plt_(got_plt
), count_(0)
3934 this->rel_
= new Reloc_section(false);
3935 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
3936 elfcpp::SHF_ALLOC
, this->rel_
, true);
3939 template<bool big_endian
>
3941 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
3946 // Add an entry to the PLT.
3948 template<bool big_endian
>
3950 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
3952 gold_assert(!gsym
->has_plt_offset());
3954 // Note that when setting the PLT offset we skip the initial
3955 // reserved PLT entry.
3956 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
3957 + sizeof(first_plt_entry
));
3961 section_offset_type got_offset
= this->got_plt_
->current_data_size();
3963 // Every PLT entry needs a GOT entry which points back to the PLT
3964 // entry (this will be changed by the dynamic linker, normally
3965 // lazily when the function is called).
3966 this->got_plt_
->set_current_data_size(got_offset
+ 4);
3968 // Every PLT entry needs a reloc.
3969 gsym
->set_needs_dynsym_entry();
3970 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
3973 // Note that we don't need to save the symbol. The contents of the
3974 // PLT are independent of which symbols are used. The symbols only
3975 // appear in the relocations.
3979 // FIXME: This is not very flexible. Right now this has only been tested
3980 // on armv5te. If we are to support additional architecture features like
3981 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3983 // The first entry in the PLT.
3984 template<bool big_endian
>
3985 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
3987 0xe52de004, // str lr, [sp, #-4]!
3988 0xe59fe004, // ldr lr, [pc, #4]
3989 0xe08fe00e, // add lr, pc, lr
3990 0xe5bef008, // ldr pc, [lr, #8]!
3991 0x00000000, // &GOT[0] - .
3994 // Subsequent entries in the PLT.
3996 template<bool big_endian
>
3997 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
3999 0xe28fc600, // add ip, pc, #0xNN00000
4000 0xe28cca00, // add ip, ip, #0xNN000
4001 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4004 // Write out the PLT. This uses the hand-coded instructions above,
4005 // and adjusts them as needed. This is all specified by the arm ELF
4006 // Processor Supplement.
4008 template<bool big_endian
>
4010 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4012 const off_t offset
= this->offset();
4013 const section_size_type oview_size
=
4014 convert_to_section_size_type(this->data_size());
4015 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4017 const off_t got_file_offset
= this->got_plt_
->offset();
4018 const section_size_type got_size
=
4019 convert_to_section_size_type(this->got_plt_
->data_size());
4020 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4022 unsigned char* pov
= oview
;
4024 Arm_address plt_address
= this->address();
4025 Arm_address got_address
= this->got_plt_
->address();
4027 // Write first PLT entry. All but the last word are constants.
4028 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4029 / sizeof(plt_entry
[0]));
4030 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4031 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4032 // Last word in first PLT entry is &GOT[0] - .
4033 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4034 got_address
- (plt_address
+ 16));
4035 pov
+= sizeof(first_plt_entry
);
4037 unsigned char* got_pov
= got_view
;
4039 memset(got_pov
, 0, 12);
4042 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4043 unsigned int plt_offset
= sizeof(first_plt_entry
);
4044 unsigned int plt_rel_offset
= 0;
4045 unsigned int got_offset
= 12;
4046 const unsigned int count
= this->count_
;
4047 for (unsigned int i
= 0;
4050 pov
+= sizeof(plt_entry
),
4052 plt_offset
+= sizeof(plt_entry
),
4053 plt_rel_offset
+= rel_size
,
4056 // Set and adjust the PLT entry itself.
4057 int32_t offset
= ((got_address
+ got_offset
)
4058 - (plt_address
+ plt_offset
+ 8));
4060 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4061 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4062 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4063 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4064 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4065 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4066 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4068 // Set the entry in the GOT.
4069 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4072 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4073 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4075 of
->write_output_view(offset
, oview_size
, oview
);
4076 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4079 // Create a PLT entry for a global symbol.
4081 template<bool big_endian
>
4083 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
4086 if (gsym
->has_plt_offset())
4089 if (this->plt_
== NULL
)
4091 // Create the GOT sections first.
4092 this->got_section(symtab
, layout
);
4094 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
4095 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4097 | elfcpp::SHF_EXECINSTR
),
4100 this->plt_
->add_entry(gsym
);
4103 // Report an unsupported relocation against a local symbol.
4105 template<bool big_endian
>
4107 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4108 Sized_relobj
<32, big_endian
>* object
,
4109 unsigned int r_type
)
4111 gold_error(_("%s: unsupported reloc %u against local symbol"),
4112 object
->name().c_str(), r_type
);
4115 // We are about to emit a dynamic relocation of type R_TYPE. If the
4116 // dynamic linker does not support it, issue an error. The GNU linker
4117 // only issues a non-PIC error for an allocated read-only section.
4118 // Here we know the section is allocated, but we don't know that it is
4119 // read-only. But we check for all the relocation types which the
4120 // glibc dynamic linker supports, so it seems appropriate to issue an
4121 // error even if the section is not read-only.
4123 template<bool big_endian
>
4125 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4126 unsigned int r_type
)
4130 // These are the relocation types supported by glibc for ARM.
4131 case elfcpp::R_ARM_RELATIVE
:
4132 case elfcpp::R_ARM_COPY
:
4133 case elfcpp::R_ARM_GLOB_DAT
:
4134 case elfcpp::R_ARM_JUMP_SLOT
:
4135 case elfcpp::R_ARM_ABS32
:
4136 case elfcpp::R_ARM_ABS32_NOI
:
4137 case elfcpp::R_ARM_PC24
:
4138 // FIXME: The following 3 types are not supported by Android's dynamic
4140 case elfcpp::R_ARM_TLS_DTPMOD32
:
4141 case elfcpp::R_ARM_TLS_DTPOFF32
:
4142 case elfcpp::R_ARM_TLS_TPOFF32
:
4146 // This prevents us from issuing more than one error per reloc
4147 // section. But we can still wind up issuing more than one
4148 // error per object file.
4149 if (this->issued_non_pic_error_
)
4151 object
->error(_("requires unsupported dynamic reloc; "
4152 "recompile with -fPIC"));
4153 this->issued_non_pic_error_
= true;
4156 case elfcpp::R_ARM_NONE
:
4161 // Scan a relocation for a local symbol.
4162 // FIXME: This only handles a subset of relocation types used by Android
4163 // on ARM v5te devices.
4165 template<bool big_endian
>
4167 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4170 Sized_relobj
<32, big_endian
>* object
,
4171 unsigned int data_shndx
,
4172 Output_section
* output_section
,
4173 const elfcpp::Rel
<32, big_endian
>& reloc
,
4174 unsigned int r_type
,
4175 const elfcpp::Sym
<32, big_endian
>&)
4177 r_type
= get_real_reloc_type(r_type
);
4180 case elfcpp::R_ARM_NONE
:
4183 case elfcpp::R_ARM_ABS32
:
4184 case elfcpp::R_ARM_ABS32_NOI
:
4185 // If building a shared library (or a position-independent
4186 // executable), we need to create a dynamic relocation for
4187 // this location. The relocation applied at link time will
4188 // apply the link-time value, so we flag the location with
4189 // an R_ARM_RELATIVE relocation so the dynamic loader can
4190 // relocate it easily.
4191 if (parameters
->options().output_is_position_independent())
4193 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4194 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4195 // If we are to add more other reloc types than R_ARM_ABS32,
4196 // we need to add check_non_pic(object, r_type) here.
4197 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4198 output_section
, data_shndx
,
4199 reloc
.get_r_offset());
4203 case elfcpp::R_ARM_REL32
:
4204 case elfcpp::R_ARM_THM_CALL
:
4205 case elfcpp::R_ARM_CALL
:
4206 case elfcpp::R_ARM_PREL31
:
4207 case elfcpp::R_ARM_JUMP24
:
4208 case elfcpp::R_ARM_PLT32
:
4209 case elfcpp::R_ARM_THM_ABS5
:
4210 case elfcpp::R_ARM_ABS8
:
4211 case elfcpp::R_ARM_ABS12
:
4212 case elfcpp::R_ARM_ABS16
:
4213 case elfcpp::R_ARM_BASE_ABS
:
4214 case elfcpp::R_ARM_MOVW_ABS_NC
:
4215 case elfcpp::R_ARM_MOVT_ABS
:
4216 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4217 case elfcpp::R_ARM_THM_MOVT_ABS
:
4218 case elfcpp::R_ARM_MOVW_PREL_NC
:
4219 case elfcpp::R_ARM_MOVT_PREL
:
4220 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4221 case elfcpp::R_ARM_THM_MOVT_PREL
:
4224 case elfcpp::R_ARM_GOTOFF32
:
4225 // We need a GOT section:
4226 target
->got_section(symtab
, layout
);
4229 case elfcpp::R_ARM_BASE_PREL
:
4230 // FIXME: What about this?
4233 case elfcpp::R_ARM_GOT_BREL
:
4234 case elfcpp::R_ARM_GOT_PREL
:
4236 // The symbol requires a GOT entry.
4237 Output_data_got
<32, big_endian
>* got
=
4238 target
->got_section(symtab
, layout
);
4239 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4240 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4242 // If we are generating a shared object, we need to add a
4243 // dynamic RELATIVE relocation for this symbol's GOT entry.
4244 if (parameters
->options().output_is_position_independent())
4246 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4247 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4248 rel_dyn
->add_local_relative(
4249 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4250 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4256 case elfcpp::R_ARM_TARGET1
:
4257 // This should have been mapped to another type already.
4259 case elfcpp::R_ARM_COPY
:
4260 case elfcpp::R_ARM_GLOB_DAT
:
4261 case elfcpp::R_ARM_JUMP_SLOT
:
4262 case elfcpp::R_ARM_RELATIVE
:
4263 // These are relocations which should only be seen by the
4264 // dynamic linker, and should never be seen here.
4265 gold_error(_("%s: unexpected reloc %u in object file"),
4266 object
->name().c_str(), r_type
);
4270 unsupported_reloc_local(object
, r_type
);
4275 // Report an unsupported relocation against a global symbol.
4277 template<bool big_endian
>
4279 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4280 Sized_relobj
<32, big_endian
>* object
,
4281 unsigned int r_type
,
4284 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4285 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4288 // Scan a relocation for a global symbol.
4289 // FIXME: This only handles a subset of relocation types used by Android
4290 // on ARM v5te devices.
4292 template<bool big_endian
>
4294 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4297 Sized_relobj
<32, big_endian
>* object
,
4298 unsigned int data_shndx
,
4299 Output_section
* output_section
,
4300 const elfcpp::Rel
<32, big_endian
>& reloc
,
4301 unsigned int r_type
,
4304 r_type
= get_real_reloc_type(r_type
);
4307 case elfcpp::R_ARM_NONE
:
4310 case elfcpp::R_ARM_ABS32
:
4311 case elfcpp::R_ARM_ABS32_NOI
:
4313 // Make a dynamic relocation if necessary.
4314 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4316 if (target
->may_need_copy_reloc(gsym
))
4318 target
->copy_reloc(symtab
, layout
, object
,
4319 data_shndx
, output_section
, gsym
, reloc
);
4321 else if (gsym
->can_use_relative_reloc(false))
4323 // If we are to add more other reloc types than R_ARM_ABS32,
4324 // we need to add check_non_pic(object, r_type) here.
4325 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4326 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4327 output_section
, object
,
4328 data_shndx
, reloc
.get_r_offset());
4332 // If we are to add more other reloc types than R_ARM_ABS32,
4333 // we need to add check_non_pic(object, r_type) here.
4334 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4335 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4336 data_shndx
, reloc
.get_r_offset());
4342 case elfcpp::R_ARM_MOVW_ABS_NC
:
4343 case elfcpp::R_ARM_MOVT_ABS
:
4344 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4345 case elfcpp::R_ARM_THM_MOVT_ABS
:
4346 case elfcpp::R_ARM_MOVW_PREL_NC
:
4347 case elfcpp::R_ARM_MOVT_PREL
:
4348 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4349 case elfcpp::R_ARM_THM_MOVT_PREL
:
4352 case elfcpp::R_ARM_THM_ABS5
:
4353 case elfcpp::R_ARM_ABS8
:
4354 case elfcpp::R_ARM_ABS12
:
4355 case elfcpp::R_ARM_ABS16
:
4356 case elfcpp::R_ARM_BASE_ABS
:
4358 // No dynamic relocs of this kinds.
4359 // Report the error in case of PIC.
4360 int flags
= Symbol::NON_PIC_REF
;
4361 if (gsym
->type() == elfcpp::STT_FUNC
4362 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4363 flags
|= Symbol::FUNCTION_CALL
;
4364 if (gsym
->needs_dynamic_reloc(flags
))
4365 check_non_pic(object
, r_type
);
4369 case elfcpp::R_ARM_REL32
:
4370 case elfcpp::R_ARM_PREL31
:
4372 // Make a dynamic relocation if necessary.
4373 int flags
= Symbol::NON_PIC_REF
;
4374 if (gsym
->needs_dynamic_reloc(flags
))
4376 if (target
->may_need_copy_reloc(gsym
))
4378 target
->copy_reloc(symtab
, layout
, object
,
4379 data_shndx
, output_section
, gsym
, reloc
);
4383 check_non_pic(object
, r_type
);
4384 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4385 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4386 data_shndx
, reloc
.get_r_offset());
4392 case elfcpp::R_ARM_JUMP24
:
4393 case elfcpp::R_ARM_THM_JUMP24
:
4394 case elfcpp::R_ARM_CALL
:
4395 case elfcpp::R_ARM_THM_CALL
:
4397 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4398 target
->make_plt_entry(symtab
, layout
, gsym
);
4401 // Check to see if this is a function that would need a PLT
4402 // but does not get one because the function symbol is untyped.
4403 // This happens in assembly code missing a proper .type directive.
4404 if ((!gsym
->is_undefined() || parameters
->options().shared())
4405 && !parameters
->doing_static_link()
4406 && gsym
->type() == elfcpp::STT_NOTYPE
4407 && (gsym
->is_from_dynobj()
4408 || gsym
->is_undefined()
4409 || gsym
->is_preemptible()))
4410 gold_error(_("%s is not a function."),
4411 gsym
->demangled_name().c_str());
4415 case elfcpp::R_ARM_PLT32
:
4416 // If the symbol is fully resolved, this is just a relative
4417 // local reloc. Otherwise we need a PLT entry.
4418 if (gsym
->final_value_is_known())
4420 // If building a shared library, we can also skip the PLT entry
4421 // if the symbol is defined in the output file and is protected
4423 if (gsym
->is_defined()
4424 && !gsym
->is_from_dynobj()
4425 && !gsym
->is_preemptible())
4427 target
->make_plt_entry(symtab
, layout
, gsym
);
4430 case elfcpp::R_ARM_GOTOFF32
:
4431 // We need a GOT section.
4432 target
->got_section(symtab
, layout
);
4435 case elfcpp::R_ARM_BASE_PREL
:
4436 // FIXME: What about this?
4439 case elfcpp::R_ARM_GOT_BREL
:
4440 case elfcpp::R_ARM_GOT_PREL
:
4442 // The symbol requires a GOT entry.
4443 Output_data_got
<32, big_endian
>* got
=
4444 target
->got_section(symtab
, layout
);
4445 if (gsym
->final_value_is_known())
4446 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4449 // If this symbol is not fully resolved, we need to add a
4450 // GOT entry with a dynamic relocation.
4451 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4452 if (gsym
->is_from_dynobj()
4453 || gsym
->is_undefined()
4454 || gsym
->is_preemptible())
4455 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4456 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4459 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4460 rel_dyn
->add_global_relative(
4461 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4462 gsym
->got_offset(GOT_TYPE_STANDARD
));
4468 case elfcpp::R_ARM_TARGET1
:
4469 // This should have been mapped to another type already.
4471 case elfcpp::R_ARM_COPY
:
4472 case elfcpp::R_ARM_GLOB_DAT
:
4473 case elfcpp::R_ARM_JUMP_SLOT
:
4474 case elfcpp::R_ARM_RELATIVE
:
4475 // These are relocations which should only be seen by the
4476 // dynamic linker, and should never be seen here.
4477 gold_error(_("%s: unexpected reloc %u in object file"),
4478 object
->name().c_str(), r_type
);
4482 unsupported_reloc_global(object
, r_type
, gsym
);
4487 // Process relocations for gc.
4489 template<bool big_endian
>
4491 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4493 Sized_relobj
<32, big_endian
>* object
,
4494 unsigned int data_shndx
,
4496 const unsigned char* prelocs
,
4498 Output_section
* output_section
,
4499 bool needs_special_offset_handling
,
4500 size_t local_symbol_count
,
4501 const unsigned char* plocal_symbols
)
4503 typedef Target_arm
<big_endian
> Arm
;
4504 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4506 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
4515 needs_special_offset_handling
,
4520 // Scan relocations for a section.
4522 template<bool big_endian
>
4524 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4526 Sized_relobj
<32, big_endian
>* object
,
4527 unsigned int data_shndx
,
4528 unsigned int sh_type
,
4529 const unsigned char* prelocs
,
4531 Output_section
* output_section
,
4532 bool needs_special_offset_handling
,
4533 size_t local_symbol_count
,
4534 const unsigned char* plocal_symbols
)
4536 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4537 if (sh_type
== elfcpp::SHT_RELA
)
4539 gold_error(_("%s: unsupported RELA reloc section"),
4540 object
->name().c_str());
4544 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
4553 needs_special_offset_handling
,
4558 // Finalize the sections.
4560 template<bool big_endian
>
4562 Target_arm
<big_endian
>::do_finalize_sections(
4564 const Input_objects
* input_objects
,
4565 Symbol_table
* symtab
)
4567 // Merge processor-specific flags.
4568 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
4569 p
!= input_objects
->relobj_end();
4572 Arm_relobj
<big_endian
>* arm_relobj
=
4573 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
4574 this->merge_processor_specific_flags(
4576 arm_relobj
->processor_specific_flags());
4579 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
4580 p
!= input_objects
->dynobj_end();
4583 Arm_dynobj
<big_endian
>* arm_dynobj
=
4584 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
4585 this->merge_processor_specific_flags(
4587 arm_dynobj
->processor_specific_flags());
4590 // Fill in some more dynamic tags.
4591 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4594 if (this->got_plt_
!= NULL
4595 && this->got_plt_
->output_section() != NULL
)
4596 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
4598 if (this->plt_
!= NULL
4599 && this->plt_
->output_section() != NULL
)
4601 const Output_data
* od
= this->plt_
->rel_plt();
4602 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
4603 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
4604 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
4607 if (this->rel_dyn_
!= NULL
4608 && this->rel_dyn_
->output_section() != NULL
)
4610 const Output_data
* od
= this->rel_dyn_
;
4611 odyn
->add_section_address(elfcpp::DT_REL
, od
);
4612 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
4613 odyn
->add_constant(elfcpp::DT_RELENT
,
4614 elfcpp::Elf_sizes
<32>::rel_size
);
4617 if (!parameters
->options().shared())
4619 // The value of the DT_DEBUG tag is filled in by the dynamic
4620 // linker at run time, and used by the debugger.
4621 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
4625 // Emit any relocs we saved in an attempt to avoid generating COPY
4627 if (this->copy_relocs_
.any_saved_relocs())
4628 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
4630 // Handle the .ARM.exidx section.
4631 Output_section
* exidx_section
= layout
->find_output_section(".ARM.exidx");
4632 if (exidx_section
!= NULL
4633 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
4634 && !parameters
->options().relocatable())
4636 // Create __exidx_start and __exdix_end symbols.
4637 symtab
->define_in_output_data("__exidx_start", NULL
, exidx_section
,
4638 0, 0, elfcpp::STT_OBJECT
,
4639 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
4641 symtab
->define_in_output_data("__exidx_end", NULL
, exidx_section
,
4642 0, 0, elfcpp::STT_OBJECT
,
4643 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
4646 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4647 // the .ARM.exidx section.
4648 if (!layout
->script_options()->saw_phdrs_clause())
4650 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
4652 Output_segment
* exidx_segment
=
4653 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
4654 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
4660 // Return whether a direct absolute static relocation needs to be applied.
4661 // In cases where Scan::local() or Scan::global() has created
4662 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4663 // of the relocation is carried in the data, and we must not
4664 // apply the static relocation.
4666 template<bool big_endian
>
4668 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
4669 const Sized_symbol
<32>* gsym
,
4672 Output_section
* output_section
)
4674 // If the output section is not allocated, then we didn't call
4675 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4677 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
4680 // For local symbols, we will have created a non-RELATIVE dynamic
4681 // relocation only if (a) the output is position independent,
4682 // (b) the relocation is absolute (not pc- or segment-relative), and
4683 // (c) the relocation is not 32 bits wide.
4685 return !(parameters
->options().output_is_position_independent()
4686 && (ref_flags
& Symbol::ABSOLUTE_REF
)
4689 // For global symbols, we use the same helper routines used in the
4690 // scan pass. If we did not create a dynamic relocation, or if we
4691 // created a RELATIVE dynamic relocation, we should apply the static
4693 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
4694 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
4695 && gsym
->can_use_relative_reloc(ref_flags
4696 & Symbol::FUNCTION_CALL
);
4697 return !has_dyn
|| is_rel
;
4700 // Perform a relocation.
4702 template<bool big_endian
>
4704 Target_arm
<big_endian
>::Relocate::relocate(
4705 const Relocate_info
<32, big_endian
>* relinfo
,
4707 Output_section
*output_section
,
4709 const elfcpp::Rel
<32, big_endian
>& rel
,
4710 unsigned int r_type
,
4711 const Sized_symbol
<32>* gsym
,
4712 const Symbol_value
<32>* psymval
,
4713 unsigned char* view
,
4714 Arm_address address
,
4715 section_size_type
/* view_size */ )
4717 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
4719 r_type
= get_real_reloc_type(r_type
);
4721 const Arm_relobj
<big_endian
>* object
=
4722 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
4724 // If the final branch target of a relocation is THUMB instruction, this
4725 // is 1. Otherwise it is 0.
4726 Arm_address thumb_bit
= 0;
4727 Symbol_value
<32> symval
;
4728 bool is_weakly_undefined_without_plt
= false;
4729 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
4733 // This is a global symbol. Determine if we use PLT and if the
4734 // final target is THUMB.
4735 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
4737 // This uses a PLT, change the symbol value.
4738 symval
.set_output_value(target
->plt_section()->address()
4739 + gsym
->plt_offset());
4742 else if (gsym
->is_weak_undefined())
4744 // This is a weakly undefined symbol and we do not use PLT
4745 // for this relocation. A branch targeting this symbol will
4746 // be converted into an NOP.
4747 is_weakly_undefined_without_plt
= true;
4751 // Set thumb bit if symbol:
4752 // -Has type STT_ARM_TFUNC or
4753 // -Has type STT_FUNC, is defined and with LSB in value set.
4755 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4756 || (gsym
->type() == elfcpp::STT_FUNC
4757 && !gsym
->is_undefined()
4758 && ((psymval
->value(object
, 0) & 1) != 0)))
4765 // This is a local symbol. Determine if the final target is THUMB.
4766 // We saved this information when all the local symbols were read.
4767 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
4768 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
4769 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
4774 // This is a fake relocation synthesized for a stub. It does not have
4775 // a real symbol. We just look at the LSB of the symbol value to
4776 // determine if the target is THUMB or not.
4777 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
4780 // Strip LSB if this points to a THUMB target.
4782 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
4783 && ((psymval
->value(object
, 0) & 1) != 0))
4785 Arm_address stripped_value
=
4786 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
4787 symval
.set_output_value(stripped_value
);
4791 // Get the GOT offset if needed.
4792 // The GOT pointer points to the end of the GOT section.
4793 // We need to subtract the size of the GOT section to get
4794 // the actual offset to use in the relocation.
4795 bool have_got_offset
= false;
4796 unsigned int got_offset
= 0;
4799 case elfcpp::R_ARM_GOT_BREL
:
4800 case elfcpp::R_ARM_GOT_PREL
:
4803 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4804 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4805 - target
->got_size());
4809 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4810 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4811 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4812 - target
->got_size());
4814 have_got_offset
= true;
4821 // To look up relocation stubs, we need to pass the symbol table index of
4823 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4825 typename
Arm_relocate_functions::Status reloc_status
=
4826 Arm_relocate_functions::STATUS_OKAY
;
4829 case elfcpp::R_ARM_NONE
:
4832 case elfcpp::R_ARM_ABS8
:
4833 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4835 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
4838 case elfcpp::R_ARM_ABS12
:
4839 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4841 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
4844 case elfcpp::R_ARM_ABS16
:
4845 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4847 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
4850 case elfcpp::R_ARM_ABS32
:
4851 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4853 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4857 case elfcpp::R_ARM_ABS32_NOI
:
4858 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4860 // No thumb bit for this relocation: (S + A)
4861 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4865 case elfcpp::R_ARM_MOVW_ABS_NC
:
4866 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4868 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
4872 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4873 "a shared object; recompile with -fPIC"));
4876 case elfcpp::R_ARM_MOVT_ABS
:
4877 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4879 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
4881 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4882 "a shared object; recompile with -fPIC"));
4885 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4886 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4888 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
4892 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4893 "making a shared object; recompile with -fPIC"));
4896 case elfcpp::R_ARM_THM_MOVT_ABS
:
4897 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4899 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
4902 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4903 "making a shared object; recompile with -fPIC"));
4906 case elfcpp::R_ARM_MOVW_PREL_NC
:
4907 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
4912 case elfcpp::R_ARM_MOVT_PREL
:
4913 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
4917 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4918 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
4923 case elfcpp::R_ARM_THM_MOVT_PREL
:
4924 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
4928 case elfcpp::R_ARM_REL32
:
4929 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4930 address
, thumb_bit
);
4933 case elfcpp::R_ARM_THM_ABS5
:
4934 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4936 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
4939 case elfcpp::R_ARM_THM_CALL
:
4941 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
4942 psymval
, address
, thumb_bit
,
4943 is_weakly_undefined_without_plt
);
4946 case elfcpp::R_ARM_XPC25
:
4948 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
4949 psymval
, address
, thumb_bit
,
4950 is_weakly_undefined_without_plt
);
4953 case elfcpp::R_ARM_THM_XPC22
:
4955 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
4956 psymval
, address
, thumb_bit
,
4957 is_weakly_undefined_without_plt
);
4960 case elfcpp::R_ARM_GOTOFF32
:
4962 Arm_address got_origin
;
4963 got_origin
= target
->got_plt_section()->address();
4964 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4965 got_origin
, thumb_bit
);
4969 case elfcpp::R_ARM_BASE_PREL
:
4972 // Get the addressing origin of the output segment defining the
4973 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4974 gold_assert(gsym
!= NULL
);
4975 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4976 origin
= gsym
->output_segment()->vaddr();
4977 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4978 origin
= gsym
->output_data()->address();
4981 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4982 _("cannot find origin of R_ARM_BASE_PREL"));
4985 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
4989 case elfcpp::R_ARM_BASE_ABS
:
4991 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4996 // Get the addressing origin of the output segment defining
4997 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4999 // R_ARM_BASE_ABS with the NULL symbol will give the
5000 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5001 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5002 origin
= target
->got_plt_section()->address();
5003 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5004 origin
= gsym
->output_segment()->vaddr();
5005 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5006 origin
= gsym
->output_data()->address();
5009 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5010 _("cannot find origin of R_ARM_BASE_ABS"));
5014 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5018 case elfcpp::R_ARM_GOT_BREL
:
5019 gold_assert(have_got_offset
);
5020 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5023 case elfcpp::R_ARM_GOT_PREL
:
5024 gold_assert(have_got_offset
);
5025 // Get the address origin for GOT PLT, which is allocated right
5026 // after the GOT section, to calculate an absolute address of
5027 // the symbol GOT entry (got_origin + got_offset).
5028 Arm_address got_origin
;
5029 got_origin
= target
->got_plt_section()->address();
5030 reloc_status
= Arm_relocate_functions::got_prel(view
,
5031 got_origin
+ got_offset
,
5035 case elfcpp::R_ARM_PLT32
:
5036 gold_assert(gsym
== NULL
5037 || gsym
->has_plt_offset()
5038 || gsym
->final_value_is_known()
5039 || (gsym
->is_defined()
5040 && !gsym
->is_from_dynobj()
5041 && !gsym
->is_preemptible()));
5043 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5044 psymval
, address
, thumb_bit
,
5045 is_weakly_undefined_without_plt
);
5048 case elfcpp::R_ARM_CALL
:
5050 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5051 psymval
, address
, thumb_bit
,
5052 is_weakly_undefined_without_plt
);
5055 case elfcpp::R_ARM_JUMP24
:
5057 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5058 psymval
, address
, thumb_bit
,
5059 is_weakly_undefined_without_plt
);
5062 case elfcpp::R_ARM_THM_JUMP24
:
5064 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5065 psymval
, address
, thumb_bit
,
5066 is_weakly_undefined_without_plt
);
5069 case elfcpp::R_ARM_PREL31
:
5070 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5071 address
, thumb_bit
);
5074 case elfcpp::R_ARM_TARGET1
:
5075 // This should have been mapped to another type already.
5077 case elfcpp::R_ARM_COPY
:
5078 case elfcpp::R_ARM_GLOB_DAT
:
5079 case elfcpp::R_ARM_JUMP_SLOT
:
5080 case elfcpp::R_ARM_RELATIVE
:
5081 // These are relocations which should only be seen by the
5082 // dynamic linker, and should never be seen here.
5083 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5084 _("unexpected reloc %u in object file"),
5089 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5090 _("unsupported reloc %u"),
5095 // Report any errors.
5096 switch (reloc_status
)
5098 case Arm_relocate_functions::STATUS_OKAY
:
5100 case Arm_relocate_functions::STATUS_OVERFLOW
:
5101 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5102 _("relocation overflow in relocation %u"),
5105 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5106 gold_error_at_location(
5110 _("unexpected opcode while processing relocation %u"),
5120 // Relocate section data.
5122 template<bool big_endian
>
5124 Target_arm
<big_endian
>::relocate_section(
5125 const Relocate_info
<32, big_endian
>* relinfo
,
5126 unsigned int sh_type
,
5127 const unsigned char* prelocs
,
5129 Output_section
* output_section
,
5130 bool needs_special_offset_handling
,
5131 unsigned char* view
,
5132 Arm_address address
,
5133 section_size_type view_size
,
5134 const Reloc_symbol_changes
* reloc_symbol_changes
)
5136 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5137 gold_assert(sh_type
== elfcpp::SHT_REL
);
5139 Arm_input_section
<big_endian
>* arm_input_section
=
5140 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5142 // This is an ARM input section and the view covers the whole output
5144 if (arm_input_section
!= NULL
)
5146 gold_assert(needs_special_offset_handling
);
5147 Arm_address section_address
= arm_input_section
->address();
5148 section_size_type section_size
= arm_input_section
->data_size();
5150 gold_assert((arm_input_section
->address() >= address
)
5151 && ((arm_input_section
->address()
5152 + arm_input_section
->data_size())
5153 <= (address
+ view_size
)));
5155 off_t offset
= section_address
- address
;
5158 view_size
= section_size
;
5161 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5168 needs_special_offset_handling
,
5172 reloc_symbol_changes
);
5175 // Return the size of a relocation while scanning during a relocatable
5178 template<bool big_endian
>
5180 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5181 unsigned int r_type
,
5184 r_type
= get_real_reloc_type(r_type
);
5187 case elfcpp::R_ARM_NONE
:
5190 case elfcpp::R_ARM_ABS8
:
5193 case elfcpp::R_ARM_ABS16
:
5194 case elfcpp::R_ARM_THM_ABS5
:
5197 case elfcpp::R_ARM_ABS32
:
5198 case elfcpp::R_ARM_ABS32_NOI
:
5199 case elfcpp::R_ARM_ABS12
:
5200 case elfcpp::R_ARM_BASE_ABS
:
5201 case elfcpp::R_ARM_REL32
:
5202 case elfcpp::R_ARM_THM_CALL
:
5203 case elfcpp::R_ARM_GOTOFF32
:
5204 case elfcpp::R_ARM_BASE_PREL
:
5205 case elfcpp::R_ARM_GOT_BREL
:
5206 case elfcpp::R_ARM_GOT_PREL
:
5207 case elfcpp::R_ARM_PLT32
:
5208 case elfcpp::R_ARM_CALL
:
5209 case elfcpp::R_ARM_JUMP24
:
5210 case elfcpp::R_ARM_PREL31
:
5211 case elfcpp::R_ARM_MOVW_ABS_NC
:
5212 case elfcpp::R_ARM_MOVT_ABS
:
5213 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5214 case elfcpp::R_ARM_THM_MOVT_ABS
:
5215 case elfcpp::R_ARM_MOVW_PREL_NC
:
5216 case elfcpp::R_ARM_MOVT_PREL
:
5217 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5218 case elfcpp::R_ARM_THM_MOVT_PREL
:
5221 case elfcpp::R_ARM_TARGET1
:
5222 // This should have been mapped to another type already.
5224 case elfcpp::R_ARM_COPY
:
5225 case elfcpp::R_ARM_GLOB_DAT
:
5226 case elfcpp::R_ARM_JUMP_SLOT
:
5227 case elfcpp::R_ARM_RELATIVE
:
5228 // These are relocations which should only be seen by the
5229 // dynamic linker, and should never be seen here.
5230 gold_error(_("%s: unexpected reloc %u in object file"),
5231 object
->name().c_str(), r_type
);
5235 object
->error(_("unsupported reloc %u in object file"), r_type
);
5240 // Scan the relocs during a relocatable link.
5242 template<bool big_endian
>
5244 Target_arm
<big_endian
>::scan_relocatable_relocs(
5245 Symbol_table
* symtab
,
5247 Sized_relobj
<32, big_endian
>* object
,
5248 unsigned int data_shndx
,
5249 unsigned int sh_type
,
5250 const unsigned char* prelocs
,
5252 Output_section
* output_section
,
5253 bool needs_special_offset_handling
,
5254 size_t local_symbol_count
,
5255 const unsigned char* plocal_symbols
,
5256 Relocatable_relocs
* rr
)
5258 gold_assert(sh_type
== elfcpp::SHT_REL
);
5260 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5261 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5263 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5264 Scan_relocatable_relocs
>(
5272 needs_special_offset_handling
,
5278 // Relocate a section during a relocatable link.
5280 template<bool big_endian
>
5282 Target_arm
<big_endian
>::relocate_for_relocatable(
5283 const Relocate_info
<32, big_endian
>* relinfo
,
5284 unsigned int sh_type
,
5285 const unsigned char* prelocs
,
5287 Output_section
* output_section
,
5288 off_t offset_in_output_section
,
5289 const Relocatable_relocs
* rr
,
5290 unsigned char* view
,
5291 Arm_address view_address
,
5292 section_size_type view_size
,
5293 unsigned char* reloc_view
,
5294 section_size_type reloc_view_size
)
5296 gold_assert(sh_type
== elfcpp::SHT_REL
);
5298 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5303 offset_in_output_section
,
5312 // Return the value to use for a dynamic symbol which requires special
5313 // treatment. This is how we support equality comparisons of function
5314 // pointers across shared library boundaries, as described in the
5315 // processor specific ABI supplement.
5317 template<bool big_endian
>
5319 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5321 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5322 return this->plt_section()->address() + gsym
->plt_offset();
5325 // Map platform-specific relocs to real relocs
5327 template<bool big_endian
>
5329 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5333 case elfcpp::R_ARM_TARGET1
:
5334 // This is either R_ARM_ABS32 or R_ARM_REL32;
5335 return elfcpp::R_ARM_ABS32
;
5337 case elfcpp::R_ARM_TARGET2
:
5338 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5339 return elfcpp::R_ARM_GOT_PREL
;
5346 // Whether if two EABI versions V1 and V2 are compatible.
5348 template<bool big_endian
>
5350 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5351 elfcpp::Elf_Word v1
,
5352 elfcpp::Elf_Word v2
)
5354 // v4 and v5 are the same spec before and after it was released,
5355 // so allow mixing them.
5356 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5357 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5363 // Combine FLAGS from an input object called NAME and the processor-specific
5364 // flags in the ELF header of the output. Much of this is adapted from the
5365 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5366 // in bfd/elf32-arm.c.
5368 template<bool big_endian
>
5370 Target_arm
<big_endian
>::merge_processor_specific_flags(
5371 const std::string
& name
,
5372 elfcpp::Elf_Word flags
)
5374 if (this->are_processor_specific_flags_set())
5376 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5378 // Nothing to merge if flags equal to those in output.
5379 if (flags
== out_flags
)
5382 // Complain about various flag mismatches.
5383 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5384 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5385 if (!this->are_eabi_versions_compatible(version1
, version2
))
5386 gold_error(_("Source object %s has EABI version %d but output has "
5387 "EABI version %d."),
5389 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5390 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5394 // If the input is the default architecture and had the default
5395 // flags then do not bother setting the flags for the output
5396 // architecture, instead allow future merges to do this. If no
5397 // future merges ever set these flags then they will retain their
5398 // uninitialised values, which surprise surprise, correspond
5399 // to the default values.
5403 // This is the first time, just copy the flags.
5404 // We only copy the EABI version for now.
5405 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5409 // Adjust ELF file header.
5410 template<bool big_endian
>
5412 Target_arm
<big_endian
>::do_adjust_elf_header(
5413 unsigned char* view
,
5416 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5418 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5419 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5420 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5422 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5423 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5424 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5426 e_ident
[elfcpp::EI_OSABI
] = 0;
5427 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5429 // FIXME: Do EF_ARM_BE8 adjustment.
5431 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5432 oehdr
.put_e_ident(e_ident
);
5435 // do_make_elf_object to override the same function in the base class.
5436 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5437 // to store ARM specific information. Hence we need to have our own
5438 // ELF object creation.
5440 template<bool big_endian
>
5442 Target_arm
<big_endian
>::do_make_elf_object(
5443 const std::string
& name
,
5444 Input_file
* input_file
,
5445 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5447 int et
= ehdr
.get_e_type();
5448 if (et
== elfcpp::ET_REL
)
5450 Arm_relobj
<big_endian
>* obj
=
5451 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5455 else if (et
== elfcpp::ET_DYN
)
5457 Sized_dynobj
<32, big_endian
>* obj
=
5458 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5464 gold_error(_("%s: unsupported ELF file type %d"),
5470 // Return whether a relocation type used the LSB to distinguish THUMB
5472 template<bool big_endian
>
5474 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
5478 case elfcpp::R_ARM_PC24
:
5479 case elfcpp::R_ARM_ABS32
:
5480 case elfcpp::R_ARM_REL32
:
5481 case elfcpp::R_ARM_SBREL32
:
5482 case elfcpp::R_ARM_THM_CALL
:
5483 case elfcpp::R_ARM_GLOB_DAT
:
5484 case elfcpp::R_ARM_JUMP_SLOT
:
5485 case elfcpp::R_ARM_GOTOFF32
:
5486 case elfcpp::R_ARM_PLT32
:
5487 case elfcpp::R_ARM_CALL
:
5488 case elfcpp::R_ARM_JUMP24
:
5489 case elfcpp::R_ARM_THM_JUMP24
:
5490 case elfcpp::R_ARM_SBREL31
:
5491 case elfcpp::R_ARM_PREL31
:
5492 case elfcpp::R_ARM_MOVW_ABS_NC
:
5493 case elfcpp::R_ARM_MOVW_PREL_NC
:
5494 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5495 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5496 case elfcpp::R_ARM_THM_JUMP19
:
5497 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
5498 case elfcpp::R_ARM_ALU_PC_G0_NC
:
5499 case elfcpp::R_ARM_ALU_PC_G0
:
5500 case elfcpp::R_ARM_ALU_PC_G1_NC
:
5501 case elfcpp::R_ARM_ALU_PC_G1
:
5502 case elfcpp::R_ARM_ALU_PC_G2
:
5503 case elfcpp::R_ARM_ALU_SB_G0_NC
:
5504 case elfcpp::R_ARM_ALU_SB_G0
:
5505 case elfcpp::R_ARM_ALU_SB_G1_NC
:
5506 case elfcpp::R_ARM_ALU_SB_G1
:
5507 case elfcpp::R_ARM_ALU_SB_G2
:
5508 case elfcpp::R_ARM_MOVW_BREL_NC
:
5509 case elfcpp::R_ARM_MOVW_BREL
:
5510 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
5511 case elfcpp::R_ARM_THM_MOVW_BREL
:
5518 // Stub-generation methods for Target_arm.
5520 // Make a new Arm_input_section object.
5522 template<bool big_endian
>
5523 Arm_input_section
<big_endian
>*
5524 Target_arm
<big_endian
>::new_arm_input_section(
5528 Input_section_specifier
iss(relobj
, shndx
);
5530 Arm_input_section
<big_endian
>* arm_input_section
=
5531 new Arm_input_section
<big_endian
>(relobj
, shndx
);
5532 arm_input_section
->init();
5534 // Register new Arm_input_section in map for look-up.
5535 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
5536 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
5538 // Make sure that it we have not created another Arm_input_section
5539 // for this input section already.
5540 gold_assert(ins
.second
);
5542 return arm_input_section
;
5545 // Find the Arm_input_section object corresponding to the SHNDX-th input
5546 // section of RELOBJ.
5548 template<bool big_endian
>
5549 Arm_input_section
<big_endian
>*
5550 Target_arm
<big_endian
>::find_arm_input_section(
5552 unsigned int shndx
) const
5554 Input_section_specifier
iss(relobj
, shndx
);
5555 typename
Arm_input_section_map::const_iterator p
=
5556 this->arm_input_section_map_
.find(iss
);
5557 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
5560 // Make a new stub table.
5562 template<bool big_endian
>
5563 Stub_table
<big_endian
>*
5564 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
5566 Stub_table
<big_endian
>* stub_table
=
5567 new Stub_table
<big_endian
>(owner
);
5568 this->stub_tables_
.push_back(stub_table
);
5570 stub_table
->set_address(owner
->address() + owner
->data_size());
5571 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
5572 stub_table
->finalize_data_size();
5577 // Scan a relocation for stub generation.
5579 template<bool big_endian
>
5581 Target_arm
<big_endian
>::scan_reloc_for_stub(
5582 const Relocate_info
<32, big_endian
>* relinfo
,
5583 unsigned int r_type
,
5584 const Sized_symbol
<32>* gsym
,
5586 const Symbol_value
<32>* psymval
,
5587 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
5588 Arm_address address
)
5590 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
5592 const Arm_relobj
<big_endian
>* arm_relobj
=
5593 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5595 bool target_is_thumb
;
5596 Symbol_value
<32> symval
;
5599 // This is a global symbol. Determine if we use PLT and if the
5600 // final target is THUMB.
5601 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
5603 // This uses a PLT, change the symbol value.
5604 symval
.set_output_value(this->plt_section()->address()
5605 + gsym
->plt_offset());
5607 target_is_thumb
= false;
5609 else if (gsym
->is_undefined())
5610 // There is no need to generate a stub symbol is undefined.
5615 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5616 || (gsym
->type() == elfcpp::STT_FUNC
5617 && !gsym
->is_undefined()
5618 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
5623 // This is a local symbol. Determine if the final target is THUMB.
5624 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
5627 // Strip LSB if this points to a THUMB target.
5629 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5630 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
5632 Arm_address stripped_value
=
5633 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
5634 symval
.set_output_value(stripped_value
);
5638 // Get the symbol value.
5639 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
5641 // Owing to pipelining, the PC relative branches below actually skip
5642 // two instructions when the branch offset is 0.
5643 Arm_address destination
;
5646 case elfcpp::R_ARM_CALL
:
5647 case elfcpp::R_ARM_JUMP24
:
5648 case elfcpp::R_ARM_PLT32
:
5650 destination
= value
+ addend
+ 8;
5652 case elfcpp::R_ARM_THM_CALL
:
5653 case elfcpp::R_ARM_THM_XPC22
:
5654 case elfcpp::R_ARM_THM_JUMP24
:
5655 case elfcpp::R_ARM_THM_JUMP19
:
5657 destination
= value
+ addend
+ 4;
5663 Stub_type stub_type
=
5664 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
5667 // This reloc does not need a stub.
5668 if (stub_type
== arm_stub_none
)
5671 // Try looking up an existing stub from a stub table.
5672 Stub_table
<big_endian
>* stub_table
=
5673 arm_relobj
->stub_table(relinfo
->data_shndx
);
5674 gold_assert(stub_table
!= NULL
);
5676 // Locate stub by destination.
5677 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
5679 // Create a stub if there is not one already
5680 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
5683 // create a new stub and add it to stub table.
5684 stub
= this->stub_factory().make_reloc_stub(stub_type
);
5685 stub_table
->add_reloc_stub(stub
, stub_key
);
5688 // Record the destination address.
5689 stub
->set_destination_address(destination
5690 | (target_is_thumb
? 1 : 0));
5693 // This function scans a relocation sections for stub generation.
5694 // The template parameter Relocate must be a class type which provides
5695 // a single function, relocate(), which implements the machine
5696 // specific part of a relocation.
5698 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
5699 // SHT_REL or SHT_RELA.
5701 // PRELOCS points to the relocation data. RELOC_COUNT is the number
5702 // of relocs. OUTPUT_SECTION is the output section.
5703 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
5704 // mapped to output offsets.
5706 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
5707 // VIEW_SIZE is the size. These refer to the input section, unless
5708 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
5709 // the output section.
5711 template<bool big_endian
>
5712 template<int sh_type
>
5714 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
5715 const Relocate_info
<32, big_endian
>* relinfo
,
5716 const unsigned char* prelocs
,
5718 Output_section
* output_section
,
5719 bool needs_special_offset_handling
,
5720 const unsigned char* view
,
5721 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
5724 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
5725 const int reloc_size
=
5726 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
5728 Arm_relobj
<big_endian
>* arm_object
=
5729 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5730 unsigned int local_count
= arm_object
->local_symbol_count();
5732 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
5734 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
5736 Reltype
reloc(prelocs
);
5738 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
5739 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5740 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
5742 r_type
= this->get_real_reloc_type(r_type
);
5744 // Only a few relocation types need stubs.
5745 if ((r_type
!= elfcpp::R_ARM_CALL
)
5746 && (r_type
!= elfcpp::R_ARM_JUMP24
)
5747 && (r_type
!= elfcpp::R_ARM_PLT32
)
5748 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
5749 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
5750 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
5751 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
5754 section_offset_type offset
=
5755 convert_to_section_size_type(reloc
.get_r_offset());
5757 if (needs_special_offset_handling
)
5759 offset
= output_section
->output_offset(relinfo
->object
,
5760 relinfo
->data_shndx
,
5767 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
5768 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
5769 stub_addend_reader(r_type
, view
+ offset
, reloc
);
5771 const Sized_symbol
<32>* sym
;
5773 Symbol_value
<32> symval
;
5774 const Symbol_value
<32> *psymval
;
5775 if (r_sym
< local_count
)
5778 psymval
= arm_object
->local_symbol(r_sym
);
5780 // If the local symbol belongs to a section we are discarding,
5781 // and that section is a debug section, try to find the
5782 // corresponding kept section and map this symbol to its
5783 // counterpart in the kept section. The symbol must not
5784 // correspond to a section we are folding.
5786 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
5788 && shndx
!= elfcpp::SHN_UNDEF
5789 && !arm_object
->is_section_included(shndx
)
5790 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
5792 if (comdat_behavior
== CB_UNDETERMINED
)
5795 arm_object
->section_name(relinfo
->data_shndx
);
5796 comdat_behavior
= get_comdat_behavior(name
.c_str());
5798 if (comdat_behavior
== CB_PRETEND
)
5801 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
5802 arm_object
->map_to_kept_section(shndx
, &found
);
5804 symval
.set_output_value(value
+ psymval
->input_value());
5806 symval
.set_output_value(0);
5810 symval
.set_output_value(0);
5812 symval
.set_no_output_symtab_entry();
5818 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
5819 gold_assert(gsym
!= NULL
);
5820 if (gsym
->is_forwarder())
5821 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
5823 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
5824 if (sym
->has_symtab_index())
5825 symval
.set_output_symtab_index(sym
->symtab_index());
5827 symval
.set_no_output_symtab_entry();
5829 // We need to compute the would-be final value of this global
5831 const Symbol_table
* symtab
= relinfo
->symtab
;
5832 const Sized_symbol
<32>* sized_symbol
=
5833 symtab
->get_sized_symbol
<32>(gsym
);
5834 Symbol_table::Compute_final_value_status status
;
5836 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
5838 // Skip this if the symbol has not output section.
5839 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
5842 symval
.set_output_value(value
);
5846 // If symbol is a section symbol, we don't know the actual type of
5847 // destination. Give up.
5848 if (psymval
->is_section_symbol())
5851 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
5852 addend
, view_address
+ offset
);
5856 // Scan an input section for stub generation.
5858 template<bool big_endian
>
5860 Target_arm
<big_endian
>::scan_section_for_stubs(
5861 const Relocate_info
<32, big_endian
>* relinfo
,
5862 unsigned int sh_type
,
5863 const unsigned char* prelocs
,
5865 Output_section
* output_section
,
5866 bool needs_special_offset_handling
,
5867 const unsigned char* view
,
5868 Arm_address view_address
,
5869 section_size_type view_size
)
5871 if (sh_type
== elfcpp::SHT_REL
)
5872 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
5877 needs_special_offset_handling
,
5881 else if (sh_type
== elfcpp::SHT_RELA
)
5882 // We do not support RELA type relocations yet. This is provided for
5884 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
5889 needs_special_offset_handling
,
5897 // Group input sections for stub generation.
5899 // We goup input sections in an output sections so that the total size,
5900 // including any padding space due to alignment is smaller than GROUP_SIZE
5901 // unless the only input section in group is bigger than GROUP_SIZE already.
5902 // Then an ARM stub table is created to follow the last input section
5903 // in group. For each group an ARM stub table is created an is placed
5904 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
5905 // extend the group after the stub table.
5907 template<bool big_endian
>
5909 Target_arm
<big_endian
>::group_sections(
5911 section_size_type group_size
,
5912 bool stubs_always_after_branch
)
5914 // Group input sections and insert stub table
5915 Layout::Section_list section_list
;
5916 layout
->get_allocated_sections(§ion_list
);
5917 for (Layout::Section_list::const_iterator p
= section_list
.begin();
5918 p
!= section_list
.end();
5921 Arm_output_section
<big_endian
>* output_section
=
5922 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
5923 output_section
->group_sections(group_size
, stubs_always_after_branch
,
5928 // Relaxation hook. This is where we do stub generation.
5930 template<bool big_endian
>
5932 Target_arm
<big_endian
>::do_relax(
5934 const Input_objects
* input_objects
,
5935 Symbol_table
* symtab
,
5938 // No need to generate stubs if this is a relocatable link.
5939 gold_assert(!parameters
->options().relocatable());
5941 // If this is the first pass, we need to group input sections into
5945 // Determine the stub group size. The group size is the absolute
5946 // value of the parameter --stub-group-size. If --stub-group-size
5947 // is passed a negative value, we restict stubs to be always after
5948 // the stubbed branches.
5949 int32_t stub_group_size_param
=
5950 parameters
->options().stub_group_size();
5951 bool stubs_always_after_branch
= stub_group_size_param
< 0;
5952 section_size_type stub_group_size
= abs(stub_group_size_param
);
5954 if (stub_group_size
== 1)
5957 // Thumb branch range is +-4MB has to be used as the default
5958 // maximum size (a given section can contain both ARM and Thumb
5959 // code, so the worst case has to be taken into account).
5961 // This value is 24K less than that, which allows for 2025
5962 // 12-byte stubs. If we exceed that, then we will fail to link.
5963 // The user will have to relink with an explicit group size
5965 stub_group_size
= 4170000;
5968 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
5971 // clear changed flags for all stub_tables
5972 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
5973 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5974 sp
!= this->stub_tables_
.end();
5976 (*sp
)->set_has_been_changed(false);
5978 // scan relocs for stubs
5979 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
5980 op
!= input_objects
->relobj_end();
5983 Arm_relobj
<big_endian
>* arm_relobj
=
5984 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
5985 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
5988 bool any_stub_table_changed
= false;
5989 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5990 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
5993 if ((*sp
)->has_been_changed())
5994 any_stub_table_changed
= true;
5997 return any_stub_table_changed
;
6002 template<bool big_endian
>
6004 Target_arm
<big_endian
>::relocate_stub(
6006 const Relocate_info
<32, big_endian
>* relinfo
,
6007 Output_section
* output_section
,
6008 unsigned char* view
,
6009 Arm_address address
,
6010 section_size_type view_size
)
6013 const Stub_template
* stub_template
= stub
->stub_template();
6014 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
6016 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
6017 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
6019 unsigned int r_type
= insn
->r_type();
6020 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
6021 section_size_type reloc_size
= insn
->size();
6022 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
6024 // This is the address of the stub destination.
6025 Arm_address target
= stub
->reloc_target(i
);
6026 Symbol_value
<32> symval
;
6027 symval
.set_output_value(target
);
6029 // Synthesize a fake reloc just in case. We don't have a symbol so
6031 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
6032 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
6033 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
6034 reloc_write
.put_r_offset(reloc_offset
);
6035 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
6036 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
6038 relocate
.relocate(relinfo
, this, output_section
,
6039 this->fake_relnum_for_stubs
, rel
, r_type
,
6040 NULL
, &symval
, view
+ reloc_offset
,
6041 address
+ reloc_offset
, reloc_size
);
6045 // The selector for arm object files.
6047 template<bool big_endian
>
6048 class Target_selector_arm
: public Target_selector
6051 Target_selector_arm()
6052 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
6053 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
6057 do_instantiate_target()
6058 { return new Target_arm
<big_endian
>(); }
6061 Target_selector_arm
<false> target_selector_arm
;
6062 Target_selector_arm
<true> target_selector_armbe
;
6064 } // End anonymous namespace.