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);
125 // Scan the relocs during a relocatable link.
127 scan_relocatable_relocs(const General_options& options,
128 Symbol_table* symtab,
130 Sized_relobj<64, false>* object,
131 unsigned int data_shndx,
132 unsigned int sh_type,
133 const unsigned char* prelocs,
135 Output_section* output_section,
136 bool needs_special_offset_handling,
137 size_t local_symbol_count,
138 const unsigned char* plocal_symbols,
139 Relocatable_relocs*);
141 // Relocate a section during a relocatable link.
143 relocate_for_relocatable(const Relocate_info<64, false>*,
144 unsigned int sh_type,
145 const unsigned char* prelocs,
147 Output_section* output_section,
148 off_t offset_in_output_section,
149 const Relocatable_relocs*,
151 elfcpp::Elf_types<64>::Elf_Addr view_address,
152 section_size_type view_size,
153 unsigned char* reloc_view,
154 section_size_type reloc_view_size);
156 // Return a string used to fill a code section with nops.
158 do_code_fill(section_size_type length) const;
160 // Return whether SYM is defined by the ABI.
162 do_is_defined_by_abi(const Symbol* sym) const
163 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
165 // Return the size of the GOT section.
169 gold_assert(this->got_ != NULL);
170 return this->got_->data_size();
174 // The class which scans relocations.
179 : issued_non_pic_error_(false)
183 local(const General_options& options, Symbol_table* symtab,
184 Layout* layout, Target_x86_64* target,
185 Sized_relobj<64, false>* object,
186 unsigned int data_shndx,
187 Output_section* output_section,
188 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
189 const elfcpp::Sym<64, false>& lsym);
192 global(const General_options& options, Symbol_table* symtab,
193 Layout* layout, Target_x86_64* target,
194 Sized_relobj<64, false>* object,
195 unsigned int data_shndx,
196 Output_section* output_section,
197 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
202 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
205 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
209 check_non_pic(Relobj*, unsigned int r_type);
211 // Whether we have issued an error about a non-PIC compilation.
212 bool issued_non_pic_error_;
215 // The class which implements relocation.
220 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
225 if (this->skip_call_tls_get_addr_)
227 // FIXME: This needs to specify the location somehow.
228 gold_error(_("missing expected TLS relocation"));
232 // Do a relocation. Return false if the caller should not issue
233 // any warnings about this relocation.
235 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
236 size_t relnum, const elfcpp::Rela<64, false>&,
237 unsigned int r_type, const Sized_symbol<64>*,
238 const Symbol_value<64>*,
239 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
243 // Do a TLS relocation.
245 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
246 size_t relnum, const elfcpp::Rela<64, false>&,
247 unsigned int r_type, const Sized_symbol<64>*,
248 const Symbol_value<64>*,
249 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
252 // Do a TLS General-Dynamic to Initial-Exec transition.
254 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
255 Output_segment* tls_segment,
256 const elfcpp::Rela<64, false>&, unsigned int r_type,
257 elfcpp::Elf_types<64>::Elf_Addr value,
259 elfcpp::Elf_types<64>::Elf_Addr,
260 section_size_type view_size);
262 // Do a TLS General-Dynamic to Local-Exec transition.
264 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
265 Output_segment* tls_segment,
266 const elfcpp::Rela<64, false>&, unsigned int r_type,
267 elfcpp::Elf_types<64>::Elf_Addr value,
269 section_size_type view_size);
271 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
273 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
274 Output_segment* tls_segment,
275 const elfcpp::Rela<64, false>&, unsigned int r_type,
276 elfcpp::Elf_types<64>::Elf_Addr value,
278 elfcpp::Elf_types<64>::Elf_Addr,
279 section_size_type view_size);
281 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
283 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
284 Output_segment* tls_segment,
285 const elfcpp::Rela<64, false>&, unsigned int r_type,
286 elfcpp::Elf_types<64>::Elf_Addr value,
288 section_size_type view_size);
290 // Do a TLS Local-Dynamic to Local-Exec transition.
292 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
293 Output_segment* tls_segment,
294 const elfcpp::Rela<64, false>&, unsigned int r_type,
295 elfcpp::Elf_types<64>::Elf_Addr value,
297 section_size_type view_size);
299 // Do a TLS Initial-Exec to Local-Exec transition.
301 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
302 Output_segment* tls_segment,
303 const elfcpp::Rela<64, false>&, unsigned int r_type,
304 elfcpp::Elf_types<64>::Elf_Addr value,
306 section_size_type view_size);
308 // This is set if we should skip the next reloc, which should be a
309 // PLT32 reloc against ___tls_get_addr.
310 bool skip_call_tls_get_addr_;
312 // This is set if we see a relocation which could load the address
313 // of the TLS block. Whether we see such a relocation determines
314 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
315 // in debugging sections.
316 bool saw_tls_block_reloc_;
319 // A class which returns the size required for a relocation type,
320 // used while scanning relocs during a relocatable link.
321 class Relocatable_size_for_reloc
325 get_size_for_reloc(unsigned int, Relobj*);
328 // Adjust TLS relocation type based on the options and whether this
329 // is a local symbol.
330 static tls::Tls_optimization
331 optimize_tls_reloc(bool is_final, int r_type);
333 // Get the GOT section, creating it if necessary.
334 Output_data_got<64, false>*
335 got_section(Symbol_table*, Layout*);
337 // Get the GOT PLT section.
339 got_plt_section() const
341 gold_assert(this->got_plt_ != NULL);
342 return this->got_plt_;
345 // Create the PLT section.
347 make_plt_section(Symbol_table* symtab, Layout* layout);
349 // Create a PLT entry for a global symbol.
351 make_plt_entry(Symbol_table*, Layout*, Symbol*);
353 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
355 define_tls_base_symbol(Symbol_table*, Layout*);
357 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
359 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
361 // Create a GOT entry for the TLS module index.
363 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
364 Sized_relobj<64, false>* object);
366 // Get the PLT section.
367 Output_data_plt_x86_64*
370 gold_assert(this->plt_ != NULL);
374 // Get the dynamic reloc section, creating it if necessary.
376 rela_dyn_section(Layout*);
378 // Return true if the symbol may need a COPY relocation.
379 // References from an executable object to non-function symbols
380 // defined in a dynamic object may need a COPY relocation.
382 may_need_copy_reloc(Symbol* gsym)
384 return (!parameters->options().shared()
385 && gsym->is_from_dynobj()
386 && gsym->type() != elfcpp::STT_FUNC);
389 // Add a potential copy relocation.
391 copy_reloc(Symbol_table* symtab, Layout* layout,
392 Sized_relobj<64, false>* object,
393 unsigned int shndx, Output_section* output_section,
394 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
396 this->copy_relocs_.copy_reloc(symtab, layout,
397 symtab->get_sized_symbol<64>(sym),
398 object, shndx, output_section,
399 reloc, this->rela_dyn_section(layout));
402 // Information about this specific target which we pass to the
403 // general Target structure.
404 static const Target::Target_info x86_64_info;
408 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
409 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
410 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
411 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
415 Output_data_got<64, false>* got_;
417 Output_data_plt_x86_64* plt_;
418 // The GOT PLT section.
419 Output_data_space* got_plt_;
420 // The dynamic reloc section.
421 Reloc_section* rela_dyn_;
422 // Relocs saved to avoid a COPY reloc.
423 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
424 // Space for variables copied with a COPY reloc.
425 Output_data_space* dynbss_;
426 // Offset of the GOT entry for the TLS module index.
427 unsigned int got_mod_index_offset_;
428 // True if the _TLS_MODULE_BASE_ symbol has been defined.
429 bool tls_base_symbol_defined_;
432 const Target::Target_info Target_x86_64::x86_64_info =
435 false, // is_big_endian
436 elfcpp::EM_X86_64, // machine_code
437 false, // has_make_symbol
438 false, // has_resolve
439 true, // has_code_fill
440 true, // is_default_stack_executable
442 "/lib/ld64.so.1", // program interpreter
443 0x400000, // default_text_segment_address
444 0x1000, // abi_pagesize (overridable by -z max-page-size)
445 0x1000, // common_pagesize (overridable by -z common-page-size)
446 elfcpp::SHN_UNDEF, // small_common_shndx
447 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
448 0, // small_common_section_flags
449 elfcpp::SHF_X86_64_LARGE // large_common_section_flags
452 // This is called when a new output section is created. This is where
453 // we handle the SHF_X86_64_LARGE.
456 Target_x86_64::do_new_output_section(Output_section *os) const
458 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
459 os->set_is_large_section();
462 // Get the GOT section, creating it if necessary.
464 Output_data_got<64, false>*
465 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
467 if (this->got_ == NULL)
469 gold_assert(symtab != NULL && layout != NULL);
471 this->got_ = new Output_data_got<64, false>();
474 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
476 | elfcpp::SHF_WRITE),
480 // The old GNU linker creates a .got.plt section. We just
481 // create another set of data in the .got section. Note that we
482 // always create a PLT if we create a GOT, although the PLT
484 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
485 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
487 | elfcpp::SHF_WRITE),
491 // The first three entries are reserved.
492 this->got_plt_->set_current_data_size(3 * 8);
494 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
495 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
497 0, 0, elfcpp::STT_OBJECT,
499 elfcpp::STV_HIDDEN, 0,
506 // Get the dynamic reloc section, creating it if necessary.
508 Target_x86_64::Reloc_section*
509 Target_x86_64::rela_dyn_section(Layout* layout)
511 if (this->rela_dyn_ == NULL)
513 gold_assert(layout != NULL);
514 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
515 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
516 elfcpp::SHF_ALLOC, this->rela_dyn_);
518 return this->rela_dyn_;
521 // A class to handle the PLT data.
523 class Output_data_plt_x86_64 : public Output_section_data
526 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
528 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
531 // Add an entry to the PLT.
533 add_entry(Symbol* gsym);
535 // Add the reserved TLSDESC_PLT entry to the PLT.
537 reserve_tlsdesc_entry(unsigned int got_offset)
538 { this->tlsdesc_got_offset_ = got_offset; }
540 // Return true if a TLSDESC_PLT entry has been reserved.
542 has_tlsdesc_entry() const
543 { return this->tlsdesc_got_offset_ != -1U; }
545 // Return the GOT offset for the reserved TLSDESC_PLT entry.
547 get_tlsdesc_got_offset() const
548 { return this->tlsdesc_got_offset_; }
550 // Return the offset of the reserved TLSDESC_PLT entry.
552 get_tlsdesc_plt_offset() const
553 { return (this->count_ + 1) * plt_entry_size; }
555 // Return the .rel.plt section data.
558 { return this->rel_; }
562 do_adjust_output_section(Output_section* os);
564 // Write to a map file.
566 do_print_to_mapfile(Mapfile* mapfile) const
567 { mapfile->print_output_data(this, _("** PLT")); }
570 // The size of an entry in the PLT.
571 static const int plt_entry_size = 16;
573 // The first entry in the PLT.
574 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
575 // procedure linkage table for both programs and shared objects."
576 static unsigned char first_plt_entry[plt_entry_size];
578 // Other entries in the PLT for an executable.
579 static unsigned char plt_entry[plt_entry_size];
581 // The reserved TLSDESC entry in the PLT for an executable.
582 static unsigned char tlsdesc_plt_entry[plt_entry_size];
584 // Set the final size.
586 set_final_data_size();
588 // Write out the PLT data.
590 do_write(Output_file*);
592 // The reloc section.
595 Output_data_got<64, false>* got_;
596 // The .got.plt section.
597 Output_data_space* got_plt_;
598 // The number of PLT entries.
600 // Offset of the reserved TLSDESC_GOT entry when needed.
601 unsigned int tlsdesc_got_offset_;
604 // Create the PLT section. The ordinary .got section is an argument,
605 // since we need to refer to the start. We also create our own .got
606 // section just for PLT entries.
608 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
609 Output_data_got<64, false>* got,
610 Output_data_space* got_plt)
611 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
612 tlsdesc_got_offset_(-1U)
614 this->rel_ = new Reloc_section(false);
615 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
616 elfcpp::SHF_ALLOC, this->rel_);
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),
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,
847 elfcpp::STV_HIDDEN, 0,
849 ? Symbol::SEGMENT_END
850 : Symbol::SEGMENT_START),
853 this->tls_base_symbol_defined_ = true;
856 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
859 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
862 if (this->plt_ == NULL)
863 this->make_plt_section(symtab, layout);
865 if (!this->plt_->has_tlsdesc_entry())
867 // Allocate the TLSDESC_GOT entry.
868 Output_data_got<64, false>* got = this->got_section(symtab, layout);
869 unsigned int got_offset = got->add_constant(0);
871 // Allocate the TLSDESC_PLT entry.
872 this->plt_->reserve_tlsdesc_entry(got_offset);
876 // Create a GOT entry for the TLS module index.
879 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
880 Sized_relobj<64, false>* object)
882 if (this->got_mod_index_offset_ == -1U)
884 gold_assert(symtab != NULL && layout != NULL && object != NULL);
885 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
886 Output_data_got<64, false>* got = this->got_section(symtab, layout);
887 unsigned int got_offset = got->add_constant(0);
888 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
890 got->add_constant(0);
891 this->got_mod_index_offset_ = got_offset;
893 return this->got_mod_index_offset_;
896 // Optimize the TLS relocation type based on what we know about the
897 // symbol. IS_FINAL is true if the final address of this symbol is
898 // known at link time.
900 tls::Tls_optimization
901 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
903 // If we are generating a shared library, then we can't do anything
905 if (parameters->options().shared())
906 return tls::TLSOPT_NONE;
910 case elfcpp::R_X86_64_TLSGD:
911 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
912 case elfcpp::R_X86_64_TLSDESC_CALL:
913 // These are General-Dynamic which permits fully general TLS
914 // access. Since we know that we are generating an executable,
915 // we can convert this to Initial-Exec. If we also know that
916 // this is a local symbol, we can further switch to Local-Exec.
918 return tls::TLSOPT_TO_LE;
919 return tls::TLSOPT_TO_IE;
921 case elfcpp::R_X86_64_TLSLD:
922 // This is Local-Dynamic, which refers to a local symbol in the
923 // dynamic TLS block. Since we know that we generating an
924 // executable, we can switch to Local-Exec.
925 return tls::TLSOPT_TO_LE;
927 case elfcpp::R_X86_64_DTPOFF32:
928 case elfcpp::R_X86_64_DTPOFF64:
929 // Another Local-Dynamic reloc.
930 return tls::TLSOPT_TO_LE;
932 case elfcpp::R_X86_64_GOTTPOFF:
933 // These are Initial-Exec relocs which get the thread offset
934 // from the GOT. If we know that we are linking against the
935 // local symbol, we can switch to Local-Exec, which links the
936 // thread offset into the instruction.
938 return tls::TLSOPT_TO_LE;
939 return tls::TLSOPT_NONE;
941 case elfcpp::R_X86_64_TPOFF32:
942 // When we already have Local-Exec, there is nothing further we
944 return tls::TLSOPT_NONE;
951 // Report an unsupported relocation against a local symbol.
954 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
957 gold_error(_("%s: unsupported reloc %u against local symbol"),
958 object->name().c_str(), r_type);
961 // We are about to emit a dynamic relocation of type R_TYPE. If the
962 // dynamic linker does not support it, issue an error. The GNU linker
963 // only issues a non-PIC error for an allocated read-only section.
964 // Here we know the section is allocated, but we don't know that it is
965 // read-only. But we check for all the relocation types which the
966 // glibc dynamic linker supports, so it seems appropriate to issue an
967 // error even if the section is not read-only.
970 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
974 // These are the relocation types supported by glibc for x86_64.
975 case elfcpp::R_X86_64_RELATIVE:
976 case elfcpp::R_X86_64_GLOB_DAT:
977 case elfcpp::R_X86_64_JUMP_SLOT:
978 case elfcpp::R_X86_64_DTPMOD64:
979 case elfcpp::R_X86_64_DTPOFF64:
980 case elfcpp::R_X86_64_TPOFF64:
981 case elfcpp::R_X86_64_64:
982 case elfcpp::R_X86_64_32:
983 case elfcpp::R_X86_64_PC32:
984 case elfcpp::R_X86_64_COPY:
988 // This prevents us from issuing more than one error per reloc
989 // section. But we can still wind up issuing more than one
990 // error per object file.
991 if (this->issued_non_pic_error_)
993 gold_assert(parameters->options().output_is_position_independent());
994 object->error(_("requires unsupported dynamic reloc; "
995 "recompile with -fPIC"));
996 this->issued_non_pic_error_ = true;
999 case elfcpp::R_X86_64_NONE:
1004 // Scan a relocation for a local symbol.
1007 Target_x86_64::Scan::local(const General_options&,
1008 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(const General_options&,
1289 Symbol_table* symtab,
1291 Target_x86_64* target,
1292 Sized_relobj<64, false>* object,
1293 unsigned int data_shndx,
1294 Output_section* output_section,
1295 const elfcpp::Rela<64, false>& reloc,
1296 unsigned int r_type,
1301 case elfcpp::R_X86_64_NONE:
1302 case elfcpp::R_386_GNU_VTINHERIT:
1303 case elfcpp::R_386_GNU_VTENTRY:
1306 case elfcpp::R_X86_64_64:
1307 case elfcpp::R_X86_64_32:
1308 case elfcpp::R_X86_64_32S:
1309 case elfcpp::R_X86_64_16:
1310 case elfcpp::R_X86_64_8:
1312 // Make a PLT entry if necessary.
1313 if (gsym->needs_plt_entry())
1315 target->make_plt_entry(symtab, layout, gsym);
1316 // Since this is not a PC-relative relocation, we may be
1317 // taking the address of a function. In that case we need to
1318 // set the entry in the dynamic symbol table to the address of
1320 if (gsym->is_from_dynobj() && !parameters->options().shared())
1321 gsym->set_needs_dynsym_value();
1323 // Make a dynamic relocation if necessary.
1324 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1326 if (target->may_need_copy_reloc(gsym))
1328 target->copy_reloc(symtab, layout, object,
1329 data_shndx, output_section, gsym, reloc);
1331 else if (r_type == elfcpp::R_X86_64_64
1332 && gsym->can_use_relative_reloc(false))
1334 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1335 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1336 output_section, object,
1337 data_shndx, reloc.get_r_offset(),
1338 reloc.get_r_addend());
1342 this->check_non_pic(object, r_type);
1343 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1344 rela_dyn->add_global(gsym, r_type, output_section, object,
1345 data_shndx, reloc.get_r_offset(),
1346 reloc.get_r_addend());
1352 case elfcpp::R_X86_64_PC64:
1353 case elfcpp::R_X86_64_PC32:
1354 case elfcpp::R_X86_64_PC16:
1355 case elfcpp::R_X86_64_PC8:
1357 // Make a PLT entry if necessary.
1358 if (gsym->needs_plt_entry())
1359 target->make_plt_entry(symtab, layout, gsym);
1360 // Make a dynamic relocation if necessary.
1361 int flags = Symbol::NON_PIC_REF;
1362 if (gsym->type() == elfcpp::STT_FUNC)
1363 flags |= Symbol::FUNCTION_CALL;
1364 if (gsym->needs_dynamic_reloc(flags))
1366 if (target->may_need_copy_reloc(gsym))
1368 target->copy_reloc(symtab, layout, object,
1369 data_shndx, output_section, gsym, reloc);
1373 this->check_non_pic(object, r_type);
1374 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1375 rela_dyn->add_global(gsym, r_type, output_section, object,
1376 data_shndx, reloc.get_r_offset(),
1377 reloc.get_r_addend());
1383 case elfcpp::R_X86_64_GOT64:
1384 case elfcpp::R_X86_64_GOT32:
1385 case elfcpp::R_X86_64_GOTPCREL64:
1386 case elfcpp::R_X86_64_GOTPCREL:
1387 case elfcpp::R_X86_64_GOTPLT64:
1389 // The symbol requires a GOT entry.
1390 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1391 if (gsym->final_value_is_known())
1392 got->add_global(gsym, GOT_TYPE_STANDARD);
1395 // If this symbol is not fully resolved, we need to add a
1396 // dynamic relocation for it.
1397 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1398 if (gsym->is_from_dynobj()
1399 || gsym->is_undefined()
1400 || gsym->is_preemptible())
1401 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1402 elfcpp::R_X86_64_GLOB_DAT);
1405 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1406 rela_dyn->add_global_relative(
1407 gsym, elfcpp::R_X86_64_RELATIVE, got,
1408 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1411 // For GOTPLT64, we also need a PLT entry (but only if the
1412 // symbol is not fully resolved).
1413 if (r_type == elfcpp::R_X86_64_GOTPLT64
1414 && !gsym->final_value_is_known())
1415 target->make_plt_entry(symtab, layout, gsym);
1419 case elfcpp::R_X86_64_PLT32:
1420 // If the symbol is fully resolved, this is just a PC32 reloc.
1421 // Otherwise we need a PLT entry.
1422 if (gsym->final_value_is_known())
1424 // If building a shared library, we can also skip the PLT entry
1425 // if the symbol is defined in the output file and is protected
1427 if (gsym->is_defined()
1428 && !gsym->is_from_dynobj()
1429 && !gsym->is_preemptible())
1431 target->make_plt_entry(symtab, layout, gsym);
1434 case elfcpp::R_X86_64_GOTPC32:
1435 case elfcpp::R_X86_64_GOTOFF64:
1436 case elfcpp::R_X86_64_GOTPC64:
1437 case elfcpp::R_X86_64_PLTOFF64:
1438 // We need a GOT section.
1439 target->got_section(symtab, layout);
1440 // For PLTOFF64, we also need a PLT entry (but only if the
1441 // symbol is not fully resolved).
1442 if (r_type == elfcpp::R_X86_64_PLTOFF64
1443 && !gsym->final_value_is_known())
1444 target->make_plt_entry(symtab, layout, gsym);
1447 case elfcpp::R_X86_64_COPY:
1448 case elfcpp::R_X86_64_GLOB_DAT:
1449 case elfcpp::R_X86_64_JUMP_SLOT:
1450 case elfcpp::R_X86_64_RELATIVE:
1451 // These are outstanding tls relocs, which are unexpected when linking
1452 case elfcpp::R_X86_64_TPOFF64:
1453 case elfcpp::R_X86_64_DTPMOD64:
1454 case elfcpp::R_X86_64_TLSDESC:
1455 gold_error(_("%s: unexpected reloc %u in object file"),
1456 object->name().c_str(), r_type);
1459 // These are initial tls relocs, which are expected for global()
1460 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1461 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1462 case elfcpp::R_X86_64_TLSDESC_CALL:
1463 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1464 case elfcpp::R_X86_64_DTPOFF32:
1465 case elfcpp::R_X86_64_DTPOFF64:
1466 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1467 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1469 const bool is_final = gsym->final_value_is_known();
1470 const tls::Tls_optimization optimized_type
1471 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1474 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1475 if (optimized_type == tls::TLSOPT_NONE)
1477 // Create a pair of GOT entries for the module index and
1478 // dtv-relative offset.
1479 Output_data_got<64, false>* got
1480 = target->got_section(symtab, layout);
1481 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1482 target->rela_dyn_section(layout),
1483 elfcpp::R_X86_64_DTPMOD64,
1484 elfcpp::R_X86_64_DTPOFF64);
1486 else if (optimized_type == tls::TLSOPT_TO_IE)
1488 // Create a GOT entry for the tp-relative offset.
1489 Output_data_got<64, false>* got
1490 = target->got_section(symtab, layout);
1491 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1492 target->rela_dyn_section(layout),
1493 elfcpp::R_X86_64_TPOFF64);
1495 else if (optimized_type != tls::TLSOPT_TO_LE)
1496 unsupported_reloc_global(object, r_type, gsym);
1499 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1500 target->define_tls_base_symbol(symtab, layout);
1501 if (optimized_type == tls::TLSOPT_NONE)
1503 // Create reserved PLT and GOT entries for the resolver.
1504 target->reserve_tlsdesc_entries(symtab, layout);
1506 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1507 Output_data_got<64, false>* got
1508 = target->got_section(symtab, layout);
1509 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1510 target->rela_dyn_section(layout),
1511 elfcpp::R_X86_64_TLSDESC, 0);
1513 else if (optimized_type == tls::TLSOPT_TO_IE)
1515 // Create a GOT entry for the tp-relative offset.
1516 Output_data_got<64, false>* got
1517 = target->got_section(symtab, layout);
1518 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1519 target->rela_dyn_section(layout),
1520 elfcpp::R_X86_64_TPOFF64);
1522 else if (optimized_type != tls::TLSOPT_TO_LE)
1523 unsupported_reloc_global(object, r_type, gsym);
1526 case elfcpp::R_X86_64_TLSDESC_CALL:
1529 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1530 if (optimized_type == tls::TLSOPT_NONE)
1532 // Create a GOT entry for the module index.
1533 target->got_mod_index_entry(symtab, layout, object);
1535 else if (optimized_type != tls::TLSOPT_TO_LE)
1536 unsupported_reloc_global(object, r_type, gsym);
1539 case elfcpp::R_X86_64_DTPOFF32:
1540 case elfcpp::R_X86_64_DTPOFF64:
1543 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1544 layout->set_has_static_tls();
1545 if (optimized_type == tls::TLSOPT_NONE)
1547 // Create a GOT entry for the tp-relative offset.
1548 Output_data_got<64, false>* got
1549 = target->got_section(symtab, layout);
1550 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1551 target->rela_dyn_section(layout),
1552 elfcpp::R_X86_64_TPOFF64);
1554 else if (optimized_type != tls::TLSOPT_TO_LE)
1555 unsupported_reloc_global(object, r_type, gsym);
1558 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1559 layout->set_has_static_tls();
1560 if (parameters->options().shared())
1561 unsupported_reloc_local(object, r_type);
1570 case elfcpp::R_X86_64_SIZE32:
1571 case elfcpp::R_X86_64_SIZE64:
1573 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1574 object->name().c_str(), r_type,
1575 gsym->demangled_name().c_str());
1581 Target_x86_64::gc_process_relocs(const General_options& options,
1582 Symbol_table* symtab,
1584 Sized_relobj<64, false>* object,
1585 unsigned int data_shndx,
1586 unsigned int sh_type,
1587 const unsigned char* prelocs,
1589 Output_section* output_section,
1590 bool needs_special_offset_handling,
1591 size_t local_symbol_count,
1592 const unsigned char* plocal_symbols)
1595 if (sh_type == elfcpp::SHT_REL)
1600 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1601 Target_x86_64::Scan>(
1611 needs_special_offset_handling,
1616 // Scan relocations for a section.
1619 Target_x86_64::scan_relocs(const General_options& options,
1620 Symbol_table* symtab,
1622 Sized_relobj<64, false>* object,
1623 unsigned int data_shndx,
1624 unsigned int sh_type,
1625 const unsigned char* prelocs,
1627 Output_section* output_section,
1628 bool needs_special_offset_handling,
1629 size_t local_symbol_count,
1630 const unsigned char* plocal_symbols)
1632 if (sh_type == elfcpp::SHT_REL)
1634 gold_error(_("%s: unsupported REL reloc section"),
1635 object->name().c_str());
1639 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1640 Target_x86_64::Scan>(
1650 needs_special_offset_handling,
1655 // Finalize the sections.
1658 Target_x86_64::do_finalize_sections(Layout* layout)
1660 // Fill in some more dynamic tags.
1661 Output_data_dynamic* const odyn = layout->dynamic_data();
1664 if (this->got_plt_ != NULL)
1665 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1667 if (this->plt_ != NULL)
1669 const Output_data* od = this->plt_->rel_plt();
1670 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1671 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1672 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1673 if (this->plt_->has_tlsdesc_entry())
1675 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1676 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1677 this->got_->finalize_data_size();
1678 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1679 this->plt_, plt_offset);
1680 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1681 this->got_, got_offset);
1685 if (this->rela_dyn_ != NULL)
1687 const Output_data* od = this->rela_dyn_;
1688 odyn->add_section_address(elfcpp::DT_RELA, od);
1689 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1690 odyn->add_constant(elfcpp::DT_RELAENT,
1691 elfcpp::Elf_sizes<64>::rela_size);
1694 if (!parameters->options().shared())
1696 // The value of the DT_DEBUG tag is filled in by the dynamic
1697 // linker at run time, and used by the debugger.
1698 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1702 // Emit any relocs we saved in an attempt to avoid generating COPY
1704 if (this->copy_relocs_.any_saved_relocs())
1705 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1708 // Perform a relocation.
1711 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1712 Target_x86_64* target,
1715 const elfcpp::Rela<64, false>& rela,
1716 unsigned int r_type,
1717 const Sized_symbol<64>* gsym,
1718 const Symbol_value<64>* psymval,
1719 unsigned char* view,
1720 elfcpp::Elf_types<64>::Elf_Addr address,
1721 section_size_type view_size)
1723 if (this->skip_call_tls_get_addr_)
1725 if ((r_type != elfcpp::R_X86_64_PLT32
1726 && r_type != elfcpp::R_X86_64_PC32)
1728 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1730 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1731 _("missing expected TLS relocation"));
1735 this->skip_call_tls_get_addr_ = false;
1740 // Pick the value to use for symbols defined in shared objects.
1741 Symbol_value<64> symval;
1743 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1744 || r_type == elfcpp::R_X86_64_PC32
1745 || r_type == elfcpp::R_X86_64_PC16
1746 || r_type == elfcpp::R_X86_64_PC8))
1748 symval.set_output_value(target->plt_section()->address()
1749 + gsym->plt_offset());
1753 const Sized_relobj<64, false>* object = relinfo->object;
1754 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1756 // Get the GOT offset if needed.
1757 // The GOT pointer points to the end of the GOT section.
1758 // We need to subtract the size of the GOT section to get
1759 // the actual offset to use in the relocation.
1760 bool have_got_offset = false;
1761 unsigned int got_offset = 0;
1764 case elfcpp::R_X86_64_GOT32:
1765 case elfcpp::R_X86_64_GOT64:
1766 case elfcpp::R_X86_64_GOTPLT64:
1767 case elfcpp::R_X86_64_GOTPCREL:
1768 case elfcpp::R_X86_64_GOTPCREL64:
1771 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1772 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1776 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1777 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1778 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1779 - target->got_size());
1781 have_got_offset = true;
1790 case elfcpp::R_X86_64_NONE:
1791 case elfcpp::R_386_GNU_VTINHERIT:
1792 case elfcpp::R_386_GNU_VTENTRY:
1795 case elfcpp::R_X86_64_64:
1796 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1799 case elfcpp::R_X86_64_PC64:
1800 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1804 case elfcpp::R_X86_64_32:
1805 // FIXME: we need to verify that value + addend fits into 32 bits:
1806 // uint64_t x = value + addend;
1807 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1808 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1809 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1812 case elfcpp::R_X86_64_32S:
1813 // FIXME: we need to verify that value + addend fits into 32 bits:
1814 // int64_t x = value + addend; // note this quantity is signed!
1815 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1816 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1819 case elfcpp::R_X86_64_PC32:
1820 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1824 case elfcpp::R_X86_64_16:
1825 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1828 case elfcpp::R_X86_64_PC16:
1829 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1833 case elfcpp::R_X86_64_8:
1834 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1837 case elfcpp::R_X86_64_PC8:
1838 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1842 case elfcpp::R_X86_64_PLT32:
1843 gold_assert(gsym == NULL
1844 || gsym->has_plt_offset()
1845 || gsym->final_value_is_known()
1846 || (gsym->is_defined()
1847 && !gsym->is_from_dynobj()
1848 && !gsym->is_preemptible()));
1849 // Note: while this code looks the same as for R_X86_64_PC32, it
1850 // behaves differently because psymval was set to point to
1851 // the PLT entry, rather than the symbol, in Scan::global().
1852 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1856 case elfcpp::R_X86_64_PLTOFF64:
1859 gold_assert(gsym->has_plt_offset()
1860 || gsym->final_value_is_known());
1861 elfcpp::Elf_types<64>::Elf_Addr got_address;
1862 got_address = target->got_section(NULL, NULL)->address();
1863 Relocate_functions<64, false>::rela64(view, object, psymval,
1864 addend - got_address);
1867 case elfcpp::R_X86_64_GOT32:
1868 gold_assert(have_got_offset);
1869 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1872 case elfcpp::R_X86_64_GOTPC32:
1875 elfcpp::Elf_types<64>::Elf_Addr value;
1876 value = target->got_plt_section()->address();
1877 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1881 case elfcpp::R_X86_64_GOT64:
1882 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1883 // Since we always add a PLT entry, this is equivalent.
1884 case elfcpp::R_X86_64_GOTPLT64:
1885 gold_assert(have_got_offset);
1886 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1889 case elfcpp::R_X86_64_GOTPC64:
1892 elfcpp::Elf_types<64>::Elf_Addr value;
1893 value = target->got_plt_section()->address();
1894 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1898 case elfcpp::R_X86_64_GOTOFF64:
1900 elfcpp::Elf_types<64>::Elf_Addr value;
1901 value = (psymval->value(object, 0)
1902 - target->got_plt_section()->address());
1903 Relocate_functions<64, false>::rela64(view, value, addend);
1907 case elfcpp::R_X86_64_GOTPCREL:
1909 gold_assert(have_got_offset);
1910 elfcpp::Elf_types<64>::Elf_Addr value;
1911 value = target->got_plt_section()->address() + got_offset;
1912 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1916 case elfcpp::R_X86_64_GOTPCREL64:
1918 gold_assert(have_got_offset);
1919 elfcpp::Elf_types<64>::Elf_Addr value;
1920 value = target->got_plt_section()->address() + got_offset;
1921 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1925 case elfcpp::R_X86_64_COPY:
1926 case elfcpp::R_X86_64_GLOB_DAT:
1927 case elfcpp::R_X86_64_JUMP_SLOT:
1928 case elfcpp::R_X86_64_RELATIVE:
1929 // These are outstanding tls relocs, which are unexpected when linking
1930 case elfcpp::R_X86_64_TPOFF64:
1931 case elfcpp::R_X86_64_DTPMOD64:
1932 case elfcpp::R_X86_64_TLSDESC:
1933 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1934 _("unexpected reloc %u in object file"),
1938 // These are initial tls relocs, which are expected when linking
1939 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1940 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1941 case elfcpp::R_X86_64_TLSDESC_CALL:
1942 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1943 case elfcpp::R_X86_64_DTPOFF32:
1944 case elfcpp::R_X86_64_DTPOFF64:
1945 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1946 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1947 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1948 view, address, view_size);
1951 case elfcpp::R_X86_64_SIZE32:
1952 case elfcpp::R_X86_64_SIZE64:
1954 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1955 _("unsupported reloc %u"),
1963 // Perform a TLS relocation.
1966 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1967 Target_x86_64* target,
1969 const elfcpp::Rela<64, false>& rela,
1970 unsigned int r_type,
1971 const Sized_symbol<64>* gsym,
1972 const Symbol_value<64>* psymval,
1973 unsigned char* view,
1974 elfcpp::Elf_types<64>::Elf_Addr address,
1975 section_size_type view_size)
1977 Output_segment* tls_segment = relinfo->layout->tls_segment();
1979 const Sized_relobj<64, false>* object = relinfo->object;
1980 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1982 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1984 const bool is_final = (gsym == NULL
1985 ? !parameters->options().output_is_position_independent()
1986 : gsym->final_value_is_known());
1987 const tls::Tls_optimization optimized_type
1988 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1991 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1992 this->saw_tls_block_reloc_ = true;
1993 if (optimized_type == tls::TLSOPT_TO_LE)
1995 gold_assert(tls_segment != NULL);
1996 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1997 rela, r_type, value, view,
2003 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2004 ? GOT_TYPE_TLS_OFFSET
2005 : GOT_TYPE_TLS_PAIR);
2006 unsigned int got_offset;
2009 gold_assert(gsym->has_got_offset(got_type));
2010 got_offset = gsym->got_offset(got_type) - target->got_size();
2014 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2015 gold_assert(object->local_has_got_offset(r_sym, got_type));
2016 got_offset = (object->local_got_offset(r_sym, got_type)
2017 - target->got_size());
2019 if (optimized_type == tls::TLSOPT_TO_IE)
2021 gold_assert(tls_segment != NULL);
2022 value = target->got_plt_section()->address() + got_offset;
2023 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2024 value, view, address, view_size);
2027 else if (optimized_type == tls::TLSOPT_NONE)
2029 // Relocate the field with the offset of the pair of GOT
2031 value = target->got_plt_section()->address() + got_offset;
2032 Relocate_functions<64, false>::pcrela32(view, value, addend,
2037 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2038 _("unsupported reloc %u"), r_type);
2041 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2042 case elfcpp::R_X86_64_TLSDESC_CALL:
2043 this->saw_tls_block_reloc_ = true;
2044 if (optimized_type == tls::TLSOPT_TO_LE)
2046 gold_assert(tls_segment != NULL);
2047 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2048 rela, r_type, value, view,
2054 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2055 ? GOT_TYPE_TLS_OFFSET
2056 : GOT_TYPE_TLS_DESC);
2057 unsigned int got_offset;
2060 gold_assert(gsym->has_got_offset(got_type));
2061 got_offset = gsym->got_offset(got_type) - target->got_size();
2065 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2066 gold_assert(object->local_has_got_offset(r_sym, got_type));
2067 got_offset = (object->local_got_offset(r_sym, got_type)
2068 - target->got_size());
2070 if (optimized_type == tls::TLSOPT_TO_IE)
2072 gold_assert(tls_segment != NULL);
2073 value = target->got_plt_section()->address() + got_offset;
2074 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2075 rela, r_type, value, view, address,
2079 else if (optimized_type == tls::TLSOPT_NONE)
2081 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2083 // Relocate the field with the offset of the pair of GOT
2085 value = target->got_plt_section()->address() + got_offset;
2086 Relocate_functions<64, false>::pcrela32(view, value, addend,
2092 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2093 _("unsupported reloc %u"), r_type);
2096 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2097 this->saw_tls_block_reloc_ = true;
2098 if (optimized_type == tls::TLSOPT_TO_LE)
2100 gold_assert(tls_segment != NULL);
2101 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2102 value, view, view_size);
2105 else if (optimized_type == tls::TLSOPT_NONE)
2107 // Relocate the field with the offset of the GOT entry for
2108 // the module index.
2109 unsigned int got_offset;
2110 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2111 - target->got_size());
2112 value = target->got_plt_section()->address() + got_offset;
2113 Relocate_functions<64, false>::pcrela32(view, value, addend,
2117 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2118 _("unsupported reloc %u"), r_type);
2121 case elfcpp::R_X86_64_DTPOFF32:
2122 gold_assert(tls_segment != NULL);
2123 if (optimized_type == tls::TLSOPT_TO_LE)
2125 // This relocation type is used in debugging information.
2126 // In that case we need to not optimize the value. If we
2127 // haven't seen a TLSLD reloc, then we assume we should not
2128 // optimize this reloc.
2129 if (this->saw_tls_block_reloc_)
2130 value -= tls_segment->memsz();
2132 Relocate_functions<64, false>::rela32(view, value, addend);
2135 case elfcpp::R_X86_64_DTPOFF64:
2136 gold_assert(tls_segment != NULL);
2137 if (optimized_type == tls::TLSOPT_TO_LE)
2139 // See R_X86_64_DTPOFF32, just above, for why we test this.
2140 if (this->saw_tls_block_reloc_)
2141 value -= tls_segment->memsz();
2143 Relocate_functions<64, false>::rela64(view, value, addend);
2146 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2147 if (optimized_type == tls::TLSOPT_TO_LE)
2149 gold_assert(tls_segment != NULL);
2150 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2151 rela, r_type, value, view,
2155 else if (optimized_type == tls::TLSOPT_NONE)
2157 // Relocate the field with the offset of the GOT entry for
2158 // the tp-relative offset of the symbol.
2159 unsigned int got_offset;
2162 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2163 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2164 - target->got_size());
2168 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2169 gold_assert(object->local_has_got_offset(r_sym,
2170 GOT_TYPE_TLS_OFFSET));
2171 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2172 - target->got_size());
2174 value = target->got_plt_section()->address() + got_offset;
2175 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2178 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2179 _("unsupported reloc type %u"),
2183 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2184 value -= tls_segment->memsz();
2185 Relocate_functions<64, false>::rela32(view, value, addend);
2190 // Do a relocation in which we convert a TLS General-Dynamic to an
2194 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2197 const elfcpp::Rela<64, false>& rela,
2199 elfcpp::Elf_types<64>::Elf_Addr value,
2200 unsigned char* view,
2201 elfcpp::Elf_types<64>::Elf_Addr address,
2202 section_size_type view_size)
2204 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2205 // .word 0x6666; rex64; call __tls_get_addr
2206 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2208 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2211 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2212 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2213 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2214 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2216 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2218 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2219 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2221 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2223 this->skip_call_tls_get_addr_ = true;
2226 // Do a relocation in which we convert a TLS General-Dynamic to a
2230 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2232 Output_segment* tls_segment,
2233 const elfcpp::Rela<64, false>& rela,
2235 elfcpp::Elf_types<64>::Elf_Addr value,
2236 unsigned char* view,
2237 section_size_type view_size)
2239 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2240 // .word 0x6666; rex64; call __tls_get_addr
2241 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2243 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2244 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2246 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2247 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2248 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2249 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2251 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2253 value -= tls_segment->memsz();
2254 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2256 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2258 this->skip_call_tls_get_addr_ = true;
2261 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2264 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2265 const Relocate_info<64, false>* relinfo,
2268 const elfcpp::Rela<64, false>& rela,
2269 unsigned int r_type,
2270 elfcpp::Elf_types<64>::Elf_Addr value,
2271 unsigned char* view,
2272 elfcpp::Elf_types<64>::Elf_Addr address,
2273 section_size_type view_size)
2275 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2277 // leaq foo@tlsdesc(%rip), %rax
2278 // ==> movq foo@gottpoff(%rip), %rax
2279 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2280 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2281 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2282 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2284 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2285 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2289 // call *foo@tlscall(%rax)
2291 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2292 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2293 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2294 view[0] == 0xff && view[1] == 0x10);
2300 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2303 Target_x86_64::Relocate::tls_desc_gd_to_le(
2304 const Relocate_info<64, false>* relinfo,
2306 Output_segment* tls_segment,
2307 const elfcpp::Rela<64, false>& rela,
2308 unsigned int r_type,
2309 elfcpp::Elf_types<64>::Elf_Addr value,
2310 unsigned char* view,
2311 section_size_type view_size)
2313 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2315 // leaq foo@tlsdesc(%rip), %rax
2316 // ==> movq foo@tpoff, %rax
2317 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2318 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2319 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2320 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2323 value -= tls_segment->memsz();
2324 Relocate_functions<64, false>::rela32(view, value, 0);
2328 // call *foo@tlscall(%rax)
2330 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2331 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2332 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2333 view[0] == 0xff && view[1] == 0x10);
2340 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2343 const elfcpp::Rela<64, false>& rela,
2345 elfcpp::Elf_types<64>::Elf_Addr,
2346 unsigned char* view,
2347 section_size_type view_size)
2349 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2350 // ... leq foo@dtpoff(%rax),%reg
2351 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2353 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2354 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2356 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2357 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2359 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2361 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2363 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2365 this->skip_call_tls_get_addr_ = true;
2368 // Do a relocation in which we convert a TLS Initial-Exec to a
2372 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2374 Output_segment* tls_segment,
2375 const elfcpp::Rela<64, false>& rela,
2377 elfcpp::Elf_types<64>::Elf_Addr value,
2378 unsigned char* view,
2379 section_size_type view_size)
2381 // We need to examine the opcodes to figure out which instruction we
2384 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2385 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2387 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2388 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2390 unsigned char op1 = view[-3];
2391 unsigned char op2 = view[-2];
2392 unsigned char op3 = view[-1];
2393 unsigned char reg = op3 >> 3;
2401 view[-1] = 0xc0 | reg;
2405 // Special handling for %rsp.
2409 view[-1] = 0xc0 | reg;
2417 view[-1] = 0x80 | reg | (reg << 3);
2420 value -= tls_segment->memsz();
2421 Relocate_functions<64, false>::rela32(view, value, 0);
2424 // Relocate section data.
2427 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2428 unsigned int sh_type,
2429 const unsigned char* prelocs,
2431 Output_section* output_section,
2432 bool needs_special_offset_handling,
2433 unsigned char* view,
2434 elfcpp::Elf_types<64>::Elf_Addr address,
2435 section_size_type view_size)
2437 gold_assert(sh_type == elfcpp::SHT_RELA);
2439 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2440 Target_x86_64::Relocate>(
2446 needs_special_offset_handling,
2452 // Return the size of a relocation while scanning during a relocatable
2456 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2457 unsigned int r_type,
2462 case elfcpp::R_X86_64_NONE:
2463 case elfcpp::R_386_GNU_VTINHERIT:
2464 case elfcpp::R_386_GNU_VTENTRY:
2465 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2466 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2467 case elfcpp::R_X86_64_TLSDESC_CALL:
2468 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2469 case elfcpp::R_X86_64_DTPOFF32:
2470 case elfcpp::R_X86_64_DTPOFF64:
2471 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2472 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2475 case elfcpp::R_X86_64_64:
2476 case elfcpp::R_X86_64_PC64:
2477 case elfcpp::R_X86_64_GOTOFF64:
2478 case elfcpp::R_X86_64_GOTPC64:
2479 case elfcpp::R_X86_64_PLTOFF64:
2480 case elfcpp::R_X86_64_GOT64:
2481 case elfcpp::R_X86_64_GOTPCREL64:
2482 case elfcpp::R_X86_64_GOTPCREL:
2483 case elfcpp::R_X86_64_GOTPLT64:
2486 case elfcpp::R_X86_64_32:
2487 case elfcpp::R_X86_64_32S:
2488 case elfcpp::R_X86_64_PC32:
2489 case elfcpp::R_X86_64_PLT32:
2490 case elfcpp::R_X86_64_GOTPC32:
2491 case elfcpp::R_X86_64_GOT32:
2494 case elfcpp::R_X86_64_16:
2495 case elfcpp::R_X86_64_PC16:
2498 case elfcpp::R_X86_64_8:
2499 case elfcpp::R_X86_64_PC8:
2502 case elfcpp::R_X86_64_COPY:
2503 case elfcpp::R_X86_64_GLOB_DAT:
2504 case elfcpp::R_X86_64_JUMP_SLOT:
2505 case elfcpp::R_X86_64_RELATIVE:
2506 // These are outstanding tls relocs, which are unexpected when linking
2507 case elfcpp::R_X86_64_TPOFF64:
2508 case elfcpp::R_X86_64_DTPMOD64:
2509 case elfcpp::R_X86_64_TLSDESC:
2510 object->error(_("unexpected reloc %u in object file"), r_type);
2513 case elfcpp::R_X86_64_SIZE32:
2514 case elfcpp::R_X86_64_SIZE64:
2516 object->error(_("unsupported reloc %u against local symbol"), r_type);
2521 // Scan the relocs during a relocatable link.
2524 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2525 Symbol_table* symtab,
2527 Sized_relobj<64, false>* object,
2528 unsigned int data_shndx,
2529 unsigned int sh_type,
2530 const unsigned char* prelocs,
2532 Output_section* output_section,
2533 bool needs_special_offset_handling,
2534 size_t local_symbol_count,
2535 const unsigned char* plocal_symbols,
2536 Relocatable_relocs* rr)
2538 gold_assert(sh_type == elfcpp::SHT_RELA);
2540 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2541 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2543 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2544 Scan_relocatable_relocs>(
2553 needs_special_offset_handling,
2559 // Relocate a section during a relocatable link.
2562 Target_x86_64::relocate_for_relocatable(
2563 const Relocate_info<64, false>* relinfo,
2564 unsigned int sh_type,
2565 const unsigned char* prelocs,
2567 Output_section* output_section,
2568 off_t offset_in_output_section,
2569 const Relocatable_relocs* rr,
2570 unsigned char* view,
2571 elfcpp::Elf_types<64>::Elf_Addr view_address,
2572 section_size_type view_size,
2573 unsigned char* reloc_view,
2574 section_size_type reloc_view_size)
2576 gold_assert(sh_type == elfcpp::SHT_RELA);
2578 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2583 offset_in_output_section,
2592 // Return the value to use for a dynamic which requires special
2593 // treatment. This is how we support equality comparisons of function
2594 // pointers across shared library boundaries, as described in the
2595 // processor specific ABI supplement.
2598 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2600 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2601 return this->plt_section()->address() + gsym->plt_offset();
2604 // Return a string used to fill a code section with nops to take up
2605 // the specified length.
2608 Target_x86_64::do_code_fill(section_size_type length) const
2612 // Build a jmpq instruction to skip over the bytes.
2613 unsigned char jmp[5];
2615 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2616 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2617 + std::string(length - 5, '\0'));
2620 // Nop sequences of various lengths.
2621 const char nop1[1] = { 0x90 }; // nop
2622 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2623 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2624 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2625 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2627 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2629 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2631 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2632 0x00, 0x00, 0x00, 0x00 };
2633 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2634 0x00, 0x00, 0x00, 0x00,
2636 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2637 0x84, 0x00, 0x00, 0x00,
2639 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2640 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2642 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2643 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2644 0x00, 0x00, 0x00, 0x00 };
2645 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2646 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2647 0x00, 0x00, 0x00, 0x00,
2649 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2650 0x66, 0x2e, 0x0f, 0x1f, // data16
2651 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2653 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2654 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2655 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2658 const char* nops[16] = {
2660 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2661 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2664 return std::string(nops[length], length);
2667 // The selector for x86_64 object files.
2669 class Target_selector_x86_64 : public Target_selector_freebsd
2672 Target_selector_x86_64()
2673 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2674 "elf64-x86-64-freebsd")
2678 do_instantiate_target()
2679 { return new Target_x86_64(); }
2683 Target_selector_x86_64 target_selector_x86_64;
2685 } // End anonymous namespace.