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 // Return the value to use for a dynamic which requires special
92 do_dynsym_value(const Symbol*) const;
94 // Relocate a section.
96 relocate_section(const Relocate_info<64, false>*,
98 const unsigned char* prelocs,
101 elfcpp::Elf_types<64>::Elf_Addr view_address,
104 // Return a string used to fill a code section with nops.
106 do_code_fill(off_t length);
109 // The class which scans relocations.
113 local(const General_options& options, Symbol_table* symtab,
114 Layout* layout, Target_x86_64* target,
115 Sized_relobj<64, false>* object,
116 unsigned int data_shndx,
117 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
118 const elfcpp::Sym<64, false>& lsym);
121 global(const General_options& options, Symbol_table* symtab,
122 Layout* layout, Target_x86_64* target,
123 Sized_relobj<64, false>* object,
124 unsigned int data_shndx,
125 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
129 // The class which implements relocation.
134 : skip_call_tls_get_addr_(false)
139 if (this->skip_call_tls_get_addr_)
141 // FIXME: This needs to specify the location somehow.
142 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
148 // Do a relocation. Return false if the caller should not issue
149 // any warnings about this relocation.
151 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
152 const elfcpp::Rela<64, false>&,
153 unsigned int r_type, const Sized_symbol<64>*,
154 const Symbol_value<64>*,
155 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
159 // Do a TLS relocation.
161 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
162 const elfcpp::Rela<64, false>&,
163 unsigned int r_type, const Sized_symbol<64>*,
164 const Symbol_value<64>*,
165 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
167 // Do a TLS Initial-Exec to Local-Exec transition.
169 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
170 Output_segment* tls_segment,
171 const elfcpp::Rela<64, false>&, unsigned int r_type,
172 elfcpp::Elf_types<64>::Elf_Addr value,
176 // Do a TLS Global-Dynamic to Local-Exec transition.
178 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
179 Output_segment* tls_segment,
180 const elfcpp::Rela<64, false>&, unsigned int r_type,
181 elfcpp::Elf_types<64>::Elf_Addr value,
185 // Check the range for a TLS relocation.
187 check_range(const Relocate_info<64, false>*, size_t relnum,
188 const elfcpp::Rela<64, false>&, off_t, off_t);
190 // Check the validity of a TLS relocation. This is like assert.
192 check_tls(const Relocate_info<64, false>*, size_t relnum,
193 const elfcpp::Rela<64, false>&, bool);
195 // This is set if we should skip the next reloc, which should be a
196 // PLT32 reloc against ___tls_get_addr.
197 bool skip_call_tls_get_addr_;
200 // Adjust TLS relocation type based on the options and whether this
201 // is a local symbol.
203 optimize_tls_reloc(bool is_final, int r_type);
205 // Get the GOT section, creating it if necessary.
206 Output_data_got<64, false>*
207 got_section(Symbol_table*, Layout*);
209 // Create a PLT entry for a global symbol.
211 make_plt_entry(Symbol_table*, Layout*, Symbol*);
213 // Get the PLT section.
214 Output_data_plt_x86_64*
217 gold_assert(this->plt_ != NULL);
221 // Get the dynamic reloc section, creating it if necessary.
223 rel_dyn_section(Layout*);
225 // Copy a relocation against a global symbol.
227 copy_reloc(const General_options*, Symbol_table*, Layout*,
228 Sized_relobj<64, false>*, unsigned int,
229 Symbol*, const elfcpp::Rela<64, false>&);
231 // Information about this specific target which we pass to the
232 // general Target structure.
233 static const Target::Target_info x86_64_info;
236 Output_data_got<64, false>* got_;
238 Output_data_plt_x86_64* plt_;
239 // The GOT PLT section.
240 Output_data_space* got_plt_;
241 // The dynamic reloc section.
242 Reloc_section* rel_dyn_;
243 // Relocs saved to avoid a COPY reloc.
244 Copy_relocs<64, false>* copy_relocs_;
245 // Space for variables copied with a COPY reloc.
246 Output_data_space* dynbss_;
249 const Target::Target_info Target_x86_64::x86_64_info =
252 false, // is_big_endian
253 elfcpp::EM_X86_64, // machine_code
254 false, // has_make_symbol
255 false, // has_resolve
256 true, // has_code_fill
257 "/lib/ld64.so.1", // program interpreter
258 0x400000, // text_segment_address
259 0x1000, // abi_pagesize
260 0x1000 // common_pagesize
263 // Get the GOT section, creating it if necessary.
265 Output_data_got<64, false>*
266 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
268 if (this->got_ == NULL)
270 gold_assert(symtab != NULL && layout != NULL);
272 this->got_ = new Output_data_got<64, false>();
274 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
275 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
278 // The old GNU linker creates a .got.plt section. We just
279 // create another set of data in the .got section. Note that we
280 // always create a PLT if we create a GOT, although the PLT
282 // TODO(csilvers): do we really need an alignment of 8?
283 this->got_plt_ = new Output_data_space(8);
284 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
285 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
288 // The first three entries are reserved.
289 this->got_plt_->set_space_size(3 * 8);
291 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
292 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
294 0, 0, elfcpp::STT_OBJECT,
296 elfcpp::STV_HIDDEN, 0,
303 // Get the dynamic reloc section, creating it if necessary.
305 Target_x86_64::Reloc_section*
306 Target_x86_64::rel_dyn_section(Layout* layout)
308 if (this->rel_dyn_ == NULL)
310 gold_assert(layout != NULL);
311 this->rel_dyn_ = new Reloc_section();
312 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
313 elfcpp::SHF_ALLOC, this->rel_dyn_);
315 return this->rel_dyn_;
318 // A class to handle the PLT data.
320 class Output_data_plt_x86_64 : public Output_section_data
323 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
325 Output_data_plt_x86_64(Layout*, Output_data_space*);
327 // Add an entry to the PLT.
329 add_entry(Symbol* gsym);
331 // Return the .rel.plt section data.
334 { return this->rel_; }
338 do_adjust_output_section(Output_section* os);
341 // The size of an entry in the PLT.
342 static const int plt_entry_size = 16;
344 // The first entry in the PLT.
345 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
346 // procedure linkage table for both programs and shared objects."
347 static unsigned char first_plt_entry[plt_entry_size];
349 // Other entries in the PLT for an executable.
350 static unsigned char plt_entry[plt_entry_size];
352 // Set the final size.
354 do_set_address(uint64_t, off_t)
355 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
357 // Write out the PLT data.
359 do_write(Output_file*);
361 // The reloc section.
363 // The .got.plt section.
364 Output_data_space* got_plt_;
365 // The number of PLT entries.
369 // Create the PLT section. The ordinary .got section is an argument,
370 // since we need to refer to the start. We also create our own .got
371 // section just for PLT entries.
373 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
374 Output_data_space* got_plt)
375 // TODO(csilvers): do we really need an alignment of 8?
376 : Output_section_data(8), got_plt_(got_plt), count_(0)
378 this->rel_ = new Reloc_section();
379 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
380 elfcpp::SHF_ALLOC, this->rel_);
384 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
386 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
387 // linker, and so do we.
391 // Add an entry to the PLT.
394 Output_data_plt_x86_64::add_entry(Symbol* gsym)
396 gold_assert(!gsym->has_plt_offset());
398 // Note that when setting the PLT offset we skip the initial
399 // reserved PLT entry.
400 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
404 off_t got_offset = this->got_plt_->data_size();
406 // Every PLT entry needs a GOT entry which points back to the PLT
407 // entry (this will be changed by the dynamic linker, normally
408 // lazily when the function is called).
409 this->got_plt_->set_space_size(got_offset + 8);
411 // Every PLT entry needs a reloc.
412 gsym->set_needs_dynsym_entry();
413 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
416 // Note that we don't need to save the symbol. The contents of the
417 // PLT are independent of which symbols are used. The symbols only
418 // appear in the relocations.
421 // The first entry in the PLT for an executable.
423 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
425 // From AMD64 ABI Draft 0.98, page 76
426 0xff, 0x35, // pushq contents of memory address
427 0, 0, 0, 0, // replaced with address of .got + 4
428 0xff, 0x25, // jmp indirect
429 0, 0, 0, 0, // replaced with address of .got + 8
430 0x90, 0x90, 0x90, 0x90 // noop (x4)
433 // Subsequent entries in the PLT for an executable.
435 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
437 // From AMD64 ABI Draft 0.98, page 76
438 0xff, 0x25, // jmpq indirect
439 0, 0, 0, 0, // replaced with address of symbol in .got
440 0x68, // pushq immediate
441 0, 0, 0, 0, // replaced with offset into relocation table
442 0xe9, // jmpq relative
443 0, 0, 0, 0 // replaced with offset to start of .plt
446 // Write out the PLT. This uses the hand-coded instructions above,
447 // and adjusts them as needed. This is specified by the AMD64 ABI.
450 Output_data_plt_x86_64::do_write(Output_file* of)
452 const off_t offset = this->offset();
453 const off_t oview_size = this->data_size();
454 unsigned char* const oview = of->get_output_view(offset, oview_size);
456 const off_t got_file_offset = this->got_plt_->offset();
457 const off_t got_size = this->got_plt_->data_size();
458 unsigned char* const got_view = of->get_output_view(got_file_offset,
461 unsigned char* pov = oview;
463 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
464 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
466 memcpy(pov, first_plt_entry, plt_entry_size);
467 if (!parameters->output_is_shared())
469 // We do a jmp relative to the PC at the end of this instruction.
470 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
471 - (plt_address + 6));
472 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
473 - (plt_address + 12));
475 pov += plt_entry_size;
477 unsigned char* got_pov = got_view;
479 memset(got_pov, 0, 24);
482 unsigned int plt_offset = plt_entry_size;
483 unsigned int got_offset = 24;
484 const unsigned int count = this->count_;
485 for (unsigned int plt_index = 0;
488 pov += plt_entry_size,
490 plt_offset += plt_entry_size,
493 // Set and adjust the PLT entry itself.
494 memcpy(pov, plt_entry, plt_entry_size);
495 if (parameters->output_is_shared())
496 // FIXME(csilvers): what's the right thing to write here?
497 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
499 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
500 (got_address + got_offset
501 - (plt_address + plt_offset
504 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
505 elfcpp::Swap<32, false>::writeval(pov + 12,
506 - (plt_offset + plt_entry_size));
508 // Set the entry in the GOT.
509 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
512 gold_assert(pov - oview == oview_size);
513 gold_assert(got_pov - got_view == got_size);
515 of->write_output_view(offset, oview_size, oview);
516 of->write_output_view(got_file_offset, got_size, got_view);
519 // Create a PLT entry for a global symbol.
522 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
525 if (gsym->has_plt_offset())
528 if (this->plt_ == NULL)
530 // Create the GOT sections first.
531 this->got_section(symtab, layout);
533 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
534 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
536 | elfcpp::SHF_EXECINSTR),
540 this->plt_->add_entry(gsym);
543 // Handle a relocation against a non-function symbol defined in a
544 // dynamic object. The traditional way to handle this is to generate
545 // a COPY relocation to copy the variable at runtime from the shared
546 // object into the executable's data segment. However, this is
547 // undesirable in general, as if the size of the object changes in the
548 // dynamic object, the executable will no longer work correctly. If
549 // this relocation is in a writable section, then we can create a
550 // dynamic reloc and the dynamic linker will resolve it to the correct
551 // address at runtime. However, we do not want do that if the
552 // relocation is in a read-only section, as it would prevent the
553 // readonly segment from being shared. And if we have to eventually
554 // generate a COPY reloc, then any dynamic relocations will be
555 // useless. So this means that if this is a writable section, we need
556 // to save the relocation until we see whether we have to create a
557 // COPY relocation for this symbol for any other relocation.
560 Target_x86_64::copy_reloc(const General_options* options,
561 Symbol_table* symtab,
563 Sized_relobj<64, false>* object,
564 unsigned int data_shndx, Symbol* gsym,
565 const elfcpp::Rela<64, false>& rel)
567 Sized_symbol<64>* ssym;
568 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
571 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
574 // So far we do not need a COPY reloc. Save this relocation.
575 // If it turns out that we never need a COPY reloc for this
576 // symbol, then we will emit the relocation.
577 if (this->copy_relocs_ == NULL)
578 this->copy_relocs_ = new Copy_relocs<64, false>();
579 this->copy_relocs_->save(ssym, object, data_shndx, rel);
583 // Allocate space for this symbol in the .bss section.
585 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
587 // There is no defined way to determine the required alignment
588 // of the symbol. We pick the alignment based on the size. We
589 // set an arbitrary maximum of 256.
591 for (align = 1; align < 512; align <<= 1)
592 if ((symsize & align) != 0)
595 if (this->dynbss_ == NULL)
597 this->dynbss_ = new Output_data_space(align);
598 layout->add_output_section_data(".bss",
601 | elfcpp::SHF_WRITE),
605 Output_data_space* dynbss = this->dynbss_;
607 if (align > dynbss->addralign())
608 dynbss->set_space_alignment(align);
610 off_t dynbss_size = dynbss->data_size();
611 dynbss_size = align_address(dynbss_size, align);
612 off_t offset = dynbss_size;
613 dynbss->set_space_size(dynbss_size + symsize);
615 // Define the symbol in the .dynbss section.
616 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
617 dynbss, offset, symsize, ssym->type(),
618 ssym->binding(), ssym->visibility(),
619 ssym->nonvis(), false, false);
621 // Add the COPY reloc.
622 ssym->set_needs_dynsym_entry();
623 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
624 // TODO(csilvers): should last arg here be rel.get_r_addend?
625 rel_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
630 // Optimize the TLS relocation type based on what we know about the
631 // symbol. IS_FINAL is true if the final address of this symbol is
632 // known at link time.
635 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
637 return is_final ? r_type : 0;
639 // If we are generating a shared library, then we can't do anything
641 if (parameters->output_is_shared())
646 case elfcpp::R_X86_64_TLSGD:
647 // These are Global-Dynamic which permits fully general TLS
648 // access. Since we know that we are generating an executable,
649 // we can convert this to Initial-Exec. If we also know that
650 // this is a local symbol, we can further switch to Local-Exec.
652 return elfcpp::R_X86_64_TLS_LE_64;
653 return elfcpp::R_X86_64_TLS_IE_64;
655 case elfcpp::R_X86_64_TLS_LDM:
656 // This is Local-Dynamic, which refers to a local symbol in the
657 // dynamic TLS block. Since we know that we generating an
658 // executable, we can switch to Local-Exec.
659 return elfcpp::R_X86_64_TLS_LE_64;
661 case elfcpp::R_X86_64_TLS_LDO_64:
662 // Another type of Local-Dynamic relocation.
663 return elfcpp::R_X86_64_TLS_LE;
665 case elfcpp::R_X86_64_TLS_IE:
666 case elfcpp::R_X86_64_TLS_GOTIE:
667 case elfcpp::R_X86_64_TLS_IE_64:
668 // These are Initial-Exec relocs which get the thread offset
669 // from the GOT. If we know that we are linking against the
670 // local symbol, we can switch to Local-Exec, which links the
671 // thread offset into the instruction.
673 return elfcpp::R_X86_64_TLS_LE_64;
676 case elfcpp::R_X86_64_TLS_LE:
677 case elfcpp::R_X86_64_TLS_LE_64:
678 // When we already have Local-Exec, there is nothing further we
688 // Scan a relocation for a local symbol.
691 Target_x86_64::Scan::local(const General_options&,
692 Symbol_table* symtab,
694 Target_x86_64* target,
695 Sized_relobj<64, false>* object,
697 const elfcpp::Rela<64, false>&,
699 const elfcpp::Sym<64, false>&)
703 case elfcpp::R_X86_64_NONE:
704 case elfcpp::R_386_GNU_VTINHERIT:
705 case elfcpp::R_386_GNU_VTENTRY:
708 case elfcpp::R_X86_64_64:
709 case elfcpp::R_X86_64_32:
710 case elfcpp::R_X86_64_32S:
711 case elfcpp::R_X86_64_16:
712 case elfcpp::R_X86_64_8:
713 // FIXME: If we are generating a shared object we need to copy
714 // this relocation into the object.
715 gold_assert(!parameters->output_is_shared());
718 case elfcpp::R_X86_64_PC64:
719 case elfcpp::R_X86_64_PC32:
720 case elfcpp::R_X86_64_PC16:
721 case elfcpp::R_X86_64_PC8:
724 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
725 case elfcpp::R_X86_64_GOTOFF64:
726 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
727 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
728 // We need a GOT section.
729 target->got_section(symtab, layout);
732 case elfcpp::R_X86_64_COPY:
733 case elfcpp::R_X86_64_GLOB_DAT:
734 case elfcpp::R_X86_64_JUMP_SLOT:
735 case elfcpp::R_X86_64_RELATIVE:
736 case elfcpp::R_X86_64_TPOFF64:
737 case elfcpp::R_X86_64_TPOFF32:
738 case elfcpp::R_X86_64_DTPMOD64:
739 case elfcpp::R_X86_64_DTPOFF64:
740 case elfcpp::R_X86_64_DTPOFF32:
741 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
742 case elfcpp::R_X86_64_TLSDESC:
743 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
744 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
745 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
746 program_name, object->name().c_str(), r_type);
751 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
752 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
754 case elfcpp::R_386_TLS_IE:
755 case elfcpp::R_386_TLS_GOTIE:
756 case elfcpp::R_386_TLS_LE:
757 case elfcpp::R_386_TLS_GD:
758 case elfcpp::R_386_TLS_LDM:
759 case elfcpp::R_386_TLS_LDO_64:
760 case elfcpp::R_386_TLS_IE_64:
761 case elfcpp::R_386_TLS_LE_64:
763 bool output_is_shared = parameters->output_is_shared();
764 r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared,
768 case elfcpp::R_X86_64_TLS_LE:
769 case elfcpp::R_X86_64_TLS_LE_64:
770 // FIXME: If generating a shared object, we need to copy
771 // this relocation into the object.
772 gold_assert(!output_is_shared);
775 case elfcpp::R_X86_64_TLS_IE:
776 case elfcpp::R_X86_64_TLS_GOTIE:
777 case elfcpp::R_X86_64_TLS_GD:
778 case elfcpp::R_X86_64_TLS_LDM:
779 case elfcpp::R_X86_64_TLS_LDO_64:
780 case elfcpp::R_X86_64_TLS_IE_64:
782 _("%s: %s: unsupported reloc %u against local symbol\n"),
783 program_name, object->name().c_str(), r_type);
790 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
791 case elfcpp::R_X86_64_GOT32:
792 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
793 case elfcpp::R_X86_64_GOTPCREL:
794 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
795 case elfcpp::R_X86_64_PLT32:
796 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
797 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
799 case elfcpp::R_386_TLS_GD_64:
800 case elfcpp::R_386_TLS_GD_PUSH:
801 case elfcpp::R_386_TLS_GD_CALL:
802 case elfcpp::R_386_TLS_GD_POP:
803 case elfcpp::R_386_TLS_LDM_64:
804 case elfcpp::R_386_TLS_LDM_PUSH:
805 case elfcpp::R_386_TLS_LDM_CALL:
806 case elfcpp::R_386_TLS_LDM_POP:
809 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
810 program_name, object->name().c_str(), r_type);
816 // Scan a relocation for a global symbol.
819 Target_x86_64::Scan::global(const General_options& options,
820 Symbol_table* symtab,
822 Target_x86_64* target,
823 Sized_relobj<64, false>* object,
824 unsigned int data_shndx,
825 const elfcpp::Rela<64, false>& reloc,
831 case elfcpp::R_X86_64_NONE:
832 case elfcpp::R_386_GNU_VTINHERIT:
833 case elfcpp::R_386_GNU_VTENTRY:
836 case elfcpp::R_X86_64_64:
837 case elfcpp::R_X86_64_PC64:
838 case elfcpp::R_X86_64_32:
839 case elfcpp::R_X86_64_32S:
840 case elfcpp::R_X86_64_PC32:
841 case elfcpp::R_X86_64_16:
842 case elfcpp::R_X86_64_PC16:
843 case elfcpp::R_X86_64_8:
844 case elfcpp::R_X86_64_PC8:
845 // FIXME: If we are generating a shared object we may need to
846 // copy this relocation into the object. If this symbol is
847 // defined in a shared object, we may need to copy this
848 // relocation in order to avoid a COPY relocation.
849 gold_assert(!parameters->output_is_shared());
851 if (gsym->is_from_dynobj())
853 // This symbol is defined in a dynamic object. If it is a
854 // function, we make a PLT entry. Otherwise we need to
855 // either generate a COPY reloc or copy this reloc.
856 if (gsym->type() == elfcpp::STT_FUNC)
858 target->make_plt_entry(symtab, layout, gsym);
860 // If this is not a PC relative reference, then we may
861 // be taking the address of the function. In that case
862 // we need to set the entry in the dynamic symbol table
863 // to the address of the PLT entry.
864 if (r_type != elfcpp::R_X86_64_PC64
865 && r_type != elfcpp::R_X86_64_PC32
866 && r_type != elfcpp::R_X86_64_PC16
867 && r_type != elfcpp::R_X86_64_PC8)
868 gsym->set_needs_dynsym_value();
871 target->copy_reloc(&options, symtab, layout, object, data_shndx,
877 case elfcpp::R_X86_64_GOT64:
878 case elfcpp::R_X86_64_GOT32:
879 case elfcpp::R_X86_64_GOTPCREL64:
880 case elfcpp::R_X86_64_GOTPCREL:
881 case elfcpp::R_X86_64_GOTPLT64:
883 // The symbol requires a GOT entry.
884 Output_data_got<64, false>* got = target->got_section(symtab, layout);
885 if (got->add_global(gsym))
887 // If this symbol is not fully resolved, we need to add a
888 // dynamic relocation for it.
889 if (!gsym->final_value_is_known())
891 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
892 rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
893 gsym->got_offset(), 0);
899 case elfcpp::R_X86_64_PLT32:
900 // If the symbol is fully resolved, this is just a PC32 reloc.
901 // Otherwise we need a PLT entry.
902 if (gsym->final_value_is_known())
904 target->make_plt_entry(symtab, layout, gsym);
907 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
908 case elfcpp::R_X86_64_GOTOFF64:
909 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
910 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
911 // We need a GOT section.
912 target->got_section(symtab, layout);
915 case elfcpp::R_X86_64_COPY:
916 case elfcpp::R_X86_64_GLOB_DAT:
917 case elfcpp::R_X86_64_JUMP_SLOT:
918 case elfcpp::R_X86_64_RELATIVE:
919 case elfcpp::R_X86_64_TPOFF64:
920 case elfcpp::R_X86_64_TPOFF32:
921 case elfcpp::R_X86_64_DTPMOD64:
922 case elfcpp::R_X86_64_DTPOFF64:
923 case elfcpp::R_X86_64_DTPOFF32:
924 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
925 case elfcpp::R_X86_64_TLSDESC:
926 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
927 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
928 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
929 program_name, object->name().c_str(), r_type);
934 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
935 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
937 case elfcpp::R_386_TLS_IE:
938 case elfcpp::R_386_TLS_GOTIE:
939 case elfcpp::R_386_TLS_LE:
940 case elfcpp::R_386_TLS_GD:
941 case elfcpp::R_386_TLS_LDM:
942 case elfcpp::R_386_TLS_LDO_64:
943 case elfcpp::R_386_TLS_IE_64:
944 case elfcpp::R_386_TLS_LE_64:
946 const bool is_final = gsym->final_value_is_known();
947 r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type);
950 case elfcpp::R_386_TLS_LE:
951 case elfcpp::R_386_TLS_LE_64:
952 // FIXME: If generating a shared object, we need to copy
953 // this relocation into the object.
954 gold_assert(!parameters->output_is_shared());
957 case elfcpp::R_386_TLS_IE:
958 case elfcpp::R_386_TLS_GOTIE:
959 case elfcpp::R_386_TLS_GD:
960 case elfcpp::R_386_TLS_LDM:
961 case elfcpp::R_386_TLS_LDO_64:
962 case elfcpp::R_386_TLS_IE_64:
964 _("%s: %s: unsupported reloc %u "
965 "against global symbol %s\n"),
966 program_name, object->name().c_str(), r_type,
974 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
975 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
977 case elfcpp::R_386_TLS_GD_64:
978 case elfcpp::R_386_TLS_GD_PUSH:
979 case elfcpp::R_386_TLS_GD_CALL:
980 case elfcpp::R_386_TLS_GD_POP:
981 case elfcpp::R_386_TLS_LDM_64:
982 case elfcpp::R_386_TLS_LDM_PUSH:
983 case elfcpp::R_386_TLS_LDM_CALL:
984 case elfcpp::R_386_TLS_LDM_POP:
988 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
989 program_name, object->name().c_str(), r_type, gsym->name());
994 // Scan relocations for a section.
997 Target_x86_64::scan_relocs(const General_options& options,
998 Symbol_table* symtab,
1000 Sized_relobj<64, false>* object,
1001 unsigned int data_shndx,
1002 unsigned int sh_type,
1003 const unsigned char* prelocs,
1005 size_t local_symbol_count,
1006 const unsigned char* plocal_symbols,
1007 Symbol** global_symbols)
1009 if (sh_type == elfcpp::SHT_REL)
1011 fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"),
1012 program_name, object->name().c_str());
1016 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1017 Target_x86_64::Scan>(
1031 // Finalize the sections.
1034 Target_x86_64::do_finalize_sections(Layout* layout)
1036 // Fill in some more dynamic tags.
1037 Output_data_dynamic* const odyn = layout->dynamic_data();
1040 if (this->got_plt_ != NULL)
1041 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1043 if (this->plt_ != NULL)
1045 const Output_data* od = this->plt_->rel_plt();
1046 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1047 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1048 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1051 if (this->rel_dyn_ != NULL)
1053 const Output_data* od = this->rel_dyn_;
1054 odyn->add_section_address(elfcpp::DT_RELA, od);
1055 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1056 odyn->add_constant(elfcpp::DT_RELAENT,
1057 elfcpp::Elf_sizes<64>::rela_size);
1060 if (!parameters->output_is_shared())
1062 // The value of the DT_DEBUG tag is filled in by the dynamic
1063 // linker at run time, and used by the debugger.
1064 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1068 // Emit any relocs we saved in an attempt to avoid generating COPY
1070 if (this->copy_relocs_ == NULL)
1072 if (this->copy_relocs_->any_to_emit())
1074 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1075 this->copy_relocs_->emit(rel_dyn);
1077 delete this->copy_relocs_;
1078 this->copy_relocs_ = NULL;
1081 // Perform a relocation.
1084 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1085 Target_x86_64* target,
1087 const elfcpp::Rela<64, false>& rel,
1088 unsigned int r_type,
1089 const Sized_symbol<64>* gsym,
1090 const Symbol_value<64>* psymval,
1091 unsigned char* view,
1092 elfcpp::Elf_types<64>::Elf_Addr address,
1095 if (this->skip_call_tls_get_addr_)
1097 if (r_type != elfcpp::R_X86_64_PLT32
1099 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1101 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1103 relinfo->location(relnum, rel.get_r_offset()).c_str());
1107 this->skip_call_tls_get_addr_ = false;
1112 // Pick the value to use for symbols defined in shared objects.
1113 Symbol_value<64> symval;
1114 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1116 symval.set_output_value(target->plt_section()->address()
1117 + gsym->plt_offset());
1121 const Sized_relobj<64, false>* object = relinfo->object;
1122 const elfcpp::Elf_Xword addend = rel.get_r_addend();
1126 case elfcpp::R_X86_64_NONE:
1127 case elfcpp::R_386_GNU_VTINHERIT:
1128 case elfcpp::R_386_GNU_VTENTRY:
1131 case elfcpp::R_X86_64_64:
1132 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1135 case elfcpp::R_X86_64_PC64:
1136 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1140 case elfcpp::R_X86_64_32:
1141 // FIXME: we need to verify that value + addend fits into 32 bits:
1142 // uint64_t x = value + addend;
1143 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1144 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1145 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1148 case elfcpp::R_X86_64_32S:
1149 // FIXME: we need to verify that value + addend fits into 32 bits:
1150 // int64_t x = value + addend; // note this quantity is signed!
1151 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1152 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1155 case elfcpp::R_X86_64_PC32:
1156 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1160 case elfcpp::R_X86_64_16:
1161 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1164 case elfcpp::R_X86_64_PC16:
1165 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1169 case elfcpp::R_X86_64_8:
1170 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1173 case elfcpp::R_X86_64_PC8:
1174 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1178 case elfcpp::R_X86_64_PLT32:
1179 gold_assert(gsym->has_plt_offset()
1180 || gsym->final_value_is_known());
1181 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1185 case elfcpp::R_X86_64_GOT32:
1186 // Local GOT offsets not yet supported.
1188 gold_assert(gsym->has_got_offset());
1189 Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend);
1192 case elfcpp::R_X86_64_GOTPC32:
1195 elfcpp::Elf_types<64>::Elf_Addr value;
1196 value = target->got_section(NULL, NULL)->address();
1197 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1201 case elfcpp::R_X86_64_GOT64:
1202 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1203 // Since we always add a PLT entry, this is equivalent.
1204 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
1205 // Local GOT offsets not yet supported.
1207 gold_assert(gsym->has_got_offset());
1208 Relocate_functions<64, false>::rela64(view, gsym->got_offset(), addend);
1211 case elfcpp::R_X86_64_GOTPC64:
1214 elfcpp::Elf_types<64>::Elf_Addr value;
1215 value = target->got_section(NULL, NULL)->address();
1216 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1220 case elfcpp::R_X86_64_GOTOFF64:
1222 elfcpp::Elf_types<64>::Elf_Addr value;
1223 value = (psymval->value(object, 0)
1224 - target->got_section(NULL, NULL)->address());
1225 Relocate_functions<64, false>::rela64(view, value, addend);
1229 case elfcpp::R_X86_64_GOTPCREL:
1231 // Local GOT offsets not yet supported.
1233 gold_assert(gsym->has_got_offset());
1234 elfcpp::Elf_types<64>::Elf_Addr value;
1235 value = (target->got_section(NULL, NULL)->address()
1236 + gsym->got_offset());
1237 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1241 case elfcpp::R_X86_64_GOTPCREL64:
1243 // Local GOT offsets not yet supported.
1245 gold_assert(gsym->has_got_offset());
1246 elfcpp::Elf_types<64>::Elf_Addr value;
1247 value = (target->got_section(NULL, NULL)->address()
1248 + gsym->got_offset());
1249 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1253 case elfcpp::R_X86_64_COPY:
1254 case elfcpp::R_X86_64_GLOB_DAT:
1255 case elfcpp::R_X86_64_JUMP_SLOT:
1256 case elfcpp::R_X86_64_RELATIVE:
1257 case elfcpp::R_X86_64_TPOFF64:
1258 case elfcpp::R_X86_64_TPOFF32:
1259 case elfcpp::R_X86_64_DTPMOD64:
1260 case elfcpp::R_X86_64_DTPOFF64:
1261 case elfcpp::R_X86_64_DTPOFF32:
1262 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
1263 case elfcpp::R_X86_64_TLSDESC:
1264 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
1265 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
1266 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1268 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1274 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
1275 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
1277 case elfcpp::R_386_TLS_IE:
1278 case elfcpp::R_386_TLS_GOTIE:
1279 case elfcpp::R_386_TLS_LE:
1280 case elfcpp::R_386_TLS_GD:
1281 case elfcpp::R_386_TLS_LDM:
1282 case elfcpp::R_386_TLS_LDO_64:
1283 case elfcpp::R_386_TLS_IE_64:
1284 case elfcpp::R_386_TLS_LE_64:
1285 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1286 address, view_size);
1289 view_size++; // this is to make view_size used
1292 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
1293 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
1294 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): implement me!
1296 case elfcpp::R_386_TLS_GD_64:
1297 case elfcpp::R_386_TLS_GD_PUSH:
1298 case elfcpp::R_386_TLS_GD_CALL:
1299 case elfcpp::R_386_TLS_GD_POP:
1300 case elfcpp::R_386_TLS_LDM_64:
1301 case elfcpp::R_386_TLS_LDM_PUSH:
1302 case elfcpp::R_386_TLS_LDM_CALL:
1303 case elfcpp::R_386_TLS_LDM_POP:
1306 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1308 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1310 // gold_exit(false);
1317 // Perform a TLS relocation.
1320 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>*, // relinfo,
1322 const elfcpp::Rela<64, false>& , // rel,
1323 unsigned int , // r_type,
1324 const Sized_symbol<64>* , // gsym,
1325 const Symbol_value<64>* , // psymval,
1326 unsigned char* , // view,
1327 elfcpp::Elf_types<64>::Elf_Addr,
1331 Output_segment* tls_segment = relinfo->layout->tls_segment();
1332 if (tls_segment == NULL)
1334 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1336 relinfo->location(relnum, rel.get_r_offset()).c_str());
1340 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1342 const bool is_final = (gsym == NULL
1343 ? !parameters->output_is_shared()
1344 : gsym->final_value_is_known());
1345 const unsigned int opt_r_type =
1346 Target_x86_64::optimize_tls_reloc(is_final, r_type);
1349 case elfcpp::R_X86_64_TLS_LE_64:
1350 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1351 Relocate_functions<64, false>::rel64(view, value);
1354 case elfcpp::R_X86_64_TLS_LE:
1355 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1356 Relocate_functions<64, false>::rel64(view, value);
1359 case elfcpp::R_X86_64_TLS_IE:
1360 case elfcpp::R_X86_64_TLS_GOTIE:
1361 case elfcpp::R_X86_64_TLS_IE_64:
1362 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1364 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1365 rel, r_type, value, view,
1369 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1371 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1373 // gold_exit(false);
1376 case elfcpp::R_X86_64_TLS_GD:
1377 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1379 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1380 rel, r_type, value, view,
1384 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1386 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1388 // gold_exit(false);
1391 case elfcpp::R_X86_64_TLS_LDM:
1392 case elfcpp::R_X86_64_TLS_LDO_64:
1393 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1395 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1397 // gold_exit(false);
1403 // Do a relocation in which we convert a TLS Initial-Exec to a
1407 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* , // relinfo,
1409 Output_segment* , // tls_segment,
1410 const elfcpp::Rela<64, false>& , // rel,
1411 unsigned int , // r_type,
1412 elfcpp::Elf_types<64>::Elf_Addr , // value,
1413 unsigned char* , // view,
1414 off_t) // view_size)
1417 // We have to actually change the instructions, which means that we
1418 // need to examine the opcodes to figure out which instruction we
1420 if (r_type == elfcpp::R_X86_64_TLS_IE)
1422 // movl %gs:XX,%eax ==> movl $YY,%eax
1423 // movl %gs:XX,%reg ==> movl $YY,%reg
1424 // addl %gs:XX,%reg ==> addl $YY,%reg
1425 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1426 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1428 unsigned char op1 = view[-1];
1431 // movl XX,%eax ==> movl $YY,%eax
1436 Target_x86_64::Relocate::check_range(relinfo, relnum, rel,
1439 unsigned char op2 = view[-2];
1442 // movl XX,%reg ==> movl $YY,%reg
1443 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1444 (op1 & 0xc7) == 0x05);
1446 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1448 else if (op2 == 0x03)
1450 // addl XX,%reg ==> addl $YY,%reg
1451 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1452 (op1 & 0xc7) == 0x05);
1454 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1457 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1462 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1463 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1464 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1465 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1466 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1468 unsigned char op1 = view[-1];
1469 unsigned char op2 = view[-2];
1470 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1471 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1474 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1476 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1478 else if (op2 == 0x2b)
1480 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1482 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1484 else if (op2 == 0x03)
1486 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1488 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1491 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1494 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1495 if (r_type == elfcpp::R_X86_64_TLS_IE || r_type == elfcpp::R_X86_64_TLS_GOTIE)
1498 Relocate_functions<64, false>::rel64(view, value);
1502 // Do a relocation in which we convert a TLS Global-Dynamic to a
1506 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* , // relinfo,
1508 Output_segment* , // tls_segment,
1509 const elfcpp::Rela<64, false>& , // rel,
1511 elfcpp::Elf_types<64>::Elf_Addr , // value,
1512 unsigned char* , // view,
1516 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1517 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1518 // leal foo(%reg),%eax; call ___tls_get_addr
1519 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1521 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1522 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1524 unsigned char op1 = view[-1];
1525 unsigned char op2 = view[-2];
1527 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1528 op2 == 0x8d || op2 == 0x04);
1529 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1536 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1537 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1539 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1540 ((op1 & 0xc7) == 0x05
1541 && op1 != (4 << 3)));
1542 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1546 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1547 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1548 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1550 // There is a trailing nop. Use the size byte subl.
1551 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1556 // Use the five byte subl.
1557 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1561 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1562 Relocate_functions<64, false>::rel64(view + roff, value);
1564 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1566 this->skip_call_tls_get_addr_ = true;
1570 // Check the range for a TLS relocation.
1573 Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo,
1575 const elfcpp::Rela<64, false>& rel,
1576 off_t view_size, off_t off)
1578 off_t offset = rel.get_r_offset() + off;
1579 if (offset < 0 || offset > view_size)
1581 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1583 relinfo->location(relnum, rel.get_r_offset()).c_str());
1588 // Check the validity of a TLS relocation. This is like assert.
1591 Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo,
1593 const elfcpp::Rela<64, false>& rel,
1599 _("%s: %s: TLS relocation against invalid instruction\n"),
1601 relinfo->location(relnum, rel.get_r_offset()).c_str());
1606 // Relocate section data.
1609 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1610 unsigned int sh_type,
1611 const unsigned char* prelocs,
1613 unsigned char* view,
1614 elfcpp::Elf_types<64>::Elf_Addr address,
1617 gold_assert(sh_type == elfcpp::SHT_RELA);
1619 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1620 Target_x86_64::Relocate>(
1630 // Return the value to use for a dynamic which requires special
1631 // treatment. This is how we support equality comparisons of function
1632 // pointers across shared library boundaries, as described in the
1633 // processor specific ABI supplement.
1636 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1638 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1639 return this->plt_section()->address() + gsym->plt_offset();
1642 // Return a string used to fill a code section with nops to take up
1643 // the specified length.
1646 Target_x86_64::do_code_fill(off_t length)
1650 // Build a jmpq instruction to skip over the bytes.
1651 unsigned char jmp[5];
1653 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1654 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1655 + std::string(length - 5, '\0'));
1658 // Nop sequences of various lengths.
1659 const char nop1[1] = { 0x90 }; // nop
1660 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1661 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1662 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1663 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1664 0x00 }; // leal 0(%esi,1),%esi
1665 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1667 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1669 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1670 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1671 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1672 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1674 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1675 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1677 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1678 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1680 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1681 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1682 0x00, 0x00, 0x00, 0x00 };
1683 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1684 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1685 0x27, 0x00, 0x00, 0x00,
1687 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1688 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1689 0xbc, 0x27, 0x00, 0x00,
1691 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1692 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1693 0x90, 0x90, 0x90, 0x90,
1696 const char* nops[16] = {
1698 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1699 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1702 return std::string(nops[length], length);
1705 // The selector for x86_64 object files.
1707 class Target_selector_x86_64 : public Target_selector
1710 Target_selector_x86_64()
1711 : Target_selector(elfcpp::EM_X86_64, 64, false)
1715 recognize(int machine, int osabi, int abiversion);
1718 Target_x86_64* target_;
1721 // Recognize an x86_64 object file when we already know that the machine
1722 // number is EM_X86_64.
1725 Target_selector_x86_64::recognize(int, int, int)
1727 if (this->target_ == NULL)
1728 this->target_ = new Target_x86_64();
1729 return this->target_;
1732 Target_selector_x86_64 target_selector_x86_64;
1734 } // End anonymous namespace.