1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 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
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
37 #include "parameters.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
54 class Output_data_plt_x86_64;
56 // The x86_64 target class.
58 // http://www.x86-64.org/documentation/abi.pdf
59 // TLS info comes from
60 // http://people.redhat.com/drepper/tls.pdf
61 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
63 class Target_x86_64 : public Sized_target<64, false>
66 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67 // uses only Elf64_Rela relocation entries with explicit addends."
68 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
71 : Sized_target<64, false>(&x86_64_info),
72 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
73 copy_relocs_(NULL), dynbss_(NULL)
76 // Scan the relocations to look for symbol adjustments.
78 scan_relocs(const General_options& options,
81 Sized_relobj<64, false>* object,
82 unsigned int data_shndx,
84 const unsigned char* prelocs,
86 Output_section* output_section,
87 bool needs_special_offset_handling,
88 size_t local_symbol_count,
89 const unsigned char* plocal_symbols);
91 // Finalize the sections.
93 do_finalize_sections(Layout*);
95 // Return the value to use for a dynamic which requires special
98 do_dynsym_value(const Symbol*) const;
100 // Relocate a section.
102 relocate_section(const Relocate_info<64, false>*,
103 unsigned int sh_type,
104 const unsigned char* prelocs,
106 Output_section* output_section,
107 bool needs_special_offset_handling,
109 elfcpp::Elf_types<64>::Elf_Addr view_address,
112 // Return a string used to fill a code section with nops.
114 do_code_fill(off_t length);
116 // Return the size of the GOT section.
120 gold_assert(this->got_ != NULL);
121 return this->got_->data_size();
125 // The class which scans relocations.
129 local(const General_options& options, Symbol_table* symtab,
130 Layout* layout, Target_x86_64* target,
131 Sized_relobj<64, false>* object,
132 unsigned int data_shndx,
133 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
134 const elfcpp::Sym<64, false>& lsym);
137 global(const General_options& options, Symbol_table* symtab,
138 Layout* layout, Target_x86_64* target,
139 Sized_relobj<64, false>* object,
140 unsigned int data_shndx,
141 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
145 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
148 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
152 // The class which implements relocation.
157 : skip_call_tls_get_addr_(false)
162 if (this->skip_call_tls_get_addr_)
164 // FIXME: This needs to specify the location somehow.
165 gold_error(_("missing expected TLS relocation"));
169 // Do a relocation. Return false if the caller should not issue
170 // any warnings about this relocation.
172 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
173 const elfcpp::Rela<64, false>&,
174 unsigned int r_type, const Sized_symbol<64>*,
175 const Symbol_value<64>*,
176 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
180 // Do a TLS relocation.
182 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
183 const elfcpp::Rela<64, false>&,
184 unsigned int r_type, const Sized_symbol<64>*,
185 const Symbol_value<64>*,
186 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
188 // Do a TLS General-Dynamic to Local-Exec transition.
190 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
191 Output_segment* tls_segment,
192 const elfcpp::Rela<64, false>&, unsigned int r_type,
193 elfcpp::Elf_types<64>::Elf_Addr value,
197 // Do a TLS Local-Dynamic to Local-Exec transition.
199 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
200 Output_segment* tls_segment,
201 const elfcpp::Rela<64, false>&, unsigned int r_type,
202 elfcpp::Elf_types<64>::Elf_Addr value,
206 // Do a TLS Initial-Exec to Local-Exec transition.
208 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
209 Output_segment* tls_segment,
210 const elfcpp::Rela<64, false>&, unsigned int r_type,
211 elfcpp::Elf_types<64>::Elf_Addr value,
215 // This is set if we should skip the next reloc, which should be a
216 // PLT32 reloc against ___tls_get_addr.
217 bool skip_call_tls_get_addr_;
220 // Adjust TLS relocation type based on the options and whether this
221 // is a local symbol.
222 static tls::Tls_optimization
223 optimize_tls_reloc(bool is_final, int r_type);
225 // Get the GOT section, creating it if necessary.
226 Output_data_got<64, false>*
227 got_section(Symbol_table*, Layout*);
229 // Get the GOT PLT section.
231 got_plt_section() const
233 gold_assert(this->got_plt_ != NULL);
234 return this->got_plt_;
237 // Create a PLT entry for a global symbol.
239 make_plt_entry(Symbol_table*, Layout*, Symbol*);
241 // Get the PLT section.
242 Output_data_plt_x86_64*
245 gold_assert(this->plt_ != NULL);
249 // Get the dynamic reloc section, creating it if necessary.
251 rela_dyn_section(Layout*);
253 // Return true if the symbol may need a COPY relocation.
254 // References from an executable object to non-function symbols
255 // defined in a dynamic object may need a COPY relocation.
257 may_need_copy_reloc(Symbol* gsym)
259 return (!parameters->output_is_shared()
260 && gsym->is_from_dynobj()
261 && gsym->type() != elfcpp::STT_FUNC);
264 // Copy a relocation against a global symbol.
266 copy_reloc(const General_options*, Symbol_table*, Layout*,
267 Sized_relobj<64, false>*, unsigned int,
268 Symbol*, const elfcpp::Rela<64, false>&);
270 // Information about this specific target which we pass to the
271 // general Target structure.
272 static const Target::Target_info x86_64_info;
275 Output_data_got<64, false>* got_;
277 Output_data_plt_x86_64* plt_;
278 // The GOT PLT section.
279 Output_data_space* got_plt_;
280 // The dynamic reloc section.
281 Reloc_section* rela_dyn_;
282 // Relocs saved to avoid a COPY reloc.
283 Copy_relocs<64, false>* copy_relocs_;
284 // Space for variables copied with a COPY reloc.
285 Output_data_space* dynbss_;
288 const Target::Target_info Target_x86_64::x86_64_info =
291 false, // is_big_endian
292 elfcpp::EM_X86_64, // machine_code
293 false, // has_make_symbol
294 false, // has_resolve
295 true, // has_code_fill
296 true, // is_default_stack_executable
297 "/lib/ld64.so.1", // program interpreter
298 0x400000, // default_text_segment_address
299 0x1000, // abi_pagesize
300 0x1000 // common_pagesize
303 // Get the GOT section, creating it if necessary.
305 Output_data_got<64, false>*
306 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
308 if (this->got_ == NULL)
310 gold_assert(symtab != NULL && layout != NULL);
312 this->got_ = new Output_data_got<64, false>();
314 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
315 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
318 // The old GNU linker creates a .got.plt section. We just
319 // create another set of data in the .got section. Note that we
320 // always create a PLT if we create a GOT, although the PLT
322 this->got_plt_ = new Output_data_space(8);
323 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
324 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
327 // The first three entries are reserved.
328 this->got_plt_->set_space_size(3 * 8);
330 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
331 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
333 0, 0, elfcpp::STT_OBJECT,
335 elfcpp::STV_HIDDEN, 0,
342 // Get the dynamic reloc section, creating it if necessary.
344 Target_x86_64::Reloc_section*
345 Target_x86_64::rela_dyn_section(Layout* layout)
347 if (this->rela_dyn_ == NULL)
349 gold_assert(layout != NULL);
350 this->rela_dyn_ = new Reloc_section();
351 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
352 elfcpp::SHF_ALLOC, this->rela_dyn_);
354 return this->rela_dyn_;
357 // A class to handle the PLT data.
359 class Output_data_plt_x86_64 : public Output_section_data
362 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
364 Output_data_plt_x86_64(Layout*, Output_data_space*);
366 // Add an entry to the PLT.
368 add_entry(Symbol* gsym);
370 // Return the .rel.plt section data.
373 { return this->rel_; }
377 do_adjust_output_section(Output_section* os);
380 // The size of an entry in the PLT.
381 static const int plt_entry_size = 16;
383 // The first entry in the PLT.
384 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
385 // procedure linkage table for both programs and shared objects."
386 static unsigned char first_plt_entry[plt_entry_size];
388 // Other entries in the PLT for an executable.
389 static unsigned char plt_entry[plt_entry_size];
391 // Set the final size.
393 do_set_address(uint64_t, off_t)
394 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
396 // Write out the PLT data.
398 do_write(Output_file*);
400 // The reloc section.
402 // The .got.plt section.
403 Output_data_space* got_plt_;
404 // The number of PLT entries.
408 // Create the PLT section. The ordinary .got section is an argument,
409 // since we need to refer to the start. We also create our own .got
410 // section just for PLT entries.
412 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
413 Output_data_space* got_plt)
414 : Output_section_data(8), got_plt_(got_plt), count_(0)
416 this->rel_ = new Reloc_section();
417 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
418 elfcpp::SHF_ALLOC, this->rel_);
422 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
424 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
425 // linker, and so do we.
429 // Add an entry to the PLT.
432 Output_data_plt_x86_64::add_entry(Symbol* gsym)
434 gold_assert(!gsym->has_plt_offset());
436 // Note that when setting the PLT offset we skip the initial
437 // reserved PLT entry.
438 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
442 off_t got_offset = this->got_plt_->data_size();
444 // Every PLT entry needs a GOT entry which points back to the PLT
445 // entry (this will be changed by the dynamic linker, normally
446 // lazily when the function is called).
447 this->got_plt_->set_space_size(got_offset + 8);
449 // Every PLT entry needs a reloc.
450 gsym->set_needs_dynsym_entry();
451 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
454 // Note that we don't need to save the symbol. The contents of the
455 // PLT are independent of which symbols are used. The symbols only
456 // appear in the relocations.
459 // The first entry in the PLT for an executable.
461 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
463 // From AMD64 ABI Draft 0.98, page 76
464 0xff, 0x35, // pushq contents of memory address
465 0, 0, 0, 0, // replaced with address of .got + 4
466 0xff, 0x25, // jmp indirect
467 0, 0, 0, 0, // replaced with address of .got + 8
468 0x90, 0x90, 0x90, 0x90 // noop (x4)
471 // Subsequent entries in the PLT for an executable.
473 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
475 // From AMD64 ABI Draft 0.98, page 76
476 0xff, 0x25, // jmpq indirect
477 0, 0, 0, 0, // replaced with address of symbol in .got
478 0x68, // pushq immediate
479 0, 0, 0, 0, // replaced with offset into relocation table
480 0xe9, // jmpq relative
481 0, 0, 0, 0 // replaced with offset to start of .plt
484 // Write out the PLT. This uses the hand-coded instructions above,
485 // and adjusts them as needed. This is specified by the AMD64 ABI.
488 Output_data_plt_x86_64::do_write(Output_file* of)
490 const off_t offset = this->offset();
491 const off_t oview_size = this->data_size();
492 unsigned char* const oview = of->get_output_view(offset, oview_size);
494 const off_t got_file_offset = this->got_plt_->offset();
495 const off_t got_size = this->got_plt_->data_size();
496 unsigned char* const got_view = of->get_output_view(got_file_offset,
499 unsigned char* pov = oview;
501 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
502 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
504 memcpy(pov, first_plt_entry, plt_entry_size);
505 if (!parameters->output_is_shared())
507 // We do a jmp relative to the PC at the end of this instruction.
508 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
509 - (plt_address + 6));
510 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
511 - (plt_address + 12));
513 pov += plt_entry_size;
515 unsigned char* got_pov = got_view;
517 memset(got_pov, 0, 24);
520 unsigned int plt_offset = plt_entry_size;
521 unsigned int got_offset = 24;
522 const unsigned int count = this->count_;
523 for (unsigned int plt_index = 0;
526 pov += plt_entry_size,
528 plt_offset += plt_entry_size,
531 // Set and adjust the PLT entry itself.
532 memcpy(pov, plt_entry, plt_entry_size);
533 if (parameters->output_is_shared())
534 // FIXME(csilvers): what's the right thing to write here?
535 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
537 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
538 (got_address + got_offset
539 - (plt_address + plt_offset
542 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
543 elfcpp::Swap<32, false>::writeval(pov + 12,
544 - (plt_offset + plt_entry_size));
546 // Set the entry in the GOT.
547 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
550 gold_assert(pov - oview == oview_size);
551 gold_assert(got_pov - got_view == got_size);
553 of->write_output_view(offset, oview_size, oview);
554 of->write_output_view(got_file_offset, got_size, got_view);
557 // Create a PLT entry for a global symbol.
560 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
563 if (gsym->has_plt_offset())
566 if (this->plt_ == NULL)
568 // Create the GOT sections first.
569 this->got_section(symtab, layout);
571 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
572 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
574 | elfcpp::SHF_EXECINSTR),
578 this->plt_->add_entry(gsym);
581 // Handle a relocation against a non-function symbol defined in a
582 // dynamic object. The traditional way to handle this is to generate
583 // a COPY relocation to copy the variable at runtime from the shared
584 // object into the executable's data segment. However, this is
585 // undesirable in general, as if the size of the object changes in the
586 // dynamic object, the executable will no longer work correctly. If
587 // this relocation is in a writable section, then we can create a
588 // dynamic reloc and the dynamic linker will resolve it to the correct
589 // address at runtime. However, we do not want do that if the
590 // relocation is in a read-only section, as it would prevent the
591 // readonly segment from being shared. And if we have to eventually
592 // generate a COPY reloc, then any dynamic relocations will be
593 // useless. So this means that if this is a writable section, we need
594 // to save the relocation until we see whether we have to create a
595 // COPY relocation for this symbol for any other relocation.
598 Target_x86_64::copy_reloc(const General_options* options,
599 Symbol_table* symtab,
601 Sized_relobj<64, false>* object,
602 unsigned int data_shndx, Symbol* gsym,
603 const elfcpp::Rela<64, false>& rela)
605 Sized_symbol<64>* ssym;
606 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
609 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
612 // So far we do not need a COPY reloc. Save this relocation.
613 // If it turns out that we never need a COPY reloc for this
614 // symbol, then we will emit the relocation.
615 if (this->copy_relocs_ == NULL)
616 this->copy_relocs_ = new Copy_relocs<64, false>();
617 this->copy_relocs_->save(ssym, object, data_shndx, rela);
621 // Allocate space for this symbol in the .bss section.
623 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
625 // There is no defined way to determine the required alignment
626 // of the symbol. We pick the alignment based on the size. We
627 // set an arbitrary maximum of 256.
629 for (align = 1; align < 512; align <<= 1)
630 if ((symsize & align) != 0)
633 if (this->dynbss_ == NULL)
635 this->dynbss_ = new Output_data_space(align);
636 layout->add_output_section_data(".bss",
639 | elfcpp::SHF_WRITE),
643 Output_data_space* dynbss = this->dynbss_;
645 if (align > dynbss->addralign())
646 dynbss->set_space_alignment(align);
648 off_t dynbss_size = dynbss->data_size();
649 dynbss_size = align_address(dynbss_size, align);
650 off_t offset = dynbss_size;
651 dynbss->set_space_size(dynbss_size + symsize);
653 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
655 // Add the COPY reloc.
656 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
657 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
662 // Optimize the TLS relocation type based on what we know about the
663 // symbol. IS_FINAL is true if the final address of this symbol is
664 // known at link time.
666 tls::Tls_optimization
667 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
669 // If we are generating a shared library, then we can't do anything
671 if (parameters->output_is_shared())
672 return tls::TLSOPT_NONE;
676 case elfcpp::R_X86_64_TLSGD:
677 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
678 case elfcpp::R_X86_64_TLSDESC_CALL:
679 // These are General-Dynamic which permits fully general TLS
680 // access. Since we know that we are generating an executable,
681 // we can convert this to Initial-Exec. If we also know that
682 // this is a local symbol, we can further switch to Local-Exec.
684 return tls::TLSOPT_TO_LE;
685 return tls::TLSOPT_TO_IE;
687 case elfcpp::R_X86_64_TLSLD:
688 // This is Local-Dynamic, which refers to a local symbol in the
689 // dynamic TLS block. Since we know that we generating an
690 // executable, we can switch to Local-Exec.
691 return tls::TLSOPT_TO_LE;
693 case elfcpp::R_X86_64_DTPOFF32:
694 case elfcpp::R_X86_64_DTPOFF64:
695 // Another Local-Dynamic reloc.
696 return tls::TLSOPT_TO_LE;
698 case elfcpp::R_X86_64_GOTTPOFF:
699 // These are Initial-Exec relocs which get the thread offset
700 // from the GOT. If we know that we are linking against the
701 // local symbol, we can switch to Local-Exec, which links the
702 // thread offset into the instruction.
704 return tls::TLSOPT_TO_LE;
705 return tls::TLSOPT_NONE;
707 case elfcpp::R_X86_64_TPOFF32:
708 // When we already have Local-Exec, there is nothing further we
710 return tls::TLSOPT_NONE;
717 // Report an unsupported relocation against a local symbol.
720 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
723 gold_error(_("%s: unsupported reloc %u against local symbol"),
724 object->name().c_str(), r_type);
727 // Scan a relocation for a local symbol.
730 Target_x86_64::Scan::local(const General_options&,
731 Symbol_table* symtab,
733 Target_x86_64* target,
734 Sized_relobj<64, false>* object,
735 unsigned int data_shndx,
736 const elfcpp::Rela<64, false>& reloc,
738 const elfcpp::Sym<64, false>&)
742 case elfcpp::R_X86_64_NONE:
743 case elfcpp::R_386_GNU_VTINHERIT:
744 case elfcpp::R_386_GNU_VTENTRY:
747 case elfcpp::R_X86_64_64:
748 // If building a shared library (or a position-independent
749 // executable), we need to create a dynamic relocation for
750 // this location. The relocation applied at link time will
751 // apply the link-time value, so we flag the location with
752 // an R_386_RELATIVE relocation so the dynamic loader can
753 // relocate it easily.
754 if (parameters->output_is_position_independent())
756 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
757 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
758 data_shndx, reloc.get_r_offset(), 0);
762 case elfcpp::R_X86_64_32:
763 case elfcpp::R_X86_64_32S:
764 case elfcpp::R_X86_64_16:
765 case elfcpp::R_X86_64_8:
766 // If building a shared library (or a position-independent
767 // executable), we need to create a dynamic relocation for
768 // this location. The relocation applied at link time will
769 // apply the link-time value, so we flag the location with
770 // an R_386_RELATIVE relocation so the dynamic loader can
771 // relocate it easily.
772 if (parameters->output_is_position_independent())
774 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
775 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
776 rela_dyn->add_local(object, r_sym, r_type, data_shndx,
777 reloc.get_r_offset(),
778 reloc.get_r_addend());
782 case elfcpp::R_X86_64_PC64:
783 case elfcpp::R_X86_64_PC32:
784 case elfcpp::R_X86_64_PC16:
785 case elfcpp::R_X86_64_PC8:
788 case elfcpp::R_X86_64_PLT32:
789 // Since we know this is a local symbol, we can handle this as a
793 case elfcpp::R_X86_64_GOTPC32:
794 case elfcpp::R_X86_64_GOTOFF64:
795 case elfcpp::R_X86_64_GOTPC64:
796 case elfcpp::R_X86_64_PLTOFF64:
797 // We need a GOT section.
798 target->got_section(symtab, layout);
799 // For PLTOFF64, we'd normally want a PLT section, but since we
800 // know this is a local symbol, no PLT is needed.
803 case elfcpp::R_X86_64_GOT64:
804 case elfcpp::R_X86_64_GOT32:
805 case elfcpp::R_X86_64_GOTPCREL64:
806 case elfcpp::R_X86_64_GOTPCREL:
807 case elfcpp::R_X86_64_GOTPLT64:
809 // The symbol requires a GOT entry.
810 Output_data_got<64, false>* got = target->got_section(symtab, layout);
811 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
812 if (got->add_local(object, r_sym))
814 // If we are generating a shared object, we need to add a
815 // dynamic RELATIVE relocation for this symbol.
816 if (parameters->output_is_position_independent())
818 // FIXME: R_X86_64_RELATIVE assumes a 64-bit relocation.
819 gold_assert(r_type != elfcpp::R_X86_64_GOT32);
821 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
822 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
823 data_shndx, reloc.get_r_offset(), 0);
826 // For GOTPLT64, we'd normally want a PLT section, but since
827 // we know this is a local symbol, no PLT is needed.
831 case elfcpp::R_X86_64_COPY:
832 case elfcpp::R_X86_64_GLOB_DAT:
833 case elfcpp::R_X86_64_JUMP_SLOT:
834 case elfcpp::R_X86_64_RELATIVE:
835 // These are outstanding tls relocs, which are unexpected when linking
836 case elfcpp::R_X86_64_TPOFF64:
837 case elfcpp::R_X86_64_DTPMOD64:
838 case elfcpp::R_X86_64_TLSDESC:
839 gold_error(_("%s: unexpected reloc %u in object file"),
840 object->name().c_str(), r_type);
843 // These are initial tls relocs, which are expected when linking
844 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
845 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
846 case elfcpp::R_X86_64_TLSDESC_CALL:
847 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
848 case elfcpp::R_X86_64_DTPOFF32:
849 case elfcpp::R_X86_64_DTPOFF64:
850 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
851 case elfcpp::R_X86_64_TPOFF32: // Local-exec
853 bool output_is_shared = parameters->output_is_shared();
854 const tls::Tls_optimization optimized_type
855 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
858 case elfcpp::R_X86_64_TLSGD: // General-dynamic
859 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
860 case elfcpp::R_X86_64_TLSDESC_CALL:
861 // FIXME: If not relaxing to LE, we need to generate
862 // DTPMOD64 and DTPOFF64 relocs.
863 if (optimized_type != tls::TLSOPT_TO_LE)
864 unsupported_reloc_local(object, r_type);
867 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
868 case elfcpp::R_X86_64_DTPOFF32:
869 case elfcpp::R_X86_64_DTPOFF64:
870 // FIXME: If not relaxing to LE, we need to generate a
872 if (optimized_type != tls::TLSOPT_TO_LE)
873 unsupported_reloc_local(object, r_type);
876 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
877 // FIXME: If not relaxing to LE, we need to generate a
879 if (optimized_type != tls::TLSOPT_TO_LE)
880 unsupported_reloc_local(object, r_type);
883 case elfcpp::R_X86_64_TPOFF32: // Local-exec
884 // FIXME: If generating a shared object, we need to copy
885 // this relocation into the object.
886 gold_assert(!output_is_shared);
895 case elfcpp::R_X86_64_SIZE32:
896 case elfcpp::R_X86_64_SIZE64:
898 gold_error(_("%s: unsupported reloc %u against local symbol"),
899 object->name().c_str(), r_type);
905 // Report an unsupported relocation against a global symbol.
908 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
912 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
913 object->name().c_str(), r_type, gsym->name());
916 // Scan a relocation for a global symbol.
919 Target_x86_64::Scan::global(const General_options& options,
920 Symbol_table* symtab,
922 Target_x86_64* target,
923 Sized_relobj<64, false>* object,
924 unsigned int data_shndx,
925 const elfcpp::Rela<64, false>& reloc,
931 case elfcpp::R_X86_64_NONE:
932 case elfcpp::R_386_GNU_VTINHERIT:
933 case elfcpp::R_386_GNU_VTENTRY:
936 case elfcpp::R_X86_64_64:
937 case elfcpp::R_X86_64_32:
938 case elfcpp::R_X86_64_32S:
939 case elfcpp::R_X86_64_16:
940 case elfcpp::R_X86_64_8:
942 // Make a PLT entry if necessary.
943 if (gsym->needs_plt_entry())
945 target->make_plt_entry(symtab, layout, gsym);
946 // Since this is not a PC-relative relocation, we may be
947 // taking the address of a function. In that case we need to
948 // set the entry in the dynamic symbol table to the address of
950 if (gsym->is_from_dynobj())
951 gsym->set_needs_dynsym_value();
953 // Make a dynamic relocation if necessary.
954 if (gsym->needs_dynamic_reloc(true, false))
956 if (target->may_need_copy_reloc(gsym))
958 target->copy_reloc(&options, symtab, layout, object, data_shndx,
961 else if (r_type == elfcpp::R_X86_64_64
962 && gsym->can_use_relative_reloc(false))
964 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
965 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
967 reloc.get_r_offset(), 0);
971 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
972 rela_dyn->add_global(gsym, r_type, object, data_shndx,
973 reloc.get_r_offset(),
974 reloc.get_r_addend());
980 case elfcpp::R_X86_64_PC64:
981 case elfcpp::R_X86_64_PC32:
982 case elfcpp::R_X86_64_PC16:
983 case elfcpp::R_X86_64_PC8:
985 // Make a PLT entry if necessary.
986 if (gsym->needs_plt_entry())
987 target->make_plt_entry(symtab, layout, gsym);
988 // Make a dynamic relocation if necessary.
989 bool is_function_call = (gsym->type() == elfcpp::STT_FUNC);
990 if (gsym->needs_dynamic_reloc(true, is_function_call))
992 if (target->may_need_copy_reloc(gsym))
994 target->copy_reloc(&options, symtab, layout, object, data_shndx,
999 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1000 rela_dyn->add_global(gsym, r_type, object, data_shndx,
1001 reloc.get_r_offset(),
1002 reloc.get_r_addend());
1008 case elfcpp::R_X86_64_GOT64:
1009 case elfcpp::R_X86_64_GOT32:
1010 case elfcpp::R_X86_64_GOTPCREL64:
1011 case elfcpp::R_X86_64_GOTPCREL:
1012 case elfcpp::R_X86_64_GOTPLT64:
1014 // The symbol requires a GOT entry.
1015 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1016 if (got->add_global(gsym))
1018 // If this symbol is not fully resolved, we need to add a
1019 // dynamic relocation for it.
1020 if (!gsym->final_value_is_known())
1022 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1023 if (gsym->is_from_dynobj()
1024 || gsym->is_preemptible())
1025 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
1026 gsym->got_offset(), 0);
1029 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
1030 got, gsym->got_offset(), 0);
1031 // Make sure we write the link-time value to the GOT.
1032 gsym->set_needs_value_in_got();
1036 // For GOTPLT64, we also need a PLT entry (but only if the
1037 // symbol is not fully resolved).
1038 if (r_type == elfcpp::R_X86_64_GOTPLT64
1039 && !gsym->final_value_is_known())
1040 target->make_plt_entry(symtab, layout, gsym);
1044 case elfcpp::R_X86_64_PLT32:
1045 // If the symbol is fully resolved, this is just a PC32 reloc.
1046 // Otherwise we need a PLT entry.
1047 if (gsym->final_value_is_known())
1049 // If building a shared library, we can also skip the PLT entry
1050 // if the symbol is defined in the output file and is protected
1052 if (gsym->is_defined()
1053 && !gsym->is_from_dynobj()
1054 && !gsym->is_preemptible())
1056 target->make_plt_entry(symtab, layout, gsym);
1059 case elfcpp::R_X86_64_GOTPC32:
1060 case elfcpp::R_X86_64_GOTOFF64:
1061 case elfcpp::R_X86_64_GOTPC64:
1062 case elfcpp::R_X86_64_PLTOFF64:
1063 // We need a GOT section.
1064 target->got_section(symtab, layout);
1065 // For PLTOFF64, we also need a PLT entry (but only if the
1066 // symbol is not fully resolved).
1067 if (r_type == elfcpp::R_X86_64_PLTOFF64
1068 && !gsym->final_value_is_known())
1069 target->make_plt_entry(symtab, layout, gsym);
1072 case elfcpp::R_X86_64_COPY:
1073 case elfcpp::R_X86_64_GLOB_DAT:
1074 case elfcpp::R_X86_64_JUMP_SLOT:
1075 case elfcpp::R_X86_64_RELATIVE:
1076 // These are outstanding tls relocs, which are unexpected when linking
1077 case elfcpp::R_X86_64_TPOFF64:
1078 case elfcpp::R_X86_64_DTPMOD64:
1079 case elfcpp::R_X86_64_TLSDESC:
1080 gold_error(_("%s: unexpected reloc %u in object file"),
1081 object->name().c_str(), r_type);
1084 // These are initial tls relocs, which are expected for global()
1085 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1086 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1087 case elfcpp::R_X86_64_TLSDESC_CALL:
1088 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1089 case elfcpp::R_X86_64_DTPOFF32:
1090 case elfcpp::R_X86_64_DTPOFF64:
1091 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1092 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1094 const bool is_final = gsym->final_value_is_known();
1095 const tls::Tls_optimization optimized_type
1096 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1099 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1100 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1101 case elfcpp::R_X86_64_TLSDESC_CALL:
1102 // FIXME: If not relaxing to LE, we need to generate
1103 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1104 if (optimized_type != tls::TLSOPT_TO_LE)
1105 unsupported_reloc_global(object, r_type, gsym);
1108 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1109 case elfcpp::R_X86_64_DTPOFF32:
1110 case elfcpp::R_X86_64_DTPOFF64:
1111 // FIXME: If not relaxing to LE, we need to generate a
1113 if (optimized_type != tls::TLSOPT_TO_LE)
1114 unsupported_reloc_global(object, r_type, gsym);
1117 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1118 // FIXME: If not relaxing to LE, we need to generate a
1120 if (optimized_type != tls::TLSOPT_TO_LE)
1121 unsupported_reloc_global(object, r_type, gsym);
1124 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1125 // FIXME: If generating a shared object, we need to copy
1126 // this relocation into the object.
1127 gold_assert(is_final);
1136 case elfcpp::R_X86_64_SIZE32:
1137 case elfcpp::R_X86_64_SIZE64:
1139 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1140 object->name().c_str(), r_type, gsym->name());
1145 // Scan relocations for a section.
1148 Target_x86_64::scan_relocs(const General_options& options,
1149 Symbol_table* symtab,
1151 Sized_relobj<64, false>* object,
1152 unsigned int data_shndx,
1153 unsigned int sh_type,
1154 const unsigned char* prelocs,
1156 Output_section* output_section,
1157 bool needs_special_offset_handling,
1158 size_t local_symbol_count,
1159 const unsigned char* plocal_symbols)
1161 if (sh_type == elfcpp::SHT_REL)
1163 gold_error(_("%s: unsupported REL reloc section"),
1164 object->name().c_str());
1168 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1169 Target_x86_64::Scan>(
1179 needs_special_offset_handling,
1184 // Finalize the sections.
1187 Target_x86_64::do_finalize_sections(Layout* layout)
1189 // Fill in some more dynamic tags.
1190 Output_data_dynamic* const odyn = layout->dynamic_data();
1193 if (this->got_plt_ != NULL)
1194 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1196 if (this->plt_ != NULL)
1198 const Output_data* od = this->plt_->rel_plt();
1199 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1200 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1201 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1204 if (this->rela_dyn_ != NULL)
1206 const Output_data* od = this->rela_dyn_;
1207 odyn->add_section_address(elfcpp::DT_RELA, od);
1208 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1209 odyn->add_constant(elfcpp::DT_RELAENT,
1210 elfcpp::Elf_sizes<64>::rela_size);
1213 if (!parameters->output_is_shared())
1215 // The value of the DT_DEBUG tag is filled in by the dynamic
1216 // linker at run time, and used by the debugger.
1217 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1221 // Emit any relocs we saved in an attempt to avoid generating COPY
1223 if (this->copy_relocs_ == NULL)
1225 if (this->copy_relocs_->any_to_emit())
1227 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1228 this->copy_relocs_->emit(rela_dyn);
1230 delete this->copy_relocs_;
1231 this->copy_relocs_ = NULL;
1234 // Perform a relocation.
1237 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1238 Target_x86_64* target,
1240 const elfcpp::Rela<64, false>& rela,
1241 unsigned int r_type,
1242 const Sized_symbol<64>* gsym,
1243 const Symbol_value<64>* psymval,
1244 unsigned char* view,
1245 elfcpp::Elf_types<64>::Elf_Addr address,
1248 if (this->skip_call_tls_get_addr_)
1250 if (r_type != elfcpp::R_X86_64_PLT32
1252 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1254 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1255 _("missing expected TLS relocation"));
1259 this->skip_call_tls_get_addr_ = false;
1264 // Pick the value to use for symbols defined in shared objects.
1265 Symbol_value<64> symval;
1267 && (gsym->is_from_dynobj()
1268 || (parameters->output_is_shared()
1269 && gsym->is_preemptible()))
1270 && gsym->has_plt_offset())
1272 symval.set_output_value(target->plt_section()->address()
1273 + gsym->plt_offset());
1277 const Sized_relobj<64, false>* object = relinfo->object;
1278 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1280 // Get the GOT offset if needed.
1281 // The GOT pointer points to the end of the GOT section.
1282 // We need to subtract the size of the GOT section to get
1283 // the actual offset to use in the relocation.
1284 bool have_got_offset = false;
1285 unsigned int got_offset = 0;
1288 case elfcpp::R_X86_64_GOT32:
1289 case elfcpp::R_X86_64_GOT64:
1290 case elfcpp::R_X86_64_GOTPLT64:
1291 case elfcpp::R_X86_64_GOTPCREL:
1292 case elfcpp::R_X86_64_GOTPCREL64:
1295 gold_assert(gsym->has_got_offset());
1296 got_offset = gsym->got_offset() - target->got_size();
1300 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1301 got_offset = object->local_got_offset(r_sym) - target->got_size();
1303 have_got_offset = true;
1312 case elfcpp::R_X86_64_NONE:
1313 case elfcpp::R_386_GNU_VTINHERIT:
1314 case elfcpp::R_386_GNU_VTENTRY:
1317 case elfcpp::R_X86_64_64:
1318 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1321 case elfcpp::R_X86_64_PC64:
1322 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1326 case elfcpp::R_X86_64_32:
1327 // FIXME: we need to verify that value + addend fits into 32 bits:
1328 // uint64_t x = value + addend;
1329 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1330 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1331 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1334 case elfcpp::R_X86_64_32S:
1335 // FIXME: we need to verify that value + addend fits into 32 bits:
1336 // int64_t x = value + addend; // note this quantity is signed!
1337 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1338 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1341 case elfcpp::R_X86_64_PC32:
1342 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1346 case elfcpp::R_X86_64_16:
1347 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1350 case elfcpp::R_X86_64_PC16:
1351 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1355 case elfcpp::R_X86_64_8:
1356 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1359 case elfcpp::R_X86_64_PC8:
1360 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1364 case elfcpp::R_X86_64_PLT32:
1365 gold_assert(gsym == NULL
1366 || gsym->has_plt_offset()
1367 || gsym->final_value_is_known());
1368 // Note: while this code looks the same as for R_X86_64_PC32, it
1369 // behaves differently because psymval was set to point to
1370 // the PLT entry, rather than the symbol, in Scan::global().
1371 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1375 case elfcpp::R_X86_64_PLTOFF64:
1378 gold_assert(gsym->has_plt_offset()
1379 || gsym->final_value_is_known());
1380 elfcpp::Elf_types<64>::Elf_Addr got_address;
1381 got_address = target->got_section(NULL, NULL)->address();
1382 Relocate_functions<64, false>::rela64(view, object, psymval,
1383 addend - got_address);
1386 case elfcpp::R_X86_64_GOT32:
1387 gold_assert(have_got_offset);
1388 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1391 case elfcpp::R_X86_64_GOTPC32:
1394 elfcpp::Elf_types<64>::Elf_Addr value;
1395 value = target->got_plt_section()->address();
1396 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1400 case elfcpp::R_X86_64_GOT64:
1401 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1402 // Since we always add a PLT entry, this is equivalent.
1403 case elfcpp::R_X86_64_GOTPLT64:
1404 gold_assert(have_got_offset);
1405 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1408 case elfcpp::R_X86_64_GOTPC64:
1411 elfcpp::Elf_types<64>::Elf_Addr value;
1412 value = target->got_plt_section()->address();
1413 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1417 case elfcpp::R_X86_64_GOTOFF64:
1419 elfcpp::Elf_types<64>::Elf_Addr value;
1420 value = (psymval->value(object, 0)
1421 - target->got_plt_section()->address());
1422 Relocate_functions<64, false>::rela64(view, value, addend);
1426 case elfcpp::R_X86_64_GOTPCREL:
1428 gold_assert(have_got_offset);
1429 elfcpp::Elf_types<64>::Elf_Addr value;
1430 value = target->got_plt_section()->address() + got_offset;
1431 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1435 case elfcpp::R_X86_64_GOTPCREL64:
1437 gold_assert(have_got_offset);
1438 elfcpp::Elf_types<64>::Elf_Addr value;
1439 value = target->got_plt_section()->address() + got_offset;
1440 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1444 case elfcpp::R_X86_64_COPY:
1445 case elfcpp::R_X86_64_GLOB_DAT:
1446 case elfcpp::R_X86_64_JUMP_SLOT:
1447 case elfcpp::R_X86_64_RELATIVE:
1448 // These are outstanding tls relocs, which are unexpected when linking
1449 case elfcpp::R_X86_64_TPOFF64:
1450 case elfcpp::R_X86_64_DTPMOD64:
1451 case elfcpp::R_X86_64_TLSDESC:
1452 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1453 _("unexpected reloc %u in object file"),
1457 // These are initial tls relocs, which are expected when linking
1458 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1459 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1460 case elfcpp::R_X86_64_TLSDESC_CALL:
1461 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1462 case elfcpp::R_X86_64_DTPOFF32:
1463 case elfcpp::R_X86_64_DTPOFF64:
1464 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1465 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1466 this->relocate_tls(relinfo, relnum, rela, r_type, gsym, psymval, view,
1467 address, view_size);
1470 case elfcpp::R_X86_64_SIZE32:
1471 case elfcpp::R_X86_64_SIZE64:
1473 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1474 _("unsupported reloc %u"),
1482 // Perform a TLS relocation.
1485 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1487 const elfcpp::Rela<64, false>& rela,
1488 unsigned int r_type,
1489 const Sized_symbol<64>* gsym,
1490 const Symbol_value<64>* psymval,
1491 unsigned char* view,
1492 elfcpp::Elf_types<64>::Elf_Addr,
1495 Output_segment* tls_segment = relinfo->layout->tls_segment();
1496 if (tls_segment == NULL)
1498 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1499 _("TLS reloc but no TLS segment"));
1503 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1505 const bool is_final = (gsym == NULL
1506 ? !parameters->output_is_position_independent()
1507 : gsym->final_value_is_known());
1508 const tls::Tls_optimization optimized_type
1509 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1512 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1513 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1514 case elfcpp::R_X86_64_TLSDESC_CALL:
1515 if (optimized_type == tls::TLSOPT_TO_LE)
1517 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1518 rela, r_type, value, view,
1522 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1523 _("unsupported reloc %u"), r_type);
1526 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1527 if (optimized_type == tls::TLSOPT_TO_LE)
1529 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1530 value, view, view_size);
1533 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1534 _("unsupported reloc %u"), r_type);
1537 case elfcpp::R_X86_64_DTPOFF32:
1538 if (optimized_type == tls::TLSOPT_TO_LE)
1539 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1541 value = value - tls_segment->vaddr();
1542 Relocate_functions<64, false>::rel32(view, value);
1545 case elfcpp::R_X86_64_DTPOFF64:
1546 if (optimized_type == tls::TLSOPT_TO_LE)
1547 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1549 value = value - tls_segment->vaddr();
1550 Relocate_functions<64, false>::rel64(view, value);
1553 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1554 if (optimized_type == tls::TLSOPT_TO_LE)
1556 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1557 rela, r_type, value, view,
1561 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1562 _("unsupported reloc type %u"),
1566 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1567 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1568 Relocate_functions<64, false>::rel32(view, value);
1573 // Do a relocation in which we convert a TLS General-Dynamic to a
1577 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1579 Output_segment* tls_segment,
1580 const elfcpp::Rela<64, false>& rela,
1582 elfcpp::Elf_types<64>::Elf_Addr value,
1583 unsigned char* view,
1586 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1587 // .word 0x6666; rex64; call __tls_get_addr
1588 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1590 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1591 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1593 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1594 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1595 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1596 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1598 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1600 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1601 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1603 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1605 this->skip_call_tls_get_addr_ = true;
1609 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1612 const elfcpp::Rela<64, false>& rela,
1614 elfcpp::Elf_types<64>::Elf_Addr,
1615 unsigned char* view,
1618 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1619 // ... leq foo@dtpoff(%rax),%reg
1620 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1622 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1623 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1625 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1626 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1628 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1630 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1632 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1634 this->skip_call_tls_get_addr_ = true;
1637 // Do a relocation in which we convert a TLS Initial-Exec to a
1641 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1643 Output_segment* tls_segment,
1644 const elfcpp::Rela<64, false>& rela,
1646 elfcpp::Elf_types<64>::Elf_Addr value,
1647 unsigned char* view,
1650 // We need to examine the opcodes to figure out which instruction we
1653 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1654 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1656 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1657 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1659 unsigned char op1 = view[-3];
1660 unsigned char op2 = view[-2];
1661 unsigned char op3 = view[-1];
1662 unsigned char reg = op3 >> 3;
1670 view[-1] = 0xc0 | reg;
1674 // Special handling for %rsp.
1678 view[-1] = 0xc0 | reg;
1686 view[-1] = 0x80 | reg | (reg << 3);
1689 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1690 Relocate_functions<64, false>::rela32(view, value, 0);
1693 // Relocate section data.
1696 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1697 unsigned int sh_type,
1698 const unsigned char* prelocs,
1700 Output_section* output_section,
1701 bool needs_special_offset_handling,
1702 unsigned char* view,
1703 elfcpp::Elf_types<64>::Elf_Addr address,
1706 gold_assert(sh_type == elfcpp::SHT_RELA);
1708 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1709 Target_x86_64::Relocate>(
1715 needs_special_offset_handling,
1721 // Return the value to use for a dynamic which requires special
1722 // treatment. This is how we support equality comparisons of function
1723 // pointers across shared library boundaries, as described in the
1724 // processor specific ABI supplement.
1727 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1729 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1730 return this->plt_section()->address() + gsym->plt_offset();
1733 // Return a string used to fill a code section with nops to take up
1734 // the specified length.
1737 Target_x86_64::do_code_fill(off_t length)
1741 // Build a jmpq instruction to skip over the bytes.
1742 unsigned char jmp[5];
1744 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1745 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1746 + std::string(length - 5, '\0'));
1749 // Nop sequences of various lengths.
1750 const char nop1[1] = { 0x90 }; // nop
1751 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1752 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1753 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1754 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1755 0x00 }; // leal 0(%esi,1),%esi
1756 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1758 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1760 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1761 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1762 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1763 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1765 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1766 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1768 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1769 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1771 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1772 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1773 0x00, 0x00, 0x00, 0x00 };
1774 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1775 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1776 0x27, 0x00, 0x00, 0x00,
1778 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1779 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1780 0xbc, 0x27, 0x00, 0x00,
1782 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1783 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1784 0x90, 0x90, 0x90, 0x90,
1787 const char* nops[16] = {
1789 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1790 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1793 return std::string(nops[length], length);
1796 // The selector for x86_64 object files.
1798 class Target_selector_x86_64 : public Target_selector
1801 Target_selector_x86_64()
1802 : Target_selector(elfcpp::EM_X86_64, 64, false)
1806 recognize(int machine, int osabi, int abiversion);
1809 Target_x86_64* target_;
1812 // Recognize an x86_64 object file when we already know that the machine
1813 // number is EM_X86_64.
1816 Target_selector_x86_64::recognize(int, int, int)
1818 if (this->target_ == NULL)
1819 this->target_ = new Target_x86_64();
1820 return this->target_;
1823 Target_selector_x86_64 target_selector_x86_64;
1825 } // End anonymous namespace.