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"
48 class Output_data_plt_x86_64;
50 // The x86_64 target class.
52 // http://www.x86-64.org/documentation/abi.pdf
53 // TLS info comes from
54 // http://people.redhat.com/drepper/tls.pdf
55 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
57 class Target_x86_64 : public Target_freebsd<64, false>
60 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
61 // uses only Elf64_Rela relocation entries with explicit addends."
62 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
65 : Target_freebsd<64, false>(&x86_64_info),
66 got_(NULL), plt_(NULL), got_plt_(NULL), global_offset_table_(NULL),
67 rela_dyn_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
68 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
71 // Hook for a new output section.
73 do_new_output_section(Output_section*) const;
75 // Scan the relocations to look for symbol adjustments.
77 gc_process_relocs(Symbol_table* symtab,
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(Symbol_table* symtab,
93 Sized_relobj<64, false>* object,
94 unsigned int data_shndx,
96 const unsigned char* prelocs,
98 Output_section* output_section,
99 bool needs_special_offset_handling,
100 size_t local_symbol_count,
101 const unsigned char* plocal_symbols);
103 // Finalize the sections.
105 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
107 // Return the value to use for a dynamic which requires special
110 do_dynsym_value(const Symbol*) const;
112 // Relocate a section.
114 relocate_section(const Relocate_info<64, false>*,
115 unsigned int sh_type,
116 const unsigned char* prelocs,
118 Output_section* output_section,
119 bool needs_special_offset_handling,
121 elfcpp::Elf_types<64>::Elf_Addr view_address,
122 section_size_type view_size,
123 const Reloc_symbol_changes*);
125 // Scan the relocs during a relocatable link.
127 scan_relocatable_relocs(Symbol_table* symtab,
129 Sized_relobj<64, false>* object,
130 unsigned int data_shndx,
131 unsigned int sh_type,
132 const unsigned char* prelocs,
134 Output_section* output_section,
135 bool needs_special_offset_handling,
136 size_t local_symbol_count,
137 const unsigned char* plocal_symbols,
138 Relocatable_relocs*);
140 // Relocate a section during a relocatable link.
142 relocate_for_relocatable(const Relocate_info<64, false>*,
143 unsigned int sh_type,
144 const unsigned char* prelocs,
146 Output_section* output_section,
147 off_t offset_in_output_section,
148 const Relocatable_relocs*,
150 elfcpp::Elf_types<64>::Elf_Addr view_address,
151 section_size_type view_size,
152 unsigned char* reloc_view,
153 section_size_type reloc_view_size);
155 // Return a string used to fill a code section with nops.
157 do_code_fill(section_size_type length) const;
159 // Return whether SYM is defined by the ABI.
161 do_is_defined_by_abi(const Symbol* sym) const
162 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
164 // Adjust -fstack-split code which calls non-stack-split code.
166 do_calls_non_split(Relobj* object, unsigned int shndx,
167 section_offset_type fnoffset, section_size_type fnsize,
168 unsigned char* view, section_size_type view_size,
169 std::string* from, std::string* to) const;
171 // Return the size of the GOT section.
175 gold_assert(this->got_ != NULL);
176 return this->got_->data_size();
180 // The class which scans relocations.
185 : issued_non_pic_error_(false)
189 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
190 Sized_relobj<64, false>* object,
191 unsigned int data_shndx,
192 Output_section* output_section,
193 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
194 const elfcpp::Sym<64, false>& lsym);
197 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
198 Sized_relobj<64, false>* object,
199 unsigned int data_shndx,
200 Output_section* output_section,
201 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
206 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
209 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
213 check_non_pic(Relobj*, unsigned int r_type);
215 // Whether we have issued an error about a non-PIC compilation.
216 bool issued_non_pic_error_;
219 // The class which implements relocation.
224 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
229 if (this->skip_call_tls_get_addr_)
231 // FIXME: This needs to specify the location somehow.
232 gold_error(_("missing expected TLS relocation"));
236 // Do a relocation. Return false if the caller should not issue
237 // any warnings about this relocation.
239 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
240 size_t relnum, const elfcpp::Rela<64, false>&,
241 unsigned int r_type, const Sized_symbol<64>*,
242 const Symbol_value<64>*,
243 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
247 // Do a TLS relocation.
249 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
250 size_t relnum, const elfcpp::Rela<64, false>&,
251 unsigned int r_type, const Sized_symbol<64>*,
252 const Symbol_value<64>*,
253 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
256 // Do a TLS General-Dynamic to Initial-Exec transition.
258 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
259 Output_segment* tls_segment,
260 const elfcpp::Rela<64, false>&, unsigned int r_type,
261 elfcpp::Elf_types<64>::Elf_Addr value,
263 elfcpp::Elf_types<64>::Elf_Addr,
264 section_size_type view_size);
266 // Do a TLS General-Dynamic to Local-Exec transition.
268 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
269 Output_segment* tls_segment,
270 const elfcpp::Rela<64, false>&, unsigned int r_type,
271 elfcpp::Elf_types<64>::Elf_Addr value,
273 section_size_type view_size);
275 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
277 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
278 Output_segment* tls_segment,
279 const elfcpp::Rela<64, false>&, unsigned int r_type,
280 elfcpp::Elf_types<64>::Elf_Addr value,
282 elfcpp::Elf_types<64>::Elf_Addr,
283 section_size_type view_size);
285 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
287 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
288 Output_segment* tls_segment,
289 const elfcpp::Rela<64, false>&, unsigned int r_type,
290 elfcpp::Elf_types<64>::Elf_Addr value,
292 section_size_type view_size);
294 // Do a TLS Local-Dynamic to Local-Exec transition.
296 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
297 Output_segment* tls_segment,
298 const elfcpp::Rela<64, false>&, unsigned int r_type,
299 elfcpp::Elf_types<64>::Elf_Addr value,
301 section_size_type view_size);
303 // Do a TLS Initial-Exec to Local-Exec transition.
305 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
306 Output_segment* tls_segment,
307 const elfcpp::Rela<64, false>&, unsigned int r_type,
308 elfcpp::Elf_types<64>::Elf_Addr value,
310 section_size_type view_size);
312 // This is set if we should skip the next reloc, which should be a
313 // PLT32 reloc against ___tls_get_addr.
314 bool skip_call_tls_get_addr_;
316 // This is set if we see a relocation which could load the address
317 // of the TLS block. Whether we see such a relocation determines
318 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
319 // in debugging sections.
320 bool saw_tls_block_reloc_;
323 // A class which returns the size required for a relocation type,
324 // used while scanning relocs during a relocatable link.
325 class Relocatable_size_for_reloc
329 get_size_for_reloc(unsigned int, Relobj*);
332 // Adjust TLS relocation type based on the options and whether this
333 // is a local symbol.
334 static tls::Tls_optimization
335 optimize_tls_reloc(bool is_final, int r_type);
337 // Get the GOT section, creating it if necessary.
338 Output_data_got<64, false>*
339 got_section(Symbol_table*, Layout*);
341 // Get the GOT PLT section.
343 got_plt_section() const
345 gold_assert(this->got_plt_ != NULL);
346 return this->got_plt_;
349 // Create the PLT section.
351 make_plt_section(Symbol_table* symtab, Layout* layout);
353 // Create a PLT entry for a global symbol.
355 make_plt_entry(Symbol_table*, Layout*, Symbol*);
357 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
359 define_tls_base_symbol(Symbol_table*, Layout*);
361 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
363 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
365 // Create a GOT entry for the TLS module index.
367 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
368 Sized_relobj<64, false>* object);
370 // Get the PLT section.
371 Output_data_plt_x86_64*
374 gold_assert(this->plt_ != NULL);
378 // Get the dynamic reloc section, creating it if necessary.
380 rela_dyn_section(Layout*);
382 // Add a potential copy relocation.
384 copy_reloc(Symbol_table* symtab, Layout* layout,
385 Sized_relobj<64, false>* object,
386 unsigned int shndx, Output_section* output_section,
387 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
389 this->copy_relocs_.copy_reloc(symtab, layout,
390 symtab->get_sized_symbol<64>(sym),
391 object, shndx, output_section,
392 reloc, this->rela_dyn_section(layout));
395 // Information about this specific target which we pass to the
396 // general Target structure.
397 static const Target::Target_info x86_64_info;
401 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
402 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
403 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
404 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
408 Output_data_got<64, false>* got_;
410 Output_data_plt_x86_64* plt_;
411 // The GOT PLT section.
412 Output_data_space* got_plt_;
413 // The _GLOBAL_OFFSET_TABLE_ symbol.
414 Symbol* global_offset_table_;
415 // The dynamic reloc section.
416 Reloc_section* rela_dyn_;
417 // Relocs saved to avoid a COPY reloc.
418 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
419 // Space for variables copied with a COPY reloc.
420 Output_data_space* dynbss_;
421 // Offset of the GOT entry for the TLS module index.
422 unsigned int got_mod_index_offset_;
423 // True if the _TLS_MODULE_BASE_ symbol has been defined.
424 bool tls_base_symbol_defined_;
427 const Target::Target_info Target_x86_64::x86_64_info =
430 false, // is_big_endian
431 elfcpp::EM_X86_64, // machine_code
432 false, // has_make_symbol
433 false, // has_resolve
434 true, // has_code_fill
435 true, // is_default_stack_executable
437 "/lib/ld64.so.1", // program interpreter
438 0x400000, // default_text_segment_address
439 0x1000, // abi_pagesize (overridable by -z max-page-size)
440 0x1000, // common_pagesize (overridable by -z common-page-size)
441 elfcpp::SHN_UNDEF, // small_common_shndx
442 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
443 0, // small_common_section_flags
444 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
445 NULL, // attributes_section
446 NULL // attributes_vendor
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),
474 this->got_, false, true, true,
477 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
478 os = layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
480 | elfcpp::SHF_WRITE),
481 this->got_plt_, false, false,
484 // The first three entries are reserved.
485 this->got_plt_->set_current_data_size(3 * 8);
487 // Those bytes can go into the relro segment.
488 layout->increase_relro(3 * 8);
490 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
491 this->global_offset_table_ =
492 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
493 Symbol_table::PREDEFINED,
495 0, 0, elfcpp::STT_OBJECT,
497 elfcpp::STV_HIDDEN, 0,
504 // Get the dynamic reloc section, creating it if necessary.
506 Target_x86_64::Reloc_section*
507 Target_x86_64::rela_dyn_section(Layout* layout)
509 if (this->rela_dyn_ == NULL)
511 gold_assert(layout != NULL);
512 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
513 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
514 elfcpp::SHF_ALLOC, this->rela_dyn_, true,
515 false, false, false);
517 return this->rela_dyn_;
520 // A class to handle the PLT data.
522 class Output_data_plt_x86_64 : public Output_section_data
525 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
527 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
530 // Add an entry to the PLT.
532 add_entry(Symbol* gsym);
534 // Add the reserved TLSDESC_PLT entry to the PLT.
536 reserve_tlsdesc_entry(unsigned int got_offset)
537 { this->tlsdesc_got_offset_ = got_offset; }
539 // Return true if a TLSDESC_PLT entry has been reserved.
541 has_tlsdesc_entry() const
542 { return this->tlsdesc_got_offset_ != -1U; }
544 // Return the GOT offset for the reserved TLSDESC_PLT entry.
546 get_tlsdesc_got_offset() const
547 { return this->tlsdesc_got_offset_; }
549 // Return the offset of the reserved TLSDESC_PLT entry.
551 get_tlsdesc_plt_offset() const
552 { return (this->count_ + 1) * plt_entry_size; }
554 // Return the .rel.plt section data.
557 { return this->rel_; }
561 do_adjust_output_section(Output_section* os);
563 // Write to a map file.
565 do_print_to_mapfile(Mapfile* mapfile) const
566 { mapfile->print_output_data(this, _("** PLT")); }
569 // The size of an entry in the PLT.
570 static const int plt_entry_size = 16;
572 // The first entry in the PLT.
573 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
574 // procedure linkage table for both programs and shared objects."
575 static unsigned char first_plt_entry[plt_entry_size];
577 // Other entries in the PLT for an executable.
578 static unsigned char plt_entry[plt_entry_size];
580 // The reserved TLSDESC entry in the PLT for an executable.
581 static unsigned char tlsdesc_plt_entry[plt_entry_size];
583 // Set the final size.
585 set_final_data_size();
587 // Write out the PLT data.
589 do_write(Output_file*);
591 // The reloc section.
594 Output_data_got<64, false>* got_;
595 // The .got.plt section.
596 Output_data_space* got_plt_;
597 // The number of PLT entries.
599 // Offset of the reserved TLSDESC_GOT entry when needed.
600 unsigned int tlsdesc_got_offset_;
603 // Create the PLT section. The ordinary .got section is an argument,
604 // since we need to refer to the start. We also create our own .got
605 // section just for PLT entries.
607 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
608 Output_data_got<64, false>* got,
609 Output_data_space* got_plt)
610 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
611 tlsdesc_got_offset_(-1U)
613 this->rel_ = new Reloc_section(false);
614 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
615 elfcpp::SHF_ALLOC, this->rel_, true,
616 false, false, false);
620 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
622 os->set_entsize(plt_entry_size);
625 // Add an entry to the PLT.
628 Output_data_plt_x86_64::add_entry(Symbol* gsym)
630 gold_assert(!gsym->has_plt_offset());
632 // Note that when setting the PLT offset we skip the initial
633 // reserved PLT entry.
634 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
638 section_offset_type got_offset = this->got_plt_->current_data_size();
640 // Every PLT entry needs a GOT entry which points back to the PLT
641 // entry (this will be changed by the dynamic linker, normally
642 // lazily when the function is called).
643 this->got_plt_->set_current_data_size(got_offset + 8);
645 // Every PLT entry needs a reloc.
646 gsym->set_needs_dynsym_entry();
647 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
650 // Note that we don't need to save the symbol. The contents of the
651 // PLT are independent of which symbols are used. The symbols only
652 // appear in the relocations.
655 // Set the final size.
657 Output_data_plt_x86_64::set_final_data_size()
659 unsigned int count = this->count_;
660 if (this->has_tlsdesc_entry())
662 this->set_data_size((count + 1) * plt_entry_size);
665 // The first entry in the PLT for an executable.
667 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
669 // From AMD64 ABI Draft 0.98, page 76
670 0xff, 0x35, // pushq contents of memory address
671 0, 0, 0, 0, // replaced with address of .got + 8
672 0xff, 0x25, // jmp indirect
673 0, 0, 0, 0, // replaced with address of .got + 16
674 0x90, 0x90, 0x90, 0x90 // noop (x4)
677 // Subsequent entries in the PLT for an executable.
679 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
681 // From AMD64 ABI Draft 0.98, page 76
682 0xff, 0x25, // jmpq indirect
683 0, 0, 0, 0, // replaced with address of symbol in .got
684 0x68, // pushq immediate
685 0, 0, 0, 0, // replaced with offset into relocation table
686 0xe9, // jmpq relative
687 0, 0, 0, 0 // replaced with offset to start of .plt
690 // The reserved TLSDESC entry in the PLT for an executable.
692 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
694 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
695 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
696 0xff, 0x35, // pushq x(%rip)
697 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
698 0xff, 0x25, // jmpq *y(%rip)
699 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
704 // Write out the PLT. This uses the hand-coded instructions above,
705 // and adjusts them as needed. This is specified by the AMD64 ABI.
708 Output_data_plt_x86_64::do_write(Output_file* of)
710 const off_t offset = this->offset();
711 const section_size_type oview_size =
712 convert_to_section_size_type(this->data_size());
713 unsigned char* const oview = of->get_output_view(offset, oview_size);
715 const off_t got_file_offset = this->got_plt_->offset();
716 const section_size_type got_size =
717 convert_to_section_size_type(this->got_plt_->data_size());
718 unsigned char* const got_view = of->get_output_view(got_file_offset,
721 unsigned char* pov = oview;
723 // The base address of the .plt section.
724 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
725 // The base address of the .got section.
726 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
727 // The base address of the PLT portion of the .got section,
728 // which is where the GOT pointer will point, and where the
729 // three reserved GOT entries are located.
730 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
732 memcpy(pov, first_plt_entry, plt_entry_size);
733 // We do a jmp relative to the PC at the end of this instruction.
734 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
736 - (plt_address + 6)));
737 elfcpp::Swap<32, false>::writeval(pov + 8,
739 - (plt_address + 12)));
740 pov += plt_entry_size;
742 unsigned char* got_pov = got_view;
744 memset(got_pov, 0, 24);
747 unsigned int plt_offset = plt_entry_size;
748 unsigned int got_offset = 24;
749 const unsigned int count = this->count_;
750 for (unsigned int plt_index = 0;
753 pov += plt_entry_size,
755 plt_offset += plt_entry_size,
758 // Set and adjust the PLT entry itself.
759 memcpy(pov, plt_entry, plt_entry_size);
760 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
761 (got_address + got_offset
762 - (plt_address + plt_offset
765 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
766 elfcpp::Swap<32, false>::writeval(pov + 12,
767 - (plt_offset + plt_entry_size));
769 // Set the entry in the GOT.
770 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
773 if (this->has_tlsdesc_entry())
775 // Set and adjust the reserved TLSDESC PLT entry.
776 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
777 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
778 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
780 - (plt_address + plt_offset
782 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
785 - (plt_address + plt_offset
787 pov += plt_entry_size;
790 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
791 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
793 of->write_output_view(offset, oview_size, oview);
794 of->write_output_view(got_file_offset, got_size, got_view);
797 // Create the PLT section.
800 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
802 if (this->plt_ == NULL)
804 // Create the GOT sections first.
805 this->got_section(symtab, layout);
807 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
809 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
811 | elfcpp::SHF_EXECINSTR),
812 this->plt_, false, false, false, false);
816 // Create a PLT entry for a global symbol.
819 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
822 if (gsym->has_plt_offset())
825 if (this->plt_ == NULL)
826 this->make_plt_section(symtab, layout);
828 this->plt_->add_entry(gsym);
831 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
834 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
836 if (this->tls_base_symbol_defined_)
839 Output_segment* tls_segment = layout->tls_segment();
840 if (tls_segment != NULL)
842 bool is_exec = parameters->options().output_is_executable();
843 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
844 Symbol_table::PREDEFINED,
848 elfcpp::STV_HIDDEN, 0,
850 ? Symbol::SEGMENT_END
851 : Symbol::SEGMENT_START),
854 this->tls_base_symbol_defined_ = true;
857 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
860 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
863 if (this->plt_ == NULL)
864 this->make_plt_section(symtab, layout);
866 if (!this->plt_->has_tlsdesc_entry())
868 // Allocate the TLSDESC_GOT entry.
869 Output_data_got<64, false>* got = this->got_section(symtab, layout);
870 unsigned int got_offset = got->add_constant(0);
872 // Allocate the TLSDESC_PLT entry.
873 this->plt_->reserve_tlsdesc_entry(got_offset);
877 // Create a GOT entry for the TLS module index.
880 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
881 Sized_relobj<64, false>* object)
883 if (this->got_mod_index_offset_ == -1U)
885 gold_assert(symtab != NULL && layout != NULL && object != NULL);
886 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
887 Output_data_got<64, false>* got = this->got_section(symtab, layout);
888 unsigned int got_offset = got->add_constant(0);
889 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
891 got->add_constant(0);
892 this->got_mod_index_offset_ = got_offset;
894 return this->got_mod_index_offset_;
897 // Optimize the TLS relocation type based on what we know about the
898 // symbol. IS_FINAL is true if the final address of this symbol is
899 // known at link time.
901 tls::Tls_optimization
902 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
904 // If we are generating a shared library, then we can't do anything
906 if (parameters->options().shared())
907 return tls::TLSOPT_NONE;
911 case elfcpp::R_X86_64_TLSGD:
912 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
913 case elfcpp::R_X86_64_TLSDESC_CALL:
914 // These are General-Dynamic which permits fully general TLS
915 // access. Since we know that we are generating an executable,
916 // we can convert this to Initial-Exec. If we also know that
917 // this is a local symbol, we can further switch to Local-Exec.
919 return tls::TLSOPT_TO_LE;
920 return tls::TLSOPT_TO_IE;
922 case elfcpp::R_X86_64_TLSLD:
923 // This is Local-Dynamic, which refers to a local symbol in the
924 // dynamic TLS block. Since we know that we generating an
925 // executable, we can switch to Local-Exec.
926 return tls::TLSOPT_TO_LE;
928 case elfcpp::R_X86_64_DTPOFF32:
929 case elfcpp::R_X86_64_DTPOFF64:
930 // Another Local-Dynamic reloc.
931 return tls::TLSOPT_TO_LE;
933 case elfcpp::R_X86_64_GOTTPOFF:
934 // These are Initial-Exec relocs which get the thread offset
935 // from the GOT. If we know that we are linking against the
936 // local symbol, we can switch to Local-Exec, which links the
937 // thread offset into the instruction.
939 return tls::TLSOPT_TO_LE;
940 return tls::TLSOPT_NONE;
942 case elfcpp::R_X86_64_TPOFF32:
943 // When we already have Local-Exec, there is nothing further we
945 return tls::TLSOPT_NONE;
952 // Report an unsupported relocation against a local symbol.
955 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
958 gold_error(_("%s: unsupported reloc %u against local symbol"),
959 object->name().c_str(), r_type);
962 // We are about to emit a dynamic relocation of type R_TYPE. If the
963 // dynamic linker does not support it, issue an error. The GNU linker
964 // only issues a non-PIC error for an allocated read-only section.
965 // Here we know the section is allocated, but we don't know that it is
966 // read-only. But we check for all the relocation types which the
967 // glibc dynamic linker supports, so it seems appropriate to issue an
968 // error even if the section is not read-only.
971 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
975 // These are the relocation types supported by glibc for x86_64.
976 case elfcpp::R_X86_64_RELATIVE:
977 case elfcpp::R_X86_64_GLOB_DAT:
978 case elfcpp::R_X86_64_JUMP_SLOT:
979 case elfcpp::R_X86_64_DTPMOD64:
980 case elfcpp::R_X86_64_DTPOFF64:
981 case elfcpp::R_X86_64_TPOFF64:
982 case elfcpp::R_X86_64_64:
983 case elfcpp::R_X86_64_32:
984 case elfcpp::R_X86_64_PC32:
985 case elfcpp::R_X86_64_COPY:
989 // This prevents us from issuing more than one error per reloc
990 // section. But we can still wind up issuing more than one
991 // error per object file.
992 if (this->issued_non_pic_error_)
994 gold_assert(parameters->options().output_is_position_independent());
995 object->error(_("requires unsupported dynamic reloc; "
996 "recompile with -fPIC"));
997 this->issued_non_pic_error_ = true;
1000 case elfcpp::R_X86_64_NONE:
1005 // Scan a relocation for a local symbol.
1008 Target_x86_64::Scan::local(Symbol_table* symtab,
1010 Target_x86_64* target,
1011 Sized_relobj<64, false>* object,
1012 unsigned int data_shndx,
1013 Output_section* output_section,
1014 const elfcpp::Rela<64, false>& reloc,
1015 unsigned int r_type,
1016 const elfcpp::Sym<64, false>& lsym)
1020 case elfcpp::R_X86_64_NONE:
1021 case elfcpp::R_386_GNU_VTINHERIT:
1022 case elfcpp::R_386_GNU_VTENTRY:
1025 case elfcpp::R_X86_64_64:
1026 // If building a shared library (or a position-independent
1027 // executable), we need to create a dynamic relocation for this
1028 // location. The relocation applied at link time will apply the
1029 // link-time value, so we flag the location with an
1030 // R_X86_64_RELATIVE relocation so the dynamic loader can
1031 // relocate it easily.
1032 if (parameters->options().output_is_position_independent())
1034 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1035 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1036 rela_dyn->add_local_relative(object, r_sym,
1037 elfcpp::R_X86_64_RELATIVE,
1038 output_section, data_shndx,
1039 reloc.get_r_offset(),
1040 reloc.get_r_addend());
1044 case elfcpp::R_X86_64_32:
1045 case elfcpp::R_X86_64_32S:
1046 case elfcpp::R_X86_64_16:
1047 case elfcpp::R_X86_64_8:
1048 // If building a shared library (or a position-independent
1049 // executable), we need to create a dynamic relocation for this
1050 // location. We can't use an R_X86_64_RELATIVE relocation
1051 // because that is always a 64-bit relocation.
1052 if (parameters->options().output_is_position_independent())
1054 this->check_non_pic(object, r_type);
1056 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1057 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1058 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1059 rela_dyn->add_local(object, r_sym, r_type, output_section,
1060 data_shndx, reloc.get_r_offset(),
1061 reloc.get_r_addend());
1064 gold_assert(lsym.get_st_value() == 0);
1065 unsigned int shndx = lsym.get_st_shndx();
1067 shndx = object->adjust_sym_shndx(r_sym, shndx,
1070 object->error(_("section symbol %u has bad shndx %u"),
1073 rela_dyn->add_local_section(object, shndx,
1074 r_type, output_section,
1075 data_shndx, reloc.get_r_offset(),
1076 reloc.get_r_addend());
1081 case elfcpp::R_X86_64_PC64:
1082 case elfcpp::R_X86_64_PC32:
1083 case elfcpp::R_X86_64_PC16:
1084 case elfcpp::R_X86_64_PC8:
1087 case elfcpp::R_X86_64_PLT32:
1088 // Since we know this is a local symbol, we can handle this as a
1092 case elfcpp::R_X86_64_GOTPC32:
1093 case elfcpp::R_X86_64_GOTOFF64:
1094 case elfcpp::R_X86_64_GOTPC64:
1095 case elfcpp::R_X86_64_PLTOFF64:
1096 // We need a GOT section.
1097 target->got_section(symtab, layout);
1098 // For PLTOFF64, we'd normally want a PLT section, but since we
1099 // know this is a local symbol, no PLT is needed.
1102 case elfcpp::R_X86_64_GOT64:
1103 case elfcpp::R_X86_64_GOT32:
1104 case elfcpp::R_X86_64_GOTPCREL64:
1105 case elfcpp::R_X86_64_GOTPCREL:
1106 case elfcpp::R_X86_64_GOTPLT64:
1108 // The symbol requires a GOT entry.
1109 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1110 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1111 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1113 // If we are generating a shared object, we need to add a
1114 // dynamic relocation for this symbol's GOT entry.
1115 if (parameters->options().output_is_position_independent())
1117 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1118 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1119 if (r_type != elfcpp::R_X86_64_GOT32)
1120 rela_dyn->add_local_relative(
1121 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1122 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1125 this->check_non_pic(object, r_type);
1127 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1128 rela_dyn->add_local(
1129 object, r_sym, r_type, got,
1130 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1134 // For GOTPLT64, we'd normally want a PLT section, but since
1135 // we know this is a local symbol, no PLT is needed.
1139 case elfcpp::R_X86_64_COPY:
1140 case elfcpp::R_X86_64_GLOB_DAT:
1141 case elfcpp::R_X86_64_JUMP_SLOT:
1142 case elfcpp::R_X86_64_RELATIVE:
1143 // These are outstanding tls relocs, which are unexpected when linking
1144 case elfcpp::R_X86_64_TPOFF64:
1145 case elfcpp::R_X86_64_DTPMOD64:
1146 case elfcpp::R_X86_64_TLSDESC:
1147 gold_error(_("%s: unexpected reloc %u in object file"),
1148 object->name().c_str(), r_type);
1151 // These are initial tls relocs, which are expected when linking
1152 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1153 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1154 case elfcpp::R_X86_64_TLSDESC_CALL:
1155 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1156 case elfcpp::R_X86_64_DTPOFF32:
1157 case elfcpp::R_X86_64_DTPOFF64:
1158 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1159 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1161 bool output_is_shared = parameters->options().shared();
1162 const tls::Tls_optimization optimized_type
1163 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1166 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1167 if (optimized_type == tls::TLSOPT_NONE)
1169 // Create a pair of GOT entries for the module index and
1170 // dtv-relative offset.
1171 Output_data_got<64, false>* got
1172 = target->got_section(symtab, layout);
1173 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1174 unsigned int shndx = lsym.get_st_shndx();
1176 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1178 object->error(_("local symbol %u has bad shndx %u"),
1181 got->add_local_pair_with_rela(object, r_sym,
1184 target->rela_dyn_section(layout),
1185 elfcpp::R_X86_64_DTPMOD64, 0);
1187 else if (optimized_type != tls::TLSOPT_TO_LE)
1188 unsupported_reloc_local(object, r_type);
1191 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1192 target->define_tls_base_symbol(symtab, layout);
1193 if (optimized_type == tls::TLSOPT_NONE)
1195 // Create reserved PLT and GOT entries for the resolver.
1196 target->reserve_tlsdesc_entries(symtab, layout);
1198 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1199 Output_data_got<64, false>* got
1200 = target->got_section(symtab, layout);
1201 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1202 unsigned int shndx = lsym.get_st_shndx();
1204 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1206 object->error(_("local symbol %u has bad shndx %u"),
1209 got->add_local_pair_with_rela(object, r_sym,
1212 target->rela_dyn_section(layout),
1213 elfcpp::R_X86_64_TLSDESC, 0);
1215 else if (optimized_type != tls::TLSOPT_TO_LE)
1216 unsupported_reloc_local(object, r_type);
1219 case elfcpp::R_X86_64_TLSDESC_CALL:
1222 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1223 if (optimized_type == tls::TLSOPT_NONE)
1225 // Create a GOT entry for the module index.
1226 target->got_mod_index_entry(symtab, layout, object);
1228 else if (optimized_type != tls::TLSOPT_TO_LE)
1229 unsupported_reloc_local(object, r_type);
1232 case elfcpp::R_X86_64_DTPOFF32:
1233 case elfcpp::R_X86_64_DTPOFF64:
1236 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1237 layout->set_has_static_tls();
1238 if (optimized_type == tls::TLSOPT_NONE)
1240 // Create a GOT entry for the tp-relative offset.
1241 Output_data_got<64, false>* got
1242 = target->got_section(symtab, layout);
1243 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1244 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1245 target->rela_dyn_section(layout),
1246 elfcpp::R_X86_64_TPOFF64);
1248 else if (optimized_type != tls::TLSOPT_TO_LE)
1249 unsupported_reloc_local(object, r_type);
1252 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1253 layout->set_has_static_tls();
1254 if (output_is_shared)
1255 unsupported_reloc_local(object, r_type);
1264 case elfcpp::R_X86_64_SIZE32:
1265 case elfcpp::R_X86_64_SIZE64:
1267 gold_error(_("%s: unsupported reloc %u against local symbol"),
1268 object->name().c_str(), r_type);
1274 // Report an unsupported relocation against a global symbol.
1277 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1278 unsigned int r_type,
1281 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1282 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1285 // Scan a relocation for a global symbol.
1288 Target_x86_64::Scan::global(Symbol_table* symtab,
1290 Target_x86_64* target,
1291 Sized_relobj<64, false>* object,
1292 unsigned int data_shndx,
1293 Output_section* output_section,
1294 const elfcpp::Rela<64, false>& reloc,
1295 unsigned int r_type,
1300 case elfcpp::R_X86_64_NONE:
1301 case elfcpp::R_386_GNU_VTINHERIT:
1302 case elfcpp::R_386_GNU_VTENTRY:
1305 case elfcpp::R_X86_64_64:
1306 case elfcpp::R_X86_64_32:
1307 case elfcpp::R_X86_64_32S:
1308 case elfcpp::R_X86_64_16:
1309 case elfcpp::R_X86_64_8:
1311 // Make a PLT entry if necessary.
1312 if (gsym->needs_plt_entry())
1314 target->make_plt_entry(symtab, layout, gsym);
1315 // Since this is not a PC-relative relocation, we may be
1316 // taking the address of a function. In that case we need to
1317 // set the entry in the dynamic symbol table to the address of
1319 if (gsym->is_from_dynobj() && !parameters->options().shared())
1320 gsym->set_needs_dynsym_value();
1322 // Make a dynamic relocation if necessary.
1323 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1325 if (gsym->may_need_copy_reloc())
1327 target->copy_reloc(symtab, layout, object,
1328 data_shndx, output_section, gsym, reloc);
1330 else if (r_type == elfcpp::R_X86_64_64
1331 && gsym->can_use_relative_reloc(false))
1333 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1334 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1335 output_section, object,
1336 data_shndx, reloc.get_r_offset(),
1337 reloc.get_r_addend());
1341 this->check_non_pic(object, r_type);
1342 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1343 rela_dyn->add_global(gsym, r_type, output_section, object,
1344 data_shndx, reloc.get_r_offset(),
1345 reloc.get_r_addend());
1351 case elfcpp::R_X86_64_PC64:
1352 case elfcpp::R_X86_64_PC32:
1353 case elfcpp::R_X86_64_PC16:
1354 case elfcpp::R_X86_64_PC8:
1356 // Make a PLT entry if necessary.
1357 if (gsym->needs_plt_entry())
1358 target->make_plt_entry(symtab, layout, gsym);
1359 // Make a dynamic relocation if necessary.
1360 int flags = Symbol::NON_PIC_REF;
1361 if (gsym->is_func())
1362 flags |= Symbol::FUNCTION_CALL;
1363 if (gsym->needs_dynamic_reloc(flags))
1365 if (gsym->may_need_copy_reloc())
1367 target->copy_reloc(symtab, layout, object,
1368 data_shndx, output_section, gsym, reloc);
1372 this->check_non_pic(object, r_type);
1373 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1374 rela_dyn->add_global(gsym, r_type, output_section, object,
1375 data_shndx, reloc.get_r_offset(),
1376 reloc.get_r_addend());
1382 case elfcpp::R_X86_64_GOT64:
1383 case elfcpp::R_X86_64_GOT32:
1384 case elfcpp::R_X86_64_GOTPCREL64:
1385 case elfcpp::R_X86_64_GOTPCREL:
1386 case elfcpp::R_X86_64_GOTPLT64:
1388 // The symbol requires a GOT entry.
1389 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1390 if (gsym->final_value_is_known())
1391 got->add_global(gsym, GOT_TYPE_STANDARD);
1394 // If this symbol is not fully resolved, we need to add a
1395 // dynamic relocation for it.
1396 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1397 if (gsym->is_from_dynobj()
1398 || gsym->is_undefined()
1399 || gsym->is_preemptible())
1400 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1401 elfcpp::R_X86_64_GLOB_DAT);
1404 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1405 rela_dyn->add_global_relative(
1406 gsym, elfcpp::R_X86_64_RELATIVE, got,
1407 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1410 // For GOTPLT64, we also need a PLT entry (but only if the
1411 // symbol is not fully resolved).
1412 if (r_type == elfcpp::R_X86_64_GOTPLT64
1413 && !gsym->final_value_is_known())
1414 target->make_plt_entry(symtab, layout, gsym);
1418 case elfcpp::R_X86_64_PLT32:
1419 // If the symbol is fully resolved, this is just a PC32 reloc.
1420 // Otherwise we need a PLT entry.
1421 if (gsym->final_value_is_known())
1423 // If building a shared library, we can also skip the PLT entry
1424 // if the symbol is defined in the output file and is protected
1426 if (gsym->is_defined()
1427 && !gsym->is_from_dynobj()
1428 && !gsym->is_preemptible())
1430 target->make_plt_entry(symtab, layout, gsym);
1433 case elfcpp::R_X86_64_GOTPC32:
1434 case elfcpp::R_X86_64_GOTOFF64:
1435 case elfcpp::R_X86_64_GOTPC64:
1436 case elfcpp::R_X86_64_PLTOFF64:
1437 // We need a GOT section.
1438 target->got_section(symtab, layout);
1439 // For PLTOFF64, we also need a PLT entry (but only if the
1440 // symbol is not fully resolved).
1441 if (r_type == elfcpp::R_X86_64_PLTOFF64
1442 && !gsym->final_value_is_known())
1443 target->make_plt_entry(symtab, layout, gsym);
1446 case elfcpp::R_X86_64_COPY:
1447 case elfcpp::R_X86_64_GLOB_DAT:
1448 case elfcpp::R_X86_64_JUMP_SLOT:
1449 case elfcpp::R_X86_64_RELATIVE:
1450 // These are outstanding tls relocs, which are unexpected when linking
1451 case elfcpp::R_X86_64_TPOFF64:
1452 case elfcpp::R_X86_64_DTPMOD64:
1453 case elfcpp::R_X86_64_TLSDESC:
1454 gold_error(_("%s: unexpected reloc %u in object file"),
1455 object->name().c_str(), r_type);
1458 // These are initial tls relocs, which are expected for global()
1459 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1460 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1461 case elfcpp::R_X86_64_TLSDESC_CALL:
1462 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1463 case elfcpp::R_X86_64_DTPOFF32:
1464 case elfcpp::R_X86_64_DTPOFF64:
1465 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1466 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1468 const bool is_final = gsym->final_value_is_known();
1469 const tls::Tls_optimization optimized_type
1470 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1473 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1474 if (optimized_type == tls::TLSOPT_NONE)
1476 // Create a pair of GOT entries for the module index and
1477 // dtv-relative offset.
1478 Output_data_got<64, false>* got
1479 = target->got_section(symtab, layout);
1480 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1481 target->rela_dyn_section(layout),
1482 elfcpp::R_X86_64_DTPMOD64,
1483 elfcpp::R_X86_64_DTPOFF64);
1485 else if (optimized_type == tls::TLSOPT_TO_IE)
1487 // Create a GOT entry for the tp-relative offset.
1488 Output_data_got<64, false>* got
1489 = target->got_section(symtab, layout);
1490 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1491 target->rela_dyn_section(layout),
1492 elfcpp::R_X86_64_TPOFF64);
1494 else if (optimized_type != tls::TLSOPT_TO_LE)
1495 unsupported_reloc_global(object, r_type, gsym);
1498 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1499 target->define_tls_base_symbol(symtab, layout);
1500 if (optimized_type == tls::TLSOPT_NONE)
1502 // Create reserved PLT and GOT entries for the resolver.
1503 target->reserve_tlsdesc_entries(symtab, layout);
1505 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1506 Output_data_got<64, false>* got
1507 = target->got_section(symtab, layout);
1508 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1509 target->rela_dyn_section(layout),
1510 elfcpp::R_X86_64_TLSDESC, 0);
1512 else if (optimized_type == tls::TLSOPT_TO_IE)
1514 // Create a GOT entry for the tp-relative offset.
1515 Output_data_got<64, false>* got
1516 = target->got_section(symtab, layout);
1517 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1518 target->rela_dyn_section(layout),
1519 elfcpp::R_X86_64_TPOFF64);
1521 else if (optimized_type != tls::TLSOPT_TO_LE)
1522 unsupported_reloc_global(object, r_type, gsym);
1525 case elfcpp::R_X86_64_TLSDESC_CALL:
1528 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1529 if (optimized_type == tls::TLSOPT_NONE)
1531 // Create a GOT entry for the module index.
1532 target->got_mod_index_entry(symtab, layout, object);
1534 else if (optimized_type != tls::TLSOPT_TO_LE)
1535 unsupported_reloc_global(object, r_type, gsym);
1538 case elfcpp::R_X86_64_DTPOFF32:
1539 case elfcpp::R_X86_64_DTPOFF64:
1542 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1543 layout->set_has_static_tls();
1544 if (optimized_type == tls::TLSOPT_NONE)
1546 // Create a GOT entry for the tp-relative offset.
1547 Output_data_got<64, false>* got
1548 = target->got_section(symtab, layout);
1549 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1550 target->rela_dyn_section(layout),
1551 elfcpp::R_X86_64_TPOFF64);
1553 else if (optimized_type != tls::TLSOPT_TO_LE)
1554 unsupported_reloc_global(object, r_type, gsym);
1557 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1558 layout->set_has_static_tls();
1559 if (parameters->options().shared())
1560 unsupported_reloc_local(object, r_type);
1569 case elfcpp::R_X86_64_SIZE32:
1570 case elfcpp::R_X86_64_SIZE64:
1572 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1573 object->name().c_str(), r_type,
1574 gsym->demangled_name().c_str());
1580 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
1582 Sized_relobj<64, false>* object,
1583 unsigned int data_shndx,
1584 unsigned int sh_type,
1585 const unsigned char* prelocs,
1587 Output_section* output_section,
1588 bool needs_special_offset_handling,
1589 size_t local_symbol_count,
1590 const unsigned char* plocal_symbols)
1593 if (sh_type == elfcpp::SHT_REL)
1598 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1599 Target_x86_64::Scan>(
1608 needs_special_offset_handling,
1613 // Scan relocations for a section.
1616 Target_x86_64::scan_relocs(Symbol_table* symtab,
1618 Sized_relobj<64, false>* object,
1619 unsigned int data_shndx,
1620 unsigned int sh_type,
1621 const unsigned char* prelocs,
1623 Output_section* output_section,
1624 bool needs_special_offset_handling,
1625 size_t local_symbol_count,
1626 const unsigned char* plocal_symbols)
1628 if (sh_type == elfcpp::SHT_REL)
1630 gold_error(_("%s: unsupported REL reloc section"),
1631 object->name().c_str());
1635 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1636 Target_x86_64::Scan>(
1645 needs_special_offset_handling,
1650 // Finalize the sections.
1653 Target_x86_64::do_finalize_sections(
1655 const Input_objects*,
1656 Symbol_table* symtab)
1658 const Reloc_section* rel_plt = (this->plt_ == NULL
1660 : this->plt_->rel_plt());
1661 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
1662 this->rela_dyn_, true);
1664 // Fill in some more dynamic tags.
1665 Output_data_dynamic* const odyn = layout->dynamic_data();
1668 if (this->plt_ != NULL
1669 && this->plt_->output_section() != NULL
1670 && 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 // Emit any relocs we saved in an attempt to avoid generating COPY
1684 if (this->copy_relocs_.any_saved_relocs())
1685 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1687 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1688 // the .got.plt section.
1689 Symbol* sym = this->global_offset_table_;
1692 uint64_t data_size = this->got_plt_->current_data_size();
1693 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
1697 // Perform a relocation.
1700 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1701 Target_x86_64* target,
1704 const elfcpp::Rela<64, false>& rela,
1705 unsigned int r_type,
1706 const Sized_symbol<64>* gsym,
1707 const Symbol_value<64>* psymval,
1708 unsigned char* view,
1709 elfcpp::Elf_types<64>::Elf_Addr address,
1710 section_size_type view_size)
1712 if (this->skip_call_tls_get_addr_)
1714 if ((r_type != elfcpp::R_X86_64_PLT32
1715 && r_type != elfcpp::R_X86_64_PC32)
1717 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1719 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1720 _("missing expected TLS relocation"));
1724 this->skip_call_tls_get_addr_ = false;
1729 // Pick the value to use for symbols defined in shared objects.
1730 Symbol_value<64> symval;
1732 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1733 || r_type == elfcpp::R_X86_64_PC32
1734 || r_type == elfcpp::R_X86_64_PC16
1735 || r_type == elfcpp::R_X86_64_PC8))
1737 symval.set_output_value(target->plt_section()->address()
1738 + gsym->plt_offset());
1742 const Sized_relobj<64, false>* object = relinfo->object;
1743 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1745 // Get the GOT offset if needed.
1746 // The GOT pointer points to the end of the GOT section.
1747 // We need to subtract the size of the GOT section to get
1748 // the actual offset to use in the relocation.
1749 bool have_got_offset = false;
1750 unsigned int got_offset = 0;
1753 case elfcpp::R_X86_64_GOT32:
1754 case elfcpp::R_X86_64_GOT64:
1755 case elfcpp::R_X86_64_GOTPLT64:
1756 case elfcpp::R_X86_64_GOTPCREL:
1757 case elfcpp::R_X86_64_GOTPCREL64:
1760 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1761 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1765 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1766 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1767 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1768 - target->got_size());
1770 have_got_offset = true;
1779 case elfcpp::R_X86_64_NONE:
1780 case elfcpp::R_386_GNU_VTINHERIT:
1781 case elfcpp::R_386_GNU_VTENTRY:
1784 case elfcpp::R_X86_64_64:
1785 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1788 case elfcpp::R_X86_64_PC64:
1789 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1793 case elfcpp::R_X86_64_32:
1794 // FIXME: we need to verify that value + addend fits into 32 bits:
1795 // uint64_t x = value + addend;
1796 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1797 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1798 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1801 case elfcpp::R_X86_64_32S:
1802 // FIXME: we need to verify that value + addend fits into 32 bits:
1803 // int64_t x = value + addend; // note this quantity is signed!
1804 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1805 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1808 case elfcpp::R_X86_64_PC32:
1809 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1813 case elfcpp::R_X86_64_16:
1814 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1817 case elfcpp::R_X86_64_PC16:
1818 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1822 case elfcpp::R_X86_64_8:
1823 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1826 case elfcpp::R_X86_64_PC8:
1827 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1831 case elfcpp::R_X86_64_PLT32:
1832 gold_assert(gsym == NULL
1833 || gsym->has_plt_offset()
1834 || gsym->final_value_is_known()
1835 || (gsym->is_defined()
1836 && !gsym->is_from_dynobj()
1837 && !gsym->is_preemptible()));
1838 // Note: while this code looks the same as for R_X86_64_PC32, it
1839 // behaves differently because psymval was set to point to
1840 // the PLT entry, rather than the symbol, in Scan::global().
1841 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1845 case elfcpp::R_X86_64_PLTOFF64:
1848 gold_assert(gsym->has_plt_offset()
1849 || gsym->final_value_is_known());
1850 elfcpp::Elf_types<64>::Elf_Addr got_address;
1851 got_address = target->got_section(NULL, NULL)->address();
1852 Relocate_functions<64, false>::rela64(view, object, psymval,
1853 addend - got_address);
1856 case elfcpp::R_X86_64_GOT32:
1857 gold_assert(have_got_offset);
1858 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1861 case elfcpp::R_X86_64_GOTPC32:
1864 elfcpp::Elf_types<64>::Elf_Addr value;
1865 value = target->got_plt_section()->address();
1866 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1870 case elfcpp::R_X86_64_GOT64:
1871 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1872 // Since we always add a PLT entry, this is equivalent.
1873 case elfcpp::R_X86_64_GOTPLT64:
1874 gold_assert(have_got_offset);
1875 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1878 case elfcpp::R_X86_64_GOTPC64:
1881 elfcpp::Elf_types<64>::Elf_Addr value;
1882 value = target->got_plt_section()->address();
1883 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1887 case elfcpp::R_X86_64_GOTOFF64:
1889 elfcpp::Elf_types<64>::Elf_Addr value;
1890 value = (psymval->value(object, 0)
1891 - target->got_plt_section()->address());
1892 Relocate_functions<64, false>::rela64(view, value, addend);
1896 case elfcpp::R_X86_64_GOTPCREL:
1898 gold_assert(have_got_offset);
1899 elfcpp::Elf_types<64>::Elf_Addr value;
1900 value = target->got_plt_section()->address() + got_offset;
1901 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1905 case elfcpp::R_X86_64_GOTPCREL64:
1907 gold_assert(have_got_offset);
1908 elfcpp::Elf_types<64>::Elf_Addr value;
1909 value = target->got_plt_section()->address() + got_offset;
1910 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1914 case elfcpp::R_X86_64_COPY:
1915 case elfcpp::R_X86_64_GLOB_DAT:
1916 case elfcpp::R_X86_64_JUMP_SLOT:
1917 case elfcpp::R_X86_64_RELATIVE:
1918 // These are outstanding tls relocs, which are unexpected when linking
1919 case elfcpp::R_X86_64_TPOFF64:
1920 case elfcpp::R_X86_64_DTPMOD64:
1921 case elfcpp::R_X86_64_TLSDESC:
1922 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1923 _("unexpected reloc %u in object file"),
1927 // These are initial tls relocs, which are expected when linking
1928 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1929 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1930 case elfcpp::R_X86_64_TLSDESC_CALL:
1931 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1932 case elfcpp::R_X86_64_DTPOFF32:
1933 case elfcpp::R_X86_64_DTPOFF64:
1934 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1935 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1936 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1937 view, address, view_size);
1940 case elfcpp::R_X86_64_SIZE32:
1941 case elfcpp::R_X86_64_SIZE64:
1943 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1944 _("unsupported reloc %u"),
1952 // Perform a TLS relocation.
1955 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1956 Target_x86_64* target,
1958 const elfcpp::Rela<64, false>& rela,
1959 unsigned int r_type,
1960 const Sized_symbol<64>* gsym,
1961 const Symbol_value<64>* psymval,
1962 unsigned char* view,
1963 elfcpp::Elf_types<64>::Elf_Addr address,
1964 section_size_type view_size)
1966 Output_segment* tls_segment = relinfo->layout->tls_segment();
1968 const Sized_relobj<64, false>* object = relinfo->object;
1969 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1971 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1973 const bool is_final = (gsym == NULL
1974 ? !parameters->options().output_is_position_independent()
1975 : gsym->final_value_is_known());
1976 const tls::Tls_optimization optimized_type
1977 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1980 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1981 this->saw_tls_block_reloc_ = true;
1982 if (optimized_type == tls::TLSOPT_TO_LE)
1984 gold_assert(tls_segment != NULL);
1985 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1986 rela, r_type, value, view,
1992 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1993 ? GOT_TYPE_TLS_OFFSET
1994 : GOT_TYPE_TLS_PAIR);
1995 unsigned int got_offset;
1998 gold_assert(gsym->has_got_offset(got_type));
1999 got_offset = gsym->got_offset(got_type) - target->got_size();
2003 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2004 gold_assert(object->local_has_got_offset(r_sym, got_type));
2005 got_offset = (object->local_got_offset(r_sym, got_type)
2006 - target->got_size());
2008 if (optimized_type == tls::TLSOPT_TO_IE)
2010 gold_assert(tls_segment != NULL);
2011 value = target->got_plt_section()->address() + got_offset;
2012 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2013 value, view, address, view_size);
2016 else if (optimized_type == tls::TLSOPT_NONE)
2018 // Relocate the field with the offset of the pair of GOT
2020 value = target->got_plt_section()->address() + got_offset;
2021 Relocate_functions<64, false>::pcrela32(view, value, addend,
2026 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2027 _("unsupported reloc %u"), r_type);
2030 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2031 case elfcpp::R_X86_64_TLSDESC_CALL:
2032 this->saw_tls_block_reloc_ = true;
2033 if (optimized_type == tls::TLSOPT_TO_LE)
2035 gold_assert(tls_segment != NULL);
2036 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2037 rela, r_type, value, view,
2043 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2044 ? GOT_TYPE_TLS_OFFSET
2045 : GOT_TYPE_TLS_DESC);
2046 unsigned int got_offset;
2049 gold_assert(gsym->has_got_offset(got_type));
2050 got_offset = gsym->got_offset(got_type) - target->got_size();
2054 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2055 gold_assert(object->local_has_got_offset(r_sym, got_type));
2056 got_offset = (object->local_got_offset(r_sym, got_type)
2057 - target->got_size());
2059 if (optimized_type == tls::TLSOPT_TO_IE)
2061 gold_assert(tls_segment != NULL);
2062 value = target->got_plt_section()->address() + got_offset;
2063 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2064 rela, r_type, value, view, address,
2068 else if (optimized_type == tls::TLSOPT_NONE)
2070 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2072 // Relocate the field with the offset of the pair of GOT
2074 value = target->got_plt_section()->address() + got_offset;
2075 Relocate_functions<64, false>::pcrela32(view, value, addend,
2081 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2082 _("unsupported reloc %u"), r_type);
2085 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2086 this->saw_tls_block_reloc_ = true;
2087 if (optimized_type == tls::TLSOPT_TO_LE)
2089 gold_assert(tls_segment != NULL);
2090 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2091 value, view, view_size);
2094 else if (optimized_type == tls::TLSOPT_NONE)
2096 // Relocate the field with the offset of the GOT entry for
2097 // the module index.
2098 unsigned int got_offset;
2099 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2100 - target->got_size());
2101 value = target->got_plt_section()->address() + got_offset;
2102 Relocate_functions<64, false>::pcrela32(view, value, addend,
2106 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2107 _("unsupported reloc %u"), r_type);
2110 case elfcpp::R_X86_64_DTPOFF32:
2111 if (optimized_type == tls::TLSOPT_TO_LE)
2113 // This relocation type is used in debugging information.
2114 // In that case we need to not optimize the value. If we
2115 // haven't seen a TLSLD reloc, then we assume we should not
2116 // optimize this reloc.
2117 if (this->saw_tls_block_reloc_)
2119 gold_assert(tls_segment != NULL);
2120 value -= tls_segment->memsz();
2123 Relocate_functions<64, false>::rela32(view, value, addend);
2126 case elfcpp::R_X86_64_DTPOFF64:
2127 if (optimized_type == tls::TLSOPT_TO_LE)
2129 // See R_X86_64_DTPOFF32, just above, for why we test this.
2130 if (this->saw_tls_block_reloc_)
2132 gold_assert(tls_segment != NULL);
2133 value -= tls_segment->memsz();
2136 Relocate_functions<64, false>::rela64(view, value, addend);
2139 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2140 if (optimized_type == tls::TLSOPT_TO_LE)
2142 gold_assert(tls_segment != NULL);
2143 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2144 rela, r_type, value, view,
2148 else if (optimized_type == tls::TLSOPT_NONE)
2150 // Relocate the field with the offset of the GOT entry for
2151 // the tp-relative offset of the symbol.
2152 unsigned int got_offset;
2155 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2156 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2157 - target->got_size());
2161 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2162 gold_assert(object->local_has_got_offset(r_sym,
2163 GOT_TYPE_TLS_OFFSET));
2164 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2165 - target->got_size());
2167 value = target->got_plt_section()->address() + got_offset;
2168 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2171 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2172 _("unsupported reloc type %u"),
2176 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2177 value -= tls_segment->memsz();
2178 Relocate_functions<64, false>::rela32(view, value, addend);
2183 // Do a relocation in which we convert a TLS General-Dynamic to an
2187 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2190 const elfcpp::Rela<64, false>& rela,
2192 elfcpp::Elf_types<64>::Elf_Addr value,
2193 unsigned char* view,
2194 elfcpp::Elf_types<64>::Elf_Addr address,
2195 section_size_type view_size)
2197 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2198 // .word 0x6666; rex64; call __tls_get_addr
2199 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2201 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2202 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2204 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2205 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2206 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2207 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2209 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2211 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2212 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2214 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2216 this->skip_call_tls_get_addr_ = true;
2219 // Do a relocation in which we convert a TLS General-Dynamic to a
2223 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2225 Output_segment* tls_segment,
2226 const elfcpp::Rela<64, false>& rela,
2228 elfcpp::Elf_types<64>::Elf_Addr value,
2229 unsigned char* view,
2230 section_size_type view_size)
2232 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2233 // .word 0x6666; rex64; call __tls_get_addr
2234 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2236 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2237 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2239 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2240 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2241 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2242 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2244 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2246 value -= tls_segment->memsz();
2247 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2249 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2251 this->skip_call_tls_get_addr_ = true;
2254 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2257 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2258 const Relocate_info<64, false>* relinfo,
2261 const elfcpp::Rela<64, false>& rela,
2262 unsigned int r_type,
2263 elfcpp::Elf_types<64>::Elf_Addr value,
2264 unsigned char* view,
2265 elfcpp::Elf_types<64>::Elf_Addr address,
2266 section_size_type view_size)
2268 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2270 // leaq foo@tlsdesc(%rip), %rax
2271 // ==> movq foo@gottpoff(%rip), %rax
2272 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2273 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2274 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2275 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2277 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2278 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2282 // call *foo@tlscall(%rax)
2284 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2285 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2286 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2287 view[0] == 0xff && view[1] == 0x10);
2293 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2296 Target_x86_64::Relocate::tls_desc_gd_to_le(
2297 const Relocate_info<64, false>* relinfo,
2299 Output_segment* tls_segment,
2300 const elfcpp::Rela<64, false>& rela,
2301 unsigned int r_type,
2302 elfcpp::Elf_types<64>::Elf_Addr value,
2303 unsigned char* view,
2304 section_size_type view_size)
2306 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2308 // leaq foo@tlsdesc(%rip), %rax
2309 // ==> movq foo@tpoff, %rax
2310 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2311 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2312 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2313 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2316 value -= tls_segment->memsz();
2317 Relocate_functions<64, false>::rela32(view, value, 0);
2321 // call *foo@tlscall(%rax)
2323 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2324 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2325 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2326 view[0] == 0xff && view[1] == 0x10);
2333 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2336 const elfcpp::Rela<64, false>& rela,
2338 elfcpp::Elf_types<64>::Elf_Addr,
2339 unsigned char* view,
2340 section_size_type view_size)
2342 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2343 // ... leq foo@dtpoff(%rax),%reg
2344 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2346 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2347 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2349 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2350 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2352 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2354 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2356 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2358 this->skip_call_tls_get_addr_ = true;
2361 // Do a relocation in which we convert a TLS Initial-Exec to a
2365 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2367 Output_segment* tls_segment,
2368 const elfcpp::Rela<64, false>& rela,
2370 elfcpp::Elf_types<64>::Elf_Addr value,
2371 unsigned char* view,
2372 section_size_type view_size)
2374 // We need to examine the opcodes to figure out which instruction we
2377 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2378 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2380 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2381 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2383 unsigned char op1 = view[-3];
2384 unsigned char op2 = view[-2];
2385 unsigned char op3 = view[-1];
2386 unsigned char reg = op3 >> 3;
2394 view[-1] = 0xc0 | reg;
2398 // Special handling for %rsp.
2402 view[-1] = 0xc0 | reg;
2410 view[-1] = 0x80 | reg | (reg << 3);
2413 value -= tls_segment->memsz();
2414 Relocate_functions<64, false>::rela32(view, value, 0);
2417 // Relocate section data.
2420 Target_x86_64::relocate_section(
2421 const Relocate_info<64, false>* relinfo,
2422 unsigned int sh_type,
2423 const unsigned char* prelocs,
2425 Output_section* output_section,
2426 bool needs_special_offset_handling,
2427 unsigned char* view,
2428 elfcpp::Elf_types<64>::Elf_Addr address,
2429 section_size_type view_size,
2430 const Reloc_symbol_changes* reloc_symbol_changes)
2432 gold_assert(sh_type == elfcpp::SHT_RELA);
2434 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2435 Target_x86_64::Relocate>(
2441 needs_special_offset_handling,
2445 reloc_symbol_changes);
2448 // Return the size of a relocation while scanning during a relocatable
2452 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2453 unsigned int r_type,
2458 case elfcpp::R_X86_64_NONE:
2459 case elfcpp::R_386_GNU_VTINHERIT:
2460 case elfcpp::R_386_GNU_VTENTRY:
2461 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2462 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2463 case elfcpp::R_X86_64_TLSDESC_CALL:
2464 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2465 case elfcpp::R_X86_64_DTPOFF32:
2466 case elfcpp::R_X86_64_DTPOFF64:
2467 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2468 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2471 case elfcpp::R_X86_64_64:
2472 case elfcpp::R_X86_64_PC64:
2473 case elfcpp::R_X86_64_GOTOFF64:
2474 case elfcpp::R_X86_64_GOTPC64:
2475 case elfcpp::R_X86_64_PLTOFF64:
2476 case elfcpp::R_X86_64_GOT64:
2477 case elfcpp::R_X86_64_GOTPCREL64:
2478 case elfcpp::R_X86_64_GOTPCREL:
2479 case elfcpp::R_X86_64_GOTPLT64:
2482 case elfcpp::R_X86_64_32:
2483 case elfcpp::R_X86_64_32S:
2484 case elfcpp::R_X86_64_PC32:
2485 case elfcpp::R_X86_64_PLT32:
2486 case elfcpp::R_X86_64_GOTPC32:
2487 case elfcpp::R_X86_64_GOT32:
2490 case elfcpp::R_X86_64_16:
2491 case elfcpp::R_X86_64_PC16:
2494 case elfcpp::R_X86_64_8:
2495 case elfcpp::R_X86_64_PC8:
2498 case elfcpp::R_X86_64_COPY:
2499 case elfcpp::R_X86_64_GLOB_DAT:
2500 case elfcpp::R_X86_64_JUMP_SLOT:
2501 case elfcpp::R_X86_64_RELATIVE:
2502 // These are outstanding tls relocs, which are unexpected when linking
2503 case elfcpp::R_X86_64_TPOFF64:
2504 case elfcpp::R_X86_64_DTPMOD64:
2505 case elfcpp::R_X86_64_TLSDESC:
2506 object->error(_("unexpected reloc %u in object file"), r_type);
2509 case elfcpp::R_X86_64_SIZE32:
2510 case elfcpp::R_X86_64_SIZE64:
2512 object->error(_("unsupported reloc %u against local symbol"), r_type);
2517 // Scan the relocs during a relocatable link.
2520 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
2522 Sized_relobj<64, false>* object,
2523 unsigned int data_shndx,
2524 unsigned int sh_type,
2525 const unsigned char* prelocs,
2527 Output_section* output_section,
2528 bool needs_special_offset_handling,
2529 size_t local_symbol_count,
2530 const unsigned char* plocal_symbols,
2531 Relocatable_relocs* rr)
2533 gold_assert(sh_type == elfcpp::SHT_RELA);
2535 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2536 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2538 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2539 Scan_relocatable_relocs>(
2547 needs_special_offset_handling,
2553 // Relocate a section during a relocatable link.
2556 Target_x86_64::relocate_for_relocatable(
2557 const Relocate_info<64, false>* relinfo,
2558 unsigned int sh_type,
2559 const unsigned char* prelocs,
2561 Output_section* output_section,
2562 off_t offset_in_output_section,
2563 const Relocatable_relocs* rr,
2564 unsigned char* view,
2565 elfcpp::Elf_types<64>::Elf_Addr view_address,
2566 section_size_type view_size,
2567 unsigned char* reloc_view,
2568 section_size_type reloc_view_size)
2570 gold_assert(sh_type == elfcpp::SHT_RELA);
2572 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2577 offset_in_output_section,
2586 // Return the value to use for a dynamic which requires special
2587 // treatment. This is how we support equality comparisons of function
2588 // pointers across shared library boundaries, as described in the
2589 // processor specific ABI supplement.
2592 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2594 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2595 return this->plt_section()->address() + gsym->plt_offset();
2598 // Return a string used to fill a code section with nops to take up
2599 // the specified length.
2602 Target_x86_64::do_code_fill(section_size_type length) const
2606 // Build a jmpq instruction to skip over the bytes.
2607 unsigned char jmp[5];
2609 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2610 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2611 + std::string(length - 5, '\0'));
2614 // Nop sequences of various lengths.
2615 const char nop1[1] = { 0x90 }; // nop
2616 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2617 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2618 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2619 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2621 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2623 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2625 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2626 0x00, 0x00, 0x00, 0x00 };
2627 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2628 0x00, 0x00, 0x00, 0x00,
2630 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2631 0x84, 0x00, 0x00, 0x00,
2633 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2634 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2636 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2637 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2638 0x00, 0x00, 0x00, 0x00 };
2639 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2640 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2641 0x00, 0x00, 0x00, 0x00,
2643 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2644 0x66, 0x2e, 0x0f, 0x1f, // data16
2645 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2647 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2648 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2649 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2652 const char* nops[16] = {
2654 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2655 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2658 return std::string(nops[length], length);
2661 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2662 // compiled with -fstack-split. The function calls non-stack-split
2663 // code. We have to change the function so that it always ensures
2664 // that it has enough stack space to run some random function.
2667 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2668 section_offset_type fnoffset,
2669 section_size_type fnsize,
2670 unsigned char* view,
2671 section_size_type view_size,
2673 std::string* to) const
2675 // The function starts with a comparison of the stack pointer and a
2676 // field in the TCB. This is followed by a jump.
2679 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2682 // We will call __morestack if the carry flag is set after this
2683 // comparison. We turn the comparison into an stc instruction
2685 view[fnoffset] = '\xf9';
2686 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2688 // lea NN(%rsp),%r10
2689 // lea NN(%rsp),%r11
2690 else if ((this->match_view(view, view_size, fnoffset,
2691 "\x4c\x8d\x94\x24", 4)
2692 || this->match_view(view, view_size, fnoffset,
2693 "\x4c\x8d\x9c\x24", 4))
2696 // This is loading an offset from the stack pointer for a
2697 // comparison. The offset is negative, so we decrease the
2698 // offset by the amount of space we need for the stack. This
2699 // means we will avoid calling __morestack if there happens to
2700 // be plenty of space on the stack already.
2701 unsigned char* pval = view + fnoffset + 4;
2702 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2703 val -= parameters->options().split_stack_adjust_size();
2704 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2708 if (!object->has_no_split_stack())
2709 object->error(_("failed to match split-stack sequence at "
2710 "section %u offset %0zx"),
2711 shndx, static_cast<size_t>(fnoffset));
2715 // We have to change the function so that it calls
2716 // __morestack_non_split instead of __morestack. The former will
2717 // allocate additional stack space.
2718 *from = "__morestack";
2719 *to = "__morestack_non_split";
2722 // The selector for x86_64 object files.
2724 class Target_selector_x86_64 : public Target_selector_freebsd
2727 Target_selector_x86_64()
2728 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2729 "elf64-x86-64-freebsd")
2733 do_instantiate_target()
2734 { return new Target_x86_64(); }
2738 Target_selector_x86_64 target_selector_x86_64;
2740 } // End anonymous namespace.