1 // aarch64.cc -- aarch64 target support for gold.
3 // Copyright (C) 2014 Free Software Foundation, Inc.
4 // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
45 #include "aarch64-reloc-property.h"
47 // The first three .got.plt entries are reserved.
48 const int32_t AARCH64_GOTPLT_RESERVE_COUNT
= 3;
55 template<int size
, bool big_endian
>
56 class Output_data_plt_aarch64
;
58 template<int size
, bool big_endian
>
59 class Output_data_plt_aarch64_standard
;
61 template<int size
, bool big_endian
>
64 template<int size
, bool big_endian
>
65 class AArch64_relocate_functions
;
67 // Output_data_got_aarch64 class.
69 template<int size
, bool big_endian
>
70 class Output_data_got_aarch64
: public Output_data_got
<size
, big_endian
>
73 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
74 Output_data_got_aarch64(Symbol_table
* symtab
, Layout
* layout
)
75 : Output_data_got
<size
, big_endian
>(),
76 symbol_table_(symtab
), layout_(layout
)
79 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
80 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
81 // applied in a static link.
83 add_static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
84 { this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, gsym
)); }
87 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
88 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
89 // relocation that needs to be applied in a static link.
91 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
92 Sized_relobj_file
<size
, big_endian
>* relobj
,
95 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
101 // Write out the GOT table.
103 do_write(Output_file
* of
) {
104 // The first entry in the GOT is the address of the .dynamic section.
105 gold_assert(this->data_size() >= size
/ 8);
106 Output_section
* dynamic
= this->layout_
->dynamic_section();
107 Valtype dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
108 this->replace_constant(0, dynamic_addr
);
109 Output_data_got
<size
, big_endian
>::do_write(of
);
111 // Handling static relocs
112 if (this->static_relocs_
.empty())
115 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
117 gold_assert(parameters
->doing_static_link());
118 const off_t offset
= this->offset();
119 const section_size_type oview_size
=
120 convert_to_section_size_type(this->data_size());
121 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
123 Output_segment
* tls_segment
= this->layout_
->tls_segment();
124 gold_assert(tls_segment
!= NULL
);
126 AArch64_address aligned_tcb_address
=
127 align_address(Target_aarch64
<size
,big_endian
>::TCB_SIZE
,
128 tls_segment
->maximum_alignment());
130 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
132 Static_reloc
& reloc(this->static_relocs_
[i
]);
133 AArch64_address value
;
135 if (!reloc
.symbol_is_global())
137 Sized_relobj_file
<size
, big_endian
>* object
= reloc
.relobj();
138 const Symbol_value
<size
>* psymval
=
139 reloc
.relobj()->local_symbol(reloc
.index());
141 // We are doing static linking. Issue an error and skip this
142 // relocation if the symbol is undefined or in a discarded_section.
144 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
145 if ((shndx
== elfcpp::SHN_UNDEF
)
147 && shndx
!= elfcpp::SHN_UNDEF
148 && !object
->is_section_included(shndx
)
149 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
151 gold_error(_("undefined or discarded local symbol %u from "
152 " object %s in GOT"),
153 reloc
.index(), reloc
.relobj()->name().c_str());
156 value
= psymval
->value(object
, 0);
160 const Symbol
* gsym
= reloc
.symbol();
161 gold_assert(gsym
!= NULL
);
162 if (gsym
->is_forwarder())
163 gsym
= this->symbol_table_
->resolve_forwards(gsym
);
165 // We are doing static linking. Issue an error and skip this
166 // relocation if the symbol is undefined or in a discarded_section
167 // unless it is a weakly_undefined symbol.
168 if ((gsym
->is_defined_in_discarded_section()
169 || gsym
->is_undefined())
170 && !gsym
->is_weak_undefined())
172 gold_error(_("undefined or discarded symbol %s in GOT"),
177 if (!gsym
->is_weak_undefined())
179 const Sized_symbol
<size
>* sym
=
180 static_cast<const Sized_symbol
<size
>*>(gsym
);
181 value
= sym
->value();
187 unsigned got_offset
= reloc
.got_offset();
188 gold_assert(got_offset
< oview_size
);
190 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Valtype
;
191 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
193 switch (reloc
.r_type())
195 case elfcpp::R_AARCH64_TLS_DTPREL64
:
198 case elfcpp::R_AARCH64_TLS_TPREL64
:
199 x
= value
+ aligned_tcb_address
;
204 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, x
);
207 of
->write_output_view(offset
, oview_size
, oview
);
211 // Symbol table of the output object.
212 Symbol_table
* symbol_table_
;
213 // A pointer to the Layout class, so that we can find the .dynamic
214 // section when we write out the GOT section.
218 // This class represent dynamic relocations that need to be applied by
219 // gold because we are using TLS relocations in a static link.
223 Static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
224 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(true)
225 { this->u_
.global
.symbol
= gsym
; }
227 Static_reloc(unsigned int got_offset
, unsigned int r_type
,
228 Sized_relobj_file
<size
, big_endian
>* relobj
, unsigned int index
)
229 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(false)
231 this->u_
.local
.relobj
= relobj
;
232 this->u_
.local
.index
= index
;
235 // Return the GOT offset.
238 { return this->got_offset_
; }
243 { return this->r_type_
; }
245 // Whether the symbol is global or not.
247 symbol_is_global() const
248 { return this->symbol_is_global_
; }
250 // For a relocation against a global symbol, the global symbol.
254 gold_assert(this->symbol_is_global_
);
255 return this->u_
.global
.symbol
;
258 // For a relocation against a local symbol, the defining object.
259 Sized_relobj_file
<size
, big_endian
>*
262 gold_assert(!this->symbol_is_global_
);
263 return this->u_
.local
.relobj
;
266 // For a relocation against a local symbol, the local symbol index.
270 gold_assert(!this->symbol_is_global_
);
271 return this->u_
.local
.index
;
275 // GOT offset of the entry to which this relocation is applied.
276 unsigned int got_offset_
;
277 // Type of relocation.
278 unsigned int r_type_
;
279 // Whether this relocation is against a global symbol.
280 bool symbol_is_global_
;
281 // A global or local symbol.
286 // For a global symbol, the symbol itself.
291 // For a local symbol, the object defining object.
292 Sized_relobj_file
<size
, big_endian
>* relobj
;
293 // For a local symbol, the symbol index.
297 }; // End of inner class Static_reloc
299 std::vector
<Static_reloc
> static_relocs_
;
302 AArch64_reloc_property_table
* aarch64_reloc_property_table
= NULL
;
304 // The aarch64 target class.
306 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
307 template<int size
, bool big_endian
>
308 class Target_aarch64
: public Sized_target
<size
, big_endian
>
311 typedef Target_aarch64
<size
,big_endian
> This
;
312 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
314 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
315 const static int TCB_SIZE
= size
/ 8 * 2;
317 Target_aarch64(const Target::Target_info
* info
= &aarch64_info
)
318 : Sized_target
<size
, big_endian
>(info
),
319 got_(NULL
), plt_(NULL
), got_plt_(NULL
),
320 global_offset_table_(NULL
), rela_dyn_(NULL
),
321 copy_relocs_(elfcpp::R_AARCH64_COPY
)
324 // Scan the relocations to determine unreferenced sections for
325 // garbage collection.
327 gc_process_relocs(Symbol_table
* symtab
,
329 Sized_relobj_file
<size
, big_endian
>* object
,
330 unsigned int data_shndx
,
331 unsigned int sh_type
,
332 const unsigned char* prelocs
,
334 Output_section
* output_section
,
335 bool needs_special_offset_handling
,
336 size_t local_symbol_count
,
337 const unsigned char* plocal_symbols
);
339 // Scan the relocations to look for symbol adjustments.
341 scan_relocs(Symbol_table
* symtab
,
343 Sized_relobj_file
<size
, big_endian
>* object
,
344 unsigned int data_shndx
,
345 unsigned int sh_type
,
346 const unsigned char* prelocs
,
348 Output_section
* output_section
,
349 bool needs_special_offset_handling
,
350 size_t local_symbol_count
,
351 const unsigned char* plocal_symbols
);
353 // Finalize the sections.
355 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
357 // Relocate a section.
359 relocate_section(const Relocate_info
<size
, big_endian
>*,
360 unsigned int sh_type
,
361 const unsigned char* prelocs
,
363 Output_section
* output_section
,
364 bool needs_special_offset_handling
,
366 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
367 section_size_type view_size
,
368 const Reloc_symbol_changes
*);
370 // Scan the relocs during a relocatable link.
372 scan_relocatable_relocs(Symbol_table
* symtab
,
374 Sized_relobj_file
<size
, big_endian
>* object
,
375 unsigned int data_shndx
,
376 unsigned int sh_type
,
377 const unsigned char* prelocs
,
379 Output_section
* output_section
,
380 bool needs_special_offset_handling
,
381 size_t local_symbol_count
,
382 const unsigned char* plocal_symbols
,
383 Relocatable_relocs
*);
385 // Relocate a section during a relocatable link.
388 const Relocate_info
<size
, big_endian
>*,
389 unsigned int sh_type
,
390 const unsigned char* prelocs
,
392 Output_section
* output_section
,
393 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
394 const Relocatable_relocs
*,
396 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
397 section_size_type view_size
,
398 unsigned char* reloc_view
,
399 section_size_type reloc_view_size
);
401 // Return the PLT section.
403 do_plt_address_for_global(const Symbol
* gsym
) const
404 { return this->plt_section()->address_for_global(gsym
); }
407 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
408 { return this->plt_section()->address_for_local(relobj
, symndx
); }
410 // Return the number of entries in the PLT.
412 plt_entry_count() const;
414 //Return the offset of the first non-reserved PLT entry.
416 first_plt_entry_offset() const;
418 // Return the size of each PLT entry.
420 plt_entry_size() const;
423 tcb_size() const { return This::TCB_SIZE
; }
427 do_select_as_default_target()
429 gold_assert(aarch64_reloc_property_table
== NULL
);
430 aarch64_reloc_property_table
= new AArch64_reloc_property_table();
433 virtual Output_data_plt_aarch64
<size
, big_endian
>*
434 do_make_data_plt(Layout
* layout
, Output_data_space
* got_plt
)
436 return new Output_data_plt_aarch64_standard
<size
, big_endian
>(layout
,
440 Output_data_plt_aarch64
<size
, big_endian
>*
441 make_data_plt(Layout
* layout
, Output_data_space
* got_plt
)
443 return this->do_make_data_plt(layout
, got_plt
);
447 // The class which scans relocations.
452 : issued_non_pic_error_(false)
456 local(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
457 Sized_relobj_file
<size
, big_endian
>* object
,
458 unsigned int data_shndx
,
459 Output_section
* output_section
,
460 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
461 const elfcpp::Sym
<size
, big_endian
>& lsym
,
465 global(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
466 Sized_relobj_file
<size
, big_endian
>* object
,
467 unsigned int data_shndx
,
468 Output_section
* output_section
,
469 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
473 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
474 Target_aarch64
<size
, big_endian
>* ,
475 Sized_relobj_file
<size
, big_endian
>* ,
478 const elfcpp::Rela
<size
, big_endian
>& ,
480 const elfcpp::Sym
<size
, big_endian
>&);
483 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
484 Target_aarch64
<size
, big_endian
>* ,
485 Sized_relobj_file
<size
, big_endian
>* ,
488 const elfcpp::Rela
<size
, big_endian
>& ,
494 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
495 unsigned int r_type
);
498 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
499 unsigned int r_type
, Symbol
*);
502 possible_function_pointer_reloc(unsigned int r_type
);
505 check_non_pic(Relobj
*, unsigned int r_type
);
507 // Whether we have issued an error about a non-PIC compilation.
508 bool issued_non_pic_error_
;
511 // The class which implements relocation.
521 // Do a relocation. Return false if the caller should not issue
522 // any warnings about this relocation.
524 relocate(const Relocate_info
<size
, big_endian
>*, Target_aarch64
*,
526 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
527 unsigned int r_type
, const Sized_symbol
<size
>*,
528 const Symbol_value
<size
>*,
529 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
533 inline typename AArch64_relocate_functions
<size
,big_endian
>::Status
534 relocate_tls(const Relocate_info
<size
,big_endian
>*,
535 Target_aarch64
<size
, big_endian
>*,
537 const elfcpp::Rela
<size
, big_endian
>&,
538 unsigned int r_type
, const Sized_symbol
<size
>*,
539 const Symbol_value
<size
>*,
541 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
543 }; // End of class Relocate
545 // A class which returns the size required for a relocation type,
546 // used while scanning relocs during a relocatable link.
547 class Relocatable_size_for_reloc
551 get_size_for_reloc(unsigned int, Relobj
*);
554 // Adjust TLS relocation type based on the options and whether this
555 // is a local symbol.
556 static tls::Tls_optimization
557 optimize_tls_reloc(bool is_final
, int r_type
);
559 // Get the GOT section, creating it if necessary.
560 Output_data_got_aarch64
<size
, big_endian
>*
561 got_section(Symbol_table
*, Layout
*);
563 // Get the GOT PLT section.
565 got_plt_section() const
567 gold_assert(this->got_plt_
!= NULL
);
568 return this->got_plt_
;
571 // Create the PLT section.
573 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
575 // Create a PLT entry for a global symbol.
577 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
579 // Get the PLT section.
580 Output_data_plt_aarch64
<size
, big_endian
>*
583 gold_assert(this->plt_
!= NULL
);
587 // Get the dynamic reloc section, creating it if necessary.
589 rela_dyn_section(Layout
*);
591 // Add a potential copy relocation.
593 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
594 Sized_relobj_file
<size
, big_endian
>* object
,
595 unsigned int shndx
, Output_section
* output_section
,
596 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
598 this->copy_relocs_
.copy_reloc(symtab
, layout
,
599 symtab
->get_sized_symbol
<size
>(sym
),
600 object
, shndx
, output_section
,
601 reloc
, this->rela_dyn_section(layout
));
604 // Information about this specific target which we pass to the
605 // general Target structure.
606 static const Target::Target_info aarch64_info
;
608 // The types of GOT entries needed for this platform.
609 // These values are exposed to the ABI in an incremental link.
610 // Do not renumber existing values without changing the version
611 // number of the .gnu_incremental_inputs section.
614 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
615 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
616 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
617 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
621 Output_data_got_aarch64
<size
, big_endian
>* got_
;
623 Output_data_plt_aarch64
<size
, big_endian
>* plt_
;
624 // The GOT PLT section.
625 Output_data_space
* got_plt_
;
626 // The _GLOBAL_OFFSET_TABLE_ symbol.
627 Symbol
* global_offset_table_
;
628 // The dynamic reloc section.
629 Reloc_section
* rela_dyn_
;
630 // Relocs saved to avoid a COPY reloc.
631 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
632 }; // End of Target_aarch64
635 const Target::Target_info Target_aarch64
<64, false>::aarch64_info
=
638 false, // is_big_endian
639 elfcpp::EM_AARCH64
, // machine_code
640 false, // has_make_symbol
641 false, // has_resolve
642 false, // has_code_fill
643 true, // is_default_stack_executable
644 false, // can_icf_inline_merge_sections
646 "/lib/ld.so.1", // program interpreter
647 0x400000, // default_text_segment_address
648 0x1000, // abi_pagesize (overridable by -z max-page-size)
649 0x1000, // common_pagesize (overridable by -z common-page-size)
650 false, // isolate_execinstr
652 elfcpp::SHN_UNDEF
, // small_common_shndx
653 elfcpp::SHN_UNDEF
, // large_common_shndx
654 0, // small_common_section_flags
655 0, // large_common_section_flags
656 NULL
, // attributes_section
657 NULL
, // attributes_vendor
658 "_start" // entry_symbol_name
662 const Target::Target_info Target_aarch64
<32, false>::aarch64_info
=
665 false, // is_big_endian
666 elfcpp::EM_AARCH64
, // machine_code
667 false, // has_make_symbol
668 false, // has_resolve
669 false, // has_code_fill
670 true, // is_default_stack_executable
671 false, // can_icf_inline_merge_sections
673 "/lib/ld.so.1", // program interpreter
674 0x400000, // default_text_segment_address
675 0x1000, // abi_pagesize (overridable by -z max-page-size)
676 0x1000, // common_pagesize (overridable by -z common-page-size)
677 false, // isolate_execinstr
679 elfcpp::SHN_UNDEF
, // small_common_shndx
680 elfcpp::SHN_UNDEF
, // large_common_shndx
681 0, // small_common_section_flags
682 0, // large_common_section_flags
683 NULL
, // attributes_section
684 NULL
, // attributes_vendor
685 "_start" // entry_symbol_name
689 const Target::Target_info Target_aarch64
<64, true>::aarch64_info
=
692 true, // is_big_endian
693 elfcpp::EM_AARCH64
, // machine_code
694 false, // has_make_symbol
695 false, // has_resolve
696 false, // has_code_fill
697 true, // is_default_stack_executable
698 false, // can_icf_inline_merge_sections
700 "/lib/ld.so.1", // program interpreter
701 0x400000, // default_text_segment_address
702 0x1000, // abi_pagesize (overridable by -z max-page-size)
703 0x1000, // common_pagesize (overridable by -z common-page-size)
704 false, // isolate_execinstr
706 elfcpp::SHN_UNDEF
, // small_common_shndx
707 elfcpp::SHN_UNDEF
, // large_common_shndx
708 0, // small_common_section_flags
709 0, // large_common_section_flags
710 NULL
, // attributes_section
711 NULL
, // attributes_vendor
712 "_start" // entry_symbol_name
716 const Target::Target_info Target_aarch64
<32, true>::aarch64_info
=
719 true, // is_big_endian
720 elfcpp::EM_AARCH64
, // machine_code
721 false, // has_make_symbol
722 false, // has_resolve
723 false, // has_code_fill
724 true, // is_default_stack_executable
725 false, // can_icf_inline_merge_sections
727 "/lib/ld.so.1", // program interpreter
728 0x400000, // default_text_segment_address
729 0x1000, // abi_pagesize (overridable by -z max-page-size)
730 0x1000, // common_pagesize (overridable by -z common-page-size)
731 false, // isolate_execinstr
733 elfcpp::SHN_UNDEF
, // small_common_shndx
734 elfcpp::SHN_UNDEF
, // large_common_shndx
735 0, // small_common_section_flags
736 0, // large_common_section_flags
737 NULL
, // attributes_section
738 NULL
, // attributes_vendor
739 "_start" // entry_symbol_name
742 // Get the GOT section, creating it if necessary.
744 template<int size
, bool big_endian
>
745 Output_data_got_aarch64
<size
, big_endian
>*
746 Target_aarch64
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
749 if (this->got_
== NULL
)
751 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
753 // When using -z now, we can treat .got.plt as a relro section.
754 // Without -z now, it is modified after program startup by lazy
756 bool is_got_plt_relro
= parameters
->options().now();
757 Output_section_order got_order
= (is_got_plt_relro
760 Output_section_order got_plt_order
= (is_got_plt_relro
762 : ORDER_NON_RELRO_FIRST
);
764 // Layout of .got and .got.plt sections.
765 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
767 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
768 // .gotplt[1] reserved for ld.so (resolver)
769 // .gotplt[2] reserved
771 // Generate .got section.
772 this->got_
= new Output_data_got_aarch64
<size
, big_endian
>(symtab
,
774 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
775 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
776 this->got_
, got_order
, true);
777 // The first word of GOT is reserved for the address of .dynamic.
778 // We put 0 here now. The value will be replaced later in
779 // Output_data_got_aarch64::do_write.
780 this->got_
->add_constant(0);
782 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
783 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
784 // even if there is a .got.plt section.
785 this->global_offset_table_
=
786 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
787 Symbol_table::PREDEFINED
,
789 0, 0, elfcpp::STT_OBJECT
,
791 elfcpp::STV_HIDDEN
, 0,
794 // Generate .got.plt section.
795 this->got_plt_
= new Output_data_space(size
/ 8, "** GOT PLT");
796 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
798 | elfcpp::SHF_WRITE
),
799 this->got_plt_
, got_plt_order
,
802 // The first three entries are reserved.
803 this->got_plt_
->set_current_data_size(
804 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
806 if (!is_got_plt_relro
)
808 // Those bytes can go into the relro segment.
809 layout
->increase_relro(
810 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
817 // Get the dynamic reloc section, creating it if necessary.
819 template<int size
, bool big_endian
>
820 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
821 Target_aarch64
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
823 if (this->rela_dyn_
== NULL
)
825 gold_assert(layout
!= NULL
);
826 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
827 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
828 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
829 ORDER_DYNAMIC_RELOCS
, false);
831 return this->rela_dyn_
;
834 // A class to handle the PLT data.
835 // This is an abstract base class that handles most of the linker details
836 // but does not know the actual contents of PLT entries. The derived
837 // classes below fill in those details.
839 template<int size
, bool big_endian
>
840 class Output_data_plt_aarch64
: public Output_section_data
843 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
845 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
847 Output_data_plt_aarch64(Layout
* layout
,
849 Output_data_space
* got_plt
)
850 : Output_section_data(addralign
),
853 { this->init(layout
); }
855 // Initialize the PLT section.
857 init(Layout
* layout
);
859 // Add an entry to the PLT.
861 add_entry(Symbol
* gsym
);
863 // Return the .rela.plt section data.
866 { return this->rel_
; }
868 // Return whether we created a section for IRELATIVE relocations.
870 has_irelative_section() const
871 { return this->irelative_rel_
!= NULL
; }
873 // Return the number of PLT entries.
876 { return this->count_
; }
878 // Return the offset of the first non-reserved PLT entry.
880 first_plt_entry_offset()
881 { return this->do_first_plt_entry_offset(); }
883 // Return the size of a PLT entry.
885 get_plt_entry_size() const
886 { return this->do_get_plt_entry_size(); }
888 // Return the PLT address to use for a global symbol.
890 address_for_global(const Symbol
*);
892 // Return the PLT address to use for a local symbol.
894 address_for_local(const Relobj
*, unsigned int symndx
);
897 // Fill in the first PLT entry.
899 fill_first_plt_entry(unsigned char* pov
,
902 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
904 // Fill in a normal PLT entry.
906 fill_plt_entry(unsigned char* pov
,
909 unsigned int got_offset
,
910 unsigned int plt_offset
)
912 this->do_fill_plt_entry(pov
, got_address
, plt_address
,
913 got_offset
, plt_offset
);
917 do_first_plt_entry_offset() const = 0;
920 do_get_plt_entry_size() const = 0;
923 do_fill_first_plt_entry(unsigned char* pov
,
925 Address plt_addr
) = 0;
928 do_fill_plt_entry(unsigned char* pov
,
931 unsigned int got_offset
,
932 unsigned int plt_offset
) = 0;
935 do_adjust_output_section(Output_section
* os
);
937 // Write to a map file.
939 do_print_to_mapfile(Mapfile
* mapfile
) const
940 { mapfile
->print_output_data(this, _("** PLT")); }
943 // Set the final size.
945 set_final_data_size();
947 // Write out the PLT data.
949 do_write(Output_file
*);
951 // The reloc section.
953 // The IRELATIVE relocs, if necessary. These must follow the
954 // regular PLT relocations.
955 Reloc_section
* irelative_rel_
;
957 Output_data_got_aarch64
<size
, big_endian
>* got_
;
958 // The .got.plt section.
959 Output_data_space
* got_plt_
;
960 // The number of PLT entries.
964 // Initialize the PLT section.
966 template<int size
, bool big_endian
>
968 Output_data_plt_aarch64
<size
, big_endian
>::init(Layout
* layout
)
970 this->rel_
= new Reloc_section(false);
971 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
972 elfcpp::SHF_ALLOC
, this->rel_
,
973 ORDER_DYNAMIC_PLT_RELOCS
, false);
976 template<int size
, bool big_endian
>
978 Output_data_plt_aarch64
<size
, big_endian
>::do_adjust_output_section(
981 os
->set_entsize(this->get_plt_entry_size());
984 // Add an entry to the PLT.
986 template<int size
, bool big_endian
>
988 Output_data_plt_aarch64
<size
, big_endian
>::add_entry(Symbol
* gsym
)
990 gold_assert(!gsym
->has_plt_offset());
992 gsym
->set_plt_offset((this->count_
) * this->get_plt_entry_size()
993 + this->first_plt_entry_offset());
997 section_offset_type got_offset
= this->got_plt_
->current_data_size();
999 // Every PLT entry needs a GOT entry which points back to the PLT
1000 // entry (this will be changed by the dynamic linker, normally
1001 // lazily when the function is called).
1002 this->got_plt_
->set_current_data_size(got_offset
+ size
/ 8);
1004 // Every PLT entry needs a reloc.
1005 gsym
->set_needs_dynsym_entry();
1006 this->rel_
->add_global(gsym
, elfcpp::R_AARCH64_JUMP_SLOT
,
1007 this->got_plt_
, got_offset
, 0);
1009 // Note that we don't need to save the symbol. The contents of the
1010 // PLT are independent of which symbols are used. The symbols only
1011 // appear in the relocations.
1014 // Return the PLT address to use for a global symbol.
1016 template<int size
, bool big_endian
>
1018 Output_data_plt_aarch64
<size
, big_endian
>::address_for_global(
1021 uint64_t offset
= 0;
1022 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1023 && gsym
->can_use_relative_reloc(false))
1024 offset
= (this->first_plt_entry_offset() +
1025 this->count_
* this->get_plt_entry_size());
1026 return this->address() + offset
+ gsym
->plt_offset();
1029 // Return the PLT address to use for a local symbol. These are always
1030 // IRELATIVE relocs.
1032 template<int size
, bool big_endian
>
1034 Output_data_plt_aarch64
<size
, big_endian
>::address_for_local(
1035 const Relobj
* object
,
1038 return (this->address()
1039 + this->first_plt_entry_offset()
1040 + this->count_
* this->get_plt_entry_size()
1041 + object
->local_plt_offset(r_sym
));
1044 // Set the final size.
1046 template<int size
, bool big_endian
>
1048 Output_data_plt_aarch64
<size
, big_endian
>::set_final_data_size()
1050 this->set_data_size(this->first_plt_entry_offset()
1051 + this->count_
* this->get_plt_entry_size());
1054 template<int size
, bool big_endian
>
1055 class Output_data_plt_aarch64_standard
:
1056 public Output_data_plt_aarch64
<size
, big_endian
>
1059 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1060 Output_data_plt_aarch64_standard(Layout
* layout
, Output_data_space
* got_plt
)
1061 : Output_data_plt_aarch64
<size
, big_endian
>(layout
,
1067 // Return the offset of the first non-reserved PLT entry.
1068 virtual unsigned int
1069 do_first_plt_entry_offset() const
1070 { return this->first_plt_entry_size
; }
1072 // Return the size of a PLT entry
1073 virtual unsigned int
1074 do_get_plt_entry_size() const
1075 { return this->plt_entry_size
; }
1078 do_fill_first_plt_entry(unsigned char* pov
,
1079 Address got_address
,
1080 Address plt_address
);
1083 do_fill_plt_entry(unsigned char* pov
,
1084 Address got_address
,
1085 Address plt_address
,
1086 unsigned int got_offset
,
1087 unsigned int plt_offset
);
1090 // The size of the first plt entry size.
1091 static const int first_plt_entry_size
= 32;
1092 // The size of the plt entry size.
1093 static const int plt_entry_size
= 16;
1094 // Template for the first PLT entry.
1095 static const uint32_t first_plt_entry
[first_plt_entry_size
/ 4];
1096 // Template for subsequent PLT entries.
1097 static const uint32_t plt_entry
[plt_entry_size
/ 4];
1100 // The first entry in the PLT for an executable.
1104 Output_data_plt_aarch64_standard
<32, false>::
1105 first_plt_entry
[first_plt_entry_size
/ 4] =
1107 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
1108 0x90000010, /* adrp x16, PLT_GOT+0x8 */
1109 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
1110 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
1111 0xd61f0220, /* br x17 */
1112 0xd503201f, /* nop */
1113 0xd503201f, /* nop */
1114 0xd503201f, /* nop */
1119 Output_data_plt_aarch64_standard
<32, true>::
1120 first_plt_entry
[first_plt_entry_size
/ 4] =
1122 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
1123 0x90000010, /* adrp x16, PLT_GOT+0x8 */
1124 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
1125 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
1126 0xd61f0220, /* br x17 */
1127 0xd503201f, /* nop */
1128 0xd503201f, /* nop */
1129 0xd503201f, /* nop */
1134 Output_data_plt_aarch64_standard
<64, false>::
1135 first_plt_entry
[first_plt_entry_size
/ 4] =
1137 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
1138 0x90000010, /* adrp x16, PLT_GOT+16 */
1139 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
1140 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
1141 0xd61f0220, /* br x17 */
1142 0xd503201f, /* nop */
1143 0xd503201f, /* nop */
1144 0xd503201f, /* nop */
1149 Output_data_plt_aarch64_standard
<64, true>::
1150 first_plt_entry
[first_plt_entry_size
/ 4] =
1152 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
1153 0x90000010, /* adrp x16, PLT_GOT+16 */
1154 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
1155 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
1156 0xd61f0220, /* br x17 */
1157 0xd503201f, /* nop */
1158 0xd503201f, /* nop */
1159 0xd503201f, /* nop */
1164 Output_data_plt_aarch64_standard
<32, false>::
1165 plt_entry
[plt_entry_size
/ 4] =
1167 0x90000010, /* adrp x16, PLTGOT + n * 4 */
1168 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
1169 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
1170 0xd61f0220, /* br x17. */
1175 Output_data_plt_aarch64_standard
<32, true>::
1176 plt_entry
[plt_entry_size
/ 4] =
1178 0x90000010, /* adrp x16, PLTGOT + n * 4 */
1179 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
1180 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
1181 0xd61f0220, /* br x17. */
1186 Output_data_plt_aarch64_standard
<64, false>::
1187 plt_entry
[plt_entry_size
/ 4] =
1189 0x90000010, /* adrp x16, PLTGOT + n * 8 */
1190 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
1191 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
1192 0xd61f0220, /* br x17. */
1197 Output_data_plt_aarch64_standard
<64, true>::
1198 plt_entry
[plt_entry_size
/ 4] =
1200 0x90000010, /* adrp x16, PLTGOT + n * 8 */
1201 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
1202 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
1203 0xd61f0220, /* br x17. */
1206 template<int size
, bool big_endian
>
1208 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_first_plt_entry(
1210 Address got_address
,
1211 Address plt_address
)
1213 // PLT0 of the small PLT looks like this in ELF64 -
1214 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
1215 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
1216 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
1218 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
1219 // GOTPLT entry for this.
1221 // PLT0 will be slightly different in ELF32 due to different got entry
1223 memcpy(pov
, this->first_plt_entry
, this->first_plt_entry_size
);
1224 Address gotplt_2nd_ent
= got_address
+ (size
/ 8) * 2;
1226 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
1227 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
1228 // FIXME: This only works for 64bit
1229 AArch64_relocate_functions
<size
, big_endian
>::adrp(pov
+ 4,
1230 gotplt_2nd_ent
, plt_address
+ 4);
1232 // Fill in R_AARCH64_LDST8_LO12
1233 elfcpp::Swap
<32, big_endian
>::writeval(
1235 ((this->first_plt_entry
[2] & 0xffc003ff)
1236 | ((gotplt_2nd_ent
& 0xff8) << 7)));
1238 // Fill in R_AARCH64_ADD_ABS_LO12
1239 elfcpp::Swap
<32, big_endian
>::writeval(
1241 ((this->first_plt_entry
[3] & 0xffc003ff)
1242 | ((gotplt_2nd_ent
& 0xfff) << 10)));
1245 // Subsequent entries in the PLT for an executable.
1246 // FIXME: This only works for 64bit
1248 template<int size
, bool big_endian
>
1250 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_plt_entry(
1252 Address got_address
,
1253 Address plt_address
,
1254 unsigned int got_offset
,
1255 unsigned int plt_offset
)
1257 memcpy(pov
, this->plt_entry
, this->plt_entry_size
);
1259 Address gotplt_entry_address
= got_address
+ got_offset
;
1260 Address plt_entry_address
= plt_address
+ plt_offset
;
1262 // Fill in R_AARCH64_PCREL_ADR_HI21
1263 AArch64_relocate_functions
<size
, big_endian
>::adrp(
1265 gotplt_entry_address
,
1268 // Fill in R_AARCH64_LDST64_ABS_LO12
1269 elfcpp::Swap
<32, big_endian
>::writeval(
1271 ((this->plt_entry
[1] & 0xffc003ff)
1272 | ((gotplt_entry_address
& 0xff8) << 7)));
1274 // Fill in R_AARCH64_ADD_ABS_LO12
1275 elfcpp::Swap
<32, big_endian
>::writeval(
1277 ((this->plt_entry
[2] & 0xffc003ff)
1278 | ((gotplt_entry_address
& 0xfff) <<10)));
1282 // Write out the PLT. This uses the hand-coded instructions above,
1283 // and adjusts them as needed. This is specified by the AMD64 ABI.
1285 template<int size
, bool big_endian
>
1287 Output_data_plt_aarch64
<size
, big_endian
>::do_write(Output_file
* of
)
1289 const off_t offset
= this->offset();
1290 const section_size_type oview_size
=
1291 convert_to_section_size_type(this->data_size());
1292 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1294 const off_t got_file_offset
= this->got_plt_
->offset();
1295 const section_size_type got_size
=
1296 convert_to_section_size_type(this->got_plt_
->data_size());
1297 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1300 unsigned char* pov
= oview
;
1302 // The base address of the .plt section.
1303 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1304 // The base address of the PLT portion of the .got section.
1305 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1306 = this->got_plt_
->address();
1308 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1309 pov
+= this->first_plt_entry_offset();
1311 // The first three entries in .got.plt are reserved.
1312 unsigned char* got_pov
= got_view
;
1313 memset(got_pov
, 0, size
/ 8 * AARCH64_GOTPLT_RESERVE_COUNT
);
1314 got_pov
+= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
1316 unsigned int plt_offset
= this->first_plt_entry_offset();
1317 unsigned int got_offset
= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
1318 const unsigned int count
= this->count_
;
1319 for (unsigned int plt_index
= 0;
1322 pov
+= this->get_plt_entry_size(),
1323 got_pov
+= size
/ 8,
1324 plt_offset
+= this->get_plt_entry_size(),
1325 got_offset
+= size
/ 8)
1327 // Set and adjust the PLT entry itself.
1328 this->fill_plt_entry(pov
, got_address
, plt_address
,
1329 got_offset
, plt_offset
);
1331 // Set the entry in the GOT, which points to plt0.
1332 elfcpp::Swap
<size
, big_endian
>::writeval(got_pov
, plt_address
);
1335 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1336 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1338 of
->write_output_view(offset
, oview_size
, oview
);
1339 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1342 // Telling how to update the immediate field of an instruction.
1343 struct AArch64_howto
1345 // The immediate field mask.
1346 elfcpp::Elf_Xword dst_mask
;
1348 // The offset to apply relocation immediate
1351 // The second part offset, if the immediate field has two parts.
1352 // -1 if the immediate field has only one part.
1356 static const AArch64_howto aarch64_howto
[AArch64_reloc_property::INST_NUM
] =
1358 {0, -1, -1}, // DATA
1359 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
1360 {0xffffe0, 5, -1}, // LD [23:5]-imm19
1361 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
1362 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
1363 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
1364 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
1365 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
1366 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
1367 {0x3ffffff, 0, -1}, // B [25:0]-imm26
1368 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
1371 // AArch64 relocate function class
1373 template<int size
, bool big_endian
>
1374 class AArch64_relocate_functions
1379 STATUS_OKAY
, // No error during relocation.
1380 STATUS_OVERFLOW
, // Relocation overflow.
1381 STATUS_BAD_RELOC
, // Relocation cannot be applied.
1385 typedef AArch64_relocate_functions
<size
, big_endian
> This
;
1386 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1388 // Return the page address of the address.
1389 // Page(address) = address & ~0xFFF
1391 static inline typename
elfcpp::Swap
<size
, big_endian
>::Valtype
1392 Page(Address address
)
1394 return (address
& (~static_cast<Address
>(0xFFF)));
1397 // Update instruction (pointed by view) with selected bits (immed).
1398 // val = (val & ~dst_mask) | (immed << doffset)
1400 template<int valsize
>
1402 update_view(unsigned char* view
,
1403 typename
elfcpp::Swap
<size
, big_endian
>::Valtype immed
,
1404 elfcpp::Elf_Xword doffset
,
1405 elfcpp::Elf_Xword dst_mask
)
1407 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1408 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1409 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1411 // Clear immediate fields.
1413 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
1414 static_cast<Valtype
>(val
| (immed
<< doffset
)));
1417 // Update two parts of an instruction (pointed by view) with selected
1418 // bits (immed1 and immed2).
1419 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
1421 template<int valsize
>
1423 update_view_two_parts(
1424 unsigned char* view
,
1425 typename
elfcpp::Swap
<size
, big_endian
>::Valtype immed1
,
1426 typename
elfcpp::Swap
<size
, big_endian
>::Valtype immed2
,
1427 elfcpp::Elf_Xword doffset1
,
1428 elfcpp::Elf_Xword doffset2
,
1429 elfcpp::Elf_Xword dst_mask
)
1431 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1432 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1433 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1435 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
1436 static_cast<Valtype
>(val
| (immed1
<< doffset1
) |
1437 (immed2
<< doffset2
)));
1440 // Update adr or adrp instruction with [32:12] of X.
1441 // In adr and adrp: [30:29] immlo [23:5] immhi
1444 update_adr(unsigned char* view
,
1445 typename
elfcpp::Swap
<size
, big_endian
>::Valtype x
,
1446 const AArch64_reloc_property
* /* reloc_property */)
1448 elfcpp::Elf_Xword dst_mask
= (0x3 << 29) | (0x7ffff << 5);
1449 typename
elfcpp::Swap
<32, big_endian
>::Valtype immed
=
1450 (x
>> 12) & 0x1fffff;
1451 This::template update_view_two_parts
<32>(
1454 (immed
& 0x1ffffc) >> 2,
1462 // Do a simple rela relocation at unaligned addresses.
1464 template<int valsize
>
1465 static inline typename
This::Status
1466 rela_ua(unsigned char* view
,
1467 const Sized_relobj_file
<size
, big_endian
>* object
,
1468 const Symbol_value
<size
>* psymval
,
1469 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1470 const AArch64_reloc_property
* reloc_property
)
1472 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1474 typename
elfcpp::Elf_types
<size
>::Elf_Addr x
=
1475 psymval
->value(object
, addend
);
1476 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
1477 static_cast<Valtype
>(x
));
1478 return (reloc_property
->checkup_x_value(x
)
1480 : This::STATUS_OVERFLOW
);
1483 // Do a simple pc-relative relocation at unaligned addresses.
1485 template<int valsize
>
1486 static inline typename
This::Status
1487 pcrela_ua(unsigned char* view
,
1488 const Sized_relobj_file
<size
, big_endian
>* object
,
1489 const Symbol_value
<size
>* psymval
,
1490 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1492 const AArch64_reloc_property
* reloc_property
)
1494 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1496 Address x
= psymval
->value(object
, addend
) - address
;
1497 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
1498 static_cast<Valtype
>(x
));
1499 return (reloc_property
->checkup_x_value(x
)
1501 : This::STATUS_OVERFLOW
);
1504 // Do a simple rela relocation at aligned addresses.
1506 template<int valsize
>
1507 static inline typename
This::Status
1509 unsigned char* view
,
1510 const Sized_relobj_file
<size
, big_endian
>* object
,
1511 const Symbol_value
<size
>* psymval
,
1512 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1513 const AArch64_reloc_property
* reloc_property
)
1515 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype
1517 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1518 Address x
= psymval
->value(object
, addend
);
1519 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
1520 static_cast<Valtype
>(x
));
1521 return (reloc_property
->checkup_x_value(x
)
1523 : This::STATUS_OVERFLOW
);
1526 // Do relocate. Update selected bits in text.
1527 // new_val = (val & ~dst_mask) | (immed << doffset)
1529 template<int valsize
>
1530 static inline typename
This::Status
1531 rela_general(unsigned char* view
,
1532 const Sized_relobj_file
<size
, big_endian
>* object
,
1533 const Symbol_value
<size
>* psymval
,
1534 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1535 const AArch64_reloc_property
* reloc_property
)
1537 // Calculate relocation.
1538 Address x
= psymval
->value(object
, addend
);
1540 // Select bits from X.
1541 Address immed
= reloc_property
->select_x_value(x
);
1544 const AArch64_reloc_property::Reloc_inst inst
=
1545 reloc_property
->reloc_inst();
1546 // If it is a data relocation or instruction has 2 parts of immediate
1547 // fields, you should not call rela_general.
1548 gold_assert(aarch64_howto
[inst
].doffset2
== -1 &&
1549 aarch64_howto
[inst
].doffset
!= -1);
1550 This::template update_view
<valsize
>(view
, immed
,
1551 aarch64_howto
[inst
].doffset
,
1552 aarch64_howto
[inst
].dst_mask
);
1554 // Do check overflow or alignment if needed.
1555 return (reloc_property
->checkup_x_value(x
)
1557 : This::STATUS_OVERFLOW
);
1560 // Do relocate. Update selected bits in text.
1561 // new val = (val & ~dst_mask) | (immed << doffset)
1563 template<int valsize
>
1564 static inline typename
This::Status
1566 unsigned char* view
,
1567 typename
elfcpp::Swap
<size
, big_endian
>::Valtype s
,
1568 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1569 const AArch64_reloc_property
* reloc_property
)
1571 // Calculate relocation.
1572 Address x
= s
+ addend
;
1574 // Select bits from X.
1575 Address immed
= reloc_property
->select_x_value(x
);
1578 const AArch64_reloc_property::Reloc_inst inst
=
1579 reloc_property
->reloc_inst();
1580 // If it is a data relocation or instruction has 2 parts of immediate
1581 // fields, you should not call rela_general.
1582 gold_assert(aarch64_howto
[inst
].doffset2
== -1 &&
1583 aarch64_howto
[inst
].doffset
!= -1);
1584 This::template update_view
<valsize
>(view
, immed
,
1585 aarch64_howto
[inst
].doffset
,
1586 aarch64_howto
[inst
].dst_mask
);
1588 // Do check overflow or alignment if needed.
1589 return (reloc_property
->checkup_x_value(x
)
1591 : This::STATUS_OVERFLOW
);
1594 // Do address relative relocate. Update selected bits in text.
1595 // new val = (val & ~dst_mask) | (immed << doffset)
1597 template<int valsize
>
1598 static inline typename
This::Status
1600 unsigned char* view
,
1601 const Sized_relobj_file
<size
, big_endian
>* object
,
1602 const Symbol_value
<size
>* psymval
,
1603 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
1605 const AArch64_reloc_property
* reloc_property
)
1607 // Calculate relocation.
1608 Address x
= psymval
->value(object
, addend
) - address
;
1610 // Select bits from X.
1611 Address immed
= reloc_property
->select_x_value(x
);
1614 const AArch64_reloc_property::Reloc_inst inst
=
1615 reloc_property
->reloc_inst();
1616 // If it is a data relocation or instruction has 2 parts of immediate
1617 // fields, you should not call pcrela_general.
1618 gold_assert(aarch64_howto
[inst
].doffset2
== -1 &&
1619 aarch64_howto
[inst
].doffset
!= -1);
1620 This::template update_view
<valsize
>(view
, immed
,
1621 aarch64_howto
[inst
].doffset
,
1622 aarch64_howto
[inst
].dst_mask
);
1624 // Do check overflow or alignment if needed.
1625 return (reloc_property
->checkup_x_value(x
)
1627 : This::STATUS_OVERFLOW
);
1630 // Calculate PG(S+A) - PG(address), update adrp instruction.
1631 // R_AARCH64_ADR_PREL_PG_HI21
1633 static inline typename
This::Status
1635 unsigned char* view
,
1639 typename
elfcpp::Swap
<size
, big_endian
>::Valtype x
=
1640 This::Page(sa
) - This::Page(address
);
1641 update_adr(view
, x
, NULL
);
1642 return (size
== 64 && Bits
<32>::has_overflow(x
)
1643 ? This::STATUS_OVERFLOW
1644 : This::STATUS_OKAY
);
1647 // Calculate PG(S+A) - PG(address), update adrp instruction.
1648 // R_AARCH64_ADR_PREL_PG_HI21
1650 static inline typename
This::Status
1651 adrp(unsigned char* view
,
1652 const Sized_relobj_file
<size
, big_endian
>* object
,
1653 const Symbol_value
<size
>* psymval
,
1656 const AArch64_reloc_property
* reloc_property
)
1658 Address sa
= psymval
->value(object
, addend
);
1659 typename
elfcpp::Swap
<size
, big_endian
>::Valtype x
=
1660 This::Page(sa
) - This::Page(address
);
1661 update_adr(view
, x
, reloc_property
);
1662 return (reloc_property
->checkup_x_value(x
)
1664 : This::STATUS_OVERFLOW
);
1669 // Return the number of entries in the PLT.
1671 template<int size
, bool big_endian
>
1673 Target_aarch64
<size
, big_endian
>::plt_entry_count() const
1675 if (this->plt_
== NULL
)
1677 return this->plt_
->entry_count();
1680 // Return the offset of the first non-reserved PLT entry.
1682 template<int size
, bool big_endian
>
1684 Target_aarch64
<size
, big_endian
>::first_plt_entry_offset() const
1686 return this->plt_
->first_plt_entry_offset();
1689 // Return the size of each PLT entry.
1691 template<int size
, bool big_endian
>
1693 Target_aarch64
<size
, big_endian
>::plt_entry_size() const
1695 return this->plt_
->get_plt_entry_size();
1698 // Optimize the TLS relocation type based on what we know about the
1699 // symbol. IS_FINAL is true if the final address of this symbol is
1700 // known at link time.
1702 template<int size
, bool big_endian
>
1703 tls::Tls_optimization
1704 Target_aarch64
<size
, big_endian
>::optimize_tls_reloc(bool /* is_final */,
1708 return tls::TLSOPT_NONE
;
1711 // Returns true if this relocation type could be that of a function pointer.
1713 template<int size
, bool big_endian
>
1715 Target_aarch64
<size
, big_endian
>::Scan::possible_function_pointer_reloc(
1716 unsigned int r_type
)
1720 case elfcpp::R_AARCH64_ABS64
:
1729 // For safe ICF, scan a relocation for a local symbol to check if it
1730 // corresponds to a function pointer being taken. In that case mark
1731 // the function whose pointer was taken as not foldable.
1733 template<int size
, bool big_endian
>
1735 Target_aarch64
<size
, big_endian
>::Scan::local_reloc_may_be_function_pointer(
1738 Target_aarch64
<size
, big_endian
>* ,
1739 Sized_relobj_file
<size
, big_endian
>* ,
1742 const elfcpp::Rela
<size
, big_endian
>& ,
1743 unsigned int r_type
,
1744 const elfcpp::Sym
<size
, big_endian
>&)
1746 // When building a shared library, do not fold any local symbols as it is
1747 // not possible to distinguish pointer taken versus a call by looking at
1748 // the relocation types.
1749 return (parameters
->options().shared()
1750 || possible_function_pointer_reloc(r_type
));
1753 // For safe ICF, scan a relocation for a global symbol to check if it
1754 // corresponds to a function pointer being taken. In that case mark
1755 // the function whose pointer was taken as not foldable.
1757 template<int size
, bool big_endian
>
1759 Target_aarch64
<size
, big_endian
>::Scan::global_reloc_may_be_function_pointer(
1762 Target_aarch64
<size
, big_endian
>* ,
1763 Sized_relobj_file
<size
, big_endian
>* ,
1766 const elfcpp::Rela
<size
, big_endian
>& ,
1767 unsigned int r_type
,
1770 // When building a shared library, do not fold symbols whose visibility
1771 // is hidden, internal or protected.
1772 return ((parameters
->options().shared()
1773 && (gsym
->visibility() == elfcpp::STV_INTERNAL
1774 || gsym
->visibility() == elfcpp::STV_PROTECTED
1775 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
1776 || possible_function_pointer_reloc(r_type
));
1779 // Report an unsupported relocation against a local symbol.
1781 template<int size
, bool big_endian
>
1783 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_local(
1784 Sized_relobj_file
<size
, big_endian
>* object
,
1785 unsigned int r_type
)
1787 gold_error(_("%s: unsupported reloc %u against local symbol"),
1788 object
->name().c_str(), r_type
);
1791 // We are about to emit a dynamic relocation of type R_TYPE. If the
1792 // dynamic linker does not support it, issue an error.
1794 template<int size
, bool big_endian
>
1796 Target_aarch64
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1797 unsigned int r_type
)
1799 gold_assert(r_type
!= elfcpp::R_AARCH64_NONE
);
1803 // These are the relocation types supported by glibc for AARCH64.
1804 case elfcpp::R_AARCH64_NONE
:
1805 case elfcpp::R_AARCH64_COPY
:
1806 case elfcpp::R_AARCH64_GLOB_DAT
:
1807 case elfcpp::R_AARCH64_JUMP_SLOT
:
1808 case elfcpp::R_AARCH64_RELATIVE
:
1809 case elfcpp::R_AARCH64_TLS_DTPREL64
:
1810 case elfcpp::R_AARCH64_TLS_DTPMOD64
:
1811 case elfcpp::R_AARCH64_TLS_TPREL64
:
1812 case elfcpp::R_AARCH64_TLSDESC
:
1813 case elfcpp::R_AARCH64_IRELATIVE
:
1814 case elfcpp::R_AARCH64_ABS32
:
1815 case elfcpp::R_AARCH64_ABS64
:
1822 // This prevents us from issuing more than one error per reloc
1823 // section. But we can still wind up issuing more than one
1824 // error per object file.
1825 if (this->issued_non_pic_error_
)
1827 gold_assert(parameters
->options().output_is_position_independent());
1828 object
->error(_("requires unsupported dynamic reloc; "
1829 "recompile with -fPIC"));
1830 this->issued_non_pic_error_
= true;
1834 // Scan a relocation for a local symbol.
1836 template<int size
, bool big_endian
>
1838 Target_aarch64
<size
, big_endian
>::Scan::local(
1839 Symbol_table
* symtab
,
1841 Target_aarch64
<size
, big_endian
>* target
,
1842 Sized_relobj_file
<size
, big_endian
>* object
,
1843 unsigned int data_shndx
,
1844 Output_section
* output_section
,
1845 const elfcpp::Rela
<size
, big_endian
>& rela
,
1846 unsigned int r_type
,
1847 const elfcpp::Sym
<size
, big_endian
>& /* lsym */,
1853 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
1858 case elfcpp::R_AARCH64_ABS32
:
1859 case elfcpp::R_AARCH64_ABS16
:
1860 if (parameters
->options().output_is_position_independent())
1862 gold_error(_("%s: unsupported reloc %u in pos independent link."),
1863 object
->name().c_str(), r_type
);
1867 case elfcpp::R_AARCH64_ABS64
:
1868 // If building a shared library or pie, we need to mark this as a dynmic
1869 // reloction, so that the dynamic loader can relocate it.
1870 if (parameters
->options().output_is_position_independent())
1872 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1873 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1874 rela_dyn
->add_local_relative(object
, r_sym
,
1875 elfcpp::R_AARCH64_RELATIVE
,
1878 rela
.get_r_offset(),
1879 rela
.get_r_addend(),
1880 false /* is ifunc */);
1884 case elfcpp::R_AARCH64_PREL64
:
1885 case elfcpp::R_AARCH64_PREL32
:
1886 case elfcpp::R_AARCH64_PREL16
:
1889 // Relocations to generate 19, 21 and 33-bit PC-relative address
1890 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
: // 275
1891 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
1892 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
1893 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
1894 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
1895 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
1896 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
: // 277
1899 // Control flow, pc-relative. We don't need to do anything for a relative
1900 // addressing relocation against a local symbol if it does not reference
1902 case elfcpp::R_AARCH64_TSTBR14
:
1903 case elfcpp::R_AARCH64_CONDBR19
:
1904 case elfcpp::R_AARCH64_JUMP26
:
1905 case elfcpp::R_AARCH64_CALL26
:
1908 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
1909 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
1911 layout
->set_has_static_tls();
1912 // Create a GOT entry for the tp-relative offset.
1913 Output_data_got_aarch64
<size
, big_endian
>* got
=
1914 target
->got_section(symtab
, layout
);
1915 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1916 if (!parameters
->doing_static_link())
1918 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1919 target
->rela_dyn_section(layout
),
1920 elfcpp::R_AARCH64_TLS_TPREL64
);
1922 else if (!object
->local_has_got_offset(r_sym
,
1923 GOT_TYPE_TLS_OFFSET
))
1925 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_OFFSET
);
1926 unsigned int got_offset
=
1927 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
1928 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1929 gold_assert(addend
== 0);
1930 got
->add_static_reloc(got_offset
, elfcpp::R_AARCH64_TLS_TPREL64
,
1936 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
1937 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
1938 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
1940 layout
->set_has_static_tls();
1941 bool output_is_shared
= parameters
->options().shared();
1942 if (output_is_shared
)
1943 gold_error(_("%s: unsupported TLSLEreloc %u in shard code."),
1944 object
->name().c_str(), r_type
);
1949 unsupported_reloc_local(object
, r_type
);
1954 // Report an unsupported relocation against a global symbol.
1956 template<int size
, bool big_endian
>
1958 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_global(
1959 Sized_relobj_file
<size
, big_endian
>* object
,
1960 unsigned int r_type
,
1963 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1964 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1967 template<int size
, bool big_endian
>
1969 Target_aarch64
<size
, big_endian
>::Scan::global(
1970 Symbol_table
* symtab
,
1972 Target_aarch64
<size
, big_endian
>* target
,
1973 Sized_relobj_file
<size
, big_endian
> * object
,
1974 unsigned int data_shndx
,
1975 Output_section
* output_section
,
1976 const elfcpp::Rela
<size
, big_endian
>& rela
,
1977 unsigned int r_type
,
1980 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
1984 case elfcpp::R_AARCH64_ABS16
:
1985 case elfcpp::R_AARCH64_ABS32
:
1986 case elfcpp::R_AARCH64_ABS64
:
1988 // Make a PLT entry if necessary.
1989 if (gsym
->needs_plt_entry())
1991 target
->make_plt_entry(symtab
, layout
, gsym
);
1992 // Since this is not a PC-relative relocation, we may be
1993 // taking the address of a function. In that case we need to
1994 // set the entry in the dynamic symbol table to the address of
1996 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1997 gsym
->set_needs_dynsym_value();
1999 // Make a dynamic relocation if necessary.
2000 const AArch64_reloc_property
* arp
=
2001 aarch64_reloc_property_table
->get_reloc_property(r_type
);
2002 gold_assert(arp
!= NULL
);
2003 if (gsym
->needs_dynamic_reloc(arp
->reference_flags()))
2005 if (!parameters
->options().output_is_position_independent()
2006 && gsym
->may_need_copy_reloc())
2009 _("%s: unsupported reloc %u which may need copyreloc."),
2010 object
->name().c_str(), r_type
);
2012 else if (r_type
== elfcpp::R_AARCH64_ABS64
2013 && gsym
->can_use_relative_reloc(false))
2015 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2016 rela_dyn
->add_global_relative(gsym
,
2017 elfcpp::R_AARCH64_RELATIVE
,
2021 rela
.get_r_offset(),
2022 rela
.get_r_addend(),
2027 check_non_pic(object
, r_type
);
2028 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*
2029 rela_dyn
= target
->rela_dyn_section(layout
);
2030 rela_dyn
->add_global(
2031 gsym
, r_type
, output_section
, object
,
2032 data_shndx
, rela
.get_r_offset(),rela
.get_r_addend());
2038 case elfcpp::R_AARCH64_PREL16
:
2039 case elfcpp::R_AARCH64_PREL32
:
2040 case elfcpp::R_AARCH64_PREL64
:
2041 // This is used to fill the GOT absolute address.
2042 if (gsym
->needs_plt_entry())
2044 target
->make_plt_entry(symtab
, layout
, gsym
);
2048 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
2049 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
2050 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
2051 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
2052 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
2053 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
2054 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
2055 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
2061 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
2062 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
2064 // This pair of relocations is used to access a specific GOT entry.
2065 // Note a GOT entry is an *address* to a symbol.
2066 // The symbol requires a GOT entry
2067 Output_data_got_aarch64
<size
, big_endian
>* got
=
2068 target
->got_section(symtab
, layout
);
2069 if (gsym
->final_value_is_known())
2071 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2075 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2076 if (gsym
->is_from_dynobj()
2077 || gsym
->is_undefined()
2078 || gsym
->is_preemptible()
2079 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2080 && parameters
->options().shared()))
2081 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
2082 rela_dyn
, elfcpp::R_AARCH64_GLOB_DAT
);
2085 // Not implemented yet.
2092 case elfcpp::R_AARCH64_TSTBR14
:
2093 case elfcpp::R_AARCH64_CONDBR19
:
2094 case elfcpp::R_AARCH64_JUMP26
:
2095 case elfcpp::R_AARCH64_CALL26
:
2097 if (gsym
->final_value_is_known())
2100 if (gsym
->is_defined() &&
2101 !gsym
->is_from_dynobj() &&
2102 !gsym
->is_preemptible())
2105 // Make plt entry for function call.
2106 const AArch64_reloc_property
* arp
=
2107 aarch64_reloc_property_table
->get_reloc_property(r_type
);
2108 gold_assert(arp
!= NULL
);
2109 target
->make_plt_entry(symtab
, layout
, gsym
);
2113 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
2114 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
2116 layout
->set_has_static_tls();
2117 // Create a GOT entry for the tp-relative offset.
2118 Output_data_got_aarch64
<size
, big_endian
>* got
2119 = target
->got_section(symtab
, layout
);
2120 if (!parameters
->doing_static_link())
2122 got
->add_global_with_rel(
2123 gsym
, GOT_TYPE_TLS_OFFSET
,
2124 target
->rela_dyn_section(layout
),
2125 elfcpp::R_AARCH64_TLS_TPREL64
);
2127 if (!gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
))
2129 got
->add_global(gsym
, GOT_TYPE_TLS_OFFSET
);
2130 unsigned int got_offset
=
2131 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
2132 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2133 gold_assert(addend
== 0);
2134 got
->add_static_reloc(got_offset
,
2135 elfcpp::R_AARCH64_TLS_TPREL64
, gsym
);
2140 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
2141 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
2142 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
2143 layout
->set_has_static_tls();
2144 if (parameters
->options().shared())
2145 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
2146 object
->name().c_str(), r_type
);
2150 const AArch64_reloc_property
* arp
=
2151 aarch64_reloc_property_table
->get_reloc_property(r_type
);
2152 gold_assert(arp
!= NULL
);
2153 gold_error(_("%s: unsupported reloc type in global scan"),
2154 arp
->name().c_str());
2157 } // End of Scan::global
2159 // Create the PLT section.
2160 template<int size
, bool big_endian
>
2162 Target_aarch64
<size
, big_endian
>::make_plt_section(
2163 Symbol_table
* symtab
, Layout
* layout
)
2165 if (this->plt_
== NULL
)
2167 // Create the GOT section first.
2168 this->got_section(symtab
, layout
);
2170 this->plt_
= this->make_data_plt(layout
, this->got_plt_
);
2172 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2174 | elfcpp::SHF_EXECINSTR
),
2175 this->plt_
, ORDER_PLT
, false);
2177 // Make the sh_info field of .rela.plt point to .plt.
2178 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2179 rela_plt_os
->set_info_section(this->plt_
->output_section());
2183 // Create a PLT entry for a global symbol.
2185 template<int size
, bool big_endian
>
2187 Target_aarch64
<size
, big_endian
>::make_plt_entry(
2188 Symbol_table
* symtab
,
2192 if (gsym
->has_plt_offset())
2195 if (this->plt_
== NULL
)
2196 this->make_plt_section(symtab
, layout
);
2198 this->plt_
->add_entry(gsym
);
2201 template<int size
, bool big_endian
>
2203 Target_aarch64
<size
, big_endian
>::gc_process_relocs(
2204 Symbol_table
* symtab
,
2206 Sized_relobj_file
<size
, big_endian
>* object
,
2207 unsigned int data_shndx
,
2208 unsigned int sh_type
,
2209 const unsigned char* prelocs
,
2211 Output_section
* output_section
,
2212 bool needs_special_offset_handling
,
2213 size_t local_symbol_count
,
2214 const unsigned char* plocal_symbols
)
2216 if (sh_type
== elfcpp::SHT_REL
)
2221 gold::gc_process_relocs
<
2223 Target_aarch64
<size
, big_endian
>,
2225 typename Target_aarch64
<size
, big_endian
>::Scan
,
2226 typename Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc
>(
2235 needs_special_offset_handling
,
2240 // Scan relocations for a section.
2242 template<int size
, bool big_endian
>
2244 Target_aarch64
<size
, big_endian
>::scan_relocs(
2245 Symbol_table
* symtab
,
2247 Sized_relobj_file
<size
, big_endian
>* object
,
2248 unsigned int data_shndx
,
2249 unsigned int sh_type
,
2250 const unsigned char* prelocs
,
2252 Output_section
* output_section
,
2253 bool needs_special_offset_handling
,
2254 size_t local_symbol_count
,
2255 const unsigned char* plocal_symbols
)
2257 if (sh_type
== elfcpp::SHT_REL
)
2259 gold_error(_("%s: unsupported REL reloc section"),
2260 object
->name().c_str());
2263 gold::scan_relocs
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
, Scan
>(
2272 needs_special_offset_handling
,
2277 // Finalize the sections.
2279 template<int size
, bool big_endian
>
2281 Target_aarch64
<size
, big_endian
>::do_finalize_sections(
2283 const Input_objects
*,
2284 Symbol_table
* symtab
)
2286 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2288 : this->plt_
->rela_plt());
2289 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2290 this->rela_dyn_
, true, false);
2292 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2293 // the .got.plt section.
2294 Symbol
* sym
= this->global_offset_table_
;
2297 uint64_t data_size
= this->got_plt_
->current_data_size();
2298 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
2300 // If the .got section is more than 0x8000 bytes, we add
2301 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
2302 // bit relocations have a greater chance of working.
2303 if (data_size
>= 0x8000)
2304 symtab
->get_sized_symbol
<size
>(sym
)->set_value(
2305 symtab
->get_sized_symbol
<size
>(sym
)->value() + 0x8000);
2308 if (parameters
->doing_static_link()
2309 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
2311 // If linking statically, make sure that the __rela_iplt symbols
2312 // were defined if necessary, even if we didn't create a PLT.
2313 static const Define_symbol_in_segment syms
[] =
2316 "__rela_iplt_start", // name
2317 elfcpp::PT_LOAD
, // segment_type
2318 elfcpp::PF_W
, // segment_flags_set
2319 elfcpp::PF(0), // segment_flags_clear
2322 elfcpp::STT_NOTYPE
, // type
2323 elfcpp::STB_GLOBAL
, // binding
2324 elfcpp::STV_HIDDEN
, // visibility
2326 Symbol::SEGMENT_START
, // offset_from_base
2330 "__rela_iplt_end", // name
2331 elfcpp::PT_LOAD
, // segment_type
2332 elfcpp::PF_W
, // segment_flags_set
2333 elfcpp::PF(0), // segment_flags_clear
2336 elfcpp::STT_NOTYPE
, // type
2337 elfcpp::STB_GLOBAL
, // binding
2338 elfcpp::STV_HIDDEN
, // visibility
2340 Symbol::SEGMENT_START
, // offset_from_base
2345 symtab
->define_symbols(layout
, 2, syms
,
2346 layout
->script_options()->saw_sections_clause());
2352 // Perform a relocation.
2354 template<int size
, bool big_endian
>
2356 Target_aarch64
<size
, big_endian
>::Relocate::relocate(
2357 const Relocate_info
<size
, big_endian
>* relinfo
,
2358 Target_aarch64
<size
, big_endian
>* target
,
2361 const elfcpp::Rela
<size
, big_endian
>& rela
,
2362 unsigned int r_type
,
2363 const Sized_symbol
<size
>* gsym
,
2364 const Symbol_value
<size
>* psymval
,
2365 unsigned char* view
,
2366 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2367 section_size_type
/* view_size */)
2372 typedef AArch64_relocate_functions
<size
, big_endian
> Reloc
;
2374 const AArch64_reloc_property
* reloc_property
=
2375 aarch64_reloc_property_table
->get_reloc_property(r_type
);
2377 if (reloc_property
== NULL
)
2379 std::string reloc_name
=
2380 aarch64_reloc_property_table
->reloc_name_in_error_message(r_type
);
2381 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2382 _("cannot relocate %s in object file"),
2383 reloc_name
.c_str());
2387 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
2389 // Pick the value to use for symbols defined in the PLT.
2390 Symbol_value
<size
> symval
;
2392 && gsym
->use_plt_offset(reloc_property
->reference_flags()))
2394 symval
.set_output_value(target
->plt_address_for_global(gsym
));
2397 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2399 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2400 if (object
->local_has_plt_offset(r_sym
))
2402 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
2407 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2409 // Get the GOT offset if needed.
2410 // For aarch64, the GOT pointer points to the start of the GOT section.
2411 bool have_got_offset
= false;
2413 int got_base
= (target
->got_
!= NULL
2414 ? (target
->got_
->current_data_size() >= 0x8000
2419 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0
:
2420 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC
:
2421 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1
:
2422 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC
:
2423 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2
:
2424 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC
:
2425 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3
:
2426 case elfcpp::R_AARCH64_GOTREL64
:
2427 case elfcpp::R_AARCH64_GOTREL32
:
2428 case elfcpp::R_AARCH64_GOT_LD_PREL19
:
2429 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15
:
2430 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
2431 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
2432 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15
:
2435 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2436 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - got_base
;
2440 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2441 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2442 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2445 have_got_offset
= true;
2448 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
2449 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
2452 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2453 got_offset
= gsym
->got_offset(GOT_TYPE_TLS_OFFSET
) - got_base
;
2457 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2458 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
));
2459 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2462 have_got_offset
= true;
2469 typename
Reloc::Status reloc_status
= Reloc::STATUS_OKAY
;
2470 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
2473 case elfcpp::R_AARCH64_NONE
:
2476 case elfcpp::R_AARCH64_ABS64
:
2477 reloc_status
= Reloc::template rela_ua
<64>(
2478 view
, object
, psymval
, addend
, reloc_property
);
2481 case elfcpp::R_AARCH64_ABS32
:
2482 reloc_status
= Reloc::template rela_ua
<32>(
2483 view
, object
, psymval
, addend
, reloc_property
);
2486 case elfcpp::R_AARCH64_ABS16
:
2487 reloc_status
= Reloc::template rela_ua
<16>(
2488 view
, object
, psymval
, addend
, reloc_property
);
2491 case elfcpp::R_AARCH64_PREL64
:
2492 reloc_status
= Reloc::template pcrela_ua
<64>(
2493 view
, object
, psymval
, addend
, address
, reloc_property
);
2495 case elfcpp::R_AARCH64_PREL32
:
2496 reloc_status
= Reloc::template pcrela_ua
<32>(
2497 view
, object
, psymval
, addend
, address
, reloc_property
);
2499 case elfcpp::R_AARCH64_PREL16
:
2500 reloc_status
= Reloc::template pcrela_ua
<16>(
2501 view
, object
, psymval
, addend
, address
, reloc_property
);
2503 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
2504 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
2505 reloc_status
= Reloc::adrp(view
, object
, psymval
, addend
, address
,
2509 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
:
2510 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
:
2511 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
:
2512 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
:
2513 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
:
2514 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
2515 reloc_status
= Reloc::template rela_general
<32>(
2516 view
, object
, psymval
, addend
, reloc_property
);
2519 case elfcpp::R_AARCH64_TSTBR14
:
2520 case elfcpp::R_AARCH64_CONDBR19
:
2521 case elfcpp::R_AARCH64_CALL26
:
2522 case elfcpp::R_AARCH64_JUMP26
:
2523 reloc_status
= Reloc::template pcrela_general
<32>(
2524 view
, object
, psymval
, addend
, address
, reloc_property
);
2527 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
2528 gold_assert(have_got_offset
);
2529 value
= target
->got_
->address() + got_base
+ got_offset
;
2530 reloc_status
= Reloc::adrp(view
, value
+ addend
, address
);
2533 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
2534 gold_assert(have_got_offset
);
2535 value
= target
->got_
->address() + got_base
+ got_offset
;
2536 reloc_status
= Reloc::template rela_general
<32>(
2537 view
, value
, addend
, reloc_property
);
2540 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
2541 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
2542 reloc_status
= relocate_tls(relinfo
, target
, relnum
, rela
, r_type
,
2543 gsym
, psymval
, view
, address
);
2546 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
2547 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
2548 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
2549 reloc_status
= relocate_tls(relinfo
, target
, relnum
, rela
, r_type
,
2550 gsym
, psymval
, view
, address
);
2554 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2555 _("unsupported reloc aaa %u"),
2560 // Report any errors.
2561 switch (reloc_status
)
2563 case Reloc::STATUS_OKAY
:
2565 case Reloc::STATUS_OVERFLOW
:
2566 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2567 _("relocation overflow in %s"),
2568 reloc_property
->name().c_str());
2570 case Reloc::STATUS_BAD_RELOC
:
2571 gold_error_at_location(
2574 rela
.get_r_offset(),
2575 _("unexpected opcode while processing relocation %s"),
2576 reloc_property
->name().c_str());
2585 template<int size
, bool big_endian
>
2587 typename AArch64_relocate_functions
<size
,big_endian
>::Status
2588 Target_aarch64
<size
, big_endian
>::Relocate::relocate_tls(
2589 const Relocate_info
<size
,big_endian
> * relinfo
,
2590 Target_aarch64
<size
, big_endian
> * target
,
2591 size_t /* relnum */,
2592 const elfcpp::Rela
<size
, big_endian
> & rela
,
2593 unsigned int r_type
, const Sized_symbol
<size
> * gsym
,
2594 const Symbol_value
<size
> * psymval
,
2595 unsigned char * view
,
2596 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
2598 typedef AArch64_relocate_functions
<size
,big_endian
> aarch64_reloc_funcs
;
2599 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_Addr
;
2601 const AArch64_reloc_property
* reloc_property
=
2602 aarch64_reloc_property_table
->get_reloc_property(r_type
);
2603 gold_assert(reloc_property
!= NULL
);
2605 Sized_relobj_file
<size
,big_endian
> * object
= relinfo
->object
;
2608 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
2609 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
2611 // Not implemented - possible IE->LE relaxation opportunity:
2612 // adrp xd, :gottprel:var => movz xd, :tprel_g1:var
2613 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
2616 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2617 got_entry_address
= target
->got_
->address() +
2618 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
2622 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2624 object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
));
2625 got_entry_address
= target
->got_
->address() +
2626 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
2628 if (r_type
== elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
)
2630 return aarch64_reloc_funcs::adrp(
2631 view
, got_entry_address
, address
);
2633 else if (r_type
== elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
)
2635 return aarch64_reloc_funcs::template rela_general
<64>(
2636 view
, got_entry_address
, 0, reloc_property
);
2642 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
2643 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
2644 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
2646 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2647 gold_assert(tls_segment
!= NULL
);
2648 AArch64_Addr value
= psymval
->value(object
, 0);
2649 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2651 if (!parameters
->options().shared())
2653 AArch64_Addr aligned_tcb_size
=
2654 align_address(target
->tcb_size(),
2655 tls_segment
->maximum_alignment());
2656 return aarch64_reloc_funcs::template
2657 rela_general
<32>(view
,
2658 value
+ aligned_tcb_size
,
2663 gold_error(_("%s: unsupported reloc %u "
2664 "in non-static TLSLE mode."),
2665 object
->name().c_str(), r_type
);
2670 gold_error(_("%s: unsupported TLS reloc %u."),
2671 object
->name().c_str(), r_type
);
2673 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
2677 // Relocate section data.
2679 template<int size
, bool big_endian
>
2681 Target_aarch64
<size
, big_endian
>::relocate_section(
2682 const Relocate_info
<size
, big_endian
>* relinfo
,
2683 unsigned int sh_type
,
2684 const unsigned char* prelocs
,
2686 Output_section
* output_section
,
2687 bool needs_special_offset_handling
,
2688 unsigned char* view
,
2689 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2690 section_size_type view_size
,
2691 const Reloc_symbol_changes
* reloc_symbol_changes
)
2693 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2694 typedef typename Target_aarch64
<size
, big_endian
>::Relocate AArch64_relocate
;
2695 gold::relocate_section
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
,
2696 AArch64_relocate
, gold::Default_comdat_behavior
>(
2702 needs_special_offset_handling
,
2706 reloc_symbol_changes
);
2709 // Return the size of a relocation while scanning during a relocatable
2712 template<int size
, bool big_endian
>
2714 Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc::
2719 // We will never support SHT_REL relocations.
2724 // Scan the relocs during a relocatable link.
2726 template<int size
, bool big_endian
>
2728 Target_aarch64
<size
, big_endian
>::scan_relocatable_relocs(
2729 Symbol_table
* symtab
,
2731 Sized_relobj_file
<size
, big_endian
>* object
,
2732 unsigned int data_shndx
,
2733 unsigned int sh_type
,
2734 const unsigned char* prelocs
,
2736 Output_section
* output_section
,
2737 bool needs_special_offset_handling
,
2738 size_t local_symbol_count
,
2739 const unsigned char* plocal_symbols
,
2740 Relocatable_relocs
* rr
)
2742 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2744 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2745 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2747 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
2748 Scan_relocatable_relocs
>(
2756 needs_special_offset_handling
,
2762 // Relocate a section during a relocatable link.
2764 template<int size
, bool big_endian
>
2766 Target_aarch64
<size
, big_endian
>::relocate_relocs(
2767 const Relocate_info
<size
, big_endian
>* relinfo
,
2768 unsigned int sh_type
,
2769 const unsigned char* prelocs
,
2771 Output_section
* output_section
,
2772 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
2773 const Relocatable_relocs
* rr
,
2774 unsigned char* view
,
2775 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2776 section_size_type view_size
,
2777 unsigned char* reloc_view
,
2778 section_size_type reloc_view_size
)
2780 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2782 gold::relocate_relocs
<size
, big_endian
, elfcpp::SHT_RELA
>(
2787 offset_in_output_section
,
2796 // The selector for aarch64 object files.
2798 template<int size
, bool big_endian
>
2799 class Target_selector_aarch64
: public Target_selector
2802 Target_selector_aarch64();
2805 do_instantiate_target()
2806 { return new Target_aarch64
<size
, big_endian
>(); }
2810 Target_selector_aarch64
<32, true>::Target_selector_aarch64()
2811 : Target_selector(elfcpp::EM_AARCH64
, 32, true,
2812 "elf32-bigaarch64", "aarch64_elf32_be_vec")
2816 Target_selector_aarch64
<32, false>::Target_selector_aarch64()
2817 : Target_selector(elfcpp::EM_AARCH64
, 32, false,
2818 "elf32-littleaarch64", "aarch64_elf32_le_vec")
2822 Target_selector_aarch64
<64, true>::Target_selector_aarch64()
2823 : Target_selector(elfcpp::EM_AARCH64
, 64, true,
2824 "elf64-bigaarch64", "aarch64_elf64_be_vec")
2828 Target_selector_aarch64
<64, false>::Target_selector_aarch64()
2829 : Target_selector(elfcpp::EM_AARCH64
, 64, false,
2830 "elf64-littleaarch64", "aarch64_elf64_le_vec")
2833 Target_selector_aarch64
<32, true> target_selector_aarch64elf32b
;
2834 Target_selector_aarch64
<32, false> target_selector_aarch64elf32
;
2835 Target_selector_aarch64
<64, true> target_selector_aarch64elfb
;
2836 Target_selector_aarch64
<64, false> target_selector_aarch64elf
;
2838 } // End anonymous namespace.