1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 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"
47 class Output_data_plt_x86_64;
49 // The x86_64 target class.
51 // http://www.x86-64.org/documentation/abi.pdf
52 // TLS info comes from
53 // http://people.redhat.com/drepper/tls.pdf
54 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
56 class Target_x86_64 : public Target_freebsd<64, false>
59 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
60 // uses only Elf64_Rela relocation entries with explicit addends."
61 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64 : Target_freebsd<64, false>(&x86_64_info),
65 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
66 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
67 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
70 // Hook for a new output section.
72 do_new_output_section(Output_section*) const;
74 // Scan the relocations to look for symbol adjustments.
76 gc_process_relocs(const General_options& options,
79 Sized_relobj<64, false>* object,
80 unsigned int data_shndx,
82 const unsigned char* prelocs,
84 Output_section* output_section,
85 bool needs_special_offset_handling,
86 size_t local_symbol_count,
87 const unsigned char* plocal_symbols);
89 // Scan the relocations to look for symbol adjustments.
91 scan_relocs(const General_options& options,
94 Sized_relobj<64, false>* object,
95 unsigned int data_shndx,
97 const unsigned char* prelocs,
99 Output_section* output_section,
100 bool needs_special_offset_handling,
101 size_t local_symbol_count,
102 const unsigned char* plocal_symbols);
104 // Finalize the sections.
106 do_finalize_sections(Layout*);
108 // Return the value to use for a dynamic which requires special
111 do_dynsym_value(const Symbol*) const;
113 // Relocate a section.
115 relocate_section(const Relocate_info<64, false>*,
116 unsigned int sh_type,
117 const unsigned char* prelocs,
119 Output_section* output_section,
120 bool needs_special_offset_handling,
122 elfcpp::Elf_types<64>::Elf_Addr view_address,
123 section_size_type view_size,
124 const Reloc_symbol_changes*);
126 // Scan the relocs during a relocatable link.
128 scan_relocatable_relocs(const General_options& options,
129 Symbol_table* symtab,
131 Sized_relobj<64, false>* object,
132 unsigned int data_shndx,
133 unsigned int sh_type,
134 const unsigned char* prelocs,
136 Output_section* output_section,
137 bool needs_special_offset_handling,
138 size_t local_symbol_count,
139 const unsigned char* plocal_symbols,
140 Relocatable_relocs*);
142 // Relocate a section during a relocatable link.
144 relocate_for_relocatable(const Relocate_info<64, false>*,
145 unsigned int sh_type,
146 const unsigned char* prelocs,
148 Output_section* output_section,
149 off_t offset_in_output_section,
150 const Relocatable_relocs*,
152 elfcpp::Elf_types<64>::Elf_Addr view_address,
153 section_size_type view_size,
154 unsigned char* reloc_view,
155 section_size_type reloc_view_size);
157 // Return a string used to fill a code section with nops.
159 do_code_fill(section_size_type length) const;
161 // Return whether SYM is defined by the ABI.
163 do_is_defined_by_abi(const Symbol* sym) const
164 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
166 // Adjust -fstack-split code which calls non-stack-split code.
168 do_calls_non_split(Relobj* object, unsigned int shndx,
169 section_offset_type fnoffset, section_size_type fnsize,
170 unsigned char* view, section_size_type view_size,
171 std::string* from, std::string* to) const;
173 // Return the size of the GOT section.
177 gold_assert(this->got_ != NULL);
178 return this->got_->data_size();
182 // The class which scans relocations.
187 : issued_non_pic_error_(false)
191 local(const General_options& options, Symbol_table* symtab,
192 Layout* layout, Target_x86_64* target,
193 Sized_relobj<64, false>* object,
194 unsigned int data_shndx,
195 Output_section* output_section,
196 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
197 const elfcpp::Sym<64, false>& lsym);
200 global(const General_options& options, Symbol_table* symtab,
201 Layout* layout, Target_x86_64* target,
202 Sized_relobj<64, false>* object,
203 unsigned int data_shndx,
204 Output_section* output_section,
205 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
210 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
213 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
217 check_non_pic(Relobj*, unsigned int r_type);
219 // Whether we have issued an error about a non-PIC compilation.
220 bool issued_non_pic_error_;
223 // The class which implements relocation.
228 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
233 if (this->skip_call_tls_get_addr_)
235 // FIXME: This needs to specify the location somehow.
236 gold_error(_("missing expected TLS relocation"));
240 // Do a relocation. Return false if the caller should not issue
241 // any warnings about this relocation.
243 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
244 size_t relnum, const elfcpp::Rela<64, false>&,
245 unsigned int r_type, const Sized_symbol<64>*,
246 const Symbol_value<64>*,
247 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
251 // Do a TLS relocation.
253 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
254 size_t relnum, const elfcpp::Rela<64, false>&,
255 unsigned int r_type, const Sized_symbol<64>*,
256 const Symbol_value<64>*,
257 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
260 // Do a TLS General-Dynamic to Initial-Exec transition.
262 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
263 Output_segment* tls_segment,
264 const elfcpp::Rela<64, false>&, unsigned int r_type,
265 elfcpp::Elf_types<64>::Elf_Addr value,
267 elfcpp::Elf_types<64>::Elf_Addr,
268 section_size_type view_size);
270 // Do a TLS General-Dynamic to Local-Exec transition.
272 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
273 Output_segment* tls_segment,
274 const elfcpp::Rela<64, false>&, unsigned int r_type,
275 elfcpp::Elf_types<64>::Elf_Addr value,
277 section_size_type view_size);
279 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
281 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
282 Output_segment* tls_segment,
283 const elfcpp::Rela<64, false>&, unsigned int r_type,
284 elfcpp::Elf_types<64>::Elf_Addr value,
286 elfcpp::Elf_types<64>::Elf_Addr,
287 section_size_type view_size);
289 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
291 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
292 Output_segment* tls_segment,
293 const elfcpp::Rela<64, false>&, unsigned int r_type,
294 elfcpp::Elf_types<64>::Elf_Addr value,
296 section_size_type view_size);
298 // Do a TLS Local-Dynamic to Local-Exec transition.
300 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
301 Output_segment* tls_segment,
302 const elfcpp::Rela<64, false>&, unsigned int r_type,
303 elfcpp::Elf_types<64>::Elf_Addr value,
305 section_size_type view_size);
307 // Do a TLS Initial-Exec to Local-Exec transition.
309 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
310 Output_segment* tls_segment,
311 const elfcpp::Rela<64, false>&, unsigned int r_type,
312 elfcpp::Elf_types<64>::Elf_Addr value,
314 section_size_type view_size);
316 // This is set if we should skip the next reloc, which should be a
317 // PLT32 reloc against ___tls_get_addr.
318 bool skip_call_tls_get_addr_;
320 // This is set if we see a relocation which could load the address
321 // of the TLS block. Whether we see such a relocation determines
322 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
323 // in debugging sections.
324 bool saw_tls_block_reloc_;
327 // A class which returns the size required for a relocation type,
328 // used while scanning relocs during a relocatable link.
329 class Relocatable_size_for_reloc
333 get_size_for_reloc(unsigned int, Relobj*);
336 // Adjust TLS relocation type based on the options and whether this
337 // is a local symbol.
338 static tls::Tls_optimization
339 optimize_tls_reloc(bool is_final, int r_type);
341 // Get the GOT section, creating it if necessary.
342 Output_data_got<64, false>*
343 got_section(Symbol_table*, Layout*);
345 // Get the GOT PLT section.
347 got_plt_section() const
349 gold_assert(this->got_plt_ != NULL);
350 return this->got_plt_;
353 // Create the PLT section.
355 make_plt_section(Symbol_table* symtab, Layout* layout);
357 // Create a PLT entry for a global symbol.
359 make_plt_entry(Symbol_table*, Layout*, Symbol*);
361 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
363 define_tls_base_symbol(Symbol_table*, Layout*);
365 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
367 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
369 // Create a GOT entry for the TLS module index.
371 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
372 Sized_relobj<64, false>* object);
374 // Get the PLT section.
375 Output_data_plt_x86_64*
378 gold_assert(this->plt_ != NULL);
382 // Get the dynamic reloc section, creating it if necessary.
384 rela_dyn_section(Layout*);
386 // Add a potential copy relocation.
388 copy_reloc(Symbol_table* symtab, Layout* layout,
389 Sized_relobj<64, false>* object,
390 unsigned int shndx, Output_section* output_section,
391 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
393 this->copy_relocs_.copy_reloc(symtab, layout,
394 symtab->get_sized_symbol<64>(sym),
395 object, shndx, output_section,
396 reloc, this->rela_dyn_section(layout));
399 // Information about this specific target which we pass to the
400 // general Target structure.
401 static const Target::Target_info x86_64_info;
405 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
406 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
407 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
408 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
412 Output_data_got<64, false>* got_;
414 Output_data_plt_x86_64* plt_;
415 // The GOT PLT section.
416 Output_data_space* got_plt_;
417 // The dynamic reloc section.
418 Reloc_section* rela_dyn_;
419 // Relocs saved to avoid a COPY reloc.
420 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
421 // Space for variables copied with a COPY reloc.
422 Output_data_space* dynbss_;
423 // Offset of the GOT entry for the TLS module index.
424 unsigned int got_mod_index_offset_;
425 // True if the _TLS_MODULE_BASE_ symbol has been defined.
426 bool tls_base_symbol_defined_;
429 const Target::Target_info Target_x86_64::x86_64_info =
432 false, // is_big_endian
433 elfcpp::EM_X86_64, // machine_code
434 false, // has_make_symbol
435 false, // has_resolve
436 true, // has_code_fill
437 true, // is_default_stack_executable
439 "/lib/ld64.so.1", // program interpreter
440 0x400000, // default_text_segment_address
441 0x1000, // abi_pagesize (overridable by -z max-page-size)
442 0x1000, // common_pagesize (overridable by -z common-page-size)
443 elfcpp::SHN_UNDEF, // small_common_shndx
444 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
445 0, // small_common_section_flags
446 elfcpp::SHF_X86_64_LARGE // large_common_section_flags
449 // This is called when a new output section is created. This is where
450 // we handle the SHF_X86_64_LARGE.
453 Target_x86_64::do_new_output_section(Output_section *os) const
455 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
456 os->set_is_large_section();
459 // Get the GOT section, creating it if necessary.
461 Output_data_got<64, false>*
462 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
464 if (this->got_ == NULL)
466 gold_assert(symtab != NULL && layout != NULL);
468 this->got_ = new Output_data_got<64, false>();
471 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
473 | elfcpp::SHF_WRITE),
477 // The old GNU linker creates a .got.plt section. We just
478 // create another set of data in the .got section. Note that we
479 // always create a PLT if we create a GOT, although the PLT
481 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
482 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
484 | elfcpp::SHF_WRITE),
488 // The first three entries are reserved.
489 this->got_plt_->set_current_data_size(3 * 8);
491 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
492 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
494 0, 0, elfcpp::STT_OBJECT,
496 elfcpp::STV_HIDDEN, 0,
503 // Get the dynamic reloc section, creating it if necessary.
505 Target_x86_64::Reloc_section*
506 Target_x86_64::rela_dyn_section(Layout* layout)
508 if (this->rela_dyn_ == NULL)
510 gold_assert(layout != NULL);
511 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
512 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
513 elfcpp::SHF_ALLOC, this->rela_dyn_);
515 return this->rela_dyn_;
518 // A class to handle the PLT data.
520 class Output_data_plt_x86_64 : public Output_section_data
523 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
525 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
528 // Add an entry to the PLT.
530 add_entry(Symbol* gsym);
532 // Add the reserved TLSDESC_PLT entry to the PLT.
534 reserve_tlsdesc_entry(unsigned int got_offset)
535 { this->tlsdesc_got_offset_ = got_offset; }
537 // Return true if a TLSDESC_PLT entry has been reserved.
539 has_tlsdesc_entry() const
540 { return this->tlsdesc_got_offset_ != -1U; }
542 // Return the GOT offset for the reserved TLSDESC_PLT entry.
544 get_tlsdesc_got_offset() const
545 { return this->tlsdesc_got_offset_; }
547 // Return the offset of the reserved TLSDESC_PLT entry.
549 get_tlsdesc_plt_offset() const
550 { return (this->count_ + 1) * plt_entry_size; }
552 // Return the .rel.plt section data.
555 { return this->rel_; }
559 do_adjust_output_section(Output_section* os);
561 // Write to a map file.
563 do_print_to_mapfile(Mapfile* mapfile) const
564 { mapfile->print_output_data(this, _("** PLT")); }
567 // The size of an entry in the PLT.
568 static const int plt_entry_size = 16;
570 // The first entry in the PLT.
571 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
572 // procedure linkage table for both programs and shared objects."
573 static unsigned char first_plt_entry[plt_entry_size];
575 // Other entries in the PLT for an executable.
576 static unsigned char plt_entry[plt_entry_size];
578 // The reserved TLSDESC entry in the PLT for an executable.
579 static unsigned char tlsdesc_plt_entry[plt_entry_size];
581 // Set the final size.
583 set_final_data_size();
585 // Write out the PLT data.
587 do_write(Output_file*);
589 // The reloc section.
592 Output_data_got<64, false>* got_;
593 // The .got.plt section.
594 Output_data_space* got_plt_;
595 // The number of PLT entries.
597 // Offset of the reserved TLSDESC_GOT entry when needed.
598 unsigned int tlsdesc_got_offset_;
601 // Create the PLT section. The ordinary .got section is an argument,
602 // since we need to refer to the start. We also create our own .got
603 // section just for PLT entries.
605 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
606 Output_data_got<64, false>* got,
607 Output_data_space* got_plt)
608 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
609 tlsdesc_got_offset_(-1U)
611 this->rel_ = new Reloc_section(false);
612 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
613 elfcpp::SHF_ALLOC, this->rel_);
617 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
619 os->set_entsize(plt_entry_size);
622 // Add an entry to the PLT.
625 Output_data_plt_x86_64::add_entry(Symbol* gsym)
627 gold_assert(!gsym->has_plt_offset());
629 // Note that when setting the PLT offset we skip the initial
630 // reserved PLT entry.
631 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
635 section_offset_type got_offset = this->got_plt_->current_data_size();
637 // Every PLT entry needs a GOT entry which points back to the PLT
638 // entry (this will be changed by the dynamic linker, normally
639 // lazily when the function is called).
640 this->got_plt_->set_current_data_size(got_offset + 8);
642 // Every PLT entry needs a reloc.
643 gsym->set_needs_dynsym_entry();
644 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
647 // Note that we don't need to save the symbol. The contents of the
648 // PLT are independent of which symbols are used. The symbols only
649 // appear in the relocations.
652 // Set the final size.
654 Output_data_plt_x86_64::set_final_data_size()
656 unsigned int count = this->count_;
657 if (this->has_tlsdesc_entry())
659 this->set_data_size((count + 1) * plt_entry_size);
662 // The first entry in the PLT for an executable.
664 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
666 // From AMD64 ABI Draft 0.98, page 76
667 0xff, 0x35, // pushq contents of memory address
668 0, 0, 0, 0, // replaced with address of .got + 8
669 0xff, 0x25, // jmp indirect
670 0, 0, 0, 0, // replaced with address of .got + 16
671 0x90, 0x90, 0x90, 0x90 // noop (x4)
674 // Subsequent entries in the PLT for an executable.
676 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
678 // From AMD64 ABI Draft 0.98, page 76
679 0xff, 0x25, // jmpq indirect
680 0, 0, 0, 0, // replaced with address of symbol in .got
681 0x68, // pushq immediate
682 0, 0, 0, 0, // replaced with offset into relocation table
683 0xe9, // jmpq relative
684 0, 0, 0, 0 // replaced with offset to start of .plt
687 // The reserved TLSDESC entry in the PLT for an executable.
689 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
691 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
692 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
693 0xff, 0x35, // pushq x(%rip)
694 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
695 0xff, 0x25, // jmpq *y(%rip)
696 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
701 // Write out the PLT. This uses the hand-coded instructions above,
702 // and adjusts them as needed. This is specified by the AMD64 ABI.
705 Output_data_plt_x86_64::do_write(Output_file* of)
707 const off_t offset = this->offset();
708 const section_size_type oview_size =
709 convert_to_section_size_type(this->data_size());
710 unsigned char* const oview = of->get_output_view(offset, oview_size);
712 const off_t got_file_offset = this->got_plt_->offset();
713 const section_size_type got_size =
714 convert_to_section_size_type(this->got_plt_->data_size());
715 unsigned char* const got_view = of->get_output_view(got_file_offset,
718 unsigned char* pov = oview;
720 // The base address of the .plt section.
721 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
722 // The base address of the .got section.
723 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
724 // The base address of the PLT portion of the .got section,
725 // which is where the GOT pointer will point, and where the
726 // three reserved GOT entries are located.
727 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
729 memcpy(pov, first_plt_entry, plt_entry_size);
730 // We do a jmp relative to the PC at the end of this instruction.
731 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
733 - (plt_address + 6)));
734 elfcpp::Swap<32, false>::writeval(pov + 8,
736 - (plt_address + 12)));
737 pov += plt_entry_size;
739 unsigned char* got_pov = got_view;
741 memset(got_pov, 0, 24);
744 unsigned int plt_offset = plt_entry_size;
745 unsigned int got_offset = 24;
746 const unsigned int count = this->count_;
747 for (unsigned int plt_index = 0;
750 pov += plt_entry_size,
752 plt_offset += plt_entry_size,
755 // Set and adjust the PLT entry itself.
756 memcpy(pov, plt_entry, plt_entry_size);
757 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
758 (got_address + got_offset
759 - (plt_address + plt_offset
762 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
763 elfcpp::Swap<32, false>::writeval(pov + 12,
764 - (plt_offset + plt_entry_size));
766 // Set the entry in the GOT.
767 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
770 if (this->has_tlsdesc_entry())
772 // Set and adjust the reserved TLSDESC PLT entry.
773 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
774 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
775 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
777 - (plt_address + plt_offset
779 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
782 - (plt_address + plt_offset
784 pov += plt_entry_size;
787 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
788 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
790 of->write_output_view(offset, oview_size, oview);
791 of->write_output_view(got_file_offset, got_size, got_view);
794 // Create the PLT section.
797 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
799 if (this->plt_ == NULL)
801 // Create the GOT sections first.
802 this->got_section(symtab, layout);
804 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
806 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
808 | elfcpp::SHF_EXECINSTR),
813 // Create a PLT entry for a global symbol.
816 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
819 if (gsym->has_plt_offset())
822 if (this->plt_ == NULL)
823 this->make_plt_section(symtab, layout);
825 this->plt_->add_entry(gsym);
828 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
831 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
833 if (this->tls_base_symbol_defined_)
836 Output_segment* tls_segment = layout->tls_segment();
837 if (tls_segment != NULL)
839 bool is_exec = parameters->options().output_is_executable();
840 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
844 elfcpp::STV_HIDDEN, 0,
846 ? Symbol::SEGMENT_END
847 : Symbol::SEGMENT_START),
850 this->tls_base_symbol_defined_ = true;
853 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
856 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
859 if (this->plt_ == NULL)
860 this->make_plt_section(symtab, layout);
862 if (!this->plt_->has_tlsdesc_entry())
864 // Allocate the TLSDESC_GOT entry.
865 Output_data_got<64, false>* got = this->got_section(symtab, layout);
866 unsigned int got_offset = got->add_constant(0);
868 // Allocate the TLSDESC_PLT entry.
869 this->plt_->reserve_tlsdesc_entry(got_offset);
873 // Create a GOT entry for the TLS module index.
876 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
877 Sized_relobj<64, false>* object)
879 if (this->got_mod_index_offset_ == -1U)
881 gold_assert(symtab != NULL && layout != NULL && object != NULL);
882 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
883 Output_data_got<64, false>* got = this->got_section(symtab, layout);
884 unsigned int got_offset = got->add_constant(0);
885 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
887 got->add_constant(0);
888 this->got_mod_index_offset_ = got_offset;
890 return this->got_mod_index_offset_;
893 // Optimize the TLS relocation type based on what we know about the
894 // symbol. IS_FINAL is true if the final address of this symbol is
895 // known at link time.
897 tls::Tls_optimization
898 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
900 // If we are generating a shared library, then we can't do anything
902 if (parameters->options().shared())
903 return tls::TLSOPT_NONE;
907 case elfcpp::R_X86_64_TLSGD:
908 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
909 case elfcpp::R_X86_64_TLSDESC_CALL:
910 // These are General-Dynamic which permits fully general TLS
911 // access. Since we know that we are generating an executable,
912 // we can convert this to Initial-Exec. If we also know that
913 // this is a local symbol, we can further switch to Local-Exec.
915 return tls::TLSOPT_TO_LE;
916 return tls::TLSOPT_TO_IE;
918 case elfcpp::R_X86_64_TLSLD:
919 // This is Local-Dynamic, which refers to a local symbol in the
920 // dynamic TLS block. Since we know that we generating an
921 // executable, we can switch to Local-Exec.
922 return tls::TLSOPT_TO_LE;
924 case elfcpp::R_X86_64_DTPOFF32:
925 case elfcpp::R_X86_64_DTPOFF64:
926 // Another Local-Dynamic reloc.
927 return tls::TLSOPT_TO_LE;
929 case elfcpp::R_X86_64_GOTTPOFF:
930 // These are Initial-Exec relocs which get the thread offset
931 // from the GOT. If we know that we are linking against the
932 // local symbol, we can switch to Local-Exec, which links the
933 // thread offset into the instruction.
935 return tls::TLSOPT_TO_LE;
936 return tls::TLSOPT_NONE;
938 case elfcpp::R_X86_64_TPOFF32:
939 // When we already have Local-Exec, there is nothing further we
941 return tls::TLSOPT_NONE;
948 // Report an unsupported relocation against a local symbol.
951 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
954 gold_error(_("%s: unsupported reloc %u against local symbol"),
955 object->name().c_str(), r_type);
958 // We are about to emit a dynamic relocation of type R_TYPE. If the
959 // dynamic linker does not support it, issue an error. The GNU linker
960 // only issues a non-PIC error for an allocated read-only section.
961 // Here we know the section is allocated, but we don't know that it is
962 // read-only. But we check for all the relocation types which the
963 // glibc dynamic linker supports, so it seems appropriate to issue an
964 // error even if the section is not read-only.
967 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
971 // These are the relocation types supported by glibc for x86_64.
972 case elfcpp::R_X86_64_RELATIVE:
973 case elfcpp::R_X86_64_GLOB_DAT:
974 case elfcpp::R_X86_64_JUMP_SLOT:
975 case elfcpp::R_X86_64_DTPMOD64:
976 case elfcpp::R_X86_64_DTPOFF64:
977 case elfcpp::R_X86_64_TPOFF64:
978 case elfcpp::R_X86_64_64:
979 case elfcpp::R_X86_64_32:
980 case elfcpp::R_X86_64_PC32:
981 case elfcpp::R_X86_64_COPY:
985 // This prevents us from issuing more than one error per reloc
986 // section. But we can still wind up issuing more than one
987 // error per object file.
988 if (this->issued_non_pic_error_)
990 gold_assert(parameters->options().output_is_position_independent());
991 object->error(_("requires unsupported dynamic reloc; "
992 "recompile with -fPIC"));
993 this->issued_non_pic_error_ = true;
996 case elfcpp::R_X86_64_NONE:
1001 // Scan a relocation for a local symbol.
1004 Target_x86_64::Scan::local(const General_options&,
1005 Symbol_table* symtab,
1007 Target_x86_64* target,
1008 Sized_relobj<64, false>* object,
1009 unsigned int data_shndx,
1010 Output_section* output_section,
1011 const elfcpp::Rela<64, false>& reloc,
1012 unsigned int r_type,
1013 const elfcpp::Sym<64, false>& lsym)
1017 case elfcpp::R_X86_64_NONE:
1018 case elfcpp::R_386_GNU_VTINHERIT:
1019 case elfcpp::R_386_GNU_VTENTRY:
1022 case elfcpp::R_X86_64_64:
1023 // If building a shared library (or a position-independent
1024 // executable), we need to create a dynamic relocation for this
1025 // location. The relocation applied at link time will apply the
1026 // link-time value, so we flag the location with an
1027 // R_X86_64_RELATIVE relocation so the dynamic loader can
1028 // relocate it easily.
1029 if (parameters->options().output_is_position_independent())
1031 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1032 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1033 rela_dyn->add_local_relative(object, r_sym,
1034 elfcpp::R_X86_64_RELATIVE,
1035 output_section, data_shndx,
1036 reloc.get_r_offset(),
1037 reloc.get_r_addend());
1041 case elfcpp::R_X86_64_32:
1042 case elfcpp::R_X86_64_32S:
1043 case elfcpp::R_X86_64_16:
1044 case elfcpp::R_X86_64_8:
1045 // If building a shared library (or a position-independent
1046 // executable), we need to create a dynamic relocation for this
1047 // location. We can't use an R_X86_64_RELATIVE relocation
1048 // because that is always a 64-bit relocation.
1049 if (parameters->options().output_is_position_independent())
1051 this->check_non_pic(object, r_type);
1053 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1054 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1055 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1056 rela_dyn->add_local(object, r_sym, r_type, output_section,
1057 data_shndx, reloc.get_r_offset(),
1058 reloc.get_r_addend());
1061 gold_assert(lsym.get_st_value() == 0);
1062 unsigned int shndx = lsym.get_st_shndx();
1064 shndx = object->adjust_sym_shndx(r_sym, shndx,
1067 object->error(_("section symbol %u has bad shndx %u"),
1070 rela_dyn->add_local_section(object, shndx,
1071 r_type, output_section,
1072 data_shndx, reloc.get_r_offset(),
1073 reloc.get_r_addend());
1078 case elfcpp::R_X86_64_PC64:
1079 case elfcpp::R_X86_64_PC32:
1080 case elfcpp::R_X86_64_PC16:
1081 case elfcpp::R_X86_64_PC8:
1084 case elfcpp::R_X86_64_PLT32:
1085 // Since we know this is a local symbol, we can handle this as a
1089 case elfcpp::R_X86_64_GOTPC32:
1090 case elfcpp::R_X86_64_GOTOFF64:
1091 case elfcpp::R_X86_64_GOTPC64:
1092 case elfcpp::R_X86_64_PLTOFF64:
1093 // We need a GOT section.
1094 target->got_section(symtab, layout);
1095 // For PLTOFF64, we'd normally want a PLT section, but since we
1096 // know this is a local symbol, no PLT is needed.
1099 case elfcpp::R_X86_64_GOT64:
1100 case elfcpp::R_X86_64_GOT32:
1101 case elfcpp::R_X86_64_GOTPCREL64:
1102 case elfcpp::R_X86_64_GOTPCREL:
1103 case elfcpp::R_X86_64_GOTPLT64:
1105 // The symbol requires a GOT entry.
1106 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1107 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1108 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1110 // If we are generating a shared object, we need to add a
1111 // dynamic relocation for this symbol's GOT entry.
1112 if (parameters->options().output_is_position_independent())
1114 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1115 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1116 if (r_type != elfcpp::R_X86_64_GOT32)
1117 rela_dyn->add_local_relative(
1118 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1119 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1122 this->check_non_pic(object, r_type);
1124 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1125 rela_dyn->add_local(
1126 object, r_sym, r_type, got,
1127 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1131 // For GOTPLT64, we'd normally want a PLT section, but since
1132 // we know this is a local symbol, no PLT is needed.
1136 case elfcpp::R_X86_64_COPY:
1137 case elfcpp::R_X86_64_GLOB_DAT:
1138 case elfcpp::R_X86_64_JUMP_SLOT:
1139 case elfcpp::R_X86_64_RELATIVE:
1140 // These are outstanding tls relocs, which are unexpected when linking
1141 case elfcpp::R_X86_64_TPOFF64:
1142 case elfcpp::R_X86_64_DTPMOD64:
1143 case elfcpp::R_X86_64_TLSDESC:
1144 gold_error(_("%s: unexpected reloc %u in object file"),
1145 object->name().c_str(), r_type);
1148 // These are initial tls relocs, which are expected when linking
1149 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1150 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1151 case elfcpp::R_X86_64_TLSDESC_CALL:
1152 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1153 case elfcpp::R_X86_64_DTPOFF32:
1154 case elfcpp::R_X86_64_DTPOFF64:
1155 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1156 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1158 bool output_is_shared = parameters->options().shared();
1159 const tls::Tls_optimization optimized_type
1160 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1163 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1164 if (optimized_type == tls::TLSOPT_NONE)
1166 // Create a pair of GOT entries for the module index and
1167 // dtv-relative offset.
1168 Output_data_got<64, false>* got
1169 = target->got_section(symtab, layout);
1170 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1171 unsigned int shndx = lsym.get_st_shndx();
1173 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1175 object->error(_("local symbol %u has bad shndx %u"),
1178 got->add_local_pair_with_rela(object, r_sym,
1181 target->rela_dyn_section(layout),
1182 elfcpp::R_X86_64_DTPMOD64, 0);
1184 else if (optimized_type != tls::TLSOPT_TO_LE)
1185 unsupported_reloc_local(object, r_type);
1188 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1189 target->define_tls_base_symbol(symtab, layout);
1190 if (optimized_type == tls::TLSOPT_NONE)
1192 // Create reserved PLT and GOT entries for the resolver.
1193 target->reserve_tlsdesc_entries(symtab, layout);
1195 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1196 Output_data_got<64, false>* got
1197 = target->got_section(symtab, layout);
1198 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1199 unsigned int shndx = lsym.get_st_shndx();
1201 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1203 object->error(_("local symbol %u has bad shndx %u"),
1206 got->add_local_pair_with_rela(object, r_sym,
1209 target->rela_dyn_section(layout),
1210 elfcpp::R_X86_64_TLSDESC, 0);
1212 else if (optimized_type != tls::TLSOPT_TO_LE)
1213 unsupported_reloc_local(object, r_type);
1216 case elfcpp::R_X86_64_TLSDESC_CALL:
1219 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1220 if (optimized_type == tls::TLSOPT_NONE)
1222 // Create a GOT entry for the module index.
1223 target->got_mod_index_entry(symtab, layout, object);
1225 else if (optimized_type != tls::TLSOPT_TO_LE)
1226 unsupported_reloc_local(object, r_type);
1229 case elfcpp::R_X86_64_DTPOFF32:
1230 case elfcpp::R_X86_64_DTPOFF64:
1233 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1234 layout->set_has_static_tls();
1235 if (optimized_type == tls::TLSOPT_NONE)
1237 // Create a GOT entry for the tp-relative offset.
1238 Output_data_got<64, false>* got
1239 = target->got_section(symtab, layout);
1240 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1241 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1242 target->rela_dyn_section(layout),
1243 elfcpp::R_X86_64_TPOFF64);
1245 else if (optimized_type != tls::TLSOPT_TO_LE)
1246 unsupported_reloc_local(object, r_type);
1249 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1250 layout->set_has_static_tls();
1251 if (output_is_shared)
1252 unsupported_reloc_local(object, r_type);
1261 case elfcpp::R_X86_64_SIZE32:
1262 case elfcpp::R_X86_64_SIZE64:
1264 gold_error(_("%s: unsupported reloc %u against local symbol"),
1265 object->name().c_str(), r_type);
1271 // Report an unsupported relocation against a global symbol.
1274 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1275 unsigned int r_type,
1278 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1279 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1282 // Scan a relocation for a global symbol.
1285 Target_x86_64::Scan::global(const General_options&,
1286 Symbol_table* symtab,
1288 Target_x86_64* target,
1289 Sized_relobj<64, false>* object,
1290 unsigned int data_shndx,
1291 Output_section* output_section,
1292 const elfcpp::Rela<64, false>& reloc,
1293 unsigned int r_type,
1298 case elfcpp::R_X86_64_NONE:
1299 case elfcpp::R_386_GNU_VTINHERIT:
1300 case elfcpp::R_386_GNU_VTENTRY:
1303 case elfcpp::R_X86_64_64:
1304 case elfcpp::R_X86_64_32:
1305 case elfcpp::R_X86_64_32S:
1306 case elfcpp::R_X86_64_16:
1307 case elfcpp::R_X86_64_8:
1309 // Make a PLT entry if necessary.
1310 if (gsym->needs_plt_entry())
1312 target->make_plt_entry(symtab, layout, gsym);
1313 // Since this is not a PC-relative relocation, we may be
1314 // taking the address of a function. In that case we need to
1315 // set the entry in the dynamic symbol table to the address of
1317 if (gsym->is_from_dynobj() && !parameters->options().shared())
1318 gsym->set_needs_dynsym_value();
1320 // Make a dynamic relocation if necessary.
1321 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1323 if (gsym->may_need_copy_reloc())
1325 target->copy_reloc(symtab, layout, object,
1326 data_shndx, output_section, gsym, reloc);
1328 else if (r_type == elfcpp::R_X86_64_64
1329 && gsym->can_use_relative_reloc(false))
1331 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1332 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1333 output_section, object,
1334 data_shndx, reloc.get_r_offset(),
1335 reloc.get_r_addend());
1339 this->check_non_pic(object, r_type);
1340 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1341 rela_dyn->add_global(gsym, r_type, output_section, object,
1342 data_shndx, reloc.get_r_offset(),
1343 reloc.get_r_addend());
1349 case elfcpp::R_X86_64_PC64:
1350 case elfcpp::R_X86_64_PC32:
1351 case elfcpp::R_X86_64_PC16:
1352 case elfcpp::R_X86_64_PC8:
1354 // Make a PLT entry if necessary.
1355 if (gsym->needs_plt_entry())
1356 target->make_plt_entry(symtab, layout, gsym);
1357 // Make a dynamic relocation if necessary.
1358 int flags = Symbol::NON_PIC_REF;
1359 if (gsym->type() == elfcpp::STT_FUNC)
1360 flags |= Symbol::FUNCTION_CALL;
1361 if (gsym->needs_dynamic_reloc(flags))
1363 if (gsym->may_need_copy_reloc())
1365 target->copy_reloc(symtab, layout, object,
1366 data_shndx, output_section, gsym, reloc);
1370 this->check_non_pic(object, r_type);
1371 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1372 rela_dyn->add_global(gsym, r_type, output_section, object,
1373 data_shndx, reloc.get_r_offset(),
1374 reloc.get_r_addend());
1380 case elfcpp::R_X86_64_GOT64:
1381 case elfcpp::R_X86_64_GOT32:
1382 case elfcpp::R_X86_64_GOTPCREL64:
1383 case elfcpp::R_X86_64_GOTPCREL:
1384 case elfcpp::R_X86_64_GOTPLT64:
1386 // The symbol requires a GOT entry.
1387 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1388 if (gsym->final_value_is_known())
1389 got->add_global(gsym, GOT_TYPE_STANDARD);
1392 // If this symbol is not fully resolved, we need to add a
1393 // dynamic relocation for it.
1394 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1395 if (gsym->is_from_dynobj()
1396 || gsym->is_undefined()
1397 || gsym->is_preemptible())
1398 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1399 elfcpp::R_X86_64_GLOB_DAT);
1402 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1403 rela_dyn->add_global_relative(
1404 gsym, elfcpp::R_X86_64_RELATIVE, got,
1405 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1408 // For GOTPLT64, we also need a PLT entry (but only if the
1409 // symbol is not fully resolved).
1410 if (r_type == elfcpp::R_X86_64_GOTPLT64
1411 && !gsym->final_value_is_known())
1412 target->make_plt_entry(symtab, layout, gsym);
1416 case elfcpp::R_X86_64_PLT32:
1417 // If the symbol is fully resolved, this is just a PC32 reloc.
1418 // Otherwise we need a PLT entry.
1419 if (gsym->final_value_is_known())
1421 // If building a shared library, we can also skip the PLT entry
1422 // if the symbol is defined in the output file and is protected
1424 if (gsym->is_defined()
1425 && !gsym->is_from_dynobj()
1426 && !gsym->is_preemptible())
1428 target->make_plt_entry(symtab, layout, gsym);
1431 case elfcpp::R_X86_64_GOTPC32:
1432 case elfcpp::R_X86_64_GOTOFF64:
1433 case elfcpp::R_X86_64_GOTPC64:
1434 case elfcpp::R_X86_64_PLTOFF64:
1435 // We need a GOT section.
1436 target->got_section(symtab, layout);
1437 // For PLTOFF64, we also need a PLT entry (but only if the
1438 // symbol is not fully resolved).
1439 if (r_type == elfcpp::R_X86_64_PLTOFF64
1440 && !gsym->final_value_is_known())
1441 target->make_plt_entry(symtab, layout, gsym);
1444 case elfcpp::R_X86_64_COPY:
1445 case elfcpp::R_X86_64_GLOB_DAT:
1446 case elfcpp::R_X86_64_JUMP_SLOT:
1447 case elfcpp::R_X86_64_RELATIVE:
1448 // These are outstanding tls relocs, which are unexpected when linking
1449 case elfcpp::R_X86_64_TPOFF64:
1450 case elfcpp::R_X86_64_DTPMOD64:
1451 case elfcpp::R_X86_64_TLSDESC:
1452 gold_error(_("%s: unexpected reloc %u in object file"),
1453 object->name().c_str(), r_type);
1456 // These are initial tls relocs, which are expected for global()
1457 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1458 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1459 case elfcpp::R_X86_64_TLSDESC_CALL:
1460 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1461 case elfcpp::R_X86_64_DTPOFF32:
1462 case elfcpp::R_X86_64_DTPOFF64:
1463 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1464 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1466 const bool is_final = gsym->final_value_is_known();
1467 const tls::Tls_optimization optimized_type
1468 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1471 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1472 if (optimized_type == tls::TLSOPT_NONE)
1474 // Create a pair of GOT entries for the module index and
1475 // dtv-relative offset.
1476 Output_data_got<64, false>* got
1477 = target->got_section(symtab, layout);
1478 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1479 target->rela_dyn_section(layout),
1480 elfcpp::R_X86_64_DTPMOD64,
1481 elfcpp::R_X86_64_DTPOFF64);
1483 else if (optimized_type == tls::TLSOPT_TO_IE)
1485 // Create a GOT entry for the tp-relative offset.
1486 Output_data_got<64, false>* got
1487 = target->got_section(symtab, layout);
1488 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1489 target->rela_dyn_section(layout),
1490 elfcpp::R_X86_64_TPOFF64);
1492 else if (optimized_type != tls::TLSOPT_TO_LE)
1493 unsupported_reloc_global(object, r_type, gsym);
1496 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1497 target->define_tls_base_symbol(symtab, layout);
1498 if (optimized_type == tls::TLSOPT_NONE)
1500 // Create reserved PLT and GOT entries for the resolver.
1501 target->reserve_tlsdesc_entries(symtab, layout);
1503 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1504 Output_data_got<64, false>* got
1505 = target->got_section(symtab, layout);
1506 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1507 target->rela_dyn_section(layout),
1508 elfcpp::R_X86_64_TLSDESC, 0);
1510 else if (optimized_type == tls::TLSOPT_TO_IE)
1512 // Create a GOT entry for the tp-relative offset.
1513 Output_data_got<64, false>* got
1514 = target->got_section(symtab, layout);
1515 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1516 target->rela_dyn_section(layout),
1517 elfcpp::R_X86_64_TPOFF64);
1519 else if (optimized_type != tls::TLSOPT_TO_LE)
1520 unsupported_reloc_global(object, r_type, gsym);
1523 case elfcpp::R_X86_64_TLSDESC_CALL:
1526 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1527 if (optimized_type == tls::TLSOPT_NONE)
1529 // Create a GOT entry for the module index.
1530 target->got_mod_index_entry(symtab, layout, object);
1532 else if (optimized_type != tls::TLSOPT_TO_LE)
1533 unsupported_reloc_global(object, r_type, gsym);
1536 case elfcpp::R_X86_64_DTPOFF32:
1537 case elfcpp::R_X86_64_DTPOFF64:
1540 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1541 layout->set_has_static_tls();
1542 if (optimized_type == tls::TLSOPT_NONE)
1544 // Create a GOT entry for the tp-relative offset.
1545 Output_data_got<64, false>* got
1546 = target->got_section(symtab, layout);
1547 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1548 target->rela_dyn_section(layout),
1549 elfcpp::R_X86_64_TPOFF64);
1551 else if (optimized_type != tls::TLSOPT_TO_LE)
1552 unsupported_reloc_global(object, r_type, gsym);
1555 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1556 layout->set_has_static_tls();
1557 if (parameters->options().shared())
1558 unsupported_reloc_local(object, r_type);
1567 case elfcpp::R_X86_64_SIZE32:
1568 case elfcpp::R_X86_64_SIZE64:
1570 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1571 object->name().c_str(), r_type,
1572 gsym->demangled_name().c_str());
1578 Target_x86_64::gc_process_relocs(const General_options& options,
1579 Symbol_table* symtab,
1581 Sized_relobj<64, false>* object,
1582 unsigned int data_shndx,
1583 unsigned int sh_type,
1584 const unsigned char* prelocs,
1586 Output_section* output_section,
1587 bool needs_special_offset_handling,
1588 size_t local_symbol_count,
1589 const unsigned char* plocal_symbols)
1592 if (sh_type == elfcpp::SHT_REL)
1597 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1598 Target_x86_64::Scan>(
1608 needs_special_offset_handling,
1613 // Scan relocations for a section.
1616 Target_x86_64::scan_relocs(const General_options& options,
1617 Symbol_table* symtab,
1619 Sized_relobj<64, false>* object,
1620 unsigned int data_shndx,
1621 unsigned int sh_type,
1622 const unsigned char* prelocs,
1624 Output_section* output_section,
1625 bool needs_special_offset_handling,
1626 size_t local_symbol_count,
1627 const unsigned char* plocal_symbols)
1629 if (sh_type == elfcpp::SHT_REL)
1631 gold_error(_("%s: unsupported REL reloc section"),
1632 object->name().c_str());
1636 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1637 Target_x86_64::Scan>(
1647 needs_special_offset_handling,
1652 // Finalize the sections.
1655 Target_x86_64::do_finalize_sections(Layout* layout)
1657 // Fill in some more dynamic tags.
1658 Output_data_dynamic* const odyn = layout->dynamic_data();
1661 if (this->got_plt_ != NULL)
1662 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1664 if (this->plt_ != NULL)
1666 const Output_data* od = this->plt_->rel_plt();
1667 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1668 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1669 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1670 if (this->plt_->has_tlsdesc_entry())
1672 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1673 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1674 this->got_->finalize_data_size();
1675 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1676 this->plt_, plt_offset);
1677 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1678 this->got_, got_offset);
1682 if (this->rela_dyn_ != NULL)
1684 const Output_data* od = this->rela_dyn_;
1685 odyn->add_section_address(elfcpp::DT_RELA, od);
1686 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1687 odyn->add_constant(elfcpp::DT_RELAENT,
1688 elfcpp::Elf_sizes<64>::rela_size);
1691 if (!parameters->options().shared())
1693 // The value of the DT_DEBUG tag is filled in by the dynamic
1694 // linker at run time, and used by the debugger.
1695 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1699 // Emit any relocs we saved in an attempt to avoid generating COPY
1701 if (this->copy_relocs_.any_saved_relocs())
1702 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1705 // Perform a relocation.
1708 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1709 Target_x86_64* target,
1712 const elfcpp::Rela<64, false>& rela,
1713 unsigned int r_type,
1714 const Sized_symbol<64>* gsym,
1715 const Symbol_value<64>* psymval,
1716 unsigned char* view,
1717 elfcpp::Elf_types<64>::Elf_Addr address,
1718 section_size_type view_size)
1720 if (this->skip_call_tls_get_addr_)
1722 if ((r_type != elfcpp::R_X86_64_PLT32
1723 && r_type != elfcpp::R_X86_64_PC32)
1725 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1727 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1728 _("missing expected TLS relocation"));
1732 this->skip_call_tls_get_addr_ = false;
1737 // Pick the value to use for symbols defined in shared objects.
1738 Symbol_value<64> symval;
1740 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1741 || r_type == elfcpp::R_X86_64_PC32
1742 || r_type == elfcpp::R_X86_64_PC16
1743 || r_type == elfcpp::R_X86_64_PC8))
1745 symval.set_output_value(target->plt_section()->address()
1746 + gsym->plt_offset());
1750 const Sized_relobj<64, false>* object = relinfo->object;
1751 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1753 // Get the GOT offset if needed.
1754 // The GOT pointer points to the end of the GOT section.
1755 // We need to subtract the size of the GOT section to get
1756 // the actual offset to use in the relocation.
1757 bool have_got_offset = false;
1758 unsigned int got_offset = 0;
1761 case elfcpp::R_X86_64_GOT32:
1762 case elfcpp::R_X86_64_GOT64:
1763 case elfcpp::R_X86_64_GOTPLT64:
1764 case elfcpp::R_X86_64_GOTPCREL:
1765 case elfcpp::R_X86_64_GOTPCREL64:
1768 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1769 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1773 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1774 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1775 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1776 - target->got_size());
1778 have_got_offset = true;
1787 case elfcpp::R_X86_64_NONE:
1788 case elfcpp::R_386_GNU_VTINHERIT:
1789 case elfcpp::R_386_GNU_VTENTRY:
1792 case elfcpp::R_X86_64_64:
1793 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1796 case elfcpp::R_X86_64_PC64:
1797 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1801 case elfcpp::R_X86_64_32:
1802 // FIXME: we need to verify that value + addend fits into 32 bits:
1803 // uint64_t x = value + addend;
1804 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1805 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1806 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1809 case elfcpp::R_X86_64_32S:
1810 // FIXME: we need to verify that value + addend fits into 32 bits:
1811 // int64_t x = value + addend; // note this quantity is signed!
1812 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1813 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1816 case elfcpp::R_X86_64_PC32:
1817 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1821 case elfcpp::R_X86_64_16:
1822 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1825 case elfcpp::R_X86_64_PC16:
1826 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1830 case elfcpp::R_X86_64_8:
1831 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1834 case elfcpp::R_X86_64_PC8:
1835 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1839 case elfcpp::R_X86_64_PLT32:
1840 gold_assert(gsym == NULL
1841 || gsym->has_plt_offset()
1842 || gsym->final_value_is_known()
1843 || (gsym->is_defined()
1844 && !gsym->is_from_dynobj()
1845 && !gsym->is_preemptible()));
1846 // Note: while this code looks the same as for R_X86_64_PC32, it
1847 // behaves differently because psymval was set to point to
1848 // the PLT entry, rather than the symbol, in Scan::global().
1849 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1853 case elfcpp::R_X86_64_PLTOFF64:
1856 gold_assert(gsym->has_plt_offset()
1857 || gsym->final_value_is_known());
1858 elfcpp::Elf_types<64>::Elf_Addr got_address;
1859 got_address = target->got_section(NULL, NULL)->address();
1860 Relocate_functions<64, false>::rela64(view, object, psymval,
1861 addend - got_address);
1864 case elfcpp::R_X86_64_GOT32:
1865 gold_assert(have_got_offset);
1866 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1869 case elfcpp::R_X86_64_GOTPC32:
1872 elfcpp::Elf_types<64>::Elf_Addr value;
1873 value = target->got_plt_section()->address();
1874 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1878 case elfcpp::R_X86_64_GOT64:
1879 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1880 // Since we always add a PLT entry, this is equivalent.
1881 case elfcpp::R_X86_64_GOTPLT64:
1882 gold_assert(have_got_offset);
1883 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1886 case elfcpp::R_X86_64_GOTPC64:
1889 elfcpp::Elf_types<64>::Elf_Addr value;
1890 value = target->got_plt_section()->address();
1891 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1895 case elfcpp::R_X86_64_GOTOFF64:
1897 elfcpp::Elf_types<64>::Elf_Addr value;
1898 value = (psymval->value(object, 0)
1899 - target->got_plt_section()->address());
1900 Relocate_functions<64, false>::rela64(view, value, addend);
1904 case elfcpp::R_X86_64_GOTPCREL:
1906 gold_assert(have_got_offset);
1907 elfcpp::Elf_types<64>::Elf_Addr value;
1908 value = target->got_plt_section()->address() + got_offset;
1909 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1913 case elfcpp::R_X86_64_GOTPCREL64:
1915 gold_assert(have_got_offset);
1916 elfcpp::Elf_types<64>::Elf_Addr value;
1917 value = target->got_plt_section()->address() + got_offset;
1918 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1922 case elfcpp::R_X86_64_COPY:
1923 case elfcpp::R_X86_64_GLOB_DAT:
1924 case elfcpp::R_X86_64_JUMP_SLOT:
1925 case elfcpp::R_X86_64_RELATIVE:
1926 // These are outstanding tls relocs, which are unexpected when linking
1927 case elfcpp::R_X86_64_TPOFF64:
1928 case elfcpp::R_X86_64_DTPMOD64:
1929 case elfcpp::R_X86_64_TLSDESC:
1930 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1931 _("unexpected reloc %u in object file"),
1935 // These are initial tls relocs, which are expected when linking
1936 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1937 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1938 case elfcpp::R_X86_64_TLSDESC_CALL:
1939 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1940 case elfcpp::R_X86_64_DTPOFF32:
1941 case elfcpp::R_X86_64_DTPOFF64:
1942 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1943 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1944 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1945 view, address, view_size);
1948 case elfcpp::R_X86_64_SIZE32:
1949 case elfcpp::R_X86_64_SIZE64:
1951 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1952 _("unsupported reloc %u"),
1960 // Perform a TLS relocation.
1963 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1964 Target_x86_64* target,
1966 const elfcpp::Rela<64, false>& rela,
1967 unsigned int r_type,
1968 const Sized_symbol<64>* gsym,
1969 const Symbol_value<64>* psymval,
1970 unsigned char* view,
1971 elfcpp::Elf_types<64>::Elf_Addr address,
1972 section_size_type view_size)
1974 Output_segment* tls_segment = relinfo->layout->tls_segment();
1976 const Sized_relobj<64, false>* object = relinfo->object;
1977 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1979 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1981 const bool is_final = (gsym == NULL
1982 ? !parameters->options().output_is_position_independent()
1983 : gsym->final_value_is_known());
1984 const tls::Tls_optimization optimized_type
1985 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1988 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1989 this->saw_tls_block_reloc_ = true;
1990 if (optimized_type == tls::TLSOPT_TO_LE)
1992 gold_assert(tls_segment != NULL);
1993 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1994 rela, r_type, value, view,
2000 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2001 ? GOT_TYPE_TLS_OFFSET
2002 : GOT_TYPE_TLS_PAIR);
2003 unsigned int got_offset;
2006 gold_assert(gsym->has_got_offset(got_type));
2007 got_offset = gsym->got_offset(got_type) - target->got_size();
2011 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2012 gold_assert(object->local_has_got_offset(r_sym, got_type));
2013 got_offset = (object->local_got_offset(r_sym, got_type)
2014 - target->got_size());
2016 if (optimized_type == tls::TLSOPT_TO_IE)
2018 gold_assert(tls_segment != NULL);
2019 value = target->got_plt_section()->address() + got_offset;
2020 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2021 value, view, address, view_size);
2024 else if (optimized_type == tls::TLSOPT_NONE)
2026 // Relocate the field with the offset of the pair of GOT
2028 value = target->got_plt_section()->address() + got_offset;
2029 Relocate_functions<64, false>::pcrela32(view, value, addend,
2034 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2035 _("unsupported reloc %u"), r_type);
2038 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2039 case elfcpp::R_X86_64_TLSDESC_CALL:
2040 this->saw_tls_block_reloc_ = true;
2041 if (optimized_type == tls::TLSOPT_TO_LE)
2043 gold_assert(tls_segment != NULL);
2044 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2045 rela, r_type, value, view,
2051 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2052 ? GOT_TYPE_TLS_OFFSET
2053 : GOT_TYPE_TLS_DESC);
2054 unsigned int got_offset;
2057 gold_assert(gsym->has_got_offset(got_type));
2058 got_offset = gsym->got_offset(got_type) - target->got_size();
2062 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2063 gold_assert(object->local_has_got_offset(r_sym, got_type));
2064 got_offset = (object->local_got_offset(r_sym, got_type)
2065 - target->got_size());
2067 if (optimized_type == tls::TLSOPT_TO_IE)
2069 gold_assert(tls_segment != NULL);
2070 value = target->got_plt_section()->address() + got_offset;
2071 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2072 rela, r_type, value, view, address,
2076 else if (optimized_type == tls::TLSOPT_NONE)
2078 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2080 // Relocate the field with the offset of the pair of GOT
2082 value = target->got_plt_section()->address() + got_offset;
2083 Relocate_functions<64, false>::pcrela32(view, value, addend,
2089 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2090 _("unsupported reloc %u"), r_type);
2093 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2094 this->saw_tls_block_reloc_ = true;
2095 if (optimized_type == tls::TLSOPT_TO_LE)
2097 gold_assert(tls_segment != NULL);
2098 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2099 value, view, view_size);
2102 else if (optimized_type == tls::TLSOPT_NONE)
2104 // Relocate the field with the offset of the GOT entry for
2105 // the module index.
2106 unsigned int got_offset;
2107 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2108 - target->got_size());
2109 value = target->got_plt_section()->address() + got_offset;
2110 Relocate_functions<64, false>::pcrela32(view, value, addend,
2114 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2115 _("unsupported reloc %u"), r_type);
2118 case elfcpp::R_X86_64_DTPOFF32:
2119 if (optimized_type == tls::TLSOPT_TO_LE)
2121 // This relocation type is used in debugging information.
2122 // In that case we need to not optimize the value. If we
2123 // haven't seen a TLSLD reloc, then we assume we should not
2124 // optimize this reloc.
2125 if (this->saw_tls_block_reloc_)
2127 gold_assert(tls_segment != NULL);
2128 value -= tls_segment->memsz();
2131 Relocate_functions<64, false>::rela32(view, value, addend);
2134 case elfcpp::R_X86_64_DTPOFF64:
2135 if (optimized_type == tls::TLSOPT_TO_LE)
2137 // See R_X86_64_DTPOFF32, just above, for why we test this.
2138 if (this->saw_tls_block_reloc_)
2140 gold_assert(tls_segment != NULL);
2141 value -= tls_segment->memsz();
2144 Relocate_functions<64, false>::rela64(view, value, addend);
2147 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2148 if (optimized_type == tls::TLSOPT_TO_LE)
2150 gold_assert(tls_segment != NULL);
2151 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2152 rela, r_type, value, view,
2156 else if (optimized_type == tls::TLSOPT_NONE)
2158 // Relocate the field with the offset of the GOT entry for
2159 // the tp-relative offset of the symbol.
2160 unsigned int got_offset;
2163 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2164 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2165 - target->got_size());
2169 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2170 gold_assert(object->local_has_got_offset(r_sym,
2171 GOT_TYPE_TLS_OFFSET));
2172 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2173 - target->got_size());
2175 value = target->got_plt_section()->address() + got_offset;
2176 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2179 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2180 _("unsupported reloc type %u"),
2184 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2185 value -= tls_segment->memsz();
2186 Relocate_functions<64, false>::rela32(view, value, addend);
2191 // Do a relocation in which we convert a TLS General-Dynamic to an
2195 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2198 const elfcpp::Rela<64, false>& rela,
2200 elfcpp::Elf_types<64>::Elf_Addr value,
2201 unsigned char* view,
2202 elfcpp::Elf_types<64>::Elf_Addr address,
2203 section_size_type view_size)
2205 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2206 // .word 0x6666; rex64; call __tls_get_addr
2207 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2210 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2212 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2213 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2214 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2215 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2217 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2219 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2220 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2222 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2224 this->skip_call_tls_get_addr_ = true;
2227 // Do a relocation in which we convert a TLS General-Dynamic to a
2231 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2233 Output_segment* tls_segment,
2234 const elfcpp::Rela<64, false>& rela,
2236 elfcpp::Elf_types<64>::Elf_Addr value,
2237 unsigned char* view,
2238 section_size_type view_size)
2240 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2241 // .word 0x6666; rex64; call __tls_get_addr
2242 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2244 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2245 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2247 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2248 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2249 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2250 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2252 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2254 value -= tls_segment->memsz();
2255 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2257 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2259 this->skip_call_tls_get_addr_ = true;
2262 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2265 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2266 const Relocate_info<64, false>* relinfo,
2269 const elfcpp::Rela<64, false>& rela,
2270 unsigned int r_type,
2271 elfcpp::Elf_types<64>::Elf_Addr value,
2272 unsigned char* view,
2273 elfcpp::Elf_types<64>::Elf_Addr address,
2274 section_size_type view_size)
2276 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2278 // leaq foo@tlsdesc(%rip), %rax
2279 // ==> movq foo@gottpoff(%rip), %rax
2280 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2281 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2282 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2283 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2285 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2286 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2290 // call *foo@tlscall(%rax)
2292 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2293 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2294 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2295 view[0] == 0xff && view[1] == 0x10);
2301 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2304 Target_x86_64::Relocate::tls_desc_gd_to_le(
2305 const Relocate_info<64, false>* relinfo,
2307 Output_segment* tls_segment,
2308 const elfcpp::Rela<64, false>& rela,
2309 unsigned int r_type,
2310 elfcpp::Elf_types<64>::Elf_Addr value,
2311 unsigned char* view,
2312 section_size_type view_size)
2314 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2316 // leaq foo@tlsdesc(%rip), %rax
2317 // ==> movq foo@tpoff, %rax
2318 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2319 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2320 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2321 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2324 value -= tls_segment->memsz();
2325 Relocate_functions<64, false>::rela32(view, value, 0);
2329 // call *foo@tlscall(%rax)
2331 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2332 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2333 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2334 view[0] == 0xff && view[1] == 0x10);
2341 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2344 const elfcpp::Rela<64, false>& rela,
2346 elfcpp::Elf_types<64>::Elf_Addr,
2347 unsigned char* view,
2348 section_size_type view_size)
2350 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2351 // ... leq foo@dtpoff(%rax),%reg
2352 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2354 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2355 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2357 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2358 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2360 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2362 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2364 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2366 this->skip_call_tls_get_addr_ = true;
2369 // Do a relocation in which we convert a TLS Initial-Exec to a
2373 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2375 Output_segment* tls_segment,
2376 const elfcpp::Rela<64, false>& rela,
2378 elfcpp::Elf_types<64>::Elf_Addr value,
2379 unsigned char* view,
2380 section_size_type view_size)
2382 // We need to examine the opcodes to figure out which instruction we
2385 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2386 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2388 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2389 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2391 unsigned char op1 = view[-3];
2392 unsigned char op2 = view[-2];
2393 unsigned char op3 = view[-1];
2394 unsigned char reg = op3 >> 3;
2402 view[-1] = 0xc0 | reg;
2406 // Special handling for %rsp.
2410 view[-1] = 0xc0 | reg;
2418 view[-1] = 0x80 | reg | (reg << 3);
2421 value -= tls_segment->memsz();
2422 Relocate_functions<64, false>::rela32(view, value, 0);
2425 // Relocate section data.
2428 Target_x86_64::relocate_section(
2429 const Relocate_info<64, false>* relinfo,
2430 unsigned int sh_type,
2431 const unsigned char* prelocs,
2433 Output_section* output_section,
2434 bool needs_special_offset_handling,
2435 unsigned char* view,
2436 elfcpp::Elf_types<64>::Elf_Addr address,
2437 section_size_type view_size,
2438 const Reloc_symbol_changes* reloc_symbol_changes)
2440 gold_assert(sh_type == elfcpp::SHT_RELA);
2442 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2443 Target_x86_64::Relocate>(
2449 needs_special_offset_handling,
2453 reloc_symbol_changes);
2456 // Return the size of a relocation while scanning during a relocatable
2460 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2461 unsigned int r_type,
2466 case elfcpp::R_X86_64_NONE:
2467 case elfcpp::R_386_GNU_VTINHERIT:
2468 case elfcpp::R_386_GNU_VTENTRY:
2469 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2470 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2471 case elfcpp::R_X86_64_TLSDESC_CALL:
2472 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2473 case elfcpp::R_X86_64_DTPOFF32:
2474 case elfcpp::R_X86_64_DTPOFF64:
2475 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2476 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2479 case elfcpp::R_X86_64_64:
2480 case elfcpp::R_X86_64_PC64:
2481 case elfcpp::R_X86_64_GOTOFF64:
2482 case elfcpp::R_X86_64_GOTPC64:
2483 case elfcpp::R_X86_64_PLTOFF64:
2484 case elfcpp::R_X86_64_GOT64:
2485 case elfcpp::R_X86_64_GOTPCREL64:
2486 case elfcpp::R_X86_64_GOTPCREL:
2487 case elfcpp::R_X86_64_GOTPLT64:
2490 case elfcpp::R_X86_64_32:
2491 case elfcpp::R_X86_64_32S:
2492 case elfcpp::R_X86_64_PC32:
2493 case elfcpp::R_X86_64_PLT32:
2494 case elfcpp::R_X86_64_GOTPC32:
2495 case elfcpp::R_X86_64_GOT32:
2498 case elfcpp::R_X86_64_16:
2499 case elfcpp::R_X86_64_PC16:
2502 case elfcpp::R_X86_64_8:
2503 case elfcpp::R_X86_64_PC8:
2506 case elfcpp::R_X86_64_COPY:
2507 case elfcpp::R_X86_64_GLOB_DAT:
2508 case elfcpp::R_X86_64_JUMP_SLOT:
2509 case elfcpp::R_X86_64_RELATIVE:
2510 // These are outstanding tls relocs, which are unexpected when linking
2511 case elfcpp::R_X86_64_TPOFF64:
2512 case elfcpp::R_X86_64_DTPMOD64:
2513 case elfcpp::R_X86_64_TLSDESC:
2514 object->error(_("unexpected reloc %u in object file"), r_type);
2517 case elfcpp::R_X86_64_SIZE32:
2518 case elfcpp::R_X86_64_SIZE64:
2520 object->error(_("unsupported reloc %u against local symbol"), r_type);
2525 // Scan the relocs during a relocatable link.
2528 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2529 Symbol_table* symtab,
2531 Sized_relobj<64, false>* object,
2532 unsigned int data_shndx,
2533 unsigned int sh_type,
2534 const unsigned char* prelocs,
2536 Output_section* output_section,
2537 bool needs_special_offset_handling,
2538 size_t local_symbol_count,
2539 const unsigned char* plocal_symbols,
2540 Relocatable_relocs* rr)
2542 gold_assert(sh_type == elfcpp::SHT_RELA);
2544 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2545 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2547 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2548 Scan_relocatable_relocs>(
2557 needs_special_offset_handling,
2563 // Relocate a section during a relocatable link.
2566 Target_x86_64::relocate_for_relocatable(
2567 const Relocate_info<64, false>* relinfo,
2568 unsigned int sh_type,
2569 const unsigned char* prelocs,
2571 Output_section* output_section,
2572 off_t offset_in_output_section,
2573 const Relocatable_relocs* rr,
2574 unsigned char* view,
2575 elfcpp::Elf_types<64>::Elf_Addr view_address,
2576 section_size_type view_size,
2577 unsigned char* reloc_view,
2578 section_size_type reloc_view_size)
2580 gold_assert(sh_type == elfcpp::SHT_RELA);
2582 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2587 offset_in_output_section,
2596 // Return the value to use for a dynamic which requires special
2597 // treatment. This is how we support equality comparisons of function
2598 // pointers across shared library boundaries, as described in the
2599 // processor specific ABI supplement.
2602 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2604 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2605 return this->plt_section()->address() + gsym->plt_offset();
2608 // Return a string used to fill a code section with nops to take up
2609 // the specified length.
2612 Target_x86_64::do_code_fill(section_size_type length) const
2616 // Build a jmpq instruction to skip over the bytes.
2617 unsigned char jmp[5];
2619 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2620 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2621 + std::string(length - 5, '\0'));
2624 // Nop sequences of various lengths.
2625 const char nop1[1] = { 0x90 }; // nop
2626 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2627 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2628 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2629 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2631 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2633 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2635 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2636 0x00, 0x00, 0x00, 0x00 };
2637 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2638 0x00, 0x00, 0x00, 0x00,
2640 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2641 0x84, 0x00, 0x00, 0x00,
2643 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2644 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2646 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2647 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2648 0x00, 0x00, 0x00, 0x00 };
2649 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2650 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2651 0x00, 0x00, 0x00, 0x00,
2653 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2654 0x66, 0x2e, 0x0f, 0x1f, // data16
2655 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2657 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2658 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2659 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2662 const char* nops[16] = {
2664 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2665 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2668 return std::string(nops[length], length);
2671 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2672 // compiled with -fstack-split. The function calls non-stack-split
2673 // code. We have to change the function so that it always ensures
2674 // that it has enough stack space to run some random function.
2677 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2678 section_offset_type fnoffset,
2679 section_size_type fnsize,
2680 unsigned char* view,
2681 section_size_type view_size,
2683 std::string* to) const
2685 // The function starts with a comparison of the stack pointer and a
2686 // field in the TCB. This is followed by a jump.
2689 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2692 // We will call __morestack if the carry flag is set after this
2693 // comparison. We turn the comparison into an stc instruction
2695 view[fnoffset] = '\xf9';
2696 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2698 // lea NN(%rsp),%r10
2699 else if (this->match_view(view, view_size, fnoffset, "\x4c\x8d\x94\x24", 4)
2702 // This is loading an offset from the stack pointer for a
2703 // comparison. The offset is negative, so we decrease the
2704 // offset by the amount of space we need for the stack. This
2705 // means we will avoid calling __morestack if there happens to
2706 // be plenty of space on the stack already.
2707 unsigned char* pval = view + fnoffset + 4;
2708 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2709 val -= parameters->options().split_stack_adjust_size();
2710 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2714 if (!object->has_no_split_stack())
2715 object->error(_("failed to match split-stack sequence at "
2716 "section %u offset %0zx"),
2721 // We have to change the function so that it calls
2722 // __morestack_non_split instead of __morestack. The former will
2723 // allocate additional stack space.
2724 *from = "__morestack";
2725 *to = "__morestack_non_split";
2728 // The selector for x86_64 object files.
2730 class Target_selector_x86_64 : public Target_selector_freebsd
2733 Target_selector_x86_64()
2734 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2735 "elf64-x86-64-freebsd")
2739 do_instantiate_target()
2740 { return new Target_x86_64(); }
2744 Target_selector_x86_64 target_selector_x86_64;
2746 } // End anonymous namespace.