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 // Return the base for a DW_EH_PE_datarel encoding.
355 do_ehframe_datarel_base() const;
357 // Adjust -fsplit-stack code which calls non-split-stack code.
359 do_calls_non_split(Relobj* object, unsigned int shndx,
360 section_offset_type fnoffset, section_size_type fnsize,
361 unsigned char* view, section_size_type view_size,
362 std::string* from, std::string* to) const;
364 // Return the size of the GOT section.
368 gold_assert(this->got_ != NULL);
369 return this->got_->data_size();
372 // Return the number of entries in the GOT.
374 got_entry_count() const
376 if (this->got_ == NULL)
378 return this->got_size() / 8;
381 // Return the number of entries in the PLT.
383 plt_entry_count() const;
385 // Return the offset of the first non-reserved PLT entry.
387 first_plt_entry_offset() const;
389 // Return the size of each PLT entry.
391 plt_entry_size() const;
393 // Create the GOT section for an incremental update.
394 Output_data_got<64, false>*
395 init_got_plt_for_update(Symbol_table* symtab,
397 unsigned int got_count,
398 unsigned int plt_count);
400 // Reserve a GOT entry for a local symbol, and regenerate any
401 // necessary dynamic relocations.
403 reserve_local_got_entry(unsigned int got_index,
404 Sized_relobj<64, false>* obj,
406 unsigned int got_type);
408 // Reserve a GOT entry for a global symbol, and regenerate any
409 // necessary dynamic relocations.
411 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
412 unsigned int got_type);
414 // Register an existing PLT entry for a global symbol.
416 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
418 // Force a COPY relocation for a given symbol.
420 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
422 // Apply an incremental relocation.
424 apply_relocation(const Relocate_info<64, false>* relinfo,
425 elfcpp::Elf_types<64>::Elf_Addr r_offset,
427 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
430 elfcpp::Elf_types<64>::Elf_Addr address,
431 section_size_type view_size);
433 // Add a new reloc argument, returning the index in the vector.
435 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
437 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
438 return this->tlsdesc_reloc_info_.size() - 1;
442 // The class which scans relocations.
447 : issued_non_pic_error_(false)
451 get_reference_flags(unsigned int r_type);
454 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
455 Sized_relobj_file<64, false>* object,
456 unsigned int data_shndx,
457 Output_section* output_section,
458 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
459 const elfcpp::Sym<64, false>& lsym);
462 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
463 Sized_relobj_file<64, false>* object,
464 unsigned int data_shndx,
465 Output_section* output_section,
466 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
470 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
471 Target_x86_64* target,
472 Sized_relobj_file<64, false>* object,
473 unsigned int data_shndx,
474 Output_section* output_section,
475 const elfcpp::Rela<64, false>& reloc,
477 const elfcpp::Sym<64, false>& lsym);
480 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
481 Target_x86_64* target,
482 Sized_relobj_file<64, false>* object,
483 unsigned int data_shndx,
484 Output_section* output_section,
485 const elfcpp::Rela<64, false>& reloc,
491 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
494 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
498 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
501 possible_function_pointer_reloc(unsigned int r_type);
504 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
505 unsigned int r_type);
507 // Whether we have issued an error about a non-PIC compilation.
508 bool issued_non_pic_error_;
511 // The class which implements relocation.
516 : skip_call_tls_get_addr_(false)
521 if (this->skip_call_tls_get_addr_)
523 // FIXME: This needs to specify the location somehow.
524 gold_error(_("missing expected TLS relocation"));
528 // Do a relocation. Return false if the caller should not issue
529 // any warnings about this relocation.
531 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
532 size_t relnum, const elfcpp::Rela<64, false>&,
533 unsigned int r_type, const Sized_symbol<64>*,
534 const Symbol_value<64>*,
535 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
539 // Do a TLS relocation.
541 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
542 size_t relnum, const elfcpp::Rela<64, false>&,
543 unsigned int r_type, const Sized_symbol<64>*,
544 const Symbol_value<64>*,
545 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
548 // Do a TLS General-Dynamic to Initial-Exec transition.
550 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
551 Output_segment* tls_segment,
552 const elfcpp::Rela<64, false>&, unsigned int r_type,
553 elfcpp::Elf_types<64>::Elf_Addr value,
555 elfcpp::Elf_types<64>::Elf_Addr,
556 section_size_type view_size);
558 // Do a TLS General-Dynamic to Local-Exec transition.
560 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
561 Output_segment* tls_segment,
562 const elfcpp::Rela<64, false>&, unsigned int r_type,
563 elfcpp::Elf_types<64>::Elf_Addr value,
565 section_size_type view_size);
567 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
569 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
570 Output_segment* tls_segment,
571 const elfcpp::Rela<64, false>&, unsigned int r_type,
572 elfcpp::Elf_types<64>::Elf_Addr value,
574 elfcpp::Elf_types<64>::Elf_Addr,
575 section_size_type view_size);
577 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
579 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
580 Output_segment* tls_segment,
581 const elfcpp::Rela<64, false>&, unsigned int r_type,
582 elfcpp::Elf_types<64>::Elf_Addr value,
584 section_size_type view_size);
586 // Do a TLS Local-Dynamic to Local-Exec transition.
588 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
589 Output_segment* tls_segment,
590 const elfcpp::Rela<64, false>&, unsigned int r_type,
591 elfcpp::Elf_types<64>::Elf_Addr value,
593 section_size_type view_size);
595 // Do a TLS Initial-Exec to Local-Exec transition.
597 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
598 Output_segment* tls_segment,
599 const elfcpp::Rela<64, false>&, unsigned int r_type,
600 elfcpp::Elf_types<64>::Elf_Addr value,
602 section_size_type view_size);
604 // This is set if we should skip the next reloc, which should be a
605 // PLT32 reloc against ___tls_get_addr.
606 bool skip_call_tls_get_addr_;
609 // A class which returns the size required for a relocation type,
610 // used while scanning relocs during a relocatable link.
611 class Relocatable_size_for_reloc
615 get_size_for_reloc(unsigned int, Relobj*);
618 // Adjust TLS relocation type based on the options and whether this
619 // is a local symbol.
620 static tls::Tls_optimization
621 optimize_tls_reloc(bool is_final, int r_type);
623 // Get the GOT section, creating it if necessary.
624 Output_data_got<64, false>*
625 got_section(Symbol_table*, Layout*);
627 // Get the GOT PLT section.
629 got_plt_section() const
631 gold_assert(this->got_plt_ != NULL);
632 return this->got_plt_;
635 // Get the GOT section for TLSDESC entries.
636 Output_data_got<64, false>*
637 got_tlsdesc_section() const
639 gold_assert(this->got_tlsdesc_ != NULL);
640 return this->got_tlsdesc_;
643 // Create the PLT section.
645 make_plt_section(Symbol_table* symtab, Layout* layout);
647 // Create a PLT entry for a global symbol.
649 make_plt_entry(Symbol_table*, Layout*, Symbol*);
651 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
653 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
654 Sized_relobj_file<64, false>* relobj,
655 unsigned int local_sym_index);
657 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
659 define_tls_base_symbol(Symbol_table*, Layout*);
661 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
663 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
665 // Create a GOT entry for the TLS module index.
667 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
668 Sized_relobj_file<64, false>* object);
670 // Get the PLT section.
671 Output_data_plt_x86_64*
674 gold_assert(this->plt_ != NULL);
678 // Get the dynamic reloc section, creating it if necessary.
680 rela_dyn_section(Layout*);
682 // Get the section to use for TLSDESC relocations.
684 rela_tlsdesc_section(Layout*) const;
686 // Add a potential copy relocation.
688 copy_reloc(Symbol_table* symtab, Layout* layout,
689 Sized_relobj_file<64, false>* object,
690 unsigned int shndx, Output_section* output_section,
691 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
693 this->copy_relocs_.copy_reloc(symtab, layout,
694 symtab->get_sized_symbol<64>(sym),
695 object, shndx, output_section,
696 reloc, this->rela_dyn_section(layout));
699 // Information about this specific target which we pass to the
700 // general Target structure.
701 static const Target::Target_info x86_64_info;
703 // The types of GOT entries needed for this platform.
704 // These values are exposed to the ABI in an incremental link.
705 // Do not renumber existing values without changing the version
706 // number of the .gnu_incremental_inputs section.
709 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
710 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
711 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
712 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
715 // This type is used as the argument to the target specific
716 // relocation routines. The only target specific reloc is
717 // R_X86_64_TLSDESC against a local symbol.
720 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
721 : object(a_object), r_sym(a_r_sym)
724 // The object in which the local symbol is defined.
725 Sized_relobj_file<64, false>* object;
726 // The local symbol index in the object.
731 Output_data_got<64, false>* got_;
733 Output_data_plt_x86_64* plt_;
734 // The GOT PLT section.
735 Output_data_space* got_plt_;
736 // The GOT section for TLSDESC relocations.
737 Output_data_got<64, false>* got_tlsdesc_;
738 // The _GLOBAL_OFFSET_TABLE_ symbol.
739 Symbol* global_offset_table_;
740 // The dynamic reloc section.
741 Reloc_section* rela_dyn_;
742 // Relocs saved to avoid a COPY reloc.
743 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
744 // Space for variables copied with a COPY reloc.
745 Output_data_space* dynbss_;
746 // Offset of the GOT entry for the TLS module index.
747 unsigned int got_mod_index_offset_;
748 // We handle R_X86_64_TLSDESC against a local symbol as a target
749 // specific relocation. Here we store the object and local symbol
750 // index for the relocation.
751 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
752 // True if the _TLS_MODULE_BASE_ symbol has been defined.
753 bool tls_base_symbol_defined_;
756 const Target::Target_info Target_x86_64::x86_64_info =
759 false, // is_big_endian
760 elfcpp::EM_X86_64, // machine_code
761 false, // has_make_symbol
762 false, // has_resolve
763 true, // has_code_fill
764 true, // is_default_stack_executable
765 true, // can_icf_inline_merge_sections
767 "/lib/ld64.so.1", // program interpreter
768 0x400000, // default_text_segment_address
769 0x1000, // abi_pagesize (overridable by -z max-page-size)
770 0x1000, // common_pagesize (overridable by -z common-page-size)
771 elfcpp::SHN_UNDEF, // small_common_shndx
772 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
773 0, // small_common_section_flags
774 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
775 NULL, // attributes_section
776 NULL // attributes_vendor
779 // This is called when a new output section is created. This is where
780 // we handle the SHF_X86_64_LARGE.
783 Target_x86_64::do_new_output_section(Output_section* os) const
785 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
786 os->set_is_large_section();
789 // Get the GOT section, creating it if necessary.
791 Output_data_got<64, false>*
792 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
794 if (this->got_ == NULL)
796 gold_assert(symtab != NULL && layout != NULL);
798 this->got_ = new Output_data_got<64, false>();
800 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
802 | elfcpp::SHF_WRITE),
803 this->got_, ORDER_RELRO_LAST,
806 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
807 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
809 | elfcpp::SHF_WRITE),
810 this->got_plt_, ORDER_NON_RELRO_FIRST,
813 // The first three entries are reserved.
814 this->got_plt_->set_current_data_size(3 * 8);
816 // Those bytes can go into the relro segment.
817 layout->increase_relro(3 * 8);
819 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
820 this->global_offset_table_ =
821 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
822 Symbol_table::PREDEFINED,
824 0, 0, elfcpp::STT_OBJECT,
826 elfcpp::STV_HIDDEN, 0,
829 // If there are any TLSDESC relocations, they get GOT entries in
830 // .got.plt after the jump slot entries.
831 this->got_tlsdesc_ = new Output_data_got<64, false>();
832 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
834 | elfcpp::SHF_WRITE),
836 ORDER_NON_RELRO_FIRST, false);
842 // Get the dynamic reloc section, creating it if necessary.
844 Target_x86_64::Reloc_section*
845 Target_x86_64::rela_dyn_section(Layout* layout)
847 if (this->rela_dyn_ == NULL)
849 gold_assert(layout != NULL);
850 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
851 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
852 elfcpp::SHF_ALLOC, this->rela_dyn_,
853 ORDER_DYNAMIC_RELOCS, false);
855 return this->rela_dyn_;
858 // Initialize the PLT section.
861 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
863 this->rel_ = new Reloc_section(false);
864 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
865 elfcpp::SHF_ALLOC, this->rel_,
866 ORDER_DYNAMIC_PLT_RELOCS, false);
868 if (parameters->doing_static_link())
870 // A statically linked executable will only have a .rela.plt
871 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
872 // symbols. The library will use these symbols to locate the
873 // IRELATIVE relocs at program startup time.
874 symtab->define_in_output_data("__rela_iplt_start", NULL,
875 Symbol_table::PREDEFINED,
876 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
877 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
879 symtab->define_in_output_data("__rela_iplt_end", NULL,
880 Symbol_table::PREDEFINED,
881 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
882 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
886 // Add unwind information if requested.
887 if (parameters->options().ld_generated_unwind_info())
888 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
889 plt_eh_frame_fde, plt_eh_frame_fde_size);
893 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
895 os->set_entsize(plt_entry_size);
898 // Add an entry to the PLT.
901 Output_data_plt_x86_64::add_entry(Symbol* gsym)
903 gold_assert(!gsym->has_plt_offset());
905 unsigned int plt_index;
907 section_offset_type got_offset;
909 if (!this->is_data_size_valid())
911 // Note that when setting the PLT offset we skip the initial
912 // reserved PLT entry.
913 plt_index = this->count_ + 1;
914 plt_offset = plt_index * plt_entry_size;
918 got_offset = (plt_index - 1 + 3) * 8;
919 gold_assert(got_offset == this->got_plt_->current_data_size());
921 // Every PLT entry needs a GOT entry which points back to the PLT
922 // entry (this will be changed by the dynamic linker, normally
923 // lazily when the function is called).
924 this->got_plt_->set_current_data_size(got_offset + 8);
928 // For incremental updates, find an available slot.
929 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
930 if (plt_offset == -1)
931 gold_fallback(_("out of patch space (PLT);"
932 " relink with --incremental-full"));
934 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
935 // can be calculated from the PLT index, adjusting for the three
936 // reserved entries at the beginning of the GOT.
937 plt_index = plt_offset / plt_entry_size - 1;
938 got_offset = (plt_index - 1 + 3) * 8;
941 gsym->set_plt_offset(plt_offset);
943 // Every PLT entry needs a reloc.
944 this->add_relocation(gsym, got_offset);
946 // Note that we don't need to save the symbol. The contents of the
947 // PLT are independent of which symbols are used. The symbols only
948 // appear in the relocations.
951 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
955 Output_data_plt_x86_64::add_local_ifunc_entry(
956 Sized_relobj_file<64, false>* relobj,
957 unsigned int local_sym_index)
959 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
962 section_offset_type got_offset = this->got_plt_->current_data_size();
964 // Every PLT entry needs a GOT entry which points back to the PLT
966 this->got_plt_->set_current_data_size(got_offset + 8);
968 // Every PLT entry needs a reloc.
969 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
970 elfcpp::R_X86_64_IRELATIVE,
971 this->got_plt_, got_offset, 0);
976 // Add the relocation for a PLT entry.
979 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
981 if (gsym->type() == elfcpp::STT_GNU_IFUNC
982 && gsym->can_use_relative_reloc(false))
983 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
984 this->got_plt_, got_offset, 0);
987 gsym->set_needs_dynsym_entry();
988 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
993 // Return where the TLSDESC relocations should go, creating it if
994 // necessary. These follow the JUMP_SLOT relocations.
996 Output_data_plt_x86_64::Reloc_section*
997 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
999 if (this->tlsdesc_rel_ == NULL)
1001 this->tlsdesc_rel_ = new Reloc_section(false);
1002 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1003 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1004 ORDER_DYNAMIC_PLT_RELOCS, false);
1005 gold_assert(this->tlsdesc_rel_->output_section() ==
1006 this->rel_->output_section());
1008 return this->tlsdesc_rel_;
1011 // Set the final size.
1013 Output_data_plt_x86_64::set_final_data_size()
1015 unsigned int count = this->count_;
1016 if (this->has_tlsdesc_entry())
1018 this->set_data_size((count + 1) * plt_entry_size);
1021 // The first entry in the PLT for an executable.
1023 const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1025 // From AMD64 ABI Draft 0.98, page 76
1026 0xff, 0x35, // pushq contents of memory address
1027 0, 0, 0, 0, // replaced with address of .got + 8
1028 0xff, 0x25, // jmp indirect
1029 0, 0, 0, 0, // replaced with address of .got + 16
1030 0x90, 0x90, 0x90, 0x90 // noop (x4)
1033 // Subsequent entries in the PLT for an executable.
1035 const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1037 // From AMD64 ABI Draft 0.98, page 76
1038 0xff, 0x25, // jmpq indirect
1039 0, 0, 0, 0, // replaced with address of symbol in .got
1040 0x68, // pushq immediate
1041 0, 0, 0, 0, // replaced with offset into relocation table
1042 0xe9, // jmpq relative
1043 0, 0, 0, 0 // replaced with offset to start of .plt
1046 // The reserved TLSDESC entry in the PLT for an executable.
1048 const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1050 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1051 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1052 0xff, 0x35, // pushq x(%rip)
1053 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1054 0xff, 0x25, // jmpq *y(%rip)
1055 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1060 // The .eh_frame unwind information for the PLT.
1063 Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1066 'z', // Augmentation: augmentation size included.
1067 'R', // Augmentation: FDE encoding included.
1068 '\0', // End of augmentation string.
1069 1, // Code alignment factor.
1070 0x78, // Data alignment factor.
1071 16, // Return address column.
1072 1, // Augmentation size.
1073 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1074 | elfcpp::DW_EH_PE_sdata4),
1075 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1076 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1077 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1082 Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1084 0, 0, 0, 0, // Replaced with offset to .plt.
1085 0, 0, 0, 0, // Replaced with size of .plt.
1086 0, // Augmentation size.
1087 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1088 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1089 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1090 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1091 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1092 11, // Block length.
1093 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1094 elfcpp::DW_OP_breg16, 0, // Push %rip.
1095 elfcpp::DW_OP_lit15, // Push 0xf.
1096 elfcpp::DW_OP_and, // & (%rip & 0xf).
1097 elfcpp::DW_OP_lit11, // Push 0xb.
1098 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1099 elfcpp::DW_OP_lit3, // Push 3.
1100 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1101 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1102 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1108 // Write out the PLT. This uses the hand-coded instructions above,
1109 // and adjusts them as needed. This is specified by the AMD64 ABI.
1112 Output_data_plt_x86_64::do_write(Output_file* of)
1114 const off_t offset = this->offset();
1115 const section_size_type oview_size =
1116 convert_to_section_size_type(this->data_size());
1117 unsigned char* const oview = of->get_output_view(offset, oview_size);
1119 const off_t got_file_offset = this->got_plt_->offset();
1120 const section_size_type got_size =
1121 convert_to_section_size_type(this->got_plt_->data_size());
1122 unsigned char* const got_view = of->get_output_view(got_file_offset,
1125 unsigned char* pov = oview;
1127 // The base address of the .plt section.
1128 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1129 // The base address of the .got section.
1130 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1131 // The base address of the PLT portion of the .got section,
1132 // which is where the GOT pointer will point, and where the
1133 // three reserved GOT entries are located.
1134 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1136 memcpy(pov, first_plt_entry, plt_entry_size);
1137 // We do a jmp relative to the PC at the end of this instruction.
1138 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1140 - (plt_address + 6)));
1141 elfcpp::Swap<32, false>::writeval(pov + 8,
1143 - (plt_address + 12)));
1144 pov += plt_entry_size;
1146 unsigned char* got_pov = got_view;
1148 memset(got_pov, 0, 24);
1151 unsigned int plt_offset = plt_entry_size;
1152 unsigned int got_offset = 24;
1153 const unsigned int count = this->count_;
1154 for (unsigned int plt_index = 0;
1157 pov += plt_entry_size,
1159 plt_offset += plt_entry_size,
1162 // Set and adjust the PLT entry itself.
1163 memcpy(pov, plt_entry, plt_entry_size);
1164 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1165 (got_address + got_offset
1166 - (plt_address + plt_offset
1169 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1170 elfcpp::Swap<32, false>::writeval(pov + 12,
1171 - (plt_offset + plt_entry_size));
1173 // Set the entry in the GOT.
1174 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1177 if (this->has_tlsdesc_entry())
1179 // Set and adjust the reserved TLSDESC PLT entry.
1180 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1181 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1182 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1184 - (plt_address + plt_offset
1186 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1188 + tlsdesc_got_offset
1189 - (plt_address + plt_offset
1191 pov += plt_entry_size;
1194 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1195 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1197 of->write_output_view(offset, oview_size, oview);
1198 of->write_output_view(got_file_offset, got_size, got_view);
1201 // Create the PLT section.
1204 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1206 if (this->plt_ == NULL)
1208 // Create the GOT sections first.
1209 this->got_section(symtab, layout);
1211 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1213 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1215 | elfcpp::SHF_EXECINSTR),
1216 this->plt_, ORDER_PLT, false);
1218 // Make the sh_info field of .rela.plt point to .plt.
1219 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1220 rela_plt_os->set_info_section(this->plt_->output_section());
1224 // Return the section for TLSDESC relocations.
1226 Target_x86_64::Reloc_section*
1227 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1229 return this->plt_section()->rela_tlsdesc(layout);
1232 // Create a PLT entry for a global symbol.
1235 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1238 if (gsym->has_plt_offset())
1241 if (this->plt_ == NULL)
1242 this->make_plt_section(symtab, layout);
1244 this->plt_->add_entry(gsym);
1247 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1250 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1251 Sized_relobj_file<64, false>* relobj,
1252 unsigned int local_sym_index)
1254 if (relobj->local_has_plt_offset(local_sym_index))
1256 if (this->plt_ == NULL)
1257 this->make_plt_section(symtab, layout);
1258 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1260 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1263 // Return the number of entries in the PLT.
1266 Target_x86_64::plt_entry_count() const
1268 if (this->plt_ == NULL)
1270 return this->plt_->entry_count();
1273 // Return the offset of the first non-reserved PLT entry.
1276 Target_x86_64::first_plt_entry_offset() const
1278 return Output_data_plt_x86_64::first_plt_entry_offset();
1281 // Return the size of each PLT entry.
1284 Target_x86_64::plt_entry_size() const
1286 return Output_data_plt_x86_64::get_plt_entry_size();
1289 // Create the GOT and PLT sections for an incremental update.
1291 Output_data_got<64, false>*
1292 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1294 unsigned int got_count,
1295 unsigned int plt_count)
1297 gold_assert(this->got_ == NULL);
1299 this->got_ = new Output_data_got<64, false>(got_count * 8);
1300 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1302 | elfcpp::SHF_WRITE),
1303 this->got_, ORDER_RELRO_LAST,
1306 // Add the three reserved entries.
1307 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1308 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1310 | elfcpp::SHF_WRITE),
1311 this->got_plt_, ORDER_NON_RELRO_FIRST,
1314 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1315 this->global_offset_table_ =
1316 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1317 Symbol_table::PREDEFINED,
1319 0, 0, elfcpp::STT_OBJECT,
1321 elfcpp::STV_HIDDEN, 0,
1324 // If there are any TLSDESC relocations, they get GOT entries in
1325 // .got.plt after the jump slot entries.
1326 // FIXME: Get the count for TLSDESC entries.
1327 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1328 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1329 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1331 ORDER_NON_RELRO_FIRST, false);
1333 // Create the PLT section.
1334 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1335 this->got_plt_, plt_count);
1336 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1337 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1338 this->plt_, ORDER_PLT, false);
1340 // Make the sh_info field of .rela.plt point to .plt.
1341 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1342 rela_plt_os->set_info_section(this->plt_->output_section());
1344 // Create the rela_dyn section.
1345 this->rela_dyn_section(layout);
1350 // Reserve a GOT entry for a local symbol, and regenerate any
1351 // necessary dynamic relocations.
1354 Target_x86_64::reserve_local_got_entry(
1355 unsigned int got_index,
1356 Sized_relobj<64, false>* obj,
1358 unsigned int got_type)
1360 unsigned int got_offset = got_index * 8;
1361 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1363 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1366 case GOT_TYPE_STANDARD:
1367 if (parameters->options().output_is_position_independent())
1368 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1369 this->got_, got_offset, 0);
1371 case GOT_TYPE_TLS_OFFSET:
1372 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1373 this->got_, got_offset, 0);
1375 case GOT_TYPE_TLS_PAIR:
1376 this->got_->reserve_slot(got_index + 1);
1377 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1378 this->got_, got_offset, 0);
1380 case GOT_TYPE_TLS_DESC:
1381 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1382 // this->got_->reserve_slot(got_index + 1);
1383 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1384 // this->got_, got_offset, 0);
1391 // Reserve a GOT entry for a global symbol, and regenerate any
1392 // necessary dynamic relocations.
1395 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1396 unsigned int got_type)
1398 unsigned int got_offset = got_index * 8;
1399 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1401 this->got_->reserve_global(got_index, gsym, got_type);
1404 case GOT_TYPE_STANDARD:
1405 if (!gsym->final_value_is_known())
1407 if (gsym->is_from_dynobj()
1408 || gsym->is_undefined()
1409 || gsym->is_preemptible()
1410 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1411 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1412 this->got_, got_offset, 0);
1414 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1415 this->got_, got_offset, 0);
1418 case GOT_TYPE_TLS_OFFSET:
1419 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1420 this->got_, got_offset, 0);
1422 case GOT_TYPE_TLS_PAIR:
1423 this->got_->reserve_slot(got_index + 1);
1424 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1425 this->got_, got_offset, 0);
1426 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1427 this->got_, got_offset + 8, 0);
1429 case GOT_TYPE_TLS_DESC:
1430 this->got_->reserve_slot(got_index + 1);
1431 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1432 this->got_, got_offset, 0);
1439 // Register an existing PLT entry for a global symbol.
1442 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1445 gold_assert(this->plt_ != NULL);
1446 gold_assert(!gsym->has_plt_offset());
1448 this->plt_->reserve_slot(plt_index);
1450 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1452 unsigned int got_offset = (plt_index + 3) * 8;
1453 this->plt_->add_relocation(gsym, got_offset);
1456 // Force a COPY relocation for a given symbol.
1459 Target_x86_64::emit_copy_reloc(
1460 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1462 this->copy_relocs_.emit_copy_reloc(symtab,
1463 symtab->get_sized_symbol<64>(sym),
1466 this->rela_dyn_section(NULL));
1469 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1472 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1474 if (this->tls_base_symbol_defined_)
1477 Output_segment* tls_segment = layout->tls_segment();
1478 if (tls_segment != NULL)
1480 bool is_exec = parameters->options().output_is_executable();
1481 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1482 Symbol_table::PREDEFINED,
1486 elfcpp::STV_HIDDEN, 0,
1488 ? Symbol::SEGMENT_END
1489 : Symbol::SEGMENT_START),
1492 this->tls_base_symbol_defined_ = true;
1495 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1498 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1501 if (this->plt_ == NULL)
1502 this->make_plt_section(symtab, layout);
1504 if (!this->plt_->has_tlsdesc_entry())
1506 // Allocate the TLSDESC_GOT entry.
1507 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1508 unsigned int got_offset = got->add_constant(0);
1510 // Allocate the TLSDESC_PLT entry.
1511 this->plt_->reserve_tlsdesc_entry(got_offset);
1515 // Create a GOT entry for the TLS module index.
1518 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1519 Sized_relobj_file<64, false>* object)
1521 if (this->got_mod_index_offset_ == -1U)
1523 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1524 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1525 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1526 unsigned int got_offset = got->add_constant(0);
1527 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1529 got->add_constant(0);
1530 this->got_mod_index_offset_ = got_offset;
1532 return this->got_mod_index_offset_;
1535 // Optimize the TLS relocation type based on what we know about the
1536 // symbol. IS_FINAL is true if the final address of this symbol is
1537 // known at link time.
1539 tls::Tls_optimization
1540 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1542 // If we are generating a shared library, then we can't do anything
1544 if (parameters->options().shared())
1545 return tls::TLSOPT_NONE;
1549 case elfcpp::R_X86_64_TLSGD:
1550 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1551 case elfcpp::R_X86_64_TLSDESC_CALL:
1552 // These are General-Dynamic which permits fully general TLS
1553 // access. Since we know that we are generating an executable,
1554 // we can convert this to Initial-Exec. If we also know that
1555 // this is a local symbol, we can further switch to Local-Exec.
1557 return tls::TLSOPT_TO_LE;
1558 return tls::TLSOPT_TO_IE;
1560 case elfcpp::R_X86_64_TLSLD:
1561 // This is Local-Dynamic, which refers to a local symbol in the
1562 // dynamic TLS block. Since we know that we generating an
1563 // executable, we can switch to Local-Exec.
1564 return tls::TLSOPT_TO_LE;
1566 case elfcpp::R_X86_64_DTPOFF32:
1567 case elfcpp::R_X86_64_DTPOFF64:
1568 // Another Local-Dynamic reloc.
1569 return tls::TLSOPT_TO_LE;
1571 case elfcpp::R_X86_64_GOTTPOFF:
1572 // These are Initial-Exec relocs which get the thread offset
1573 // from the GOT. If we know that we are linking against the
1574 // local symbol, we can switch to Local-Exec, which links the
1575 // thread offset into the instruction.
1577 return tls::TLSOPT_TO_LE;
1578 return tls::TLSOPT_NONE;
1580 case elfcpp::R_X86_64_TPOFF32:
1581 // When we already have Local-Exec, there is nothing further we
1583 return tls::TLSOPT_NONE;
1590 // Get the Reference_flags for a particular relocation.
1593 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1597 case elfcpp::R_X86_64_NONE:
1598 case elfcpp::R_X86_64_GNU_VTINHERIT:
1599 case elfcpp::R_X86_64_GNU_VTENTRY:
1600 case elfcpp::R_X86_64_GOTPC32:
1601 case elfcpp::R_X86_64_GOTPC64:
1602 // No symbol reference.
1605 case elfcpp::R_X86_64_64:
1606 case elfcpp::R_X86_64_32:
1607 case elfcpp::R_X86_64_32S:
1608 case elfcpp::R_X86_64_16:
1609 case elfcpp::R_X86_64_8:
1610 return Symbol::ABSOLUTE_REF;
1612 case elfcpp::R_X86_64_PC64:
1613 case elfcpp::R_X86_64_PC32:
1614 case elfcpp::R_X86_64_PC16:
1615 case elfcpp::R_X86_64_PC8:
1616 case elfcpp::R_X86_64_GOTOFF64:
1617 return Symbol::RELATIVE_REF;
1619 case elfcpp::R_X86_64_PLT32:
1620 case elfcpp::R_X86_64_PLTOFF64:
1621 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1623 case elfcpp::R_X86_64_GOT64:
1624 case elfcpp::R_X86_64_GOT32:
1625 case elfcpp::R_X86_64_GOTPCREL64:
1626 case elfcpp::R_X86_64_GOTPCREL:
1627 case elfcpp::R_X86_64_GOTPLT64:
1629 return Symbol::ABSOLUTE_REF;
1631 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1632 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1633 case elfcpp::R_X86_64_TLSDESC_CALL:
1634 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1635 case elfcpp::R_X86_64_DTPOFF32:
1636 case elfcpp::R_X86_64_DTPOFF64:
1637 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1638 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1639 return Symbol::TLS_REF;
1641 case elfcpp::R_X86_64_COPY:
1642 case elfcpp::R_X86_64_GLOB_DAT:
1643 case elfcpp::R_X86_64_JUMP_SLOT:
1644 case elfcpp::R_X86_64_RELATIVE:
1645 case elfcpp::R_X86_64_IRELATIVE:
1646 case elfcpp::R_X86_64_TPOFF64:
1647 case elfcpp::R_X86_64_DTPMOD64:
1648 case elfcpp::R_X86_64_TLSDESC:
1649 case elfcpp::R_X86_64_SIZE32:
1650 case elfcpp::R_X86_64_SIZE64:
1652 // Not expected. We will give an error later.
1657 // Report an unsupported relocation against a local symbol.
1660 Target_x86_64::Scan::unsupported_reloc_local(
1661 Sized_relobj_file<64, false>* object,
1662 unsigned int r_type)
1664 gold_error(_("%s: unsupported reloc %u against local symbol"),
1665 object->name().c_str(), r_type);
1668 // We are about to emit a dynamic relocation of type R_TYPE. If the
1669 // dynamic linker does not support it, issue an error. The GNU linker
1670 // only issues a non-PIC error for an allocated read-only section.
1671 // Here we know the section is allocated, but we don't know that it is
1672 // read-only. But we check for all the relocation types which the
1673 // glibc dynamic linker supports, so it seems appropriate to issue an
1674 // error even if the section is not read-only. If GSYM is not NULL,
1675 // it is the symbol the relocation is against; if it is NULL, the
1676 // relocation is against a local symbol.
1679 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1684 // These are the relocation types supported by glibc for x86_64
1685 // which should always work.
1686 case elfcpp::R_X86_64_RELATIVE:
1687 case elfcpp::R_X86_64_IRELATIVE:
1688 case elfcpp::R_X86_64_GLOB_DAT:
1689 case elfcpp::R_X86_64_JUMP_SLOT:
1690 case elfcpp::R_X86_64_DTPMOD64:
1691 case elfcpp::R_X86_64_DTPOFF64:
1692 case elfcpp::R_X86_64_TPOFF64:
1693 case elfcpp::R_X86_64_64:
1694 case elfcpp::R_X86_64_COPY:
1697 // glibc supports these reloc types, but they can overflow.
1698 case elfcpp::R_X86_64_PC32:
1699 // A PC relative reference is OK against a local symbol or if
1700 // the symbol is defined locally.
1702 || (!gsym->is_from_dynobj()
1703 && !gsym->is_undefined()
1704 && !gsym->is_preemptible()))
1707 case elfcpp::R_X86_64_32:
1708 if (this->issued_non_pic_error_)
1710 gold_assert(parameters->options().output_is_position_independent());
1712 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1713 "overflow at runtime; recompile with -fPIC"));
1715 object->error(_("requires dynamic %s reloc against '%s' which may "
1716 "overflow at runtime; recompile with -fPIC"),
1717 (r_type == elfcpp::R_X86_64_32
1721 this->issued_non_pic_error_ = true;
1725 // This prevents us from issuing more than one error per reloc
1726 // section. But we can still wind up issuing more than one
1727 // error per object file.
1728 if (this->issued_non_pic_error_)
1730 gold_assert(parameters->options().output_is_position_independent());
1731 object->error(_("requires unsupported dynamic reloc %u; "
1732 "recompile with -fPIC"),
1734 this->issued_non_pic_error_ = true;
1737 case elfcpp::R_X86_64_NONE:
1742 // Return whether we need to make a PLT entry for a relocation of the
1743 // given type against a STT_GNU_IFUNC symbol.
1746 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1747 Sized_relobj_file<64, false>* object,
1748 unsigned int r_type)
1750 int flags = Scan::get_reference_flags(r_type);
1751 if (flags & Symbol::TLS_REF)
1752 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1753 object->name().c_str(), r_type);
1757 // Scan a relocation for a local symbol.
1760 Target_x86_64::Scan::local(Symbol_table* symtab,
1762 Target_x86_64* target,
1763 Sized_relobj_file<64, false>* object,
1764 unsigned int data_shndx,
1765 Output_section* output_section,
1766 const elfcpp::Rela<64, false>& reloc,
1767 unsigned int r_type,
1768 const elfcpp::Sym<64, false>& lsym)
1770 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1771 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1772 && this->reloc_needs_plt_for_ifunc(object, r_type))
1774 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1775 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1780 case elfcpp::R_X86_64_NONE:
1781 case elfcpp::R_X86_64_GNU_VTINHERIT:
1782 case elfcpp::R_X86_64_GNU_VTENTRY:
1785 case elfcpp::R_X86_64_64:
1786 // If building a shared library (or a position-independent
1787 // executable), we need to create a dynamic relocation for this
1788 // location. The relocation applied at link time will apply the
1789 // link-time value, so we flag the location with an
1790 // R_X86_64_RELATIVE relocation so the dynamic loader can
1791 // relocate it easily.
1792 if (parameters->options().output_is_position_independent())
1794 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1795 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1796 rela_dyn->add_local_relative(object, r_sym,
1797 elfcpp::R_X86_64_RELATIVE,
1798 output_section, data_shndx,
1799 reloc.get_r_offset(),
1800 reloc.get_r_addend());
1804 case elfcpp::R_X86_64_32:
1805 case elfcpp::R_X86_64_32S:
1806 case elfcpp::R_X86_64_16:
1807 case elfcpp::R_X86_64_8:
1808 // If building a shared library (or a position-independent
1809 // executable), we need to create a dynamic relocation for this
1810 // location. We can't use an R_X86_64_RELATIVE relocation
1811 // because that is always a 64-bit relocation.
1812 if (parameters->options().output_is_position_independent())
1814 this->check_non_pic(object, r_type, NULL);
1816 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1817 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1818 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1819 rela_dyn->add_local(object, r_sym, r_type, output_section,
1820 data_shndx, reloc.get_r_offset(),
1821 reloc.get_r_addend());
1824 gold_assert(lsym.get_st_value() == 0);
1825 unsigned int shndx = lsym.get_st_shndx();
1827 shndx = object->adjust_sym_shndx(r_sym, shndx,
1830 object->error(_("section symbol %u has bad shndx %u"),
1833 rela_dyn->add_local_section(object, shndx,
1834 r_type, output_section,
1835 data_shndx, reloc.get_r_offset(),
1836 reloc.get_r_addend());
1841 case elfcpp::R_X86_64_PC64:
1842 case elfcpp::R_X86_64_PC32:
1843 case elfcpp::R_X86_64_PC16:
1844 case elfcpp::R_X86_64_PC8:
1847 case elfcpp::R_X86_64_PLT32:
1848 // Since we know this is a local symbol, we can handle this as a
1852 case elfcpp::R_X86_64_GOTPC32:
1853 case elfcpp::R_X86_64_GOTOFF64:
1854 case elfcpp::R_X86_64_GOTPC64:
1855 case elfcpp::R_X86_64_PLTOFF64:
1856 // We need a GOT section.
1857 target->got_section(symtab, layout);
1858 // For PLTOFF64, we'd normally want a PLT section, but since we
1859 // know this is a local symbol, no PLT is needed.
1862 case elfcpp::R_X86_64_GOT64:
1863 case elfcpp::R_X86_64_GOT32:
1864 case elfcpp::R_X86_64_GOTPCREL64:
1865 case elfcpp::R_X86_64_GOTPCREL:
1866 case elfcpp::R_X86_64_GOTPLT64:
1868 // The symbol requires a GOT entry.
1869 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1870 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1872 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1873 // lets function pointers compare correctly with shared
1874 // libraries. Otherwise we would need an IRELATIVE reloc.
1876 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1877 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1879 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1882 // If we are generating a shared object, we need to add a
1883 // dynamic relocation for this symbol's GOT entry.
1884 if (parameters->options().output_is_position_independent())
1886 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1887 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1888 if (r_type != elfcpp::R_X86_64_GOT32)
1890 unsigned int got_offset =
1891 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1892 rela_dyn->add_local_relative(object, r_sym,
1893 elfcpp::R_X86_64_RELATIVE,
1894 got, got_offset, 0);
1898 this->check_non_pic(object, r_type, NULL);
1900 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1901 rela_dyn->add_local(
1902 object, r_sym, r_type, got,
1903 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1907 // For GOTPLT64, we'd normally want a PLT section, but since
1908 // we know this is a local symbol, no PLT is needed.
1912 case elfcpp::R_X86_64_COPY:
1913 case elfcpp::R_X86_64_GLOB_DAT:
1914 case elfcpp::R_X86_64_JUMP_SLOT:
1915 case elfcpp::R_X86_64_RELATIVE:
1916 case elfcpp::R_X86_64_IRELATIVE:
1917 // These are outstanding tls relocs, which are unexpected when linking
1918 case elfcpp::R_X86_64_TPOFF64:
1919 case elfcpp::R_X86_64_DTPMOD64:
1920 case elfcpp::R_X86_64_TLSDESC:
1921 gold_error(_("%s: unexpected reloc %u in object file"),
1922 object->name().c_str(), r_type);
1925 // These are initial tls relocs, which are expected when linking
1926 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1927 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1928 case elfcpp::R_X86_64_TLSDESC_CALL:
1929 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1930 case elfcpp::R_X86_64_DTPOFF32:
1931 case elfcpp::R_X86_64_DTPOFF64:
1932 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1933 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1935 bool output_is_shared = parameters->options().shared();
1936 const tls::Tls_optimization optimized_type
1937 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1940 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1941 if (optimized_type == tls::TLSOPT_NONE)
1943 // Create a pair of GOT entries for the module index and
1944 // dtv-relative offset.
1945 Output_data_got<64, false>* got
1946 = target->got_section(symtab, layout);
1947 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1948 unsigned int shndx = lsym.get_st_shndx();
1950 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1952 object->error(_("local symbol %u has bad shndx %u"),
1955 got->add_local_pair_with_rela(object, r_sym,
1958 target->rela_dyn_section(layout),
1959 elfcpp::R_X86_64_DTPMOD64, 0);
1961 else if (optimized_type != tls::TLSOPT_TO_LE)
1962 unsupported_reloc_local(object, r_type);
1965 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1966 target->define_tls_base_symbol(symtab, layout);
1967 if (optimized_type == tls::TLSOPT_NONE)
1969 // Create reserved PLT and GOT entries for the resolver.
1970 target->reserve_tlsdesc_entries(symtab, layout);
1972 // Generate a double GOT entry with an
1973 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1974 // is resolved lazily, so the GOT entry needs to be in
1975 // an area in .got.plt, not .got. Call got_section to
1976 // make sure the section has been created.
1977 target->got_section(symtab, layout);
1978 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1979 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1980 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1982 unsigned int got_offset = got->add_constant(0);
1983 got->add_constant(0);
1984 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1986 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1987 // We store the arguments we need in a vector, and
1988 // use the index into the vector as the parameter
1989 // to pass to the target specific routines.
1990 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1991 void* arg = reinterpret_cast<void*>(intarg);
1992 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1993 got, got_offset, 0);
1996 else if (optimized_type != tls::TLSOPT_TO_LE)
1997 unsupported_reloc_local(object, r_type);
2000 case elfcpp::R_X86_64_TLSDESC_CALL:
2003 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2004 if (optimized_type == tls::TLSOPT_NONE)
2006 // Create a GOT entry for the module index.
2007 target->got_mod_index_entry(symtab, layout, object);
2009 else if (optimized_type != tls::TLSOPT_TO_LE)
2010 unsupported_reloc_local(object, r_type);
2013 case elfcpp::R_X86_64_DTPOFF32:
2014 case elfcpp::R_X86_64_DTPOFF64:
2017 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2018 layout->set_has_static_tls();
2019 if (optimized_type == tls::TLSOPT_NONE)
2021 // Create a GOT entry for the tp-relative offset.
2022 Output_data_got<64, false>* got
2023 = target->got_section(symtab, layout);
2024 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
2025 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
2026 target->rela_dyn_section(layout),
2027 elfcpp::R_X86_64_TPOFF64);
2029 else if (optimized_type != tls::TLSOPT_TO_LE)
2030 unsupported_reloc_local(object, r_type);
2033 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2034 layout->set_has_static_tls();
2035 if (output_is_shared)
2036 unsupported_reloc_local(object, r_type);
2045 case elfcpp::R_X86_64_SIZE32:
2046 case elfcpp::R_X86_64_SIZE64:
2048 gold_error(_("%s: unsupported reloc %u against local symbol"),
2049 object->name().c_str(), r_type);
2055 // Report an unsupported relocation against a global symbol.
2058 Target_x86_64::Scan::unsupported_reloc_global(
2059 Sized_relobj_file<64, false>* object,
2060 unsigned int r_type,
2063 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2064 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2067 // Returns true if this relocation type could be that of a function pointer.
2069 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2073 case elfcpp::R_X86_64_64:
2074 case elfcpp::R_X86_64_32:
2075 case elfcpp::R_X86_64_32S:
2076 case elfcpp::R_X86_64_16:
2077 case elfcpp::R_X86_64_8:
2078 case elfcpp::R_X86_64_GOT64:
2079 case elfcpp::R_X86_64_GOT32:
2080 case elfcpp::R_X86_64_GOTPCREL64:
2081 case elfcpp::R_X86_64_GOTPCREL:
2082 case elfcpp::R_X86_64_GOTPLT64:
2090 // For safe ICF, scan a relocation for a local symbol to check if it
2091 // corresponds to a function pointer being taken. In that case mark
2092 // the function whose pointer was taken as not foldable.
2095 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2099 Sized_relobj_file<64, false>* ,
2102 const elfcpp::Rela<64, false>& ,
2103 unsigned int r_type,
2104 const elfcpp::Sym<64, false>&)
2106 // When building a shared library, do not fold any local symbols as it is
2107 // not possible to distinguish pointer taken versus a call by looking at
2108 // the relocation types.
2109 return (parameters->options().shared()
2110 || possible_function_pointer_reloc(r_type));
2113 // For safe ICF, scan a relocation for a global symbol to check if it
2114 // corresponds to a function pointer being taken. In that case mark
2115 // the function whose pointer was taken as not foldable.
2118 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2122 Sized_relobj_file<64, false>* ,
2125 const elfcpp::Rela<64, false>& ,
2126 unsigned int r_type,
2129 // When building a shared library, do not fold symbols whose visibility
2130 // is hidden, internal or protected.
2131 return ((parameters->options().shared()
2132 && (gsym->visibility() == elfcpp::STV_INTERNAL
2133 || gsym->visibility() == elfcpp::STV_PROTECTED
2134 || gsym->visibility() == elfcpp::STV_HIDDEN))
2135 || possible_function_pointer_reloc(r_type));
2138 // Scan a relocation for a global symbol.
2141 Target_x86_64::Scan::global(Symbol_table* symtab,
2143 Target_x86_64* target,
2144 Sized_relobj_file<64, false>* object,
2145 unsigned int data_shndx,
2146 Output_section* output_section,
2147 const elfcpp::Rela<64, false>& reloc,
2148 unsigned int r_type,
2151 // A STT_GNU_IFUNC symbol may require a PLT entry.
2152 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2153 && this->reloc_needs_plt_for_ifunc(object, r_type))
2154 target->make_plt_entry(symtab, layout, gsym);
2158 case elfcpp::R_X86_64_NONE:
2159 case elfcpp::R_X86_64_GNU_VTINHERIT:
2160 case elfcpp::R_X86_64_GNU_VTENTRY:
2163 case elfcpp::R_X86_64_64:
2164 case elfcpp::R_X86_64_32:
2165 case elfcpp::R_X86_64_32S:
2166 case elfcpp::R_X86_64_16:
2167 case elfcpp::R_X86_64_8:
2169 // Make a PLT entry if necessary.
2170 if (gsym->needs_plt_entry())
2172 target->make_plt_entry(symtab, layout, gsym);
2173 // Since this is not a PC-relative relocation, we may be
2174 // taking the address of a function. In that case we need to
2175 // set the entry in the dynamic symbol table to the address of
2177 if (gsym->is_from_dynobj() && !parameters->options().shared())
2178 gsym->set_needs_dynsym_value();
2180 // Make a dynamic relocation if necessary.
2181 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2183 if (gsym->may_need_copy_reloc())
2185 target->copy_reloc(symtab, layout, object,
2186 data_shndx, output_section, gsym, reloc);
2188 else if (r_type == elfcpp::R_X86_64_64
2189 && gsym->type() == elfcpp::STT_GNU_IFUNC
2190 && gsym->can_use_relative_reloc(false)
2191 && !gsym->is_from_dynobj()
2192 && !gsym->is_undefined()
2193 && !gsym->is_preemptible())
2195 // Use an IRELATIVE reloc for a locally defined
2196 // STT_GNU_IFUNC symbol. This makes a function
2197 // address in a PIE executable match the address in a
2198 // shared library that it links against.
2199 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2200 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2201 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2202 output_section, object,
2204 reloc.get_r_offset(),
2205 reloc.get_r_addend());
2207 else if (r_type == elfcpp::R_X86_64_64
2208 && gsym->can_use_relative_reloc(false))
2210 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2211 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2212 output_section, object,
2214 reloc.get_r_offset(),
2215 reloc.get_r_addend());
2219 this->check_non_pic(object, r_type, gsym);
2220 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2221 rela_dyn->add_global(gsym, r_type, output_section, object,
2222 data_shndx, reloc.get_r_offset(),
2223 reloc.get_r_addend());
2229 case elfcpp::R_X86_64_PC64:
2230 case elfcpp::R_X86_64_PC32:
2231 case elfcpp::R_X86_64_PC16:
2232 case elfcpp::R_X86_64_PC8:
2234 // Make a PLT entry if necessary.
2235 if (gsym->needs_plt_entry())
2236 target->make_plt_entry(symtab, layout, gsym);
2237 // Make a dynamic relocation if necessary.
2238 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2240 if (gsym->may_need_copy_reloc())
2242 target->copy_reloc(symtab, layout, object,
2243 data_shndx, output_section, gsym, reloc);
2247 this->check_non_pic(object, r_type, gsym);
2248 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2249 rela_dyn->add_global(gsym, r_type, output_section, object,
2250 data_shndx, reloc.get_r_offset(),
2251 reloc.get_r_addend());
2257 case elfcpp::R_X86_64_GOT64:
2258 case elfcpp::R_X86_64_GOT32:
2259 case elfcpp::R_X86_64_GOTPCREL64:
2260 case elfcpp::R_X86_64_GOTPCREL:
2261 case elfcpp::R_X86_64_GOTPLT64:
2263 // The symbol requires a GOT entry.
2264 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2265 if (gsym->final_value_is_known())
2267 // For a STT_GNU_IFUNC symbol we want the PLT address.
2268 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2269 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2271 got->add_global(gsym, GOT_TYPE_STANDARD);
2275 // If this symbol is not fully resolved, we need to add a
2276 // dynamic relocation for it.
2277 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2278 if (gsym->is_from_dynobj()
2279 || gsym->is_undefined()
2280 || gsym->is_preemptible()
2281 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2282 && parameters->options().output_is_position_independent()))
2283 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2284 elfcpp::R_X86_64_GLOB_DAT);
2287 // For a STT_GNU_IFUNC symbol we want to write the PLT
2288 // offset into the GOT, so that function pointer
2289 // comparisons work correctly.
2291 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2292 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2295 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2296 // Tell the dynamic linker to use the PLT address
2297 // when resolving relocations.
2298 if (gsym->is_from_dynobj()
2299 && !parameters->options().shared())
2300 gsym->set_needs_dynsym_value();
2304 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2305 rela_dyn->add_global_relative(gsym,
2306 elfcpp::R_X86_64_RELATIVE,
2311 // For GOTPLT64, we also need a PLT entry (but only if the
2312 // symbol is not fully resolved).
2313 if (r_type == elfcpp::R_X86_64_GOTPLT64
2314 && !gsym->final_value_is_known())
2315 target->make_plt_entry(symtab, layout, gsym);
2319 case elfcpp::R_X86_64_PLT32:
2320 // If the symbol is fully resolved, this is just a PC32 reloc.
2321 // Otherwise we need a PLT entry.
2322 if (gsym->final_value_is_known())
2324 // If building a shared library, we can also skip the PLT entry
2325 // if the symbol is defined in the output file and is protected
2327 if (gsym->is_defined()
2328 && !gsym->is_from_dynobj()
2329 && !gsym->is_preemptible())
2331 target->make_plt_entry(symtab, layout, gsym);
2334 case elfcpp::R_X86_64_GOTPC32:
2335 case elfcpp::R_X86_64_GOTOFF64:
2336 case elfcpp::R_X86_64_GOTPC64:
2337 case elfcpp::R_X86_64_PLTOFF64:
2338 // We need a GOT section.
2339 target->got_section(symtab, layout);
2340 // For PLTOFF64, we also need a PLT entry (but only if the
2341 // symbol is not fully resolved).
2342 if (r_type == elfcpp::R_X86_64_PLTOFF64
2343 && !gsym->final_value_is_known())
2344 target->make_plt_entry(symtab, layout, gsym);
2347 case elfcpp::R_X86_64_COPY:
2348 case elfcpp::R_X86_64_GLOB_DAT:
2349 case elfcpp::R_X86_64_JUMP_SLOT:
2350 case elfcpp::R_X86_64_RELATIVE:
2351 case elfcpp::R_X86_64_IRELATIVE:
2352 // These are outstanding tls relocs, which are unexpected when linking
2353 case elfcpp::R_X86_64_TPOFF64:
2354 case elfcpp::R_X86_64_DTPMOD64:
2355 case elfcpp::R_X86_64_TLSDESC:
2356 gold_error(_("%s: unexpected reloc %u in object file"),
2357 object->name().c_str(), r_type);
2360 // These are initial tls relocs, which are expected for global()
2361 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2362 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2363 case elfcpp::R_X86_64_TLSDESC_CALL:
2364 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2365 case elfcpp::R_X86_64_DTPOFF32:
2366 case elfcpp::R_X86_64_DTPOFF64:
2367 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2368 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2370 const bool is_final = gsym->final_value_is_known();
2371 const tls::Tls_optimization optimized_type
2372 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2375 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2376 if (optimized_type == tls::TLSOPT_NONE)
2378 // Create a pair of GOT entries for the module index and
2379 // dtv-relative offset.
2380 Output_data_got<64, false>* got
2381 = target->got_section(symtab, layout);
2382 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2383 target->rela_dyn_section(layout),
2384 elfcpp::R_X86_64_DTPMOD64,
2385 elfcpp::R_X86_64_DTPOFF64);
2387 else if (optimized_type == tls::TLSOPT_TO_IE)
2389 // Create a GOT entry for the tp-relative offset.
2390 Output_data_got<64, false>* got
2391 = target->got_section(symtab, layout);
2392 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2393 target->rela_dyn_section(layout),
2394 elfcpp::R_X86_64_TPOFF64);
2396 else if (optimized_type != tls::TLSOPT_TO_LE)
2397 unsupported_reloc_global(object, r_type, gsym);
2400 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2401 target->define_tls_base_symbol(symtab, layout);
2402 if (optimized_type == tls::TLSOPT_NONE)
2404 // Create reserved PLT and GOT entries for the resolver.
2405 target->reserve_tlsdesc_entries(symtab, layout);
2407 // Create a double GOT entry with an R_X86_64_TLSDESC
2408 // reloc. The R_X86_64_TLSDESC reloc is resolved
2409 // lazily, so the GOT entry needs to be in an area in
2410 // .got.plt, not .got. Call got_section to make sure
2411 // the section has been created.
2412 target->got_section(symtab, layout);
2413 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2414 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2415 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2416 elfcpp::R_X86_64_TLSDESC, 0);
2418 else if (optimized_type == tls::TLSOPT_TO_IE)
2420 // Create a GOT entry for the tp-relative offset.
2421 Output_data_got<64, false>* got
2422 = target->got_section(symtab, layout);
2423 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2424 target->rela_dyn_section(layout),
2425 elfcpp::R_X86_64_TPOFF64);
2427 else if (optimized_type != tls::TLSOPT_TO_LE)
2428 unsupported_reloc_global(object, r_type, gsym);
2431 case elfcpp::R_X86_64_TLSDESC_CALL:
2434 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2435 if (optimized_type == tls::TLSOPT_NONE)
2437 // Create a GOT entry for the module index.
2438 target->got_mod_index_entry(symtab, layout, object);
2440 else if (optimized_type != tls::TLSOPT_TO_LE)
2441 unsupported_reloc_global(object, r_type, gsym);
2444 case elfcpp::R_X86_64_DTPOFF32:
2445 case elfcpp::R_X86_64_DTPOFF64:
2448 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2449 layout->set_has_static_tls();
2450 if (optimized_type == tls::TLSOPT_NONE)
2452 // Create a GOT entry for the tp-relative offset.
2453 Output_data_got<64, false>* got
2454 = target->got_section(symtab, layout);
2455 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2456 target->rela_dyn_section(layout),
2457 elfcpp::R_X86_64_TPOFF64);
2459 else if (optimized_type != tls::TLSOPT_TO_LE)
2460 unsupported_reloc_global(object, r_type, gsym);
2463 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2464 layout->set_has_static_tls();
2465 if (parameters->options().shared())
2466 unsupported_reloc_local(object, r_type);
2475 case elfcpp::R_X86_64_SIZE32:
2476 case elfcpp::R_X86_64_SIZE64:
2478 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2479 object->name().c_str(), r_type,
2480 gsym->demangled_name().c_str());
2486 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2488 Sized_relobj_file<64, false>* object,
2489 unsigned int data_shndx,
2490 unsigned int sh_type,
2491 const unsigned char* prelocs,
2493 Output_section* output_section,
2494 bool needs_special_offset_handling,
2495 size_t local_symbol_count,
2496 const unsigned char* plocal_symbols)
2499 if (sh_type == elfcpp::SHT_REL)
2504 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2505 Target_x86_64::Scan,
2506 Target_x86_64::Relocatable_size_for_reloc>(
2515 needs_special_offset_handling,
2520 // Scan relocations for a section.
2523 Target_x86_64::scan_relocs(Symbol_table* symtab,
2525 Sized_relobj_file<64, false>* object,
2526 unsigned int data_shndx,
2527 unsigned int sh_type,
2528 const unsigned char* prelocs,
2530 Output_section* output_section,
2531 bool needs_special_offset_handling,
2532 size_t local_symbol_count,
2533 const unsigned char* plocal_symbols)
2535 if (sh_type == elfcpp::SHT_REL)
2537 gold_error(_("%s: unsupported REL reloc section"),
2538 object->name().c_str());
2542 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2543 Target_x86_64::Scan>(
2552 needs_special_offset_handling,
2557 // Finalize the sections.
2560 Target_x86_64::do_finalize_sections(
2562 const Input_objects*,
2563 Symbol_table* symtab)
2565 const Reloc_section* rel_plt = (this->plt_ == NULL
2567 : this->plt_->rela_plt());
2568 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2569 this->rela_dyn_, true, false);
2571 // Fill in some more dynamic tags.
2572 Output_data_dynamic* const odyn = layout->dynamic_data();
2575 if (this->plt_ != NULL
2576 && this->plt_->output_section() != NULL
2577 && this->plt_->has_tlsdesc_entry())
2579 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2580 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2581 this->got_->finalize_data_size();
2582 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2583 this->plt_, plt_offset);
2584 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2585 this->got_, got_offset);
2589 // Emit any relocs we saved in an attempt to avoid generating COPY
2591 if (this->copy_relocs_.any_saved_relocs())
2592 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2594 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2595 // the .got.plt section.
2596 Symbol* sym = this->global_offset_table_;
2599 uint64_t data_size = this->got_plt_->current_data_size();
2600 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2603 if (parameters->doing_static_link() && this->plt_ == NULL)
2605 // If linking statically, make sure that the __rela_iplt symbols
2606 // were defined if necessary, even if we didn't create a PLT.
2607 static const Define_symbol_in_segment syms[] =
2610 "__rela_iplt_start", // name
2611 elfcpp::PT_LOAD, // segment_type
2612 elfcpp::PF_W, // segment_flags_set
2613 elfcpp::PF(0), // segment_flags_clear
2616 elfcpp::STT_NOTYPE, // type
2617 elfcpp::STB_GLOBAL, // binding
2618 elfcpp::STV_HIDDEN, // visibility
2620 Symbol::SEGMENT_START, // offset_from_base
2624 "__rela_iplt_end", // name
2625 elfcpp::PT_LOAD, // segment_type
2626 elfcpp::PF_W, // segment_flags_set
2627 elfcpp::PF(0), // segment_flags_clear
2630 elfcpp::STT_NOTYPE, // type
2631 elfcpp::STB_GLOBAL, // binding
2632 elfcpp::STV_HIDDEN, // visibility
2634 Symbol::SEGMENT_START, // offset_from_base
2639 symtab->define_symbols(layout, 2, syms,
2640 layout->script_options()->saw_sections_clause());
2644 // Perform a relocation.
2647 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2648 Target_x86_64* target,
2651 const elfcpp::Rela<64, false>& rela,
2652 unsigned int r_type,
2653 const Sized_symbol<64>* gsym,
2654 const Symbol_value<64>* psymval,
2655 unsigned char* view,
2656 elfcpp::Elf_types<64>::Elf_Addr address,
2657 section_size_type view_size)
2659 if (this->skip_call_tls_get_addr_)
2661 if ((r_type != elfcpp::R_X86_64_PLT32
2662 && r_type != elfcpp::R_X86_64_PC32)
2664 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2666 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2667 _("missing expected TLS relocation"));
2671 this->skip_call_tls_get_addr_ = false;
2676 const Sized_relobj_file<64, false>* object = relinfo->object;
2678 // Pick the value to use for symbols defined in the PLT.
2679 Symbol_value<64> symval;
2681 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2683 symval.set_output_value(target->plt_section()->address()
2684 + gsym->plt_offset());
2687 else if (gsym == NULL && psymval->is_ifunc_symbol())
2689 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2690 if (object->local_has_plt_offset(r_sym))
2692 symval.set_output_value(target->plt_section()->address()
2693 + object->local_plt_offset(r_sym));
2698 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2700 // Get the GOT offset if needed.
2701 // The GOT pointer points to the end of the GOT section.
2702 // We need to subtract the size of the GOT section to get
2703 // the actual offset to use in the relocation.
2704 bool have_got_offset = false;
2705 unsigned int got_offset = 0;
2708 case elfcpp::R_X86_64_GOT32:
2709 case elfcpp::R_X86_64_GOT64:
2710 case elfcpp::R_X86_64_GOTPLT64:
2711 case elfcpp::R_X86_64_GOTPCREL:
2712 case elfcpp::R_X86_64_GOTPCREL64:
2715 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2716 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2720 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2721 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2722 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2723 - target->got_size());
2725 have_got_offset = true;
2734 case elfcpp::R_X86_64_NONE:
2735 case elfcpp::R_X86_64_GNU_VTINHERIT:
2736 case elfcpp::R_X86_64_GNU_VTENTRY:
2739 case elfcpp::R_X86_64_64:
2740 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2743 case elfcpp::R_X86_64_PC64:
2744 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2748 case elfcpp::R_X86_64_32:
2749 // FIXME: we need to verify that value + addend fits into 32 bits:
2750 // uint64_t x = value + addend;
2751 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2752 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2753 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2756 case elfcpp::R_X86_64_32S:
2757 // FIXME: we need to verify that value + addend fits into 32 bits:
2758 // int64_t x = value + addend; // note this quantity is signed!
2759 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2760 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2763 case elfcpp::R_X86_64_PC32:
2764 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2768 case elfcpp::R_X86_64_16:
2769 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2772 case elfcpp::R_X86_64_PC16:
2773 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2777 case elfcpp::R_X86_64_8:
2778 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2781 case elfcpp::R_X86_64_PC8:
2782 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2786 case elfcpp::R_X86_64_PLT32:
2787 gold_assert(gsym == NULL
2788 || gsym->has_plt_offset()
2789 || gsym->final_value_is_known()
2790 || (gsym->is_defined()
2791 && !gsym->is_from_dynobj()
2792 && !gsym->is_preemptible()));
2793 // Note: while this code looks the same as for R_X86_64_PC32, it
2794 // behaves differently because psymval was set to point to
2795 // the PLT entry, rather than the symbol, in Scan::global().
2796 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2800 case elfcpp::R_X86_64_PLTOFF64:
2803 gold_assert(gsym->has_plt_offset()
2804 || gsym->final_value_is_known());
2805 elfcpp::Elf_types<64>::Elf_Addr got_address;
2806 got_address = target->got_section(NULL, NULL)->address();
2807 Relocate_functions<64, false>::rela64(view, object, psymval,
2808 addend - got_address);
2811 case elfcpp::R_X86_64_GOT32:
2812 gold_assert(have_got_offset);
2813 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2816 case elfcpp::R_X86_64_GOTPC32:
2819 elfcpp::Elf_types<64>::Elf_Addr value;
2820 value = target->got_plt_section()->address();
2821 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2825 case elfcpp::R_X86_64_GOT64:
2826 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2827 // Since we always add a PLT entry, this is equivalent.
2828 case elfcpp::R_X86_64_GOTPLT64:
2829 gold_assert(have_got_offset);
2830 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2833 case elfcpp::R_X86_64_GOTPC64:
2836 elfcpp::Elf_types<64>::Elf_Addr value;
2837 value = target->got_plt_section()->address();
2838 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2842 case elfcpp::R_X86_64_GOTOFF64:
2844 elfcpp::Elf_types<64>::Elf_Addr value;
2845 value = (psymval->value(object, 0)
2846 - target->got_plt_section()->address());
2847 Relocate_functions<64, false>::rela64(view, value, addend);
2851 case elfcpp::R_X86_64_GOTPCREL:
2853 gold_assert(have_got_offset);
2854 elfcpp::Elf_types<64>::Elf_Addr value;
2855 value = target->got_plt_section()->address() + got_offset;
2856 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2860 case elfcpp::R_X86_64_GOTPCREL64:
2862 gold_assert(have_got_offset);
2863 elfcpp::Elf_types<64>::Elf_Addr value;
2864 value = target->got_plt_section()->address() + got_offset;
2865 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2869 case elfcpp::R_X86_64_COPY:
2870 case elfcpp::R_X86_64_GLOB_DAT:
2871 case elfcpp::R_X86_64_JUMP_SLOT:
2872 case elfcpp::R_X86_64_RELATIVE:
2873 case elfcpp::R_X86_64_IRELATIVE:
2874 // These are outstanding tls relocs, which are unexpected when linking
2875 case elfcpp::R_X86_64_TPOFF64:
2876 case elfcpp::R_X86_64_DTPMOD64:
2877 case elfcpp::R_X86_64_TLSDESC:
2878 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2879 _("unexpected reloc %u in object file"),
2883 // These are initial tls relocs, which are expected when linking
2884 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2885 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2886 case elfcpp::R_X86_64_TLSDESC_CALL:
2887 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2888 case elfcpp::R_X86_64_DTPOFF32:
2889 case elfcpp::R_X86_64_DTPOFF64:
2890 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2891 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2892 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2893 view, address, view_size);
2896 case elfcpp::R_X86_64_SIZE32:
2897 case elfcpp::R_X86_64_SIZE64:
2899 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2900 _("unsupported reloc %u"),
2908 // Perform a TLS relocation.
2911 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2912 Target_x86_64* target,
2914 const elfcpp::Rela<64, false>& rela,
2915 unsigned int r_type,
2916 const Sized_symbol<64>* gsym,
2917 const Symbol_value<64>* psymval,
2918 unsigned char* view,
2919 elfcpp::Elf_types<64>::Elf_Addr address,
2920 section_size_type view_size)
2922 Output_segment* tls_segment = relinfo->layout->tls_segment();
2924 const Sized_relobj_file<64, false>* object = relinfo->object;
2925 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2926 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2927 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2929 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2931 const bool is_final = (gsym == NULL
2932 ? !parameters->options().shared()
2933 : gsym->final_value_is_known());
2934 tls::Tls_optimization optimized_type
2935 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2938 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2939 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2941 // If this code sequence is used in a non-executable section,
2942 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2943 // on the assumption that it's being used by itself in a debug
2944 // section. Therefore, in the unlikely event that the code
2945 // sequence appears in a non-executable section, we simply
2946 // leave it unoptimized.
2947 optimized_type = tls::TLSOPT_NONE;
2949 if (optimized_type == tls::TLSOPT_TO_LE)
2951 gold_assert(tls_segment != NULL);
2952 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2953 rela, r_type, value, view,
2959 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2960 ? GOT_TYPE_TLS_OFFSET
2961 : GOT_TYPE_TLS_PAIR);
2962 unsigned int got_offset;
2965 gold_assert(gsym->has_got_offset(got_type));
2966 got_offset = gsym->got_offset(got_type) - target->got_size();
2970 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2971 gold_assert(object->local_has_got_offset(r_sym, got_type));
2972 got_offset = (object->local_got_offset(r_sym, got_type)
2973 - target->got_size());
2975 if (optimized_type == tls::TLSOPT_TO_IE)
2977 gold_assert(tls_segment != NULL);
2978 value = target->got_plt_section()->address() + got_offset;
2979 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2980 value, view, address, view_size);
2983 else if (optimized_type == tls::TLSOPT_NONE)
2985 // Relocate the field with the offset of the pair of GOT
2987 value = target->got_plt_section()->address() + got_offset;
2988 Relocate_functions<64, false>::pcrela32(view, value, addend,
2993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2994 _("unsupported reloc %u"), r_type);
2997 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2998 case elfcpp::R_X86_64_TLSDESC_CALL:
2999 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3001 // See above comment for R_X86_64_TLSGD.
3002 optimized_type = tls::TLSOPT_NONE;
3004 if (optimized_type == tls::TLSOPT_TO_LE)
3006 gold_assert(tls_segment != NULL);
3007 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3008 rela, r_type, value, view,
3014 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3015 ? GOT_TYPE_TLS_OFFSET
3016 : GOT_TYPE_TLS_DESC);
3017 unsigned int got_offset = 0;
3018 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3019 && optimized_type == tls::TLSOPT_NONE)
3021 // We created GOT entries in the .got.tlsdesc portion of
3022 // the .got.plt section, but the offset stored in the
3023 // symbol is the offset within .got.tlsdesc.
3024 got_offset = (target->got_size()
3025 + target->got_plt_section()->data_size());
3029 gold_assert(gsym->has_got_offset(got_type));
3030 got_offset += gsym->got_offset(got_type) - target->got_size();
3034 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3035 gold_assert(object->local_has_got_offset(r_sym, got_type));
3036 got_offset += (object->local_got_offset(r_sym, got_type)
3037 - target->got_size());
3039 if (optimized_type == tls::TLSOPT_TO_IE)
3041 gold_assert(tls_segment != NULL);
3042 value = target->got_plt_section()->address() + got_offset;
3043 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3044 rela, r_type, value, view, address,
3048 else if (optimized_type == tls::TLSOPT_NONE)
3050 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3052 // Relocate the field with the offset of the pair of GOT
3054 value = target->got_plt_section()->address() + got_offset;
3055 Relocate_functions<64, false>::pcrela32(view, value, addend,
3061 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3062 _("unsupported reloc %u"), r_type);
3065 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3066 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3068 // See above comment for R_X86_64_TLSGD.
3069 optimized_type = tls::TLSOPT_NONE;
3071 if (optimized_type == tls::TLSOPT_TO_LE)
3073 gold_assert(tls_segment != NULL);
3074 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3075 value, view, view_size);
3078 else if (optimized_type == tls::TLSOPT_NONE)
3080 // Relocate the field with the offset of the GOT entry for
3081 // the module index.
3082 unsigned int got_offset;
3083 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3084 - target->got_size());
3085 value = target->got_plt_section()->address() + got_offset;
3086 Relocate_functions<64, false>::pcrela32(view, value, addend,
3090 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3091 _("unsupported reloc %u"), r_type);
3094 case elfcpp::R_X86_64_DTPOFF32:
3095 // This relocation type is used in debugging information.
3096 // In that case we need to not optimize the value. If the
3097 // section is not executable, then we assume we should not
3098 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3099 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3101 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3103 gold_assert(tls_segment != NULL);
3104 value -= tls_segment->memsz();
3106 Relocate_functions<64, false>::rela32(view, value, addend);
3109 case elfcpp::R_X86_64_DTPOFF64:
3110 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3111 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3113 gold_assert(tls_segment != NULL);
3114 value -= tls_segment->memsz();
3116 Relocate_functions<64, false>::rela64(view, value, addend);
3119 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3120 if (optimized_type == tls::TLSOPT_TO_LE)
3122 gold_assert(tls_segment != NULL);
3123 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3124 rela, r_type, value, view,
3128 else if (optimized_type == tls::TLSOPT_NONE)
3130 // Relocate the field with the offset of the GOT entry for
3131 // the tp-relative offset of the symbol.
3132 unsigned int got_offset;
3135 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3136 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3137 - target->got_size());
3141 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3142 gold_assert(object->local_has_got_offset(r_sym,
3143 GOT_TYPE_TLS_OFFSET));
3144 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3145 - target->got_size());
3147 value = target->got_plt_section()->address() + got_offset;
3148 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3151 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3152 _("unsupported reloc type %u"),
3156 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3157 value -= tls_segment->memsz();
3158 Relocate_functions<64, false>::rela32(view, value, addend);
3163 // Do a relocation in which we convert a TLS General-Dynamic to an
3167 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3170 const elfcpp::Rela<64, false>& rela,
3172 elfcpp::Elf_types<64>::Elf_Addr value,
3173 unsigned char* view,
3174 elfcpp::Elf_types<64>::Elf_Addr address,
3175 section_size_type view_size)
3177 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3178 // .word 0x6666; rex64; call __tls_get_addr
3179 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3181 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3182 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3184 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3185 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3186 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3187 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3189 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3191 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3192 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3194 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3196 this->skip_call_tls_get_addr_ = true;
3199 // Do a relocation in which we convert a TLS General-Dynamic to a
3203 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3205 Output_segment* tls_segment,
3206 const elfcpp::Rela<64, false>& rela,
3208 elfcpp::Elf_types<64>::Elf_Addr value,
3209 unsigned char* view,
3210 section_size_type view_size)
3212 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3213 // .word 0x6666; rex64; call __tls_get_addr
3214 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3216 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3217 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3219 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3220 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3221 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3222 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3224 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3226 value -= tls_segment->memsz();
3227 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3229 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3231 this->skip_call_tls_get_addr_ = true;
3234 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3237 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3238 const Relocate_info<64, false>* relinfo,
3241 const elfcpp::Rela<64, false>& rela,
3242 unsigned int r_type,
3243 elfcpp::Elf_types<64>::Elf_Addr value,
3244 unsigned char* view,
3245 elfcpp::Elf_types<64>::Elf_Addr address,
3246 section_size_type view_size)
3248 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3250 // leaq foo@tlsdesc(%rip), %rax
3251 // ==> movq foo@gottpoff(%rip), %rax
3252 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3253 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3254 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3255 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3257 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3258 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3262 // call *foo@tlscall(%rax)
3264 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3265 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3266 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3267 view[0] == 0xff && view[1] == 0x10);
3273 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3276 Target_x86_64::Relocate::tls_desc_gd_to_le(
3277 const Relocate_info<64, false>* relinfo,
3279 Output_segment* tls_segment,
3280 const elfcpp::Rela<64, false>& rela,
3281 unsigned int r_type,
3282 elfcpp::Elf_types<64>::Elf_Addr value,
3283 unsigned char* view,
3284 section_size_type view_size)
3286 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3288 // leaq foo@tlsdesc(%rip), %rax
3289 // ==> movq foo@tpoff, %rax
3290 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3291 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3292 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3293 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3296 value -= tls_segment->memsz();
3297 Relocate_functions<64, false>::rela32(view, value, 0);
3301 // call *foo@tlscall(%rax)
3303 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3304 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3305 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3306 view[0] == 0xff && view[1] == 0x10);
3313 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3316 const elfcpp::Rela<64, false>& rela,
3318 elfcpp::Elf_types<64>::Elf_Addr,
3319 unsigned char* view,
3320 section_size_type view_size)
3322 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3323 // ... leq foo@dtpoff(%rax),%reg
3324 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3326 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3327 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3329 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3330 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3332 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3334 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3336 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3338 this->skip_call_tls_get_addr_ = true;
3341 // Do a relocation in which we convert a TLS Initial-Exec to a
3345 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3347 Output_segment* tls_segment,
3348 const elfcpp::Rela<64, false>& rela,
3350 elfcpp::Elf_types<64>::Elf_Addr value,
3351 unsigned char* view,
3352 section_size_type view_size)
3354 // We need to examine the opcodes to figure out which instruction we
3357 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3358 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3360 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3361 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3363 unsigned char op1 = view[-3];
3364 unsigned char op2 = view[-2];
3365 unsigned char op3 = view[-1];
3366 unsigned char reg = op3 >> 3;
3374 view[-1] = 0xc0 | reg;
3378 // Special handling for %rsp.
3382 view[-1] = 0xc0 | reg;
3390 view[-1] = 0x80 | reg | (reg << 3);
3393 value -= tls_segment->memsz();
3394 Relocate_functions<64, false>::rela32(view, value, 0);
3397 // Relocate section data.
3400 Target_x86_64::relocate_section(
3401 const Relocate_info<64, false>* relinfo,
3402 unsigned int sh_type,
3403 const unsigned char* prelocs,
3405 Output_section* output_section,
3406 bool needs_special_offset_handling,
3407 unsigned char* view,
3408 elfcpp::Elf_types<64>::Elf_Addr address,
3409 section_size_type view_size,
3410 const Reloc_symbol_changes* reloc_symbol_changes)
3412 gold_assert(sh_type == elfcpp::SHT_RELA);
3414 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3415 Target_x86_64::Relocate>(
3421 needs_special_offset_handling,
3425 reloc_symbol_changes);
3428 // Apply an incremental relocation. Incremental relocations always refer
3429 // to global symbols.
3432 Target_x86_64::apply_relocation(
3433 const Relocate_info<64, false>* relinfo,
3434 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3435 unsigned int r_type,
3436 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3438 unsigned char* view,
3439 elfcpp::Elf_types<64>::Elf_Addr address,
3440 section_size_type view_size)
3442 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3454 // Return the size of a relocation while scanning during a relocatable
3458 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3459 unsigned int r_type,
3464 case elfcpp::R_X86_64_NONE:
3465 case elfcpp::R_X86_64_GNU_VTINHERIT:
3466 case elfcpp::R_X86_64_GNU_VTENTRY:
3467 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3468 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3469 case elfcpp::R_X86_64_TLSDESC_CALL:
3470 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3471 case elfcpp::R_X86_64_DTPOFF32:
3472 case elfcpp::R_X86_64_DTPOFF64:
3473 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3474 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3477 case elfcpp::R_X86_64_64:
3478 case elfcpp::R_X86_64_PC64:
3479 case elfcpp::R_X86_64_GOTOFF64:
3480 case elfcpp::R_X86_64_GOTPC64:
3481 case elfcpp::R_X86_64_PLTOFF64:
3482 case elfcpp::R_X86_64_GOT64:
3483 case elfcpp::R_X86_64_GOTPCREL64:
3484 case elfcpp::R_X86_64_GOTPCREL:
3485 case elfcpp::R_X86_64_GOTPLT64:
3488 case elfcpp::R_X86_64_32:
3489 case elfcpp::R_X86_64_32S:
3490 case elfcpp::R_X86_64_PC32:
3491 case elfcpp::R_X86_64_PLT32:
3492 case elfcpp::R_X86_64_GOTPC32:
3493 case elfcpp::R_X86_64_GOT32:
3496 case elfcpp::R_X86_64_16:
3497 case elfcpp::R_X86_64_PC16:
3500 case elfcpp::R_X86_64_8:
3501 case elfcpp::R_X86_64_PC8:
3504 case elfcpp::R_X86_64_COPY:
3505 case elfcpp::R_X86_64_GLOB_DAT:
3506 case elfcpp::R_X86_64_JUMP_SLOT:
3507 case elfcpp::R_X86_64_RELATIVE:
3508 case elfcpp::R_X86_64_IRELATIVE:
3509 // These are outstanding tls relocs, which are unexpected when linking
3510 case elfcpp::R_X86_64_TPOFF64:
3511 case elfcpp::R_X86_64_DTPMOD64:
3512 case elfcpp::R_X86_64_TLSDESC:
3513 object->error(_("unexpected reloc %u in object file"), r_type);
3516 case elfcpp::R_X86_64_SIZE32:
3517 case elfcpp::R_X86_64_SIZE64:
3519 object->error(_("unsupported reloc %u against local symbol"), r_type);
3524 // Scan the relocs during a relocatable link.
3527 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3529 Sized_relobj_file<64, false>* object,
3530 unsigned int data_shndx,
3531 unsigned int sh_type,
3532 const unsigned char* prelocs,
3534 Output_section* output_section,
3535 bool needs_special_offset_handling,
3536 size_t local_symbol_count,
3537 const unsigned char* plocal_symbols,
3538 Relocatable_relocs* rr)
3540 gold_assert(sh_type == elfcpp::SHT_RELA);
3542 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3543 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3545 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3546 Scan_relocatable_relocs>(
3554 needs_special_offset_handling,
3560 // Relocate a section during a relocatable link.
3563 Target_x86_64::relocate_for_relocatable(
3564 const Relocate_info<64, false>* relinfo,
3565 unsigned int sh_type,
3566 const unsigned char* prelocs,
3568 Output_section* output_section,
3569 off_t offset_in_output_section,
3570 const Relocatable_relocs* rr,
3571 unsigned char* view,
3572 elfcpp::Elf_types<64>::Elf_Addr view_address,
3573 section_size_type view_size,
3574 unsigned char* reloc_view,
3575 section_size_type reloc_view_size)
3577 gold_assert(sh_type == elfcpp::SHT_RELA);
3579 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3584 offset_in_output_section,
3593 // Return the value to use for a dynamic which requires special
3594 // treatment. This is how we support equality comparisons of function
3595 // pointers across shared library boundaries, as described in the
3596 // processor specific ABI supplement.
3599 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3601 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3602 return this->plt_section()->address() + gsym->plt_offset();
3605 // Return a string used to fill a code section with nops to take up
3606 // the specified length.
3609 Target_x86_64::do_code_fill(section_size_type length) const
3613 // Build a jmpq instruction to skip over the bytes.
3614 unsigned char jmp[5];
3616 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3617 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3618 + std::string(length - 5, '\0'));
3621 // Nop sequences of various lengths.
3622 const char nop1[1] = { 0x90 }; // nop
3623 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3624 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3625 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3626 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3628 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3630 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3632 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3633 0x00, 0x00, 0x00, 0x00 };
3634 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3635 0x00, 0x00, 0x00, 0x00,
3637 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3638 0x84, 0x00, 0x00, 0x00,
3640 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3641 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3643 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3644 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3645 0x00, 0x00, 0x00, 0x00 };
3646 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3647 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3648 0x00, 0x00, 0x00, 0x00,
3650 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3651 0x66, 0x2e, 0x0f, 0x1f, // data16
3652 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3654 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3655 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3656 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3659 const char* nops[16] = {
3661 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3662 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3665 return std::string(nops[length], length);
3668 // Return the addend to use for a target specific relocation. The
3669 // only target specific relocation is R_X86_64_TLSDESC for a local
3670 // symbol. We want to set the addend is the offset of the local
3671 // symbol in the TLS segment.
3674 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3677 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3678 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3679 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3680 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3681 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3682 gold_assert(psymval->is_tls_symbol());
3683 // The value of a TLS symbol is the offset in the TLS segment.
3684 return psymval->value(ti.object, 0);
3687 // Return the value to use for the base of a DW_EH_PE_datarel offset
3688 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3689 // assembler can not write out the difference between two labels in
3690 // different sections, so instead of using a pc-relative value they
3691 // use an offset from the GOT.
3694 Target_x86_64::do_ehframe_datarel_base() const
3696 gold_assert(this->global_offset_table_ != NULL);
3697 Symbol* sym = this->global_offset_table_;
3698 Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym);
3699 return ssym->value();
3702 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3703 // compiled with -fsplit-stack. The function calls non-split-stack
3704 // code. We have to change the function so that it always ensures
3705 // that it has enough stack space to run some random function.
3708 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3709 section_offset_type fnoffset,
3710 section_size_type fnsize,
3711 unsigned char* view,
3712 section_size_type view_size,
3714 std::string* to) const
3716 // The function starts with a comparison of the stack pointer and a
3717 // field in the TCB. This is followed by a jump.
3720 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3723 // We will call __morestack if the carry flag is set after this
3724 // comparison. We turn the comparison into an stc instruction
3726 view[fnoffset] = '\xf9';
3727 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3729 // lea NN(%rsp),%r10
3730 // lea NN(%rsp),%r11
3731 else if ((this->match_view(view, view_size, fnoffset,
3732 "\x4c\x8d\x94\x24", 4)
3733 || this->match_view(view, view_size, fnoffset,
3734 "\x4c\x8d\x9c\x24", 4))
3737 // This is loading an offset from the stack pointer for a
3738 // comparison. The offset is negative, so we decrease the
3739 // offset by the amount of space we need for the stack. This
3740 // means we will avoid calling __morestack if there happens to
3741 // be plenty of space on the stack already.
3742 unsigned char* pval = view + fnoffset + 4;
3743 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3744 val -= parameters->options().split_stack_adjust_size();
3745 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3749 if (!object->has_no_split_stack())
3750 object->error(_("failed to match split-stack sequence at "
3751 "section %u offset %0zx"),
3752 shndx, static_cast<size_t>(fnoffset));
3756 // We have to change the function so that it calls
3757 // __morestack_non_split instead of __morestack. The former will
3758 // allocate additional stack space.
3759 *from = "__morestack";
3760 *to = "__morestack_non_split";
3763 // The selector for x86_64 object files.
3765 class Target_selector_x86_64 : public Target_selector_freebsd
3768 Target_selector_x86_64()
3769 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3770 "elf64-x86-64-freebsd", "elf_x86_64")
3774 do_instantiate_target()
3775 { return new Target_x86_64(); }
3779 Target_selector_x86_64 target_selector_x86_64;
3781 } // End anonymous namespace.