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 Arm_symbol 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 General_options
& options
,
976 const Symbol_table
* symtab
, const Layout
* layout
,
977 const unsigned char* pshdrs
,
978 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
980 // Read the symbol information.
982 do_read_symbols(Read_symbols_data
* sd
);
985 // List of stub tables.
986 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
987 Stub_table_list stub_tables_
;
988 // Bit vector to tell if a local symbol is a thumb function or not.
989 // This is only valid after do_count_local_symbol is called.
990 std::vector
<bool> local_symbol_is_thumb_function_
;
991 // processor-specific flags in ELF file header.
992 elfcpp::Elf_Word processor_specific_flags_
;
997 template<bool big_endian
>
998 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1001 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1002 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1003 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1004 processor_specific_flags_(0)
1010 // Downcast a base pointer to an Arm_relobj pointer. This is
1011 // not type-safe but we only use Arm_relobj not the base class.
1012 static Arm_dynobj
<big_endian
>*
1013 as_arm_dynobj(Dynobj
* dynobj
)
1014 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1016 // Processor-specific flags in ELF file header. This is valid only after
1019 processor_specific_flags() const
1020 { return this->processor_specific_flags_
; }
1023 // Read the symbol information.
1025 do_read_symbols(Read_symbols_data
* sd
);
1028 // processor-specific flags in ELF file header.
1029 elfcpp::Elf_Word processor_specific_flags_
;
1032 // Functor to read reloc addends during stub generation.
1034 template<int sh_type
, bool big_endian
>
1035 struct Stub_addend_reader
1037 // Return the addend for a relocation of a particular type. Depending
1038 // on whether this is a REL or RELA relocation, read the addend from a
1039 // view or from a Reloc object.
1040 elfcpp::Elf_types
<32>::Elf_Swxword
1042 unsigned int /* r_type */,
1043 const unsigned char* /* view */,
1044 const typename Reloc_types
<sh_type
,
1045 32, big_endian
>::Reloc
& /* reloc */) const;
1048 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1050 template<bool big_endian
>
1051 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1053 elfcpp::Elf_types
<32>::Elf_Swxword
1056 const unsigned char*,
1057 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1060 // Specialized Stub_addend_reader for RELA type relocation sections.
1061 // We currently do not handle RELA type relocation sections but it is trivial
1062 // to implement the addend reader. This is provided for completeness and to
1063 // make it easier to add support for RELA relocation sections in the future.
1065 template<bool big_endian
>
1066 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1068 elfcpp::Elf_types
<32>::Elf_Swxword
1071 const unsigned char*,
1072 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1073 big_endian
>::Reloc
& reloc
) const
1074 { return reloc
.get_r_addend(); }
1077 // Utilities for manipulating integers of up to 32-bits
1081 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1082 // an int32_t. NO_BITS must be between 1 to 32.
1083 template<int no_bits
>
1084 static inline int32_t
1085 sign_extend(uint32_t bits
)
1087 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1089 return static_cast<int32_t>(bits
);
1090 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1092 uint32_t top_bit
= 1U << (no_bits
- 1);
1093 int32_t as_signed
= static_cast<int32_t>(bits
);
1094 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1097 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1098 template<int no_bits
>
1100 has_overflow(uint32_t bits
)
1102 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1105 int32_t max
= (1 << (no_bits
- 1)) - 1;
1106 int32_t min
= -(1 << (no_bits
- 1));
1107 int32_t as_signed
= static_cast<int32_t>(bits
);
1108 return as_signed
> max
|| as_signed
< min
;
1111 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1112 // fits in the given number of bits as either a signed or unsigned value.
1113 // For example, has_signed_unsigned_overflow<8> would check
1114 // -128 <= bits <= 255
1115 template<int no_bits
>
1117 has_signed_unsigned_overflow(uint32_t bits
)
1119 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1122 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1123 int32_t min
= -(1 << (no_bits
- 1));
1124 int32_t as_signed
= static_cast<int32_t>(bits
);
1125 return as_signed
> max
|| as_signed
< min
;
1128 // Select bits from A and B using bits in MASK. For each n in [0..31],
1129 // the n-th bit in the result is chosen from the n-th bits of A and B.
1130 // A zero selects A and a one selects B.
1131 static inline uint32_t
1132 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1133 { return (a
& ~mask
) | (b
& mask
); }
1136 template<bool big_endian
>
1137 class Target_arm
: public Sized_target
<32, big_endian
>
1140 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1144 : Sized_target
<32, big_endian
>(&arm_info
),
1145 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1146 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
),
1147 may_use_blx_(true), should_force_pic_veneer_(false)
1150 // Whether we can use BLX.
1153 { return this->may_use_blx_
; }
1155 // Set use-BLX flag.
1157 set_may_use_blx(bool value
)
1158 { this->may_use_blx_
= value
; }
1160 // Whether we force PCI branch veneers.
1162 should_force_pic_veneer() const
1163 { return this->should_force_pic_veneer_
; }
1165 // Set PIC veneer flag.
1167 set_should_force_pic_veneer(bool value
)
1168 { this->should_force_pic_veneer_
= value
; }
1170 // Whether we use THUMB-2 instructions.
1172 using_thumb2() const
1174 // FIXME: This should not hard-coded.
1178 // Whether we use THUMB/THUMB-2 instructions only.
1180 using_thumb_only() const
1182 // FIXME: This should not hard-coded.
1186 // Process the relocations to determine unreferenced sections for
1187 // garbage collection.
1189 gc_process_relocs(Symbol_table
* symtab
,
1191 Sized_relobj
<32, big_endian
>* object
,
1192 unsigned int data_shndx
,
1193 unsigned int sh_type
,
1194 const unsigned char* prelocs
,
1196 Output_section
* output_section
,
1197 bool needs_special_offset_handling
,
1198 size_t local_symbol_count
,
1199 const unsigned char* plocal_symbols
);
1201 // Scan the relocations to look for symbol adjustments.
1203 scan_relocs(Symbol_table
* symtab
,
1205 Sized_relobj
<32, big_endian
>* object
,
1206 unsigned int data_shndx
,
1207 unsigned int sh_type
,
1208 const unsigned char* prelocs
,
1210 Output_section
* output_section
,
1211 bool needs_special_offset_handling
,
1212 size_t local_symbol_count
,
1213 const unsigned char* plocal_symbols
);
1215 // Finalize the sections.
1217 do_finalize_sections(Layout
*, const Input_objects
*);
1219 // Return the value to use for a dynamic symbol which requires special
1222 do_dynsym_value(const Symbol
*) const;
1224 // Relocate a section.
1226 relocate_section(const Relocate_info
<32, big_endian
>*,
1227 unsigned int sh_type
,
1228 const unsigned char* prelocs
,
1230 Output_section
* output_section
,
1231 bool needs_special_offset_handling
,
1232 unsigned char* view
,
1233 Arm_address view_address
,
1234 section_size_type view_size
,
1235 const Reloc_symbol_changes
*);
1237 // Scan the relocs during a relocatable link.
1239 scan_relocatable_relocs(Symbol_table
* symtab
,
1241 Sized_relobj
<32, big_endian
>* object
,
1242 unsigned int data_shndx
,
1243 unsigned int sh_type
,
1244 const unsigned char* prelocs
,
1246 Output_section
* output_section
,
1247 bool needs_special_offset_handling
,
1248 size_t local_symbol_count
,
1249 const unsigned char* plocal_symbols
,
1250 Relocatable_relocs
*);
1252 // Relocate a section during a relocatable link.
1254 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1255 unsigned int sh_type
,
1256 const unsigned char* prelocs
,
1258 Output_section
* output_section
,
1259 off_t offset_in_output_section
,
1260 const Relocatable_relocs
*,
1261 unsigned char* view
,
1262 Arm_address view_address
,
1263 section_size_type view_size
,
1264 unsigned char* reloc_view
,
1265 section_size_type reloc_view_size
);
1267 // Return whether SYM is defined by the ABI.
1269 do_is_defined_by_abi(Symbol
* sym
) const
1270 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1272 // Return the size of the GOT section.
1276 gold_assert(this->got_
!= NULL
);
1277 return this->got_
->data_size();
1280 // Map platform-specific reloc types
1282 get_real_reloc_type (unsigned int r_type
);
1284 // Get the default ARM target.
1285 static const Target_arm
<big_endian
>&
1288 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1289 && parameters
->target().is_big_endian() == big_endian
);
1290 return static_cast<const Target_arm
<big_endian
>&>(parameters
->target());
1295 do_adjust_elf_header(unsigned char* view
, int len
) const;
1298 // The class which scans relocations.
1303 : issued_non_pic_error_(false)
1307 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1308 Sized_relobj
<32, big_endian
>* object
,
1309 unsigned int data_shndx
,
1310 Output_section
* output_section
,
1311 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1312 const elfcpp::Sym
<32, big_endian
>& lsym
);
1315 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1316 Sized_relobj
<32, big_endian
>* object
,
1317 unsigned int data_shndx
,
1318 Output_section
* output_section
,
1319 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1324 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1325 unsigned int r_type
);
1328 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1329 unsigned int r_type
, Symbol
*);
1332 check_non_pic(Relobj
*, unsigned int r_type
);
1334 // Almost identical to Symbol::needs_plt_entry except that it also
1335 // handles STT_ARM_TFUNC.
1337 symbol_needs_plt_entry(const Symbol
* sym
)
1339 // An undefined symbol from an executable does not need a PLT entry.
1340 if (sym
->is_undefined() && !parameters
->options().shared())
1343 return (!parameters
->doing_static_link()
1344 && (sym
->type() == elfcpp::STT_FUNC
1345 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1346 && (sym
->is_from_dynobj()
1347 || sym
->is_undefined()
1348 || sym
->is_preemptible()));
1351 // Whether we have issued an error about a non-PIC compilation.
1352 bool issued_non_pic_error_
;
1355 // The class which implements relocation.
1365 // Return whether the static relocation needs to be applied.
1367 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1370 Output_section
* output_section
);
1372 // Do a relocation. Return false if the caller should not issue
1373 // any warnings about this relocation.
1375 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1376 Output_section
*, size_t relnum
,
1377 const elfcpp::Rel
<32, big_endian
>&,
1378 unsigned int r_type
, const Sized_symbol
<32>*,
1379 const Symbol_value
<32>*,
1380 unsigned char*, Arm_address
,
1383 // Return whether we want to pass flag NON_PIC_REF for this
1386 reloc_is_non_pic (unsigned int r_type
)
1390 case elfcpp::R_ARM_REL32
:
1391 case elfcpp::R_ARM_THM_CALL
:
1392 case elfcpp::R_ARM_CALL
:
1393 case elfcpp::R_ARM_JUMP24
:
1394 case elfcpp::R_ARM_PREL31
:
1395 case elfcpp::R_ARM_THM_ABS5
:
1396 case elfcpp::R_ARM_ABS8
:
1397 case elfcpp::R_ARM_ABS12
:
1398 case elfcpp::R_ARM_ABS16
:
1399 case elfcpp::R_ARM_BASE_ABS
:
1407 // A class which returns the size required for a relocation type,
1408 // used while scanning relocs during a relocatable link.
1409 class Relocatable_size_for_reloc
1413 get_size_for_reloc(unsigned int, Relobj
*);
1416 // Get the GOT section, creating it if necessary.
1417 Output_data_got
<32, big_endian
>*
1418 got_section(Symbol_table
*, Layout
*);
1420 // Get the GOT PLT section.
1422 got_plt_section() const
1424 gold_assert(this->got_plt_
!= NULL
);
1425 return this->got_plt_
;
1428 // Create a PLT entry for a global symbol.
1430 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1432 // Get the PLT section.
1433 const Output_data_plt_arm
<big_endian
>*
1436 gold_assert(this->plt_
!= NULL
);
1440 // Get the dynamic reloc section, creating it if necessary.
1442 rel_dyn_section(Layout
*);
1444 // Return true if the symbol may need a COPY relocation.
1445 // References from an executable object to non-function symbols
1446 // defined in a dynamic object may need a COPY relocation.
1448 may_need_copy_reloc(Symbol
* gsym
)
1450 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1451 && gsym
->may_need_copy_reloc());
1454 // Add a potential copy relocation.
1456 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1457 Sized_relobj
<32, big_endian
>* object
,
1458 unsigned int shndx
, Output_section
* output_section
,
1459 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1461 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1462 symtab
->get_sized_symbol
<32>(sym
),
1463 object
, shndx
, output_section
, reloc
,
1464 this->rel_dyn_section(layout
));
1467 // Whether two EABI versions are compatible.
1469 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1471 // Merge processor-specific flags from input object and those in the ELF
1472 // header of the output.
1474 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1477 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1478 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1481 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1482 const elfcpp::Ehdr
<32, !big_endian
>&)
1483 { gold_unreachable(); }
1486 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1487 const elfcpp::Ehdr
<64, false>&)
1488 { gold_unreachable(); }
1491 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1492 const elfcpp::Ehdr
<64, true>&)
1493 { gold_unreachable(); }
1495 // Information about this specific target which we pass to the
1496 // general Target structure.
1497 static const Target::Target_info arm_info
;
1499 // The types of GOT entries needed for this platform.
1502 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1506 Output_data_got
<32, big_endian
>* got_
;
1508 Output_data_plt_arm
<big_endian
>* plt_
;
1509 // The GOT PLT section.
1510 Output_data_space
* got_plt_
;
1511 // The dynamic reloc section.
1512 Reloc_section
* rel_dyn_
;
1513 // Relocs saved to avoid a COPY reloc.
1514 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1515 // Space for variables copied with a COPY reloc.
1516 Output_data_space
* dynbss_
;
1517 // Whether we can use BLX.
1519 // Whether we force PIC branch veneers.
1520 bool should_force_pic_veneer_
;
1523 template<bool big_endian
>
1524 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1527 big_endian
, // is_big_endian
1528 elfcpp::EM_ARM
, // machine_code
1529 false, // has_make_symbol
1530 false, // has_resolve
1531 false, // has_code_fill
1532 true, // is_default_stack_executable
1534 "/usr/lib/libc.so.1", // dynamic_linker
1535 0x8000, // default_text_segment_address
1536 0x1000, // abi_pagesize (overridable by -z max-page-size)
1537 0x1000, // common_pagesize (overridable by -z common-page-size)
1538 elfcpp::SHN_UNDEF
, // small_common_shndx
1539 elfcpp::SHN_UNDEF
, // large_common_shndx
1540 0, // small_common_section_flags
1541 0 // large_common_section_flags
1544 // Arm relocate functions class
1547 template<bool big_endian
>
1548 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1553 STATUS_OKAY
, // No error during relocation.
1554 STATUS_OVERFLOW
, // Relocation oveflow.
1555 STATUS_BAD_RELOC
// Relocation cannot be applied.
1559 typedef Relocate_functions
<32, big_endian
> Base
;
1560 typedef Arm_relocate_functions
<big_endian
> This
;
1562 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1563 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1564 // to distinguish ARM and THUMB functions and it is treated specially.
1565 static inline Symbol_value
<32>::Value
1566 arm_symbol_value (const Sized_relobj
<32, big_endian
> *object
,
1567 const Symbol_value
<32>* psymval
,
1568 Symbol_value
<32>::Value addend
,
1571 typedef Symbol_value
<32>::Value Valtype
;
1575 Valtype raw
= psymval
->value(object
, 0);
1576 Valtype thumb_bit
= raw
& 1;
1577 return ((raw
& ~((Valtype
) 1)) + addend
) | thumb_bit
;
1580 return psymval
->value(object
, addend
);
1583 // Encoding of imm16 argument for movt and movw ARM instructions
1586 // imm16 := imm4 | imm12
1588 // 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
1589 // +-------+---------------+-------+-------+-----------------------+
1590 // | | |imm4 | |imm12 |
1591 // +-------+---------------+-------+-------+-----------------------+
1593 // Extract the relocation addend from VAL based on the ARM
1594 // instruction encoding described above.
1595 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1596 extract_arm_movw_movt_addend(
1597 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1599 // According to the Elf ABI for ARM Architecture the immediate
1600 // field is sign-extended to form the addend.
1601 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1604 // Insert X into VAL based on the ARM instruction encoding described
1606 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1607 insert_val_arm_movw_movt(
1608 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1609 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1613 val
|= (x
& 0xf000) << 4;
1617 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1620 // imm16 := imm4 | i | imm3 | imm8
1622 // 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
1623 // +---------+-+-----------+-------++-+-----+-------+---------------+
1624 // | |i| |imm4 || |imm3 | |imm8 |
1625 // +---------+-+-----------+-------++-+-----+-------+---------------+
1627 // Extract the relocation addend from VAL based on the Thumb2
1628 // instruction encoding described above.
1629 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1630 extract_thumb_movw_movt_addend(
1631 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1633 // According to the Elf ABI for ARM Architecture the immediate
1634 // field is sign-extended to form the addend.
1635 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1636 | ((val
>> 15) & 0x0800)
1637 | ((val
>> 4) & 0x0700)
1641 // Insert X into VAL based on the Thumb2 instruction encoding
1643 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1644 insert_val_thumb_movw_movt(
1645 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1646 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1649 val
|= (x
& 0xf000) << 4;
1650 val
|= (x
& 0x0800) << 15;
1651 val
|= (x
& 0x0700) << 4;
1652 val
|= (x
& 0x00ff);
1656 // FIXME: This probably only works for Android on ARM v5te. We should
1657 // following GNU ld for the general case.
1658 template<unsigned r_type
>
1659 static inline typename
This::Status
1660 arm_branch_common(unsigned char *view
,
1661 const Sized_relobj
<32, big_endian
>* object
,
1662 const Symbol_value
<32>* psymval
,
1663 Arm_address address
,
1666 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1667 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1668 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1670 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
1671 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
1672 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
1673 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
1674 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
1675 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
1676 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
1678 if (r_type
== elfcpp::R_ARM_CALL
)
1680 if (!insn_is_uncond_bl
&& !insn_is_blx
)
1681 return This::STATUS_BAD_RELOC
;
1683 else if (r_type
== elfcpp::R_ARM_JUMP24
)
1685 if (!insn_is_b
&& !insn_is_cond_bl
)
1686 return This::STATUS_BAD_RELOC
;
1688 else if (r_type
== elfcpp::R_ARM_PLT32
)
1690 if (!insn_is_any_branch
)
1691 return This::STATUS_BAD_RELOC
;
1696 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
1697 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1700 // If target has thumb bit set, we need to either turn the BL
1701 // into a BLX (for ARMv5 or above) or generate a stub.
1705 if (insn_is_uncond_bl
)
1706 val
= (val
& 0xffffff) | 0xfa000000 | ((x
& 2) << 23);
1708 return This::STATUS_BAD_RELOC
;
1711 gold_assert(!insn_is_blx
);
1713 val
= utils::bit_select(val
, (x
>> 2), 0xffffffUL
);
1714 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1715 return (utils::has_overflow
<26>(x
)
1716 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
1721 // R_ARM_ABS8: S + A
1722 static inline typename
This::Status
1723 abs8(unsigned char *view
,
1724 const Sized_relobj
<32, big_endian
>* object
,
1725 const Symbol_value
<32>* psymval
)
1727 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1728 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1729 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1730 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1731 Reltype addend
= utils::sign_extend
<8>(val
);
1732 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1733 val
= utils::bit_select(val
, x
, 0xffU
);
1734 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1735 return (utils::has_signed_unsigned_overflow
<8>(x
)
1736 ? This::STATUS_OVERFLOW
1737 : This::STATUS_OKAY
);
1740 // R_ARM_THM_ABS5: S + A
1741 static inline typename
This::Status
1742 thm_abs5(unsigned char *view
,
1743 const Sized_relobj
<32, big_endian
>* object
,
1744 const Symbol_value
<32>* psymval
)
1746 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1747 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1748 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1749 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1750 Reltype addend
= (val
& 0x7e0U
) >> 6;
1751 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1752 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1753 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1754 return (utils::has_overflow
<5>(x
)
1755 ? This::STATUS_OVERFLOW
1756 : This::STATUS_OKAY
);
1759 // R_ARM_ABS12: S + A
1760 static inline typename
This::Status
1761 abs12(unsigned char *view
,
1762 const Sized_relobj
<32, big_endian
>* object
,
1763 const Symbol_value
<32>* psymval
)
1765 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1766 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1767 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1768 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1769 Reltype addend
= val
& 0x0fffU
;
1770 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1771 val
= utils::bit_select(val
, x
, 0x0fffU
);
1772 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1773 return (utils::has_overflow
<12>(x
)
1774 ? This::STATUS_OVERFLOW
1775 : This::STATUS_OKAY
);
1778 // R_ARM_ABS16: S + A
1779 static inline typename
This::Status
1780 abs16(unsigned char *view
,
1781 const Sized_relobj
<32, big_endian
>* object
,
1782 const Symbol_value
<32>* psymval
)
1784 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1785 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1786 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1787 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1788 Reltype addend
= utils::sign_extend
<16>(val
);
1789 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, false);
1790 val
= utils::bit_select(val
, x
, 0xffffU
);
1791 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1792 return (utils::has_signed_unsigned_overflow
<16>(x
)
1793 ? This::STATUS_OVERFLOW
1794 : This::STATUS_OKAY
);
1797 // R_ARM_ABS32: (S + A) | T
1798 static inline typename
This::Status
1799 abs32(unsigned char *view
,
1800 const Sized_relobj
<32, big_endian
>* object
,
1801 const Symbol_value
<32>* psymval
,
1804 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1805 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1806 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1807 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
1808 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1809 return This::STATUS_OKAY
;
1812 // R_ARM_REL32: (S + A) | T - P
1813 static inline typename
This::Status
1814 rel32(unsigned char *view
,
1815 const Sized_relobj
<32, big_endian
>* object
,
1816 const Symbol_value
<32>* psymval
,
1817 Arm_address address
,
1820 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1821 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1822 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1823 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1825 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1826 return This::STATUS_OKAY
;
1829 // R_ARM_THM_CALL: (S + A) | T - P
1830 static inline typename
This::Status
1831 thm_call(unsigned char *view
,
1832 const Sized_relobj
<32, big_endian
>* object
,
1833 const Symbol_value
<32>* psymval
,
1834 Arm_address address
,
1837 // A thumb call consists of two instructions.
1838 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1839 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1840 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1841 Valtype hi
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1842 Valtype lo
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
1843 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1844 gold_assert((lo
& 0xf800) == 0xf800);
1845 Reltype addend
= utils::sign_extend
<23>(((hi
& 0x7ff) << 12)
1846 | ((lo
& 0x7ff) << 1));
1847 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1850 // If target has no thumb bit set, we need to either turn the BL
1851 // into a BLX (for ARMv5 or above) or generate a stub.
1854 // This only works for ARMv5 and above with interworking enabled.
1857 hi
= utils::bit_select(hi
, (x
>> 12), 0x7ffU
);
1858 lo
= utils::bit_select(lo
, (x
>> 1), 0x7ffU
);
1859 elfcpp::Swap
<16, big_endian
>::writeval(wv
, hi
);
1860 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lo
);
1861 return (utils::has_overflow
<23>(x
)
1862 ? This::STATUS_OVERFLOW
1863 : This::STATUS_OKAY
);
1866 // R_ARM_BASE_PREL: B(S) + A - P
1867 static inline typename
This::Status
1868 base_prel(unsigned char* view
,
1870 Arm_address address
)
1872 Base::rel32(view
, origin
- address
);
1876 // R_ARM_BASE_ABS: B(S) + A
1877 static inline typename
This::Status
1878 base_abs(unsigned char* view
,
1881 Base::rel32(view
, origin
);
1885 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1886 static inline typename
This::Status
1887 got_brel(unsigned char* view
,
1888 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
1890 Base::rel32(view
, got_offset
);
1891 return This::STATUS_OKAY
;
1894 // R_ARM_GOT_PREL: GOT(S) + A – P
1895 static inline typename
This::Status
1896 got_prel(unsigned char* view
,
1897 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
,
1898 Arm_address address
)
1900 Base::rel32(view
, got_offset
- address
);
1901 return This::STATUS_OKAY
;
1904 // R_ARM_PLT32: (S + A) | T - P
1905 static inline typename
This::Status
1906 plt32(unsigned char *view
,
1907 const Sized_relobj
<32, big_endian
>* object
,
1908 const Symbol_value
<32>* psymval
,
1909 Arm_address address
,
1912 return arm_branch_common
<elfcpp::R_ARM_PLT32
>(view
, object
, psymval
,
1913 address
, has_thumb_bit
);
1916 // R_ARM_CALL: (S + A) | T - P
1917 static inline typename
This::Status
1918 call(unsigned char *view
,
1919 const Sized_relobj
<32, big_endian
>* object
,
1920 const Symbol_value
<32>* psymval
,
1921 Arm_address address
,
1924 return arm_branch_common
<elfcpp::R_ARM_CALL
>(view
, object
, psymval
,
1925 address
, has_thumb_bit
);
1928 // R_ARM_JUMP24: (S + A) | T - P
1929 static inline typename
This::Status
1930 jump24(unsigned char *view
,
1931 const Sized_relobj
<32, big_endian
>* object
,
1932 const Symbol_value
<32>* psymval
,
1933 Arm_address address
,
1936 return arm_branch_common
<elfcpp::R_ARM_JUMP24
>(view
, object
, psymval
,
1937 address
, has_thumb_bit
);
1940 // R_ARM_PREL: (S + A) | T - P
1941 static inline typename
This::Status
1942 prel31(unsigned char *view
,
1943 const Sized_relobj
<32, big_endian
>* object
,
1944 const Symbol_value
<32>* psymval
,
1945 Arm_address address
,
1948 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1949 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1950 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1951 Valtype addend
= utils::sign_extend
<31>(val
);
1952 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
1954 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
1955 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1956 return (utils::has_overflow
<31>(x
) ?
1957 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
1960 // R_ARM_MOVW_ABS_NC: (S + A) | T
1961 static inline typename
This::Status
1962 movw_abs_nc(unsigned char *view
,
1963 const Sized_relobj
<32, big_endian
>* object
,
1964 const Symbol_value
<32>* psymval
,
1967 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1968 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1969 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1970 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1971 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
1972 val
= This::insert_val_arm_movw_movt(val
, x
);
1973 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1974 return This::STATUS_OKAY
;
1977 // R_ARM_MOVT_ABS: S + A
1978 static inline typename
This::Status
1979 movt_abs(unsigned char *view
,
1980 const Sized_relobj
<32, big_endian
>* object
,
1981 const Symbol_value
<32>* psymval
)
1983 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1984 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1985 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1986 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
1987 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, 0) >> 16;
1988 val
= This::insert_val_arm_movw_movt(val
, x
);
1989 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1990 return This::STATUS_OKAY
;
1993 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1994 static inline typename
This::Status
1995 thm_movw_abs_nc(unsigned char *view
,
1996 const Sized_relobj
<32, big_endian
>* object
,
1997 const Symbol_value
<32>* psymval
,
2000 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2001 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2002 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2003 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2004 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2005 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2006 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
2007 val
= This::insert_val_thumb_movw_movt(val
, x
);
2008 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2009 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2010 return This::STATUS_OKAY
;
2013 // R_ARM_THM_MOVT_ABS: S + A
2014 static inline typename
This::Status
2015 thm_movt_abs(unsigned char *view
,
2016 const Sized_relobj
<32, big_endian
>* object
,
2017 const Symbol_value
<32>* psymval
)
2019 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2020 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2021 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2022 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2023 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2024 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2025 Reltype x
= This::arm_symbol_value(object
, psymval
, addend
, 0) >> 16;
2026 val
= This::insert_val_thumb_movw_movt(val
, x
);
2027 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2028 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2029 return This::STATUS_OKAY
;
2032 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2033 static inline typename
This::Status
2034 movw_prel_nc(unsigned char *view
,
2035 const Sized_relobj
<32, big_endian
>* object
,
2036 const Symbol_value
<32>* psymval
,
2037 Arm_address address
,
2040 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2041 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2042 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2043 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2044 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
2046 val
= This::insert_val_arm_movw_movt(val
, x
);
2047 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2048 return This::STATUS_OKAY
;
2051 // R_ARM_MOVT_PREL: S + A - P
2052 static inline typename
This::Status
2053 movt_prel(unsigned char *view
,
2054 const Sized_relobj
<32, big_endian
>* object
,
2055 const Symbol_value
<32>* psymval
,
2056 Arm_address address
)
2058 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2059 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2060 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2061 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2062 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, 0)
2064 val
= This::insert_val_arm_movw_movt(val
, x
);
2065 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2066 return This::STATUS_OKAY
;
2069 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2070 static inline typename
This::Status
2071 thm_movw_prel_nc(unsigned char *view
,
2072 const Sized_relobj
<32, big_endian
>* object
,
2073 const Symbol_value
<32>* psymval
,
2074 Arm_address address
,
2077 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2078 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2079 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2080 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2081 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2082 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2083 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
2085 val
= This::insert_val_thumb_movw_movt(val
, x
);
2086 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2087 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2088 return This::STATUS_OKAY
;
2091 // R_ARM_THM_MOVT_PREL: S + A - P
2092 static inline typename
This::Status
2093 thm_movt_prel(unsigned char *view
,
2094 const Sized_relobj
<32, big_endian
>* object
,
2095 const Symbol_value
<32>* psymval
,
2096 Arm_address address
)
2098 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2099 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2100 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2101 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2102 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2103 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2104 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, 0)
2106 val
= This::insert_val_thumb_movw_movt(val
, x
);
2107 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2108 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2109 return This::STATUS_OKAY
;
2113 // Get the GOT section, creating it if necessary.
2115 template<bool big_endian
>
2116 Output_data_got
<32, big_endian
>*
2117 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2119 if (this->got_
== NULL
)
2121 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2123 this->got_
= new Output_data_got
<32, big_endian
>();
2126 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2128 | elfcpp::SHF_WRITE
),
2132 // The old GNU linker creates a .got.plt section. We just
2133 // create another set of data in the .got section. Note that we
2134 // always create a PLT if we create a GOT, although the PLT
2136 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2137 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2139 | elfcpp::SHF_WRITE
),
2143 // The first three entries are reserved.
2144 this->got_plt_
->set_current_data_size(3 * 4);
2146 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2147 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2149 0, 0, elfcpp::STT_OBJECT
,
2151 elfcpp::STV_HIDDEN
, 0,
2157 // Get the dynamic reloc section, creating it if necessary.
2159 template<bool big_endian
>
2160 typename Target_arm
<big_endian
>::Reloc_section
*
2161 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2163 if (this->rel_dyn_
== NULL
)
2165 gold_assert(layout
!= NULL
);
2166 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2167 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2168 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
2170 return this->rel_dyn_
;
2173 // Insn_template methods.
2175 // Return byte size of an instruction template.
2178 Insn_template::size() const
2180 switch (this->type())
2193 // Return alignment of an instruction template.
2196 Insn_template::alignment() const
2198 switch (this->type())
2211 // Stub_template methods.
2213 Stub_template::Stub_template(
2214 Stub_type type
, const Insn_template
* insns
,
2216 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2217 entry_in_thumb_mode_(false), relocs_()
2221 // Compute byte size and alignment of stub template.
2222 for (size_t i
= 0; i
< insn_count
; i
++)
2224 unsigned insn_alignment
= insns
[i
].alignment();
2225 size_t insn_size
= insns
[i
].size();
2226 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2227 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2228 switch (insns
[i
].type())
2230 case Insn_template::THUMB16_TYPE
:
2232 this->entry_in_thumb_mode_
= true;
2235 case Insn_template::THUMB32_TYPE
:
2236 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2237 this->relocs_
.push_back(Reloc(i
, offset
));
2239 this->entry_in_thumb_mode_
= true;
2242 case Insn_template::ARM_TYPE
:
2243 // Handle cases where the target is encoded within the
2245 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2246 this->relocs_
.push_back(Reloc(i
, offset
));
2249 case Insn_template::DATA_TYPE
:
2250 // Entry point cannot be data.
2251 gold_assert(i
!= 0);
2252 this->relocs_
.push_back(Reloc(i
, offset
));
2258 offset
+= insn_size
;
2260 this->size_
= offset
;
2263 // Reloc_stub::Key methods.
2265 // Dump a Key as a string for debugging.
2268 Reloc_stub::Key::name() const
2270 if (this->r_sym_
== invalid_index
)
2272 // Global symbol key name
2273 // <stub-type>:<symbol name>:<addend>.
2274 const std::string sym_name
= this->u_
.symbol
->name();
2275 // We need to print two hex number and two colons. So just add 100 bytes
2276 // to the symbol name size.
2277 size_t len
= sym_name
.size() + 100;
2278 char* buffer
= new char[len
];
2279 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2280 sym_name
.c_str(), this->addend_
);
2281 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2283 return std::string(buffer
);
2287 // local symbol key name
2288 // <stub-type>:<object>:<r_sym>:<addend>.
2289 const size_t len
= 200;
2291 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2292 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2293 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2294 return std::string(buffer
);
2298 // Reloc_stub methods.
2300 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2301 // LOCATION to DESTINATION.
2302 // This code is based on the arm_type_of_stub function in
2303 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2307 Reloc_stub::stub_type_for_reloc(
2308 unsigned int r_type
,
2309 Arm_address location
,
2310 Arm_address destination
,
2311 bool target_is_thumb
)
2313 Stub_type stub_type
= arm_stub_none
;
2315 // This is a bit ugly but we want to avoid using a templated class for
2316 // big and little endianities.
2318 bool should_force_pic_veneer
;
2321 if (parameters
->target().is_big_endian())
2323 const Target_arm
<true>& big_endian_target
=
2324 Target_arm
<true>::default_target();
2325 may_use_blx
= big_endian_target
.may_use_blx();
2326 should_force_pic_veneer
= big_endian_target
.should_force_pic_veneer();
2327 thumb2
= big_endian_target
.using_thumb2();
2328 thumb_only
= big_endian_target
.using_thumb_only();
2332 const Target_arm
<false>& little_endian_target
=
2333 Target_arm
<false>::default_target();
2334 may_use_blx
= little_endian_target
.may_use_blx();
2335 should_force_pic_veneer
= little_endian_target
.should_force_pic_veneer();
2336 thumb2
= little_endian_target
.using_thumb2();
2337 thumb_only
= little_endian_target
.using_thumb_only();
2340 int64_t branch_offset
= (int64_t)destination
- location
;
2342 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2344 // Handle cases where:
2345 // - this call goes too far (different Thumb/Thumb2 max
2347 // - it's a Thumb->Arm call and blx is not available, or it's a
2348 // Thumb->Arm branch (not bl). A stub is needed in this case.
2350 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2351 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2353 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2354 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2355 || ((!target_is_thumb
)
2356 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2357 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2359 if (target_is_thumb
)
2364 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2367 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2368 // V5T and above. Stub starts with ARM code, so
2369 // we must be able to switch mode before
2370 // reaching it, which is only possible for 'bl'
2371 // (ie R_ARM_THM_CALL relocation).
2372 ? arm_stub_long_branch_any_thumb_pic
2373 // On V4T, use Thumb code only.
2374 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2378 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2379 ? arm_stub_long_branch_any_any
// V5T and above.
2380 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2384 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2385 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2386 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2393 // FIXME: We should check that the input section is from an
2394 // object that has interwork enabled.
2396 stub_type
= (parameters
->options().shared()
2397 || should_force_pic_veneer
)
2400 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2401 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2402 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2406 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2407 ? arm_stub_long_branch_any_any
// V5T and above.
2408 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2410 // Handle v4t short branches.
2411 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2412 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2413 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2414 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2418 else if (r_type
== elfcpp::R_ARM_CALL
2419 || r_type
== elfcpp::R_ARM_JUMP24
2420 || r_type
== elfcpp::R_ARM_PLT32
)
2422 if (target_is_thumb
)
2426 // FIXME: We should check that the input section is from an
2427 // object that has interwork enabled.
2429 // We have an extra 2-bytes reach because of
2430 // the mode change (bit 24 (H) of BLX encoding).
2431 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2432 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2433 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2434 || (r_type
== elfcpp::R_ARM_JUMP24
)
2435 || (r_type
== elfcpp::R_ARM_PLT32
))
2437 stub_type
= (parameters
->options().shared()
2438 || should_force_pic_veneer
)
2441 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2442 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2446 ? arm_stub_long_branch_any_any
// V5T and above.
2447 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2453 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2454 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2456 stub_type
= (parameters
->options().shared()
2457 || should_force_pic_veneer
)
2458 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2459 : arm_stub_long_branch_any_any
; /// non-PIC.
2467 // Template to implement do_write for a specific target endianity.
2469 template<bool big_endian
>
2471 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2472 section_size_type view_size
)
2474 const Stub_template
* stub_template
= this->stub_template();
2475 const Insn_template
* insns
= stub_template
->insns();
2477 // FIXME: We do not handle BE8 encoding yet.
2478 unsigned char* pov
= view
;
2479 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2481 switch (insns
[i
].type())
2483 case Insn_template::THUMB16_TYPE
:
2484 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2485 gold_assert(insns
[i
].reloc_addend() == 0);
2486 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2488 case Insn_template::THUMB32_TYPE
:
2490 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2491 uint32_t lo
= insns
[i
].data() & 0xffff;
2492 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2493 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2496 case Insn_template::ARM_TYPE
:
2497 case Insn_template::DATA_TYPE
:
2498 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2503 pov
+= insns
[i
].size();
2505 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2508 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2511 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2515 this->do_fixed_endian_write
<true>(view
, view_size
);
2517 this->do_fixed_endian_write
<false>(view
, view_size
);
2520 // Stub_factory methods.
2522 Stub_factory::Stub_factory()
2524 // The instruction template sequences are declared as static
2525 // objects and initialized first time the constructor runs.
2527 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2528 // to reach the stub if necessary.
2529 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2531 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2532 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2533 // dcd R_ARM_ABS32(X)
2536 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2538 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
2540 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2541 Insn_template::arm_insn(0xe12fff1c), // bx ip
2542 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2543 // dcd R_ARM_ABS32(X)
2546 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2547 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
2549 Insn_template::thumb16_insn(0xb401), // push {r0}
2550 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2551 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2552 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2553 Insn_template::thumb16_insn(0x4760), // bx ip
2554 Insn_template::thumb16_insn(0xbf00), // nop
2555 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2556 // dcd R_ARM_ABS32(X)
2559 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2561 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
2563 Insn_template::thumb16_insn(0x4778), // bx pc
2564 Insn_template::thumb16_insn(0x46c0), // nop
2565 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2566 Insn_template::arm_insn(0xe12fff1c), // bx ip
2567 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2568 // dcd R_ARM_ABS32(X)
2571 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2573 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
2575 Insn_template::thumb16_insn(0x4778), // bx pc
2576 Insn_template::thumb16_insn(0x46c0), // nop
2577 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2578 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2579 // dcd R_ARM_ABS32(X)
2582 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2583 // one, when the destination is close enough.
2584 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
2586 Insn_template::thumb16_insn(0x4778), // bx pc
2587 Insn_template::thumb16_insn(0x46c0), // nop
2588 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2591 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2592 // blx to reach the stub if necessary.
2593 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
2595 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2596 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2597 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2598 // dcd R_ARM_REL32(X-4)
2601 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2602 // blx to reach the stub if necessary. We can not add into pc;
2603 // it is not guaranteed to mode switch (different in ARMv6 and
2605 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
2607 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2608 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2609 Insn_template::arm_insn(0xe12fff1c), // bx ip
2610 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2611 // dcd R_ARM_REL32(X)
2614 // V4T ARM -> ARM long branch stub, PIC.
2615 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
2617 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2618 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2619 Insn_template::arm_insn(0xe12fff1c), // bx ip
2620 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2621 // dcd R_ARM_REL32(X)
2624 // V4T Thumb -> ARM long branch stub, PIC.
2625 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
2627 Insn_template::thumb16_insn(0x4778), // bx pc
2628 Insn_template::thumb16_insn(0x46c0), // nop
2629 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2630 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2631 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2632 // dcd R_ARM_REL32(X)
2635 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2637 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
2639 Insn_template::thumb16_insn(0xb401), // push {r0}
2640 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2641 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2642 Insn_template::thumb16_insn(0x4484), // add ip, r0
2643 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2644 Insn_template::thumb16_insn(0x4760), // bx ip
2645 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
2646 // dcd R_ARM_REL32(X)
2649 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2651 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
2653 Insn_template::thumb16_insn(0x4778), // bx pc
2654 Insn_template::thumb16_insn(0x46c0), // nop
2655 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2656 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2657 Insn_template::arm_insn(0xe12fff1c), // bx ip
2658 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2659 // dcd R_ARM_REL32(X)
2662 // Cortex-A8 erratum-workaround stubs.
2664 // Stub used for conditional branches (which may be beyond +/-1MB away,
2665 // so we can't use a conditional branch to reach this stub).
2672 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
2674 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2675 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2676 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2680 // Stub used for b.w and bl.w instructions.
2682 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
2684 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2687 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
2689 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2692 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2693 // instruction (which switches to ARM mode) to point to this stub. Jump to
2694 // the real destination using an ARM-mode branch.
2695 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
2697 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2700 // Fill in the stub template look-up table. Stub templates are constructed
2701 // per instance of Stub_factory for fast look-up without locking
2702 // in a thread-enabled environment.
2704 this->stub_templates_
[arm_stub_none
] =
2705 new Stub_template(arm_stub_none
, NULL
, 0);
2707 #define DEF_STUB(x) \
2711 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2712 Stub_type type = arm_stub_##x; \
2713 this->stub_templates_[type] = \
2714 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2722 // Stub_table methods.
2724 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2725 // if already a STUB with the same key has been added.
2727 template<bool big_endian
>
2729 Stub_table
<big_endian
>::add_reloc_stub(
2731 const Reloc_stub::Key
& key
)
2733 const Stub_template
* stub_template
= stub
->stub_template();
2734 gold_assert(stub_template
->type() == key
.stub_type());
2735 this->reloc_stubs_
[key
] = stub
;
2736 if (this->addralign_
< stub_template
->alignment())
2737 this->addralign_
= stub_template
->alignment();
2738 this->has_been_changed_
= true;
2741 template<bool big_endian
>
2743 Stub_table
<big_endian
>::relocate_stubs(
2744 const Relocate_info
<32, big_endian
>* relinfo
,
2745 Target_arm
<big_endian
>* arm_target
,
2746 Output_section
* output_section
,
2747 unsigned char* view
,
2748 Arm_address address
,
2749 section_size_type view_size
)
2751 // If we are passed a view bigger than the stub table's. we need to
2753 gold_assert(address
== this->address()
2755 == static_cast<section_size_type
>(this->data_size())));
2757 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2758 p
!= this->reloc_stubs_
.end();
2761 Reloc_stub
* stub
= p
->second
;
2762 const Stub_template
* stub_template
= stub
->stub_template();
2763 if (stub_template
->reloc_count() != 0)
2765 // Adjust view to cover the stub only.
2766 section_size_type offset
= stub
->offset();
2767 section_size_type stub_size
= stub_template
->size();
2768 gold_assert(offset
+ stub_size
<= view_size
);
2770 arm_target
->relocate_stub(stub
, relinfo
, output_section
,
2771 view
+ offset
, address
+ offset
,
2777 // Reset address and file offset.
2779 template<bool big_endian
>
2781 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
2784 uint64_t max_addralign
= 1;
2785 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2786 p
!= this->reloc_stubs_
.end();
2789 Reloc_stub
* stub
= p
->second
;
2790 const Stub_template
* stub_template
= stub
->stub_template();
2791 uint64_t stub_addralign
= stub_template
->alignment();
2792 max_addralign
= std::max(max_addralign
, stub_addralign
);
2793 off
= align_address(off
, stub_addralign
);
2794 stub
->set_offset(off
);
2795 stub
->reset_destination_address();
2796 off
+= stub_template
->size();
2799 this->addralign_
= max_addralign
;
2800 this->set_current_data_size_for_child(off
);
2803 // Write out the stubs to file.
2805 template<bool big_endian
>
2807 Stub_table
<big_endian
>::do_write(Output_file
* of
)
2809 off_t offset
= this->offset();
2810 const section_size_type oview_size
=
2811 convert_to_section_size_type(this->data_size());
2812 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2814 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2815 p
!= this->reloc_stubs_
.end();
2818 Reloc_stub
* stub
= p
->second
;
2819 Arm_address address
= this->address() + stub
->offset();
2821 == align_address(address
,
2822 stub
->stub_template()->alignment()));
2823 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
2826 of
->write_output_view(this->offset(), oview_size
, oview
);
2829 // Arm_input_section methods.
2831 // Initialize an Arm_input_section.
2833 template<bool big_endian
>
2835 Arm_input_section
<big_endian
>::init()
2837 Relobj
* relobj
= this->relobj();
2838 unsigned int shndx
= this->shndx();
2840 // Cache these to speed up size and alignment queries. It is too slow
2841 // to call section_addraglin and section_size every time.
2842 this->original_addralign_
= relobj
->section_addralign(shndx
);
2843 this->original_size_
= relobj
->section_size(shndx
);
2845 // We want to make this look like the original input section after
2846 // output sections are finalized.
2847 Output_section
* os
= relobj
->output_section(shndx
);
2848 off_t offset
= relobj
->output_section_offset(shndx
);
2849 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
2850 this->set_address(os
->address() + offset
);
2851 this->set_file_offset(os
->offset() + offset
);
2853 this->set_current_data_size(this->original_size_
);
2854 this->finalize_data_size();
2857 template<bool big_endian
>
2859 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
2861 // We have to write out the original section content.
2862 section_size_type section_size
;
2863 const unsigned char* section_contents
=
2864 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
2865 of
->write(this->offset(), section_contents
, section_size
);
2867 // If this owns a stub table and it is not empty, write it.
2868 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
2869 this->stub_table_
->write(of
);
2872 // Finalize data size.
2874 template<bool big_endian
>
2876 Arm_input_section
<big_endian
>::set_final_data_size()
2878 // If this owns a stub table, finalize its data size as well.
2879 if (this->is_stub_table_owner())
2881 uint64_t address
= this->address();
2883 // The stub table comes after the original section contents.
2884 address
+= this->original_size_
;
2885 address
= align_address(address
, this->stub_table_
->addralign());
2886 off_t offset
= this->offset() + (address
- this->address());
2887 this->stub_table_
->set_address_and_file_offset(address
, offset
);
2888 address
+= this->stub_table_
->data_size();
2889 gold_assert(address
== this->address() + this->current_data_size());
2892 this->set_data_size(this->current_data_size());
2895 // Reset address and file offset.
2897 template<bool big_endian
>
2899 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
2901 // Size of the original input section contents.
2902 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2904 // If this is a stub table owner, account for the stub table size.
2905 if (this->is_stub_table_owner())
2907 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
2909 // Reset the stub table's address and file offset. The
2910 // current data size for child will be updated after that.
2911 stub_table_
->reset_address_and_file_offset();
2912 off
= align_address(off
, stub_table_
->addralign());
2913 off
+= stub_table
->current_data_size();
2916 this->set_current_data_size(off
);
2919 // Arm_output_section methods.
2921 // Create a stub group for input sections from BEGIN to END. OWNER
2922 // points to the input section to be the owner a new stub table.
2924 template<bool big_endian
>
2926 Arm_output_section
<big_endian
>::create_stub_group(
2927 Input_section_list::const_iterator begin
,
2928 Input_section_list::const_iterator end
,
2929 Input_section_list::const_iterator owner
,
2930 Target_arm
<big_endian
>* target
,
2931 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
2933 // Currently we convert ordinary input sections into relaxed sections only
2934 // at this point but we may want to support creating relaxed input section
2935 // very early. So we check here to see if owner is already a relaxed
2938 Arm_input_section
<big_endian
>* arm_input_section
;
2939 if (owner
->is_relaxed_input_section())
2942 Arm_input_section
<big_endian
>::as_arm_input_section(
2943 owner
->relaxed_input_section());
2947 gold_assert(owner
->is_input_section());
2948 // Create a new relaxed input section.
2950 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
2951 new_relaxed_sections
->push_back(arm_input_section
);
2954 // Create a stub table.
2955 Stub_table
<big_endian
>* stub_table
=
2956 target
->new_stub_table(arm_input_section
);
2958 arm_input_section
->set_stub_table(stub_table
);
2960 Input_section_list::const_iterator p
= begin
;
2961 Input_section_list::const_iterator prev_p
;
2963 // Look for input sections or relaxed input sections in [begin ... end].
2966 if (p
->is_input_section() || p
->is_relaxed_input_section())
2968 // The stub table information for input sections live
2969 // in their objects.
2970 Arm_relobj
<big_endian
>* arm_relobj
=
2971 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
2972 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
2976 while (prev_p
!= end
);
2979 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
2980 // of stub groups. We grow a stub group by adding input section until the
2981 // size is just below GROUP_SIZE. The last input section will be converted
2982 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
2983 // input section after the stub table, effectively double the group size.
2985 // This is similar to the group_sections() function in elf32-arm.c but is
2986 // implemented differently.
2988 template<bool big_endian
>
2990 Arm_output_section
<big_endian
>::group_sections(
2991 section_size_type group_size
,
2992 bool stubs_always_after_branch
,
2993 Target_arm
<big_endian
>* target
)
2995 // We only care about sections containing code.
2996 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
2999 // States for grouping.
3002 // No group is being built.
3004 // A group is being built but the stub table is not found yet.
3005 // We keep group a stub group until the size is just under GROUP_SIZE.
3006 // The last input section in the group will be used as the stub table.
3007 FINDING_STUB_SECTION
,
3008 // A group is being built and we have already found a stub table.
3009 // We enter this state to grow a stub group by adding input section
3010 // after the stub table. This effectively doubles the group size.
3014 // Any newly created relaxed sections are stored here.
3015 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3017 State state
= NO_GROUP
;
3018 section_size_type off
= 0;
3019 section_size_type group_begin_offset
= 0;
3020 section_size_type group_end_offset
= 0;
3021 section_size_type stub_table_end_offset
= 0;
3022 Input_section_list::const_iterator group_begin
=
3023 this->input_sections().end();
3024 Input_section_list::const_iterator stub_table
=
3025 this->input_sections().end();
3026 Input_section_list::const_iterator group_end
= this->input_sections().end();
3027 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3028 p
!= this->input_sections().end();
3031 section_size_type section_begin_offset
=
3032 align_address(off
, p
->addralign());
3033 section_size_type section_end_offset
=
3034 section_begin_offset
+ p
->data_size();
3036 // Check to see if we should group the previously seens sections.
3042 case FINDING_STUB_SECTION
:
3043 // Adding this section makes the group larger than GROUP_SIZE.
3044 if (section_end_offset
- group_begin_offset
>= group_size
)
3046 if (stubs_always_after_branch
)
3048 gold_assert(group_end
!= this->input_sections().end());
3049 this->create_stub_group(group_begin
, group_end
, group_end
,
3050 target
, &new_relaxed_sections
);
3055 // But wait, there's more! Input sections up to
3056 // stub_group_size bytes after the stub table can be
3057 // handled by it too.
3058 state
= HAS_STUB_SECTION
;
3059 stub_table
= group_end
;
3060 stub_table_end_offset
= group_end_offset
;
3065 case HAS_STUB_SECTION
:
3066 // Adding this section makes the post stub-section group larger
3068 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3070 gold_assert(group_end
!= this->input_sections().end());
3071 this->create_stub_group(group_begin
, group_end
, stub_table
,
3072 target
, &new_relaxed_sections
);
3081 // If we see an input section and currently there is no group, start
3082 // a new one. Skip any empty sections.
3083 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3084 && (p
->relobj()->section_size(p
->shndx()) != 0))
3086 if (state
== NO_GROUP
)
3088 state
= FINDING_STUB_SECTION
;
3090 group_begin_offset
= section_begin_offset
;
3093 // Keep track of the last input section seen.
3095 group_end_offset
= section_end_offset
;
3098 off
= section_end_offset
;
3101 // Create a stub group for any ungrouped sections.
3102 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3104 gold_assert(group_end
!= this->input_sections().end());
3105 this->create_stub_group(group_begin
, group_end
,
3106 (state
== FINDING_STUB_SECTION
3109 target
, &new_relaxed_sections
);
3112 // Convert input section into relaxed input section in a batch.
3113 if (!new_relaxed_sections
.empty())
3114 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3116 // Update the section offsets
3117 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3119 Arm_relobj
<big_endian
>* arm_relobj
=
3120 Arm_relobj
<big_endian
>::as_arm_relobj(
3121 new_relaxed_sections
[i
]->relobj());
3122 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3123 // Tell Arm_relobj that this input section is converted.
3124 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3128 // Arm_relobj methods.
3130 // Scan relocations for stub generation.
3132 template<bool big_endian
>
3134 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3135 Target_arm
<big_endian
>* arm_target
,
3136 const Symbol_table
* symtab
,
3137 const Layout
* layout
)
3139 unsigned int shnum
= this->shnum();
3140 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3142 // Read the section headers.
3143 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3147 // To speed up processing, we set up hash tables for fast lookup of
3148 // input offsets to output addresses.
3149 this->initialize_input_to_output_maps();
3151 const Relobj::Output_sections
& out_sections(this->output_sections());
3153 Relocate_info
<32, big_endian
> relinfo
;
3154 relinfo
.symtab
= symtab
;
3155 relinfo
.layout
= layout
;
3156 relinfo
.object
= this;
3158 const unsigned char* p
= pshdrs
+ shdr_size
;
3159 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3161 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3163 unsigned int sh_type
= shdr
.get_sh_type();
3164 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3167 off_t sh_size
= shdr
.get_sh_size();
3171 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3172 if (index
>= this->shnum())
3174 // Ignore reloc section with bad info. This error will be
3175 // reported in the final link.
3179 Output_section
* os
= out_sections
[index
];
3182 // This relocation section is against a section which we
3186 Arm_address output_offset
= this->get_output_section_offset(index
);
3188 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
3190 // Ignore reloc section with unexpected symbol table. The
3191 // error will be reported in the final link.
3195 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
3196 sh_size
, true, false);
3198 unsigned int reloc_size
;
3199 if (sh_type
== elfcpp::SHT_REL
)
3200 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3202 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
3204 if (reloc_size
!= shdr
.get_sh_entsize())
3206 // Ignore reloc section with unexpected entsize. The error
3207 // will be reported in the final link.
3211 size_t reloc_count
= sh_size
/ reloc_size
;
3212 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
3214 // Ignore reloc section with uneven size. The error will be
3215 // reported in the final link.
3219 gold_assert(output_offset
!= invalid_address
3220 || this->relocs_must_follow_section_writes());
3222 // Get the section contents. This does work for the case in which
3223 // we modify the contents of an input section. We need to pass the
3224 // output view under such circumstances.
3225 section_size_type input_view_size
= 0;
3226 const unsigned char* input_view
=
3227 this->section_contents(index
, &input_view_size
, false);
3229 relinfo
.reloc_shndx
= i
;
3230 relinfo
.data_shndx
= index
;
3231 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
3233 output_offset
== invalid_address
,
3239 // After we've done the relocations, we release the hash tables,
3240 // since we no longer need them.
3241 this->free_input_to_output_maps();
3244 // Count the local symbols. The ARM backend needs to know if a symbol
3245 // is a THUMB function or not. For global symbols, it is easy because
3246 // the Symbol object keeps the ELF symbol type. For local symbol it is
3247 // harder because we cannot access this information. So we override the
3248 // do_count_local_symbol in parent and scan local symbols to mark
3249 // THUMB functions. This is not the most efficient way but I do not want to
3250 // slow down other ports by calling a per symbol targer hook inside
3251 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3253 template<bool big_endian
>
3255 Arm_relobj
<big_endian
>::do_count_local_symbols(
3256 Stringpool_template
<char>* pool
,
3257 Stringpool_template
<char>* dynpool
)
3259 // We need to fix-up the values of any local symbols whose type are
3262 // Ask parent to count the local symbols.
3263 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
3264 const unsigned int loccount
= this->local_symbol_count();
3268 // Intialize the thumb function bit-vector.
3269 std::vector
<bool> empty_vector(loccount
, false);
3270 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
3272 // Read the symbol table section header.
3273 const unsigned int symtab_shndx
= this->symtab_shndx();
3274 elfcpp::Shdr
<32, big_endian
>
3275 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
3276 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
3278 // Read the local symbols.
3279 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
3280 gold_assert(loccount
== symtabshdr
.get_sh_info());
3281 off_t locsize
= loccount
* sym_size
;
3282 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
3283 locsize
, true, true);
3285 // Loop over the local symbols and mark any local symbols pointing
3286 // to THUMB functions.
3288 // Skip the first dummy symbol.
3290 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
3291 this->local_values();
3292 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
3294 elfcpp::Sym
<32, big_endian
> sym(psyms
);
3295 elfcpp::STT st_type
= sym
.get_st_type();
3296 Symbol_value
<32>& lv((*plocal_values
)[i
]);
3297 Arm_address input_value
= lv
.input_value();
3299 if (st_type
== elfcpp::STT_ARM_TFUNC
3300 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
3302 // This is a THUMB function. Mark this and canonicalize the
3303 // symbol value by setting LSB.
3304 this->local_symbol_is_thumb_function_
[i
] = true;
3305 if ((input_value
& 1) == 0)
3306 lv
.set_input_value(input_value
| 1);
3311 // Relocate sections.
3312 template<bool big_endian
>
3314 Arm_relobj
<big_endian
>::do_relocate_sections(
3315 const General_options
& options
,
3316 const Symbol_table
* symtab
,
3317 const Layout
* layout
,
3318 const unsigned char* pshdrs
,
3319 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
3321 // Call parent to relocate sections.
3322 Sized_relobj
<32, big_endian
>::do_relocate_sections(options
, symtab
, layout
,
3325 // We do not generate stubs if doing a relocatable link.
3326 if (parameters
->options().relocatable())
3329 // Relocate stub tables.
3330 unsigned int shnum
= this->shnum();
3332 Target_arm
<big_endian
>* arm_target
=
3333 Target_arm
<big_endian
>::default_target();
3335 Relocate_info
<32, big_endian
> relinfo
;
3336 relinfo
.options
= &options
;
3337 relinfo
.symtab
= symtab
;
3338 relinfo
.layout
= layout
;
3339 relinfo
.object
= this;
3341 for (unsigned int i
= 1; i
< shnum
; ++i
)
3343 Arm_input_section
<big_endian
>* arm_input_section
=
3344 arm_target
->find_arm_input_section(this, i
);
3346 if (arm_input_section
== NULL
3347 || !arm_input_section
->is_stub_table_owner()
3348 || arm_input_section
->stub_table()->empty())
3351 // We cannot discard a section if it owns a stub table.
3352 Output_section
* os
= this->output_section(i
);
3353 gold_assert(os
!= NULL
);
3355 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
3356 relinfo
.reloc_shdr
= NULL
;
3357 relinfo
.data_shndx
= i
;
3358 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
3360 gold_assert((*pviews
)[i
].view
!= NULL
);
3362 // We are passed the output section view. Adjust it to cover the
3364 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
3365 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
3366 && ((stub_table
->address() + stub_table
->data_size())
3367 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
3369 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
3370 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
3371 Arm_address address
= stub_table
->address();
3372 section_size_type view_size
= stub_table
->data_size();
3374 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
3379 // Read the symbol information.
3381 template<bool big_endian
>
3383 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3385 // Call parent class to read symbol information.
3386 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
3388 // Read processor-specific flags in ELF file header.
3389 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3390 elfcpp::Elf_sizes
<32>::ehdr_size
,
3392 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3393 this->processor_specific_flags_
= ehdr
.get_e_flags();
3396 // Arm_dynobj methods.
3398 // Read the symbol information.
3400 template<bool big_endian
>
3402 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3404 // Call parent class to read symbol information.
3405 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
3407 // Read processor-specific flags in ELF file header.
3408 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3409 elfcpp::Elf_sizes
<32>::ehdr_size
,
3411 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3412 this->processor_specific_flags_
= ehdr
.get_e_flags();
3415 // Stub_addend_reader methods.
3417 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3419 template<bool big_endian
>
3420 elfcpp::Elf_types
<32>::Elf_Swxword
3421 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
3422 unsigned int r_type
,
3423 const unsigned char* view
,
3424 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
3428 case elfcpp::R_ARM_CALL
:
3429 case elfcpp::R_ARM_JUMP24
:
3430 case elfcpp::R_ARM_PLT32
:
3432 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3433 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3434 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3435 return utils::sign_extend
<26>(val
<< 2);
3438 case elfcpp::R_ARM_THM_CALL
:
3439 case elfcpp::R_ARM_THM_JUMP24
:
3440 case elfcpp::R_ARM_THM_XPC22
:
3442 // Fetch the addend. We use the Thumb-2 encoding (backwards
3443 // compatible with Thumb-1) involving the J1 and J2 bits.
3444 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3445 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3446 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3447 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3449 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
3450 uint32_t upper
= upper_insn
& 0x3ff;
3451 uint32_t lower
= lower_insn
& 0x7ff;
3452 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
3453 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
3454 uint32_t i1
= j1
^ s
? 0 : 1;
3455 uint32_t i2
= j2
^ s
? 0 : 1;
3457 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3458 | (upper
<< 12) | (lower
<< 1));
3461 case elfcpp::R_ARM_THM_JUMP19
:
3463 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3464 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3465 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3466 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3468 // Reconstruct the top three bits and squish the two 11 bit pieces
3470 uint32_t S
= (upper_insn
& 0x0400) >> 10;
3471 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
3472 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
3474 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
3475 uint32_t lower
= (lower_insn
& 0x07ff);
3476 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
3484 // A class to handle the PLT data.
3486 template<bool big_endian
>
3487 class Output_data_plt_arm
: public Output_section_data
3490 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
3493 Output_data_plt_arm(Layout
*, Output_data_space
*);
3495 // Add an entry to the PLT.
3497 add_entry(Symbol
* gsym
);
3499 // Return the .rel.plt section data.
3500 const Reloc_section
*
3502 { return this->rel_
; }
3506 do_adjust_output_section(Output_section
* os
);
3508 // Write to a map file.
3510 do_print_to_mapfile(Mapfile
* mapfile
) const
3511 { mapfile
->print_output_data(this, _("** PLT")); }
3514 // Template for the first PLT entry.
3515 static const uint32_t first_plt_entry
[5];
3517 // Template for subsequent PLT entries.
3518 static const uint32_t plt_entry
[3];
3520 // Set the final size.
3522 set_final_data_size()
3524 this->set_data_size(sizeof(first_plt_entry
)
3525 + this->count_
* sizeof(plt_entry
));
3528 // Write out the PLT data.
3530 do_write(Output_file
*);
3532 // The reloc section.
3533 Reloc_section
* rel_
;
3534 // The .got.plt section.
3535 Output_data_space
* got_plt_
;
3536 // The number of PLT entries.
3537 unsigned int count_
;
3540 // Create the PLT section. The ordinary .got section is an argument,
3541 // since we need to refer to the start. We also create our own .got
3542 // section just for PLT entries.
3544 template<bool big_endian
>
3545 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
3546 Output_data_space
* got_plt
)
3547 : Output_section_data(4), got_plt_(got_plt
), count_(0)
3549 this->rel_
= new Reloc_section(false);
3550 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
3551 elfcpp::SHF_ALLOC
, this->rel_
);
3554 template<bool big_endian
>
3556 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
3561 // Add an entry to the PLT.
3563 template<bool big_endian
>
3565 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
3567 gold_assert(!gsym
->has_plt_offset());
3569 // Note that when setting the PLT offset we skip the initial
3570 // reserved PLT entry.
3571 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
3572 + sizeof(first_plt_entry
));
3576 section_offset_type got_offset
= this->got_plt_
->current_data_size();
3578 // Every PLT entry needs a GOT entry which points back to the PLT
3579 // entry (this will be changed by the dynamic linker, normally
3580 // lazily when the function is called).
3581 this->got_plt_
->set_current_data_size(got_offset
+ 4);
3583 // Every PLT entry needs a reloc.
3584 gsym
->set_needs_dynsym_entry();
3585 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
3588 // Note that we don't need to save the symbol. The contents of the
3589 // PLT are independent of which symbols are used. The symbols only
3590 // appear in the relocations.
3594 // FIXME: This is not very flexible. Right now this has only been tested
3595 // on armv5te. If we are to support additional architecture features like
3596 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3598 // The first entry in the PLT.
3599 template<bool big_endian
>
3600 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
3602 0xe52de004, // str lr, [sp, #-4]!
3603 0xe59fe004, // ldr lr, [pc, #4]
3604 0xe08fe00e, // add lr, pc, lr
3605 0xe5bef008, // ldr pc, [lr, #8]!
3606 0x00000000, // &GOT[0] - .
3609 // Subsequent entries in the PLT.
3611 template<bool big_endian
>
3612 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
3614 0xe28fc600, // add ip, pc, #0xNN00000
3615 0xe28cca00, // add ip, ip, #0xNN000
3616 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3619 // Write out the PLT. This uses the hand-coded instructions above,
3620 // and adjusts them as needed. This is all specified by the arm ELF
3621 // Processor Supplement.
3623 template<bool big_endian
>
3625 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
3627 const off_t offset
= this->offset();
3628 const section_size_type oview_size
=
3629 convert_to_section_size_type(this->data_size());
3630 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3632 const off_t got_file_offset
= this->got_plt_
->offset();
3633 const section_size_type got_size
=
3634 convert_to_section_size_type(this->got_plt_
->data_size());
3635 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
3637 unsigned char* pov
= oview
;
3639 Arm_address plt_address
= this->address();
3640 Arm_address got_address
= this->got_plt_
->address();
3642 // Write first PLT entry. All but the last word are constants.
3643 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
3644 / sizeof(plt_entry
[0]));
3645 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
3646 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
3647 // Last word in first PLT entry is &GOT[0] - .
3648 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
3649 got_address
- (plt_address
+ 16));
3650 pov
+= sizeof(first_plt_entry
);
3652 unsigned char* got_pov
= got_view
;
3654 memset(got_pov
, 0, 12);
3657 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3658 unsigned int plt_offset
= sizeof(first_plt_entry
);
3659 unsigned int plt_rel_offset
= 0;
3660 unsigned int got_offset
= 12;
3661 const unsigned int count
= this->count_
;
3662 for (unsigned int i
= 0;
3665 pov
+= sizeof(plt_entry
),
3667 plt_offset
+= sizeof(plt_entry
),
3668 plt_rel_offset
+= rel_size
,
3671 // Set and adjust the PLT entry itself.
3672 int32_t offset
= ((got_address
+ got_offset
)
3673 - (plt_address
+ plt_offset
+ 8));
3675 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
3676 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
3677 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
3678 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
3679 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
3680 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
3681 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
3683 // Set the entry in the GOT.
3684 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
3687 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
3688 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
3690 of
->write_output_view(offset
, oview_size
, oview
);
3691 of
->write_output_view(got_file_offset
, got_size
, got_view
);
3694 // Create a PLT entry for a global symbol.
3696 template<bool big_endian
>
3698 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
3701 if (gsym
->has_plt_offset())
3704 if (this->plt_
== NULL
)
3706 // Create the GOT sections first.
3707 this->got_section(symtab
, layout
);
3709 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
3710 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
3712 | elfcpp::SHF_EXECINSTR
),
3715 this->plt_
->add_entry(gsym
);
3718 // Report an unsupported relocation against a local symbol.
3720 template<bool big_endian
>
3722 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
3723 Sized_relobj
<32, big_endian
>* object
,
3724 unsigned int r_type
)
3726 gold_error(_("%s: unsupported reloc %u against local symbol"),
3727 object
->name().c_str(), r_type
);
3730 // We are about to emit a dynamic relocation of type R_TYPE. If the
3731 // dynamic linker does not support it, issue an error. The GNU linker
3732 // only issues a non-PIC error for an allocated read-only section.
3733 // Here we know the section is allocated, but we don't know that it is
3734 // read-only. But we check for all the relocation types which the
3735 // glibc dynamic linker supports, so it seems appropriate to issue an
3736 // error even if the section is not read-only.
3738 template<bool big_endian
>
3740 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
3741 unsigned int r_type
)
3745 // These are the relocation types supported by glibc for ARM.
3746 case elfcpp::R_ARM_RELATIVE
:
3747 case elfcpp::R_ARM_COPY
:
3748 case elfcpp::R_ARM_GLOB_DAT
:
3749 case elfcpp::R_ARM_JUMP_SLOT
:
3750 case elfcpp::R_ARM_ABS32
:
3751 case elfcpp::R_ARM_ABS32_NOI
:
3752 case elfcpp::R_ARM_PC24
:
3753 // FIXME: The following 3 types are not supported by Android's dynamic
3755 case elfcpp::R_ARM_TLS_DTPMOD32
:
3756 case elfcpp::R_ARM_TLS_DTPOFF32
:
3757 case elfcpp::R_ARM_TLS_TPOFF32
:
3761 // This prevents us from issuing more than one error per reloc
3762 // section. But we can still wind up issuing more than one
3763 // error per object file.
3764 if (this->issued_non_pic_error_
)
3766 object
->error(_("requires unsupported dynamic reloc; "
3767 "recompile with -fPIC"));
3768 this->issued_non_pic_error_
= true;
3771 case elfcpp::R_ARM_NONE
:
3776 // Scan a relocation for a local symbol.
3777 // FIXME: This only handles a subset of relocation types used by Android
3778 // on ARM v5te devices.
3780 template<bool big_endian
>
3782 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
3785 Sized_relobj
<32, big_endian
>* object
,
3786 unsigned int data_shndx
,
3787 Output_section
* output_section
,
3788 const elfcpp::Rel
<32, big_endian
>& reloc
,
3789 unsigned int r_type
,
3790 const elfcpp::Sym
<32, big_endian
>&)
3792 r_type
= get_real_reloc_type(r_type
);
3795 case elfcpp::R_ARM_NONE
:
3798 case elfcpp::R_ARM_ABS32
:
3799 case elfcpp::R_ARM_ABS32_NOI
:
3800 // If building a shared library (or a position-independent
3801 // executable), we need to create a dynamic relocation for
3802 // this location. The relocation applied at link time will
3803 // apply the link-time value, so we flag the location with
3804 // an R_ARM_RELATIVE relocation so the dynamic loader can
3805 // relocate it easily.
3806 if (parameters
->options().output_is_position_independent())
3808 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3809 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3810 // If we are to add more other reloc types than R_ARM_ABS32,
3811 // we need to add check_non_pic(object, r_type) here.
3812 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
3813 output_section
, data_shndx
,
3814 reloc
.get_r_offset());
3818 case elfcpp::R_ARM_REL32
:
3819 case elfcpp::R_ARM_THM_CALL
:
3820 case elfcpp::R_ARM_CALL
:
3821 case elfcpp::R_ARM_PREL31
:
3822 case elfcpp::R_ARM_JUMP24
:
3823 case elfcpp::R_ARM_PLT32
:
3824 case elfcpp::R_ARM_THM_ABS5
:
3825 case elfcpp::R_ARM_ABS8
:
3826 case elfcpp::R_ARM_ABS12
:
3827 case elfcpp::R_ARM_ABS16
:
3828 case elfcpp::R_ARM_BASE_ABS
:
3829 case elfcpp::R_ARM_MOVW_ABS_NC
:
3830 case elfcpp::R_ARM_MOVT_ABS
:
3831 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3832 case elfcpp::R_ARM_THM_MOVT_ABS
:
3833 case elfcpp::R_ARM_MOVW_PREL_NC
:
3834 case elfcpp::R_ARM_MOVT_PREL
:
3835 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3836 case elfcpp::R_ARM_THM_MOVT_PREL
:
3839 case elfcpp::R_ARM_GOTOFF32
:
3840 // We need a GOT section:
3841 target
->got_section(symtab
, layout
);
3844 case elfcpp::R_ARM_BASE_PREL
:
3845 // FIXME: What about this?
3848 case elfcpp::R_ARM_GOT_BREL
:
3849 case elfcpp::R_ARM_GOT_PREL
:
3851 // The symbol requires a GOT entry.
3852 Output_data_got
<32, big_endian
>* got
=
3853 target
->got_section(symtab
, layout
);
3854 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3855 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
3857 // If we are generating a shared object, we need to add a
3858 // dynamic RELATIVE relocation for this symbol's GOT entry.
3859 if (parameters
->options().output_is_position_independent())
3861 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3862 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3863 rel_dyn
->add_local_relative(
3864 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
3865 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3871 case elfcpp::R_ARM_TARGET1
:
3872 // This should have been mapped to another type already.
3874 case elfcpp::R_ARM_COPY
:
3875 case elfcpp::R_ARM_GLOB_DAT
:
3876 case elfcpp::R_ARM_JUMP_SLOT
:
3877 case elfcpp::R_ARM_RELATIVE
:
3878 // These are relocations which should only be seen by the
3879 // dynamic linker, and should never be seen here.
3880 gold_error(_("%s: unexpected reloc %u in object file"),
3881 object
->name().c_str(), r_type
);
3885 unsupported_reloc_local(object
, r_type
);
3890 // Report an unsupported relocation against a global symbol.
3892 template<bool big_endian
>
3894 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
3895 Sized_relobj
<32, big_endian
>* object
,
3896 unsigned int r_type
,
3899 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3900 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3903 // Scan a relocation for a global symbol.
3904 // FIXME: This only handles a subset of relocation types used by Android
3905 // on ARM v5te devices.
3907 template<bool big_endian
>
3909 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
3912 Sized_relobj
<32, big_endian
>* object
,
3913 unsigned int data_shndx
,
3914 Output_section
* output_section
,
3915 const elfcpp::Rel
<32, big_endian
>& reloc
,
3916 unsigned int r_type
,
3919 r_type
= get_real_reloc_type(r_type
);
3922 case elfcpp::R_ARM_NONE
:
3925 case elfcpp::R_ARM_ABS32
:
3926 case elfcpp::R_ARM_ABS32_NOI
:
3928 // Make a dynamic relocation if necessary.
3929 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
3931 if (target
->may_need_copy_reloc(gsym
))
3933 target
->copy_reloc(symtab
, layout
, object
,
3934 data_shndx
, output_section
, gsym
, reloc
);
3936 else if (gsym
->can_use_relative_reloc(false))
3938 // If we are to add more other reloc types than R_ARM_ABS32,
3939 // we need to add check_non_pic(object, r_type) here.
3940 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3941 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
3942 output_section
, object
,
3943 data_shndx
, reloc
.get_r_offset());
3947 // If we are to add more other reloc types than R_ARM_ABS32,
3948 // we need to add check_non_pic(object, r_type) here.
3949 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3950 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3951 data_shndx
, reloc
.get_r_offset());
3957 case elfcpp::R_ARM_MOVW_ABS_NC
:
3958 case elfcpp::R_ARM_MOVT_ABS
:
3959 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
3960 case elfcpp::R_ARM_THM_MOVT_ABS
:
3961 case elfcpp::R_ARM_MOVW_PREL_NC
:
3962 case elfcpp::R_ARM_MOVT_PREL
:
3963 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
3964 case elfcpp::R_ARM_THM_MOVT_PREL
:
3967 case elfcpp::R_ARM_THM_ABS5
:
3968 case elfcpp::R_ARM_ABS8
:
3969 case elfcpp::R_ARM_ABS12
:
3970 case elfcpp::R_ARM_ABS16
:
3971 case elfcpp::R_ARM_BASE_ABS
:
3973 // No dynamic relocs of this kinds.
3974 // Report the error in case of PIC.
3975 int flags
= Symbol::NON_PIC_REF
;
3976 if (gsym
->type() == elfcpp::STT_FUNC
3977 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
3978 flags
|= Symbol::FUNCTION_CALL
;
3979 if (gsym
->needs_dynamic_reloc(flags
))
3980 check_non_pic(object
, r_type
);
3984 case elfcpp::R_ARM_REL32
:
3985 case elfcpp::R_ARM_PREL31
:
3987 // Make a dynamic relocation if necessary.
3988 int flags
= Symbol::NON_PIC_REF
;
3989 if (gsym
->needs_dynamic_reloc(flags
))
3991 if (target
->may_need_copy_reloc(gsym
))
3993 target
->copy_reloc(symtab
, layout
, object
,
3994 data_shndx
, output_section
, gsym
, reloc
);
3998 check_non_pic(object
, r_type
);
3999 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4000 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4001 data_shndx
, reloc
.get_r_offset());
4007 case elfcpp::R_ARM_JUMP24
:
4008 case elfcpp::R_ARM_THM_CALL
:
4009 case elfcpp::R_ARM_CALL
:
4011 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4012 target
->make_plt_entry(symtab
, layout
, gsym
);
4013 // Make a dynamic relocation if necessary.
4014 int flags
= Symbol::NON_PIC_REF
;
4015 if (gsym
->type() == elfcpp::STT_FUNC
4016 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4017 flags
|= Symbol::FUNCTION_CALL
;
4018 if (gsym
->needs_dynamic_reloc(flags
))
4020 if (target
->may_need_copy_reloc(gsym
))
4022 target
->copy_reloc(symtab
, layout
, object
,
4023 data_shndx
, output_section
, gsym
,
4028 check_non_pic(object
, r_type
);
4029 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4030 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4031 data_shndx
, reloc
.get_r_offset());
4037 case elfcpp::R_ARM_PLT32
:
4038 // If the symbol is fully resolved, this is just a relative
4039 // local reloc. Otherwise we need a PLT entry.
4040 if (gsym
->final_value_is_known())
4042 // If building a shared library, we can also skip the PLT entry
4043 // if the symbol is defined in the output file and is protected
4045 if (gsym
->is_defined()
4046 && !gsym
->is_from_dynobj()
4047 && !gsym
->is_preemptible())
4049 target
->make_plt_entry(symtab
, layout
, gsym
);
4052 case elfcpp::R_ARM_GOTOFF32
:
4053 // We need a GOT section.
4054 target
->got_section(symtab
, layout
);
4057 case elfcpp::R_ARM_BASE_PREL
:
4058 // FIXME: What about this?
4061 case elfcpp::R_ARM_GOT_BREL
:
4062 case elfcpp::R_ARM_GOT_PREL
:
4064 // The symbol requires a GOT entry.
4065 Output_data_got
<32, big_endian
>* got
=
4066 target
->got_section(symtab
, layout
);
4067 if (gsym
->final_value_is_known())
4068 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4071 // If this symbol is not fully resolved, we need to add a
4072 // GOT entry with a dynamic relocation.
4073 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4074 if (gsym
->is_from_dynobj()
4075 || gsym
->is_undefined()
4076 || gsym
->is_preemptible())
4077 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4078 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4081 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4082 rel_dyn
->add_global_relative(
4083 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4084 gsym
->got_offset(GOT_TYPE_STANDARD
));
4090 case elfcpp::R_ARM_TARGET1
:
4091 // This should have been mapped to another type already.
4093 case elfcpp::R_ARM_COPY
:
4094 case elfcpp::R_ARM_GLOB_DAT
:
4095 case elfcpp::R_ARM_JUMP_SLOT
:
4096 case elfcpp::R_ARM_RELATIVE
:
4097 // These are relocations which should only be seen by the
4098 // dynamic linker, and should never be seen here.
4099 gold_error(_("%s: unexpected reloc %u in object file"),
4100 object
->name().c_str(), r_type
);
4104 unsupported_reloc_global(object
, r_type
, gsym
);
4109 // Process relocations for gc.
4111 template<bool big_endian
>
4113 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4115 Sized_relobj
<32, big_endian
>* object
,
4116 unsigned int data_shndx
,
4118 const unsigned char* prelocs
,
4120 Output_section
* output_section
,
4121 bool needs_special_offset_handling
,
4122 size_t local_symbol_count
,
4123 const unsigned char* plocal_symbols
)
4125 typedef Target_arm
<big_endian
> Arm
;
4126 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4128 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
4137 needs_special_offset_handling
,
4142 // Scan relocations for a section.
4144 template<bool big_endian
>
4146 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4148 Sized_relobj
<32, big_endian
>* object
,
4149 unsigned int data_shndx
,
4150 unsigned int sh_type
,
4151 const unsigned char* prelocs
,
4153 Output_section
* output_section
,
4154 bool needs_special_offset_handling
,
4155 size_t local_symbol_count
,
4156 const unsigned char* plocal_symbols
)
4158 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4159 if (sh_type
== elfcpp::SHT_RELA
)
4161 gold_error(_("%s: unsupported RELA reloc section"),
4162 object
->name().c_str());
4166 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
4175 needs_special_offset_handling
,
4180 // Finalize the sections.
4182 template<bool big_endian
>
4184 Target_arm
<big_endian
>::do_finalize_sections(
4186 const Input_objects
* input_objects
)
4188 // Merge processor-specific flags.
4189 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
4190 p
!= input_objects
->relobj_end();
4193 Arm_relobj
<big_endian
>* arm_relobj
=
4194 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
4195 this->merge_processor_specific_flags(
4197 arm_relobj
->processor_specific_flags());
4200 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
4201 p
!= input_objects
->dynobj_end();
4204 Arm_dynobj
<big_endian
>* arm_dynobj
=
4205 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
4206 this->merge_processor_specific_flags(
4208 arm_dynobj
->processor_specific_flags());
4211 // Fill in some more dynamic tags.
4212 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4215 if (this->got_plt_
!= NULL
)
4216 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
4218 if (this->plt_
!= NULL
)
4220 const Output_data
* od
= this->plt_
->rel_plt();
4221 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
4222 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
4223 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
4226 if (this->rel_dyn_
!= NULL
)
4228 const Output_data
* od
= this->rel_dyn_
;
4229 odyn
->add_section_address(elfcpp::DT_REL
, od
);
4230 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
4231 odyn
->add_constant(elfcpp::DT_RELENT
,
4232 elfcpp::Elf_sizes
<32>::rel_size
);
4235 if (!parameters
->options().shared())
4237 // The value of the DT_DEBUG tag is filled in by the dynamic
4238 // linker at run time, and used by the debugger.
4239 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
4243 // Emit any relocs we saved in an attempt to avoid generating COPY
4245 if (this->copy_relocs_
.any_saved_relocs())
4246 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
4248 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4249 // the .ARM.exidx section.
4250 if (!layout
->script_options()->saw_phdrs_clause()
4251 && !parameters
->options().relocatable())
4253 Output_section
* exidx_section
=
4254 layout
->find_output_section(".ARM.exidx");
4256 if (exidx_section
!= NULL
4257 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
)
4259 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
4261 Output_segment
* exidx_segment
=
4262 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
4263 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
);
4268 // Return whether a direct absolute static relocation needs to be applied.
4269 // In cases where Scan::local() or Scan::global() has created
4270 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4271 // of the relocation is carried in the data, and we must not
4272 // apply the static relocation.
4274 template<bool big_endian
>
4276 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
4277 const Sized_symbol
<32>* gsym
,
4280 Output_section
* output_section
)
4282 // If the output section is not allocated, then we didn't call
4283 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4285 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
4288 // For local symbols, we will have created a non-RELATIVE dynamic
4289 // relocation only if (a) the output is position independent,
4290 // (b) the relocation is absolute (not pc- or segment-relative), and
4291 // (c) the relocation is not 32 bits wide.
4293 return !(parameters
->options().output_is_position_independent()
4294 && (ref_flags
& Symbol::ABSOLUTE_REF
)
4297 // For global symbols, we use the same helper routines used in the
4298 // scan pass. If we did not create a dynamic relocation, or if we
4299 // created a RELATIVE dynamic relocation, we should apply the static
4301 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
4302 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
4303 && gsym
->can_use_relative_reloc(ref_flags
4304 & Symbol::FUNCTION_CALL
);
4305 return !has_dyn
|| is_rel
;
4308 // Perform a relocation.
4310 template<bool big_endian
>
4312 Target_arm
<big_endian
>::Relocate::relocate(
4313 const Relocate_info
<32, big_endian
>* relinfo
,
4315 Output_section
*output_section
,
4317 const elfcpp::Rel
<32, big_endian
>& rel
,
4318 unsigned int r_type
,
4319 const Sized_symbol
<32>* gsym
,
4320 const Symbol_value
<32>* psymval
,
4321 unsigned char* view
,
4322 Arm_address address
,
4323 section_size_type
/* view_size */ )
4325 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
4327 r_type
= get_real_reloc_type(r_type
);
4329 // If this the symbol may be a Thumb function, set thumb bit to 1.
4330 bool has_thumb_bit
= ((gsym
!= NULL
)
4331 && (gsym
->type() == elfcpp::STT_FUNC
4332 || gsym
->type() == elfcpp::STT_ARM_TFUNC
));
4334 // Pick the value to use for symbols defined in shared objects.
4335 Symbol_value
<32> symval
;
4337 && gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
4339 symval
.set_output_value(target
->plt_section()->address()
4340 + gsym
->plt_offset());
4345 const Sized_relobj
<32, big_endian
>* object
= relinfo
->object
;
4347 // Get the GOT offset if needed.
4348 // The GOT pointer points to the end of the GOT section.
4349 // We need to subtract the size of the GOT section to get
4350 // the actual offset to use in the relocation.
4351 bool have_got_offset
= false;
4352 unsigned int got_offset
= 0;
4355 case elfcpp::R_ARM_GOT_BREL
:
4356 case elfcpp::R_ARM_GOT_PREL
:
4359 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4360 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4361 - target
->got_size());
4365 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4366 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4367 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4368 - target
->got_size());
4370 have_got_offset
= true;
4377 typename
Arm_relocate_functions::Status reloc_status
=
4378 Arm_relocate_functions::STATUS_OKAY
;
4381 case elfcpp::R_ARM_NONE
:
4384 case elfcpp::R_ARM_ABS8
:
4385 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4387 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
4390 case elfcpp::R_ARM_ABS12
:
4391 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4393 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
4396 case elfcpp::R_ARM_ABS16
:
4397 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4399 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
4402 case elfcpp::R_ARM_ABS32
:
4403 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4405 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4409 case elfcpp::R_ARM_ABS32_NOI
:
4410 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4412 // No thumb bit for this relocation: (S + A)
4413 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4417 case elfcpp::R_ARM_MOVW_ABS_NC
:
4418 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4420 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
4424 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4425 "a shared object; recompile with -fPIC"));
4428 case elfcpp::R_ARM_MOVT_ABS
:
4429 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4431 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
4433 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4434 "a shared object; recompile with -fPIC"));
4437 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4438 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4440 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
4444 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4445 "making a shared object; recompile with -fPIC"));
4448 case elfcpp::R_ARM_THM_MOVT_ABS
:
4449 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4451 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
4454 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4455 "making a shared object; recompile with -fPIC"));
4458 case elfcpp::R_ARM_MOVW_PREL_NC
:
4459 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
4464 case elfcpp::R_ARM_MOVT_PREL
:
4465 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
4469 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4470 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
4475 case elfcpp::R_ARM_THM_MOVT_PREL
:
4476 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
4480 case elfcpp::R_ARM_REL32
:
4481 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4482 address
, has_thumb_bit
);
4485 case elfcpp::R_ARM_THM_ABS5
:
4486 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4488 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
4491 case elfcpp::R_ARM_THM_CALL
:
4492 reloc_status
= Arm_relocate_functions::thm_call(view
, object
, psymval
,
4493 address
, has_thumb_bit
);
4496 case elfcpp::R_ARM_GOTOFF32
:
4498 Arm_address got_origin
;
4499 got_origin
= target
->got_plt_section()->address();
4500 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4501 got_origin
, has_thumb_bit
);
4505 case elfcpp::R_ARM_BASE_PREL
:
4508 // Get the addressing origin of the output segment defining the
4509 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4510 gold_assert(gsym
!= NULL
);
4511 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4512 origin
= gsym
->output_segment()->vaddr();
4513 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4514 origin
= gsym
->output_data()->address();
4517 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4518 _("cannot find origin of R_ARM_BASE_PREL"));
4521 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
4525 case elfcpp::R_ARM_BASE_ABS
:
4527 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4532 // Get the addressing origin of the output segment defining
4533 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4535 // R_ARM_BASE_ABS with the NULL symbol will give the
4536 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4537 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4538 origin
= target
->got_plt_section()->address();
4539 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4540 origin
= gsym
->output_segment()->vaddr();
4541 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4542 origin
= gsym
->output_data()->address();
4545 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4546 _("cannot find origin of R_ARM_BASE_ABS"));
4550 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
4554 case elfcpp::R_ARM_GOT_BREL
:
4555 gold_assert(have_got_offset
);
4556 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
4559 case elfcpp::R_ARM_GOT_PREL
:
4560 gold_assert(have_got_offset
);
4561 // Get the address origin for GOT PLT, which is allocated right
4562 // after the GOT section, to calculate an absolute address of
4563 // the symbol GOT entry (got_origin + got_offset).
4564 Arm_address got_origin
;
4565 got_origin
= target
->got_plt_section()->address();
4566 reloc_status
= Arm_relocate_functions::got_prel(view
,
4567 got_origin
+ got_offset
,
4571 case elfcpp::R_ARM_PLT32
:
4572 gold_assert(gsym
== NULL
4573 || gsym
->has_plt_offset()
4574 || gsym
->final_value_is_known()
4575 || (gsym
->is_defined()
4576 && !gsym
->is_from_dynobj()
4577 && !gsym
->is_preemptible()));
4578 reloc_status
= Arm_relocate_functions::plt32(view
, object
, psymval
,
4579 address
, has_thumb_bit
);
4582 case elfcpp::R_ARM_CALL
:
4583 reloc_status
= Arm_relocate_functions::call(view
, object
, psymval
,
4584 address
, has_thumb_bit
);
4587 case elfcpp::R_ARM_JUMP24
:
4588 reloc_status
= Arm_relocate_functions::jump24(view
, object
, psymval
,
4589 address
, has_thumb_bit
);
4592 case elfcpp::R_ARM_PREL31
:
4593 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
4594 address
, has_thumb_bit
);
4597 case elfcpp::R_ARM_TARGET1
:
4598 // This should have been mapped to another type already.
4600 case elfcpp::R_ARM_COPY
:
4601 case elfcpp::R_ARM_GLOB_DAT
:
4602 case elfcpp::R_ARM_JUMP_SLOT
:
4603 case elfcpp::R_ARM_RELATIVE
:
4604 // These are relocations which should only be seen by the
4605 // dynamic linker, and should never be seen here.
4606 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4607 _("unexpected reloc %u in object file"),
4612 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4613 _("unsupported reloc %u"),
4618 // Report any errors.
4619 switch (reloc_status
)
4621 case Arm_relocate_functions::STATUS_OKAY
:
4623 case Arm_relocate_functions::STATUS_OVERFLOW
:
4624 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4625 _("relocation overflow in relocation %u"),
4628 case Arm_relocate_functions::STATUS_BAD_RELOC
:
4629 gold_error_at_location(
4633 _("unexpected opcode while processing relocation %u"),
4643 // Relocate section data.
4645 template<bool big_endian
>
4647 Target_arm
<big_endian
>::relocate_section(
4648 const Relocate_info
<32, big_endian
>* relinfo
,
4649 unsigned int sh_type
,
4650 const unsigned char* prelocs
,
4652 Output_section
* output_section
,
4653 bool needs_special_offset_handling
,
4654 unsigned char* view
,
4655 Arm_address address
,
4656 section_size_type view_size
,
4657 const Reloc_symbol_changes
* reloc_symbol_changes
)
4659 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
4660 gold_assert(sh_type
== elfcpp::SHT_REL
);
4662 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
4669 needs_special_offset_handling
,
4673 reloc_symbol_changes
);
4676 // Return the size of a relocation while scanning during a relocatable
4679 template<bool big_endian
>
4681 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
4682 unsigned int r_type
,
4685 r_type
= get_real_reloc_type(r_type
);
4688 case elfcpp::R_ARM_NONE
:
4691 case elfcpp::R_ARM_ABS8
:
4694 case elfcpp::R_ARM_ABS16
:
4695 case elfcpp::R_ARM_THM_ABS5
:
4698 case elfcpp::R_ARM_ABS32
:
4699 case elfcpp::R_ARM_ABS32_NOI
:
4700 case elfcpp::R_ARM_ABS12
:
4701 case elfcpp::R_ARM_BASE_ABS
:
4702 case elfcpp::R_ARM_REL32
:
4703 case elfcpp::R_ARM_THM_CALL
:
4704 case elfcpp::R_ARM_GOTOFF32
:
4705 case elfcpp::R_ARM_BASE_PREL
:
4706 case elfcpp::R_ARM_GOT_BREL
:
4707 case elfcpp::R_ARM_GOT_PREL
:
4708 case elfcpp::R_ARM_PLT32
:
4709 case elfcpp::R_ARM_CALL
:
4710 case elfcpp::R_ARM_JUMP24
:
4711 case elfcpp::R_ARM_PREL31
:
4712 case elfcpp::R_ARM_MOVW_ABS_NC
:
4713 case elfcpp::R_ARM_MOVT_ABS
:
4714 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4715 case elfcpp::R_ARM_THM_MOVT_ABS
:
4716 case elfcpp::R_ARM_MOVW_PREL_NC
:
4717 case elfcpp::R_ARM_MOVT_PREL
:
4718 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4719 case elfcpp::R_ARM_THM_MOVT_PREL
:
4722 case elfcpp::R_ARM_TARGET1
:
4723 // This should have been mapped to another type already.
4725 case elfcpp::R_ARM_COPY
:
4726 case elfcpp::R_ARM_GLOB_DAT
:
4727 case elfcpp::R_ARM_JUMP_SLOT
:
4728 case elfcpp::R_ARM_RELATIVE
:
4729 // These are relocations which should only be seen by the
4730 // dynamic linker, and should never be seen here.
4731 gold_error(_("%s: unexpected reloc %u in object file"),
4732 object
->name().c_str(), r_type
);
4736 object
->error(_("unsupported reloc %u in object file"), r_type
);
4741 // Scan the relocs during a relocatable link.
4743 template<bool big_endian
>
4745 Target_arm
<big_endian
>::scan_relocatable_relocs(
4746 Symbol_table
* symtab
,
4748 Sized_relobj
<32, big_endian
>* object
,
4749 unsigned int data_shndx
,
4750 unsigned int sh_type
,
4751 const unsigned char* prelocs
,
4753 Output_section
* output_section
,
4754 bool needs_special_offset_handling
,
4755 size_t local_symbol_count
,
4756 const unsigned char* plocal_symbols
,
4757 Relocatable_relocs
* rr
)
4759 gold_assert(sh_type
== elfcpp::SHT_REL
);
4761 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
4762 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4764 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
4765 Scan_relocatable_relocs
>(
4773 needs_special_offset_handling
,
4779 // Relocate a section during a relocatable link.
4781 template<bool big_endian
>
4783 Target_arm
<big_endian
>::relocate_for_relocatable(
4784 const Relocate_info
<32, big_endian
>* relinfo
,
4785 unsigned int sh_type
,
4786 const unsigned char* prelocs
,
4788 Output_section
* output_section
,
4789 off_t offset_in_output_section
,
4790 const Relocatable_relocs
* rr
,
4791 unsigned char* view
,
4792 Arm_address view_address
,
4793 section_size_type view_size
,
4794 unsigned char* reloc_view
,
4795 section_size_type reloc_view_size
)
4797 gold_assert(sh_type
== elfcpp::SHT_REL
);
4799 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
4804 offset_in_output_section
,
4813 // Return the value to use for a dynamic symbol which requires special
4814 // treatment. This is how we support equality comparisons of function
4815 // pointers across shared library boundaries, as described in the
4816 // processor specific ABI supplement.
4818 template<bool big_endian
>
4820 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
4822 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4823 return this->plt_section()->address() + gsym
->plt_offset();
4826 // Map platform-specific relocs to real relocs
4828 template<bool big_endian
>
4830 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
4834 case elfcpp::R_ARM_TARGET1
:
4835 // This is either R_ARM_ABS32 or R_ARM_REL32;
4836 return elfcpp::R_ARM_ABS32
;
4838 case elfcpp::R_ARM_TARGET2
:
4839 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4840 return elfcpp::R_ARM_GOT_PREL
;
4847 // Whether if two EABI versions V1 and V2 are compatible.
4849 template<bool big_endian
>
4851 Target_arm
<big_endian
>::are_eabi_versions_compatible(
4852 elfcpp::Elf_Word v1
,
4853 elfcpp::Elf_Word v2
)
4855 // v4 and v5 are the same spec before and after it was released,
4856 // so allow mixing them.
4857 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
4858 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
4864 // Combine FLAGS from an input object called NAME and the processor-specific
4865 // flags in the ELF header of the output. Much of this is adapted from the
4866 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
4867 // in bfd/elf32-arm.c.
4869 template<bool big_endian
>
4871 Target_arm
<big_endian
>::merge_processor_specific_flags(
4872 const std::string
& name
,
4873 elfcpp::Elf_Word flags
)
4875 if (this->are_processor_specific_flags_set())
4877 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
4879 // Nothing to merge if flags equal to those in output.
4880 if (flags
== out_flags
)
4883 // Complain about various flag mismatches.
4884 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
4885 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
4886 if (!this->are_eabi_versions_compatible(version1
, version2
))
4887 gold_error(_("Source object %s has EABI version %d but output has "
4888 "EABI version %d."),
4890 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
4891 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
4895 // If the input is the default architecture and had the default
4896 // flags then do not bother setting the flags for the output
4897 // architecture, instead allow future merges to do this. If no
4898 // future merges ever set these flags then they will retain their
4899 // uninitialised values, which surprise surprise, correspond
4900 // to the default values.
4904 // This is the first time, just copy the flags.
4905 // We only copy the EABI version for now.
4906 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
4910 // Adjust ELF file header.
4911 template<bool big_endian
>
4913 Target_arm
<big_endian
>::do_adjust_elf_header(
4914 unsigned char* view
,
4917 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
4919 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
4920 unsigned char e_ident
[elfcpp::EI_NIDENT
];
4921 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
4923 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
4924 == elfcpp::EF_ARM_EABI_UNKNOWN
)
4925 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
4927 e_ident
[elfcpp::EI_OSABI
] = 0;
4928 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
4930 // FIXME: Do EF_ARM_BE8 adjustment.
4932 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
4933 oehdr
.put_e_ident(e_ident
);
4936 // do_make_elf_object to override the same function in the base class.
4937 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
4938 // to store ARM specific information. Hence we need to have our own
4939 // ELF object creation.
4941 template<bool big_endian
>
4943 Target_arm
<big_endian
>::do_make_elf_object(
4944 const std::string
& name
,
4945 Input_file
* input_file
,
4946 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
4948 int et
= ehdr
.get_e_type();
4949 if (et
== elfcpp::ET_REL
)
4951 Arm_relobj
<big_endian
>* obj
=
4952 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
4956 else if (et
== elfcpp::ET_DYN
)
4958 Sized_dynobj
<32, big_endian
>* obj
=
4959 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
4965 gold_error(_("%s: unsupported ELF file type %d"),
4971 // The selector for arm object files.
4973 template<bool big_endian
>
4974 class Target_selector_arm
: public Target_selector
4977 Target_selector_arm()
4978 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
4979 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
4983 do_instantiate_target()
4984 { return new Target_arm
<big_endian
>(); }
4987 Target_selector_arm
<false> target_selector_arm
;
4988 Target_selector_arm
<true> target_selector_armbe
;
4990 } // End anonymous namespace.