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"
49 #include "attributes.h"
56 template<bool big_endian
>
57 class Output_data_plt_arm
;
59 template<bool big_endian
>
62 template<bool big_endian
>
63 class Arm_input_section
;
65 template<bool big_endian
>
66 class Arm_output_section
;
68 template<bool big_endian
>
71 template<bool big_endian
>
75 typedef elfcpp::Elf_types
<32>::Elf_Addr Arm_address
;
77 // Maximum branch offsets for ARM, THUMB and THUMB2.
78 const int32_t ARM_MAX_FWD_BRANCH_OFFSET
= ((((1 << 23) - 1) << 2) + 8);
79 const int32_t ARM_MAX_BWD_BRANCH_OFFSET
= ((-((1 << 23) << 2)) + 8);
80 const int32_t THM_MAX_FWD_BRANCH_OFFSET
= ((1 << 22) -2 + 4);
81 const int32_t THM_MAX_BWD_BRANCH_OFFSET
= (-(1 << 22) + 4);
82 const int32_t THM2_MAX_FWD_BRANCH_OFFSET
= (((1 << 24) - 2) + 4);
83 const int32_t THM2_MAX_BWD_BRANCH_OFFSET
= (-(1 << 24) + 4);
85 // The arm target class.
87 // This is a very simple port of gold for ARM-EABI. It is intended for
88 // supporting Android only for the time being. Only these relocation types
117 // R_ARM_THM_MOVW_ABS_NC
118 // R_ARM_THM_MOVT_ABS
119 // R_ARM_MOVW_PREL_NC
121 // R_ARM_THM_MOVW_PREL_NC
122 // R_ARM_THM_MOVT_PREL
125 // - Support more relocation types as needed.
126 // - Make PLTs more flexible for different architecture features like
128 // There are probably a lot more.
130 // Instruction template class. This class is similar to the insn_sequence
131 // struct in bfd/elf32-arm.c.
136 // Types of instruction templates.
145 // Factory methods to create instrunction templates in different formats.
147 static const Insn_template
148 thumb16_insn(uint32_t data
)
149 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 0); }
151 // A bit of a hack. A Thumb conditional branch, in which the proper
152 // condition is inserted when we build the stub.
153 static const Insn_template
154 thumb16_bcond_insn(uint32_t data
)
155 { return Insn_template(data
, THUMB16_TYPE
, elfcpp::R_ARM_NONE
, 1); }
157 static const Insn_template
158 thumb32_insn(uint32_t data
)
159 { return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_NONE
, 0); }
161 static const Insn_template
162 thumb32_b_insn(uint32_t data
, int reloc_addend
)
164 return Insn_template(data
, THUMB32_TYPE
, elfcpp::R_ARM_THM_JUMP24
,
168 static const Insn_template
169 arm_insn(uint32_t data
)
170 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_NONE
, 0); }
172 static const Insn_template
173 arm_rel_insn(unsigned data
, int reloc_addend
)
174 { return Insn_template(data
, ARM_TYPE
, elfcpp::R_ARM_JUMP24
, reloc_addend
); }
176 static const Insn_template
177 data_word(unsigned data
, unsigned int r_type
, int reloc_addend
)
178 { return Insn_template(data
, DATA_TYPE
, r_type
, reloc_addend
); }
180 // Accessors. This class is used for read-only objects so no modifiers
185 { return this->data_
; }
187 // Return the instruction sequence type of this.
190 { return this->type_
; }
192 // Return the ARM relocation type of this.
195 { return this->r_type_
; }
199 { return this->reloc_addend_
; }
201 // Return size of instrunction template in bytes.
205 // Return byte-alignment of instrunction template.
210 // We make the constructor private to ensure that only the factory
213 Insn_template(unsigned adata
, Type atype
, unsigned int rtype
, int relocaddend
)
214 : data_(adata
), type_(atype
), r_type_(rtype
), reloc_addend_(relocaddend
)
217 // Instruction specific data. This is used to store information like
218 // some of the instruction bits.
220 // Instruction template type.
222 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
223 unsigned int r_type_
;
224 // Relocation addend.
225 int32_t reloc_addend_
;
228 // Macro for generating code to stub types. One entry per long/short
232 DEF_STUB(long_branch_any_any) \
233 DEF_STUB(long_branch_v4t_arm_thumb) \
234 DEF_STUB(long_branch_thumb_only) \
235 DEF_STUB(long_branch_v4t_thumb_thumb) \
236 DEF_STUB(long_branch_v4t_thumb_arm) \
237 DEF_STUB(short_branch_v4t_thumb_arm) \
238 DEF_STUB(long_branch_any_arm_pic) \
239 DEF_STUB(long_branch_any_thumb_pic) \
240 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
241 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
243 DEF_STUB(long_branch_thumb_only_pic) \
244 DEF_STUB(a8_veneer_b_cond) \
245 DEF_STUB(a8_veneer_b) \
246 DEF_STUB(a8_veneer_bl) \
247 DEF_STUB(a8_veneer_blx)
251 #define DEF_STUB(x) arm_stub_##x,
257 // First reloc stub type.
258 arm_stub_reloc_first
= arm_stub_long_branch_any_any
,
259 // Last reloc stub type.
260 arm_stub_reloc_last
= arm_stub_long_branch_thumb_only_pic
,
262 // First Cortex-A8 stub type.
263 arm_stub_cortex_a8_first
= arm_stub_a8_veneer_b_cond
,
264 // Last Cortex-A8 stub type.
265 arm_stub_cortex_a8_last
= arm_stub_a8_veneer_blx
,
268 arm_stub_type_last
= arm_stub_a8_veneer_blx
272 // Stub template class. Templates are meant to be read-only objects.
273 // A stub template for a stub type contains all read-only attributes
274 // common to all stubs of the same type.
279 Stub_template(Stub_type
, const Insn_template
*, size_t);
287 { return this->type_
; }
289 // Return an array of instruction templates.
292 { return this->insns_
; }
294 // Return size of template in number of instructions.
297 { return this->insn_count_
; }
299 // Return size of template in bytes.
302 { return this->size_
; }
304 // Return alignment of the stub template.
307 { return this->alignment_
; }
309 // Return whether entry point is in thumb mode.
311 entry_in_thumb_mode() const
312 { return this->entry_in_thumb_mode_
; }
314 // Return number of relocations in this template.
317 { return this->relocs_
.size(); }
319 // Return index of the I-th instruction with relocation.
321 reloc_insn_index(size_t i
) const
323 gold_assert(i
< this->relocs_
.size());
324 return this->relocs_
[i
].first
;
327 // Return the offset of the I-th instruction with relocation from the
328 // beginning of the stub.
330 reloc_offset(size_t i
) const
332 gold_assert(i
< this->relocs_
.size());
333 return this->relocs_
[i
].second
;
337 // This contains information about an instruction template with a relocation
338 // and its offset from start of stub.
339 typedef std::pair
<size_t, section_size_type
> Reloc
;
341 // A Stub_template may not be copied. We want to share templates as much
343 Stub_template(const Stub_template
&);
344 Stub_template
& operator=(const Stub_template
&);
348 // Points to an array of Insn_templates.
349 const Insn_template
* insns_
;
350 // Number of Insn_templates in insns_[].
352 // Size of templated instructions in bytes.
354 // Alignment of templated instructions.
356 // Flag to indicate if entry is in thumb mode.
357 bool entry_in_thumb_mode_
;
358 // A table of reloc instruction indices and offsets. We can find these by
359 // looking at the instruction templates but we pre-compute and then stash
360 // them here for speed.
361 std::vector
<Reloc
> relocs_
;
365 // A class for code stubs. This is a base class for different type of
366 // stubs used in the ARM target.
372 static const section_offset_type invalid_offset
=
373 static_cast<section_offset_type
>(-1);
376 Stub(const Stub_template
* stubtemplate
)
377 : stub_template_(stubtemplate
), offset_(invalid_offset
)
384 // Return the stub template.
386 stub_template() const
387 { return this->stub_template_
; }
389 // Return offset of code stub from beginning of its containing stub table.
393 gold_assert(this->offset_
!= invalid_offset
);
394 return this->offset_
;
397 // Set offset of code stub from beginning of its containing stub table.
399 set_offset(section_offset_type off
)
400 { this->offset_
= off
; }
402 // Return the relocation target address of the i-th relocation in the
403 // stub. This must be defined in a child class.
405 reloc_target(size_t i
)
406 { return this->do_reloc_target(i
); }
408 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
410 write(unsigned char* view
, section_size_type view_size
, bool big_endian
)
411 { this->do_write(view
, view_size
, big_endian
); }
414 // This must be defined in the child class.
416 do_reloc_target(size_t) = 0;
418 // This must be defined in the child class.
420 do_write(unsigned char*, section_size_type
, bool) = 0;
424 const Stub_template
* stub_template_
;
425 // Offset within the section of containing this stub.
426 section_offset_type offset_
;
429 // Reloc stub class. These are stubs we use to fix up relocation because
430 // of limited branch ranges.
432 class Reloc_stub
: public Stub
435 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
436 // We assume we never jump to this address.
437 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
439 // Return destination address.
441 destination_address() const
443 gold_assert(this->destination_address_
!= this->invalid_address
);
444 return this->destination_address_
;
447 // Set destination address.
449 set_destination_address(Arm_address address
)
451 gold_assert(address
!= this->invalid_address
);
452 this->destination_address_
= address
;
455 // Reset destination address.
457 reset_destination_address()
458 { this->destination_address_
= this->invalid_address
; }
460 // Determine stub type for a branch of a relocation of R_TYPE going
461 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
462 // the branch target is a thumb instruction. TARGET is used for look
463 // up ARM-specific linker settings.
465 stub_type_for_reloc(unsigned int r_type
, Arm_address branch_address
,
466 Arm_address branch_target
, bool target_is_thumb
);
468 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
469 // and an addend. Since we treat global and local symbol differently, we
470 // use a Symbol object for a global symbol and a object-index pair for
475 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
476 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
477 // and R_SYM must not be invalid_index.
478 Key(Stub_type stubtype
, const Symbol
* sym
, const Relobj
* rel_obj
,
479 unsigned int rsym
, int32_t addend
)
480 : stub_type_(stubtype
), addend_(addend
)
484 this->r_sym_
= Reloc_stub::invalid_index
;
485 this->u_
.symbol
= sym
;
489 gold_assert(rel_obj
!= NULL
&& rsym
!= invalid_index
);
491 this->u_
.relobj
= rel_obj
;
498 // Accessors: Keys are meant to be read-only object so no modifiers are
504 { return this->stub_type_
; }
506 // Return the local symbol index or invalid_index.
509 { return this->r_sym_
; }
511 // Return the symbol if there is one.
514 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
516 // Return the relobj if there is one.
519 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
521 // Whether this equals to another key k.
523 eq(const Key
& k
) const
525 return ((this->stub_type_
== k
.stub_type_
)
526 && (this->r_sym_
== k
.r_sym_
)
527 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
528 ? (this->u_
.relobj
== k
.u_
.relobj
)
529 : (this->u_
.symbol
== k
.u_
.symbol
))
530 && (this->addend_
== k
.addend_
));
533 // Return a hash value.
537 return (this->stub_type_
539 ^ gold::string_hash
<char>(
540 (this->r_sym_
!= Reloc_stub::invalid_index
)
541 ? this->u_
.relobj
->name().c_str()
542 : this->u_
.symbol
->name())
546 // Functors for STL associative containers.
550 operator()(const Key
& k
) const
551 { return k
.hash_value(); }
557 operator()(const Key
& k1
, const Key
& k2
) const
558 { return k1
.eq(k2
); }
561 // Name of key. This is mainly for debugging.
567 Stub_type stub_type_
;
568 // If this is a local symbol, this is the index in the defining object.
569 // Otherwise, it is invalid_index for a global symbol.
571 // If r_sym_ is invalid index. This points to a global symbol.
572 // Otherwise, this points a relobj. We used the unsized and target
573 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
574 // Arm_relobj. This is done to avoid making the stub class a template
575 // as most of the stub machinery is endianity-neutral. However, it
576 // may require a bit of casting done by users of this class.
579 const Symbol
* symbol
;
580 const Relobj
* relobj
;
582 // Addend associated with a reloc.
587 // Reloc_stubs are created via a stub factory. So these are protected.
588 Reloc_stub(const Stub_template
* stubtemplate
)
589 : Stub(stubtemplate
), destination_address_(invalid_address
)
595 friend class Stub_factory
;
598 // Return the relocation target address of the i-th relocation in the
601 do_reloc_target(size_t i
)
603 // All reloc stub have only one relocation.
605 return this->destination_address_
;
608 // A template to implement do_write below.
609 template<bool big_endian
>
611 do_fixed_endian_write(unsigned char*, section_size_type
);
615 do_write(unsigned char* view
, section_size_type view_size
, bool big_endian
);
617 // Address of destination.
618 Arm_address destination_address_
;
621 // Stub factory class.
626 // Return the unique instance of this class.
627 static const Stub_factory
&
630 static Stub_factory singleton
;
634 // Make a relocation stub.
636 make_reloc_stub(Stub_type stub_type
) const
638 gold_assert(stub_type
>= arm_stub_reloc_first
639 && stub_type
<= arm_stub_reloc_last
);
640 return new Reloc_stub(this->stub_templates_
[stub_type
]);
644 // Constructor and destructor are protected since we only return a single
645 // instance created in Stub_factory::get_instance().
649 // A Stub_factory may not be copied since it is a singleton.
650 Stub_factory(const Stub_factory
&);
651 Stub_factory
& operator=(Stub_factory
&);
653 // Stub templates. These are initialized in the constructor.
654 const Stub_template
* stub_templates_
[arm_stub_type_last
+1];
657 // A class to hold stubs for the ARM target.
659 template<bool big_endian
>
660 class Stub_table
: public Output_data
663 Stub_table(Arm_input_section
<big_endian
>* own
)
664 : Output_data(), addralign_(1), owner_(own
), has_been_changed_(false),
671 // Owner of this stub table.
672 Arm_input_section
<big_endian
>*
674 { return this->owner_
; }
676 // Whether this stub table is empty.
679 { return this->reloc_stubs_
.empty(); }
681 // Whether this has been changed.
683 has_been_changed() const
684 { return this->has_been_changed_
; }
686 // Set the has-been-changed flag.
688 set_has_been_changed(bool value
)
689 { this->has_been_changed_
= value
; }
691 // Return the current data size.
693 current_data_size() const
694 { return this->current_data_size_for_child(); }
696 // Add a STUB with using KEY. Caller is reponsible for avoid adding
697 // if already a STUB with the same key has been added.
699 add_reloc_stub(Reloc_stub
* stub
, const Reloc_stub::Key
& key
);
701 // Look up a relocation stub using KEY. Return NULL if there is none.
703 find_reloc_stub(const Reloc_stub::Key
& key
) const
705 typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.find(key
);
706 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
709 // Relocate stubs in this stub table.
711 relocate_stubs(const Relocate_info
<32, big_endian
>*,
712 Target_arm
<big_endian
>*, Output_section
*,
713 unsigned char*, Arm_address
, section_size_type
);
716 // Write out section contents.
718 do_write(Output_file
*);
720 // Return the required alignment.
723 { return this->addralign_
; }
725 // Finalize data size.
727 set_final_data_size()
728 { this->set_data_size(this->current_data_size_for_child()); }
730 // Reset address and file offset.
732 do_reset_address_and_file_offset();
735 // Unordered map of stubs.
737 Unordered_map
<Reloc_stub::Key
, Reloc_stub
*, Reloc_stub::Key::hash
,
738 Reloc_stub::Key::equal_to
>
743 // Owner of this stub table.
744 Arm_input_section
<big_endian
>* owner_
;
745 // This is set to true during relaxiong if the size of the stub table
747 bool has_been_changed_
;
748 // The relocation stubs.
749 Reloc_stub_map reloc_stubs_
;
752 // A class to wrap an ordinary input section containing executable code.
754 template<bool big_endian
>
755 class Arm_input_section
: public Output_relaxed_input_section
758 Arm_input_section(Relobj
* rel_obj
, unsigned int sec_shndx
)
759 : Output_relaxed_input_section(rel_obj
, sec_shndx
, 1),
760 original_addralign_(1), original_size_(0), stub_table_(NULL
)
770 // Whether this is a stub table owner.
772 is_stub_table_owner() const
773 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
775 // Return the stub table.
776 Stub_table
<big_endian
>*
778 { return this->stub_table_
; }
780 // Set the stub_table.
782 set_stub_table(Stub_table
<big_endian
>* stubtable
)
783 { this->stub_table_
= stubtable
; }
785 // Downcast a base pointer to an Arm_input_section pointer. This is
786 // not type-safe but we only use Arm_input_section not the base class.
787 static Arm_input_section
<big_endian
>*
788 as_arm_input_section(Output_relaxed_input_section
* poris
)
789 { return static_cast<Arm_input_section
<big_endian
>*>(poris
); }
792 // Write data to output file.
794 do_write(Output_file
*);
796 // Return required alignment of this.
800 if (this->is_stub_table_owner())
801 return std::max(this->stub_table_
->addralign(),
802 this->original_addralign_
);
804 return this->original_addralign_
;
807 // Finalize data size.
809 set_final_data_size();
811 // Reset address and file offset.
813 do_reset_address_and_file_offset();
817 do_output_offset(const Relobj
* object
, unsigned int sec_shndx
,
818 section_offset_type off
,
819 section_offset_type
* poutput
) const
821 if ((object
== this->relobj())
822 && (sec_shndx
== this->shndx())
824 && (convert_types
<uint64_t, section_offset_type
>(off
)
825 <= this->original_size_
))
835 // Copying is not allowed.
836 Arm_input_section(const Arm_input_section
&);
837 Arm_input_section
& operator=(const Arm_input_section
&);
839 // Address alignment of the original input section.
840 uint64_t original_addralign_
;
841 // Section size of the original input section.
842 uint64_t original_size_
;
844 Stub_table
<big_endian
>* stub_table_
;
847 // Arm output section class. This is defined mainly to add a number of
848 // stub generation methods.
850 template<bool big_endian
>
851 class Arm_output_section
: public Output_section
854 Arm_output_section(const char* aname
, elfcpp::Elf_Word atype
,
855 elfcpp::Elf_Xword xflags
)
856 : Output_section(aname
, atype
, xflags
)
859 ~Arm_output_section()
862 // Group input sections for stub generation.
864 group_sections(section_size_type
, bool, Target_arm
<big_endian
>*);
866 // Downcast a base pointer to an Arm_output_section pointer. This is
867 // not type-safe but we only use Arm_output_section not the base class.
868 static Arm_output_section
<big_endian
>*
869 as_arm_output_section(Output_section
* os
)
870 { return static_cast<Arm_output_section
<big_endian
>*>(os
); }
874 typedef Output_section::Input_section Input_section
;
875 typedef Output_section::Input_section_list Input_section_list
;
877 // Create a stub group.
878 void create_stub_group(Input_section_list::const_iterator
,
879 Input_section_list::const_iterator
,
880 Input_section_list::const_iterator
,
881 Target_arm
<big_endian
>*,
882 std::vector
<Output_relaxed_input_section
*>*);
887 template<bool big_endian
>
888 class Arm_relobj
: public Sized_relobj
<32, big_endian
>
891 static const Arm_address invalid_address
= static_cast<Arm_address
>(-1);
893 Arm_relobj(const std::string
& aname
, Input_file
* inputfile
, off_t off
,
894 const typename
elfcpp::Ehdr
<32, big_endian
>& ehdr
)
895 : Sized_relobj
<32, big_endian
>(aname
, inputfile
, off
, ehdr
),
896 stub_tables_(), local_symbol_is_thumb_function_(),
897 attributes_section_data_(NULL
)
901 { delete this->attributes_section_data_
; }
903 // Return the stub table of the SHNDX-th section if there is one.
904 Stub_table
<big_endian
>*
905 stub_table(unsigned int sec_shndx
) const
907 gold_assert(sec_shndx
< this->stub_tables_
.size());
908 return this->stub_tables_
[sec_shndx
];
911 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
913 set_stub_table(unsigned int sec_shndx
, Stub_table
<big_endian
>* stubtable
)
915 gold_assert(sec_shndx
< this->stub_tables_
.size());
916 this->stub_tables_
[sec_shndx
] = stubtable
;
919 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
920 // index. This is only valid after do_count_local_symbol is called.
922 local_symbol_is_thumb_function(unsigned int r_sym
) const
924 gold_assert(r_sym
< this->local_symbol_is_thumb_function_
.size());
925 return this->local_symbol_is_thumb_function_
[r_sym
];
928 // Scan all relocation sections for stub generation.
930 scan_sections_for_stubs(Target_arm
<big_endian
>*, const Symbol_table
*,
933 // Convert regular input section with index SHNDX to a relaxed section.
935 convert_input_section_to_relaxed_section(unsigned sec_shndx
)
937 // The stubs have relocations and we need to process them after writing
938 // out the stubs. So relocation now must follow section write.
939 this->invalidate_section_offset(sec_shndx
);
940 this->set_relocs_must_follow_section_writes();
943 // Downcast a base pointer to an Arm_relobj pointer. This is
944 // not type-safe but we only use Arm_relobj not the base class.
945 static Arm_relobj
<big_endian
>*
946 as_arm_relobj(Relobj
* rel_obj
)
947 { return static_cast<Arm_relobj
<big_endian
>*>(rel_obj
); }
949 // Processor-specific flags in ELF file header. This is valid only after
952 processor_specific_flags() const
953 { return this->processor_specific_flags_
; }
955 // Attribute section data This is the contents of the .ARM.attribute section
957 const Attributes_section_data
*
958 attributes_section_data() const
959 { return this->attributes_section_data_
; }
962 // Post constructor setup.
966 // Call parent's setup method.
967 Sized_relobj
<32, big_endian
>::do_setup();
969 // Initialize look-up tables.
970 Stub_table_list
empty_stub_table_list(this->shnum(), NULL
);
971 this->stub_tables_
.swap(empty_stub_table_list
);
974 // Count the local symbols.
976 do_count_local_symbols(Stringpool_template
<char>*,
977 Stringpool_template
<char>*);
980 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
981 const unsigned char* pshdrs
,
982 typename Sized_relobj
<32, big_endian
>::Views
* pivews
);
984 // Read the symbol information.
986 do_read_symbols(Read_symbols_data
* sd
);
989 // List of stub tables.
990 typedef std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
991 Stub_table_list stub_tables_
;
992 // Bit vector to tell if a local symbol is a thumb function or not.
993 // This is only valid after do_count_local_symbol is called.
994 std::vector
<bool> local_symbol_is_thumb_function_
;
995 // processor-specific flags in ELF file header.
996 elfcpp::Elf_Word processor_specific_flags_
;
997 // Object attributes if there is an .ARM.attributes section or NULL.
998 Attributes_section_data
* attributes_section_data_
;
1001 // Arm_dynobj class.
1003 template<bool big_endian
>
1004 class Arm_dynobj
: public Sized_dynobj
<32, big_endian
>
1007 Arm_dynobj(const std::string
& aname
, Input_file
* inputfile
, off_t off
,
1008 const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
1009 : Sized_dynobj
<32, big_endian
>(aname
, inputfile
, off
, ehdr
),
1010 processor_specific_flags_(0)
1014 { delete this->attributes_section_data_
; }
1016 // Downcast a base pointer to an Arm_relobj pointer. This is
1017 // not type-safe but we only use Arm_relobj not the base class.
1018 static Arm_dynobj
<big_endian
>*
1019 as_arm_dynobj(Dynobj
* dynobj
)
1020 { return static_cast<Arm_dynobj
<big_endian
>*>(dynobj
); }
1022 // Processor-specific flags in ELF file header. This is valid only after
1025 processor_specific_flags() const
1026 { return this->processor_specific_flags_
; }
1028 // Attributes section data.
1029 const Attributes_section_data
*
1030 attributes_section_data() const
1031 { return this->attributes_section_data_
; }
1034 // Read the symbol information.
1036 do_read_symbols(Read_symbols_data
* sd
);
1039 // processor-specific flags in ELF file header.
1040 elfcpp::Elf_Word processor_specific_flags_
;
1041 // Object attributes if there is an .ARM.attributes section or NULL.
1042 Attributes_section_data
* attributes_section_data_
;
1045 // Functor to read reloc addends during stub generation.
1047 template<int sh_type
, bool big_endian
>
1048 struct Stub_addend_reader
1050 // Return the addend for a relocation of a particular type. Depending
1051 // on whether this is a REL or RELA relocation, read the addend from a
1052 // view or from a Reloc object.
1053 elfcpp::Elf_types
<32>::Elf_Swxword
1055 unsigned int /* r_type */,
1056 const unsigned char* /* view */,
1057 const typename Reloc_types
<sh_type
,
1058 32, big_endian
>::Reloc
& /* reloc */) const;
1061 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1063 template<bool big_endian
>
1064 struct Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>
1066 elfcpp::Elf_types
<32>::Elf_Swxword
1069 const unsigned char*,
1070 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const;
1073 // Specialized Stub_addend_reader for RELA type relocation sections.
1074 // We currently do not handle RELA type relocation sections but it is trivial
1075 // to implement the addend reader. This is provided for completeness and to
1076 // make it easier to add support for RELA relocation sections in the future.
1078 template<bool big_endian
>
1079 struct Stub_addend_reader
<elfcpp::SHT_RELA
, big_endian
>
1081 elfcpp::Elf_types
<32>::Elf_Swxword
1084 const unsigned char*,
1085 const typename Reloc_types
<elfcpp::SHT_RELA
, 32,
1086 big_endian
>::Reloc
& reloc
) const
1087 { return reloc
.get_r_addend(); }
1090 // Utilities for manipulating integers of up to 32-bits
1094 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1095 // an int32_t. NO_BITS must be between 1 to 32.
1096 template<int no_bits
>
1097 static inline int32_t
1098 sign_extend(uint32_t bits
)
1100 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1102 return static_cast<int32_t>(bits
);
1103 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
1105 uint32_t top_bit
= 1U << (no_bits
- 1);
1106 int32_t as_signed
= static_cast<int32_t>(bits
);
1107 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
1110 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1111 template<int no_bits
>
1113 has_overflow(uint32_t bits
)
1115 gold_assert(no_bits
>= 0 && no_bits
<= 32);
1118 int32_t max
= (1 << (no_bits
- 1)) - 1;
1119 int32_t min
= -(1 << (no_bits
- 1));
1120 int32_t as_signed
= static_cast<int32_t>(bits
);
1121 return as_signed
> max
|| as_signed
< min
;
1124 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1125 // fits in the given number of bits as either a signed or unsigned value.
1126 // For example, has_signed_unsigned_overflow<8> would check
1127 // -128 <= bits <= 255
1128 template<int no_bits
>
1130 has_signed_unsigned_overflow(uint32_t bits
)
1132 gold_assert(no_bits
>= 2 && no_bits
<= 32);
1135 int32_t max
= static_cast<int32_t>((1U << no_bits
) - 1);
1136 int32_t min
= -(1 << (no_bits
- 1));
1137 int32_t as_signed
= static_cast<int32_t>(bits
);
1138 return as_signed
> max
|| as_signed
< min
;
1141 // Select bits from A and B using bits in MASK. For each n in [0..31],
1142 // the n-th bit in the result is chosen from the n-th bits of A and B.
1143 // A zero selects A and a one selects B.
1144 static inline uint32_t
1145 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
1146 { return (a
& ~mask
) | (b
& mask
); }
1149 template<bool big_endian
>
1150 class Target_arm
: public Sized_target
<32, big_endian
>
1153 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
1156 // When were are relocating a stub, we pass this as the relocation number.
1157 static const size_t fake_relnum_for_stubs
= static_cast<size_t>(-1);
1160 : Sized_target
<32, big_endian
>(&arm_info
),
1161 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
1162 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
), stub_tables_(),
1163 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1164 should_force_pic_veneer_(false), arm_input_section_map_(),
1165 attributes_section_data_(NULL
)
1168 // Whether we can use BLX.
1171 { return this->may_use_blx_
; }
1173 // Set use-BLX flag.
1175 set_may_use_blx(bool value
)
1176 { this->may_use_blx_
= value
; }
1178 // Whether we force PCI branch veneers.
1180 should_force_pic_veneer() const
1181 { return this->should_force_pic_veneer_
; }
1183 // Set PIC veneer flag.
1185 set_should_force_pic_veneer(bool value
)
1186 { this->should_force_pic_veneer_
= value
; }
1188 // Whether we use THUMB-2 instructions.
1190 using_thumb2() const
1192 Object_attribute
* attr
=
1193 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1194 int arch
= attr
->int_value();
1195 return arch
== elfcpp::TAG_CPU_ARCH_V6T2
|| arch
>= elfcpp::TAG_CPU_ARCH_V7
;
1198 // Whether we use THUMB/THUMB-2 instructions only.
1200 using_thumb_only() const
1202 Object_attribute
* attr
=
1203 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1204 if (attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7
1205 && attr
->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M
)
1207 attr
= this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile
);
1208 return attr
->int_value() == 'M';
1211 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1213 may_use_arm_nop() const
1215 Object_attribute
* attr
=
1216 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1217 int arch
= attr
->int_value();
1218 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1219 || arch
== elfcpp::TAG_CPU_ARCH_V6K
1220 || arch
== elfcpp::TAG_CPU_ARCH_V7
1221 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1224 // Whether we have THUMB-2 NOP.W instruction.
1226 may_use_thumb2_nop() const
1228 Object_attribute
* attr
=
1229 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
1230 int arch
= attr
->int_value();
1231 return (arch
== elfcpp::TAG_CPU_ARCH_V6T2
1232 || arch
== elfcpp::TAG_CPU_ARCH_V7
1233 || arch
== elfcpp::TAG_CPU_ARCH_V7E_M
);
1236 // Process the relocations to determine unreferenced sections for
1237 // garbage collection.
1239 gc_process_relocs(Symbol_table
* symtab
,
1241 Sized_relobj
<32, big_endian
>* object
,
1242 unsigned int data_shndx
,
1243 unsigned int sh_type
,
1244 const unsigned char* prelocs
,
1246 Output_section
* output_section
,
1247 bool needs_special_offset_handling
,
1248 size_t local_symbol_count
,
1249 const unsigned char* plocal_symbols
);
1251 // Scan the relocations to look for symbol adjustments.
1253 scan_relocs(Symbol_table
* symtab
,
1255 Sized_relobj
<32, big_endian
>* object
,
1256 unsigned int data_shndx
,
1257 unsigned int sh_type
,
1258 const unsigned char* prelocs
,
1260 Output_section
* output_section
,
1261 bool needs_special_offset_handling
,
1262 size_t local_symbol_count
,
1263 const unsigned char* plocal_symbols
);
1265 // Finalize the sections.
1267 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
1269 // Return the value to use for a dynamic symbol which requires special
1272 do_dynsym_value(const Symbol
*) const;
1274 // Relocate a section.
1276 relocate_section(const Relocate_info
<32, big_endian
>*,
1277 unsigned int sh_type
,
1278 const unsigned char* prelocs
,
1280 Output_section
* output_section
,
1281 bool needs_special_offset_handling
,
1282 unsigned char* view
,
1283 Arm_address view_address
,
1284 section_size_type view_size
,
1285 const Reloc_symbol_changes
*);
1287 // Scan the relocs during a relocatable link.
1289 scan_relocatable_relocs(Symbol_table
* symtab
,
1291 Sized_relobj
<32, big_endian
>* object
,
1292 unsigned int data_shndx
,
1293 unsigned int sh_type
,
1294 const unsigned char* prelocs
,
1296 Output_section
* output_section
,
1297 bool needs_special_offset_handling
,
1298 size_t local_symbol_count
,
1299 const unsigned char* plocal_symbols
,
1300 Relocatable_relocs
*);
1302 // Relocate a section during a relocatable link.
1304 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
1305 unsigned int sh_type
,
1306 const unsigned char* prelocs
,
1308 Output_section
* output_section
,
1309 off_t offset_in_output_section
,
1310 const Relocatable_relocs
*,
1311 unsigned char* view
,
1312 Arm_address view_address
,
1313 section_size_type view_size
,
1314 unsigned char* reloc_view
,
1315 section_size_type reloc_view_size
);
1317 // Return whether SYM is defined by the ABI.
1319 do_is_defined_by_abi(Symbol
* sym
) const
1320 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
1322 // Return the size of the GOT section.
1326 gold_assert(this->got_
!= NULL
);
1327 return this->got_
->data_size();
1330 // Map platform-specific reloc types
1332 get_real_reloc_type (unsigned int r_type
);
1335 // Methods to support stub-generations.
1338 // Return the stub factory
1340 stub_factory() const
1341 { return this->stub_factory_
; }
1343 // Make a new Arm_input_section object.
1344 Arm_input_section
<big_endian
>*
1345 new_arm_input_section(Relobj
*, unsigned int);
1347 // Find the Arm_input_section object corresponding to the SHNDX-th input
1348 // section of RELOBJ.
1349 Arm_input_section
<big_endian
>*
1350 find_arm_input_section(Relobj
* rel_obj
, unsigned int sec_shndx
) const;
1352 // Make a new Stub_table
1353 Stub_table
<big_endian
>*
1354 new_stub_table(Arm_input_section
<big_endian
>*);
1356 // Scan a section for stub generation.
1358 scan_section_for_stubs(const Relocate_info
<32, big_endian
>*, unsigned int,
1359 const unsigned char*, size_t, Output_section
*,
1360 bool, const unsigned char*, Arm_address
,
1365 relocate_stub(Reloc_stub
*, const Relocate_info
<32, big_endian
>*,
1366 Output_section
*, unsigned char*, Arm_address
,
1369 // Get the default ARM target.
1370 static Target_arm
<big_endian
>*
1373 gold_assert(parameters
->target().machine_code() == elfcpp::EM_ARM
1374 && parameters
->target().is_big_endian() == big_endian
);
1375 return static_cast<Target_arm
<big_endian
>*>(
1376 parameters
->sized_target
<32, big_endian
>());
1379 // Whether relocation type uses LSB to distinguish THUMB addresses.
1381 reloc_uses_thumb_bit(unsigned int r_type
);
1384 // Make an ELF object.
1386 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1387 const elfcpp::Ehdr
<32, big_endian
>& ehdr
);
1390 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1391 const elfcpp::Ehdr
<32, !big_endian
>&)
1392 { gold_unreachable(); }
1395 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1396 const elfcpp::Ehdr
<64, false>&)
1397 { gold_unreachable(); }
1400 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
1401 const elfcpp::Ehdr
<64, true>&)
1402 { gold_unreachable(); }
1404 // Make an output section.
1406 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
1407 elfcpp::Elf_Xword flags
)
1408 { return new Arm_output_section
<big_endian
>(name
, type
, flags
); }
1411 do_adjust_elf_header(unsigned char* view
, int len
) const;
1413 // We only need to generate stubs, and hence perform relaxation if we are
1414 // not doing relocatable linking.
1416 do_may_relax() const
1417 { return !parameters
->options().relocatable(); }
1420 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*);
1422 // Determine whether an object attribute tag takes an integer, a
1425 do_attribute_arg_type(int tag
) const;
1427 // Reorder tags during output.
1429 do_attributes_order(int num
) const;
1432 // The class which scans relocations.
1437 : issued_non_pic_error_(false)
1441 local(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1442 Sized_relobj
<32, big_endian
>* object
,
1443 unsigned int data_shndx
,
1444 Output_section
* output_section
,
1445 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1446 const elfcpp::Sym
<32, big_endian
>& lsym
);
1449 global(Symbol_table
* symtab
, Layout
* layout
, Target_arm
* target
,
1450 Sized_relobj
<32, big_endian
>* object
,
1451 unsigned int data_shndx
,
1452 Output_section
* output_section
,
1453 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
1458 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
1459 unsigned int r_type
);
1462 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
1463 unsigned int r_type
, Symbol
*);
1466 check_non_pic(Relobj
*, unsigned int r_type
);
1468 // Almost identical to Symbol::needs_plt_entry except that it also
1469 // handles STT_ARM_TFUNC.
1471 symbol_needs_plt_entry(const Symbol
* sym
)
1473 // An undefined symbol from an executable does not need a PLT entry.
1474 if (sym
->is_undefined() && !parameters
->options().shared())
1477 return (!parameters
->doing_static_link()
1478 && (sym
->type() == elfcpp::STT_FUNC
1479 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
1480 && (sym
->is_from_dynobj()
1481 || sym
->is_undefined()
1482 || sym
->is_preemptible()));
1485 // Whether we have issued an error about a non-PIC compilation.
1486 bool issued_non_pic_error_
;
1489 // The class which implements relocation.
1499 // Return whether the static relocation needs to be applied.
1501 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1504 Output_section
* output_section
);
1506 // Do a relocation. Return false if the caller should not issue
1507 // any warnings about this relocation.
1509 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
1510 Output_section
*, size_t relnum
,
1511 const elfcpp::Rel
<32, big_endian
>&,
1512 unsigned int r_type
, const Sized_symbol
<32>*,
1513 const Symbol_value
<32>*,
1514 unsigned char*, Arm_address
,
1517 // Return whether we want to pass flag NON_PIC_REF for this
1518 // reloc. This means the relocation type accesses a symbol not via
1521 reloc_is_non_pic (unsigned int r_type
)
1525 // These relocation types reference GOT or PLT entries explicitly.
1526 case elfcpp::R_ARM_GOT_BREL
:
1527 case elfcpp::R_ARM_GOT_ABS
:
1528 case elfcpp::R_ARM_GOT_PREL
:
1529 case elfcpp::R_ARM_GOT_BREL12
:
1530 case elfcpp::R_ARM_PLT32_ABS
:
1531 case elfcpp::R_ARM_TLS_GD32
:
1532 case elfcpp::R_ARM_TLS_LDM32
:
1533 case elfcpp::R_ARM_TLS_IE32
:
1534 case elfcpp::R_ARM_TLS_IE12GP
:
1536 // These relocate types may use PLT entries.
1537 case elfcpp::R_ARM_CALL
:
1538 case elfcpp::R_ARM_THM_CALL
:
1539 case elfcpp::R_ARM_JUMP24
:
1540 case elfcpp::R_ARM_THM_JUMP24
:
1541 case elfcpp::R_ARM_THM_JUMP19
:
1542 case elfcpp::R_ARM_PLT32
:
1543 case elfcpp::R_ARM_THM_XPC22
:
1552 // A class which returns the size required for a relocation type,
1553 // used while scanning relocs during a relocatable link.
1554 class Relocatable_size_for_reloc
1558 get_size_for_reloc(unsigned int, Relobj
*);
1561 // Get the GOT section, creating it if necessary.
1562 Output_data_got
<32, big_endian
>*
1563 got_section(Symbol_table
*, Layout
*);
1565 // Get the GOT PLT section.
1567 got_plt_section() const
1569 gold_assert(this->got_plt_
!= NULL
);
1570 return this->got_plt_
;
1573 // Create a PLT entry for a global symbol.
1575 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1577 // Get the PLT section.
1578 const Output_data_plt_arm
<big_endian
>*
1581 gold_assert(this->plt_
!= NULL
);
1585 // Get the dynamic reloc section, creating it if necessary.
1587 rel_dyn_section(Layout
*);
1589 // Return true if the symbol may need a COPY relocation.
1590 // References from an executable object to non-function symbols
1591 // defined in a dynamic object may need a COPY relocation.
1593 may_need_copy_reloc(Symbol
* gsym
)
1595 return (gsym
->type() != elfcpp::STT_ARM_TFUNC
1596 && gsym
->may_need_copy_reloc());
1599 // Add a potential copy relocation.
1601 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1602 Sized_relobj
<32, big_endian
>* object
,
1603 unsigned int sec_shndx
, Output_section
* output_section
,
1604 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
1606 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1607 symtab
->get_sized_symbol
<32>(sym
),
1608 object
, sec_shndx
, output_section
, reloc
,
1609 this->rel_dyn_section(layout
));
1612 // Whether two EABI versions are compatible.
1614 are_eabi_versions_compatible(elfcpp::Elf_Word v1
, elfcpp::Elf_Word v2
);
1616 // Merge processor-specific flags from input object and those in the ELF
1617 // header of the output.
1619 merge_processor_specific_flags(const std::string
&, elfcpp::Elf_Word
);
1621 // Get the secondary compatible architecture.
1623 get_secondary_compatible_arch(const Attributes_section_data
*);
1625 // Set the secondary compatible architecture.
1627 set_secondary_compatible_arch(Attributes_section_data
*, int);
1630 tag_cpu_arch_combine(const char*, int, int*, int, int);
1632 // Helper to print AEABI enum tag value.
1634 aeabi_enum_name(unsigned int);
1636 // Return string value for TAG_CPU_name.
1638 tag_cpu_name_value(unsigned int);
1640 // Merge object attributes from input object and those in the output.
1642 merge_object_attributes(const char*, const Attributes_section_data
*);
1644 // Helper to get an AEABI object attribute
1646 get_aeabi_object_attribute(int tag
) const
1648 Attributes_section_data
* pasd
= this->attributes_section_data_
;
1649 gold_assert(pasd
!= NULL
);
1650 Object_attribute
* attr
=
1651 pasd
->get_attribute(Object_attribute::OBJ_ATTR_PROC
, tag
);
1652 gold_assert(attr
!= NULL
);
1657 // Methods to support stub-generations.
1660 // Group input sections for stub generation.
1662 group_sections(Layout
*, section_size_type
, bool);
1664 // Scan a relocation for stub generation.
1666 scan_reloc_for_stub(const Relocate_info
<32, big_endian
>*, unsigned int,
1667 const Sized_symbol
<32>*, unsigned int,
1668 const Symbol_value
<32>*,
1669 elfcpp::Elf_types
<32>::Elf_Swxword
, Arm_address
);
1671 // Scan a relocation section for stub.
1672 template<int sh_type
>
1674 scan_reloc_section_for_stubs(
1675 const Relocate_info
<32, big_endian
>* relinfo
,
1676 const unsigned char* prelocs
,
1678 Output_section
* output_section
,
1679 bool needs_special_offset_handling
,
1680 const unsigned char* view
,
1681 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1684 // Information about this specific target which we pass to the
1685 // general Target structure.
1686 static const Target::Target_info arm_info
;
1688 // The types of GOT entries needed for this platform.
1691 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
1694 typedef typename
std::vector
<Stub_table
<big_endian
>*> Stub_table_list
;
1696 // Map input section to Arm_input_section.
1697 typedef Unordered_map
<Input_section_specifier
,
1698 Arm_input_section
<big_endian
>*,
1699 Input_section_specifier::hash
,
1700 Input_section_specifier::equal_to
>
1701 Arm_input_section_map
;
1704 Output_data_got
<32, big_endian
>* got_
;
1706 Output_data_plt_arm
<big_endian
>* plt_
;
1707 // The GOT PLT section.
1708 Output_data_space
* got_plt_
;
1709 // The dynamic reloc section.
1710 Reloc_section
* rel_dyn_
;
1711 // Relocs saved to avoid a COPY reloc.
1712 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
1713 // Space for variables copied with a COPY reloc.
1714 Output_data_space
* dynbss_
;
1715 // Vector of Stub_tables created.
1716 Stub_table_list stub_tables_
;
1718 const Stub_factory
&stub_factory_
;
1719 // Whether we can use BLX.
1721 // Whether we force PIC branch veneers.
1722 bool should_force_pic_veneer_
;
1723 // Map for locating Arm_input_sections.
1724 Arm_input_section_map arm_input_section_map_
;
1725 // Attributes section data in output.
1726 Attributes_section_data
* attributes_section_data_
;
1729 template<bool big_endian
>
1730 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
1733 big_endian
, // is_big_endian
1734 elfcpp::EM_ARM
, // machine_code
1735 false, // has_make_symbol
1736 false, // has_resolve
1737 false, // has_code_fill
1738 true, // is_default_stack_executable
1740 "/usr/lib/libc.so.1", // dynamic_linker
1741 0x8000, // default_text_segment_address
1742 0x1000, // abi_pagesize (overridable by -z max-page-size)
1743 0x1000, // common_pagesize (overridable by -z common-page-size)
1744 elfcpp::SHN_UNDEF
, // small_common_shndx
1745 elfcpp::SHN_UNDEF
, // large_common_shndx
1746 0, // small_common_section_flags
1747 0, // large_common_section_flags
1748 ".ARM.attributes", // attributes_section
1749 "aeabi" // attributes_vendor
1752 // Arm relocate functions class
1755 template<bool big_endian
>
1756 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
1761 STATUS_OKAY
, // No error during relocation.
1762 STATUS_OVERFLOW
, // Relocation oveflow.
1763 STATUS_BAD_RELOC
// Relocation cannot be applied.
1767 typedef Relocate_functions
<32, big_endian
> Base
;
1768 typedef Arm_relocate_functions
<big_endian
> This
;
1770 // Encoding of imm16 argument for movt and movw ARM instructions
1773 // imm16 := imm4 | imm12
1775 // 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
1776 // +-------+---------------+-------+-------+-----------------------+
1777 // | | |imm4 | |imm12 |
1778 // +-------+---------------+-------+-------+-----------------------+
1780 // Extract the relocation addend from VAL based on the ARM
1781 // instruction encoding described above.
1782 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1783 extract_arm_movw_movt_addend(
1784 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1786 // According to the Elf ABI for ARM Architecture the immediate
1787 // field is sign-extended to form the addend.
1788 return utils::sign_extend
<16>(((val
>> 4) & 0xf000) | (val
& 0xfff));
1791 // Insert X into VAL based on the ARM instruction encoding described
1793 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1794 insert_val_arm_movw_movt(
1795 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1796 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1800 val
|= (x
& 0xf000) << 4;
1804 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1807 // imm16 := imm4 | i | imm3 | imm8
1809 // 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
1810 // +---------+-+-----------+-------++-+-----+-------+---------------+
1811 // | |i| |imm4 || |imm3 | |imm8 |
1812 // +---------+-+-----------+-------++-+-----+-------+---------------+
1814 // Extract the relocation addend from VAL based on the Thumb2
1815 // instruction encoding described above.
1816 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1817 extract_thumb_movw_movt_addend(
1818 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
)
1820 // According to the Elf ABI for ARM Architecture the immediate
1821 // field is sign-extended to form the addend.
1822 return utils::sign_extend
<16>(((val
>> 4) & 0xf000)
1823 | ((val
>> 15) & 0x0800)
1824 | ((val
>> 4) & 0x0700)
1828 // Insert X into VAL based on the Thumb2 instruction encoding
1830 static inline typename
elfcpp::Swap
<32, big_endian
>::Valtype
1831 insert_val_thumb_movw_movt(
1832 typename
elfcpp::Swap
<32, big_endian
>::Valtype val
,
1833 typename
elfcpp::Swap
<32, big_endian
>::Valtype x
)
1836 val
|= (x
& 0xf000) << 4;
1837 val
|= (x
& 0x0800) << 15;
1838 val
|= (x
& 0x0700) << 4;
1839 val
|= (x
& 0x00ff);
1843 // Handle ARM long branches.
1844 static typename
This::Status
1845 arm_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1846 unsigned char *, const Sized_symbol
<32>*,
1847 const Arm_relobj
<big_endian
>*, unsigned int,
1848 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1850 // Handle THUMB long branches.
1851 static typename
This::Status
1852 thumb_branch_common(unsigned int, const Relocate_info
<32, big_endian
>*,
1853 unsigned char *, const Sized_symbol
<32>*,
1854 const Arm_relobj
<big_endian
>*, unsigned int,
1855 const Symbol_value
<32>*, Arm_address
, Arm_address
, bool);
1859 // R_ARM_ABS8: S + A
1860 static inline typename
This::Status
1861 abs8(unsigned char *view
,
1862 const Sized_relobj
<32, big_endian
>* object
,
1863 const Symbol_value
<32>* psymval
)
1865 typedef typename
elfcpp::Swap
<8, big_endian
>::Valtype Valtype
;
1866 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1867 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1868 Valtype val
= elfcpp::Swap
<8, big_endian
>::readval(wv
);
1869 Reltype addend
= utils::sign_extend
<8>(val
);
1870 Reltype x
= psymval
->value(object
, addend
);
1871 val
= utils::bit_select(val
, x
, 0xffU
);
1872 elfcpp::Swap
<8, big_endian
>::writeval(wv
, val
);
1873 return (utils::has_signed_unsigned_overflow
<8>(x
)
1874 ? This::STATUS_OVERFLOW
1875 : This::STATUS_OKAY
);
1878 // R_ARM_THM_ABS5: S + A
1879 static inline typename
This::Status
1880 thm_abs5(unsigned char *view
,
1881 const Sized_relobj
<32, big_endian
>* object
,
1882 const Symbol_value
<32>* psymval
)
1884 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1885 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1886 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1887 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1888 Reltype addend
= (val
& 0x7e0U
) >> 6;
1889 Reltype x
= psymval
->value(object
, addend
);
1890 val
= utils::bit_select(val
, x
<< 6, 0x7e0U
);
1891 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1892 return (utils::has_overflow
<5>(x
)
1893 ? This::STATUS_OVERFLOW
1894 : This::STATUS_OKAY
);
1897 // R_ARM_ABS12: S + A
1898 static inline typename
This::Status
1899 abs12(unsigned char *view
,
1900 const Sized_relobj
<32, big_endian
>* object
,
1901 const Symbol_value
<32>* psymval
)
1903 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1904 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1905 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1906 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1907 Reltype addend
= val
& 0x0fffU
;
1908 Reltype x
= psymval
->value(object
, addend
);
1909 val
= utils::bit_select(val
, x
, 0x0fffU
);
1910 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1911 return (utils::has_overflow
<12>(x
)
1912 ? This::STATUS_OVERFLOW
1913 : This::STATUS_OKAY
);
1916 // R_ARM_ABS16: S + A
1917 static inline typename
This::Status
1918 abs16(unsigned char *view
,
1919 const Sized_relobj
<32, big_endian
>* object
,
1920 const Symbol_value
<32>* psymval
)
1922 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
1923 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
1924 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1925 Valtype val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
1926 Reltype addend
= utils::sign_extend
<16>(val
);
1927 Reltype x
= psymval
->value(object
, addend
);
1928 val
= utils::bit_select(val
, x
, 0xffffU
);
1929 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
1930 return (utils::has_signed_unsigned_overflow
<16>(x
)
1931 ? This::STATUS_OVERFLOW
1932 : This::STATUS_OKAY
);
1935 // R_ARM_ABS32: (S + A) | T
1936 static inline typename
This::Status
1937 abs32(unsigned char *view
,
1938 const Sized_relobj
<32, big_endian
>* object
,
1939 const Symbol_value
<32>* psymval
,
1940 Arm_address thumb_bit
)
1942 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1943 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1944 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1945 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
1946 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1947 return This::STATUS_OKAY
;
1950 // R_ARM_REL32: (S + A) | T - P
1951 static inline typename
This::Status
1952 rel32(unsigned char *view
,
1953 const Sized_relobj
<32, big_endian
>* object
,
1954 const Symbol_value
<32>* psymval
,
1955 Arm_address address
,
1956 Arm_address thumb_bit
)
1958 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1959 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1960 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1961 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
1962 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
1963 return This::STATUS_OKAY
;
1966 // R_ARM_THM_CALL: (S + A) | T - P
1967 static inline typename
This::Status
1968 thm_call(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1969 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1970 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1971 Arm_address address
, Arm_address thumb_bit
,
1972 bool is_weakly_undefined_without_plt
)
1974 return thumb_branch_common(elfcpp::R_ARM_THM_CALL
, relinfo
, view
, gsym
,
1975 object
, r_sym
, psymval
, address
, thumb_bit
,
1976 is_weakly_undefined_without_plt
);
1979 // R_ARM_THM_JUMP24: (S + A) | T - P
1980 static inline typename
This::Status
1981 thm_jump24(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1982 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1983 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1984 Arm_address address
, Arm_address thumb_bit
,
1985 bool is_weakly_undefined_without_plt
)
1987 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24
, relinfo
, view
, gsym
,
1988 object
, r_sym
, psymval
, address
, thumb_bit
,
1989 is_weakly_undefined_without_plt
);
1992 // R_ARM_THM_XPC22: (S + A) | T - P
1993 static inline typename
This::Status
1994 thm_xpc22(const Relocate_info
<32, big_endian
>* relinfo
, unsigned char *view
,
1995 const Sized_symbol
<32>* gsym
, const Arm_relobj
<big_endian
>* object
,
1996 unsigned int r_sym
, const Symbol_value
<32>* psymval
,
1997 Arm_address address
, Arm_address thumb_bit
,
1998 bool is_weakly_undefined_without_plt
)
2000 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22
, relinfo
, view
, gsym
,
2001 object
, r_sym
, psymval
, address
, thumb_bit
,
2002 is_weakly_undefined_without_plt
);
2005 // R_ARM_BASE_PREL: B(S) + A - P
2006 static inline typename
This::Status
2007 base_prel(unsigned char* view
,
2009 Arm_address address
)
2011 Base::rel32(view
, origin
- address
);
2015 // R_ARM_BASE_ABS: B(S) + A
2016 static inline typename
This::Status
2017 base_abs(unsigned char* view
,
2020 Base::rel32(view
, origin
);
2024 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2025 static inline typename
This::Status
2026 got_brel(unsigned char* view
,
2027 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
2029 Base::rel32(view
, got_offset
);
2030 return This::STATUS_OKAY
;
2033 // R_ARM_GOT_PREL: GOT(S) + A - P
2034 static inline typename
This::Status
2035 got_prel(unsigned char *view
,
2036 Arm_address got_entry
,
2037 Arm_address address
)
2039 Base::rel32(view
, got_entry
- address
);
2040 return This::STATUS_OKAY
;
2043 // R_ARM_PLT32: (S + A) | T - P
2044 static inline typename
This::Status
2045 plt32(const Relocate_info
<32, big_endian
>* relinfo
,
2046 unsigned char *view
,
2047 const Sized_symbol
<32>* gsym
,
2048 const Arm_relobj
<big_endian
>* object
,
2050 const Symbol_value
<32>* psymval
,
2051 Arm_address address
,
2052 Arm_address thumb_bit
,
2053 bool is_weakly_undefined_without_plt
)
2055 return arm_branch_common(elfcpp::R_ARM_PLT32
, relinfo
, view
, gsym
,
2056 object
, r_sym
, psymval
, address
, thumb_bit
,
2057 is_weakly_undefined_without_plt
);
2060 // R_ARM_XPC25: (S + A) | T - P
2061 static inline typename
This::Status
2062 xpc25(const Relocate_info
<32, big_endian
>* relinfo
,
2063 unsigned char *view
,
2064 const Sized_symbol
<32>* gsym
,
2065 const Arm_relobj
<big_endian
>* object
,
2067 const Symbol_value
<32>* psymval
,
2068 Arm_address address
,
2069 Arm_address thumb_bit
,
2070 bool is_weakly_undefined_without_plt
)
2072 return arm_branch_common(elfcpp::R_ARM_XPC25
, relinfo
, view
, gsym
,
2073 object
, r_sym
, psymval
, address
, thumb_bit
,
2074 is_weakly_undefined_without_plt
);
2077 // R_ARM_CALL: (S + A) | T - P
2078 static inline typename
This::Status
2079 call(const Relocate_info
<32, big_endian
>* relinfo
,
2080 unsigned char *view
,
2081 const Sized_symbol
<32>* gsym
,
2082 const Arm_relobj
<big_endian
>* object
,
2084 const Symbol_value
<32>* psymval
,
2085 Arm_address address
,
2086 Arm_address thumb_bit
,
2087 bool is_weakly_undefined_without_plt
)
2089 return arm_branch_common(elfcpp::R_ARM_CALL
, relinfo
, view
, gsym
,
2090 object
, r_sym
, psymval
, address
, thumb_bit
,
2091 is_weakly_undefined_without_plt
);
2094 // R_ARM_JUMP24: (S + A) | T - P
2095 static inline typename
This::Status
2096 jump24(const Relocate_info
<32, big_endian
>* relinfo
,
2097 unsigned char *view
,
2098 const Sized_symbol
<32>* gsym
,
2099 const Arm_relobj
<big_endian
>* object
,
2101 const Symbol_value
<32>* psymval
,
2102 Arm_address address
,
2103 Arm_address thumb_bit
,
2104 bool is_weakly_undefined_without_plt
)
2106 return arm_branch_common(elfcpp::R_ARM_JUMP24
, relinfo
, view
, gsym
,
2107 object
, r_sym
, psymval
, address
, thumb_bit
,
2108 is_weakly_undefined_without_plt
);
2111 // R_ARM_PREL: (S + A) | T - P
2112 static inline typename
This::Status
2113 prel31(unsigned char *view
,
2114 const Sized_relobj
<32, big_endian
>* object
,
2115 const Symbol_value
<32>* psymval
,
2116 Arm_address address
,
2117 Arm_address thumb_bit
)
2119 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2120 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2121 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2122 Valtype addend
= utils::sign_extend
<31>(val
);
2123 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2124 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
2125 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2126 return (utils::has_overflow
<31>(x
) ?
2127 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2130 // R_ARM_MOVW_ABS_NC: (S + A) | T
2131 static inline typename
This::Status
2132 movw_abs_nc(unsigned char *view
,
2133 const Sized_relobj
<32, big_endian
>* object
,
2134 const Symbol_value
<32>* psymval
,
2135 Arm_address thumb_bit
)
2137 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2138 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2139 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2140 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2141 Valtype x
= psymval
->value(object
, addend
) | thumb_bit
;
2142 val
= This::insert_val_arm_movw_movt(val
, x
);
2143 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2144 return This::STATUS_OKAY
;
2147 // R_ARM_MOVT_ABS: S + A
2148 static inline typename
This::Status
2149 movt_abs(unsigned char *view
,
2150 const Sized_relobj
<32, big_endian
>* object
,
2151 const Symbol_value
<32>* psymval
)
2153 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2154 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2155 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2156 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2157 Valtype x
= psymval
->value(object
, addend
) >> 16;
2158 val
= This::insert_val_arm_movw_movt(val
, x
);
2159 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2160 return This::STATUS_OKAY
;
2163 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2164 static inline typename
This::Status
2165 thm_movw_abs_nc(unsigned char *view
,
2166 const Sized_relobj
<32, big_endian
>* object
,
2167 const Symbol_value
<32>* psymval
,
2168 Arm_address thumb_bit
)
2170 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2171 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2172 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2173 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2174 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2175 Reltype addend
= extract_thumb_movw_movt_addend(val
);
2176 Reltype x
= psymval
->value(object
, addend
) | thumb_bit
;
2177 val
= This::insert_val_thumb_movw_movt(val
, x
);
2178 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2179 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2180 return This::STATUS_OKAY
;
2183 // R_ARM_THM_MOVT_ABS: S + A
2184 static inline typename
This::Status
2185 thm_movt_abs(unsigned char *view
,
2186 const Sized_relobj
<32, big_endian
>* object
,
2187 const Symbol_value
<32>* psymval
)
2189 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2190 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2191 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2192 Reltype val
= ((elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2193 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1));
2194 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2195 Reltype x
= psymval
->value(object
, addend
) >> 16;
2196 val
= This::insert_val_thumb_movw_movt(val
, x
);
2197 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2198 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2199 return This::STATUS_OKAY
;
2202 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2203 static inline typename
This::Status
2204 movw_prel_nc(unsigned char *view
,
2205 const Sized_relobj
<32, big_endian
>* object
,
2206 const Symbol_value
<32>* psymval
,
2207 Arm_address address
,
2208 Arm_address thumb_bit
)
2210 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2211 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2212 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2213 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2214 Valtype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2215 val
= This::insert_val_arm_movw_movt(val
, x
);
2216 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2217 return This::STATUS_OKAY
;
2220 // R_ARM_MOVT_PREL: S + A - P
2221 static inline typename
This::Status
2222 movt_prel(unsigned char *view
,
2223 const Sized_relobj
<32, big_endian
>* object
,
2224 const Symbol_value
<32>* psymval
,
2225 Arm_address address
)
2227 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2228 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2229 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2230 Valtype addend
= This::extract_arm_movw_movt_addend(val
);
2231 Valtype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2232 val
= This::insert_val_arm_movw_movt(val
, x
);
2233 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2234 return This::STATUS_OKAY
;
2237 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2238 static inline typename
This::Status
2239 thm_movw_prel_nc(unsigned char *view
,
2240 const Sized_relobj
<32, big_endian
>* object
,
2241 const Symbol_value
<32>* psymval
,
2242 Arm_address address
,
2243 Arm_address thumb_bit
)
2245 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2246 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2247 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2248 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2249 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2250 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2251 Reltype x
= (psymval
->value(object
, addend
) | thumb_bit
) - address
;
2252 val
= This::insert_val_thumb_movw_movt(val
, x
);
2253 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2254 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2255 return This::STATUS_OKAY
;
2258 // R_ARM_THM_MOVT_PREL: S + A - P
2259 static inline typename
This::Status
2260 thm_movt_prel(unsigned char *view
,
2261 const Sized_relobj
<32, big_endian
>* object
,
2262 const Symbol_value
<32>* psymval
,
2263 Arm_address address
)
2265 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2266 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
2267 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2268 Reltype val
= (elfcpp::Swap
<16, big_endian
>::readval(wv
) << 16)
2269 | elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2270 Reltype addend
= This::extract_thumb_movw_movt_addend(val
);
2271 Reltype x
= (psymval
->value(object
, addend
) - address
) >> 16;
2272 val
= This::insert_val_thumb_movw_movt(val
, x
);
2273 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
>> 16);
2274 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, val
& 0xffff);
2275 return This::STATUS_OKAY
;
2279 // Relocate ARM long branches. This handles relocation types
2280 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2281 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2282 // undefined and we do not use PLT in this relocation. In such a case,
2283 // the branch is converted into an NOP.
2285 template<bool big_endian
>
2286 typename Arm_relocate_functions
<big_endian
>::Status
2287 Arm_relocate_functions
<big_endian
>::arm_branch_common(
2288 unsigned int r_type
,
2289 const Relocate_info
<32, big_endian
>* relinfo
,
2290 unsigned char *view
,
2291 const Sized_symbol
<32>* gsym
,
2292 const Arm_relobj
<big_endian
>* object
,
2294 const Symbol_value
<32>* psymval
,
2295 Arm_address address
,
2296 Arm_address thumb_bit
,
2297 bool is_weakly_undefined_without_plt
)
2299 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2300 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2301 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2303 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
2304 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
2305 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
2306 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
2307 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
2308 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
2309 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
2311 // Check that the instruction is valid.
2312 if (r_type
== elfcpp::R_ARM_CALL
)
2314 if (!insn_is_uncond_bl
&& !insn_is_blx
)
2315 return This::STATUS_BAD_RELOC
;
2317 else if (r_type
== elfcpp::R_ARM_JUMP24
)
2319 if (!insn_is_b
&& !insn_is_cond_bl
)
2320 return This::STATUS_BAD_RELOC
;
2322 else if (r_type
== elfcpp::R_ARM_PLT32
)
2324 if (!insn_is_any_branch
)
2325 return This::STATUS_BAD_RELOC
;
2327 else if (r_type
== elfcpp::R_ARM_XPC25
)
2329 // FIXME: AAELF document IH0044C does not say much about it other
2330 // than it being obsolete.
2331 if (!insn_is_any_branch
)
2332 return This::STATUS_BAD_RELOC
;
2337 // A branch to an undefined weak symbol is turned into a jump to
2338 // the next instruction unless a PLT entry will be created.
2339 // Do the same for local undefined symbols.
2340 // The jump to the next instruction is optimized as a NOP depending
2341 // on the architecture.
2342 const Target_arm
<big_endian
>* arm_target
=
2343 Target_arm
<big_endian
>::default_target();
2344 if (is_weakly_undefined_without_plt
)
2346 Valtype cond
= val
& 0xf0000000U
;
2347 if (arm_target
->may_use_arm_nop())
2348 val
= cond
| 0x0320f000;
2350 val
= cond
| 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2351 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2352 return This::STATUS_OKAY
;
2355 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
2356 Valtype branch_target
= psymval
->value(object
, addend
);
2357 int32_t branch_offset
= branch_target
- address
;
2359 // We need a stub if the branch offset is too large or if we need
2361 bool may_use_blx
= arm_target
->may_use_blx();
2362 Reloc_stub
* stub
= NULL
;
2363 if ((branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
)
2364 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2365 || ((thumb_bit
!= 0) && !(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
)))
2367 Stub_type stub_type
=
2368 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2370 if (stub_type
!= arm_stub_none
)
2372 Stub_table
<big_endian
>* stubtable
=
2373 object
->stub_table(relinfo
->data_shndx
);
2374 gold_assert(stubtable
!= NULL
);
2376 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2377 stub
= stubtable
->find_reloc_stub(stub_key
);
2378 gold_assert(stub
!= NULL
);
2379 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2380 branch_target
= stubtable
->address() + stub
->offset() + addend
;
2381 branch_offset
= branch_target
- address
;
2382 gold_assert((branch_offset
<= ARM_MAX_FWD_BRANCH_OFFSET
)
2383 && (branch_offset
>= ARM_MAX_BWD_BRANCH_OFFSET
));
2387 // At this point, if we still need to switch mode, the instruction
2388 // must either be a BLX or a BL that can be converted to a BLX.
2392 gold_assert(may_use_blx
&& r_type
== elfcpp::R_ARM_CALL
);
2393 val
= (val
& 0xffffff) | 0xfa000000 | ((branch_offset
& 2) << 23);
2396 val
= utils::bit_select(val
, (branch_offset
>> 2), 0xffffffUL
);
2397 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2398 return (utils::has_overflow
<26>(branch_offset
)
2399 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
2402 // Relocate THUMB long branches. This handles relocation types
2403 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2404 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2405 // undefined and we do not use PLT in this relocation. In such a case,
2406 // the branch is converted into an NOP.
2408 template<bool big_endian
>
2409 typename Arm_relocate_functions
<big_endian
>::Status
2410 Arm_relocate_functions
<big_endian
>::thumb_branch_common(
2411 unsigned int r_type
,
2412 const Relocate_info
<32, big_endian
>* relinfo
,
2413 unsigned char *view
,
2414 const Sized_symbol
<32>* gsym
,
2415 const Arm_relobj
<big_endian
>* object
,
2417 const Symbol_value
<32>* psymval
,
2418 Arm_address address
,
2419 Arm_address thumb_bit
,
2420 bool is_weakly_undefined_without_plt
)
2422 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
2423 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2424 uint32_t upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
2425 uint32_t lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
2427 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2429 bool is_bl_insn
= (lower_insn
& 0x1000U
) == 0x1000U
;
2430 bool is_blx_insn
= (lower_insn
& 0x1000U
) == 0x0000U
;
2432 // Check that the instruction is valid.
2433 if (r_type
== elfcpp::R_ARM_THM_CALL
)
2435 if (!is_bl_insn
&& !is_blx_insn
)
2436 return This::STATUS_BAD_RELOC
;
2438 else if (r_type
== elfcpp::R_ARM_THM_JUMP24
)
2440 // This cannot be a BLX.
2442 return This::STATUS_BAD_RELOC
;
2444 else if (r_type
== elfcpp::R_ARM_THM_XPC22
)
2446 // Check for Thumb to Thumb call.
2448 return This::STATUS_BAD_RELOC
;
2451 gold_warning(_("%s: Thumb BLX instruction targets "
2452 "thumb function '%s'."),
2453 object
->name().c_str(),
2454 (gsym
? gsym
->name() : "(local)"));
2455 // Convert BLX to BL.
2456 lower_insn
|= 0x1000U
;
2462 // A branch to an undefined weak symbol is turned into a jump to
2463 // the next instruction unless a PLT entry will be created.
2464 // The jump to the next instruction is optimized as a NOP.W for
2465 // Thumb-2 enabled architectures.
2466 const Target_arm
<big_endian
>* arm_target
=
2467 Target_arm
<big_endian
>::default_target();
2468 if (is_weakly_undefined_without_plt
)
2470 if (arm_target
->may_use_thumb2_nop())
2472 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xf3af);
2473 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0x8000);
2477 elfcpp::Swap
<16, big_endian
>::writeval(wv
, 0xe000);
2478 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, 0xbf00);
2480 return This::STATUS_OKAY
;
2483 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2484 // with Thumb-1) involving the J1 and J2 bits.
2485 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
2486 uint32_t upper
= upper_insn
& 0x3ff;
2487 uint32_t lower
= lower_insn
& 0x7ff;
2488 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
2489 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
2490 uint32_t i1
= j1
^ s
? 0 : 1;
2491 uint32_t i2
= j2
^ s
? 0 : 1;
2493 int32_t addend
= (i1
<< 23) | (i2
<< 22) | (upper
<< 12) | (lower
<< 1);
2495 addend
= (addend
| ((s
? 0 : 1) << 24)) - (1 << 24);
2497 Arm_address branch_target
= psymval
->value(object
, addend
);
2498 int32_t branch_offset
= branch_target
- address
;
2500 // We need a stub if the branch offset is too large or if we need
2502 bool may_use_blx
= arm_target
->may_use_blx();
2503 bool thumb2
= arm_target
->using_thumb2();
2505 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2506 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2508 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2509 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2510 || ((thumb_bit
== 0)
2511 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2512 || r_type
== elfcpp::R_ARM_THM_JUMP24
)))
2514 Stub_type stub_type
=
2515 Reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
,
2517 if (stub_type
!= arm_stub_none
)
2519 Stub_table
<big_endian
>* stubtable
=
2520 object
->stub_table(relinfo
->data_shndx
);
2521 gold_assert(stubtable
!= NULL
);
2523 Reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
2524 Reloc_stub
* stub
= stubtable
->find_reloc_stub(stub_key
);
2525 gold_assert(stub
!= NULL
);
2526 thumb_bit
= stub
->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2527 branch_target
= stubtable
->address() + stub
->offset() + addend
;
2528 branch_offset
= branch_target
- address
;
2532 // At this point, if we still need to switch mode, the instruction
2533 // must either be a BLX or a BL that can be converted to a BLX.
2536 gold_assert(may_use_blx
2537 && (r_type
== elfcpp::R_ARM_THM_CALL
2538 || r_type
== elfcpp::R_ARM_THM_XPC22
));
2539 // Make sure this is a BLX.
2540 lower_insn
&= ~0x1000U
;
2544 // Make sure this is a BL.
2545 lower_insn
|= 0x1000U
;
2548 uint32_t reloc_sign
= (branch_offset
< 0) ? 1 : 0;
2549 uint32_t relocation
= static_cast<uint32_t>(branch_offset
);
2551 if ((lower_insn
& 0x5000U
) == 0x4000U
)
2552 // For a BLX instruction, make sure that the relocation is rounded up
2553 // to a word boundary. This follows the semantics of the instruction
2554 // which specifies that bit 1 of the target address will come from bit
2555 // 1 of the base address.
2556 relocation
= (relocation
+ 2U) & ~3U;
2558 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2559 // We use the Thumb-2 encoding, which is safe even if dealing with
2560 // a Thumb-1 instruction by virtue of our overflow check above. */
2561 upper_insn
= (upper_insn
& ~0x7ffU
)
2562 | ((relocation
>> 12) & 0x3ffU
)
2563 | (reloc_sign
<< 10);
2564 lower_insn
= (lower_insn
& ~0x2fffU
)
2565 | (((!((relocation
>> 23) & 1U)) ^ reloc_sign
) << 13)
2566 | (((!((relocation
>> 22) & 1U)) ^ reloc_sign
) << 11)
2567 | ((relocation
>> 1) & 0x7ffU
);
2569 elfcpp::Swap
<16, big_endian
>::writeval(wv
, upper_insn
);
2570 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lower_insn
);
2573 ? utils::has_overflow
<25>(relocation
)
2574 : utils::has_overflow
<23>(relocation
))
2575 ? This::STATUS_OVERFLOW
2576 : This::STATUS_OKAY
);
2579 // Get the GOT section, creating it if necessary.
2581 template<bool big_endian
>
2582 Output_data_got
<32, big_endian
>*
2583 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
2585 if (this->got_
== NULL
)
2587 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2589 this->got_
= new Output_data_got
<32, big_endian
>();
2592 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2594 | elfcpp::SHF_WRITE
),
2598 // The old GNU linker creates a .got.plt section. We just
2599 // create another set of data in the .got section. Note that we
2600 // always create a PLT if we create a GOT, although the PLT
2602 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
2603 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2605 | elfcpp::SHF_WRITE
),
2606 this->got_plt_
, false);
2609 // The first three entries are reserved.
2610 this->got_plt_
->set_current_data_size(3 * 4);
2612 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2613 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2615 0, 0, elfcpp::STT_OBJECT
,
2617 elfcpp::STV_HIDDEN
, 0,
2623 // Get the dynamic reloc section, creating it if necessary.
2625 template<bool big_endian
>
2626 typename Target_arm
<big_endian
>::Reloc_section
*
2627 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
2629 if (this->rel_dyn_
== NULL
)
2631 gold_assert(layout
!= NULL
);
2632 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
2633 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
2634 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true);
2636 return this->rel_dyn_
;
2639 // Insn_template methods.
2641 // Return byte size of an instruction template.
2644 Insn_template::size() const
2646 switch (this->type())
2659 // Return alignment of an instruction template.
2662 Insn_template::alignment() const
2664 switch (this->type())
2677 // Stub_template methods.
2679 Stub_template::Stub_template(
2680 Stub_type atype
, const Insn_template
* iinsns
,
2682 : type_(atype
), insns_(iinsns
), insn_count_(insncount
), alignment_(1),
2683 entry_in_thumb_mode_(false), relocs_()
2687 // Compute byte size and alignment of stub template.
2688 for (size_t i
= 0; i
< insncount
; i
++)
2690 unsigned insn_alignment
= iinsns
[i
].alignment();
2691 size_t insn_size
= iinsns
[i
].size();
2692 gold_assert((off
& (insn_alignment
- 1)) == 0);
2693 this->alignment_
= std::max(this->alignment_
, insn_alignment
);
2694 switch (iinsns
[i
].type())
2696 case Insn_template::THUMB16_TYPE
:
2698 this->entry_in_thumb_mode_
= true;
2701 case Insn_template::THUMB32_TYPE
:
2702 if (iinsns
[i
].r_type() != elfcpp::R_ARM_NONE
)
2703 this->relocs_
.push_back(Reloc(i
, off
));
2705 this->entry_in_thumb_mode_
= true;
2708 case Insn_template::ARM_TYPE
:
2709 // Handle cases where the target is encoded within the
2711 if (iinsns
[i
].r_type() == elfcpp::R_ARM_JUMP24
)
2712 this->relocs_
.push_back(Reloc(i
, off
));
2715 case Insn_template::DATA_TYPE
:
2716 // Entry point cannot be data.
2717 gold_assert(i
!= 0);
2718 this->relocs_
.push_back(Reloc(i
, off
));
2729 // Reloc_stub::Key methods.
2731 // Dump a Key as a string for debugging.
2734 Reloc_stub::Key::name() const
2736 if (this->r_sym_
== invalid_index
)
2738 // Global symbol key name
2739 // <stub-type>:<symbol name>:<addend>.
2740 const std::string sym_name
= this->u_
.symbol
->name();
2741 // We need to print two hex number and two colons. So just add 100 bytes
2742 // to the symbol name size.
2743 size_t len
= sym_name
.size() + 100;
2744 char* buffer
= new char[len
];
2745 int c
= snprintf(buffer
, len
, "%d:%s:%x", this->stub_type_
,
2746 sym_name
.c_str(), this->addend_
);
2747 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2749 return std::string(buffer
);
2753 // local symbol key name
2754 // <stub-type>:<object>:<r_sym>:<addend>.
2755 const size_t len
= 200;
2757 int c
= snprintf(buffer
, len
, "%d:%p:%u:%x", this->stub_type_
,
2758 this->u_
.relobj
, this->r_sym_
, this->addend_
);
2759 gold_assert(c
> 0 && c
< static_cast<int>(len
));
2760 return std::string(buffer
);
2764 // Reloc_stub methods.
2766 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2767 // LOCATION to DESTINATION.
2768 // This code is based on the arm_type_of_stub function in
2769 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2773 Reloc_stub::stub_type_for_reloc(
2774 unsigned int r_type
,
2775 Arm_address location
,
2776 Arm_address destination
,
2777 bool target_is_thumb
)
2779 Stub_type stub_type
= arm_stub_none
;
2781 // This is a bit ugly but we want to avoid using a templated class for
2782 // big and little endianities.
2784 bool should_force_pic_veneer
;
2787 if (parameters
->target().is_big_endian())
2789 const Target_arm
<true>* big_endian_target
=
2790 Target_arm
<true>::default_target();
2791 may_use_blx
= big_endian_target
->may_use_blx();
2792 should_force_pic_veneer
= big_endian_target
->should_force_pic_veneer();
2793 thumb2
= big_endian_target
->using_thumb2();
2794 thumb_only
= big_endian_target
->using_thumb_only();
2798 const Target_arm
<false>* little_endian_target
=
2799 Target_arm
<false>::default_target();
2800 may_use_blx
= little_endian_target
->may_use_blx();
2801 should_force_pic_veneer
= little_endian_target
->should_force_pic_veneer();
2802 thumb2
= little_endian_target
->using_thumb2();
2803 thumb_only
= little_endian_target
->using_thumb_only();
2806 int64_t branch_offset
= (int64_t)destination
- location
;
2808 if (r_type
== elfcpp::R_ARM_THM_CALL
|| r_type
== elfcpp::R_ARM_THM_JUMP24
)
2810 // Handle cases where:
2811 // - this call goes too far (different Thumb/Thumb2 max
2813 // - it's a Thumb->Arm call and blx is not available, or it's a
2814 // Thumb->Arm branch (not bl). A stub is needed in this case.
2816 && (branch_offset
> THM_MAX_FWD_BRANCH_OFFSET
2817 || (branch_offset
< THM_MAX_BWD_BRANCH_OFFSET
)))
2819 && (branch_offset
> THM2_MAX_FWD_BRANCH_OFFSET
2820 || (branch_offset
< THM2_MAX_BWD_BRANCH_OFFSET
)))
2821 || ((!target_is_thumb
)
2822 && (((r_type
== elfcpp::R_ARM_THM_CALL
) && !may_use_blx
)
2823 || (r_type
== elfcpp::R_ARM_THM_JUMP24
))))
2825 if (target_is_thumb
)
2830 stub_type
= (parameters
->options().shared()
2831 || should_force_pic_veneer
)
2834 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2835 // V5T and above. Stub starts with ARM code, so
2836 // we must be able to switch mode before
2837 // reaching it, which is only possible for 'bl'
2838 // (ie R_ARM_THM_CALL relocation).
2839 ? arm_stub_long_branch_any_thumb_pic
2840 // On V4T, use Thumb code only.
2841 : arm_stub_long_branch_v4t_thumb_thumb_pic
)
2845 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2846 ? arm_stub_long_branch_any_any
// V5T and above.
2847 : arm_stub_long_branch_v4t_thumb_thumb
); // V4T.
2851 stub_type
= (parameters
->options().shared()
2852 || should_force_pic_veneer
)
2853 ? arm_stub_long_branch_thumb_only_pic
// PIC stub.
2854 : arm_stub_long_branch_thumb_only
; // non-PIC stub.
2861 // FIXME: We should check that the input section is from an
2862 // object that has interwork enabled.
2864 stub_type
= (parameters
->options().shared()
2865 || should_force_pic_veneer
)
2868 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2869 ? arm_stub_long_branch_any_arm_pic
// V5T and above.
2870 : arm_stub_long_branch_v4t_thumb_arm_pic
) // V4T.
2874 && (r_type
== elfcpp::R_ARM_THM_CALL
))
2875 ? arm_stub_long_branch_any_any
// V5T and above.
2876 : arm_stub_long_branch_v4t_thumb_arm
); // V4T.
2878 // Handle v4t short branches.
2879 if ((stub_type
== arm_stub_long_branch_v4t_thumb_arm
)
2880 && (branch_offset
<= THM_MAX_FWD_BRANCH_OFFSET
)
2881 && (branch_offset
>= THM_MAX_BWD_BRANCH_OFFSET
))
2882 stub_type
= arm_stub_short_branch_v4t_thumb_arm
;
2886 else if (r_type
== elfcpp::R_ARM_CALL
2887 || r_type
== elfcpp::R_ARM_JUMP24
2888 || r_type
== elfcpp::R_ARM_PLT32
)
2890 if (target_is_thumb
)
2894 // FIXME: We should check that the input section is from an
2895 // object that has interwork enabled.
2897 // We have an extra 2-bytes reach because of
2898 // the mode change (bit 24 (H) of BLX encoding).
2899 if (branch_offset
> (ARM_MAX_FWD_BRANCH_OFFSET
+ 2)
2900 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
)
2901 || ((r_type
== elfcpp::R_ARM_CALL
) && !may_use_blx
)
2902 || (r_type
== elfcpp::R_ARM_JUMP24
)
2903 || (r_type
== elfcpp::R_ARM_PLT32
))
2905 stub_type
= (parameters
->options().shared()
2906 || should_force_pic_veneer
)
2909 ? arm_stub_long_branch_any_thumb_pic
// V5T and above.
2910 : arm_stub_long_branch_v4t_arm_thumb_pic
) // V4T stub.
2914 ? arm_stub_long_branch_any_any
// V5T and above.
2915 : arm_stub_long_branch_v4t_arm_thumb
); // V4T.
2921 if (branch_offset
> ARM_MAX_FWD_BRANCH_OFFSET
2922 || (branch_offset
< ARM_MAX_BWD_BRANCH_OFFSET
))
2924 stub_type
= (parameters
->options().shared()
2925 || should_force_pic_veneer
)
2926 ? arm_stub_long_branch_any_arm_pic
// PIC stubs.
2927 : arm_stub_long_branch_any_any
; /// non-PIC.
2935 // Template to implement do_write for a specific target endianity.
2937 template<bool big_endian
>
2939 Reloc_stub::do_fixed_endian_write(unsigned char* view
,
2940 section_size_type view_size
)
2942 const Stub_template
* stubtemplate
= this->stub_template();
2943 const Insn_template
* insns
= stubtemplate
->insns();
2945 // FIXME: We do not handle BE8 encoding yet.
2946 unsigned char* pov
= view
;
2947 for (size_t i
= 0; i
< stubtemplate
->insn_count(); i
++)
2949 switch (insns
[i
].type())
2951 case Insn_template::THUMB16_TYPE
:
2952 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2953 gold_assert(insns
[i
].reloc_addend() == 0);
2954 elfcpp::Swap
<16, big_endian
>::writeval(pov
, insns
[i
].data() & 0xffff);
2956 case Insn_template::THUMB32_TYPE
:
2958 uint32_t hi
= (insns
[i
].data() >> 16) & 0xffff;
2959 uint32_t lo
= insns
[i
].data() & 0xffff;
2960 elfcpp::Swap
<16, big_endian
>::writeval(pov
, hi
);
2961 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lo
);
2964 case Insn_template::ARM_TYPE
:
2965 case Insn_template::DATA_TYPE
:
2966 elfcpp::Swap
<32, big_endian
>::writeval(pov
, insns
[i
].data());
2971 pov
+= insns
[i
].size();
2973 gold_assert(static_cast<section_size_type
>(pov
- view
) == view_size
);
2976 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2979 Reloc_stub::do_write(unsigned char* view
, section_size_type view_size
,
2983 this->do_fixed_endian_write
<true>(view
, view_size
);
2985 this->do_fixed_endian_write
<false>(view
, view_size
);
2988 // Stub_factory methods.
2990 Stub_factory::Stub_factory()
2992 // The instruction template sequences are declared as static
2993 // objects and initialized first time the constructor runs.
2995 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2996 // to reach the stub if necessary.
2997 static const Insn_template elf32_arm_stub_long_branch_any_any
[] =
2999 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3000 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3001 // dcd R_ARM_ABS32(X)
3004 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3006 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb
[] =
3008 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3009 Insn_template::arm_insn(0xe12fff1c), // bx ip
3010 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3011 // dcd R_ARM_ABS32(X)
3014 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3015 static const Insn_template elf32_arm_stub_long_branch_thumb_only
[] =
3017 Insn_template::thumb16_insn(0xb401), // push {r0}
3018 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3019 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3020 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3021 Insn_template::thumb16_insn(0x4760), // bx ip
3022 Insn_template::thumb16_insn(0xbf00), // nop
3023 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3024 // dcd R_ARM_ABS32(X)
3027 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3029 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb
[] =
3031 Insn_template::thumb16_insn(0x4778), // bx pc
3032 Insn_template::thumb16_insn(0x46c0), // nop
3033 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3034 Insn_template::arm_insn(0xe12fff1c), // bx ip
3035 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3036 // dcd R_ARM_ABS32(X)
3039 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3041 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm
[] =
3043 Insn_template::thumb16_insn(0x4778), // bx pc
3044 Insn_template::thumb16_insn(0x46c0), // nop
3045 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3046 Insn_template::data_word(0, elfcpp::R_ARM_ABS32
, 0),
3047 // dcd R_ARM_ABS32(X)
3050 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3051 // one, when the destination is close enough.
3052 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm
[] =
3054 Insn_template::thumb16_insn(0x4778), // bx pc
3055 Insn_template::thumb16_insn(0x46c0), // nop
3056 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3059 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3060 // blx to reach the stub if necessary.
3061 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic
[] =
3063 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3064 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3065 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3066 // dcd R_ARM_REL32(X-4)
3069 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3070 // blx to reach the stub if necessary. We can not add into pc;
3071 // it is not guaranteed to mode switch (different in ARMv6 and
3073 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic
[] =
3075 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3076 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3077 Insn_template::arm_insn(0xe12fff1c), // bx ip
3078 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3079 // dcd R_ARM_REL32(X)
3082 // V4T ARM -> ARM long branch stub, PIC.
3083 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic
[] =
3085 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3086 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3087 Insn_template::arm_insn(0xe12fff1c), // bx ip
3088 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3089 // dcd R_ARM_REL32(X)
3092 // V4T Thumb -> ARM long branch stub, PIC.
3093 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic
[] =
3095 Insn_template::thumb16_insn(0x4778), // bx pc
3096 Insn_template::thumb16_insn(0x46c0), // nop
3097 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3098 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3099 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, -4),
3100 // dcd R_ARM_REL32(X)
3103 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3105 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic
[] =
3107 Insn_template::thumb16_insn(0xb401), // push {r0}
3108 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3109 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3110 Insn_template::thumb16_insn(0x4484), // add ip, r0
3111 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3112 Insn_template::thumb16_insn(0x4760), // bx ip
3113 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 4),
3114 // dcd R_ARM_REL32(X)
3117 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3119 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic
[] =
3121 Insn_template::thumb16_insn(0x4778), // bx pc
3122 Insn_template::thumb16_insn(0x46c0), // nop
3123 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3124 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3125 Insn_template::arm_insn(0xe12fff1c), // bx ip
3126 Insn_template::data_word(0, elfcpp::R_ARM_REL32
, 0),
3127 // dcd R_ARM_REL32(X)
3130 // Cortex-A8 erratum-workaround stubs.
3132 // Stub used for conditional branches (which may be beyond +/-1MB away,
3133 // so we can't use a conditional branch to reach this stub).
3140 static const Insn_template elf32_arm_stub_a8_veneer_b_cond
[] =
3142 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3143 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3144 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3148 // Stub used for b.w and bl.w instructions.
3150 static const Insn_template elf32_arm_stub_a8_veneer_b
[] =
3152 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3155 static const Insn_template elf32_arm_stub_a8_veneer_bl
[] =
3157 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3160 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3161 // instruction (which switches to ARM mode) to point to this stub. Jump to
3162 // the real destination using an ARM-mode branch.
3163 const Insn_template elf32_arm_stub_a8_veneer_blx
[] =
3165 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3168 // Fill in the stub template look-up table. Stub templates are constructed
3169 // per instance of Stub_factory for fast look-up without locking
3170 // in a thread-enabled environment.
3172 this->stub_templates_
[arm_stub_none
] =
3173 new Stub_template(arm_stub_none
, NULL
, 0);
3175 #define DEF_STUB(x) \
3179 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3180 Stub_type type = arm_stub_##x; \
3181 this->stub_templates_[type] = \
3182 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3190 // Stub_table methods.
3192 // Add a STUB with using KEY. Caller is reponsible for avoid adding
3193 // if already a STUB with the same key has been added.
3195 template<bool big_endian
>
3197 Stub_table
<big_endian
>::add_reloc_stub(
3199 const Reloc_stub::Key
& key
)
3201 const Stub_template
* stubtemplate
= stub
->stub_template();
3202 gold_assert(stubtemplate
->type() == key
.stub_type());
3203 this->reloc_stubs_
[key
] = stub
;
3204 if (this->addralign_
< stubtemplate
->alignment())
3205 this->addralign_
= stubtemplate
->alignment();
3206 this->has_been_changed_
= true;
3209 template<bool big_endian
>
3211 Stub_table
<big_endian
>::relocate_stubs(
3212 const Relocate_info
<32, big_endian
>* relinfo
,
3213 Target_arm
<big_endian
>* arm_target
,
3214 Output_section
* out_section
,
3215 unsigned char* view
,
3217 section_size_type view_size
)
3219 // If we are passed a view bigger than the stub table's. we need to
3221 gold_assert(addr
== this->address()
3223 == static_cast<section_size_type
>(this->data_size())));
3225 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3226 p
!= this->reloc_stubs_
.end();
3229 Reloc_stub
* stub
= p
->second
;
3230 const Stub_template
* stubtemplate
= stub
->stub_template();
3231 if (stubtemplate
->reloc_count() != 0)
3233 // Adjust view to cover the stub only.
3234 section_size_type off
= stub
->offset();
3235 section_size_type stub_size
= stubtemplate
->size();
3236 gold_assert(off
+ stub_size
<= view_size
);
3238 arm_target
->relocate_stub(stub
, relinfo
, out_section
,
3239 view
+ off
, addr
+ off
,
3245 // Reset address and file offset.
3247 template<bool big_endian
>
3249 Stub_table
<big_endian
>::do_reset_address_and_file_offset()
3252 uint64_t max_addralign
= 1;
3253 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3254 p
!= this->reloc_stubs_
.end();
3257 Reloc_stub
* stub
= p
->second
;
3258 const Stub_template
* stubtemplate
= stub
->stub_template();
3259 uint64_t stub_addralign
= stubtemplate
->alignment();
3260 max_addralign
= std::max(max_addralign
, stub_addralign
);
3261 off
= align_address(off
, stub_addralign
);
3262 stub
->set_offset(off
);
3263 stub
->reset_destination_address();
3264 off
+= stubtemplate
->size();
3267 this->addralign_
= max_addralign
;
3268 this->set_current_data_size_for_child(off
);
3271 // Write out the stubs to file.
3273 template<bool big_endian
>
3275 Stub_table
<big_endian
>::do_write(Output_file
* of
)
3277 off_t off
= this->offset();
3278 const section_size_type oview_size
=
3279 convert_to_section_size_type(this->data_size());
3280 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3282 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
3283 p
!= this->reloc_stubs_
.end();
3286 Reloc_stub
* stub
= p
->second
;
3287 Arm_address addr
= this->address() + stub
->offset();
3289 == align_address(addr
,
3290 stub
->stub_template()->alignment()));
3291 stub
->write(oview
+ stub
->offset(), stub
->stub_template()->size(),
3294 of
->write_output_view(this->offset(), oview_size
, oview
);
3297 // Arm_input_section methods.
3299 // Initialize an Arm_input_section.
3301 template<bool big_endian
>
3303 Arm_input_section
<big_endian
>::init()
3305 Relobj
* rel_obj
= this->relobj();
3306 unsigned int sec_shndx
= this->shndx();
3308 // Cache these to speed up size and alignment queries. It is too slow
3309 // to call section_addraglin and section_size every time.
3310 this->original_addralign_
= rel_obj
->section_addralign(sec_shndx
);
3311 this->original_size_
= rel_obj
->section_size(sec_shndx
);
3313 // We want to make this look like the original input section after
3314 // output sections are finalized.
3315 Output_section
* os
= rel_obj
->output_section(sec_shndx
);
3316 off_t off
= rel_obj
->output_section_offset(sec_shndx
);
3317 gold_assert(os
!= NULL
&& !rel_obj
->is_output_section_offset_invalid(sec_shndx
));
3318 this->set_address(os
->address() + off
);
3319 this->set_file_offset(os
->offset() + off
);
3321 this->set_current_data_size(this->original_size_
);
3322 this->finalize_data_size();
3325 template<bool big_endian
>
3327 Arm_input_section
<big_endian
>::do_write(Output_file
* of
)
3329 // We have to write out the original section content.
3330 section_size_type section_size
;
3331 const unsigned char* section_contents
=
3332 this->relobj()->section_contents(this->shndx(), §ion_size
, false);
3333 of
->write(this->offset(), section_contents
, section_size
);
3335 // If this owns a stub table and it is not empty, write it.
3336 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
3337 this->stub_table_
->write(of
);
3340 // Finalize data size.
3342 template<bool big_endian
>
3344 Arm_input_section
<big_endian
>::set_final_data_size()
3346 // If this owns a stub table, finalize its data size as well.
3347 if (this->is_stub_table_owner())
3349 uint64_t addr
= this->address();
3351 // The stub table comes after the original section contents.
3352 addr
+= this->original_size_
;
3353 addr
= align_address(addr
, this->stub_table_
->addralign());
3354 off_t off
= this->offset() + (addr
- this->address());
3355 this->stub_table_
->set_address_and_file_offset(addr
, off
);
3356 addr
+= this->stub_table_
->data_size();
3357 gold_assert(addr
== this->address() + this->current_data_size());
3360 this->set_data_size(this->current_data_size());
3363 // Reset address and file offset.
3365 template<bool big_endian
>
3367 Arm_input_section
<big_endian
>::do_reset_address_and_file_offset()
3369 // Size of the original input section contents.
3370 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
3372 // If this is a stub table owner, account for the stub table size.
3373 if (this->is_stub_table_owner())
3375 Stub_table
<big_endian
>* stubtable
= this->stub_table_
;
3377 // Reset the stub table's address and file offset. The
3378 // current data size for child will be updated after that.
3379 stub_table_
->reset_address_and_file_offset();
3380 off
= align_address(off
, stub_table_
->addralign());
3381 off
+= stubtable
->current_data_size();
3384 this->set_current_data_size(off
);
3387 // Arm_output_section methods.
3389 // Create a stub group for input sections from BEGIN to END. OWNER
3390 // points to the input section to be the owner a new stub table.
3392 template<bool big_endian
>
3394 Arm_output_section
<big_endian
>::create_stub_group(
3395 Input_section_list::const_iterator begin
,
3396 Input_section_list::const_iterator end
,
3397 Input_section_list::const_iterator owner
,
3398 Target_arm
<big_endian
>* target
,
3399 std::vector
<Output_relaxed_input_section
*>* new_relaxed_sections
)
3401 // Currently we convert ordinary input sections into relaxed sections only
3402 // at this point but we may want to support creating relaxed input section
3403 // very early. So we check here to see if owner is already a relaxed
3406 Arm_input_section
<big_endian
>* arm_input_section
;
3407 if (owner
->is_relaxed_input_section())
3410 Arm_input_section
<big_endian
>::as_arm_input_section(
3411 owner
->relaxed_input_section());
3415 gold_assert(owner
->is_input_section());
3416 // Create a new relaxed input section.
3418 target
->new_arm_input_section(owner
->relobj(), owner
->shndx());
3419 new_relaxed_sections
->push_back(arm_input_section
);
3422 // Create a stub table.
3423 Stub_table
<big_endian
>* stubtable
=
3424 target
->new_stub_table(arm_input_section
);
3426 arm_input_section
->set_stub_table(stubtable
);
3428 Input_section_list::const_iterator p
= begin
;
3429 Input_section_list::const_iterator prev_p
;
3431 // Look for input sections or relaxed input sections in [begin ... end].
3434 if (p
->is_input_section() || p
->is_relaxed_input_section())
3436 // The stub table information for input sections live
3437 // in their objects.
3438 Arm_relobj
<big_endian
>* arm_relobj
=
3439 Arm_relobj
<big_endian
>::as_arm_relobj(p
->relobj());
3440 arm_relobj
->set_stub_table(p
->shndx(), stubtable
);
3444 while (prev_p
!= end
);
3447 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3448 // of stub groups. We grow a stub group by adding input section until the
3449 // size is just below GROUP_SIZE. The last input section will be converted
3450 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3451 // input section after the stub table, effectively double the group size.
3453 // This is similar to the group_sections() function in elf32-arm.c but is
3454 // implemented differently.
3456 template<bool big_endian
>
3458 Arm_output_section
<big_endian
>::group_sections(
3459 section_size_type group_size
,
3460 bool stubs_always_after_branch
,
3461 Target_arm
<big_endian
>* target
)
3463 // We only care about sections containing code.
3464 if ((this->flags() & elfcpp::SHF_EXECINSTR
) == 0)
3467 // States for grouping.
3470 // No group is being built.
3472 // A group is being built but the stub table is not found yet.
3473 // We keep group a stub group until the size is just under GROUP_SIZE.
3474 // The last input section in the group will be used as the stub table.
3475 FINDING_STUB_SECTION
,
3476 // A group is being built and we have already found a stub table.
3477 // We enter this state to grow a stub group by adding input section
3478 // after the stub table. This effectively doubles the group size.
3482 // Any newly created relaxed sections are stored here.
3483 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
3485 State state
= NO_GROUP
;
3486 section_size_type off
= 0;
3487 section_size_type group_begin_offset
= 0;
3488 section_size_type group_end_offset
= 0;
3489 section_size_type stub_table_end_offset
= 0;
3490 Input_section_list::const_iterator group_begin
=
3491 this->input_sections().end();
3492 Input_section_list::const_iterator stubtable
=
3493 this->input_sections().end();
3494 Input_section_list::const_iterator group_end
= this->input_sections().end();
3495 for (Input_section_list::const_iterator p
= this->input_sections().begin();
3496 p
!= this->input_sections().end();
3499 section_size_type section_begin_offset
=
3500 align_address(off
, p
->addralign());
3501 section_size_type section_end_offset
=
3502 section_begin_offset
+ p
->data_size();
3504 // Check to see if we should group the previously seens sections.
3510 case FINDING_STUB_SECTION
:
3511 // Adding this section makes the group larger than GROUP_SIZE.
3512 if (section_end_offset
- group_begin_offset
>= group_size
)
3514 if (stubs_always_after_branch
)
3516 gold_assert(group_end
!= this->input_sections().end());
3517 this->create_stub_group(group_begin
, group_end
, group_end
,
3518 target
, &new_relaxed_sections
);
3523 // But wait, there's more! Input sections up to
3524 // stub_group_size bytes after the stub table can be
3525 // handled by it too.
3526 state
= HAS_STUB_SECTION
;
3527 stubtable
= group_end
;
3528 stub_table_end_offset
= group_end_offset
;
3533 case HAS_STUB_SECTION
:
3534 // Adding this section makes the post stub-section group larger
3536 if (section_end_offset
- stub_table_end_offset
>= group_size
)
3538 gold_assert(group_end
!= this->input_sections().end());
3539 this->create_stub_group(group_begin
, group_end
, stubtable
,
3540 target
, &new_relaxed_sections
);
3549 // If we see an input section and currently there is no group, start
3550 // a new one. Skip any empty sections.
3551 if ((p
->is_input_section() || p
->is_relaxed_input_section())
3552 && (p
->relobj()->section_size(p
->shndx()) != 0))
3554 if (state
== NO_GROUP
)
3556 state
= FINDING_STUB_SECTION
;
3558 group_begin_offset
= section_begin_offset
;
3561 // Keep track of the last input section seen.
3563 group_end_offset
= section_end_offset
;
3566 off
= section_end_offset
;
3569 // Create a stub group for any ungrouped sections.
3570 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
3572 gold_assert(group_end
!= this->input_sections().end());
3573 this->create_stub_group(group_begin
, group_end
,
3574 (state
== FINDING_STUB_SECTION
3577 target
, &new_relaxed_sections
);
3580 // Convert input section into relaxed input section in a batch.
3581 if (!new_relaxed_sections
.empty())
3582 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
3584 // Update the section offsets
3585 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
3587 Arm_relobj
<big_endian
>* arm_relobj
=
3588 Arm_relobj
<big_endian
>::as_arm_relobj(
3589 new_relaxed_sections
[i
]->relobj());
3590 unsigned int sec_shndx
= new_relaxed_sections
[i
]->shndx();
3591 // Tell Arm_relobj that this input section is converted.
3592 arm_relobj
->convert_input_section_to_relaxed_section(sec_shndx
);
3596 // Arm_relobj methods.
3598 // Scan relocations for stub generation.
3600 template<bool big_endian
>
3602 Arm_relobj
<big_endian
>::scan_sections_for_stubs(
3603 Target_arm
<big_endian
>* arm_target
,
3604 const Symbol_table
* symtab
,
3605 const Layout
* alayout
)
3607 unsigned int sec_shnum
= this->shnum();
3608 const unsigned int shdrsize
= elfcpp::Elf_sizes
<32>::shdr_size
;
3610 // Read the section headers.
3611 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
3612 sec_shnum
* shdrsize
,
3615 // To speed up processing, we set up hash tables for fast lookup of
3616 // input offsets to output addresses.
3617 this->initialize_input_to_output_maps();
3619 const Relobj::Output_sections
& out_sections(this->output_sections());
3621 Relocate_info
<32, big_endian
> relinfo
;
3622 relinfo
.symtab
= symtab
;
3623 relinfo
.layout
= alayout
;
3624 relinfo
.object
= this;
3626 const unsigned char* p
= pshdrs
+ shdrsize
;
3627 for (unsigned int i
= 1; i
< sec_shnum
; ++i
, p
+= shdrsize
)
3629 typename
elfcpp::Shdr
<32, big_endian
> shdr(p
);
3631 unsigned int sh_type
= shdr
.get_sh_type();
3632 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
3635 off_t sh_size
= shdr
.get_sh_size();
3639 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
3640 if (index
>= this->shnum())
3642 // Ignore reloc section with bad info. This error will be
3643 // reported in the final link.
3647 Output_section
* os
= out_sections
[index
];
3650 // This relocation section is against a section which we
3654 Arm_address output_offset
= this->get_output_section_offset(index
);
3656 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
3658 // Ignore reloc section with unexpected symbol table. The
3659 // error will be reported in the final link.
3663 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
3664 sh_size
, true, false);
3666 unsigned int reloc_size
;
3667 if (sh_type
== elfcpp::SHT_REL
)
3668 reloc_size
= elfcpp::Elf_sizes
<32>::rel_size
;
3670 reloc_size
= elfcpp::Elf_sizes
<32>::rela_size
;
3672 if (reloc_size
!= shdr
.get_sh_entsize())
3674 // Ignore reloc section with unexpected entsize. The error
3675 // will be reported in the final link.
3679 size_t reloc_count
= sh_size
/ reloc_size
;
3680 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
3682 // Ignore reloc section with uneven size. The error will be
3683 // reported in the final link.
3687 gold_assert(output_offset
!= invalid_address
3688 || this->relocs_must_follow_section_writes());
3690 // Get the section contents. This does work for the case in which
3691 // we modify the contents of an input section. We need to pass the
3692 // output view under such circumstances.
3693 section_size_type input_view_size
= 0;
3694 const unsigned char* input_view
=
3695 this->section_contents(index
, &input_view_size
, false);
3697 relinfo
.reloc_shndx
= i
;
3698 relinfo
.data_shndx
= index
;
3699 arm_target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
3701 output_offset
== invalid_address
,
3707 // After we've done the relocations, we release the hash tables,
3708 // since we no longer need them.
3709 this->free_input_to_output_maps();
3712 // Count the local symbols. The ARM backend needs to know if a symbol
3713 // is a THUMB function or not. For global symbols, it is easy because
3714 // the Symbol object keeps the ELF symbol type. For local symbol it is
3715 // harder because we cannot access this information. So we override the
3716 // do_count_local_symbol in parent and scan local symbols to mark
3717 // THUMB functions. This is not the most efficient way but I do not want to
3718 // slow down other ports by calling a per symbol targer hook inside
3719 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3721 template<bool big_endian
>
3723 Arm_relobj
<big_endian
>::do_count_local_symbols(
3724 Stringpool_template
<char>* pool
,
3725 Stringpool_template
<char>* dynpool
)
3727 // We need to fix-up the values of any local symbols whose type are
3730 // Ask parent to count the local symbols.
3731 Sized_relobj
<32, big_endian
>::do_count_local_symbols(pool
, dynpool
);
3732 const unsigned int loccount
= this->local_symbol_count();
3736 // Intialize the thumb function bit-vector.
3737 std::vector
<bool> empty_vector(loccount
, false);
3738 this->local_symbol_is_thumb_function_
.swap(empty_vector
);
3740 // Read the symbol table section header.
3741 const unsigned int sym_tab_shndx
= this->symtab_shndx();
3742 elfcpp::Shdr
<32, big_endian
>
3743 symtabshdr(this, this->elf_file()->section_header(sym_tab_shndx
));
3744 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
3746 // Read the local symbols.
3747 const int symsize
=elfcpp::Elf_sizes
<32>::sym_size
;
3748 gold_assert(loccount
== symtabshdr
.get_sh_info());
3749 off_t locsize
= loccount
* symsize
;
3750 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
3751 locsize
, true, true);
3753 // Loop over the local symbols and mark any local symbols pointing
3754 // to THUMB functions.
3756 // Skip the first dummy symbol.
3758 typename Sized_relobj
<32, big_endian
>::Local_values
* plocal_values
=
3759 this->local_values();
3760 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= symsize
)
3762 elfcpp::Sym
<32, big_endian
> sym(psyms
);
3763 elfcpp::STT st_type
= sym
.get_st_type();
3764 Symbol_value
<32>& lv((*plocal_values
)[i
]);
3765 Arm_address input_value
= lv
.input_value();
3767 if (st_type
== elfcpp::STT_ARM_TFUNC
3768 || (st_type
== elfcpp::STT_FUNC
&& ((input_value
& 1) != 0)))
3770 // This is a THUMB function. Mark this and canonicalize the
3771 // symbol value by setting LSB.
3772 this->local_symbol_is_thumb_function_
[i
] = true;
3773 if ((input_value
& 1) == 0)
3774 lv
.set_input_value(input_value
| 1);
3779 // Relocate sections.
3780 template<bool big_endian
>
3782 Arm_relobj
<big_endian
>::do_relocate_sections(
3783 const Symbol_table
* symtab
,
3784 const Layout
* alayout
,
3785 const unsigned char* pshdrs
,
3786 typename Sized_relobj
<32, big_endian
>::Views
* pviews
)
3788 // Call parent to relocate sections.
3789 Sized_relobj
<32, big_endian
>::do_relocate_sections(symtab
, alayout
, pshdrs
,
3792 // We do not generate stubs if doing a relocatable link.
3793 if (parameters
->options().relocatable())
3796 // Relocate stub tables.
3797 unsigned int sec_shnum
= this->shnum();
3799 Target_arm
<big_endian
>* arm_target
=
3800 Target_arm
<big_endian
>::default_target();
3802 Relocate_info
<32, big_endian
> relinfo
;
3803 relinfo
.symtab
= symtab
;
3804 relinfo
.layout
= alayout
;
3805 relinfo
.object
= this;
3807 for (unsigned int i
= 1; i
< sec_shnum
; ++i
)
3809 Arm_input_section
<big_endian
>* arm_input_section
=
3810 arm_target
->find_arm_input_section(this, i
);
3812 if (arm_input_section
== NULL
3813 || !arm_input_section
->is_stub_table_owner()
3814 || arm_input_section
->stub_table()->empty())
3817 // We cannot discard a section if it owns a stub table.
3818 Output_section
* os
= this->output_section(i
);
3819 gold_assert(os
!= NULL
);
3821 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
3822 relinfo
.reloc_shdr
= NULL
;
3823 relinfo
.data_shndx
= i
;
3824 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<32>::shdr_size
;
3826 gold_assert((*pviews
)[i
].view
!= NULL
);
3828 // We are passed the output section view. Adjust it to cover the
3830 Stub_table
<big_endian
>* stubtable
= arm_input_section
->stub_table();
3831 gold_assert((stubtable
->address() >= (*pviews
)[i
].address
)
3832 && ((stubtable
->address() + stubtable
->data_size())
3833 <= (*pviews
)[i
].address
+ (*pviews
)[i
].view_size
));
3835 off_t off
= stubtable
->address() - (*pviews
)[i
].address
;
3836 unsigned char* pview
= (*pviews
)[i
].view
+ off
;
3837 Arm_address address
= stubtable
->address();
3838 section_size_type view_size
= stubtable
->data_size();
3840 stubtable
->relocate_stubs(&relinfo
, arm_target
, os
, pview
, address
,
3845 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
3848 template<bool big_endian
>
3849 Attributes_section_data
*
3850 read_arm_attributes_section(
3852 Read_symbols_data
*sd
)
3854 // Read the attributes section if there is one.
3855 // We read from the end because gas seems to put it near the end of
3856 // the section headers.
3857 const size_t shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
3858 const unsigned char *ps
=
3859 sd
->section_headers
->data() + shdr_size
* (object
->shnum() - 1);
3860 for (unsigned int i
= object
->shnum(); i
> 0; --i
, ps
-= shdr_size
)
3862 elfcpp::Shdr
<32, big_endian
> shdr(ps
);
3863 if (shdr
.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES
)
3865 section_offset_type section_offset
= shdr
.get_sh_offset();
3866 section_size_type section_size
=
3867 convert_to_section_size_type(shdr
.get_sh_size());
3868 File_view
* view
= object
->get_lasting_view(section_offset
,
3869 section_size
, true, false);
3870 return new Attributes_section_data(view
->data(), section_size
);
3876 // Read the symbol information.
3878 template<bool big_endian
>
3880 Arm_relobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3882 // Call parent class to read symbol information.
3883 Sized_relobj
<32, big_endian
>::do_read_symbols(sd
);
3885 // Read processor-specific flags in ELF file header.
3886 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3887 elfcpp::Elf_sizes
<32>::ehdr_size
,
3889 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3890 this->processor_specific_flags_
= ehdr
.get_e_flags();
3891 this->attributes_section_data_
=
3892 read_arm_attributes_section
<big_endian
>(this, sd
);
3895 // Arm_dynobj methods.
3897 // Read the symbol information.
3899 template<bool big_endian
>
3901 Arm_dynobj
<big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
3903 // Call parent class to read symbol information.
3904 Sized_dynobj
<32, big_endian
>::do_read_symbols(sd
);
3906 // Read processor-specific flags in ELF file header.
3907 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
3908 elfcpp::Elf_sizes
<32>::ehdr_size
,
3910 elfcpp::Ehdr
<32, big_endian
> ehdr(pehdr
);
3911 this->processor_specific_flags_
= ehdr
.get_e_flags();
3912 this->attributes_section_data_
=
3913 read_arm_attributes_section
<big_endian
>(this, sd
);
3916 // Stub_addend_reader methods.
3918 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3920 template<bool big_endian
>
3921 elfcpp::Elf_types
<32>::Elf_Swxword
3922 Stub_addend_reader
<elfcpp::SHT_REL
, big_endian
>::operator()(
3923 unsigned int r_type
,
3924 const unsigned char* view
,
3925 const typename Reloc_types
<elfcpp::SHT_REL
, 32, big_endian
>::Reloc
&) const
3929 case elfcpp::R_ARM_CALL
:
3930 case elfcpp::R_ARM_JUMP24
:
3931 case elfcpp::R_ARM_PLT32
:
3933 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3934 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3935 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3936 return utils::sign_extend
<26>(val
<< 2);
3939 case elfcpp::R_ARM_THM_CALL
:
3940 case elfcpp::R_ARM_THM_JUMP24
:
3941 case elfcpp::R_ARM_THM_XPC22
:
3943 // Fetch the addend. We use the Thumb-2 encoding (backwards
3944 // compatible with Thumb-1) involving the J1 and J2 bits.
3945 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3946 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3947 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3948 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3950 uint32_t s
= (upper_insn
& (1 << 10)) >> 10;
3951 uint32_t upper
= upper_insn
& 0x3ff;
3952 uint32_t lower
= lower_insn
& 0x7ff;
3953 uint32_t j1
= (lower_insn
& (1 << 13)) >> 13;
3954 uint32_t j2
= (lower_insn
& (1 << 11)) >> 11;
3955 uint32_t i1
= j1
^ s
? 0 : 1;
3956 uint32_t i2
= j2
^ s
? 0 : 1;
3958 return utils::sign_extend
<25>((s
<< 24) | (i1
<< 23) | (i2
<< 22)
3959 | (upper
<< 12) | (lower
<< 1));
3962 case elfcpp::R_ARM_THM_JUMP19
:
3964 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
3965 const Valtype
* wv
= reinterpret_cast<const Valtype
*>(view
);
3966 Valtype upper_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
3967 Valtype lower_insn
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
3969 // Reconstruct the top three bits and squish the two 11 bit pieces
3971 uint32_t S
= (upper_insn
& 0x0400) >> 10;
3972 uint32_t J1
= (lower_insn
& 0x2000) >> 13;
3973 uint32_t J2
= (lower_insn
& 0x0800) >> 11;
3975 (S
<< 8) | (J2
<< 7) | (J1
<< 6) | (upper_insn
& 0x003f);
3976 uint32_t lower
= (lower_insn
& 0x07ff);
3977 return utils::sign_extend
<23>((upper
<< 12) | (lower
<< 1));
3985 // A class to handle the PLT data.
3987 template<bool big_endian
>
3988 class Output_data_plt_arm
: public Output_section_data
3991 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
3994 Output_data_plt_arm(Layout
*, Output_data_space
*);
3996 // Add an entry to the PLT.
3998 add_entry(Symbol
* gsym
);
4000 // Return the .rel.plt section data.
4001 const Reloc_section
*
4003 { return this->rel_
; }
4007 do_adjust_output_section(Output_section
* os
);
4009 // Write to a map file.
4011 do_print_to_mapfile(Mapfile
* mapfile
) const
4012 { mapfile
->print_output_data(this, _("** PLT")); }
4015 // Template for the first PLT entry.
4016 static const uint32_t first_plt_entry
[5];
4018 // Template for subsequent PLT entries.
4019 static const uint32_t plt_entry
[3];
4021 // Set the final size.
4023 set_final_data_size()
4025 this->set_data_size(sizeof(first_plt_entry
)
4026 + this->count_
* sizeof(plt_entry
));
4029 // Write out the PLT data.
4031 do_write(Output_file
*);
4033 // The reloc section.
4034 Reloc_section
* rel_
;
4035 // The .got.plt section.
4036 Output_data_space
* got_plt_
;
4037 // The number of PLT entries.
4038 unsigned int count_
;
4041 // Create the PLT section. The ordinary .got section is an argument,
4042 // since we need to refer to the start. We also create our own .got
4043 // section just for PLT entries.
4045 template<bool big_endian
>
4046 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* alayout
,
4047 Output_data_space
* got_plt
)
4048 : Output_section_data(4), got_plt_(got_plt
), count_(0)
4050 this->rel_
= new Reloc_section(false);
4051 alayout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
4052 elfcpp::SHF_ALLOC
, this->rel_
, true);
4055 template<bool big_endian
>
4057 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
4062 // Add an entry to the PLT.
4064 template<bool big_endian
>
4066 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
4068 gold_assert(!gsym
->has_plt_offset());
4070 // Note that when setting the PLT offset we skip the initial
4071 // reserved PLT entry.
4072 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
4073 + sizeof(first_plt_entry
));
4077 section_offset_type got_offset
= this->got_plt_
->current_data_size();
4079 // Every PLT entry needs a GOT entry which points back to the PLT
4080 // entry (this will be changed by the dynamic linker, normally
4081 // lazily when the function is called).
4082 this->got_plt_
->set_current_data_size(got_offset
+ 4);
4084 // Every PLT entry needs a reloc.
4085 gsym
->set_needs_dynsym_entry();
4086 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
4089 // Note that we don't need to save the symbol. The contents of the
4090 // PLT are independent of which symbols are used. The symbols only
4091 // appear in the relocations.
4095 // FIXME: This is not very flexible. Right now this has only been tested
4096 // on armv5te. If we are to support additional architecture features like
4097 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4099 // The first entry in the PLT.
4100 template<bool big_endian
>
4101 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
4103 0xe52de004, // str lr, [sp, #-4]!
4104 0xe59fe004, // ldr lr, [pc, #4]
4105 0xe08fe00e, // add lr, pc, lr
4106 0xe5bef008, // ldr pc, [lr, #8]!
4107 0x00000000, // &GOT[0] - .
4110 // Subsequent entries in the PLT.
4112 template<bool big_endian
>
4113 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
4115 0xe28fc600, // add ip, pc, #0xNN00000
4116 0xe28cca00, // add ip, ip, #0xNN000
4117 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4120 // Write out the PLT. This uses the hand-coded instructions above,
4121 // and adjusts them as needed. This is all specified by the arm ELF
4122 // Processor Supplement.
4124 template<bool big_endian
>
4126 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
4128 const off_t off
= this->offset();
4129 const section_size_type oview_size
=
4130 convert_to_section_size_type(this->data_size());
4131 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4133 const off_t got_file_offset
= this->got_plt_
->offset();
4134 const section_size_type got_size
=
4135 convert_to_section_size_type(this->got_plt_
->data_size());
4136 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4138 unsigned char* pov
= oview
;
4140 Arm_address plt_address
= this->address();
4141 Arm_address got_address
= this->got_plt_
->address();
4143 // Write first PLT entry. All but the last word are constants.
4144 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
4145 / sizeof(plt_entry
[0]));
4146 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
4147 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
4148 // Last word in first PLT entry is &GOT[0] - .
4149 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
4150 got_address
- (plt_address
+ 16));
4151 pov
+= sizeof(first_plt_entry
);
4153 unsigned char* got_pov
= got_view
;
4155 memset(got_pov
, 0, 12);
4158 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
4159 unsigned int plt_offset
= sizeof(first_plt_entry
);
4160 unsigned int plt_rel_offset
= 0;
4161 unsigned int got_offset
= 12;
4162 const unsigned int count
= this->count_
;
4163 for (unsigned int i
= 0;
4166 pov
+= sizeof(plt_entry
),
4168 plt_offset
+= sizeof(plt_entry
),
4169 plt_rel_offset
+= rel_size
,
4172 // Set and adjust the PLT entry itself.
4173 int32_t offst
= ((got_address
+ got_offset
)
4174 - (plt_address
+ plt_offset
+ 8));
4176 gold_assert(offst
>= 0 && offst
< 0x0fffffff);
4177 uint32_t plt_insn0
= plt_entry
[0] | ((offst
>> 20) & 0xff);
4178 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
4179 uint32_t plt_insn1
= plt_entry
[1] | ((offst
>> 12) & 0xff);
4180 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
4181 uint32_t plt_insn2
= plt_entry
[2] | (offst
& 0xfff);
4182 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
4184 // Set the entry in the GOT.
4185 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
4188 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4189 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4191 of
->write_output_view(off
, oview_size
, oview
);
4192 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4195 // Create a PLT entry for a global symbol.
4197 template<bool big_endian
>
4199 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* alayout
,
4202 if (gsym
->has_plt_offset())
4205 if (this->plt_
== NULL
)
4207 // Create the GOT sections first.
4208 this->got_section(symtab
, alayout
);
4210 this->plt_
= new Output_data_plt_arm
<big_endian
>(alayout
, this->got_plt_
);
4211 alayout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
4213 | elfcpp::SHF_EXECINSTR
),
4216 this->plt_
->add_entry(gsym
);
4219 // Report an unsupported relocation against a local symbol.
4221 template<bool big_endian
>
4223 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
4224 Sized_relobj
<32, big_endian
>* object
,
4225 unsigned int r_type
)
4227 gold_error(_("%s: unsupported reloc %u against local symbol"),
4228 object
->name().c_str(), r_type
);
4231 // We are about to emit a dynamic relocation of type R_TYPE. If the
4232 // dynamic linker does not support it, issue an error. The GNU linker
4233 // only issues a non-PIC error for an allocated read-only section.
4234 // Here we know the section is allocated, but we don't know that it is
4235 // read-only. But we check for all the relocation types which the
4236 // glibc dynamic linker supports, so it seems appropriate to issue an
4237 // error even if the section is not read-only.
4239 template<bool big_endian
>
4241 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
4242 unsigned int r_type
)
4246 // These are the relocation types supported by glibc for ARM.
4247 case elfcpp::R_ARM_RELATIVE
:
4248 case elfcpp::R_ARM_COPY
:
4249 case elfcpp::R_ARM_GLOB_DAT
:
4250 case elfcpp::R_ARM_JUMP_SLOT
:
4251 case elfcpp::R_ARM_ABS32
:
4252 case elfcpp::R_ARM_ABS32_NOI
:
4253 case elfcpp::R_ARM_PC24
:
4254 // FIXME: The following 3 types are not supported by Android's dynamic
4256 case elfcpp::R_ARM_TLS_DTPMOD32
:
4257 case elfcpp::R_ARM_TLS_DTPOFF32
:
4258 case elfcpp::R_ARM_TLS_TPOFF32
:
4262 // This prevents us from issuing more than one error per reloc
4263 // section. But we can still wind up issuing more than one
4264 // error per object file.
4265 if (this->issued_non_pic_error_
)
4267 object
->error(_("requires unsupported dynamic reloc; "
4268 "recompile with -fPIC"));
4269 this->issued_non_pic_error_
= true;
4272 case elfcpp::R_ARM_NONE
:
4277 // Scan a relocation for a local symbol.
4278 // FIXME: This only handles a subset of relocation types used by Android
4279 // on ARM v5te devices.
4281 template<bool big_endian
>
4283 Target_arm
<big_endian
>::Scan::local(Symbol_table
* symtab
,
4286 Sized_relobj
<32, big_endian
>* object
,
4287 unsigned int data_shndx
,
4288 Output_section
* output_section
,
4289 const elfcpp::Rel
<32, big_endian
>& reloc
,
4290 unsigned int r_type
,
4291 const elfcpp::Sym
<32, big_endian
>&)
4293 r_type
= get_real_reloc_type(r_type
);
4296 case elfcpp::R_ARM_NONE
:
4299 case elfcpp::R_ARM_ABS32
:
4300 case elfcpp::R_ARM_ABS32_NOI
:
4301 // If building a shared library (or a position-independent
4302 // executable), we need to create a dynamic relocation for
4303 // this location. The relocation applied at link time will
4304 // apply the link-time value, so we flag the location with
4305 // an R_ARM_RELATIVE relocation so the dynamic loader can
4306 // relocate it easily.
4307 if (parameters
->options().output_is_position_independent())
4309 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4310 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4311 // If we are to add more other reloc types than R_ARM_ABS32,
4312 // we need to add check_non_pic(object, r_type) here.
4313 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
4314 output_section
, data_shndx
,
4315 reloc
.get_r_offset());
4319 case elfcpp::R_ARM_REL32
:
4320 case elfcpp::R_ARM_THM_CALL
:
4321 case elfcpp::R_ARM_CALL
:
4322 case elfcpp::R_ARM_PREL31
:
4323 case elfcpp::R_ARM_JUMP24
:
4324 case elfcpp::R_ARM_PLT32
:
4325 case elfcpp::R_ARM_THM_ABS5
:
4326 case elfcpp::R_ARM_ABS8
:
4327 case elfcpp::R_ARM_ABS12
:
4328 case elfcpp::R_ARM_ABS16
:
4329 case elfcpp::R_ARM_BASE_ABS
:
4330 case elfcpp::R_ARM_MOVW_ABS_NC
:
4331 case elfcpp::R_ARM_MOVT_ABS
:
4332 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4333 case elfcpp::R_ARM_THM_MOVT_ABS
:
4334 case elfcpp::R_ARM_MOVW_PREL_NC
:
4335 case elfcpp::R_ARM_MOVT_PREL
:
4336 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4337 case elfcpp::R_ARM_THM_MOVT_PREL
:
4340 case elfcpp::R_ARM_GOTOFF32
:
4341 // We need a GOT section:
4342 target
->got_section(symtab
, alayout
);
4345 case elfcpp::R_ARM_BASE_PREL
:
4346 // FIXME: What about this?
4349 case elfcpp::R_ARM_GOT_BREL
:
4350 case elfcpp::R_ARM_GOT_PREL
:
4352 // The symbol requires a GOT entry.
4353 Output_data_got
<32, big_endian
>* got
=
4354 target
->got_section(symtab
, alayout
);
4355 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4356 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
4358 // If we are generating a shared object, we need to add a
4359 // dynamic RELATIVE relocation for this symbol's GOT entry.
4360 if (parameters
->options().output_is_position_independent())
4362 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4363 unsigned int rsym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
4364 rel_dyn
->add_local_relative(
4365 object
, rsym
, elfcpp::R_ARM_RELATIVE
, got
,
4366 object
->local_got_offset(rsym
, GOT_TYPE_STANDARD
));
4372 case elfcpp::R_ARM_TARGET1
:
4373 // This should have been mapped to another type already.
4375 case elfcpp::R_ARM_COPY
:
4376 case elfcpp::R_ARM_GLOB_DAT
:
4377 case elfcpp::R_ARM_JUMP_SLOT
:
4378 case elfcpp::R_ARM_RELATIVE
:
4379 // These are relocations which should only be seen by the
4380 // dynamic linker, and should never be seen here.
4381 gold_error(_("%s: unexpected reloc %u in object file"),
4382 object
->name().c_str(), r_type
);
4386 unsupported_reloc_local(object
, r_type
);
4391 // Report an unsupported relocation against a global symbol.
4393 template<bool big_endian
>
4395 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
4396 Sized_relobj
<32, big_endian
>* object
,
4397 unsigned int r_type
,
4400 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4401 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4404 // Scan a relocation for a global symbol.
4405 // FIXME: This only handles a subset of relocation types used by Android
4406 // on ARM v5te devices.
4408 template<bool big_endian
>
4410 Target_arm
<big_endian
>::Scan::global(Symbol_table
* symtab
,
4413 Sized_relobj
<32, big_endian
>* object
,
4414 unsigned int data_shndx
,
4415 Output_section
* output_section
,
4416 const elfcpp::Rel
<32, big_endian
>& reloc
,
4417 unsigned int r_type
,
4420 r_type
= get_real_reloc_type(r_type
);
4423 case elfcpp::R_ARM_NONE
:
4426 case elfcpp::R_ARM_ABS32
:
4427 case elfcpp::R_ARM_ABS32_NOI
:
4429 // Make a dynamic relocation if necessary.
4430 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
4432 if (target
->may_need_copy_reloc(gsym
))
4434 target
->copy_reloc(symtab
, alayout
, object
,
4435 data_shndx
, output_section
, gsym
, reloc
);
4437 else if (gsym
->can_use_relative_reloc(false))
4439 // If we are to add more other reloc types than R_ARM_ABS32,
4440 // we need to add check_non_pic(object, r_type) here.
4441 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4442 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
4443 output_section
, object
,
4444 data_shndx
, reloc
.get_r_offset());
4448 // If we are to add more other reloc types than R_ARM_ABS32,
4449 // we need to add check_non_pic(object, r_type) here.
4450 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4451 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4452 data_shndx
, reloc
.get_r_offset());
4458 case elfcpp::R_ARM_MOVW_ABS_NC
:
4459 case elfcpp::R_ARM_MOVT_ABS
:
4460 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
4461 case elfcpp::R_ARM_THM_MOVT_ABS
:
4462 case elfcpp::R_ARM_MOVW_PREL_NC
:
4463 case elfcpp::R_ARM_MOVT_PREL
:
4464 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
4465 case elfcpp::R_ARM_THM_MOVT_PREL
:
4468 case elfcpp::R_ARM_THM_ABS5
:
4469 case elfcpp::R_ARM_ABS8
:
4470 case elfcpp::R_ARM_ABS12
:
4471 case elfcpp::R_ARM_ABS16
:
4472 case elfcpp::R_ARM_BASE_ABS
:
4474 // No dynamic relocs of this kinds.
4475 // Report the error in case of PIC.
4476 int flags
= Symbol::NON_PIC_REF
;
4477 if (gsym
->type() == elfcpp::STT_FUNC
4478 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4479 flags
|= Symbol::FUNCTION_CALL
;
4480 if (gsym
->needs_dynamic_reloc(flags
))
4481 check_non_pic(object
, r_type
);
4485 case elfcpp::R_ARM_REL32
:
4486 case elfcpp::R_ARM_PREL31
:
4488 // Make a dynamic relocation if necessary.
4489 int flags
= Symbol::NON_PIC_REF
;
4490 if (gsym
->needs_dynamic_reloc(flags
))
4492 if (target
->may_need_copy_reloc(gsym
))
4494 target
->copy_reloc(symtab
, alayout
, object
,
4495 data_shndx
, output_section
, gsym
, reloc
);
4499 check_non_pic(object
, r_type
);
4500 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4501 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4502 data_shndx
, reloc
.get_r_offset());
4508 case elfcpp::R_ARM_JUMP24
:
4509 case elfcpp::R_ARM_THM_JUMP24
:
4510 case elfcpp::R_ARM_CALL
:
4511 case elfcpp::R_ARM_THM_CALL
:
4513 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
4514 target
->make_plt_entry(symtab
, alayout
, gsym
);
4517 // Check to see if this is a function that would need a PLT
4518 // but does not get one because the function symbol is untyped.
4519 // This happens in assembly code missing a proper .type directive.
4520 if ((!gsym
->is_undefined() || parameters
->options().shared())
4521 && !parameters
->doing_static_link()
4522 && gsym
->type() == elfcpp::STT_NOTYPE
4523 && (gsym
->is_from_dynobj()
4524 || gsym
->is_undefined()
4525 || gsym
->is_preemptible()))
4526 gold_error(_("%s is not a function."),
4527 gsym
->demangled_name().c_str());
4531 case elfcpp::R_ARM_PLT32
:
4532 // If the symbol is fully resolved, this is just a relative
4533 // local reloc. Otherwise we need a PLT entry.
4534 if (gsym
->final_value_is_known())
4536 // If building a shared library, we can also skip the PLT entry
4537 // if the symbol is defined in the output file and is protected
4539 if (gsym
->is_defined()
4540 && !gsym
->is_from_dynobj()
4541 && !gsym
->is_preemptible())
4543 target
->make_plt_entry(symtab
, alayout
, gsym
);
4546 case elfcpp::R_ARM_GOTOFF32
:
4547 // We need a GOT section.
4548 target
->got_section(symtab
, alayout
);
4551 case elfcpp::R_ARM_BASE_PREL
:
4552 // FIXME: What about this?
4555 case elfcpp::R_ARM_GOT_BREL
:
4556 case elfcpp::R_ARM_GOT_PREL
:
4558 // The symbol requires a GOT entry.
4559 Output_data_got
<32, big_endian
>* got
=
4560 target
->got_section(symtab
, alayout
);
4561 if (gsym
->final_value_is_known())
4562 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4565 // If this symbol is not fully resolved, we need to add a
4566 // GOT entry with a dynamic relocation.
4567 Reloc_section
* rel_dyn
= target
->rel_dyn_section(alayout
);
4568 if (gsym
->is_from_dynobj()
4569 || gsym
->is_undefined()
4570 || gsym
->is_preemptible())
4571 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
4572 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
4575 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
4576 rel_dyn
->add_global_relative(
4577 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
4578 gsym
->got_offset(GOT_TYPE_STANDARD
));
4584 case elfcpp::R_ARM_TARGET1
:
4585 // This should have been mapped to another type already.
4587 case elfcpp::R_ARM_COPY
:
4588 case elfcpp::R_ARM_GLOB_DAT
:
4589 case elfcpp::R_ARM_JUMP_SLOT
:
4590 case elfcpp::R_ARM_RELATIVE
:
4591 // These are relocations which should only be seen by the
4592 // dynamic linker, and should never be seen here.
4593 gold_error(_("%s: unexpected reloc %u in object file"),
4594 object
->name().c_str(), r_type
);
4598 unsupported_reloc_global(object
, r_type
, gsym
);
4603 // Process relocations for gc.
4605 template<bool big_endian
>
4607 Target_arm
<big_endian
>::gc_process_relocs(Symbol_table
* symtab
,
4609 Sized_relobj
<32, big_endian
>* object
,
4610 unsigned int data_shndx
,
4612 const unsigned char* prelocs
,
4614 Output_section
* output_section
,
4615 bool needs_special_offset_handling
,
4616 size_t local_symbol_count
,
4617 const unsigned char* plocal_symbols
)
4619 typedef Target_arm
<big_endian
> Arm
;
4620 typedef typename Target_arm
<big_endian
>::Scan scan
;
4622 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, scan
>(
4631 needs_special_offset_handling
,
4636 // Scan relocations for a section.
4638 template<bool big_endian
>
4640 Target_arm
<big_endian
>::scan_relocs(Symbol_table
* symtab
,
4642 Sized_relobj
<32, big_endian
>* object
,
4643 unsigned int data_shndx
,
4644 unsigned int sh_type
,
4645 const unsigned char* prelocs
,
4647 Output_section
* output_section
,
4648 bool needs_special_offset_handling
,
4649 size_t local_symbol_count
,
4650 const unsigned char* plocal_symbols
)
4652 typedef typename Target_arm
<big_endian
>::Scan scan
;
4653 if (sh_type
== elfcpp::SHT_RELA
)
4655 gold_error(_("%s: unsupported RELA reloc section"),
4656 object
->name().c_str());
4660 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, scan
>(
4669 needs_special_offset_handling
,
4674 // Finalize the sections.
4676 template<bool big_endian
>
4678 Target_arm
<big_endian
>::do_finalize_sections(
4680 const Input_objects
* input_objects
,
4681 Symbol_table
* symtab
)
4683 // Merge processor-specific flags.
4684 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
4685 p
!= input_objects
->relobj_end();
4688 Arm_relobj
<big_endian
>* arm_relobj
=
4689 Arm_relobj
<big_endian
>::as_arm_relobj(*p
);
4690 this->merge_processor_specific_flags(
4692 arm_relobj
->processor_specific_flags());
4693 this->merge_object_attributes(arm_relobj
->name().c_str(),
4694 arm_relobj
->attributes_section_data());
4698 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
4699 p
!= input_objects
->dynobj_end();
4702 Arm_dynobj
<big_endian
>* arm_dynobj
=
4703 Arm_dynobj
<big_endian
>::as_arm_dynobj(*p
);
4704 this->merge_processor_specific_flags(
4706 arm_dynobj
->processor_specific_flags());
4707 this->merge_object_attributes(arm_dynobj
->name().c_str(),
4708 arm_dynobj
->attributes_section_data());
4712 Object_attribute
* attr
=
4713 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch
);
4714 if (attr
->int_value() > elfcpp::TAG_CPU_ARCH_V4
)
4715 this->set_may_use_blx(true);
4717 // Fill in some more dynamic tags.
4718 Output_data_dynamic
* const odyn
= alayout
->dynamic_data();
4721 if (this->got_plt_
!= NULL
4722 && this->got_plt_
->output_section() != NULL
)
4723 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
4725 if (this->plt_
!= NULL
4726 && this->plt_
->output_section() != NULL
)
4728 const Output_data
* od
= this->plt_
->rel_plt();
4729 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
4730 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
4731 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
4734 if (this->rel_dyn_
!= NULL
4735 && this->rel_dyn_
->output_section() != NULL
)
4737 const Output_data
* od
= this->rel_dyn_
;
4738 odyn
->add_section_address(elfcpp::DT_REL
, od
);
4739 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
4740 odyn
->add_constant(elfcpp::DT_RELENT
,
4741 elfcpp::Elf_sizes
<32>::rel_size
);
4744 if (!parameters
->options().shared())
4746 // The value of the DT_DEBUG tag is filled in by the dynamic
4747 // linker at run time, and used by the debugger.
4748 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
4752 // Emit any relocs we saved in an attempt to avoid generating COPY
4754 if (this->copy_relocs_
.any_saved_relocs())
4755 this->copy_relocs_
.emit(this->rel_dyn_section(alayout
));
4757 // Handle the .ARM.exidx section.
4758 Output_section
* exidx_section
= alayout
->find_output_section(".ARM.exidx");
4759 if (exidx_section
!= NULL
4760 && exidx_section
->type() == elfcpp::SHT_ARM_EXIDX
4761 && !parameters
->options().relocatable())
4763 // Create __exidx_start and __exdix_end symbols.
4764 symtab
->define_in_output_data("__exidx_start", NULL
, exidx_section
,
4765 0, 0, elfcpp::STT_OBJECT
,
4766 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
4768 symtab
->define_in_output_data("__exidx_end", NULL
, exidx_section
,
4769 0, 0, elfcpp::STT_OBJECT
,
4770 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
, 0,
4773 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4774 // the .ARM.exidx section.
4775 if (!alayout
->script_options()->saw_phdrs_clause())
4777 gold_assert(alayout
->find_output_segment(elfcpp::PT_ARM_EXIDX
, 0, 0)
4779 Output_segment
* exidx_segment
=
4780 alayout
->make_output_segment(elfcpp::PT_ARM_EXIDX
, elfcpp::PF_R
);
4781 exidx_segment
->add_output_section(exidx_section
, elfcpp::PF_R
,
4786 // Create an .ARM.attributes section if there is not one already.
4787 Output_attributes_section_data
* as
=
4788 new Output_attributes_section_data(*this->attributes_section_data_
);
4789 alayout
->add_output_section_data(".ARM.attributes",
4790 elfcpp::SHT_ARM_ATTRIBUTES
, 0, as
, false);
4793 // Return whether a direct absolute static relocation needs to be applied.
4794 // In cases where Scan::local() or Scan::global() has created
4795 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4796 // of the relocation is carried in the data, and we must not
4797 // apply the static relocation.
4799 template<bool big_endian
>
4801 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
4802 const Sized_symbol
<32>* gsym
,
4805 Output_section
* output_section
)
4807 // If the output section is not allocated, then we didn't call
4808 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4810 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
4813 // For local symbols, we will have created a non-RELATIVE dynamic
4814 // relocation only if (a) the output is position independent,
4815 // (b) the relocation is absolute (not pc- or segment-relative), and
4816 // (c) the relocation is not 32 bits wide.
4818 return !(parameters
->options().output_is_position_independent()
4819 && (ref_flags
& Symbol::ABSOLUTE_REF
)
4822 // For global symbols, we use the same helper routines used in the
4823 // scan pass. If we did not create a dynamic relocation, or if we
4824 // created a RELATIVE dynamic relocation, we should apply the static
4826 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
4827 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
4828 && gsym
->can_use_relative_reloc(ref_flags
4829 & Symbol::FUNCTION_CALL
);
4830 return !has_dyn
|| is_rel
;
4833 // Perform a relocation.
4835 template<bool big_endian
>
4837 Target_arm
<big_endian
>::Relocate::relocate(
4838 const Relocate_info
<32, big_endian
>* relinfo
,
4840 Output_section
*output_section
,
4842 const elfcpp::Rel
<32, big_endian
>& rel
,
4843 unsigned int r_type
,
4844 const Sized_symbol
<32>* gsym
,
4845 const Symbol_value
<32>* psymval
,
4846 unsigned char* view
,
4847 Arm_address address
,
4848 section_size_type
/* view_size */ )
4850 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
4852 r_type
= get_real_reloc_type(r_type
);
4854 const Arm_relobj
<big_endian
>* object
=
4855 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
4857 // If the final branch target of a relocation is THUMB instruction, this
4858 // is 1. Otherwise it is 0.
4859 Arm_address thumb_bit
= 0;
4860 Symbol_value
<32> symval
;
4861 bool is_weakly_undefined_without_plt
= false;
4862 if (relnum
!= Target_arm
<big_endian
>::fake_relnum_for_stubs
)
4866 // This is a global symbol. Determine if we use PLT and if the
4867 // final target is THUMB.
4868 if (gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
4870 // This uses a PLT, change the symbol value.
4871 symval
.set_output_value(target
->plt_section()->address()
4872 + gsym
->plt_offset());
4875 else if (gsym
->is_weak_undefined())
4877 // This is a weakly undefined symbol and we do not use PLT
4878 // for this relocation. A branch targeting this symbol will
4879 // be converted into an NOP.
4880 is_weakly_undefined_without_plt
= true;
4884 // Set thumb bit if symbol:
4885 // -Has type STT_ARM_TFUNC or
4886 // -Has type STT_FUNC, is defined and with LSB in value set.
4888 (((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
4889 || (gsym
->type() == elfcpp::STT_FUNC
4890 && !gsym
->is_undefined()
4891 && ((psymval
->value(object
, 0) & 1) != 0)))
4898 // This is a local symbol. Determine if the final target is THUMB.
4899 // We saved this information when all the local symbols were read.
4900 elfcpp::Elf_types
<32>::Elf_WXword r_info
= rel
.get_r_info();
4901 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
4902 thumb_bit
= object
->local_symbol_is_thumb_function(r_sym
) ? 1 : 0;
4907 // This is a fake relocation synthesized for a stub. It does not have
4908 // a real symbol. We just look at the LSB of the symbol value to
4909 // determine if the target is THUMB or not.
4910 thumb_bit
= ((psymval
->value(object
, 0) & 1) != 0);
4913 // Strip LSB if this points to a THUMB target.
4915 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
4916 && ((psymval
->value(object
, 0) & 1) != 0))
4918 Arm_address stripped_value
=
4919 psymval
->value(object
, 0) & ~static_cast<Arm_address
>(1);
4920 symval
.set_output_value(stripped_value
);
4924 // Get the GOT offset if needed.
4925 // The GOT pointer points to the end of the GOT section.
4926 // We need to subtract the size of the GOT section to get
4927 // the actual offset to use in the relocation.
4928 bool have_got_offset
= false;
4929 unsigned int got_offset
= 0;
4932 case elfcpp::R_ARM_GOT_BREL
:
4933 case elfcpp::R_ARM_GOT_PREL
:
4936 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4937 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4938 - target
->got_size());
4942 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4943 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4944 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4945 - target
->got_size());
4947 have_got_offset
= true;
4954 // To look up relocation stubs, we need to pass the symbol table index of
4956 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
4958 typename
Arm_relocate_functions::Status reloc_status
=
4959 Arm_relocate_functions::STATUS_OKAY
;
4962 case elfcpp::R_ARM_NONE
:
4965 case elfcpp::R_ARM_ABS8
:
4966 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4968 reloc_status
= Arm_relocate_functions::abs8(view
, object
, psymval
);
4971 case elfcpp::R_ARM_ABS12
:
4972 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4974 reloc_status
= Arm_relocate_functions::abs12(view
, object
, psymval
);
4977 case elfcpp::R_ARM_ABS16
:
4978 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
4980 reloc_status
= Arm_relocate_functions::abs16(view
, object
, psymval
);
4983 case elfcpp::R_ARM_ABS32
:
4984 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4986 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4990 case elfcpp::R_ARM_ABS32_NOI
:
4991 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
4993 // No thumb bit for this relocation: (S + A)
4994 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
4998 case elfcpp::R_ARM_MOVW_ABS_NC
:
4999 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5001 reloc_status
= Arm_relocate_functions::movw_abs_nc(view
, object
,
5005 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5006 "a shared object; recompile with -fPIC"));
5009 case elfcpp::R_ARM_MOVT_ABS
:
5010 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5012 reloc_status
= Arm_relocate_functions::movt_abs(view
, object
, psymval
);
5014 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5015 "a shared object; recompile with -fPIC"));
5018 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5019 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5021 reloc_status
= Arm_relocate_functions::thm_movw_abs_nc(view
, object
,
5025 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5026 "making a shared object; recompile with -fPIC"));
5029 case elfcpp::R_ARM_THM_MOVT_ABS
:
5030 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5032 reloc_status
= Arm_relocate_functions::thm_movt_abs(view
, object
,
5035 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5036 "making a shared object; recompile with -fPIC"));
5039 case elfcpp::R_ARM_MOVW_PREL_NC
:
5040 reloc_status
= Arm_relocate_functions::movw_prel_nc(view
, object
,
5045 case elfcpp::R_ARM_MOVT_PREL
:
5046 reloc_status
= Arm_relocate_functions::movt_prel(view
, object
,
5050 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5051 reloc_status
= Arm_relocate_functions::thm_movw_prel_nc(view
, object
,
5056 case elfcpp::R_ARM_THM_MOVT_PREL
:
5057 reloc_status
= Arm_relocate_functions::thm_movt_prel(view
, object
,
5061 case elfcpp::R_ARM_REL32
:
5062 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5063 address
, thumb_bit
);
5066 case elfcpp::R_ARM_THM_ABS5
:
5067 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
5069 reloc_status
= Arm_relocate_functions::thm_abs5(view
, object
, psymval
);
5072 case elfcpp::R_ARM_THM_CALL
:
5074 Arm_relocate_functions::thm_call(relinfo
, view
, gsym
, object
, r_sym
,
5075 psymval
, address
, thumb_bit
,
5076 is_weakly_undefined_without_plt
);
5079 case elfcpp::R_ARM_XPC25
:
5081 Arm_relocate_functions::xpc25(relinfo
, view
, gsym
, object
, r_sym
,
5082 psymval
, address
, thumb_bit
,
5083 is_weakly_undefined_without_plt
);
5086 case elfcpp::R_ARM_THM_XPC22
:
5088 Arm_relocate_functions::thm_xpc22(relinfo
, view
, gsym
, object
, r_sym
,
5089 psymval
, address
, thumb_bit
,
5090 is_weakly_undefined_without_plt
);
5093 case elfcpp::R_ARM_GOTOFF32
:
5095 Arm_address got_origin
;
5096 got_origin
= target
->got_plt_section()->address();
5097 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
5098 got_origin
, thumb_bit
);
5102 case elfcpp::R_ARM_BASE_PREL
:
5105 // Get the addressing origin of the output segment defining the
5106 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5107 gold_assert(gsym
!= NULL
);
5108 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5109 origin
= gsym
->output_segment()->vaddr();
5110 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5111 origin
= gsym
->output_data()->address();
5114 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5115 _("cannot find origin of R_ARM_BASE_PREL"));
5118 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
5122 case elfcpp::R_ARM_BASE_ABS
:
5124 if (!should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
5129 // Get the addressing origin of the output segment defining
5130 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5132 // R_ARM_BASE_ABS with the NULL symbol will give the
5133 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5134 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5135 origin
= target
->got_plt_section()->address();
5136 else if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
5137 origin
= gsym
->output_segment()->vaddr();
5138 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
5139 origin
= gsym
->output_data()->address();
5142 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5143 _("cannot find origin of R_ARM_BASE_ABS"));
5147 reloc_status
= Arm_relocate_functions::base_abs(view
, origin
);
5151 case elfcpp::R_ARM_GOT_BREL
:
5152 gold_assert(have_got_offset
);
5153 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
5156 case elfcpp::R_ARM_GOT_PREL
:
5157 gold_assert(have_got_offset
);
5158 // Get the address origin for GOT PLT, which is allocated right
5159 // after the GOT section, to calculate an absolute address of
5160 // the symbol GOT entry (got_origin + got_offset).
5161 Arm_address got_origin
;
5162 got_origin
= target
->got_plt_section()->address();
5163 reloc_status
= Arm_relocate_functions::got_prel(view
,
5164 got_origin
+ got_offset
,
5168 case elfcpp::R_ARM_PLT32
:
5169 gold_assert(gsym
== NULL
5170 || gsym
->has_plt_offset()
5171 || gsym
->final_value_is_known()
5172 || (gsym
->is_defined()
5173 && !gsym
->is_from_dynobj()
5174 && !gsym
->is_preemptible()));
5176 Arm_relocate_functions::plt32(relinfo
, view
, gsym
, object
, r_sym
,
5177 psymval
, address
, thumb_bit
,
5178 is_weakly_undefined_without_plt
);
5181 case elfcpp::R_ARM_CALL
:
5183 Arm_relocate_functions::call(relinfo
, view
, gsym
, object
, r_sym
,
5184 psymval
, address
, thumb_bit
,
5185 is_weakly_undefined_without_plt
);
5188 case elfcpp::R_ARM_JUMP24
:
5190 Arm_relocate_functions::jump24(relinfo
, view
, gsym
, object
, r_sym
,
5191 psymval
, address
, thumb_bit
,
5192 is_weakly_undefined_without_plt
);
5195 case elfcpp::R_ARM_THM_JUMP24
:
5197 Arm_relocate_functions::thm_jump24(relinfo
, view
, gsym
, object
, r_sym
,
5198 psymval
, address
, thumb_bit
,
5199 is_weakly_undefined_without_plt
);
5202 case elfcpp::R_ARM_PREL31
:
5203 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
5204 address
, thumb_bit
);
5207 case elfcpp::R_ARM_TARGET1
:
5208 // This should have been mapped to another type already.
5210 case elfcpp::R_ARM_COPY
:
5211 case elfcpp::R_ARM_GLOB_DAT
:
5212 case elfcpp::R_ARM_JUMP_SLOT
:
5213 case elfcpp::R_ARM_RELATIVE
:
5214 // These are relocations which should only be seen by the
5215 // dynamic linker, and should never be seen here.
5216 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5217 _("unexpected reloc %u in object file"),
5222 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5223 _("unsupported reloc %u"),
5228 // Report any errors.
5229 switch (reloc_status
)
5231 case Arm_relocate_functions::STATUS_OKAY
:
5233 case Arm_relocate_functions::STATUS_OVERFLOW
:
5234 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
5235 _("relocation overflow in relocation %u"),
5238 case Arm_relocate_functions::STATUS_BAD_RELOC
:
5239 gold_error_at_location(
5243 _("unexpected opcode while processing relocation %u"),
5253 // Relocate section data.
5255 template<bool big_endian
>
5257 Target_arm
<big_endian
>::relocate_section(
5258 const Relocate_info
<32, big_endian
>* relinfo
,
5259 unsigned int sh_type
,
5260 const unsigned char* prelocs
,
5262 Output_section
* output_section
,
5263 bool needs_special_offset_handling
,
5264 unsigned char* view
,
5265 Arm_address address
,
5266 section_size_type view_size
,
5267 const Reloc_symbol_changes
* reloc_symbol_changes
)
5269 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
5270 gold_assert(sh_type
== elfcpp::SHT_REL
);
5272 Arm_input_section
<big_endian
>* arm_input_section
=
5273 this->find_arm_input_section(relinfo
->object
, relinfo
->data_shndx
);
5275 // This is an ARM input section and the view covers the whole output
5277 if (arm_input_section
!= NULL
)
5279 gold_assert(needs_special_offset_handling
);
5280 Arm_address section_address
= arm_input_section
->address();
5281 section_size_type section_size
= arm_input_section
->data_size();
5283 gold_assert((arm_input_section
->address() >= address
)
5284 && ((arm_input_section
->address()
5285 + arm_input_section
->data_size())
5286 <= (address
+ view_size
)));
5288 off_t off
= section_address
- address
;
5291 view_size
= section_size
;
5294 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
5301 needs_special_offset_handling
,
5305 reloc_symbol_changes
);
5308 // Return the size of a relocation while scanning during a relocatable
5311 template<bool big_endian
>
5313 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
5314 unsigned int r_type
,
5317 r_type
= get_real_reloc_type(r_type
);
5320 case elfcpp::R_ARM_NONE
:
5323 case elfcpp::R_ARM_ABS8
:
5326 case elfcpp::R_ARM_ABS16
:
5327 case elfcpp::R_ARM_THM_ABS5
:
5330 case elfcpp::R_ARM_ABS32
:
5331 case elfcpp::R_ARM_ABS32_NOI
:
5332 case elfcpp::R_ARM_ABS12
:
5333 case elfcpp::R_ARM_BASE_ABS
:
5334 case elfcpp::R_ARM_REL32
:
5335 case elfcpp::R_ARM_THM_CALL
:
5336 case elfcpp::R_ARM_GOTOFF32
:
5337 case elfcpp::R_ARM_BASE_PREL
:
5338 case elfcpp::R_ARM_GOT_BREL
:
5339 case elfcpp::R_ARM_GOT_PREL
:
5340 case elfcpp::R_ARM_PLT32
:
5341 case elfcpp::R_ARM_CALL
:
5342 case elfcpp::R_ARM_JUMP24
:
5343 case elfcpp::R_ARM_PREL31
:
5344 case elfcpp::R_ARM_MOVW_ABS_NC
:
5345 case elfcpp::R_ARM_MOVT_ABS
:
5346 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
5347 case elfcpp::R_ARM_THM_MOVT_ABS
:
5348 case elfcpp::R_ARM_MOVW_PREL_NC
:
5349 case elfcpp::R_ARM_MOVT_PREL
:
5350 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
5351 case elfcpp::R_ARM_THM_MOVT_PREL
:
5354 case elfcpp::R_ARM_TARGET1
:
5355 // This should have been mapped to another type already.
5357 case elfcpp::R_ARM_COPY
:
5358 case elfcpp::R_ARM_GLOB_DAT
:
5359 case elfcpp::R_ARM_JUMP_SLOT
:
5360 case elfcpp::R_ARM_RELATIVE
:
5361 // These are relocations which should only be seen by the
5362 // dynamic linker, and should never be seen here.
5363 gold_error(_("%s: unexpected reloc %u in object file"),
5364 object
->name().c_str(), r_type
);
5368 object
->error(_("unsupported reloc %u in object file"), r_type
);
5373 // Scan the relocs during a relocatable link.
5375 template<bool big_endian
>
5377 Target_arm
<big_endian
>::scan_relocatable_relocs(
5378 Symbol_table
* symtab
,
5380 Sized_relobj
<32, big_endian
>* object
,
5381 unsigned int data_shndx
,
5382 unsigned int sh_type
,
5383 const unsigned char* prelocs
,
5385 Output_section
* output_section
,
5386 bool needs_special_offset_handling
,
5387 size_t local_symbol_count
,
5388 const unsigned char* plocal_symbols
,
5389 Relocatable_relocs
* rr
)
5391 gold_assert(sh_type
== elfcpp::SHT_REL
);
5393 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
5394 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
5396 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
5397 Scan_relocatable_relocs
>(
5405 needs_special_offset_handling
,
5411 // Relocate a section during a relocatable link.
5413 template<bool big_endian
>
5415 Target_arm
<big_endian
>::relocate_for_relocatable(
5416 const Relocate_info
<32, big_endian
>* relinfo
,
5417 unsigned int sh_type
,
5418 const unsigned char* prelocs
,
5420 Output_section
* output_section
,
5421 off_t offset_in_output_section
,
5422 const Relocatable_relocs
* rr
,
5423 unsigned char* view
,
5424 Arm_address view_address
,
5425 section_size_type view_size
,
5426 unsigned char* reloc_view
,
5427 section_size_type reloc_view_size
)
5429 gold_assert(sh_type
== elfcpp::SHT_REL
);
5431 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
5436 offset_in_output_section
,
5445 // Return the value to use for a dynamic symbol which requires special
5446 // treatment. This is how we support equality comparisons of function
5447 // pointers across shared library boundaries, as described in the
5448 // processor specific ABI supplement.
5450 template<bool big_endian
>
5452 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
5454 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5455 return this->plt_section()->address() + gsym
->plt_offset();
5458 // Map platform-specific relocs to real relocs
5460 template<bool big_endian
>
5462 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
5466 case elfcpp::R_ARM_TARGET1
:
5467 // This is either R_ARM_ABS32 or R_ARM_REL32;
5468 return elfcpp::R_ARM_ABS32
;
5470 case elfcpp::R_ARM_TARGET2
:
5471 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5472 return elfcpp::R_ARM_GOT_PREL
;
5479 // Whether if two EABI versions V1 and V2 are compatible.
5481 template<bool big_endian
>
5483 Target_arm
<big_endian
>::are_eabi_versions_compatible(
5484 elfcpp::Elf_Word v1
,
5485 elfcpp::Elf_Word v2
)
5487 // v4 and v5 are the same spec before and after it was released,
5488 // so allow mixing them.
5489 if ((v1
== elfcpp::EF_ARM_EABI_VER4
&& v2
== elfcpp::EF_ARM_EABI_VER5
)
5490 || (v1
== elfcpp::EF_ARM_EABI_VER5
&& v2
== elfcpp::EF_ARM_EABI_VER4
))
5496 // Combine FLAGS from an input object called NAME and the processor-specific
5497 // flags in the ELF header of the output. Much of this is adapted from the
5498 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5499 // in bfd/elf32-arm.c.
5501 template<bool big_endian
>
5503 Target_arm
<big_endian
>::merge_processor_specific_flags(
5504 const std::string
& name
,
5505 elfcpp::Elf_Word flags
)
5507 if (this->are_processor_specific_flags_set())
5509 elfcpp::Elf_Word out_flags
= this->processor_specific_flags();
5511 // Nothing to merge if flags equal to those in output.
5512 if (flags
== out_flags
)
5515 // Complain about various flag mismatches.
5516 elfcpp::Elf_Word version1
= elfcpp::arm_eabi_version(flags
);
5517 elfcpp::Elf_Word version2
= elfcpp::arm_eabi_version(out_flags
);
5518 if (!this->are_eabi_versions_compatible(version1
, version2
))
5519 gold_error(_("Source object %s has EABI version %d but output has "
5520 "EABI version %d."),
5522 (flags
& elfcpp::EF_ARM_EABIMASK
) >> 24,
5523 (out_flags
& elfcpp::EF_ARM_EABIMASK
) >> 24);
5527 // If the input is the default architecture and had the default
5528 // flags then do not bother setting the flags for the output
5529 // architecture, instead allow future merges to do this. If no
5530 // future merges ever set these flags then they will retain their
5531 // uninitialised values, which surprise surprise, correspond
5532 // to the default values.
5536 // This is the first time, just copy the flags.
5537 // We only copy the EABI version for now.
5538 this->set_processor_specific_flags(flags
& elfcpp::EF_ARM_EABIMASK
);
5542 // Adjust ELF file header.
5543 template<bool big_endian
>
5545 Target_arm
<big_endian
>::do_adjust_elf_header(
5546 unsigned char* view
,
5549 gold_assert(len
== elfcpp::Elf_sizes
<32>::ehdr_size
);
5551 elfcpp::Ehdr
<32, big_endian
> ehdr(view
);
5552 unsigned char e_ident
[elfcpp::EI_NIDENT
];
5553 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
5555 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5556 == elfcpp::EF_ARM_EABI_UNKNOWN
)
5557 e_ident
[elfcpp::EI_OSABI
] = elfcpp::ELFOSABI_ARM
;
5559 e_ident
[elfcpp::EI_OSABI
] = 0;
5560 e_ident
[elfcpp::EI_ABIVERSION
] = 0;
5562 // FIXME: Do EF_ARM_BE8 adjustment.
5564 elfcpp::Ehdr_write
<32, big_endian
> oehdr(view
);
5565 oehdr
.put_e_ident(e_ident
);
5568 // do_make_elf_object to override the same function in the base class.
5569 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5570 // to store ARM specific information. Hence we need to have our own
5571 // ELF object creation.
5573 template<bool big_endian
>
5575 Target_arm
<big_endian
>::do_make_elf_object(
5576 const std::string
& name
,
5577 Input_file
* input_file
,
5578 off_t off
, const elfcpp::Ehdr
<32, big_endian
>& ehdr
)
5580 int et
= ehdr
.get_e_type();
5581 if (et
== elfcpp::ET_REL
)
5583 Arm_relobj
<big_endian
>* obj
=
5584 new Arm_relobj
<big_endian
>(name
, input_file
, off
, ehdr
);
5588 else if (et
== elfcpp::ET_DYN
)
5590 Sized_dynobj
<32, big_endian
>* obj
=
5591 new Arm_dynobj
<big_endian
>(name
, input_file
, off
, ehdr
);
5597 gold_error(_("%s: unsupported ELF file type %d"),
5603 // Read the architecture from the Tag_also_compatible_with attribute, if any.
5604 // Returns -1 if no architecture could be read.
5605 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5607 template<bool big_endian
>
5609 Target_arm
<big_endian
>::get_secondary_compatible_arch(
5610 const Attributes_section_data
* pasd
)
5612 const Object_attribute
*known_attributes
=
5613 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
5615 // Note: the tag and its argument below are uleb128 values, though
5616 // currently-defined values fit in one byte for each.
5617 const std::string
& sv
=
5618 known_attributes
[elfcpp::Tag_also_compatible_with
].string_value();
5620 && sv
.data()[0] == elfcpp::Tag_CPU_arch
5621 && (sv
.data()[1] & 128) != 128)
5622 return sv
.data()[1];
5624 // This tag is "safely ignorable", so don't complain if it looks funny.
5628 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5629 // The tag is removed if ARCH is -1.
5630 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5632 template<bool big_endian
>
5634 Target_arm
<big_endian
>::set_secondary_compatible_arch(
5635 Attributes_section_data
* pasd
,
5638 Object_attribute
*known_attributes
=
5639 pasd
->known_attributes(Object_attribute::OBJ_ATTR_PROC
);
5643 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value("");
5647 // Note: the tag and its argument below are uleb128 values, though
5648 // currently-defined values fit in one byte for each.
5650 sv
[0] = elfcpp::Tag_CPU_arch
;
5651 gold_assert(arch
!= 0);
5655 known_attributes
[elfcpp::Tag_also_compatible_with
].set_string_value(sv
);
5658 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
5660 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
5662 template<bool big_endian
>
5664 Target_arm
<big_endian
>::tag_cpu_arch_combine(
5667 int* secondary_compat_out
,
5669 int secondary_compat
)
5671 #define T(X) elfcpp::TAG_CPU_ARCH_##X
5672 static const int v6t2
[] =
5684 static const int v6k
[] =
5697 static const int v7
[] =
5711 static const int v6_m
[] =
5726 static const int v6s_m
[] =
5742 static const int v7e_m
[] =
5759 static const int v4t_plus_v6_m
[] =
5775 T(V4T_PLUS_V6_M
) // V4T plus V6_M.
5777 static const int *comb
[] =
5785 // Pseudo-architecture.
5789 // Check we've not got a higher architecture than we know about.
5791 if (oldtag
>= elfcpp::MAX_TAG_CPU_ARCH
|| newtag
>= elfcpp::MAX_TAG_CPU_ARCH
)
5793 gold_error(_("%s: unknown CPU architecture"), name
);
5797 // Override old tag if we have a Tag_also_compatible_with on the output.
5799 if ((oldtag
== T(V6_M
) && *secondary_compat_out
== T(V4T
))
5800 || (oldtag
== T(V4T
) && *secondary_compat_out
== T(V6_M
)))
5801 oldtag
= T(V4T_PLUS_V6_M
);
5803 // And override the new tag if we have a Tag_also_compatible_with on the
5806 if ((newtag
== T(V6_M
) && secondary_compat
== T(V4T
))
5807 || (newtag
== T(V4T
) && secondary_compat
== T(V6_M
)))
5808 newtag
= T(V4T_PLUS_V6_M
);
5810 // Architectures before V6KZ add features monotonically.
5811 int tagh
= std::max(oldtag
, newtag
);
5812 if (tagh
<= elfcpp::TAG_CPU_ARCH_V6KZ
)
5815 int tagl
= std::min(oldtag
, newtag
);
5816 int result
= comb
[tagh
- T(V6T2
)][tagl
];
5818 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
5819 // as the canonical version.
5820 if (result
== T(V4T_PLUS_V6_M
))
5823 *secondary_compat_out
= T(V6_M
);
5826 *secondary_compat_out
= -1;
5830 gold_error(_("%s: conflicting CPU architectures %d/%d"),
5831 name
, oldtag
, newtag
);
5839 // Helper to print AEABI enum tag value.
5841 template<bool big_endian
>
5843 Target_arm
<big_endian
>::aeabi_enum_name(unsigned int value
)
5845 static const char *aeabi_enum_names
[] =
5846 { "", "variable-size", "32-bit", "" };
5847 const size_t aeabi_enum_names_size
=
5848 sizeof(aeabi_enum_names
) / sizeof(aeabi_enum_names
[0]);
5850 if (value
< aeabi_enum_names_size
)
5851 return std::string(aeabi_enum_names
[value
]);
5855 sprintf(buffer
, "<unknown value %u>", value
);
5856 return std::string(buffer
);
5860 // Return the string value to store in TAG_CPU_name.
5862 template<bool big_endian
>
5864 Target_arm
<big_endian
>::tag_cpu_name_value(unsigned int value
)
5866 static const char *name_table
[] = {
5867 // These aren't real CPU names, but we can't guess
5868 // that from the architecture version alone.
5884 const size_t name_table_size
= sizeof(name_table
) / sizeof(name_table
[0]);
5886 if (value
< name_table_size
)
5887 return std::string(name_table
[value
]);
5891 sprintf(buffer
, "<unknown CPU value %u>", value
);
5892 return std::string(buffer
);
5896 // Merge object attributes from input file called NAME with those of the
5897 // output. The input object attributes are in the object pointed by PASD.
5899 template<bool big_endian
>
5901 Target_arm
<big_endian
>::merge_object_attributes(
5903 const Attributes_section_data
* pasd
)
5905 // Return if there is no attributes section data.
5909 // If output has no object attributes, just copy.
5910 if (this->attributes_section_data_
== NULL
)
5912 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
5916 const int vendor
= Object_attribute::OBJ_ATTR_PROC
;
5917 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
5918 Object_attribute
* out_attr
=
5919 this->attributes_section_data_
->known_attributes(vendor
);
5921 // This needs to happen before Tag_ABI_FP_number_model is merged. */
5922 if (in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value()
5923 != out_attr
[elfcpp::Tag_ABI_VFP_args
].int_value())
5925 // Ignore mismatches if the object doesn't use floating point. */
5926 if (out_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() == 0)
5927 out_attr
[elfcpp::Tag_ABI_VFP_args
].set_int_value(
5928 in_attr
[elfcpp::Tag_ABI_VFP_args
].int_value());
5929 else if (in_attr
[elfcpp::Tag_ABI_FP_number_model
].int_value() != 0)
5930 gold_error(_("%s uses VFP register arguments, output does not"),
5934 for (int i
= 4; i
< Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES
; ++i
)
5936 // Merge this attribute with existing attributes.
5939 case elfcpp::Tag_CPU_raw_name
:
5940 case elfcpp::Tag_CPU_name
:
5941 // These are merged after Tag_CPU_arch.
5944 case elfcpp::Tag_ABI_optimization_goals
:
5945 case elfcpp::Tag_ABI_FP_optimization_goals
:
5946 // Use the first value seen.
5949 case elfcpp::Tag_CPU_arch
:
5951 unsigned int saved_out_attr
= out_attr
->int_value();
5952 // Merge Tag_CPU_arch and Tag_also_compatible_with.
5953 int secondary_compat
=
5954 this->get_secondary_compatible_arch(pasd
);
5955 int secondary_compat_out
=
5956 this->get_secondary_compatible_arch(
5957 this->attributes_section_data_
);
5958 out_attr
[i
].set_int_value(
5959 tag_cpu_arch_combine(name
, out_attr
[i
].int_value(),
5960 &secondary_compat_out
,
5961 in_attr
[i
].int_value(),
5963 this->set_secondary_compatible_arch(this->attributes_section_data_
,
5964 secondary_compat_out
);
5966 // Merge Tag_CPU_name and Tag_CPU_raw_name.
5967 if (out_attr
[i
].int_value() == saved_out_attr
)
5968 ; // Leave the names alone.
5969 else if (out_attr
[i
].int_value() == in_attr
[i
].int_value())
5971 // The output architecture has been changed to match the
5972 // input architecture. Use the input names.
5973 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(
5974 in_attr
[elfcpp::Tag_CPU_name
].string_value());
5975 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value(
5976 in_attr
[elfcpp::Tag_CPU_raw_name
].string_value());
5980 out_attr
[elfcpp::Tag_CPU_name
].set_string_value("");
5981 out_attr
[elfcpp::Tag_CPU_raw_name
].set_string_value("");
5984 // If we still don't have a value for Tag_CPU_name,
5985 // make one up now. Tag_CPU_raw_name remains blank.
5986 if (out_attr
[elfcpp::Tag_CPU_name
].string_value() == "")
5988 const std::string cpu_name
=
5989 this->tag_cpu_name_value(out_attr
[i
].int_value());
5990 // FIXME: If we see an unknown CPU, this will be set
5991 // to "<unknown CPU n>", where n is the attribute value.
5992 // This is different from BFD, which leaves the name alone.
5993 out_attr
[elfcpp::Tag_CPU_name
].set_string_value(cpu_name
);
5998 case elfcpp::Tag_ARM_ISA_use
:
5999 case elfcpp::Tag_THUMB_ISA_use
:
6000 case elfcpp::Tag_WMMX_arch
:
6001 case elfcpp::Tag_Advanced_SIMD_arch
:
6002 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6003 case elfcpp::Tag_ABI_FP_rounding
:
6004 case elfcpp::Tag_ABI_FP_exceptions
:
6005 case elfcpp::Tag_ABI_FP_user_exceptions
:
6006 case elfcpp::Tag_ABI_FP_number_model
:
6007 case elfcpp::Tag_VFP_HP_extension
:
6008 case elfcpp::Tag_CPU_unaligned_access
:
6009 case elfcpp::Tag_T2EE_use
:
6010 case elfcpp::Tag_Virtualization_use
:
6011 case elfcpp::Tag_MPextension_use
:
6012 // Use the largest value specified.
6013 if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6014 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6017 case elfcpp::Tag_ABI_align8_preserved
:
6018 case elfcpp::Tag_ABI_PCS_RO_data
:
6019 // Use the smallest value specified.
6020 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6021 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6024 case elfcpp::Tag_ABI_align8_needed
:
6025 if ((in_attr
[i
].int_value() > 0 || out_attr
[i
].int_value() > 0)
6026 && (in_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value() == 0
6027 || (out_attr
[elfcpp::Tag_ABI_align8_preserved
].int_value()
6030 // This error message should be enabled once all non-conformant
6031 // binaries in the toolchain have had the attributes set
6033 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6037 case elfcpp::Tag_ABI_FP_denormal
:
6038 case elfcpp::Tag_ABI_PCS_GOT_use
:
6040 // These tags have 0 = don't care, 1 = strong requirement,
6041 // 2 = weak requirement.
6042 static const int order_021
[3] = {0, 2, 1};
6044 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6045 // value if greater than 2 (for future-proofing).
6046 if ((in_attr
[i
].int_value() > 2
6047 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6048 || (in_attr
[i
].int_value() <= 2
6049 && out_attr
[i
].int_value() <= 2
6050 && (order_021
[in_attr
[i
].int_value()]
6051 > order_021
[out_attr
[i
].int_value()])))
6052 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6056 case elfcpp::Tag_CPU_arch_profile
:
6057 if (out_attr
[i
].int_value() != in_attr
[i
].int_value())
6059 // 0 will merge with anything.
6060 // 'A' and 'S' merge to 'A'.
6061 // 'R' and 'S' merge to 'R'.
6062 // 'M' and 'A|R|S' is an error.
6063 if (out_attr
[i
].int_value() == 0
6064 || (out_attr
[i
].int_value() == 'S'
6065 && (in_attr
[i
].int_value() == 'A'
6066 || in_attr
[i
].int_value() == 'R')))
6067 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6068 else if (in_attr
[i
].int_value() == 0
6069 || (in_attr
[i
].int_value() == 'S'
6070 && (out_attr
[i
].int_value() == 'A'
6071 || out_attr
[i
].int_value() == 'R')))
6076 (_("conflicting architecture profiles %c/%c"),
6077 in_attr
[i
].int_value() ? in_attr
[i
].int_value() : '0',
6078 out_attr
[i
].int_value() ? out_attr
[i
].int_value() : '0');
6082 case elfcpp::Tag_VFP_arch
:
6099 // Values greater than 6 aren't defined, so just pick the
6101 if (in_attr
[i
].int_value() > 6
6102 && in_attr
[i
].int_value() > out_attr
[i
].int_value())
6104 *out_attr
= *in_attr
;
6107 // The output uses the superset of input features
6108 // (ISA version) and registers.
6109 int ver
= std::max(vfp_versions
[in_attr
[i
].int_value()].ver
,
6110 vfp_versions
[out_attr
[i
].int_value()].ver
);
6111 int regs
= std::max(vfp_versions
[in_attr
[i
].int_value()].regs
,
6112 vfp_versions
[out_attr
[i
].int_value()].regs
);
6113 // This assumes all possible supersets are also a valid
6116 for (newval
= 6; newval
> 0; newval
--)
6118 if (regs
== vfp_versions
[newval
].regs
6119 && ver
== vfp_versions
[newval
].ver
)
6122 out_attr
[i
].set_int_value(newval
);
6125 case elfcpp::Tag_PCS_config
:
6126 if (out_attr
[i
].int_value() == 0)
6127 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6128 else if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6130 // It's sometimes ok to mix different configs, so this is only
6132 gold_warning(_("%s: conflicting platform configuration"), name
);
6135 case elfcpp::Tag_ABI_PCS_R9_use
:
6136 if (in_attr
[i
].int_value() != out_attr
[i
].int_value()
6137 && out_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
6138 && in_attr
[i
].int_value() != elfcpp::AEABI_R9_unused
)
6140 gold_error(_("%s: conflicting use of R9"), name
);
6142 if (out_attr
[i
].int_value() == elfcpp::AEABI_R9_unused
)
6143 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6145 case elfcpp::Tag_ABI_PCS_RW_data
:
6146 if (in_attr
[i
].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6147 && (in_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6148 != elfcpp::AEABI_R9_SB
)
6149 && (out_attr
[elfcpp::Tag_ABI_PCS_R9_use
].int_value()
6150 != elfcpp::AEABI_R9_unused
))
6152 gold_error(_("%s: SB relative addressing conflicts with use "
6156 // Use the smallest value specified.
6157 if (in_attr
[i
].int_value() < out_attr
[i
].int_value())
6158 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6160 case elfcpp::Tag_ABI_PCS_wchar_t
:
6161 // FIXME: Make it possible to turn off this warning.
6162 if (out_attr
[i
].int_value()
6163 && in_attr
[i
].int_value()
6164 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6166 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6167 "use %u-byte wchar_t; use of wchar_t values "
6168 "across objects may fail"),
6169 name
, in_attr
[i
].int_value(),
6170 out_attr
[i
].int_value());
6172 else if (in_attr
[i
].int_value() && !out_attr
[i
].int_value())
6173 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6175 case elfcpp::Tag_ABI_enum_size
:
6176 if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_unused
)
6178 if (out_attr
[i
].int_value() == elfcpp::AEABI_enum_unused
6179 || out_attr
[i
].int_value() == elfcpp::AEABI_enum_forced_wide
)
6181 // The existing object is compatible with anything.
6182 // Use whatever requirements the new object has.
6183 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6185 // FIXME: Make it possible to turn off this warning.
6186 else if (in_attr
[i
].int_value() != elfcpp::AEABI_enum_forced_wide
6187 && out_attr
[i
].int_value() != in_attr
[i
].int_value())
6189 unsigned int in_value
= in_attr
[i
].int_value();
6190 unsigned int out_value
= out_attr
[i
].int_value();
6191 gold_warning(_("%s uses %s enums yet the output is to use "
6192 "%s enums; use of enum values across objects "
6195 this->aeabi_enum_name(in_value
).c_str(),
6196 this->aeabi_enum_name(out_value
).c_str());
6200 case elfcpp::Tag_ABI_VFP_args
:
6203 case elfcpp::Tag_ABI_WMMX_args
:
6204 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6206 gold_error(_("%s uses iWMMXt register arguments, output does "
6211 case Object_attribute::Tag_compatibility
:
6212 // Merged in target-independent code.
6214 case elfcpp::Tag_ABI_HardFP_use
:
6215 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6216 if ((in_attr
[i
].int_value() == 1 && out_attr
[i
].int_value() == 2)
6217 || (in_attr
[i
].int_value() == 2 && out_attr
[i
].int_value() == 1))
6218 out_attr
[i
].set_int_value(3);
6219 else if (in_attr
[i
].int_value() > out_attr
[i
].int_value())
6220 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6222 case elfcpp::Tag_ABI_FP_16bit_format
:
6223 if (in_attr
[i
].int_value() != 0 && out_attr
[i
].int_value() != 0)
6225 if (in_attr
[i
].int_value() != out_attr
[i
].int_value())
6226 gold_error(_("fp16 format mismatch between %s and output"),
6229 if (in_attr
[i
].int_value() != 0)
6230 out_attr
[i
].set_int_value(in_attr
[i
].int_value());
6233 case elfcpp::Tag_nodefaults
:
6234 // This tag is set if it exists, but the value is unused (and is
6235 // typically zero). We don't actually need to do anything here -
6236 // the merge happens automatically when the type flags are merged
6239 case elfcpp::Tag_also_compatible_with
:
6240 // Already done in Tag_CPU_arch.
6242 case elfcpp::Tag_conformance
:
6243 // Keep the attribute if it matches. Throw it away otherwise.
6244 // No attribute means no claim to conform.
6245 if (in_attr
[i
].string_value() != out_attr
[i
].string_value())
6246 out_attr
[i
].set_string_value("");
6251 const char* err_object
= NULL
;
6253 // The "known_obj_attributes" table does contain some undefined
6254 // attributes. Ensure that there are unused.
6255 if (out_attr
[i
].int_value() != 0
6256 || out_attr
[i
].string_value() != "")
6257 err_object
= "output";
6258 else if (in_attr
[i
].int_value() != 0
6259 || in_attr
[i
].string_value() != "")
6262 if (err_object
!= NULL
)
6264 // Attribute numbers >=64 (mod 128) can be safely ignored.
6266 gold_error(_("%s: unknown mandatory EABI object attribute "
6270 gold_warning(_("%s: unknown EABI object attribute %d"),
6274 // Only pass on attributes that match in both inputs.
6275 if (!in_attr
[i
].matches(out_attr
[i
]))
6277 out_attr
[i
].set_int_value(0);
6278 out_attr
[i
].set_string_value("");
6283 // If out_attr was copied from in_attr then it won't have a type yet.
6284 if (in_attr
[i
].type() && !out_attr
[i
].type())
6285 out_attr
[i
].set_type(in_attr
[i
].type());
6288 // Merge Tag_compatibility attributes and any common GNU ones.
6289 this->attributes_section_data_
->merge(name
, pasd
);
6291 // Check for any attributes not known on ARM.
6292 typedef Vendor_object_attributes::Other_attributes Other_attributes
;
6293 const Other_attributes
* in_other_attributes
= pasd
->other_attributes(vendor
);
6294 Other_attributes::const_iterator in_iter
= in_other_attributes
->begin();
6295 Other_attributes
* out_other_attributes
=
6296 this->attributes_section_data_
->other_attributes(vendor
);
6297 Other_attributes::iterator out_iter
= out_other_attributes
->begin();
6299 while (in_iter
!= in_other_attributes
->end()
6300 || out_iter
!= out_other_attributes
->end())
6302 const char* err_object
= NULL
;
6305 // The tags for each list are in numerical order.
6306 // If the tags are equal, then merge.
6307 if (out_iter
!= out_other_attributes
->end()
6308 && (in_iter
== in_other_attributes
->end()
6309 || in_iter
->first
> out_iter
->first
))
6311 // This attribute only exists in output. We can't merge, and we
6312 // don't know what the tag means, so delete it.
6313 err_object
= "output";
6314 err_tag
= out_iter
->first
;
6315 int saved_tag
= out_iter
->first
;
6316 delete out_iter
->second
;
6317 out_other_attributes
->erase(out_iter
);
6318 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6320 else if (in_iter
!= in_other_attributes
->end()
6321 && (out_iter
!= out_other_attributes
->end()
6322 || in_iter
->first
< out_iter
->first
))
6324 // This attribute only exists in input. We can't merge, and we
6325 // don't know what the tag means, so ignore it.
6327 err_tag
= in_iter
->first
;
6330 else // The tags are equal.
6332 // As present, all attributes in the list are unknown, and
6333 // therefore can't be merged meaningfully.
6334 err_object
= "output";
6335 err_tag
= out_iter
->first
;
6337 // Only pass on attributes that match in both inputs.
6338 if (!in_iter
->second
->matches(*(out_iter
->second
)))
6340 // No match. Delete the attribute.
6341 int saved_tag
= out_iter
->first
;
6342 delete out_iter
->second
;
6343 out_other_attributes
->erase(out_iter
);
6344 out_iter
= out_other_attributes
->upper_bound(saved_tag
);
6348 // Matched. Keep the attribute and move to the next.
6356 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6357 if ((err_tag
& 127) < 64)
6359 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6360 err_object
, err_tag
);
6364 gold_warning(_("%s: unknown EABI object attribute %d"),
6365 err_object
, err_tag
);
6371 // Return whether a relocation type used the LSB to distinguish THUMB
6373 template<bool big_endian
>
6375 Target_arm
<big_endian
>::reloc_uses_thumb_bit(unsigned int r_type
)
6379 case elfcpp::R_ARM_PC24
:
6380 case elfcpp::R_ARM_ABS32
:
6381 case elfcpp::R_ARM_REL32
:
6382 case elfcpp::R_ARM_SBREL32
:
6383 case elfcpp::R_ARM_THM_CALL
:
6384 case elfcpp::R_ARM_GLOB_DAT
:
6385 case elfcpp::R_ARM_JUMP_SLOT
:
6386 case elfcpp::R_ARM_GOTOFF32
:
6387 case elfcpp::R_ARM_PLT32
:
6388 case elfcpp::R_ARM_CALL
:
6389 case elfcpp::R_ARM_JUMP24
:
6390 case elfcpp::R_ARM_THM_JUMP24
:
6391 case elfcpp::R_ARM_SBREL31
:
6392 case elfcpp::R_ARM_PREL31
:
6393 case elfcpp::R_ARM_MOVW_ABS_NC
:
6394 case elfcpp::R_ARM_MOVW_PREL_NC
:
6395 case elfcpp::R_ARM_THM_MOVW_ABS_NC
:
6396 case elfcpp::R_ARM_THM_MOVW_PREL_NC
:
6397 case elfcpp::R_ARM_THM_JUMP19
:
6398 case elfcpp::R_ARM_THM_ALU_PREL_11_0
:
6399 case elfcpp::R_ARM_ALU_PC_G0_NC
:
6400 case elfcpp::R_ARM_ALU_PC_G0
:
6401 case elfcpp::R_ARM_ALU_PC_G1_NC
:
6402 case elfcpp::R_ARM_ALU_PC_G1
:
6403 case elfcpp::R_ARM_ALU_PC_G2
:
6404 case elfcpp::R_ARM_ALU_SB_G0_NC
:
6405 case elfcpp::R_ARM_ALU_SB_G0
:
6406 case elfcpp::R_ARM_ALU_SB_G1_NC
:
6407 case elfcpp::R_ARM_ALU_SB_G1
:
6408 case elfcpp::R_ARM_ALU_SB_G2
:
6409 case elfcpp::R_ARM_MOVW_BREL_NC
:
6410 case elfcpp::R_ARM_MOVW_BREL
:
6411 case elfcpp::R_ARM_THM_MOVW_BREL_NC
:
6412 case elfcpp::R_ARM_THM_MOVW_BREL
:
6419 // Stub-generation methods for Target_arm.
6421 // Make a new Arm_input_section object.
6423 template<bool big_endian
>
6424 Arm_input_section
<big_endian
>*
6425 Target_arm
<big_endian
>::new_arm_input_section(
6427 unsigned int sec_shndx
)
6429 Input_section_specifier
iss(rel_obj
, sec_shndx
);
6431 Arm_input_section
<big_endian
>* arm_input_section
=
6432 new Arm_input_section
<big_endian
>(rel_obj
, sec_shndx
);
6433 arm_input_section
->init();
6435 // Register new Arm_input_section in map for look-up.
6436 std::pair
<typename
Arm_input_section_map::iterator
, bool> ins
=
6437 this->arm_input_section_map_
.insert(std::make_pair(iss
, arm_input_section
));
6439 // Make sure that it we have not created another Arm_input_section
6440 // for this input section already.
6441 gold_assert(ins
.second
);
6443 return arm_input_section
;
6446 // Find the Arm_input_section object corresponding to the SHNDX-th input
6447 // section of RELOBJ.
6449 template<bool big_endian
>
6450 Arm_input_section
<big_endian
>*
6451 Target_arm
<big_endian
>::find_arm_input_section(
6453 unsigned int sec_shndx
) const
6455 Input_section_specifier
iss(rel_obj
, sec_shndx
);
6456 typename
Arm_input_section_map::const_iterator p
=
6457 this->arm_input_section_map_
.find(iss
);
6458 return (p
!= this->arm_input_section_map_
.end()) ? p
->second
: NULL
;
6461 // Make a new stub table.
6463 template<bool big_endian
>
6464 Stub_table
<big_endian
>*
6465 Target_arm
<big_endian
>::new_stub_table(Arm_input_section
<big_endian
>* owner
)
6467 Stub_table
<big_endian
>* stubtable
=
6468 new Stub_table
<big_endian
>(owner
);
6469 this->stub_tables_
.push_back(stubtable
);
6471 stubtable
->set_address(owner
->address() + owner
->data_size());
6472 stubtable
->set_file_offset(owner
->offset() + owner
->data_size());
6473 stubtable
->finalize_data_size();
6478 // Scan a relocation for stub generation.
6480 template<bool big_endian
>
6482 Target_arm
<big_endian
>::scan_reloc_for_stub(
6483 const Relocate_info
<32, big_endian
>* relinfo
,
6484 unsigned int r_type
,
6485 const Sized_symbol
<32>* gsym
,
6487 const Symbol_value
<32>* psymval
,
6488 elfcpp::Elf_types
<32>::Elf_Swxword addend
,
6489 Arm_address address
)
6491 typedef typename Target_arm
<big_endian
>::Relocate relocate
;
6493 const Arm_relobj
<big_endian
>* arm_relobj
=
6494 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6496 bool target_is_thumb
;
6497 Symbol_value
<32> symval
;
6500 // This is a global symbol. Determine if we use PLT and if the
6501 // final target is THUMB.
6502 if (gsym
->use_plt_offset(relocate::reloc_is_non_pic(r_type
)))
6504 // This uses a PLT, change the symbol value.
6505 symval
.set_output_value(this->plt_section()->address()
6506 + gsym
->plt_offset());
6508 target_is_thumb
= false;
6510 else if (gsym
->is_undefined())
6511 // There is no need to generate a stub symbol is undefined.
6516 ((gsym
->type() == elfcpp::STT_ARM_TFUNC
)
6517 || (gsym
->type() == elfcpp::STT_FUNC
6518 && !gsym
->is_undefined()
6519 && ((psymval
->value(arm_relobj
, 0) & 1) != 0)));
6524 // This is a local symbol. Determine if the final target is THUMB.
6525 target_is_thumb
= arm_relobj
->local_symbol_is_thumb_function(r_sym
);
6528 // Strip LSB if this points to a THUMB target.
6530 && Target_arm
<big_endian
>::reloc_uses_thumb_bit(r_type
)
6531 && ((psymval
->value(arm_relobj
, 0) & 1) != 0))
6533 Arm_address stripped_value
=
6534 psymval
->value(arm_relobj
, 0) & ~static_cast<Arm_address
>(1);
6535 symval
.set_output_value(stripped_value
);
6539 // Get the symbol value.
6540 Symbol_value
<32>::Value value
= psymval
->value(arm_relobj
, 0);
6542 // Owing to pipelining, the PC relative branches below actually skip
6543 // two instructions when the branch offset is 0.
6544 Arm_address destination
;
6547 case elfcpp::R_ARM_CALL
:
6548 case elfcpp::R_ARM_JUMP24
:
6549 case elfcpp::R_ARM_PLT32
:
6551 destination
= value
+ addend
+ 8;
6553 case elfcpp::R_ARM_THM_CALL
:
6554 case elfcpp::R_ARM_THM_XPC22
:
6555 case elfcpp::R_ARM_THM_JUMP24
:
6556 case elfcpp::R_ARM_THM_JUMP19
:
6558 destination
= value
+ addend
+ 4;
6564 Stub_type stub_type
=
6565 Reloc_stub::stub_type_for_reloc(r_type
, address
, destination
,
6568 // This reloc does not need a stub.
6569 if (stub_type
== arm_stub_none
)
6572 // Try looking up an existing stub from a stub table.
6573 Stub_table
<big_endian
>* stubtable
=
6574 arm_relobj
->stub_table(relinfo
->data_shndx
);
6575 gold_assert(stubtable
!= NULL
);
6577 // Locate stub by destination.
6578 Reloc_stub::Key
stub_key(stub_type
, gsym
, arm_relobj
, r_sym
, addend
);
6580 // Create a stub if there is not one already
6581 Reloc_stub
* stub
= stubtable
->find_reloc_stub(stub_key
);
6584 // create a new stub and add it to stub table.
6585 stub
= this->stub_factory().make_reloc_stub(stub_type
);
6586 stubtable
->add_reloc_stub(stub
, stub_key
);
6589 // Record the destination address.
6590 stub
->set_destination_address(destination
6591 | (target_is_thumb
? 1 : 0));
6594 // This function scans a relocation sections for stub generation.
6595 // The template parameter Relocate must be a class type which provides
6596 // a single function, relocate(), which implements the machine
6597 // specific part of a relocation.
6599 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
6600 // SHT_REL or SHT_RELA.
6602 // PRELOCS points to the relocation data. RELOC_COUNT is the number
6603 // of relocs. OUTPUT_SECTION is the output section.
6604 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6605 // mapped to output offsets.
6607 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
6608 // VIEW_SIZE is the size. These refer to the input section, unless
6609 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6610 // the output section.
6612 template<bool big_endian
>
6613 template<int sh_type
>
6615 Target_arm
<big_endian
>::scan_reloc_section_for_stubs(
6616 const Relocate_info
<32, big_endian
>* relinfo
,
6617 const unsigned char* prelocs
,
6619 Output_section
* output_section
,
6620 bool needs_special_offset_handling
,
6621 const unsigned char* view
,
6622 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
6625 typedef typename Reloc_types
<sh_type
, 32, big_endian
>::Reloc Reltype
;
6626 const int reloc_size
=
6627 Reloc_types
<sh_type
, 32, big_endian
>::reloc_size
;
6629 Arm_relobj
<big_endian
>* arm_object
=
6630 Arm_relobj
<big_endian
>::as_arm_relobj(relinfo
->object
);
6631 unsigned int local_count
= arm_object
->local_symbol_count();
6633 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
6635 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6637 Reltype
reloc(prelocs
);
6639 typename
elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
6640 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(r_info
);
6641 unsigned int r_type
= elfcpp::elf_r_type
<32>(r_info
);
6643 r_type
= this->get_real_reloc_type(r_type
);
6645 // Only a few relocation types need stubs.
6646 if ((r_type
!= elfcpp::R_ARM_CALL
)
6647 && (r_type
!= elfcpp::R_ARM_JUMP24
)
6648 && (r_type
!= elfcpp::R_ARM_PLT32
)
6649 && (r_type
!= elfcpp::R_ARM_THM_CALL
)
6650 && (r_type
!= elfcpp::R_ARM_THM_XPC22
)
6651 && (r_type
!= elfcpp::R_ARM_THM_JUMP24
)
6652 && (r_type
!= elfcpp::R_ARM_THM_JUMP19
))
6655 section_offset_type off
=
6656 convert_to_section_size_type(reloc
.get_r_offset());
6658 if (needs_special_offset_handling
)
6660 off
= output_section
->output_offset(relinfo
->object
,
6661 relinfo
->data_shndx
,
6668 Stub_addend_reader
<sh_type
, big_endian
> stub_addend_reader
;
6669 elfcpp::Elf_types
<32>::Elf_Swxword addend
=
6670 stub_addend_reader(r_type
, view
+ off
, reloc
);
6672 const Sized_symbol
<32>* sym
;
6674 Symbol_value
<32> symval
;
6675 const Symbol_value
<32> *psymval
;
6676 if (r_sym
< local_count
)
6679 psymval
= arm_object
->local_symbol(r_sym
);
6681 // If the local symbol belongs to a section we are discarding,
6682 // and that section is a debug section, try to find the
6683 // corresponding kept section and map this symbol to its
6684 // counterpart in the kept section. The symbol must not
6685 // correspond to a section we are folding.
6687 unsigned int sec_shndx
= psymval
->input_shndx(&is_ordinary
);
6689 && sec_shndx
!= elfcpp::SHN_UNDEF
6690 && !arm_object
->is_section_included(sec_shndx
)
6691 && !(relinfo
->symtab
->is_section_folded(arm_object
, sec_shndx
)))
6693 if (comdat_behavior
== CB_UNDETERMINED
)
6696 arm_object
->section_name(relinfo
->data_shndx
);
6697 comdat_behavior
= get_comdat_behavior(name
.c_str());
6699 if (comdat_behavior
== CB_PRETEND
)
6702 typename
elfcpp::Elf_types
<32>::Elf_Addr value
=
6703 arm_object
->map_to_kept_section(sec_shndx
, &found
);
6705 symval
.set_output_value(value
+ psymval
->input_value());
6707 symval
.set_output_value(0);
6711 symval
.set_output_value(0);
6713 symval
.set_no_output_symtab_entry();
6719 const Symbol
* gsym
= arm_object
->global_symbol(r_sym
);
6720 gold_assert(gsym
!= NULL
);
6721 if (gsym
->is_forwarder())
6722 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
6724 sym
= static_cast<const Sized_symbol
<32>*>(gsym
);
6725 if (sym
->has_symtab_index())
6726 symval
.set_output_symtab_index(sym
->symtab_index());
6728 symval
.set_no_output_symtab_entry();
6730 // We need to compute the would-be final value of this global
6732 const Symbol_table
* symtab
= relinfo
->symtab
;
6733 const Sized_symbol
<32>* sized_symbol
=
6734 symtab
->get_sized_symbol
<32>(gsym
);
6735 Symbol_table::Compute_final_value_status status
;
6737 symtab
->compute_final_value
<32>(sized_symbol
, &status
);
6739 // Skip this if the symbol has not output section.
6740 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
6743 symval
.set_output_value(value
);
6747 // If symbol is a section symbol, we don't know the actual type of
6748 // destination. Give up.
6749 if (psymval
->is_section_symbol())
6752 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
6753 addend
, view_address
+ off
);
6757 // Scan an input section for stub generation.
6759 template<bool big_endian
>
6761 Target_arm
<big_endian
>::scan_section_for_stubs(
6762 const Relocate_info
<32, big_endian
>* relinfo
,
6763 unsigned int sh_type
,
6764 const unsigned char* prelocs
,
6766 Output_section
* output_section
,
6767 bool needs_special_offset_handling
,
6768 const unsigned char* view
,
6769 Arm_address view_address
,
6770 section_size_type view_size
)
6772 if (sh_type
== elfcpp::SHT_REL
)
6773 this->scan_reloc_section_for_stubs
<elfcpp::SHT_REL
>(
6778 needs_special_offset_handling
,
6782 else if (sh_type
== elfcpp::SHT_RELA
)
6783 // We do not support RELA type relocations yet. This is provided for
6785 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
6790 needs_special_offset_handling
,
6798 // Group input sections for stub generation.
6800 // We goup input sections in an output sections so that the total size,
6801 // including any padding space due to alignment is smaller than GROUP_SIZE
6802 // unless the only input section in group is bigger than GROUP_SIZE already.
6803 // Then an ARM stub table is created to follow the last input section
6804 // in group. For each group an ARM stub table is created an is placed
6805 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
6806 // extend the group after the stub table.
6808 template<bool big_endian
>
6810 Target_arm
<big_endian
>::group_sections(
6812 section_size_type group_size
,
6813 bool stubs_always_after_branch
)
6815 // Group input sections and insert stub table
6816 Layout::Section_list section_list
;
6817 alayout
->get_allocated_sections(§ion_list
);
6818 for (Layout::Section_list::const_iterator p
= section_list
.begin();
6819 p
!= section_list
.end();
6822 Arm_output_section
<big_endian
>* output_section
=
6823 Arm_output_section
<big_endian
>::as_arm_output_section(*p
);
6824 output_section
->group_sections(group_size
, stubs_always_after_branch
,
6829 // Relaxation hook. This is where we do stub generation.
6831 template<bool big_endian
>
6833 Target_arm
<big_endian
>::do_relax(
6835 const Input_objects
* input_objects
,
6836 Symbol_table
* symtab
,
6839 // No need to generate stubs if this is a relocatable link.
6840 gold_assert(!parameters
->options().relocatable());
6842 // If this is the first pass, we need to group input sections into
6846 // Determine the stub group size. The group size is the absolute
6847 // value of the parameter --stub-group-size. If --stub-group-size
6848 // is passed a negative value, we restict stubs to be always after
6849 // the stubbed branches.
6850 int32_t stub_group_size_param
=
6851 parameters
->options().stub_group_size();
6852 bool stubs_always_after_branch
= stub_group_size_param
< 0;
6853 section_size_type stub_group_size
= abs(stub_group_size_param
);
6855 if (stub_group_size
== 1)
6858 // Thumb branch range is +-4MB has to be used as the default
6859 // maximum size (a given section can contain both ARM and Thumb
6860 // code, so the worst case has to be taken into account).
6862 // This value is 24K less than that, which allows for 2025
6863 // 12-byte stubs. If we exceed that, then we will fail to link.
6864 // The user will have to relink with an explicit group size
6866 stub_group_size
= 4170000;
6869 group_sections(alayout
, stub_group_size
, stubs_always_after_branch
);
6872 // clear changed flags for all stub_tables
6873 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
6874 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
6875 sp
!= this->stub_tables_
.end();
6877 (*sp
)->set_has_been_changed(false);
6879 // scan relocs for stubs
6880 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
6881 op
!= input_objects
->relobj_end();
6884 Arm_relobj
<big_endian
>* arm_relobj
=
6885 Arm_relobj
<big_endian
>::as_arm_relobj(*op
);
6886 arm_relobj
->scan_sections_for_stubs(this, symtab
, alayout
);
6889 bool any_stub_table_changed
= false;
6890 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
6891 (sp
!= this->stub_tables_
.end()) && !any_stub_table_changed
;
6894 if ((*sp
)->has_been_changed())
6895 any_stub_table_changed
= true;
6898 return any_stub_table_changed
;
6903 template<bool big_endian
>
6905 Target_arm
<big_endian
>::relocate_stub(
6907 const Relocate_info
<32, big_endian
>* relinfo
,
6908 Output_section
* output_section
,
6909 unsigned char* view
,
6910 Arm_address address
,
6911 section_size_type view_size
)
6914 const Stub_template
* stubtemplate
= stub
->stub_template();
6915 for (size_t i
= 0; i
< stubtemplate
->reloc_count(); i
++)
6917 size_t reloc_insn_index
= stubtemplate
->reloc_insn_index(i
);
6918 const Insn_template
* insn
= &stubtemplate
->insns()[reloc_insn_index
];
6920 unsigned int r_type
= insn
->r_type();
6921 section_size_type reloc_offset
= stubtemplate
->reloc_offset(i
);
6922 section_size_type reloc_size
= insn
->size();
6923 gold_assert(reloc_offset
+ reloc_size
<= view_size
);
6925 // This is the address of the stub destination.
6926 Arm_address target
= stub
->reloc_target(i
);
6927 Symbol_value
<32> symval
;
6928 symval
.set_output_value(target
);
6930 // Synthesize a fake reloc just in case. We don't have a symbol so
6932 unsigned char reloc_buffer
[elfcpp::Elf_sizes
<32>::rel_size
];
6933 memset(reloc_buffer
, 0, sizeof(reloc_buffer
));
6934 elfcpp::Rel_write
<32, big_endian
> reloc_write(reloc_buffer
);
6935 reloc_write
.put_r_offset(reloc_offset
);
6936 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(0, r_type
));
6937 elfcpp::Rel
<32, big_endian
> rel(reloc_buffer
);
6939 relocate
.relocate(relinfo
, this, output_section
,
6940 this->fake_relnum_for_stubs
, rel
, r_type
,
6941 NULL
, &symval
, view
+ reloc_offset
,
6942 address
+ reloc_offset
, reloc_size
);
6946 // Determine whether an object attribute tag takes an integer, a
6949 template<bool big_endian
>
6951 Target_arm
<big_endian
>::do_attribute_arg_type(int tag
) const
6953 if (tag
== Object_attribute::Tag_compatibility
)
6954 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
6955 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL
);
6956 else if (tag
== elfcpp::Tag_nodefaults
)
6957 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
6958 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT
);
6959 else if (tag
== elfcpp::Tag_CPU_raw_name
|| tag
== elfcpp::Tag_CPU_name
)
6960 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL
;
6962 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL
;
6964 return ((tag
& 1) != 0
6965 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
6966 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
6969 // Reorder attributes.
6971 // The ABI defines that Tag_conformance should be emitted first, and that
6972 // Tag_nodefaults should be second (if either is defined). This sets those
6973 // two positions, and bumps up the position of all the remaining tags to
6976 template<bool big_endian
>
6978 Target_arm
<big_endian
>::do_attributes_order(int num
) const
6980 // Reorder the known object attributes in output. We want to move
6981 // Tag_conformance to position 4 and Tag_conformance to position 5
6982 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
6984 return elfcpp::Tag_conformance
;
6986 return elfcpp::Tag_nodefaults
;
6987 if ((num
- 2) < elfcpp::Tag_nodefaults
)
6989 if ((num
- 1) < elfcpp::Tag_conformance
)
6994 template<bool big_endian
>
6995 class Target_selector_arm
: public Target_selector
6998 Target_selector_arm()
6999 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
7000 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
7004 do_instantiate_target()
7005 { return new Target_arm
<big_endian
>(); }
7008 Target_selector_arm
<false> target_selector_arm
;
7009 Target_selector_arm
<true> target_selector_armbe
;
7011 } // End anonymous namespace.