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 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
130 // There are probably a lot more.
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
138 // Types of instruction templates.
147 // Factory methods to create instrunction templates in different formats.
149 static const Insn_template
150 thumb16_insn(uint32_t data
)
151 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data
)
157 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 1); }
159 static const Insn_template
160 thumb32_insn(uint32_t data
)
161 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
163 static const Insn_template
164 thumb32_b_insn(uint32_t data
, int reloc_addend
)
166 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
170 static const Insn_template
171 arm_insn(uint32_t data
)
172 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
174 static const Insn_template
175 arm_rel_insn(unsigned data
, int reloc_addend
)
176 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
178 static const Insn_template
179 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
180 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
182 // Accessors. This class is used for read-only objects so no modifiers
187 { return this->data_
; }
189 // Return the instruction sequence type of this.
192 { return this->type_
; }
194 // Return the ARM relocation type of this.
197 { return this->r_type_
; }
201 { return this->reloc_addend_
; }
203 // Return size of instrunction template in bytes.
207 // Return byte-alignment of instrunction template.
212 // We make the constructor private to ensure that only the factory
215 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
216 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
222 // Instruction template type.
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_
;
226 // Relocation addend.
227 int32_t reloc_addend_
;
230 // Macro for generating code to stub types. One entry per long/short
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
253 #define DEF_STUB(x) arm_stub_##x,
259 // First reloc stub type.
260 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
261 // Last reloc stub type.
262 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
270 arm_stub_type_last
= arm_stub_a8_veneer_blx
274 // Stub template class. Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
281 Stub_template(Stub_type
, const Insn_template
*, size_t);
289 { return this->type_
; }
291 // Return an array of instruction templates.
294 { return this->insns_
; }
296 // Return size of template in number of instructions.
299 { return this->insn_count_
; }
301 // Return size of template in bytes.
304 { return this->size_
; }
306 // Return alignment of the stub template.
309 { return this->alignment_
; }
311 // Return whether entry point is in thumb mode.
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_
; }
316 // Return number of relocations in this template.
319 { return this->relocs_
.size(); }
321 // Return index of the I-th instruction with relocation.
323 reloc_insn_index(size_t i
) const
325 gold_assert(i
< this->relocs_
.size());
326 return this->relocs_
[i
].first
;
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
332 reloc_offset(size_t i
) const
334 gold_assert(i
< this->relocs_
.size());
335 return this->relocs_
[i
].second
;
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair
<size_t, section_size_type
> Reloc
;
343 // A Stub_template may not be copied. We want to share templates as much
345 Stub_template(const Stub_template
&);
346 Stub_template
& operator=(const Stub_template
&);
350 // Points to an array of Insn_templates.
351 const Insn_template
* insns_
;
352 // Number of Insn_templates in insns_[].
354 // Size of templated instructions in bytes.
356 // Alignment of templated instructions.
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_
;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector
<Reloc
> relocs_
;
367 // A class for code stubs. This is a base class for different type of
368 // stubs used in the ARM target.
374 static const section_offset_type invalid_offset
=
375 static_cast<section_offset_type
>(-1);
378 Stub(const Stub_template
* stub_template
)
379 : stub_template_(stub_template
), offset_(invalid_offset
)
386 // Return the stub template.
388 stub_template() const
389 { return this->stub_template_
; }
391 // Return offset of code stub from beginning of its containing stub table.
395 gold_assert(this->offset_
!= invalid_offset
);
396 return this->offset_
;
399 // Set offset of code stub from beginning of its containing stub table.
401 set_offset(section_offset_type offset
)
402 { this->offset_
= offset
; }
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
407 reloc_target(size_t i
)
408 { return this->do_reloc_target(i
); }
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
412 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
413 { this->do_write(view
, view_size
, big_endian
); }
416 // This must be defined in the child class.
418 do_reloc_target(size_t) = 0;
420 // This must be defined in the child class.
422 do_write(unsigned char*, section_size_type
, bool) = 0;
426 const Stub_template
* stub_template_
;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_
;
431 // Reloc stub class. These are stubs we use to fix up relocation because
432 // of limited branch ranges.
434 class Reloc_stub
: public Stub
437 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
441 // Return destination address.
443 destination_address() const
445 gold_assert(this->destination_address_
!= this->invalid_address
);
446 return this->destination_address_
;
449 // Set destination address.
451 set_destination_address(Arm_address address
)
453 gold_assert(address
!= this->invalid_address
);
454 this->destination_address_
= address
;
457 // Reset destination address.
459 reset_destination_address()
460 { this->destination_address_
= this->invalid_address
; }
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
467 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
468 Arm_address branch_target
, bool target_is_thumb
);
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
481 unsigned int r_sym
, int32_t addend
)
482 : stub_type_(stub_type
), addend_(addend
)
486 this->r_sym_
= Reloc_stub::invalid_index
;
487 this->u_
.symbol
= symbol
;
491 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
492 this->r_sym_
= r_sym
;
493 this->u_
.relobj
= relobj
;
500 // Accessors: Keys are meant to be read-only object so no modifiers are
506 { return this->stub_type_
; }
508 // Return the local symbol index or invalid_index.
511 { return this->r_sym_
; }
513 // Return the symbol if there is one.
516 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
518 // Return the relobj if there is one.
521 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
523 // Whether this equals to another key k.
525 eq(const Key
& k
) const
527 return ((this->stub_type_
== k
.stub_type_
)
528 && (this->r_sym_
== k
.r_sym_
)
529 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
530 ? (this->u_
.relobj
== k
.u_
.relobj
)
531 : (this->u_
.symbol
== k
.u_
.symbol
))
532 && (this->addend_
== k
.addend_
));
535 // Return a hash value.
539 return (this->stub_type_
541 ^ gold::string_hash
<char>(
542 (this->r_sym_
!= Reloc_stub::invalid_index
)
543 ? this->u_
.relobj
->name().c_str()
544 : this->u_
.symbol
->name())
548 // Functors for STL associative containers.
552 operator()(const Key
& k
) const
553 { return k
.hash_value(); }
559 operator()(const Key
& k1
, const Key
& k2
) const
560 { return k1
.eq(k2
); }
563 // Name of key. This is mainly for debugging.
569 Stub_type stub_type_
;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
581 const Symbol
* symbol
;
582 const Relobj
* relobj
;
584 // Addend associated with a reloc.
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template
* stub_template
)
591 : Stub(stub_template
), destination_address_(invalid_address
)
597 friend class Stub_factory
;
600 // Return the relocation target address of the i-th relocation in the
603 do_reloc_target(size_t i
)
605 // All reloc stub have only one relocation.
607 return this->destination_address_
;
610 // A template to implement do_write below.
611 template<bool big_endian
>
613 do_fixed_endian_write(unsigned char*, section_size_type
);
617 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
);
619 // Address of destination.
620 Arm_address destination_address_
;
623 // Stub factory class.
628 // Return the unique instance of this class.
629 static const Stub_factory
&
632 static Stub_factory singleton
;
636 // Make a relocation stub.
638 make_reloc_stub(Stub_type stub_type
) const
640 gold_assert(stub_type
>= arm_stub_reloc_first
641 && stub_type
<= arm_stub_reloc_last
);
642 return new Reloc_stub(this->stub_templates_
[stub_type
]);
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory
&);
653 Stub_factory
& operator=(Stub_factory
&);
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
659 // A class to hold stubs for the ARM target.
661 template<bool big_endian
>
662 class Stub_table
: public Output_data
665 Stub_table(Arm_input_section
<big_endian
>* owner
)
666 : Output_data(), addralign_(1), owner_(owner
), has_been_changed_(false),
673 // Owner of this stub table.
674 Arm_input_section
<big_endian
>*
676 { return this->owner_
; }
678 // Whether this stub table is empty.
681 { return this->reloc_stubs_
.empty(); }
683 // Whether this has been changed.
685 has_been_changed() const
686 { return this->has_been_changed_
; }
688 // Set the has-been-changed flag.
690 set_has_been_changed(bool value
)
691 { this->has_been_changed_
= value
; }
693 // Return the current data size.
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
701 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
);
703 // Look up a relocation stub using KEY. Return NULL if there is none.
705 find_reloc_stub(const Reloc_stub::Key
& key
) const
707 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
708 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
711 // Relocate stubs in this stub table.
713 relocate_stubs(const Relocate_info
<32, big_endian
>*,
714 Target_arm
<big_endian
>*, Output_section
*,
715 unsigned char*, Arm_address
, section_size_type
);
718 // Write out section contents.
720 do_write(Output_file
*);
722 // Return the required alignment.
725 { return this->addralign_
; }
727 // Finalize data size.
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
732 // Reset address and file offset.
734 do_reset_address_and_file_offset();
737 // Unordered map of stubs.
739 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
740 Reloc_stub::Key::equal_to
>
745 // Owner of this stub table.
746 Arm_input_section
<big_endian
>* owner_
;
747 // This is set to true during relaxiong if the size of the stub table
749 bool has_been_changed_
;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_
;
754 // A class to wrap an ordinary input section containing executable code.
756 template<bool big_endian
>
757 class Arm_input_section
: public Output_relaxed_input_section
760 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
761 : Output_relaxed_input_section(relobj
, shndx
, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL
)
772 // Whether this is a stub table owner.
774 is_stub_table_owner() const
775 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
777 // Return the stub table.
778 Stub_table
<big_endian
>*
780 { return this->stub_table_
; }
782 // Set the stub_table.
784 set_stub_table(Stub_table
<big_endian
>* stub_table
)
785 { this->stub_table_
= stub_table
; }
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section
<big_endian
>*
790 as_arm_input_section(Output_relaxed_input_section
* poris
)
791 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
794 // Write data to output file.
796 do_write(Output_file
*);
798 // Return required alignment of this.
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_
->addralign(),
804 this->original_addralign_
);
806 return this->original_addralign_
;
809 // Finalize data size.
811 set_final_data_size();
813 // Reset address and file offset.
815 do_reset_address_and_file_offset();
819 do_output_offset(const Relobj
* object
, unsigned int shndx
,
820 section_offset_type offset
,
821 section_offset_type
* poutput
) const
823 if ((object
== this->relobj())
824 && (shndx
== this->shndx())
826 && (convert_types
<uint64_t, section_offset_type
>(offset
)
827 <= this->original_size_
))
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section
&);
839 Arm_input_section
& operator=(const Arm_input_section
&);
841 // Address alignment of the original input section.
842 uint64_t original_addralign_
;
843 // Section size of the original input section.
844 uint64_t original_size_
;
846 Stub_table
<big_endian
>* stub_table_
;
849 // Arm output section class. This is defined mainly to add a number of
850 // stub generation methods.
852 template<bool big_endian
>
853 class Arm_output_section
: public Output_section
856 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
857 elfcpp::Elf_Xword flags
)
858 : Output_section(name
, type
, flags
)
861 ~Arm_output_section()
864 // Group input sections for stub generation.
866 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section
<big_endian
>*
871 as_arm_output_section(Output_section
* os
)
872 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
876 typedef Output_section::Input_section Input_section
;
877 typedef Output_section::Input_section_list Input_section_list
;
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator
,
881 Input_section_list::const_iterator
,
882 Input_section_list::const_iterator
,
883 Target_arm
<big_endian
>*,
884 std::vector
<Output_relaxed_input_section
*>*);
889 template<bool big_endian
>
890 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
893 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
895 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
896 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
897 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
898 stub_tables_(), local_symbol_is_thumb_function_()
904 // Return the stub table of the SHNDX-th section if there is one.
905 Stub_table
<big_endian
>*
906 stub_table(unsigned int shndx
) const
908 gold_assert(shndx
< this->stub_tables_
.size());
909 return this->stub_tables_
[shndx
];
912 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
914 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
916 gold_assert(shndx
< this->stub_tables_
.size());
917 this->stub_tables_
[shndx
] = stub_table
;
920 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
921 // index. This is only valid after do_count_local_symbol is called.
923 local_symbol_is_thumb_function(unsigned int r_sym
) const
925 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
926 return this->local_symbol_is_thumb_function_
[r_sym
];
929 // Scan all relocation sections for stub generation.
931 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
934 // Convert regular input section with index SHNDX to a relaxed section.
936 convert_input_section_to_relaxed_section(unsigned shndx
)
938 // The stubs have relocations and we need to process them after writing
939 // out the stubs. So relocation now must follow section write.
940 this->invalidate_section_offset(shndx
);
941 this->set_relocs_must_follow_section_writes();
944 // Downcast a base pointer to an Arm_relobj pointer. This is
945 // not type-safe but we only use Arm_relobj not the base class.
946 static Arm_relobj
<big_endian
>*
947 as_arm_relobj(Relobj
* relobj
)
948 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
950 // Processor-specific flags in ELF file header. This is valid only after
953 processor_specific_flags() const
954 { return this->processor_specific_flags_
; }
957 // Post constructor setup.
961 // Call parent's setup method.
962 Sized_relobj
<32, big_endian
>::do_setup();
964 // Initialize look-up tables.
965 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
966 this->stub_tables_
.swap(empty_stub_table_list
);
969 // Count the local symbols.
971 do_count_local_symbols(Stringpool_template
<char>*,
972 Stringpool_template
<char>*);
975 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
976 const unsigned char* pshdrs
,
977 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
979 // Read the symbol information.
981 do_read_symbols(Read_symbols_data
* sd
);
984 // List of stub tables.
985 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
986 Stub_table_list stub_tables_
;
987 // Bit vector to tell if a local symbol is a thumb function or not.
988 // This is only valid after do_count_local_symbol is called.
989 std::vector
<bool> local_symbol_is_thumb_function_
;
990 // processor-specific flags in ELF file header.
991 elfcpp::Elf_Word processor_specific_flags_
;
996 template<bool big_endian
>
997 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1000 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1001 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1002 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1003 processor_specific_flags_(0)
1009 // Downcast a base pointer to an Arm_relobj pointer. This is
1010 // not type-safe but we only use Arm_relobj not the base class.
1011 static Arm_dynobj
<big_endian
>*
1012 as_arm_dynobj(Dynobj
* dynobj
)
1013 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1015 // Processor-specific flags in ELF file header. This is valid only after
1018 processor_specific_flags() const
1019 { return this->processor_specific_flags_
; }
1022 // Read the symbol information.
1024 do_read_symbols(Read_symbols_data
* sd
);
1027 // processor-specific flags in ELF file header.
1028 elfcpp::Elf_Word processor_specific_flags_
;
1031 // Functor to read reloc addends during stub generation.
1033 template<int sh_type
, bool big_endian
>
1034 struct Stub_addend_reader
1036 // Return the addend for a relocation of a particular type. Depending
1037 // on whether this is a REL or RELA relocation, read the addend from a
1038 // view or from a Reloc object.
1039 elfcpp::Elf_types
<32>::Elf_Swxword
1041 unsigned int /* r_type */,
1042 const unsigned char* /* view */,
1043 const typename Reloc_types
<sh_type
,
1044 32, big_endian
>::Reloc
& /* reloc */) const;
1047 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1049 template<bool big_endian
>
1050 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1052 elfcpp::Elf_types
<32>::Elf_Swxword
1055 const unsigned char*,
1056 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1059 // Specialized Stub_addend_reader for RELA type relocation sections.
1060 // We currently do not handle RELA type relocation sections but it is trivial
1061 // to implement the addend reader. This is provided for completeness and to
1062 // make it easier to add support for RELA relocation sections in the future.
1064 template<bool big_endian
>
1065 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1067 elfcpp::Elf_types
<32>::Elf_Swxword
1070 const unsigned char*,
1071 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1072 big_endian
>::Reloc
& reloc
) const
1073 { return reloc
.get_r_addend(); }
1076 // Utilities for manipulating integers of up to 32-bits
1080 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1081 // an int32_t. NO_BITS must be between 1 to 32.
1082 template<int no_bits
>
1083 static inline int32_t
1084 sign_extend(uint32_t bits
)
1086 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1088 return static_cast<int32_t>(bits
);
1089 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1091 uint32_t top_bit
= 1U << (no_bits
- 1);
1092 int32_t as_signed
= static_cast<int32_t>(bits
);
1093 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1096 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1097 template<int no_bits
>
1099 has_overflow(uint32_t bits
)
1101 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1104 int32_t max
= (1 << (no_bits
- 1)) - 1;
1105 int32_t min
= -(1 << (no_bits
- 1));
1106 int32_t as_signed
= static_cast<int32_t>(bits
);
1107 return as_signed
> max
|| as_signed
< min
;
1110 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1111 // fits in the given number of bits as either a signed or unsigned value.
1112 // For example, has_signed_unsigned_overflow<8> would check
1113 // -128 <= bits <= 255
1114 template<int no_bits
>
1116 has_signed_unsigned_overflow(uint32_t bits
)
1118 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1121 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1122 int32_t min
= -(1 << (no_bits
- 1));
1123 int32_t as_signed
= static_cast<int32_t>(bits
);
1124 return as_signed
> max
|| as_signed
< min
;
1127 // Select bits from A and B using bits in MASK. For each n in [0..31],
1128 // the n-th bit in the result is chosen from the n-th bits of A and B.
1129 // A zero selects A and a one selects B.
1130 static inline uint32_t
1131 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1132 { return (a
& ~mask
) | (b
& mask
); }
1135 template<bool big_endian
>
1136 class Target_arm
: public Sized_target
<32, big_endian
>
1139 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1142 // When were are relocating a stub, we pass this as the relocation number.
1143 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1146 : Sized_target
<32, big_endian
>(&arm_info
),
1147 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1148 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1149 stub_factory_(Stub_factory::get_instance()),
1150 may_use_blx_(true), should_force_pic_veneer_(false),
1151 arm_input_section_map_()
1154 // Whether we can use BLX.
1157 { return this->may_use_blx_
; }
1159 // Set use-BLX flag.
1161 set_may_use_blx(bool value
)
1162 { this->may_use_blx_
= value
; }
1164 // Whether we force PCI branch veneers.
1166 should_force_pic_veneer() const
1167 { return this->should_force_pic_veneer_
; }
1169 // Set PIC veneer flag.
1171 set_should_force_pic_veneer(bool value
)
1172 { this->should_force_pic_veneer_
= value
; }
1174 // Whether we use THUMB-2 instructions.
1176 using_thumb2() const
1178 // FIXME: This should not hard-coded.
1182 // Whether we use THUMB/THUMB-2 instructions only.
1184 using_thumb_only() const
1186 // FIXME: This should not hard-coded.
1190 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1192 may_use_arm_nop() const
1194 // FIXME: This should not hard-coded.
1198 // Whether we have THUMB-2 NOP.W instruction.
1200 may_use_thumb2_nop() const
1202 // FIXME: This should not hard-coded.
1206 // Process the relocations to determine unreferenced sections for
1207 // garbage collection.
1209 gc_process_relocs(Symbol_table
* symtab
,
1211 Sized_relobj
<32, big_endian
>* object
,
1212 unsigned int data_shndx
,
1213 unsigned int sh_type
,
1214 const unsigned char* prelocs
,
1216 Output_section
* output_section
,
1217 bool needs_special_offset_handling
,
1218 size_t local_symbol_count
,
1219 const unsigned char* plocal_symbols
);
1221 // Scan the relocations to look for symbol adjustments.
1223 scan_relocs(Symbol_table
* symtab
,
1225 Sized_relobj
<32, big_endian
>* object
,
1226 unsigned int data_shndx
,
1227 unsigned int sh_type
,
1228 const unsigned char* prelocs
,
1230 Output_section
* output_section
,
1231 bool needs_special_offset_handling
,
1232 size_t local_symbol_count
,
1233 const unsigned char* plocal_symbols
);
1235 // Finalize the sections.
1237 do_finalize_sections(Layout
*, const Input_objects
*);
1239 // Return the value to use for a dynamic symbol which requires special
1242 do_dynsym_value(const Symbol
*) const;
1244 // Relocate a section.
1246 relocate_section(const Relocate_info
<32, big_endian
>*,
1247 unsigned int sh_type
,
1248 const unsigned char* prelocs
,
1250 Output_section
* output_section
,
1251 bool needs_special_offset_handling
,
1252 unsigned char* view
,
1253 Arm_address view_address
,
1254 section_size_type view_size
,
1255 const Reloc_symbol_changes
*);
1257 // Scan the relocs during a relocatable link.
1259 scan_relocatable_relocs(Symbol_table
* symtab
,
1261 Sized_relobj
<32, big_endian
>* object
,
1262 unsigned int data_shndx
,
1263 unsigned int sh_type
,
1264 const unsigned char* prelocs
,
1266 Output_section
* output_section
,
1267 bool needs_special_offset_handling
,
1268 size_t local_symbol_count
,
1269 const unsigned char* plocal_symbols
,
1270 Relocatable_relocs
*);
1272 // Relocate a section during a relocatable link.
1274 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1275 unsigned int sh_type
,
1276 const unsigned char* prelocs
,
1278 Output_section
* output_section
,
1279 off_t offset_in_output_section
,
1280 const Relocatable_relocs
*,
1281 unsigned char* view
,
1282 Arm_address view_address
,
1283 section_size_type view_size
,
1284 unsigned char* reloc_view
,
1285 section_size_type reloc_view_size
);
1287 // Return whether SYM is defined by the ABI.
1289 do_is_defined_by_abi(Symbol
* sym
) const
1290 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1292 // Return the size of the GOT section.
1296 gold_assert(this->got_
!= NULL
);
1297 return this->got_
->data_size();
1300 // Map platform-specific reloc types
1302 get_real_reloc_type (unsigned int r_type
);
1305 // Methods to support stub-generations.
1308 // Return the stub factory
1310 stub_factory() const
1311 { return this->stub_factory_
; }
1313 // Make a new Arm_input_section object.
1314 Arm_input_section
<big_endian
>*
1315 new_arm_input_section(Relobj
*, unsigned int);
1317 // Find the Arm_input_section object corresponding to the SHNDX-th input
1318 // section of RELOBJ.
1319 Arm_input_section
<big_endian
>*
1320 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1322 // Make a new Stub_table
1323 Stub_table
<big_endian
>*
1324 new_stub_table(Arm_input_section
<big_endian
>*);
1326 // Scan a section for stub generation.
1328 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1329 const unsigned char*, size_t, Output_section
*,
1330 bool, const unsigned char*, Arm_address
,
1335 relocate_stub(Reloc_stub
*, const Relocate_info
<32, big_endian
>*,
1336 Output_section
*, unsigned char*, Arm_address
,
1339 // Get the default ARM target.
1340 static Target_arm
<big_endian
>*
1343 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1344 && parameters
->target().is_big_endian() == big_endian
);
1345 return static_cast<Target_arm
<big_endian
>*>(
1346 parameters
->sized_target
<32, big_endian
>());
1349 // Whether relocation type uses LSB to distinguish THUMB addresses.
1351 reloc_uses_thumb_bit(unsigned int r_type
);
1354 // Make an ELF object.
1356 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1357 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1360 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1361 const elfcpp::Ehdr
<32, !big_endian
>&)
1362 { gold_unreachable(); }
1365 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1366 const elfcpp::Ehdr
<64, false>&)
1367 { gold_unreachable(); }
1370 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1371 const elfcpp::Ehdr
<64, true>&)
1372 { gold_unreachable(); }
1374 // Make an output section.
1376 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1377 elfcpp::Elf_Xword flags
)
1378 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1381 do_adjust_elf_header(unsigned char* view
, int len
) const;
1383 // We only need to generate stubs, and hence perform relaxation if we are
1384 // not doing relocatable linking.
1386 do_may_relax() const
1387 { return !parameters
->options().relocatable(); }
1390 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1393 // The class which scans relocations.
1398 : issued_non_pic_error_(false)
1402 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1403 Sized_relobj
<32, big_endian
>* object
,
1404 unsigned int data_shndx
,
1405 Output_section
* output_section
,
1406 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1407 const elfcpp::Sym
<32, big_endian
>& lsym
);
1410 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1411 Sized_relobj
<32, big_endian
>* object
,
1412 unsigned int data_shndx
,
1413 Output_section
* output_section
,
1414 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1419 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1420 unsigned int r_type
);
1423 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1424 unsigned int r_type
, Symbol
*);
1427 check_non_pic(Relobj
*, unsigned int r_type
);
1429 // Almost identical to Symbol::needs_plt_entry except that it also
1430 // handles STT_ARM_TFUNC.
1432 symbol_needs_plt_entry(const Symbol
* sym
)
1434 // An undefined symbol from an executable does not need a PLT entry.
1435 if (sym
->is_undefined() && !parameters
->options().shared())
1438 return (!parameters
->doing_static_link()
1439 && (sym
->type() == elfcpp::STT_FUNC
1440 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1441 && (sym
->is_from_dynobj()
1442 || sym
->is_undefined()
1443 || sym
->is_preemptible()));
1446 // Whether we have issued an error about a non-PIC compilation.
1447 bool issued_non_pic_error_
;
1450 // The class which implements relocation.
1460 // Return whether the static relocation needs to be applied.
1462 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1465 Output_section
* output_section
);
1467 // Do a relocation. Return false if the caller should not issue
1468 // any warnings about this relocation.
1470 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1471 Output_section
*, size_t relnum
,
1472 const elfcpp::Rel
<32, big_endian
>&,
1473 unsigned int r_type
, const Sized_symbol
<32>*,
1474 const Symbol_value
<32>*,
1475 unsigned char*, Arm_address
,
1478 // Return whether we want to pass flag NON_PIC_REF for this
1481 reloc_is_non_pic (unsigned int r_type
)
1485 case elfcpp::R_ARM_REL32
:
1486 case elfcpp::R_ARM_THM_CALL
:
1487 case elfcpp::R_ARM_CALL
:
1488 case elfcpp::R_ARM_JUMP24
:
1489 case elfcpp::R_ARM_PREL31
:
1490 case elfcpp::R_ARM_THM_ABS5
:
1491 case elfcpp::R_ARM_ABS8
:
1492 case elfcpp::R_ARM_ABS12
:
1493 case elfcpp::R_ARM_ABS16
:
1494 case elfcpp::R_ARM_BASE_ABS
:
1502 // A class which returns the size required for a relocation type,
1503 // used while scanning relocs during a relocatable link.
1504 class Relocatable_size_for_reloc
1508 get_size_for_reloc(unsigned int, Relobj
*);
1511 // Get the GOT section, creating it if necessary.
1512 Output_data_got
<32, big_endian
>*
1513 got_section(Symbol_table
*, Layout
*);
1515 // Get the GOT PLT section.
1517 got_plt_section() const
1519 gold_assert(this->got_plt_
!= NULL
);
1520 return this->got_plt_
;
1523 // Create a PLT entry for a global symbol.
1525 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1527 // Get the PLT section.
1528 const Output_data_plt_arm
<big_endian
>*
1531 gold_assert(this->plt_
!= NULL
);
1535 // Get the dynamic reloc section, creating it if necessary.
1537 rel_dyn_section(Layout
*);
1539 // Return true if the symbol may need a COPY relocation.
1540 // References from an executable object to non-function symbols
1541 // defined in a dynamic object may need a COPY relocation.
1543 may_need_copy_reloc(Symbol
* gsym
)
1545 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1546 && gsym
->may_need_copy_reloc());
1549 // Add a potential copy relocation.
1551 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1552 Sized_relobj
<32, big_endian
>* object
,
1553 unsigned int shndx
, Output_section
* output_section
,
1554 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1556 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1557 symtab
->get_sized_symbol
<32>(sym
),
1558 object
, shndx
, output_section
, reloc
,
1559 this->rel_dyn_section(layout
));
1562 // Whether two EABI versions are compatible.
1564 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1566 // Merge processor-specific flags from input object and those in the ELF
1567 // header of the output.
1569 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1572 // Methods to support stub-generations.
1575 // Group input sections for stub generation.
1577 group_sections(Layout
*, section_size_type
, bool);
1579 // Scan a relocation for stub generation.
1581 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1582 const Sized_symbol
<32>*, unsigned int,
1583 const Symbol_value
<32>*,
1584 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1586 // Scan a relocation section for stub.
1587 template<int sh_type
>
1589 scan_reloc_section_for_stubs(
1590 const Relocate_info
<32, big_endian
>* relinfo
,
1591 const unsigned char* prelocs
,
1593 Output_section
* output_section
,
1594 bool needs_special_offset_handling
,
1595 const unsigned char* view
,
1596 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1599 // Information about this specific target which we pass to the
1600 // general Target structure.
1601 static const Target::Target_info arm_info
;
1603 // The types of GOT entries needed for this platform.
1606 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1609 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1611 // Map input section to Arm_input_section.
1612 typedef Unordered_map
<Input_section_specifier
,
1613 Arm_input_section
<big_endian
>*,
1614 Input_section_specifier::hash
,
1615 Input_section_specifier::equal_to
>
1616 Arm_input_section_map
;
1619 Output_data_got
<32, big_endian
>* got_
;
1621 Output_data_plt_arm
<big_endian
>* plt_
;
1622 // The GOT PLT section.
1623 Output_data_space
* got_plt_
;
1624 // The dynamic reloc section.
1625 Reloc_section
* rel_dyn_
;
1626 // Relocs saved to avoid a COPY reloc.
1627 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1628 // Space for variables copied with a COPY reloc.
1629 Output_data_space
* dynbss_
;
1630 // Vector of Stub_tables created.
1631 Stub_table_list stub_tables_
;
1633 const Stub_factory
&stub_factory_
;
1634 // Whether we can use BLX.
1636 // Whether we force PIC branch veneers.
1637 bool should_force_pic_veneer_
;
1638 // Map for locating Arm_input_sections.
1639 Arm_input_section_map arm_input_section_map_
;
1642 template<bool big_endian
>
1643 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1646 big_endian
, // is_big_endian
1647 elfcpp::EM_ARM
, // machine_code
1648 false, // has_make_symbol
1649 false, // has_resolve
1650 false, // has_code_fill
1651 true, // is_default_stack_executable
1653 "/usr/lib/libc.so.1", // dynamic_linker
1654 0x8000, // default_text_segment_address
1655 0x1000, // abi_pagesize (overridable by -z max-page-size)
1656 0x1000, // common_pagesize (overridable by -z common-page-size)
1657 elfcpp::SHN_UNDEF
, // small_common_shndx
1658 elfcpp::SHN_UNDEF
, // large_common_shndx
1659 0, // small_common_section_flags
1660 0 // large_common_section_flags
1663 // Arm relocate functions class
1666 template<bool big_endian
>
1667 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1672 STATUS_OKAY
, // No error during relocation.
1673 STATUS_OVERFLOW
, // Relocation oveflow.
1674 STATUS_BAD_RELOC
// Relocation cannot be applied.
1678 typedef Relocate_functions
<32, big_endian
> Base
;
1679 typedef Arm_relocate_functions
<big_endian
> This
;
1681 // Encoding of imm16 argument for movt and movw ARM instructions
1684 // imm16 := imm4 | imm12
1686 // 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
1687 // +-------+---------------+-------+-------+-----------------------+
1688 // | | |imm4 | |imm12 |
1689 // +-------+---------------+-------+-------+-----------------------+
1691 // Extract the relocation addend from VAL based on the ARM
1692 // instruction encoding described above.
1693 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1694 extract_arm_movw_movt_addend(
1695 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1697 // According to the Elf ABI for ARM Architecture the immediate
1698 // field is sign-extended to form the addend.
1699 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1702 // Insert X into VAL based on the ARM instruction encoding described
1704 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1705 insert_val_arm_movw_movt(
1706 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1707 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1711 val
|= (x
& 0xf000) << 4;
1715 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1718 // imm16 := imm4 | i | imm3 | imm8
1720 // 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
1721 // +---------+-+-----------+-------++-+-----+-------+---------------+
1722 // | |i| |imm4 || |imm3 | |imm8 |
1723 // +---------+-+-----------+-------++-+-----+-------+---------------+
1725 // Extract the relocation addend from VAL based on the Thumb2
1726 // instruction encoding described above.
1727 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1728 extract_thumb_movw_movt_addend(
1729 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1731 // According to the Elf ABI for ARM Architecture the immediate
1732 // field is sign-extended to form the addend.
1733 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1734 | ((val
>> 15) & 0x0800)
1735 | ((val
>> 4) & 0x0700)
1739 // Insert X into VAL based on the Thumb2 instruction encoding
1741 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1742 insert_val_thumb_movw_movt(
1743 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1744 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1747 val
|= (x
& 0xf000) << 4;
1748 val
|= (x
& 0x0800) << 15;
1749 val
|= (x
& 0x0700) << 4;
1750 val
|= (x
& 0x00ff);
1754 // Handle ARM long branches.
1755 static typename
This::Status
1756 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1757 unsigned char *, const Sized_symbol
<32>*,
1758 const Arm_relobj
<big_endian
>*, unsigned int,
1759 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1761 // Handle THUMB long branches.
1762 static typename
This::Status
1763 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1764 unsigned char *, const Sized_symbol
<32>*,
1765 const Arm_relobj
<big_endian
>*, unsigned int,
1766 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1770 // R_ARM_ABS8: S + A
1771 static inline typename
This::Status
1772 abs8(unsigned char *view
,
1773 const Sized_relobj
<32, big_endian
>* object
,
1774 const Symbol_value
<32>* psymval
)
1776 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1777 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1778 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1779 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1780 Reltype addend
= utils::sign_extend
<8>(val
);
1781 Reltype x
= psymval
->value(object
, addend
);
1782 val
= utils::bit_select(val
, x
, 0xffU
);
1783 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1784 return (utils::has_signed_unsigned_overflow
<8>(x
)
1785 ? This::STATUS_OVERFLOW
1786 : This::STATUS_OKAY
);
1789 // R_ARM_THM_ABS5: S + A
1790 static inline typename
This::Status
1791 thm_abs5(unsigned char *view
,
1792 const Sized_relobj
<32, big_endian
>* object
,
1793 const Symbol_value
<32>* psymval
)
1795 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1796 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1797 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1798 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1799 Reltype addend
= (val
& 0x7e0U
) >> 6;
1800 Reltype x
= psymval
->value(object
, addend
);
1801 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1802 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1803 return (utils::has_overflow
<5>(x
)
1804 ? This::STATUS_OVERFLOW
1805 : This::STATUS_OKAY
);
1808 // R_ARM_ABS12: S + A
1809 static inline typename
This::Status
1810 abs12(unsigned char *view
,
1811 const Sized_relobj
<32, big_endian
>* object
,
1812 const Symbol_value
<32>* psymval
)
1814 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1815 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1816 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1817 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1818 Reltype addend
= val
& 0x0fffU
;
1819 Reltype x
= psymval
->value(object
, addend
);
1820 val
= utils::bit_select(val
, x
, 0x0fffU
);
1821 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1822 return (utils::has_overflow
<12>(x
)
1823 ? This::STATUS_OVERFLOW
1824 : This::STATUS_OKAY
);
1827 // R_ARM_ABS16: S + A
1828 static inline typename
This::Status
1829 abs16(unsigned char *view
,
1830 const Sized_relobj
<32, big_endian
>* object
,
1831 const Symbol_value
<32>* psymval
)
1833 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1834 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1835 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1836 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1837 Reltype addend
= utils::sign_extend
<16>(val
);
1838 Reltype x
= psymval
->value(object
, addend
);
1839 val
= utils::bit_select(val
, x
, 0xffffU
);
1840 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1841 return (utils::has_signed_unsigned_overflow
<16>(x
)
1842 ? This::STATUS_OVERFLOW
1843 : This::STATUS_OKAY
);
1846 // R_ARM_ABS32: (S + A) | T
1847 static inline typename
This::Status
1848 abs32(unsigned char *view
,
1849 const Sized_relobj
<32, big_endian
>* object
,
1850 const Symbol_value
<32>* psymval
,
1851 Arm_address thumb_bit
)
1853 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1854 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1855 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1856 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
1857 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1858 return This::STATUS_OKAY
;
1861 // R_ARM_REL32: (S + A) | T - P
1862 static inline typename
This::Status
1863 rel32(unsigned char *view
,
1864 const Sized_relobj
<32, big_endian
>* object
,
1865 const Symbol_value
<32>* psymval
,
1866 Arm_address address
,
1867 Arm_address thumb_bit
)
1869 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1870 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1871 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1872 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
1873 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1874 return This::STATUS_OKAY
;
1877 // R_ARM_THM_CALL: (S + A) | T - P
1878 static inline typename
This::Status
1879 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1880 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1881 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1882 Arm_address address
, Arm_address thumb_bit
,
1883 bool is_weakly_undefined_without_plt
)
1885 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
1886 object
, r_sym
, psymval
, address
, thumb_bit
,
1887 is_weakly_undefined_without_plt
);
1890 // R_ARM_THM_JUMP24: (S + A) | T - P
1891 static inline typename
This::Status
1892 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1893 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1894 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1895 Arm_address address
, Arm_address thumb_bit
,
1896 bool is_weakly_undefined_without_plt
)
1898 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
1899 object
, r_sym
, psymval
, address
, thumb_bit
,
1900 is_weakly_undefined_without_plt
);
1903 // R_ARM_THM_XPC22: (S + A) | T - P
1904 static inline typename
This::Status
1905 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1906 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1907 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1908 Arm_address address
, Arm_address thumb_bit
,
1909 bool is_weakly_undefined_without_plt
)
1911 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
1912 object
, r_sym
, psymval
, address
, thumb_bit
,
1913 is_weakly_undefined_without_plt
);
1916 // R_ARM_BASE_PREL: B(S) + A - P
1917 static inline typename
This::Status
1918 base_prel(unsigned char* view
,
1920 Arm_address address
)
1922 Base::rel32(view
, origin
- address
);
1926 // R_ARM_BASE_ABS: B(S) + A
1927 static inline typename
This::Status
1928 base_abs(unsigned char* view
,
1931 Base::rel32(view
, origin
);
1935 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1936 static inline typename
This::Status
1937 got_brel(unsigned char* view
,
1938 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
1940 Base::rel32(view
, got_offset
);
1941 return This::STATUS_OKAY
;
1944 // R_ARM_GOT_PREL: GOT(S) + A – P
1945 static inline typename
This::Status
1946 got_prel(unsigned char* view
,
1947 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
,
1948 Arm_address address
)
1950 Base::rel32(view
, got_offset
- address
);
1951 return This::STATUS_OKAY
;
1954 // R_ARM_PLT32: (S + A) | T - P
1955 static inline typename
This::Status
1956 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
1957 unsigned char *view
,
1958 const Sized_symbol
<32>* gsym
,
1959 const Arm_relobj
<big_endian
>* object
,
1961 const Symbol_value
<32>* psymval
,
1962 Arm_address address
,
1963 Arm_address thumb_bit
,
1964 bool is_weakly_undefined_without_plt
)
1966 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
1967 object
, r_sym
, psymval
, address
, thumb_bit
,
1968 is_weakly_undefined_without_plt
);
1971 // R_ARM_XPC25: (S + A) | T - P
1972 static inline typename
This::Status
1973 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
1974 unsigned char *view
,
1975 const Sized_symbol
<32>* gsym
,
1976 const Arm_relobj
<big_endian
>* object
,
1978 const Symbol_value
<32>* psymval
,
1979 Arm_address address
,
1980 Arm_address thumb_bit
,
1981 bool is_weakly_undefined_without_plt
)
1983 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
1984 object
, r_sym
, psymval
, address
, thumb_bit
,
1985 is_weakly_undefined_without_plt
);
1988 // R_ARM_CALL: (S + A) | T - P
1989 static inline typename
This::Status
1990 call(const Relocate_info
<32, big_endian
>* relinfo
,
1991 unsigned char *view
,
1992 const Sized_symbol
<32>* gsym
,
1993 const Arm_relobj
<big_endian
>* object
,
1995 const Symbol_value
<32>* psymval
,
1996 Arm_address address
,
1997 Arm_address thumb_bit
,
1998 bool is_weakly_undefined_without_plt
)
2000 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2001 object
, r_sym
, psymval
, address
, thumb_bit
,
2002 is_weakly_undefined_without_plt
);
2005 // R_ARM_JUMP24: (S + A) | T - P
2006 static inline typename
This::Status
2007 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2008 unsigned char *view
,
2009 const Sized_symbol
<32>* gsym
,
2010 const Arm_relobj
<big_endian
>* object
,
2012 const Symbol_value
<32>* psymval
,
2013 Arm_address address
,
2014 Arm_address thumb_bit
,
2015 bool is_weakly_undefined_without_plt
)
2017 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2018 object
, r_sym
, psymval
, address
, thumb_bit
,
2019 is_weakly_undefined_without_plt
);
2022 // R_ARM_PREL: (S + A) | T - P
2023 static inline typename
This::Status
2024 prel31(unsigned char *view
,
2025 const Sized_relobj
<32, big_endian
>* object
,
2026 const Symbol_value
<32>* psymval
,
2027 Arm_address address
,
2028 Arm_address thumb_bit
)
2030 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2031 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2032 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2033 Valtype addend
= utils::sign_extend
<31>(val
);
2034 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2035 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2036 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2037 return (utils::has_overflow
<31>(x
) ?
2038 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2041 // R_ARM_MOVW_ABS_NC: (S + A) | T
2042 static inline typename
This::Status
2043 movw_abs_nc(unsigned char *view
,
2044 const Sized_relobj
<32, big_endian
>* object
,
2045 const Symbol_value
<32>* psymval
,
2046 Arm_address thumb_bit
)
2048 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2049 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2050 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2051 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2052 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2053 val
= This::insert_val_arm_movw_movt(val
, x
);
2054 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2055 return This::STATUS_OKAY
;
2058 // R_ARM_MOVT_ABS: S + A
2059 static inline typename
This::Status
2060 movt_abs(unsigned char *view
,
2061 const Sized_relobj
<32, big_endian
>* object
,
2062 const Symbol_value
<32>* psymval
)
2064 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2065 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2066 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2067 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2068 Valtype x
= psymval
->value(object
, addend
) >> 16;
2069 val
= This::insert_val_arm_movw_movt(val
, x
);
2070 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2071 return This::STATUS_OKAY
;
2074 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2075 static inline typename
This::Status
2076 thm_movw_abs_nc(unsigned char *view
,
2077 const Sized_relobj
<32, big_endian
>* object
,
2078 const Symbol_value
<32>* psymval
,
2079 Arm_address thumb_bit
)
2081 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2082 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2083 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2084 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2085 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2086 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2087 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2088 val
= This::insert_val_thumb_movw_movt(val
, x
);
2089 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2090 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2091 return This::STATUS_OKAY
;
2094 // R_ARM_THM_MOVT_ABS: S + A
2095 static inline typename
This::Status
2096 thm_movt_abs(unsigned char *view
,
2097 const Sized_relobj
<32, big_endian
>* object
,
2098 const Symbol_value
<32>* psymval
)
2100 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2101 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2102 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2103 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2104 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2105 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2106 Reltype x
= psymval
->value(object
, addend
) >> 16;
2107 val
= This::insert_val_thumb_movw_movt(val
, x
);
2108 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2109 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2110 return This::STATUS_OKAY
;
2113 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2114 static inline typename
This::Status
2115 movw_prel_nc(unsigned char *view
,
2116 const Sized_relobj
<32, big_endian
>* object
,
2117 const Symbol_value
<32>* psymval
,
2118 Arm_address address
,
2119 Arm_address thumb_bit
)
2121 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2122 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2123 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2124 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2125 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2126 val
= This::insert_val_arm_movw_movt(val
, x
);
2127 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2128 return This::STATUS_OKAY
;
2131 // R_ARM_MOVT_PREL: S + A - P
2132 static inline typename
This::Status
2133 movt_prel(unsigned char *view
,
2134 const Sized_relobj
<32, big_endian
>* object
,
2135 const Symbol_value
<32>* psymval
,
2136 Arm_address address
)
2138 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2139 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2140 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2141 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2142 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2143 val
= This::insert_val_arm_movw_movt(val
, x
);
2144 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2145 return This::STATUS_OKAY
;
2148 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2149 static inline typename
This::Status
2150 thm_movw_prel_nc(unsigned char *view
,
2151 const Sized_relobj
<32, big_endian
>* object
,
2152 const Symbol_value
<32>* psymval
,
2153 Arm_address address
,
2154 Arm_address thumb_bit
)
2156 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2157 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2158 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2159 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2160 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2161 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2162 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2163 val
= This::insert_val_thumb_movw_movt(val
, x
);
2164 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2165 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2166 return This::STATUS_OKAY
;
2169 // R_ARM_THM_MOVT_PREL: S + A - P
2170 static inline typename
This::Status
2171 thm_movt_prel(unsigned char *view
,
2172 const Sized_relobj
<32, big_endian
>* object
,
2173 const Symbol_value
<32>* psymval
,
2174 Arm_address address
)
2176 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2177 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2178 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2179 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2180 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2181 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2182 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2183 val
= This::insert_val_thumb_movw_movt(val
, x
);
2184 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2185 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2186 return This::STATUS_OKAY
;
2190 // Relocate ARM long branches. This handles relocation types
2191 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2192 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2193 // undefined and we do not use PLT in this relocation. In such a case,
2194 // the branch is converted into an NOP.
2196 template<bool big_endian
>
2197 typename Arm_relocate_functions
<big_endian
>::Status
2198 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2199 unsigned int r_type
,
2200 const Relocate_info
<32, big_endian
>* relinfo
,
2201 unsigned char *view
,
2202 const Sized_symbol
<32>* gsym
,
2203 const Arm_relobj
<big_endian
>* object
,
2205 const Symbol_value
<32>* psymval
,
2206 Arm_address address
,
2207 Arm_address thumb_bit
,
2208 bool is_weakly_undefined_without_plt
)
2210 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2211 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2212 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2214 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2215 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2216 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2217 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2218 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2219 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2220 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2222 // Check that the instruction is valid.
2223 if (r_type
== elfcpp::R_ARM_CALL
)
2225 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2226 return This::STATUS_BAD_RELOC
;
2228 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2230 if (!insn_is_b
&& !insn_is_cond_bl
)
2231 return This::STATUS_BAD_RELOC
;
2233 else if (r_type
== elfcpp::R_ARM_PLT32
)
2235 if (!insn_is_any_branch
)
2236 return This::STATUS_BAD_RELOC
;
2238 else if (r_type
== elfcpp::R_ARM_XPC25
)
2240 // FIXME: AAELF document IH0044C does not say much about it other
2241 // than it being obsolete.
2242 if (!insn_is_any_branch
)
2243 return This::STATUS_BAD_RELOC
;
2248 // A branch to an undefined weak symbol is turned into a jump to
2249 // the next instruction unless a PLT entry will be created.
2250 // Do the same for local undefined symbols.
2251 // The jump to the next instruction is optimized as a NOP depending
2252 // on the architecture.
2253 const Target_arm
<big_endian
>* arm_target
=
2254 Target_arm
<big_endian
>::default_target();
2255 if (is_weakly_undefined_without_plt
)
2257 Valtype cond
= val
& 0xf0000000U
;
2258 if (arm_target
->may_use_arm_nop())
2259 val
= cond
| 0x0320f000;
2261 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2262 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2263 return This::STATUS_OKAY
;
2266 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2267 Valtype branch_target
= psymval
->value(object
, addend
);
2268 int32_t branch_offset
= branch_target
- address
;
2270 // We need a stub if the branch offset is too large or if we need
2272 bool may_use_blx
= arm_target
->may_use_blx();
2273 Reloc_stub
* stub
= NULL
;
2274 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2275 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2276 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2278 Stub_type stub_type
=
2279 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2281 if (stub_type
!= arm_stub_none
)
2283 Stub_table
<big_endian
>* stub_table
=
2284 object
->stub_table(relinfo
->data_shndx
);
2285 gold_assert(stub_table
!= NULL
);
2287 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2288 stub
= stub_table
->find_reloc_stub(stub_key
);
2289 gold_assert(stub
!= NULL
);
2290 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2291 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2292 branch_offset
= branch_target
- address
;
2293 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2294 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2298 // At this point, if we still need to switch mode, the instruction
2299 // must either be a BLX or a BL that can be converted to a BLX.
2303 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2304 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2307 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2308 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2309 return (utils::has_overflow
<26>(branch_offset
)
2310 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2313 // Relocate THUMB long branches. This handles relocation types
2314 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2315 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2316 // undefined and we do not use PLT in this relocation. In such a case,
2317 // the branch is converted into an NOP.
2319 template<bool big_endian
>
2320 typename Arm_relocate_functions
<big_endian
>::Status
2321 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2322 unsigned int r_type
,
2323 const Relocate_info
<32, big_endian
>* relinfo
,
2324 unsigned char *view
,
2325 const Sized_symbol
<32>* gsym
,
2326 const Arm_relobj
<big_endian
>* object
,
2328 const Symbol_value
<32>* psymval
,
2329 Arm_address address
,
2330 Arm_address thumb_bit
,
2331 bool is_weakly_undefined_without_plt
)
2333 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2334 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2335 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2336 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2338 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2340 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2341 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2343 // Check that the instruction is valid.
2344 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2346 if (!is_bl_insn
&& !is_blx_insn
)
2347 return This::STATUS_BAD_RELOC
;
2349 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2351 // This cannot be a BLX.
2353 return This::STATUS_BAD_RELOC
;
2355 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2357 // Check for Thumb to Thumb call.
2359 return This::STATUS_BAD_RELOC
;
2362 gold_warning(_("%s: Thumb BLX instruction targets "
2363 "thumb function '%s'."),
2364 object
->name().c_str(),
2365 (gsym
? gsym
->name() : "(local)"));
2366 // Convert BLX to BL.
2367 lower_insn
|= 0x1000U
;
2373 // A branch to an undefined weak symbol is turned into a jump to
2374 // the next instruction unless a PLT entry will be created.
2375 // The jump to the next instruction is optimized as a NOP.W for
2376 // Thumb-2 enabled architectures.
2377 const Target_arm
<big_endian
>* arm_target
=
2378 Target_arm
<big_endian
>::default_target();
2379 if (is_weakly_undefined_without_plt
)
2381 if (arm_target
->may_use_thumb2_nop())
2383 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2384 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2388 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2389 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2391 return This::STATUS_OKAY
;
2394 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2395 // with Thumb-1) involving the J1 and J2 bits.
2396 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
2397 uint32_t upper
= upper_insn
& 0x3ff;
2398 uint32_t lower
= lower_insn
& 0x7ff;
2399 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
2400 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
2401 uint32_t i1
= j1
^ s
? 0 : 1;
2402 uint32_t i2
= j2
^ s
? 0 : 1;
2404 int32_t addend
= (i1
<< 23) | (i2
<< 22) | (upper
<< 12) | (lower
<< 1);
2406 addend
= (addend
| ((s
? 0 : 1) << 24)) - (1 << 24);
2408 Arm_address branch_target
= psymval
->value(object
, addend
);
2409 int32_t branch_offset
= branch_target
- address
;
2411 // We need a stub if the branch offset is too large or if we need
2413 bool may_use_blx
= arm_target
->may_use_blx();
2414 bool thumb2
= arm_target
->using_thumb2();
2416 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2417 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2419 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2420 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2421 || ((thumb_bit
== 0)
2422 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2423 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2425 Stub_type stub_type
=
2426 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2428 if (stub_type
!= arm_stub_none
)
2430 Stub_table
<big_endian
>* stub_table
=
2431 object
->stub_table(relinfo
->data_shndx
);
2432 gold_assert(stub_table
!= NULL
);
2434 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2435 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
2436 gold_assert(stub
!= NULL
);
2437 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2438 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2439 branch_offset
= branch_target
- address
;
2443 // At this point, if we still need to switch mode, the instruction
2444 // must either be a BLX or a BL that can be converted to a BLX.
2447 gold_assert(may_use_blx
2448 && (r_type
== elfcpp::R_ARM_THM_CALL
2449 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2450 // Make sure this is a BLX.
2451 lower_insn
&= ~0x1000U
;
2455 // Make sure this is a BL.
2456 lower_insn
|= 0x1000U
;
2459 uint32_t reloc_sign
= (branch_offset
< 0) ? 1 : 0;
2460 uint32_t relocation
= static_cast<uint32_t>(branch_offset
);
2462 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2463 // For a BLX instruction, make sure that the relocation is rounded up
2464 // to a word boundary. This follows the semantics of the instruction
2465 // which specifies that bit 1 of the target address will come from bit
2466 // 1 of the base address.
2467 relocation
= (relocation
+ 2U) & ~3U;
2469 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2470 // We use the Thumb-2 encoding, which is safe even if dealing with
2471 // a Thumb-1 instruction by virtue of our overflow check above. */
2472 upper_insn
= (upper_insn
& ~0x7ffU
)
2473 | ((relocation
>> 12) & 0x3ffU
)
2474 | (reloc_sign
<< 10);
2475 lower_insn
= (lower_insn
& ~0x2fffU
)
2476 | (((!((relocation
>> 23) & 1U)) ^ reloc_sign
) << 13)
2477 | (((!((relocation
>> 22) & 1U)) ^ reloc_sign
) << 11)
2478 | ((relocation
>> 1) & 0x7ffU
);
2480 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2481 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2484 ? utils::has_overflow
<25>(relocation
)
2485 : utils::has_overflow
<23>(relocation
))
2486 ? This::STATUS_OVERFLOW
2487 : This::STATUS_OKAY
);
2490 // Get the GOT section, creating it if necessary.
2492 template<bool big_endian
>
2493 Output_data_got
<32, big_endian
>*
2494 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2496 if (this->got_
== NULL
)
2498 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2500 this->got_
= new Output_data_got
<32, big_endian
>();
2503 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2505 | elfcpp::SHF_WRITE
),
2509 // The old GNU linker creates a .got.plt section. We just
2510 // create another set of data in the .got section. Note that we
2511 // always create a PLT if we create a GOT, although the PLT
2513 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2514 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2516 | elfcpp::SHF_WRITE
),
2517 this->got_plt_
, false);
2520 // The first three entries are reserved.
2521 this->got_plt_
->set_current_data_size(3 * 4);
2523 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2524 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2526 0, 0, elfcpp::STT_OBJECT
,
2528 elfcpp::STV_HIDDEN
, 0,
2534 // Get the dynamic reloc section, creating it if necessary.
2536 template<bool big_endian
>
2537 typename Target_arm
<big_endian
>::Reloc_section
*
2538 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2540 if (this->rel_dyn_
== NULL
)
2542 gold_assert(layout
!= NULL
);
2543 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2544 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2545 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true);
2547 return this->rel_dyn_
;
2550 // Insn_template methods.
2552 // Return byte size of an instruction template.
2555 Insn_template::size() const
2557 switch (this->type())
2570 // Return alignment of an instruction template.
2573 Insn_template::alignment() const
2575 switch (this->type())
2588 // Stub_template methods.
2590 Stub_template::Stub_template(
2591 Stub_type type
, const Insn_template
* insns
,
2593 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2594 entry_in_thumb_mode_(false), relocs_()
2598 // Compute byte size and alignment of stub template.
2599 for (size_t i
= 0; i
< insn_count
; i
++)
2601 unsigned insn_alignment
= insns
[i
].alignment();
2602 size_t insn_size
= insns
[i
].size();
2603 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2604 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2605 switch (insns
[i
].type())
2607 case Insn_template::THUMB16_TYPE
:
2609 this->entry_in_thumb_mode_
= true;
2612 case Insn_template::THUMB32_TYPE
:
2613 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2614 this->relocs_
.push_back(Reloc(i
, offset
));
2616 this->entry_in_thumb_mode_
= true;
2619 case Insn_template::ARM_TYPE
:
2620 // Handle cases where the target is encoded within the
2622 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2623 this->relocs_
.push_back(Reloc(i
, offset
));
2626 case Insn_template::DATA_TYPE
:
2627 // Entry point cannot be data.
2628 gold_assert(i
!= 0);
2629 this->relocs_
.push_back(Reloc(i
, offset
));
2635 offset
+= insn_size
;
2637 this->size_
= offset
;
2640 // Reloc_stub::Key methods.
2642 // Dump a Key as a string for debugging.
2645 Reloc_stub::Key::name() const
2647 if (this->r_sym_
== invalid_index
)
2649 // Global symbol key name
2650 // <stub-type>:<symbol name>:<addend>.
2651 const std::string sym_name
= this->u_
.symbol
->name();
2652 // We need to print two hex number and two colons. So just add 100 bytes
2653 // to the symbol name size.
2654 size_t len
= sym_name
.size() + 100;
2655 char* buffer
= new char[len
];
2656 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2657 sym_name
.c_str(), this->addend_
);
2658 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2660 return std::string(buffer
);
2664 // local symbol key name
2665 // <stub-type>:<object>:<r_sym>:<addend>.
2666 const size_t len
= 200;
2668 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2669 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2670 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2671 return std::string(buffer
);
2675 // Reloc_stub methods.
2677 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2678 // LOCATION to DESTINATION.
2679 // This code is based on the arm_type_of_stub function in
2680 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2684 Reloc_stub::stub_type_for_reloc(
2685 unsigned int r_type
,
2686 Arm_address location
,
2687 Arm_address destination
,
2688 bool target_is_thumb
)
2690 Stub_type stub_type
= arm_stub_none
;
2692 // This is a bit ugly but we want to avoid using a templated class for
2693 // big and little endianities.
2695 bool should_force_pic_veneer
;
2698 if (parameters
->target().is_big_endian())
2700 const Target_arm
<true>* big_endian_target
=
2701 Target_arm
<true>::default_target();
2702 may_use_blx
= big_endian_target
->may_use_blx();
2703 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
2704 thumb2
= big_endian_target
->using_thumb2();
2705 thumb_only
= big_endian_target
->using_thumb_only();
2709 const Target_arm
<false>* little_endian_target
=
2710 Target_arm
<false>::default_target();
2711 may_use_blx
= little_endian_target
->may_use_blx();
2712 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
2713 thumb2
= little_endian_target
->using_thumb2();
2714 thumb_only
= little_endian_target
->using_thumb_only();
2717 int64_t branch_offset
= (int64_t)destination
- location
;
2719 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2721 // Handle cases where:
2722 // - this call goes too far (different Thumb/Thumb2 max
2724 // - it's a Thumb->Arm call and blx is not available, or it's a
2725 // Thumb->Arm branch (not bl). A stub is needed in this case.
2727 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2728 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2730 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2731 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2732 || ((!target_is_thumb
)
2733 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2734 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2736 if (target_is_thumb
)
2741 stub_type
= (parameters
->options().shared()
2742 || should_force_pic_veneer
)
2745 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2746 // V5T and above. Stub starts with ARM code, so
2747 // we must be able to switch mode before
2748 // reaching it, which is only possible for 'bl'
2749 // (ie R_ARM_THM_CALL relocation).
2750 ? arm_stub_long_branch_any_thumb_pic
2751 // On V4T, use Thumb code only.
2752 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2756 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2757 ? arm_stub_long_branch_any_any
// V5T and above.
2758 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2762 stub_type
= (parameters
->options().shared()
2763 || should_force_pic_veneer
)
2764 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2765 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2772 // FIXME: We should check that the input section is from an
2773 // object that has interwork enabled.
2775 stub_type
= (parameters
->options().shared()
2776 || should_force_pic_veneer
)
2779 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2780 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2781 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2785 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2786 ? arm_stub_long_branch_any_any
// V5T and above.
2787 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2789 // Handle v4t short branches.
2790 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2791 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2792 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2793 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2797 else if (r_type
== elfcpp::R_ARM_CALL
2798 || r_type
== elfcpp::R_ARM_JUMP24
2799 || r_type
== elfcpp::R_ARM_PLT32
)
2801 if (target_is_thumb
)
2805 // FIXME: We should check that the input section is from an
2806 // object that has interwork enabled.
2808 // We have an extra 2-bytes reach because of
2809 // the mode change (bit 24 (H) of BLX encoding).
2810 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2811 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2812 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2813 || (r_type
== elfcpp::R_ARM_JUMP24
)
2814 || (r_type
== elfcpp::R_ARM_PLT32
))
2816 stub_type
= (parameters
->options().shared()
2817 || should_force_pic_veneer
)
2820 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2821 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2825 ? arm_stub_long_branch_any_any
// V5T and above.
2826 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2832 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2833 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2835 stub_type
= (parameters
->options().shared()
2836 || should_force_pic_veneer
)
2837 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2838 : arm_stub_long_branch_any_any
; /// non-PIC.
2846 // Template to implement do_write for a specific target endianity.
2848 template<bool big_endian
>
2850 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2851 section_size_type view_size
)
2853 const Stub_template
* stub_template
= this->stub_template();
2854 const Insn_template
* insns
= stub_template
->insns();
2856 // FIXME: We do not handle BE8 encoding yet.
2857 unsigned char* pov
= view
;
2858 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2860 switch (insns
[i
].type())
2862 case Insn_template::THUMB16_TYPE
:
2863 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2864 gold_assert(insns
[i
].reloc_addend() == 0);
2865 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2867 case Insn_template::THUMB32_TYPE
:
2869 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2870 uint32_t lo
= insns
[i
].data() & 0xffff;
2871 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2872 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2875 case Insn_template::ARM_TYPE
:
2876 case Insn_template::DATA_TYPE
:
2877 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2882 pov
+= insns
[i
].size();
2884 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2887 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2890 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2894 this->do_fixed_endian_write
<true>(view
, view_size
);
2896 this->do_fixed_endian_write
<false>(view
, view_size
);
2899 // Stub_factory methods.
2901 Stub_factory::Stub_factory()
2903 // The instruction template sequences are declared as static
2904 // objects and initialized first time the constructor runs.
2906 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2907 // to reach the stub if necessary.
2908 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2910 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2911 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2912 // dcd R_ARM_ABS32(X)
2915 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2917 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
2919 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2920 Insn_template::arm_insn(0xe12fff1c), // bx ip
2921 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2922 // dcd R_ARM_ABS32(X)
2925 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2926 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
2928 Insn_template::thumb16_insn(0xb401), // push {r0}
2929 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2930 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2931 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2932 Insn_template::thumb16_insn(0x4760), // bx ip
2933 Insn_template::thumb16_insn(0xbf00), // nop
2934 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2935 // dcd R_ARM_ABS32(X)
2938 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2940 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
2942 Insn_template::thumb16_insn(0x4778), // bx pc
2943 Insn_template::thumb16_insn(0x46c0), // nop
2944 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2945 Insn_template::arm_insn(0xe12fff1c), // bx ip
2946 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2947 // dcd R_ARM_ABS32(X)
2950 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2952 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
2954 Insn_template::thumb16_insn(0x4778), // bx pc
2955 Insn_template::thumb16_insn(0x46c0), // nop
2956 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2957 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2958 // dcd R_ARM_ABS32(X)
2961 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2962 // one, when the destination is close enough.
2963 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
2965 Insn_template::thumb16_insn(0x4778), // bx pc
2966 Insn_template::thumb16_insn(0x46c0), // nop
2967 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2970 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2971 // blx to reach the stub if necessary.
2972 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
2974 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2975 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2976 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2977 // dcd R_ARM_REL32(X-4)
2980 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2981 // blx to reach the stub if necessary. We can not add into pc;
2982 // it is not guaranteed to mode switch (different in ARMv6 and
2984 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
2986 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2987 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2988 Insn_template::arm_insn(0xe12fff1c), // bx ip
2989 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2990 // dcd R_ARM_REL32(X)
2993 // V4T ARM -> ARM long branch stub, PIC.
2994 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
2996 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2997 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2998 Insn_template::arm_insn(0xe12fff1c), // bx ip
2999 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3000 // dcd R_ARM_REL32(X)
3003 // V4T Thumb -> ARM long branch stub, PIC.
3004 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3006 Insn_template::thumb16_insn(0x4778), // bx pc
3007 Insn_template::thumb16_insn(0x46c0), // nop
3008 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3009 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3010 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3011 // dcd R_ARM_REL32(X)
3014 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3016 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3018 Insn_template::thumb16_insn(0xb401), // push {r0}
3019 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3020 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3021 Insn_template::thumb16_insn(0x4484), // add ip, r0
3022 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3023 Insn_template::thumb16_insn(0x4760), // bx ip
3024 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3025 // dcd R_ARM_REL32(X)
3028 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3030 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3032 Insn_template::thumb16_insn(0x4778), // bx pc
3033 Insn_template::thumb16_insn(0x46c0), // nop
3034 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3035 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3036 Insn_template::arm_insn(0xe12fff1c), // bx ip
3037 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3038 // dcd R_ARM_REL32(X)
3041 // Cortex-A8 erratum-workaround stubs.
3043 // Stub used for conditional branches (which may be beyond +/-1MB away,
3044 // so we can't use a conditional branch to reach this stub).
3051 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3053 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3054 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3055 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3059 // Stub used for b.w and bl.w instructions.
3061 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3063 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3066 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3068 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3071 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3072 // instruction (which switches to ARM mode) to point to this stub. Jump to
3073 // the real destination using an ARM-mode branch.
3074 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3076 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3079 // Fill in the stub template look-up table. Stub templates are constructed
3080 // per instance of Stub_factory for fast look-up without locking
3081 // in a thread-enabled environment.
3083 this->stub_templates_
[arm_stub_none
] =
3084 new Stub_template(arm_stub_none
, NULL
, 0);
3086 #define DEF_STUB(x) \
3090 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3091 Stub_type type = arm_stub_##x; \
3092 this->stub_templates_[type] = \
3093 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3101 // Stub_table methods.
3103 // Add a STUB with using KEY. Caller is reponsible for avoid adding
3104 // if already a STUB with the same key has been added.
3106 template<bool big_endian
>
3108 Stub_table
<big_endian
>::add_reloc_stub(
3110 const Reloc_stub::Key
& key
)
3112 const Stub_template
* stub_template
= stub
->stub_template();
3113 gold_assert(stub_template
->type() == key
.stub_type());
3114 this->reloc_stubs_
[key
] = stub
;
3115 if (this->addralign_
< stub_template
->alignment())
3116 this->addralign_
= stub_template
->alignment();
3117 this->has_been_changed_
= true;
3120 template<bool big_endian
>
3122 Stub_table
<big_endian
>::relocate_stubs(
3123 const Relocate_info
<32, big_endian
>* relinfo
,
3124 Target_arm
<big_endian
>* arm_target
,
3125 Output_section
* output_section
,
3126 unsigned char* view
,
3127 Arm_address address
,
3128 section_size_type view_size
)
3130 // If we are passed a view bigger than the stub table's. we need to
3132 gold_assert(address
== this->address()
3134 == static_cast<section_size_type
>(this->data_size())));
3136 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3137 p
!= this->reloc_stubs_
.end();
3140 Reloc_stub
* stub
= p
->second
;
3141 const Stub_template
* stub_template
= stub
->stub_template();
3142 if (stub_template
->reloc_count() != 0)
3144 // Adjust view to cover the stub only.
3145 section_size_type offset
= stub
->offset();
3146 section_size_type stub_size
= stub_template
->size();
3147 gold_assert(offset
+ stub_size
<= view_size
);
3149 arm_target
->relocate_stub(stub
, relinfo
, output_section
,
3150 view
+ offset
, address
+ offset
,
3156 // Reset address and file offset.
3158 template<bool big_endian
>
3160 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
3163 uint64_t max_addralign
= 1;
3164 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3165 p
!= this->reloc_stubs_
.end();
3168 Reloc_stub
* stub
= p
->second
;
3169 const Stub_template
* stub_template
= stub
->stub_template();
3170 uint64_t stub_addralign
= stub_template
->alignment();
3171 max_addralign
= std::max(max_addralign
, stub_addralign
);
3172 off
= align_address(off
, stub_addralign
);
3173 stub
->set_offset(off
);
3174 stub
->reset_destination_address();
3175 off
+= stub_template
->size();
3178 this->addralign_
= max_addralign
;
3179 this->set_current_data_size_for_child(off
);
3182 // Write out the stubs to file.
3184 template<bool big_endian
>
3186 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3188 off_t offset
= this->offset();
3189 const section_size_type oview_size
=
3190 convert_to_section_size_type(this->data_size());
3191 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3193 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3194 p
!= this->reloc_stubs_
.end();
3197 Reloc_stub
* stub
= p
->second
;
3198 Arm_address address
= this->address() + stub
->offset();
3200 == align_address(address
,
3201 stub
->stub_template()->alignment()));
3202 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3205 of
->write_output_view(this->offset(), oview_size
, oview
);
3208 // Arm_input_section methods.
3210 // Initialize an Arm_input_section.
3212 template<bool big_endian
>
3214 Arm_input_section
<big_endian
>::init()
3216 Relobj
* relobj
= this->relobj();
3217 unsigned int shndx
= this->shndx();
3219 // Cache these to speed up size and alignment queries. It is too slow
3220 // to call section_addraglin and section_size every time.
3221 this->original_addralign_
= relobj
->section_addralign(shndx
);
3222 this->original_size_
= relobj
->section_size(shndx
);
3224 // We want to make this look like the original input section after
3225 // output sections are finalized.
3226 Output_section
* os
= relobj
->output_section(shndx
);
3227 off_t offset
= relobj
->output_section_offset(shndx
);
3228 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3229 this->set_address(os
->address() + offset
);
3230 this->set_file_offset(os
->offset() + offset
);
3232 this->set_current_data_size(this->original_size_
);
3233 this->finalize_data_size();
3236 template<bool big_endian
>
3238 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3240 // We have to write out the original section content.
3241 section_size_type section_size
;
3242 const unsigned char* section_contents
=
3243 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3244 of
->write(this->offset(), section_contents
, section_size
);
3246 // If this owns a stub table and it is not empty, write it.
3247 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3248 this->stub_table_
->write(of
);
3251 // Finalize data size.
3253 template<bool big_endian
>
3255 Arm_input_section
<big_endian
>::set_final_data_size()
3257 // If this owns a stub table, finalize its data size as well.
3258 if (this->is_stub_table_owner())
3260 uint64_t address
= this->address();
3262 // The stub table comes after the original section contents.
3263 address
+= this->original_size_
;
3264 address
= align_address(address
, this->stub_table_
->addralign());
3265 off_t offset
= this->offset() + (address
- this->address());
3266 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3267 address
+= this->stub_table_
->data_size();
3268 gold_assert(address
== this->address() + this->current_data_size());
3271 this->set_data_size(this->current_data_size());
3274 // Reset address and file offset.
3276 template<bool big_endian
>
3278 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3280 // Size of the original input section contents.
3281 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3283 // If this is a stub table owner, account for the stub table size.
3284 if (this->is_stub_table_owner())
3286 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3288 // Reset the stub table's address and file offset. The
3289 // current data size for child will be updated after that.
3290 stub_table_
->reset_address_and_file_offset();
3291 off
= align_address(off
, stub_table_
->addralign());
3292 off
+= stub_table
->current_data_size();
3295 this->set_current_data_size(off
);
3298 // Arm_output_section methods.
3300 // Create a stub group for input sections from BEGIN to END. OWNER
3301 // points to the input section to be the owner a new stub table.
3303 template<bool big_endian
>
3305 Arm_output_section
<big_endian
>::create_stub_group(
3306 Input_section_list::const_iterator begin
,
3307 Input_section_list::const_iterator end
,
3308 Input_section_list::const_iterator owner
,
3309 Target_arm
<big_endian
>* target
,
3310 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3312 // Currently we convert ordinary input sections into relaxed sections only
3313 // at this point but we may want to support creating relaxed input section
3314 // very early. So we check here to see if owner is already a relaxed
3317 Arm_input_section
<big_endian
>* arm_input_section
;
3318 if (owner
->is_relaxed_input_section())
3321 Arm_input_section
<big_endian
>::as_arm_input_section(
3322 owner
->relaxed_input_section());
3326 gold_assert(owner
->is_input_section());
3327 // Create a new relaxed input section.
3329 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3330 new_relaxed_sections
->push_back(arm_input_section
);
3333 // Create a stub table.
3334 Stub_table
<big_endian
>* stub_table
=
3335 target
->new_stub_table(arm_input_section
);
3337 arm_input_section
->set_stub_table(stub_table
);
3339 Input_section_list::const_iterator p
= begin
;
3340 Input_section_list::const_iterator prev_p
;
3342 // Look for input sections or relaxed input sections in [begin ... end].
3345 if (p
->is_input_section() || p
->is_relaxed_input_section())
3347 // The stub table information for input sections live
3348 // in their objects.
3349 Arm_relobj
<big_endian
>* arm_relobj
=
3350 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3351 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3355 while (prev_p
!= end
);
3358 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3359 // of stub groups. We grow a stub group by adding input section until the
3360 // size is just below GROUP_SIZE. The last input section will be converted
3361 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3362 // input section after the stub table, effectively double the group size.
3364 // This is similar to the group_sections() function in elf32-arm.c but is
3365 // implemented differently.
3367 template<bool big_endian
>
3369 Arm_output_section
<big_endian
>::group_sections(
3370 section_size_type group_size
,
3371 bool stubs_always_after_branch
,
3372 Target_arm
<big_endian
>* target
)
3374 // We only care about sections containing code.
3375 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3378 // States for grouping.
3381 // No group is being built.
3383 // A group is being built but the stub table is not found yet.
3384 // We keep group a stub group until the size is just under GROUP_SIZE.
3385 // The last input section in the group will be used as the stub table.
3386 FINDING_STUB_SECTION
,
3387 // A group is being built and we have already found a stub table.
3388 // We enter this state to grow a stub group by adding input section
3389 // after the stub table. This effectively doubles the group size.
3393 // Any newly created relaxed sections are stored here.
3394 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3396 State state
= NO_GROUP
;
3397 section_size_type off
= 0;
3398 section_size_type group_begin_offset
= 0;
3399 section_size_type group_end_offset
= 0;
3400 section_size_type stub_table_end_offset
= 0;
3401 Input_section_list::const_iterator group_begin
=
3402 this->input_sections().end();
3403 Input_section_list::const_iterator stub_table
=
3404 this->input_sections().end();
3405 Input_section_list::const_iterator group_end
= this->input_sections().end();
3406 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3407 p
!= this->input_sections().end();
3410 section_size_type section_begin_offset
=
3411 align_address(off
, p
->addralign());
3412 section_size_type section_end_offset
=
3413 section_begin_offset
+ p
->data_size();
3415 // Check to see if we should group the previously seens sections.
3421 case FINDING_STUB_SECTION
:
3422 // Adding this section makes the group larger than GROUP_SIZE.
3423 if (section_end_offset
- group_begin_offset
>= group_size
)
3425 if (stubs_always_after_branch
)
3427 gold_assert(group_end
!= this->input_sections().end());
3428 this->create_stub_group(group_begin
, group_end
, group_end
,
3429 target
, &new_relaxed_sections
);
3434 // But wait, there's more! Input sections up to
3435 // stub_group_size bytes after the stub table can be
3436 // handled by it too.
3437 state
= HAS_STUB_SECTION
;
3438 stub_table
= group_end
;
3439 stub_table_end_offset
= group_end_offset
;
3444 case HAS_STUB_SECTION
:
3445 // Adding this section makes the post stub-section group larger
3447 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3449 gold_assert(group_end
!= this->input_sections().end());
3450 this->create_stub_group(group_begin
, group_end
, stub_table
,
3451 target
, &new_relaxed_sections
);
3460 // If we see an input section and currently there is no group, start
3461 // a new one. Skip any empty sections.
3462 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3463 && (p
->relobj()->section_size(p
->shndx()) != 0))
3465 if (state
== NO_GROUP
)
3467 state
= FINDING_STUB_SECTION
;
3469 group_begin_offset
= section_begin_offset
;
3472 // Keep track of the last input section seen.
3474 group_end_offset
= section_end_offset
;
3477 off
= section_end_offset
;
3480 // Create a stub group for any ungrouped sections.
3481 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3483 gold_assert(group_end
!= this->input_sections().end());
3484 this->create_stub_group(group_begin
, group_end
,
3485 (state
== FINDING_STUB_SECTION
3488 target
, &new_relaxed_sections
);
3491 // Convert input section into relaxed input section in a batch.
3492 if (!new_relaxed_sections
.empty())
3493 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3495 // Update the section offsets
3496 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3498 Arm_relobj
<big_endian
>* arm_relobj
=
3499 Arm_relobj
<big_endian
>::as_arm_relobj(
3500 new_relaxed_sections
[i
]->relobj());
3501 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3502 // Tell Arm_relobj that this input section is converted.
3503 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3507 // Arm_relobj methods.
3509 // Scan relocations for stub generation.
3511 template<bool big_endian
>
3513 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3514 Target_arm
<big_endian
>* arm_target
,
3515 const Symbol_table
* symtab
,
3516 const Layout
* layout
)
3518 unsigned int shnum
= this->shnum();
3519 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3521 // Read the section headers.
3522 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3526 // To speed up processing, we set up hash tables for fast lookup of
3527 // input offsets to output addresses.
3528 this->initialize_input_to_output_maps();
3530 const Relobj::Output_sections
& out_sections(this->output_sections());
3532 Relocate_info
<32, big_endian
> relinfo
;
3533 relinfo
.symtab
= symtab
;
3534 relinfo
.layout
= layout
;
3535 relinfo
.object
= this;
3537 const unsigned char* p
= pshdrs
+ shdr_size
;
3538 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3540 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3542 unsigned int sh_type
= shdr
.get_sh_type();
3543 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3546 off_t sh_size
= shdr
.get_sh_size();
3550 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3551 if (index
>= this->shnum())
3553 // Ignore reloc section with bad info. This error will be
3554 // reported in the final link.
3558 Output_section
* os
= out_sections
[index
];
3561 // This relocation section is against a section which we
3565 Arm_address output_offset
= this->get_output_section_offset(index
);
3567 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
3569 // Ignore reloc section with unexpected symbol table. The
3570 // error will be reported in the final link.
3574 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
3575 sh_size
, true, false);
3577 unsigned int reloc_size
;
3578 if (sh_type
== elfcpp::SHT_REL
)
3579 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3581 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
3583 if (reloc_size
!= shdr
.get_sh_entsize())
3585 // Ignore reloc section with unexpected entsize. The error
3586 // will be reported in the final link.
3590 size_t reloc_count
= sh_size
/ reloc_size
;
3591 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
3593 // Ignore reloc section with uneven size. The error will be
3594 // reported in the final link.
3598 gold_assert(output_offset
!= invalid_address
3599 || this->relocs_must_follow_section_writes());
3601 // Get the section contents. This does work for the case in which
3602 // we modify the contents of an input section. We need to pass the
3603 // output view under such circumstances.
3604 section_size_type input_view_size
= 0;
3605 const unsigned char* input_view
=
3606 this->section_contents(index
, &input_view_size
, false);
3608 relinfo
.reloc_shndx
= i
;
3609 relinfo
.data_shndx
= index
;
3610 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
3612 output_offset
== invalid_address
,
3618 // After we've done the relocations, we release the hash tables,
3619 // since we no longer need them.
3620 this->free_input_to_output_maps();
3623 // Count the local symbols. The ARM backend needs to know if a symbol
3624 // is a THUMB function or not. For global symbols, it is easy because
3625 // the Symbol object keeps the ELF symbol type. For local symbol it is
3626 // harder because we cannot access this information. So we override the
3627 // do_count_local_symbol in parent and scan local symbols to mark
3628 // THUMB functions. This is not the most efficient way but I do not want to
3629 // slow down other ports by calling a per symbol targer hook inside
3630 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3632 template<bool big_endian
>
3634 Arm_relobj
<big_endian
>::do_count_local_symbols(
3635 Stringpool_template
<char>* pool
,
3636 Stringpool_template
<char>* dynpool
)
3638 // We need to fix-up the values of any local symbols whose type are
3641 // Ask parent to count the local symbols.
3642 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
3643 const unsigned int loccount
= this->local_symbol_count();
3647 // Intialize the thumb function bit-vector.
3648 std::vector
<bool> empty_vector(loccount
, false);
3649 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
3651 // Read the symbol table section header.
3652 const unsigned int symtab_shndx
= this->symtab_shndx();
3653 elfcpp::Shdr
<32, big_endian
>
3654 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
3655 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
3657 // Read the local symbols.
3658 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
3659 gold_assert(loccount
== symtabshdr
.get_sh_info());
3660 off_t locsize
= loccount
* sym_size
;
3661 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
3662 locsize
, true, true);
3664 // Loop over the local symbols and mark any local symbols pointing
3665 // to THUMB functions.
3667 // Skip the first dummy symbol.
3669 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
3670 this->local_values();
3671 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
3673 elfcpp::Sym
<32, big_endian
> sym(psyms
);
3674 elfcpp::STT st_type
= sym
.get_st_type();
3675 Symbol_value
<32>& lv((*plocal_values
)[i
]);
3676 Arm_address input_value
= lv
.input_value();
3678 if (st_type
== elfcpp::STT_ARM_TFUNC
3679 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
3681 // This is a THUMB function. Mark this and canonicalize the
3682 // symbol value by setting LSB.
3683 this->local_symbol_is_thumb_function_
[i
] = true;
3684 if ((input_value
& 1) == 0)
3685 lv
.set_input_value(input_value
| 1);
3690 // Relocate sections.
3691 template<bool big_endian
>
3693 Arm_relobj
<big_endian
>::do_relocate_sections(
3694 const Symbol_table
* symtab
,
3695 const Layout
* layout
,
3696 const unsigned char* pshdrs
,
3697 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
3699 // Call parent to relocate sections.
3700 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
3703 // We do not generate stubs if doing a relocatable link.
3704 if (parameters
->options().relocatable())
3707 // Relocate stub tables.
3708 unsigned int shnum
= this->shnum();
3710 Target_arm
<big_endian
>* arm_target
=
3711 Target_arm
<big_endian
>::default_target();
3713 Relocate_info
<32, big_endian
> relinfo
;
3714 relinfo
.symtab
= symtab
;
3715 relinfo
.layout
= layout
;
3716 relinfo
.object
= this;
3718 for (unsigned int i
= 1; i
< shnum
; ++i
)
3720 Arm_input_section
<big_endian
>* arm_input_section
=
3721 arm_target
->find_arm_input_section(this, i
);
3723 if (arm_input_section
== NULL
3724 || !arm_input_section
->is_stub_table_owner()
3725 || arm_input_section
->stub_table()->empty())
3728 // We cannot discard a section if it owns a stub table.
3729 Output_section
* os
= this->output_section(i
);
3730 gold_assert(os
!= NULL
);
3732 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
3733 relinfo
.reloc_shdr
= NULL
;
3734 relinfo
.data_shndx
= i
;
3735 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
3737 gold_assert((*pviews
)[i
].view
!= NULL
);
3739 // We are passed the output section view. Adjust it to cover the
3741 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
3742 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
3743 && ((stub_table
->address() + stub_table
->data_size())
3744 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
3746 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
3747 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
3748 Arm_address address
= stub_table
->address();
3749 section_size_type view_size
= stub_table
->data_size();
3751 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
3756 // Read the symbol information.
3758 template<bool big_endian
>
3760 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3762 // Call parent class to read symbol information.
3763 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
3765 // Read processor-specific flags in ELF file header.
3766 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3767 elfcpp::Elf_sizes
<32>::ehdr_size
,
3769 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3770 this->processor_specific_flags_
= ehdr
.get_e_flags();
3773 // Arm_dynobj methods.
3775 // Read the symbol information.
3777 template<bool big_endian
>
3779 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3781 // Call parent class to read symbol information.
3782 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
3784 // Read processor-specific flags in ELF file header.
3785 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3786 elfcpp::Elf_sizes
<32>::ehdr_size
,
3788 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3789 this->processor_specific_flags_
= ehdr
.get_e_flags();
3792 // Stub_addend_reader methods.
3794 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3796 template<bool big_endian
>
3797 elfcpp::Elf_types
<32>::Elf_Swxword
3798 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
3799 unsigned int r_type
,
3800 const unsigned char* view
,
3801 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
3805 case elfcpp::R_ARM_CALL
:
3806 case elfcpp::R_ARM_JUMP24
:
3807 case elfcpp::R_ARM_PLT32
:
3809 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3810 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3811 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3812 return utils::sign_extend
<26>(val
<< 2);
3815 case elfcpp::R_ARM_THM_CALL
:
3816 case elfcpp::R_ARM_THM_JUMP24
:
3817 case elfcpp::R_ARM_THM_XPC22
:
3819 // Fetch the addend. We use the Thumb-2 encoding (backwards
3820 // compatible with Thumb-1) involving the J1 and J2 bits.
3821 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3822 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3823 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3824 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3826 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
3827 uint32_t upper
= upper_insn
& 0x3ff;
3828 uint32_t lower
= lower_insn
& 0x7ff;
3829 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
3830 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
3831 uint32_t i1
= j1
^ s
? 0 : 1;
3832 uint32_t i2
= j2
^ s
? 0 : 1;
3834 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3835 | (upper
<< 12) | (lower
<< 1));
3838 case elfcpp::R_ARM_THM_JUMP19
:
3840 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3841 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3842 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3843 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3845 // Reconstruct the top three bits and squish the two 11 bit pieces
3847 uint32_t S
= (upper_insn
& 0x0400) >> 10;
3848 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
3849 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
3851 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
3852 uint32_t lower
= (lower_insn
& 0x07ff);
3853 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
3861 // A class to handle the PLT data.
3863 template<bool big_endian
>
3864 class Output_data_plt_arm
: public Output_section_data
3867 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
3870 Output_data_plt_arm(Layout
*, Output_data_space
*);
3872 // Add an entry to the PLT.
3874 add_entry(Symbol
* gsym
);
3876 // Return the .rel.plt section data.
3877 const Reloc_section
*
3879 { return this->rel_
; }
3883 do_adjust_output_section(Output_section
* os
);
3885 // Write to a map file.
3887 do_print_to_mapfile(Mapfile
* mapfile
) const
3888 { mapfile
->print_output_data(this, _("** PLT")); }
3891 // Template for the first PLT entry.
3892 static const uint32_t first_plt_entry
[5];
3894 // Template for subsequent PLT entries.
3895 static const uint32_t plt_entry
[3];
3897 // Set the final size.
3899 set_final_data_size()
3901 this->set_data_size(sizeof(first_plt_entry
)
3902 + this->count_
* sizeof(plt_entry
));
3905 // Write out the PLT data.
3907 do_write(Output_file
*);
3909 // The reloc section.
3910 Reloc_section
* rel_
;
3911 // The .got.plt section.
3912 Output_data_space
* got_plt_
;
3913 // The number of PLT entries.
3914 unsigned int count_
;
3917 // Create the PLT section. The ordinary .got section is an argument,
3918 // since we need to refer to the start. We also create our own .got
3919 // section just for PLT entries.
3921 template<bool big_endian
>
3922 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
3923 Output_data_space
* got_plt
)
3924 : Output_section_data(4), got_plt_(got_plt
), count_(0)
3926 this->rel_
= new Reloc_section(false);
3927 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
3928 elfcpp::SHF_ALLOC
, this->rel_
, true);
3931 template<bool big_endian
>
3933 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
3938 // Add an entry to the PLT.
3940 template<bool big_endian
>
3942 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
3944 gold_assert(!gsym
->has_plt_offset());
3946 // Note that when setting the PLT offset we skip the initial
3947 // reserved PLT entry.
3948 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
3949 + sizeof(first_plt_entry
));
3953 section_offset_type got_offset
= this->got_plt_
->current_data_size();
3955 // Every PLT entry needs a GOT entry which points back to the PLT
3956 // entry (this will be changed by the dynamic linker, normally
3957 // lazily when the function is called).
3958 this->got_plt_
->set_current_data_size(got_offset
+ 4);
3960 // Every PLT entry needs a reloc.
3961 gsym
->set_needs_dynsym_entry();
3962 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
3965 // Note that we don't need to save the symbol. The contents of the
3966 // PLT are independent of which symbols are used. The symbols only
3967 // appear in the relocations.
3971 // FIXME: This is not very flexible. Right now this has only been tested
3972 // on armv5te. If we are to support additional architecture features like
3973 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3975 // The first entry in the PLT.
3976 template<bool big_endian
>
3977 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
3979 0xe52de004, // str lr, [sp, #-4]!
3980 0xe59fe004, // ldr lr, [pc, #4]
3981 0xe08fe00e, // add lr, pc, lr
3982 0xe5bef008, // ldr pc, [lr, #8]!
3983 0x00000000, // &GOT[0] - .
3986 // Subsequent entries in the PLT.
3988 template<bool big_endian
>
3989 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
3991 0xe28fc600, // add ip, pc, #0xNN00000
3992 0xe28cca00, // add ip, ip, #0xNN000
3993 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3996 // Write out the PLT. This uses the hand-coded instructions above,
3997 // and adjusts them as needed. This is all specified by the arm ELF
3998 // Processor Supplement.
4000 template<bool big_endian
>
4002 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4004 const off_t offset
= this->offset();
4005 const section_size_type oview_size
=
4006 convert_to_section_size_type(this->data_size());
4007 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4009 const off_t got_file_offset
= this->got_plt_
->offset();
4010 const section_size_type got_size
=
4011 convert_to_section_size_type(this->got_plt_
->data_size());
4012 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4014 unsigned char* pov
= oview
;
4016 Arm_address plt_address
= this->address();
4017 Arm_address got_address
= this->got_plt_
->address();
4019 // Write first PLT entry. All but the last word are constants.
4020 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4021 / sizeof(plt_entry
[0]));
4022 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4023 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4024 // Last word in first PLT entry is &GOT[0] - .
4025 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4026 got_address
- (plt_address
+ 16));
4027 pov
+= sizeof(first_plt_entry
);
4029 unsigned char* got_pov
= got_view
;
4031 memset(got_pov
, 0, 12);
4034 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4035 unsigned int plt_offset
= sizeof(first_plt_entry
);
4036 unsigned int plt_rel_offset
= 0;
4037 unsigned int got_offset
= 12;
4038 const unsigned int count
= this->count_
;
4039 for (unsigned int i
= 0;
4042 pov
+= sizeof(plt_entry
),
4044 plt_offset
+= sizeof(plt_entry
),
4045 plt_rel_offset
+= rel_size
,
4048 // Set and adjust the PLT entry itself.
4049 int32_t offset
= ((got_address
+ got_offset
)
4050 - (plt_address
+ plt_offset
+ 8));
4052 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
4053 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
4054 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4055 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
4056 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4057 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
4058 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4060 // Set the entry in the GOT.
4061 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4064 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4065 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4067 of
->write_output_view(offset
, oview_size
, oview
);
4068 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4071 // Create a PLT entry for a global symbol.
4073 template<bool big_endian
>
4075 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
4078 if (gsym
->has_plt_offset())
4081 if (this->plt_
== NULL
)
4083 // Create the GOT sections first.
4084 this->got_section(symtab
, layout
);
4086 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
4087 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4089 | elfcpp::SHF_EXECINSTR
),
4092 this->plt_
->add_entry(gsym
);
4095 // Report an unsupported relocation against a local symbol.
4097 template<bool big_endian
>
4099 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4100 Sized_relobj
<32, big_endian
>* object
,
4101 unsigned int r_type
)
4103 gold_error(_("%s: unsupported reloc %u against local symbol"),
4104 object
->name().c_str(), r_type
);
4107 // We are about to emit a dynamic relocation of type R_TYPE. If the
4108 // dynamic linker does not support it, issue an error. The GNU linker
4109 // only issues a non-PIC error for an allocated read-only section.
4110 // Here we know the section is allocated, but we don't know that it is
4111 // read-only. But we check for all the relocation types which the
4112 // glibc dynamic linker supports, so it seems appropriate to issue an
4113 // error even if the section is not read-only.
4115 template<bool big_endian
>
4117 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4118 unsigned int r_type
)
4122 // These are the relocation types supported by glibc for ARM.
4123 case elfcpp::R_ARM_RELATIVE
:
4124 case elfcpp::R_ARM_COPY
:
4125 case elfcpp::R_ARM_GLOB_DAT
:
4126 case elfcpp::R_ARM_JUMP_SLOT
:
4127 case elfcpp::R_ARM_ABS32
:
4128 case elfcpp::R_ARM_ABS32_NOI
:
4129 case elfcpp::R_ARM_PC24
:
4130 // FIXME: The following 3 types are not supported by Android's dynamic
4132 case elfcpp::R_ARM_TLS_DTPMOD32
:
4133 case elfcpp::R_ARM_TLS_DTPOFF32
:
4134 case elfcpp::R_ARM_TLS_TPOFF32
:
4138 // This prevents us from issuing more than one error per reloc
4139 // section. But we can still wind up issuing more than one
4140 // error per object file.
4141 if (this->issued_non_pic_error_
)
4143 object
->error(_("requires unsupported dynamic reloc; "
4144 "recompile with -fPIC"));
4145 this->issued_non_pic_error_
= true;
4148 case elfcpp::R_ARM_NONE
:
4153 // Scan a relocation for a local symbol.
4154 // FIXME: This only handles a subset of relocation types used by Android
4155 // on ARM v5te devices.
4157 template<bool big_endian
>
4159 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4162 Sized_relobj
<32, big_endian
>* object
,
4163 unsigned int data_shndx
,
4164 Output_section
* output_section
,
4165 const elfcpp::Rel
<32, big_endian
>& reloc
,
4166 unsigned int r_type
,
4167 const elfcpp::Sym
<32, big_endian
>&)
4169 r_type
= get_real_reloc_type(r_type
);
4172 case elfcpp::R_ARM_NONE
:
4175 case elfcpp::R_ARM_ABS32
:
4176 case elfcpp::R_ARM_ABS32_NOI
:
4177 // If building a shared library (or a position-independent
4178 // executable), we need to create a dynamic relocation for
4179 // this location. The relocation applied at link time will
4180 // apply the link-time value, so we flag the location with
4181 // an R_ARM_RELATIVE relocation so the dynamic loader can
4182 // relocate it easily.
4183 if (parameters
->options().output_is_position_independent())
4185 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4186 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4187 // If we are to add more other reloc types than R_ARM_ABS32,
4188 // we need to add check_non_pic(object, r_type) here.
4189 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4190 output_section
, data_shndx
,
4191 reloc
.get_r_offset());
4195 case elfcpp::R_ARM_REL32
:
4196 case elfcpp::R_ARM_THM_CALL
:
4197 case elfcpp::R_ARM_CALL
:
4198 case elfcpp::R_ARM_PREL31
:
4199 case elfcpp::R_ARM_JUMP24
:
4200 case elfcpp::R_ARM_PLT32
:
4201 case elfcpp::R_ARM_THM_ABS5
:
4202 case elfcpp::R_ARM_ABS8
:
4203 case elfcpp::R_ARM_ABS12
:
4204 case elfcpp::R_ARM_ABS16
:
4205 case elfcpp::R_ARM_BASE_ABS
:
4206 case elfcpp::R_ARM_MOVW_ABS_NC
:
4207 case elfcpp::R_ARM_MOVT_ABS
:
4208 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4209 case elfcpp::R_ARM_THM_MOVT_ABS
:
4210 case elfcpp::R_ARM_MOVW_PREL_NC
:
4211 case elfcpp::R_ARM_MOVT_PREL
:
4212 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4213 case elfcpp::R_ARM_THM_MOVT_PREL
:
4216 case elfcpp::R_ARM_GOTOFF32
:
4217 // We need a GOT section:
4218 target
->got_section(symtab
, layout
);
4221 case elfcpp::R_ARM_BASE_PREL
:
4222 // FIXME: What about this?
4225 case elfcpp::R_ARM_GOT_BREL
:
4226 case elfcpp::R_ARM_GOT_PREL
:
4228 // The symbol requires a GOT entry.
4229 Output_data_got
<32, big_endian
>* got
=
4230 target
->got_section(symtab
, layout
);
4231 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4232 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4234 // If we are generating a shared object, we need to add a
4235 // dynamic RELATIVE relocation for this symbol's GOT entry.
4236 if (parameters
->options().output_is_position_independent())
4238 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4239 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4240 rel_dyn
->add_local_relative(
4241 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4242 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4248 case elfcpp::R_ARM_TARGET1
:
4249 // This should have been mapped to another type already.
4251 case elfcpp::R_ARM_COPY
:
4252 case elfcpp::R_ARM_GLOB_DAT
:
4253 case elfcpp::R_ARM_JUMP_SLOT
:
4254 case elfcpp::R_ARM_RELATIVE
:
4255 // These are relocations which should only be seen by the
4256 // dynamic linker, and should never be seen here.
4257 gold_error(_("%s: unexpected reloc %u in object file"),
4258 object
->name().c_str(), r_type
);
4262 unsupported_reloc_local(object
, r_type
);
4267 // Report an unsupported relocation against a global symbol.
4269 template<bool big_endian
>
4271 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4272 Sized_relobj
<32, big_endian
>* object
,
4273 unsigned int r_type
,
4276 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4277 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4280 // Scan a relocation for a global symbol.
4281 // FIXME: This only handles a subset of relocation types used by Android
4282 // on ARM v5te devices.
4284 template<bool big_endian
>
4286 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4289 Sized_relobj
<32, big_endian
>* object
,
4290 unsigned int data_shndx
,
4291 Output_section
* output_section
,
4292 const elfcpp::Rel
<32, big_endian
>& reloc
,
4293 unsigned int r_type
,
4296 r_type
= get_real_reloc_type(r_type
);
4299 case elfcpp::R_ARM_NONE
:
4302 case elfcpp::R_ARM_ABS32
:
4303 case elfcpp::R_ARM_ABS32_NOI
:
4305 // Make a dynamic relocation if necessary.
4306 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4308 if (target
->may_need_copy_reloc(gsym
))
4310 target
->copy_reloc(symtab
, layout
, object
,
4311 data_shndx
, output_section
, gsym
, reloc
);
4313 else if (gsym
->can_use_relative_reloc(false))
4315 // If we are to add more other reloc types than R_ARM_ABS32,
4316 // we need to add check_non_pic(object, r_type) here.
4317 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4318 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4319 output_section
, object
,
4320 data_shndx
, reloc
.get_r_offset());
4324 // If we are to add more other reloc types than R_ARM_ABS32,
4325 // we need to add check_non_pic(object, r_type) here.
4326 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4327 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4328 data_shndx
, reloc
.get_r_offset());
4334 case elfcpp::R_ARM_MOVW_ABS_NC
:
4335 case elfcpp::R_ARM_MOVT_ABS
:
4336 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4337 case elfcpp::R_ARM_THM_MOVT_ABS
:
4338 case elfcpp::R_ARM_MOVW_PREL_NC
:
4339 case elfcpp::R_ARM_MOVT_PREL
:
4340 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4341 case elfcpp::R_ARM_THM_MOVT_PREL
:
4344 case elfcpp::R_ARM_THM_ABS5
:
4345 case elfcpp::R_ARM_ABS8
:
4346 case elfcpp::R_ARM_ABS12
:
4347 case elfcpp::R_ARM_ABS16
:
4348 case elfcpp::R_ARM_BASE_ABS
:
4350 // No dynamic relocs of this kinds.
4351 // Report the error in case of PIC.
4352 int flags
= Symbol::NON_PIC_REF
;
4353 if (gsym
->type() == elfcpp::STT_FUNC
4354 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4355 flags
|= Symbol::FUNCTION_CALL
;
4356 if (gsym
->needs_dynamic_reloc(flags
))
4357 check_non_pic(object
, r_type
);
4361 case elfcpp::R_ARM_REL32
:
4362 case elfcpp::R_ARM_PREL31
:
4364 // Make a dynamic relocation if necessary.
4365 int flags
= Symbol::NON_PIC_REF
;
4366 if (gsym
->needs_dynamic_reloc(flags
))
4368 if (target
->may_need_copy_reloc(gsym
))
4370 target
->copy_reloc(symtab
, layout
, object
,
4371 data_shndx
, output_section
, gsym
, reloc
);
4375 check_non_pic(object
, r_type
);
4376 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4377 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4378 data_shndx
, reloc
.get_r_offset());
4384 case elfcpp::R_ARM_JUMP24
:
4385 case elfcpp::R_ARM_THM_CALL
:
4386 case elfcpp::R_ARM_CALL
:
4388 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4389 target
->make_plt_entry(symtab
, layout
, gsym
);
4390 // Make a dynamic relocation if necessary.
4391 int flags
= Symbol::NON_PIC_REF
;
4392 if (gsym
->type() == elfcpp::STT_FUNC
4393 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4394 flags
|= Symbol::FUNCTION_CALL
;
4395 if (gsym
->needs_dynamic_reloc(flags
))
4397 if (target
->may_need_copy_reloc(gsym
))
4399 target
->copy_reloc(symtab
, layout
, object
,
4400 data_shndx
, output_section
, gsym
,
4405 check_non_pic(object
, r_type
);
4406 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4407 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4408 data_shndx
, reloc
.get_r_offset());
4414 case elfcpp::R_ARM_PLT32
:
4415 // If the symbol is fully resolved, this is just a relative
4416 // local reloc. Otherwise we need a PLT entry.
4417 if (gsym
->final_value_is_known())
4419 // If building a shared library, we can also skip the PLT entry
4420 // if the symbol is defined in the output file and is protected
4422 if (gsym
->is_defined()
4423 && !gsym
->is_from_dynobj()
4424 && !gsym
->is_preemptible())
4426 target
->make_plt_entry(symtab
, layout
, gsym
);
4429 case elfcpp::R_ARM_GOTOFF32
:
4430 // We need a GOT section.
4431 target
->got_section(symtab
, layout
);
4434 case elfcpp::R_ARM_BASE_PREL
:
4435 // FIXME: What about this?
4438 case elfcpp::R_ARM_GOT_BREL
:
4439 case elfcpp::R_ARM_GOT_PREL
:
4441 // The symbol requires a GOT entry.
4442 Output_data_got
<32, big_endian
>* got
=
4443 target
->got_section(symtab
, layout
);
4444 if (gsym
->final_value_is_known())
4445 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4448 // If this symbol is not fully resolved, we need to add a
4449 // GOT entry with a dynamic relocation.
4450 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4451 if (gsym
->is_from_dynobj()
4452 || gsym
->is_undefined()
4453 || gsym
->is_preemptible())
4454 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4455 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4458 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4459 rel_dyn
->add_global_relative(
4460 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4461 gsym
->got_offset(GOT_TYPE_STANDARD
));
4467 case elfcpp::R_ARM_TARGET1
:
4468 // This should have been mapped to another type already.
4470 case elfcpp::R_ARM_COPY
:
4471 case elfcpp::R_ARM_GLOB_DAT
:
4472 case elfcpp::R_ARM_JUMP_SLOT
:
4473 case elfcpp::R_ARM_RELATIVE
:
4474 // These are relocations which should only be seen by the
4475 // dynamic linker, and should never be seen here.
4476 gold_error(_("%s: unexpected reloc %u in object file"),
4477 object
->name().c_str(), r_type
);
4481 unsupported_reloc_global(object
, r_type
, gsym
);
4486 // Process relocations for gc.
4488 template<bool big_endian
>
4490 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4492 Sized_relobj
<32, big_endian
>* object
,
4493 unsigned int data_shndx
,
4495 const unsigned char* prelocs
,
4497 Output_section
* output_section
,
4498 bool needs_special_offset_handling
,
4499 size_t local_symbol_count
,
4500 const unsigned char* plocal_symbols
)
4502 typedef Target_arm
<big_endian
> Arm
;
4503 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4505 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
4514 needs_special_offset_handling
,
4519 // Scan relocations for a section.
4521 template<bool big_endian
>
4523 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4525 Sized_relobj
<32, big_endian
>* object
,
4526 unsigned int data_shndx
,
4527 unsigned int sh_type
,
4528 const unsigned char* prelocs
,
4530 Output_section
* output_section
,
4531 bool needs_special_offset_handling
,
4532 size_t local_symbol_count
,
4533 const unsigned char* plocal_symbols
)
4535 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4536 if (sh_type
== elfcpp::SHT_RELA
)
4538 gold_error(_("%s: unsupported RELA reloc section"),
4539 object
->name().c_str());
4543 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
4552 needs_special_offset_handling
,
4557 // Finalize the sections.
4559 template<bool big_endian
>
4561 Target_arm
<big_endian
>::do_finalize_sections(
4563 const Input_objects
* input_objects
)
4565 // Merge processor-specific flags.
4566 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
4567 p
!= input_objects
->relobj_end();
4570 Arm_relobj
<big_endian
>* arm_relobj
=
4571 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
4572 this->merge_processor_specific_flags(
4574 arm_relobj
->processor_specific_flags());
4577 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
4578 p
!= input_objects
->dynobj_end();
4581 Arm_dynobj
<big_endian
>* arm_dynobj
=
4582 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
4583 this->merge_processor_specific_flags(
4585 arm_dynobj
->processor_specific_flags());
4588 // Fill in some more dynamic tags.
4589 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4592 if (this->got_plt_
!= NULL
4593 && this->got_plt_
->output_section() != NULL
)
4594 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
4596 if (this->plt_
!= NULL
4597 && this->plt_
->output_section() != NULL
)
4599 const Output_data
* od
= this->plt_
->rel_plt();
4600 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
4601 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
4602 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
4605 if (this->rel_dyn_
!= NULL
4606 && this->rel_dyn_
->output_section() != NULL
)
4608 const Output_data
* od
= this->rel_dyn_
;
4609 odyn
->add_section_address(elfcpp::DT_REL
, od
);
4610 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
4611 odyn
->add_constant(elfcpp::DT_RELENT
,
4612 elfcpp::Elf_sizes
<32>::rel_size
);
4615 if (!parameters
->options().shared())
4617 // The value of the DT_DEBUG tag is filled in by the dynamic
4618 // linker at run time, and used by the debugger.
4619 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
4623 // Emit any relocs we saved in an attempt to avoid generating COPY
4625 if (this->copy_relocs_
.any_saved_relocs())
4626 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
4628 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4629 // the .ARM.exidx section.
4630 if (!layout
->script_options()->saw_phdrs_clause()
4631 && !parameters
->options().relocatable())
4633 Output_section
* exidx_section
=
4634 layout
->find_output_section(".ARM.exidx");
4636 if (exidx_section
!= NULL
4637 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
)
4639 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
4641 Output_segment
* exidx_segment
=
4642 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
4643 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
4649 // Return whether a direct absolute static relocation needs to be applied.
4650 // In cases where Scan::local() or Scan::global() has created
4651 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4652 // of the relocation is carried in the data, and we must not
4653 // apply the static relocation.
4655 template<bool big_endian
>
4657 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
4658 const Sized_symbol
<32>* gsym
,
4661 Output_section
* output_section
)
4663 // If the output section is not allocated, then we didn't call
4664 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4666 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
4669 // For local symbols, we will have created a non-RELATIVE dynamic
4670 // relocation only if (a) the output is position independent,
4671 // (b) the relocation is absolute (not pc- or segment-relative), and
4672 // (c) the relocation is not 32 bits wide.
4674 return !(parameters
->options().output_is_position_independent()
4675 && (ref_flags
& Symbol::ABSOLUTE_REF
)
4678 // For global symbols, we use the same helper routines used in the
4679 // scan pass. If we did not create a dynamic relocation, or if we
4680 // created a RELATIVE dynamic relocation, we should apply the static
4682 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
4683 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
4684 && gsym
->can_use_relative_reloc(ref_flags
4685 & Symbol::FUNCTION_CALL
);
4686 return !has_dyn
|| is_rel
;
4689 // Perform a relocation.
4691 template<bool big_endian
>
4693 Target_arm
<big_endian
>::Relocate::relocate(
4694 const Relocate_info
<32, big_endian
>* relinfo
,
4696 Output_section
*output_section
,
4698 const elfcpp::Rel
<32, big_endian
>& rel
,
4699 unsigned int r_type
,
4700 const Sized_symbol
<32>* gsym
,
4701 const Symbol_value
<32>* psymval
,
4702 unsigned char* view
,
4703 Arm_address address
,
4704 section_size_type
/* view_size */ )
4706 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
4708 r_type
= get_real_reloc_type(r_type
);
4710 const Arm_relobj
<big_endian
>* object
=
4711 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
4713 // If the final branch target of a relocation is THUMB instruction, this
4714 // is 1. Otherwise it is 0.
4715 Arm_address thumb_bit
= 0;
4716 Symbol_value
<32> symval
;
4717 bool is_weakly_undefined_without_plt
= false;
4718 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
4722 // This is a global symbol. Determine if we use PLT and if the
4723 // final target is THUMB.
4724 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
4726 // This uses a PLT, change the symbol value.
4727 symval
.set_output_value(target
->plt_section()->address()
4728 + gsym
->plt_offset());
4731 else if (gsym
->is_weak_undefined())
4733 // This is a weakly undefined symbol and we do not use PLT
4734 // for this relocation. A branch targeting this symbol will
4735 // be converted into an NOP.
4736 is_weakly_undefined_without_plt
= true;
4740 // Set thumb bit if symbol:
4741 // -Has type STT_ARM_TFUNC or
4742 // -Has type STT_FUNC, is defined and with LSB in value set.
4744 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4745 || (gsym
->type() == elfcpp::STT_FUNC
4746 && !gsym
->is_undefined()
4747 && ((psymval
->value(object
, 0) & 1) != 0)))
4754 // This is a local symbol. Determine if the final target is THUMB.
4755 // We saved this information when all the local symbols were read.
4756 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
4757 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
4758 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
4763 // This is a fake relocation synthesized for a stub. It does not have
4764 // a real symbol. We just look at the LSB of the symbol value to
4765 // determine if the target is THUMB or not.
4766 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
4769 // Strip LSB if this points to a THUMB target.
4771 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
4772 && ((psymval
->value(object
, 0) & 1) != 0))
4774 Arm_address stripped_value
=
4775 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
4776 symval
.set_output_value(stripped_value
);
4780 // Get the GOT offset if needed.
4781 // The GOT pointer points to the end of the GOT section.
4782 // We need to subtract the size of the GOT section to get
4783 // the actual offset to use in the relocation.
4784 bool have_got_offset
= false;
4785 unsigned int got_offset
= 0;
4788 case elfcpp::R_ARM_GOT_BREL
:
4789 case elfcpp::R_ARM_GOT_PREL
:
4792 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4793 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4794 - target
->got_size());
4798 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4799 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4800 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4801 - target
->got_size());
4803 have_got_offset
= true;
4810 // To look up relocation stubs, we need to pass the symbol table index of
4812 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4814 typename
Arm_relocate_functions::Status reloc_status
=
4815 Arm_relocate_functions::STATUS_OKAY
;
4818 case elfcpp::R_ARM_NONE
:
4821 case elfcpp::R_ARM_ABS8
:
4822 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4824 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
4827 case elfcpp::R_ARM_ABS12
:
4828 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4830 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
4833 case elfcpp::R_ARM_ABS16
:
4834 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4836 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
4839 case elfcpp::R_ARM_ABS32
:
4840 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4842 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4846 case elfcpp::R_ARM_ABS32_NOI
:
4847 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4849 // No thumb bit for this relocation: (S + A)
4850 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4854 case elfcpp::R_ARM_MOVW_ABS_NC
:
4855 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4857 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
4861 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4862 "a shared object; recompile with -fPIC"));
4865 case elfcpp::R_ARM_MOVT_ABS
:
4866 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4868 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
4870 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4871 "a shared object; recompile with -fPIC"));
4874 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4875 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4877 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
4881 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4882 "making a shared object; recompile with -fPIC"));
4885 case elfcpp::R_ARM_THM_MOVT_ABS
:
4886 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4888 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
4891 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4892 "making a shared object; recompile with -fPIC"));
4895 case elfcpp::R_ARM_MOVW_PREL_NC
:
4896 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
4901 case elfcpp::R_ARM_MOVT_PREL
:
4902 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
4906 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4907 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
4912 case elfcpp::R_ARM_THM_MOVT_PREL
:
4913 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
4917 case elfcpp::R_ARM_REL32
:
4918 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4919 address
, thumb_bit
);
4922 case elfcpp::R_ARM_THM_ABS5
:
4923 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4925 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
4928 case elfcpp::R_ARM_THM_CALL
:
4930 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
4931 psymval
, address
, thumb_bit
,
4932 is_weakly_undefined_without_plt
);
4935 case elfcpp::R_ARM_XPC25
:
4937 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
4938 psymval
, address
, thumb_bit
,
4939 is_weakly_undefined_without_plt
);
4942 case elfcpp::R_ARM_THM_XPC22
:
4944 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
4945 psymval
, address
, thumb_bit
,
4946 is_weakly_undefined_without_plt
);
4949 case elfcpp::R_ARM_GOTOFF32
:
4951 Arm_address got_origin
;
4952 got_origin
= target
->got_plt_section()->address();
4953 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4954 got_origin
, thumb_bit
);
4958 case elfcpp::R_ARM_BASE_PREL
:
4961 // Get the addressing origin of the output segment defining the
4962 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4963 gold_assert(gsym
!= NULL
);
4964 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4965 origin
= gsym
->output_segment()->vaddr();
4966 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4967 origin
= gsym
->output_data()->address();
4970 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4971 _("cannot find origin of R_ARM_BASE_PREL"));
4974 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
4978 case elfcpp::R_ARM_BASE_ABS
:
4980 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4985 // Get the addressing origin of the output segment defining
4986 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4988 // R_ARM_BASE_ABS with the NULL symbol will give the
4989 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4990 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4991 origin
= target
->got_plt_section()->address();
4992 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4993 origin
= gsym
->output_segment()->vaddr();
4994 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4995 origin
= gsym
->output_data()->address();
4998 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4999 _("cannot find origin of R_ARM_BASE_ABS"));
5003 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5007 case elfcpp::R_ARM_GOT_BREL
:
5008 gold_assert(have_got_offset
);
5009 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5012 case elfcpp::R_ARM_GOT_PREL
:
5013 gold_assert(have_got_offset
);
5014 // Get the address origin for GOT PLT, which is allocated right
5015 // after the GOT section, to calculate an absolute address of
5016 // the symbol GOT entry (got_origin + got_offset).
5017 Arm_address got_origin
;
5018 got_origin
= target
->got_plt_section()->address();
5019 reloc_status
= Arm_relocate_functions::got_prel(view
,
5020 got_origin
+ got_offset
,
5024 case elfcpp::R_ARM_PLT32
:
5025 gold_assert(gsym
== NULL
5026 || gsym
->has_plt_offset()
5027 || gsym
->final_value_is_known()
5028 || (gsym
->is_defined()
5029 && !gsym
->is_from_dynobj()
5030 && !gsym
->is_preemptible()));
5032 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5033 psymval
, address
, thumb_bit
,
5034 is_weakly_undefined_without_plt
);
5037 case elfcpp::R_ARM_CALL
:
5039 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5040 psymval
, address
, thumb_bit
,
5041 is_weakly_undefined_without_plt
);
5044 case elfcpp::R_ARM_JUMP24
:
5046 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5047 psymval
, address
, thumb_bit
,
5048 is_weakly_undefined_without_plt
);
5051 case elfcpp::R_ARM_THM_JUMP24
:
5053 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5054 psymval
, address
, thumb_bit
,
5055 is_weakly_undefined_without_plt
);
5058 case elfcpp::R_ARM_PREL31
:
5059 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5060 address
, thumb_bit
);
5063 case elfcpp::R_ARM_TARGET1
:
5064 // This should have been mapped to another type already.
5066 case elfcpp::R_ARM_COPY
:
5067 case elfcpp::R_ARM_GLOB_DAT
:
5068 case elfcpp::R_ARM_JUMP_SLOT
:
5069 case elfcpp::R_ARM_RELATIVE
:
5070 // These are relocations which should only be seen by the
5071 // dynamic linker, and should never be seen here.
5072 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5073 _("unexpected reloc %u in object file"),
5078 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5079 _("unsupported reloc %u"),
5084 // Report any errors.
5085 switch (reloc_status
)
5087 case Arm_relocate_functions::STATUS_OKAY
:
5089 case Arm_relocate_functions::STATUS_OVERFLOW
:
5090 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5091 _("relocation overflow in relocation %u"),
5094 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5095 gold_error_at_location(
5099 _("unexpected opcode while processing relocation %u"),
5109 // Relocate section data.
5111 template<bool big_endian
>
5113 Target_arm
<big_endian
>::relocate_section(
5114 const Relocate_info
<32, big_endian
>* relinfo
,
5115 unsigned int sh_type
,
5116 const unsigned char* prelocs
,
5118 Output_section
* output_section
,
5119 bool needs_special_offset_handling
,
5120 unsigned char* view
,
5121 Arm_address address
,
5122 section_size_type view_size
,
5123 const Reloc_symbol_changes
* reloc_symbol_changes
)
5125 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5126 gold_assert(sh_type
== elfcpp::SHT_REL
);
5128 Arm_input_section
<big_endian
>* arm_input_section
=
5129 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5131 // This is an ARM input section and the view covers the whole output
5133 if (arm_input_section
!= NULL
)
5135 gold_assert(needs_special_offset_handling
);
5136 Arm_address section_address
= arm_input_section
->address();
5137 section_size_type section_size
= arm_input_section
->data_size();
5139 gold_assert((arm_input_section
->address() >= address
)
5140 && ((arm_input_section
->address()
5141 + arm_input_section
->data_size())
5142 <= (address
+ view_size
)));
5144 off_t offset
= section_address
- address
;
5147 view_size
= section_size
;
5150 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5157 needs_special_offset_handling
,
5161 reloc_symbol_changes
);
5164 // Return the size of a relocation while scanning during a relocatable
5167 template<bool big_endian
>
5169 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5170 unsigned int r_type
,
5173 r_type
= get_real_reloc_type(r_type
);
5176 case elfcpp::R_ARM_NONE
:
5179 case elfcpp::R_ARM_ABS8
:
5182 case elfcpp::R_ARM_ABS16
:
5183 case elfcpp::R_ARM_THM_ABS5
:
5186 case elfcpp::R_ARM_ABS32
:
5187 case elfcpp::R_ARM_ABS32_NOI
:
5188 case elfcpp::R_ARM_ABS12
:
5189 case elfcpp::R_ARM_BASE_ABS
:
5190 case elfcpp::R_ARM_REL32
:
5191 case elfcpp::R_ARM_THM_CALL
:
5192 case elfcpp::R_ARM_GOTOFF32
:
5193 case elfcpp::R_ARM_BASE_PREL
:
5194 case elfcpp::R_ARM_GOT_BREL
:
5195 case elfcpp::R_ARM_GOT_PREL
:
5196 case elfcpp::R_ARM_PLT32
:
5197 case elfcpp::R_ARM_CALL
:
5198 case elfcpp::R_ARM_JUMP24
:
5199 case elfcpp::R_ARM_PREL31
:
5200 case elfcpp::R_ARM_MOVW_ABS_NC
:
5201 case elfcpp::R_ARM_MOVT_ABS
:
5202 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5203 case elfcpp::R_ARM_THM_MOVT_ABS
:
5204 case elfcpp::R_ARM_MOVW_PREL_NC
:
5205 case elfcpp::R_ARM_MOVT_PREL
:
5206 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5207 case elfcpp::R_ARM_THM_MOVT_PREL
:
5210 case elfcpp::R_ARM_TARGET1
:
5211 // This should have been mapped to another type already.
5213 case elfcpp::R_ARM_COPY
:
5214 case elfcpp::R_ARM_GLOB_DAT
:
5215 case elfcpp::R_ARM_JUMP_SLOT
:
5216 case elfcpp::R_ARM_RELATIVE
:
5217 // These are relocations which should only be seen by the
5218 // dynamic linker, and should never be seen here.
5219 gold_error(_("%s: unexpected reloc %u in object file"),
5220 object
->name().c_str(), r_type
);
5224 object
->error(_("unsupported reloc %u in object file"), r_type
);
5229 // Scan the relocs during a relocatable link.
5231 template<bool big_endian
>
5233 Target_arm
<big_endian
>::scan_relocatable_relocs(
5234 Symbol_table
* symtab
,
5236 Sized_relobj
<32, big_endian
>* object
,
5237 unsigned int data_shndx
,
5238 unsigned int sh_type
,
5239 const unsigned char* prelocs
,
5241 Output_section
* output_section
,
5242 bool needs_special_offset_handling
,
5243 size_t local_symbol_count
,
5244 const unsigned char* plocal_symbols
,
5245 Relocatable_relocs
* rr
)
5247 gold_assert(sh_type
== elfcpp::SHT_REL
);
5249 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5250 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5252 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5253 Scan_relocatable_relocs
>(
5261 needs_special_offset_handling
,
5267 // Relocate a section during a relocatable link.
5269 template<bool big_endian
>
5271 Target_arm
<big_endian
>::relocate_for_relocatable(
5272 const Relocate_info
<32, big_endian
>* relinfo
,
5273 unsigned int sh_type
,
5274 const unsigned char* prelocs
,
5276 Output_section
* output_section
,
5277 off_t offset_in_output_section
,
5278 const Relocatable_relocs
* rr
,
5279 unsigned char* view
,
5280 Arm_address view_address
,
5281 section_size_type view_size
,
5282 unsigned char* reloc_view
,
5283 section_size_type reloc_view_size
)
5285 gold_assert(sh_type
== elfcpp::SHT_REL
);
5287 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5292 offset_in_output_section
,
5301 // Return the value to use for a dynamic symbol which requires special
5302 // treatment. This is how we support equality comparisons of function
5303 // pointers across shared library boundaries, as described in the
5304 // processor specific ABI supplement.
5306 template<bool big_endian
>
5308 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5310 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5311 return this->plt_section()->address() + gsym
->plt_offset();
5314 // Map platform-specific relocs to real relocs
5316 template<bool big_endian
>
5318 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5322 case elfcpp::R_ARM_TARGET1
:
5323 // This is either R_ARM_ABS32 or R_ARM_REL32;
5324 return elfcpp::R_ARM_ABS32
;
5326 case elfcpp::R_ARM_TARGET2
:
5327 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5328 return elfcpp::R_ARM_GOT_PREL
;
5335 // Whether if two EABI versions V1 and V2 are compatible.
5337 template<bool big_endian
>
5339 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5340 elfcpp::Elf_Word v1
,
5341 elfcpp::Elf_Word v2
)
5343 // v4 and v5 are the same spec before and after it was released,
5344 // so allow mixing them.
5345 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5346 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5352 // Combine FLAGS from an input object called NAME and the processor-specific
5353 // flags in the ELF header of the output. Much of this is adapted from the
5354 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5355 // in bfd/elf32-arm.c.
5357 template<bool big_endian
>
5359 Target_arm
<big_endian
>::merge_processor_specific_flags(
5360 const std::string
& name
,
5361 elfcpp::Elf_Word flags
)
5363 if (this->are_processor_specific_flags_set())
5365 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5367 // Nothing to merge if flags equal to those in output.
5368 if (flags
== out_flags
)
5371 // Complain about various flag mismatches.
5372 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5373 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5374 if (!this->are_eabi_versions_compatible(version1
, version2
))
5375 gold_error(_("Source object %s has EABI version %d but output has "
5376 "EABI version %d."),
5378 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5379 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5383 // If the input is the default architecture and had the default
5384 // flags then do not bother setting the flags for the output
5385 // architecture, instead allow future merges to do this. If no
5386 // future merges ever set these flags then they will retain their
5387 // uninitialised values, which surprise surprise, correspond
5388 // to the default values.
5392 // This is the first time, just copy the flags.
5393 // We only copy the EABI version for now.
5394 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5398 // Adjust ELF file header.
5399 template<bool big_endian
>
5401 Target_arm
<big_endian
>::do_adjust_elf_header(
5402 unsigned char* view
,
5405 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5407 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5408 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5409 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5411 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5412 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5413 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5415 e_ident
[elfcpp::EI_OSABI
] = 0;
5416 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5418 // FIXME: Do EF_ARM_BE8 adjustment.
5420 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5421 oehdr
.put_e_ident(e_ident
);
5424 // do_make_elf_object to override the same function in the base class.
5425 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5426 // to store ARM specific information. Hence we need to have our own
5427 // ELF object creation.
5429 template<bool big_endian
>
5431 Target_arm
<big_endian
>::do_make_elf_object(
5432 const std::string
& name
,
5433 Input_file
* input_file
,
5434 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5436 int et
= ehdr
.get_e_type();
5437 if (et
== elfcpp::ET_REL
)
5439 Arm_relobj
<big_endian
>* obj
=
5440 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5444 else if (et
== elfcpp::ET_DYN
)
5446 Sized_dynobj
<32, big_endian
>* obj
=
5447 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5453 gold_error(_("%s: unsupported ELF file type %d"),
5459 // Return whether a relocation type used the LSB to distinguish THUMB
5461 template<bool big_endian
>
5463 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
5467 case elfcpp::R_ARM_PC24
:
5468 case elfcpp::R_ARM_ABS32
:
5469 case elfcpp::R_ARM_REL32
:
5470 case elfcpp::R_ARM_SBREL32
:
5471 case elfcpp::R_ARM_THM_CALL
:
5472 case elfcpp::R_ARM_GLOB_DAT
:
5473 case elfcpp::R_ARM_JUMP_SLOT
:
5474 case elfcpp::R_ARM_GOTOFF32
:
5475 case elfcpp::R_ARM_PLT32
:
5476 case elfcpp::R_ARM_CALL
:
5477 case elfcpp::R_ARM_JUMP24
:
5478 case elfcpp::R_ARM_THM_JUMP24
:
5479 case elfcpp::R_ARM_SBREL31
:
5480 case elfcpp::R_ARM_PREL31
:
5481 case elfcpp::R_ARM_MOVW_ABS_NC
:
5482 case elfcpp::R_ARM_MOVW_PREL_NC
:
5483 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5484 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5485 case elfcpp::R_ARM_THM_JUMP19
:
5486 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
5487 case elfcpp::R_ARM_ALU_PC_G0_NC
:
5488 case elfcpp::R_ARM_ALU_PC_G0
:
5489 case elfcpp::R_ARM_ALU_PC_G1_NC
:
5490 case elfcpp::R_ARM_ALU_PC_G1
:
5491 case elfcpp::R_ARM_ALU_PC_G2
:
5492 case elfcpp::R_ARM_ALU_SB_G0_NC
:
5493 case elfcpp::R_ARM_ALU_SB_G0
:
5494 case elfcpp::R_ARM_ALU_SB_G1_NC
:
5495 case elfcpp::R_ARM_ALU_SB_G1
:
5496 case elfcpp::R_ARM_ALU_SB_G2
:
5497 case elfcpp::R_ARM_MOVW_BREL_NC
:
5498 case elfcpp::R_ARM_MOVW_BREL
:
5499 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
5500 case elfcpp::R_ARM_THM_MOVW_BREL
:
5507 // Stub-generation methods for Target_arm.
5509 // Make a new Arm_input_section object.
5511 template<bool big_endian
>
5512 Arm_input_section
<big_endian
>*
5513 Target_arm
<big_endian
>::new_arm_input_section(
5517 Input_section_specifier
iss(relobj
, shndx
);
5519 Arm_input_section
<big_endian
>* arm_input_section
=
5520 new Arm_input_section
<big_endian
>(relobj
, shndx
);
5521 arm_input_section
->init();
5523 // Register new Arm_input_section in map for look-up.
5524 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
5525 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
5527 // Make sure that it we have not created another Arm_input_section
5528 // for this input section already.
5529 gold_assert(ins
.second
);
5531 return arm_input_section
;
5534 // Find the Arm_input_section object corresponding to the SHNDX-th input
5535 // section of RELOBJ.
5537 template<bool big_endian
>
5538 Arm_input_section
<big_endian
>*
5539 Target_arm
<big_endian
>::find_arm_input_section(
5541 unsigned int shndx
) const
5543 Input_section_specifier
iss(relobj
, shndx
);
5544 typename
Arm_input_section_map::const_iterator p
=
5545 this->arm_input_section_map_
.find(iss
);
5546 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
5549 // Make a new stub table.
5551 template<bool big_endian
>
5552 Stub_table
<big_endian
>*
5553 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
5555 Stub_table
<big_endian
>* stub_table
=
5556 new Stub_table
<big_endian
>(owner
);
5557 this->stub_tables_
.push_back(stub_table
);
5559 stub_table
->set_address(owner
->address() + owner
->data_size());
5560 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
5561 stub_table
->finalize_data_size();
5566 // Scan a relocation for stub generation.
5568 template<bool big_endian
>
5570 Target_arm
<big_endian
>::scan_reloc_for_stub(
5571 const Relocate_info
<32, big_endian
>* relinfo
,
5572 unsigned int r_type
,
5573 const Sized_symbol
<32>* gsym
,
5575 const Symbol_value
<32>* psymval
,
5576 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
5577 Arm_address address
)
5579 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
5581 const Arm_relobj
<big_endian
>* arm_relobj
=
5582 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5584 bool target_is_thumb
;
5585 Symbol_value
<32> symval
;
5588 // This is a global symbol. Determine if we use PLT and if the
5589 // final target is THUMB.
5590 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
5592 // This uses a PLT, change the symbol value.
5593 symval
.set_output_value(this->plt_section()->address()
5594 + gsym
->plt_offset());
5596 target_is_thumb
= false;
5598 else if (gsym
->is_undefined())
5599 // There is no need to generate a stub symbol is undefined.
5604 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5605 || (gsym
->type() == elfcpp::STT_FUNC
5606 && !gsym
->is_undefined()
5607 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
5612 // This is a local symbol. Determine if the final target is THUMB.
5613 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
5616 // Strip LSB if this points to a THUMB target.
5618 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5619 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
5621 Arm_address stripped_value
=
5622 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
5623 symval
.set_output_value(stripped_value
);
5627 // Get the symbol value.
5628 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
5630 // Owing to pipelining, the PC relative branches below actually skip
5631 // two instructions when the branch offset is 0.
5632 Arm_address destination
;
5635 case elfcpp::R_ARM_CALL
:
5636 case elfcpp::R_ARM_JUMP24
:
5637 case elfcpp::R_ARM_PLT32
:
5639 destination
= value
+ addend
+ 8;
5641 case elfcpp::R_ARM_THM_CALL
:
5642 case elfcpp::R_ARM_THM_XPC22
:
5643 case elfcpp::R_ARM_THM_JUMP24
:
5644 case elfcpp::R_ARM_THM_JUMP19
:
5646 destination
= value
+ addend
+ 4;
5652 Stub_type stub_type
=
5653 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
5656 // This reloc does not need a stub.
5657 if (stub_type
== arm_stub_none
)
5660 // Try looking up an existing stub from a stub table.
5661 Stub_table
<big_endian
>* stub_table
=
5662 arm_relobj
->stub_table(relinfo
->data_shndx
);
5663 gold_assert(stub_table
!= NULL
);
5665 // Locate stub by destination.
5666 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
5668 // Create a stub if there is not one already
5669 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
5672 // create a new stub and add it to stub table.
5673 stub
= this->stub_factory().make_reloc_stub(stub_type
);
5674 stub_table
->add_reloc_stub(stub
, stub_key
);
5677 // Record the destination address.
5678 stub
->set_destination_address(destination
5679 | (target_is_thumb
? 1 : 0));
5682 // This function scans a relocation sections for stub generation.
5683 // The template parameter Relocate must be a class type which provides
5684 // a single function, relocate(), which implements the machine
5685 // specific part of a relocation.
5687 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
5688 // SHT_REL or SHT_RELA.
5690 // PRELOCS points to the relocation data. RELOC_COUNT is the number
5691 // of relocs. OUTPUT_SECTION is the output section.
5692 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
5693 // mapped to output offsets.
5695 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
5696 // VIEW_SIZE is the size. These refer to the input section, unless
5697 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
5698 // the output section.
5700 template<bool big_endian
>
5701 template<int sh_type
>
5703 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
5704 const Relocate_info
<32, big_endian
>* relinfo
,
5705 const unsigned char* prelocs
,
5707 Output_section
* output_section
,
5708 bool needs_special_offset_handling
,
5709 const unsigned char* view
,
5710 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
5713 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
5714 const int reloc_size
=
5715 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
5717 Arm_relobj
<big_endian
>* arm_object
=
5718 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5719 unsigned int local_count
= arm_object
->local_symbol_count();
5721 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
5723 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
5725 Reltype
reloc(prelocs
);
5727 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
5728 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5729 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
5731 r_type
= this->get_real_reloc_type(r_type
);
5733 // Only a few relocation types need stubs.
5734 if ((r_type
!= elfcpp::R_ARM_CALL
)
5735 && (r_type
!= elfcpp::R_ARM_JUMP24
)
5736 && (r_type
!= elfcpp::R_ARM_PLT32
)
5737 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
5738 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
5739 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
5740 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
5743 section_offset_type offset
=
5744 convert_to_section_size_type(reloc
.get_r_offset());
5746 if (needs_special_offset_handling
)
5748 offset
= output_section
->output_offset(relinfo
->object
,
5749 relinfo
->data_shndx
,
5756 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
5757 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
5758 stub_addend_reader(r_type
, view
+ offset
, reloc
);
5760 const Sized_symbol
<32>* sym
;
5762 Symbol_value
<32> symval
;
5763 const Symbol_value
<32> *psymval
;
5764 if (r_sym
< local_count
)
5767 psymval
= arm_object
->local_symbol(r_sym
);
5769 // If the local symbol belongs to a section we are discarding,
5770 // and that section is a debug section, try to find the
5771 // corresponding kept section and map this symbol to its
5772 // counterpart in the kept section. The symbol must not
5773 // correspond to a section we are folding.
5775 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
5777 && shndx
!= elfcpp::SHN_UNDEF
5778 && !arm_object
->is_section_included(shndx
)
5779 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
5781 if (comdat_behavior
== CB_UNDETERMINED
)
5784 arm_object
->section_name(relinfo
->data_shndx
);
5785 comdat_behavior
= get_comdat_behavior(name
.c_str());
5787 if (comdat_behavior
== CB_PRETEND
)
5790 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
5791 arm_object
->map_to_kept_section(shndx
, &found
);
5793 symval
.set_output_value(value
+ psymval
->input_value());
5795 symval
.set_output_value(0);
5799 symval
.set_output_value(0);
5801 symval
.set_no_output_symtab_entry();
5807 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
5808 gold_assert(gsym
!= NULL
);
5809 if (gsym
->is_forwarder())
5810 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
5812 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
5813 if (sym
->has_symtab_index())
5814 symval
.set_output_symtab_index(sym
->symtab_index());
5816 symval
.set_no_output_symtab_entry();
5818 // We need to compute the would-be final value of this global
5820 const Symbol_table
* symtab
= relinfo
->symtab
;
5821 const Sized_symbol
<32>* sized_symbol
=
5822 symtab
->get_sized_symbol
<32>(gsym
);
5823 Symbol_table::Compute_final_value_status status
;
5825 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
5827 // Skip this if the symbol has not output section.
5828 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
5831 symval
.set_output_value(value
);
5835 // If symbol is a section symbol, we don't know the actual type of
5836 // destination. Give up.
5837 if (psymval
->is_section_symbol())
5840 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
5841 addend
, view_address
+ offset
);
5845 // Scan an input section for stub generation.
5847 template<bool big_endian
>
5849 Target_arm
<big_endian
>::scan_section_for_stubs(
5850 const Relocate_info
<32, big_endian
>* relinfo
,
5851 unsigned int sh_type
,
5852 const unsigned char* prelocs
,
5854 Output_section
* output_section
,
5855 bool needs_special_offset_handling
,
5856 const unsigned char* view
,
5857 Arm_address view_address
,
5858 section_size_type view_size
)
5860 if (sh_type
== elfcpp::SHT_REL
)
5861 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
5866 needs_special_offset_handling
,
5870 else if (sh_type
== elfcpp::SHT_RELA
)
5871 // We do not support RELA type relocations yet. This is provided for
5873 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
5878 needs_special_offset_handling
,
5886 // Group input sections for stub generation.
5888 // We goup input sections in an output sections so that the total size,
5889 // including any padding space due to alignment is smaller than GROUP_SIZE
5890 // unless the only input section in group is bigger than GROUP_SIZE already.
5891 // Then an ARM stub table is created to follow the last input section
5892 // in group. For each group an ARM stub table is created an is placed
5893 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
5894 // extend the group after the stub table.
5896 template<bool big_endian
>
5898 Target_arm
<big_endian
>::group_sections(
5900 section_size_type group_size
,
5901 bool stubs_always_after_branch
)
5903 // Group input sections and insert stub table
5904 Layout::Section_list section_list
;
5905 layout
->get_allocated_sections(§ion_list
);
5906 for (Layout::Section_list::const_iterator p
= section_list
.begin();
5907 p
!= section_list
.end();
5910 Arm_output_section
<big_endian
>* output_section
=
5911 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
5912 output_section
->group_sections(group_size
, stubs_always_after_branch
,
5917 // Relaxation hook. This is where we do stub generation.
5919 template<bool big_endian
>
5921 Target_arm
<big_endian
>::do_relax(
5923 const Input_objects
* input_objects
,
5924 Symbol_table
* symtab
,
5927 // No need to generate stubs if this is a relocatable link.
5928 gold_assert(!parameters
->options().relocatable());
5930 // If this is the first pass, we need to group input sections into
5934 // Determine the stub group size. The group size is the absolute
5935 // value of the parameter --stub-group-size. If --stub-group-size
5936 // is passed a negative value, we restict stubs to be always after
5937 // the stubbed branches.
5938 int32_t stub_group_size_param
=
5939 parameters
->options().stub_group_size();
5940 bool stubs_always_after_branch
= stub_group_size_param
< 0;
5941 section_size_type stub_group_size
= abs(stub_group_size_param
);
5943 if (stub_group_size
== 1)
5946 // Thumb branch range is +-4MB has to be used as the default
5947 // maximum size (a given section can contain both ARM and Thumb
5948 // code, so the worst case has to be taken into account).
5950 // This value is 24K less than that, which allows for 2025
5951 // 12-byte stubs. If we exceed that, then we will fail to link.
5952 // The user will have to relink with an explicit group size
5954 stub_group_size
= 4170000;
5957 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
5960 // clear changed flags for all stub_tables
5961 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
5962 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5963 sp
!= this->stub_tables_
.end();
5965 (*sp
)->set_has_been_changed(false);
5967 // scan relocs for stubs
5968 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
5969 op
!= input_objects
->relobj_end();
5972 Arm_relobj
<big_endian
>* arm_relobj
=
5973 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
5974 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
5977 bool any_stub_table_changed
= false;
5978 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5979 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
5982 if ((*sp
)->has_been_changed())
5983 any_stub_table_changed
= true;
5986 return any_stub_table_changed
;
5991 template<bool big_endian
>
5993 Target_arm
<big_endian
>::relocate_stub(
5995 const Relocate_info
<32, big_endian
>* relinfo
,
5996 Output_section
* output_section
,
5997 unsigned char* view
,
5998 Arm_address address
,
5999 section_size_type view_size
)
6002 const Stub_template
* stub_template
= stub
->stub_template();
6003 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
6005 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
6006 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
6008 unsigned int r_type
= insn
->r_type();
6009 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
6010 section_size_type reloc_size
= insn
->size();
6011 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
6013 // This is the address of the stub destination.
6014 Arm_address target
= stub
->reloc_target(i
);
6015 Symbol_value
<32> symval
;
6016 symval
.set_output_value(target
);
6018 // Synthesize a fake reloc just in case. We don't have a symbol so
6020 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
6021 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
6022 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
6023 reloc_write
.put_r_offset(reloc_offset
);
6024 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
6025 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
6027 relocate
.relocate(relinfo
, this, output_section
,
6028 this->fake_relnum_for_stubs
, rel
, r_type
,
6029 NULL
, &symval
, view
+ reloc_offset
,
6030 address
+ reloc_offset
, reloc_size
);
6034 // The selector for arm object files.
6036 template<bool big_endian
>
6037 class Target_selector_arm
: public Target_selector
6040 Target_selector_arm()
6041 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
6042 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
6046 do_instantiate_target()
6047 { return new Target_arm
<big_endian
>(); }
6050 Target_selector_arm
<false> target_selector_arm
;
6051 Target_selector_arm
<true> target_selector_armbe
;
6053 } // End anonymous namespace.