1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_x86_64 : public Output_section_data
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
56 Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
57 Output_data_got<64, false>* got,
58 Output_data_space* got_plt)
59 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
60 count_(0), tlsdesc_got_offset_(-1U), free_list_()
61 { this->init(symtab, layout); }
63 Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
64 Output_data_got<64, false>* got,
65 Output_data_space* got_plt,
66 unsigned int plt_count)
67 : Output_section_data((plt_count + 1) * plt_entry_size, 8, false),
68 tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
69 count_(plt_count), tlsdesc_got_offset_(-1U), free_list_()
71 this->init(symtab, layout);
73 // Initialize the free list and reserve the first entry.
74 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
75 this->free_list_.remove(0, plt_entry_size);
78 // Initialize the PLT section.
80 init(Symbol_table* symtab, Layout* layout);
82 // Add an entry to the PLT.
84 add_entry(Symbol* gsym);
86 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
88 add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
89 unsigned int local_sym_index);
91 // Add the relocation for a PLT entry.
93 add_relocation(Symbol* gsym, unsigned int got_offset);
95 // Add the reserved TLSDESC_PLT entry to the PLT.
97 reserve_tlsdesc_entry(unsigned int got_offset)
98 { this->tlsdesc_got_offset_ = got_offset; }
100 // Return true if a TLSDESC_PLT entry has been reserved.
102 has_tlsdesc_entry() const
103 { return this->tlsdesc_got_offset_ != -1U; }
105 // Return the GOT offset for the reserved TLSDESC_PLT entry.
107 get_tlsdesc_got_offset() const
108 { return this->tlsdesc_got_offset_; }
110 // Return the offset of the reserved TLSDESC_PLT entry.
112 get_tlsdesc_plt_offset() const
113 { return (this->count_ + 1) * plt_entry_size; }
115 // Return the .rela.plt section data.
118 { return this->rel_; }
120 // Return where the TLSDESC relocations should go.
122 rela_tlsdesc(Layout*);
124 // Return the number of PLT entries.
127 { return this->count_; }
129 // Return the offset of the first non-reserved PLT entry.
131 first_plt_entry_offset()
132 { return plt_entry_size; }
134 // Return the size of a PLT entry.
137 { return plt_entry_size; }
139 // Reserve a slot in the PLT for an existing symbol in an incremental update.
141 reserve_slot(unsigned int plt_index)
143 this->free_list_.remove((plt_index + 1) * plt_entry_size,
144 (plt_index + 2) * plt_entry_size);
149 do_adjust_output_section(Output_section* os);
151 // Write to a map file.
153 do_print_to_mapfile(Mapfile* mapfile) const
154 { mapfile->print_output_data(this, _("** PLT")); }
157 // The size of an entry in the PLT.
158 static const int plt_entry_size = 16;
160 // The first entry in the PLT.
161 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
162 // procedure linkage table for both programs and shared objects."
163 static unsigned char first_plt_entry[plt_entry_size];
165 // Other entries in the PLT for an executable.
166 static unsigned char plt_entry[plt_entry_size];
168 // The reserved TLSDESC entry in the PLT for an executable.
169 static unsigned char tlsdesc_plt_entry[plt_entry_size];
171 // Set the final size.
173 set_final_data_size();
175 // Write out the PLT data.
177 do_write(Output_file*);
179 // The reloc section.
181 // The TLSDESC relocs, if necessary. These must follow the regular
183 Reloc_section* tlsdesc_rel_;
185 Output_data_got<64, false>* got_;
186 // The .got.plt section.
187 Output_data_space* got_plt_;
188 // The number of PLT entries.
190 // Offset of the reserved TLSDESC_GOT entry when needed.
191 unsigned int tlsdesc_got_offset_;
192 // List of available regions within the section, for incremental
194 Free_list free_list_;
197 // The x86_64 target class.
199 // http://www.x86-64.org/documentation/abi.pdf
200 // TLS info comes from
201 // http://people.redhat.com/drepper/tls.pdf
202 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
204 class Target_x86_64 : public Target_freebsd<64, false>
207 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
208 // uses only Elf64_Rela relocation entries with explicit addends."
209 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
212 : Target_freebsd<64, false>(&x86_64_info),
213 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
214 global_offset_table_(NULL), rela_dyn_(NULL),
215 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
216 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
217 tls_base_symbol_defined_(false)
220 // This function should be defined in targets that can use relocation
221 // types to determine (implemented in local_reloc_may_be_function_pointer
222 // and global_reloc_may_be_function_pointer)
223 // if a function's pointer is taken. ICF uses this in safe mode to only
224 // fold those functions whose pointer is defintely not taken. For x86_64
225 // pie binaries, safe ICF cannot be done by looking at relocation types.
227 can_check_for_function_pointers() const
228 { return !parameters->options().pie(); }
231 can_icf_inline_merge_sections () const
234 // Hook for a new output section.
236 do_new_output_section(Output_section*) const;
238 // Scan the relocations to look for symbol adjustments.
240 gc_process_relocs(Symbol_table* symtab,
242 Sized_relobj<64, false>* object,
243 unsigned int data_shndx,
244 unsigned int sh_type,
245 const unsigned char* prelocs,
247 Output_section* output_section,
248 bool needs_special_offset_handling,
249 size_t local_symbol_count,
250 const unsigned char* plocal_symbols);
252 // Scan the relocations to look for symbol adjustments.
254 scan_relocs(Symbol_table* symtab,
256 Sized_relobj<64, false>* object,
257 unsigned int data_shndx,
258 unsigned int sh_type,
259 const unsigned char* prelocs,
261 Output_section* output_section,
262 bool needs_special_offset_handling,
263 size_t local_symbol_count,
264 const unsigned char* plocal_symbols);
266 // Finalize the sections.
268 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
270 // Return the value to use for a dynamic which requires special
273 do_dynsym_value(const Symbol*) const;
275 // Relocate a section.
277 relocate_section(const Relocate_info<64, false>*,
278 unsigned int sh_type,
279 const unsigned char* prelocs,
281 Output_section* output_section,
282 bool needs_special_offset_handling,
284 elfcpp::Elf_types<64>::Elf_Addr view_address,
285 section_size_type view_size,
286 const Reloc_symbol_changes*);
288 // Scan the relocs during a relocatable link.
290 scan_relocatable_relocs(Symbol_table* symtab,
292 Sized_relobj<64, false>* object,
293 unsigned int data_shndx,
294 unsigned int sh_type,
295 const unsigned char* prelocs,
297 Output_section* output_section,
298 bool needs_special_offset_handling,
299 size_t local_symbol_count,
300 const unsigned char* plocal_symbols,
301 Relocatable_relocs*);
303 // Relocate a section during a relocatable link.
305 relocate_for_relocatable(const Relocate_info<64, false>*,
306 unsigned int sh_type,
307 const unsigned char* prelocs,
309 Output_section* output_section,
310 off_t offset_in_output_section,
311 const Relocatable_relocs*,
313 elfcpp::Elf_types<64>::Elf_Addr view_address,
314 section_size_type view_size,
315 unsigned char* reloc_view,
316 section_size_type reloc_view_size);
318 // Return a string used to fill a code section with nops.
320 do_code_fill(section_size_type length) const;
322 // Return whether SYM is defined by the ABI.
324 do_is_defined_by_abi(const Symbol* sym) const
325 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
327 // Return the symbol index to use for a target specific relocation.
328 // The only target specific relocation is R_X86_64_TLSDESC for a
329 // local symbol, which is an absolute reloc.
331 do_reloc_symbol_index(void*, unsigned int r_type) const
333 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
337 // Return the addend to use for a target specific relocation.
339 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
341 // Return the PLT section.
343 do_plt_section_for_global(const Symbol*) const
344 { return this->plt_section(); }
347 do_plt_section_for_local(const Relobj*, unsigned int) const
348 { return this->plt_section(); }
350 // Adjust -fsplit-stack code which calls non-split-stack code.
352 do_calls_non_split(Relobj* object, unsigned int shndx,
353 section_offset_type fnoffset, section_size_type fnsize,
354 unsigned char* view, section_size_type view_size,
355 std::string* from, std::string* to) const;
357 // Return the size of the GOT section.
361 gold_assert(this->got_ != NULL);
362 return this->got_->data_size();
365 // Return the number of entries in the GOT.
367 got_entry_count() const
369 if (this->got_ == NULL)
371 return this->got_size() / 8;
374 // Return the number of entries in the PLT.
376 plt_entry_count() const;
378 // Return the offset of the first non-reserved PLT entry.
380 first_plt_entry_offset() const;
382 // Return the size of each PLT entry.
384 plt_entry_size() const;
386 // Create the GOT section for an incremental update.
387 Output_data_got<64, false>*
388 init_got_plt_for_update(Symbol_table* symtab,
390 unsigned int got_count,
391 unsigned int plt_count);
393 // Register an existing PLT entry for a global symbol.
394 // A target needs to implement this to support incremental linking.
396 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
398 // Apply an incremental relocation.
400 apply_relocation(const Relocate_info<64, false>* relinfo,
401 elfcpp::Elf_types<64>::Elf_Addr r_offset,
403 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
406 elfcpp::Elf_types<64>::Elf_Addr address,
407 section_size_type view_size);
409 // Add a new reloc argument, returning the index in the vector.
411 add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
413 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
414 return this->tlsdesc_reloc_info_.size() - 1;
418 // The class which scans relocations.
423 : issued_non_pic_error_(false)
427 get_reference_flags(unsigned int r_type);
430 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
431 Sized_relobj<64, false>* object,
432 unsigned int data_shndx,
433 Output_section* output_section,
434 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
435 const elfcpp::Sym<64, false>& lsym);
438 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
439 Sized_relobj<64, false>* object,
440 unsigned int data_shndx,
441 Output_section* output_section,
442 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
446 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
447 Target_x86_64* target,
448 Sized_relobj<64, false>* object,
449 unsigned int data_shndx,
450 Output_section* output_section,
451 const elfcpp::Rela<64, false>& reloc,
453 const elfcpp::Sym<64, false>& lsym);
456 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
457 Target_x86_64* target,
458 Sized_relobj<64, false>* object,
459 unsigned int data_shndx,
460 Output_section* output_section,
461 const elfcpp::Rela<64, false>& reloc,
467 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
470 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
474 check_non_pic(Relobj*, unsigned int r_type);
477 possible_function_pointer_reloc(unsigned int r_type);
480 reloc_needs_plt_for_ifunc(Sized_relobj<64, false>*, unsigned int r_type);
482 // Whether we have issued an error about a non-PIC compilation.
483 bool issued_non_pic_error_;
486 // The class which implements relocation.
491 : skip_call_tls_get_addr_(false)
496 if (this->skip_call_tls_get_addr_)
498 // FIXME: This needs to specify the location somehow.
499 gold_error(_("missing expected TLS relocation"));
503 // Do a relocation. Return false if the caller should not issue
504 // any warnings about this relocation.
506 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
507 size_t relnum, const elfcpp::Rela<64, false>&,
508 unsigned int r_type, const Sized_symbol<64>*,
509 const Symbol_value<64>*,
510 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
514 // Do a TLS relocation.
516 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
517 size_t relnum, const elfcpp::Rela<64, false>&,
518 unsigned int r_type, const Sized_symbol<64>*,
519 const Symbol_value<64>*,
520 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
523 // Do a TLS General-Dynamic to Initial-Exec transition.
525 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
526 Output_segment* tls_segment,
527 const elfcpp::Rela<64, false>&, unsigned int r_type,
528 elfcpp::Elf_types<64>::Elf_Addr value,
530 elfcpp::Elf_types<64>::Elf_Addr,
531 section_size_type view_size);
533 // Do a TLS General-Dynamic to Local-Exec transition.
535 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
536 Output_segment* tls_segment,
537 const elfcpp::Rela<64, false>&, unsigned int r_type,
538 elfcpp::Elf_types<64>::Elf_Addr value,
540 section_size_type view_size);
542 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
544 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
545 Output_segment* tls_segment,
546 const elfcpp::Rela<64, false>&, unsigned int r_type,
547 elfcpp::Elf_types<64>::Elf_Addr value,
549 elfcpp::Elf_types<64>::Elf_Addr,
550 section_size_type view_size);
552 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
554 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
555 Output_segment* tls_segment,
556 const elfcpp::Rela<64, false>&, unsigned int r_type,
557 elfcpp::Elf_types<64>::Elf_Addr value,
559 section_size_type view_size);
561 // Do a TLS Local-Dynamic to Local-Exec transition.
563 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
564 Output_segment* tls_segment,
565 const elfcpp::Rela<64, false>&, unsigned int r_type,
566 elfcpp::Elf_types<64>::Elf_Addr value,
568 section_size_type view_size);
570 // Do a TLS Initial-Exec to Local-Exec transition.
572 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
573 Output_segment* tls_segment,
574 const elfcpp::Rela<64, false>&, unsigned int r_type,
575 elfcpp::Elf_types<64>::Elf_Addr value,
577 section_size_type view_size);
579 // This is set if we should skip the next reloc, which should be a
580 // PLT32 reloc against ___tls_get_addr.
581 bool skip_call_tls_get_addr_;
584 // A class which returns the size required for a relocation type,
585 // used while scanning relocs during a relocatable link.
586 class Relocatable_size_for_reloc
590 get_size_for_reloc(unsigned int, Relobj*);
593 // Adjust TLS relocation type based on the options and whether this
594 // is a local symbol.
595 static tls::Tls_optimization
596 optimize_tls_reloc(bool is_final, int r_type);
598 // Get the GOT section, creating it if necessary.
599 Output_data_got<64, false>*
600 got_section(Symbol_table*, Layout*);
602 // Get the GOT PLT section.
604 got_plt_section() const
606 gold_assert(this->got_plt_ != NULL);
607 return this->got_plt_;
610 // Get the GOT section for TLSDESC entries.
611 Output_data_got<64, false>*
612 got_tlsdesc_section() const
614 gold_assert(this->got_tlsdesc_ != NULL);
615 return this->got_tlsdesc_;
618 // Create the PLT section.
620 make_plt_section(Symbol_table* symtab, Layout* layout);
622 // Create a PLT entry for a global symbol.
624 make_plt_entry(Symbol_table*, Layout*, Symbol*);
626 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
628 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
629 Sized_relobj<64, false>* relobj,
630 unsigned int local_sym_index);
632 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
634 define_tls_base_symbol(Symbol_table*, Layout*);
636 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
638 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
640 // Create a GOT entry for the TLS module index.
642 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
643 Sized_relobj<64, false>* object);
645 // Get the PLT section.
646 Output_data_plt_x86_64*
649 gold_assert(this->plt_ != NULL);
653 // Get the dynamic reloc section, creating it if necessary.
655 rela_dyn_section(Layout*);
657 // Get the section to use for TLSDESC relocations.
659 rela_tlsdesc_section(Layout*) const;
661 // Add a potential copy relocation.
663 copy_reloc(Symbol_table* symtab, Layout* layout,
664 Sized_relobj<64, false>* object,
665 unsigned int shndx, Output_section* output_section,
666 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
668 this->copy_relocs_.copy_reloc(symtab, layout,
669 symtab->get_sized_symbol<64>(sym),
670 object, shndx, output_section,
671 reloc, this->rela_dyn_section(layout));
674 // Information about this specific target which we pass to the
675 // general Target structure.
676 static const Target::Target_info x86_64_info;
678 // The types of GOT entries needed for this platform.
679 // These values are exposed to the ABI in an incremental link.
680 // Do not renumber existing values without changing the version
681 // number of the .gnu_incremental_inputs section.
684 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
685 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
686 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
687 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
690 // This type is used as the argument to the target specific
691 // relocation routines. The only target specific reloc is
692 // R_X86_64_TLSDESC against a local symbol.
695 Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
696 : object(a_object), r_sym(a_r_sym)
699 // The object in which the local symbol is defined.
700 Sized_relobj<64, false>* object;
701 // The local symbol index in the object.
706 Output_data_got<64, false>* got_;
708 Output_data_plt_x86_64* plt_;
709 // The GOT PLT section.
710 Output_data_space* got_plt_;
711 // The GOT section for TLSDESC relocations.
712 Output_data_got<64, false>* got_tlsdesc_;
713 // The _GLOBAL_OFFSET_TABLE_ symbol.
714 Symbol* global_offset_table_;
715 // The dynamic reloc section.
716 Reloc_section* rela_dyn_;
717 // Relocs saved to avoid a COPY reloc.
718 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
719 // Space for variables copied with a COPY reloc.
720 Output_data_space* dynbss_;
721 // Offset of the GOT entry for the TLS module index.
722 unsigned int got_mod_index_offset_;
723 // We handle R_X86_64_TLSDESC against a local symbol as a target
724 // specific relocation. Here we store the object and local symbol
725 // index for the relocation.
726 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
727 // True if the _TLS_MODULE_BASE_ symbol has been defined.
728 bool tls_base_symbol_defined_;
731 const Target::Target_info Target_x86_64::x86_64_info =
734 false, // is_big_endian
735 elfcpp::EM_X86_64, // machine_code
736 false, // has_make_symbol
737 false, // has_resolve
738 true, // has_code_fill
739 true, // is_default_stack_executable
741 "/lib/ld64.so.1", // program interpreter
742 0x400000, // default_text_segment_address
743 0x1000, // abi_pagesize (overridable by -z max-page-size)
744 0x1000, // common_pagesize (overridable by -z common-page-size)
745 elfcpp::SHN_UNDEF, // small_common_shndx
746 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
747 0, // small_common_section_flags
748 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
749 NULL, // attributes_section
750 NULL // attributes_vendor
753 // This is called when a new output section is created. This is where
754 // we handle the SHF_X86_64_LARGE.
757 Target_x86_64::do_new_output_section(Output_section* os) const
759 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
760 os->set_is_large_section();
763 // Get the GOT section, creating it if necessary.
765 Output_data_got<64, false>*
766 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
768 if (this->got_ == NULL)
770 gold_assert(symtab != NULL && layout != NULL);
772 this->got_ = new Output_data_got<64, false>();
774 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
776 | elfcpp::SHF_WRITE),
777 this->got_, ORDER_RELRO_LAST,
780 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
781 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
783 | elfcpp::SHF_WRITE),
784 this->got_plt_, ORDER_NON_RELRO_FIRST,
787 // The first three entries are reserved.
788 this->got_plt_->set_current_data_size(3 * 8);
790 // Those bytes can go into the relro segment.
791 layout->increase_relro(3 * 8);
793 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
794 this->global_offset_table_ =
795 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
796 Symbol_table::PREDEFINED,
798 0, 0, elfcpp::STT_OBJECT,
800 elfcpp::STV_HIDDEN, 0,
803 // If there are any TLSDESC relocations, they get GOT entries in
804 // .got.plt after the jump slot entries.
805 this->got_tlsdesc_ = new Output_data_got<64, false>();
806 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
808 | elfcpp::SHF_WRITE),
810 ORDER_NON_RELRO_FIRST, false);
816 // Get the dynamic reloc section, creating it if necessary.
818 Target_x86_64::Reloc_section*
819 Target_x86_64::rela_dyn_section(Layout* layout)
821 if (this->rela_dyn_ == NULL)
823 gold_assert(layout != NULL);
824 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
825 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
826 elfcpp::SHF_ALLOC, this->rela_dyn_,
827 ORDER_DYNAMIC_RELOCS, false);
829 return this->rela_dyn_;
832 // Initialize the PLT section.
835 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
837 this->rel_ = new Reloc_section(false);
838 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
839 elfcpp::SHF_ALLOC, this->rel_,
840 ORDER_DYNAMIC_PLT_RELOCS, false);
842 if (parameters->doing_static_link())
844 // A statically linked executable will only have a .rela.plt
845 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
846 // symbols. The library will use these symbols to locate the
847 // IRELATIVE relocs at program startup time.
848 symtab->define_in_output_data("__rela_iplt_start", NULL,
849 Symbol_table::PREDEFINED,
850 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
851 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
853 symtab->define_in_output_data("__rela_iplt_end", NULL,
854 Symbol_table::PREDEFINED,
855 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
856 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
862 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
864 os->set_entsize(plt_entry_size);
867 // Add an entry to the PLT.
870 Output_data_plt_x86_64::add_entry(Symbol* gsym)
872 gold_assert(!gsym->has_plt_offset());
874 unsigned int plt_index;
876 section_offset_type got_offset;
878 if (!this->is_data_size_valid())
880 // Note that when setting the PLT offset we skip the initial
881 // reserved PLT entry.
882 plt_index = this->count_ + 1;
883 plt_offset = plt_index * plt_entry_size;
887 got_offset = (plt_index - 1 + 3) * 8;
888 gold_assert(got_offset == this->got_plt_->current_data_size());
890 // Every PLT entry needs a GOT entry which points back to the PLT
891 // entry (this will be changed by the dynamic linker, normally
892 // lazily when the function is called).
893 this->got_plt_->set_current_data_size(got_offset + 8);
897 // For incremental updates, find an available slot.
898 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
899 if (plt_offset == -1)
900 gold_fatal(_("out of patch space (PLT);"
901 " relink with --incremental-full"));
903 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
904 // can be calculated from the PLT index, adjusting for the three
905 // reserved entries at the beginning of the GOT.
906 plt_index = plt_offset / plt_entry_size - 1;
907 got_offset = (plt_index - 1 + 3) * 8;
910 gsym->set_plt_offset(plt_offset);
912 // Every PLT entry needs a reloc.
913 this->add_relocation(gsym, got_offset);
915 // Note that we don't need to save the symbol. The contents of the
916 // PLT are independent of which symbols are used. The symbols only
917 // appear in the relocations.
920 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
924 Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
925 unsigned int local_sym_index)
927 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
930 section_offset_type got_offset = this->got_plt_->current_data_size();
932 // Every PLT entry needs a GOT entry which points back to the PLT
934 this->got_plt_->set_current_data_size(got_offset + 8);
936 // Every PLT entry needs a reloc.
937 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
938 elfcpp::R_X86_64_IRELATIVE,
939 this->got_plt_, got_offset, 0);
944 // Add the relocation for a PLT entry.
947 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
949 if (gsym->type() == elfcpp::STT_GNU_IFUNC
950 && gsym->can_use_relative_reloc(false))
951 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
952 this->got_plt_, got_offset, 0);
955 gsym->set_needs_dynsym_entry();
956 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
961 // Return where the TLSDESC relocations should go, creating it if
962 // necessary. These follow the JUMP_SLOT relocations.
964 Output_data_plt_x86_64::Reloc_section*
965 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
967 if (this->tlsdesc_rel_ == NULL)
969 this->tlsdesc_rel_ = new Reloc_section(false);
970 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
971 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
972 ORDER_DYNAMIC_PLT_RELOCS, false);
973 gold_assert(this->tlsdesc_rel_->output_section() ==
974 this->rel_->output_section());
976 return this->tlsdesc_rel_;
979 // Set the final size.
981 Output_data_plt_x86_64::set_final_data_size()
983 unsigned int count = this->count_;
984 if (this->has_tlsdesc_entry())
986 this->set_data_size((count + 1) * plt_entry_size);
989 // The first entry in the PLT for an executable.
991 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
993 // From AMD64 ABI Draft 0.98, page 76
994 0xff, 0x35, // pushq contents of memory address
995 0, 0, 0, 0, // replaced with address of .got + 8
996 0xff, 0x25, // jmp indirect
997 0, 0, 0, 0, // replaced with address of .got + 16
998 0x90, 0x90, 0x90, 0x90 // noop (x4)
1001 // Subsequent entries in the PLT for an executable.
1003 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1005 // From AMD64 ABI Draft 0.98, page 76
1006 0xff, 0x25, // jmpq indirect
1007 0, 0, 0, 0, // replaced with address of symbol in .got
1008 0x68, // pushq immediate
1009 0, 0, 0, 0, // replaced with offset into relocation table
1010 0xe9, // jmpq relative
1011 0, 0, 0, 0 // replaced with offset to start of .plt
1014 // The reserved TLSDESC entry in the PLT for an executable.
1016 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1018 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1019 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1020 0xff, 0x35, // pushq x(%rip)
1021 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1022 0xff, 0x25, // jmpq *y(%rip)
1023 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1028 // Write out the PLT. This uses the hand-coded instructions above,
1029 // and adjusts them as needed. This is specified by the AMD64 ABI.
1032 Output_data_plt_x86_64::do_write(Output_file* of)
1034 const off_t offset = this->offset();
1035 const section_size_type oview_size =
1036 convert_to_section_size_type(this->data_size());
1037 unsigned char* const oview = of->get_output_view(offset, oview_size);
1039 const off_t got_file_offset = this->got_plt_->offset();
1040 const section_size_type got_size =
1041 convert_to_section_size_type(this->got_plt_->data_size());
1042 unsigned char* const got_view = of->get_output_view(got_file_offset,
1045 unsigned char* pov = oview;
1047 // The base address of the .plt section.
1048 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1049 // The base address of the .got section.
1050 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1051 // The base address of the PLT portion of the .got section,
1052 // which is where the GOT pointer will point, and where the
1053 // three reserved GOT entries are located.
1054 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1056 memcpy(pov, first_plt_entry, plt_entry_size);
1057 // We do a jmp relative to the PC at the end of this instruction.
1058 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1060 - (plt_address + 6)));
1061 elfcpp::Swap<32, false>::writeval(pov + 8,
1063 - (plt_address + 12)));
1064 pov += plt_entry_size;
1066 unsigned char* got_pov = got_view;
1068 memset(got_pov, 0, 24);
1071 unsigned int plt_offset = plt_entry_size;
1072 unsigned int got_offset = 24;
1073 const unsigned int count = this->count_;
1074 for (unsigned int plt_index = 0;
1077 pov += plt_entry_size,
1079 plt_offset += plt_entry_size,
1082 // Set and adjust the PLT entry itself.
1083 memcpy(pov, plt_entry, plt_entry_size);
1084 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1085 (got_address + got_offset
1086 - (plt_address + plt_offset
1089 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1090 elfcpp::Swap<32, false>::writeval(pov + 12,
1091 - (plt_offset + plt_entry_size));
1093 // Set the entry in the GOT.
1094 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1097 if (this->has_tlsdesc_entry())
1099 // Set and adjust the reserved TLSDESC PLT entry.
1100 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1101 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1102 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1104 - (plt_address + plt_offset
1106 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1108 + tlsdesc_got_offset
1109 - (plt_address + plt_offset
1111 pov += plt_entry_size;
1114 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1115 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1117 of->write_output_view(offset, oview_size, oview);
1118 of->write_output_view(got_file_offset, got_size, got_view);
1121 // Create the PLT section.
1124 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1126 if (this->plt_ == NULL)
1128 // Create the GOT sections first.
1129 this->got_section(symtab, layout);
1131 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1133 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1135 | elfcpp::SHF_EXECINSTR),
1136 this->plt_, ORDER_PLT, false);
1138 // Make the sh_info field of .rela.plt point to .plt.
1139 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1140 rela_plt_os->set_info_section(this->plt_->output_section());
1144 // Return the section for TLSDESC relocations.
1146 Target_x86_64::Reloc_section*
1147 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1149 return this->plt_section()->rela_tlsdesc(layout);
1152 // Create a PLT entry for a global symbol.
1155 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1158 if (gsym->has_plt_offset())
1161 if (this->plt_ == NULL)
1162 this->make_plt_section(symtab, layout);
1164 this->plt_->add_entry(gsym);
1167 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1170 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1171 Sized_relobj<64, false>* relobj,
1172 unsigned int local_sym_index)
1174 if (relobj->local_has_plt_offset(local_sym_index))
1176 if (this->plt_ == NULL)
1177 this->make_plt_section(symtab, layout);
1178 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1180 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1183 // Return the number of entries in the PLT.
1186 Target_x86_64::plt_entry_count() const
1188 if (this->plt_ == NULL)
1190 return this->plt_->entry_count();
1193 // Return the offset of the first non-reserved PLT entry.
1196 Target_x86_64::first_plt_entry_offset() const
1198 return Output_data_plt_x86_64::first_plt_entry_offset();
1201 // Return the size of each PLT entry.
1204 Target_x86_64::plt_entry_size() const
1206 return Output_data_plt_x86_64::get_plt_entry_size();
1209 // Create the GOT and PLT sections for an incremental update.
1211 Output_data_got<64, false>*
1212 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1214 unsigned int got_count,
1215 unsigned int plt_count)
1217 gold_assert(this->got_ == NULL);
1219 this->got_ = new Output_data_got<64, false>(got_count * 8);
1220 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1222 | elfcpp::SHF_WRITE),
1223 this->got_, ORDER_RELRO_LAST,
1226 // Add the three reserved entries.
1227 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1228 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1230 | elfcpp::SHF_WRITE),
1231 this->got_plt_, ORDER_NON_RELRO_FIRST,
1234 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1235 this->global_offset_table_ =
1236 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1237 Symbol_table::PREDEFINED,
1239 0, 0, elfcpp::STT_OBJECT,
1241 elfcpp::STV_HIDDEN, 0,
1244 // If there are any TLSDESC relocations, they get GOT entries in
1245 // .got.plt after the jump slot entries.
1246 // FIXME: Get the count for TLSDESC entries.
1247 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1248 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1249 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1251 ORDER_NON_RELRO_FIRST, false);
1253 // Create the PLT section.
1254 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1255 this->got_plt_, plt_count);
1256 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1257 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1258 this->plt_, ORDER_PLT, false);
1260 // Make the sh_info field of .rela.plt point to .plt.
1261 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1262 rela_plt_os->set_info_section(this->plt_->output_section());
1267 // Register an existing PLT entry for a global symbol.
1270 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1273 gold_assert(this->plt_ != NULL);
1274 gold_assert(!gsym->has_plt_offset());
1276 this->plt_->reserve_slot(plt_index);
1278 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1280 unsigned int got_offset = (plt_index + 3) * 8;
1281 this->plt_->add_relocation(gsym, got_offset);
1284 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1287 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1289 if (this->tls_base_symbol_defined_)
1292 Output_segment* tls_segment = layout->tls_segment();
1293 if (tls_segment != NULL)
1295 bool is_exec = parameters->options().output_is_executable();
1296 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1297 Symbol_table::PREDEFINED,
1301 elfcpp::STV_HIDDEN, 0,
1303 ? Symbol::SEGMENT_END
1304 : Symbol::SEGMENT_START),
1307 this->tls_base_symbol_defined_ = true;
1310 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1313 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1316 if (this->plt_ == NULL)
1317 this->make_plt_section(symtab, layout);
1319 if (!this->plt_->has_tlsdesc_entry())
1321 // Allocate the TLSDESC_GOT entry.
1322 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1323 unsigned int got_offset = got->add_constant(0);
1325 // Allocate the TLSDESC_PLT entry.
1326 this->plt_->reserve_tlsdesc_entry(got_offset);
1330 // Create a GOT entry for the TLS module index.
1333 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1334 Sized_relobj<64, false>* object)
1336 if (this->got_mod_index_offset_ == -1U)
1338 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1339 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1340 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1341 unsigned int got_offset = got->add_constant(0);
1342 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1344 got->add_constant(0);
1345 this->got_mod_index_offset_ = got_offset;
1347 return this->got_mod_index_offset_;
1350 // Optimize the TLS relocation type based on what we know about the
1351 // symbol. IS_FINAL is true if the final address of this symbol is
1352 // known at link time.
1354 tls::Tls_optimization
1355 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1357 // If we are generating a shared library, then we can't do anything
1359 if (parameters->options().shared())
1360 return tls::TLSOPT_NONE;
1364 case elfcpp::R_X86_64_TLSGD:
1365 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1366 case elfcpp::R_X86_64_TLSDESC_CALL:
1367 // These are General-Dynamic which permits fully general TLS
1368 // access. Since we know that we are generating an executable,
1369 // we can convert this to Initial-Exec. If we also know that
1370 // this is a local symbol, we can further switch to Local-Exec.
1372 return tls::TLSOPT_TO_LE;
1373 return tls::TLSOPT_TO_IE;
1375 case elfcpp::R_X86_64_TLSLD:
1376 // This is Local-Dynamic, which refers to a local symbol in the
1377 // dynamic TLS block. Since we know that we generating an
1378 // executable, we can switch to Local-Exec.
1379 return tls::TLSOPT_TO_LE;
1381 case elfcpp::R_X86_64_DTPOFF32:
1382 case elfcpp::R_X86_64_DTPOFF64:
1383 // Another Local-Dynamic reloc.
1384 return tls::TLSOPT_TO_LE;
1386 case elfcpp::R_X86_64_GOTTPOFF:
1387 // These are Initial-Exec relocs which get the thread offset
1388 // from the GOT. If we know that we are linking against the
1389 // local symbol, we can switch to Local-Exec, which links the
1390 // thread offset into the instruction.
1392 return tls::TLSOPT_TO_LE;
1393 return tls::TLSOPT_NONE;
1395 case elfcpp::R_X86_64_TPOFF32:
1396 // When we already have Local-Exec, there is nothing further we
1398 return tls::TLSOPT_NONE;
1405 // Get the Reference_flags for a particular relocation.
1408 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1412 case elfcpp::R_X86_64_NONE:
1413 case elfcpp::R_X86_64_GNU_VTINHERIT:
1414 case elfcpp::R_X86_64_GNU_VTENTRY:
1415 case elfcpp::R_X86_64_GOTPC32:
1416 case elfcpp::R_X86_64_GOTPC64:
1417 // No symbol reference.
1420 case elfcpp::R_X86_64_64:
1421 case elfcpp::R_X86_64_32:
1422 case elfcpp::R_X86_64_32S:
1423 case elfcpp::R_X86_64_16:
1424 case elfcpp::R_X86_64_8:
1425 return Symbol::ABSOLUTE_REF;
1427 case elfcpp::R_X86_64_PC64:
1428 case elfcpp::R_X86_64_PC32:
1429 case elfcpp::R_X86_64_PC16:
1430 case elfcpp::R_X86_64_PC8:
1431 case elfcpp::R_X86_64_GOTOFF64:
1432 return Symbol::RELATIVE_REF;
1434 case elfcpp::R_X86_64_PLT32:
1435 case elfcpp::R_X86_64_PLTOFF64:
1436 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1438 case elfcpp::R_X86_64_GOT64:
1439 case elfcpp::R_X86_64_GOT32:
1440 case elfcpp::R_X86_64_GOTPCREL64:
1441 case elfcpp::R_X86_64_GOTPCREL:
1442 case elfcpp::R_X86_64_GOTPLT64:
1444 return Symbol::ABSOLUTE_REF;
1446 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1447 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1448 case elfcpp::R_X86_64_TLSDESC_CALL:
1449 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1450 case elfcpp::R_X86_64_DTPOFF32:
1451 case elfcpp::R_X86_64_DTPOFF64:
1452 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1453 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1454 return Symbol::TLS_REF;
1456 case elfcpp::R_X86_64_COPY:
1457 case elfcpp::R_X86_64_GLOB_DAT:
1458 case elfcpp::R_X86_64_JUMP_SLOT:
1459 case elfcpp::R_X86_64_RELATIVE:
1460 case elfcpp::R_X86_64_IRELATIVE:
1461 case elfcpp::R_X86_64_TPOFF64:
1462 case elfcpp::R_X86_64_DTPMOD64:
1463 case elfcpp::R_X86_64_TLSDESC:
1464 case elfcpp::R_X86_64_SIZE32:
1465 case elfcpp::R_X86_64_SIZE64:
1467 // Not expected. We will give an error later.
1472 // Report an unsupported relocation against a local symbol.
1475 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1476 unsigned int r_type)
1478 gold_error(_("%s: unsupported reloc %u against local symbol"),
1479 object->name().c_str(), r_type);
1482 // We are about to emit a dynamic relocation of type R_TYPE. If the
1483 // dynamic linker does not support it, issue an error. The GNU linker
1484 // only issues a non-PIC error for an allocated read-only section.
1485 // Here we know the section is allocated, but we don't know that it is
1486 // read-only. But we check for all the relocation types which the
1487 // glibc dynamic linker supports, so it seems appropriate to issue an
1488 // error even if the section is not read-only.
1491 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1495 // These are the relocation types supported by glibc for x86_64
1496 // which should always work.
1497 case elfcpp::R_X86_64_RELATIVE:
1498 case elfcpp::R_X86_64_IRELATIVE:
1499 case elfcpp::R_X86_64_GLOB_DAT:
1500 case elfcpp::R_X86_64_JUMP_SLOT:
1501 case elfcpp::R_X86_64_DTPMOD64:
1502 case elfcpp::R_X86_64_DTPOFF64:
1503 case elfcpp::R_X86_64_TPOFF64:
1504 case elfcpp::R_X86_64_64:
1505 case elfcpp::R_X86_64_COPY:
1508 // glibc supports these reloc types, but they can overflow.
1509 case elfcpp::R_X86_64_32:
1510 case elfcpp::R_X86_64_PC32:
1511 if (this->issued_non_pic_error_)
1513 gold_assert(parameters->options().output_is_position_independent());
1514 object->error(_("requires dynamic reloc which may overflow at runtime; "
1515 "recompile with -fPIC"));
1516 this->issued_non_pic_error_ = true;
1520 // This prevents us from issuing more than one error per reloc
1521 // section. But we can still wind up issuing more than one
1522 // error per object file.
1523 if (this->issued_non_pic_error_)
1525 gold_assert(parameters->options().output_is_position_independent());
1526 object->error(_("requires unsupported dynamic reloc; "
1527 "recompile with -fPIC"));
1528 this->issued_non_pic_error_ = true;
1531 case elfcpp::R_X86_64_NONE:
1536 // Return whether we need to make a PLT entry for a relocation of the
1537 // given type against a STT_GNU_IFUNC symbol.
1540 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<64, false>* object,
1541 unsigned int r_type)
1543 int flags = Scan::get_reference_flags(r_type);
1544 if (flags & Symbol::TLS_REF)
1545 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1546 object->name().c_str(), r_type);
1550 // Scan a relocation for a local symbol.
1553 Target_x86_64::Scan::local(Symbol_table* symtab,
1555 Target_x86_64* target,
1556 Sized_relobj<64, false>* object,
1557 unsigned int data_shndx,
1558 Output_section* output_section,
1559 const elfcpp::Rela<64, false>& reloc,
1560 unsigned int r_type,
1561 const elfcpp::Sym<64, false>& lsym)
1563 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1564 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1565 && this->reloc_needs_plt_for_ifunc(object, r_type))
1567 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1568 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1573 case elfcpp::R_X86_64_NONE:
1574 case elfcpp::R_X86_64_GNU_VTINHERIT:
1575 case elfcpp::R_X86_64_GNU_VTENTRY:
1578 case elfcpp::R_X86_64_64:
1579 // If building a shared library (or a position-independent
1580 // executable), we need to create a dynamic relocation for this
1581 // location. The relocation applied at link time will apply the
1582 // link-time value, so we flag the location with an
1583 // R_X86_64_RELATIVE relocation so the dynamic loader can
1584 // relocate it easily.
1585 if (parameters->options().output_is_position_independent())
1587 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1588 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1589 rela_dyn->add_local_relative(object, r_sym,
1590 elfcpp::R_X86_64_RELATIVE,
1591 output_section, data_shndx,
1592 reloc.get_r_offset(),
1593 reloc.get_r_addend());
1597 case elfcpp::R_X86_64_32:
1598 case elfcpp::R_X86_64_32S:
1599 case elfcpp::R_X86_64_16:
1600 case elfcpp::R_X86_64_8:
1601 // If building a shared library (or a position-independent
1602 // executable), we need to create a dynamic relocation for this
1603 // location. We can't use an R_X86_64_RELATIVE relocation
1604 // because that is always a 64-bit relocation.
1605 if (parameters->options().output_is_position_independent())
1607 this->check_non_pic(object, r_type);
1609 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1610 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1611 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1612 rela_dyn->add_local(object, r_sym, r_type, output_section,
1613 data_shndx, reloc.get_r_offset(),
1614 reloc.get_r_addend());
1617 gold_assert(lsym.get_st_value() == 0);
1618 unsigned int shndx = lsym.get_st_shndx();
1620 shndx = object->adjust_sym_shndx(r_sym, shndx,
1623 object->error(_("section symbol %u has bad shndx %u"),
1626 rela_dyn->add_local_section(object, shndx,
1627 r_type, output_section,
1628 data_shndx, reloc.get_r_offset(),
1629 reloc.get_r_addend());
1634 case elfcpp::R_X86_64_PC64:
1635 case elfcpp::R_X86_64_PC32:
1636 case elfcpp::R_X86_64_PC16:
1637 case elfcpp::R_X86_64_PC8:
1640 case elfcpp::R_X86_64_PLT32:
1641 // Since we know this is a local symbol, we can handle this as a
1645 case elfcpp::R_X86_64_GOTPC32:
1646 case elfcpp::R_X86_64_GOTOFF64:
1647 case elfcpp::R_X86_64_GOTPC64:
1648 case elfcpp::R_X86_64_PLTOFF64:
1649 // We need a GOT section.
1650 target->got_section(symtab, layout);
1651 // For PLTOFF64, we'd normally want a PLT section, but since we
1652 // know this is a local symbol, no PLT is needed.
1655 case elfcpp::R_X86_64_GOT64:
1656 case elfcpp::R_X86_64_GOT32:
1657 case elfcpp::R_X86_64_GOTPCREL64:
1658 case elfcpp::R_X86_64_GOTPCREL:
1659 case elfcpp::R_X86_64_GOTPLT64:
1661 // The symbol requires a GOT entry.
1662 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1663 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1665 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1666 // lets function pointers compare correctly with shared
1667 // libraries. Otherwise we would need an IRELATIVE reloc.
1669 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1670 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1672 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1675 // If we are generating a shared object, we need to add a
1676 // dynamic relocation for this symbol's GOT entry.
1677 if (parameters->options().output_is_position_independent())
1679 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1680 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1681 if (r_type != elfcpp::R_X86_64_GOT32)
1683 unsigned int got_offset =
1684 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1685 rela_dyn->add_local_relative(object, r_sym,
1686 elfcpp::R_X86_64_RELATIVE,
1687 got, got_offset, 0);
1691 this->check_non_pic(object, r_type);
1693 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1694 rela_dyn->add_local(
1695 object, r_sym, r_type, got,
1696 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1700 // For GOTPLT64, we'd normally want a PLT section, but since
1701 // we know this is a local symbol, no PLT is needed.
1705 case elfcpp::R_X86_64_COPY:
1706 case elfcpp::R_X86_64_GLOB_DAT:
1707 case elfcpp::R_X86_64_JUMP_SLOT:
1708 case elfcpp::R_X86_64_RELATIVE:
1709 case elfcpp::R_X86_64_IRELATIVE:
1710 // These are outstanding tls relocs, which are unexpected when linking
1711 case elfcpp::R_X86_64_TPOFF64:
1712 case elfcpp::R_X86_64_DTPMOD64:
1713 case elfcpp::R_X86_64_TLSDESC:
1714 gold_error(_("%s: unexpected reloc %u in object file"),
1715 object->name().c_str(), r_type);
1718 // These are initial tls relocs, which are expected when linking
1719 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1720 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1721 case elfcpp::R_X86_64_TLSDESC_CALL:
1722 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1723 case elfcpp::R_X86_64_DTPOFF32:
1724 case elfcpp::R_X86_64_DTPOFF64:
1725 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1726 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1728 bool output_is_shared = parameters->options().shared();
1729 const tls::Tls_optimization optimized_type
1730 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1733 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1734 if (optimized_type == tls::TLSOPT_NONE)
1736 // Create a pair of GOT entries for the module index and
1737 // dtv-relative offset.
1738 Output_data_got<64, false>* got
1739 = target->got_section(symtab, layout);
1740 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1741 unsigned int shndx = lsym.get_st_shndx();
1743 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1745 object->error(_("local symbol %u has bad shndx %u"),
1748 got->add_local_pair_with_rela(object, r_sym,
1751 target->rela_dyn_section(layout),
1752 elfcpp::R_X86_64_DTPMOD64, 0);
1754 else if (optimized_type != tls::TLSOPT_TO_LE)
1755 unsupported_reloc_local(object, r_type);
1758 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1759 target->define_tls_base_symbol(symtab, layout);
1760 if (optimized_type == tls::TLSOPT_NONE)
1762 // Create reserved PLT and GOT entries for the resolver.
1763 target->reserve_tlsdesc_entries(symtab, layout);
1765 // Generate a double GOT entry with an
1766 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1767 // is resolved lazily, so the GOT entry needs to be in
1768 // an area in .got.plt, not .got. Call got_section to
1769 // make sure the section has been created.
1770 target->got_section(symtab, layout);
1771 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1772 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1773 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1775 unsigned int got_offset = got->add_constant(0);
1776 got->add_constant(0);
1777 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1779 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1780 // We store the arguments we need in a vector, and
1781 // use the index into the vector as the parameter
1782 // to pass to the target specific routines.
1783 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1784 void* arg = reinterpret_cast<void*>(intarg);
1785 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1786 got, got_offset, 0);
1789 else if (optimized_type != tls::TLSOPT_TO_LE)
1790 unsupported_reloc_local(object, r_type);
1793 case elfcpp::R_X86_64_TLSDESC_CALL:
1796 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1797 if (optimized_type == tls::TLSOPT_NONE)
1799 // Create a GOT entry for the module index.
1800 target->got_mod_index_entry(symtab, layout, object);
1802 else if (optimized_type != tls::TLSOPT_TO_LE)
1803 unsupported_reloc_local(object, r_type);
1806 case elfcpp::R_X86_64_DTPOFF32:
1807 case elfcpp::R_X86_64_DTPOFF64:
1810 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1811 layout->set_has_static_tls();
1812 if (optimized_type == tls::TLSOPT_NONE)
1814 // Create a GOT entry for the tp-relative offset.
1815 Output_data_got<64, false>* got
1816 = target->got_section(symtab, layout);
1817 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1818 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1819 target->rela_dyn_section(layout),
1820 elfcpp::R_X86_64_TPOFF64);
1822 else if (optimized_type != tls::TLSOPT_TO_LE)
1823 unsupported_reloc_local(object, r_type);
1826 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1827 layout->set_has_static_tls();
1828 if (output_is_shared)
1829 unsupported_reloc_local(object, r_type);
1838 case elfcpp::R_X86_64_SIZE32:
1839 case elfcpp::R_X86_64_SIZE64:
1841 gold_error(_("%s: unsupported reloc %u against local symbol"),
1842 object->name().c_str(), r_type);
1848 // Report an unsupported relocation against a global symbol.
1851 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1852 unsigned int r_type,
1855 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1856 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1859 // Returns true if this relocation type could be that of a function pointer.
1861 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1865 case elfcpp::R_X86_64_64:
1866 case elfcpp::R_X86_64_32:
1867 case elfcpp::R_X86_64_32S:
1868 case elfcpp::R_X86_64_16:
1869 case elfcpp::R_X86_64_8:
1870 case elfcpp::R_X86_64_GOT64:
1871 case elfcpp::R_X86_64_GOT32:
1872 case elfcpp::R_X86_64_GOTPCREL64:
1873 case elfcpp::R_X86_64_GOTPCREL:
1874 case elfcpp::R_X86_64_GOTPLT64:
1882 // For safe ICF, scan a relocation for a local symbol to check if it
1883 // corresponds to a function pointer being taken. In that case mark
1884 // the function whose pointer was taken as not foldable.
1887 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1891 Sized_relobj<64, false>* ,
1894 const elfcpp::Rela<64, false>& ,
1895 unsigned int r_type,
1896 const elfcpp::Sym<64, false>&)
1898 // When building a shared library, do not fold any local symbols as it is
1899 // not possible to distinguish pointer taken versus a call by looking at
1900 // the relocation types.
1901 return (parameters->options().shared()
1902 || possible_function_pointer_reloc(r_type));
1905 // For safe ICF, scan a relocation for a global symbol to check if it
1906 // corresponds to a function pointer being taken. In that case mark
1907 // the function whose pointer was taken as not foldable.
1910 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1914 Sized_relobj<64, false>* ,
1917 const elfcpp::Rela<64, false>& ,
1918 unsigned int r_type,
1921 // When building a shared library, do not fold symbols whose visibility
1922 // is hidden, internal or protected.
1923 return ((parameters->options().shared()
1924 && (gsym->visibility() == elfcpp::STV_INTERNAL
1925 || gsym->visibility() == elfcpp::STV_PROTECTED
1926 || gsym->visibility() == elfcpp::STV_HIDDEN))
1927 || possible_function_pointer_reloc(r_type));
1930 // Scan a relocation for a global symbol.
1933 Target_x86_64::Scan::global(Symbol_table* symtab,
1935 Target_x86_64* target,
1936 Sized_relobj<64, false>* object,
1937 unsigned int data_shndx,
1938 Output_section* output_section,
1939 const elfcpp::Rela<64, false>& reloc,
1940 unsigned int r_type,
1943 // A STT_GNU_IFUNC symbol may require a PLT entry.
1944 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1945 && this->reloc_needs_plt_for_ifunc(object, r_type))
1946 target->make_plt_entry(symtab, layout, gsym);
1950 case elfcpp::R_X86_64_NONE:
1951 case elfcpp::R_X86_64_GNU_VTINHERIT:
1952 case elfcpp::R_X86_64_GNU_VTENTRY:
1955 case elfcpp::R_X86_64_64:
1956 case elfcpp::R_X86_64_32:
1957 case elfcpp::R_X86_64_32S:
1958 case elfcpp::R_X86_64_16:
1959 case elfcpp::R_X86_64_8:
1961 // Make a PLT entry if necessary.
1962 if (gsym->needs_plt_entry())
1964 target->make_plt_entry(symtab, layout, gsym);
1965 // Since this is not a PC-relative relocation, we may be
1966 // taking the address of a function. In that case we need to
1967 // set the entry in the dynamic symbol table to the address of
1969 if (gsym->is_from_dynobj() && !parameters->options().shared())
1970 gsym->set_needs_dynsym_value();
1972 // Make a dynamic relocation if necessary.
1973 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1975 if (gsym->may_need_copy_reloc())
1977 target->copy_reloc(symtab, layout, object,
1978 data_shndx, output_section, gsym, reloc);
1980 else if (r_type == elfcpp::R_X86_64_64
1981 && gsym->type() == elfcpp::STT_GNU_IFUNC
1982 && gsym->can_use_relative_reloc(false)
1983 && !gsym->is_from_dynobj()
1984 && !gsym->is_undefined()
1985 && !gsym->is_preemptible())
1987 // Use an IRELATIVE reloc for a locally defined
1988 // STT_GNU_IFUNC symbol. This makes a function
1989 // address in a PIE executable match the address in a
1990 // shared library that it links against.
1991 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1992 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
1993 rela_dyn->add_symbolless_global_addend(gsym, r_type,
1994 output_section, object,
1996 reloc.get_r_offset(),
1997 reloc.get_r_addend());
1999 else if (r_type == elfcpp::R_X86_64_64
2000 && gsym->can_use_relative_reloc(false))
2002 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2003 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2004 output_section, object,
2006 reloc.get_r_offset(),
2007 reloc.get_r_addend());
2011 this->check_non_pic(object, r_type);
2012 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2013 rela_dyn->add_global(gsym, r_type, output_section, object,
2014 data_shndx, reloc.get_r_offset(),
2015 reloc.get_r_addend());
2021 case elfcpp::R_X86_64_PC64:
2022 case elfcpp::R_X86_64_PC32:
2023 case elfcpp::R_X86_64_PC16:
2024 case elfcpp::R_X86_64_PC8:
2026 // Make a PLT entry if necessary.
2027 if (gsym->needs_plt_entry())
2028 target->make_plt_entry(symtab, layout, gsym);
2029 // Make a dynamic relocation if necessary.
2030 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2032 if (gsym->may_need_copy_reloc())
2034 target->copy_reloc(symtab, layout, object,
2035 data_shndx, output_section, gsym, reloc);
2039 this->check_non_pic(object, r_type);
2040 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2041 rela_dyn->add_global(gsym, r_type, output_section, object,
2042 data_shndx, reloc.get_r_offset(),
2043 reloc.get_r_addend());
2049 case elfcpp::R_X86_64_GOT64:
2050 case elfcpp::R_X86_64_GOT32:
2051 case elfcpp::R_X86_64_GOTPCREL64:
2052 case elfcpp::R_X86_64_GOTPCREL:
2053 case elfcpp::R_X86_64_GOTPLT64:
2055 // The symbol requires a GOT entry.
2056 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2057 if (gsym->final_value_is_known())
2059 // For a STT_GNU_IFUNC symbol we want the PLT address.
2060 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2061 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2063 got->add_global(gsym, GOT_TYPE_STANDARD);
2067 // If this symbol is not fully resolved, we need to add a
2068 // dynamic relocation for it.
2069 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2070 if (gsym->is_from_dynobj()
2071 || gsym->is_undefined()
2072 || gsym->is_preemptible()
2073 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2074 && parameters->options().output_is_position_independent()))
2075 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2076 elfcpp::R_X86_64_GLOB_DAT);
2079 // For a STT_GNU_IFUNC symbol we want to write the PLT
2080 // offset into the GOT, so that function pointer
2081 // comparisons work correctly.
2083 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2084 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2087 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2088 // Tell the dynamic linker to use the PLT address
2089 // when resolving relocations.
2090 if (gsym->is_from_dynobj()
2091 && !parameters->options().shared())
2092 gsym->set_needs_dynsym_value();
2096 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2097 rela_dyn->add_global_relative(gsym,
2098 elfcpp::R_X86_64_RELATIVE,
2103 // For GOTPLT64, we also need a PLT entry (but only if the
2104 // symbol is not fully resolved).
2105 if (r_type == elfcpp::R_X86_64_GOTPLT64
2106 && !gsym->final_value_is_known())
2107 target->make_plt_entry(symtab, layout, gsym);
2111 case elfcpp::R_X86_64_PLT32:
2112 // If the symbol is fully resolved, this is just a PC32 reloc.
2113 // Otherwise we need a PLT entry.
2114 if (gsym->final_value_is_known())
2116 // If building a shared library, we can also skip the PLT entry
2117 // if the symbol is defined in the output file and is protected
2119 if (gsym->is_defined()
2120 && !gsym->is_from_dynobj()
2121 && !gsym->is_preemptible())
2123 target->make_plt_entry(symtab, layout, gsym);
2126 case elfcpp::R_X86_64_GOTPC32:
2127 case elfcpp::R_X86_64_GOTOFF64:
2128 case elfcpp::R_X86_64_GOTPC64:
2129 case elfcpp::R_X86_64_PLTOFF64:
2130 // We need a GOT section.
2131 target->got_section(symtab, layout);
2132 // For PLTOFF64, we also need a PLT entry (but only if the
2133 // symbol is not fully resolved).
2134 if (r_type == elfcpp::R_X86_64_PLTOFF64
2135 && !gsym->final_value_is_known())
2136 target->make_plt_entry(symtab, layout, gsym);
2139 case elfcpp::R_X86_64_COPY:
2140 case elfcpp::R_X86_64_GLOB_DAT:
2141 case elfcpp::R_X86_64_JUMP_SLOT:
2142 case elfcpp::R_X86_64_RELATIVE:
2143 case elfcpp::R_X86_64_IRELATIVE:
2144 // These are outstanding tls relocs, which are unexpected when linking
2145 case elfcpp::R_X86_64_TPOFF64:
2146 case elfcpp::R_X86_64_DTPMOD64:
2147 case elfcpp::R_X86_64_TLSDESC:
2148 gold_error(_("%s: unexpected reloc %u in object file"),
2149 object->name().c_str(), r_type);
2152 // These are initial tls relocs, which are expected for global()
2153 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2154 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2155 case elfcpp::R_X86_64_TLSDESC_CALL:
2156 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2157 case elfcpp::R_X86_64_DTPOFF32:
2158 case elfcpp::R_X86_64_DTPOFF64:
2159 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2160 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2162 const bool is_final = gsym->final_value_is_known();
2163 const tls::Tls_optimization optimized_type
2164 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2167 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2168 if (optimized_type == tls::TLSOPT_NONE)
2170 // Create a pair of GOT entries for the module index and
2171 // dtv-relative offset.
2172 Output_data_got<64, false>* got
2173 = target->got_section(symtab, layout);
2174 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2175 target->rela_dyn_section(layout),
2176 elfcpp::R_X86_64_DTPMOD64,
2177 elfcpp::R_X86_64_DTPOFF64);
2179 else if (optimized_type == tls::TLSOPT_TO_IE)
2181 // Create a GOT entry for the tp-relative offset.
2182 Output_data_got<64, false>* got
2183 = target->got_section(symtab, layout);
2184 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2185 target->rela_dyn_section(layout),
2186 elfcpp::R_X86_64_TPOFF64);
2188 else if (optimized_type != tls::TLSOPT_TO_LE)
2189 unsupported_reloc_global(object, r_type, gsym);
2192 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2193 target->define_tls_base_symbol(symtab, layout);
2194 if (optimized_type == tls::TLSOPT_NONE)
2196 // Create reserved PLT and GOT entries for the resolver.
2197 target->reserve_tlsdesc_entries(symtab, layout);
2199 // Create a double GOT entry with an R_X86_64_TLSDESC
2200 // reloc. The R_X86_64_TLSDESC reloc is resolved
2201 // lazily, so the GOT entry needs to be in an area in
2202 // .got.plt, not .got. Call got_section to make sure
2203 // the section has been created.
2204 target->got_section(symtab, layout);
2205 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2206 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2207 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2208 elfcpp::R_X86_64_TLSDESC, 0);
2210 else if (optimized_type == tls::TLSOPT_TO_IE)
2212 // Create a GOT entry for the tp-relative offset.
2213 Output_data_got<64, false>* got
2214 = target->got_section(symtab, layout);
2215 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2216 target->rela_dyn_section(layout),
2217 elfcpp::R_X86_64_TPOFF64);
2219 else if (optimized_type != tls::TLSOPT_TO_LE)
2220 unsupported_reloc_global(object, r_type, gsym);
2223 case elfcpp::R_X86_64_TLSDESC_CALL:
2226 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2227 if (optimized_type == tls::TLSOPT_NONE)
2229 // Create a GOT entry for the module index.
2230 target->got_mod_index_entry(symtab, layout, object);
2232 else if (optimized_type != tls::TLSOPT_TO_LE)
2233 unsupported_reloc_global(object, r_type, gsym);
2236 case elfcpp::R_X86_64_DTPOFF32:
2237 case elfcpp::R_X86_64_DTPOFF64:
2240 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2241 layout->set_has_static_tls();
2242 if (optimized_type == tls::TLSOPT_NONE)
2244 // Create a GOT entry for the tp-relative offset.
2245 Output_data_got<64, false>* got
2246 = target->got_section(symtab, layout);
2247 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2248 target->rela_dyn_section(layout),
2249 elfcpp::R_X86_64_TPOFF64);
2251 else if (optimized_type != tls::TLSOPT_TO_LE)
2252 unsupported_reloc_global(object, r_type, gsym);
2255 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2256 layout->set_has_static_tls();
2257 if (parameters->options().shared())
2258 unsupported_reloc_local(object, r_type);
2267 case elfcpp::R_X86_64_SIZE32:
2268 case elfcpp::R_X86_64_SIZE64:
2270 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2271 object->name().c_str(), r_type,
2272 gsym->demangled_name().c_str());
2278 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2280 Sized_relobj<64, false>* object,
2281 unsigned int data_shndx,
2282 unsigned int sh_type,
2283 const unsigned char* prelocs,
2285 Output_section* output_section,
2286 bool needs_special_offset_handling,
2287 size_t local_symbol_count,
2288 const unsigned char* plocal_symbols)
2291 if (sh_type == elfcpp::SHT_REL)
2296 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2297 Target_x86_64::Scan,
2298 Target_x86_64::Relocatable_size_for_reloc>(
2307 needs_special_offset_handling,
2312 // Scan relocations for a section.
2315 Target_x86_64::scan_relocs(Symbol_table* symtab,
2317 Sized_relobj<64, false>* object,
2318 unsigned int data_shndx,
2319 unsigned int sh_type,
2320 const unsigned char* prelocs,
2322 Output_section* output_section,
2323 bool needs_special_offset_handling,
2324 size_t local_symbol_count,
2325 const unsigned char* plocal_symbols)
2327 if (sh_type == elfcpp::SHT_REL)
2329 gold_error(_("%s: unsupported REL reloc section"),
2330 object->name().c_str());
2334 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2335 Target_x86_64::Scan>(
2344 needs_special_offset_handling,
2349 // Finalize the sections.
2352 Target_x86_64::do_finalize_sections(
2354 const Input_objects*,
2355 Symbol_table* symtab)
2357 const Reloc_section* rel_plt = (this->plt_ == NULL
2359 : this->plt_->rela_plt());
2360 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2361 this->rela_dyn_, true, false);
2363 // Fill in some more dynamic tags.
2364 Output_data_dynamic* const odyn = layout->dynamic_data();
2367 if (this->plt_ != NULL
2368 && this->plt_->output_section() != NULL
2369 && this->plt_->has_tlsdesc_entry())
2371 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2372 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2373 this->got_->finalize_data_size();
2374 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2375 this->plt_, plt_offset);
2376 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2377 this->got_, got_offset);
2381 // Emit any relocs we saved in an attempt to avoid generating COPY
2383 if (this->copy_relocs_.any_saved_relocs())
2384 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2386 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2387 // the .got.plt section.
2388 Symbol* sym = this->global_offset_table_;
2391 uint64_t data_size = this->got_plt_->current_data_size();
2392 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2396 // Perform a relocation.
2399 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2400 Target_x86_64* target,
2403 const elfcpp::Rela<64, false>& rela,
2404 unsigned int r_type,
2405 const Sized_symbol<64>* gsym,
2406 const Symbol_value<64>* psymval,
2407 unsigned char* view,
2408 elfcpp::Elf_types<64>::Elf_Addr address,
2409 section_size_type view_size)
2411 if (this->skip_call_tls_get_addr_)
2413 if ((r_type != elfcpp::R_X86_64_PLT32
2414 && r_type != elfcpp::R_X86_64_PC32)
2416 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2418 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2419 _("missing expected TLS relocation"));
2423 this->skip_call_tls_get_addr_ = false;
2428 const Sized_relobj<64, false>* object = relinfo->object;
2430 // Pick the value to use for symbols defined in the PLT.
2431 Symbol_value<64> symval;
2433 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2435 symval.set_output_value(target->plt_section()->address()
2436 + gsym->plt_offset());
2439 else if (gsym == NULL && psymval->is_ifunc_symbol())
2441 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2442 if (object->local_has_plt_offset(r_sym))
2444 symval.set_output_value(target->plt_section()->address()
2445 + object->local_plt_offset(r_sym));
2450 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2452 // Get the GOT offset if needed.
2453 // The GOT pointer points to the end of the GOT section.
2454 // We need to subtract the size of the GOT section to get
2455 // the actual offset to use in the relocation.
2456 bool have_got_offset = false;
2457 unsigned int got_offset = 0;
2460 case elfcpp::R_X86_64_GOT32:
2461 case elfcpp::R_X86_64_GOT64:
2462 case elfcpp::R_X86_64_GOTPLT64:
2463 case elfcpp::R_X86_64_GOTPCREL:
2464 case elfcpp::R_X86_64_GOTPCREL64:
2467 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2468 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2472 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2473 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2474 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2475 - target->got_size());
2477 have_got_offset = true;
2486 case elfcpp::R_X86_64_NONE:
2487 case elfcpp::R_X86_64_GNU_VTINHERIT:
2488 case elfcpp::R_X86_64_GNU_VTENTRY:
2491 case elfcpp::R_X86_64_64:
2492 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2495 case elfcpp::R_X86_64_PC64:
2496 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2500 case elfcpp::R_X86_64_32:
2501 // FIXME: we need to verify that value + addend fits into 32 bits:
2502 // uint64_t x = value + addend;
2503 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2504 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2505 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2508 case elfcpp::R_X86_64_32S:
2509 // FIXME: we need to verify that value + addend fits into 32 bits:
2510 // int64_t x = value + addend; // note this quantity is signed!
2511 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2512 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2515 case elfcpp::R_X86_64_PC32:
2516 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2520 case elfcpp::R_X86_64_16:
2521 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2524 case elfcpp::R_X86_64_PC16:
2525 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2529 case elfcpp::R_X86_64_8:
2530 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2533 case elfcpp::R_X86_64_PC8:
2534 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2538 case elfcpp::R_X86_64_PLT32:
2539 gold_assert(gsym == NULL
2540 || gsym->has_plt_offset()
2541 || gsym->final_value_is_known()
2542 || (gsym->is_defined()
2543 && !gsym->is_from_dynobj()
2544 && !gsym->is_preemptible()));
2545 // Note: while this code looks the same as for R_X86_64_PC32, it
2546 // behaves differently because psymval was set to point to
2547 // the PLT entry, rather than the symbol, in Scan::global().
2548 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2552 case elfcpp::R_X86_64_PLTOFF64:
2555 gold_assert(gsym->has_plt_offset()
2556 || gsym->final_value_is_known());
2557 elfcpp::Elf_types<64>::Elf_Addr got_address;
2558 got_address = target->got_section(NULL, NULL)->address();
2559 Relocate_functions<64, false>::rela64(view, object, psymval,
2560 addend - got_address);
2563 case elfcpp::R_X86_64_GOT32:
2564 gold_assert(have_got_offset);
2565 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2568 case elfcpp::R_X86_64_GOTPC32:
2571 elfcpp::Elf_types<64>::Elf_Addr value;
2572 value = target->got_plt_section()->address();
2573 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2577 case elfcpp::R_X86_64_GOT64:
2578 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2579 // Since we always add a PLT entry, this is equivalent.
2580 case elfcpp::R_X86_64_GOTPLT64:
2581 gold_assert(have_got_offset);
2582 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2585 case elfcpp::R_X86_64_GOTPC64:
2588 elfcpp::Elf_types<64>::Elf_Addr value;
2589 value = target->got_plt_section()->address();
2590 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2594 case elfcpp::R_X86_64_GOTOFF64:
2596 elfcpp::Elf_types<64>::Elf_Addr value;
2597 value = (psymval->value(object, 0)
2598 - target->got_plt_section()->address());
2599 Relocate_functions<64, false>::rela64(view, value, addend);
2603 case elfcpp::R_X86_64_GOTPCREL:
2605 gold_assert(have_got_offset);
2606 elfcpp::Elf_types<64>::Elf_Addr value;
2607 value = target->got_plt_section()->address() + got_offset;
2608 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2612 case elfcpp::R_X86_64_GOTPCREL64:
2614 gold_assert(have_got_offset);
2615 elfcpp::Elf_types<64>::Elf_Addr value;
2616 value = target->got_plt_section()->address() + got_offset;
2617 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2621 case elfcpp::R_X86_64_COPY:
2622 case elfcpp::R_X86_64_GLOB_DAT:
2623 case elfcpp::R_X86_64_JUMP_SLOT:
2624 case elfcpp::R_X86_64_RELATIVE:
2625 case elfcpp::R_X86_64_IRELATIVE:
2626 // These are outstanding tls relocs, which are unexpected when linking
2627 case elfcpp::R_X86_64_TPOFF64:
2628 case elfcpp::R_X86_64_DTPMOD64:
2629 case elfcpp::R_X86_64_TLSDESC:
2630 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2631 _("unexpected reloc %u in object file"),
2635 // These are initial tls relocs, which are expected when linking
2636 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2637 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2638 case elfcpp::R_X86_64_TLSDESC_CALL:
2639 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2640 case elfcpp::R_X86_64_DTPOFF32:
2641 case elfcpp::R_X86_64_DTPOFF64:
2642 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2643 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2644 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2645 view, address, view_size);
2648 case elfcpp::R_X86_64_SIZE32:
2649 case elfcpp::R_X86_64_SIZE64:
2651 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2652 _("unsupported reloc %u"),
2660 // Perform a TLS relocation.
2663 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2664 Target_x86_64* target,
2666 const elfcpp::Rela<64, false>& rela,
2667 unsigned int r_type,
2668 const Sized_symbol<64>* gsym,
2669 const Symbol_value<64>* psymval,
2670 unsigned char* view,
2671 elfcpp::Elf_types<64>::Elf_Addr address,
2672 section_size_type view_size)
2674 Output_segment* tls_segment = relinfo->layout->tls_segment();
2676 const Sized_relobj<64, false>* object = relinfo->object;
2677 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2678 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2679 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2681 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2683 const bool is_final = (gsym == NULL
2684 ? !parameters->options().shared()
2685 : gsym->final_value_is_known());
2686 tls::Tls_optimization optimized_type
2687 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2690 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2691 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2693 // If this code sequence is used in a non-executable section,
2694 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2695 // on the assumption that it's being used by itself in a debug
2696 // section. Therefore, in the unlikely event that the code
2697 // sequence appears in a non-executable section, we simply
2698 // leave it unoptimized.
2699 optimized_type = tls::TLSOPT_NONE;
2701 if (optimized_type == tls::TLSOPT_TO_LE)
2703 gold_assert(tls_segment != NULL);
2704 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2705 rela, r_type, value, view,
2711 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2712 ? GOT_TYPE_TLS_OFFSET
2713 : GOT_TYPE_TLS_PAIR);
2714 unsigned int got_offset;
2717 gold_assert(gsym->has_got_offset(got_type));
2718 got_offset = gsym->got_offset(got_type) - target->got_size();
2722 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2723 gold_assert(object->local_has_got_offset(r_sym, got_type));
2724 got_offset = (object->local_got_offset(r_sym, got_type)
2725 - target->got_size());
2727 if (optimized_type == tls::TLSOPT_TO_IE)
2729 gold_assert(tls_segment != NULL);
2730 value = target->got_plt_section()->address() + got_offset;
2731 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2732 value, view, address, view_size);
2735 else if (optimized_type == tls::TLSOPT_NONE)
2737 // Relocate the field with the offset of the pair of GOT
2739 value = target->got_plt_section()->address() + got_offset;
2740 Relocate_functions<64, false>::pcrela32(view, value, addend,
2745 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2746 _("unsupported reloc %u"), r_type);
2749 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2750 case elfcpp::R_X86_64_TLSDESC_CALL:
2751 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2753 // See above comment for R_X86_64_TLSGD.
2754 optimized_type = tls::TLSOPT_NONE;
2756 if (optimized_type == tls::TLSOPT_TO_LE)
2758 gold_assert(tls_segment != NULL);
2759 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2760 rela, r_type, value, view,
2766 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2767 ? GOT_TYPE_TLS_OFFSET
2768 : GOT_TYPE_TLS_DESC);
2769 unsigned int got_offset = 0;
2770 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2771 && optimized_type == tls::TLSOPT_NONE)
2773 // We created GOT entries in the .got.tlsdesc portion of
2774 // the .got.plt section, but the offset stored in the
2775 // symbol is the offset within .got.tlsdesc.
2776 got_offset = (target->got_size()
2777 + target->got_plt_section()->data_size());
2781 gold_assert(gsym->has_got_offset(got_type));
2782 got_offset += gsym->got_offset(got_type) - target->got_size();
2786 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2787 gold_assert(object->local_has_got_offset(r_sym, got_type));
2788 got_offset += (object->local_got_offset(r_sym, got_type)
2789 - target->got_size());
2791 if (optimized_type == tls::TLSOPT_TO_IE)
2793 gold_assert(tls_segment != NULL);
2794 value = target->got_plt_section()->address() + got_offset;
2795 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2796 rela, r_type, value, view, address,
2800 else if (optimized_type == tls::TLSOPT_NONE)
2802 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2804 // Relocate the field with the offset of the pair of GOT
2806 value = target->got_plt_section()->address() + got_offset;
2807 Relocate_functions<64, false>::pcrela32(view, value, addend,
2813 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2814 _("unsupported reloc %u"), r_type);
2817 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2818 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2820 // See above comment for R_X86_64_TLSGD.
2821 optimized_type = tls::TLSOPT_NONE;
2823 if (optimized_type == tls::TLSOPT_TO_LE)
2825 gold_assert(tls_segment != NULL);
2826 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2827 value, view, view_size);
2830 else if (optimized_type == tls::TLSOPT_NONE)
2832 // Relocate the field with the offset of the GOT entry for
2833 // the module index.
2834 unsigned int got_offset;
2835 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2836 - target->got_size());
2837 value = target->got_plt_section()->address() + got_offset;
2838 Relocate_functions<64, false>::pcrela32(view, value, addend,
2842 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2843 _("unsupported reloc %u"), r_type);
2846 case elfcpp::R_X86_64_DTPOFF32:
2847 // This relocation type is used in debugging information.
2848 // In that case we need to not optimize the value. If the
2849 // section is not executable, then we assume we should not
2850 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2851 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2853 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2855 gold_assert(tls_segment != NULL);
2856 value -= tls_segment->memsz();
2858 Relocate_functions<64, false>::rela32(view, value, addend);
2861 case elfcpp::R_X86_64_DTPOFF64:
2862 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2863 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2865 gold_assert(tls_segment != NULL);
2866 value -= tls_segment->memsz();
2868 Relocate_functions<64, false>::rela64(view, value, addend);
2871 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2872 if (optimized_type == tls::TLSOPT_TO_LE)
2874 gold_assert(tls_segment != NULL);
2875 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2876 rela, r_type, value, view,
2880 else if (optimized_type == tls::TLSOPT_NONE)
2882 // Relocate the field with the offset of the GOT entry for
2883 // the tp-relative offset of the symbol.
2884 unsigned int got_offset;
2887 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2888 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2889 - target->got_size());
2893 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2894 gold_assert(object->local_has_got_offset(r_sym,
2895 GOT_TYPE_TLS_OFFSET));
2896 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2897 - target->got_size());
2899 value = target->got_plt_section()->address() + got_offset;
2900 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2903 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2904 _("unsupported reloc type %u"),
2908 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2909 value -= tls_segment->memsz();
2910 Relocate_functions<64, false>::rela32(view, value, addend);
2915 // Do a relocation in which we convert a TLS General-Dynamic to an
2919 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2922 const elfcpp::Rela<64, false>& rela,
2924 elfcpp::Elf_types<64>::Elf_Addr value,
2925 unsigned char* view,
2926 elfcpp::Elf_types<64>::Elf_Addr address,
2927 section_size_type view_size)
2929 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2930 // .word 0x6666; rex64; call __tls_get_addr
2931 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2933 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2934 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2936 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2937 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2938 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2939 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2941 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2943 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2944 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2946 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2948 this->skip_call_tls_get_addr_ = true;
2951 // Do a relocation in which we convert a TLS General-Dynamic to a
2955 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2957 Output_segment* tls_segment,
2958 const elfcpp::Rela<64, false>& rela,
2960 elfcpp::Elf_types<64>::Elf_Addr value,
2961 unsigned char* view,
2962 section_size_type view_size)
2964 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2965 // .word 0x6666; rex64; call __tls_get_addr
2966 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2968 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2969 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2971 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2972 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2973 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2974 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2976 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2978 value -= tls_segment->memsz();
2979 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2981 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2983 this->skip_call_tls_get_addr_ = true;
2986 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2989 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2990 const Relocate_info<64, false>* relinfo,
2993 const elfcpp::Rela<64, false>& rela,
2994 unsigned int r_type,
2995 elfcpp::Elf_types<64>::Elf_Addr value,
2996 unsigned char* view,
2997 elfcpp::Elf_types<64>::Elf_Addr address,
2998 section_size_type view_size)
3000 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3002 // leaq foo@tlsdesc(%rip), %rax
3003 // ==> movq foo@gottpoff(%rip), %rax
3004 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3005 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3006 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3007 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3009 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3010 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3014 // call *foo@tlscall(%rax)
3016 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3017 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3018 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3019 view[0] == 0xff && view[1] == 0x10);
3025 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3028 Target_x86_64::Relocate::tls_desc_gd_to_le(
3029 const Relocate_info<64, false>* relinfo,
3031 Output_segment* tls_segment,
3032 const elfcpp::Rela<64, false>& rela,
3033 unsigned int r_type,
3034 elfcpp::Elf_types<64>::Elf_Addr value,
3035 unsigned char* view,
3036 section_size_type view_size)
3038 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3040 // leaq foo@tlsdesc(%rip), %rax
3041 // ==> movq foo@tpoff, %rax
3042 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3043 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3044 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3045 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3048 value -= tls_segment->memsz();
3049 Relocate_functions<64, false>::rela32(view, value, 0);
3053 // call *foo@tlscall(%rax)
3055 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3056 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3057 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3058 view[0] == 0xff && view[1] == 0x10);
3065 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3068 const elfcpp::Rela<64, false>& rela,
3070 elfcpp::Elf_types<64>::Elf_Addr,
3071 unsigned char* view,
3072 section_size_type view_size)
3074 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3075 // ... leq foo@dtpoff(%rax),%reg
3076 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3078 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3079 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3081 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3082 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3084 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3086 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3088 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3090 this->skip_call_tls_get_addr_ = true;
3093 // Do a relocation in which we convert a TLS Initial-Exec to a
3097 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3099 Output_segment* tls_segment,
3100 const elfcpp::Rela<64, false>& rela,
3102 elfcpp::Elf_types<64>::Elf_Addr value,
3103 unsigned char* view,
3104 section_size_type view_size)
3106 // We need to examine the opcodes to figure out which instruction we
3109 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3110 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3112 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3113 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3115 unsigned char op1 = view[-3];
3116 unsigned char op2 = view[-2];
3117 unsigned char op3 = view[-1];
3118 unsigned char reg = op3 >> 3;
3126 view[-1] = 0xc0 | reg;
3130 // Special handling for %rsp.
3134 view[-1] = 0xc0 | reg;
3142 view[-1] = 0x80 | reg | (reg << 3);
3145 value -= tls_segment->memsz();
3146 Relocate_functions<64, false>::rela32(view, value, 0);
3149 // Relocate section data.
3152 Target_x86_64::relocate_section(
3153 const Relocate_info<64, false>* relinfo,
3154 unsigned int sh_type,
3155 const unsigned char* prelocs,
3157 Output_section* output_section,
3158 bool needs_special_offset_handling,
3159 unsigned char* view,
3160 elfcpp::Elf_types<64>::Elf_Addr address,
3161 section_size_type view_size,
3162 const Reloc_symbol_changes* reloc_symbol_changes)
3164 gold_assert(sh_type == elfcpp::SHT_RELA);
3166 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3167 Target_x86_64::Relocate>(
3173 needs_special_offset_handling,
3177 reloc_symbol_changes);
3180 // Apply an incremental relocation. Incremental relocations always refer
3181 // to global symbols.
3184 Target_x86_64::apply_relocation(
3185 const Relocate_info<64, false>* relinfo,
3186 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3187 unsigned int r_type,
3188 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3190 unsigned char* view,
3191 elfcpp::Elf_types<64>::Elf_Addr address,
3192 section_size_type view_size)
3194 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3206 // Return the size of a relocation while scanning during a relocatable
3210 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3211 unsigned int r_type,
3216 case elfcpp::R_X86_64_NONE:
3217 case elfcpp::R_X86_64_GNU_VTINHERIT:
3218 case elfcpp::R_X86_64_GNU_VTENTRY:
3219 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3220 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3221 case elfcpp::R_X86_64_TLSDESC_CALL:
3222 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3223 case elfcpp::R_X86_64_DTPOFF32:
3224 case elfcpp::R_X86_64_DTPOFF64:
3225 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3226 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3229 case elfcpp::R_X86_64_64:
3230 case elfcpp::R_X86_64_PC64:
3231 case elfcpp::R_X86_64_GOTOFF64:
3232 case elfcpp::R_X86_64_GOTPC64:
3233 case elfcpp::R_X86_64_PLTOFF64:
3234 case elfcpp::R_X86_64_GOT64:
3235 case elfcpp::R_X86_64_GOTPCREL64:
3236 case elfcpp::R_X86_64_GOTPCREL:
3237 case elfcpp::R_X86_64_GOTPLT64:
3240 case elfcpp::R_X86_64_32:
3241 case elfcpp::R_X86_64_32S:
3242 case elfcpp::R_X86_64_PC32:
3243 case elfcpp::R_X86_64_PLT32:
3244 case elfcpp::R_X86_64_GOTPC32:
3245 case elfcpp::R_X86_64_GOT32:
3248 case elfcpp::R_X86_64_16:
3249 case elfcpp::R_X86_64_PC16:
3252 case elfcpp::R_X86_64_8:
3253 case elfcpp::R_X86_64_PC8:
3256 case elfcpp::R_X86_64_COPY:
3257 case elfcpp::R_X86_64_GLOB_DAT:
3258 case elfcpp::R_X86_64_JUMP_SLOT:
3259 case elfcpp::R_X86_64_RELATIVE:
3260 case elfcpp::R_X86_64_IRELATIVE:
3261 // These are outstanding tls relocs, which are unexpected when linking
3262 case elfcpp::R_X86_64_TPOFF64:
3263 case elfcpp::R_X86_64_DTPMOD64:
3264 case elfcpp::R_X86_64_TLSDESC:
3265 object->error(_("unexpected reloc %u in object file"), r_type);
3268 case elfcpp::R_X86_64_SIZE32:
3269 case elfcpp::R_X86_64_SIZE64:
3271 object->error(_("unsupported reloc %u against local symbol"), r_type);
3276 // Scan the relocs during a relocatable link.
3279 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3281 Sized_relobj<64, false>* object,
3282 unsigned int data_shndx,
3283 unsigned int sh_type,
3284 const unsigned char* prelocs,
3286 Output_section* output_section,
3287 bool needs_special_offset_handling,
3288 size_t local_symbol_count,
3289 const unsigned char* plocal_symbols,
3290 Relocatable_relocs* rr)
3292 gold_assert(sh_type == elfcpp::SHT_RELA);
3294 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3295 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3297 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3298 Scan_relocatable_relocs>(
3306 needs_special_offset_handling,
3312 // Relocate a section during a relocatable link.
3315 Target_x86_64::relocate_for_relocatable(
3316 const Relocate_info<64, false>* relinfo,
3317 unsigned int sh_type,
3318 const unsigned char* prelocs,
3320 Output_section* output_section,
3321 off_t offset_in_output_section,
3322 const Relocatable_relocs* rr,
3323 unsigned char* view,
3324 elfcpp::Elf_types<64>::Elf_Addr view_address,
3325 section_size_type view_size,
3326 unsigned char* reloc_view,
3327 section_size_type reloc_view_size)
3329 gold_assert(sh_type == elfcpp::SHT_RELA);
3331 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3336 offset_in_output_section,
3345 // Return the value to use for a dynamic which requires special
3346 // treatment. This is how we support equality comparisons of function
3347 // pointers across shared library boundaries, as described in the
3348 // processor specific ABI supplement.
3351 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3353 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3354 return this->plt_section()->address() + gsym->plt_offset();
3357 // Return a string used to fill a code section with nops to take up
3358 // the specified length.
3361 Target_x86_64::do_code_fill(section_size_type length) const
3365 // Build a jmpq instruction to skip over the bytes.
3366 unsigned char jmp[5];
3368 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3369 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3370 + std::string(length - 5, '\0'));
3373 // Nop sequences of various lengths.
3374 const char nop1[1] = { 0x90 }; // nop
3375 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3376 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3377 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3378 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3380 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3382 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3384 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3385 0x00, 0x00, 0x00, 0x00 };
3386 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3387 0x00, 0x00, 0x00, 0x00,
3389 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3390 0x84, 0x00, 0x00, 0x00,
3392 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3393 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3395 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3396 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3397 0x00, 0x00, 0x00, 0x00 };
3398 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3399 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3400 0x00, 0x00, 0x00, 0x00,
3402 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3403 0x66, 0x2e, 0x0f, 0x1f, // data16
3404 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3406 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3407 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3408 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3411 const char* nops[16] = {
3413 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3414 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3417 return std::string(nops[length], length);
3420 // Return the addend to use for a target specific relocation. The
3421 // only target specific relocation is R_X86_64_TLSDESC for a local
3422 // symbol. We want to set the addend is the offset of the local
3423 // symbol in the TLS segment.
3426 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3429 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3430 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3431 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3432 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3433 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3434 gold_assert(psymval->is_tls_symbol());
3435 // The value of a TLS symbol is the offset in the TLS segment.
3436 return psymval->value(ti.object, 0);
3439 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3440 // compiled with -fsplit-stack. The function calls non-split-stack
3441 // code. We have to change the function so that it always ensures
3442 // that it has enough stack space to run some random function.
3445 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3446 section_offset_type fnoffset,
3447 section_size_type fnsize,
3448 unsigned char* view,
3449 section_size_type view_size,
3451 std::string* to) const
3453 // The function starts with a comparison of the stack pointer and a
3454 // field in the TCB. This is followed by a jump.
3457 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3460 // We will call __morestack if the carry flag is set after this
3461 // comparison. We turn the comparison into an stc instruction
3463 view[fnoffset] = '\xf9';
3464 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3466 // lea NN(%rsp),%r10
3467 // lea NN(%rsp),%r11
3468 else if ((this->match_view(view, view_size, fnoffset,
3469 "\x4c\x8d\x94\x24", 4)
3470 || this->match_view(view, view_size, fnoffset,
3471 "\x4c\x8d\x9c\x24", 4))
3474 // This is loading an offset from the stack pointer for a
3475 // comparison. The offset is negative, so we decrease the
3476 // offset by the amount of space we need for the stack. This
3477 // means we will avoid calling __morestack if there happens to
3478 // be plenty of space on the stack already.
3479 unsigned char* pval = view + fnoffset + 4;
3480 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3481 val -= parameters->options().split_stack_adjust_size();
3482 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3486 if (!object->has_no_split_stack())
3487 object->error(_("failed to match split-stack sequence at "
3488 "section %u offset %0zx"),
3489 shndx, static_cast<size_t>(fnoffset));
3493 // We have to change the function so that it calls
3494 // __morestack_non_split instead of __morestack. The former will
3495 // allocate additional stack space.
3496 *from = "__morestack";
3497 *to = "__morestack_non_split";
3500 // The selector for x86_64 object files.
3502 class Target_selector_x86_64 : public Target_selector_freebsd
3505 Target_selector_x86_64()
3506 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3507 "elf64-x86-64-freebsd")
3511 do_instantiate_target()
3512 { return new Target_x86_64(); }
3516 Target_selector_x86_64 target_selector_x86_64;
3518 } // End anonymous namespace.