1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@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"
50 // A class to handle the PLT data.
52 class Output_data_plt_x86_64 : public Output_section_data
55 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
57 Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got,
58 Output_data_space* got_plt,
59 Output_data_space* got_irelative)
60 : Output_section_data(16), tlsdesc_rel_(NULL), irelative_rel_(NULL),
61 got_(got), got_plt_(got_plt), got_irelative_(got_irelative), count_(0),
62 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
63 { this->init(layout); }
65 Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got,
66 Output_data_space* got_plt,
67 Output_data_space* got_irelative,
68 unsigned int plt_count)
69 : Output_section_data((plt_count + 1) * plt_entry_size, 16, false),
70 tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got), got_plt_(got_plt),
71 got_irelative_(got_irelative), count_(plt_count), irelative_count_(0),
72 tlsdesc_got_offset_(-1U), free_list_()
76 // Initialize the free list and reserve the first entry.
77 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
78 this->free_list_.remove(0, plt_entry_size);
81 // Initialize the PLT section.
85 // Add an entry to the PLT.
87 add_entry(Symbol_table*, Layout*, Symbol* gsym);
89 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
91 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
92 Sized_relobj_file<64, false>* relobj,
93 unsigned int local_sym_index);
95 // Add the relocation for a PLT entry.
97 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
98 unsigned int got_offset);
100 // Add the reserved TLSDESC_PLT entry to the PLT.
102 reserve_tlsdesc_entry(unsigned int got_offset)
103 { this->tlsdesc_got_offset_ = got_offset; }
105 // Return true if a TLSDESC_PLT entry has been reserved.
107 has_tlsdesc_entry() const
108 { return this->tlsdesc_got_offset_ != -1U; }
110 // Return the GOT offset for the reserved TLSDESC_PLT entry.
112 get_tlsdesc_got_offset() const
113 { return this->tlsdesc_got_offset_; }
115 // Return the offset of the reserved TLSDESC_PLT entry.
117 get_tlsdesc_plt_offset() const
118 { return (this->count_ + this->irelative_count_ + 1) * plt_entry_size; }
120 // Return the .rela.plt section data.
123 { return this->rel_; }
125 // Return where the TLSDESC relocations should go.
127 rela_tlsdesc(Layout*);
129 // Return where the IRELATIVE relocations should go in the PLT
132 rela_irelative(Symbol_table*, Layout*);
134 // Return whether we created a section for IRELATIVE relocations.
136 has_irelative_section() const
137 { return this->irelative_rel_ != NULL; }
139 // Return the number of PLT entries.
142 { return this->count_ + this->irelative_count_; }
144 // Return the offset of the first non-reserved PLT entry.
146 first_plt_entry_offset()
147 { return plt_entry_size; }
149 // Return the size of a PLT entry.
152 { return plt_entry_size; }
154 // Reserve a slot in the PLT for an existing symbol in an incremental update.
156 reserve_slot(unsigned int plt_index)
158 this->free_list_.remove((plt_index + 1) * plt_entry_size,
159 (plt_index + 2) * plt_entry_size);
162 // Return the PLT address to use for a global symbol.
164 address_for_global(const Symbol*);
166 // Return the PLT address to use for a local symbol.
168 address_for_local(const Relobj*, unsigned int symndx);
172 do_adjust_output_section(Output_section* os);
174 // Write to a map file.
176 do_print_to_mapfile(Mapfile* mapfile) const
177 { mapfile->print_output_data(this, _("** PLT")); }
180 // The size of an entry in the PLT.
181 static const int plt_entry_size = 16;
183 // The first entry in the PLT.
184 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
185 // procedure linkage table for both programs and shared objects."
186 static const unsigned char first_plt_entry[plt_entry_size];
188 // Other entries in the PLT for an executable.
189 static const unsigned char plt_entry[plt_entry_size];
191 // The reserved TLSDESC entry in the PLT for an executable.
192 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
194 // The .eh_frame unwind information for the PLT.
195 static const int plt_eh_frame_cie_size = 16;
196 static const int plt_eh_frame_fde_size = 32;
197 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
198 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
200 // Set the final size.
202 set_final_data_size();
204 // Write out the PLT data.
206 do_write(Output_file*);
208 // The reloc section.
210 // The TLSDESC relocs, if necessary. These must follow the regular
212 Reloc_section* tlsdesc_rel_;
213 // The IRELATIVE relocs, if necessary. These must follow the
214 // regular PLT relocations and the TLSDESC relocations.
215 Reloc_section* irelative_rel_;
217 Output_data_got<64, false>* got_;
218 // The .got.plt section.
219 Output_data_space* got_plt_;
220 // The part of the .got.plt section used for IRELATIVE relocs.
221 Output_data_space* got_irelative_;
222 // The number of PLT entries.
224 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
225 // follow the regular PLT entries.
226 unsigned int irelative_count_;
227 // Offset of the reserved TLSDESC_GOT entry when needed.
228 unsigned int tlsdesc_got_offset_;
229 // List of available regions within the section, for incremental
231 Free_list free_list_;
234 // The x86_64 target class.
236 // http://www.x86-64.org/documentation/abi.pdf
237 // TLS info comes from
238 // http://people.redhat.com/drepper/tls.pdf
239 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
241 class Target_x86_64 : public Sized_target<64, false>
244 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
245 // uses only Elf64_Rela relocation entries with explicit addends."
246 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
249 : Sized_target<64, false>(&x86_64_info),
250 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
251 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
252 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
253 dynbss_(NULL), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
254 tls_base_symbol_defined_(false)
257 // Hook for a new output section.
259 do_new_output_section(Output_section*) const;
261 // Scan the relocations to look for symbol adjustments.
263 gc_process_relocs(Symbol_table* symtab,
265 Sized_relobj_file<64, false>* object,
266 unsigned int data_shndx,
267 unsigned int sh_type,
268 const unsigned char* prelocs,
270 Output_section* output_section,
271 bool needs_special_offset_handling,
272 size_t local_symbol_count,
273 const unsigned char* plocal_symbols);
275 // Scan the relocations to look for symbol adjustments.
277 scan_relocs(Symbol_table* symtab,
279 Sized_relobj_file<64, false>* object,
280 unsigned int data_shndx,
281 unsigned int sh_type,
282 const unsigned char* prelocs,
284 Output_section* output_section,
285 bool needs_special_offset_handling,
286 size_t local_symbol_count,
287 const unsigned char* plocal_symbols);
289 // Finalize the sections.
291 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
293 // Return the value to use for a dynamic which requires special
296 do_dynsym_value(const Symbol*) const;
298 // Relocate a section.
300 relocate_section(const Relocate_info<64, false>*,
301 unsigned int sh_type,
302 const unsigned char* prelocs,
304 Output_section* output_section,
305 bool needs_special_offset_handling,
307 elfcpp::Elf_types<64>::Elf_Addr view_address,
308 section_size_type view_size,
309 const Reloc_symbol_changes*);
311 // Scan the relocs during a relocatable link.
313 scan_relocatable_relocs(Symbol_table* symtab,
315 Sized_relobj_file<64, false>* object,
316 unsigned int data_shndx,
317 unsigned int sh_type,
318 const unsigned char* prelocs,
320 Output_section* output_section,
321 bool needs_special_offset_handling,
322 size_t local_symbol_count,
323 const unsigned char* plocal_symbols,
324 Relocatable_relocs*);
326 // Relocate a section during a relocatable link.
328 relocate_for_relocatable(const Relocate_info<64, false>*,
329 unsigned int sh_type,
330 const unsigned char* prelocs,
332 Output_section* output_section,
333 off_t offset_in_output_section,
334 const Relocatable_relocs*,
336 elfcpp::Elf_types<64>::Elf_Addr view_address,
337 section_size_type view_size,
338 unsigned char* reloc_view,
339 section_size_type reloc_view_size);
341 // Return a string used to fill a code section with nops.
343 do_code_fill(section_size_type length) const;
345 // Return whether SYM is defined by the ABI.
347 do_is_defined_by_abi(const Symbol* sym) const
348 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
350 // Return the symbol index to use for a target specific relocation.
351 // The only target specific relocation is R_X86_64_TLSDESC for a
352 // local symbol, which is an absolute reloc.
354 do_reloc_symbol_index(void*, unsigned int r_type) const
356 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
360 // Return the addend to use for a target specific relocation.
362 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
364 // Return the PLT section.
366 do_plt_address_for_global(const Symbol* gsym) const
367 { return this->plt_section()->address_for_global(gsym); }
370 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
371 { return this->plt_section()->address_for_local(relobj, symndx); }
373 // This function should be defined in targets that can use relocation
374 // types to determine (implemented in local_reloc_may_be_function_pointer
375 // and global_reloc_may_be_function_pointer)
376 // if a function's pointer is taken. ICF uses this in safe mode to only
377 // fold those functions whose pointer is defintely not taken. For x86_64
378 // pie binaries, safe ICF cannot be done by looking at relocation types.
380 do_can_check_for_function_pointers() const
381 { return !parameters->options().pie(); }
383 // Return the base for a DW_EH_PE_datarel encoding.
385 do_ehframe_datarel_base() const;
387 // Adjust -fsplit-stack code which calls non-split-stack code.
389 do_calls_non_split(Relobj* object, unsigned int shndx,
390 section_offset_type fnoffset, section_size_type fnsize,
391 unsigned char* view, section_size_type view_size,
392 std::string* from, std::string* to) const;
394 // Return the size of the GOT section.
398 gold_assert(this->got_ != NULL);
399 return this->got_->data_size();
402 // Return the number of entries in the GOT.
404 got_entry_count() const
406 if (this->got_ == NULL)
408 return this->got_size() / 8;
411 // Return the number of entries in the PLT.
413 plt_entry_count() const;
415 // Return the offset of the first non-reserved PLT entry.
417 first_plt_entry_offset() const;
419 // Return the size of each PLT entry.
421 plt_entry_size() const;
423 // Create the GOT section for an incremental update.
424 Output_data_got<64, false>*
425 init_got_plt_for_update(Symbol_table* symtab,
427 unsigned int got_count,
428 unsigned int plt_count);
430 // Reserve a GOT entry for a local symbol, and regenerate any
431 // necessary dynamic relocations.
433 reserve_local_got_entry(unsigned int got_index,
434 Sized_relobj<64, false>* obj,
436 unsigned int got_type);
438 // Reserve a GOT entry for a global symbol, and regenerate any
439 // necessary dynamic relocations.
441 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
442 unsigned int got_type);
444 // Register an existing PLT entry for a global symbol.
446 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
449 // Force a COPY relocation for a given symbol.
451 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
453 // Apply an incremental relocation.
455 apply_relocation(const Relocate_info<64, false>* relinfo,
456 elfcpp::Elf_types<64>::Elf_Addr r_offset,
458 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
461 elfcpp::Elf_types<64>::Elf_Addr address,
462 section_size_type view_size);
464 // Add a new reloc argument, returning the index in the vector.
466 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
468 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
469 return this->tlsdesc_reloc_info_.size() - 1;
473 // The class which scans relocations.
478 : issued_non_pic_error_(false)
482 get_reference_flags(unsigned int r_type);
485 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
486 Sized_relobj_file<64, false>* object,
487 unsigned int data_shndx,
488 Output_section* output_section,
489 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
490 const elfcpp::Sym<64, false>& lsym);
493 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
494 Sized_relobj_file<64, false>* object,
495 unsigned int data_shndx,
496 Output_section* output_section,
497 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
501 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
502 Target_x86_64* target,
503 Sized_relobj_file<64, false>* object,
504 unsigned int data_shndx,
505 Output_section* output_section,
506 const elfcpp::Rela<64, false>& reloc,
508 const elfcpp::Sym<64, false>& lsym);
511 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
512 Target_x86_64* target,
513 Sized_relobj_file<64, false>* object,
514 unsigned int data_shndx,
515 Output_section* output_section,
516 const elfcpp::Rela<64, false>& reloc,
522 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
525 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
529 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
532 possible_function_pointer_reloc(unsigned int r_type);
535 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
536 unsigned int r_type);
538 // Whether we have issued an error about a non-PIC compilation.
539 bool issued_non_pic_error_;
542 // The class which implements relocation.
547 : skip_call_tls_get_addr_(false)
552 if (this->skip_call_tls_get_addr_)
554 // FIXME: This needs to specify the location somehow.
555 gold_error(_("missing expected TLS relocation"));
559 // Do a relocation. Return false if the caller should not issue
560 // any warnings about this relocation.
562 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
563 size_t relnum, const elfcpp::Rela<64, false>&,
564 unsigned int r_type, const Sized_symbol<64>*,
565 const Symbol_value<64>*,
566 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
570 // Do a TLS relocation.
572 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
573 size_t relnum, const elfcpp::Rela<64, false>&,
574 unsigned int r_type, const Sized_symbol<64>*,
575 const Symbol_value<64>*,
576 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
579 // Do a TLS General-Dynamic to Initial-Exec transition.
581 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
582 Output_segment* tls_segment,
583 const elfcpp::Rela<64, false>&, unsigned int r_type,
584 elfcpp::Elf_types<64>::Elf_Addr value,
586 elfcpp::Elf_types<64>::Elf_Addr,
587 section_size_type view_size);
589 // Do a TLS General-Dynamic to Local-Exec transition.
591 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
592 Output_segment* tls_segment,
593 const elfcpp::Rela<64, false>&, unsigned int r_type,
594 elfcpp::Elf_types<64>::Elf_Addr value,
596 section_size_type view_size);
598 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
600 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
601 Output_segment* tls_segment,
602 const elfcpp::Rela<64, false>&, unsigned int r_type,
603 elfcpp::Elf_types<64>::Elf_Addr value,
605 elfcpp::Elf_types<64>::Elf_Addr,
606 section_size_type view_size);
608 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
610 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
611 Output_segment* tls_segment,
612 const elfcpp::Rela<64, false>&, unsigned int r_type,
613 elfcpp::Elf_types<64>::Elf_Addr value,
615 section_size_type view_size);
617 // Do a TLS Local-Dynamic to Local-Exec transition.
619 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
620 Output_segment* tls_segment,
621 const elfcpp::Rela<64, false>&, unsigned int r_type,
622 elfcpp::Elf_types<64>::Elf_Addr value,
624 section_size_type view_size);
626 // Do a TLS Initial-Exec to Local-Exec transition.
628 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
629 Output_segment* tls_segment,
630 const elfcpp::Rela<64, false>&, unsigned int r_type,
631 elfcpp::Elf_types<64>::Elf_Addr value,
633 section_size_type view_size);
635 // This is set if we should skip the next reloc, which should be a
636 // PLT32 reloc against ___tls_get_addr.
637 bool skip_call_tls_get_addr_;
640 // A class which returns the size required for a relocation type,
641 // used while scanning relocs during a relocatable link.
642 class Relocatable_size_for_reloc
646 get_size_for_reloc(unsigned int, Relobj*);
649 // Adjust TLS relocation type based on the options and whether this
650 // is a local symbol.
651 static tls::Tls_optimization
652 optimize_tls_reloc(bool is_final, int r_type);
654 // Get the GOT section, creating it if necessary.
655 Output_data_got<64, false>*
656 got_section(Symbol_table*, Layout*);
658 // Get the GOT PLT section.
660 got_plt_section() const
662 gold_assert(this->got_plt_ != NULL);
663 return this->got_plt_;
666 // Get the GOT section for TLSDESC entries.
667 Output_data_got<64, false>*
668 got_tlsdesc_section() const
670 gold_assert(this->got_tlsdesc_ != NULL);
671 return this->got_tlsdesc_;
674 // Create the PLT section.
676 make_plt_section(Symbol_table* symtab, Layout* layout);
678 // Create a PLT entry for a global symbol.
680 make_plt_entry(Symbol_table*, Layout*, Symbol*);
682 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
684 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
685 Sized_relobj_file<64, false>* relobj,
686 unsigned int local_sym_index);
688 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
690 define_tls_base_symbol(Symbol_table*, Layout*);
692 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
694 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
696 // Create a GOT entry for the TLS module index.
698 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
699 Sized_relobj_file<64, false>* object);
701 // Get the PLT section.
702 Output_data_plt_x86_64*
705 gold_assert(this->plt_ != NULL);
709 // Get the dynamic reloc section, creating it if necessary.
711 rela_dyn_section(Layout*);
713 // Get the section to use for TLSDESC relocations.
715 rela_tlsdesc_section(Layout*) const;
717 // Get the section to use for IRELATIVE relocations.
719 rela_irelative_section(Layout*);
721 // Add a potential copy relocation.
723 copy_reloc(Symbol_table* symtab, Layout* layout,
724 Sized_relobj_file<64, false>* object,
725 unsigned int shndx, Output_section* output_section,
726 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
728 this->copy_relocs_.copy_reloc(symtab, layout,
729 symtab->get_sized_symbol<64>(sym),
730 object, shndx, output_section,
731 reloc, this->rela_dyn_section(layout));
734 // Information about this specific target which we pass to the
735 // general Target structure.
736 static const Target::Target_info x86_64_info;
738 // The types of GOT entries needed for this platform.
739 // These values are exposed to the ABI in an incremental link.
740 // Do not renumber existing values without changing the version
741 // number of the .gnu_incremental_inputs section.
744 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
745 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
746 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
747 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
750 // This type is used as the argument to the target specific
751 // relocation routines. The only target specific reloc is
752 // R_X86_64_TLSDESC against a local symbol.
755 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
756 : object(a_object), r_sym(a_r_sym)
759 // The object in which the local symbol is defined.
760 Sized_relobj_file<64, false>* object;
761 // The local symbol index in the object.
766 Output_data_got<64, false>* got_;
768 Output_data_plt_x86_64* plt_;
769 // The GOT PLT section.
770 Output_data_space* got_plt_;
771 // The GOT section for IRELATIVE relocations.
772 Output_data_space* got_irelative_;
773 // The GOT section for TLSDESC relocations.
774 Output_data_got<64, false>* got_tlsdesc_;
775 // The _GLOBAL_OFFSET_TABLE_ symbol.
776 Symbol* global_offset_table_;
777 // The dynamic reloc section.
778 Reloc_section* rela_dyn_;
779 // The section to use for IRELATIVE relocs.
780 Reloc_section* rela_irelative_;
781 // Relocs saved to avoid a COPY reloc.
782 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
783 // Space for variables copied with a COPY reloc.
784 Output_data_space* dynbss_;
785 // Offset of the GOT entry for the TLS module index.
786 unsigned int got_mod_index_offset_;
787 // We handle R_X86_64_TLSDESC against a local symbol as a target
788 // specific relocation. Here we store the object and local symbol
789 // index for the relocation.
790 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
791 // True if the _TLS_MODULE_BASE_ symbol has been defined.
792 bool tls_base_symbol_defined_;
795 const Target::Target_info Target_x86_64::x86_64_info =
798 false, // is_big_endian
799 elfcpp::EM_X86_64, // machine_code
800 false, // has_make_symbol
801 false, // has_resolve
802 true, // has_code_fill
803 true, // is_default_stack_executable
804 true, // can_icf_inline_merge_sections
806 "/lib/ld64.so.1", // program interpreter
807 0x400000, // default_text_segment_address
808 0x1000, // abi_pagesize (overridable by -z max-page-size)
809 0x1000, // common_pagesize (overridable by -z common-page-size)
810 elfcpp::SHN_UNDEF, // small_common_shndx
811 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
812 0, // small_common_section_flags
813 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
814 NULL, // attributes_section
815 NULL // attributes_vendor
818 // This is called when a new output section is created. This is where
819 // we handle the SHF_X86_64_LARGE.
822 Target_x86_64::do_new_output_section(Output_section* os) const
824 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
825 os->set_is_large_section();
828 // Get the GOT section, creating it if necessary.
830 Output_data_got<64, false>*
831 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
833 if (this->got_ == NULL)
835 gold_assert(symtab != NULL && layout != NULL);
837 this->got_ = new Output_data_got<64, false>();
839 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
841 | elfcpp::SHF_WRITE),
842 this->got_, ORDER_RELRO_LAST,
845 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
846 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
848 | elfcpp::SHF_WRITE),
849 this->got_plt_, ORDER_NON_RELRO_FIRST,
852 // The first three entries are reserved.
853 this->got_plt_->set_current_data_size(3 * 8);
855 // Those bytes can go into the relro segment.
856 layout->increase_relro(3 * 8);
858 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
859 this->global_offset_table_ =
860 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
861 Symbol_table::PREDEFINED,
863 0, 0, elfcpp::STT_OBJECT,
865 elfcpp::STV_HIDDEN, 0,
868 // If there are any IRELATIVE relocations, they get GOT entries
869 // in .got.plt after the jump slot entries.
870 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
871 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
873 | elfcpp::SHF_WRITE),
874 this->got_irelative_,
875 ORDER_NON_RELRO_FIRST, false);
877 // If there are any TLSDESC relocations, they get GOT entries in
878 // .got.plt after the jump slot and IRELATIVE entries.
879 this->got_tlsdesc_ = new Output_data_got<64, false>();
880 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
882 | elfcpp::SHF_WRITE),
884 ORDER_NON_RELRO_FIRST, false);
890 // Get the dynamic reloc section, creating it if necessary.
892 Target_x86_64::Reloc_section*
893 Target_x86_64::rela_dyn_section(Layout* layout)
895 if (this->rela_dyn_ == NULL)
897 gold_assert(layout != NULL);
898 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
899 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
900 elfcpp::SHF_ALLOC, this->rela_dyn_,
901 ORDER_DYNAMIC_RELOCS, false);
903 return this->rela_dyn_;
906 // Get the section to use for IRELATIVE relocs, creating it if
907 // necessary. These go in .rela.dyn, but only after all other dynamic
908 // relocations. They need to follow the other dynamic relocations so
909 // that they can refer to global variables initialized by those
912 Target_x86_64::Reloc_section*
913 Target_x86_64::rela_irelative_section(Layout* layout)
915 if (this->rela_irelative_ == NULL)
917 // Make sure we have already created the dynamic reloc section.
918 this->rela_dyn_section(layout);
919 this->rela_irelative_ = new Reloc_section(false);
920 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
921 elfcpp::SHF_ALLOC, this->rela_irelative_,
922 ORDER_DYNAMIC_RELOCS, false);
923 gold_assert(this->rela_dyn_->output_section()
924 == this->rela_irelative_->output_section());
926 return this->rela_irelative_;
929 // Initialize the PLT section.
932 Output_data_plt_x86_64::init(Layout* layout)
934 this->rel_ = new Reloc_section(false);
935 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
936 elfcpp::SHF_ALLOC, this->rel_,
937 ORDER_DYNAMIC_PLT_RELOCS, false);
939 // Add unwind information if requested.
940 if (parameters->options().ld_generated_unwind_info())
941 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
942 plt_eh_frame_fde, plt_eh_frame_fde_size);
946 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
948 os->set_entsize(plt_entry_size);
951 // Add an entry to the PLT.
954 Output_data_plt_x86_64::add_entry(Symbol_table* symtab, Layout* layout,
957 gold_assert(!gsym->has_plt_offset());
959 unsigned int plt_index;
961 section_offset_type got_offset;
963 unsigned int* pcount;
965 unsigned int reserved;
966 Output_data_space* got;
967 if (gsym->type() == elfcpp::STT_GNU_IFUNC
968 && gsym->can_use_relative_reloc(false))
970 pcount = &this->irelative_count_;
973 got = this->got_irelative_;
977 pcount = &this->count_;
980 got = this->got_plt_;
983 if (!this->is_data_size_valid())
985 // Note that when setting the PLT offset for a non-IRELATIVE
986 // entry we skip the initial reserved PLT entry.
987 plt_index = *pcount + offset;
988 plt_offset = plt_index * plt_entry_size;
992 got_offset = (plt_index - offset + reserved) * 8;
993 gold_assert(got_offset == got->current_data_size());
995 // Every PLT entry needs a GOT entry which points back to the PLT
996 // entry (this will be changed by the dynamic linker, normally
997 // lazily when the function is called).
998 got->set_current_data_size(got_offset + 8);
1002 // FIXME: This is probably not correct for IRELATIVE relocs.
1004 // For incremental updates, find an available slot.
1005 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
1006 if (plt_offset == -1)
1007 gold_fallback(_("out of patch space (PLT);"
1008 " relink with --incremental-full"));
1010 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1011 // can be calculated from the PLT index, adjusting for the three
1012 // reserved entries at the beginning of the GOT.
1013 plt_index = plt_offset / plt_entry_size - 1;
1014 got_offset = (plt_index - offset + reserved) * 8;
1017 gsym->set_plt_offset(plt_offset);
1019 // Every PLT entry needs a reloc.
1020 this->add_relocation(symtab, layout, gsym, got_offset);
1022 // Note that we don't need to save the symbol. The contents of the
1023 // PLT are independent of which symbols are used. The symbols only
1024 // appear in the relocations.
1027 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1031 Output_data_plt_x86_64::add_local_ifunc_entry(
1032 Symbol_table* symtab,
1034 Sized_relobj_file<64, false>* relobj,
1035 unsigned int local_sym_index)
1037 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
1038 ++this->irelative_count_;
1040 section_offset_type got_offset = this->got_irelative_->current_data_size();
1042 // Every PLT entry needs a GOT entry which points back to the PLT
1044 this->got_irelative_->set_current_data_size(got_offset + 8);
1046 // Every PLT entry needs a reloc.
1047 Reloc_section* rela = this->rela_irelative(symtab, layout);
1048 rela->add_symbolless_local_addend(relobj, local_sym_index,
1049 elfcpp::R_X86_64_IRELATIVE,
1050 this->got_irelative_, got_offset, 0);
1055 // Add the relocation for a PLT entry.
1058 Output_data_plt_x86_64::add_relocation(Symbol_table* symtab, Layout* layout,
1059 Symbol* gsym, unsigned int got_offset)
1061 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1062 && gsym->can_use_relative_reloc(false))
1064 Reloc_section* rela = this->rela_irelative(symtab, layout);
1065 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1066 this->got_irelative_, got_offset, 0);
1070 gsym->set_needs_dynsym_entry();
1071 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1076 // Return where the TLSDESC relocations should go, creating it if
1077 // necessary. These follow the JUMP_SLOT relocations.
1079 Output_data_plt_x86_64::Reloc_section*
1080 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
1082 if (this->tlsdesc_rel_ == NULL)
1084 this->tlsdesc_rel_ = new Reloc_section(false);
1085 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1086 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1087 ORDER_DYNAMIC_PLT_RELOCS, false);
1088 gold_assert(this->tlsdesc_rel_->output_section()
1089 == this->rel_->output_section());
1091 return this->tlsdesc_rel_;
1094 // Return where the IRELATIVE relocations should go in the PLT. These
1095 // follow the JUMP_SLOT and the TLSDESC relocations.
1097 Output_data_plt_x86_64::Reloc_section*
1098 Output_data_plt_x86_64::rela_irelative(Symbol_table* symtab, Layout* layout)
1100 if (this->irelative_rel_ == NULL)
1102 // Make sure we have a place for the TLSDESC relocations, in
1103 // case we see any later on.
1104 this->rela_tlsdesc(layout);
1105 this->irelative_rel_ = new Reloc_section(false);
1106 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1107 elfcpp::SHF_ALLOC, this->irelative_rel_,
1108 ORDER_DYNAMIC_PLT_RELOCS, false);
1109 gold_assert(this->irelative_rel_->output_section()
1110 == this->rel_->output_section());
1112 if (parameters->doing_static_link())
1114 // A statically linked executable will only have a .rela.plt
1115 // section to hold R_X86_64_IRELATIVE relocs for
1116 // STT_GNU_IFUNC symbols. The library will use these
1117 // symbols to locate the IRELATIVE relocs at program startup
1119 symtab->define_in_output_data("__rela_iplt_start", NULL,
1120 Symbol_table::PREDEFINED,
1121 this->irelative_rel_, 0, 0,
1122 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1123 elfcpp::STV_HIDDEN, 0, false, true);
1124 symtab->define_in_output_data("__rela_iplt_end", NULL,
1125 Symbol_table::PREDEFINED,
1126 this->irelative_rel_, 0, 0,
1127 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1128 elfcpp::STV_HIDDEN, 0, true, true);
1131 return this->irelative_rel_;
1134 // Return the PLT address to use for a global symbol.
1137 Output_data_plt_x86_64::address_for_global(const Symbol* gsym)
1139 uint64_t offset = 0;
1140 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1141 && gsym->can_use_relative_reloc(false))
1142 offset = (this->count_ + 1) * plt_entry_size;
1143 return this->address() + offset;
1146 // Return the PLT address to use for a local symbol. These are always
1147 // IRELATIVE relocs.
1150 Output_data_plt_x86_64::address_for_local(const Relobj*, unsigned int)
1152 return this->address() + (this->count_ + 1) * plt_entry_size;
1155 // Set the final size.
1157 Output_data_plt_x86_64::set_final_data_size()
1159 unsigned int count = this->count_ + this->irelative_count_;
1160 if (this->has_tlsdesc_entry())
1162 this->set_data_size((count + 1) * plt_entry_size);
1165 // The first entry in the PLT for an executable.
1167 const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1169 // From AMD64 ABI Draft 0.98, page 76
1170 0xff, 0x35, // pushq contents of memory address
1171 0, 0, 0, 0, // replaced with address of .got + 8
1172 0xff, 0x25, // jmp indirect
1173 0, 0, 0, 0, // replaced with address of .got + 16
1174 0x90, 0x90, 0x90, 0x90 // noop (x4)
1177 // Subsequent entries in the PLT for an executable.
1179 const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1181 // From AMD64 ABI Draft 0.98, page 76
1182 0xff, 0x25, // jmpq indirect
1183 0, 0, 0, 0, // replaced with address of symbol in .got
1184 0x68, // pushq immediate
1185 0, 0, 0, 0, // replaced with offset into relocation table
1186 0xe9, // jmpq relative
1187 0, 0, 0, 0 // replaced with offset to start of .plt
1190 // The reserved TLSDESC entry in the PLT for an executable.
1192 const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1194 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1195 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1196 0xff, 0x35, // pushq x(%rip)
1197 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1198 0xff, 0x25, // jmpq *y(%rip)
1199 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1204 // The .eh_frame unwind information for the PLT.
1207 Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1210 'z', // Augmentation: augmentation size included.
1211 'R', // Augmentation: FDE encoding included.
1212 '\0', // End of augmentation string.
1213 1, // Code alignment factor.
1214 0x78, // Data alignment factor.
1215 16, // Return address column.
1216 1, // Augmentation size.
1217 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1218 | elfcpp::DW_EH_PE_sdata4),
1219 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1220 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1221 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1226 Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1228 0, 0, 0, 0, // Replaced with offset to .plt.
1229 0, 0, 0, 0, // Replaced with size of .plt.
1230 0, // Augmentation size.
1231 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1232 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1233 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1234 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1235 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1236 11, // Block length.
1237 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1238 elfcpp::DW_OP_breg16, 0, // Push %rip.
1239 elfcpp::DW_OP_lit15, // Push 0xf.
1240 elfcpp::DW_OP_and, // & (%rip & 0xf).
1241 elfcpp::DW_OP_lit11, // Push 0xb.
1242 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1243 elfcpp::DW_OP_lit3, // Push 3.
1244 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1245 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1246 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1252 // Write out the PLT. This uses the hand-coded instructions above,
1253 // and adjusts them as needed. This is specified by the AMD64 ABI.
1256 Output_data_plt_x86_64::do_write(Output_file* of)
1258 const off_t offset = this->offset();
1259 const section_size_type oview_size =
1260 convert_to_section_size_type(this->data_size());
1261 unsigned char* const oview = of->get_output_view(offset, oview_size);
1263 const off_t got_file_offset = this->got_plt_->offset();
1264 gold_assert(parameters->incremental_update()
1265 || (got_file_offset + this->got_plt_->data_size()
1266 == this->got_irelative_->offset()));
1267 const section_size_type got_size =
1268 convert_to_section_size_type(this->got_plt_->data_size()
1269 + this->got_irelative_->data_size());
1270 unsigned char* const got_view = of->get_output_view(got_file_offset,
1273 unsigned char* pov = oview;
1275 // The base address of the .plt section.
1276 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1277 // The base address of the .got section.
1278 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1279 // The base address of the PLT portion of the .got section,
1280 // which is where the GOT pointer will point, and where the
1281 // three reserved GOT entries are located.
1282 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1284 memcpy(pov, first_plt_entry, plt_entry_size);
1285 // We do a jmp relative to the PC at the end of this instruction.
1286 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1288 - (plt_address + 6)));
1289 elfcpp::Swap<32, false>::writeval(pov + 8,
1291 - (plt_address + 12)));
1292 pov += plt_entry_size;
1294 unsigned char* got_pov = got_view;
1296 memset(got_pov, 0, 24);
1299 unsigned int plt_offset = plt_entry_size;
1300 unsigned int got_offset = 24;
1301 const unsigned int count = this->count_ + this->irelative_count_;
1302 for (unsigned int plt_index = 0;
1305 pov += plt_entry_size,
1307 plt_offset += plt_entry_size,
1310 // Set and adjust the PLT entry itself.
1311 memcpy(pov, plt_entry, plt_entry_size);
1312 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1313 (got_address + got_offset
1314 - (plt_address + plt_offset
1317 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1318 elfcpp::Swap<32, false>::writeval(pov + 12,
1319 - (plt_offset + plt_entry_size));
1321 // Set the entry in the GOT.
1322 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1325 if (this->has_tlsdesc_entry())
1327 // Set and adjust the reserved TLSDESC PLT entry.
1328 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1329 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1330 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1332 - (plt_address + plt_offset
1334 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1336 + tlsdesc_got_offset
1337 - (plt_address + plt_offset
1339 pov += plt_entry_size;
1342 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1343 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1345 of->write_output_view(offset, oview_size, oview);
1346 of->write_output_view(got_file_offset, got_size, got_view);
1349 // Create the PLT section.
1352 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1354 if (this->plt_ == NULL)
1356 // Create the GOT sections first.
1357 this->got_section(symtab, layout);
1359 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
1361 this->got_irelative_);
1362 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1364 | elfcpp::SHF_EXECINSTR),
1365 this->plt_, ORDER_PLT, false);
1367 // Make the sh_info field of .rela.plt point to .plt.
1368 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1369 rela_plt_os->set_info_section(this->plt_->output_section());
1373 // Return the section for TLSDESC relocations.
1375 Target_x86_64::Reloc_section*
1376 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1378 return this->plt_section()->rela_tlsdesc(layout);
1381 // Create a PLT entry for a global symbol.
1384 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1387 if (gsym->has_plt_offset())
1390 if (this->plt_ == NULL)
1391 this->make_plt_section(symtab, layout);
1393 this->plt_->add_entry(symtab, layout, gsym);
1396 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1399 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1400 Sized_relobj_file<64, false>* relobj,
1401 unsigned int local_sym_index)
1403 if (relobj->local_has_plt_offset(local_sym_index))
1405 if (this->plt_ == NULL)
1406 this->make_plt_section(symtab, layout);
1407 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1410 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1413 // Return the number of entries in the PLT.
1416 Target_x86_64::plt_entry_count() const
1418 if (this->plt_ == NULL)
1420 return this->plt_->entry_count();
1423 // Return the offset of the first non-reserved PLT entry.
1426 Target_x86_64::first_plt_entry_offset() const
1428 return Output_data_plt_x86_64::first_plt_entry_offset();
1431 // Return the size of each PLT entry.
1434 Target_x86_64::plt_entry_size() const
1436 return Output_data_plt_x86_64::get_plt_entry_size();
1439 // Create the GOT and PLT sections for an incremental update.
1441 Output_data_got<64, false>*
1442 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1444 unsigned int got_count,
1445 unsigned int plt_count)
1447 gold_assert(this->got_ == NULL);
1449 this->got_ = new Output_data_got<64, false>(got_count * 8);
1450 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1452 | elfcpp::SHF_WRITE),
1453 this->got_, ORDER_RELRO_LAST,
1456 // Add the three reserved entries.
1457 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1458 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1460 | elfcpp::SHF_WRITE),
1461 this->got_plt_, ORDER_NON_RELRO_FIRST,
1464 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1465 this->global_offset_table_ =
1466 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1467 Symbol_table::PREDEFINED,
1469 0, 0, elfcpp::STT_OBJECT,
1471 elfcpp::STV_HIDDEN, 0,
1474 // If there are any TLSDESC relocations, they get GOT entries in
1475 // .got.plt after the jump slot entries.
1476 // FIXME: Get the count for TLSDESC entries.
1477 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1478 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1479 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1481 ORDER_NON_RELRO_FIRST, false);
1483 // If there are any IRELATIVE relocations, they get GOT entries in
1484 // .got.plt after the jump slot and TLSDESC entries.
1485 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1486 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1487 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1488 this->got_irelative_,
1489 ORDER_NON_RELRO_FIRST, false);
1491 // Create the PLT section.
1492 this->plt_ = new Output_data_plt_x86_64(layout, this->got_, this->got_plt_,
1493 this->got_irelative_, plt_count);
1494 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1495 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1496 this->plt_, ORDER_PLT, false);
1498 // Make the sh_info field of .rela.plt point to .plt.
1499 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1500 rela_plt_os->set_info_section(this->plt_->output_section());
1502 // Create the rela_dyn section.
1503 this->rela_dyn_section(layout);
1508 // Reserve a GOT entry for a local symbol, and regenerate any
1509 // necessary dynamic relocations.
1512 Target_x86_64::reserve_local_got_entry(
1513 unsigned int got_index,
1514 Sized_relobj<64, false>* obj,
1516 unsigned int got_type)
1518 unsigned int got_offset = got_index * 8;
1519 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1521 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1524 case GOT_TYPE_STANDARD:
1525 if (parameters->options().output_is_position_independent())
1526 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1527 this->got_, got_offset, 0);
1529 case GOT_TYPE_TLS_OFFSET:
1530 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1531 this->got_, got_offset, 0);
1533 case GOT_TYPE_TLS_PAIR:
1534 this->got_->reserve_slot(got_index + 1);
1535 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1536 this->got_, got_offset, 0);
1538 case GOT_TYPE_TLS_DESC:
1539 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1540 // this->got_->reserve_slot(got_index + 1);
1541 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1542 // this->got_, got_offset, 0);
1549 // Reserve a GOT entry for a global symbol, and regenerate any
1550 // necessary dynamic relocations.
1553 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1554 unsigned int got_type)
1556 unsigned int got_offset = got_index * 8;
1557 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1559 this->got_->reserve_global(got_index, gsym, got_type);
1562 case GOT_TYPE_STANDARD:
1563 if (!gsym->final_value_is_known())
1565 if (gsym->is_from_dynobj()
1566 || gsym->is_undefined()
1567 || gsym->is_preemptible()
1568 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1569 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1570 this->got_, got_offset, 0);
1572 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1573 this->got_, got_offset, 0);
1576 case GOT_TYPE_TLS_OFFSET:
1577 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1578 this->got_, got_offset, 0);
1580 case GOT_TYPE_TLS_PAIR:
1581 this->got_->reserve_slot(got_index + 1);
1582 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1583 this->got_, got_offset, 0);
1584 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1585 this->got_, got_offset + 8, 0);
1587 case GOT_TYPE_TLS_DESC:
1588 this->got_->reserve_slot(got_index + 1);
1589 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1590 this->got_, got_offset, 0);
1597 // Register an existing PLT entry for a global symbol.
1600 Target_x86_64::register_global_plt_entry(Symbol_table* symtab,
1602 unsigned int plt_index,
1605 gold_assert(this->plt_ != NULL);
1606 gold_assert(!gsym->has_plt_offset());
1608 this->plt_->reserve_slot(plt_index);
1610 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1612 unsigned int got_offset = (plt_index + 3) * 8;
1613 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
1616 // Force a COPY relocation for a given symbol.
1619 Target_x86_64::emit_copy_reloc(
1620 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1622 this->copy_relocs_.emit_copy_reloc(symtab,
1623 symtab->get_sized_symbol<64>(sym),
1626 this->rela_dyn_section(NULL));
1629 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1632 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1634 if (this->tls_base_symbol_defined_)
1637 Output_segment* tls_segment = layout->tls_segment();
1638 if (tls_segment != NULL)
1640 bool is_exec = parameters->options().output_is_executable();
1641 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1642 Symbol_table::PREDEFINED,
1646 elfcpp::STV_HIDDEN, 0,
1648 ? Symbol::SEGMENT_END
1649 : Symbol::SEGMENT_START),
1652 this->tls_base_symbol_defined_ = true;
1655 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1658 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1661 if (this->plt_ == NULL)
1662 this->make_plt_section(symtab, layout);
1664 if (!this->plt_->has_tlsdesc_entry())
1666 // Allocate the TLSDESC_GOT entry.
1667 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1668 unsigned int got_offset = got->add_constant(0);
1670 // Allocate the TLSDESC_PLT entry.
1671 this->plt_->reserve_tlsdesc_entry(got_offset);
1675 // Create a GOT entry for the TLS module index.
1678 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1679 Sized_relobj_file<64, false>* object)
1681 if (this->got_mod_index_offset_ == -1U)
1683 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1684 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1685 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1686 unsigned int got_offset = got->add_constant(0);
1687 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1689 got->add_constant(0);
1690 this->got_mod_index_offset_ = got_offset;
1692 return this->got_mod_index_offset_;
1695 // Optimize the TLS relocation type based on what we know about the
1696 // symbol. IS_FINAL is true if the final address of this symbol is
1697 // known at link time.
1699 tls::Tls_optimization
1700 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1702 // If we are generating a shared library, then we can't do anything
1704 if (parameters->options().shared())
1705 return tls::TLSOPT_NONE;
1709 case elfcpp::R_X86_64_TLSGD:
1710 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1711 case elfcpp::R_X86_64_TLSDESC_CALL:
1712 // These are General-Dynamic which permits fully general TLS
1713 // access. Since we know that we are generating an executable,
1714 // we can convert this to Initial-Exec. If we also know that
1715 // this is a local symbol, we can further switch to Local-Exec.
1717 return tls::TLSOPT_TO_LE;
1718 return tls::TLSOPT_TO_IE;
1720 case elfcpp::R_X86_64_TLSLD:
1721 // This is Local-Dynamic, which refers to a local symbol in the
1722 // dynamic TLS block. Since we know that we generating an
1723 // executable, we can switch to Local-Exec.
1724 return tls::TLSOPT_TO_LE;
1726 case elfcpp::R_X86_64_DTPOFF32:
1727 case elfcpp::R_X86_64_DTPOFF64:
1728 // Another Local-Dynamic reloc.
1729 return tls::TLSOPT_TO_LE;
1731 case elfcpp::R_X86_64_GOTTPOFF:
1732 // These are Initial-Exec relocs which get the thread offset
1733 // from the GOT. If we know that we are linking against the
1734 // local symbol, we can switch to Local-Exec, which links the
1735 // thread offset into the instruction.
1737 return tls::TLSOPT_TO_LE;
1738 return tls::TLSOPT_NONE;
1740 case elfcpp::R_X86_64_TPOFF32:
1741 // When we already have Local-Exec, there is nothing further we
1743 return tls::TLSOPT_NONE;
1750 // Get the Reference_flags for a particular relocation.
1753 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1757 case elfcpp::R_X86_64_NONE:
1758 case elfcpp::R_X86_64_GNU_VTINHERIT:
1759 case elfcpp::R_X86_64_GNU_VTENTRY:
1760 case elfcpp::R_X86_64_GOTPC32:
1761 case elfcpp::R_X86_64_GOTPC64:
1762 // No symbol reference.
1765 case elfcpp::R_X86_64_64:
1766 case elfcpp::R_X86_64_32:
1767 case elfcpp::R_X86_64_32S:
1768 case elfcpp::R_X86_64_16:
1769 case elfcpp::R_X86_64_8:
1770 return Symbol::ABSOLUTE_REF;
1772 case elfcpp::R_X86_64_PC64:
1773 case elfcpp::R_X86_64_PC32:
1774 case elfcpp::R_X86_64_PC16:
1775 case elfcpp::R_X86_64_PC8:
1776 case elfcpp::R_X86_64_GOTOFF64:
1777 return Symbol::RELATIVE_REF;
1779 case elfcpp::R_X86_64_PLT32:
1780 case elfcpp::R_X86_64_PLTOFF64:
1781 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1783 case elfcpp::R_X86_64_GOT64:
1784 case elfcpp::R_X86_64_GOT32:
1785 case elfcpp::R_X86_64_GOTPCREL64:
1786 case elfcpp::R_X86_64_GOTPCREL:
1787 case elfcpp::R_X86_64_GOTPLT64:
1789 return Symbol::ABSOLUTE_REF;
1791 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1792 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1793 case elfcpp::R_X86_64_TLSDESC_CALL:
1794 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1795 case elfcpp::R_X86_64_DTPOFF32:
1796 case elfcpp::R_X86_64_DTPOFF64:
1797 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1798 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1799 return Symbol::TLS_REF;
1801 case elfcpp::R_X86_64_COPY:
1802 case elfcpp::R_X86_64_GLOB_DAT:
1803 case elfcpp::R_X86_64_JUMP_SLOT:
1804 case elfcpp::R_X86_64_RELATIVE:
1805 case elfcpp::R_X86_64_IRELATIVE:
1806 case elfcpp::R_X86_64_TPOFF64:
1807 case elfcpp::R_X86_64_DTPMOD64:
1808 case elfcpp::R_X86_64_TLSDESC:
1809 case elfcpp::R_X86_64_SIZE32:
1810 case elfcpp::R_X86_64_SIZE64:
1812 // Not expected. We will give an error later.
1817 // Report an unsupported relocation against a local symbol.
1820 Target_x86_64::Scan::unsupported_reloc_local(
1821 Sized_relobj_file<64, false>* object,
1822 unsigned int r_type)
1824 gold_error(_("%s: unsupported reloc %u against local symbol"),
1825 object->name().c_str(), r_type);
1828 // We are about to emit a dynamic relocation of type R_TYPE. If the
1829 // dynamic linker does not support it, issue an error. The GNU linker
1830 // only issues a non-PIC error for an allocated read-only section.
1831 // Here we know the section is allocated, but we don't know that it is
1832 // read-only. But we check for all the relocation types which the
1833 // glibc dynamic linker supports, so it seems appropriate to issue an
1834 // error even if the section is not read-only. If GSYM is not NULL,
1835 // it is the symbol the relocation is against; if it is NULL, the
1836 // relocation is against a local symbol.
1839 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1844 // These are the relocation types supported by glibc for x86_64
1845 // which should always work.
1846 case elfcpp::R_X86_64_RELATIVE:
1847 case elfcpp::R_X86_64_IRELATIVE:
1848 case elfcpp::R_X86_64_GLOB_DAT:
1849 case elfcpp::R_X86_64_JUMP_SLOT:
1850 case elfcpp::R_X86_64_DTPMOD64:
1851 case elfcpp::R_X86_64_DTPOFF64:
1852 case elfcpp::R_X86_64_TPOFF64:
1853 case elfcpp::R_X86_64_64:
1854 case elfcpp::R_X86_64_COPY:
1857 // glibc supports these reloc types, but they can overflow.
1858 case elfcpp::R_X86_64_PC32:
1859 // A PC relative reference is OK against a local symbol or if
1860 // the symbol is defined locally.
1862 || (!gsym->is_from_dynobj()
1863 && !gsym->is_undefined()
1864 && !gsym->is_preemptible()))
1867 case elfcpp::R_X86_64_32:
1868 if (this->issued_non_pic_error_)
1870 gold_assert(parameters->options().output_is_position_independent());
1872 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1873 "overflow at runtime; recompile with -fPIC"));
1875 object->error(_("requires dynamic %s reloc against '%s' which may "
1876 "overflow at runtime; recompile with -fPIC"),
1877 (r_type == elfcpp::R_X86_64_32
1881 this->issued_non_pic_error_ = true;
1885 // This prevents us from issuing more than one error per reloc
1886 // section. But we can still wind up issuing more than one
1887 // error per object file.
1888 if (this->issued_non_pic_error_)
1890 gold_assert(parameters->options().output_is_position_independent());
1891 object->error(_("requires unsupported dynamic reloc %u; "
1892 "recompile with -fPIC"),
1894 this->issued_non_pic_error_ = true;
1897 case elfcpp::R_X86_64_NONE:
1902 // Return whether we need to make a PLT entry for a relocation of the
1903 // given type against a STT_GNU_IFUNC symbol.
1906 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1907 Sized_relobj_file<64, false>* object,
1908 unsigned int r_type)
1910 int flags = Scan::get_reference_flags(r_type);
1911 if (flags & Symbol::TLS_REF)
1912 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1913 object->name().c_str(), r_type);
1917 // Scan a relocation for a local symbol.
1920 Target_x86_64::Scan::local(Symbol_table* symtab,
1922 Target_x86_64* target,
1923 Sized_relobj_file<64, false>* object,
1924 unsigned int data_shndx,
1925 Output_section* output_section,
1926 const elfcpp::Rela<64, false>& reloc,
1927 unsigned int r_type,
1928 const elfcpp::Sym<64, false>& lsym)
1930 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1931 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1932 && this->reloc_needs_plt_for_ifunc(object, r_type))
1934 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1935 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1940 case elfcpp::R_X86_64_NONE:
1941 case elfcpp::R_X86_64_GNU_VTINHERIT:
1942 case elfcpp::R_X86_64_GNU_VTENTRY:
1945 case elfcpp::R_X86_64_64:
1946 // If building a shared library (or a position-independent
1947 // executable), we need to create a dynamic relocation for this
1948 // location. The relocation applied at link time will apply the
1949 // link-time value, so we flag the location with an
1950 // R_X86_64_RELATIVE relocation so the dynamic loader can
1951 // relocate it easily.
1952 if (parameters->options().output_is_position_independent())
1954 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1955 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1956 rela_dyn->add_local_relative(object, r_sym,
1957 elfcpp::R_X86_64_RELATIVE,
1958 output_section, data_shndx,
1959 reloc.get_r_offset(),
1960 reloc.get_r_addend());
1964 case elfcpp::R_X86_64_32:
1965 case elfcpp::R_X86_64_32S:
1966 case elfcpp::R_X86_64_16:
1967 case elfcpp::R_X86_64_8:
1968 // If building a shared library (or a position-independent
1969 // executable), we need to create a dynamic relocation for this
1970 // location. We can't use an R_X86_64_RELATIVE relocation
1971 // because that is always a 64-bit relocation.
1972 if (parameters->options().output_is_position_independent())
1974 this->check_non_pic(object, r_type, NULL);
1976 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1977 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1978 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1979 rela_dyn->add_local(object, r_sym, r_type, output_section,
1980 data_shndx, reloc.get_r_offset(),
1981 reloc.get_r_addend());
1984 gold_assert(lsym.get_st_value() == 0);
1985 unsigned int shndx = lsym.get_st_shndx();
1987 shndx = object->adjust_sym_shndx(r_sym, shndx,
1990 object->error(_("section symbol %u has bad shndx %u"),
1993 rela_dyn->add_local_section(object, shndx,
1994 r_type, output_section,
1995 data_shndx, reloc.get_r_offset(),
1996 reloc.get_r_addend());
2001 case elfcpp::R_X86_64_PC64:
2002 case elfcpp::R_X86_64_PC32:
2003 case elfcpp::R_X86_64_PC16:
2004 case elfcpp::R_X86_64_PC8:
2007 case elfcpp::R_X86_64_PLT32:
2008 // Since we know this is a local symbol, we can handle this as a
2012 case elfcpp::R_X86_64_GOTPC32:
2013 case elfcpp::R_X86_64_GOTOFF64:
2014 case elfcpp::R_X86_64_GOTPC64:
2015 case elfcpp::R_X86_64_PLTOFF64:
2016 // We need a GOT section.
2017 target->got_section(symtab, layout);
2018 // For PLTOFF64, we'd normally want a PLT section, but since we
2019 // know this is a local symbol, no PLT is needed.
2022 case elfcpp::R_X86_64_GOT64:
2023 case elfcpp::R_X86_64_GOT32:
2024 case elfcpp::R_X86_64_GOTPCREL64:
2025 case elfcpp::R_X86_64_GOTPCREL:
2026 case elfcpp::R_X86_64_GOTPLT64:
2028 // The symbol requires a GOT entry.
2029 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2030 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2032 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2033 // lets function pointers compare correctly with shared
2034 // libraries. Otherwise we would need an IRELATIVE reloc.
2036 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
2037 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2039 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2042 // If we are generating a shared object, we need to add a
2043 // dynamic relocation for this symbol's GOT entry.
2044 if (parameters->options().output_is_position_independent())
2046 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2047 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2048 if (r_type != elfcpp::R_X86_64_GOT32)
2050 unsigned int got_offset =
2051 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2052 rela_dyn->add_local_relative(object, r_sym,
2053 elfcpp::R_X86_64_RELATIVE,
2054 got, got_offset, 0);
2058 this->check_non_pic(object, r_type, NULL);
2060 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2061 rela_dyn->add_local(
2062 object, r_sym, r_type, got,
2063 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2067 // For GOTPLT64, we'd normally want a PLT section, but since
2068 // we know this is a local symbol, no PLT is needed.
2072 case elfcpp::R_X86_64_COPY:
2073 case elfcpp::R_X86_64_GLOB_DAT:
2074 case elfcpp::R_X86_64_JUMP_SLOT:
2075 case elfcpp::R_X86_64_RELATIVE:
2076 case elfcpp::R_X86_64_IRELATIVE:
2077 // These are outstanding tls relocs, which are unexpected when linking
2078 case elfcpp::R_X86_64_TPOFF64:
2079 case elfcpp::R_X86_64_DTPMOD64:
2080 case elfcpp::R_X86_64_TLSDESC:
2081 gold_error(_("%s: unexpected reloc %u in object file"),
2082 object->name().c_str(), r_type);
2085 // These are initial tls relocs, which are expected when linking
2086 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2087 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2088 case elfcpp::R_X86_64_TLSDESC_CALL:
2089 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2090 case elfcpp::R_X86_64_DTPOFF32:
2091 case elfcpp::R_X86_64_DTPOFF64:
2092 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2093 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2095 bool output_is_shared = parameters->options().shared();
2096 const tls::Tls_optimization optimized_type
2097 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2100 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2101 if (optimized_type == tls::TLSOPT_NONE)
2103 // Create a pair of GOT entries for the module index and
2104 // dtv-relative offset.
2105 Output_data_got<64, false>* got
2106 = target->got_section(symtab, layout);
2107 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2108 unsigned int shndx = lsym.get_st_shndx();
2110 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2112 object->error(_("local symbol %u has bad shndx %u"),
2115 got->add_local_pair_with_rela(object, r_sym,
2118 target->rela_dyn_section(layout),
2119 elfcpp::R_X86_64_DTPMOD64, 0);
2121 else if (optimized_type != tls::TLSOPT_TO_LE)
2122 unsupported_reloc_local(object, r_type);
2125 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2126 target->define_tls_base_symbol(symtab, layout);
2127 if (optimized_type == tls::TLSOPT_NONE)
2129 // Create reserved PLT and GOT entries for the resolver.
2130 target->reserve_tlsdesc_entries(symtab, layout);
2132 // Generate a double GOT entry with an
2133 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2134 // is resolved lazily, so the GOT entry needs to be in
2135 // an area in .got.plt, not .got. Call got_section to
2136 // make sure the section has been created.
2137 target->got_section(symtab, layout);
2138 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2139 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2140 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2142 unsigned int got_offset = got->add_constant(0);
2143 got->add_constant(0);
2144 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2146 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2147 // We store the arguments we need in a vector, and
2148 // use the index into the vector as the parameter
2149 // to pass to the target specific routines.
2150 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2151 void* arg = reinterpret_cast<void*>(intarg);
2152 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2153 got, got_offset, 0);
2156 else if (optimized_type != tls::TLSOPT_TO_LE)
2157 unsupported_reloc_local(object, r_type);
2160 case elfcpp::R_X86_64_TLSDESC_CALL:
2163 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2164 if (optimized_type == tls::TLSOPT_NONE)
2166 // Create a GOT entry for the module index.
2167 target->got_mod_index_entry(symtab, layout, object);
2169 else if (optimized_type != tls::TLSOPT_TO_LE)
2170 unsupported_reloc_local(object, r_type);
2173 case elfcpp::R_X86_64_DTPOFF32:
2174 case elfcpp::R_X86_64_DTPOFF64:
2177 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2178 layout->set_has_static_tls();
2179 if (optimized_type == tls::TLSOPT_NONE)
2181 // Create a GOT entry for the tp-relative offset.
2182 Output_data_got<64, false>* got
2183 = target->got_section(symtab, layout);
2184 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2185 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
2186 target->rela_dyn_section(layout),
2187 elfcpp::R_X86_64_TPOFF64);
2189 else if (optimized_type != tls::TLSOPT_TO_LE)
2190 unsupported_reloc_local(object, r_type);
2193 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2194 layout->set_has_static_tls();
2195 if (output_is_shared)
2196 unsupported_reloc_local(object, r_type);
2205 case elfcpp::R_X86_64_SIZE32:
2206 case elfcpp::R_X86_64_SIZE64:
2208 gold_error(_("%s: unsupported reloc %u against local symbol"),
2209 object->name().c_str(), r_type);
2215 // Report an unsupported relocation against a global symbol.
2218 Target_x86_64::Scan::unsupported_reloc_global(
2219 Sized_relobj_file<64, false>* object,
2220 unsigned int r_type,
2223 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2224 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2227 // Returns true if this relocation type could be that of a function pointer.
2229 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2233 case elfcpp::R_X86_64_64:
2234 case elfcpp::R_X86_64_32:
2235 case elfcpp::R_X86_64_32S:
2236 case elfcpp::R_X86_64_16:
2237 case elfcpp::R_X86_64_8:
2238 case elfcpp::R_X86_64_GOT64:
2239 case elfcpp::R_X86_64_GOT32:
2240 case elfcpp::R_X86_64_GOTPCREL64:
2241 case elfcpp::R_X86_64_GOTPCREL:
2242 case elfcpp::R_X86_64_GOTPLT64:
2250 // For safe ICF, scan a relocation for a local symbol to check if it
2251 // corresponds to a function pointer being taken. In that case mark
2252 // the function whose pointer was taken as not foldable.
2255 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2259 Sized_relobj_file<64, false>* ,
2262 const elfcpp::Rela<64, false>& ,
2263 unsigned int r_type,
2264 const elfcpp::Sym<64, false>&)
2266 // When building a shared library, do not fold any local symbols as it is
2267 // not possible to distinguish pointer taken versus a call by looking at
2268 // the relocation types.
2269 return (parameters->options().shared()
2270 || possible_function_pointer_reloc(r_type));
2273 // For safe ICF, scan a relocation for a global symbol to check if it
2274 // corresponds to a function pointer being taken. In that case mark
2275 // the function whose pointer was taken as not foldable.
2278 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2282 Sized_relobj_file<64, false>* ,
2285 const elfcpp::Rela<64, false>& ,
2286 unsigned int r_type,
2289 // When building a shared library, do not fold symbols whose visibility
2290 // is hidden, internal or protected.
2291 return ((parameters->options().shared()
2292 && (gsym->visibility() == elfcpp::STV_INTERNAL
2293 || gsym->visibility() == elfcpp::STV_PROTECTED
2294 || gsym->visibility() == elfcpp::STV_HIDDEN))
2295 || possible_function_pointer_reloc(r_type));
2298 // Scan a relocation for a global symbol.
2301 Target_x86_64::Scan::global(Symbol_table* symtab,
2303 Target_x86_64* target,
2304 Sized_relobj_file<64, false>* object,
2305 unsigned int data_shndx,
2306 Output_section* output_section,
2307 const elfcpp::Rela<64, false>& reloc,
2308 unsigned int r_type,
2311 // A STT_GNU_IFUNC symbol may require a PLT entry.
2312 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2313 && this->reloc_needs_plt_for_ifunc(object, r_type))
2314 target->make_plt_entry(symtab, layout, gsym);
2318 case elfcpp::R_X86_64_NONE:
2319 case elfcpp::R_X86_64_GNU_VTINHERIT:
2320 case elfcpp::R_X86_64_GNU_VTENTRY:
2323 case elfcpp::R_X86_64_64:
2324 case elfcpp::R_X86_64_32:
2325 case elfcpp::R_X86_64_32S:
2326 case elfcpp::R_X86_64_16:
2327 case elfcpp::R_X86_64_8:
2329 // Make a PLT entry if necessary.
2330 if (gsym->needs_plt_entry())
2332 target->make_plt_entry(symtab, layout, gsym);
2333 // Since this is not a PC-relative relocation, we may be
2334 // taking the address of a function. In that case we need to
2335 // set the entry in the dynamic symbol table to the address of
2337 if (gsym->is_from_dynobj() && !parameters->options().shared())
2338 gsym->set_needs_dynsym_value();
2340 // Make a dynamic relocation if necessary.
2341 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2343 if (gsym->may_need_copy_reloc())
2345 target->copy_reloc(symtab, layout, object,
2346 data_shndx, output_section, gsym, reloc);
2348 else if (r_type == elfcpp::R_X86_64_64
2349 && gsym->type() == elfcpp::STT_GNU_IFUNC
2350 && gsym->can_use_relative_reloc(false)
2351 && !gsym->is_from_dynobj()
2352 && !gsym->is_undefined()
2353 && !gsym->is_preemptible())
2355 // Use an IRELATIVE reloc for a locally defined
2356 // STT_GNU_IFUNC symbol. This makes a function
2357 // address in a PIE executable match the address in a
2358 // shared library that it links against.
2359 Reloc_section* rela_dyn =
2360 target->rela_irelative_section(layout);
2361 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2362 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2363 output_section, object,
2365 reloc.get_r_offset(),
2366 reloc.get_r_addend());
2368 else if (r_type == elfcpp::R_X86_64_64
2369 && gsym->can_use_relative_reloc(false))
2371 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2372 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2373 output_section, object,
2375 reloc.get_r_offset(),
2376 reloc.get_r_addend());
2380 this->check_non_pic(object, r_type, gsym);
2381 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2382 rela_dyn->add_global(gsym, r_type, output_section, object,
2383 data_shndx, reloc.get_r_offset(),
2384 reloc.get_r_addend());
2390 case elfcpp::R_X86_64_PC64:
2391 case elfcpp::R_X86_64_PC32:
2392 case elfcpp::R_X86_64_PC16:
2393 case elfcpp::R_X86_64_PC8:
2395 // Make a PLT entry if necessary.
2396 if (gsym->needs_plt_entry())
2397 target->make_plt_entry(symtab, layout, gsym);
2398 // Make a dynamic relocation if necessary.
2399 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2401 if (gsym->may_need_copy_reloc())
2403 target->copy_reloc(symtab, layout, object,
2404 data_shndx, output_section, gsym, reloc);
2408 this->check_non_pic(object, r_type, gsym);
2409 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2410 rela_dyn->add_global(gsym, r_type, output_section, object,
2411 data_shndx, reloc.get_r_offset(),
2412 reloc.get_r_addend());
2418 case elfcpp::R_X86_64_GOT64:
2419 case elfcpp::R_X86_64_GOT32:
2420 case elfcpp::R_X86_64_GOTPCREL64:
2421 case elfcpp::R_X86_64_GOTPCREL:
2422 case elfcpp::R_X86_64_GOTPLT64:
2424 // The symbol requires a GOT entry.
2425 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2426 if (gsym->final_value_is_known())
2428 // For a STT_GNU_IFUNC symbol we want the PLT address.
2429 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2430 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2432 got->add_global(gsym, GOT_TYPE_STANDARD);
2436 // If this symbol is not fully resolved, we need to add a
2437 // dynamic relocation for it.
2438 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2439 if (gsym->is_from_dynobj()
2440 || gsym->is_undefined()
2441 || gsym->is_preemptible()
2442 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2443 && parameters->options().output_is_position_independent()))
2444 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2445 elfcpp::R_X86_64_GLOB_DAT);
2448 // For a STT_GNU_IFUNC symbol we want to write the PLT
2449 // offset into the GOT, so that function pointer
2450 // comparisons work correctly.
2452 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2453 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2456 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2457 // Tell the dynamic linker to use the PLT address
2458 // when resolving relocations.
2459 if (gsym->is_from_dynobj()
2460 && !parameters->options().shared())
2461 gsym->set_needs_dynsym_value();
2465 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2466 rela_dyn->add_global_relative(gsym,
2467 elfcpp::R_X86_64_RELATIVE,
2472 // For GOTPLT64, we also need a PLT entry (but only if the
2473 // symbol is not fully resolved).
2474 if (r_type == elfcpp::R_X86_64_GOTPLT64
2475 && !gsym->final_value_is_known())
2476 target->make_plt_entry(symtab, layout, gsym);
2480 case elfcpp::R_X86_64_PLT32:
2481 // If the symbol is fully resolved, this is just a PC32 reloc.
2482 // Otherwise we need a PLT entry.
2483 if (gsym->final_value_is_known())
2485 // If building a shared library, we can also skip the PLT entry
2486 // if the symbol is defined in the output file and is protected
2488 if (gsym->is_defined()
2489 && !gsym->is_from_dynobj()
2490 && !gsym->is_preemptible())
2492 target->make_plt_entry(symtab, layout, gsym);
2495 case elfcpp::R_X86_64_GOTPC32:
2496 case elfcpp::R_X86_64_GOTOFF64:
2497 case elfcpp::R_X86_64_GOTPC64:
2498 case elfcpp::R_X86_64_PLTOFF64:
2499 // We need a GOT section.
2500 target->got_section(symtab, layout);
2501 // For PLTOFF64, we also need a PLT entry (but only if the
2502 // symbol is not fully resolved).
2503 if (r_type == elfcpp::R_X86_64_PLTOFF64
2504 && !gsym->final_value_is_known())
2505 target->make_plt_entry(symtab, layout, gsym);
2508 case elfcpp::R_X86_64_COPY:
2509 case elfcpp::R_X86_64_GLOB_DAT:
2510 case elfcpp::R_X86_64_JUMP_SLOT:
2511 case elfcpp::R_X86_64_RELATIVE:
2512 case elfcpp::R_X86_64_IRELATIVE:
2513 // These are outstanding tls relocs, which are unexpected when linking
2514 case elfcpp::R_X86_64_TPOFF64:
2515 case elfcpp::R_X86_64_DTPMOD64:
2516 case elfcpp::R_X86_64_TLSDESC:
2517 gold_error(_("%s: unexpected reloc %u in object file"),
2518 object->name().c_str(), r_type);
2521 // These are initial tls relocs, which are expected for global()
2522 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2523 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2524 case elfcpp::R_X86_64_TLSDESC_CALL:
2525 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2526 case elfcpp::R_X86_64_DTPOFF32:
2527 case elfcpp::R_X86_64_DTPOFF64:
2528 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2529 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2531 const bool is_final = gsym->final_value_is_known();
2532 const tls::Tls_optimization optimized_type
2533 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2536 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2537 if (optimized_type == tls::TLSOPT_NONE)
2539 // Create a pair of GOT entries for the module index and
2540 // dtv-relative offset.
2541 Output_data_got<64, false>* got
2542 = target->got_section(symtab, layout);
2543 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2544 target->rela_dyn_section(layout),
2545 elfcpp::R_X86_64_DTPMOD64,
2546 elfcpp::R_X86_64_DTPOFF64);
2548 else if (optimized_type == tls::TLSOPT_TO_IE)
2550 // Create a GOT entry for the tp-relative offset.
2551 Output_data_got<64, false>* got
2552 = target->got_section(symtab, layout);
2553 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2554 target->rela_dyn_section(layout),
2555 elfcpp::R_X86_64_TPOFF64);
2557 else if (optimized_type != tls::TLSOPT_TO_LE)
2558 unsupported_reloc_global(object, r_type, gsym);
2561 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2562 target->define_tls_base_symbol(symtab, layout);
2563 if (optimized_type == tls::TLSOPT_NONE)
2565 // Create reserved PLT and GOT entries for the resolver.
2566 target->reserve_tlsdesc_entries(symtab, layout);
2568 // Create a double GOT entry with an R_X86_64_TLSDESC
2569 // reloc. The R_X86_64_TLSDESC reloc is resolved
2570 // lazily, so the GOT entry needs to be in an area in
2571 // .got.plt, not .got. Call got_section to make sure
2572 // the section has been created.
2573 target->got_section(symtab, layout);
2574 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2575 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2576 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2577 elfcpp::R_X86_64_TLSDESC, 0);
2579 else if (optimized_type == tls::TLSOPT_TO_IE)
2581 // Create a GOT entry for the tp-relative offset.
2582 Output_data_got<64, false>* got
2583 = target->got_section(symtab, layout);
2584 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2585 target->rela_dyn_section(layout),
2586 elfcpp::R_X86_64_TPOFF64);
2588 else if (optimized_type != tls::TLSOPT_TO_LE)
2589 unsupported_reloc_global(object, r_type, gsym);
2592 case elfcpp::R_X86_64_TLSDESC_CALL:
2595 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2596 if (optimized_type == tls::TLSOPT_NONE)
2598 // Create a GOT entry for the module index.
2599 target->got_mod_index_entry(symtab, layout, object);
2601 else if (optimized_type != tls::TLSOPT_TO_LE)
2602 unsupported_reloc_global(object, r_type, gsym);
2605 case elfcpp::R_X86_64_DTPOFF32:
2606 case elfcpp::R_X86_64_DTPOFF64:
2609 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2610 layout->set_has_static_tls();
2611 if (optimized_type == tls::TLSOPT_NONE)
2613 // Create a GOT entry for the tp-relative offset.
2614 Output_data_got<64, false>* got
2615 = target->got_section(symtab, layout);
2616 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2617 target->rela_dyn_section(layout),
2618 elfcpp::R_X86_64_TPOFF64);
2620 else if (optimized_type != tls::TLSOPT_TO_LE)
2621 unsupported_reloc_global(object, r_type, gsym);
2624 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2625 layout->set_has_static_tls();
2626 if (parameters->options().shared())
2627 unsupported_reloc_local(object, r_type);
2636 case elfcpp::R_X86_64_SIZE32:
2637 case elfcpp::R_X86_64_SIZE64:
2639 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2640 object->name().c_str(), r_type,
2641 gsym->demangled_name().c_str());
2647 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2649 Sized_relobj_file<64, false>* object,
2650 unsigned int data_shndx,
2651 unsigned int sh_type,
2652 const unsigned char* prelocs,
2654 Output_section* output_section,
2655 bool needs_special_offset_handling,
2656 size_t local_symbol_count,
2657 const unsigned char* plocal_symbols)
2660 if (sh_type == elfcpp::SHT_REL)
2665 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2666 Target_x86_64::Scan,
2667 Target_x86_64::Relocatable_size_for_reloc>(
2676 needs_special_offset_handling,
2681 // Scan relocations for a section.
2684 Target_x86_64::scan_relocs(Symbol_table* symtab,
2686 Sized_relobj_file<64, false>* object,
2687 unsigned int data_shndx,
2688 unsigned int sh_type,
2689 const unsigned char* prelocs,
2691 Output_section* output_section,
2692 bool needs_special_offset_handling,
2693 size_t local_symbol_count,
2694 const unsigned char* plocal_symbols)
2696 if (sh_type == elfcpp::SHT_REL)
2698 gold_error(_("%s: unsupported REL reloc section"),
2699 object->name().c_str());
2703 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2704 Target_x86_64::Scan>(
2713 needs_special_offset_handling,
2718 // Finalize the sections.
2721 Target_x86_64::do_finalize_sections(
2723 const Input_objects*,
2724 Symbol_table* symtab)
2726 const Reloc_section* rel_plt = (this->plt_ == NULL
2728 : this->plt_->rela_plt());
2729 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2730 this->rela_dyn_, true, false);
2732 // Fill in some more dynamic tags.
2733 Output_data_dynamic* const odyn = layout->dynamic_data();
2736 if (this->plt_ != NULL
2737 && this->plt_->output_section() != NULL
2738 && this->plt_->has_tlsdesc_entry())
2740 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2741 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2742 this->got_->finalize_data_size();
2743 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2744 this->plt_, plt_offset);
2745 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2746 this->got_, got_offset);
2750 // Emit any relocs we saved in an attempt to avoid generating COPY
2752 if (this->copy_relocs_.any_saved_relocs())
2753 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2755 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2756 // the .got.plt section.
2757 Symbol* sym = this->global_offset_table_;
2760 uint64_t data_size = this->got_plt_->current_data_size();
2761 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2764 if (parameters->doing_static_link()
2765 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2767 // If linking statically, make sure that the __rela_iplt symbols
2768 // were defined if necessary, even if we didn't create a PLT.
2769 static const Define_symbol_in_segment syms[] =
2772 "__rela_iplt_start", // name
2773 elfcpp::PT_LOAD, // segment_type
2774 elfcpp::PF_W, // segment_flags_set
2775 elfcpp::PF(0), // segment_flags_clear
2778 elfcpp::STT_NOTYPE, // type
2779 elfcpp::STB_GLOBAL, // binding
2780 elfcpp::STV_HIDDEN, // visibility
2782 Symbol::SEGMENT_START, // offset_from_base
2786 "__rela_iplt_end", // name
2787 elfcpp::PT_LOAD, // segment_type
2788 elfcpp::PF_W, // segment_flags_set
2789 elfcpp::PF(0), // segment_flags_clear
2792 elfcpp::STT_NOTYPE, // type
2793 elfcpp::STB_GLOBAL, // binding
2794 elfcpp::STV_HIDDEN, // visibility
2796 Symbol::SEGMENT_START, // offset_from_base
2801 symtab->define_symbols(layout, 2, syms,
2802 layout->script_options()->saw_sections_clause());
2806 // Perform a relocation.
2809 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2810 Target_x86_64* target,
2813 const elfcpp::Rela<64, false>& rela,
2814 unsigned int r_type,
2815 const Sized_symbol<64>* gsym,
2816 const Symbol_value<64>* psymval,
2817 unsigned char* view,
2818 elfcpp::Elf_types<64>::Elf_Addr address,
2819 section_size_type view_size)
2821 if (this->skip_call_tls_get_addr_)
2823 if ((r_type != elfcpp::R_X86_64_PLT32
2824 && r_type != elfcpp::R_X86_64_PC32)
2826 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2828 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2829 _("missing expected TLS relocation"));
2833 this->skip_call_tls_get_addr_ = false;
2838 const Sized_relobj_file<64, false>* object = relinfo->object;
2840 // Pick the value to use for symbols defined in the PLT.
2841 Symbol_value<64> symval;
2843 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2845 symval.set_output_value(target->plt_address_for_global(gsym)
2846 + gsym->plt_offset());
2849 else if (gsym == NULL && psymval->is_ifunc_symbol())
2851 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2852 if (object->local_has_plt_offset(r_sym))
2854 symval.set_output_value(target->plt_address_for_local(object, r_sym)
2855 + object->local_plt_offset(r_sym));
2860 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2862 // Get the GOT offset if needed.
2863 // The GOT pointer points to the end of the GOT section.
2864 // We need to subtract the size of the GOT section to get
2865 // the actual offset to use in the relocation.
2866 bool have_got_offset = false;
2867 unsigned int got_offset = 0;
2870 case elfcpp::R_X86_64_GOT32:
2871 case elfcpp::R_X86_64_GOT64:
2872 case elfcpp::R_X86_64_GOTPLT64:
2873 case elfcpp::R_X86_64_GOTPCREL:
2874 case elfcpp::R_X86_64_GOTPCREL64:
2877 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2878 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2882 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2883 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2884 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2885 - target->got_size());
2887 have_got_offset = true;
2896 case elfcpp::R_X86_64_NONE:
2897 case elfcpp::R_X86_64_GNU_VTINHERIT:
2898 case elfcpp::R_X86_64_GNU_VTENTRY:
2901 case elfcpp::R_X86_64_64:
2902 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2905 case elfcpp::R_X86_64_PC64:
2906 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2910 case elfcpp::R_X86_64_32:
2911 // FIXME: we need to verify that value + addend fits into 32 bits:
2912 // uint64_t x = value + addend;
2913 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2914 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2915 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2918 case elfcpp::R_X86_64_32S:
2919 // FIXME: we need to verify that value + addend fits into 32 bits:
2920 // int64_t x = value + addend; // note this quantity is signed!
2921 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2922 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2925 case elfcpp::R_X86_64_PC32:
2926 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2930 case elfcpp::R_X86_64_16:
2931 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2934 case elfcpp::R_X86_64_PC16:
2935 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2939 case elfcpp::R_X86_64_8:
2940 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2943 case elfcpp::R_X86_64_PC8:
2944 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2948 case elfcpp::R_X86_64_PLT32:
2949 gold_assert(gsym == NULL
2950 || gsym->has_plt_offset()
2951 || gsym->final_value_is_known()
2952 || (gsym->is_defined()
2953 && !gsym->is_from_dynobj()
2954 && !gsym->is_preemptible()));
2955 // Note: while this code looks the same as for R_X86_64_PC32, it
2956 // behaves differently because psymval was set to point to
2957 // the PLT entry, rather than the symbol, in Scan::global().
2958 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2962 case elfcpp::R_X86_64_PLTOFF64:
2965 gold_assert(gsym->has_plt_offset()
2966 || gsym->final_value_is_known());
2967 elfcpp::Elf_types<64>::Elf_Addr got_address;
2968 got_address = target->got_section(NULL, NULL)->address();
2969 Relocate_functions<64, false>::rela64(view, object, psymval,
2970 addend - got_address);
2973 case elfcpp::R_X86_64_GOT32:
2974 gold_assert(have_got_offset);
2975 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2978 case elfcpp::R_X86_64_GOTPC32:
2981 elfcpp::Elf_types<64>::Elf_Addr value;
2982 value = target->got_plt_section()->address();
2983 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2987 case elfcpp::R_X86_64_GOT64:
2988 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2989 // Since we always add a PLT entry, this is equivalent.
2990 case elfcpp::R_X86_64_GOTPLT64:
2991 gold_assert(have_got_offset);
2992 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2995 case elfcpp::R_X86_64_GOTPC64:
2998 elfcpp::Elf_types<64>::Elf_Addr value;
2999 value = target->got_plt_section()->address();
3000 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
3004 case elfcpp::R_X86_64_GOTOFF64:
3006 elfcpp::Elf_types<64>::Elf_Addr value;
3007 value = (psymval->value(object, 0)
3008 - target->got_plt_section()->address());
3009 Relocate_functions<64, false>::rela64(view, value, addend);
3013 case elfcpp::R_X86_64_GOTPCREL:
3015 gold_assert(have_got_offset);
3016 elfcpp::Elf_types<64>::Elf_Addr value;
3017 value = target->got_plt_section()->address() + got_offset;
3018 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3022 case elfcpp::R_X86_64_GOTPCREL64:
3024 gold_assert(have_got_offset);
3025 elfcpp::Elf_types<64>::Elf_Addr value;
3026 value = target->got_plt_section()->address() + got_offset;
3027 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
3031 case elfcpp::R_X86_64_COPY:
3032 case elfcpp::R_X86_64_GLOB_DAT:
3033 case elfcpp::R_X86_64_JUMP_SLOT:
3034 case elfcpp::R_X86_64_RELATIVE:
3035 case elfcpp::R_X86_64_IRELATIVE:
3036 // These are outstanding tls relocs, which are unexpected when linking
3037 case elfcpp::R_X86_64_TPOFF64:
3038 case elfcpp::R_X86_64_DTPMOD64:
3039 case elfcpp::R_X86_64_TLSDESC:
3040 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3041 _("unexpected reloc %u in object file"),
3045 // These are initial tls relocs, which are expected when linking
3046 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3047 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3048 case elfcpp::R_X86_64_TLSDESC_CALL:
3049 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3050 case elfcpp::R_X86_64_DTPOFF32:
3051 case elfcpp::R_X86_64_DTPOFF64:
3052 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3053 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3054 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3055 view, address, view_size);
3058 case elfcpp::R_X86_64_SIZE32:
3059 case elfcpp::R_X86_64_SIZE64:
3061 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3062 _("unsupported reloc %u"),
3070 // Perform a TLS relocation.
3073 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
3074 Target_x86_64* target,
3076 const elfcpp::Rela<64, false>& rela,
3077 unsigned int r_type,
3078 const Sized_symbol<64>* gsym,
3079 const Symbol_value<64>* psymval,
3080 unsigned char* view,
3081 elfcpp::Elf_types<64>::Elf_Addr address,
3082 section_size_type view_size)
3084 Output_segment* tls_segment = relinfo->layout->tls_segment();
3086 const Sized_relobj_file<64, false>* object = relinfo->object;
3087 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3088 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
3089 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3091 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
3093 const bool is_final = (gsym == NULL
3094 ? !parameters->options().shared()
3095 : gsym->final_value_is_known());
3096 tls::Tls_optimization optimized_type
3097 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
3100 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3101 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3103 // If this code sequence is used in a non-executable section,
3104 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3105 // on the assumption that it's being used by itself in a debug
3106 // section. Therefore, in the unlikely event that the code
3107 // sequence appears in a non-executable section, we simply
3108 // leave it unoptimized.
3109 optimized_type = tls::TLSOPT_NONE;
3111 if (optimized_type == tls::TLSOPT_TO_LE)
3113 gold_assert(tls_segment != NULL);
3114 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3115 rela, r_type, value, view,
3121 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3122 ? GOT_TYPE_TLS_OFFSET
3123 : GOT_TYPE_TLS_PAIR);
3124 unsigned int got_offset;
3127 gold_assert(gsym->has_got_offset(got_type));
3128 got_offset = gsym->got_offset(got_type) - target->got_size();
3132 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3133 gold_assert(object->local_has_got_offset(r_sym, got_type));
3134 got_offset = (object->local_got_offset(r_sym, got_type)
3135 - target->got_size());
3137 if (optimized_type == tls::TLSOPT_TO_IE)
3139 gold_assert(tls_segment != NULL);
3140 value = target->got_plt_section()->address() + got_offset;
3141 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3142 value, view, address, view_size);
3145 else if (optimized_type == tls::TLSOPT_NONE)
3147 // Relocate the field with the offset of the pair of GOT
3149 value = target->got_plt_section()->address() + got_offset;
3150 Relocate_functions<64, false>::pcrela32(view, value, addend,
3155 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3156 _("unsupported reloc %u"), r_type);
3159 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3160 case elfcpp::R_X86_64_TLSDESC_CALL:
3161 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3163 // See above comment for R_X86_64_TLSGD.
3164 optimized_type = tls::TLSOPT_NONE;
3166 if (optimized_type == tls::TLSOPT_TO_LE)
3168 gold_assert(tls_segment != NULL);
3169 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3170 rela, r_type, value, view,
3176 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3177 ? GOT_TYPE_TLS_OFFSET
3178 : GOT_TYPE_TLS_DESC);
3179 unsigned int got_offset = 0;
3180 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3181 && optimized_type == tls::TLSOPT_NONE)
3183 // We created GOT entries in the .got.tlsdesc portion of
3184 // the .got.plt section, but the offset stored in the
3185 // symbol is the offset within .got.tlsdesc.
3186 got_offset = (target->got_size()
3187 + target->got_plt_section()->data_size());
3191 gold_assert(gsym->has_got_offset(got_type));
3192 got_offset += gsym->got_offset(got_type) - target->got_size();
3196 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3197 gold_assert(object->local_has_got_offset(r_sym, got_type));
3198 got_offset += (object->local_got_offset(r_sym, got_type)
3199 - target->got_size());
3201 if (optimized_type == tls::TLSOPT_TO_IE)
3203 gold_assert(tls_segment != NULL);
3204 value = target->got_plt_section()->address() + got_offset;
3205 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3206 rela, r_type, value, view, address,
3210 else if (optimized_type == tls::TLSOPT_NONE)
3212 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3214 // Relocate the field with the offset of the pair of GOT
3216 value = target->got_plt_section()->address() + got_offset;
3217 Relocate_functions<64, false>::pcrela32(view, value, addend,
3223 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3224 _("unsupported reloc %u"), r_type);
3227 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3228 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3230 // See above comment for R_X86_64_TLSGD.
3231 optimized_type = tls::TLSOPT_NONE;
3233 if (optimized_type == tls::TLSOPT_TO_LE)
3235 gold_assert(tls_segment != NULL);
3236 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3237 value, view, view_size);
3240 else if (optimized_type == tls::TLSOPT_NONE)
3242 // Relocate the field with the offset of the GOT entry for
3243 // the module index.
3244 unsigned int got_offset;
3245 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3246 - target->got_size());
3247 value = target->got_plt_section()->address() + got_offset;
3248 Relocate_functions<64, false>::pcrela32(view, value, addend,
3252 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3253 _("unsupported reloc %u"), r_type);
3256 case elfcpp::R_X86_64_DTPOFF32:
3257 // This relocation type is used in debugging information.
3258 // In that case we need to not optimize the value. If the
3259 // section is not executable, then we assume we should not
3260 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3261 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3263 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3265 gold_assert(tls_segment != NULL);
3266 value -= tls_segment->memsz();
3268 Relocate_functions<64, false>::rela32(view, value, addend);
3271 case elfcpp::R_X86_64_DTPOFF64:
3272 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3273 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3275 gold_assert(tls_segment != NULL);
3276 value -= tls_segment->memsz();
3278 Relocate_functions<64, false>::rela64(view, value, addend);
3281 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3282 if (optimized_type == tls::TLSOPT_TO_LE)
3284 gold_assert(tls_segment != NULL);
3285 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3286 rela, r_type, value, view,
3290 else if (optimized_type == tls::TLSOPT_NONE)
3292 // Relocate the field with the offset of the GOT entry for
3293 // the tp-relative offset of the symbol.
3294 unsigned int got_offset;
3297 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3298 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3299 - target->got_size());
3303 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3304 gold_assert(object->local_has_got_offset(r_sym,
3305 GOT_TYPE_TLS_OFFSET));
3306 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3307 - target->got_size());
3309 value = target->got_plt_section()->address() + got_offset;
3310 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3313 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3314 _("unsupported reloc type %u"),
3318 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3319 value -= tls_segment->memsz();
3320 Relocate_functions<64, false>::rela32(view, value, addend);
3325 // Do a relocation in which we convert a TLS General-Dynamic to an
3329 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3332 const elfcpp::Rela<64, false>& rela,
3334 elfcpp::Elf_types<64>::Elf_Addr value,
3335 unsigned char* view,
3336 elfcpp::Elf_types<64>::Elf_Addr address,
3337 section_size_type view_size)
3339 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3340 // .word 0x6666; rex64; call __tls_get_addr
3341 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3343 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3344 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3346 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3347 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3348 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3349 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3351 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3353 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3354 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3356 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3358 this->skip_call_tls_get_addr_ = true;
3361 // Do a relocation in which we convert a TLS General-Dynamic to a
3365 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3367 Output_segment* tls_segment,
3368 const elfcpp::Rela<64, false>& rela,
3370 elfcpp::Elf_types<64>::Elf_Addr value,
3371 unsigned char* view,
3372 section_size_type view_size)
3374 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3375 // .word 0x6666; rex64; call __tls_get_addr
3376 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3378 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3379 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3381 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3382 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3383 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3384 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3386 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3388 value -= tls_segment->memsz();
3389 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3391 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3393 this->skip_call_tls_get_addr_ = true;
3396 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3399 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3400 const Relocate_info<64, false>* relinfo,
3403 const elfcpp::Rela<64, false>& rela,
3404 unsigned int r_type,
3405 elfcpp::Elf_types<64>::Elf_Addr value,
3406 unsigned char* view,
3407 elfcpp::Elf_types<64>::Elf_Addr address,
3408 section_size_type view_size)
3410 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3412 // leaq foo@tlsdesc(%rip), %rax
3413 // ==> movq foo@gottpoff(%rip), %rax
3414 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3415 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3416 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3417 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3419 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3420 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3424 // call *foo@tlscall(%rax)
3426 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3427 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3428 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3429 view[0] == 0xff && view[1] == 0x10);
3435 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3438 Target_x86_64::Relocate::tls_desc_gd_to_le(
3439 const Relocate_info<64, false>* relinfo,
3441 Output_segment* tls_segment,
3442 const elfcpp::Rela<64, false>& rela,
3443 unsigned int r_type,
3444 elfcpp::Elf_types<64>::Elf_Addr value,
3445 unsigned char* view,
3446 section_size_type view_size)
3448 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3450 // leaq foo@tlsdesc(%rip), %rax
3451 // ==> movq foo@tpoff, %rax
3452 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3453 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3454 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3455 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3458 value -= tls_segment->memsz();
3459 Relocate_functions<64, false>::rela32(view, value, 0);
3463 // call *foo@tlscall(%rax)
3465 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3466 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3467 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3468 view[0] == 0xff && view[1] == 0x10);
3475 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3478 const elfcpp::Rela<64, false>& rela,
3480 elfcpp::Elf_types<64>::Elf_Addr,
3481 unsigned char* view,
3482 section_size_type view_size)
3484 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3485 // ... leq foo@dtpoff(%rax),%reg
3486 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3488 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3489 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3491 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3492 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3494 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3496 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3498 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3500 this->skip_call_tls_get_addr_ = true;
3503 // Do a relocation in which we convert a TLS Initial-Exec to a
3507 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3509 Output_segment* tls_segment,
3510 const elfcpp::Rela<64, false>& rela,
3512 elfcpp::Elf_types<64>::Elf_Addr value,
3513 unsigned char* view,
3514 section_size_type view_size)
3516 // We need to examine the opcodes to figure out which instruction we
3519 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3520 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3522 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3523 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3525 unsigned char op1 = view[-3];
3526 unsigned char op2 = view[-2];
3527 unsigned char op3 = view[-1];
3528 unsigned char reg = op3 >> 3;
3536 view[-1] = 0xc0 | reg;
3540 // Special handling for %rsp.
3544 view[-1] = 0xc0 | reg;
3552 view[-1] = 0x80 | reg | (reg << 3);
3555 value -= tls_segment->memsz();
3556 Relocate_functions<64, false>::rela32(view, value, 0);
3559 // Relocate section data.
3562 Target_x86_64::relocate_section(
3563 const Relocate_info<64, false>* relinfo,
3564 unsigned int sh_type,
3565 const unsigned char* prelocs,
3567 Output_section* output_section,
3568 bool needs_special_offset_handling,
3569 unsigned char* view,
3570 elfcpp::Elf_types<64>::Elf_Addr address,
3571 section_size_type view_size,
3572 const Reloc_symbol_changes* reloc_symbol_changes)
3574 gold_assert(sh_type == elfcpp::SHT_RELA);
3576 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3577 Target_x86_64::Relocate>(
3583 needs_special_offset_handling,
3587 reloc_symbol_changes);
3590 // Apply an incremental relocation. Incremental relocations always refer
3591 // to global symbols.
3594 Target_x86_64::apply_relocation(
3595 const Relocate_info<64, false>* relinfo,
3596 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3597 unsigned int r_type,
3598 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3600 unsigned char* view,
3601 elfcpp::Elf_types<64>::Elf_Addr address,
3602 section_size_type view_size)
3604 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3616 // Return the size of a relocation while scanning during a relocatable
3620 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3621 unsigned int r_type,
3626 case elfcpp::R_X86_64_NONE:
3627 case elfcpp::R_X86_64_GNU_VTINHERIT:
3628 case elfcpp::R_X86_64_GNU_VTENTRY:
3629 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3630 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3631 case elfcpp::R_X86_64_TLSDESC_CALL:
3632 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3633 case elfcpp::R_X86_64_DTPOFF32:
3634 case elfcpp::R_X86_64_DTPOFF64:
3635 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3636 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3639 case elfcpp::R_X86_64_64:
3640 case elfcpp::R_X86_64_PC64:
3641 case elfcpp::R_X86_64_GOTOFF64:
3642 case elfcpp::R_X86_64_GOTPC64:
3643 case elfcpp::R_X86_64_PLTOFF64:
3644 case elfcpp::R_X86_64_GOT64:
3645 case elfcpp::R_X86_64_GOTPCREL64:
3646 case elfcpp::R_X86_64_GOTPCREL:
3647 case elfcpp::R_X86_64_GOTPLT64:
3650 case elfcpp::R_X86_64_32:
3651 case elfcpp::R_X86_64_32S:
3652 case elfcpp::R_X86_64_PC32:
3653 case elfcpp::R_X86_64_PLT32:
3654 case elfcpp::R_X86_64_GOTPC32:
3655 case elfcpp::R_X86_64_GOT32:
3658 case elfcpp::R_X86_64_16:
3659 case elfcpp::R_X86_64_PC16:
3662 case elfcpp::R_X86_64_8:
3663 case elfcpp::R_X86_64_PC8:
3666 case elfcpp::R_X86_64_COPY:
3667 case elfcpp::R_X86_64_GLOB_DAT:
3668 case elfcpp::R_X86_64_JUMP_SLOT:
3669 case elfcpp::R_X86_64_RELATIVE:
3670 case elfcpp::R_X86_64_IRELATIVE:
3671 // These are outstanding tls relocs, which are unexpected when linking
3672 case elfcpp::R_X86_64_TPOFF64:
3673 case elfcpp::R_X86_64_DTPMOD64:
3674 case elfcpp::R_X86_64_TLSDESC:
3675 object->error(_("unexpected reloc %u in object file"), r_type);
3678 case elfcpp::R_X86_64_SIZE32:
3679 case elfcpp::R_X86_64_SIZE64:
3681 object->error(_("unsupported reloc %u against local symbol"), r_type);
3686 // Scan the relocs during a relocatable link.
3689 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3691 Sized_relobj_file<64, false>* object,
3692 unsigned int data_shndx,
3693 unsigned int sh_type,
3694 const unsigned char* prelocs,
3696 Output_section* output_section,
3697 bool needs_special_offset_handling,
3698 size_t local_symbol_count,
3699 const unsigned char* plocal_symbols,
3700 Relocatable_relocs* rr)
3702 gold_assert(sh_type == elfcpp::SHT_RELA);
3704 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3705 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3707 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3708 Scan_relocatable_relocs>(
3716 needs_special_offset_handling,
3722 // Relocate a section during a relocatable link.
3725 Target_x86_64::relocate_for_relocatable(
3726 const Relocate_info<64, false>* relinfo,
3727 unsigned int sh_type,
3728 const unsigned char* prelocs,
3730 Output_section* output_section,
3731 off_t offset_in_output_section,
3732 const Relocatable_relocs* rr,
3733 unsigned char* view,
3734 elfcpp::Elf_types<64>::Elf_Addr view_address,
3735 section_size_type view_size,
3736 unsigned char* reloc_view,
3737 section_size_type reloc_view_size)
3739 gold_assert(sh_type == elfcpp::SHT_RELA);
3741 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3746 offset_in_output_section,
3755 // Return the value to use for a dynamic which requires special
3756 // treatment. This is how we support equality comparisons of function
3757 // pointers across shared library boundaries, as described in the
3758 // processor specific ABI supplement.
3761 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3763 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3764 return this->plt_address_for_global(gsym) + gsym->plt_offset();
3767 // Return a string used to fill a code section with nops to take up
3768 // the specified length.
3771 Target_x86_64::do_code_fill(section_size_type length) const
3775 // Build a jmpq instruction to skip over the bytes.
3776 unsigned char jmp[5];
3778 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3779 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3780 + std::string(length - 5, '\0'));
3783 // Nop sequences of various lengths.
3784 const char nop1[1] = { 0x90 }; // nop
3785 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3786 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3787 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3788 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3790 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3792 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3794 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3795 0x00, 0x00, 0x00, 0x00 };
3796 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3797 0x00, 0x00, 0x00, 0x00,
3799 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3800 0x84, 0x00, 0x00, 0x00,
3802 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3803 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3805 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3806 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3807 0x00, 0x00, 0x00, 0x00 };
3808 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3809 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3810 0x00, 0x00, 0x00, 0x00,
3812 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3813 0x66, 0x2e, 0x0f, 0x1f, // data16
3814 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3816 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3817 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3818 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3821 const char* nops[16] = {
3823 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3824 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3827 return std::string(nops[length], length);
3830 // Return the addend to use for a target specific relocation. The
3831 // only target specific relocation is R_X86_64_TLSDESC for a local
3832 // symbol. We want to set the addend is the offset of the local
3833 // symbol in the TLS segment.
3836 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3839 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3840 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3841 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3842 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3843 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3844 gold_assert(psymval->is_tls_symbol());
3845 // The value of a TLS symbol is the offset in the TLS segment.
3846 return psymval->value(ti.object, 0);
3849 // Return the value to use for the base of a DW_EH_PE_datarel offset
3850 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3851 // assembler can not write out the difference between two labels in
3852 // different sections, so instead of using a pc-relative value they
3853 // use an offset from the GOT.
3856 Target_x86_64::do_ehframe_datarel_base() const
3858 gold_assert(this->global_offset_table_ != NULL);
3859 Symbol* sym = this->global_offset_table_;
3860 Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym);
3861 return ssym->value();
3864 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3865 // compiled with -fsplit-stack. The function calls non-split-stack
3866 // code. We have to change the function so that it always ensures
3867 // that it has enough stack space to run some random function.
3870 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3871 section_offset_type fnoffset,
3872 section_size_type fnsize,
3873 unsigned char* view,
3874 section_size_type view_size,
3876 std::string* to) const
3878 // The function starts with a comparison of the stack pointer and a
3879 // field in the TCB. This is followed by a jump.
3882 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3885 // We will call __morestack if the carry flag is set after this
3886 // comparison. We turn the comparison into an stc instruction
3888 view[fnoffset] = '\xf9';
3889 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3891 // lea NN(%rsp),%r10
3892 // lea NN(%rsp),%r11
3893 else if ((this->match_view(view, view_size, fnoffset,
3894 "\x4c\x8d\x94\x24", 4)
3895 || this->match_view(view, view_size, fnoffset,
3896 "\x4c\x8d\x9c\x24", 4))
3899 // This is loading an offset from the stack pointer for a
3900 // comparison. The offset is negative, so we decrease the
3901 // offset by the amount of space we need for the stack. This
3902 // means we will avoid calling __morestack if there happens to
3903 // be plenty of space on the stack already.
3904 unsigned char* pval = view + fnoffset + 4;
3905 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3906 val -= parameters->options().split_stack_adjust_size();
3907 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3911 if (!object->has_no_split_stack())
3912 object->error(_("failed to match split-stack sequence at "
3913 "section %u offset %0zx"),
3914 shndx, static_cast<size_t>(fnoffset));
3918 // We have to change the function so that it calls
3919 // __morestack_non_split instead of __morestack. The former will
3920 // allocate additional stack space.
3921 *from = "__morestack";
3922 *to = "__morestack_non_split";
3925 // The selector for x86_64 object files.
3927 class Target_selector_x86_64 : public Target_selector_freebsd
3930 Target_selector_x86_64()
3931 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3932 "elf64-x86-64-freebsd", "elf_x86_64")
3936 do_instantiate_target()
3937 { return new Target_x86_64(); }
3941 Target_selector_x86_64 target_selector_x86_64;
3943 } // End anonymous namespace.