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"
53 class Output_data_plt_x86_64;
55 // The x86_64 target class.
56 // See the ABI at http://www.x86-64.org/documentation/abi.pdf
58 class Target_x86_64 : public Sized_target<64, false>
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
66 : Sized_target<64, false>(&x86_64_info),
67 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
68 copy_relocs_(NULL), dynbss_(NULL)
71 // Scan the relocations to look for symbol adjustments.
73 scan_relocs(const General_options& options,
76 Sized_relobj<64, false>* object,
77 unsigned int data_shndx,
79 const unsigned char* prelocs,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols,
83 Symbol** global_symbols);
85 // Finalize the sections.
87 do_finalize_sections(Layout*);
89 // Relocate a section.
91 relocate_section(const Relocate_info<64, false>*,
93 const unsigned char* prelocs,
96 elfcpp::Elf_types<64>::Elf_Addr view_address,
99 // Return a string used to fill a code section with nops.
101 do_code_fill(off_t length);
104 // The class which scans relocations.
108 local(const General_options& options, Symbol_table* symtab,
109 Layout* layout, Target_x86_64* target,
110 Sized_relobj<64, false>* object,
111 unsigned int data_shndx,
112 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
113 const elfcpp::Sym<64, false>& lsym);
116 global(const General_options& options, Symbol_table* symtab,
117 Layout* layout, Target_x86_64* target,
118 Sized_relobj<64, false>* object,
119 unsigned int data_shndx,
120 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
124 // The class which implements relocation.
129 : skip_call_tls_get_addr_(false)
134 if (this->skip_call_tls_get_addr_)
136 // FIXME: This needs to specify the location somehow.
137 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
143 // Do a relocation. Return false if the caller should not issue
144 // any warnings about this relocation.
146 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
147 const elfcpp::Rela<64, false>&,
148 unsigned int r_type, const Sized_symbol<64>*,
149 const Symbol_value<64>*,
150 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
154 // Do a TLS relocation.
156 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
157 const elfcpp::Rela<64, false>&,
158 unsigned int r_type, const Sized_symbol<64>*,
159 const Symbol_value<64>*,
160 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
162 // Do a TLS Initial-Exec to Local-Exec transition.
164 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
165 Output_segment* tls_segment,
166 const elfcpp::Rela<64, false>&, unsigned int r_type,
167 elfcpp::Elf_types<64>::Elf_Addr value,
171 // Do a TLS Global-Dynamic to Local-Exec transition.
173 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
174 Output_segment* tls_segment,
175 const elfcpp::Rela<64, false>&, unsigned int r_type,
176 elfcpp::Elf_types<64>::Elf_Addr value,
180 // Check the range for a TLS relocation.
182 check_range(const Relocate_info<64, false>*, size_t relnum,
183 const elfcpp::Rela<64, false>&, off_t, off_t);
185 // Check the validity of a TLS relocation. This is like assert.
187 check_tls(const Relocate_info<64, false>*, size_t relnum,
188 const elfcpp::Rela<64, false>&, bool);
190 // This is set if we should skip the next reloc, which should be a
191 // PLT32 reloc against ___tls_get_addr.
192 bool skip_call_tls_get_addr_;
195 // Adjust TLS relocation type based on the options and whether this
196 // is a local symbol.
198 optimize_tls_reloc(bool is_final, int r_type);
200 // Get the GOT section, creating it if necessary.
201 Output_data_got<64, false>*
202 got_section(Symbol_table*, Layout*);
204 // Create a PLT entry for a global symbol.
206 make_plt_entry(Symbol_table*, Layout*, Symbol*);
208 // Get the PLT section.
209 Output_data_plt_x86_64*
212 gold_assert(this->plt_ != NULL);
216 // Get the dynamic reloc section, creating it if necessary.
218 rel_dyn_section(Layout*);
220 // Copy a relocation against a global symbol.
222 copy_reloc(const General_options*, Symbol_table*, Layout*,
223 Sized_relobj<64, false>*, unsigned int,
224 Symbol*, const elfcpp::Rela<64, false>&);
226 // Information about this specific target which we pass to the
227 // general Target structure.
228 static const Target::Target_info x86_64_info;
231 Output_data_got<64, false>* got_;
233 Output_data_plt_x86_64* plt_;
234 // The GOT PLT section.
235 Output_data_space* got_plt_;
236 // The dynamic reloc section.
237 Reloc_section* rel_dyn_;
238 // Relocs saved to avoid a COPY reloc.
239 Copy_relocs<64, false>* copy_relocs_;
240 // Space for variables copied with a COPY reloc.
241 Output_data_space* dynbss_;
244 const Target::Target_info Target_x86_64::x86_64_info =
247 false, // is_big_endian
248 elfcpp::EM_X86_64, // machine_code
249 false, // has_make_symbol
250 false, // has_resolve
251 true, // has_code_fill
252 "/lib/ld64.so.1", // program interpreter
253 0x400000, // text_segment_address
254 0x1000, // abi_pagesize
255 0x1000 // common_pagesize
258 // Get the GOT section, creating it if necessary.
260 Output_data_got<64, false>*
261 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
263 if (this->got_ == NULL)
265 gold_assert(symtab != NULL && layout != NULL);
267 this->got_ = new Output_data_got<64, false>();
269 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
270 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
273 // The old GNU linker creates a .got.plt section. We just
274 // create another set of data in the .got section. Note that we
275 // always create a PLT if we create a GOT, although the PLT
277 // TODO(csilvers): do we really need an alignment of 8?
278 this->got_plt_ = new Output_data_space(8);
279 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
280 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
283 // The first three entries are reserved.
284 this->got_plt_->set_space_size(3 * 8);
286 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
287 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
289 0, 0, elfcpp::STT_OBJECT,
291 elfcpp::STV_HIDDEN, 0,
298 // Get the dynamic reloc section, creating it if necessary.
300 Target_x86_64::Reloc_section*
301 Target_x86_64::rel_dyn_section(Layout* layout)
303 if (this->rel_dyn_ == NULL)
305 gold_assert(layout != NULL);
306 this->rel_dyn_ = new Reloc_section();
307 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
308 elfcpp::SHF_ALLOC, this->rel_dyn_);
310 return this->rel_dyn_;
313 // A class to handle the PLT data.
315 class Output_data_plt_x86_64 : public Output_section_data
318 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
320 Output_data_plt_x86_64(Layout*, Output_data_space*);
322 // Add an entry to the PLT.
324 add_entry(Symbol* gsym);
326 // Return the .rel.plt section data.
329 { return this->rel_; }
333 do_adjust_output_section(Output_section* os);
336 // The size of an entry in the PLT.
337 static const int plt_entry_size = 16;
339 // The first entry in the PLT.
340 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
341 // procedure linkage table for both programs and shared objects."
342 static unsigned char first_plt_entry[plt_entry_size];
344 // Other entries in the PLT for an executable.
345 static unsigned char plt_entry[plt_entry_size];
347 // Set the final size.
349 do_set_address(uint64_t, off_t)
350 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
352 // Write out the PLT data.
354 do_write(Output_file*);
356 // The reloc section.
358 // The .got.plt section.
359 Output_data_space* got_plt_;
360 // The number of PLT entries.
364 // Create the PLT section. The ordinary .got section is an argument,
365 // since we need to refer to the start. We also create our own .got
366 // section just for PLT entries.
368 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
369 Output_data_space* got_plt)
370 // TODO(csilvers): do we really need an alignment of 8?
371 : Output_section_data(8), got_plt_(got_plt), count_(0)
373 this->rel_ = new Reloc_section();
374 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
375 elfcpp::SHF_ALLOC, this->rel_);
379 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
381 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
382 // linker, and so do we.
386 // Add an entry to the PLT.
389 Output_data_plt_x86_64::add_entry(Symbol* gsym)
391 gold_assert(!gsym->has_plt_offset());
393 // Note that when setting the PLT offset we skip the initial
394 // reserved PLT entry.
395 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
399 off_t got_offset = this->got_plt_->data_size();
401 // Every PLT entry needs a GOT entry which points back to the PLT
402 // entry (this will be changed by the dynamic linker, normally
403 // lazily when the function is called).
404 this->got_plt_->set_space_size(got_offset + 8);
406 // Every PLT entry needs a reloc.
407 gsym->set_needs_dynsym_entry();
408 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
411 // Note that we don't need to save the symbol. The contents of the
412 // PLT are independent of which symbols are used. The symbols only
413 // appear in the relocations.
416 // The first entry in the PLT for an executable.
418 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
420 // From AMD64 ABI Draft 0.98, page 76
421 0xff, 0x35, // pushq contents of memory address
422 0, 0, 0, 0, // replaced with address of .got + 4
423 0xff, 0x25, // jmp indirect
424 0, 0, 0, 0, // replaced with address of .got + 8
425 0x90, 0x90, 0x90, 0x90 // noop (x4)
428 // Subsequent entries in the PLT for an executable.
430 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
432 // From AMD64 ABI Draft 0.98, page 76
433 0xff, 0x25, // jmpq indirect
434 0, 0, 0, 0, // replaced with address of symbol in .got
435 0x68, // pushq immediate
436 0, 0, 0, 0, // replaced with offset into relocation table
437 0xe9, // jmpq relative
438 0, 0, 0, 0 // replaced with offset to start of .plt
441 // Write out the PLT. This uses the hand-coded instructions above,
442 // and adjusts them as needed. This is specified by the AMD64 ABI.
445 Output_data_plt_x86_64::do_write(Output_file* of)
447 const off_t offset = this->offset();
448 const off_t oview_size = this->data_size();
449 unsigned char* const oview = of->get_output_view(offset, oview_size);
451 const off_t got_file_offset = this->got_plt_->offset();
452 const off_t got_size = this->got_plt_->data_size();
453 unsigned char* const got_view = of->get_output_view(got_file_offset,
456 unsigned char* pov = oview;
458 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
459 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
461 memcpy(pov, first_plt_entry, plt_entry_size);
462 if (!parameters->output_is_shared())
464 // We do a jmp relative to the PC at the end of this instruction.
465 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
466 - (plt_address + 6));
467 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
468 - (plt_address + 12));
470 pov += plt_entry_size;
472 unsigned char* got_pov = got_view;
474 memset(got_pov, 0, 24);
477 unsigned int plt_offset = plt_entry_size;
478 unsigned int got_offset = 24;
479 const unsigned int count = this->count_;
480 for (unsigned int plt_index = 0;
483 pov += plt_entry_size,
485 plt_offset += plt_entry_size,
488 // Set and adjust the PLT entry itself.
489 memcpy(pov, plt_entry, plt_entry_size);
490 if (parameters->output_is_shared())
491 // FIXME(csilvers): what's the right thing to write here?
492 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
494 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
495 (got_address + got_offset
496 - (plt_address + plt_offset
499 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
500 elfcpp::Swap<32, false>::writeval(pov + 12,
501 - (plt_offset + plt_entry_size));
503 // Set the entry in the GOT.
504 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
507 gold_assert(pov - oview == oview_size);
508 gold_assert(got_pov - got_view == got_size);
510 of->write_output_view(offset, oview_size, oview);
511 of->write_output_view(got_file_offset, got_size, got_view);
514 // Create a PLT entry for a global symbol.
517 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
520 if (gsym->has_plt_offset())
523 if (this->plt_ == NULL)
525 // Create the GOT sections first.
526 this->got_section(symtab, layout);
528 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
529 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
531 | elfcpp::SHF_EXECINSTR),
535 this->plt_->add_entry(gsym);
538 // Handle a relocation against a non-function symbol defined in a
539 // dynamic object. The traditional way to handle this is to generate
540 // a COPY relocation to copy the variable at runtime from the shared
541 // object into the executable's data segment. However, this is
542 // undesirable in general, as if the size of the object changes in the
543 // dynamic object, the executable will no longer work correctly. If
544 // this relocation is in a writable section, then we can create a
545 // dynamic reloc and the dynamic linker will resolve it to the correct
546 // address at runtime. However, we do not want do that if the
547 // relocation is in a read-only section, as it would prevent the
548 // readonly segment from being shared. And if we have to eventually
549 // generate a COPY reloc, then any dynamic relocations will be
550 // useless. So this means that if this is a writable section, we need
551 // to save the relocation until we see whether we have to create a
552 // COPY relocation for this symbol for any other relocation.
555 Target_x86_64::copy_reloc(const General_options* options,
556 Symbol_table* symtab,
558 Sized_relobj<64, false>* object,
559 unsigned int data_shndx, Symbol* gsym,
560 const elfcpp::Rela<64, false>& rel)
562 Sized_symbol<64>* ssym;
563 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
566 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
569 // So far we do not need a COPY reloc. Save this relocation.
570 // If it turns out that we never need a COPY reloc for this
571 // symbol, then we will emit the relocation.
572 if (this->copy_relocs_ == NULL)
573 this->copy_relocs_ = new Copy_relocs<64, false>();
574 this->copy_relocs_->save(ssym, object, data_shndx, rel);
578 // Allocate space for this symbol in the .bss section.
580 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
582 // There is no defined way to determine the required alignment
583 // of the symbol. We pick the alignment based on the size. We
584 // set an arbitrary maximum of 256.
586 for (align = 1; align < 512; align <<= 1)
587 if ((symsize & align) != 0)
590 if (this->dynbss_ == NULL)
592 this->dynbss_ = new Output_data_space(align);
593 layout->add_output_section_data(".bss",
596 | elfcpp::SHF_WRITE),
600 Output_data_space* dynbss = this->dynbss_;
602 if (align > dynbss->addralign())
603 dynbss->set_space_alignment(align);
605 off_t dynbss_size = dynbss->data_size();
606 dynbss_size = align_address(dynbss_size, align);
607 off_t offset = dynbss_size;
608 dynbss->set_space_size(dynbss_size + symsize);
610 // Define the symbol in the .dynbss section.
611 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
612 dynbss, offset, symsize, ssym->type(),
613 ssym->binding(), ssym->visibility(),
614 ssym->nonvis(), false, false);
616 // Add the COPY reloc.
617 ssym->set_needs_dynsym_entry();
618 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
619 rel_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset,
625 // Optimize the TLS relocation type based on what we know about the
626 // symbol. IS_FINAL is true if the final address of this symbol is
627 // known at link time.
630 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
632 return is_final ? r_type : 0;
634 // If we are generating a shared library, then we can't do anything
636 if (parameters->output_is_shared())
641 case elfcpp::R_X86_64_TLSGD:
642 // These are Global-Dynamic which permits fully general TLS
643 // access. Since we know that we are generating an executable,
644 // we can convert this to Initial-Exec. If we also know that
645 // this is a local symbol, we can further switch to Local-Exec.
647 return elfcpp::R_X86_64_TLS_LE_64;
648 return elfcpp::R_X86_64_TLS_IE_64;
650 case elfcpp::R_X86_64_TLS_LDM:
651 // This is Local-Dynamic, which refers to a local symbol in the
652 // dynamic TLS block. Since we know that we generating an
653 // executable, we can switch to Local-Exec.
654 return elfcpp::R_X86_64_TLS_LE_64;
656 case elfcpp::R_X86_64_TLS_LDO_64:
657 // Another type of Local-Dynamic relocation.
658 return elfcpp::R_X86_64_TLS_LE;
660 case elfcpp::R_X86_64_TLS_IE:
661 case elfcpp::R_X86_64_TLS_GOTIE:
662 case elfcpp::R_X86_64_TLS_IE_64:
663 // These are Initial-Exec relocs which get the thread offset
664 // from the GOT. If we know that we are linking against the
665 // local symbol, we can switch to Local-Exec, which links the
666 // thread offset into the instruction.
668 return elfcpp::R_X86_64_TLS_LE_64;
671 case elfcpp::R_X86_64_TLS_LE:
672 case elfcpp::R_X86_64_TLS_LE_64:
673 // When we already have Local-Exec, there is nothing further we
683 // Scan a relocation for a local symbol.
686 Target_x86_64::Scan::local(const General_options&,
687 Symbol_table* symtab,
689 Target_x86_64* target,
690 Sized_relobj<64, false>* object,
692 const elfcpp::Rela<64, false>&,
694 const elfcpp::Sym<64, false>&)
698 case elfcpp::R_X86_64_NONE:
699 case elfcpp::R_386_GNU_VTINHERIT:
700 case elfcpp::R_386_GNU_VTENTRY:
703 case elfcpp::R_X86_64_64:
704 case elfcpp::R_X86_64_32:
705 case elfcpp::R_X86_64_32S:
706 case elfcpp::R_X86_64_16:
707 case elfcpp::R_X86_64_8:
708 // FIXME: If we are generating a shared object we need to copy
709 // this relocation into the object.
710 gold_assert(!parameters->output_is_shared());
713 case elfcpp::R_X86_64_PC64:
714 case elfcpp::R_X86_64_PC32:
715 case elfcpp::R_X86_64_PC16:
716 case elfcpp::R_X86_64_PC8:
719 case elfcpp::R_X86_64_GOTPCREL:
720 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
721 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
722 case elfcpp::R_X86_64_GOTOFF64:
723 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
724 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
725 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
726 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
727 // We need a GOT section.
728 target->got_section(symtab, layout);
731 case elfcpp::R_X86_64_COPY:
732 case elfcpp::R_X86_64_GLOB_DAT:
733 case elfcpp::R_X86_64_JUMP_SLOT:
734 case elfcpp::R_X86_64_RELATIVE:
735 case elfcpp::R_X86_64_TPOFF64:
736 case elfcpp::R_X86_64_TPOFF32:
737 case elfcpp::R_X86_64_DTPMOD64:
738 case elfcpp::R_X86_64_DTPOFF64:
739 case elfcpp::R_X86_64_DTPOFF32:
740 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
741 case elfcpp::R_X86_64_TLSDESC:
742 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
743 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
744 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
745 program_name, object->name().c_str(), r_type);
750 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
751 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
753 case elfcpp::R_386_TLS_IE:
754 case elfcpp::R_386_TLS_GOTIE:
755 case elfcpp::R_386_TLS_LE:
756 case elfcpp::R_386_TLS_GD:
757 case elfcpp::R_386_TLS_LDM:
758 case elfcpp::R_386_TLS_LDO_64:
759 case elfcpp::R_386_TLS_IE_64:
760 case elfcpp::R_386_TLS_LE_64:
762 bool output_is_shared = parameters->output_is_shared();
763 r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared,
767 case elfcpp::R_X86_64_TLS_LE:
768 case elfcpp::R_X86_64_TLS_LE_64:
769 // FIXME: If generating a shared object, we need to copy
770 // this relocation into the object.
771 gold_assert(!output_is_shared);
774 case elfcpp::R_X86_64_TLS_IE:
775 case elfcpp::R_X86_64_TLS_GOTIE:
776 case elfcpp::R_X86_64_TLS_GD:
777 case elfcpp::R_X86_64_TLS_LDM:
778 case elfcpp::R_X86_64_TLS_LDO_64:
779 case elfcpp::R_X86_64_TLS_IE_64:
781 _("%s: %s: unsupported reloc %u against local symbol\n"),
782 program_name, object->name().c_str(), r_type);
789 case elfcpp::R_X86_64_GOT32:
790 case elfcpp::R_X86_64_PLT32:
791 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
792 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
794 case elfcpp::R_386_TLS_GD_64:
795 case elfcpp::R_386_TLS_GD_PUSH:
796 case elfcpp::R_386_TLS_GD_CALL:
797 case elfcpp::R_386_TLS_GD_POP:
798 case elfcpp::R_386_TLS_LDM_64:
799 case elfcpp::R_386_TLS_LDM_PUSH:
800 case elfcpp::R_386_TLS_LDM_CALL:
801 case elfcpp::R_386_TLS_LDM_POP:
804 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
805 program_name, object->name().c_str(), r_type);
811 // Scan a relocation for a global symbol.
814 Target_x86_64::Scan::global(const General_options& options,
815 Symbol_table* symtab,
817 Target_x86_64* target,
818 Sized_relobj<64, false>* object,
819 unsigned int data_shndx,
820 const elfcpp::Rela<64, false>& reloc,
826 case elfcpp::R_X86_64_NONE:
827 case elfcpp::R_386_GNU_VTINHERIT:
828 case elfcpp::R_386_GNU_VTENTRY:
831 case elfcpp::R_X86_64_64:
832 case elfcpp::R_X86_64_PC64:
833 case elfcpp::R_X86_64_32:
834 case elfcpp::R_X86_64_32S:
835 case elfcpp::R_X86_64_PC32:
836 case elfcpp::R_X86_64_16:
837 case elfcpp::R_X86_64_PC16:
838 case elfcpp::R_X86_64_8:
839 case elfcpp::R_X86_64_PC8:
840 // FIXME: If we are generating a shared object we may need to
841 // copy this relocation into the object. If this symbol is
842 // defined in a shared object, we may need to copy this
843 // relocation in order to avoid a COPY relocation.
844 gold_assert(!parameters->output_is_shared());
846 if (gsym->is_from_dynobj())
848 // This symbol is defined in a dynamic object. If it is a
849 // function, we make a PLT entry. Otherwise we need to
850 // either generate a COPY reloc or copy this reloc.
851 if (gsym->type() == elfcpp::STT_FUNC)
852 target->make_plt_entry(symtab, layout, gsym);
854 target->copy_reloc(&options, symtab, layout, object, data_shndx,
860 case elfcpp::R_X86_64_GOT32:
862 // The symbol requires a GOT entry.
863 Output_data_got<64, false>* got = target->got_section(symtab, layout);
864 if (got->add_global(gsym))
866 // If this symbol is not fully resolved, we need to add a
867 // dynamic relocation for it.
868 if (!gsym->final_value_is_known())
870 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
871 rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
872 gsym->got_offset(), reloc.get_r_addend());
878 case elfcpp::R_X86_64_PLT32:
879 // If the symbol is fully resolved, this is just a PC32 reloc.
880 // Otherwise we need a PLT entry.
881 if (gsym->final_value_is_known())
883 target->make_plt_entry(symtab, layout, gsym);
886 case elfcpp::R_X86_64_GOTPCREL:
887 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
888 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
889 case elfcpp::R_X86_64_GOTOFF64:
890 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
891 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
892 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
893 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
894 // We need a GOT section.
895 target->got_section(symtab, layout);
898 case elfcpp::R_X86_64_COPY:
899 case elfcpp::R_X86_64_GLOB_DAT:
900 case elfcpp::R_X86_64_JUMP_SLOT:
901 case elfcpp::R_X86_64_RELATIVE:
902 case elfcpp::R_X86_64_TPOFF64:
903 case elfcpp::R_X86_64_TPOFF32:
904 case elfcpp::R_X86_64_DTPMOD64:
905 case elfcpp::R_X86_64_DTPOFF64:
906 case elfcpp::R_X86_64_DTPOFF32:
907 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
908 case elfcpp::R_X86_64_TLSDESC:
909 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
910 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
911 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
912 program_name, object->name().c_str(), r_type);
917 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
918 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
920 case elfcpp::R_386_TLS_IE:
921 case elfcpp::R_386_TLS_GOTIE:
922 case elfcpp::R_386_TLS_LE:
923 case elfcpp::R_386_TLS_GD:
924 case elfcpp::R_386_TLS_LDM:
925 case elfcpp::R_386_TLS_LDO_64:
926 case elfcpp::R_386_TLS_IE_64:
927 case elfcpp::R_386_TLS_LE_64:
929 const bool is_final = gsym->final_value_is_known();
930 r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type);
933 case elfcpp::R_386_TLS_LE:
934 case elfcpp::R_386_TLS_LE_64:
935 // FIXME: If generating a shared object, we need to copy
936 // this relocation into the object.
937 gold_assert(!parameters->output_is_shared());
940 case elfcpp::R_386_TLS_IE:
941 case elfcpp::R_386_TLS_GOTIE:
942 case elfcpp::R_386_TLS_GD:
943 case elfcpp::R_386_TLS_LDM:
944 case elfcpp::R_386_TLS_LDO_64:
945 case elfcpp::R_386_TLS_IE_64:
947 _("%s: %s: unsupported reloc %u "
948 "against global symbol %s\n"),
949 program_name, object->name().c_str(), r_type,
957 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
958 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
960 case elfcpp::R_386_TLS_GD_64:
961 case elfcpp::R_386_TLS_GD_PUSH:
962 case elfcpp::R_386_TLS_GD_CALL:
963 case elfcpp::R_386_TLS_GD_POP:
964 case elfcpp::R_386_TLS_LDM_64:
965 case elfcpp::R_386_TLS_LDM_PUSH:
966 case elfcpp::R_386_TLS_LDM_CALL:
967 case elfcpp::R_386_TLS_LDM_POP:
971 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
972 program_name, object->name().c_str(), r_type, gsym->name());
977 // Scan relocations for a section.
980 Target_x86_64::scan_relocs(const General_options& options,
981 Symbol_table* symtab,
983 Sized_relobj<64, false>* object,
984 unsigned int data_shndx,
985 unsigned int sh_type,
986 const unsigned char* prelocs,
988 size_t local_symbol_count,
989 const unsigned char* plocal_symbols,
990 Symbol** global_symbols)
992 if (sh_type == elfcpp::SHT_REL)
994 fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"),
995 program_name, object->name().c_str());
999 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1000 Target_x86_64::Scan>(
1014 // Finalize the sections.
1017 Target_x86_64::do_finalize_sections(Layout* layout)
1019 // Fill in some more dynamic tags.
1020 Output_data_dynamic* const odyn = layout->dynamic_data();
1023 if (this->got_plt_ != NULL)
1024 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1026 if (this->plt_ != NULL)
1028 const Output_data* od = this->plt_->rel_plt();
1029 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1030 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1031 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1034 if (this->rel_dyn_ != NULL)
1036 const Output_data* od = this->rel_dyn_;
1037 odyn->add_section_address(elfcpp::DT_RELA, od);
1038 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1039 odyn->add_constant(elfcpp::DT_RELAENT,
1040 elfcpp::Elf_sizes<64>::rel_size);
1043 if (!parameters->output_is_shared())
1045 // The value of the DT_DEBUG tag is filled in by the dynamic
1046 // linker at run time, and used by the debugger.
1047 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1051 // Emit any relocs we saved in an attempt to avoid generating COPY
1053 if (this->copy_relocs_ == NULL)
1055 if (this->copy_relocs_->any_to_emit())
1057 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1058 this->copy_relocs_->emit(rel_dyn);
1060 delete this->copy_relocs_;
1061 this->copy_relocs_ = NULL;
1064 // Perform a relocation.
1067 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1068 Target_x86_64* target,
1070 const elfcpp::Rela<64, false>& rel,
1071 unsigned int r_type,
1072 const Sized_symbol<64>* gsym,
1073 const Symbol_value<64>* psymval,
1074 unsigned char* view,
1075 elfcpp::Elf_types<64>::Elf_Addr address,
1078 if (this->skip_call_tls_get_addr_)
1080 if (r_type != elfcpp::R_X86_64_PLT32
1082 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1084 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1086 relinfo->location(relnum, rel.get_r_offset()).c_str());
1090 this->skip_call_tls_get_addr_ = false;
1095 // Pick the value to use for symbols defined in shared objects.
1096 Symbol_value<64> symval;
1097 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1099 symval.set_output_value(target->plt_section()->address()
1100 + gsym->plt_offset());
1104 const Sized_relobj<64, false>* object = relinfo->object;
1105 const elfcpp::Elf_Xword addend = rel.get_r_addend();
1109 case elfcpp::R_X86_64_NONE:
1110 case elfcpp::R_386_GNU_VTINHERIT:
1111 case elfcpp::R_386_GNU_VTENTRY:
1114 case elfcpp::R_X86_64_64:
1115 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1118 case elfcpp::R_X86_64_PC64:
1119 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1123 case elfcpp::R_X86_64_32:
1124 // FIXME: we need to verify that value + addend fits into 32 bits:
1125 // uint64_t x = value + addend;
1126 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1127 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1128 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1131 case elfcpp::R_X86_64_32S:
1132 // FIXME: we need to verify that value + addend fits into 32 bits:
1133 // int64_t x = value + addend; // note this quantity is signed!
1134 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1135 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1138 case elfcpp::R_X86_64_PC32:
1139 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1143 case elfcpp::R_X86_64_16:
1144 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1147 case elfcpp::R_X86_64_PC16:
1148 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1152 case elfcpp::R_X86_64_8:
1153 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1156 case elfcpp::R_X86_64_PC8:
1157 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1161 case elfcpp::R_X86_64_PLT32:
1162 gold_assert(gsym->has_plt_offset()
1163 || gsym->final_value_is_known());
1164 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1168 case elfcpp::R_X86_64_GOT32:
1169 // Local GOT offsets not yet supported.
1171 gold_assert(gsym->has_got_offset());
1172 Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend);
1175 case elfcpp::R_X86_64_GOTPC32:
1178 elfcpp::Elf_types<64>::Elf_Addr value;
1179 value = target->got_section(NULL, NULL)->address();
1180 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1184 case elfcpp::R_X86_64_GOT64:
1185 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1186 // Since we always add a PLT entry, this is equivalent.
1187 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
1188 // Local GOT offsets not yet supported.
1190 gold_assert(gsym->has_got_offset());
1191 Relocate_functions<64, false>::rela64(view, gsym->got_offset(), addend);
1194 case elfcpp::R_X86_64_GOTPC64:
1197 elfcpp::Elf_types<64>::Elf_Addr value;
1198 value = target->got_section(NULL, NULL)->address();
1199 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1203 case elfcpp::R_X86_64_GOTOFF64:
1205 elfcpp::Elf_types<64>::Elf_Addr value;
1206 value = (psymval->value(object, 0)
1207 - target->got_section(NULL, NULL)->address());
1208 Relocate_functions<64, false>::rela64(view, value, addend);
1212 case elfcpp::R_X86_64_GOTPCREL:
1215 elfcpp::Elf_types<64>::Elf_Addr value;
1216 // FIXME(csilvers): this is probably totally wrong for G + GOT
1217 value = (target->got_section(NULL, NULL)->address()
1218 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1219 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1223 case elfcpp::R_X86_64_GOTPCREL64:
1226 elfcpp::Elf_types<64>::Elf_Addr value;
1227 // FIXME(csilvers): this is probably totally wrong for G + GOT
1228 value = (target->got_section(NULL, NULL)->address()
1229 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1230 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1234 case elfcpp::R_X86_64_COPY:
1235 case elfcpp::R_X86_64_GLOB_DAT:
1236 case elfcpp::R_X86_64_JUMP_SLOT:
1237 case elfcpp::R_X86_64_RELATIVE:
1238 case elfcpp::R_X86_64_TPOFF64:
1239 case elfcpp::R_X86_64_TPOFF32:
1240 case elfcpp::R_X86_64_DTPMOD64:
1241 case elfcpp::R_X86_64_DTPOFF64:
1242 case elfcpp::R_X86_64_DTPOFF32:
1243 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
1244 case elfcpp::R_X86_64_TLSDESC:
1245 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
1246 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
1247 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1249 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1255 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
1256 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
1258 case elfcpp::R_386_TLS_IE:
1259 case elfcpp::R_386_TLS_GOTIE:
1260 case elfcpp::R_386_TLS_LE:
1261 case elfcpp::R_386_TLS_GD:
1262 case elfcpp::R_386_TLS_LDM:
1263 case elfcpp::R_386_TLS_LDO_64:
1264 case elfcpp::R_386_TLS_IE_64:
1265 case elfcpp::R_386_TLS_LE_64:
1266 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1267 address, view_size);
1270 view_size++; // this is to make view_size used
1273 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
1274 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
1275 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): implement me!
1277 case elfcpp::R_386_TLS_GD_64:
1278 case elfcpp::R_386_TLS_GD_PUSH:
1279 case elfcpp::R_386_TLS_GD_CALL:
1280 case elfcpp::R_386_TLS_GD_POP:
1281 case elfcpp::R_386_TLS_LDM_64:
1282 case elfcpp::R_386_TLS_LDM_PUSH:
1283 case elfcpp::R_386_TLS_LDM_CALL:
1284 case elfcpp::R_386_TLS_LDM_POP:
1287 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1289 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1291 // gold_exit(false);
1298 // Perform a TLS relocation.
1301 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>*, // relinfo,
1303 const elfcpp::Rela<64, false>& , // rel,
1304 unsigned int , // r_type,
1305 const Sized_symbol<64>* , // gsym,
1306 const Symbol_value<64>* , // psymval,
1307 unsigned char* , // view,
1308 elfcpp::Elf_types<64>::Elf_Addr,
1312 Output_segment* tls_segment = relinfo->layout->tls_segment();
1313 if (tls_segment == NULL)
1315 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1317 relinfo->location(relnum, rel.get_r_offset()).c_str());
1321 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1323 const bool is_final = (gsym == NULL
1324 ? !parameters->output_is_shared()
1325 : gsym->final_value_is_known());
1326 const unsigned int opt_r_type =
1327 Target_x86_64::optimize_tls_reloc(is_final, r_type);
1330 case elfcpp::R_X86_64_TLS_LE_64:
1331 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1332 Relocate_functions<64, false>::rel64(view, value);
1335 case elfcpp::R_X86_64_TLS_LE:
1336 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1337 Relocate_functions<64, false>::rel64(view, value);
1340 case elfcpp::R_X86_64_TLS_IE:
1341 case elfcpp::R_X86_64_TLS_GOTIE:
1342 case elfcpp::R_X86_64_TLS_IE_64:
1343 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1345 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1346 rel, r_type, value, view,
1350 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1352 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1354 // gold_exit(false);
1357 case elfcpp::R_X86_64_TLS_GD:
1358 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1360 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1361 rel, r_type, value, view,
1365 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1367 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1369 // gold_exit(false);
1372 case elfcpp::R_X86_64_TLS_LDM:
1373 case elfcpp::R_X86_64_TLS_LDO_64:
1374 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1376 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1378 // gold_exit(false);
1384 // Do a relocation in which we convert a TLS Initial-Exec to a
1388 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* , // relinfo,
1390 Output_segment* , // tls_segment,
1391 const elfcpp::Rela<64, false>& , // rel,
1392 unsigned int , // r_type,
1393 elfcpp::Elf_types<64>::Elf_Addr , // value,
1394 unsigned char* , // view,
1395 off_t) // view_size)
1398 // We have to actually change the instructions, which means that we
1399 // need to examine the opcodes to figure out which instruction we
1401 if (r_type == elfcpp::R_X86_64_TLS_IE)
1403 // movl %gs:XX,%eax ==> movl $YY,%eax
1404 // movl %gs:XX,%reg ==> movl $YY,%reg
1405 // addl %gs:XX,%reg ==> addl $YY,%reg
1406 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1407 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1409 unsigned char op1 = view[-1];
1412 // movl XX,%eax ==> movl $YY,%eax
1417 Target_x86_64::Relocate::check_range(relinfo, relnum, rel,
1420 unsigned char op2 = view[-2];
1423 // movl XX,%reg ==> movl $YY,%reg
1424 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1425 (op1 & 0xc7) == 0x05);
1427 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1429 else if (op2 == 0x03)
1431 // addl XX,%reg ==> addl $YY,%reg
1432 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1433 (op1 & 0xc7) == 0x05);
1435 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1438 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1443 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1444 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1445 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1446 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1447 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1449 unsigned char op1 = view[-1];
1450 unsigned char op2 = view[-2];
1451 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1452 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1455 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1457 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1459 else if (op2 == 0x2b)
1461 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1463 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1465 else if (op2 == 0x03)
1467 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1469 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1472 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1475 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1476 if (r_type == elfcpp::R_X86_64_TLS_IE || r_type == elfcpp::R_X86_64_TLS_GOTIE)
1479 Relocate_functions<64, false>::rel64(view, value);
1483 // Do a relocation in which we convert a TLS Global-Dynamic to a
1487 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* , // relinfo,
1489 Output_segment* , // tls_segment,
1490 const elfcpp::Rela<64, false>& , // rel,
1492 elfcpp::Elf_types<64>::Elf_Addr , // value,
1493 unsigned char* , // view,
1497 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1498 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1499 // leal foo(%reg),%eax; call ___tls_get_addr
1500 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1502 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1503 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1505 unsigned char op1 = view[-1];
1506 unsigned char op2 = view[-2];
1508 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1509 op2 == 0x8d || op2 == 0x04);
1510 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1517 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1518 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1520 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1521 ((op1 & 0xc7) == 0x05
1522 && op1 != (4 << 3)));
1523 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1527 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1528 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1529 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1531 // There is a trailing nop. Use the size byte subl.
1532 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1537 // Use the five byte subl.
1538 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1542 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1543 Relocate_functions<64, false>::rel64(view + roff, value);
1545 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1547 this->skip_call_tls_get_addr_ = true;
1551 // Check the range for a TLS relocation.
1554 Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo,
1556 const elfcpp::Rela<64, false>& rel,
1557 off_t view_size, off_t off)
1559 off_t offset = rel.get_r_offset() + off;
1560 if (offset < 0 || offset > view_size)
1562 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1564 relinfo->location(relnum, rel.get_r_offset()).c_str());
1569 // Check the validity of a TLS relocation. This is like assert.
1572 Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo,
1574 const elfcpp::Rela<64, false>& rel,
1580 _("%s: %s: TLS relocation against invalid instruction\n"),
1582 relinfo->location(relnum, rel.get_r_offset()).c_str());
1587 // Relocate section data.
1590 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1591 unsigned int sh_type,
1592 const unsigned char* prelocs,
1594 unsigned char* view,
1595 elfcpp::Elf_types<64>::Elf_Addr address,
1598 gold_assert(sh_type == elfcpp::SHT_RELA);
1600 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1601 Target_x86_64::Relocate>(
1611 // Return a string used to fill a code section with nops to take up
1612 // the specified length.
1615 Target_x86_64::do_code_fill(off_t length)
1619 // Build a jmpq instruction to skip over the bytes.
1620 unsigned char jmp[5];
1622 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1623 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1624 + std::string(length - 5, '\0'));
1627 // Nop sequences of various lengths.
1628 const char nop1[1] = { 0x90 }; // nop
1629 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1630 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1631 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1632 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1633 0x00 }; // leal 0(%esi,1),%esi
1634 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1636 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1638 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1639 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1640 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1641 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1643 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1644 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1646 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1647 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1649 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1650 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1651 0x00, 0x00, 0x00, 0x00 };
1652 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1653 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1654 0x27, 0x00, 0x00, 0x00,
1656 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1657 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1658 0xbc, 0x27, 0x00, 0x00,
1660 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1661 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1662 0x90, 0x90, 0x90, 0x90,
1665 const char* nops[16] = {
1667 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1668 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1671 return std::string(nops[length], length);
1674 // The selector for x86_64 object files.
1676 class Target_selector_x86_64 : public Target_selector
1679 Target_selector_x86_64()
1680 : Target_selector(elfcpp::EM_X86_64, 64, false)
1684 recognize(int machine, int osabi, int abiversion);
1687 Target_x86_64* target_;
1690 // Recognize an x86_64 object file when we already know that the machine
1691 // number is EM_X86_64.
1694 Target_selector_x86_64::recognize(int, int, int)
1696 if (this->target_ == NULL)
1697 this->target_ = new Target_x86_64();
1698 return this->target_;
1701 Target_selector_x86_64 target_selector_x86_64;
1703 } // End anonymous namespace.