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 // When using -z now, we can treat .got.plt as a relro section.
838 // Without -z now, it is modified after program startup by lazy
840 bool is_got_plt_relro = parameters->options().now();
841 Output_section_order got_order = (is_got_plt_relro
844 Output_section_order got_plt_order = (is_got_plt_relro
846 : ORDER_NON_RELRO_FIRST);
848 this->got_ = new Output_data_got<64, false>();
850 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
852 | elfcpp::SHF_WRITE),
853 this->got_, got_order, true);
855 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
856 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
858 | elfcpp::SHF_WRITE),
859 this->got_plt_, got_plt_order,
862 // The first three entries are reserved.
863 this->got_plt_->set_current_data_size(3 * 8);
865 if (!is_got_plt_relro)
867 // Those bytes can go into the relro segment.
868 layout->increase_relro(3 * 8);
871 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
872 this->global_offset_table_ =
873 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
874 Symbol_table::PREDEFINED,
876 0, 0, elfcpp::STT_OBJECT,
878 elfcpp::STV_HIDDEN, 0,
881 // If there are any IRELATIVE relocations, they get GOT entries
882 // in .got.plt after the jump slot entries.
883 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
884 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
886 | elfcpp::SHF_WRITE),
887 this->got_irelative_,
888 got_plt_order, is_got_plt_relro);
890 // If there are any TLSDESC relocations, they get GOT entries in
891 // .got.plt after the jump slot and IRELATIVE entries.
892 this->got_tlsdesc_ = new Output_data_got<64, false>();
893 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
895 | elfcpp::SHF_WRITE),
897 got_plt_order, is_got_plt_relro);
903 // Get the dynamic reloc section, creating it if necessary.
905 Target_x86_64::Reloc_section*
906 Target_x86_64::rela_dyn_section(Layout* layout)
908 if (this->rela_dyn_ == NULL)
910 gold_assert(layout != NULL);
911 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
912 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
913 elfcpp::SHF_ALLOC, this->rela_dyn_,
914 ORDER_DYNAMIC_RELOCS, false);
916 return this->rela_dyn_;
919 // Get the section to use for IRELATIVE relocs, creating it if
920 // necessary. These go in .rela.dyn, but only after all other dynamic
921 // relocations. They need to follow the other dynamic relocations so
922 // that they can refer to global variables initialized by those
925 Target_x86_64::Reloc_section*
926 Target_x86_64::rela_irelative_section(Layout* layout)
928 if (this->rela_irelative_ == NULL)
930 // Make sure we have already created the dynamic reloc section.
931 this->rela_dyn_section(layout);
932 this->rela_irelative_ = new Reloc_section(false);
933 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
934 elfcpp::SHF_ALLOC, this->rela_irelative_,
935 ORDER_DYNAMIC_RELOCS, false);
936 gold_assert(this->rela_dyn_->output_section()
937 == this->rela_irelative_->output_section());
939 return this->rela_irelative_;
942 // Initialize the PLT section.
945 Output_data_plt_x86_64::init(Layout* layout)
947 this->rel_ = new Reloc_section(false);
948 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
949 elfcpp::SHF_ALLOC, this->rel_,
950 ORDER_DYNAMIC_PLT_RELOCS, false);
952 // Add unwind information if requested.
953 if (parameters->options().ld_generated_unwind_info())
954 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
955 plt_eh_frame_fde, plt_eh_frame_fde_size);
959 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
961 os->set_entsize(plt_entry_size);
964 // Add an entry to the PLT.
967 Output_data_plt_x86_64::add_entry(Symbol_table* symtab, Layout* layout,
970 gold_assert(!gsym->has_plt_offset());
972 unsigned int plt_index;
974 section_offset_type got_offset;
976 unsigned int* pcount;
978 unsigned int reserved;
979 Output_data_space* got;
980 if (gsym->type() == elfcpp::STT_GNU_IFUNC
981 && gsym->can_use_relative_reloc(false))
983 pcount = &this->irelative_count_;
986 got = this->got_irelative_;
990 pcount = &this->count_;
993 got = this->got_plt_;
996 if (!this->is_data_size_valid())
998 // Note that when setting the PLT offset for a non-IRELATIVE
999 // entry we skip the initial reserved PLT entry.
1000 plt_index = *pcount + offset;
1001 plt_offset = plt_index * plt_entry_size;
1005 got_offset = (plt_index - offset + reserved) * 8;
1006 gold_assert(got_offset == got->current_data_size());
1008 // Every PLT entry needs a GOT entry which points back to the PLT
1009 // entry (this will be changed by the dynamic linker, normally
1010 // lazily when the function is called).
1011 got->set_current_data_size(got_offset + 8);
1015 // FIXME: This is probably not correct for IRELATIVE relocs.
1017 // For incremental updates, find an available slot.
1018 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
1019 if (plt_offset == -1)
1020 gold_fallback(_("out of patch space (PLT);"
1021 " relink with --incremental-full"));
1023 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1024 // can be calculated from the PLT index, adjusting for the three
1025 // reserved entries at the beginning of the GOT.
1026 plt_index = plt_offset / plt_entry_size - 1;
1027 got_offset = (plt_index - offset + reserved) * 8;
1030 gsym->set_plt_offset(plt_offset);
1032 // Every PLT entry needs a reloc.
1033 this->add_relocation(symtab, layout, gsym, got_offset);
1035 // Note that we don't need to save the symbol. The contents of the
1036 // PLT are independent of which symbols are used. The symbols only
1037 // appear in the relocations.
1040 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1044 Output_data_plt_x86_64::add_local_ifunc_entry(
1045 Symbol_table* symtab,
1047 Sized_relobj_file<64, false>* relobj,
1048 unsigned int local_sym_index)
1050 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
1051 ++this->irelative_count_;
1053 section_offset_type got_offset = this->got_irelative_->current_data_size();
1055 // Every PLT entry needs a GOT entry which points back to the PLT
1057 this->got_irelative_->set_current_data_size(got_offset + 8);
1059 // Every PLT entry needs a reloc.
1060 Reloc_section* rela = this->rela_irelative(symtab, layout);
1061 rela->add_symbolless_local_addend(relobj, local_sym_index,
1062 elfcpp::R_X86_64_IRELATIVE,
1063 this->got_irelative_, got_offset, 0);
1068 // Add the relocation for a PLT entry.
1071 Output_data_plt_x86_64::add_relocation(Symbol_table* symtab, Layout* layout,
1072 Symbol* gsym, unsigned int got_offset)
1074 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1075 && gsym->can_use_relative_reloc(false))
1077 Reloc_section* rela = this->rela_irelative(symtab, layout);
1078 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1079 this->got_irelative_, got_offset, 0);
1083 gsym->set_needs_dynsym_entry();
1084 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1089 // Return where the TLSDESC relocations should go, creating it if
1090 // necessary. These follow the JUMP_SLOT relocations.
1092 Output_data_plt_x86_64::Reloc_section*
1093 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
1095 if (this->tlsdesc_rel_ == NULL)
1097 this->tlsdesc_rel_ = new Reloc_section(false);
1098 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1099 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1100 ORDER_DYNAMIC_PLT_RELOCS, false);
1101 gold_assert(this->tlsdesc_rel_->output_section()
1102 == this->rel_->output_section());
1104 return this->tlsdesc_rel_;
1107 // Return where the IRELATIVE relocations should go in the PLT. These
1108 // follow the JUMP_SLOT and the TLSDESC relocations.
1110 Output_data_plt_x86_64::Reloc_section*
1111 Output_data_plt_x86_64::rela_irelative(Symbol_table* symtab, Layout* layout)
1113 if (this->irelative_rel_ == NULL)
1115 // Make sure we have a place for the TLSDESC relocations, in
1116 // case we see any later on.
1117 this->rela_tlsdesc(layout);
1118 this->irelative_rel_ = new Reloc_section(false);
1119 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1120 elfcpp::SHF_ALLOC, this->irelative_rel_,
1121 ORDER_DYNAMIC_PLT_RELOCS, false);
1122 gold_assert(this->irelative_rel_->output_section()
1123 == this->rel_->output_section());
1125 if (parameters->doing_static_link())
1127 // A statically linked executable will only have a .rela.plt
1128 // section to hold R_X86_64_IRELATIVE relocs for
1129 // STT_GNU_IFUNC symbols. The library will use these
1130 // symbols to locate the IRELATIVE relocs at program startup
1132 symtab->define_in_output_data("__rela_iplt_start", NULL,
1133 Symbol_table::PREDEFINED,
1134 this->irelative_rel_, 0, 0,
1135 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1136 elfcpp::STV_HIDDEN, 0, false, true);
1137 symtab->define_in_output_data("__rela_iplt_end", NULL,
1138 Symbol_table::PREDEFINED,
1139 this->irelative_rel_, 0, 0,
1140 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1141 elfcpp::STV_HIDDEN, 0, true, true);
1144 return this->irelative_rel_;
1147 // Return the PLT address to use for a global symbol.
1150 Output_data_plt_x86_64::address_for_global(const Symbol* gsym)
1152 uint64_t offset = 0;
1153 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1154 && gsym->can_use_relative_reloc(false))
1155 offset = (this->count_ + 1) * plt_entry_size;
1156 return this->address() + offset;
1159 // Return the PLT address to use for a local symbol. These are always
1160 // IRELATIVE relocs.
1163 Output_data_plt_x86_64::address_for_local(const Relobj*, unsigned int)
1165 return this->address() + (this->count_ + 1) * plt_entry_size;
1168 // Set the final size.
1170 Output_data_plt_x86_64::set_final_data_size()
1172 unsigned int count = this->count_ + this->irelative_count_;
1173 if (this->has_tlsdesc_entry())
1175 this->set_data_size((count + 1) * plt_entry_size);
1178 // The first entry in the PLT for an executable.
1180 const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1182 // From AMD64 ABI Draft 0.98, page 76
1183 0xff, 0x35, // pushq contents of memory address
1184 0, 0, 0, 0, // replaced with address of .got + 8
1185 0xff, 0x25, // jmp indirect
1186 0, 0, 0, 0, // replaced with address of .got + 16
1187 0x90, 0x90, 0x90, 0x90 // noop (x4)
1190 // Subsequent entries in the PLT for an executable.
1192 const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1194 // From AMD64 ABI Draft 0.98, page 76
1195 0xff, 0x25, // jmpq indirect
1196 0, 0, 0, 0, // replaced with address of symbol in .got
1197 0x68, // pushq immediate
1198 0, 0, 0, 0, // replaced with offset into relocation table
1199 0xe9, // jmpq relative
1200 0, 0, 0, 0 // replaced with offset to start of .plt
1203 // The reserved TLSDESC entry in the PLT for an executable.
1205 const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1207 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1208 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1209 0xff, 0x35, // pushq x(%rip)
1210 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1211 0xff, 0x25, // jmpq *y(%rip)
1212 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1217 // The .eh_frame unwind information for the PLT.
1220 Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1223 'z', // Augmentation: augmentation size included.
1224 'R', // Augmentation: FDE encoding included.
1225 '\0', // End of augmentation string.
1226 1, // Code alignment factor.
1227 0x78, // Data alignment factor.
1228 16, // Return address column.
1229 1, // Augmentation size.
1230 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1231 | elfcpp::DW_EH_PE_sdata4),
1232 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1233 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1234 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1239 Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1241 0, 0, 0, 0, // Replaced with offset to .plt.
1242 0, 0, 0, 0, // Replaced with size of .plt.
1243 0, // Augmentation size.
1244 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1245 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1246 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1247 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1248 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1249 11, // Block length.
1250 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1251 elfcpp::DW_OP_breg16, 0, // Push %rip.
1252 elfcpp::DW_OP_lit15, // Push 0xf.
1253 elfcpp::DW_OP_and, // & (%rip & 0xf).
1254 elfcpp::DW_OP_lit11, // Push 0xb.
1255 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1256 elfcpp::DW_OP_lit3, // Push 3.
1257 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1258 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1259 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1265 // Write out the PLT. This uses the hand-coded instructions above,
1266 // and adjusts them as needed. This is specified by the AMD64 ABI.
1269 Output_data_plt_x86_64::do_write(Output_file* of)
1271 const off_t offset = this->offset();
1272 const section_size_type oview_size =
1273 convert_to_section_size_type(this->data_size());
1274 unsigned char* const oview = of->get_output_view(offset, oview_size);
1276 const off_t got_file_offset = this->got_plt_->offset();
1277 gold_assert(parameters->incremental_update()
1278 || (got_file_offset + this->got_plt_->data_size()
1279 == this->got_irelative_->offset()));
1280 const section_size_type got_size =
1281 convert_to_section_size_type(this->got_plt_->data_size()
1282 + this->got_irelative_->data_size());
1283 unsigned char* const got_view = of->get_output_view(got_file_offset,
1286 unsigned char* pov = oview;
1288 // The base address of the .plt section.
1289 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1290 // The base address of the .got section.
1291 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1292 // The base address of the PLT portion of the .got section,
1293 // which is where the GOT pointer will point, and where the
1294 // three reserved GOT entries are located.
1295 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1297 memcpy(pov, first_plt_entry, plt_entry_size);
1298 // We do a jmp relative to the PC at the end of this instruction.
1299 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1301 - (plt_address + 6)));
1302 elfcpp::Swap<32, false>::writeval(pov + 8,
1304 - (plt_address + 12)));
1305 pov += plt_entry_size;
1307 unsigned char* got_pov = got_view;
1309 memset(got_pov, 0, 24);
1312 unsigned int plt_offset = plt_entry_size;
1313 unsigned int got_offset = 24;
1314 const unsigned int count = this->count_ + this->irelative_count_;
1315 for (unsigned int plt_index = 0;
1318 pov += plt_entry_size,
1320 plt_offset += plt_entry_size,
1323 // Set and adjust the PLT entry itself.
1324 memcpy(pov, plt_entry, plt_entry_size);
1325 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1326 (got_address + got_offset
1327 - (plt_address + plt_offset
1330 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1331 elfcpp::Swap<32, false>::writeval(pov + 12,
1332 - (plt_offset + plt_entry_size));
1334 // Set the entry in the GOT.
1335 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1338 if (this->has_tlsdesc_entry())
1340 // Set and adjust the reserved TLSDESC PLT entry.
1341 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1342 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1343 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1345 - (plt_address + plt_offset
1347 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1349 + tlsdesc_got_offset
1350 - (plt_address + plt_offset
1352 pov += plt_entry_size;
1355 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1356 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1358 of->write_output_view(offset, oview_size, oview);
1359 of->write_output_view(got_file_offset, got_size, got_view);
1362 // Create the PLT section.
1365 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1367 if (this->plt_ == NULL)
1369 // Create the GOT sections first.
1370 this->got_section(symtab, layout);
1372 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
1374 this->got_irelative_);
1375 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1377 | elfcpp::SHF_EXECINSTR),
1378 this->plt_, ORDER_PLT, false);
1380 // Make the sh_info field of .rela.plt point to .plt.
1381 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1382 rela_plt_os->set_info_section(this->plt_->output_section());
1386 // Return the section for TLSDESC relocations.
1388 Target_x86_64::Reloc_section*
1389 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1391 return this->plt_section()->rela_tlsdesc(layout);
1394 // Create a PLT entry for a global symbol.
1397 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1400 if (gsym->has_plt_offset())
1403 if (this->plt_ == NULL)
1404 this->make_plt_section(symtab, layout);
1406 this->plt_->add_entry(symtab, layout, gsym);
1409 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1412 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1413 Sized_relobj_file<64, false>* relobj,
1414 unsigned int local_sym_index)
1416 if (relobj->local_has_plt_offset(local_sym_index))
1418 if (this->plt_ == NULL)
1419 this->make_plt_section(symtab, layout);
1420 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1423 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1426 // Return the number of entries in the PLT.
1429 Target_x86_64::plt_entry_count() const
1431 if (this->plt_ == NULL)
1433 return this->plt_->entry_count();
1436 // Return the offset of the first non-reserved PLT entry.
1439 Target_x86_64::first_plt_entry_offset() const
1441 return Output_data_plt_x86_64::first_plt_entry_offset();
1444 // Return the size of each PLT entry.
1447 Target_x86_64::plt_entry_size() const
1449 return Output_data_plt_x86_64::get_plt_entry_size();
1452 // Create the GOT and PLT sections for an incremental update.
1454 Output_data_got<64, false>*
1455 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1457 unsigned int got_count,
1458 unsigned int plt_count)
1460 gold_assert(this->got_ == NULL);
1462 this->got_ = new Output_data_got<64, false>(got_count * 8);
1463 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1465 | elfcpp::SHF_WRITE),
1466 this->got_, ORDER_RELRO_LAST,
1469 // Add the three reserved entries.
1470 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1471 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1473 | elfcpp::SHF_WRITE),
1474 this->got_plt_, ORDER_NON_RELRO_FIRST,
1477 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1478 this->global_offset_table_ =
1479 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1480 Symbol_table::PREDEFINED,
1482 0, 0, elfcpp::STT_OBJECT,
1484 elfcpp::STV_HIDDEN, 0,
1487 // If there are any TLSDESC relocations, they get GOT entries in
1488 // .got.plt after the jump slot entries.
1489 // FIXME: Get the count for TLSDESC entries.
1490 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1491 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1492 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1494 ORDER_NON_RELRO_FIRST, false);
1496 // If there are any IRELATIVE relocations, they get GOT entries in
1497 // .got.plt after the jump slot and TLSDESC entries.
1498 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1499 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1500 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1501 this->got_irelative_,
1502 ORDER_NON_RELRO_FIRST, false);
1504 // Create the PLT section.
1505 this->plt_ = new Output_data_plt_x86_64(layout, this->got_, this->got_plt_,
1506 this->got_irelative_, plt_count);
1507 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1508 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1509 this->plt_, ORDER_PLT, false);
1511 // Make the sh_info field of .rela.plt point to .plt.
1512 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1513 rela_plt_os->set_info_section(this->plt_->output_section());
1515 // Create the rela_dyn section.
1516 this->rela_dyn_section(layout);
1521 // Reserve a GOT entry for a local symbol, and regenerate any
1522 // necessary dynamic relocations.
1525 Target_x86_64::reserve_local_got_entry(
1526 unsigned int got_index,
1527 Sized_relobj<64, false>* obj,
1529 unsigned int got_type)
1531 unsigned int got_offset = got_index * 8;
1532 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1534 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1537 case GOT_TYPE_STANDARD:
1538 if (parameters->options().output_is_position_independent())
1539 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1540 this->got_, got_offset, 0);
1542 case GOT_TYPE_TLS_OFFSET:
1543 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1544 this->got_, got_offset, 0);
1546 case GOT_TYPE_TLS_PAIR:
1547 this->got_->reserve_slot(got_index + 1);
1548 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1549 this->got_, got_offset, 0);
1551 case GOT_TYPE_TLS_DESC:
1552 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1553 // this->got_->reserve_slot(got_index + 1);
1554 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1555 // this->got_, got_offset, 0);
1562 // Reserve a GOT entry for a global symbol, and regenerate any
1563 // necessary dynamic relocations.
1566 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1567 unsigned int got_type)
1569 unsigned int got_offset = got_index * 8;
1570 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1572 this->got_->reserve_global(got_index, gsym, got_type);
1575 case GOT_TYPE_STANDARD:
1576 if (!gsym->final_value_is_known())
1578 if (gsym->is_from_dynobj()
1579 || gsym->is_undefined()
1580 || gsym->is_preemptible()
1581 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1582 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1583 this->got_, got_offset, 0);
1585 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1586 this->got_, got_offset, 0);
1589 case GOT_TYPE_TLS_OFFSET:
1590 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1591 this->got_, got_offset, 0);
1593 case GOT_TYPE_TLS_PAIR:
1594 this->got_->reserve_slot(got_index + 1);
1595 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1596 this->got_, got_offset, 0);
1597 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1598 this->got_, got_offset + 8, 0);
1600 case GOT_TYPE_TLS_DESC:
1601 this->got_->reserve_slot(got_index + 1);
1602 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1603 this->got_, got_offset, 0);
1610 // Register an existing PLT entry for a global symbol.
1613 Target_x86_64::register_global_plt_entry(Symbol_table* symtab,
1615 unsigned int plt_index,
1618 gold_assert(this->plt_ != NULL);
1619 gold_assert(!gsym->has_plt_offset());
1621 this->plt_->reserve_slot(plt_index);
1623 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1625 unsigned int got_offset = (plt_index + 3) * 8;
1626 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
1629 // Force a COPY relocation for a given symbol.
1632 Target_x86_64::emit_copy_reloc(
1633 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1635 this->copy_relocs_.emit_copy_reloc(symtab,
1636 symtab->get_sized_symbol<64>(sym),
1639 this->rela_dyn_section(NULL));
1642 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1645 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1647 if (this->tls_base_symbol_defined_)
1650 Output_segment* tls_segment = layout->tls_segment();
1651 if (tls_segment != NULL)
1653 bool is_exec = parameters->options().output_is_executable();
1654 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1655 Symbol_table::PREDEFINED,
1659 elfcpp::STV_HIDDEN, 0,
1661 ? Symbol::SEGMENT_END
1662 : Symbol::SEGMENT_START),
1665 this->tls_base_symbol_defined_ = true;
1668 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1671 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1674 if (this->plt_ == NULL)
1675 this->make_plt_section(symtab, layout);
1677 if (!this->plt_->has_tlsdesc_entry())
1679 // Allocate the TLSDESC_GOT entry.
1680 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1681 unsigned int got_offset = got->add_constant(0);
1683 // Allocate the TLSDESC_PLT entry.
1684 this->plt_->reserve_tlsdesc_entry(got_offset);
1688 // Create a GOT entry for the TLS module index.
1691 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1692 Sized_relobj_file<64, false>* object)
1694 if (this->got_mod_index_offset_ == -1U)
1696 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1697 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1698 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1699 unsigned int got_offset = got->add_constant(0);
1700 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1702 got->add_constant(0);
1703 this->got_mod_index_offset_ = got_offset;
1705 return this->got_mod_index_offset_;
1708 // Optimize the TLS relocation type based on what we know about the
1709 // symbol. IS_FINAL is true if the final address of this symbol is
1710 // known at link time.
1712 tls::Tls_optimization
1713 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1715 // If we are generating a shared library, then we can't do anything
1717 if (parameters->options().shared())
1718 return tls::TLSOPT_NONE;
1722 case elfcpp::R_X86_64_TLSGD:
1723 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1724 case elfcpp::R_X86_64_TLSDESC_CALL:
1725 // These are General-Dynamic which permits fully general TLS
1726 // access. Since we know that we are generating an executable,
1727 // we can convert this to Initial-Exec. If we also know that
1728 // this is a local symbol, we can further switch to Local-Exec.
1730 return tls::TLSOPT_TO_LE;
1731 return tls::TLSOPT_TO_IE;
1733 case elfcpp::R_X86_64_TLSLD:
1734 // This is Local-Dynamic, which refers to a local symbol in the
1735 // dynamic TLS block. Since we know that we generating an
1736 // executable, we can switch to Local-Exec.
1737 return tls::TLSOPT_TO_LE;
1739 case elfcpp::R_X86_64_DTPOFF32:
1740 case elfcpp::R_X86_64_DTPOFF64:
1741 // Another Local-Dynamic reloc.
1742 return tls::TLSOPT_TO_LE;
1744 case elfcpp::R_X86_64_GOTTPOFF:
1745 // These are Initial-Exec relocs which get the thread offset
1746 // from the GOT. If we know that we are linking against the
1747 // local symbol, we can switch to Local-Exec, which links the
1748 // thread offset into the instruction.
1750 return tls::TLSOPT_TO_LE;
1751 return tls::TLSOPT_NONE;
1753 case elfcpp::R_X86_64_TPOFF32:
1754 // When we already have Local-Exec, there is nothing further we
1756 return tls::TLSOPT_NONE;
1763 // Get the Reference_flags for a particular relocation.
1766 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1770 case elfcpp::R_X86_64_NONE:
1771 case elfcpp::R_X86_64_GNU_VTINHERIT:
1772 case elfcpp::R_X86_64_GNU_VTENTRY:
1773 case elfcpp::R_X86_64_GOTPC32:
1774 case elfcpp::R_X86_64_GOTPC64:
1775 // No symbol reference.
1778 case elfcpp::R_X86_64_64:
1779 case elfcpp::R_X86_64_32:
1780 case elfcpp::R_X86_64_32S:
1781 case elfcpp::R_X86_64_16:
1782 case elfcpp::R_X86_64_8:
1783 return Symbol::ABSOLUTE_REF;
1785 case elfcpp::R_X86_64_PC64:
1786 case elfcpp::R_X86_64_PC32:
1787 case elfcpp::R_X86_64_PC16:
1788 case elfcpp::R_X86_64_PC8:
1789 case elfcpp::R_X86_64_GOTOFF64:
1790 return Symbol::RELATIVE_REF;
1792 case elfcpp::R_X86_64_PLT32:
1793 case elfcpp::R_X86_64_PLTOFF64:
1794 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1796 case elfcpp::R_X86_64_GOT64:
1797 case elfcpp::R_X86_64_GOT32:
1798 case elfcpp::R_X86_64_GOTPCREL64:
1799 case elfcpp::R_X86_64_GOTPCREL:
1800 case elfcpp::R_X86_64_GOTPLT64:
1802 return Symbol::ABSOLUTE_REF;
1804 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1805 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1806 case elfcpp::R_X86_64_TLSDESC_CALL:
1807 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1808 case elfcpp::R_X86_64_DTPOFF32:
1809 case elfcpp::R_X86_64_DTPOFF64:
1810 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1811 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1812 return Symbol::TLS_REF;
1814 case elfcpp::R_X86_64_COPY:
1815 case elfcpp::R_X86_64_GLOB_DAT:
1816 case elfcpp::R_X86_64_JUMP_SLOT:
1817 case elfcpp::R_X86_64_RELATIVE:
1818 case elfcpp::R_X86_64_IRELATIVE:
1819 case elfcpp::R_X86_64_TPOFF64:
1820 case elfcpp::R_X86_64_DTPMOD64:
1821 case elfcpp::R_X86_64_TLSDESC:
1822 case elfcpp::R_X86_64_SIZE32:
1823 case elfcpp::R_X86_64_SIZE64:
1825 // Not expected. We will give an error later.
1830 // Report an unsupported relocation against a local symbol.
1833 Target_x86_64::Scan::unsupported_reloc_local(
1834 Sized_relobj_file<64, false>* object,
1835 unsigned int r_type)
1837 gold_error(_("%s: unsupported reloc %u against local symbol"),
1838 object->name().c_str(), r_type);
1841 // We are about to emit a dynamic relocation of type R_TYPE. If the
1842 // dynamic linker does not support it, issue an error. The GNU linker
1843 // only issues a non-PIC error for an allocated read-only section.
1844 // Here we know the section is allocated, but we don't know that it is
1845 // read-only. But we check for all the relocation types which the
1846 // glibc dynamic linker supports, so it seems appropriate to issue an
1847 // error even if the section is not read-only. If GSYM is not NULL,
1848 // it is the symbol the relocation is against; if it is NULL, the
1849 // relocation is against a local symbol.
1852 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1857 // These are the relocation types supported by glibc for x86_64
1858 // which should always work.
1859 case elfcpp::R_X86_64_RELATIVE:
1860 case elfcpp::R_X86_64_IRELATIVE:
1861 case elfcpp::R_X86_64_GLOB_DAT:
1862 case elfcpp::R_X86_64_JUMP_SLOT:
1863 case elfcpp::R_X86_64_DTPMOD64:
1864 case elfcpp::R_X86_64_DTPOFF64:
1865 case elfcpp::R_X86_64_TPOFF64:
1866 case elfcpp::R_X86_64_64:
1867 case elfcpp::R_X86_64_COPY:
1870 // glibc supports these reloc types, but they can overflow.
1871 case elfcpp::R_X86_64_PC32:
1872 // A PC relative reference is OK against a local symbol or if
1873 // the symbol is defined locally.
1875 || (!gsym->is_from_dynobj()
1876 && !gsym->is_undefined()
1877 && !gsym->is_preemptible()))
1880 case elfcpp::R_X86_64_32:
1881 if (this->issued_non_pic_error_)
1883 gold_assert(parameters->options().output_is_position_independent());
1885 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1886 "overflow at runtime; recompile with -fPIC"));
1888 object->error(_("requires dynamic %s reloc against '%s' which may "
1889 "overflow at runtime; recompile with -fPIC"),
1890 (r_type == elfcpp::R_X86_64_32
1894 this->issued_non_pic_error_ = true;
1898 // This prevents us from issuing more than one error per reloc
1899 // section. But we can still wind up issuing more than one
1900 // error per object file.
1901 if (this->issued_non_pic_error_)
1903 gold_assert(parameters->options().output_is_position_independent());
1904 object->error(_("requires unsupported dynamic reloc %u; "
1905 "recompile with -fPIC"),
1907 this->issued_non_pic_error_ = true;
1910 case elfcpp::R_X86_64_NONE:
1915 // Return whether we need to make a PLT entry for a relocation of the
1916 // given type against a STT_GNU_IFUNC symbol.
1919 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1920 Sized_relobj_file<64, false>* object,
1921 unsigned int r_type)
1923 int flags = Scan::get_reference_flags(r_type);
1924 if (flags & Symbol::TLS_REF)
1925 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1926 object->name().c_str(), r_type);
1930 // Scan a relocation for a local symbol.
1933 Target_x86_64::Scan::local(Symbol_table* symtab,
1935 Target_x86_64* target,
1936 Sized_relobj_file<64, false>* object,
1937 unsigned int data_shndx,
1938 Output_section* output_section,
1939 const elfcpp::Rela<64, false>& reloc,
1940 unsigned int r_type,
1941 const elfcpp::Sym<64, false>& lsym)
1943 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1944 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1945 && this->reloc_needs_plt_for_ifunc(object, r_type))
1947 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1948 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1953 case elfcpp::R_X86_64_NONE:
1954 case elfcpp::R_X86_64_GNU_VTINHERIT:
1955 case elfcpp::R_X86_64_GNU_VTENTRY:
1958 case elfcpp::R_X86_64_64:
1959 // If building a shared library (or a position-independent
1960 // executable), we need to create a dynamic relocation for this
1961 // location. The relocation applied at link time will apply the
1962 // link-time value, so we flag the location with an
1963 // R_X86_64_RELATIVE relocation so the dynamic loader can
1964 // relocate it easily.
1965 if (parameters->options().output_is_position_independent())
1967 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1968 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1969 rela_dyn->add_local_relative(object, r_sym,
1970 elfcpp::R_X86_64_RELATIVE,
1971 output_section, data_shndx,
1972 reloc.get_r_offset(),
1973 reloc.get_r_addend());
1977 case elfcpp::R_X86_64_32:
1978 case elfcpp::R_X86_64_32S:
1979 case elfcpp::R_X86_64_16:
1980 case elfcpp::R_X86_64_8:
1981 // If building a shared library (or a position-independent
1982 // executable), we need to create a dynamic relocation for this
1983 // location. We can't use an R_X86_64_RELATIVE relocation
1984 // because that is always a 64-bit relocation.
1985 if (parameters->options().output_is_position_independent())
1987 this->check_non_pic(object, r_type, NULL);
1989 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1990 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1991 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1992 rela_dyn->add_local(object, r_sym, r_type, output_section,
1993 data_shndx, reloc.get_r_offset(),
1994 reloc.get_r_addend());
1997 gold_assert(lsym.get_st_value() == 0);
1998 unsigned int shndx = lsym.get_st_shndx();
2000 shndx = object->adjust_sym_shndx(r_sym, shndx,
2003 object->error(_("section symbol %u has bad shndx %u"),
2006 rela_dyn->add_local_section(object, shndx,
2007 r_type, output_section,
2008 data_shndx, reloc.get_r_offset(),
2009 reloc.get_r_addend());
2014 case elfcpp::R_X86_64_PC64:
2015 case elfcpp::R_X86_64_PC32:
2016 case elfcpp::R_X86_64_PC16:
2017 case elfcpp::R_X86_64_PC8:
2020 case elfcpp::R_X86_64_PLT32:
2021 // Since we know this is a local symbol, we can handle this as a
2025 case elfcpp::R_X86_64_GOTPC32:
2026 case elfcpp::R_X86_64_GOTOFF64:
2027 case elfcpp::R_X86_64_GOTPC64:
2028 case elfcpp::R_X86_64_PLTOFF64:
2029 // We need a GOT section.
2030 target->got_section(symtab, layout);
2031 // For PLTOFF64, we'd normally want a PLT section, but since we
2032 // know this is a local symbol, no PLT is needed.
2035 case elfcpp::R_X86_64_GOT64:
2036 case elfcpp::R_X86_64_GOT32:
2037 case elfcpp::R_X86_64_GOTPCREL64:
2038 case elfcpp::R_X86_64_GOTPCREL:
2039 case elfcpp::R_X86_64_GOTPLT64:
2041 // The symbol requires a GOT entry.
2042 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2043 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2045 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2046 // lets function pointers compare correctly with shared
2047 // libraries. Otherwise we would need an IRELATIVE reloc.
2049 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
2050 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2052 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2055 // If we are generating a shared object, we need to add a
2056 // dynamic relocation for this symbol's GOT entry.
2057 if (parameters->options().output_is_position_independent())
2059 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2060 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2061 if (r_type != elfcpp::R_X86_64_GOT32)
2063 unsigned int got_offset =
2064 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2065 rela_dyn->add_local_relative(object, r_sym,
2066 elfcpp::R_X86_64_RELATIVE,
2067 got, got_offset, 0);
2071 this->check_non_pic(object, r_type, NULL);
2073 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2074 rela_dyn->add_local(
2075 object, r_sym, r_type, got,
2076 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2080 // For GOTPLT64, we'd normally want a PLT section, but since
2081 // we know this is a local symbol, no PLT is needed.
2085 case elfcpp::R_X86_64_COPY:
2086 case elfcpp::R_X86_64_GLOB_DAT:
2087 case elfcpp::R_X86_64_JUMP_SLOT:
2088 case elfcpp::R_X86_64_RELATIVE:
2089 case elfcpp::R_X86_64_IRELATIVE:
2090 // These are outstanding tls relocs, which are unexpected when linking
2091 case elfcpp::R_X86_64_TPOFF64:
2092 case elfcpp::R_X86_64_DTPMOD64:
2093 case elfcpp::R_X86_64_TLSDESC:
2094 gold_error(_("%s: unexpected reloc %u in object file"),
2095 object->name().c_str(), r_type);
2098 // These are initial tls relocs, which are expected when linking
2099 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2100 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2101 case elfcpp::R_X86_64_TLSDESC_CALL:
2102 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2103 case elfcpp::R_X86_64_DTPOFF32:
2104 case elfcpp::R_X86_64_DTPOFF64:
2105 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2106 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2108 bool output_is_shared = parameters->options().shared();
2109 const tls::Tls_optimization optimized_type
2110 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2113 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2114 if (optimized_type == tls::TLSOPT_NONE)
2116 // Create a pair of GOT entries for the module index and
2117 // dtv-relative offset.
2118 Output_data_got<64, false>* got
2119 = target->got_section(symtab, layout);
2120 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2121 unsigned int shndx = lsym.get_st_shndx();
2123 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2125 object->error(_("local symbol %u has bad shndx %u"),
2128 got->add_local_pair_with_rela(object, r_sym,
2131 target->rela_dyn_section(layout),
2132 elfcpp::R_X86_64_DTPMOD64, 0);
2134 else if (optimized_type != tls::TLSOPT_TO_LE)
2135 unsupported_reloc_local(object, r_type);
2138 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2139 target->define_tls_base_symbol(symtab, layout);
2140 if (optimized_type == tls::TLSOPT_NONE)
2142 // Create reserved PLT and GOT entries for the resolver.
2143 target->reserve_tlsdesc_entries(symtab, layout);
2145 // Generate a double GOT entry with an
2146 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2147 // is resolved lazily, so the GOT entry needs to be in
2148 // an area in .got.plt, not .got. Call got_section to
2149 // make sure the section has been created.
2150 target->got_section(symtab, layout);
2151 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2152 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2153 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2155 unsigned int got_offset = got->add_constant(0);
2156 got->add_constant(0);
2157 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2159 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2160 // We store the arguments we need in a vector, and
2161 // use the index into the vector as the parameter
2162 // to pass to the target specific routines.
2163 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2164 void* arg = reinterpret_cast<void*>(intarg);
2165 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2166 got, got_offset, 0);
2169 else if (optimized_type != tls::TLSOPT_TO_LE)
2170 unsupported_reloc_local(object, r_type);
2173 case elfcpp::R_X86_64_TLSDESC_CALL:
2176 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2177 if (optimized_type == tls::TLSOPT_NONE)
2179 // Create a GOT entry for the module index.
2180 target->got_mod_index_entry(symtab, layout, object);
2182 else if (optimized_type != tls::TLSOPT_TO_LE)
2183 unsupported_reloc_local(object, r_type);
2186 case elfcpp::R_X86_64_DTPOFF32:
2187 case elfcpp::R_X86_64_DTPOFF64:
2190 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2191 layout->set_has_static_tls();
2192 if (optimized_type == tls::TLSOPT_NONE)
2194 // Create a GOT entry for the tp-relative offset.
2195 Output_data_got<64, false>* got
2196 = target->got_section(symtab, layout);
2197 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2198 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
2199 target->rela_dyn_section(layout),
2200 elfcpp::R_X86_64_TPOFF64);
2202 else if (optimized_type != tls::TLSOPT_TO_LE)
2203 unsupported_reloc_local(object, r_type);
2206 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2207 layout->set_has_static_tls();
2208 if (output_is_shared)
2209 unsupported_reloc_local(object, r_type);
2218 case elfcpp::R_X86_64_SIZE32:
2219 case elfcpp::R_X86_64_SIZE64:
2221 gold_error(_("%s: unsupported reloc %u against local symbol"),
2222 object->name().c_str(), r_type);
2228 // Report an unsupported relocation against a global symbol.
2231 Target_x86_64::Scan::unsupported_reloc_global(
2232 Sized_relobj_file<64, false>* object,
2233 unsigned int r_type,
2236 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2237 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2240 // Returns true if this relocation type could be that of a function pointer.
2242 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2246 case elfcpp::R_X86_64_64:
2247 case elfcpp::R_X86_64_32:
2248 case elfcpp::R_X86_64_32S:
2249 case elfcpp::R_X86_64_16:
2250 case elfcpp::R_X86_64_8:
2251 case elfcpp::R_X86_64_GOT64:
2252 case elfcpp::R_X86_64_GOT32:
2253 case elfcpp::R_X86_64_GOTPCREL64:
2254 case elfcpp::R_X86_64_GOTPCREL:
2255 case elfcpp::R_X86_64_GOTPLT64:
2263 // For safe ICF, scan a relocation for a local symbol to check if it
2264 // corresponds to a function pointer being taken. In that case mark
2265 // the function whose pointer was taken as not foldable.
2268 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2272 Sized_relobj_file<64, false>* ,
2275 const elfcpp::Rela<64, false>& ,
2276 unsigned int r_type,
2277 const elfcpp::Sym<64, false>&)
2279 // When building a shared library, do not fold any local symbols as it is
2280 // not possible to distinguish pointer taken versus a call by looking at
2281 // the relocation types.
2282 return (parameters->options().shared()
2283 || possible_function_pointer_reloc(r_type));
2286 // For safe ICF, scan a relocation for a global symbol to check if it
2287 // corresponds to a function pointer being taken. In that case mark
2288 // the function whose pointer was taken as not foldable.
2291 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2295 Sized_relobj_file<64, false>* ,
2298 const elfcpp::Rela<64, false>& ,
2299 unsigned int r_type,
2302 // When building a shared library, do not fold symbols whose visibility
2303 // is hidden, internal or protected.
2304 return ((parameters->options().shared()
2305 && (gsym->visibility() == elfcpp::STV_INTERNAL
2306 || gsym->visibility() == elfcpp::STV_PROTECTED
2307 || gsym->visibility() == elfcpp::STV_HIDDEN))
2308 || possible_function_pointer_reloc(r_type));
2311 // Scan a relocation for a global symbol.
2314 Target_x86_64::Scan::global(Symbol_table* symtab,
2316 Target_x86_64* target,
2317 Sized_relobj_file<64, false>* object,
2318 unsigned int data_shndx,
2319 Output_section* output_section,
2320 const elfcpp::Rela<64, false>& reloc,
2321 unsigned int r_type,
2324 // A STT_GNU_IFUNC symbol may require a PLT entry.
2325 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2326 && this->reloc_needs_plt_for_ifunc(object, r_type))
2327 target->make_plt_entry(symtab, layout, gsym);
2331 case elfcpp::R_X86_64_NONE:
2332 case elfcpp::R_X86_64_GNU_VTINHERIT:
2333 case elfcpp::R_X86_64_GNU_VTENTRY:
2336 case elfcpp::R_X86_64_64:
2337 case elfcpp::R_X86_64_32:
2338 case elfcpp::R_X86_64_32S:
2339 case elfcpp::R_X86_64_16:
2340 case elfcpp::R_X86_64_8:
2342 // Make a PLT entry if necessary.
2343 if (gsym->needs_plt_entry())
2345 target->make_plt_entry(symtab, layout, gsym);
2346 // Since this is not a PC-relative relocation, we may be
2347 // taking the address of a function. In that case we need to
2348 // set the entry in the dynamic symbol table to the address of
2350 if (gsym->is_from_dynobj() && !parameters->options().shared())
2351 gsym->set_needs_dynsym_value();
2353 // Make a dynamic relocation if necessary.
2354 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2356 if (gsym->may_need_copy_reloc())
2358 target->copy_reloc(symtab, layout, object,
2359 data_shndx, output_section, gsym, reloc);
2361 else if (r_type == elfcpp::R_X86_64_64
2362 && gsym->type() == elfcpp::STT_GNU_IFUNC
2363 && gsym->can_use_relative_reloc(false)
2364 && !gsym->is_from_dynobj()
2365 && !gsym->is_undefined()
2366 && !gsym->is_preemptible())
2368 // Use an IRELATIVE reloc for a locally defined
2369 // STT_GNU_IFUNC symbol. This makes a function
2370 // address in a PIE executable match the address in a
2371 // shared library that it links against.
2372 Reloc_section* rela_dyn =
2373 target->rela_irelative_section(layout);
2374 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2375 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2376 output_section, object,
2378 reloc.get_r_offset(),
2379 reloc.get_r_addend());
2381 else if (r_type == elfcpp::R_X86_64_64
2382 && gsym->can_use_relative_reloc(false))
2384 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2385 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2386 output_section, object,
2388 reloc.get_r_offset(),
2389 reloc.get_r_addend());
2393 this->check_non_pic(object, r_type, gsym);
2394 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2395 rela_dyn->add_global(gsym, r_type, output_section, object,
2396 data_shndx, reloc.get_r_offset(),
2397 reloc.get_r_addend());
2403 case elfcpp::R_X86_64_PC64:
2404 case elfcpp::R_X86_64_PC32:
2405 case elfcpp::R_X86_64_PC16:
2406 case elfcpp::R_X86_64_PC8:
2408 // Make a PLT entry if necessary.
2409 if (gsym->needs_plt_entry())
2410 target->make_plt_entry(symtab, layout, gsym);
2411 // Make a dynamic relocation if necessary.
2412 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2414 if (gsym->may_need_copy_reloc())
2416 target->copy_reloc(symtab, layout, object,
2417 data_shndx, output_section, gsym, reloc);
2421 this->check_non_pic(object, r_type, gsym);
2422 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2423 rela_dyn->add_global(gsym, r_type, output_section, object,
2424 data_shndx, reloc.get_r_offset(),
2425 reloc.get_r_addend());
2431 case elfcpp::R_X86_64_GOT64:
2432 case elfcpp::R_X86_64_GOT32:
2433 case elfcpp::R_X86_64_GOTPCREL64:
2434 case elfcpp::R_X86_64_GOTPCREL:
2435 case elfcpp::R_X86_64_GOTPLT64:
2437 // The symbol requires a GOT entry.
2438 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2439 if (gsym->final_value_is_known())
2441 // For a STT_GNU_IFUNC symbol we want the PLT address.
2442 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2443 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2445 got->add_global(gsym, GOT_TYPE_STANDARD);
2449 // If this symbol is not fully resolved, we need to add a
2450 // dynamic relocation for it.
2451 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2453 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2455 // 1) The symbol may be defined in some other module.
2457 // 2) We are building a shared library and this is a
2458 // protected symbol; using GLOB_DAT means that the dynamic
2459 // linker can use the address of the PLT in the main
2460 // executable when appropriate so that function address
2461 // comparisons work.
2463 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2464 // code, again so that function address comparisons work.
2465 if (gsym->is_from_dynobj()
2466 || gsym->is_undefined()
2467 || gsym->is_preemptible()
2468 || (gsym->visibility() == elfcpp::STV_PROTECTED
2469 && parameters->options().shared())
2470 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2471 && parameters->options().output_is_position_independent()))
2472 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2473 elfcpp::R_X86_64_GLOB_DAT);
2476 // For a STT_GNU_IFUNC symbol we want to write the PLT
2477 // offset into the GOT, so that function pointer
2478 // comparisons work correctly.
2480 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2481 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2484 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2485 // Tell the dynamic linker to use the PLT address
2486 // when resolving relocations.
2487 if (gsym->is_from_dynobj()
2488 && !parameters->options().shared())
2489 gsym->set_needs_dynsym_value();
2493 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2494 rela_dyn->add_global_relative(gsym,
2495 elfcpp::R_X86_64_RELATIVE,
2500 // For GOTPLT64, we also need a PLT entry (but only if the
2501 // symbol is not fully resolved).
2502 if (r_type == elfcpp::R_X86_64_GOTPLT64
2503 && !gsym->final_value_is_known())
2504 target->make_plt_entry(symtab, layout, gsym);
2508 case elfcpp::R_X86_64_PLT32:
2509 // If the symbol is fully resolved, this is just a PC32 reloc.
2510 // Otherwise we need a PLT entry.
2511 if (gsym->final_value_is_known())
2513 // If building a shared library, we can also skip the PLT entry
2514 // if the symbol is defined in the output file and is protected
2516 if (gsym->is_defined()
2517 && !gsym->is_from_dynobj()
2518 && !gsym->is_preemptible())
2520 target->make_plt_entry(symtab, layout, gsym);
2523 case elfcpp::R_X86_64_GOTPC32:
2524 case elfcpp::R_X86_64_GOTOFF64:
2525 case elfcpp::R_X86_64_GOTPC64:
2526 case elfcpp::R_X86_64_PLTOFF64:
2527 // We need a GOT section.
2528 target->got_section(symtab, layout);
2529 // For PLTOFF64, we also need a PLT entry (but only if the
2530 // symbol is not fully resolved).
2531 if (r_type == elfcpp::R_X86_64_PLTOFF64
2532 && !gsym->final_value_is_known())
2533 target->make_plt_entry(symtab, layout, gsym);
2536 case elfcpp::R_X86_64_COPY:
2537 case elfcpp::R_X86_64_GLOB_DAT:
2538 case elfcpp::R_X86_64_JUMP_SLOT:
2539 case elfcpp::R_X86_64_RELATIVE:
2540 case elfcpp::R_X86_64_IRELATIVE:
2541 // These are outstanding tls relocs, which are unexpected when linking
2542 case elfcpp::R_X86_64_TPOFF64:
2543 case elfcpp::R_X86_64_DTPMOD64:
2544 case elfcpp::R_X86_64_TLSDESC:
2545 gold_error(_("%s: unexpected reloc %u in object file"),
2546 object->name().c_str(), r_type);
2549 // These are initial tls relocs, which are expected for global()
2550 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2551 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2552 case elfcpp::R_X86_64_TLSDESC_CALL:
2553 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2554 case elfcpp::R_X86_64_DTPOFF32:
2555 case elfcpp::R_X86_64_DTPOFF64:
2556 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2557 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2559 const bool is_final = gsym->final_value_is_known();
2560 const tls::Tls_optimization optimized_type
2561 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2564 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2565 if (optimized_type == tls::TLSOPT_NONE)
2567 // Create a pair of GOT entries for the module index and
2568 // dtv-relative offset.
2569 Output_data_got<64, false>* got
2570 = target->got_section(symtab, layout);
2571 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2572 target->rela_dyn_section(layout),
2573 elfcpp::R_X86_64_DTPMOD64,
2574 elfcpp::R_X86_64_DTPOFF64);
2576 else if (optimized_type == tls::TLSOPT_TO_IE)
2578 // Create a GOT entry for the tp-relative offset.
2579 Output_data_got<64, false>* got
2580 = target->got_section(symtab, layout);
2581 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2582 target->rela_dyn_section(layout),
2583 elfcpp::R_X86_64_TPOFF64);
2585 else if (optimized_type != tls::TLSOPT_TO_LE)
2586 unsupported_reloc_global(object, r_type, gsym);
2589 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2590 target->define_tls_base_symbol(symtab, layout);
2591 if (optimized_type == tls::TLSOPT_NONE)
2593 // Create reserved PLT and GOT entries for the resolver.
2594 target->reserve_tlsdesc_entries(symtab, layout);
2596 // Create a double GOT entry with an R_X86_64_TLSDESC
2597 // reloc. The R_X86_64_TLSDESC reloc is resolved
2598 // lazily, so the GOT entry needs to be in an area in
2599 // .got.plt, not .got. Call got_section to make sure
2600 // the section has been created.
2601 target->got_section(symtab, layout);
2602 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2603 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2604 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2605 elfcpp::R_X86_64_TLSDESC, 0);
2607 else if (optimized_type == tls::TLSOPT_TO_IE)
2609 // Create a GOT entry for the tp-relative offset.
2610 Output_data_got<64, false>* got
2611 = target->got_section(symtab, layout);
2612 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2613 target->rela_dyn_section(layout),
2614 elfcpp::R_X86_64_TPOFF64);
2616 else if (optimized_type != tls::TLSOPT_TO_LE)
2617 unsupported_reloc_global(object, r_type, gsym);
2620 case elfcpp::R_X86_64_TLSDESC_CALL:
2623 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2624 if (optimized_type == tls::TLSOPT_NONE)
2626 // Create a GOT entry for the module index.
2627 target->got_mod_index_entry(symtab, layout, object);
2629 else if (optimized_type != tls::TLSOPT_TO_LE)
2630 unsupported_reloc_global(object, r_type, gsym);
2633 case elfcpp::R_X86_64_DTPOFF32:
2634 case elfcpp::R_X86_64_DTPOFF64:
2637 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2638 layout->set_has_static_tls();
2639 if (optimized_type == tls::TLSOPT_NONE)
2641 // Create a GOT entry for the tp-relative offset.
2642 Output_data_got<64, false>* got
2643 = target->got_section(symtab, layout);
2644 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2645 target->rela_dyn_section(layout),
2646 elfcpp::R_X86_64_TPOFF64);
2648 else if (optimized_type != tls::TLSOPT_TO_LE)
2649 unsupported_reloc_global(object, r_type, gsym);
2652 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2653 layout->set_has_static_tls();
2654 if (parameters->options().shared())
2655 unsupported_reloc_local(object, r_type);
2664 case elfcpp::R_X86_64_SIZE32:
2665 case elfcpp::R_X86_64_SIZE64:
2667 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2668 object->name().c_str(), r_type,
2669 gsym->demangled_name().c_str());
2675 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2677 Sized_relobj_file<64, false>* object,
2678 unsigned int data_shndx,
2679 unsigned int sh_type,
2680 const unsigned char* prelocs,
2682 Output_section* output_section,
2683 bool needs_special_offset_handling,
2684 size_t local_symbol_count,
2685 const unsigned char* plocal_symbols)
2688 if (sh_type == elfcpp::SHT_REL)
2693 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2694 Target_x86_64::Scan,
2695 Target_x86_64::Relocatable_size_for_reloc>(
2704 needs_special_offset_handling,
2709 // Scan relocations for a section.
2712 Target_x86_64::scan_relocs(Symbol_table* symtab,
2714 Sized_relobj_file<64, false>* object,
2715 unsigned int data_shndx,
2716 unsigned int sh_type,
2717 const unsigned char* prelocs,
2719 Output_section* output_section,
2720 bool needs_special_offset_handling,
2721 size_t local_symbol_count,
2722 const unsigned char* plocal_symbols)
2724 if (sh_type == elfcpp::SHT_REL)
2726 gold_error(_("%s: unsupported REL reloc section"),
2727 object->name().c_str());
2731 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2732 Target_x86_64::Scan>(
2741 needs_special_offset_handling,
2746 // Finalize the sections.
2749 Target_x86_64::do_finalize_sections(
2751 const Input_objects*,
2752 Symbol_table* symtab)
2754 const Reloc_section* rel_plt = (this->plt_ == NULL
2756 : this->plt_->rela_plt());
2757 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2758 this->rela_dyn_, true, false);
2760 // Fill in some more dynamic tags.
2761 Output_data_dynamic* const odyn = layout->dynamic_data();
2764 if (this->plt_ != NULL
2765 && this->plt_->output_section() != NULL
2766 && this->plt_->has_tlsdesc_entry())
2768 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2769 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2770 this->got_->finalize_data_size();
2771 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2772 this->plt_, plt_offset);
2773 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2774 this->got_, got_offset);
2778 // Emit any relocs we saved in an attempt to avoid generating COPY
2780 if (this->copy_relocs_.any_saved_relocs())
2781 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2783 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2784 // the .got.plt section.
2785 Symbol* sym = this->global_offset_table_;
2788 uint64_t data_size = this->got_plt_->current_data_size();
2789 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2792 if (parameters->doing_static_link()
2793 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2795 // If linking statically, make sure that the __rela_iplt symbols
2796 // were defined if necessary, even if we didn't create a PLT.
2797 static const Define_symbol_in_segment syms[] =
2800 "__rela_iplt_start", // name
2801 elfcpp::PT_LOAD, // segment_type
2802 elfcpp::PF_W, // segment_flags_set
2803 elfcpp::PF(0), // segment_flags_clear
2806 elfcpp::STT_NOTYPE, // type
2807 elfcpp::STB_GLOBAL, // binding
2808 elfcpp::STV_HIDDEN, // visibility
2810 Symbol::SEGMENT_START, // offset_from_base
2814 "__rela_iplt_end", // name
2815 elfcpp::PT_LOAD, // segment_type
2816 elfcpp::PF_W, // segment_flags_set
2817 elfcpp::PF(0), // segment_flags_clear
2820 elfcpp::STT_NOTYPE, // type
2821 elfcpp::STB_GLOBAL, // binding
2822 elfcpp::STV_HIDDEN, // visibility
2824 Symbol::SEGMENT_START, // offset_from_base
2829 symtab->define_symbols(layout, 2, syms,
2830 layout->script_options()->saw_sections_clause());
2834 // Perform a relocation.
2837 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2838 Target_x86_64* target,
2841 const elfcpp::Rela<64, false>& rela,
2842 unsigned int r_type,
2843 const Sized_symbol<64>* gsym,
2844 const Symbol_value<64>* psymval,
2845 unsigned char* view,
2846 elfcpp::Elf_types<64>::Elf_Addr address,
2847 section_size_type view_size)
2849 if (this->skip_call_tls_get_addr_)
2851 if ((r_type != elfcpp::R_X86_64_PLT32
2852 && r_type != elfcpp::R_X86_64_PC32)
2854 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2856 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2857 _("missing expected TLS relocation"));
2861 this->skip_call_tls_get_addr_ = false;
2866 const Sized_relobj_file<64, false>* object = relinfo->object;
2868 // Pick the value to use for symbols defined in the PLT.
2869 Symbol_value<64> symval;
2871 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2873 symval.set_output_value(target->plt_address_for_global(gsym)
2874 + gsym->plt_offset());
2877 else if (gsym == NULL && psymval->is_ifunc_symbol())
2879 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2880 if (object->local_has_plt_offset(r_sym))
2882 symval.set_output_value(target->plt_address_for_local(object, r_sym)
2883 + object->local_plt_offset(r_sym));
2888 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2890 // Get the GOT offset if needed.
2891 // The GOT pointer points to the end of the GOT section.
2892 // We need to subtract the size of the GOT section to get
2893 // the actual offset to use in the relocation.
2894 bool have_got_offset = false;
2895 unsigned int got_offset = 0;
2898 case elfcpp::R_X86_64_GOT32:
2899 case elfcpp::R_X86_64_GOT64:
2900 case elfcpp::R_X86_64_GOTPLT64:
2901 case elfcpp::R_X86_64_GOTPCREL:
2902 case elfcpp::R_X86_64_GOTPCREL64:
2905 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2906 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2910 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2911 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2912 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2913 - target->got_size());
2915 have_got_offset = true;
2924 case elfcpp::R_X86_64_NONE:
2925 case elfcpp::R_X86_64_GNU_VTINHERIT:
2926 case elfcpp::R_X86_64_GNU_VTENTRY:
2929 case elfcpp::R_X86_64_64:
2930 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2933 case elfcpp::R_X86_64_PC64:
2934 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2938 case elfcpp::R_X86_64_32:
2939 // FIXME: we need to verify that value + addend fits into 32 bits:
2940 // uint64_t x = value + addend;
2941 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2942 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2943 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2946 case elfcpp::R_X86_64_32S:
2947 // FIXME: we need to verify that value + addend fits into 32 bits:
2948 // int64_t x = value + addend; // note this quantity is signed!
2949 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2950 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2953 case elfcpp::R_X86_64_PC32:
2954 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2958 case elfcpp::R_X86_64_16:
2959 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2962 case elfcpp::R_X86_64_PC16:
2963 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2967 case elfcpp::R_X86_64_8:
2968 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2971 case elfcpp::R_X86_64_PC8:
2972 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2976 case elfcpp::R_X86_64_PLT32:
2977 gold_assert(gsym == NULL
2978 || gsym->has_plt_offset()
2979 || gsym->final_value_is_known()
2980 || (gsym->is_defined()
2981 && !gsym->is_from_dynobj()
2982 && !gsym->is_preemptible()));
2983 // Note: while this code looks the same as for R_X86_64_PC32, it
2984 // behaves differently because psymval was set to point to
2985 // the PLT entry, rather than the symbol, in Scan::global().
2986 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2990 case elfcpp::R_X86_64_PLTOFF64:
2993 gold_assert(gsym->has_plt_offset()
2994 || gsym->final_value_is_known());
2995 elfcpp::Elf_types<64>::Elf_Addr got_address;
2996 got_address = target->got_section(NULL, NULL)->address();
2997 Relocate_functions<64, false>::rela64(view, object, psymval,
2998 addend - got_address);
3001 case elfcpp::R_X86_64_GOT32:
3002 gold_assert(have_got_offset);
3003 Relocate_functions<64, false>::rela32(view, got_offset, addend);
3006 case elfcpp::R_X86_64_GOTPC32:
3009 elfcpp::Elf_types<64>::Elf_Addr value;
3010 value = target->got_plt_section()->address();
3011 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3015 case elfcpp::R_X86_64_GOT64:
3016 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3017 // Since we always add a PLT entry, this is equivalent.
3018 case elfcpp::R_X86_64_GOTPLT64:
3019 gold_assert(have_got_offset);
3020 Relocate_functions<64, false>::rela64(view, got_offset, addend);
3023 case elfcpp::R_X86_64_GOTPC64:
3026 elfcpp::Elf_types<64>::Elf_Addr value;
3027 value = target->got_plt_section()->address();
3028 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
3032 case elfcpp::R_X86_64_GOTOFF64:
3034 elfcpp::Elf_types<64>::Elf_Addr value;
3035 value = (psymval->value(object, 0)
3036 - target->got_plt_section()->address());
3037 Relocate_functions<64, false>::rela64(view, value, addend);
3041 case elfcpp::R_X86_64_GOTPCREL:
3043 gold_assert(have_got_offset);
3044 elfcpp::Elf_types<64>::Elf_Addr value;
3045 value = target->got_plt_section()->address() + got_offset;
3046 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3050 case elfcpp::R_X86_64_GOTPCREL64:
3052 gold_assert(have_got_offset);
3053 elfcpp::Elf_types<64>::Elf_Addr value;
3054 value = target->got_plt_section()->address() + got_offset;
3055 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
3059 case elfcpp::R_X86_64_COPY:
3060 case elfcpp::R_X86_64_GLOB_DAT:
3061 case elfcpp::R_X86_64_JUMP_SLOT:
3062 case elfcpp::R_X86_64_RELATIVE:
3063 case elfcpp::R_X86_64_IRELATIVE:
3064 // These are outstanding tls relocs, which are unexpected when linking
3065 case elfcpp::R_X86_64_TPOFF64:
3066 case elfcpp::R_X86_64_DTPMOD64:
3067 case elfcpp::R_X86_64_TLSDESC:
3068 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3069 _("unexpected reloc %u in object file"),
3073 // These are initial tls relocs, which are expected when linking
3074 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3075 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3076 case elfcpp::R_X86_64_TLSDESC_CALL:
3077 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3078 case elfcpp::R_X86_64_DTPOFF32:
3079 case elfcpp::R_X86_64_DTPOFF64:
3080 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3081 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3082 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3083 view, address, view_size);
3086 case elfcpp::R_X86_64_SIZE32:
3087 case elfcpp::R_X86_64_SIZE64:
3089 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3090 _("unsupported reloc %u"),
3098 // Perform a TLS relocation.
3101 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
3102 Target_x86_64* target,
3104 const elfcpp::Rela<64, false>& rela,
3105 unsigned int r_type,
3106 const Sized_symbol<64>* gsym,
3107 const Symbol_value<64>* psymval,
3108 unsigned char* view,
3109 elfcpp::Elf_types<64>::Elf_Addr address,
3110 section_size_type view_size)
3112 Output_segment* tls_segment = relinfo->layout->tls_segment();
3114 const Sized_relobj_file<64, false>* object = relinfo->object;
3115 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3116 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
3117 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3119 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
3121 const bool is_final = (gsym == NULL
3122 ? !parameters->options().shared()
3123 : gsym->final_value_is_known());
3124 tls::Tls_optimization optimized_type
3125 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
3128 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3129 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3131 // If this code sequence is used in a non-executable section,
3132 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3133 // on the assumption that it's being used by itself in a debug
3134 // section. Therefore, in the unlikely event that the code
3135 // sequence appears in a non-executable section, we simply
3136 // leave it unoptimized.
3137 optimized_type = tls::TLSOPT_NONE;
3139 if (optimized_type == tls::TLSOPT_TO_LE)
3141 if (tls_segment == NULL)
3143 gold_assert(parameters->errors()->error_count() > 0
3144 || issue_undefined_symbol_error(gsym));
3147 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3148 rela, r_type, value, view,
3154 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3155 ? GOT_TYPE_TLS_OFFSET
3156 : GOT_TYPE_TLS_PAIR);
3157 unsigned int got_offset;
3160 gold_assert(gsym->has_got_offset(got_type));
3161 got_offset = gsym->got_offset(got_type) - target->got_size();
3165 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3166 gold_assert(object->local_has_got_offset(r_sym, got_type));
3167 got_offset = (object->local_got_offset(r_sym, got_type)
3168 - target->got_size());
3170 if (optimized_type == tls::TLSOPT_TO_IE)
3172 if (tls_segment == NULL)
3174 gold_assert(parameters->errors()->error_count() > 0
3175 || issue_undefined_symbol_error(gsym));
3178 value = target->got_plt_section()->address() + got_offset;
3179 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3180 value, view, address, view_size);
3183 else if (optimized_type == tls::TLSOPT_NONE)
3185 // Relocate the field with the offset of the pair of GOT
3187 value = target->got_plt_section()->address() + got_offset;
3188 Relocate_functions<64, false>::pcrela32(view, value, addend,
3193 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3194 _("unsupported reloc %u"), r_type);
3197 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3198 case elfcpp::R_X86_64_TLSDESC_CALL:
3199 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3201 // See above comment for R_X86_64_TLSGD.
3202 optimized_type = tls::TLSOPT_NONE;
3204 if (optimized_type == tls::TLSOPT_TO_LE)
3206 if (tls_segment == NULL)
3208 gold_assert(parameters->errors()->error_count() > 0
3209 || issue_undefined_symbol_error(gsym));
3212 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3213 rela, r_type, value, view,
3219 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3220 ? GOT_TYPE_TLS_OFFSET
3221 : GOT_TYPE_TLS_DESC);
3222 unsigned int got_offset = 0;
3223 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3224 && optimized_type == tls::TLSOPT_NONE)
3226 // We created GOT entries in the .got.tlsdesc portion of
3227 // the .got.plt section, but the offset stored in the
3228 // symbol is the offset within .got.tlsdesc.
3229 got_offset = (target->got_size()
3230 + target->got_plt_section()->data_size());
3234 gold_assert(gsym->has_got_offset(got_type));
3235 got_offset += gsym->got_offset(got_type) - target->got_size();
3239 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3240 gold_assert(object->local_has_got_offset(r_sym, got_type));
3241 got_offset += (object->local_got_offset(r_sym, got_type)
3242 - target->got_size());
3244 if (optimized_type == tls::TLSOPT_TO_IE)
3246 if (tls_segment == NULL)
3248 gold_assert(parameters->errors()->error_count() > 0
3249 || issue_undefined_symbol_error(gsym));
3252 value = target->got_plt_section()->address() + got_offset;
3253 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3254 rela, r_type, value, view, address,
3258 else if (optimized_type == tls::TLSOPT_NONE)
3260 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3262 // Relocate the field with the offset of the pair of GOT
3264 value = target->got_plt_section()->address() + got_offset;
3265 Relocate_functions<64, false>::pcrela32(view, value, addend,
3271 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3272 _("unsupported reloc %u"), r_type);
3275 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3276 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3278 // See above comment for R_X86_64_TLSGD.
3279 optimized_type = tls::TLSOPT_NONE;
3281 if (optimized_type == tls::TLSOPT_TO_LE)
3283 if (tls_segment == NULL)
3285 gold_assert(parameters->errors()->error_count() > 0
3286 || issue_undefined_symbol_error(gsym));
3289 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3290 value, view, view_size);
3293 else if (optimized_type == tls::TLSOPT_NONE)
3295 // Relocate the field with the offset of the GOT entry for
3296 // the module index.
3297 unsigned int got_offset;
3298 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3299 - target->got_size());
3300 value = target->got_plt_section()->address() + got_offset;
3301 Relocate_functions<64, false>::pcrela32(view, value, addend,
3305 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3306 _("unsupported reloc %u"), r_type);
3309 case elfcpp::R_X86_64_DTPOFF32:
3310 // This relocation type is used in debugging information.
3311 // In that case we need to not optimize the value. If the
3312 // section is not executable, then we assume we should not
3313 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3314 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3316 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3318 if (tls_segment == NULL)
3320 gold_assert(parameters->errors()->error_count() > 0
3321 || issue_undefined_symbol_error(gsym));
3324 value -= tls_segment->memsz();
3326 Relocate_functions<64, false>::rela32(view, value, addend);
3329 case elfcpp::R_X86_64_DTPOFF64:
3330 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3331 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3333 if (tls_segment == NULL)
3335 gold_assert(parameters->errors()->error_count() > 0
3336 || issue_undefined_symbol_error(gsym));
3339 value -= tls_segment->memsz();
3341 Relocate_functions<64, false>::rela64(view, value, addend);
3344 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3345 if (optimized_type == tls::TLSOPT_TO_LE)
3347 if (tls_segment == NULL)
3349 gold_assert(parameters->errors()->error_count() > 0
3350 || issue_undefined_symbol_error(gsym));
3353 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3354 rela, r_type, value, view,
3358 else if (optimized_type == tls::TLSOPT_NONE)
3360 // Relocate the field with the offset of the GOT entry for
3361 // the tp-relative offset of the symbol.
3362 unsigned int got_offset;
3365 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3366 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3367 - target->got_size());
3371 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3372 gold_assert(object->local_has_got_offset(r_sym,
3373 GOT_TYPE_TLS_OFFSET));
3374 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3375 - target->got_size());
3377 value = target->got_plt_section()->address() + got_offset;
3378 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3381 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3382 _("unsupported reloc type %u"),
3386 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3387 if (tls_segment == NULL)
3389 gold_assert(parameters->errors()->error_count() > 0
3390 || issue_undefined_symbol_error(gsym));
3393 value -= tls_segment->memsz();
3394 Relocate_functions<64, false>::rela32(view, value, addend);
3399 // Do a relocation in which we convert a TLS General-Dynamic to an
3403 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3406 const elfcpp::Rela<64, false>& rela,
3408 elfcpp::Elf_types<64>::Elf_Addr value,
3409 unsigned char* view,
3410 elfcpp::Elf_types<64>::Elf_Addr address,
3411 section_size_type view_size)
3413 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3414 // .word 0x6666; rex64; call __tls_get_addr
3415 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3417 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3418 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3420 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3421 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3422 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3423 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3425 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3427 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3428 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3430 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3432 this->skip_call_tls_get_addr_ = true;
3435 // Do a relocation in which we convert a TLS General-Dynamic to a
3439 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3441 Output_segment* tls_segment,
3442 const elfcpp::Rela<64, false>& rela,
3444 elfcpp::Elf_types<64>::Elf_Addr value,
3445 unsigned char* view,
3446 section_size_type view_size)
3448 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3449 // .word 0x6666; rex64; call __tls_get_addr
3450 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3452 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3453 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3455 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3456 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3457 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3458 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3460 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3462 value -= tls_segment->memsz();
3463 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3465 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3467 this->skip_call_tls_get_addr_ = true;
3470 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3473 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3474 const Relocate_info<64, false>* relinfo,
3477 const elfcpp::Rela<64, false>& rela,
3478 unsigned int r_type,
3479 elfcpp::Elf_types<64>::Elf_Addr value,
3480 unsigned char* view,
3481 elfcpp::Elf_types<64>::Elf_Addr address,
3482 section_size_type view_size)
3484 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3486 // leaq foo@tlsdesc(%rip), %rax
3487 // ==> movq foo@gottpoff(%rip), %rax
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, 4);
3490 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3491 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3493 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3494 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3498 // call *foo@tlscall(%rax)
3500 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3501 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3502 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3503 view[0] == 0xff && view[1] == 0x10);
3509 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3512 Target_x86_64::Relocate::tls_desc_gd_to_le(
3513 const Relocate_info<64, false>* relinfo,
3515 Output_segment* tls_segment,
3516 const elfcpp::Rela<64, false>& rela,
3517 unsigned int r_type,
3518 elfcpp::Elf_types<64>::Elf_Addr value,
3519 unsigned char* view,
3520 section_size_type view_size)
3522 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3524 // leaq foo@tlsdesc(%rip), %rax
3525 // ==> movq foo@tpoff, %rax
3526 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3527 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3528 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3529 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3532 value -= tls_segment->memsz();
3533 Relocate_functions<64, false>::rela32(view, value, 0);
3537 // call *foo@tlscall(%rax)
3539 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3540 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3541 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3542 view[0] == 0xff && view[1] == 0x10);
3549 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3552 const elfcpp::Rela<64, false>& rela,
3554 elfcpp::Elf_types<64>::Elf_Addr,
3555 unsigned char* view,
3556 section_size_type view_size)
3558 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3559 // ... leq foo@dtpoff(%rax),%reg
3560 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3562 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3563 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3565 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3566 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3568 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3570 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3572 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3574 this->skip_call_tls_get_addr_ = true;
3577 // Do a relocation in which we convert a TLS Initial-Exec to a
3581 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3583 Output_segment* tls_segment,
3584 const elfcpp::Rela<64, false>& rela,
3586 elfcpp::Elf_types<64>::Elf_Addr value,
3587 unsigned char* view,
3588 section_size_type view_size)
3590 // We need to examine the opcodes to figure out which instruction we
3593 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3594 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3596 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3597 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3599 unsigned char op1 = view[-3];
3600 unsigned char op2 = view[-2];
3601 unsigned char op3 = view[-1];
3602 unsigned char reg = op3 >> 3;
3610 view[-1] = 0xc0 | reg;
3614 // Special handling for %rsp.
3618 view[-1] = 0xc0 | reg;
3626 view[-1] = 0x80 | reg | (reg << 3);
3629 value -= tls_segment->memsz();
3630 Relocate_functions<64, false>::rela32(view, value, 0);
3633 // Relocate section data.
3636 Target_x86_64::relocate_section(
3637 const Relocate_info<64, false>* relinfo,
3638 unsigned int sh_type,
3639 const unsigned char* prelocs,
3641 Output_section* output_section,
3642 bool needs_special_offset_handling,
3643 unsigned char* view,
3644 elfcpp::Elf_types<64>::Elf_Addr address,
3645 section_size_type view_size,
3646 const Reloc_symbol_changes* reloc_symbol_changes)
3648 gold_assert(sh_type == elfcpp::SHT_RELA);
3650 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3651 Target_x86_64::Relocate>(
3657 needs_special_offset_handling,
3661 reloc_symbol_changes);
3664 // Apply an incremental relocation. Incremental relocations always refer
3665 // to global symbols.
3668 Target_x86_64::apply_relocation(
3669 const Relocate_info<64, false>* relinfo,
3670 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3671 unsigned int r_type,
3672 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3674 unsigned char* view,
3675 elfcpp::Elf_types<64>::Elf_Addr address,
3676 section_size_type view_size)
3678 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3690 // Return the size of a relocation while scanning during a relocatable
3694 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3695 unsigned int r_type,
3700 case elfcpp::R_X86_64_NONE:
3701 case elfcpp::R_X86_64_GNU_VTINHERIT:
3702 case elfcpp::R_X86_64_GNU_VTENTRY:
3703 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3704 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3705 case elfcpp::R_X86_64_TLSDESC_CALL:
3706 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3707 case elfcpp::R_X86_64_DTPOFF32:
3708 case elfcpp::R_X86_64_DTPOFF64:
3709 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3710 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3713 case elfcpp::R_X86_64_64:
3714 case elfcpp::R_X86_64_PC64:
3715 case elfcpp::R_X86_64_GOTOFF64:
3716 case elfcpp::R_X86_64_GOTPC64:
3717 case elfcpp::R_X86_64_PLTOFF64:
3718 case elfcpp::R_X86_64_GOT64:
3719 case elfcpp::R_X86_64_GOTPCREL64:
3720 case elfcpp::R_X86_64_GOTPCREL:
3721 case elfcpp::R_X86_64_GOTPLT64:
3724 case elfcpp::R_X86_64_32:
3725 case elfcpp::R_X86_64_32S:
3726 case elfcpp::R_X86_64_PC32:
3727 case elfcpp::R_X86_64_PLT32:
3728 case elfcpp::R_X86_64_GOTPC32:
3729 case elfcpp::R_X86_64_GOT32:
3732 case elfcpp::R_X86_64_16:
3733 case elfcpp::R_X86_64_PC16:
3736 case elfcpp::R_X86_64_8:
3737 case elfcpp::R_X86_64_PC8:
3740 case elfcpp::R_X86_64_COPY:
3741 case elfcpp::R_X86_64_GLOB_DAT:
3742 case elfcpp::R_X86_64_JUMP_SLOT:
3743 case elfcpp::R_X86_64_RELATIVE:
3744 case elfcpp::R_X86_64_IRELATIVE:
3745 // These are outstanding tls relocs, which are unexpected when linking
3746 case elfcpp::R_X86_64_TPOFF64:
3747 case elfcpp::R_X86_64_DTPMOD64:
3748 case elfcpp::R_X86_64_TLSDESC:
3749 object->error(_("unexpected reloc %u in object file"), r_type);
3752 case elfcpp::R_X86_64_SIZE32:
3753 case elfcpp::R_X86_64_SIZE64:
3755 object->error(_("unsupported reloc %u against local symbol"), r_type);
3760 // Scan the relocs during a relocatable link.
3763 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3765 Sized_relobj_file<64, false>* object,
3766 unsigned int data_shndx,
3767 unsigned int sh_type,
3768 const unsigned char* prelocs,
3770 Output_section* output_section,
3771 bool needs_special_offset_handling,
3772 size_t local_symbol_count,
3773 const unsigned char* plocal_symbols,
3774 Relocatable_relocs* rr)
3776 gold_assert(sh_type == elfcpp::SHT_RELA);
3778 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3779 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3781 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3782 Scan_relocatable_relocs>(
3790 needs_special_offset_handling,
3796 // Relocate a section during a relocatable link.
3799 Target_x86_64::relocate_for_relocatable(
3800 const Relocate_info<64, false>* relinfo,
3801 unsigned int sh_type,
3802 const unsigned char* prelocs,
3804 Output_section* output_section,
3805 off_t offset_in_output_section,
3806 const Relocatable_relocs* rr,
3807 unsigned char* view,
3808 elfcpp::Elf_types<64>::Elf_Addr view_address,
3809 section_size_type view_size,
3810 unsigned char* reloc_view,
3811 section_size_type reloc_view_size)
3813 gold_assert(sh_type == elfcpp::SHT_RELA);
3815 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3820 offset_in_output_section,
3829 // Return the value to use for a dynamic which requires special
3830 // treatment. This is how we support equality comparisons of function
3831 // pointers across shared library boundaries, as described in the
3832 // processor specific ABI supplement.
3835 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3837 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3838 return this->plt_address_for_global(gsym) + gsym->plt_offset();
3841 // Return a string used to fill a code section with nops to take up
3842 // the specified length.
3845 Target_x86_64::do_code_fill(section_size_type length) const
3849 // Build a jmpq instruction to skip over the bytes.
3850 unsigned char jmp[5];
3852 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3853 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3854 + std::string(length - 5, '\0'));
3857 // Nop sequences of various lengths.
3858 const char nop1[1] = { 0x90 }; // nop
3859 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3860 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3861 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3862 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3864 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3866 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3868 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3869 0x00, 0x00, 0x00, 0x00 };
3870 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3871 0x00, 0x00, 0x00, 0x00,
3873 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3874 0x84, 0x00, 0x00, 0x00,
3876 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3877 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3879 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3880 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3881 0x00, 0x00, 0x00, 0x00 };
3882 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3883 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3884 0x00, 0x00, 0x00, 0x00,
3886 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3887 0x66, 0x2e, 0x0f, 0x1f, // data16
3888 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3890 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3891 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3892 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3895 const char* nops[16] = {
3897 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3898 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3901 return std::string(nops[length], length);
3904 // Return the addend to use for a target specific relocation. The
3905 // only target specific relocation is R_X86_64_TLSDESC for a local
3906 // symbol. We want to set the addend is the offset of the local
3907 // symbol in the TLS segment.
3910 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3913 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3914 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3915 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3916 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3917 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3918 gold_assert(psymval->is_tls_symbol());
3919 // The value of a TLS symbol is the offset in the TLS segment.
3920 return psymval->value(ti.object, 0);
3923 // Return the value to use for the base of a DW_EH_PE_datarel offset
3924 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3925 // assembler can not write out the difference between two labels in
3926 // different sections, so instead of using a pc-relative value they
3927 // use an offset from the GOT.
3930 Target_x86_64::do_ehframe_datarel_base() const
3932 gold_assert(this->global_offset_table_ != NULL);
3933 Symbol* sym = this->global_offset_table_;
3934 Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym);
3935 return ssym->value();
3938 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3939 // compiled with -fsplit-stack. The function calls non-split-stack
3940 // code. We have to change the function so that it always ensures
3941 // that it has enough stack space to run some random function.
3944 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3945 section_offset_type fnoffset,
3946 section_size_type fnsize,
3947 unsigned char* view,
3948 section_size_type view_size,
3950 std::string* to) const
3952 // The function starts with a comparison of the stack pointer and a
3953 // field in the TCB. This is followed by a jump.
3956 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3959 // We will call __morestack if the carry flag is set after this
3960 // comparison. We turn the comparison into an stc instruction
3962 view[fnoffset] = '\xf9';
3963 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3965 // lea NN(%rsp),%r10
3966 // lea NN(%rsp),%r11
3967 else if ((this->match_view(view, view_size, fnoffset,
3968 "\x4c\x8d\x94\x24", 4)
3969 || this->match_view(view, view_size, fnoffset,
3970 "\x4c\x8d\x9c\x24", 4))
3973 // This is loading an offset from the stack pointer for a
3974 // comparison. The offset is negative, so we decrease the
3975 // offset by the amount of space we need for the stack. This
3976 // means we will avoid calling __morestack if there happens to
3977 // be plenty of space on the stack already.
3978 unsigned char* pval = view + fnoffset + 4;
3979 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3980 val -= parameters->options().split_stack_adjust_size();
3981 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3985 if (!object->has_no_split_stack())
3986 object->error(_("failed to match split-stack sequence at "
3987 "section %u offset %0zx"),
3988 shndx, static_cast<size_t>(fnoffset));
3992 // We have to change the function so that it calls
3993 // __morestack_non_split instead of __morestack. The former will
3994 // allocate additional stack space.
3995 *from = "__morestack";
3996 *to = "__morestack_non_split";
3999 // The selector for x86_64 object files.
4001 class Target_selector_x86_64 : public Target_selector_freebsd
4004 Target_selector_x86_64()
4005 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
4006 "elf64-x86-64-freebsd", "elf_x86_64")
4010 do_instantiate_target()
4011 { return new Target_x86_64(); }
4015 Target_selector_x86_64 target_selector_x86_64;
4017 } // End anonymous namespace.