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(Symbol_table* symtab, Layout* layout,
58 Output_data_got<64, false>* got,
59 Output_data_space* got_plt)
60 : Output_section_data(16), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
61 count_(0), tlsdesc_got_offset_(-1U), free_list_()
62 { this->init(symtab, layout); }
64 Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
65 Output_data_got<64, false>* got,
66 Output_data_space* got_plt,
67 unsigned int plt_count)
68 : Output_section_data((plt_count + 1) * plt_entry_size, 16, false),
69 tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
70 count_(plt_count), tlsdesc_got_offset_(-1U), free_list_()
72 this->init(symtab, layout);
74 // Initialize the free list and reserve the first entry.
75 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
76 this->free_list_.remove(0, plt_entry_size);
79 // Initialize the PLT section.
81 init(Symbol_table* symtab, Layout* layout);
83 // Add an entry to the PLT.
85 add_entry(Symbol* gsym);
87 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
89 add_local_ifunc_entry(Sized_relobj_file<64, false>* relobj,
90 unsigned int local_sym_index);
92 // Add the relocation for a PLT entry.
94 add_relocation(Symbol* gsym, unsigned int got_offset);
96 // Add the reserved TLSDESC_PLT entry to the PLT.
98 reserve_tlsdesc_entry(unsigned int got_offset)
99 { this->tlsdesc_got_offset_ = got_offset; }
101 // Return true if a TLSDESC_PLT entry has been reserved.
103 has_tlsdesc_entry() const
104 { return this->tlsdesc_got_offset_ != -1U; }
106 // Return the GOT offset for the reserved TLSDESC_PLT entry.
108 get_tlsdesc_got_offset() const
109 { return this->tlsdesc_got_offset_; }
111 // Return the offset of the reserved TLSDESC_PLT entry.
113 get_tlsdesc_plt_offset() const
114 { return (this->count_ + 1) * plt_entry_size; }
116 // Return the .rela.plt section data.
119 { return this->rel_; }
121 // Return where the TLSDESC relocations should go.
123 rela_tlsdesc(Layout*);
125 // Return the number of PLT entries.
128 { return this->count_; }
130 // Return the offset of the first non-reserved PLT entry.
132 first_plt_entry_offset()
133 { return plt_entry_size; }
135 // Return the size of a PLT entry.
138 { return plt_entry_size; }
140 // Reserve a slot in the PLT for an existing symbol in an incremental update.
142 reserve_slot(unsigned int plt_index)
144 this->free_list_.remove((plt_index + 1) * plt_entry_size,
145 (plt_index + 2) * plt_entry_size);
150 do_adjust_output_section(Output_section* os);
152 // Write to a map file.
154 do_print_to_mapfile(Mapfile* mapfile) const
155 { mapfile->print_output_data(this, _("** PLT")); }
158 // The size of an entry in the PLT.
159 static const int plt_entry_size = 16;
161 // The first entry in the PLT.
162 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
163 // procedure linkage table for both programs and shared objects."
164 static const unsigned char first_plt_entry[plt_entry_size];
166 // Other entries in the PLT for an executable.
167 static const unsigned char plt_entry[plt_entry_size];
169 // The reserved TLSDESC entry in the PLT for an executable.
170 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
172 // The .eh_frame unwind information for the PLT.
173 static const int plt_eh_frame_cie_size = 16;
174 static const int plt_eh_frame_fde_size = 32;
175 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
176 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
178 // Set the final size.
180 set_final_data_size();
182 // Write out the PLT data.
184 do_write(Output_file*);
186 // The reloc section.
188 // The TLSDESC relocs, if necessary. These must follow the regular
190 Reloc_section* tlsdesc_rel_;
192 Output_data_got<64, false>* got_;
193 // The .got.plt section.
194 Output_data_space* got_plt_;
195 // The number of PLT entries.
197 // Offset of the reserved TLSDESC_GOT entry when needed.
198 unsigned int tlsdesc_got_offset_;
199 // List of available regions within the section, for incremental
201 Free_list free_list_;
204 // The x86_64 target class.
206 // http://www.x86-64.org/documentation/abi.pdf
207 // TLS info comes from
208 // http://people.redhat.com/drepper/tls.pdf
209 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
211 class Target_x86_64 : public Sized_target<64, false>
214 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
215 // uses only Elf64_Rela relocation entries with explicit addends."
216 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
219 : Sized_target<64, false>(&x86_64_info),
220 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
221 global_offset_table_(NULL), rela_dyn_(NULL),
222 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
223 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
224 tls_base_symbol_defined_(false)
227 // Hook for a new output section.
229 do_new_output_section(Output_section*) const;
231 // Scan the relocations to look for symbol adjustments.
233 gc_process_relocs(Symbol_table* symtab,
235 Sized_relobj_file<64, false>* object,
236 unsigned int data_shndx,
237 unsigned int sh_type,
238 const unsigned char* prelocs,
240 Output_section* output_section,
241 bool needs_special_offset_handling,
242 size_t local_symbol_count,
243 const unsigned char* plocal_symbols);
245 // Scan the relocations to look for symbol adjustments.
247 scan_relocs(Symbol_table* symtab,
249 Sized_relobj_file<64, false>* object,
250 unsigned int data_shndx,
251 unsigned int sh_type,
252 const unsigned char* prelocs,
254 Output_section* output_section,
255 bool needs_special_offset_handling,
256 size_t local_symbol_count,
257 const unsigned char* plocal_symbols);
259 // Finalize the sections.
261 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
263 // Return the value to use for a dynamic which requires special
266 do_dynsym_value(const Symbol*) const;
268 // Relocate a section.
270 relocate_section(const Relocate_info<64, false>*,
271 unsigned int sh_type,
272 const unsigned char* prelocs,
274 Output_section* output_section,
275 bool needs_special_offset_handling,
277 elfcpp::Elf_types<64>::Elf_Addr view_address,
278 section_size_type view_size,
279 const Reloc_symbol_changes*);
281 // Scan the relocs during a relocatable link.
283 scan_relocatable_relocs(Symbol_table* symtab,
285 Sized_relobj_file<64, false>* object,
286 unsigned int data_shndx,
287 unsigned int sh_type,
288 const unsigned char* prelocs,
290 Output_section* output_section,
291 bool needs_special_offset_handling,
292 size_t local_symbol_count,
293 const unsigned char* plocal_symbols,
294 Relocatable_relocs*);
296 // Relocate a section during a relocatable link.
298 relocate_for_relocatable(const Relocate_info<64, false>*,
299 unsigned int sh_type,
300 const unsigned char* prelocs,
302 Output_section* output_section,
303 off_t offset_in_output_section,
304 const Relocatable_relocs*,
306 elfcpp::Elf_types<64>::Elf_Addr view_address,
307 section_size_type view_size,
308 unsigned char* reloc_view,
309 section_size_type reloc_view_size);
311 // Return a string used to fill a code section with nops.
313 do_code_fill(section_size_type length) const;
315 // Return whether SYM is defined by the ABI.
317 do_is_defined_by_abi(const Symbol* sym) const
318 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
320 // Return the symbol index to use for a target specific relocation.
321 // The only target specific relocation is R_X86_64_TLSDESC for a
322 // local symbol, which is an absolute reloc.
324 do_reloc_symbol_index(void*, unsigned int r_type) const
326 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
330 // Return the addend to use for a target specific relocation.
332 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
334 // Return the PLT section.
336 do_plt_section_for_global(const Symbol*) const
337 { return this->plt_section(); }
340 do_plt_section_for_local(const Relobj*, unsigned int) const
341 { return this->plt_section(); }
343 // This function should be defined in targets that can use relocation
344 // types to determine (implemented in local_reloc_may_be_function_pointer
345 // and global_reloc_may_be_function_pointer)
346 // if a function's pointer is taken. ICF uses this in safe mode to only
347 // fold those functions whose pointer is defintely not taken. For x86_64
348 // pie binaries, safe ICF cannot be done by looking at relocation types.
350 do_can_check_for_function_pointers() const
351 { return !parameters->options().pie(); }
353 // Adjust -fsplit-stack code which calls non-split-stack code.
355 do_calls_non_split(Relobj* object, unsigned int shndx,
356 section_offset_type fnoffset, section_size_type fnsize,
357 unsigned char* view, section_size_type view_size,
358 std::string* from, std::string* to) const;
360 // Return the size of the GOT section.
364 gold_assert(this->got_ != NULL);
365 return this->got_->data_size();
368 // Return the number of entries in the GOT.
370 got_entry_count() const
372 if (this->got_ == NULL)
374 return this->got_size() / 8;
377 // Return the number of entries in the PLT.
379 plt_entry_count() const;
381 // Return the offset of the first non-reserved PLT entry.
383 first_plt_entry_offset() const;
385 // Return the size of each PLT entry.
387 plt_entry_size() const;
389 // Create the GOT section for an incremental update.
390 Output_data_got<64, false>*
391 init_got_plt_for_update(Symbol_table* symtab,
393 unsigned int got_count,
394 unsigned int plt_count);
396 // Reserve a GOT entry for a local symbol, and regenerate any
397 // necessary dynamic relocations.
399 reserve_local_got_entry(unsigned int got_index,
400 Sized_relobj<64, false>* obj,
402 unsigned int got_type);
404 // Reserve a GOT entry for a global symbol, and regenerate any
405 // necessary dynamic relocations.
407 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
408 unsigned int got_type);
410 // Register an existing PLT entry for a global symbol.
412 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
414 // Force a COPY relocation for a given symbol.
416 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
418 // Apply an incremental relocation.
420 apply_relocation(const Relocate_info<64, false>* relinfo,
421 elfcpp::Elf_types<64>::Elf_Addr r_offset,
423 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
426 elfcpp::Elf_types<64>::Elf_Addr address,
427 section_size_type view_size);
429 // Add a new reloc argument, returning the index in the vector.
431 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
433 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
434 return this->tlsdesc_reloc_info_.size() - 1;
438 // The class which scans relocations.
443 : issued_non_pic_error_(false)
447 get_reference_flags(unsigned int r_type);
450 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
451 Sized_relobj_file<64, false>* object,
452 unsigned int data_shndx,
453 Output_section* output_section,
454 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
455 const elfcpp::Sym<64, false>& lsym);
458 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
459 Sized_relobj_file<64, false>* object,
460 unsigned int data_shndx,
461 Output_section* output_section,
462 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
466 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
467 Target_x86_64* target,
468 Sized_relobj_file<64, false>* object,
469 unsigned int data_shndx,
470 Output_section* output_section,
471 const elfcpp::Rela<64, false>& reloc,
473 const elfcpp::Sym<64, false>& lsym);
476 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
477 Target_x86_64* target,
478 Sized_relobj_file<64, false>* object,
479 unsigned int data_shndx,
480 Output_section* output_section,
481 const elfcpp::Rela<64, false>& reloc,
487 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
490 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
494 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
497 possible_function_pointer_reloc(unsigned int r_type);
500 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
501 unsigned int r_type);
503 // Whether we have issued an error about a non-PIC compilation.
504 bool issued_non_pic_error_;
507 // The class which implements relocation.
512 : skip_call_tls_get_addr_(false)
517 if (this->skip_call_tls_get_addr_)
519 // FIXME: This needs to specify the location somehow.
520 gold_error(_("missing expected TLS relocation"));
524 // Do a relocation. Return false if the caller should not issue
525 // any warnings about this relocation.
527 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
528 size_t relnum, const elfcpp::Rela<64, false>&,
529 unsigned int r_type, const Sized_symbol<64>*,
530 const Symbol_value<64>*,
531 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
535 // Do a TLS relocation.
537 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
538 size_t relnum, const elfcpp::Rela<64, false>&,
539 unsigned int r_type, const Sized_symbol<64>*,
540 const Symbol_value<64>*,
541 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
544 // Do a TLS General-Dynamic to Initial-Exec transition.
546 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
547 Output_segment* tls_segment,
548 const elfcpp::Rela<64, false>&, unsigned int r_type,
549 elfcpp::Elf_types<64>::Elf_Addr value,
551 elfcpp::Elf_types<64>::Elf_Addr,
552 section_size_type view_size);
554 // Do a TLS General-Dynamic to Local-Exec transition.
556 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
557 Output_segment* tls_segment,
558 const elfcpp::Rela<64, false>&, unsigned int r_type,
559 elfcpp::Elf_types<64>::Elf_Addr value,
561 section_size_type view_size);
563 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
565 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
566 Output_segment* tls_segment,
567 const elfcpp::Rela<64, false>&, unsigned int r_type,
568 elfcpp::Elf_types<64>::Elf_Addr value,
570 elfcpp::Elf_types<64>::Elf_Addr,
571 section_size_type view_size);
573 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
575 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
576 Output_segment* tls_segment,
577 const elfcpp::Rela<64, false>&, unsigned int r_type,
578 elfcpp::Elf_types<64>::Elf_Addr value,
580 section_size_type view_size);
582 // Do a TLS Local-Dynamic to Local-Exec transition.
584 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
585 Output_segment* tls_segment,
586 const elfcpp::Rela<64, false>&, unsigned int r_type,
587 elfcpp::Elf_types<64>::Elf_Addr value,
589 section_size_type view_size);
591 // Do a TLS Initial-Exec to Local-Exec transition.
593 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
594 Output_segment* tls_segment,
595 const elfcpp::Rela<64, false>&, unsigned int r_type,
596 elfcpp::Elf_types<64>::Elf_Addr value,
598 section_size_type view_size);
600 // This is set if we should skip the next reloc, which should be a
601 // PLT32 reloc against ___tls_get_addr.
602 bool skip_call_tls_get_addr_;
605 // A class which returns the size required for a relocation type,
606 // used while scanning relocs during a relocatable link.
607 class Relocatable_size_for_reloc
611 get_size_for_reloc(unsigned int, Relobj*);
614 // Adjust TLS relocation type based on the options and whether this
615 // is a local symbol.
616 static tls::Tls_optimization
617 optimize_tls_reloc(bool is_final, int r_type);
619 // Get the GOT section, creating it if necessary.
620 Output_data_got<64, false>*
621 got_section(Symbol_table*, Layout*);
623 // Get the GOT PLT section.
625 got_plt_section() const
627 gold_assert(this->got_plt_ != NULL);
628 return this->got_plt_;
631 // Get the GOT section for TLSDESC entries.
632 Output_data_got<64, false>*
633 got_tlsdesc_section() const
635 gold_assert(this->got_tlsdesc_ != NULL);
636 return this->got_tlsdesc_;
639 // Create the PLT section.
641 make_plt_section(Symbol_table* symtab, Layout* layout);
643 // Create a PLT entry for a global symbol.
645 make_plt_entry(Symbol_table*, Layout*, Symbol*);
647 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
649 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
650 Sized_relobj_file<64, false>* relobj,
651 unsigned int local_sym_index);
653 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
655 define_tls_base_symbol(Symbol_table*, Layout*);
657 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
659 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
661 // Create a GOT entry for the TLS module index.
663 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
664 Sized_relobj_file<64, false>* object);
666 // Get the PLT section.
667 Output_data_plt_x86_64*
670 gold_assert(this->plt_ != NULL);
674 // Get the dynamic reloc section, creating it if necessary.
676 rela_dyn_section(Layout*);
678 // Get the section to use for TLSDESC relocations.
680 rela_tlsdesc_section(Layout*) const;
682 // Add a potential copy relocation.
684 copy_reloc(Symbol_table* symtab, Layout* layout,
685 Sized_relobj_file<64, false>* object,
686 unsigned int shndx, Output_section* output_section,
687 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
689 this->copy_relocs_.copy_reloc(symtab, layout,
690 symtab->get_sized_symbol<64>(sym),
691 object, shndx, output_section,
692 reloc, this->rela_dyn_section(layout));
695 // Information about this specific target which we pass to the
696 // general Target structure.
697 static const Target::Target_info x86_64_info;
699 // The types of GOT entries needed for this platform.
700 // These values are exposed to the ABI in an incremental link.
701 // Do not renumber existing values without changing the version
702 // number of the .gnu_incremental_inputs section.
705 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
706 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
707 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
708 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
711 // This type is used as the argument to the target specific
712 // relocation routines. The only target specific reloc is
713 // R_X86_64_TLSDESC against a local symbol.
716 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
717 : object(a_object), r_sym(a_r_sym)
720 // The object in which the local symbol is defined.
721 Sized_relobj_file<64, false>* object;
722 // The local symbol index in the object.
727 Output_data_got<64, false>* got_;
729 Output_data_plt_x86_64* plt_;
730 // The GOT PLT section.
731 Output_data_space* got_plt_;
732 // The GOT section for TLSDESC relocations.
733 Output_data_got<64, false>* got_tlsdesc_;
734 // The _GLOBAL_OFFSET_TABLE_ symbol.
735 Symbol* global_offset_table_;
736 // The dynamic reloc section.
737 Reloc_section* rela_dyn_;
738 // Relocs saved to avoid a COPY reloc.
739 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
740 // Space for variables copied with a COPY reloc.
741 Output_data_space* dynbss_;
742 // Offset of the GOT entry for the TLS module index.
743 unsigned int got_mod_index_offset_;
744 // We handle R_X86_64_TLSDESC against a local symbol as a target
745 // specific relocation. Here we store the object and local symbol
746 // index for the relocation.
747 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
748 // True if the _TLS_MODULE_BASE_ symbol has been defined.
749 bool tls_base_symbol_defined_;
752 const Target::Target_info Target_x86_64::x86_64_info =
755 false, // is_big_endian
756 elfcpp::EM_X86_64, // machine_code
757 false, // has_make_symbol
758 false, // has_resolve
759 true, // has_code_fill
760 true, // is_default_stack_executable
761 true, // can_icf_inline_merge_sections
763 "/lib/ld64.so.1", // program interpreter
764 0x400000, // default_text_segment_address
765 0x1000, // abi_pagesize (overridable by -z max-page-size)
766 0x1000, // common_pagesize (overridable by -z common-page-size)
767 elfcpp::SHN_UNDEF, // small_common_shndx
768 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
769 0, // small_common_section_flags
770 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
771 NULL, // attributes_section
772 NULL // attributes_vendor
775 // This is called when a new output section is created. This is where
776 // we handle the SHF_X86_64_LARGE.
779 Target_x86_64::do_new_output_section(Output_section* os) const
781 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
782 os->set_is_large_section();
785 // Get the GOT section, creating it if necessary.
787 Output_data_got<64, false>*
788 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
790 if (this->got_ == NULL)
792 gold_assert(symtab != NULL && layout != NULL);
794 this->got_ = new Output_data_got<64, false>();
796 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
798 | elfcpp::SHF_WRITE),
799 this->got_, ORDER_RELRO_LAST,
802 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
803 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
805 | elfcpp::SHF_WRITE),
806 this->got_plt_, ORDER_NON_RELRO_FIRST,
809 // The first three entries are reserved.
810 this->got_plt_->set_current_data_size(3 * 8);
812 // Those bytes can go into the relro segment.
813 layout->increase_relro(3 * 8);
815 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
816 this->global_offset_table_ =
817 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
818 Symbol_table::PREDEFINED,
820 0, 0, elfcpp::STT_OBJECT,
822 elfcpp::STV_HIDDEN, 0,
825 // If there are any TLSDESC relocations, they get GOT entries in
826 // .got.plt after the jump slot entries.
827 this->got_tlsdesc_ = new Output_data_got<64, false>();
828 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
830 | elfcpp::SHF_WRITE),
832 ORDER_NON_RELRO_FIRST, false);
838 // Get the dynamic reloc section, creating it if necessary.
840 Target_x86_64::Reloc_section*
841 Target_x86_64::rela_dyn_section(Layout* layout)
843 if (this->rela_dyn_ == NULL)
845 gold_assert(layout != NULL);
846 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
847 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
848 elfcpp::SHF_ALLOC, this->rela_dyn_,
849 ORDER_DYNAMIC_RELOCS, false);
851 return this->rela_dyn_;
854 // Initialize the PLT section.
857 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
859 this->rel_ = new Reloc_section(false);
860 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
861 elfcpp::SHF_ALLOC, this->rel_,
862 ORDER_DYNAMIC_PLT_RELOCS, false);
864 if (parameters->doing_static_link())
866 // A statically linked executable will only have a .rela.plt
867 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
868 // symbols. The library will use these symbols to locate the
869 // IRELATIVE relocs at program startup time.
870 symtab->define_in_output_data("__rela_iplt_start", NULL,
871 Symbol_table::PREDEFINED,
872 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
873 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
875 symtab->define_in_output_data("__rela_iplt_end", NULL,
876 Symbol_table::PREDEFINED,
877 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
878 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
882 // Add unwind information if requested.
883 if (parameters->options().ld_generated_unwind_info())
884 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
885 plt_eh_frame_fde, plt_eh_frame_fde_size);
889 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
891 os->set_entsize(plt_entry_size);
894 // Add an entry to the PLT.
897 Output_data_plt_x86_64::add_entry(Symbol* gsym)
899 gold_assert(!gsym->has_plt_offset());
901 unsigned int plt_index;
903 section_offset_type got_offset;
905 if (!this->is_data_size_valid())
907 // Note that when setting the PLT offset we skip the initial
908 // reserved PLT entry.
909 plt_index = this->count_ + 1;
910 plt_offset = plt_index * plt_entry_size;
914 got_offset = (plt_index - 1 + 3) * 8;
915 gold_assert(got_offset == this->got_plt_->current_data_size());
917 // Every PLT entry needs a GOT entry which points back to the PLT
918 // entry (this will be changed by the dynamic linker, normally
919 // lazily when the function is called).
920 this->got_plt_->set_current_data_size(got_offset + 8);
924 // For incremental updates, find an available slot.
925 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
926 if (plt_offset == -1)
927 gold_fallback(_("out of patch space (PLT);"
928 " relink with --incremental-full"));
930 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
931 // can be calculated from the PLT index, adjusting for the three
932 // reserved entries at the beginning of the GOT.
933 plt_index = plt_offset / plt_entry_size - 1;
934 got_offset = (plt_index - 1 + 3) * 8;
937 gsym->set_plt_offset(plt_offset);
939 // Every PLT entry needs a reloc.
940 this->add_relocation(gsym, got_offset);
942 // Note that we don't need to save the symbol. The contents of the
943 // PLT are independent of which symbols are used. The symbols only
944 // appear in the relocations.
947 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
951 Output_data_plt_x86_64::add_local_ifunc_entry(
952 Sized_relobj_file<64, false>* relobj,
953 unsigned int local_sym_index)
955 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
958 section_offset_type got_offset = this->got_plt_->current_data_size();
960 // Every PLT entry needs a GOT entry which points back to the PLT
962 this->got_plt_->set_current_data_size(got_offset + 8);
964 // Every PLT entry needs a reloc.
965 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
966 elfcpp::R_X86_64_IRELATIVE,
967 this->got_plt_, got_offset, 0);
972 // Add the relocation for a PLT entry.
975 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
977 if (gsym->type() == elfcpp::STT_GNU_IFUNC
978 && gsym->can_use_relative_reloc(false))
979 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
980 this->got_plt_, got_offset, 0);
983 gsym->set_needs_dynsym_entry();
984 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
989 // Return where the TLSDESC relocations should go, creating it if
990 // necessary. These follow the JUMP_SLOT relocations.
992 Output_data_plt_x86_64::Reloc_section*
993 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
995 if (this->tlsdesc_rel_ == NULL)
997 this->tlsdesc_rel_ = new Reloc_section(false);
998 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
999 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1000 ORDER_DYNAMIC_PLT_RELOCS, false);
1001 gold_assert(this->tlsdesc_rel_->output_section() ==
1002 this->rel_->output_section());
1004 return this->tlsdesc_rel_;
1007 // Set the final size.
1009 Output_data_plt_x86_64::set_final_data_size()
1011 unsigned int count = this->count_;
1012 if (this->has_tlsdesc_entry())
1014 this->set_data_size((count + 1) * plt_entry_size);
1017 // The first entry in the PLT for an executable.
1019 const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1021 // From AMD64 ABI Draft 0.98, page 76
1022 0xff, 0x35, // pushq contents of memory address
1023 0, 0, 0, 0, // replaced with address of .got + 8
1024 0xff, 0x25, // jmp indirect
1025 0, 0, 0, 0, // replaced with address of .got + 16
1026 0x90, 0x90, 0x90, 0x90 // noop (x4)
1029 // Subsequent entries in the PLT for an executable.
1031 const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1033 // From AMD64 ABI Draft 0.98, page 76
1034 0xff, 0x25, // jmpq indirect
1035 0, 0, 0, 0, // replaced with address of symbol in .got
1036 0x68, // pushq immediate
1037 0, 0, 0, 0, // replaced with offset into relocation table
1038 0xe9, // jmpq relative
1039 0, 0, 0, 0 // replaced with offset to start of .plt
1042 // The reserved TLSDESC entry in the PLT for an executable.
1044 const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1046 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1047 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1048 0xff, 0x35, // pushq x(%rip)
1049 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1050 0xff, 0x25, // jmpq *y(%rip)
1051 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1056 // The .eh_frame unwind information for the PLT.
1059 Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1062 'z', // Augmentation: augmentation size included.
1063 'R', // Augmentation: FDE encoding included.
1064 '\0', // End of augmentation string.
1065 1, // Code alignment factor.
1066 0x78, // Data alignment factor.
1067 16, // Return address column.
1068 1, // Augmentation size.
1069 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1070 | elfcpp::DW_EH_PE_sdata4),
1071 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1072 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1073 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1078 Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1080 0, 0, 0, 0, // Replaced with offset to .plt.
1081 0, 0, 0, 0, // Replaced with size of .plt.
1082 0, // Augmentation size.
1083 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1084 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1085 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1086 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1087 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1088 11, // Block length.
1089 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1090 elfcpp::DW_OP_breg16, 0, // Push %rip.
1091 elfcpp::DW_OP_lit15, // Push 0xf.
1092 elfcpp::DW_OP_and, // & (%rip & 0xf).
1093 elfcpp::DW_OP_lit11, // Push 0xb.
1094 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1095 elfcpp::DW_OP_lit3, // Push 3.
1096 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1097 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1098 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1104 // Write out the PLT. This uses the hand-coded instructions above,
1105 // and adjusts them as needed. This is specified by the AMD64 ABI.
1108 Output_data_plt_x86_64::do_write(Output_file* of)
1110 const off_t offset = this->offset();
1111 const section_size_type oview_size =
1112 convert_to_section_size_type(this->data_size());
1113 unsigned char* const oview = of->get_output_view(offset, oview_size);
1115 const off_t got_file_offset = this->got_plt_->offset();
1116 const section_size_type got_size =
1117 convert_to_section_size_type(this->got_plt_->data_size());
1118 unsigned char* const got_view = of->get_output_view(got_file_offset,
1121 unsigned char* pov = oview;
1123 // The base address of the .plt section.
1124 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1125 // The base address of the .got section.
1126 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1127 // The base address of the PLT portion of the .got section,
1128 // which is where the GOT pointer will point, and where the
1129 // three reserved GOT entries are located.
1130 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1132 memcpy(pov, first_plt_entry, plt_entry_size);
1133 // We do a jmp relative to the PC at the end of this instruction.
1134 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1136 - (plt_address + 6)));
1137 elfcpp::Swap<32, false>::writeval(pov + 8,
1139 - (plt_address + 12)));
1140 pov += plt_entry_size;
1142 unsigned char* got_pov = got_view;
1144 memset(got_pov, 0, 24);
1147 unsigned int plt_offset = plt_entry_size;
1148 unsigned int got_offset = 24;
1149 const unsigned int count = this->count_;
1150 for (unsigned int plt_index = 0;
1153 pov += plt_entry_size,
1155 plt_offset += plt_entry_size,
1158 // Set and adjust the PLT entry itself.
1159 memcpy(pov, plt_entry, plt_entry_size);
1160 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1161 (got_address + got_offset
1162 - (plt_address + plt_offset
1165 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1166 elfcpp::Swap<32, false>::writeval(pov + 12,
1167 - (plt_offset + plt_entry_size));
1169 // Set the entry in the GOT.
1170 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1173 if (this->has_tlsdesc_entry())
1175 // Set and adjust the reserved TLSDESC PLT entry.
1176 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1177 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1178 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1180 - (plt_address + plt_offset
1182 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1184 + tlsdesc_got_offset
1185 - (plt_address + plt_offset
1187 pov += plt_entry_size;
1190 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1191 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1193 of->write_output_view(offset, oview_size, oview);
1194 of->write_output_view(got_file_offset, got_size, got_view);
1197 // Create the PLT section.
1200 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1202 if (this->plt_ == NULL)
1204 // Create the GOT sections first.
1205 this->got_section(symtab, layout);
1207 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1209 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1211 | elfcpp::SHF_EXECINSTR),
1212 this->plt_, ORDER_PLT, false);
1214 // Make the sh_info field of .rela.plt point to .plt.
1215 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1216 rela_plt_os->set_info_section(this->plt_->output_section());
1220 // Return the section for TLSDESC relocations.
1222 Target_x86_64::Reloc_section*
1223 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1225 return this->plt_section()->rela_tlsdesc(layout);
1228 // Create a PLT entry for a global symbol.
1231 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1234 if (gsym->has_plt_offset())
1237 if (this->plt_ == NULL)
1238 this->make_plt_section(symtab, layout);
1240 this->plt_->add_entry(gsym);
1243 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1246 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1247 Sized_relobj_file<64, false>* relobj,
1248 unsigned int local_sym_index)
1250 if (relobj->local_has_plt_offset(local_sym_index))
1252 if (this->plt_ == NULL)
1253 this->make_plt_section(symtab, layout);
1254 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1256 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1259 // Return the number of entries in the PLT.
1262 Target_x86_64::plt_entry_count() const
1264 if (this->plt_ == NULL)
1266 return this->plt_->entry_count();
1269 // Return the offset of the first non-reserved PLT entry.
1272 Target_x86_64::first_plt_entry_offset() const
1274 return Output_data_plt_x86_64::first_plt_entry_offset();
1277 // Return the size of each PLT entry.
1280 Target_x86_64::plt_entry_size() const
1282 return Output_data_plt_x86_64::get_plt_entry_size();
1285 // Create the GOT and PLT sections for an incremental update.
1287 Output_data_got<64, false>*
1288 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1290 unsigned int got_count,
1291 unsigned int plt_count)
1293 gold_assert(this->got_ == NULL);
1295 this->got_ = new Output_data_got<64, false>(got_count * 8);
1296 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1298 | elfcpp::SHF_WRITE),
1299 this->got_, ORDER_RELRO_LAST,
1302 // Add the three reserved entries.
1303 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1304 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1306 | elfcpp::SHF_WRITE),
1307 this->got_plt_, ORDER_NON_RELRO_FIRST,
1310 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1311 this->global_offset_table_ =
1312 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1313 Symbol_table::PREDEFINED,
1315 0, 0, elfcpp::STT_OBJECT,
1317 elfcpp::STV_HIDDEN, 0,
1320 // If there are any TLSDESC relocations, they get GOT entries in
1321 // .got.plt after the jump slot entries.
1322 // FIXME: Get the count for TLSDESC entries.
1323 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1324 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1325 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1327 ORDER_NON_RELRO_FIRST, false);
1329 // Create the PLT section.
1330 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1331 this->got_plt_, plt_count);
1332 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1333 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1334 this->plt_, ORDER_PLT, false);
1336 // Make the sh_info field of .rela.plt point to .plt.
1337 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1338 rela_plt_os->set_info_section(this->plt_->output_section());
1340 // Create the rela_dyn section.
1341 this->rela_dyn_section(layout);
1346 // Reserve a GOT entry for a local symbol, and regenerate any
1347 // necessary dynamic relocations.
1350 Target_x86_64::reserve_local_got_entry(
1351 unsigned int got_index,
1352 Sized_relobj<64, false>* obj,
1354 unsigned int got_type)
1356 unsigned int got_offset = got_index * 8;
1357 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1359 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1362 case GOT_TYPE_STANDARD:
1363 if (parameters->options().output_is_position_independent())
1364 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1365 this->got_, got_offset, 0);
1367 case GOT_TYPE_TLS_OFFSET:
1368 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1369 this->got_, got_offset, 0);
1371 case GOT_TYPE_TLS_PAIR:
1372 this->got_->reserve_slot(got_index + 1);
1373 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1374 this->got_, got_offset, 0);
1376 case GOT_TYPE_TLS_DESC:
1377 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1378 // this->got_->reserve_slot(got_index + 1);
1379 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1380 // this->got_, got_offset, 0);
1387 // Reserve a GOT entry for a global symbol, and regenerate any
1388 // necessary dynamic relocations.
1391 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1392 unsigned int got_type)
1394 unsigned int got_offset = got_index * 8;
1395 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1397 this->got_->reserve_global(got_index, gsym, got_type);
1400 case GOT_TYPE_STANDARD:
1401 if (!gsym->final_value_is_known())
1403 if (gsym->is_from_dynobj()
1404 || gsym->is_undefined()
1405 || gsym->is_preemptible()
1406 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1407 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1408 this->got_, got_offset, 0);
1410 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1411 this->got_, got_offset, 0);
1414 case GOT_TYPE_TLS_OFFSET:
1415 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1416 this->got_, got_offset, 0);
1418 case GOT_TYPE_TLS_PAIR:
1419 this->got_->reserve_slot(got_index + 1);
1420 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1421 this->got_, got_offset, 0);
1422 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1423 this->got_, got_offset + 8, 0);
1425 case GOT_TYPE_TLS_DESC:
1426 this->got_->reserve_slot(got_index + 1);
1427 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1428 this->got_, got_offset, 0);
1435 // Register an existing PLT entry for a global symbol.
1438 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1441 gold_assert(this->plt_ != NULL);
1442 gold_assert(!gsym->has_plt_offset());
1444 this->plt_->reserve_slot(plt_index);
1446 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1448 unsigned int got_offset = (plt_index + 3) * 8;
1449 this->plt_->add_relocation(gsym, got_offset);
1452 // Force a COPY relocation for a given symbol.
1455 Target_x86_64::emit_copy_reloc(
1456 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1458 this->copy_relocs_.emit_copy_reloc(symtab,
1459 symtab->get_sized_symbol<64>(sym),
1462 this->rela_dyn_section(NULL));
1465 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1468 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1470 if (this->tls_base_symbol_defined_)
1473 Output_segment* tls_segment = layout->tls_segment();
1474 if (tls_segment != NULL)
1476 bool is_exec = parameters->options().output_is_executable();
1477 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1478 Symbol_table::PREDEFINED,
1482 elfcpp::STV_HIDDEN, 0,
1484 ? Symbol::SEGMENT_END
1485 : Symbol::SEGMENT_START),
1488 this->tls_base_symbol_defined_ = true;
1491 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1494 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1497 if (this->plt_ == NULL)
1498 this->make_plt_section(symtab, layout);
1500 if (!this->plt_->has_tlsdesc_entry())
1502 // Allocate the TLSDESC_GOT entry.
1503 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1504 unsigned int got_offset = got->add_constant(0);
1506 // Allocate the TLSDESC_PLT entry.
1507 this->plt_->reserve_tlsdesc_entry(got_offset);
1511 // Create a GOT entry for the TLS module index.
1514 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1515 Sized_relobj_file<64, false>* object)
1517 if (this->got_mod_index_offset_ == -1U)
1519 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1520 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1521 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1522 unsigned int got_offset = got->add_constant(0);
1523 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1525 got->add_constant(0);
1526 this->got_mod_index_offset_ = got_offset;
1528 return this->got_mod_index_offset_;
1531 // Optimize the TLS relocation type based on what we know about the
1532 // symbol. IS_FINAL is true if the final address of this symbol is
1533 // known at link time.
1535 tls::Tls_optimization
1536 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1538 // If we are generating a shared library, then we can't do anything
1540 if (parameters->options().shared())
1541 return tls::TLSOPT_NONE;
1545 case elfcpp::R_X86_64_TLSGD:
1546 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1547 case elfcpp::R_X86_64_TLSDESC_CALL:
1548 // These are General-Dynamic which permits fully general TLS
1549 // access. Since we know that we are generating an executable,
1550 // we can convert this to Initial-Exec. If we also know that
1551 // this is a local symbol, we can further switch to Local-Exec.
1553 return tls::TLSOPT_TO_LE;
1554 return tls::TLSOPT_TO_IE;
1556 case elfcpp::R_X86_64_TLSLD:
1557 // This is Local-Dynamic, which refers to a local symbol in the
1558 // dynamic TLS block. Since we know that we generating an
1559 // executable, we can switch to Local-Exec.
1560 return tls::TLSOPT_TO_LE;
1562 case elfcpp::R_X86_64_DTPOFF32:
1563 case elfcpp::R_X86_64_DTPOFF64:
1564 // Another Local-Dynamic reloc.
1565 return tls::TLSOPT_TO_LE;
1567 case elfcpp::R_X86_64_GOTTPOFF:
1568 // These are Initial-Exec relocs which get the thread offset
1569 // from the GOT. If we know that we are linking against the
1570 // local symbol, we can switch to Local-Exec, which links the
1571 // thread offset into the instruction.
1573 return tls::TLSOPT_TO_LE;
1574 return tls::TLSOPT_NONE;
1576 case elfcpp::R_X86_64_TPOFF32:
1577 // When we already have Local-Exec, there is nothing further we
1579 return tls::TLSOPT_NONE;
1586 // Get the Reference_flags for a particular relocation.
1589 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1593 case elfcpp::R_X86_64_NONE:
1594 case elfcpp::R_X86_64_GNU_VTINHERIT:
1595 case elfcpp::R_X86_64_GNU_VTENTRY:
1596 case elfcpp::R_X86_64_GOTPC32:
1597 case elfcpp::R_X86_64_GOTPC64:
1598 // No symbol reference.
1601 case elfcpp::R_X86_64_64:
1602 case elfcpp::R_X86_64_32:
1603 case elfcpp::R_X86_64_32S:
1604 case elfcpp::R_X86_64_16:
1605 case elfcpp::R_X86_64_8:
1606 return Symbol::ABSOLUTE_REF;
1608 case elfcpp::R_X86_64_PC64:
1609 case elfcpp::R_X86_64_PC32:
1610 case elfcpp::R_X86_64_PC16:
1611 case elfcpp::R_X86_64_PC8:
1612 case elfcpp::R_X86_64_GOTOFF64:
1613 return Symbol::RELATIVE_REF;
1615 case elfcpp::R_X86_64_PLT32:
1616 case elfcpp::R_X86_64_PLTOFF64:
1617 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1619 case elfcpp::R_X86_64_GOT64:
1620 case elfcpp::R_X86_64_GOT32:
1621 case elfcpp::R_X86_64_GOTPCREL64:
1622 case elfcpp::R_X86_64_GOTPCREL:
1623 case elfcpp::R_X86_64_GOTPLT64:
1625 return Symbol::ABSOLUTE_REF;
1627 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1628 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1629 case elfcpp::R_X86_64_TLSDESC_CALL:
1630 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1631 case elfcpp::R_X86_64_DTPOFF32:
1632 case elfcpp::R_X86_64_DTPOFF64:
1633 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1634 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1635 return Symbol::TLS_REF;
1637 case elfcpp::R_X86_64_COPY:
1638 case elfcpp::R_X86_64_GLOB_DAT:
1639 case elfcpp::R_X86_64_JUMP_SLOT:
1640 case elfcpp::R_X86_64_RELATIVE:
1641 case elfcpp::R_X86_64_IRELATIVE:
1642 case elfcpp::R_X86_64_TPOFF64:
1643 case elfcpp::R_X86_64_DTPMOD64:
1644 case elfcpp::R_X86_64_TLSDESC:
1645 case elfcpp::R_X86_64_SIZE32:
1646 case elfcpp::R_X86_64_SIZE64:
1648 // Not expected. We will give an error later.
1653 // Report an unsupported relocation against a local symbol.
1656 Target_x86_64::Scan::unsupported_reloc_local(
1657 Sized_relobj_file<64, false>* object,
1658 unsigned int r_type)
1660 gold_error(_("%s: unsupported reloc %u against local symbol"),
1661 object->name().c_str(), r_type);
1664 // We are about to emit a dynamic relocation of type R_TYPE. If the
1665 // dynamic linker does not support it, issue an error. The GNU linker
1666 // only issues a non-PIC error for an allocated read-only section.
1667 // Here we know the section is allocated, but we don't know that it is
1668 // read-only. But we check for all the relocation types which the
1669 // glibc dynamic linker supports, so it seems appropriate to issue an
1670 // error even if the section is not read-only. If GSYM is not NULL,
1671 // it is the symbol the relocation is against; if it is NULL, the
1672 // relocation is against a local symbol.
1675 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1680 // These are the relocation types supported by glibc for x86_64
1681 // which should always work.
1682 case elfcpp::R_X86_64_RELATIVE:
1683 case elfcpp::R_X86_64_IRELATIVE:
1684 case elfcpp::R_X86_64_GLOB_DAT:
1685 case elfcpp::R_X86_64_JUMP_SLOT:
1686 case elfcpp::R_X86_64_DTPMOD64:
1687 case elfcpp::R_X86_64_DTPOFF64:
1688 case elfcpp::R_X86_64_TPOFF64:
1689 case elfcpp::R_X86_64_64:
1690 case elfcpp::R_X86_64_COPY:
1693 // glibc supports these reloc types, but they can overflow.
1694 case elfcpp::R_X86_64_PC32:
1695 // A PC relative reference is OK against a local symbol or if
1696 // the symbol is defined locally.
1698 || (!gsym->is_from_dynobj()
1699 && !gsym->is_undefined()
1700 && !gsym->is_preemptible()))
1703 case elfcpp::R_X86_64_32:
1704 if (this->issued_non_pic_error_)
1706 gold_assert(parameters->options().output_is_position_independent());
1708 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1709 "overflow at runtime; recompile with -fPIC"));
1711 object->error(_("requires dynamic %s reloc against '%s' which may "
1712 "overflow at runtime; recompile with -fPIC"),
1713 (r_type == elfcpp::R_X86_64_32
1717 this->issued_non_pic_error_ = true;
1721 // This prevents us from issuing more than one error per reloc
1722 // section. But we can still wind up issuing more than one
1723 // error per object file.
1724 if (this->issued_non_pic_error_)
1726 gold_assert(parameters->options().output_is_position_independent());
1727 object->error(_("requires unsupported dynamic reloc %u; "
1728 "recompile with -fPIC"),
1730 this->issued_non_pic_error_ = true;
1733 case elfcpp::R_X86_64_NONE:
1738 // Return whether we need to make a PLT entry for a relocation of the
1739 // given type against a STT_GNU_IFUNC symbol.
1742 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1743 Sized_relobj_file<64, false>* object,
1744 unsigned int r_type)
1746 int flags = Scan::get_reference_flags(r_type);
1747 if (flags & Symbol::TLS_REF)
1748 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1749 object->name().c_str(), r_type);
1753 // Scan a relocation for a local symbol.
1756 Target_x86_64::Scan::local(Symbol_table* symtab,
1758 Target_x86_64* target,
1759 Sized_relobj_file<64, false>* object,
1760 unsigned int data_shndx,
1761 Output_section* output_section,
1762 const elfcpp::Rela<64, false>& reloc,
1763 unsigned int r_type,
1764 const elfcpp::Sym<64, false>& lsym)
1766 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1767 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1768 && this->reloc_needs_plt_for_ifunc(object, r_type))
1770 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1771 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1776 case elfcpp::R_X86_64_NONE:
1777 case elfcpp::R_X86_64_GNU_VTINHERIT:
1778 case elfcpp::R_X86_64_GNU_VTENTRY:
1781 case elfcpp::R_X86_64_64:
1782 // If building a shared library (or a position-independent
1783 // executable), we need to create a dynamic relocation for this
1784 // location. The relocation applied at link time will apply the
1785 // link-time value, so we flag the location with an
1786 // R_X86_64_RELATIVE relocation so the dynamic loader can
1787 // relocate it easily.
1788 if (parameters->options().output_is_position_independent())
1790 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1791 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1792 rela_dyn->add_local_relative(object, r_sym,
1793 elfcpp::R_X86_64_RELATIVE,
1794 output_section, data_shndx,
1795 reloc.get_r_offset(),
1796 reloc.get_r_addend());
1800 case elfcpp::R_X86_64_32:
1801 case elfcpp::R_X86_64_32S:
1802 case elfcpp::R_X86_64_16:
1803 case elfcpp::R_X86_64_8:
1804 // If building a shared library (or a position-independent
1805 // executable), we need to create a dynamic relocation for this
1806 // location. We can't use an R_X86_64_RELATIVE relocation
1807 // because that is always a 64-bit relocation.
1808 if (parameters->options().output_is_position_independent())
1810 this->check_non_pic(object, r_type, NULL);
1812 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1813 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1814 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1815 rela_dyn->add_local(object, r_sym, r_type, output_section,
1816 data_shndx, reloc.get_r_offset(),
1817 reloc.get_r_addend());
1820 gold_assert(lsym.get_st_value() == 0);
1821 unsigned int shndx = lsym.get_st_shndx();
1823 shndx = object->adjust_sym_shndx(r_sym, shndx,
1826 object->error(_("section symbol %u has bad shndx %u"),
1829 rela_dyn->add_local_section(object, shndx,
1830 r_type, output_section,
1831 data_shndx, reloc.get_r_offset(),
1832 reloc.get_r_addend());
1837 case elfcpp::R_X86_64_PC64:
1838 case elfcpp::R_X86_64_PC32:
1839 case elfcpp::R_X86_64_PC16:
1840 case elfcpp::R_X86_64_PC8:
1843 case elfcpp::R_X86_64_PLT32:
1844 // Since we know this is a local symbol, we can handle this as a
1848 case elfcpp::R_X86_64_GOTPC32:
1849 case elfcpp::R_X86_64_GOTOFF64:
1850 case elfcpp::R_X86_64_GOTPC64:
1851 case elfcpp::R_X86_64_PLTOFF64:
1852 // We need a GOT section.
1853 target->got_section(symtab, layout);
1854 // For PLTOFF64, we'd normally want a PLT section, but since we
1855 // know this is a local symbol, no PLT is needed.
1858 case elfcpp::R_X86_64_GOT64:
1859 case elfcpp::R_X86_64_GOT32:
1860 case elfcpp::R_X86_64_GOTPCREL64:
1861 case elfcpp::R_X86_64_GOTPCREL:
1862 case elfcpp::R_X86_64_GOTPLT64:
1864 // The symbol requires a GOT entry.
1865 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1866 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1868 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1869 // lets function pointers compare correctly with shared
1870 // libraries. Otherwise we would need an IRELATIVE reloc.
1872 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1873 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1875 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1878 // If we are generating a shared object, we need to add a
1879 // dynamic relocation for this symbol's GOT entry.
1880 if (parameters->options().output_is_position_independent())
1882 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1883 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1884 if (r_type != elfcpp::R_X86_64_GOT32)
1886 unsigned int got_offset =
1887 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1888 rela_dyn->add_local_relative(object, r_sym,
1889 elfcpp::R_X86_64_RELATIVE,
1890 got, got_offset, 0);
1894 this->check_non_pic(object, r_type, NULL);
1896 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1897 rela_dyn->add_local(
1898 object, r_sym, r_type, got,
1899 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1903 // For GOTPLT64, we'd normally want a PLT section, but since
1904 // we know this is a local symbol, no PLT is needed.
1908 case elfcpp::R_X86_64_COPY:
1909 case elfcpp::R_X86_64_GLOB_DAT:
1910 case elfcpp::R_X86_64_JUMP_SLOT:
1911 case elfcpp::R_X86_64_RELATIVE:
1912 case elfcpp::R_X86_64_IRELATIVE:
1913 // These are outstanding tls relocs, which are unexpected when linking
1914 case elfcpp::R_X86_64_TPOFF64:
1915 case elfcpp::R_X86_64_DTPMOD64:
1916 case elfcpp::R_X86_64_TLSDESC:
1917 gold_error(_("%s: unexpected reloc %u in object file"),
1918 object->name().c_str(), r_type);
1921 // These are initial tls relocs, which are expected when linking
1922 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1923 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1924 case elfcpp::R_X86_64_TLSDESC_CALL:
1925 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1926 case elfcpp::R_X86_64_DTPOFF32:
1927 case elfcpp::R_X86_64_DTPOFF64:
1928 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1929 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1931 bool output_is_shared = parameters->options().shared();
1932 const tls::Tls_optimization optimized_type
1933 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1936 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1937 if (optimized_type == tls::TLSOPT_NONE)
1939 // Create a pair of GOT entries for the module index and
1940 // dtv-relative offset.
1941 Output_data_got<64, false>* got
1942 = target->got_section(symtab, layout);
1943 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1944 unsigned int shndx = lsym.get_st_shndx();
1946 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1948 object->error(_("local symbol %u has bad shndx %u"),
1951 got->add_local_pair_with_rela(object, r_sym,
1954 target->rela_dyn_section(layout),
1955 elfcpp::R_X86_64_DTPMOD64, 0);
1957 else if (optimized_type != tls::TLSOPT_TO_LE)
1958 unsupported_reloc_local(object, r_type);
1961 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1962 target->define_tls_base_symbol(symtab, layout);
1963 if (optimized_type == tls::TLSOPT_NONE)
1965 // Create reserved PLT and GOT entries for the resolver.
1966 target->reserve_tlsdesc_entries(symtab, layout);
1968 // Generate a double GOT entry with an
1969 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1970 // is resolved lazily, so the GOT entry needs to be in
1971 // an area in .got.plt, not .got. Call got_section to
1972 // make sure the section has been created.
1973 target->got_section(symtab, layout);
1974 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1975 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1976 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1978 unsigned int got_offset = got->add_constant(0);
1979 got->add_constant(0);
1980 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1982 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1983 // We store the arguments we need in a vector, and
1984 // use the index into the vector as the parameter
1985 // to pass to the target specific routines.
1986 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1987 void* arg = reinterpret_cast<void*>(intarg);
1988 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1989 got, got_offset, 0);
1992 else if (optimized_type != tls::TLSOPT_TO_LE)
1993 unsupported_reloc_local(object, r_type);
1996 case elfcpp::R_X86_64_TLSDESC_CALL:
1999 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2000 if (optimized_type == tls::TLSOPT_NONE)
2002 // Create a GOT entry for the module index.
2003 target->got_mod_index_entry(symtab, layout, object);
2005 else if (optimized_type != tls::TLSOPT_TO_LE)
2006 unsupported_reloc_local(object, r_type);
2009 case elfcpp::R_X86_64_DTPOFF32:
2010 case elfcpp::R_X86_64_DTPOFF64:
2013 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2014 layout->set_has_static_tls();
2015 if (optimized_type == tls::TLSOPT_NONE)
2017 // Create a GOT entry for the tp-relative offset.
2018 Output_data_got<64, false>* got
2019 = target->got_section(symtab, layout);
2020 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2021 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
2022 target->rela_dyn_section(layout),
2023 elfcpp::R_X86_64_TPOFF64);
2025 else if (optimized_type != tls::TLSOPT_TO_LE)
2026 unsupported_reloc_local(object, r_type);
2029 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2030 layout->set_has_static_tls();
2031 if (output_is_shared)
2032 unsupported_reloc_local(object, r_type);
2041 case elfcpp::R_X86_64_SIZE32:
2042 case elfcpp::R_X86_64_SIZE64:
2044 gold_error(_("%s: unsupported reloc %u against local symbol"),
2045 object->name().c_str(), r_type);
2051 // Report an unsupported relocation against a global symbol.
2054 Target_x86_64::Scan::unsupported_reloc_global(
2055 Sized_relobj_file<64, false>* object,
2056 unsigned int r_type,
2059 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2060 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2063 // Returns true if this relocation type could be that of a function pointer.
2065 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2069 case elfcpp::R_X86_64_64:
2070 case elfcpp::R_X86_64_32:
2071 case elfcpp::R_X86_64_32S:
2072 case elfcpp::R_X86_64_16:
2073 case elfcpp::R_X86_64_8:
2074 case elfcpp::R_X86_64_GOT64:
2075 case elfcpp::R_X86_64_GOT32:
2076 case elfcpp::R_X86_64_GOTPCREL64:
2077 case elfcpp::R_X86_64_GOTPCREL:
2078 case elfcpp::R_X86_64_GOTPLT64:
2086 // For safe ICF, scan a relocation for a local symbol to check if it
2087 // corresponds to a function pointer being taken. In that case mark
2088 // the function whose pointer was taken as not foldable.
2091 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2095 Sized_relobj_file<64, false>* ,
2098 const elfcpp::Rela<64, false>& ,
2099 unsigned int r_type,
2100 const elfcpp::Sym<64, false>&)
2102 // When building a shared library, do not fold any local symbols as it is
2103 // not possible to distinguish pointer taken versus a call by looking at
2104 // the relocation types.
2105 return (parameters->options().shared()
2106 || possible_function_pointer_reloc(r_type));
2109 // For safe ICF, scan a relocation for a global symbol to check if it
2110 // corresponds to a function pointer being taken. In that case mark
2111 // the function whose pointer was taken as not foldable.
2114 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2118 Sized_relobj_file<64, false>* ,
2121 const elfcpp::Rela<64, false>& ,
2122 unsigned int r_type,
2125 // When building a shared library, do not fold symbols whose visibility
2126 // is hidden, internal or protected.
2127 return ((parameters->options().shared()
2128 && (gsym->visibility() == elfcpp::STV_INTERNAL
2129 || gsym->visibility() == elfcpp::STV_PROTECTED
2130 || gsym->visibility() == elfcpp::STV_HIDDEN))
2131 || possible_function_pointer_reloc(r_type));
2134 // Scan a relocation for a global symbol.
2137 Target_x86_64::Scan::global(Symbol_table* symtab,
2139 Target_x86_64* target,
2140 Sized_relobj_file<64, false>* object,
2141 unsigned int data_shndx,
2142 Output_section* output_section,
2143 const elfcpp::Rela<64, false>& reloc,
2144 unsigned int r_type,
2147 // A STT_GNU_IFUNC symbol may require a PLT entry.
2148 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2149 && this->reloc_needs_plt_for_ifunc(object, r_type))
2150 target->make_plt_entry(symtab, layout, gsym);
2154 case elfcpp::R_X86_64_NONE:
2155 case elfcpp::R_X86_64_GNU_VTINHERIT:
2156 case elfcpp::R_X86_64_GNU_VTENTRY:
2159 case elfcpp::R_X86_64_64:
2160 case elfcpp::R_X86_64_32:
2161 case elfcpp::R_X86_64_32S:
2162 case elfcpp::R_X86_64_16:
2163 case elfcpp::R_X86_64_8:
2165 // Make a PLT entry if necessary.
2166 if (gsym->needs_plt_entry())
2168 target->make_plt_entry(symtab, layout, gsym);
2169 // Since this is not a PC-relative relocation, we may be
2170 // taking the address of a function. In that case we need to
2171 // set the entry in the dynamic symbol table to the address of
2173 if (gsym->is_from_dynobj() && !parameters->options().shared())
2174 gsym->set_needs_dynsym_value();
2176 // Make a dynamic relocation if necessary.
2177 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2179 if (gsym->may_need_copy_reloc())
2181 target->copy_reloc(symtab, layout, object,
2182 data_shndx, output_section, gsym, reloc);
2184 else if (r_type == elfcpp::R_X86_64_64
2185 && gsym->type() == elfcpp::STT_GNU_IFUNC
2186 && gsym->can_use_relative_reloc(false)
2187 && !gsym->is_from_dynobj()
2188 && !gsym->is_undefined()
2189 && !gsym->is_preemptible())
2191 // Use an IRELATIVE reloc for a locally defined
2192 // STT_GNU_IFUNC symbol. This makes a function
2193 // address in a PIE executable match the address in a
2194 // shared library that it links against.
2195 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2196 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2197 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2198 output_section, object,
2200 reloc.get_r_offset(),
2201 reloc.get_r_addend());
2203 else if (r_type == elfcpp::R_X86_64_64
2204 && gsym->can_use_relative_reloc(false))
2206 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2207 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2208 output_section, object,
2210 reloc.get_r_offset(),
2211 reloc.get_r_addend());
2215 this->check_non_pic(object, r_type, gsym);
2216 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2217 rela_dyn->add_global(gsym, r_type, output_section, object,
2218 data_shndx, reloc.get_r_offset(),
2219 reloc.get_r_addend());
2225 case elfcpp::R_X86_64_PC64:
2226 case elfcpp::R_X86_64_PC32:
2227 case elfcpp::R_X86_64_PC16:
2228 case elfcpp::R_X86_64_PC8:
2230 // Make a PLT entry if necessary.
2231 if (gsym->needs_plt_entry())
2232 target->make_plt_entry(symtab, layout, gsym);
2233 // Make a dynamic relocation if necessary.
2234 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2236 if (gsym->may_need_copy_reloc())
2238 target->copy_reloc(symtab, layout, object,
2239 data_shndx, output_section, gsym, reloc);
2243 this->check_non_pic(object, r_type, gsym);
2244 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2245 rela_dyn->add_global(gsym, r_type, output_section, object,
2246 data_shndx, reloc.get_r_offset(),
2247 reloc.get_r_addend());
2253 case elfcpp::R_X86_64_GOT64:
2254 case elfcpp::R_X86_64_GOT32:
2255 case elfcpp::R_X86_64_GOTPCREL64:
2256 case elfcpp::R_X86_64_GOTPCREL:
2257 case elfcpp::R_X86_64_GOTPLT64:
2259 // The symbol requires a GOT entry.
2260 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2261 if (gsym->final_value_is_known())
2263 // For a STT_GNU_IFUNC symbol we want the PLT address.
2264 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2265 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2267 got->add_global(gsym, GOT_TYPE_STANDARD);
2271 // If this symbol is not fully resolved, we need to add a
2272 // dynamic relocation for it.
2273 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2274 if (gsym->is_from_dynobj()
2275 || gsym->is_undefined()
2276 || gsym->is_preemptible()
2277 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2278 && parameters->options().output_is_position_independent()))
2279 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2280 elfcpp::R_X86_64_GLOB_DAT);
2283 // For a STT_GNU_IFUNC symbol we want to write the PLT
2284 // offset into the GOT, so that function pointer
2285 // comparisons work correctly.
2287 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2288 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2291 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2292 // Tell the dynamic linker to use the PLT address
2293 // when resolving relocations.
2294 if (gsym->is_from_dynobj()
2295 && !parameters->options().shared())
2296 gsym->set_needs_dynsym_value();
2300 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2301 rela_dyn->add_global_relative(gsym,
2302 elfcpp::R_X86_64_RELATIVE,
2307 // For GOTPLT64, we also need a PLT entry (but only if the
2308 // symbol is not fully resolved).
2309 if (r_type == elfcpp::R_X86_64_GOTPLT64
2310 && !gsym->final_value_is_known())
2311 target->make_plt_entry(symtab, layout, gsym);
2315 case elfcpp::R_X86_64_PLT32:
2316 // If the symbol is fully resolved, this is just a PC32 reloc.
2317 // Otherwise we need a PLT entry.
2318 if (gsym->final_value_is_known())
2320 // If building a shared library, we can also skip the PLT entry
2321 // if the symbol is defined in the output file and is protected
2323 if (gsym->is_defined()
2324 && !gsym->is_from_dynobj()
2325 && !gsym->is_preemptible())
2327 target->make_plt_entry(symtab, layout, gsym);
2330 case elfcpp::R_X86_64_GOTPC32:
2331 case elfcpp::R_X86_64_GOTOFF64:
2332 case elfcpp::R_X86_64_GOTPC64:
2333 case elfcpp::R_X86_64_PLTOFF64:
2334 // We need a GOT section.
2335 target->got_section(symtab, layout);
2336 // For PLTOFF64, we also need a PLT entry (but only if the
2337 // symbol is not fully resolved).
2338 if (r_type == elfcpp::R_X86_64_PLTOFF64
2339 && !gsym->final_value_is_known())
2340 target->make_plt_entry(symtab, layout, gsym);
2343 case elfcpp::R_X86_64_COPY:
2344 case elfcpp::R_X86_64_GLOB_DAT:
2345 case elfcpp::R_X86_64_JUMP_SLOT:
2346 case elfcpp::R_X86_64_RELATIVE:
2347 case elfcpp::R_X86_64_IRELATIVE:
2348 // These are outstanding tls relocs, which are unexpected when linking
2349 case elfcpp::R_X86_64_TPOFF64:
2350 case elfcpp::R_X86_64_DTPMOD64:
2351 case elfcpp::R_X86_64_TLSDESC:
2352 gold_error(_("%s: unexpected reloc %u in object file"),
2353 object->name().c_str(), r_type);
2356 // These are initial tls relocs, which are expected for global()
2357 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2358 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2359 case elfcpp::R_X86_64_TLSDESC_CALL:
2360 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2361 case elfcpp::R_X86_64_DTPOFF32:
2362 case elfcpp::R_X86_64_DTPOFF64:
2363 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2364 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2366 const bool is_final = gsym->final_value_is_known();
2367 const tls::Tls_optimization optimized_type
2368 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2371 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2372 if (optimized_type == tls::TLSOPT_NONE)
2374 // Create a pair of GOT entries for the module index and
2375 // dtv-relative offset.
2376 Output_data_got<64, false>* got
2377 = target->got_section(symtab, layout);
2378 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2379 target->rela_dyn_section(layout),
2380 elfcpp::R_X86_64_DTPMOD64,
2381 elfcpp::R_X86_64_DTPOFF64);
2383 else if (optimized_type == tls::TLSOPT_TO_IE)
2385 // Create a GOT entry for the tp-relative offset.
2386 Output_data_got<64, false>* got
2387 = target->got_section(symtab, layout);
2388 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2389 target->rela_dyn_section(layout),
2390 elfcpp::R_X86_64_TPOFF64);
2392 else if (optimized_type != tls::TLSOPT_TO_LE)
2393 unsupported_reloc_global(object, r_type, gsym);
2396 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2397 target->define_tls_base_symbol(symtab, layout);
2398 if (optimized_type == tls::TLSOPT_NONE)
2400 // Create reserved PLT and GOT entries for the resolver.
2401 target->reserve_tlsdesc_entries(symtab, layout);
2403 // Create a double GOT entry with an R_X86_64_TLSDESC
2404 // reloc. The R_X86_64_TLSDESC reloc is resolved
2405 // lazily, so the GOT entry needs to be in an area in
2406 // .got.plt, not .got. Call got_section to make sure
2407 // the section has been created.
2408 target->got_section(symtab, layout);
2409 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2410 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2411 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2412 elfcpp::R_X86_64_TLSDESC, 0);
2414 else if (optimized_type == tls::TLSOPT_TO_IE)
2416 // Create a GOT entry for the tp-relative offset.
2417 Output_data_got<64, false>* got
2418 = target->got_section(symtab, layout);
2419 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2420 target->rela_dyn_section(layout),
2421 elfcpp::R_X86_64_TPOFF64);
2423 else if (optimized_type != tls::TLSOPT_TO_LE)
2424 unsupported_reloc_global(object, r_type, gsym);
2427 case elfcpp::R_X86_64_TLSDESC_CALL:
2430 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2431 if (optimized_type == tls::TLSOPT_NONE)
2433 // Create a GOT entry for the module index.
2434 target->got_mod_index_entry(symtab, layout, object);
2436 else if (optimized_type != tls::TLSOPT_TO_LE)
2437 unsupported_reloc_global(object, r_type, gsym);
2440 case elfcpp::R_X86_64_DTPOFF32:
2441 case elfcpp::R_X86_64_DTPOFF64:
2444 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2445 layout->set_has_static_tls();
2446 if (optimized_type == tls::TLSOPT_NONE)
2448 // Create a GOT entry for the tp-relative offset.
2449 Output_data_got<64, false>* got
2450 = target->got_section(symtab, layout);
2451 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2452 target->rela_dyn_section(layout),
2453 elfcpp::R_X86_64_TPOFF64);
2455 else if (optimized_type != tls::TLSOPT_TO_LE)
2456 unsupported_reloc_global(object, r_type, gsym);
2459 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2460 layout->set_has_static_tls();
2461 if (parameters->options().shared())
2462 unsupported_reloc_local(object, r_type);
2471 case elfcpp::R_X86_64_SIZE32:
2472 case elfcpp::R_X86_64_SIZE64:
2474 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2475 object->name().c_str(), r_type,
2476 gsym->demangled_name().c_str());
2482 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2484 Sized_relobj_file<64, false>* object,
2485 unsigned int data_shndx,
2486 unsigned int sh_type,
2487 const unsigned char* prelocs,
2489 Output_section* output_section,
2490 bool needs_special_offset_handling,
2491 size_t local_symbol_count,
2492 const unsigned char* plocal_symbols)
2495 if (sh_type == elfcpp::SHT_REL)
2500 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2501 Target_x86_64::Scan,
2502 Target_x86_64::Relocatable_size_for_reloc>(
2511 needs_special_offset_handling,
2516 // Scan relocations for a section.
2519 Target_x86_64::scan_relocs(Symbol_table* symtab,
2521 Sized_relobj_file<64, false>* object,
2522 unsigned int data_shndx,
2523 unsigned int sh_type,
2524 const unsigned char* prelocs,
2526 Output_section* output_section,
2527 bool needs_special_offset_handling,
2528 size_t local_symbol_count,
2529 const unsigned char* plocal_symbols)
2531 if (sh_type == elfcpp::SHT_REL)
2533 gold_error(_("%s: unsupported REL reloc section"),
2534 object->name().c_str());
2538 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2539 Target_x86_64::Scan>(
2548 needs_special_offset_handling,
2553 // Finalize the sections.
2556 Target_x86_64::do_finalize_sections(
2558 const Input_objects*,
2559 Symbol_table* symtab)
2561 const Reloc_section* rel_plt = (this->plt_ == NULL
2563 : this->plt_->rela_plt());
2564 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2565 this->rela_dyn_, true, false);
2567 // Fill in some more dynamic tags.
2568 Output_data_dynamic* const odyn = layout->dynamic_data();
2571 if (this->plt_ != NULL
2572 && this->plt_->output_section() != NULL
2573 && this->plt_->has_tlsdesc_entry())
2575 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2576 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2577 this->got_->finalize_data_size();
2578 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2579 this->plt_, plt_offset);
2580 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2581 this->got_, got_offset);
2585 // Emit any relocs we saved in an attempt to avoid generating COPY
2587 if (this->copy_relocs_.any_saved_relocs())
2588 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2590 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2591 // the .got.plt section.
2592 Symbol* sym = this->global_offset_table_;
2595 uint64_t data_size = this->got_plt_->current_data_size();
2596 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2600 // Perform a relocation.
2603 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2604 Target_x86_64* target,
2607 const elfcpp::Rela<64, false>& rela,
2608 unsigned int r_type,
2609 const Sized_symbol<64>* gsym,
2610 const Symbol_value<64>* psymval,
2611 unsigned char* view,
2612 elfcpp::Elf_types<64>::Elf_Addr address,
2613 section_size_type view_size)
2615 if (this->skip_call_tls_get_addr_)
2617 if ((r_type != elfcpp::R_X86_64_PLT32
2618 && r_type != elfcpp::R_X86_64_PC32)
2620 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2622 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2623 _("missing expected TLS relocation"));
2627 this->skip_call_tls_get_addr_ = false;
2632 const Sized_relobj_file<64, false>* object = relinfo->object;
2634 // Pick the value to use for symbols defined in the PLT.
2635 Symbol_value<64> symval;
2637 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2639 symval.set_output_value(target->plt_section()->address()
2640 + gsym->plt_offset());
2643 else if (gsym == NULL && psymval->is_ifunc_symbol())
2645 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2646 if (object->local_has_plt_offset(r_sym))
2648 symval.set_output_value(target->plt_section()->address()
2649 + object->local_plt_offset(r_sym));
2654 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2656 // Get the GOT offset if needed.
2657 // The GOT pointer points to the end of the GOT section.
2658 // We need to subtract the size of the GOT section to get
2659 // the actual offset to use in the relocation.
2660 bool have_got_offset = false;
2661 unsigned int got_offset = 0;
2664 case elfcpp::R_X86_64_GOT32:
2665 case elfcpp::R_X86_64_GOT64:
2666 case elfcpp::R_X86_64_GOTPLT64:
2667 case elfcpp::R_X86_64_GOTPCREL:
2668 case elfcpp::R_X86_64_GOTPCREL64:
2671 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2672 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2676 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2677 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2678 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2679 - target->got_size());
2681 have_got_offset = true;
2690 case elfcpp::R_X86_64_NONE:
2691 case elfcpp::R_X86_64_GNU_VTINHERIT:
2692 case elfcpp::R_X86_64_GNU_VTENTRY:
2695 case elfcpp::R_X86_64_64:
2696 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2699 case elfcpp::R_X86_64_PC64:
2700 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2704 case elfcpp::R_X86_64_32:
2705 // FIXME: we need to verify that value + addend fits into 32 bits:
2706 // uint64_t x = value + addend;
2707 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2708 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2709 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2712 case elfcpp::R_X86_64_32S:
2713 // FIXME: we need to verify that value + addend fits into 32 bits:
2714 // int64_t x = value + addend; // note this quantity is signed!
2715 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2716 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2719 case elfcpp::R_X86_64_PC32:
2720 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2724 case elfcpp::R_X86_64_16:
2725 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2728 case elfcpp::R_X86_64_PC16:
2729 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2733 case elfcpp::R_X86_64_8:
2734 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2737 case elfcpp::R_X86_64_PC8:
2738 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2742 case elfcpp::R_X86_64_PLT32:
2743 gold_assert(gsym == NULL
2744 || gsym->has_plt_offset()
2745 || gsym->final_value_is_known()
2746 || (gsym->is_defined()
2747 && !gsym->is_from_dynobj()
2748 && !gsym->is_preemptible()));
2749 // Note: while this code looks the same as for R_X86_64_PC32, it
2750 // behaves differently because psymval was set to point to
2751 // the PLT entry, rather than the symbol, in Scan::global().
2752 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2756 case elfcpp::R_X86_64_PLTOFF64:
2759 gold_assert(gsym->has_plt_offset()
2760 || gsym->final_value_is_known());
2761 elfcpp::Elf_types<64>::Elf_Addr got_address;
2762 got_address = target->got_section(NULL, NULL)->address();
2763 Relocate_functions<64, false>::rela64(view, object, psymval,
2764 addend - got_address);
2767 case elfcpp::R_X86_64_GOT32:
2768 gold_assert(have_got_offset);
2769 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2772 case elfcpp::R_X86_64_GOTPC32:
2775 elfcpp::Elf_types<64>::Elf_Addr value;
2776 value = target->got_plt_section()->address();
2777 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2781 case elfcpp::R_X86_64_GOT64:
2782 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2783 // Since we always add a PLT entry, this is equivalent.
2784 case elfcpp::R_X86_64_GOTPLT64:
2785 gold_assert(have_got_offset);
2786 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2789 case elfcpp::R_X86_64_GOTPC64:
2792 elfcpp::Elf_types<64>::Elf_Addr value;
2793 value = target->got_plt_section()->address();
2794 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2798 case elfcpp::R_X86_64_GOTOFF64:
2800 elfcpp::Elf_types<64>::Elf_Addr value;
2801 value = (psymval->value(object, 0)
2802 - target->got_plt_section()->address());
2803 Relocate_functions<64, false>::rela64(view, value, addend);
2807 case elfcpp::R_X86_64_GOTPCREL:
2809 gold_assert(have_got_offset);
2810 elfcpp::Elf_types<64>::Elf_Addr value;
2811 value = target->got_plt_section()->address() + got_offset;
2812 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2816 case elfcpp::R_X86_64_GOTPCREL64:
2818 gold_assert(have_got_offset);
2819 elfcpp::Elf_types<64>::Elf_Addr value;
2820 value = target->got_plt_section()->address() + got_offset;
2821 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2825 case elfcpp::R_X86_64_COPY:
2826 case elfcpp::R_X86_64_GLOB_DAT:
2827 case elfcpp::R_X86_64_JUMP_SLOT:
2828 case elfcpp::R_X86_64_RELATIVE:
2829 case elfcpp::R_X86_64_IRELATIVE:
2830 // These are outstanding tls relocs, which are unexpected when linking
2831 case elfcpp::R_X86_64_TPOFF64:
2832 case elfcpp::R_X86_64_DTPMOD64:
2833 case elfcpp::R_X86_64_TLSDESC:
2834 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2835 _("unexpected reloc %u in object file"),
2839 // These are initial tls relocs, which are expected when linking
2840 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2841 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2842 case elfcpp::R_X86_64_TLSDESC_CALL:
2843 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2844 case elfcpp::R_X86_64_DTPOFF32:
2845 case elfcpp::R_X86_64_DTPOFF64:
2846 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2847 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2848 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2849 view, address, view_size);
2852 case elfcpp::R_X86_64_SIZE32:
2853 case elfcpp::R_X86_64_SIZE64:
2855 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2856 _("unsupported reloc %u"),
2864 // Perform a TLS relocation.
2867 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2868 Target_x86_64* target,
2870 const elfcpp::Rela<64, false>& rela,
2871 unsigned int r_type,
2872 const Sized_symbol<64>* gsym,
2873 const Symbol_value<64>* psymval,
2874 unsigned char* view,
2875 elfcpp::Elf_types<64>::Elf_Addr address,
2876 section_size_type view_size)
2878 Output_segment* tls_segment = relinfo->layout->tls_segment();
2880 const Sized_relobj_file<64, false>* object = relinfo->object;
2881 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2882 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2883 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2885 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2887 const bool is_final = (gsym == NULL
2888 ? !parameters->options().shared()
2889 : gsym->final_value_is_known());
2890 tls::Tls_optimization optimized_type
2891 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2894 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2895 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2897 // If this code sequence is used in a non-executable section,
2898 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2899 // on the assumption that it's being used by itself in a debug
2900 // section. Therefore, in the unlikely event that the code
2901 // sequence appears in a non-executable section, we simply
2902 // leave it unoptimized.
2903 optimized_type = tls::TLSOPT_NONE;
2905 if (optimized_type == tls::TLSOPT_TO_LE)
2907 gold_assert(tls_segment != NULL);
2908 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2909 rela, r_type, value, view,
2915 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2916 ? GOT_TYPE_TLS_OFFSET
2917 : GOT_TYPE_TLS_PAIR);
2918 unsigned int got_offset;
2921 gold_assert(gsym->has_got_offset(got_type));
2922 got_offset = gsym->got_offset(got_type) - target->got_size();
2926 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2927 gold_assert(object->local_has_got_offset(r_sym, got_type));
2928 got_offset = (object->local_got_offset(r_sym, got_type)
2929 - target->got_size());
2931 if (optimized_type == tls::TLSOPT_TO_IE)
2933 gold_assert(tls_segment != NULL);
2934 value = target->got_plt_section()->address() + got_offset;
2935 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2936 value, view, address, view_size);
2939 else if (optimized_type == tls::TLSOPT_NONE)
2941 // Relocate the field with the offset of the pair of GOT
2943 value = target->got_plt_section()->address() + got_offset;
2944 Relocate_functions<64, false>::pcrela32(view, value, addend,
2949 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2950 _("unsupported reloc %u"), r_type);
2953 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2954 case elfcpp::R_X86_64_TLSDESC_CALL:
2955 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2957 // See above comment for R_X86_64_TLSGD.
2958 optimized_type = tls::TLSOPT_NONE;
2960 if (optimized_type == tls::TLSOPT_TO_LE)
2962 gold_assert(tls_segment != NULL);
2963 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2964 rela, r_type, value, view,
2970 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2971 ? GOT_TYPE_TLS_OFFSET
2972 : GOT_TYPE_TLS_DESC);
2973 unsigned int got_offset = 0;
2974 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2975 && optimized_type == tls::TLSOPT_NONE)
2977 // We created GOT entries in the .got.tlsdesc portion of
2978 // the .got.plt section, but the offset stored in the
2979 // symbol is the offset within .got.tlsdesc.
2980 got_offset = (target->got_size()
2981 + target->got_plt_section()->data_size());
2985 gold_assert(gsym->has_got_offset(got_type));
2986 got_offset += gsym->got_offset(got_type) - target->got_size();
2990 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2991 gold_assert(object->local_has_got_offset(r_sym, got_type));
2992 got_offset += (object->local_got_offset(r_sym, got_type)
2993 - target->got_size());
2995 if (optimized_type == tls::TLSOPT_TO_IE)
2997 gold_assert(tls_segment != NULL);
2998 value = target->got_plt_section()->address() + got_offset;
2999 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3000 rela, r_type, value, view, address,
3004 else if (optimized_type == tls::TLSOPT_NONE)
3006 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3008 // Relocate the field with the offset of the pair of GOT
3010 value = target->got_plt_section()->address() + got_offset;
3011 Relocate_functions<64, false>::pcrela32(view, value, addend,
3017 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3018 _("unsupported reloc %u"), r_type);
3021 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3022 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3024 // See above comment for R_X86_64_TLSGD.
3025 optimized_type = tls::TLSOPT_NONE;
3027 if (optimized_type == tls::TLSOPT_TO_LE)
3029 gold_assert(tls_segment != NULL);
3030 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3031 value, view, view_size);
3034 else if (optimized_type == tls::TLSOPT_NONE)
3036 // Relocate the field with the offset of the GOT entry for
3037 // the module index.
3038 unsigned int got_offset;
3039 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3040 - target->got_size());
3041 value = target->got_plt_section()->address() + got_offset;
3042 Relocate_functions<64, false>::pcrela32(view, value, addend,
3046 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3047 _("unsupported reloc %u"), r_type);
3050 case elfcpp::R_X86_64_DTPOFF32:
3051 // This relocation type is used in debugging information.
3052 // In that case we need to not optimize the value. If the
3053 // section is not executable, then we assume we should not
3054 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3055 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3057 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3059 gold_assert(tls_segment != NULL);
3060 value -= tls_segment->memsz();
3062 Relocate_functions<64, false>::rela32(view, value, addend);
3065 case elfcpp::R_X86_64_DTPOFF64:
3066 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3067 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3069 gold_assert(tls_segment != NULL);
3070 value -= tls_segment->memsz();
3072 Relocate_functions<64, false>::rela64(view, value, addend);
3075 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3076 if (optimized_type == tls::TLSOPT_TO_LE)
3078 gold_assert(tls_segment != NULL);
3079 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3080 rela, r_type, value, view,
3084 else if (optimized_type == tls::TLSOPT_NONE)
3086 // Relocate the field with the offset of the GOT entry for
3087 // the tp-relative offset of the symbol.
3088 unsigned int got_offset;
3091 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3092 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3093 - target->got_size());
3097 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3098 gold_assert(object->local_has_got_offset(r_sym,
3099 GOT_TYPE_TLS_OFFSET));
3100 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3101 - target->got_size());
3103 value = target->got_plt_section()->address() + got_offset;
3104 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3107 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3108 _("unsupported reloc type %u"),
3112 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3113 value -= tls_segment->memsz();
3114 Relocate_functions<64, false>::rela32(view, value, addend);
3119 // Do a relocation in which we convert a TLS General-Dynamic to an
3123 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3126 const elfcpp::Rela<64, false>& rela,
3128 elfcpp::Elf_types<64>::Elf_Addr value,
3129 unsigned char* view,
3130 elfcpp::Elf_types<64>::Elf_Addr address,
3131 section_size_type view_size)
3133 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3134 // .word 0x6666; rex64; call __tls_get_addr
3135 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3137 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3138 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3140 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3141 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3142 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3143 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3145 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3147 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3148 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3150 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3152 this->skip_call_tls_get_addr_ = true;
3155 // Do a relocation in which we convert a TLS General-Dynamic to a
3159 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3161 Output_segment* tls_segment,
3162 const elfcpp::Rela<64, false>& rela,
3164 elfcpp::Elf_types<64>::Elf_Addr value,
3165 unsigned char* view,
3166 section_size_type view_size)
3168 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3169 // .word 0x6666; rex64; call __tls_get_addr
3170 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3172 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3173 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3175 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3176 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3177 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3178 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3180 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3182 value -= tls_segment->memsz();
3183 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3185 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3187 this->skip_call_tls_get_addr_ = true;
3190 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3193 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3194 const Relocate_info<64, false>* relinfo,
3197 const elfcpp::Rela<64, false>& rela,
3198 unsigned int r_type,
3199 elfcpp::Elf_types<64>::Elf_Addr value,
3200 unsigned char* view,
3201 elfcpp::Elf_types<64>::Elf_Addr address,
3202 section_size_type view_size)
3204 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3206 // leaq foo@tlsdesc(%rip), %rax
3207 // ==> movq foo@gottpoff(%rip), %rax
3208 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3210 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3211 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3213 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3214 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3218 // call *foo@tlscall(%rax)
3220 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3221 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3222 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3223 view[0] == 0xff && view[1] == 0x10);
3229 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3232 Target_x86_64::Relocate::tls_desc_gd_to_le(
3233 const Relocate_info<64, false>* relinfo,
3235 Output_segment* tls_segment,
3236 const elfcpp::Rela<64, false>& rela,
3237 unsigned int r_type,
3238 elfcpp::Elf_types<64>::Elf_Addr value,
3239 unsigned char* view,
3240 section_size_type view_size)
3242 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3244 // leaq foo@tlsdesc(%rip), %rax
3245 // ==> movq foo@tpoff, %rax
3246 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3247 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3248 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3249 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3252 value -= tls_segment->memsz();
3253 Relocate_functions<64, false>::rela32(view, value, 0);
3257 // call *foo@tlscall(%rax)
3259 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3260 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3261 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3262 view[0] == 0xff && view[1] == 0x10);
3269 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3272 const elfcpp::Rela<64, false>& rela,
3274 elfcpp::Elf_types<64>::Elf_Addr,
3275 unsigned char* view,
3276 section_size_type view_size)
3278 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3279 // ... leq foo@dtpoff(%rax),%reg
3280 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3282 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3283 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3285 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3286 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3288 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3290 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3292 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3294 this->skip_call_tls_get_addr_ = true;
3297 // Do a relocation in which we convert a TLS Initial-Exec to a
3301 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3303 Output_segment* tls_segment,
3304 const elfcpp::Rela<64, false>& rela,
3306 elfcpp::Elf_types<64>::Elf_Addr value,
3307 unsigned char* view,
3308 section_size_type view_size)
3310 // We need to examine the opcodes to figure out which instruction we
3313 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3314 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3316 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3317 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3319 unsigned char op1 = view[-3];
3320 unsigned char op2 = view[-2];
3321 unsigned char op3 = view[-1];
3322 unsigned char reg = op3 >> 3;
3330 view[-1] = 0xc0 | reg;
3334 // Special handling for %rsp.
3338 view[-1] = 0xc0 | reg;
3346 view[-1] = 0x80 | reg | (reg << 3);
3349 value -= tls_segment->memsz();
3350 Relocate_functions<64, false>::rela32(view, value, 0);
3353 // Relocate section data.
3356 Target_x86_64::relocate_section(
3357 const Relocate_info<64, false>* relinfo,
3358 unsigned int sh_type,
3359 const unsigned char* prelocs,
3361 Output_section* output_section,
3362 bool needs_special_offset_handling,
3363 unsigned char* view,
3364 elfcpp::Elf_types<64>::Elf_Addr address,
3365 section_size_type view_size,
3366 const Reloc_symbol_changes* reloc_symbol_changes)
3368 gold_assert(sh_type == elfcpp::SHT_RELA);
3370 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3371 Target_x86_64::Relocate>(
3377 needs_special_offset_handling,
3381 reloc_symbol_changes);
3384 // Apply an incremental relocation. Incremental relocations always refer
3385 // to global symbols.
3388 Target_x86_64::apply_relocation(
3389 const Relocate_info<64, false>* relinfo,
3390 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3391 unsigned int r_type,
3392 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3394 unsigned char* view,
3395 elfcpp::Elf_types<64>::Elf_Addr address,
3396 section_size_type view_size)
3398 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3410 // Return the size of a relocation while scanning during a relocatable
3414 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3415 unsigned int r_type,
3420 case elfcpp::R_X86_64_NONE:
3421 case elfcpp::R_X86_64_GNU_VTINHERIT:
3422 case elfcpp::R_X86_64_GNU_VTENTRY:
3423 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3424 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3425 case elfcpp::R_X86_64_TLSDESC_CALL:
3426 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3427 case elfcpp::R_X86_64_DTPOFF32:
3428 case elfcpp::R_X86_64_DTPOFF64:
3429 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3430 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3433 case elfcpp::R_X86_64_64:
3434 case elfcpp::R_X86_64_PC64:
3435 case elfcpp::R_X86_64_GOTOFF64:
3436 case elfcpp::R_X86_64_GOTPC64:
3437 case elfcpp::R_X86_64_PLTOFF64:
3438 case elfcpp::R_X86_64_GOT64:
3439 case elfcpp::R_X86_64_GOTPCREL64:
3440 case elfcpp::R_X86_64_GOTPCREL:
3441 case elfcpp::R_X86_64_GOTPLT64:
3444 case elfcpp::R_X86_64_32:
3445 case elfcpp::R_X86_64_32S:
3446 case elfcpp::R_X86_64_PC32:
3447 case elfcpp::R_X86_64_PLT32:
3448 case elfcpp::R_X86_64_GOTPC32:
3449 case elfcpp::R_X86_64_GOT32:
3452 case elfcpp::R_X86_64_16:
3453 case elfcpp::R_X86_64_PC16:
3456 case elfcpp::R_X86_64_8:
3457 case elfcpp::R_X86_64_PC8:
3460 case elfcpp::R_X86_64_COPY:
3461 case elfcpp::R_X86_64_GLOB_DAT:
3462 case elfcpp::R_X86_64_JUMP_SLOT:
3463 case elfcpp::R_X86_64_RELATIVE:
3464 case elfcpp::R_X86_64_IRELATIVE:
3465 // These are outstanding tls relocs, which are unexpected when linking
3466 case elfcpp::R_X86_64_TPOFF64:
3467 case elfcpp::R_X86_64_DTPMOD64:
3468 case elfcpp::R_X86_64_TLSDESC:
3469 object->error(_("unexpected reloc %u in object file"), r_type);
3472 case elfcpp::R_X86_64_SIZE32:
3473 case elfcpp::R_X86_64_SIZE64:
3475 object->error(_("unsupported reloc %u against local symbol"), r_type);
3480 // Scan the relocs during a relocatable link.
3483 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3485 Sized_relobj_file<64, false>* object,
3486 unsigned int data_shndx,
3487 unsigned int sh_type,
3488 const unsigned char* prelocs,
3490 Output_section* output_section,
3491 bool needs_special_offset_handling,
3492 size_t local_symbol_count,
3493 const unsigned char* plocal_symbols,
3494 Relocatable_relocs* rr)
3496 gold_assert(sh_type == elfcpp::SHT_RELA);
3498 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3499 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3501 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3502 Scan_relocatable_relocs>(
3510 needs_special_offset_handling,
3516 // Relocate a section during a relocatable link.
3519 Target_x86_64::relocate_for_relocatable(
3520 const Relocate_info<64, false>* relinfo,
3521 unsigned int sh_type,
3522 const unsigned char* prelocs,
3524 Output_section* output_section,
3525 off_t offset_in_output_section,
3526 const Relocatable_relocs* rr,
3527 unsigned char* view,
3528 elfcpp::Elf_types<64>::Elf_Addr view_address,
3529 section_size_type view_size,
3530 unsigned char* reloc_view,
3531 section_size_type reloc_view_size)
3533 gold_assert(sh_type == elfcpp::SHT_RELA);
3535 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3540 offset_in_output_section,
3549 // Return the value to use for a dynamic which requires special
3550 // treatment. This is how we support equality comparisons of function
3551 // pointers across shared library boundaries, as described in the
3552 // processor specific ABI supplement.
3555 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3557 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3558 return this->plt_section()->address() + gsym->plt_offset();
3561 // Return a string used to fill a code section with nops to take up
3562 // the specified length.
3565 Target_x86_64::do_code_fill(section_size_type length) const
3569 // Build a jmpq instruction to skip over the bytes.
3570 unsigned char jmp[5];
3572 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3573 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3574 + std::string(length - 5, '\0'));
3577 // Nop sequences of various lengths.
3578 const char nop1[1] = { 0x90 }; // nop
3579 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3580 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3581 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3582 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3584 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3586 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3588 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3589 0x00, 0x00, 0x00, 0x00 };
3590 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3591 0x00, 0x00, 0x00, 0x00,
3593 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3594 0x84, 0x00, 0x00, 0x00,
3596 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3597 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3599 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3600 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3601 0x00, 0x00, 0x00, 0x00 };
3602 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3603 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3604 0x00, 0x00, 0x00, 0x00,
3606 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3607 0x66, 0x2e, 0x0f, 0x1f, // data16
3608 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3610 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3611 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3612 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3615 const char* nops[16] = {
3617 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3618 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3621 return std::string(nops[length], length);
3624 // Return the addend to use for a target specific relocation. The
3625 // only target specific relocation is R_X86_64_TLSDESC for a local
3626 // symbol. We want to set the addend is the offset of the local
3627 // symbol in the TLS segment.
3630 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3633 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3634 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3635 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3636 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3637 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3638 gold_assert(psymval->is_tls_symbol());
3639 // The value of a TLS symbol is the offset in the TLS segment.
3640 return psymval->value(ti.object, 0);
3643 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3644 // compiled with -fsplit-stack. The function calls non-split-stack
3645 // code. We have to change the function so that it always ensures
3646 // that it has enough stack space to run some random function.
3649 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3650 section_offset_type fnoffset,
3651 section_size_type fnsize,
3652 unsigned char* view,
3653 section_size_type view_size,
3655 std::string* to) const
3657 // The function starts with a comparison of the stack pointer and a
3658 // field in the TCB. This is followed by a jump.
3661 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3664 // We will call __morestack if the carry flag is set after this
3665 // comparison. We turn the comparison into an stc instruction
3667 view[fnoffset] = '\xf9';
3668 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3670 // lea NN(%rsp),%r10
3671 // lea NN(%rsp),%r11
3672 else if ((this->match_view(view, view_size, fnoffset,
3673 "\x4c\x8d\x94\x24", 4)
3674 || this->match_view(view, view_size, fnoffset,
3675 "\x4c\x8d\x9c\x24", 4))
3678 // This is loading an offset from the stack pointer for a
3679 // comparison. The offset is negative, so we decrease the
3680 // offset by the amount of space we need for the stack. This
3681 // means we will avoid calling __morestack if there happens to
3682 // be plenty of space on the stack already.
3683 unsigned char* pval = view + fnoffset + 4;
3684 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3685 val -= parameters->options().split_stack_adjust_size();
3686 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3690 if (!object->has_no_split_stack())
3691 object->error(_("failed to match split-stack sequence at "
3692 "section %u offset %0zx"),
3693 shndx, static_cast<size_t>(fnoffset));
3697 // We have to change the function so that it calls
3698 // __morestack_non_split instead of __morestack. The former will
3699 // allocate additional stack space.
3700 *from = "__morestack";
3701 *to = "__morestack_non_split";
3704 // The selector for x86_64 object files.
3706 class Target_selector_x86_64 : public Target_selector_freebsd
3709 Target_selector_x86_64()
3710 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3711 "elf64-x86-64-freebsd", "elf_x86_64")
3715 do_instantiate_target()
3716 { return new Target_x86_64(); }
3720 Target_selector_x86_64 target_selector_x86_64;
3722 } // End anonymous namespace.