1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
35 #include "parameters.h"
42 #include "copy-relocs.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
55 template<bool big_endian
>
56 class Output_data_plt_arm
;
58 template<bool big_endian
>
61 template<bool big_endian
>
62 class Arm_input_section
;
64 template<bool big_endian
>
65 class Arm_output_section
;
67 template<bool big_endian
>
70 template<bool big_endian
>
74 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
84 // The arm target class.
86 // This is a very simple port of gold for ARM-EABI. It is intended for
87 // supporting Android only for the time being. Only these relocation types
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
124 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
130 // There are probably a lot more.
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
138 // Types of instruction templates.
147 // Factory methods to create instrunction templates in different formats.
149 static const Insn_template
150 thumb16_insn(uint32_t data
)
151 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data
)
157 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 1); }
159 static const Insn_template
160 thumb32_insn(uint32_t data
)
161 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
163 static const Insn_template
164 thumb32_b_insn(uint32_t data
, int reloc_addend
)
166 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
170 static const Insn_template
171 arm_insn(uint32_t data
)
172 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
174 static const Insn_template
175 arm_rel_insn(unsigned data
, int reloc_addend
)
176 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
178 static const Insn_template
179 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
180 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
182 // Accessors. This class is used for read-only objects so no modifiers
187 { return this->data_
; }
189 // Return the instruction sequence type of this.
192 { return this->type_
; }
194 // Return the ARM relocation type of this.
197 { return this->r_type_
; }
201 { return this->reloc_addend_
; }
203 // Return size of instrunction template in bytes.
207 // Return byte-alignment of instrunction template.
212 // We make the constructor private to ensure that only the factory
215 Insn_template(unsigned data
, Type type
, unsigned int r_type
, int reloc_addend
)
216 : data_(data
), type_(type
), r_type_(r_type
), reloc_addend_(reloc_addend
)
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
222 // Instruction template type.
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_
;
226 // Relocation addend.
227 int32_t reloc_addend_
;
230 // Macro for generating code to stub types. One entry per long/short
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
253 #define DEF_STUB(x) arm_stub_##x,
259 // First reloc stub type.
260 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
261 // Last reloc stub type.
262 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
270 arm_stub_type_last
= arm_stub_a8_veneer_blx
274 // Stub template class. Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
281 Stub_template(Stub_type
, const Insn_template
*, size_t);
289 { return this->type_
; }
291 // Return an array of instruction templates.
294 { return this->insns_
; }
296 // Return size of template in number of instructions.
299 { return this->insn_count_
; }
301 // Return size of template in bytes.
304 { return this->size_
; }
306 // Return alignment of the stub template.
309 { return this->alignment_
; }
311 // Return whether entry point is in thumb mode.
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_
; }
316 // Return number of relocations in this template.
319 { return this->relocs_
.size(); }
321 // Return index of the I-th instruction with relocation.
323 reloc_insn_index(size_t i
) const
325 gold_assert(i
< this->relocs_
.size());
326 return this->relocs_
[i
].first
;
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
332 reloc_offset(size_t i
) const
334 gold_assert(i
< this->relocs_
.size());
335 return this->relocs_
[i
].second
;
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair
<size_t, section_size_type
> Reloc
;
343 // A Stub_template may not be copied. We want to share templates as much
345 Stub_template(const Stub_template
&);
346 Stub_template
& operator=(const Stub_template
&);
350 // Points to an array of Insn_templates.
351 const Insn_template
* insns_
;
352 // Number of Insn_templates in insns_[].
354 // Size of templated instructions in bytes.
356 // Alignment of templated instructions.
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_
;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector
<Reloc
> relocs_
;
367 // A class for code stubs. This is a base class for different type of
368 // stubs used in the ARM target.
374 static const section_offset_type invalid_offset
=
375 static_cast<section_offset_type
>(-1);
378 Stub(const Stub_template
* stub_template
)
379 : stub_template_(stub_template
), offset_(invalid_offset
)
386 // Return the stub template.
388 stub_template() const
389 { return this->stub_template_
; }
391 // Return offset of code stub from beginning of its containing stub table.
395 gold_assert(this->offset_
!= invalid_offset
);
396 return this->offset_
;
399 // Set offset of code stub from beginning of its containing stub table.
401 set_offset(section_offset_type offset
)
402 { this->offset_
= offset
; }
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
407 reloc_target(size_t i
)
408 { return this->do_reloc_target(i
); }
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
412 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
413 { this->do_write(view
, view_size
, big_endian
); }
416 // This must be defined in the child class.
418 do_reloc_target(size_t) = 0;
420 // This must be defined in the child class.
422 do_write(unsigned char*, section_size_type
, bool) = 0;
426 const Stub_template
* stub_template_
;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_
;
431 // Reloc stub class. These are stubs we use to fix up relocation because
432 // of limited branch ranges.
434 class Reloc_stub
: public Stub
437 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
441 // Return destination address.
443 destination_address() const
445 gold_assert(this->destination_address_
!= this->invalid_address
);
446 return this->destination_address_
;
449 // Set destination address.
451 set_destination_address(Arm_address address
)
453 gold_assert(address
!= this->invalid_address
);
454 this->destination_address_
= address
;
457 // Reset destination address.
459 reset_destination_address()
460 { this->destination_address_
= this->invalid_address
; }
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
467 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
468 Arm_address branch_target
, bool target_is_thumb
);
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type
, const Symbol
* symbol
, const Relobj
* relobj
,
481 unsigned int r_sym
, int32_t addend
)
482 : stub_type_(stub_type
), addend_(addend
)
486 this->r_sym_
= Reloc_stub::invalid_index
;
487 this->u_
.symbol
= symbol
;
491 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
492 this->r_sym_
= r_sym
;
493 this->u_
.relobj
= relobj
;
500 // Accessors: Keys are meant to be read-only object so no modifiers are
506 { return this->stub_type_
; }
508 // Return the local symbol index or invalid_index.
511 { return this->r_sym_
; }
513 // Return the symbol if there is one.
516 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
518 // Return the relobj if there is one.
521 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
523 // Whether this equals to another key k.
525 eq(const Key
& k
) const
527 return ((this->stub_type_
== k
.stub_type_
)
528 && (this->r_sym_
== k
.r_sym_
)
529 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
530 ? (this->u_
.relobj
== k
.u_
.relobj
)
531 : (this->u_
.symbol
== k
.u_
.symbol
))
532 && (this->addend_
== k
.addend_
));
535 // Return a hash value.
539 return (this->stub_type_
541 ^ gold::string_hash
<char>(
542 (this->r_sym_
!= Reloc_stub::invalid_index
)
543 ? this->u_
.relobj
->name().c_str()
544 : this->u_
.symbol
->name())
548 // Functors for STL associative containers.
552 operator()(const Key
& k
) const
553 { return k
.hash_value(); }
559 operator()(const Key
& k1
, const Key
& k2
) const
560 { return k1
.eq(k2
); }
563 // Name of key. This is mainly for debugging.
569 Stub_type stub_type_
;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
581 const Symbol
* symbol
;
582 const Relobj
* relobj
;
584 // Addend associated with a reloc.
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template
* stub_template
)
591 : Stub(stub_template
), destination_address_(invalid_address
)
597 friend class Stub_factory
;
600 // Return the relocation target address of the i-th relocation in the
603 do_reloc_target(size_t i
)
605 // All reloc stub have only one relocation.
607 return this->destination_address_
;
610 // A template to implement do_write below.
611 template<bool big_endian
>
613 do_fixed_endian_write(unsigned char*, section_size_type
);
617 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
);
619 // Address of destination.
620 Arm_address destination_address_
;
623 // Stub factory class.
628 // Return the unique instance of this class.
629 static const Stub_factory
&
632 static Stub_factory singleton
;
636 // Make a relocation stub.
638 make_reloc_stub(Stub_type stub_type
) const
640 gold_assert(stub_type
>= arm_stub_reloc_first
641 && stub_type
<= arm_stub_reloc_last
);
642 return new Reloc_stub(this->stub_templates_
[stub_type
]);
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory
&);
653 Stub_factory
& operator=(Stub_factory
&);
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
659 // A class to hold stubs for the ARM target.
661 template<bool big_endian
>
662 class Stub_table
: public Output_data
665 Stub_table(Arm_input_section
<big_endian
>* owner
)
666 : Output_data(), addralign_(1), owner_(owner
), has_been_changed_(false),
673 // Owner of this stub table.
674 Arm_input_section
<big_endian
>*
676 { return this->owner_
; }
678 // Whether this stub table is empty.
681 { return this->reloc_stubs_
.empty(); }
683 // Whether this has been changed.
685 has_been_changed() const
686 { return this->has_been_changed_
; }
688 // Set the has-been-changed flag.
690 set_has_been_changed(bool value
)
691 { this->has_been_changed_
= value
; }
693 // Return the current data size.
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
701 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
);
703 // Look up a relocation stub using KEY. Return NULL if there is none.
705 find_reloc_stub(const Reloc_stub::Key
& key
) const
707 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
708 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
711 // Relocate stubs in this stub table.
713 relocate_stubs(const Relocate_info
<32, big_endian
>*,
714 Target_arm
<big_endian
>*, Output_section
*,
715 unsigned char*, Arm_address
, section_size_type
);
718 // Write out section contents.
720 do_write(Output_file
*);
722 // Return the required alignment.
725 { return this->addralign_
; }
727 // Finalize data size.
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
732 // Reset address and file offset.
734 do_reset_address_and_file_offset();
737 // Unordered map of stubs.
739 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
740 Reloc_stub::Key::equal_to
>
745 // Owner of this stub table.
746 Arm_input_section
<big_endian
>* owner_
;
747 // This is set to true during relaxiong if the size of the stub table
749 bool has_been_changed_
;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_
;
754 // A class to wrap an ordinary input section containing executable code.
756 template<bool big_endian
>
757 class Arm_input_section
: public Output_relaxed_input_section
760 Arm_input_section(Relobj
* relobj
, unsigned int shndx
)
761 : Output_relaxed_input_section(relobj
, shndx
, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL
)
772 // Whether this is a stub table owner.
774 is_stub_table_owner() const
775 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
777 // Return the stub table.
778 Stub_table
<big_endian
>*
780 { return this->stub_table_
; }
782 // Set the stub_table.
784 set_stub_table(Stub_table
<big_endian
>* stub_table
)
785 { this->stub_table_
= stub_table
; }
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section
<big_endian
>*
790 as_arm_input_section(Output_relaxed_input_section
* poris
)
791 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
794 // Write data to output file.
796 do_write(Output_file
*);
798 // Return required alignment of this.
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_
->addralign(),
804 this->original_addralign_
);
806 return this->original_addralign_
;
809 // Finalize data size.
811 set_final_data_size();
813 // Reset address and file offset.
815 do_reset_address_and_file_offset();
819 do_output_offset(const Relobj
* object
, unsigned int shndx
,
820 section_offset_type offset
,
821 section_offset_type
* poutput
) const
823 if ((object
== this->relobj())
824 && (shndx
== this->shndx())
826 && (convert_types
<uint64_t, section_offset_type
>(offset
)
827 <= this->original_size_
))
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section
&);
839 Arm_input_section
& operator=(const Arm_input_section
&);
841 // Address alignment of the original input section.
842 uint64_t original_addralign_
;
843 // Section size of the original input section.
844 uint64_t original_size_
;
846 Stub_table
<big_endian
>* stub_table_
;
849 // Arm output section class. This is defined mainly to add a number of
850 // stub generation methods.
852 template<bool big_endian
>
853 class Arm_output_section
: public Output_section
856 Arm_output_section(const char* name
, elfcpp::Elf_Word type
,
857 elfcpp::Elf_Xword flags
)
858 : Output_section(name
, type
, flags
)
861 ~Arm_output_section()
864 // Group input sections for stub generation.
866 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section
<big_endian
>*
871 as_arm_output_section(Output_section
* os
)
872 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
876 typedef Output_section::Input_section Input_section
;
877 typedef Output_section::Input_section_list Input_section_list
;
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator
,
881 Input_section_list::const_iterator
,
882 Input_section_list::const_iterator
,
883 Target_arm
<big_endian
>*,
884 std::vector
<Output_relaxed_input_section
*>*);
889 template<bool big_endian
>
890 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
893 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
895 Arm_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
896 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
897 : Sized_relobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
898 stub_tables_(), local_symbol_is_thumb_function_()
904 // Return the stub table of the SHNDX-th section if there is one.
905 Stub_table
<big_endian
>*
906 stub_table(unsigned int shndx
) const
908 gold_assert(shndx
< this->stub_tables_
.size());
909 return this->stub_tables_
[shndx
];
912 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
914 set_stub_table(unsigned int shndx
, Stub_table
<big_endian
>* stub_table
)
916 gold_assert(shndx
< this->stub_tables_
.size());
917 this->stub_tables_
[shndx
] = stub_table
;
920 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
921 // index. This is only valid after do_count_local_symbol is called.
923 local_symbol_is_thumb_function(unsigned int r_sym
) const
925 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
926 return this->local_symbol_is_thumb_function_
[r_sym
];
929 // Scan all relocation sections for stub generation.
931 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
934 // Convert regular input section with index SHNDX to a relaxed section.
936 convert_input_section_to_relaxed_section(unsigned shndx
)
938 // The stubs have relocations and we need to process them after writing
939 // out the stubs. So relocation now must follow section write.
940 this->invalidate_section_offset(shndx
);
941 this->set_relocs_must_follow_section_writes();
944 // Downcast a base pointer to an Arm_relobj pointer. This is
945 // not type-safe but we only use Arm_relobj not the base class.
946 static Arm_relobj
<big_endian
>*
947 as_arm_relobj(Relobj
* relobj
)
948 { return static_cast<Arm_relobj
<big_endian
>*>(relobj
); }
950 // Processor-specific flags in ELF file header. This is valid only after
953 processor_specific_flags() const
954 { return this->processor_specific_flags_
; }
957 // Post constructor setup.
961 // Call parent's setup method.
962 Sized_relobj
<32, big_endian
>::do_setup();
964 // Initialize look-up tables.
965 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
966 this->stub_tables_
.swap(empty_stub_table_list
);
969 // Count the local symbols.
971 do_count_local_symbols(Stringpool_template
<char>*,
972 Stringpool_template
<char>*);
975 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
976 const unsigned char* pshdrs
,
977 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
979 // Read the symbol information.
981 do_read_symbols(Read_symbols_data
* sd
);
984 // List of stub tables.
985 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
986 Stub_table_list stub_tables_
;
987 // Bit vector to tell if a local symbol is a thumb function or not.
988 // This is only valid after do_count_local_symbol is called.
989 std::vector
<bool> local_symbol_is_thumb_function_
;
990 // processor-specific flags in ELF file header.
991 elfcpp::Elf_Word processor_specific_flags_
;
996 template<bool big_endian
>
997 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1000 Arm_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1001 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1002 : Sized_dynobj
<32, big_endian
>(name
, input_file
, offset
, ehdr
),
1003 processor_specific_flags_(0)
1009 // Downcast a base pointer to an Arm_relobj pointer. This is
1010 // not type-safe but we only use Arm_relobj not the base class.
1011 static Arm_dynobj
<big_endian
>*
1012 as_arm_dynobj(Dynobj
* dynobj
)
1013 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1015 // Processor-specific flags in ELF file header. This is valid only after
1018 processor_specific_flags() const
1019 { return this->processor_specific_flags_
; }
1022 // Read the symbol information.
1024 do_read_symbols(Read_symbols_data
* sd
);
1027 // processor-specific flags in ELF file header.
1028 elfcpp::Elf_Word processor_specific_flags_
;
1031 // Functor to read reloc addends during stub generation.
1033 template<int sh_type
, bool big_endian
>
1034 struct Stub_addend_reader
1036 // Return the addend for a relocation of a particular type. Depending
1037 // on whether this is a REL or RELA relocation, read the addend from a
1038 // view or from a Reloc object.
1039 elfcpp::Elf_types
<32>::Elf_Swxword
1041 unsigned int /* r_type */,
1042 const unsigned char* /* view */,
1043 const typename Reloc_types
<sh_type
,
1044 32, big_endian
>::Reloc
& /* reloc */) const;
1047 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1049 template<bool big_endian
>
1050 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1052 elfcpp::Elf_types
<32>::Elf_Swxword
1055 const unsigned char*,
1056 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1059 // Specialized Stub_addend_reader for RELA type relocation sections.
1060 // We currently do not handle RELA type relocation sections but it is trivial
1061 // to implement the addend reader. This is provided for completeness and to
1062 // make it easier to add support for RELA relocation sections in the future.
1064 template<bool big_endian
>
1065 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1067 elfcpp::Elf_types
<32>::Elf_Swxword
1070 const unsigned char*,
1071 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1072 big_endian
>::Reloc
& reloc
) const
1073 { return reloc
.get_r_addend(); }
1076 // Utilities for manipulating integers of up to 32-bits
1080 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1081 // an int32_t. NO_BITS must be between 1 to 32.
1082 template<int no_bits
>
1083 static inline int32_t
1084 sign_extend(uint32_t bits
)
1086 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1088 return static_cast<int32_t>(bits
);
1089 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1091 uint32_t top_bit
= 1U << (no_bits
- 1);
1092 int32_t as_signed
= static_cast<int32_t>(bits
);
1093 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1096 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1097 template<int no_bits
>
1099 has_overflow(uint32_t bits
)
1101 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1104 int32_t max
= (1 << (no_bits
- 1)) - 1;
1105 int32_t min
= -(1 << (no_bits
- 1));
1106 int32_t as_signed
= static_cast<int32_t>(bits
);
1107 return as_signed
> max
|| as_signed
< min
;
1110 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1111 // fits in the given number of bits as either a signed or unsigned value.
1112 // For example, has_signed_unsigned_overflow<8> would check
1113 // -128 <= bits <= 255
1114 template<int no_bits
>
1116 has_signed_unsigned_overflow(uint32_t bits
)
1118 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1121 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1122 int32_t min
= -(1 << (no_bits
- 1));
1123 int32_t as_signed
= static_cast<int32_t>(bits
);
1124 return as_signed
> max
|| as_signed
< min
;
1127 // Select bits from A and B using bits in MASK. For each n in [0..31],
1128 // the n-th bit in the result is chosen from the n-th bits of A and B.
1129 // A zero selects A and a one selects B.
1130 static inline uint32_t
1131 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1132 { return (a
& ~mask
) | (b
& mask
); }
1135 template<bool big_endian
>
1136 class Target_arm
: public Sized_target
<32, big_endian
>
1139 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1142 // When were are relocating a stub, we pass this as the relocation number.
1143 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1146 : Sized_target
<32, big_endian
>(&arm_info
),
1147 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1148 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1149 stub_factory_(Stub_factory::get_instance()),
1150 may_use_blx_(true), should_force_pic_veneer_(false),
1151 arm_input_section_map_()
1154 // Whether we can use BLX.
1157 { return this->may_use_blx_
; }
1159 // Set use-BLX flag.
1161 set_may_use_blx(bool value
)
1162 { this->may_use_blx_
= value
; }
1164 // Whether we force PCI branch veneers.
1166 should_force_pic_veneer() const
1167 { return this->should_force_pic_veneer_
; }
1169 // Set PIC veneer flag.
1171 set_should_force_pic_veneer(bool value
)
1172 { this->should_force_pic_veneer_
= value
; }
1174 // Whether we use THUMB-2 instructions.
1176 using_thumb2() const
1178 // FIXME: This should not hard-coded.
1182 // Whether we use THUMB/THUMB-2 instructions only.
1184 using_thumb_only() const
1186 // FIXME: This should not hard-coded.
1190 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1192 may_use_arm_nop() const
1194 // FIXME: This should not hard-coded.
1198 // Process the relocations to determine unreferenced sections for
1199 // garbage collection.
1201 gc_process_relocs(Symbol_table
* symtab
,
1203 Sized_relobj
<32, big_endian
>* object
,
1204 unsigned int data_shndx
,
1205 unsigned int sh_type
,
1206 const unsigned char* prelocs
,
1208 Output_section
* output_section
,
1209 bool needs_special_offset_handling
,
1210 size_t local_symbol_count
,
1211 const unsigned char* plocal_symbols
);
1213 // Scan the relocations to look for symbol adjustments.
1215 scan_relocs(Symbol_table
* symtab
,
1217 Sized_relobj
<32, big_endian
>* object
,
1218 unsigned int data_shndx
,
1219 unsigned int sh_type
,
1220 const unsigned char* prelocs
,
1222 Output_section
* output_section
,
1223 bool needs_special_offset_handling
,
1224 size_t local_symbol_count
,
1225 const unsigned char* plocal_symbols
);
1227 // Finalize the sections.
1229 do_finalize_sections(Layout
*, const Input_objects
*);
1231 // Return the value to use for a dynamic symbol which requires special
1234 do_dynsym_value(const Symbol
*) const;
1236 // Relocate a section.
1238 relocate_section(const Relocate_info
<32, big_endian
>*,
1239 unsigned int sh_type
,
1240 const unsigned char* prelocs
,
1242 Output_section
* output_section
,
1243 bool needs_special_offset_handling
,
1244 unsigned char* view
,
1245 Arm_address view_address
,
1246 section_size_type view_size
,
1247 const Reloc_symbol_changes
*);
1249 // Scan the relocs during a relocatable link.
1251 scan_relocatable_relocs(Symbol_table
* symtab
,
1253 Sized_relobj
<32, big_endian
>* object
,
1254 unsigned int data_shndx
,
1255 unsigned int sh_type
,
1256 const unsigned char* prelocs
,
1258 Output_section
* output_section
,
1259 bool needs_special_offset_handling
,
1260 size_t local_symbol_count
,
1261 const unsigned char* plocal_symbols
,
1262 Relocatable_relocs
*);
1264 // Relocate a section during a relocatable link.
1266 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1267 unsigned int sh_type
,
1268 const unsigned char* prelocs
,
1270 Output_section
* output_section
,
1271 off_t offset_in_output_section
,
1272 const Relocatable_relocs
*,
1273 unsigned char* view
,
1274 Arm_address view_address
,
1275 section_size_type view_size
,
1276 unsigned char* reloc_view
,
1277 section_size_type reloc_view_size
);
1279 // Return whether SYM is defined by the ABI.
1281 do_is_defined_by_abi(Symbol
* sym
) const
1282 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1284 // Return the size of the GOT section.
1288 gold_assert(this->got_
!= NULL
);
1289 return this->got_
->data_size();
1292 // Map platform-specific reloc types
1294 get_real_reloc_type (unsigned int r_type
);
1297 // Methods to support stub-generations.
1300 // Return the stub factory
1302 stub_factory() const
1303 { return this->stub_factory_
; }
1305 // Make a new Arm_input_section object.
1306 Arm_input_section
<big_endian
>*
1307 new_arm_input_section(Relobj
*, unsigned int);
1309 // Find the Arm_input_section object corresponding to the SHNDX-th input
1310 // section of RELOBJ.
1311 Arm_input_section
<big_endian
>*
1312 find_arm_input_section(Relobj
* relobj
, unsigned int shndx
) const;
1314 // Make a new Stub_table
1315 Stub_table
<big_endian
>*
1316 new_stub_table(Arm_input_section
<big_endian
>*);
1318 // Scan a section for stub generation.
1320 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1321 const unsigned char*, size_t, Output_section
*,
1322 bool, const unsigned char*, Arm_address
,
1327 relocate_stub(Reloc_stub
*, const Relocate_info
<32, big_endian
>*,
1328 Output_section
*, unsigned char*, Arm_address
,
1331 // Get the default ARM target.
1332 static Target_arm
<big_endian
>*
1335 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1336 && parameters
->target().is_big_endian() == big_endian
);
1337 return static_cast<Target_arm
<big_endian
>*>(
1338 parameters
->sized_target
<32, big_endian
>());
1341 // Whether relocation type uses LSB to distinguish THUMB addresses.
1343 reloc_uses_thumb_bit(unsigned int r_type
);
1346 // Make an ELF object.
1348 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1349 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1352 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1353 const elfcpp::Ehdr
<32, !big_endian
>&)
1354 { gold_unreachable(); }
1357 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1358 const elfcpp::Ehdr
<64, false>&)
1359 { gold_unreachable(); }
1362 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1363 const elfcpp::Ehdr
<64, true>&)
1364 { gold_unreachable(); }
1366 // Make an output section.
1368 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1369 elfcpp::Elf_Xword flags
)
1370 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1373 do_adjust_elf_header(unsigned char* view
, int len
) const;
1375 // We only need to generate stubs, and hence perform relaxation if we are
1376 // not doing relocatable linking.
1378 do_may_relax() const
1379 { return !parameters
->options().relocatable(); }
1382 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1385 // The class which scans relocations.
1390 : issued_non_pic_error_(false)
1394 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1395 Sized_relobj
<32, big_endian
>* object
,
1396 unsigned int data_shndx
,
1397 Output_section
* output_section
,
1398 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1399 const elfcpp::Sym
<32, big_endian
>& lsym
);
1402 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1403 Sized_relobj
<32, big_endian
>* object
,
1404 unsigned int data_shndx
,
1405 Output_section
* output_section
,
1406 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1411 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1412 unsigned int r_type
);
1415 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1416 unsigned int r_type
, Symbol
*);
1419 check_non_pic(Relobj
*, unsigned int r_type
);
1421 // Almost identical to Symbol::needs_plt_entry except that it also
1422 // handles STT_ARM_TFUNC.
1424 symbol_needs_plt_entry(const Symbol
* sym
)
1426 // An undefined symbol from an executable does not need a PLT entry.
1427 if (sym
->is_undefined() && !parameters
->options().shared())
1430 return (!parameters
->doing_static_link()
1431 && (sym
->type() == elfcpp::STT_FUNC
1432 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1433 && (sym
->is_from_dynobj()
1434 || sym
->is_undefined()
1435 || sym
->is_preemptible()));
1438 // Whether we have issued an error about a non-PIC compilation.
1439 bool issued_non_pic_error_
;
1442 // The class which implements relocation.
1452 // Return whether the static relocation needs to be applied.
1454 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1457 Output_section
* output_section
);
1459 // Do a relocation. Return false if the caller should not issue
1460 // any warnings about this relocation.
1462 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1463 Output_section
*, size_t relnum
,
1464 const elfcpp::Rel
<32, big_endian
>&,
1465 unsigned int r_type
, const Sized_symbol
<32>*,
1466 const Symbol_value
<32>*,
1467 unsigned char*, Arm_address
,
1470 // Return whether we want to pass flag NON_PIC_REF for this
1473 reloc_is_non_pic (unsigned int r_type
)
1477 case elfcpp::R_ARM_REL32
:
1478 case elfcpp::R_ARM_THM_CALL
:
1479 case elfcpp::R_ARM_CALL
:
1480 case elfcpp::R_ARM_JUMP24
:
1481 case elfcpp::R_ARM_PREL31
:
1482 case elfcpp::R_ARM_THM_ABS5
:
1483 case elfcpp::R_ARM_ABS8
:
1484 case elfcpp::R_ARM_ABS12
:
1485 case elfcpp::R_ARM_ABS16
:
1486 case elfcpp::R_ARM_BASE_ABS
:
1494 // A class which returns the size required for a relocation type,
1495 // used while scanning relocs during a relocatable link.
1496 class Relocatable_size_for_reloc
1500 get_size_for_reloc(unsigned int, Relobj
*);
1503 // Get the GOT section, creating it if necessary.
1504 Output_data_got
<32, big_endian
>*
1505 got_section(Symbol_table
*, Layout
*);
1507 // Get the GOT PLT section.
1509 got_plt_section() const
1511 gold_assert(this->got_plt_
!= NULL
);
1512 return this->got_plt_
;
1515 // Create a PLT entry for a global symbol.
1517 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1519 // Get the PLT section.
1520 const Output_data_plt_arm
<big_endian
>*
1523 gold_assert(this->plt_
!= NULL
);
1527 // Get the dynamic reloc section, creating it if necessary.
1529 rel_dyn_section(Layout
*);
1531 // Return true if the symbol may need a COPY relocation.
1532 // References from an executable object to non-function symbols
1533 // defined in a dynamic object may need a COPY relocation.
1535 may_need_copy_reloc(Symbol
* gsym
)
1537 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1538 && gsym
->may_need_copy_reloc());
1541 // Add a potential copy relocation.
1543 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1544 Sized_relobj
<32, big_endian
>* object
,
1545 unsigned int shndx
, Output_section
* output_section
,
1546 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1548 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1549 symtab
->get_sized_symbol
<32>(sym
),
1550 object
, shndx
, output_section
, reloc
,
1551 this->rel_dyn_section(layout
));
1554 // Whether two EABI versions are compatible.
1556 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1558 // Merge processor-specific flags from input object and those in the ELF
1559 // header of the output.
1561 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1564 // Methods to support stub-generations.
1567 // Group input sections for stub generation.
1569 group_sections(Layout
*, section_size_type
, bool);
1571 // Scan a relocation for stub generation.
1573 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1574 const Sized_symbol
<32>*, unsigned int,
1575 const Symbol_value
<32>*,
1576 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1578 // Scan a relocation section for stub.
1579 template<int sh_type
>
1581 scan_reloc_section_for_stubs(
1582 const Relocate_info
<32, big_endian
>* relinfo
,
1583 const unsigned char* prelocs
,
1585 Output_section
* output_section
,
1586 bool needs_special_offset_handling
,
1587 const unsigned char* view
,
1588 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1591 // Information about this specific target which we pass to the
1592 // general Target structure.
1593 static const Target::Target_info arm_info
;
1595 // The types of GOT entries needed for this platform.
1598 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1601 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1603 // Map input section to Arm_input_section.
1604 typedef Unordered_map
<Input_section_specifier
,
1605 Arm_input_section
<big_endian
>*,
1606 Input_section_specifier::hash
,
1607 Input_section_specifier::equal_to
>
1608 Arm_input_section_map
;
1611 Output_data_got
<32, big_endian
>* got_
;
1613 Output_data_plt_arm
<big_endian
>* plt_
;
1614 // The GOT PLT section.
1615 Output_data_space
* got_plt_
;
1616 // The dynamic reloc section.
1617 Reloc_section
* rel_dyn_
;
1618 // Relocs saved to avoid a COPY reloc.
1619 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1620 // Space for variables copied with a COPY reloc.
1621 Output_data_space
* dynbss_
;
1622 // Vector of Stub_tables created.
1623 Stub_table_list stub_tables_
;
1625 const Stub_factory
&stub_factory_
;
1626 // Whether we can use BLX.
1628 // Whether we force PIC branch veneers.
1629 bool should_force_pic_veneer_
;
1630 // Map for locating Arm_input_sections.
1631 Arm_input_section_map arm_input_section_map_
;
1634 template<bool big_endian
>
1635 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1638 big_endian
, // is_big_endian
1639 elfcpp::EM_ARM
, // machine_code
1640 false, // has_make_symbol
1641 false, // has_resolve
1642 false, // has_code_fill
1643 true, // is_default_stack_executable
1645 "/usr/lib/libc.so.1", // dynamic_linker
1646 0x8000, // default_text_segment_address
1647 0x1000, // abi_pagesize (overridable by -z max-page-size)
1648 0x1000, // common_pagesize (overridable by -z common-page-size)
1649 elfcpp::SHN_UNDEF
, // small_common_shndx
1650 elfcpp::SHN_UNDEF
, // large_common_shndx
1651 0, // small_common_section_flags
1652 0 // large_common_section_flags
1655 // Arm relocate functions class
1658 template<bool big_endian
>
1659 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1664 STATUS_OKAY
, // No error during relocation.
1665 STATUS_OVERFLOW
, // Relocation oveflow.
1666 STATUS_BAD_RELOC
// Relocation cannot be applied.
1670 typedef Relocate_functions
<32, big_endian
> Base
;
1671 typedef Arm_relocate_functions
<big_endian
> This
;
1673 // Encoding of imm16 argument for movt and movw ARM instructions
1676 // imm16 := imm4 | imm12
1678 // 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
1679 // +-------+---------------+-------+-------+-----------------------+
1680 // | | |imm4 | |imm12 |
1681 // +-------+---------------+-------+-------+-----------------------+
1683 // Extract the relocation addend from VAL based on the ARM
1684 // instruction encoding described above.
1685 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1686 extract_arm_movw_movt_addend(
1687 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1689 // According to the Elf ABI for ARM Architecture the immediate
1690 // field is sign-extended to form the addend.
1691 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1694 // Insert X into VAL based on the ARM instruction encoding described
1696 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1697 insert_val_arm_movw_movt(
1698 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1699 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1703 val
|= (x
& 0xf000) << 4;
1707 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1710 // imm16 := imm4 | i | imm3 | imm8
1712 // 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
1713 // +---------+-+-----------+-------++-+-----+-------+---------------+
1714 // | |i| |imm4 || |imm3 | |imm8 |
1715 // +---------+-+-----------+-------++-+-----+-------+---------------+
1717 // Extract the relocation addend from VAL based on the Thumb2
1718 // instruction encoding described above.
1719 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1720 extract_thumb_movw_movt_addend(
1721 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1723 // According to the Elf ABI for ARM Architecture the immediate
1724 // field is sign-extended to form the addend.
1725 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1726 | ((val
>> 15) & 0x0800)
1727 | ((val
>> 4) & 0x0700)
1731 // Insert X into VAL based on the Thumb2 instruction encoding
1733 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1734 insert_val_thumb_movw_movt(
1735 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1736 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1739 val
|= (x
& 0xf000) << 4;
1740 val
|= (x
& 0x0800) << 15;
1741 val
|= (x
& 0x0700) << 4;
1742 val
|= (x
& 0x00ff);
1746 // Handle ARM long branches.
1747 static typename
This::Status
1748 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1749 unsigned char *, const Sized_symbol
<32>*,
1750 const Arm_relobj
<big_endian
>*, unsigned int,
1751 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1755 // R_ARM_ABS8: S + A
1756 static inline typename
This::Status
1757 abs8(unsigned char *view
,
1758 const Sized_relobj
<32, big_endian
>* object
,
1759 const Symbol_value
<32>* psymval
)
1761 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1762 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1763 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1764 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1765 Reltype addend
= utils::sign_extend
<8>(val
);
1766 Reltype x
= psymval
->value(object
, addend
);
1767 val
= utils::bit_select(val
, x
, 0xffU
);
1768 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1769 return (utils::has_signed_unsigned_overflow
<8>(x
)
1770 ? This::STATUS_OVERFLOW
1771 : This::STATUS_OKAY
);
1774 // R_ARM_THM_ABS5: S + A
1775 static inline typename
This::Status
1776 thm_abs5(unsigned char *view
,
1777 const Sized_relobj
<32, big_endian
>* object
,
1778 const Symbol_value
<32>* psymval
)
1780 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1781 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1782 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1783 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1784 Reltype addend
= (val
& 0x7e0U
) >> 6;
1785 Reltype x
= psymval
->value(object
, addend
);
1786 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1787 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1788 return (utils::has_overflow
<5>(x
)
1789 ? This::STATUS_OVERFLOW
1790 : This::STATUS_OKAY
);
1793 // R_ARM_ABS12: S + A
1794 static inline typename
This::Status
1795 abs12(unsigned char *view
,
1796 const Sized_relobj
<32, big_endian
>* object
,
1797 const Symbol_value
<32>* psymval
)
1799 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1800 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1801 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1802 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1803 Reltype addend
= val
& 0x0fffU
;
1804 Reltype x
= psymval
->value(object
, addend
);
1805 val
= utils::bit_select(val
, x
, 0x0fffU
);
1806 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1807 return (utils::has_overflow
<12>(x
)
1808 ? This::STATUS_OVERFLOW
1809 : This::STATUS_OKAY
);
1812 // R_ARM_ABS16: S + A
1813 static inline typename
This::Status
1814 abs16(unsigned char *view
,
1815 const Sized_relobj
<32, big_endian
>* object
,
1816 const Symbol_value
<32>* psymval
)
1818 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1819 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1820 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1821 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1822 Reltype addend
= utils::sign_extend
<16>(val
);
1823 Reltype x
= psymval
->value(object
, addend
);
1824 val
= utils::bit_select(val
, x
, 0xffffU
);
1825 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1826 return (utils::has_signed_unsigned_overflow
<16>(x
)
1827 ? This::STATUS_OVERFLOW
1828 : This::STATUS_OKAY
);
1831 // R_ARM_ABS32: (S + A) | T
1832 static inline typename
This::Status
1833 abs32(unsigned char *view
,
1834 const Sized_relobj
<32, big_endian
>* object
,
1835 const Symbol_value
<32>* psymval
,
1836 Arm_address thumb_bit
)
1838 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1839 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1840 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1841 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
1842 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1843 return This::STATUS_OKAY
;
1846 // R_ARM_REL32: (S + A) | T - P
1847 static inline typename
This::Status
1848 rel32(unsigned char *view
,
1849 const Sized_relobj
<32, big_endian
>* object
,
1850 const Symbol_value
<32>* psymval
,
1851 Arm_address address
,
1852 Arm_address thumb_bit
)
1854 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1855 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1856 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1857 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
1858 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1859 return This::STATUS_OKAY
;
1862 // R_ARM_THM_CALL: (S + A) | T - P
1863 static inline typename
This::Status
1864 thm_call(unsigned char *view
,
1865 const Sized_relobj
<32, big_endian
>* object
,
1866 const Symbol_value
<32>* psymval
,
1867 Arm_address address
,
1868 Arm_address thumb_bit
)
1870 // A thumb call consists of two instructions.
1871 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1872 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1873 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1874 Valtype hi
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1875 Valtype lo
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
1876 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1877 gold_assert((lo
& 0xf800) == 0xf800);
1878 Reltype addend
= utils::sign_extend
<23>(((hi
& 0x7ff) << 12)
1879 | ((lo
& 0x7ff) << 1));
1880 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
1882 // If target has no thumb bit set, we need to either turn the BL
1883 // into a BLX (for ARMv5 or above) or generate a stub.
1886 // This only works for ARMv5 and above with interworking enabled.
1889 hi
= utils::bit_select(hi
, (x
>> 12), 0x7ffU
);
1890 lo
= utils::bit_select(lo
, (x
>> 1), 0x7ffU
);
1891 elfcpp::Swap
<16, big_endian
>::writeval(wv
, hi
);
1892 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lo
);
1893 return (utils::has_overflow
<23>(x
)
1894 ? This::STATUS_OVERFLOW
1895 : This::STATUS_OKAY
);
1898 // R_ARM_BASE_PREL: B(S) + A - P
1899 static inline typename
This::Status
1900 base_prel(unsigned char* view
,
1902 Arm_address address
)
1904 Base::rel32(view
, origin
- address
);
1908 // R_ARM_BASE_ABS: B(S) + A
1909 static inline typename
This::Status
1910 base_abs(unsigned char* view
,
1913 Base::rel32(view
, origin
);
1917 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1918 static inline typename
This::Status
1919 got_brel(unsigned char* view
,
1920 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
1922 Base::rel32(view
, got_offset
);
1923 return This::STATUS_OKAY
;
1926 // R_ARM_GOT_PREL: GOT(S) + A – P
1927 static inline typename
This::Status
1928 got_prel(unsigned char* view
,
1929 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
,
1930 Arm_address address
)
1932 Base::rel32(view
, got_offset
- address
);
1933 return This::STATUS_OKAY
;
1936 // R_ARM_PLT32: (S + A) | T - P
1937 static inline typename
This::Status
1938 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
1939 unsigned char *view
,
1940 const Sized_symbol
<32>* gsym
,
1941 const Arm_relobj
<big_endian
>* object
,
1943 const Symbol_value
<32>* psymval
,
1944 Arm_address address
,
1945 Arm_address thumb_bit
,
1946 bool is_weakly_undefined_without_plt
)
1948 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
1949 object
, r_sym
, psymval
, address
, thumb_bit
,
1950 is_weakly_undefined_without_plt
);
1953 // R_ARM_XPC25: (S + A) | T - P
1954 static inline typename
This::Status
1955 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
1956 unsigned char *view
,
1957 const Sized_symbol
<32>* gsym
,
1958 const Arm_relobj
<big_endian
>* object
,
1960 const Symbol_value
<32>* psymval
,
1961 Arm_address address
,
1962 Arm_address thumb_bit
,
1963 bool is_weakly_undefined_without_plt
)
1965 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
1966 object
, r_sym
, psymval
, address
, thumb_bit
,
1967 is_weakly_undefined_without_plt
);
1970 // R_ARM_CALL: (S + A) | T - P
1971 static inline typename
This::Status
1972 call(const Relocate_info
<32, big_endian
>* relinfo
,
1973 unsigned char *view
,
1974 const Sized_symbol
<32>* gsym
,
1975 const Arm_relobj
<big_endian
>* object
,
1977 const Symbol_value
<32>* psymval
,
1978 Arm_address address
,
1979 Arm_address thumb_bit
,
1980 bool is_weakly_undefined_without_plt
)
1982 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
1983 object
, r_sym
, psymval
, address
, thumb_bit
,
1984 is_weakly_undefined_without_plt
);
1987 // R_ARM_JUMP24: (S + A) | T - P
1988 static inline typename
This::Status
1989 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
1990 unsigned char *view
,
1991 const Sized_symbol
<32>* gsym
,
1992 const Arm_relobj
<big_endian
>* object
,
1994 const Symbol_value
<32>* psymval
,
1995 Arm_address address
,
1996 Arm_address thumb_bit
,
1997 bool is_weakly_undefined_without_plt
)
1999 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2000 object
, r_sym
, psymval
, address
, thumb_bit
,
2001 is_weakly_undefined_without_plt
);
2004 // R_ARM_PREL: (S + A) | T - P
2005 static inline typename
This::Status
2006 prel31(unsigned char *view
,
2007 const Sized_relobj
<32, big_endian
>* object
,
2008 const Symbol_value
<32>* psymval
,
2009 Arm_address address
,
2010 Arm_address thumb_bit
)
2012 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2013 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2014 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2015 Valtype addend
= utils::sign_extend
<31>(val
);
2016 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2017 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2018 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2019 return (utils::has_overflow
<31>(x
) ?
2020 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2023 // R_ARM_MOVW_ABS_NC: (S + A) | T
2024 static inline typename
This::Status
2025 movw_abs_nc(unsigned char *view
,
2026 const Sized_relobj
<32, big_endian
>* object
,
2027 const Symbol_value
<32>* psymval
,
2028 Arm_address thumb_bit
)
2030 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2031 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2032 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2033 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2034 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2035 val
= This::insert_val_arm_movw_movt(val
, x
);
2036 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2037 return This::STATUS_OKAY
;
2040 // R_ARM_MOVT_ABS: S + A
2041 static inline typename
This::Status
2042 movt_abs(unsigned char *view
,
2043 const Sized_relobj
<32, big_endian
>* object
,
2044 const Symbol_value
<32>* psymval
)
2046 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2047 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2048 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2049 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2050 Valtype x
= psymval
->value(object
, addend
) >> 16;
2051 val
= This::insert_val_arm_movw_movt(val
, x
);
2052 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2053 return This::STATUS_OKAY
;
2056 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2057 static inline typename
This::Status
2058 thm_movw_abs_nc(unsigned char *view
,
2059 const Sized_relobj
<32, big_endian
>* object
,
2060 const Symbol_value
<32>* psymval
,
2061 Arm_address thumb_bit
)
2063 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2064 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2065 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2066 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2067 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2068 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2069 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2070 val
= This::insert_val_thumb_movw_movt(val
, x
);
2071 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2072 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2073 return This::STATUS_OKAY
;
2076 // R_ARM_THM_MOVT_ABS: S + A
2077 static inline typename
This::Status
2078 thm_movt_abs(unsigned char *view
,
2079 const Sized_relobj
<32, big_endian
>* object
,
2080 const Symbol_value
<32>* psymval
)
2082 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2083 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2084 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2085 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2086 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2087 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2088 Reltype x
= psymval
->value(object
, addend
) >> 16;
2089 val
= This::insert_val_thumb_movw_movt(val
, x
);
2090 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2091 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2092 return This::STATUS_OKAY
;
2095 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2096 static inline typename
This::Status
2097 movw_prel_nc(unsigned char *view
,
2098 const Sized_relobj
<32, big_endian
>* object
,
2099 const Symbol_value
<32>* psymval
,
2100 Arm_address address
,
2101 Arm_address thumb_bit
)
2103 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2104 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2105 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2106 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2107 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2108 val
= This::insert_val_arm_movw_movt(val
, x
);
2109 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2110 return This::STATUS_OKAY
;
2113 // R_ARM_MOVT_PREL: S + A - P
2114 static inline typename
This::Status
2115 movt_prel(unsigned char *view
,
2116 const Sized_relobj
<32, big_endian
>* object
,
2117 const Symbol_value
<32>* psymval
,
2118 Arm_address address
)
2120 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2121 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2122 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2123 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2124 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2125 val
= This::insert_val_arm_movw_movt(val
, x
);
2126 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2127 return This::STATUS_OKAY
;
2130 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2131 static inline typename
This::Status
2132 thm_movw_prel_nc(unsigned char *view
,
2133 const Sized_relobj
<32, big_endian
>* object
,
2134 const Symbol_value
<32>* psymval
,
2135 Arm_address address
,
2136 Arm_address thumb_bit
)
2138 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2139 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2140 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2141 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2142 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2143 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2144 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2145 val
= This::insert_val_thumb_movw_movt(val
, x
);
2146 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2147 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2148 return This::STATUS_OKAY
;
2151 // R_ARM_THM_MOVT_PREL: S + A - P
2152 static inline typename
This::Status
2153 thm_movt_prel(unsigned char *view
,
2154 const Sized_relobj
<32, big_endian
>* object
,
2155 const Symbol_value
<32>* psymval
,
2156 Arm_address address
)
2158 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2159 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2160 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2161 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2162 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2163 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2164 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2165 val
= This::insert_val_thumb_movw_movt(val
, x
);
2166 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2167 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2168 return This::STATUS_OKAY
;
2172 // Relocate ARM long branches. This handles relocation types
2173 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2174 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2175 // undefined and we do not use PLT in this relocation. In such a case,
2176 // the branch is converted into an NOP.
2178 template<bool big_endian
>
2179 typename Arm_relocate_functions
<big_endian
>::Status
2180 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2181 unsigned int r_type
,
2182 const Relocate_info
<32, big_endian
>* relinfo
,
2183 unsigned char *view
,
2184 const Sized_symbol
<32>* gsym
,
2185 const Arm_relobj
<big_endian
>* object
,
2187 const Symbol_value
<32>* psymval
,
2188 Arm_address address
,
2189 Arm_address thumb_bit
,
2190 bool is_weakly_undefined_without_plt
)
2192 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2193 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2194 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2196 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2197 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2198 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2199 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2200 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2201 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2202 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2204 // Check that the instruction is valid.
2205 if (r_type
== elfcpp::R_ARM_CALL
)
2207 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2208 return This::STATUS_BAD_RELOC
;
2210 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2212 if (!insn_is_b
&& !insn_is_cond_bl
)
2213 return This::STATUS_BAD_RELOC
;
2215 else if (r_type
== elfcpp::R_ARM_PLT32
)
2217 if (!insn_is_any_branch
)
2218 return This::STATUS_BAD_RELOC
;
2220 else if (r_type
== elfcpp::R_ARM_XPC25
)
2222 // FIXME: AAELF document IH0044C does not say much about it other
2223 // than it being obsolete.
2224 if (!insn_is_any_branch
)
2225 return This::STATUS_BAD_RELOC
;
2230 // A branch to an undefined weak symbol is turned into a jump to
2231 // the next instruction unless a PLT entry will be created.
2232 // Do the same for local undefined symbols.
2233 // The jump to the next instruction is optimized as a NOP depending
2234 // on the architecture.
2235 const Target_arm
<big_endian
>* arm_target
=
2236 Target_arm
<big_endian
>::default_target();
2237 if (is_weakly_undefined_without_plt
)
2239 Valtype cond
= val
& 0xf0000000U
;
2240 if (arm_target
->may_use_arm_nop())
2241 val
= cond
| 0x0320f000;
2243 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2244 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2245 return This::STATUS_OKAY
;
2248 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2249 Valtype branch_target
= psymval
->value(object
, addend
);
2250 int32_t branch_offset
= branch_target
- address
;
2252 // We need a stub if the branch offset is too large or if we need
2254 bool may_use_blx
= arm_target
->may_use_blx();
2255 Reloc_stub
* stub
= NULL
;
2256 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2257 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2258 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2260 Stub_type stub_type
=
2261 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2263 if (stub_type
!= arm_stub_none
)
2265 Stub_table
<big_endian
>* stub_table
=
2266 object
->stub_table(relinfo
->data_shndx
);
2267 gold_assert(stub_table
!= NULL
);
2269 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2270 stub
= stub_table
->find_reloc_stub(stub_key
);
2271 gold_assert(stub
!= NULL
);
2272 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2273 branch_target
= stub_table
->address() + stub
->offset() + addend
;
2274 branch_offset
= branch_target
- address
;
2275 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2276 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2280 // At this point, if we still need to switch mode, the instruction
2281 // must either be a BLX or a BL that can be converted to a BLX.
2285 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2286 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2289 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2290 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2291 return (utils::has_overflow
<26>(branch_offset
)
2292 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2295 // Get the GOT section, creating it if necessary.
2297 template<bool big_endian
>
2298 Output_data_got
<32, big_endian
>*
2299 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2301 if (this->got_
== NULL
)
2303 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2305 this->got_
= new Output_data_got
<32, big_endian
>();
2308 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2310 | elfcpp::SHF_WRITE
),
2314 // The old GNU linker creates a .got.plt section. We just
2315 // create another set of data in the .got section. Note that we
2316 // always create a PLT if we create a GOT, although the PLT
2318 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2319 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2321 | elfcpp::SHF_WRITE
),
2322 this->got_plt_
, false);
2325 // The first three entries are reserved.
2326 this->got_plt_
->set_current_data_size(3 * 4);
2328 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2329 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2331 0, 0, elfcpp::STT_OBJECT
,
2333 elfcpp::STV_HIDDEN
, 0,
2339 // Get the dynamic reloc section, creating it if necessary.
2341 template<bool big_endian
>
2342 typename Target_arm
<big_endian
>::Reloc_section
*
2343 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2345 if (this->rel_dyn_
== NULL
)
2347 gold_assert(layout
!= NULL
);
2348 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2349 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2350 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true);
2352 return this->rel_dyn_
;
2355 // Insn_template methods.
2357 // Return byte size of an instruction template.
2360 Insn_template::size() const
2362 switch (this->type())
2375 // Return alignment of an instruction template.
2378 Insn_template::alignment() const
2380 switch (this->type())
2393 // Stub_template methods.
2395 Stub_template::Stub_template(
2396 Stub_type type
, const Insn_template
* insns
,
2398 : type_(type
), insns_(insns
), insn_count_(insn_count
), alignment_(1),
2399 entry_in_thumb_mode_(false), relocs_()
2403 // Compute byte size and alignment of stub template.
2404 for (size_t i
= 0; i
< insn_count
; i
++)
2406 unsigned insn_alignment
= insns
[i
].alignment();
2407 size_t insn_size
= insns
[i
].size();
2408 gold_assert((offset
& (insn_alignment
- 1)) == 0);
2409 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2410 switch (insns
[i
].type())
2412 case Insn_template::THUMB16_TYPE
:
2414 this->entry_in_thumb_mode_
= true;
2417 case Insn_template::THUMB32_TYPE
:
2418 if (insns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2419 this->relocs_
.push_back(Reloc(i
, offset
));
2421 this->entry_in_thumb_mode_
= true;
2424 case Insn_template::ARM_TYPE
:
2425 // Handle cases where the target is encoded within the
2427 if (insns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2428 this->relocs_
.push_back(Reloc(i
, offset
));
2431 case Insn_template::DATA_TYPE
:
2432 // Entry point cannot be data.
2433 gold_assert(i
!= 0);
2434 this->relocs_
.push_back(Reloc(i
, offset
));
2440 offset
+= insn_size
;
2442 this->size_
= offset
;
2445 // Reloc_stub::Key methods.
2447 // Dump a Key as a string for debugging.
2450 Reloc_stub::Key::name() const
2452 if (this->r_sym_
== invalid_index
)
2454 // Global symbol key name
2455 // <stub-type>:<symbol name>:<addend>.
2456 const std::string sym_name
= this->u_
.symbol
->name();
2457 // We need to print two hex number and two colons. So just add 100 bytes
2458 // to the symbol name size.
2459 size_t len
= sym_name
.size() + 100;
2460 char* buffer
= new char[len
];
2461 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2462 sym_name
.c_str(), this->addend_
);
2463 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2465 return std::string(buffer
);
2469 // local symbol key name
2470 // <stub-type>:<object>:<r_sym>:<addend>.
2471 const size_t len
= 200;
2473 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2474 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2475 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2476 return std::string(buffer
);
2480 // Reloc_stub methods.
2482 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2483 // LOCATION to DESTINATION.
2484 // This code is based on the arm_type_of_stub function in
2485 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2489 Reloc_stub::stub_type_for_reloc(
2490 unsigned int r_type
,
2491 Arm_address location
,
2492 Arm_address destination
,
2493 bool target_is_thumb
)
2495 Stub_type stub_type
= arm_stub_none
;
2497 // This is a bit ugly but we want to avoid using a templated class for
2498 // big and little endianities.
2500 bool should_force_pic_veneer
;
2503 if (parameters
->target().is_big_endian())
2505 const Target_arm
<true>* big_endian_target
=
2506 Target_arm
<true>::default_target();
2507 may_use_blx
= big_endian_target
->may_use_blx();
2508 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
2509 thumb2
= big_endian_target
->using_thumb2();
2510 thumb_only
= big_endian_target
->using_thumb_only();
2514 const Target_arm
<false>* little_endian_target
=
2515 Target_arm
<false>::default_target();
2516 may_use_blx
= little_endian_target
->may_use_blx();
2517 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
2518 thumb2
= little_endian_target
->using_thumb2();
2519 thumb_only
= little_endian_target
->using_thumb_only();
2522 int64_t branch_offset
= (int64_t)destination
- location
;
2524 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2526 // Handle cases where:
2527 // - this call goes too far (different Thumb/Thumb2 max
2529 // - it's a Thumb->Arm call and blx is not available, or it's a
2530 // Thumb->Arm branch (not bl). A stub is needed in this case.
2532 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2533 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2535 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2536 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2537 || ((!target_is_thumb
)
2538 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2539 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2541 if (target_is_thumb
)
2546 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2549 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2550 // V5T and above. Stub starts with ARM code, so
2551 // we must be able to switch mode before
2552 // reaching it, which is only possible for 'bl'
2553 // (ie R_ARM_THM_CALL relocation).
2554 ? arm_stub_long_branch_any_thumb_pic
2555 // On V4T, use Thumb code only.
2556 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2560 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2561 ? arm_stub_long_branch_any_any
// V5T and above.
2562 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2566 stub_type
= (parameters
->options().shared() | should_force_pic_veneer
)
2567 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2568 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2575 // FIXME: We should check that the input section is from an
2576 // object that has interwork enabled.
2578 stub_type
= (parameters
->options().shared()
2579 || should_force_pic_veneer
)
2582 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2583 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2584 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2588 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2589 ? arm_stub_long_branch_any_any
// V5T and above.
2590 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2592 // Handle v4t short branches.
2593 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2594 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2595 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2596 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2600 else if (r_type
== elfcpp::R_ARM_CALL
2601 || r_type
== elfcpp::R_ARM_JUMP24
2602 || r_type
== elfcpp::R_ARM_PLT32
)
2604 if (target_is_thumb
)
2608 // FIXME: We should check that the input section is from an
2609 // object that has interwork enabled.
2611 // We have an extra 2-bytes reach because of
2612 // the mode change (bit 24 (H) of BLX encoding).
2613 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2614 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2615 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2616 || (r_type
== elfcpp::R_ARM_JUMP24
)
2617 || (r_type
== elfcpp::R_ARM_PLT32
))
2619 stub_type
= (parameters
->options().shared()
2620 || should_force_pic_veneer
)
2623 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2624 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2628 ? arm_stub_long_branch_any_any
// V5T and above.
2629 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2635 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2636 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2638 stub_type
= (parameters
->options().shared()
2639 || should_force_pic_veneer
)
2640 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2641 : arm_stub_long_branch_any_any
; /// non-PIC.
2649 // Template to implement do_write for a specific target endianity.
2651 template<bool big_endian
>
2653 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2654 section_size_type view_size
)
2656 const Stub_template
* stub_template
= this->stub_template();
2657 const Insn_template
* insns
= stub_template
->insns();
2659 // FIXME: We do not handle BE8 encoding yet.
2660 unsigned char* pov
= view
;
2661 for (size_t i
= 0; i
< stub_template
->insn_count(); i
++)
2663 switch (insns
[i
].type())
2665 case Insn_template::THUMB16_TYPE
:
2666 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2667 gold_assert(insns
[i
].reloc_addend() == 0);
2668 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2670 case Insn_template::THUMB32_TYPE
:
2672 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2673 uint32_t lo
= insns
[i
].data() & 0xffff;
2674 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2675 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2678 case Insn_template::ARM_TYPE
:
2679 case Insn_template::DATA_TYPE
:
2680 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2685 pov
+= insns
[i
].size();
2687 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2690 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2693 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2697 this->do_fixed_endian_write
<true>(view
, view_size
);
2699 this->do_fixed_endian_write
<false>(view
, view_size
);
2702 // Stub_factory methods.
2704 Stub_factory::Stub_factory()
2706 // The instruction template sequences are declared as static
2707 // objects and initialized first time the constructor runs.
2709 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2710 // to reach the stub if necessary.
2711 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2713 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2714 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2715 // dcd R_ARM_ABS32(X)
2718 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2720 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
2722 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2723 Insn_template::arm_insn(0xe12fff1c), // bx ip
2724 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2725 // dcd R_ARM_ABS32(X)
2728 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2729 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
2731 Insn_template::thumb16_insn(0xb401), // push {r0}
2732 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2733 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2734 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2735 Insn_template::thumb16_insn(0x4760), // bx ip
2736 Insn_template::thumb16_insn(0xbf00), // nop
2737 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2738 // dcd R_ARM_ABS32(X)
2741 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2743 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
2745 Insn_template::thumb16_insn(0x4778), // bx pc
2746 Insn_template::thumb16_insn(0x46c0), // nop
2747 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2748 Insn_template::arm_insn(0xe12fff1c), // bx ip
2749 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2750 // dcd R_ARM_ABS32(X)
2753 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2755 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
2757 Insn_template::thumb16_insn(0x4778), // bx pc
2758 Insn_template::thumb16_insn(0x46c0), // nop
2759 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2760 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
2761 // dcd R_ARM_ABS32(X)
2764 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2765 // one, when the destination is close enough.
2766 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
2768 Insn_template::thumb16_insn(0x4778), // bx pc
2769 Insn_template::thumb16_insn(0x46c0), // nop
2770 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2773 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2774 // blx to reach the stub if necessary.
2775 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
2777 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2778 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2779 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2780 // dcd R_ARM_REL32(X-4)
2783 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2784 // blx to reach the stub if necessary. We can not add into pc;
2785 // it is not guaranteed to mode switch (different in ARMv6 and
2787 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
2789 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2790 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2791 Insn_template::arm_insn(0xe12fff1c), // bx ip
2792 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2793 // dcd R_ARM_REL32(X)
2796 // V4T ARM -> ARM long branch stub, PIC.
2797 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
2799 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2800 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2801 Insn_template::arm_insn(0xe12fff1c), // bx ip
2802 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2803 // dcd R_ARM_REL32(X)
2806 // V4T Thumb -> ARM long branch stub, PIC.
2807 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
2809 Insn_template::thumb16_insn(0x4778), // bx pc
2810 Insn_template::thumb16_insn(0x46c0), // nop
2811 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2812 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2813 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
2814 // dcd R_ARM_REL32(X)
2817 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2819 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
2821 Insn_template::thumb16_insn(0xb401), // push {r0}
2822 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2823 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2824 Insn_template::thumb16_insn(0x4484), // add ip, r0
2825 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2826 Insn_template::thumb16_insn(0x4760), // bx ip
2827 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
2828 // dcd R_ARM_REL32(X)
2831 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2833 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
2835 Insn_template::thumb16_insn(0x4778), // bx pc
2836 Insn_template::thumb16_insn(0x46c0), // nop
2837 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2838 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2839 Insn_template::arm_insn(0xe12fff1c), // bx ip
2840 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
2841 // dcd R_ARM_REL32(X)
2844 // Cortex-A8 erratum-workaround stubs.
2846 // Stub used for conditional branches (which may be beyond +/-1MB away,
2847 // so we can't use a conditional branch to reach this stub).
2854 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
2856 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2857 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2858 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2862 // Stub used for b.w and bl.w instructions.
2864 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
2866 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2869 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
2871 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2874 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2875 // instruction (which switches to ARM mode) to point to this stub. Jump to
2876 // the real destination using an ARM-mode branch.
2877 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
2879 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2882 // Fill in the stub template look-up table. Stub templates are constructed
2883 // per instance of Stub_factory for fast look-up without locking
2884 // in a thread-enabled environment.
2886 this->stub_templates_
[arm_stub_none
] =
2887 new Stub_template(arm_stub_none
, NULL
, 0);
2889 #define DEF_STUB(x) \
2893 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2894 Stub_type type = arm_stub_##x; \
2895 this->stub_templates_[type] = \
2896 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2904 // Stub_table methods.
2906 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2907 // if already a STUB with the same key has been added.
2909 template<bool big_endian
>
2911 Stub_table
<big_endian
>::add_reloc_stub(
2913 const Reloc_stub::Key
& key
)
2915 const Stub_template
* stub_template
= stub
->stub_template();
2916 gold_assert(stub_template
->type() == key
.stub_type());
2917 this->reloc_stubs_
[key
] = stub
;
2918 if (this->addralign_
< stub_template
->alignment())
2919 this->addralign_
= stub_template
->alignment();
2920 this->has_been_changed_
= true;
2923 template<bool big_endian
>
2925 Stub_table
<big_endian
>::relocate_stubs(
2926 const Relocate_info
<32, big_endian
>* relinfo
,
2927 Target_arm
<big_endian
>* arm_target
,
2928 Output_section
* output_section
,
2929 unsigned char* view
,
2930 Arm_address address
,
2931 section_size_type view_size
)
2933 // If we are passed a view bigger than the stub table's. we need to
2935 gold_assert(address
== this->address()
2937 == static_cast<section_size_type
>(this->data_size())));
2939 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2940 p
!= this->reloc_stubs_
.end();
2943 Reloc_stub
* stub
= p
->second
;
2944 const Stub_template
* stub_template
= stub
->stub_template();
2945 if (stub_template
->reloc_count() != 0)
2947 // Adjust view to cover the stub only.
2948 section_size_type offset
= stub
->offset();
2949 section_size_type stub_size
= stub_template
->size();
2950 gold_assert(offset
+ stub_size
<= view_size
);
2952 arm_target
->relocate_stub(stub
, relinfo
, output_section
,
2953 view
+ offset
, address
+ offset
,
2959 // Reset address and file offset.
2961 template<bool big_endian
>
2963 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
2966 uint64_t max_addralign
= 1;
2967 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2968 p
!= this->reloc_stubs_
.end();
2971 Reloc_stub
* stub
= p
->second
;
2972 const Stub_template
* stub_template
= stub
->stub_template();
2973 uint64_t stub_addralign
= stub_template
->alignment();
2974 max_addralign
= std::max(max_addralign
, stub_addralign
);
2975 off
= align_address(off
, stub_addralign
);
2976 stub
->set_offset(off
);
2977 stub
->reset_destination_address();
2978 off
+= stub_template
->size();
2981 this->addralign_
= max_addralign
;
2982 this->set_current_data_size_for_child(off
);
2985 // Write out the stubs to file.
2987 template<bool big_endian
>
2989 Stub_table
<big_endian
>::do_write(Output_file
* of
)
2991 off_t offset
= this->offset();
2992 const section_size_type oview_size
=
2993 convert_to_section_size_type(this->data_size());
2994 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2996 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
2997 p
!= this->reloc_stubs_
.end();
3000 Reloc_stub
* stub
= p
->second
;
3001 Arm_address address
= this->address() + stub
->offset();
3003 == align_address(address
,
3004 stub
->stub_template()->alignment()));
3005 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3008 of
->write_output_view(this->offset(), oview_size
, oview
);
3011 // Arm_input_section methods.
3013 // Initialize an Arm_input_section.
3015 template<bool big_endian
>
3017 Arm_input_section
<big_endian
>::init()
3019 Relobj
* relobj
= this->relobj();
3020 unsigned int shndx
= this->shndx();
3022 // Cache these to speed up size and alignment queries. It is too slow
3023 // to call section_addraglin and section_size every time.
3024 this->original_addralign_
= relobj
->section_addralign(shndx
);
3025 this->original_size_
= relobj
->section_size(shndx
);
3027 // We want to make this look like the original input section after
3028 // output sections are finalized.
3029 Output_section
* os
= relobj
->output_section(shndx
);
3030 off_t offset
= relobj
->output_section_offset(shndx
);
3031 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
3032 this->set_address(os
->address() + offset
);
3033 this->set_file_offset(os
->offset() + offset
);
3035 this->set_current_data_size(this->original_size_
);
3036 this->finalize_data_size();
3039 template<bool big_endian
>
3041 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3043 // We have to write out the original section content.
3044 section_size_type section_size
;
3045 const unsigned char* section_contents
=
3046 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3047 of
->write(this->offset(), section_contents
, section_size
);
3049 // If this owns a stub table and it is not empty, write it.
3050 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3051 this->stub_table_
->write(of
);
3054 // Finalize data size.
3056 template<bool big_endian
>
3058 Arm_input_section
<big_endian
>::set_final_data_size()
3060 // If this owns a stub table, finalize its data size as well.
3061 if (this->is_stub_table_owner())
3063 uint64_t address
= this->address();
3065 // The stub table comes after the original section contents.
3066 address
+= this->original_size_
;
3067 address
= align_address(address
, this->stub_table_
->addralign());
3068 off_t offset
= this->offset() + (address
- this->address());
3069 this->stub_table_
->set_address_and_file_offset(address
, offset
);
3070 address
+= this->stub_table_
->data_size();
3071 gold_assert(address
== this->address() + this->current_data_size());
3074 this->set_data_size(this->current_data_size());
3077 // Reset address and file offset.
3079 template<bool big_endian
>
3081 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3083 // Size of the original input section contents.
3084 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3086 // If this is a stub table owner, account for the stub table size.
3087 if (this->is_stub_table_owner())
3089 Stub_table
<big_endian
>* stub_table
= this->stub_table_
;
3091 // Reset the stub table's address and file offset. The
3092 // current data size for child will be updated after that.
3093 stub_table_
->reset_address_and_file_offset();
3094 off
= align_address(off
, stub_table_
->addralign());
3095 off
+= stub_table
->current_data_size();
3098 this->set_current_data_size(off
);
3101 // Arm_output_section methods.
3103 // Create a stub group for input sections from BEGIN to END. OWNER
3104 // points to the input section to be the owner a new stub table.
3106 template<bool big_endian
>
3108 Arm_output_section
<big_endian
>::create_stub_group(
3109 Input_section_list::const_iterator begin
,
3110 Input_section_list::const_iterator end
,
3111 Input_section_list::const_iterator owner
,
3112 Target_arm
<big_endian
>* target
,
3113 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3115 // Currently we convert ordinary input sections into relaxed sections only
3116 // at this point but we may want to support creating relaxed input section
3117 // very early. So we check here to see if owner is already a relaxed
3120 Arm_input_section
<big_endian
>* arm_input_section
;
3121 if (owner
->is_relaxed_input_section())
3124 Arm_input_section
<big_endian
>::as_arm_input_section(
3125 owner
->relaxed_input_section());
3129 gold_assert(owner
->is_input_section());
3130 // Create a new relaxed input section.
3132 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3133 new_relaxed_sections
->push_back(arm_input_section
);
3136 // Create a stub table.
3137 Stub_table
<big_endian
>* stub_table
=
3138 target
->new_stub_table(arm_input_section
);
3140 arm_input_section
->set_stub_table(stub_table
);
3142 Input_section_list::const_iterator p
= begin
;
3143 Input_section_list::const_iterator prev_p
;
3145 // Look for input sections or relaxed input sections in [begin ... end].
3148 if (p
->is_input_section() || p
->is_relaxed_input_section())
3150 // The stub table information for input sections live
3151 // in their objects.
3152 Arm_relobj
<big_endian
>* arm_relobj
=
3153 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3154 arm_relobj
->set_stub_table(p
->shndx(), stub_table
);
3158 while (prev_p
!= end
);
3161 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3162 // of stub groups. We grow a stub group by adding input section until the
3163 // size is just below GROUP_SIZE. The last input section will be converted
3164 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3165 // input section after the stub table, effectively double the group size.
3167 // This is similar to the group_sections() function in elf32-arm.c but is
3168 // implemented differently.
3170 template<bool big_endian
>
3172 Arm_output_section
<big_endian
>::group_sections(
3173 section_size_type group_size
,
3174 bool stubs_always_after_branch
,
3175 Target_arm
<big_endian
>* target
)
3177 // We only care about sections containing code.
3178 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3181 // States for grouping.
3184 // No group is being built.
3186 // A group is being built but the stub table is not found yet.
3187 // We keep group a stub group until the size is just under GROUP_SIZE.
3188 // The last input section in the group will be used as the stub table.
3189 FINDING_STUB_SECTION
,
3190 // A group is being built and we have already found a stub table.
3191 // We enter this state to grow a stub group by adding input section
3192 // after the stub table. This effectively doubles the group size.
3196 // Any newly created relaxed sections are stored here.
3197 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3199 State state
= NO_GROUP
;
3200 section_size_type off
= 0;
3201 section_size_type group_begin_offset
= 0;
3202 section_size_type group_end_offset
= 0;
3203 section_size_type stub_table_end_offset
= 0;
3204 Input_section_list::const_iterator group_begin
=
3205 this->input_sections().end();
3206 Input_section_list::const_iterator stub_table
=
3207 this->input_sections().end();
3208 Input_section_list::const_iterator group_end
= this->input_sections().end();
3209 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3210 p
!= this->input_sections().end();
3213 section_size_type section_begin_offset
=
3214 align_address(off
, p
->addralign());
3215 section_size_type section_end_offset
=
3216 section_begin_offset
+ p
->data_size();
3218 // Check to see if we should group the previously seens sections.
3224 case FINDING_STUB_SECTION
:
3225 // Adding this section makes the group larger than GROUP_SIZE.
3226 if (section_end_offset
- group_begin_offset
>= group_size
)
3228 if (stubs_always_after_branch
)
3230 gold_assert(group_end
!= this->input_sections().end());
3231 this->create_stub_group(group_begin
, group_end
, group_end
,
3232 target
, &new_relaxed_sections
);
3237 // But wait, there's more! Input sections up to
3238 // stub_group_size bytes after the stub table can be
3239 // handled by it too.
3240 state
= HAS_STUB_SECTION
;
3241 stub_table
= group_end
;
3242 stub_table_end_offset
= group_end_offset
;
3247 case HAS_STUB_SECTION
:
3248 // Adding this section makes the post stub-section group larger
3250 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3252 gold_assert(group_end
!= this->input_sections().end());
3253 this->create_stub_group(group_begin
, group_end
, stub_table
,
3254 target
, &new_relaxed_sections
);
3263 // If we see an input section and currently there is no group, start
3264 // a new one. Skip any empty sections.
3265 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3266 && (p
->relobj()->section_size(p
->shndx()) != 0))
3268 if (state
== NO_GROUP
)
3270 state
= FINDING_STUB_SECTION
;
3272 group_begin_offset
= section_begin_offset
;
3275 // Keep track of the last input section seen.
3277 group_end_offset
= section_end_offset
;
3280 off
= section_end_offset
;
3283 // Create a stub group for any ungrouped sections.
3284 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3286 gold_assert(group_end
!= this->input_sections().end());
3287 this->create_stub_group(group_begin
, group_end
,
3288 (state
== FINDING_STUB_SECTION
3291 target
, &new_relaxed_sections
);
3294 // Convert input section into relaxed input section in a batch.
3295 if (!new_relaxed_sections
.empty())
3296 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3298 // Update the section offsets
3299 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3301 Arm_relobj
<big_endian
>* arm_relobj
=
3302 Arm_relobj
<big_endian
>::as_arm_relobj(
3303 new_relaxed_sections
[i
]->relobj());
3304 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
3305 // Tell Arm_relobj that this input section is converted.
3306 arm_relobj
->convert_input_section_to_relaxed_section(shndx
);
3310 // Arm_relobj methods.
3312 // Scan relocations for stub generation.
3314 template<bool big_endian
>
3316 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3317 Target_arm
<big_endian
>* arm_target
,
3318 const Symbol_table
* symtab
,
3319 const Layout
* layout
)
3321 unsigned int shnum
= this->shnum();
3322 const unsigned int shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3324 // Read the section headers.
3325 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3329 // To speed up processing, we set up hash tables for fast lookup of
3330 // input offsets to output addresses.
3331 this->initialize_input_to_output_maps();
3333 const Relobj::Output_sections
& out_sections(this->output_sections());
3335 Relocate_info
<32, big_endian
> relinfo
;
3336 relinfo
.symtab
= symtab
;
3337 relinfo
.layout
= layout
;
3338 relinfo
.object
= this;
3340 const unsigned char* p
= pshdrs
+ shdr_size
;
3341 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
3343 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3345 unsigned int sh_type
= shdr
.get_sh_type();
3346 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3349 off_t sh_size
= shdr
.get_sh_size();
3353 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3354 if (index
>= this->shnum())
3356 // Ignore reloc section with bad info. This error will be
3357 // reported in the final link.
3361 Output_section
* os
= out_sections
[index
];
3364 // This relocation section is against a section which we
3368 Arm_address output_offset
= this->get_output_section_offset(index
);
3370 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
3372 // Ignore reloc section with unexpected symbol table. The
3373 // error will be reported in the final link.
3377 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
3378 sh_size
, true, false);
3380 unsigned int reloc_size
;
3381 if (sh_type
== elfcpp::SHT_REL
)
3382 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3384 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
3386 if (reloc_size
!= shdr
.get_sh_entsize())
3388 // Ignore reloc section with unexpected entsize. The error
3389 // will be reported in the final link.
3393 size_t reloc_count
= sh_size
/ reloc_size
;
3394 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
3396 // Ignore reloc section with uneven size. The error will be
3397 // reported in the final link.
3401 gold_assert(output_offset
!= invalid_address
3402 || this->relocs_must_follow_section_writes());
3404 // Get the section contents. This does work for the case in which
3405 // we modify the contents of an input section. We need to pass the
3406 // output view under such circumstances.
3407 section_size_type input_view_size
= 0;
3408 const unsigned char* input_view
=
3409 this->section_contents(index
, &input_view_size
, false);
3411 relinfo
.reloc_shndx
= i
;
3412 relinfo
.data_shndx
= index
;
3413 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
3415 output_offset
== invalid_address
,
3421 // After we've done the relocations, we release the hash tables,
3422 // since we no longer need them.
3423 this->free_input_to_output_maps();
3426 // Count the local symbols. The ARM backend needs to know if a symbol
3427 // is a THUMB function or not. For global symbols, it is easy because
3428 // the Symbol object keeps the ELF symbol type. For local symbol it is
3429 // harder because we cannot access this information. So we override the
3430 // do_count_local_symbol in parent and scan local symbols to mark
3431 // THUMB functions. This is not the most efficient way but I do not want to
3432 // slow down other ports by calling a per symbol targer hook inside
3433 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3435 template<bool big_endian
>
3437 Arm_relobj
<big_endian
>::do_count_local_symbols(
3438 Stringpool_template
<char>* pool
,
3439 Stringpool_template
<char>* dynpool
)
3441 // We need to fix-up the values of any local symbols whose type are
3444 // Ask parent to count the local symbols.
3445 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
3446 const unsigned int loccount
= this->local_symbol_count();
3450 // Intialize the thumb function bit-vector.
3451 std::vector
<bool> empty_vector(loccount
, false);
3452 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
3454 // Read the symbol table section header.
3455 const unsigned int symtab_shndx
= this->symtab_shndx();
3456 elfcpp::Shdr
<32, big_endian
>
3457 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
3458 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
3460 // Read the local symbols.
3461 const int sym_size
=elfcpp::Elf_sizes
<32>::sym_size
;
3462 gold_assert(loccount
== symtabshdr
.get_sh_info());
3463 off_t locsize
= loccount
* sym_size
;
3464 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
3465 locsize
, true, true);
3467 // Loop over the local symbols and mark any local symbols pointing
3468 // to THUMB functions.
3470 // Skip the first dummy symbol.
3472 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
3473 this->local_values();
3474 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
3476 elfcpp::Sym
<32, big_endian
> sym(psyms
);
3477 elfcpp::STT st_type
= sym
.get_st_type();
3478 Symbol_value
<32>& lv((*plocal_values
)[i
]);
3479 Arm_address input_value
= lv
.input_value();
3481 if (st_type
== elfcpp::STT_ARM_TFUNC
3482 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
3484 // This is a THUMB function. Mark this and canonicalize the
3485 // symbol value by setting LSB.
3486 this->local_symbol_is_thumb_function_
[i
] = true;
3487 if ((input_value
& 1) == 0)
3488 lv
.set_input_value(input_value
| 1);
3493 // Relocate sections.
3494 template<bool big_endian
>
3496 Arm_relobj
<big_endian
>::do_relocate_sections(
3497 const Symbol_table
* symtab
,
3498 const Layout
* layout
,
3499 const unsigned char* pshdrs
,
3500 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
3502 // Call parent to relocate sections.
3503 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, layout
, pshdrs
,
3506 // We do not generate stubs if doing a relocatable link.
3507 if (parameters
->options().relocatable())
3510 // Relocate stub tables.
3511 unsigned int shnum
= this->shnum();
3513 Target_arm
<big_endian
>* arm_target
=
3514 Target_arm
<big_endian
>::default_target();
3516 Relocate_info
<32, big_endian
> relinfo
;
3517 relinfo
.symtab
= symtab
;
3518 relinfo
.layout
= layout
;
3519 relinfo
.object
= this;
3521 for (unsigned int i
= 1; i
< shnum
; ++i
)
3523 Arm_input_section
<big_endian
>* arm_input_section
=
3524 arm_target
->find_arm_input_section(this, i
);
3526 if (arm_input_section
== NULL
3527 || !arm_input_section
->is_stub_table_owner()
3528 || arm_input_section
->stub_table()->empty())
3531 // We cannot discard a section if it owns a stub table.
3532 Output_section
* os
= this->output_section(i
);
3533 gold_assert(os
!= NULL
);
3535 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
3536 relinfo
.reloc_shdr
= NULL
;
3537 relinfo
.data_shndx
= i
;
3538 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
3540 gold_assert((*pviews
)[i
].view
!= NULL
);
3542 // We are passed the output section view. Adjust it to cover the
3544 Stub_table
<big_endian
>* stub_table
= arm_input_section
->stub_table();
3545 gold_assert((stub_table
->address() >= (*pviews
)[i
].address
)
3546 && ((stub_table
->address() + stub_table
->data_size())
3547 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
3549 off_t offset
= stub_table
->address() - (*pviews
)[i
].address
;
3550 unsigned char* view
= (*pviews
)[i
].view
+ offset
;
3551 Arm_address address
= stub_table
->address();
3552 section_size_type view_size
= stub_table
->data_size();
3554 stub_table
->relocate_stubs(&relinfo
, arm_target
, os
, view
, address
,
3559 // Read the symbol information.
3561 template<bool big_endian
>
3563 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3565 // Call parent class to read symbol information.
3566 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
3568 // Read processor-specific flags in ELF file header.
3569 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3570 elfcpp::Elf_sizes
<32>::ehdr_size
,
3572 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3573 this->processor_specific_flags_
= ehdr
.get_e_flags();
3576 // Arm_dynobj methods.
3578 // Read the symbol information.
3580 template<bool big_endian
>
3582 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3584 // Call parent class to read symbol information.
3585 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
3587 // Read processor-specific flags in ELF file header.
3588 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3589 elfcpp::Elf_sizes
<32>::ehdr_size
,
3591 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3592 this->processor_specific_flags_
= ehdr
.get_e_flags();
3595 // Stub_addend_reader methods.
3597 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3599 template<bool big_endian
>
3600 elfcpp::Elf_types
<32>::Elf_Swxword
3601 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
3602 unsigned int r_type
,
3603 const unsigned char* view
,
3604 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
3608 case elfcpp::R_ARM_CALL
:
3609 case elfcpp::R_ARM_JUMP24
:
3610 case elfcpp::R_ARM_PLT32
:
3612 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3613 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3614 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3615 return utils::sign_extend
<26>(val
<< 2);
3618 case elfcpp::R_ARM_THM_CALL
:
3619 case elfcpp::R_ARM_THM_JUMP24
:
3620 case elfcpp::R_ARM_THM_XPC22
:
3622 // Fetch the addend. We use the Thumb-2 encoding (backwards
3623 // compatible with Thumb-1) involving the J1 and J2 bits.
3624 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3625 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3626 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3627 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3629 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
3630 uint32_t upper
= upper_insn
& 0x3ff;
3631 uint32_t lower
= lower_insn
& 0x7ff;
3632 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
3633 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
3634 uint32_t i1
= j1
^ s
? 0 : 1;
3635 uint32_t i2
= j2
^ s
? 0 : 1;
3637 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3638 | (upper
<< 12) | (lower
<< 1));
3641 case elfcpp::R_ARM_THM_JUMP19
:
3643 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3644 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3645 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3646 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3648 // Reconstruct the top three bits and squish the two 11 bit pieces
3650 uint32_t S
= (upper_insn
& 0x0400) >> 10;
3651 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
3652 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
3654 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
3655 uint32_t lower
= (lower_insn
& 0x07ff);
3656 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
3664 // A class to handle the PLT data.
3666 template<bool big_endian
>
3667 class Output_data_plt_arm
: public Output_section_data
3670 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
3673 Output_data_plt_arm(Layout
*, Output_data_space
*);
3675 // Add an entry to the PLT.
3677 add_entry(Symbol
* gsym
);
3679 // Return the .rel.plt section data.
3680 const Reloc_section
*
3682 { return this->rel_
; }
3686 do_adjust_output_section(Output_section
* os
);
3688 // Write to a map file.
3690 do_print_to_mapfile(Mapfile
* mapfile
) const
3691 { mapfile
->print_output_data(this, _("** PLT")); }
3694 // Template for the first PLT entry.
3695 static const uint32_t first_plt_entry
[5];
3697 // Template for subsequent PLT entries.
3698 static const uint32_t plt_entry
[3];
3700 // Set the final size.
3702 set_final_data_size()
3704 this->set_data_size(sizeof(first_plt_entry
)
3705 + this->count_
* sizeof(plt_entry
));
3708 // Write out the PLT data.
3710 do_write(Output_file
*);
3712 // The reloc section.
3713 Reloc_section
* rel_
;
3714 // The .got.plt section.
3715 Output_data_space
* got_plt_
;
3716 // The number of PLT entries.
3717 unsigned int count_
;
3720 // Create the PLT section. The ordinary .got section is an argument,
3721 // since we need to refer to the start. We also create our own .got
3722 // section just for PLT entries.
3724 template<bool big_endian
>
3725 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
3726 Output_data_space
* got_plt
)
3727 : Output_section_data(4), got_plt_(got_plt
), count_(0)
3729 this->rel_
= new Reloc_section(false);
3730 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
3731 elfcpp::SHF_ALLOC
, this->rel_
, true);
3734 template<bool big_endian
>
3736 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
3741 // Add an entry to the PLT.
3743 template<bool big_endian
>
3745 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
3747 gold_assert(!gsym
->has_plt_offset());
3749 // Note that when setting the PLT offset we skip the initial
3750 // reserved PLT entry.
3751 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
3752 + sizeof(first_plt_entry
));
3756 section_offset_type got_offset
= this->got_plt_
->current_data_size();
3758 // Every PLT entry needs a GOT entry which points back to the PLT
3759 // entry (this will be changed by the dynamic linker, normally
3760 // lazily when the function is called).
3761 this->got_plt_
->set_current_data_size(got_offset
+ 4);
3763 // Every PLT entry needs a reloc.
3764 gsym
->set_needs_dynsym_entry();
3765 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
3768 // Note that we don't need to save the symbol. The contents of the
3769 // PLT are independent of which symbols are used. The symbols only
3770 // appear in the relocations.
3774 // FIXME: This is not very flexible. Right now this has only been tested
3775 // on armv5te. If we are to support additional architecture features like
3776 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3778 // The first entry in the PLT.
3779 template<bool big_endian
>
3780 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
3782 0xe52de004, // str lr, [sp, #-4]!
3783 0xe59fe004, // ldr lr, [pc, #4]
3784 0xe08fe00e, // add lr, pc, lr
3785 0xe5bef008, // ldr pc, [lr, #8]!
3786 0x00000000, // &GOT[0] - .
3789 // Subsequent entries in the PLT.
3791 template<bool big_endian
>
3792 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
3794 0xe28fc600, // add ip, pc, #0xNN00000
3795 0xe28cca00, // add ip, ip, #0xNN000
3796 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3799 // Write out the PLT. This uses the hand-coded instructions above,
3800 // and adjusts them as needed. This is all specified by the arm ELF
3801 // Processor Supplement.
3803 template<bool big_endian
>
3805 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
3807 const off_t offset
= this->offset();
3808 const section_size_type oview_size
=
3809 convert_to_section_size_type(this->data_size());
3810 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3812 const off_t got_file_offset
= this->got_plt_
->offset();
3813 const section_size_type got_size
=
3814 convert_to_section_size_type(this->got_plt_
->data_size());
3815 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
3817 unsigned char* pov
= oview
;
3819 Arm_address plt_address
= this->address();
3820 Arm_address got_address
= this->got_plt_
->address();
3822 // Write first PLT entry. All but the last word are constants.
3823 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
3824 / sizeof(plt_entry
[0]));
3825 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
3826 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
3827 // Last word in first PLT entry is &GOT[0] - .
3828 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
3829 got_address
- (plt_address
+ 16));
3830 pov
+= sizeof(first_plt_entry
);
3832 unsigned char* got_pov
= got_view
;
3834 memset(got_pov
, 0, 12);
3837 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3838 unsigned int plt_offset
= sizeof(first_plt_entry
);
3839 unsigned int plt_rel_offset
= 0;
3840 unsigned int got_offset
= 12;
3841 const unsigned int count
= this->count_
;
3842 for (unsigned int i
= 0;
3845 pov
+= sizeof(plt_entry
),
3847 plt_offset
+= sizeof(plt_entry
),
3848 plt_rel_offset
+= rel_size
,
3851 // Set and adjust the PLT entry itself.
3852 int32_t offset
= ((got_address
+ got_offset
)
3853 - (plt_address
+ plt_offset
+ 8));
3855 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
3856 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
3857 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
3858 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
3859 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
3860 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
3861 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
3863 // Set the entry in the GOT.
3864 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
3867 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
3868 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
3870 of
->write_output_view(offset
, oview_size
, oview
);
3871 of
->write_output_view(got_file_offset
, got_size
, got_view
);
3874 // Create a PLT entry for a global symbol.
3876 template<bool big_endian
>
3878 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
3881 if (gsym
->has_plt_offset())
3884 if (this->plt_
== NULL
)
3886 // Create the GOT sections first.
3887 this->got_section(symtab
, layout
);
3889 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
3890 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
3892 | elfcpp::SHF_EXECINSTR
),
3895 this->plt_
->add_entry(gsym
);
3898 // Report an unsupported relocation against a local symbol.
3900 template<bool big_endian
>
3902 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
3903 Sized_relobj
<32, big_endian
>* object
,
3904 unsigned int r_type
)
3906 gold_error(_("%s: unsupported reloc %u against local symbol"),
3907 object
->name().c_str(), r_type
);
3910 // We are about to emit a dynamic relocation of type R_TYPE. If the
3911 // dynamic linker does not support it, issue an error. The GNU linker
3912 // only issues a non-PIC error for an allocated read-only section.
3913 // Here we know the section is allocated, but we don't know that it is
3914 // read-only. But we check for all the relocation types which the
3915 // glibc dynamic linker supports, so it seems appropriate to issue an
3916 // error even if the section is not read-only.
3918 template<bool big_endian
>
3920 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
3921 unsigned int r_type
)
3925 // These are the relocation types supported by glibc for ARM.
3926 case elfcpp::R_ARM_RELATIVE
:
3927 case elfcpp::R_ARM_COPY
:
3928 case elfcpp::R_ARM_GLOB_DAT
:
3929 case elfcpp::R_ARM_JUMP_SLOT
:
3930 case elfcpp::R_ARM_ABS32
:
3931 case elfcpp::R_ARM_ABS32_NOI
:
3932 case elfcpp::R_ARM_PC24
:
3933 // FIXME: The following 3 types are not supported by Android's dynamic
3935 case elfcpp::R_ARM_TLS_DTPMOD32
:
3936 case elfcpp::R_ARM_TLS_DTPOFF32
:
3937 case elfcpp::R_ARM_TLS_TPOFF32
:
3941 // This prevents us from issuing more than one error per reloc
3942 // section. But we can still wind up issuing more than one
3943 // error per object file.
3944 if (this->issued_non_pic_error_
)
3946 object
->error(_("requires unsupported dynamic reloc; "
3947 "recompile with -fPIC"));
3948 this->issued_non_pic_error_
= true;
3951 case elfcpp::R_ARM_NONE
:
3956 // Scan a relocation for a local symbol.
3957 // FIXME: This only handles a subset of relocation types used by Android
3958 // on ARM v5te devices.
3960 template<bool big_endian
>
3962 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
3965 Sized_relobj
<32, big_endian
>* object
,
3966 unsigned int data_shndx
,
3967 Output_section
* output_section
,
3968 const elfcpp::Rel
<32, big_endian
>& reloc
,
3969 unsigned int r_type
,
3970 const elfcpp::Sym
<32, big_endian
>&)
3972 r_type
= get_real_reloc_type(r_type
);
3975 case elfcpp::R_ARM_NONE
:
3978 case elfcpp::R_ARM_ABS32
:
3979 case elfcpp::R_ARM_ABS32_NOI
:
3980 // If building a shared library (or a position-independent
3981 // executable), we need to create a dynamic relocation for
3982 // this location. The relocation applied at link time will
3983 // apply the link-time value, so we flag the location with
3984 // an R_ARM_RELATIVE relocation so the dynamic loader can
3985 // relocate it easily.
3986 if (parameters
->options().output_is_position_independent())
3988 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
3989 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
3990 // If we are to add more other reloc types than R_ARM_ABS32,
3991 // we need to add check_non_pic(object, r_type) here.
3992 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
3993 output_section
, data_shndx
,
3994 reloc
.get_r_offset());
3998 case elfcpp::R_ARM_REL32
:
3999 case elfcpp::R_ARM_THM_CALL
:
4000 case elfcpp::R_ARM_CALL
:
4001 case elfcpp::R_ARM_PREL31
:
4002 case elfcpp::R_ARM_JUMP24
:
4003 case elfcpp::R_ARM_PLT32
:
4004 case elfcpp::R_ARM_THM_ABS5
:
4005 case elfcpp::R_ARM_ABS8
:
4006 case elfcpp::R_ARM_ABS12
:
4007 case elfcpp::R_ARM_ABS16
:
4008 case elfcpp::R_ARM_BASE_ABS
:
4009 case elfcpp::R_ARM_MOVW_ABS_NC
:
4010 case elfcpp::R_ARM_MOVT_ABS
:
4011 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4012 case elfcpp::R_ARM_THM_MOVT_ABS
:
4013 case elfcpp::R_ARM_MOVW_PREL_NC
:
4014 case elfcpp::R_ARM_MOVT_PREL
:
4015 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4016 case elfcpp::R_ARM_THM_MOVT_PREL
:
4019 case elfcpp::R_ARM_GOTOFF32
:
4020 // We need a GOT section:
4021 target
->got_section(symtab
, layout
);
4024 case elfcpp::R_ARM_BASE_PREL
:
4025 // FIXME: What about this?
4028 case elfcpp::R_ARM_GOT_BREL
:
4029 case elfcpp::R_ARM_GOT_PREL
:
4031 // The symbol requires a GOT entry.
4032 Output_data_got
<32, big_endian
>* got
=
4033 target
->got_section(symtab
, layout
);
4034 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4035 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4037 // If we are generating a shared object, we need to add a
4038 // dynamic RELATIVE relocation for this symbol's GOT entry.
4039 if (parameters
->options().output_is_position_independent())
4041 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4042 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4043 rel_dyn
->add_local_relative(
4044 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
4045 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4051 case elfcpp::R_ARM_TARGET1
:
4052 // This should have been mapped to another type already.
4054 case elfcpp::R_ARM_COPY
:
4055 case elfcpp::R_ARM_GLOB_DAT
:
4056 case elfcpp::R_ARM_JUMP_SLOT
:
4057 case elfcpp::R_ARM_RELATIVE
:
4058 // These are relocations which should only be seen by the
4059 // dynamic linker, and should never be seen here.
4060 gold_error(_("%s: unexpected reloc %u in object file"),
4061 object
->name().c_str(), r_type
);
4065 unsupported_reloc_local(object
, r_type
);
4070 // Report an unsupported relocation against a global symbol.
4072 template<bool big_endian
>
4074 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4075 Sized_relobj
<32, big_endian
>* object
,
4076 unsigned int r_type
,
4079 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4080 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4083 // Scan a relocation for a global symbol.
4084 // FIXME: This only handles a subset of relocation types used by Android
4085 // on ARM v5te devices.
4087 template<bool big_endian
>
4089 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4092 Sized_relobj
<32, big_endian
>* object
,
4093 unsigned int data_shndx
,
4094 Output_section
* output_section
,
4095 const elfcpp::Rel
<32, big_endian
>& reloc
,
4096 unsigned int r_type
,
4099 r_type
= get_real_reloc_type(r_type
);
4102 case elfcpp::R_ARM_NONE
:
4105 case elfcpp::R_ARM_ABS32
:
4106 case elfcpp::R_ARM_ABS32_NOI
:
4108 // Make a dynamic relocation if necessary.
4109 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4111 if (target
->may_need_copy_reloc(gsym
))
4113 target
->copy_reloc(symtab
, layout
, object
,
4114 data_shndx
, output_section
, gsym
, reloc
);
4116 else if (gsym
->can_use_relative_reloc(false))
4118 // If we are to add more other reloc types than R_ARM_ABS32,
4119 // we need to add check_non_pic(object, r_type) here.
4120 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4121 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4122 output_section
, object
,
4123 data_shndx
, reloc
.get_r_offset());
4127 // If we are to add more other reloc types than R_ARM_ABS32,
4128 // we need to add check_non_pic(object, r_type) here.
4129 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4130 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4131 data_shndx
, reloc
.get_r_offset());
4137 case elfcpp::R_ARM_MOVW_ABS_NC
:
4138 case elfcpp::R_ARM_MOVT_ABS
:
4139 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4140 case elfcpp::R_ARM_THM_MOVT_ABS
:
4141 case elfcpp::R_ARM_MOVW_PREL_NC
:
4142 case elfcpp::R_ARM_MOVT_PREL
:
4143 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4144 case elfcpp::R_ARM_THM_MOVT_PREL
:
4147 case elfcpp::R_ARM_THM_ABS5
:
4148 case elfcpp::R_ARM_ABS8
:
4149 case elfcpp::R_ARM_ABS12
:
4150 case elfcpp::R_ARM_ABS16
:
4151 case elfcpp::R_ARM_BASE_ABS
:
4153 // No dynamic relocs of this kinds.
4154 // Report the error in case of PIC.
4155 int flags
= Symbol::NON_PIC_REF
;
4156 if (gsym
->type() == elfcpp::STT_FUNC
4157 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4158 flags
|= Symbol::FUNCTION_CALL
;
4159 if (gsym
->needs_dynamic_reloc(flags
))
4160 check_non_pic(object
, r_type
);
4164 case elfcpp::R_ARM_REL32
:
4165 case elfcpp::R_ARM_PREL31
:
4167 // Make a dynamic relocation if necessary.
4168 int flags
= Symbol::NON_PIC_REF
;
4169 if (gsym
->needs_dynamic_reloc(flags
))
4171 if (target
->may_need_copy_reloc(gsym
))
4173 target
->copy_reloc(symtab
, layout
, object
,
4174 data_shndx
, output_section
, gsym
, reloc
);
4178 check_non_pic(object
, r_type
);
4179 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4180 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4181 data_shndx
, reloc
.get_r_offset());
4187 case elfcpp::R_ARM_JUMP24
:
4188 case elfcpp::R_ARM_THM_CALL
:
4189 case elfcpp::R_ARM_CALL
:
4191 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4192 target
->make_plt_entry(symtab
, layout
, gsym
);
4193 // Make a dynamic relocation if necessary.
4194 int flags
= Symbol::NON_PIC_REF
;
4195 if (gsym
->type() == elfcpp::STT_FUNC
4196 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4197 flags
|= Symbol::FUNCTION_CALL
;
4198 if (gsym
->needs_dynamic_reloc(flags
))
4200 if (target
->may_need_copy_reloc(gsym
))
4202 target
->copy_reloc(symtab
, layout
, object
,
4203 data_shndx
, output_section
, gsym
,
4208 check_non_pic(object
, r_type
);
4209 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4210 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4211 data_shndx
, reloc
.get_r_offset());
4217 case elfcpp::R_ARM_PLT32
:
4218 // If the symbol is fully resolved, this is just a relative
4219 // local reloc. Otherwise we need a PLT entry.
4220 if (gsym
->final_value_is_known())
4222 // If building a shared library, we can also skip the PLT entry
4223 // if the symbol is defined in the output file and is protected
4225 if (gsym
->is_defined()
4226 && !gsym
->is_from_dynobj()
4227 && !gsym
->is_preemptible())
4229 target
->make_plt_entry(symtab
, layout
, gsym
);
4232 case elfcpp::R_ARM_GOTOFF32
:
4233 // We need a GOT section.
4234 target
->got_section(symtab
, layout
);
4237 case elfcpp::R_ARM_BASE_PREL
:
4238 // FIXME: What about this?
4241 case elfcpp::R_ARM_GOT_BREL
:
4242 case elfcpp::R_ARM_GOT_PREL
:
4244 // The symbol requires a GOT entry.
4245 Output_data_got
<32, big_endian
>* got
=
4246 target
->got_section(symtab
, layout
);
4247 if (gsym
->final_value_is_known())
4248 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4251 // If this symbol is not fully resolved, we need to add a
4252 // GOT entry with a dynamic relocation.
4253 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
4254 if (gsym
->is_from_dynobj()
4255 || gsym
->is_undefined()
4256 || gsym
->is_preemptible())
4257 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4258 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4261 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4262 rel_dyn
->add_global_relative(
4263 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4264 gsym
->got_offset(GOT_TYPE_STANDARD
));
4270 case elfcpp::R_ARM_TARGET1
:
4271 // This should have been mapped to another type already.
4273 case elfcpp::R_ARM_COPY
:
4274 case elfcpp::R_ARM_GLOB_DAT
:
4275 case elfcpp::R_ARM_JUMP_SLOT
:
4276 case elfcpp::R_ARM_RELATIVE
:
4277 // These are relocations which should only be seen by the
4278 // dynamic linker, and should never be seen here.
4279 gold_error(_("%s: unexpected reloc %u in object file"),
4280 object
->name().c_str(), r_type
);
4284 unsupported_reloc_global(object
, r_type
, gsym
);
4289 // Process relocations for gc.
4291 template<bool big_endian
>
4293 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4295 Sized_relobj
<32, big_endian
>* object
,
4296 unsigned int data_shndx
,
4298 const unsigned char* prelocs
,
4300 Output_section
* output_section
,
4301 bool needs_special_offset_handling
,
4302 size_t local_symbol_count
,
4303 const unsigned char* plocal_symbols
)
4305 typedef Target_arm
<big_endian
> Arm
;
4306 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4308 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
4317 needs_special_offset_handling
,
4322 // Scan relocations for a section.
4324 template<bool big_endian
>
4326 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4328 Sized_relobj
<32, big_endian
>* object
,
4329 unsigned int data_shndx
,
4330 unsigned int sh_type
,
4331 const unsigned char* prelocs
,
4333 Output_section
* output_section
,
4334 bool needs_special_offset_handling
,
4335 size_t local_symbol_count
,
4336 const unsigned char* plocal_symbols
)
4338 typedef typename Target_arm
<big_endian
>::Scan Scan
;
4339 if (sh_type
== elfcpp::SHT_RELA
)
4341 gold_error(_("%s: unsupported RELA reloc section"),
4342 object
->name().c_str());
4346 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
4355 needs_special_offset_handling
,
4360 // Finalize the sections.
4362 template<bool big_endian
>
4364 Target_arm
<big_endian
>::do_finalize_sections(
4366 const Input_objects
* input_objects
)
4368 // Merge processor-specific flags.
4369 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
4370 p
!= input_objects
->relobj_end();
4373 Arm_relobj
<big_endian
>* arm_relobj
=
4374 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
4375 this->merge_processor_specific_flags(
4377 arm_relobj
->processor_specific_flags());
4380 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
4381 p
!= input_objects
->dynobj_end();
4384 Arm_dynobj
<big_endian
>* arm_dynobj
=
4385 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
4386 this->merge_processor_specific_flags(
4388 arm_dynobj
->processor_specific_flags());
4391 // Fill in some more dynamic tags.
4392 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4395 if (this->got_plt_
!= NULL
4396 && this->got_plt_
->output_section() != NULL
)
4397 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
4399 if (this->plt_
!= NULL
4400 && this->plt_
->output_section() != NULL
)
4402 const Output_data
* od
= this->plt_
->rel_plt();
4403 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
4404 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
4405 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
4408 if (this->rel_dyn_
!= NULL
4409 && this->rel_dyn_
->output_section() != NULL
)
4411 const Output_data
* od
= this->rel_dyn_
;
4412 odyn
->add_section_address(elfcpp::DT_REL
, od
);
4413 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
4414 odyn
->add_constant(elfcpp::DT_RELENT
,
4415 elfcpp::Elf_sizes
<32>::rel_size
);
4418 if (!parameters
->options().shared())
4420 // The value of the DT_DEBUG tag is filled in by the dynamic
4421 // linker at run time, and used by the debugger.
4422 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
4426 // Emit any relocs we saved in an attempt to avoid generating COPY
4428 if (this->copy_relocs_
.any_saved_relocs())
4429 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
4431 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4432 // the .ARM.exidx section.
4433 if (!layout
->script_options()->saw_phdrs_clause()
4434 && !parameters
->options().relocatable())
4436 Output_section
* exidx_section
=
4437 layout
->find_output_section(".ARM.exidx");
4439 if (exidx_section
!= NULL
4440 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
)
4442 gold_assert(layout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
4444 Output_segment
* exidx_segment
=
4445 layout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
4446 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
4452 // Return whether a direct absolute static relocation needs to be applied.
4453 // In cases where Scan::local() or Scan::global() has created
4454 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4455 // of the relocation is carried in the data, and we must not
4456 // apply the static relocation.
4458 template<bool big_endian
>
4460 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
4461 const Sized_symbol
<32>* gsym
,
4464 Output_section
* output_section
)
4466 // If the output section is not allocated, then we didn't call
4467 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4469 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
4472 // For local symbols, we will have created a non-RELATIVE dynamic
4473 // relocation only if (a) the output is position independent,
4474 // (b) the relocation is absolute (not pc- or segment-relative), and
4475 // (c) the relocation is not 32 bits wide.
4477 return !(parameters
->options().output_is_position_independent()
4478 && (ref_flags
& Symbol::ABSOLUTE_REF
)
4481 // For global symbols, we use the same helper routines used in the
4482 // scan pass. If we did not create a dynamic relocation, or if we
4483 // created a RELATIVE dynamic relocation, we should apply the static
4485 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
4486 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
4487 && gsym
->can_use_relative_reloc(ref_flags
4488 & Symbol::FUNCTION_CALL
);
4489 return !has_dyn
|| is_rel
;
4492 // Perform a relocation.
4494 template<bool big_endian
>
4496 Target_arm
<big_endian
>::Relocate::relocate(
4497 const Relocate_info
<32, big_endian
>* relinfo
,
4499 Output_section
*output_section
,
4501 const elfcpp::Rel
<32, big_endian
>& rel
,
4502 unsigned int r_type
,
4503 const Sized_symbol
<32>* gsym
,
4504 const Symbol_value
<32>* psymval
,
4505 unsigned char* view
,
4506 Arm_address address
,
4507 section_size_type
/* view_size */ )
4509 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
4511 r_type
= get_real_reloc_type(r_type
);
4513 const Arm_relobj
<big_endian
>* object
=
4514 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
4516 // If the final branch target of a relocation is THUMB instruction, this
4517 // is 1. Otherwise it is 0.
4518 Arm_address thumb_bit
= 0;
4519 Symbol_value
<32> symval
;
4520 bool is_weakly_undefined_without_plt
= false;
4521 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
4525 // This is a global symbol. Determine if we use PLT and if the
4526 // final target is THUMB.
4527 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
4529 // This uses a PLT, change the symbol value.
4530 symval
.set_output_value(target
->plt_section()->address()
4531 + gsym
->plt_offset());
4534 else if (gsym
->is_weak_undefined())
4536 // This is a weakly undefined symbol and we do not use PLT
4537 // for this relocation. A branch targeting this symbol will
4538 // be converted into an NOP.
4539 is_weakly_undefined_without_plt
= true;
4543 // Set thumb bit if symbol:
4544 // -Has type STT_ARM_TFUNC or
4545 // -Has type STT_FUNC, is defined and with LSB in value set.
4547 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4548 || (gsym
->type() == elfcpp::STT_FUNC
4549 && !gsym
->is_undefined()
4550 && ((psymval
->value(object
, 0) & 1) != 0)))
4557 // This is a local symbol. Determine if the final target is THUMB.
4558 // We saved this information when all the local symbols were read.
4559 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
4560 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
4561 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
4566 // This is a fake relocation synthesized for a stub. It does not have
4567 // a real symbol. We just look at the LSB of the symbol value to
4568 // determine if the target is THUMB or not.
4569 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
4572 // Strip LSB if this points to a THUMB target.
4574 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
4575 && ((psymval
->value(object
, 0) & 1) != 0))
4577 Arm_address stripped_value
=
4578 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
4579 symval
.set_output_value(stripped_value
);
4583 // Get the GOT offset if needed.
4584 // The GOT pointer points to the end of the GOT section.
4585 // We need to subtract the size of the GOT section to get
4586 // the actual offset to use in the relocation.
4587 bool have_got_offset
= false;
4588 unsigned int got_offset
= 0;
4591 case elfcpp::R_ARM_GOT_BREL
:
4592 case elfcpp::R_ARM_GOT_PREL
:
4595 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4596 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4597 - target
->got_size());
4601 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4602 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4603 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4604 - target
->got_size());
4606 have_got_offset
= true;
4613 // To look up relocation stubs, we need to pass the symbol table index of
4615 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4617 typename
Arm_relocate_functions::Status reloc_status
=
4618 Arm_relocate_functions::STATUS_OKAY
;
4621 case elfcpp::R_ARM_NONE
:
4624 case elfcpp::R_ARM_ABS8
:
4625 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4627 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
4630 case elfcpp::R_ARM_ABS12
:
4631 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4633 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
4636 case elfcpp::R_ARM_ABS16
:
4637 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4639 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
4642 case elfcpp::R_ARM_ABS32
:
4643 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4645 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4649 case elfcpp::R_ARM_ABS32_NOI
:
4650 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4652 // No thumb bit for this relocation: (S + A)
4653 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4657 case elfcpp::R_ARM_MOVW_ABS_NC
:
4658 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4660 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
4664 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4665 "a shared object; recompile with -fPIC"));
4668 case elfcpp::R_ARM_MOVT_ABS
:
4669 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4671 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
4673 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4674 "a shared object; recompile with -fPIC"));
4677 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4678 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4680 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
4684 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4685 "making a shared object; recompile with -fPIC"));
4688 case elfcpp::R_ARM_THM_MOVT_ABS
:
4689 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4691 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
4694 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4695 "making a shared object; recompile with -fPIC"));
4698 case elfcpp::R_ARM_MOVW_PREL_NC
:
4699 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
4704 case elfcpp::R_ARM_MOVT_PREL
:
4705 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
4709 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4710 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
4715 case elfcpp::R_ARM_THM_MOVT_PREL
:
4716 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
4720 case elfcpp::R_ARM_REL32
:
4721 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4722 address
, thumb_bit
);
4725 case elfcpp::R_ARM_THM_ABS5
:
4726 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4728 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
4731 case elfcpp::R_ARM_THM_CALL
:
4732 reloc_status
= Arm_relocate_functions::thm_call(view
, object
, psymval
,
4733 address
, thumb_bit
);
4736 case elfcpp::R_ARM_XPC25
:
4738 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
4739 psymval
, address
, thumb_bit
,
4740 is_weakly_undefined_without_plt
);
4743 case elfcpp::R_ARM_GOTOFF32
:
4745 Arm_address got_origin
;
4746 got_origin
= target
->got_plt_section()->address();
4747 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
4748 got_origin
, thumb_bit
);
4752 case elfcpp::R_ARM_BASE_PREL
:
4755 // Get the addressing origin of the output segment defining the
4756 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4757 gold_assert(gsym
!= NULL
);
4758 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4759 origin
= gsym
->output_segment()->vaddr();
4760 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4761 origin
= gsym
->output_data()->address();
4764 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4765 _("cannot find origin of R_ARM_BASE_PREL"));
4768 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
4772 case elfcpp::R_ARM_BASE_ABS
:
4774 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4779 // Get the addressing origin of the output segment defining
4780 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4782 // R_ARM_BASE_ABS with the NULL symbol will give the
4783 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4784 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4785 origin
= target
->got_plt_section()->address();
4786 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
4787 origin
= gsym
->output_segment()->vaddr();
4788 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
4789 origin
= gsym
->output_data()->address();
4792 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4793 _("cannot find origin of R_ARM_BASE_ABS"));
4797 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
4801 case elfcpp::R_ARM_GOT_BREL
:
4802 gold_assert(have_got_offset
);
4803 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
4806 case elfcpp::R_ARM_GOT_PREL
:
4807 gold_assert(have_got_offset
);
4808 // Get the address origin for GOT PLT, which is allocated right
4809 // after the GOT section, to calculate an absolute address of
4810 // the symbol GOT entry (got_origin + got_offset).
4811 Arm_address got_origin
;
4812 got_origin
= target
->got_plt_section()->address();
4813 reloc_status
= Arm_relocate_functions::got_prel(view
,
4814 got_origin
+ got_offset
,
4818 case elfcpp::R_ARM_PLT32
:
4819 gold_assert(gsym
== NULL
4820 || gsym
->has_plt_offset()
4821 || gsym
->final_value_is_known()
4822 || (gsym
->is_defined()
4823 && !gsym
->is_from_dynobj()
4824 && !gsym
->is_preemptible()));
4826 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
4827 psymval
, address
, thumb_bit
,
4828 is_weakly_undefined_without_plt
);
4831 case elfcpp::R_ARM_CALL
:
4833 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
4834 psymval
, address
, thumb_bit
,
4835 is_weakly_undefined_without_plt
);
4838 case elfcpp::R_ARM_JUMP24
:
4840 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
4841 psymval
, address
, thumb_bit
,
4842 is_weakly_undefined_without_plt
);
4845 case elfcpp::R_ARM_PREL31
:
4846 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
4847 address
, thumb_bit
);
4850 case elfcpp::R_ARM_TARGET1
:
4851 // This should have been mapped to another type already.
4853 case elfcpp::R_ARM_COPY
:
4854 case elfcpp::R_ARM_GLOB_DAT
:
4855 case elfcpp::R_ARM_JUMP_SLOT
:
4856 case elfcpp::R_ARM_RELATIVE
:
4857 // These are relocations which should only be seen by the
4858 // dynamic linker, and should never be seen here.
4859 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4860 _("unexpected reloc %u in object file"),
4865 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4866 _("unsupported reloc %u"),
4871 // Report any errors.
4872 switch (reloc_status
)
4874 case Arm_relocate_functions::STATUS_OKAY
:
4876 case Arm_relocate_functions::STATUS_OVERFLOW
:
4877 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
4878 _("relocation overflow in relocation %u"),
4881 case Arm_relocate_functions::STATUS_BAD_RELOC
:
4882 gold_error_at_location(
4886 _("unexpected opcode while processing relocation %u"),
4896 // Relocate section data.
4898 template<bool big_endian
>
4900 Target_arm
<big_endian
>::relocate_section(
4901 const Relocate_info
<32, big_endian
>* relinfo
,
4902 unsigned int sh_type
,
4903 const unsigned char* prelocs
,
4905 Output_section
* output_section
,
4906 bool needs_special_offset_handling
,
4907 unsigned char* view
,
4908 Arm_address address
,
4909 section_size_type view_size
,
4910 const Reloc_symbol_changes
* reloc_symbol_changes
)
4912 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
4913 gold_assert(sh_type
== elfcpp::SHT_REL
);
4915 Arm_input_section
<big_endian
>* arm_input_section
=
4916 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
4918 // This is an ARM input section and the view covers the whole output
4920 if (arm_input_section
!= NULL
)
4922 gold_assert(needs_special_offset_handling
);
4923 Arm_address section_address
= arm_input_section
->address();
4924 section_size_type section_size
= arm_input_section
->data_size();
4926 gold_assert((arm_input_section
->address() >= address
)
4927 && ((arm_input_section
->address()
4928 + arm_input_section
->data_size())
4929 <= (address
+ view_size
)));
4931 off_t offset
= section_address
- address
;
4934 view_size
= section_size
;
4937 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
4944 needs_special_offset_handling
,
4948 reloc_symbol_changes
);
4951 // Return the size of a relocation while scanning during a relocatable
4954 template<bool big_endian
>
4956 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
4957 unsigned int r_type
,
4960 r_type
= get_real_reloc_type(r_type
);
4963 case elfcpp::R_ARM_NONE
:
4966 case elfcpp::R_ARM_ABS8
:
4969 case elfcpp::R_ARM_ABS16
:
4970 case elfcpp::R_ARM_THM_ABS5
:
4973 case elfcpp::R_ARM_ABS32
:
4974 case elfcpp::R_ARM_ABS32_NOI
:
4975 case elfcpp::R_ARM_ABS12
:
4976 case elfcpp::R_ARM_BASE_ABS
:
4977 case elfcpp::R_ARM_REL32
:
4978 case elfcpp::R_ARM_THM_CALL
:
4979 case elfcpp::R_ARM_GOTOFF32
:
4980 case elfcpp::R_ARM_BASE_PREL
:
4981 case elfcpp::R_ARM_GOT_BREL
:
4982 case elfcpp::R_ARM_GOT_PREL
:
4983 case elfcpp::R_ARM_PLT32
:
4984 case elfcpp::R_ARM_CALL
:
4985 case elfcpp::R_ARM_JUMP24
:
4986 case elfcpp::R_ARM_PREL31
:
4987 case elfcpp::R_ARM_MOVW_ABS_NC
:
4988 case elfcpp::R_ARM_MOVT_ABS
:
4989 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4990 case elfcpp::R_ARM_THM_MOVT_ABS
:
4991 case elfcpp::R_ARM_MOVW_PREL_NC
:
4992 case elfcpp::R_ARM_MOVT_PREL
:
4993 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4994 case elfcpp::R_ARM_THM_MOVT_PREL
:
4997 case elfcpp::R_ARM_TARGET1
:
4998 // This should have been mapped to another type already.
5000 case elfcpp::R_ARM_COPY
:
5001 case elfcpp::R_ARM_GLOB_DAT
:
5002 case elfcpp::R_ARM_JUMP_SLOT
:
5003 case elfcpp::R_ARM_RELATIVE
:
5004 // These are relocations which should only be seen by the
5005 // dynamic linker, and should never be seen here.
5006 gold_error(_("%s: unexpected reloc %u in object file"),
5007 object
->name().c_str(), r_type
);
5011 object
->error(_("unsupported reloc %u in object file"), r_type
);
5016 // Scan the relocs during a relocatable link.
5018 template<bool big_endian
>
5020 Target_arm
<big_endian
>::scan_relocatable_relocs(
5021 Symbol_table
* symtab
,
5023 Sized_relobj
<32, big_endian
>* object
,
5024 unsigned int data_shndx
,
5025 unsigned int sh_type
,
5026 const unsigned char* prelocs
,
5028 Output_section
* output_section
,
5029 bool needs_special_offset_handling
,
5030 size_t local_symbol_count
,
5031 const unsigned char* plocal_symbols
,
5032 Relocatable_relocs
* rr
)
5034 gold_assert(sh_type
== elfcpp::SHT_REL
);
5036 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5037 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5039 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5040 Scan_relocatable_relocs
>(
5048 needs_special_offset_handling
,
5054 // Relocate a section during a relocatable link.
5056 template<bool big_endian
>
5058 Target_arm
<big_endian
>::relocate_for_relocatable(
5059 const Relocate_info
<32, big_endian
>* relinfo
,
5060 unsigned int sh_type
,
5061 const unsigned char* prelocs
,
5063 Output_section
* output_section
,
5064 off_t offset_in_output_section
,
5065 const Relocatable_relocs
* rr
,
5066 unsigned char* view
,
5067 Arm_address view_address
,
5068 section_size_type view_size
,
5069 unsigned char* reloc_view
,
5070 section_size_type reloc_view_size
)
5072 gold_assert(sh_type
== elfcpp::SHT_REL
);
5074 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5079 offset_in_output_section
,
5088 // Return the value to use for a dynamic symbol which requires special
5089 // treatment. This is how we support equality comparisons of function
5090 // pointers across shared library boundaries, as described in the
5091 // processor specific ABI supplement.
5093 template<bool big_endian
>
5095 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5097 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5098 return this->plt_section()->address() + gsym
->plt_offset();
5101 // Map platform-specific relocs to real relocs
5103 template<bool big_endian
>
5105 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5109 case elfcpp::R_ARM_TARGET1
:
5110 // This is either R_ARM_ABS32 or R_ARM_REL32;
5111 return elfcpp::R_ARM_ABS32
;
5113 case elfcpp::R_ARM_TARGET2
:
5114 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5115 return elfcpp::R_ARM_GOT_PREL
;
5122 // Whether if two EABI versions V1 and V2 are compatible.
5124 template<bool big_endian
>
5126 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5127 elfcpp::Elf_Word v1
,
5128 elfcpp::Elf_Word v2
)
5130 // v4 and v5 are the same spec before and after it was released,
5131 // so allow mixing them.
5132 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5133 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5139 // Combine FLAGS from an input object called NAME and the processor-specific
5140 // flags in the ELF header of the output. Much of this is adapted from the
5141 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5142 // in bfd/elf32-arm.c.
5144 template<bool big_endian
>
5146 Target_arm
<big_endian
>::merge_processor_specific_flags(
5147 const std::string
& name
,
5148 elfcpp::Elf_Word flags
)
5150 if (this->are_processor_specific_flags_set())
5152 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5154 // Nothing to merge if flags equal to those in output.
5155 if (flags
== out_flags
)
5158 // Complain about various flag mismatches.
5159 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5160 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5161 if (!this->are_eabi_versions_compatible(version1
, version2
))
5162 gold_error(_("Source object %s has EABI version %d but output has "
5163 "EABI version %d."),
5165 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5166 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5170 // If the input is the default architecture and had the default
5171 // flags then do not bother setting the flags for the output
5172 // architecture, instead allow future merges to do this. If no
5173 // future merges ever set these flags then they will retain their
5174 // uninitialised values, which surprise surprise, correspond
5175 // to the default values.
5179 // This is the first time, just copy the flags.
5180 // We only copy the EABI version for now.
5181 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5185 // Adjust ELF file header.
5186 template<bool big_endian
>
5188 Target_arm
<big_endian
>::do_adjust_elf_header(
5189 unsigned char* view
,
5192 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5194 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5195 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5196 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5198 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5199 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5200 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5202 e_ident
[elfcpp::EI_OSABI
] = 0;
5203 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5205 // FIXME: Do EF_ARM_BE8 adjustment.
5207 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5208 oehdr
.put_e_ident(e_ident
);
5211 // do_make_elf_object to override the same function in the base class.
5212 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5213 // to store ARM specific information. Hence we need to have our own
5214 // ELF object creation.
5216 template<bool big_endian
>
5218 Target_arm
<big_endian
>::do_make_elf_object(
5219 const std::string
& name
,
5220 Input_file
* input_file
,
5221 off_t offset
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5223 int et
= ehdr
.get_e_type();
5224 if (et
== elfcpp::ET_REL
)
5226 Arm_relobj
<big_endian
>* obj
=
5227 new Arm_relobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5231 else if (et
== elfcpp::ET_DYN
)
5233 Sized_dynobj
<32, big_endian
>* obj
=
5234 new Arm_dynobj
<big_endian
>(name
, input_file
, offset
, ehdr
);
5240 gold_error(_("%s: unsupported ELF file type %d"),
5246 // Return whether a relocation type used the LSB to distinguish THUMB
5248 template<bool big_endian
>
5250 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
5254 case elfcpp::R_ARM_PC24
:
5255 case elfcpp::R_ARM_ABS32
:
5256 case elfcpp::R_ARM_REL32
:
5257 case elfcpp::R_ARM_SBREL32
:
5258 case elfcpp::R_ARM_THM_CALL
:
5259 case elfcpp::R_ARM_GLOB_DAT
:
5260 case elfcpp::R_ARM_JUMP_SLOT
:
5261 case elfcpp::R_ARM_GOTOFF32
:
5262 case elfcpp::R_ARM_PLT32
:
5263 case elfcpp::R_ARM_CALL
:
5264 case elfcpp::R_ARM_JUMP24
:
5265 case elfcpp::R_ARM_THM_JUMP24
:
5266 case elfcpp::R_ARM_SBREL31
:
5267 case elfcpp::R_ARM_PREL31
:
5268 case elfcpp::R_ARM_MOVW_ABS_NC
:
5269 case elfcpp::R_ARM_MOVW_PREL_NC
:
5270 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5271 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5272 case elfcpp::R_ARM_THM_JUMP19
:
5273 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
5274 case elfcpp::R_ARM_ALU_PC_G0_NC
:
5275 case elfcpp::R_ARM_ALU_PC_G0
:
5276 case elfcpp::R_ARM_ALU_PC_G1_NC
:
5277 case elfcpp::R_ARM_ALU_PC_G1
:
5278 case elfcpp::R_ARM_ALU_PC_G2
:
5279 case elfcpp::R_ARM_ALU_SB_G0_NC
:
5280 case elfcpp::R_ARM_ALU_SB_G0
:
5281 case elfcpp::R_ARM_ALU_SB_G1_NC
:
5282 case elfcpp::R_ARM_ALU_SB_G1
:
5283 case elfcpp::R_ARM_ALU_SB_G2
:
5284 case elfcpp::R_ARM_MOVW_BREL_NC
:
5285 case elfcpp::R_ARM_MOVW_BREL
:
5286 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
5287 case elfcpp::R_ARM_THM_MOVW_BREL
:
5294 // Stub-generation methods for Target_arm.
5296 // Make a new Arm_input_section object.
5298 template<bool big_endian
>
5299 Arm_input_section
<big_endian
>*
5300 Target_arm
<big_endian
>::new_arm_input_section(
5304 Input_section_specifier
iss(relobj
, shndx
);
5306 Arm_input_section
<big_endian
>* arm_input_section
=
5307 new Arm_input_section
<big_endian
>(relobj
, shndx
);
5308 arm_input_section
->init();
5310 // Register new Arm_input_section in map for look-up.
5311 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
5312 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
5314 // Make sure that it we have not created another Arm_input_section
5315 // for this input section already.
5316 gold_assert(ins
.second
);
5318 return arm_input_section
;
5321 // Find the Arm_input_section object corresponding to the SHNDX-th input
5322 // section of RELOBJ.
5324 template<bool big_endian
>
5325 Arm_input_section
<big_endian
>*
5326 Target_arm
<big_endian
>::find_arm_input_section(
5328 unsigned int shndx
) const
5330 Input_section_specifier
iss(relobj
, shndx
);
5331 typename
Arm_input_section_map::const_iterator p
=
5332 this->arm_input_section_map_
.find(iss
);
5333 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
5336 // Make a new stub table.
5338 template<bool big_endian
>
5339 Stub_table
<big_endian
>*
5340 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
5342 Stub_table
<big_endian
>* stub_table
=
5343 new Stub_table
<big_endian
>(owner
);
5344 this->stub_tables_
.push_back(stub_table
);
5346 stub_table
->set_address(owner
->address() + owner
->data_size());
5347 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
5348 stub_table
->finalize_data_size();
5353 // Scan a relocation for stub generation.
5355 template<bool big_endian
>
5357 Target_arm
<big_endian
>::scan_reloc_for_stub(
5358 const Relocate_info
<32, big_endian
>* relinfo
,
5359 unsigned int r_type
,
5360 const Sized_symbol
<32>* gsym
,
5362 const Symbol_value
<32>* psymval
,
5363 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
5364 Arm_address address
)
5366 typedef typename Target_arm
<big_endian
>::Relocate Relocate
;
5368 const Arm_relobj
<big_endian
>* arm_relobj
=
5369 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5371 bool target_is_thumb
;
5372 Symbol_value
<32> symval
;
5375 // This is a global symbol. Determine if we use PLT and if the
5376 // final target is THUMB.
5377 if (gsym
->use_plt_offset(Relocate::reloc_is_non_pic(r_type
)))
5379 // This uses a PLT, change the symbol value.
5380 symval
.set_output_value(this->plt_section()->address()
5381 + gsym
->plt_offset());
5383 target_is_thumb
= false;
5385 else if (gsym
->is_undefined())
5386 // There is no need to generate a stub symbol is undefined.
5391 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
5392 || (gsym
->type() == elfcpp::STT_FUNC
5393 && !gsym
->is_undefined()
5394 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
5399 // This is a local symbol. Determine if the final target is THUMB.
5400 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
5403 // Strip LSB if this points to a THUMB target.
5405 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
5406 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
5408 Arm_address stripped_value
=
5409 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
5410 symval
.set_output_value(stripped_value
);
5414 // Get the symbol value.
5415 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
5417 // Owing to pipelining, the PC relative branches below actually skip
5418 // two instructions when the branch offset is 0.
5419 Arm_address destination
;
5422 case elfcpp::R_ARM_CALL
:
5423 case elfcpp::R_ARM_JUMP24
:
5424 case elfcpp::R_ARM_PLT32
:
5426 destination
= value
+ addend
+ 8;
5428 case elfcpp::R_ARM_THM_CALL
:
5429 case elfcpp::R_ARM_THM_XPC22
:
5430 case elfcpp::R_ARM_THM_JUMP24
:
5431 case elfcpp::R_ARM_THM_JUMP19
:
5433 destination
= value
+ addend
+ 4;
5439 Stub_type stub_type
=
5440 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
5443 // This reloc does not need a stub.
5444 if (stub_type
== arm_stub_none
)
5447 // Try looking up an existing stub from a stub table.
5448 Stub_table
<big_endian
>* stub_table
=
5449 arm_relobj
->stub_table(relinfo
->data_shndx
);
5450 gold_assert(stub_table
!= NULL
);
5452 // Locate stub by destination.
5453 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
5455 // Create a stub if there is not one already
5456 Reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
5459 // create a new stub and add it to stub table.
5460 stub
= this->stub_factory().make_reloc_stub(stub_type
);
5461 stub_table
->add_reloc_stub(stub
, stub_key
);
5464 // Record the destination address.
5465 stub
->set_destination_address(destination
5466 | (target_is_thumb
? 1 : 0));
5469 // This function scans a relocation sections for stub generation.
5470 // The template parameter Relocate must be a class type which provides
5471 // a single function, relocate(), which implements the machine
5472 // specific part of a relocation.
5474 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
5475 // SHT_REL or SHT_RELA.
5477 // PRELOCS points to the relocation data. RELOC_COUNT is the number
5478 // of relocs. OUTPUT_SECTION is the output section.
5479 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
5480 // mapped to output offsets.
5482 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
5483 // VIEW_SIZE is the size. These refer to the input section, unless
5484 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
5485 // the output section.
5487 template<bool big_endian
>
5488 template<int sh_type
>
5490 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
5491 const Relocate_info
<32, big_endian
>* relinfo
,
5492 const unsigned char* prelocs
,
5494 Output_section
* output_section
,
5495 bool needs_special_offset_handling
,
5496 const unsigned char* view
,
5497 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
5500 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
5501 const int reloc_size
=
5502 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
5504 Arm_relobj
<big_endian
>* arm_object
=
5505 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
5506 unsigned int local_count
= arm_object
->local_symbol_count();
5508 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
5510 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
5512 Reltype
reloc(prelocs
);
5514 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
5515 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
5516 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
5518 r_type
= this->get_real_reloc_type(r_type
);
5520 // Only a few relocation types need stubs.
5521 if ((r_type
!= elfcpp::R_ARM_CALL
)
5522 && (r_type
!= elfcpp::R_ARM_JUMP24
)
5523 && (r_type
!= elfcpp::R_ARM_PLT32
)
5524 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
5525 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
5526 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
5527 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
5530 section_offset_type offset
=
5531 convert_to_section_size_type(reloc
.get_r_offset());
5533 if (needs_special_offset_handling
)
5535 offset
= output_section
->output_offset(relinfo
->object
,
5536 relinfo
->data_shndx
,
5543 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
5544 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
5545 stub_addend_reader(r_type
, view
+ offset
, reloc
);
5547 const Sized_symbol
<32>* sym
;
5549 Symbol_value
<32> symval
;
5550 const Symbol_value
<32> *psymval
;
5551 if (r_sym
< local_count
)
5554 psymval
= arm_object
->local_symbol(r_sym
);
5556 // If the local symbol belongs to a section we are discarding,
5557 // and that section is a debug section, try to find the
5558 // corresponding kept section and map this symbol to its
5559 // counterpart in the kept section. The symbol must not
5560 // correspond to a section we are folding.
5562 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
5564 && shndx
!= elfcpp::SHN_UNDEF
5565 && !arm_object
->is_section_included(shndx
)
5566 && !(relinfo
->symtab
->is_section_folded(arm_object
, shndx
)))
5568 if (comdat_behavior
== CB_UNDETERMINED
)
5571 arm_object
->section_name(relinfo
->data_shndx
);
5572 comdat_behavior
= get_comdat_behavior(name
.c_str());
5574 if (comdat_behavior
== CB_PRETEND
)
5577 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
5578 arm_object
->map_to_kept_section(shndx
, &found
);
5580 symval
.set_output_value(value
+ psymval
->input_value());
5582 symval
.set_output_value(0);
5586 symval
.set_output_value(0);
5588 symval
.set_no_output_symtab_entry();
5594 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
5595 gold_assert(gsym
!= NULL
);
5596 if (gsym
->is_forwarder())
5597 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
5599 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
5600 if (sym
->has_symtab_index())
5601 symval
.set_output_symtab_index(sym
->symtab_index());
5603 symval
.set_no_output_symtab_entry();
5605 // We need to compute the would-be final value of this global
5607 const Symbol_table
* symtab
= relinfo
->symtab
;
5608 const Sized_symbol
<32>* sized_symbol
=
5609 symtab
->get_sized_symbol
<32>(gsym
);
5610 Symbol_table::Compute_final_value_status status
;
5612 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
5614 // Skip this if the symbol has not output section.
5615 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
5618 symval
.set_output_value(value
);
5622 // If symbol is a section symbol, we don't know the actual type of
5623 // destination. Give up.
5624 if (psymval
->is_section_symbol())
5627 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
5628 addend
, view_address
+ offset
);
5632 // Scan an input section for stub generation.
5634 template<bool big_endian
>
5636 Target_arm
<big_endian
>::scan_section_for_stubs(
5637 const Relocate_info
<32, big_endian
>* relinfo
,
5638 unsigned int sh_type
,
5639 const unsigned char* prelocs
,
5641 Output_section
* output_section
,
5642 bool needs_special_offset_handling
,
5643 const unsigned char* view
,
5644 Arm_address view_address
,
5645 section_size_type view_size
)
5647 if (sh_type
== elfcpp::SHT_REL
)
5648 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
5653 needs_special_offset_handling
,
5657 else if (sh_type
== elfcpp::SHT_RELA
)
5658 // We do not support RELA type relocations yet. This is provided for
5660 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
5665 needs_special_offset_handling
,
5673 // Group input sections for stub generation.
5675 // We goup input sections in an output sections so that the total size,
5676 // including any padding space due to alignment is smaller than GROUP_SIZE
5677 // unless the only input section in group is bigger than GROUP_SIZE already.
5678 // Then an ARM stub table is created to follow the last input section
5679 // in group. For each group an ARM stub table is created an is placed
5680 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
5681 // extend the group after the stub table.
5683 template<bool big_endian
>
5685 Target_arm
<big_endian
>::group_sections(
5687 section_size_type group_size
,
5688 bool stubs_always_after_branch
)
5690 // Group input sections and insert stub table
5691 Layout::Section_list section_list
;
5692 layout
->get_allocated_sections(§ion_list
);
5693 for (Layout::Section_list::const_iterator p
= section_list
.begin();
5694 p
!= section_list
.end();
5697 Arm_output_section
<big_endian
>* output_section
=
5698 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
5699 output_section
->group_sections(group_size
, stubs_always_after_branch
,
5704 // Relaxation hook. This is where we do stub generation.
5706 template<bool big_endian
>
5708 Target_arm
<big_endian
>::do_relax(
5710 const Input_objects
* input_objects
,
5711 Symbol_table
* symtab
,
5714 // No need to generate stubs if this is a relocatable link.
5715 gold_assert(!parameters
->options().relocatable());
5717 // If this is the first pass, we need to group input sections into
5721 // Determine the stub group size. The group size is the absolute
5722 // value of the parameter --stub-group-size. If --stub-group-size
5723 // is passed a negative value, we restict stubs to be always after
5724 // the stubbed branches.
5725 int32_t stub_group_size_param
=
5726 parameters
->options().stub_group_size();
5727 bool stubs_always_after_branch
= stub_group_size_param
< 0;
5728 section_size_type stub_group_size
= abs(stub_group_size_param
);
5730 if (stub_group_size
== 1)
5733 // Thumb branch range is +-4MB has to be used as the default
5734 // maximum size (a given section can contain both ARM and Thumb
5735 // code, so the worst case has to be taken into account).
5737 // This value is 24K less than that, which allows for 2025
5738 // 12-byte stubs. If we exceed that, then we will fail to link.
5739 // The user will have to relink with an explicit group size
5741 stub_group_size
= 4170000;
5744 group_sections(layout
, stub_group_size
, stubs_always_after_branch
);
5747 // clear changed flags for all stub_tables
5748 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
5749 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5750 sp
!= this->stub_tables_
.end();
5752 (*sp
)->set_has_been_changed(false);
5754 // scan relocs for stubs
5755 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
5756 op
!= input_objects
->relobj_end();
5759 Arm_relobj
<big_endian
>* arm_relobj
=
5760 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
5761 arm_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
5764 bool any_stub_table_changed
= false;
5765 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5766 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
5769 if ((*sp
)->has_been_changed())
5770 any_stub_table_changed
= true;
5773 return any_stub_table_changed
;
5778 template<bool big_endian
>
5780 Target_arm
<big_endian
>::relocate_stub(
5782 const Relocate_info
<32, big_endian
>* relinfo
,
5783 Output_section
* output_section
,
5784 unsigned char* view
,
5785 Arm_address address
,
5786 section_size_type view_size
)
5789 const Stub_template
* stub_template
= stub
->stub_template();
5790 for (size_t i
= 0; i
< stub_template
->reloc_count(); i
++)
5792 size_t reloc_insn_index
= stub_template
->reloc_insn_index(i
);
5793 const Insn_template
* insn
= &stub_template
->insns()[reloc_insn_index
];
5795 unsigned int r_type
= insn
->r_type();
5796 section_size_type reloc_offset
= stub_template
->reloc_offset(i
);
5797 section_size_type reloc_size
= insn
->size();
5798 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
5800 // This is the address of the stub destination.
5801 Arm_address target
= stub
->reloc_target(i
);
5802 Symbol_value
<32> symval
;
5803 symval
.set_output_value(target
);
5805 // Synthesize a fake reloc just in case. We don't have a symbol so
5807 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
5808 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
5809 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
5810 reloc_write
.put_r_offset(reloc_offset
);
5811 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
5812 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
5814 relocate
.relocate(relinfo
, this, output_section
,
5815 this->fake_relnum_for_stubs
, rel
, r_type
,
5816 NULL
, &symval
, view
+ reloc_offset
,
5817 address
+ reloc_offset
, reloc_size
);
5821 // The selector for arm object files.
5823 template<bool big_endian
>
5824 class Target_selector_arm
: public Target_selector
5827 Target_selector_arm()
5828 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
5829 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
5833 do_instantiate_target()
5834 { return new Target_arm
<big_endian
>(); }
5837 Target_selector_arm
<false> target_selector_arm
;
5838 Target_selector_arm
<true> target_selector_armbe
;
5840 } // End anonymous namespace.