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 // Copy a relocation against a global symbol.
255 copy_reloc(const General_options*, Symbol_table*, Layout*,
256 Sized_relobj<64, false>*, unsigned int,
257 Symbol*, const elfcpp::Rela<64, false>&);
259 // Information about this specific target which we pass to the
260 // general Target structure.
261 static const Target::Target_info x86_64_info;
264 Output_data_got<64, false>* got_;
266 Output_data_plt_x86_64* plt_;
267 // The GOT PLT section.
268 Output_data_space* got_plt_;
269 // The dynamic reloc section.
270 Reloc_section* rela_dyn_;
271 // Relocs saved to avoid a COPY reloc.
272 Copy_relocs<64, false>* copy_relocs_;
273 // Space for variables copied with a COPY reloc.
274 Output_data_space* dynbss_;
277 const Target::Target_info Target_x86_64::x86_64_info =
280 false, // is_big_endian
281 elfcpp::EM_X86_64, // machine_code
282 false, // has_make_symbol
283 false, // has_resolve
284 true, // has_code_fill
285 true, // is_default_stack_executable
286 "/lib/ld64.so.1", // program interpreter
287 0x400000, // default_text_segment_address
288 0x1000, // abi_pagesize
289 0x1000 // common_pagesize
292 // Get the GOT section, creating it if necessary.
294 Output_data_got<64, false>*
295 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
297 if (this->got_ == NULL)
299 gold_assert(symtab != NULL && layout != NULL);
301 this->got_ = new Output_data_got<64, false>();
303 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
304 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
307 // The old GNU linker creates a .got.plt section. We just
308 // create another set of data in the .got section. Note that we
309 // always create a PLT if we create a GOT, although the PLT
311 this->got_plt_ = new Output_data_space(8);
312 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
313 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
316 // The first three entries are reserved.
317 this->got_plt_->set_space_size(3 * 8);
319 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
320 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
322 0, 0, elfcpp::STT_OBJECT,
324 elfcpp::STV_HIDDEN, 0,
331 // Get the dynamic reloc section, creating it if necessary.
333 Target_x86_64::Reloc_section*
334 Target_x86_64::rela_dyn_section(Layout* layout)
336 if (this->rela_dyn_ == NULL)
338 gold_assert(layout != NULL);
339 this->rela_dyn_ = new Reloc_section();
340 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
341 elfcpp::SHF_ALLOC, this->rela_dyn_);
343 return this->rela_dyn_;
346 // A class to handle the PLT data.
348 class Output_data_plt_x86_64 : public Output_section_data
351 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
353 Output_data_plt_x86_64(Layout*, Output_data_space*);
355 // Add an entry to the PLT.
357 add_entry(Symbol* gsym);
359 // Return the .rel.plt section data.
362 { return this->rel_; }
366 do_adjust_output_section(Output_section* os);
369 // The size of an entry in the PLT.
370 static const int plt_entry_size = 16;
372 // The first entry in the PLT.
373 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
374 // procedure linkage table for both programs and shared objects."
375 static unsigned char first_plt_entry[plt_entry_size];
377 // Other entries in the PLT for an executable.
378 static unsigned char plt_entry[plt_entry_size];
380 // Set the final size.
382 do_set_address(uint64_t, off_t)
383 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
385 // Write out the PLT data.
387 do_write(Output_file*);
389 // The reloc section.
391 // The .got.plt section.
392 Output_data_space* got_plt_;
393 // The number of PLT entries.
397 // Create the PLT section. The ordinary .got section is an argument,
398 // since we need to refer to the start. We also create our own .got
399 // section just for PLT entries.
401 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
402 Output_data_space* got_plt)
403 : Output_section_data(8), got_plt_(got_plt), count_(0)
405 this->rel_ = new Reloc_section();
406 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
407 elfcpp::SHF_ALLOC, this->rel_);
411 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
413 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
414 // linker, and so do we.
418 // Add an entry to the PLT.
421 Output_data_plt_x86_64::add_entry(Symbol* gsym)
423 gold_assert(!gsym->has_plt_offset());
425 // Note that when setting the PLT offset we skip the initial
426 // reserved PLT entry.
427 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
431 off_t got_offset = this->got_plt_->data_size();
433 // Every PLT entry needs a GOT entry which points back to the PLT
434 // entry (this will be changed by the dynamic linker, normally
435 // lazily when the function is called).
436 this->got_plt_->set_space_size(got_offset + 8);
438 // Every PLT entry needs a reloc.
439 gsym->set_needs_dynsym_entry();
440 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
443 // Note that we don't need to save the symbol. The contents of the
444 // PLT are independent of which symbols are used. The symbols only
445 // appear in the relocations.
448 // The first entry in the PLT for an executable.
450 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
452 // From AMD64 ABI Draft 0.98, page 76
453 0xff, 0x35, // pushq contents of memory address
454 0, 0, 0, 0, // replaced with address of .got + 4
455 0xff, 0x25, // jmp indirect
456 0, 0, 0, 0, // replaced with address of .got + 8
457 0x90, 0x90, 0x90, 0x90 // noop (x4)
460 // Subsequent entries in the PLT for an executable.
462 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
464 // From AMD64 ABI Draft 0.98, page 76
465 0xff, 0x25, // jmpq indirect
466 0, 0, 0, 0, // replaced with address of symbol in .got
467 0x68, // pushq immediate
468 0, 0, 0, 0, // replaced with offset into relocation table
469 0xe9, // jmpq relative
470 0, 0, 0, 0 // replaced with offset to start of .plt
473 // Write out the PLT. This uses the hand-coded instructions above,
474 // and adjusts them as needed. This is specified by the AMD64 ABI.
477 Output_data_plt_x86_64::do_write(Output_file* of)
479 const off_t offset = this->offset();
480 const off_t oview_size = this->data_size();
481 unsigned char* const oview = of->get_output_view(offset, oview_size);
483 const off_t got_file_offset = this->got_plt_->offset();
484 const off_t got_size = this->got_plt_->data_size();
485 unsigned char* const got_view = of->get_output_view(got_file_offset,
488 unsigned char* pov = oview;
490 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
491 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
493 memcpy(pov, first_plt_entry, plt_entry_size);
494 if (!parameters->output_is_shared())
496 // We do a jmp relative to the PC at the end of this instruction.
497 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
498 - (plt_address + 6));
499 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
500 - (plt_address + 12));
502 pov += plt_entry_size;
504 unsigned char* got_pov = got_view;
506 memset(got_pov, 0, 24);
509 unsigned int plt_offset = plt_entry_size;
510 unsigned int got_offset = 24;
511 const unsigned int count = this->count_;
512 for (unsigned int plt_index = 0;
515 pov += plt_entry_size,
517 plt_offset += plt_entry_size,
520 // Set and adjust the PLT entry itself.
521 memcpy(pov, plt_entry, plt_entry_size);
522 if (parameters->output_is_shared())
523 // FIXME(csilvers): what's the right thing to write here?
524 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
526 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
527 (got_address + got_offset
528 - (plt_address + plt_offset
531 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
532 elfcpp::Swap<32, false>::writeval(pov + 12,
533 - (plt_offset + plt_entry_size));
535 // Set the entry in the GOT.
536 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
539 gold_assert(pov - oview == oview_size);
540 gold_assert(got_pov - got_view == got_size);
542 of->write_output_view(offset, oview_size, oview);
543 of->write_output_view(got_file_offset, got_size, got_view);
546 // Create a PLT entry for a global symbol.
549 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
552 if (gsym->has_plt_offset())
555 if (this->plt_ == NULL)
557 // Create the GOT sections first.
558 this->got_section(symtab, layout);
560 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
561 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
563 | elfcpp::SHF_EXECINSTR),
567 this->plt_->add_entry(gsym);
570 // Handle a relocation against a non-function symbol defined in a
571 // dynamic object. The traditional way to handle this is to generate
572 // a COPY relocation to copy the variable at runtime from the shared
573 // object into the executable's data segment. However, this is
574 // undesirable in general, as if the size of the object changes in the
575 // dynamic object, the executable will no longer work correctly. If
576 // this relocation is in a writable section, then we can create a
577 // dynamic reloc and the dynamic linker will resolve it to the correct
578 // address at runtime. However, we do not want do that if the
579 // relocation is in a read-only section, as it would prevent the
580 // readonly segment from being shared. And if we have to eventually
581 // generate a COPY reloc, then any dynamic relocations will be
582 // useless. So this means that if this is a writable section, we need
583 // to save the relocation until we see whether we have to create a
584 // COPY relocation for this symbol for any other relocation.
587 Target_x86_64::copy_reloc(const General_options* options,
588 Symbol_table* symtab,
590 Sized_relobj<64, false>* object,
591 unsigned int data_shndx, Symbol* gsym,
592 const elfcpp::Rela<64, false>& rela)
594 Sized_symbol<64>* ssym;
595 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
598 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
601 // So far we do not need a COPY reloc. Save this relocation.
602 // If it turns out that we never need a COPY reloc for this
603 // symbol, then we will emit the relocation.
604 if (this->copy_relocs_ == NULL)
605 this->copy_relocs_ = new Copy_relocs<64, false>();
606 this->copy_relocs_->save(ssym, object, data_shndx, rela);
610 // Allocate space for this symbol in the .bss section.
612 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
614 // There is no defined way to determine the required alignment
615 // of the symbol. We pick the alignment based on the size. We
616 // set an arbitrary maximum of 256.
618 for (align = 1; align < 512; align <<= 1)
619 if ((symsize & align) != 0)
622 if (this->dynbss_ == NULL)
624 this->dynbss_ = new Output_data_space(align);
625 layout->add_output_section_data(".bss",
628 | elfcpp::SHF_WRITE),
632 Output_data_space* dynbss = this->dynbss_;
634 if (align > dynbss->addralign())
635 dynbss->set_space_alignment(align);
637 off_t dynbss_size = dynbss->data_size();
638 dynbss_size = align_address(dynbss_size, align);
639 off_t offset = dynbss_size;
640 dynbss->set_space_size(dynbss_size + symsize);
642 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
644 // Add the COPY reloc.
645 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
646 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
651 // Optimize the TLS relocation type based on what we know about the
652 // symbol. IS_FINAL is true if the final address of this symbol is
653 // known at link time.
655 tls::Tls_optimization
656 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
658 // If we are generating a shared library, then we can't do anything
660 if (parameters->output_is_shared())
661 return tls::TLSOPT_NONE;
665 case elfcpp::R_X86_64_TLSGD:
666 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
667 case elfcpp::R_X86_64_TLSDESC_CALL:
668 // These are General-Dynamic which permits fully general TLS
669 // access. Since we know that we are generating an executable,
670 // we can convert this to Initial-Exec. If we also know that
671 // this is a local symbol, we can further switch to Local-Exec.
673 return tls::TLSOPT_TO_LE;
674 return tls::TLSOPT_TO_IE;
676 case elfcpp::R_X86_64_TLSLD:
677 // This is Local-Dynamic, which refers to a local symbol in the
678 // dynamic TLS block. Since we know that we generating an
679 // executable, we can switch to Local-Exec.
680 return tls::TLSOPT_TO_LE;
682 case elfcpp::R_X86_64_DTPOFF32:
683 case elfcpp::R_X86_64_DTPOFF64:
684 // Another Local-Dynamic reloc.
685 return tls::TLSOPT_TO_LE;
687 case elfcpp::R_X86_64_GOTTPOFF:
688 // These are Initial-Exec relocs which get the thread offset
689 // from the GOT. If we know that we are linking against the
690 // local symbol, we can switch to Local-Exec, which links the
691 // thread offset into the instruction.
693 return tls::TLSOPT_TO_LE;
694 return tls::TLSOPT_NONE;
696 case elfcpp::R_X86_64_TPOFF32:
697 // When we already have Local-Exec, there is nothing further we
699 return tls::TLSOPT_NONE;
706 // Report an unsupported relocation against a local symbol.
709 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
712 gold_error(_("%s: unsupported reloc %u against local symbol"),
713 object->name().c_str(), r_type);
716 // Scan a relocation for a local symbol.
719 Target_x86_64::Scan::local(const General_options&,
720 Symbol_table* symtab,
722 Target_x86_64* target,
723 Sized_relobj<64, false>* object,
724 unsigned int data_shndx,
725 const elfcpp::Rela<64, false>& reloc,
727 const elfcpp::Sym<64, false>&)
731 case elfcpp::R_X86_64_NONE:
732 case elfcpp::R_386_GNU_VTINHERIT:
733 case elfcpp::R_386_GNU_VTENTRY:
736 case elfcpp::R_X86_64_64:
737 case elfcpp::R_X86_64_32:
738 case elfcpp::R_X86_64_32S:
739 case elfcpp::R_X86_64_16:
740 case elfcpp::R_X86_64_8:
741 // If building a shared library (or a position-independent
742 // executable), we need to create a dynamic relocation for
743 // this location. The relocation applied at link time will
744 // apply the link-time value, so we flag the location with
745 // an R_386_RELATIVE relocation so the dynamic loader can
746 // relocate it easily.
747 if (parameters->output_is_position_independent())
749 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
750 if (r_type == elfcpp::R_X86_64_64)
751 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
752 data_shndx, reloc.get_r_offset(), 0);
755 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
756 rela_dyn->add_local(object, r_sym, r_type, data_shndx,
757 reloc.get_r_offset(),
758 reloc.get_r_addend());
763 case elfcpp::R_X86_64_PC64:
764 case elfcpp::R_X86_64_PC32:
765 case elfcpp::R_X86_64_PC16:
766 case elfcpp::R_X86_64_PC8:
769 case elfcpp::R_X86_64_PLT32:
770 // Since we know this is a local symbol, we can handle this as a
774 case elfcpp::R_X86_64_GOTPC32:
775 case elfcpp::R_X86_64_GOTOFF64:
776 case elfcpp::R_X86_64_GOTPC64:
777 case elfcpp::R_X86_64_PLTOFF64:
778 // We need a GOT section.
779 target->got_section(symtab, layout);
780 // For PLTOFF64, we'd normally want a PLT section, but since we
781 // know this is a local symbol, no PLT is needed.
784 case elfcpp::R_X86_64_GOT64:
785 case elfcpp::R_X86_64_GOT32:
786 case elfcpp::R_X86_64_GOTPCREL64:
787 case elfcpp::R_X86_64_GOTPCREL:
788 case elfcpp::R_X86_64_GOTPLT64:
790 // The symbol requires a GOT entry.
791 Output_data_got<64, false>* got = target->got_section(symtab, layout);
792 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
793 if (got->add_local(object, r_sym))
795 // If we are generating a shared object, we need to add a
796 // dynamic RELATIVE relocation for this symbol.
797 if (parameters->output_is_position_independent())
799 // FIXME: R_X86_64_RELATIVE assumes a 64-bit relocation.
800 gold_assert(r_type != elfcpp::R_X86_64_GOT32);
802 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
803 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
804 data_shndx, reloc.get_r_offset(), 0);
807 // For GOTPLT64, we'd normally want a PLT section, but since
808 // we know this is a local symbol, no PLT is needed.
812 case elfcpp::R_X86_64_COPY:
813 case elfcpp::R_X86_64_GLOB_DAT:
814 case elfcpp::R_X86_64_JUMP_SLOT:
815 case elfcpp::R_X86_64_RELATIVE:
816 // These are outstanding tls relocs, which are unexpected when linking
817 case elfcpp::R_X86_64_TPOFF64:
818 case elfcpp::R_X86_64_DTPMOD64:
819 case elfcpp::R_X86_64_TLSDESC:
820 gold_error(_("%s: unexpected reloc %u in object file"),
821 object->name().c_str(), r_type);
824 // These are initial tls relocs, which are expected when linking
825 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
826 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
827 case elfcpp::R_X86_64_TLSDESC_CALL:
828 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
829 case elfcpp::R_X86_64_DTPOFF32:
830 case elfcpp::R_X86_64_DTPOFF64:
831 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
832 case elfcpp::R_X86_64_TPOFF32: // Local-exec
834 bool output_is_shared = parameters->output_is_shared();
835 const tls::Tls_optimization optimized_type
836 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
839 case elfcpp::R_X86_64_TLSGD: // General-dynamic
840 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
841 case elfcpp::R_X86_64_TLSDESC_CALL:
842 // FIXME: If not relaxing to LE, we need to generate
843 // DTPMOD64 and DTPOFF64 relocs.
844 if (optimized_type != tls::TLSOPT_TO_LE)
845 unsupported_reloc_local(object, r_type);
848 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
849 case elfcpp::R_X86_64_DTPOFF32:
850 case elfcpp::R_X86_64_DTPOFF64:
851 // FIXME: If not relaxing to LE, we need to generate a
853 if (optimized_type != tls::TLSOPT_TO_LE)
854 unsupported_reloc_local(object, r_type);
857 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
858 // FIXME: If not relaxing to LE, we need to generate a
860 if (optimized_type != tls::TLSOPT_TO_LE)
861 unsupported_reloc_local(object, r_type);
864 case elfcpp::R_X86_64_TPOFF32: // Local-exec
865 // FIXME: If generating a shared object, we need to copy
866 // this relocation into the object.
867 gold_assert(!output_is_shared);
876 case elfcpp::R_X86_64_SIZE32:
877 case elfcpp::R_X86_64_SIZE64:
879 gold_error(_("%s: unsupported reloc %u against local symbol"),
880 object->name().c_str(), r_type);
886 // Report an unsupported relocation against a global symbol.
889 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
893 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
894 object->name().c_str(), r_type, gsym->name());
897 // Scan a relocation for a global symbol.
900 Target_x86_64::Scan::global(const General_options& options,
901 Symbol_table* symtab,
903 Target_x86_64* target,
904 Sized_relobj<64, false>* object,
905 unsigned int data_shndx,
906 const elfcpp::Rela<64, false>& reloc,
912 case elfcpp::R_X86_64_NONE:
913 case elfcpp::R_386_GNU_VTINHERIT:
914 case elfcpp::R_386_GNU_VTENTRY:
917 case elfcpp::R_X86_64_64:
918 case elfcpp::R_X86_64_PC64:
919 case elfcpp::R_X86_64_32:
920 case elfcpp::R_X86_64_32S:
921 case elfcpp::R_X86_64_PC32:
922 case elfcpp::R_X86_64_16:
923 case elfcpp::R_X86_64_PC16:
924 case elfcpp::R_X86_64_8:
925 case elfcpp::R_X86_64_PC8:
927 bool is_pcrel = (r_type == elfcpp::R_X86_64_PC64
928 || r_type == elfcpp::R_X86_64_PC32
929 || r_type == elfcpp::R_X86_64_PC16
930 || r_type == elfcpp::R_X86_64_PC8);
932 if (gsym->is_from_dynobj()
933 || (parameters->output_is_shared()
934 && gsym->is_preemptible()))
936 // (a) This symbol is defined in a dynamic object. If it is a
937 // function, we make a PLT entry. Otherwise we need to
938 // either generate a COPY reloc or copy this reloc.
939 // (b) We are building a shared object and this symbol is
940 // preemptible. If it is a function, we make a PLT entry.
941 // Otherwise, we copy the reloc.
942 if (gsym->type() == elfcpp::STT_FUNC)
944 target->make_plt_entry(symtab, layout, gsym);
946 // If this is not a PC relative reference, then we may
947 // be taking the address of the function. In that case
948 // we need to set the entry in the dynamic symbol table
949 // to the address of the PLT entry. We will also need to
950 // create a dynamic relocation.
953 if (gsym->is_from_dynobj())
954 gsym->set_needs_dynsym_value();
955 if (parameters->output_is_position_independent())
957 Reloc_section* rela_dyn =
958 target->rela_dyn_section(layout);
959 rela_dyn->add_global(gsym, r_type, object, data_shndx,
960 reloc.get_r_offset(),
961 reloc.get_r_addend());
965 else if (parameters->output_is_shared())
967 // We do not make COPY relocs in shared objects.
968 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
969 rela_dyn->add_global(gsym, r_type, object, data_shndx,
970 reloc.get_r_offset(),
971 reloc.get_r_addend());
974 target->copy_reloc(&options, symtab, layout, object, data_shndx,
977 else if (!is_pcrel && parameters->output_is_position_independent())
979 // This is not a PC-relative reference, so we need to generate
980 // a dynamic relocation. At this point, we know the symbol
981 // is not preemptible, so we can use the RELATIVE relocation.
982 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
983 if (r_type == elfcpp::R_X86_64_64)
984 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
986 reloc.get_r_offset(), 0);
988 rela_dyn->add_global(gsym, r_type, object, data_shndx,
989 reloc.get_r_offset(),
990 reloc.get_r_addend());
995 case elfcpp::R_X86_64_GOT64:
996 case elfcpp::R_X86_64_GOT32:
997 case elfcpp::R_X86_64_GOTPCREL64:
998 case elfcpp::R_X86_64_GOTPCREL:
999 case elfcpp::R_X86_64_GOTPLT64:
1001 // The symbol requires a GOT entry.
1002 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1003 if (got->add_global(gsym))
1005 // If this symbol is not fully resolved, we need to add a
1006 // dynamic relocation for it.
1007 if (!gsym->final_value_is_known())
1009 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1010 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
1011 gsym->got_offset(), 0);
1014 // For GOTPLT64, we also need a PLT entry (but only if the
1015 // symbol is not fully resolved).
1016 if (r_type == elfcpp::R_X86_64_GOTPLT64
1017 && !gsym->final_value_is_known())
1018 target->make_plt_entry(symtab, layout, gsym);
1022 case elfcpp::R_X86_64_PLT32:
1023 // If the symbol is fully resolved, this is just a PC32 reloc.
1024 // Otherwise we need a PLT entry.
1025 if (gsym->final_value_is_known())
1027 // If building a shared library, we can also skip the PLT entry
1028 // if the symbol is defined in the output file and is protected
1030 if (gsym->is_defined()
1031 && !gsym->is_from_dynobj()
1032 && !gsym->is_preemptible())
1034 target->make_plt_entry(symtab, layout, gsym);
1037 case elfcpp::R_X86_64_GOTPC32:
1038 case elfcpp::R_X86_64_GOTOFF64:
1039 case elfcpp::R_X86_64_GOTPC64:
1040 case elfcpp::R_X86_64_PLTOFF64:
1041 // We need a GOT section.
1042 target->got_section(symtab, layout);
1043 // For PLTOFF64, we also need a PLT entry (but only if the
1044 // symbol is not fully resolved).
1045 if (r_type == elfcpp::R_X86_64_PLTOFF64
1046 && !gsym->final_value_is_known())
1047 target->make_plt_entry(symtab, layout, gsym);
1050 case elfcpp::R_X86_64_COPY:
1051 case elfcpp::R_X86_64_GLOB_DAT:
1052 case elfcpp::R_X86_64_JUMP_SLOT:
1053 case elfcpp::R_X86_64_RELATIVE:
1054 // These are outstanding tls relocs, which are unexpected when linking
1055 case elfcpp::R_X86_64_TPOFF64:
1056 case elfcpp::R_X86_64_DTPMOD64:
1057 case elfcpp::R_X86_64_TLSDESC:
1058 gold_error(_("%s: unexpected reloc %u in object file"),
1059 object->name().c_str(), r_type);
1062 // These are initial tls relocs, which are expected for global()
1063 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1064 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1065 case elfcpp::R_X86_64_TLSDESC_CALL:
1066 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1067 case elfcpp::R_X86_64_DTPOFF32:
1068 case elfcpp::R_X86_64_DTPOFF64:
1069 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1070 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1072 const bool is_final = gsym->final_value_is_known();
1073 const tls::Tls_optimization optimized_type
1074 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1077 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1078 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1079 case elfcpp::R_X86_64_TLSDESC_CALL:
1080 // FIXME: If not relaxing to LE, we need to generate
1081 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1082 if (optimized_type != tls::TLSOPT_TO_LE)
1083 unsupported_reloc_global(object, r_type, gsym);
1086 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1087 case elfcpp::R_X86_64_DTPOFF32:
1088 case elfcpp::R_X86_64_DTPOFF64:
1089 // FIXME: If not relaxing to LE, we need to generate a
1091 if (optimized_type != tls::TLSOPT_TO_LE)
1092 unsupported_reloc_global(object, r_type, gsym);
1095 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1096 // FIXME: If not relaxing to LE, we need to generate a
1098 if (optimized_type != tls::TLSOPT_TO_LE)
1099 unsupported_reloc_global(object, r_type, gsym);
1102 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1103 // FIXME: If generating a shared object, we need to copy
1104 // this relocation into the object.
1105 gold_assert(is_final);
1114 case elfcpp::R_X86_64_SIZE32:
1115 case elfcpp::R_X86_64_SIZE64:
1117 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1118 object->name().c_str(), r_type, gsym->name());
1123 // Scan relocations for a section.
1126 Target_x86_64::scan_relocs(const General_options& options,
1127 Symbol_table* symtab,
1129 Sized_relobj<64, false>* object,
1130 unsigned int data_shndx,
1131 unsigned int sh_type,
1132 const unsigned char* prelocs,
1134 Output_section* output_section,
1135 bool needs_special_offset_handling,
1136 size_t local_symbol_count,
1137 const unsigned char* plocal_symbols)
1139 if (sh_type == elfcpp::SHT_REL)
1141 gold_error(_("%s: unsupported REL reloc section"),
1142 object->name().c_str());
1146 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1147 Target_x86_64::Scan>(
1157 needs_special_offset_handling,
1162 // Finalize the sections.
1165 Target_x86_64::do_finalize_sections(Layout* layout)
1167 // Fill in some more dynamic tags.
1168 Output_data_dynamic* const odyn = layout->dynamic_data();
1171 if (this->got_plt_ != NULL)
1172 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1174 if (this->plt_ != NULL)
1176 const Output_data* od = this->plt_->rel_plt();
1177 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1178 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1179 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1182 if (this->rela_dyn_ != NULL)
1184 const Output_data* od = this->rela_dyn_;
1185 odyn->add_section_address(elfcpp::DT_RELA, od);
1186 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1187 odyn->add_constant(elfcpp::DT_RELAENT,
1188 elfcpp::Elf_sizes<64>::rela_size);
1191 if (!parameters->output_is_shared())
1193 // The value of the DT_DEBUG tag is filled in by the dynamic
1194 // linker at run time, and used by the debugger.
1195 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1199 // Emit any relocs we saved in an attempt to avoid generating COPY
1201 if (this->copy_relocs_ == NULL)
1203 if (this->copy_relocs_->any_to_emit())
1205 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1206 this->copy_relocs_->emit(rela_dyn);
1208 delete this->copy_relocs_;
1209 this->copy_relocs_ = NULL;
1212 // Perform a relocation.
1215 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1216 Target_x86_64* target,
1218 const elfcpp::Rela<64, false>& rela,
1219 unsigned int r_type,
1220 const Sized_symbol<64>* gsym,
1221 const Symbol_value<64>* psymval,
1222 unsigned char* view,
1223 elfcpp::Elf_types<64>::Elf_Addr address,
1226 if (this->skip_call_tls_get_addr_)
1228 if (r_type != elfcpp::R_X86_64_PLT32
1230 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1232 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1233 _("missing expected TLS relocation"));
1237 this->skip_call_tls_get_addr_ = false;
1242 // Pick the value to use for symbols defined in shared objects.
1243 Symbol_value<64> symval;
1245 && (gsym->is_from_dynobj()
1246 || (parameters->output_is_shared()
1247 && gsym->is_preemptible()))
1248 && gsym->has_plt_offset())
1250 symval.set_output_value(target->plt_section()->address()
1251 + gsym->plt_offset());
1255 const Sized_relobj<64, false>* object = relinfo->object;
1256 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1258 // Get the GOT offset if needed.
1259 // The GOT pointer points to the end of the GOT section.
1260 // We need to subtract the size of the GOT section to get
1261 // the actual offset to use in the relocation.
1262 bool have_got_offset = false;
1263 unsigned int got_offset = 0;
1266 case elfcpp::R_X86_64_GOT32:
1267 case elfcpp::R_X86_64_GOT64:
1268 case elfcpp::R_X86_64_GOTPLT64:
1269 case elfcpp::R_X86_64_GOTPCREL:
1270 case elfcpp::R_X86_64_GOTPCREL64:
1273 gold_assert(gsym->has_got_offset());
1274 got_offset = gsym->got_offset() - target->got_size();
1278 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1279 got_offset = object->local_got_offset(r_sym) - target->got_size();
1281 have_got_offset = true;
1290 case elfcpp::R_X86_64_NONE:
1291 case elfcpp::R_386_GNU_VTINHERIT:
1292 case elfcpp::R_386_GNU_VTENTRY:
1295 case elfcpp::R_X86_64_64:
1296 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1299 case elfcpp::R_X86_64_PC64:
1300 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1304 case elfcpp::R_X86_64_32:
1305 // FIXME: we need to verify that value + addend fits into 32 bits:
1306 // uint64_t x = value + addend;
1307 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1308 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1309 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1312 case elfcpp::R_X86_64_32S:
1313 // FIXME: we need to verify that value + addend fits into 32 bits:
1314 // int64_t x = value + addend; // note this quantity is signed!
1315 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1316 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1319 case elfcpp::R_X86_64_PC32:
1320 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1324 case elfcpp::R_X86_64_16:
1325 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1328 case elfcpp::R_X86_64_PC16:
1329 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1333 case elfcpp::R_X86_64_8:
1334 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1337 case elfcpp::R_X86_64_PC8:
1338 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1342 case elfcpp::R_X86_64_PLT32:
1343 gold_assert(gsym == NULL
1344 || gsym->has_plt_offset()
1345 || gsym->final_value_is_known());
1346 // Note: while this code looks the same as for R_X86_64_PC32, it
1347 // behaves differently because psymval was set to point to
1348 // the PLT entry, rather than the symbol, in Scan::global().
1349 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1353 case elfcpp::R_X86_64_PLTOFF64:
1356 gold_assert(gsym->has_plt_offset()
1357 || gsym->final_value_is_known());
1358 elfcpp::Elf_types<64>::Elf_Addr got_address;
1359 got_address = target->got_section(NULL, NULL)->address();
1360 Relocate_functions<64, false>::rela64(view, object, psymval,
1361 addend - got_address);
1364 case elfcpp::R_X86_64_GOT32:
1365 gold_assert(have_got_offset);
1366 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1369 case elfcpp::R_X86_64_GOTPC32:
1372 elfcpp::Elf_types<64>::Elf_Addr value;
1373 value = target->got_plt_section()->address();
1374 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1378 case elfcpp::R_X86_64_GOT64:
1379 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1380 // Since we always add a PLT entry, this is equivalent.
1381 case elfcpp::R_X86_64_GOTPLT64:
1382 gold_assert(have_got_offset);
1383 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1386 case elfcpp::R_X86_64_GOTPC64:
1389 elfcpp::Elf_types<64>::Elf_Addr value;
1390 value = target->got_plt_section()->address();
1391 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1395 case elfcpp::R_X86_64_GOTOFF64:
1397 elfcpp::Elf_types<64>::Elf_Addr value;
1398 value = (psymval->value(object, 0)
1399 - target->got_plt_section()->address());
1400 Relocate_functions<64, false>::rela64(view, value, addend);
1404 case elfcpp::R_X86_64_GOTPCREL:
1406 gold_assert(have_got_offset);
1407 elfcpp::Elf_types<64>::Elf_Addr value;
1408 value = target->got_plt_section()->address() + got_offset;
1409 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1413 case elfcpp::R_X86_64_GOTPCREL64:
1415 gold_assert(have_got_offset);
1416 elfcpp::Elf_types<64>::Elf_Addr value;
1417 value = target->got_plt_section()->address() + got_offset;
1418 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1422 case elfcpp::R_X86_64_COPY:
1423 case elfcpp::R_X86_64_GLOB_DAT:
1424 case elfcpp::R_X86_64_JUMP_SLOT:
1425 case elfcpp::R_X86_64_RELATIVE:
1426 // These are outstanding tls relocs, which are unexpected when linking
1427 case elfcpp::R_X86_64_TPOFF64:
1428 case elfcpp::R_X86_64_DTPMOD64:
1429 case elfcpp::R_X86_64_TLSDESC:
1430 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1431 _("unexpected reloc %u in object file"),
1435 // These are initial tls relocs, which are expected when linking
1436 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1437 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1438 case elfcpp::R_X86_64_TLSDESC_CALL:
1439 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1440 case elfcpp::R_X86_64_DTPOFF32:
1441 case elfcpp::R_X86_64_DTPOFF64:
1442 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1443 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1444 this->relocate_tls(relinfo, relnum, rela, r_type, gsym, psymval, view,
1445 address, view_size);
1448 case elfcpp::R_X86_64_SIZE32:
1449 case elfcpp::R_X86_64_SIZE64:
1451 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1452 _("unsupported reloc %u"),
1460 // Perform a TLS relocation.
1463 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1465 const elfcpp::Rela<64, false>& rela,
1466 unsigned int r_type,
1467 const Sized_symbol<64>* gsym,
1468 const Symbol_value<64>* psymval,
1469 unsigned char* view,
1470 elfcpp::Elf_types<64>::Elf_Addr,
1473 Output_segment* tls_segment = relinfo->layout->tls_segment();
1474 if (tls_segment == NULL)
1476 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1477 _("TLS reloc but no TLS segment"));
1481 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1483 const bool is_final = (gsym == NULL
1484 ? !parameters->output_is_position_independent()
1485 : gsym->final_value_is_known());
1486 const tls::Tls_optimization optimized_type
1487 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1490 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1491 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1492 case elfcpp::R_X86_64_TLSDESC_CALL:
1493 if (optimized_type == tls::TLSOPT_TO_LE)
1495 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1496 rela, r_type, value, view,
1500 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1501 _("unsupported reloc %u"), r_type);
1504 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1505 if (optimized_type == tls::TLSOPT_TO_LE)
1507 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1508 value, view, view_size);
1511 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1512 _("unsupported reloc %u"), r_type);
1515 case elfcpp::R_X86_64_DTPOFF32:
1516 if (optimized_type == tls::TLSOPT_TO_LE)
1517 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1519 value = value - tls_segment->vaddr();
1520 Relocate_functions<64, false>::rel32(view, value);
1523 case elfcpp::R_X86_64_DTPOFF64:
1524 if (optimized_type == tls::TLSOPT_TO_LE)
1525 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1527 value = value - tls_segment->vaddr();
1528 Relocate_functions<64, false>::rel64(view, value);
1531 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1532 if (optimized_type == tls::TLSOPT_TO_LE)
1534 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1535 rela, r_type, value, view,
1539 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1540 _("unsupported reloc type %u"),
1544 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1545 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1546 Relocate_functions<64, false>::rel32(view, value);
1551 // Do a relocation in which we convert a TLS General-Dynamic to a
1555 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1557 Output_segment* tls_segment,
1558 const elfcpp::Rela<64, false>& rela,
1560 elfcpp::Elf_types<64>::Elf_Addr value,
1561 unsigned char* view,
1564 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1565 // .word 0x6666; rex64; call __tls_get_addr
1566 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1568 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1569 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1571 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1572 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1573 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1574 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1576 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1578 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1579 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1581 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1583 this->skip_call_tls_get_addr_ = true;
1587 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1590 const elfcpp::Rela<64, false>& rela,
1592 elfcpp::Elf_types<64>::Elf_Addr,
1593 unsigned char* view,
1596 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1597 // ... leq foo@dtpoff(%rax),%reg
1598 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1600 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1601 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1603 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1604 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1606 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1608 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1610 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1612 this->skip_call_tls_get_addr_ = true;
1615 // Do a relocation in which we convert a TLS Initial-Exec to a
1619 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1621 Output_segment* tls_segment,
1622 const elfcpp::Rela<64, false>& rela,
1624 elfcpp::Elf_types<64>::Elf_Addr value,
1625 unsigned char* view,
1628 // We need to examine the opcodes to figure out which instruction we
1631 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1632 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1634 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1635 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1637 unsigned char op1 = view[-3];
1638 unsigned char op2 = view[-2];
1639 unsigned char op3 = view[-1];
1640 unsigned char reg = op3 >> 3;
1648 view[-1] = 0xc0 | reg;
1652 // Special handling for %rsp.
1656 view[-1] = 0xc0 | reg;
1664 view[-1] = 0x80 | reg | (reg << 3);
1667 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1668 Relocate_functions<64, false>::rela32(view, value, 0);
1671 // Relocate section data.
1674 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1675 unsigned int sh_type,
1676 const unsigned char* prelocs,
1678 Output_section* output_section,
1679 bool needs_special_offset_handling,
1680 unsigned char* view,
1681 elfcpp::Elf_types<64>::Elf_Addr address,
1684 gold_assert(sh_type == elfcpp::SHT_RELA);
1686 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1687 Target_x86_64::Relocate>(
1693 needs_special_offset_handling,
1699 // Return the value to use for a dynamic which requires special
1700 // treatment. This is how we support equality comparisons of function
1701 // pointers across shared library boundaries, as described in the
1702 // processor specific ABI supplement.
1705 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1707 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1708 return this->plt_section()->address() + gsym->plt_offset();
1711 // Return a string used to fill a code section with nops to take up
1712 // the specified length.
1715 Target_x86_64::do_code_fill(off_t length)
1719 // Build a jmpq instruction to skip over the bytes.
1720 unsigned char jmp[5];
1722 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1723 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1724 + std::string(length - 5, '\0'));
1727 // Nop sequences of various lengths.
1728 const char nop1[1] = { 0x90 }; // nop
1729 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1730 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1731 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1732 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1733 0x00 }; // leal 0(%esi,1),%esi
1734 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1736 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1738 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1739 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1740 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1741 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1743 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1744 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1746 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1747 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1749 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1750 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1751 0x00, 0x00, 0x00, 0x00 };
1752 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1753 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1754 0x27, 0x00, 0x00, 0x00,
1756 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1757 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1758 0xbc, 0x27, 0x00, 0x00,
1760 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1761 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1762 0x90, 0x90, 0x90, 0x90,
1765 const char* nops[16] = {
1767 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1768 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1771 return std::string(nops[length], length);
1774 // The selector for x86_64 object files.
1776 class Target_selector_x86_64 : public Target_selector
1779 Target_selector_x86_64()
1780 : Target_selector(elfcpp::EM_X86_64, 64, false)
1784 recognize(int machine, int osabi, int abiversion);
1787 Target_x86_64* target_;
1790 // Recognize an x86_64 object file when we already know that the machine
1791 // number is EM_X86_64.
1794 Target_selector_x86_64::recognize(int, int, int)
1796 if (this->target_ == NULL)
1797 this->target_ = new Target_x86_64();
1798 return this->target_;
1801 Target_selector_x86_64 target_selector_x86_64;
1803 } // End anonymous namespace.