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)
80 // Write out the GOT table.
82 do_write(Output_file* of) {
83 // The first entry in the GOT is the address of the .dynamic section.
84 gold_assert(this->data_size() >= size / 8);
85 Output_section* dynamic = this->layout_->dynamic_section();
86 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
87 this->replace_constant(0, dynamic_addr);
88 Output_data_got<size, big_endian>::do_write(of);
92 // Symbol table of the output object.
93 Symbol_table* symbol_table_;
94 // A pointer to the Layout class, so that we can find the .dynamic
95 // section when we write out the GOT section.
99 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
101 // The aarch64 target class.
103 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
104 template<int size, bool big_endian>
105 class Target_aarch64 : public Sized_target<size, big_endian>
108 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
110 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
112 Target_aarch64(const Target::Target_info* info = &aarch64_info)
113 : Sized_target<size, big_endian>(info),
114 got_(NULL), plt_(NULL), got_plt_(NULL),
115 global_offset_table_(NULL), rela_dyn_(NULL),
116 copy_relocs_(elfcpp::R_AARCH64_COPY)
119 // Scan the relocations to determine unreferenced sections for
120 // garbage collection.
122 gc_process_relocs(Symbol_table* symtab,
124 Sized_relobj_file<size, big_endian>* object,
125 unsigned int data_shndx,
126 unsigned int sh_type,
127 const unsigned char* prelocs,
129 Output_section* output_section,
130 bool needs_special_offset_handling,
131 size_t local_symbol_count,
132 const unsigned char* plocal_symbols);
134 // Scan the relocations to look for symbol adjustments.
136 scan_relocs(Symbol_table* symtab,
138 Sized_relobj_file<size, big_endian>* object,
139 unsigned int data_shndx,
140 unsigned int sh_type,
141 const unsigned char* prelocs,
143 Output_section* output_section,
144 bool needs_special_offset_handling,
145 size_t local_symbol_count,
146 const unsigned char* plocal_symbols);
148 // Finalize the sections.
150 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
152 // Relocate a section.
154 relocate_section(const Relocate_info<size, big_endian>*,
155 unsigned int sh_type,
156 const unsigned char* prelocs,
158 Output_section* output_section,
159 bool needs_special_offset_handling,
161 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
162 section_size_type view_size,
163 const Reloc_symbol_changes*);
165 // Scan the relocs during a relocatable link.
167 scan_relocatable_relocs(Symbol_table* symtab,
169 Sized_relobj_file<size, big_endian>* object,
170 unsigned int data_shndx,
171 unsigned int sh_type,
172 const unsigned char* prelocs,
174 Output_section* output_section,
175 bool needs_special_offset_handling,
176 size_t local_symbol_count,
177 const unsigned char* plocal_symbols,
178 Relocatable_relocs*);
180 // Relocate a section during a relocatable link.
183 const Relocate_info<size, big_endian>*,
184 unsigned int sh_type,
185 const unsigned char* prelocs,
187 Output_section* output_section,
188 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
189 const Relocatable_relocs*,
191 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
192 section_size_type view_size,
193 unsigned char* reloc_view,
194 section_size_type reloc_view_size);
196 // Return the PLT section.
198 do_plt_address_for_global(const Symbol* gsym) const
199 { return this->plt_section()->address_for_global(gsym); }
202 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
203 { return this->plt_section()->address_for_local(relobj, symndx); }
205 // Return the number of entries in the PLT.
207 plt_entry_count() const;
209 //Return the offset of the first non-reserved PLT entry.
211 first_plt_entry_offset() const;
213 // Return the size of each PLT entry.
215 plt_entry_size() const;
219 do_select_as_default_target()
221 gold_assert(aarch64_reloc_property_table == NULL);
222 aarch64_reloc_property_table = new AArch64_reloc_property_table();
225 virtual Output_data_plt_aarch64<size, big_endian>*
226 do_make_data_plt(Layout* layout, Output_data_space* got_plt)
228 return new Output_data_plt_aarch64_standard<size, big_endian>(layout,
232 Output_data_plt_aarch64<size, big_endian>*
233 make_data_plt(Layout* layout, Output_data_space* got_plt)
235 return this->do_make_data_plt(layout, got_plt);
239 // The class which scans relocations.
244 : issued_non_pic_error_(false)
248 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
249 Sized_relobj_file<size, big_endian>* object,
250 unsigned int data_shndx,
251 Output_section* output_section,
252 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
253 const elfcpp::Sym<size, big_endian>& lsym,
257 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
258 Sized_relobj_file<size, big_endian>* object,
259 unsigned int data_shndx,
260 Output_section* output_section,
261 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
265 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
266 Target_aarch64<size, big_endian>* ,
267 Sized_relobj_file<size, big_endian>* ,
270 const elfcpp::Rela<size, big_endian>& ,
272 const elfcpp::Sym<size, big_endian>&);
275 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
276 Target_aarch64<size, big_endian>* ,
277 Sized_relobj_file<size, big_endian>* ,
280 const elfcpp::Rela<size, big_endian>& ,
286 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
287 unsigned int r_type);
290 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
291 unsigned int r_type, Symbol*);
294 possible_function_pointer_reloc(unsigned int r_type);
297 check_non_pic(Relobj*, unsigned int r_type);
299 // Whether we have issued an error about a non-PIC compilation.
300 bool issued_non_pic_error_;
303 // The class which implements relocation.
313 // Do a relocation. Return false if the caller should not issue
314 // any warnings about this relocation.
316 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
318 size_t relnum, const elfcpp::Rela<size, big_endian>&,
319 unsigned int r_type, const Sized_symbol<size>*,
320 const Symbol_value<size>*,
321 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
326 // A class which returns the size required for a relocation type,
327 // used while scanning relocs during a relocatable link.
328 class Relocatable_size_for_reloc
332 get_size_for_reloc(unsigned int, Relobj*);
335 // Adjust TLS relocation type based on the options and whether this
336 // is a local symbol.
337 static tls::Tls_optimization
338 optimize_tls_reloc(bool is_final, int r_type);
340 // Get the GOT section, creating it if necessary.
341 Output_data_got_aarch64<size, big_endian>*
342 got_section(Symbol_table*, Layout*);
344 // Get the GOT PLT section.
346 got_plt_section() const
348 gold_assert(this->got_plt_ != NULL);
349 return this->got_plt_;
352 // Create the PLT section.
354 make_plt_section(Symbol_table* symtab, Layout* layout);
356 // Create a PLT entry for a global symbol.
358 make_plt_entry(Symbol_table*, Layout*, Symbol*);
360 // Get the PLT section.
361 Output_data_plt_aarch64<size, big_endian>*
364 gold_assert(this->plt_ != NULL);
368 // Get the dynamic reloc section, creating it if necessary.
370 rela_dyn_section(Layout*);
372 // Add a potential copy relocation.
374 copy_reloc(Symbol_table* symtab, Layout* layout,
375 Sized_relobj_file<size, big_endian>* object,
376 unsigned int shndx, Output_section* output_section,
377 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
379 this->copy_relocs_.copy_reloc(symtab, layout,
380 symtab->get_sized_symbol<size>(sym),
381 object, shndx, output_section,
382 reloc, this->rela_dyn_section(layout));
385 // Information about this specific target which we pass to the
386 // general Target structure.
387 static const Target::Target_info aarch64_info;
389 // The types of GOT entries needed for this platform.
390 // These values are exposed to the ABI in an incremental link.
391 // Do not renumber existing values without changing the version
392 // number of the .gnu_incremental_inputs section.
395 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
396 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
397 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
398 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
402 Output_data_got_aarch64<size, big_endian>* got_;
404 Output_data_plt_aarch64<size, big_endian>* plt_;
405 // The GOT PLT section.
406 Output_data_space* got_plt_;
407 // The _GLOBAL_OFFSET_TABLE_ symbol.
408 Symbol* global_offset_table_;
409 // The dynamic reloc section.
410 Reloc_section* rela_dyn_;
411 // Relocs saved to avoid a COPY reloc.
412 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
416 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
419 false, // is_big_endian
420 elfcpp::EM_AARCH64, // machine_code
421 false, // has_make_symbol
422 false, // has_resolve
423 false, // has_code_fill
424 true, // is_default_stack_executable
425 false, // can_icf_inline_merge_sections
427 "/lib/ld.so.1", // program interpreter
428 0x400000, // default_text_segment_address
429 0x1000, // abi_pagesize (overridable by -z max-page-size)
430 0x1000, // common_pagesize (overridable by -z common-page-size)
431 false, // isolate_execinstr
433 elfcpp::SHN_UNDEF, // small_common_shndx
434 elfcpp::SHN_UNDEF, // large_common_shndx
435 0, // small_common_section_flags
436 0, // large_common_section_flags
437 NULL, // attributes_section
438 NULL, // attributes_vendor
439 "_start" // entry_symbol_name
443 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
446 false, // is_big_endian
447 elfcpp::EM_AARCH64, // machine_code
448 false, // has_make_symbol
449 false, // has_resolve
450 false, // has_code_fill
451 true, // is_default_stack_executable
452 false, // can_icf_inline_merge_sections
454 "/lib/ld.so.1", // program interpreter
455 0x400000, // default_text_segment_address
456 0x1000, // abi_pagesize (overridable by -z max-page-size)
457 0x1000, // common_pagesize (overridable by -z common-page-size)
458 false, // isolate_execinstr
460 elfcpp::SHN_UNDEF, // small_common_shndx
461 elfcpp::SHN_UNDEF, // large_common_shndx
462 0, // small_common_section_flags
463 0, // large_common_section_flags
464 NULL, // attributes_section
465 NULL, // attributes_vendor
466 "_start" // entry_symbol_name
470 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
473 true, // is_big_endian
474 elfcpp::EM_AARCH64, // machine_code
475 false, // has_make_symbol
476 false, // has_resolve
477 false, // has_code_fill
478 true, // is_default_stack_executable
479 false, // can_icf_inline_merge_sections
481 "/lib/ld.so.1", // program interpreter
482 0x400000, // default_text_segment_address
483 0x1000, // abi_pagesize (overridable by -z max-page-size)
484 0x1000, // common_pagesize (overridable by -z common-page-size)
485 false, // isolate_execinstr
487 elfcpp::SHN_UNDEF, // small_common_shndx
488 elfcpp::SHN_UNDEF, // large_common_shndx
489 0, // small_common_section_flags
490 0, // large_common_section_flags
491 NULL, // attributes_section
492 NULL, // attributes_vendor
493 "_start" // entry_symbol_name
497 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
500 true, // is_big_endian
501 elfcpp::EM_AARCH64, // machine_code
502 false, // has_make_symbol
503 false, // has_resolve
504 false, // has_code_fill
505 true, // is_default_stack_executable
506 false, // can_icf_inline_merge_sections
508 "/lib/ld.so.1", // program interpreter
509 0x400000, // default_text_segment_address
510 0x1000, // abi_pagesize (overridable by -z max-page-size)
511 0x1000, // common_pagesize (overridable by -z common-page-size)
512 false, // isolate_execinstr
514 elfcpp::SHN_UNDEF, // small_common_shndx
515 elfcpp::SHN_UNDEF, // large_common_shndx
516 0, // small_common_section_flags
517 0, // large_common_section_flags
518 NULL, // attributes_section
519 NULL, // attributes_vendor
520 "_start" // entry_symbol_name
523 // Get the GOT section, creating it if necessary.
525 template<int size, bool big_endian>
526 Output_data_got_aarch64<size, big_endian>*
527 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
530 if (this->got_ == NULL)
532 gold_assert(symtab != NULL && layout != NULL);
534 // When using -z now, we can treat .got.plt as a relro section.
535 // Without -z now, it is modified after program startup by lazy
537 bool is_got_plt_relro = parameters->options().now();
538 Output_section_order got_order = (is_got_plt_relro
541 Output_section_order got_plt_order = (is_got_plt_relro
543 : ORDER_NON_RELRO_FIRST);
545 // Layout of .got and .got.plt sections.
546 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
548 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
549 // .gotplt[1] reserved for ld.so (resolver)
550 // .gotplt[2] reserved
552 // Generate .got section.
553 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
555 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
556 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
557 this->got_, got_order, true);
558 // The first word of GOT is reserved for the address of .dynamic.
559 // We put 0 here now. The value will be replaced later in
560 // Output_data_got_aarch64::do_write.
561 this->got_->add_constant(0);
563 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
564 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
565 // even if there is a .got.plt section.
566 this->global_offset_table_ =
567 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
568 Symbol_table::PREDEFINED,
570 0, 0, elfcpp::STT_OBJECT,
572 elfcpp::STV_HIDDEN, 0,
575 // Generate .got.plt section.
576 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
577 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
579 | elfcpp::SHF_WRITE),
580 this->got_plt_, got_plt_order,
583 // The first three entries are reserved.
584 this->got_plt_->set_current_data_size(
585 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
587 if (!is_got_plt_relro)
589 // Those bytes can go into the relro segment.
590 layout->increase_relro(
591 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
598 // Get the dynamic reloc section, creating it if necessary.
600 template<int size, bool big_endian>
601 typename Target_aarch64<size, big_endian>::Reloc_section*
602 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
604 if (this->rela_dyn_ == NULL)
606 gold_assert(layout != NULL);
607 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
608 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
609 elfcpp::SHF_ALLOC, this->rela_dyn_,
610 ORDER_DYNAMIC_RELOCS, false);
612 return this->rela_dyn_;
615 // A class to handle the PLT data.
616 // This is an abstract base class that handles most of the linker details
617 // but does not know the actual contents of PLT entries. The derived
618 // classes below fill in those details.
620 template<int size, bool big_endian>
621 class Output_data_plt_aarch64 : public Output_section_data
624 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
626 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
628 Output_data_plt_aarch64(Layout* layout,
630 Output_data_space* got_plt)
631 : Output_section_data(addralign),
634 { this->init(layout); }
636 // Initialize the PLT section.
638 init(Layout* layout);
640 // Add an entry to the PLT.
642 add_entry(Symbol* gsym);
644 // Return the .rela.plt section data.
647 { return this->rel_; }
649 // Return whether we created a section for IRELATIVE relocations.
651 has_irelative_section() const
652 { return this->irelative_rel_ != NULL; }
654 // Return the number of PLT entries.
657 { return this->count_; }
659 // Return the offset of the first non-reserved PLT entry.
661 first_plt_entry_offset()
662 { return this->do_first_plt_entry_offset(); }
664 // Return the size of a PLT entry.
666 get_plt_entry_size() const
667 { return this->do_get_plt_entry_size(); }
669 // Return the PLT address to use for a global symbol.
671 address_for_global(const Symbol*);
673 // Return the PLT address to use for a local symbol.
675 address_for_local(const Relobj*, unsigned int symndx);
678 // Fill in the first PLT entry.
680 fill_first_plt_entry(unsigned char* pov,
683 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
685 // Fill in a normal PLT entry.
687 fill_plt_entry(unsigned char* pov,
690 unsigned int got_offset,
691 unsigned int plt_offset)
693 this->do_fill_plt_entry(pov, got_address, plt_address,
694 got_offset, plt_offset);
698 do_first_plt_entry_offset() const = 0;
701 do_get_plt_entry_size() const = 0;
704 do_fill_first_plt_entry(unsigned char* pov,
706 Address plt_addr) = 0;
709 do_fill_plt_entry(unsigned char* pov,
712 unsigned int got_offset,
713 unsigned int plt_offset) = 0;
716 do_adjust_output_section(Output_section* os);
718 // Write to a map file.
720 do_print_to_mapfile(Mapfile* mapfile) const
721 { mapfile->print_output_data(this, _("** PLT")); }
724 // Set the final size.
726 set_final_data_size();
728 // Write out the PLT data.
730 do_write(Output_file*);
732 // The reloc section.
734 // The IRELATIVE relocs, if necessary. These must follow the
735 // regular PLT relocations.
736 Reloc_section* irelative_rel_;
738 Output_data_got_aarch64<size, big_endian>* got_;
739 // The .got.plt section.
740 Output_data_space* got_plt_;
741 // The number of PLT entries.
745 // Initialize the PLT section.
747 template<int size, bool big_endian>
749 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
751 this->rel_ = new Reloc_section(false);
752 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
753 elfcpp::SHF_ALLOC, this->rel_,
754 ORDER_DYNAMIC_PLT_RELOCS, false);
757 template<int size, bool big_endian>
759 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
762 os->set_entsize(this->get_plt_entry_size());
765 // Add an entry to the PLT.
767 template<int size, bool big_endian>
769 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol* gsym)
771 gold_assert(!gsym->has_plt_offset());
773 gsym->set_plt_offset((this->count_) * this->get_plt_entry_size()
774 + this->first_plt_entry_offset());
778 section_offset_type got_offset = this->got_plt_->current_data_size();
780 // Every PLT entry needs a GOT entry which points back to the PLT
781 // entry (this will be changed by the dynamic linker, normally
782 // lazily when the function is called).
783 this->got_plt_->set_current_data_size(got_offset + size / 8);
785 // Every PLT entry needs a reloc.
786 gsym->set_needs_dynsym_entry();
787 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT,
788 this->got_plt_, got_offset, 0);
790 // Note that we don't need to save the symbol. The contents of the
791 // PLT are independent of which symbols are used. The symbols only
792 // appear in the relocations.
795 // Return the PLT address to use for a global symbol.
797 template<int size, bool big_endian>
799 Output_data_plt_aarch64<size, big_endian>::address_for_global(
803 if (gsym->type() == elfcpp::STT_GNU_IFUNC
804 && gsym->can_use_relative_reloc(false))
805 offset = (this->first_plt_entry_offset() +
806 this->count_ * this->get_plt_entry_size());
807 return this->address() + offset + gsym->plt_offset();
810 // Return the PLT address to use for a local symbol. These are always
813 template<int size, bool big_endian>
815 Output_data_plt_aarch64<size, big_endian>::address_for_local(
816 const Relobj* object,
819 return (this->address()
820 + this->first_plt_entry_offset()
821 + this->count_ * this->get_plt_entry_size()
822 + object->local_plt_offset(r_sym));
825 // Set the final size.
827 template<int size, bool big_endian>
829 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
831 this->set_data_size(this->first_plt_entry_offset()
832 + this->count_ * this->get_plt_entry_size());
835 template<int size, bool big_endian>
836 class Output_data_plt_aarch64_standard :
837 public Output_data_plt_aarch64<size, big_endian>
840 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
841 Output_data_plt_aarch64_standard(Layout* layout, Output_data_space* got_plt)
842 : Output_data_plt_aarch64<size, big_endian>(layout,
848 // Return the offset of the first non-reserved PLT entry.
850 do_first_plt_entry_offset() const
851 { return this->first_plt_entry_size; }
853 // Return the size of a PLT entry
855 do_get_plt_entry_size() const
856 { return this->plt_entry_size; }
859 do_fill_first_plt_entry(unsigned char* pov,
861 Address plt_address);
864 do_fill_plt_entry(unsigned char* pov,
867 unsigned int got_offset,
868 unsigned int plt_offset);
871 // The size of the first plt entry size.
872 static const int first_plt_entry_size = 32;
873 // The size of the plt entry size.
874 static const int plt_entry_size = 16;
875 // Template for the first PLT entry.
876 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
877 // Template for subsequent PLT entries.
878 static const uint32_t plt_entry[plt_entry_size / 4];
881 // The first entry in the PLT for an executable.
885 Output_data_plt_aarch64_standard<32, false>::
886 first_plt_entry[first_plt_entry_size / 4] =
888 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
889 0x90000010, /* adrp x16, PLT_GOT+0x8 */
890 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
891 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
892 0xd61f0220, /* br x17 */
893 0xd503201f, /* nop */
894 0xd503201f, /* nop */
895 0xd503201f, /* nop */
900 Output_data_plt_aarch64_standard<32, true>::
901 first_plt_entry[first_plt_entry_size / 4] =
903 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
904 0x90000010, /* adrp x16, PLT_GOT+0x8 */
905 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
906 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
907 0xd61f0220, /* br x17 */
908 0xd503201f, /* nop */
909 0xd503201f, /* nop */
910 0xd503201f, /* nop */
915 Output_data_plt_aarch64_standard<64, false>::
916 first_plt_entry[first_plt_entry_size / 4] =
918 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
919 0x90000010, /* adrp x16, PLT_GOT+16 */
920 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
921 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
922 0xd61f0220, /* br x17 */
923 0xd503201f, /* nop */
924 0xd503201f, /* nop */
925 0xd503201f, /* nop */
930 Output_data_plt_aarch64_standard<64, true>::
931 first_plt_entry[first_plt_entry_size / 4] =
933 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
934 0x90000010, /* adrp x16, PLT_GOT+16 */
935 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
936 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
937 0xd61f0220, /* br x17 */
938 0xd503201f, /* nop */
939 0xd503201f, /* nop */
940 0xd503201f, /* nop */
945 Output_data_plt_aarch64_standard<32, false>::
946 plt_entry[plt_entry_size / 4] =
948 0x90000010, /* adrp x16, PLTGOT + n * 4 */
949 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
950 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
951 0xd61f0220, /* br x17. */
956 Output_data_plt_aarch64_standard<32, true>::
957 plt_entry[plt_entry_size / 4] =
959 0x90000010, /* adrp x16, PLTGOT + n * 4 */
960 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
961 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
962 0xd61f0220, /* br x17. */
967 Output_data_plt_aarch64_standard<64, false>::
968 plt_entry[plt_entry_size / 4] =
970 0x90000010, /* adrp x16, PLTGOT + n * 8 */
971 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
972 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
973 0xd61f0220, /* br x17. */
978 Output_data_plt_aarch64_standard<64, true>::
979 plt_entry[plt_entry_size / 4] =
981 0x90000010, /* adrp x16, PLTGOT + n * 8 */
982 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
983 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
984 0xd61f0220, /* br x17. */
987 template<int size, bool big_endian>
989 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
994 // PLT0 of the small PLT looks like this in ELF64 -
995 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
996 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
997 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
999 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
1000 // GOTPLT entry for this.
1002 // PLT0 will be slightly different in ELF32 due to different got entry
1004 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
1005 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
1007 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
1008 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
1009 // FIXME: This only works for 64bit
1010 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
1011 gotplt_2nd_ent, plt_address + 4);
1013 // Fill in R_AARCH64_LDST8_LO12
1014 elfcpp::Swap<32, big_endian>::writeval(
1016 ((this->first_plt_entry[2] & 0xffc003ff)
1017 | ((gotplt_2nd_ent & 0xff8) << 7)));
1019 // Fill in R_AARCH64_ADD_ABS_LO12
1020 elfcpp::Swap<32, big_endian>::writeval(
1022 ((this->first_plt_entry[3] & 0xffc003ff)
1023 | ((gotplt_2nd_ent & 0xfff) << 10)));
1026 // Subsequent entries in the PLT for an executable.
1027 // FIXME: This only works for 64bit
1029 template<int size, bool big_endian>
1031 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
1033 Address got_address,
1034 Address plt_address,
1035 unsigned int got_offset,
1036 unsigned int plt_offset)
1038 memcpy(pov, this->plt_entry, this->plt_entry_size);
1040 Address gotplt_entry_address = got_address + got_offset;
1041 Address plt_entry_address = plt_address + plt_offset;
1043 // Fill in R_AARCH64_PCREL_ADR_HI21
1044 AArch64_relocate_functions<size, big_endian>::adrp(
1046 gotplt_entry_address,
1049 // Fill in R_AARCH64_LDST64_ABS_LO12
1050 elfcpp::Swap<32, big_endian>::writeval(
1052 ((this->plt_entry[1] & 0xffc003ff)
1053 | ((gotplt_entry_address & 0xff8) << 7)));
1055 // Fill in R_AARCH64_ADD_ABS_LO12
1056 elfcpp::Swap<32, big_endian>::writeval(
1058 ((this->plt_entry[2] & 0xffc003ff)
1059 | ((gotplt_entry_address & 0xfff) <<10)));
1063 // Write out the PLT. This uses the hand-coded instructions above,
1064 // and adjusts them as needed. This is specified by the AMD64 ABI.
1066 template<int size, bool big_endian>
1068 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
1070 const off_t offset = this->offset();
1071 const section_size_type oview_size =
1072 convert_to_section_size_type(this->data_size());
1073 unsigned char* const oview = of->get_output_view(offset, oview_size);
1075 const off_t got_file_offset = this->got_plt_->offset();
1076 const section_size_type got_size =
1077 convert_to_section_size_type(this->got_plt_->data_size());
1078 unsigned char* const got_view = of->get_output_view(got_file_offset,
1081 unsigned char* pov = oview;
1083 // The base address of the .plt section.
1084 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1085 // The base address of the PLT portion of the .got section.
1086 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1087 = this->got_plt_->address();
1089 this->fill_first_plt_entry(pov, got_address, plt_address);
1090 pov += this->first_plt_entry_offset();
1092 // The first three entries in .got.plt are reserved.
1093 unsigned char* got_pov = got_view;
1094 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
1095 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
1097 unsigned int plt_offset = this->first_plt_entry_offset();
1098 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
1099 const unsigned int count = this->count_;
1100 for (unsigned int plt_index = 0;
1103 pov += this->get_plt_entry_size(),
1104 got_pov += size / 8,
1105 plt_offset += this->get_plt_entry_size(),
1106 got_offset += size / 8)
1108 // Set and adjust the PLT entry itself.
1109 this->fill_plt_entry(pov, got_address, plt_address,
1110 got_offset, plt_offset);
1112 // Set the entry in the GOT, which points to plt0.
1113 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
1116 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1117 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1119 of->write_output_view(offset, oview_size, oview);
1120 of->write_output_view(got_file_offset, got_size, got_view);
1123 // Telling how to update the immediate field of an instruction.
1124 struct AArch64_howto
1126 // The immediate field mask.
1127 elfcpp::Elf_Xword dst_mask;
1129 // The offset to apply relocation immediate
1132 // The second part offset, if the immediate field has two parts.
1133 // -1 if the immediate field has only one part.
1137 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
1139 {0, -1, -1}, // DATA
1140 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
1141 {0xffffe0, 5, -1}, // LD [23:5]-imm19
1142 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
1143 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
1144 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
1145 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
1146 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
1147 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
1148 {0x3ffffff, 0, -1}, // B [25:0]-imm26
1149 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
1152 // AArch64 relocate function class
1154 template<int size, bool big_endian>
1155 class AArch64_relocate_functions
1160 STATUS_OKAY, // No error during relocation.
1161 STATUS_OVERFLOW, // Relocation overflow.
1162 STATUS_BAD_RELOC, // Relocation cannot be applied.
1166 typedef AArch64_relocate_functions<size, big_endian> This;
1167 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1169 // Return the page address of the address.
1170 // Page(address) = address & ~0xFFF
1172 static inline typename elfcpp::Swap<size, big_endian>::Valtype
1173 Page(Address address)
1175 return (address & (~static_cast<Address>(0xFFF)));
1178 // Update instruction (pointed by view) with selected bits (immed).
1179 // val = (val & ~dst_mask) | (immed << doffset)
1181 template<int valsize>
1183 update_view(unsigned char* view,
1184 typename elfcpp::Swap<size, big_endian>::Valtype immed,
1185 elfcpp::Elf_Xword doffset,
1186 elfcpp::Elf_Xword dst_mask)
1188 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1189 Valtype* wv = reinterpret_cast<Valtype*>(view);
1190 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1192 // Clear immediate fields.
1194 elfcpp::Swap<valsize, big_endian>::writeval(wv,
1195 static_cast<Valtype>(val | (immed << doffset)));
1198 // Update two parts of an instruction (pointed by view) with selected
1199 // bits (immed1 and immed2).
1200 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
1202 template<int valsize>
1204 update_view_two_parts(
1205 unsigned char* view,
1206 typename elfcpp::Swap<size, big_endian>::Valtype immed1,
1207 typename elfcpp::Swap<size, big_endian>::Valtype immed2,
1208 elfcpp::Elf_Xword doffset1,
1209 elfcpp::Elf_Xword doffset2,
1210 elfcpp::Elf_Xword dst_mask)
1212 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1213 Valtype* wv = reinterpret_cast<Valtype*>(view);
1214 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1216 elfcpp::Swap<valsize, big_endian>::writeval(wv,
1217 static_cast<Valtype>(val | (immed1 << doffset1) |
1218 (immed2 << doffset2)));
1221 // Update adr or adrp instruction with [32:12] of X.
1222 // In adr and adrp: [30:29] immlo [23:5] immhi
1225 update_adr(unsigned char* view,
1226 typename elfcpp::Swap<size, big_endian>::Valtype x,
1227 const AArch64_reloc_property* /* reloc_property */)
1229 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
1230 typename elfcpp::Swap<32, big_endian>::Valtype immed =
1231 (x >> 12) & 0x1fffff;
1232 This::template update_view_two_parts<32>(
1235 (immed & 0x1ffffc) >> 2,
1243 // Do a simple rela relocation at unaligned addresses.
1245 template<int valsize>
1246 static inline typename This::Status
1247 rela_ua(unsigned char* view,
1248 const Sized_relobj_file<size, big_endian>* object,
1249 const Symbol_value<size>* psymval,
1250 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1251 const AArch64_reloc_property* reloc_property)
1253 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1255 typename elfcpp::Elf_types<size>::Elf_Addr x =
1256 psymval->value(object, addend);
1257 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
1258 static_cast<Valtype>(x));
1259 return (reloc_property->checkup_x_value(x)
1261 : This::STATUS_OVERFLOW);
1264 // Do a simple pc-relative relocation at unaligned addresses.
1266 template<int valsize>
1267 static inline typename This::Status
1268 pcrela_ua(unsigned char* view,
1269 const Sized_relobj_file<size, big_endian>* object,
1270 const Symbol_value<size>* psymval,
1271 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1273 const AArch64_reloc_property* reloc_property)
1275 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1277 Address x = psymval->value(object, addend) - address;
1278 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
1279 static_cast<Valtype>(x));
1280 return (reloc_property->checkup_x_value(x)
1282 : This::STATUS_OVERFLOW);
1285 // Do a simple rela relocation at aligned addresses.
1287 template<int valsize>
1288 static inline typename This::Status
1290 unsigned char* view,
1291 const Sized_relobj_file<size, big_endian>* object,
1292 const Symbol_value<size>* psymval,
1293 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1294 const AArch64_reloc_property* reloc_property)
1296 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype
1298 Valtype* wv = reinterpret_cast<Valtype*>(view);
1299 Address x = psymval->value(object, addend);
1300 elfcpp::Swap<valsize, big_endian>::writeval(wv,
1301 static_cast<Valtype>(x));
1302 return (reloc_property->checkup_x_value(x)
1304 : This::STATUS_OVERFLOW);
1307 // Do relocate. Update selected bits in text.
1308 // new_val = (val & ~dst_mask) | (immed << doffset)
1310 template<int valsize>
1311 static inline typename This::Status
1312 rela_general(unsigned char* view,
1313 const Sized_relobj_file<size, big_endian>* object,
1314 const Symbol_value<size>* psymval,
1315 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1316 const AArch64_reloc_property* reloc_property)
1318 // Calculate relocation.
1319 Address x = psymval->value(object, addend);
1321 // Select bits from X.
1322 Address immed = reloc_property->select_x_value(x);
1325 const AArch64_reloc_property::Reloc_inst inst =
1326 reloc_property->reloc_inst();
1327 // If it is a data relocation or instruction has 2 parts of immediate
1328 // fields, you should not call rela_general.
1329 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1330 aarch64_howto[inst].doffset != -1);
1331 This::template update_view<valsize>(view, immed,
1332 aarch64_howto[inst].doffset,
1333 aarch64_howto[inst].dst_mask);
1335 // Do check overflow or alignment if needed.
1336 return (reloc_property->checkup_x_value(x)
1338 : This::STATUS_OVERFLOW);
1341 // Do relocate. Update selected bits in text.
1342 // new val = (val & ~dst_mask) | (immed << doffset)
1344 template<int valsize>
1345 static inline typename This::Status
1347 unsigned char* view,
1348 typename elfcpp::Swap<size, big_endian>::Valtype s,
1349 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1350 const AArch64_reloc_property* reloc_property)
1352 // Calculate relocation.
1353 Address x = s + addend;
1355 // Select bits from X.
1356 Address immed = reloc_property->select_x_value(x);
1359 const AArch64_reloc_property::Reloc_inst inst =
1360 reloc_property->reloc_inst();
1361 // If it is a data relocation or instruction has 2 parts of immediate
1362 // fields, you should not call rela_general.
1363 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1364 aarch64_howto[inst].doffset != -1);
1365 This::template update_view<valsize>(view, immed,
1366 aarch64_howto[inst].doffset,
1367 aarch64_howto[inst].dst_mask);
1369 // Do check overflow or alignment if needed.
1370 return (reloc_property->checkup_x_value(x)
1372 : This::STATUS_OVERFLOW);
1375 // Do address relative relocate. Update selected bits in text.
1376 // new val = (val & ~dst_mask) | (immed << doffset)
1378 template<int valsize>
1379 static inline typename This::Status
1381 unsigned char* view,
1382 const Sized_relobj_file<size, big_endian>* object,
1383 const Symbol_value<size>* psymval,
1384 typename elfcpp::Swap<size, big_endian>::Valtype addend,
1386 const AArch64_reloc_property* reloc_property)
1388 // Calculate relocation.
1389 Address x = psymval->value(object, addend) - address;
1391 // Select bits from X.
1392 Address immed = reloc_property->select_x_value(x);
1395 const AArch64_reloc_property::Reloc_inst inst =
1396 reloc_property->reloc_inst();
1397 // If it is a data relocation or instruction has 2 parts of immediate
1398 // fields, you should not call pcrela_general.
1399 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1400 aarch64_howto[inst].doffset != -1);
1401 This::template update_view<valsize>(view, immed,
1402 aarch64_howto[inst].doffset,
1403 aarch64_howto[inst].dst_mask);
1405 // Do check overflow or alignment if needed.
1406 return (reloc_property->checkup_x_value(x)
1408 : This::STATUS_OVERFLOW);
1411 // Calculate PG(S+A) - PG(address), update adrp instruction.
1412 // R_AARCH64_ADR_PREL_PG_HI21
1414 static inline typename This::Status
1416 unsigned char* view,
1420 typename elfcpp::Swap<size, big_endian>::Valtype x =
1421 This::Page(sa) - This::Page(address);
1422 update_adr(view, x, NULL);
1423 return (size == 64 && Bits<32>::has_overflow(x)
1424 ? This::STATUS_OVERFLOW
1425 : This::STATUS_OKAY);
1428 // Calculate PG(S+A) - PG(address), update adrp instruction.
1429 // R_AARCH64_ADR_PREL_PG_HI21
1431 static inline typename This::Status
1432 adrp(unsigned char* view,
1433 const Sized_relobj_file<size, big_endian>* object,
1434 const Symbol_value<size>* psymval,
1437 const AArch64_reloc_property* reloc_property)
1439 Address sa = psymval->value(object, addend);
1440 typename elfcpp::Swap<size, big_endian>::Valtype x =
1441 This::Page(sa) - This::Page(address);
1442 update_adr(view, x, reloc_property);
1443 return (reloc_property->checkup_x_value(x)
1445 : This::STATUS_OVERFLOW);
1450 // Return the number of entries in the PLT.
1452 template<int size, bool big_endian>
1454 Target_aarch64<size, big_endian>::plt_entry_count() const
1456 if (this->plt_ == NULL)
1458 return this->plt_->entry_count();
1461 // Return the offset of the first non-reserved PLT entry.
1463 template<int size, bool big_endian>
1465 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
1467 return this->plt_->first_plt_entry_offset();
1470 // Return the size of each PLT entry.
1472 template<int size, bool big_endian>
1474 Target_aarch64<size, big_endian>::plt_entry_size() const
1476 return this->plt_->get_plt_entry_size();
1479 // Optimize the TLS relocation type based on what we know about the
1480 // symbol. IS_FINAL is true if the final address of this symbol is
1481 // known at link time.
1483 template<int size, bool big_endian>
1484 tls::Tls_optimization
1485 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool /* is_final */,
1489 return tls::TLSOPT_NONE;
1492 // Returns true if this relocation type could be that of a function pointer.
1494 template<int size, bool big_endian>
1496 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
1497 unsigned int r_type)
1501 case elfcpp::R_AARCH64_ABS64:
1510 // For safe ICF, scan a relocation for a local symbol to check if it
1511 // corresponds to a function pointer being taken. In that case mark
1512 // the function whose pointer was taken as not foldable.
1514 template<int size, bool big_endian>
1516 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
1519 Target_aarch64<size, big_endian>* ,
1520 Sized_relobj_file<size, big_endian>* ,
1523 const elfcpp::Rela<size, big_endian>& ,
1524 unsigned int r_type,
1525 const elfcpp::Sym<size, big_endian>&)
1527 // When building a shared library, do not fold any local symbols as it is
1528 // not possible to distinguish pointer taken versus a call by looking at
1529 // the relocation types.
1530 return (parameters->options().shared()
1531 || possible_function_pointer_reloc(r_type));
1534 // For safe ICF, scan a relocation for a global symbol to check if it
1535 // corresponds to a function pointer being taken. In that case mark
1536 // the function whose pointer was taken as not foldable.
1538 template<int size, bool big_endian>
1540 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
1543 Target_aarch64<size, big_endian>* ,
1544 Sized_relobj_file<size, big_endian>* ,
1547 const elfcpp::Rela<size, big_endian>& ,
1548 unsigned int r_type,
1551 // When building a shared library, do not fold symbols whose visibility
1552 // is hidden, internal or protected.
1553 return ((parameters->options().shared()
1554 && (gsym->visibility() == elfcpp::STV_INTERNAL
1555 || gsym->visibility() == elfcpp::STV_PROTECTED
1556 || gsym->visibility() == elfcpp::STV_HIDDEN))
1557 || possible_function_pointer_reloc(r_type));
1560 // Report an unsupported relocation against a local symbol.
1562 template<int size, bool big_endian>
1564 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
1565 Sized_relobj_file<size, big_endian>* object,
1566 unsigned int r_type)
1568 gold_error(_("%s: unsupported reloc %u against local symbol"),
1569 object->name().c_str(), r_type);
1572 // We are about to emit a dynamic relocation of type R_TYPE. If the
1573 // dynamic linker does not support it, issue an error.
1575 template<int size, bool big_endian>
1577 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
1578 unsigned int r_type)
1580 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
1584 // These are the relocation types supported by glibc for AARCH64.
1585 case elfcpp::R_AARCH64_NONE:
1586 case elfcpp::R_AARCH64_COPY:
1587 case elfcpp::R_AARCH64_GLOB_DAT:
1588 case elfcpp::R_AARCH64_JUMP_SLOT:
1589 case elfcpp::R_AARCH64_RELATIVE:
1590 case elfcpp::R_AARCH64_TLS_DTPREL64:
1591 case elfcpp::R_AARCH64_TLS_DTPMOD64:
1592 case elfcpp::R_AARCH64_TLS_TPREL64:
1593 case elfcpp::R_AARCH64_TLSDESC:
1594 case elfcpp::R_AARCH64_IRELATIVE:
1595 case elfcpp::R_AARCH64_ABS32:
1596 case elfcpp::R_AARCH64_ABS64:
1603 // This prevents us from issuing more than one error per reloc
1604 // section. But we can still wind up issuing more than one
1605 // error per object file.
1606 if (this->issued_non_pic_error_)
1608 gold_assert(parameters->options().output_is_position_independent());
1609 object->error(_("requires unsupported dynamic reloc; "
1610 "recompile with -fPIC"));
1611 this->issued_non_pic_error_ = true;
1615 // Scan a relocation for a local symbol.
1617 template<int size, bool big_endian>
1619 Target_aarch64<size, big_endian>::Scan::local(
1620 Symbol_table* /* symtab */,
1621 Layout* /* layout */,
1622 Target_aarch64<size, big_endian>* /* target */,
1623 Sized_relobj_file<size, big_endian>* object,
1624 unsigned int /* data_shndx */,
1625 Output_section* /* output_section */,
1626 const elfcpp::Rela<size, big_endian>& /* reloc */,
1627 unsigned int r_type,
1628 const elfcpp::Sym<size, big_endian>& /* lsym */,
1636 case elfcpp::R_AARCH64_ABS64:
1637 case elfcpp::R_AARCH64_ABS32:
1638 case elfcpp::R_AARCH64_ABS16:
1639 // If building a shared library or pie, we need to mark this as a dynmic
1640 // reloction, so that the dynamic loader can relocate it.
1641 // Not supported yet.
1642 if (parameters->options().output_is_position_independent())
1644 gold_error(_("%s: unsupported ABS64 relocation type for pie or "
1645 "shared library.\n"),
1646 object->name().c_str());
1650 // Relocations to generate 19, 21 and 33-bit PC-relative address
1651 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
1652 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
1653 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
1654 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
1657 // Control flow, pc-relative. We don't need to do anything for a relative
1658 // addressing relocation against a local symbol if it does not reference
1660 case elfcpp::R_AARCH64_CALL26: // 283
1664 unsupported_reloc_local(object, r_type);
1669 // Report an unsupported relocation against a global symbol.
1671 template<int size, bool big_endian>
1673 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
1674 Sized_relobj_file<size, big_endian>* object,
1675 unsigned int r_type,
1678 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1679 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1682 template<int size, bool big_endian>
1684 Target_aarch64<size, big_endian>::Scan::global(
1685 Symbol_table* symtab,
1687 Target_aarch64<size, big_endian>* target,
1688 Sized_relobj_file<size, big_endian>* /* object */,
1689 unsigned int /* data_shndx */,
1690 Output_section* /* output_section */,
1691 const elfcpp::Rela<size, big_endian>& /* reloc */,
1692 unsigned int r_type,
1697 case elfcpp::R_AARCH64_ABS64:
1698 // This is used to fill the GOT absolute address.
1699 if (gsym->needs_plt_entry())
1701 target->make_plt_entry(symtab, layout, gsym);
1705 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
1706 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
1707 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
1713 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
1714 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
1716 // This pair of relocations is used to access a specific GOT entry.
1717 // Note a GOT entry is an *address* to a symbol.
1718 // The symbol requires a GOT entry
1719 Output_data_got_aarch64<size, big_endian>* got =
1720 target->got_section(symtab, layout);
1721 if (gsym->final_value_is_known())
1723 got->add_global(gsym, GOT_TYPE_STANDARD);
1727 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1728 if (gsym->is_from_dynobj()
1729 || gsym->is_undefined()
1730 || gsym->is_preemptible()
1731 || (gsym->visibility() == elfcpp::STV_PROTECTED
1732 && parameters->options().shared()))
1733 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1734 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
1737 // Not implemented yet.
1744 case elfcpp::R_AARCH64_JUMP26:
1745 case elfcpp::R_AARCH64_CALL26:
1747 if (gsym->final_value_is_known())
1750 if (gsym->is_defined() &&
1751 !gsym->is_from_dynobj() &&
1752 !gsym->is_preemptible())
1755 // Make plt entry for function call.
1756 const AArch64_reloc_property* arp =
1757 aarch64_reloc_property_table->get_reloc_property(r_type);
1758 gold_assert(arp != NULL);
1759 target->make_plt_entry(symtab, layout, gsym);
1764 gold_error(_("%s: unsupported reloc type"),
1765 aarch64_reloc_property_table->
1766 reloc_name_in_error_message(r_type).c_str());
1769 } // End of Scan::global
1771 // Create the PLT section.
1772 template<int size, bool big_endian>
1774 Target_aarch64<size, big_endian>::make_plt_section(
1775 Symbol_table* symtab, Layout* layout)
1777 if (this->plt_ == NULL)
1779 // Create the GOT section first.
1780 this->got_section(symtab, layout);
1782 this->plt_ = this->make_data_plt(layout, this->got_plt_);
1784 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1786 | elfcpp::SHF_EXECINSTR),
1787 this->plt_, ORDER_PLT, false);
1789 // Make the sh_info field of .rela.plt point to .plt.
1790 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1791 rela_plt_os->set_info_section(this->plt_->output_section());
1795 // Create a PLT entry for a global symbol.
1797 template<int size, bool big_endian>
1799 Target_aarch64<size, big_endian>::make_plt_entry(
1800 Symbol_table* symtab,
1804 if (gsym->has_plt_offset())
1807 if (this->plt_ == NULL)
1808 this->make_plt_section(symtab, layout);
1810 this->plt_->add_entry(gsym);
1813 template<int size, bool big_endian>
1815 Target_aarch64<size, big_endian>::gc_process_relocs(
1816 Symbol_table* symtab,
1818 Sized_relobj_file<size, big_endian>* object,
1819 unsigned int data_shndx,
1820 unsigned int sh_type,
1821 const unsigned char* prelocs,
1823 Output_section* output_section,
1824 bool needs_special_offset_handling,
1825 size_t local_symbol_count,
1826 const unsigned char* plocal_symbols)
1828 if (sh_type == elfcpp::SHT_REL)
1833 gold::gc_process_relocs<
1835 Target_aarch64<size, big_endian>,
1837 typename Target_aarch64<size, big_endian>::Scan,
1838 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
1847 needs_special_offset_handling,
1852 // Scan relocations for a section.
1854 template<int size, bool big_endian>
1856 Target_aarch64<size, big_endian>::scan_relocs(
1857 Symbol_table* symtab,
1859 Sized_relobj_file<size, big_endian>* object,
1860 unsigned int data_shndx,
1861 unsigned int sh_type,
1862 const unsigned char* prelocs,
1864 Output_section* output_section,
1865 bool needs_special_offset_handling,
1866 size_t local_symbol_count,
1867 const unsigned char* plocal_symbols)
1869 if (sh_type == elfcpp::SHT_REL)
1871 gold_error(_("%s: unsupported REL reloc section"),
1872 object->name().c_str());
1875 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
1884 needs_special_offset_handling,
1889 // Finalize the sections.
1891 template<int size, bool big_endian>
1893 Target_aarch64<size, big_endian>::do_finalize_sections(
1895 const Input_objects*,
1896 Symbol_table* symtab)
1898 const Reloc_section* rel_plt = (this->plt_ == NULL
1900 : this->plt_->rela_plt());
1901 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
1902 this->rela_dyn_, true, false);
1904 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1905 // the .got.plt section.
1906 Symbol* sym = this->global_offset_table_;
1909 uint64_t data_size = this->got_plt_->current_data_size();
1910 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
1912 // If the .got section is more than 0x8000 bytes, we add
1913 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
1914 // bit relocations have a greater chance of working.
1915 if (data_size >= 0x8000)
1916 symtab->get_sized_symbol<size>(sym)->set_value(
1917 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
1920 if (parameters->doing_static_link()
1921 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
1923 // If linking statically, make sure that the __rela_iplt symbols
1924 // were defined if necessary, even if we didn't create a PLT.
1925 static const Define_symbol_in_segment syms[] =
1928 "__rela_iplt_start", // name
1929 elfcpp::PT_LOAD, // segment_type
1930 elfcpp::PF_W, // segment_flags_set
1931 elfcpp::PF(0), // segment_flags_clear
1934 elfcpp::STT_NOTYPE, // type
1935 elfcpp::STB_GLOBAL, // binding
1936 elfcpp::STV_HIDDEN, // visibility
1938 Symbol::SEGMENT_START, // offset_from_base
1942 "__rela_iplt_end", // name
1943 elfcpp::PT_LOAD, // segment_type
1944 elfcpp::PF_W, // segment_flags_set
1945 elfcpp::PF(0), // segment_flags_clear
1948 elfcpp::STT_NOTYPE, // type
1949 elfcpp::STB_GLOBAL, // binding
1950 elfcpp::STV_HIDDEN, // visibility
1952 Symbol::SEGMENT_START, // offset_from_base
1957 symtab->define_symbols(layout, 2, syms,
1958 layout->script_options()->saw_sections_clause());
1964 // Perform a relocation.
1966 template<int size, bool big_endian>
1968 Target_aarch64<size, big_endian>::Relocate::relocate(
1969 const Relocate_info<size, big_endian>* relinfo,
1970 Target_aarch64<size, big_endian>* target,
1973 const elfcpp::Rela<size, big_endian>& rela,
1974 unsigned int r_type,
1975 const Sized_symbol<size>* gsym,
1976 const Symbol_value<size>* psymval,
1977 unsigned char* view,
1978 typename elfcpp::Elf_types<size>::Elf_Addr address,
1979 section_size_type /* view_size */)
1984 typedef AArch64_relocate_functions<size, big_endian> Reloc;
1986 const AArch64_reloc_property* reloc_property =
1987 aarch64_reloc_property_table->get_reloc_property(r_type);
1989 if (reloc_property == NULL)
1991 std::string reloc_name =
1992 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
1993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1994 _("cannot relocate %s in object file"),
1995 reloc_name.c_str());
1999 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2001 // Pick the value to use for symbols defined in the PLT.
2002 Symbol_value<size> symval;
2004 && gsym->use_plt_offset(reloc_property->reference_flags()))
2006 symval.set_output_value(target->plt_address_for_global(gsym));
2009 else if (gsym == NULL && psymval->is_ifunc_symbol())
2011 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2012 if (object->local_has_plt_offset(r_sym))
2014 symval.set_output_value(target->plt_address_for_local(object, r_sym));
2019 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2021 // Get the GOT offset if needed.
2022 // For aarch64, the GOT pointer points to the start of the GOT section.
2023 bool have_got_offset = false;
2025 int got_base = (target->got_ != NULL
2026 ? (target->got_->current_data_size() >= 0x8000
2031 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
2032 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
2033 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
2034 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
2035 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
2036 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
2037 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
2038 case elfcpp::R_AARCH64_GOTREL64:
2039 case elfcpp::R_AARCH64_GOTREL32:
2040 case elfcpp::R_AARCH64_GOT_LD_PREL19:
2041 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
2042 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
2043 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
2044 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
2047 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2048 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
2052 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2053 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2054 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2057 have_got_offset = true;
2063 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
2064 typename elfcpp::Elf_types<size>::Elf_Addr value;
2067 case elfcpp::R_AARCH64_NONE:
2070 case elfcpp::R_AARCH64_ABS64:
2071 reloc_status = Reloc::template rela_ua<64>(
2072 view, object, psymval, addend, reloc_property);
2075 case elfcpp::R_AARCH64_ABS32:
2076 reloc_status = Reloc::template rela_ua<32>(
2077 view, object, psymval, addend, reloc_property);
2080 case elfcpp::R_AARCH64_ABS16:
2081 reloc_status = Reloc::template rela_ua<16>(
2082 view, object, psymval, addend, reloc_property);
2085 case elfcpp::R_AARCH64_PREL64:
2086 reloc_status = Reloc::template pcrela_ua<64>(
2087 view, object, psymval, addend, address, reloc_property);
2089 case elfcpp::R_AARCH64_PREL32:
2090 reloc_status = Reloc::template pcrela_ua<32>(
2091 view, object, psymval, addend, address, reloc_property);
2093 case elfcpp::R_AARCH64_PREL16:
2094 reloc_status = Reloc::template pcrela_ua<16>(
2095 view, object, psymval, addend, address, reloc_property);
2097 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
2098 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
2099 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
2103 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
2104 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
2105 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
2106 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
2107 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
2108 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
2109 reloc_status = Reloc::template rela_general<32>(
2110 view, object, psymval, addend, reloc_property);
2113 case elfcpp::R_AARCH64_CALL26:
2114 case elfcpp::R_AARCH64_JUMP26:
2115 reloc_status = Reloc::template pcrela_general<32>(
2116 view, object, psymval, addend, address, reloc_property);
2119 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
2120 gold_assert(have_got_offset);
2121 value = target->got_->address() + got_base + got_offset;
2122 reloc_status = Reloc::adrp(view, value + addend, address);
2125 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
2126 gold_assert(have_got_offset);
2127 value = target->got_->address() + got_base + got_offset;
2128 reloc_status = Reloc::template rela_general<32>(
2129 view, value, addend, reloc_property);
2133 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2134 _("unsupported reloc aaa %u"),
2139 // Report any errors.
2140 switch (reloc_status)
2142 case Reloc::STATUS_OKAY:
2144 case Reloc::STATUS_OVERFLOW:
2145 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2146 _("relocation overflow in %s"),
2147 reloc_property->name().c_str());
2149 case Reloc::STATUS_BAD_RELOC:
2150 gold_error_at_location(
2153 rela.get_r_offset(),
2154 _("unexpected opcode while processing relocation %s"),
2155 reloc_property->name().c_str());
2164 // Relocate section data.
2166 template<int size, bool big_endian>
2168 Target_aarch64<size, big_endian>::relocate_section(
2169 const Relocate_info<size, big_endian>* relinfo,
2170 unsigned int sh_type,
2171 const unsigned char* prelocs,
2173 Output_section* output_section,
2174 bool needs_special_offset_handling,
2175 unsigned char* view,
2176 typename elfcpp::Elf_types<size>::Elf_Addr address,
2177 section_size_type view_size,
2178 const Reloc_symbol_changes* reloc_symbol_changes)
2180 gold_assert(sh_type == elfcpp::SHT_RELA);
2181 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
2182 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
2183 AArch64_relocate, gold::Default_comdat_behavior>(
2189 needs_special_offset_handling,
2193 reloc_symbol_changes);
2196 // Return the size of a relocation while scanning during a relocatable
2199 template<int size, bool big_endian>
2201 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
2206 // We will never support SHT_REL relocations.
2211 // Scan the relocs during a relocatable link.
2213 template<int size, bool big_endian>
2215 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
2216 Symbol_table* /* symtab */,
2217 Layout* /* layout */,
2218 Sized_relobj_file<size, big_endian>* /* object */,
2219 unsigned int /* data_shndx */,
2220 unsigned int sh_type,
2221 const unsigned char* /* prelocs */,
2222 size_t /* reloc_count */,
2223 Output_section* /* output_section */,
2224 bool /* needs_special_offset_handling */,
2225 size_t /* local_symbol_count */,
2226 const unsigned char* /* plocal_symbols */,
2227 Relocatable_relocs* /* rr */)
2230 gold_assert(sh_type == elfcpp::SHT_RELA);
2233 // Relocate a section during a relocatable link.
2235 template<int size, bool big_endian>
2237 Target_aarch64<size, big_endian>::relocate_relocs(
2238 const Relocate_info<size, big_endian>* /* relinfo */,
2239 unsigned int sh_type,
2240 const unsigned char* /* prelocs */,
2241 size_t /* reloc_count */,
2242 Output_section* /* output_section */,
2243 typename elfcpp::Elf_types<size>::Elf_Off /* offset_in_output_section */,
2244 const Relocatable_relocs* /* rr */,
2245 unsigned char* /* view */,
2246 typename elfcpp::Elf_types<size>::Elf_Addr /* view_address */,
2247 section_size_type /* view_size */,
2248 unsigned char* /* reloc_view */,
2249 section_size_type /* reloc_view_size */)
2252 gold_assert(sh_type == elfcpp::SHT_RELA);
2255 // The selector for aarch64 object files.
2257 template<int size, bool big_endian>
2258 class Target_selector_aarch64 : public Target_selector
2261 Target_selector_aarch64();
2264 do_instantiate_target()
2265 { return new Target_aarch64<size, big_endian>(); }
2269 Target_selector_aarch64<32, true>::Target_selector_aarch64()
2270 : Target_selector(elfcpp::EM_AARCH64, 32, true,
2271 "elf32-bigaarch64", "aarch64_elf32_be_vec")
2275 Target_selector_aarch64<32, false>::Target_selector_aarch64()
2276 : Target_selector(elfcpp::EM_AARCH64, 32, false,
2277 "elf32-littleaarch64", "aarch64_elf32_le_vec")
2281 Target_selector_aarch64<64, true>::Target_selector_aarch64()
2282 : Target_selector(elfcpp::EM_AARCH64, 64, true,
2283 "elf64-bigaarch64", "aarch64_elf64_be_vec")
2287 Target_selector_aarch64<64, false>::Target_selector_aarch64()
2288 : Target_selector(elfcpp::EM_AARCH64, 64, false,
2289 "elf64-littleaarch64", "aarch64_elf64_le_vec")
2292 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
2293 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
2294 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
2295 Target_selector_aarch64<64, false> target_selector_aarch64elf;
2297 } // End anonymous namespace.