1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
37 #include "parameters.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
54 class Output_data_plt_x86_64;
56 // The x86_64 target class.
58 // http://www.x86-64.org/documentation/abi.pdf
59 // TLS info comes from
60 // http://people.redhat.com/drepper/tls.pdf
61 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
63 class Target_x86_64 : public Sized_target<64, false>
66 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67 // uses only Elf64_Rela relocation entries with explicit addends."
68 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
71 : Sized_target<64, false>(&x86_64_info),
72 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
73 copy_relocs_(NULL), dynbss_(NULL), got_mod_index_offset_(-1U)
76 // Scan the relocations to look for symbol adjustments.
78 scan_relocs(const General_options& options,
81 Sized_relobj<64, false>* object,
82 unsigned int data_shndx,
84 const unsigned char* prelocs,
86 Output_section* output_section,
87 bool needs_special_offset_handling,
88 size_t local_symbol_count,
89 const unsigned char* plocal_symbols);
91 // Finalize the sections.
93 do_finalize_sections(Layout*);
95 // Return the value to use for a dynamic which requires special
98 do_dynsym_value(const Symbol*) const;
100 // Relocate a section.
102 relocate_section(const Relocate_info<64, false>*,
103 unsigned int sh_type,
104 const unsigned char* prelocs,
106 Output_section* output_section,
107 bool needs_special_offset_handling,
109 elfcpp::Elf_types<64>::Elf_Addr view_address,
112 // Return a string used to fill a code section with nops.
114 do_code_fill(off_t length);
116 // Return whether SYM is defined by the ABI.
118 do_is_defined_by_abi(Symbol* sym) const
119 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
121 // Return the size of the GOT section.
125 gold_assert(this->got_ != NULL);
126 return this->got_->data_size();
130 // The class which scans relocations.
134 local(const General_options& options, Symbol_table* symtab,
135 Layout* layout, Target_x86_64* target,
136 Sized_relobj<64, false>* object,
137 unsigned int data_shndx,
138 Output_section* output_section,
139 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
140 const elfcpp::Sym<64, false>& lsym);
143 global(const General_options& options, Symbol_table* symtab,
144 Layout* layout, Target_x86_64* target,
145 Sized_relobj<64, false>* object,
146 unsigned int data_shndx,
147 Output_section* output_section,
148 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
152 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
155 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
159 // The class which implements relocation.
164 : skip_call_tls_get_addr_(false)
169 if (this->skip_call_tls_get_addr_)
171 // FIXME: This needs to specify the location somehow.
172 gold_error(_("missing expected TLS relocation"));
176 // Do a relocation. Return false if the caller should not issue
177 // any warnings about this relocation.
179 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
180 const elfcpp::Rela<64, false>&,
181 unsigned int r_type, const Sized_symbol<64>*,
182 const Symbol_value<64>*,
183 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
187 // Do a TLS relocation.
189 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
190 size_t relnum, const elfcpp::Rela<64, false>&,
191 unsigned int r_type, const Sized_symbol<64>*,
192 const Symbol_value<64>*,
193 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
195 // Do a TLS General-Dynamic to Local-Exec transition.
197 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
198 Output_segment* tls_segment,
199 const elfcpp::Rela<64, false>&, unsigned int r_type,
200 elfcpp::Elf_types<64>::Elf_Addr value,
204 // Do a TLS General-Dynamic to Local-Exec transition.
206 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
207 Output_segment* tls_segment,
208 const elfcpp::Rela<64, false>&, unsigned int r_type,
209 elfcpp::Elf_types<64>::Elf_Addr value,
213 // Do a TLS Local-Dynamic to Local-Exec transition.
215 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
216 Output_segment* tls_segment,
217 const elfcpp::Rela<64, false>&, unsigned int r_type,
218 elfcpp::Elf_types<64>::Elf_Addr value,
222 // Do a TLS Initial-Exec to Local-Exec transition.
224 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
225 Output_segment* tls_segment,
226 const elfcpp::Rela<64, false>&, unsigned int r_type,
227 elfcpp::Elf_types<64>::Elf_Addr value,
231 // This is set if we should skip the next reloc, which should be a
232 // PLT32 reloc against ___tls_get_addr.
233 bool skip_call_tls_get_addr_;
236 // Adjust TLS relocation type based on the options and whether this
237 // is a local symbol.
238 static tls::Tls_optimization
239 optimize_tls_reloc(bool is_final, int r_type);
241 // Get the GOT section, creating it if necessary.
242 Output_data_got<64, false>*
243 got_section(Symbol_table*, Layout*);
245 // Get the GOT PLT section.
247 got_plt_section() const
249 gold_assert(this->got_plt_ != NULL);
250 return this->got_plt_;
253 // Create a PLT entry for a global symbol.
255 make_plt_entry(Symbol_table*, Layout*, Symbol*);
257 // Create a GOT entry for the TLS module index.
259 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
260 Sized_relobj<64, false>* object);
262 // Get the PLT section.
263 Output_data_plt_x86_64*
266 gold_assert(this->plt_ != NULL);
270 // Get the dynamic reloc section, creating it if necessary.
272 rela_dyn_section(Layout*);
274 // Return true if the symbol may need a COPY relocation.
275 // References from an executable object to non-function symbols
276 // defined in a dynamic object may need a COPY relocation.
278 may_need_copy_reloc(Symbol* gsym)
280 return (!parameters->output_is_shared()
281 && gsym->is_from_dynobj()
282 && gsym->type() != elfcpp::STT_FUNC);
285 // Copy a relocation against a global symbol.
287 copy_reloc(const General_options*, Symbol_table*, Layout*,
288 Sized_relobj<64, false>*, unsigned int,
289 Output_section*, Symbol*, const elfcpp::Rela<64, false>&);
291 // Information about this specific target which we pass to the
292 // general Target structure.
293 static const Target::Target_info x86_64_info;
296 Output_data_got<64, false>* got_;
298 Output_data_plt_x86_64* plt_;
299 // The GOT PLT section.
300 Output_data_space* got_plt_;
301 // The dynamic reloc section.
302 Reloc_section* rela_dyn_;
303 // Relocs saved to avoid a COPY reloc.
304 Copy_relocs<64, false>* copy_relocs_;
305 // Space for variables copied with a COPY reloc.
306 Output_data_space* dynbss_;
307 // Offset of the GOT entry for the TLS module index;
308 unsigned int got_mod_index_offset_;
311 const Target::Target_info Target_x86_64::x86_64_info =
314 false, // is_big_endian
315 elfcpp::EM_X86_64, // machine_code
316 false, // has_make_symbol
317 false, // has_resolve
318 true, // has_code_fill
319 true, // is_default_stack_executable
320 "/lib/ld64.so.1", // program interpreter
321 0x400000, // default_text_segment_address
322 0x1000, // abi_pagesize
323 0x1000 // common_pagesize
326 // Get the GOT section, creating it if necessary.
328 Output_data_got<64, false>*
329 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
331 if (this->got_ == NULL)
333 gold_assert(symtab != NULL && layout != NULL);
335 this->got_ = new Output_data_got<64, false>();
337 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
338 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
341 // The old GNU linker creates a .got.plt section. We just
342 // create another set of data in the .got section. Note that we
343 // always create a PLT if we create a GOT, although the PLT
345 this->got_plt_ = new Output_data_space(8);
346 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
347 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
350 // The first three entries are reserved.
351 this->got_plt_->set_current_data_size(3 * 8);
353 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
354 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
356 0, 0, elfcpp::STT_OBJECT,
358 elfcpp::STV_HIDDEN, 0,
365 // Get the dynamic reloc section, creating it if necessary.
367 Target_x86_64::Reloc_section*
368 Target_x86_64::rela_dyn_section(Layout* layout)
370 if (this->rela_dyn_ == NULL)
372 gold_assert(layout != NULL);
373 this->rela_dyn_ = new Reloc_section();
374 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
375 elfcpp::SHF_ALLOC, this->rela_dyn_);
377 return this->rela_dyn_;
380 // A class to handle the PLT data.
382 class Output_data_plt_x86_64 : public Output_section_data
385 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
387 Output_data_plt_x86_64(Layout*, Output_data_space*);
389 // Add an entry to the PLT.
391 add_entry(Symbol* gsym);
393 // Return the .rel.plt section data.
396 { return this->rel_; }
400 do_adjust_output_section(Output_section* os);
403 // The size of an entry in the PLT.
404 static const int plt_entry_size = 16;
406 // The first entry in the PLT.
407 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
408 // procedure linkage table for both programs and shared objects."
409 static unsigned char first_plt_entry[plt_entry_size];
411 // Other entries in the PLT for an executable.
412 static unsigned char plt_entry[plt_entry_size];
414 // Set the final size.
416 set_final_data_size()
417 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
419 // Write out the PLT data.
421 do_write(Output_file*);
423 // The reloc section.
425 // The .got.plt section.
426 Output_data_space* got_plt_;
427 // The number of PLT entries.
431 // Create the PLT section. The ordinary .got section is an argument,
432 // since we need to refer to the start. We also create our own .got
433 // section just for PLT entries.
435 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
436 Output_data_space* got_plt)
437 : Output_section_data(8), got_plt_(got_plt), count_(0)
439 this->rel_ = new Reloc_section();
440 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
441 elfcpp::SHF_ALLOC, this->rel_);
445 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
447 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
448 // linker, and so do we.
452 // Add an entry to the PLT.
455 Output_data_plt_x86_64::add_entry(Symbol* gsym)
457 gold_assert(!gsym->has_plt_offset());
459 // Note that when setting the PLT offset we skip the initial
460 // reserved PLT entry.
461 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
465 off_t got_offset = this->got_plt_->current_data_size();
467 // Every PLT entry needs a GOT entry which points back to the PLT
468 // entry (this will be changed by the dynamic linker, normally
469 // lazily when the function is called).
470 this->got_plt_->set_current_data_size(got_offset + 8);
472 // Every PLT entry needs a reloc.
473 gsym->set_needs_dynsym_entry();
474 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
477 // Note that we don't need to save the symbol. The contents of the
478 // PLT are independent of which symbols are used. The symbols only
479 // appear in the relocations.
482 // The first entry in the PLT for an executable.
484 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
486 // From AMD64 ABI Draft 0.98, page 76
487 0xff, 0x35, // pushq contents of memory address
488 0, 0, 0, 0, // replaced with address of .got + 8
489 0xff, 0x25, // jmp indirect
490 0, 0, 0, 0, // replaced with address of .got + 16
491 0x90, 0x90, 0x90, 0x90 // noop (x4)
494 // Subsequent entries in the PLT for an executable.
496 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
498 // From AMD64 ABI Draft 0.98, page 76
499 0xff, 0x25, // jmpq indirect
500 0, 0, 0, 0, // replaced with address of symbol in .got
501 0x68, // pushq immediate
502 0, 0, 0, 0, // replaced with offset into relocation table
503 0xe9, // jmpq relative
504 0, 0, 0, 0 // replaced with offset to start of .plt
507 // Write out the PLT. This uses the hand-coded instructions above,
508 // and adjusts them as needed. This is specified by the AMD64 ABI.
511 Output_data_plt_x86_64::do_write(Output_file* of)
513 const off_t offset = this->offset();
514 const off_t oview_size = this->data_size();
515 unsigned char* const oview = of->get_output_view(offset, oview_size);
517 const off_t got_file_offset = this->got_plt_->offset();
518 const off_t got_size = this->got_plt_->data_size();
519 unsigned char* const got_view = of->get_output_view(got_file_offset,
522 unsigned char* pov = oview;
524 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
525 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
527 memcpy(pov, first_plt_entry, plt_entry_size);
528 // We do a jmp relative to the PC at the end of this instruction.
529 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
530 - (plt_address + 6));
531 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
532 - (plt_address + 12));
533 pov += plt_entry_size;
535 unsigned char* got_pov = got_view;
537 memset(got_pov, 0, 24);
540 unsigned int plt_offset = plt_entry_size;
541 unsigned int got_offset = 24;
542 const unsigned int count = this->count_;
543 for (unsigned int plt_index = 0;
546 pov += plt_entry_size,
548 plt_offset += plt_entry_size,
551 // Set and adjust the PLT entry itself.
552 memcpy(pov, plt_entry, plt_entry_size);
553 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
554 (got_address + got_offset
555 - (plt_address + plt_offset
558 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
559 elfcpp::Swap<32, false>::writeval(pov + 12,
560 - (plt_offset + plt_entry_size));
562 // Set the entry in the GOT.
563 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
566 gold_assert(pov - oview == oview_size);
567 gold_assert(got_pov - got_view == got_size);
569 of->write_output_view(offset, oview_size, oview);
570 of->write_output_view(got_file_offset, got_size, got_view);
573 // Create a PLT entry for a global symbol.
576 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
579 if (gsym->has_plt_offset())
582 if (this->plt_ == NULL)
584 // Create the GOT sections first.
585 this->got_section(symtab, layout);
587 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
588 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
590 | elfcpp::SHF_EXECINSTR),
594 this->plt_->add_entry(gsym);
597 // Create a GOT entry for the TLS module index.
600 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
601 Sized_relobj<64, false>* object)
603 if (this->got_mod_index_offset_ == -1U)
605 gold_assert(symtab != NULL && layout != NULL && object != NULL);
606 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
607 Output_data_got<64, false>* got = this->got_section(symtab, layout);
608 unsigned int got_offset = got->add_constant(0);
609 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
611 got->add_constant(0);
612 this->got_mod_index_offset_ = got_offset;
614 return this->got_mod_index_offset_;
617 // Handle a relocation against a non-function symbol defined in a
618 // dynamic object. The traditional way to handle this is to generate
619 // a COPY relocation to copy the variable at runtime from the shared
620 // object into the executable's data segment. However, this is
621 // undesirable in general, as if the size of the object changes in the
622 // dynamic object, the executable will no longer work correctly. If
623 // this relocation is in a writable section, then we can create a
624 // dynamic reloc and the dynamic linker will resolve it to the correct
625 // address at runtime. However, we do not want do that if the
626 // relocation is in a read-only section, as it would prevent the
627 // readonly segment from being shared. And if we have to eventually
628 // generate a COPY reloc, then any dynamic relocations will be
629 // useless. So this means that if this is a writable section, we need
630 // to save the relocation until we see whether we have to create a
631 // COPY relocation for this symbol for any other relocation.
634 Target_x86_64::copy_reloc(const General_options* options,
635 Symbol_table* symtab,
637 Sized_relobj<64, false>* object,
638 unsigned int data_shndx,
639 Output_section* output_section,
641 const elfcpp::Rela<64, false>& rela)
643 Sized_symbol<64>* ssym;
644 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
647 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
650 // So far we do not need a COPY reloc. Save this relocation.
651 // If it turns out that we never need a COPY reloc for this
652 // symbol, then we will emit the relocation.
653 if (this->copy_relocs_ == NULL)
654 this->copy_relocs_ = new Copy_relocs<64, false>();
655 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
659 // Allocate space for this symbol in the .bss section.
661 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
663 // There is no defined way to determine the required alignment
664 // of the symbol. We pick the alignment based on the size. We
665 // set an arbitrary maximum of 256.
667 for (align = 1; align < 512; align <<= 1)
668 if ((symsize & align) != 0)
671 if (this->dynbss_ == NULL)
673 this->dynbss_ = new Output_data_space(align);
674 layout->add_output_section_data(".bss",
677 | elfcpp::SHF_WRITE),
681 Output_data_space* dynbss = this->dynbss_;
683 if (align > dynbss->addralign())
684 dynbss->set_space_alignment(align);
686 off_t dynbss_size = dynbss->current_data_size();
687 dynbss_size = align_address(dynbss_size, align);
688 off_t offset = dynbss_size;
689 dynbss->set_current_data_size(dynbss_size + symsize);
691 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
693 // Add the COPY reloc.
694 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
695 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
700 // Optimize the TLS relocation type based on what we know about the
701 // symbol. IS_FINAL is true if the final address of this symbol is
702 // known at link time.
704 tls::Tls_optimization
705 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
707 // If we are generating a shared library, then we can't do anything
709 if (parameters->output_is_shared())
710 return tls::TLSOPT_NONE;
714 case elfcpp::R_X86_64_TLSGD:
715 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
716 case elfcpp::R_X86_64_TLSDESC_CALL:
717 // These are General-Dynamic which permits fully general TLS
718 // access. Since we know that we are generating an executable,
719 // we can convert this to Initial-Exec. If we also know that
720 // this is a local symbol, we can further switch to Local-Exec.
722 return tls::TLSOPT_TO_LE;
723 return tls::TLSOPT_TO_IE;
725 case elfcpp::R_X86_64_TLSLD:
726 // This is Local-Dynamic, which refers to a local symbol in the
727 // dynamic TLS block. Since we know that we generating an
728 // executable, we can switch to Local-Exec.
729 return tls::TLSOPT_TO_LE;
731 case elfcpp::R_X86_64_DTPOFF32:
732 case elfcpp::R_X86_64_DTPOFF64:
733 // Another Local-Dynamic reloc.
734 return tls::TLSOPT_TO_LE;
736 case elfcpp::R_X86_64_GOTTPOFF:
737 // These are Initial-Exec relocs which get the thread offset
738 // from the GOT. If we know that we are linking against the
739 // local symbol, we can switch to Local-Exec, which links the
740 // thread offset into the instruction.
742 return tls::TLSOPT_TO_LE;
743 return tls::TLSOPT_NONE;
745 case elfcpp::R_X86_64_TPOFF32:
746 // When we already have Local-Exec, there is nothing further we
748 return tls::TLSOPT_NONE;
755 // Report an unsupported relocation against a local symbol.
758 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
761 gold_error(_("%s: unsupported reloc %u against local symbol"),
762 object->name().c_str(), r_type);
765 // Scan a relocation for a local symbol.
768 Target_x86_64::Scan::local(const General_options&,
769 Symbol_table* symtab,
771 Target_x86_64* target,
772 Sized_relobj<64, false>* object,
773 unsigned int data_shndx,
774 Output_section* output_section,
775 const elfcpp::Rela<64, false>& reloc,
777 const elfcpp::Sym<64, false>& lsym)
781 case elfcpp::R_X86_64_NONE:
782 case elfcpp::R_386_GNU_VTINHERIT:
783 case elfcpp::R_386_GNU_VTENTRY:
786 case elfcpp::R_X86_64_64:
787 // If building a shared library (or a position-independent
788 // executable), we need to create a dynamic relocation for
789 // this location. The relocation applied at link time will
790 // apply the link-time value, so we flag the location with
791 // an R_386_RELATIVE relocation so the dynamic loader can
792 // relocate it easily.
793 if (parameters->output_is_position_independent())
795 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
796 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
797 rela_dyn->add_local_relative(object, r_sym,
798 elfcpp::R_X86_64_RELATIVE,
799 output_section, data_shndx,
800 reloc.get_r_offset(),
801 reloc.get_r_addend());
805 case elfcpp::R_X86_64_32:
806 case elfcpp::R_X86_64_32S:
807 case elfcpp::R_X86_64_16:
808 case elfcpp::R_X86_64_8:
809 // If building a shared library (or a position-independent
810 // executable), we need to create a dynamic relocation for
811 // this location. The relocation applied at link time will
812 // apply the link-time value, so we flag the location with
813 // an R_386_RELATIVE relocation so the dynamic loader can
814 // relocate it easily.
815 if (parameters->output_is_position_independent())
817 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
818 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
819 rela_dyn->add_local(object, r_sym, r_type, output_section,
820 data_shndx, reloc.get_r_offset(),
821 reloc.get_r_addend());
825 case elfcpp::R_X86_64_PC64:
826 case elfcpp::R_X86_64_PC32:
827 case elfcpp::R_X86_64_PC16:
828 case elfcpp::R_X86_64_PC8:
831 case elfcpp::R_X86_64_PLT32:
832 // Since we know this is a local symbol, we can handle this as a
836 case elfcpp::R_X86_64_GOTPC32:
837 case elfcpp::R_X86_64_GOTOFF64:
838 case elfcpp::R_X86_64_GOTPC64:
839 case elfcpp::R_X86_64_PLTOFF64:
840 // We need a GOT section.
841 target->got_section(symtab, layout);
842 // For PLTOFF64, we'd normally want a PLT section, but since we
843 // know this is a local symbol, no PLT is needed.
846 case elfcpp::R_X86_64_GOT64:
847 case elfcpp::R_X86_64_GOT32:
848 case elfcpp::R_X86_64_GOTPCREL64:
849 case elfcpp::R_X86_64_GOTPCREL:
850 case elfcpp::R_X86_64_GOTPLT64:
852 // The symbol requires a GOT entry.
853 Output_data_got<64, false>* got = target->got_section(symtab, layout);
854 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
855 if (got->add_local(object, r_sym))
857 // If we are generating a shared object, we need to add a
858 // dynamic relocation for this symbol's GOT entry.
859 if (parameters->output_is_position_independent())
861 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
862 // R_X86_64_RELATIVE assumes a 64-bit relocation.
863 if (r_type != elfcpp::R_X86_64_GOT32)
864 rela_dyn->add_local_relative(object, r_sym,
865 elfcpp::R_X86_64_RELATIVE, got,
866 object->local_got_offset(r_sym),
869 rela_dyn->add_local(object, r_sym, r_type,
870 got, object->local_got_offset(r_sym), 0);
873 // For GOTPLT64, we'd normally want a PLT section, but since
874 // we know this is a local symbol, no PLT is needed.
878 case elfcpp::R_X86_64_COPY:
879 case elfcpp::R_X86_64_GLOB_DAT:
880 case elfcpp::R_X86_64_JUMP_SLOT:
881 case elfcpp::R_X86_64_RELATIVE:
882 // These are outstanding tls relocs, which are unexpected when linking
883 case elfcpp::R_X86_64_TPOFF64:
884 case elfcpp::R_X86_64_DTPMOD64:
885 case elfcpp::R_X86_64_TLSDESC:
886 gold_error(_("%s: unexpected reloc %u in object file"),
887 object->name().c_str(), r_type);
890 // These are initial tls relocs, which are expected when linking
891 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
892 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
893 case elfcpp::R_X86_64_TLSDESC_CALL:
894 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
895 case elfcpp::R_X86_64_DTPOFF32:
896 case elfcpp::R_X86_64_DTPOFF64:
897 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
898 case elfcpp::R_X86_64_TPOFF32: // Local-exec
900 bool output_is_shared = parameters->output_is_shared();
901 const tls::Tls_optimization optimized_type
902 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
905 case elfcpp::R_X86_64_TLSGD: // General-dynamic
906 if (optimized_type == tls::TLSOPT_NONE)
908 // Create a pair of GOT entries for the module index and
909 // dtv-relative offset.
910 Output_data_got<64, false>* got
911 = target->got_section(symtab, layout);
912 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
913 got->add_local_tls_with_rela(object, r_sym,
914 lsym.get_st_shndx(), true,
915 target->rela_dyn_section(layout),
916 elfcpp::R_X86_64_DTPMOD64);
918 else if (optimized_type != tls::TLSOPT_TO_LE)
919 unsupported_reloc_local(object, r_type);
922 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
923 case elfcpp::R_X86_64_TLSDESC_CALL:
924 // FIXME: If not relaxing to LE, we need to generate
925 // a GOT entry with a R_x86_64_TLSDESC reloc.
926 if (optimized_type != tls::TLSOPT_TO_LE)
927 unsupported_reloc_local(object, r_type);
930 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
931 if (optimized_type == tls::TLSOPT_NONE)
933 // Create a GOT entry for the module index.
934 target->got_mod_index_entry(symtab, layout, object);
936 else if (optimized_type != tls::TLSOPT_TO_LE)
937 unsupported_reloc_local(object, r_type);
940 case elfcpp::R_X86_64_DTPOFF32:
941 case elfcpp::R_X86_64_DTPOFF64:
944 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
945 if (optimized_type == tls::TLSOPT_NONE)
947 // Create a GOT entry for the tp-relative offset.
948 Output_data_got<64, false>* got
949 = target->got_section(symtab, layout);
950 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
951 got->add_local_with_rela(object, r_sym,
952 target->rela_dyn_section(layout),
953 elfcpp::R_X86_64_TPOFF64);
955 else if (optimized_type != tls::TLSOPT_TO_LE)
956 unsupported_reloc_local(object, r_type);
959 case elfcpp::R_X86_64_TPOFF32: // Local-exec
960 if (output_is_shared)
961 unsupported_reloc_local(object, r_type);
970 case elfcpp::R_X86_64_SIZE32:
971 case elfcpp::R_X86_64_SIZE64:
973 gold_error(_("%s: unsupported reloc %u against local symbol"),
974 object->name().c_str(), r_type);
980 // Report an unsupported relocation against a global symbol.
983 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
987 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
988 object->name().c_str(), r_type, gsym->demangled_name().c_str());
991 // Scan a relocation for a global symbol.
994 Target_x86_64::Scan::global(const General_options& options,
995 Symbol_table* symtab,
997 Target_x86_64* target,
998 Sized_relobj<64, false>* object,
999 unsigned int data_shndx,
1000 Output_section* output_section,
1001 const elfcpp::Rela<64, false>& reloc,
1002 unsigned int r_type,
1007 case elfcpp::R_X86_64_NONE:
1008 case elfcpp::R_386_GNU_VTINHERIT:
1009 case elfcpp::R_386_GNU_VTENTRY:
1012 case elfcpp::R_X86_64_64:
1013 case elfcpp::R_X86_64_32:
1014 case elfcpp::R_X86_64_32S:
1015 case elfcpp::R_X86_64_16:
1016 case elfcpp::R_X86_64_8:
1018 // Make a PLT entry if necessary.
1019 if (gsym->needs_plt_entry())
1021 target->make_plt_entry(symtab, layout, gsym);
1022 // Since this is not a PC-relative relocation, we may be
1023 // taking the address of a function. In that case we need to
1024 // set the entry in the dynamic symbol table to the address of
1026 if (gsym->is_from_dynobj())
1027 gsym->set_needs_dynsym_value();
1029 // Make a dynamic relocation if necessary.
1030 if (gsym->needs_dynamic_reloc(true, false))
1032 if (target->may_need_copy_reloc(gsym))
1034 target->copy_reloc(&options, symtab, layout, object,
1035 data_shndx, output_section, gsym, reloc);
1037 else if (r_type == elfcpp::R_X86_64_64
1038 && gsym->can_use_relative_reloc(false))
1040 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1041 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1042 output_section, object,
1043 data_shndx, reloc.get_r_offset(),
1044 reloc.get_r_addend());
1048 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1049 rela_dyn->add_global(gsym, r_type, output_section, object,
1050 data_shndx, reloc.get_r_offset(),
1051 reloc.get_r_addend());
1057 case elfcpp::R_X86_64_PC64:
1058 case elfcpp::R_X86_64_PC32:
1059 case elfcpp::R_X86_64_PC16:
1060 case elfcpp::R_X86_64_PC8:
1062 // Make a PLT entry if necessary.
1063 if (gsym->needs_plt_entry())
1064 target->make_plt_entry(symtab, layout, gsym);
1065 // Make a dynamic relocation if necessary.
1066 bool is_function_call = (gsym->type() == elfcpp::STT_FUNC);
1067 if (gsym->needs_dynamic_reloc(false, is_function_call))
1069 if (target->may_need_copy_reloc(gsym))
1071 target->copy_reloc(&options, symtab, layout, object,
1072 data_shndx, output_section, gsym, reloc);
1076 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1077 rela_dyn->add_global(gsym, r_type, output_section, object,
1078 data_shndx, reloc.get_r_offset(),
1079 reloc.get_r_addend());
1085 case elfcpp::R_X86_64_GOT64:
1086 case elfcpp::R_X86_64_GOT32:
1087 case elfcpp::R_X86_64_GOTPCREL64:
1088 case elfcpp::R_X86_64_GOTPCREL:
1089 case elfcpp::R_X86_64_GOTPLT64:
1091 // The symbol requires a GOT entry.
1092 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1093 if (gsym->final_value_is_known())
1094 got->add_global(gsym);
1097 // If this symbol is not fully resolved, we need to add a
1098 // dynamic relocation for it.
1099 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1100 if (gsym->is_from_dynobj() || gsym->is_preemptible())
1101 got->add_global_with_rela(gsym, rela_dyn,
1102 elfcpp::R_X86_64_GLOB_DAT);
1105 if (got->add_global(gsym))
1106 rela_dyn->add_global_relative(gsym,
1107 elfcpp::R_X86_64_RELATIVE,
1108 got, gsym->got_offset(), 0);
1111 // For GOTPLT64, we also need a PLT entry (but only if the
1112 // symbol is not fully resolved).
1113 if (r_type == elfcpp::R_X86_64_GOTPLT64
1114 && !gsym->final_value_is_known())
1115 target->make_plt_entry(symtab, layout, gsym);
1119 case elfcpp::R_X86_64_PLT32:
1120 // If the symbol is fully resolved, this is just a PC32 reloc.
1121 // Otherwise we need a PLT entry.
1122 if (gsym->final_value_is_known())
1124 // If building a shared library, we can also skip the PLT entry
1125 // if the symbol is defined in the output file and is protected
1127 if (gsym->is_defined()
1128 && !gsym->is_from_dynobj()
1129 && !gsym->is_preemptible())
1131 target->make_plt_entry(symtab, layout, gsym);
1134 case elfcpp::R_X86_64_GOTPC32:
1135 case elfcpp::R_X86_64_GOTOFF64:
1136 case elfcpp::R_X86_64_GOTPC64:
1137 case elfcpp::R_X86_64_PLTOFF64:
1138 // We need a GOT section.
1139 target->got_section(symtab, layout);
1140 // For PLTOFF64, we also need a PLT entry (but only if the
1141 // symbol is not fully resolved).
1142 if (r_type == elfcpp::R_X86_64_PLTOFF64
1143 && !gsym->final_value_is_known())
1144 target->make_plt_entry(symtab, layout, gsym);
1147 case elfcpp::R_X86_64_COPY:
1148 case elfcpp::R_X86_64_GLOB_DAT:
1149 case elfcpp::R_X86_64_JUMP_SLOT:
1150 case elfcpp::R_X86_64_RELATIVE:
1151 // These are outstanding tls relocs, which are unexpected when linking
1152 case elfcpp::R_X86_64_TPOFF64:
1153 case elfcpp::R_X86_64_DTPMOD64:
1154 case elfcpp::R_X86_64_TLSDESC:
1155 gold_error(_("%s: unexpected reloc %u in object file"),
1156 object->name().c_str(), r_type);
1159 // These are initial tls relocs, which are expected for global()
1160 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1161 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1162 case elfcpp::R_X86_64_TLSDESC_CALL:
1163 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1164 case elfcpp::R_X86_64_DTPOFF32:
1165 case elfcpp::R_X86_64_DTPOFF64:
1166 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1167 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1169 const bool is_final = gsym->final_value_is_known();
1170 const tls::Tls_optimization optimized_type
1171 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1174 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1175 if (optimized_type == tls::TLSOPT_NONE)
1177 // Create a pair of GOT entries for the module index and
1178 // dtv-relative offset.
1179 Output_data_got<64, false>* got
1180 = target->got_section(symtab, layout);
1181 got->add_global_tls_with_rela(gsym,
1182 target->rela_dyn_section(layout),
1183 elfcpp::R_X86_64_DTPMOD64,
1184 elfcpp::R_X86_64_DTPOFF64);
1186 else if (optimized_type == tls::TLSOPT_TO_IE)
1188 // Create a GOT entry for the tp-relative offset.
1189 Output_data_got<64, false>* got
1190 = target->got_section(symtab, layout);
1191 got->add_global_with_rela(gsym,
1192 target->rela_dyn_section(layout),
1193 elfcpp::R_X86_64_TPOFF64);
1195 else if (optimized_type != tls::TLSOPT_TO_LE)
1196 unsupported_reloc_global(object, r_type, gsym);
1199 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1200 case elfcpp::R_X86_64_TLSDESC_CALL:
1201 // FIXME: If not relaxing to LE, we need to generate
1202 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1203 if (optimized_type != tls::TLSOPT_TO_LE)
1204 unsupported_reloc_global(object, r_type, gsym);
1207 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1208 if (optimized_type == tls::TLSOPT_NONE)
1210 // Create a GOT entry for the module index.
1211 target->got_mod_index_entry(symtab, layout, object);
1213 else if (optimized_type != tls::TLSOPT_TO_LE)
1214 unsupported_reloc_global(object, r_type, gsym);
1217 case elfcpp::R_X86_64_DTPOFF32:
1218 case elfcpp::R_X86_64_DTPOFF64:
1221 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1222 if (optimized_type == tls::TLSOPT_NONE)
1224 // Create a GOT entry for the tp-relative offset.
1225 Output_data_got<64, false>* got
1226 = target->got_section(symtab, layout);
1227 got->add_global_with_rela(gsym,
1228 target->rela_dyn_section(layout),
1229 elfcpp::R_X86_64_TPOFF64);
1231 else if (optimized_type != tls::TLSOPT_TO_LE)
1232 unsupported_reloc_global(object, r_type, gsym);
1235 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1236 if (parameters->output_is_shared())
1237 unsupported_reloc_local(object, r_type);
1246 case elfcpp::R_X86_64_SIZE32:
1247 case elfcpp::R_X86_64_SIZE64:
1249 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1250 object->name().c_str(), r_type,
1251 gsym->demangled_name().c_str());
1256 // Scan relocations for a section.
1259 Target_x86_64::scan_relocs(const General_options& options,
1260 Symbol_table* symtab,
1262 Sized_relobj<64, false>* object,
1263 unsigned int data_shndx,
1264 unsigned int sh_type,
1265 const unsigned char* prelocs,
1267 Output_section* output_section,
1268 bool needs_special_offset_handling,
1269 size_t local_symbol_count,
1270 const unsigned char* plocal_symbols)
1272 if (sh_type == elfcpp::SHT_REL)
1274 gold_error(_("%s: unsupported REL reloc section"),
1275 object->name().c_str());
1279 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1280 Target_x86_64::Scan>(
1290 needs_special_offset_handling,
1295 // Finalize the sections.
1298 Target_x86_64::do_finalize_sections(Layout* layout)
1300 // Fill in some more dynamic tags.
1301 Output_data_dynamic* const odyn = layout->dynamic_data();
1304 if (this->got_plt_ != NULL)
1305 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1307 if (this->plt_ != NULL)
1309 const Output_data* od = this->plt_->rel_plt();
1310 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1311 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1312 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1315 if (this->rela_dyn_ != NULL)
1317 const Output_data* od = this->rela_dyn_;
1318 odyn->add_section_address(elfcpp::DT_RELA, od);
1319 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1320 odyn->add_constant(elfcpp::DT_RELAENT,
1321 elfcpp::Elf_sizes<64>::rela_size);
1324 if (!parameters->output_is_shared())
1326 // The value of the DT_DEBUG tag is filled in by the dynamic
1327 // linker at run time, and used by the debugger.
1328 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1332 // Emit any relocs we saved in an attempt to avoid generating COPY
1334 if (this->copy_relocs_ == NULL)
1336 if (this->copy_relocs_->any_to_emit())
1338 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1339 this->copy_relocs_->emit(rela_dyn);
1341 delete this->copy_relocs_;
1342 this->copy_relocs_ = NULL;
1345 // Perform a relocation.
1348 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1349 Target_x86_64* target,
1351 const elfcpp::Rela<64, false>& rela,
1352 unsigned int r_type,
1353 const Sized_symbol<64>* gsym,
1354 const Symbol_value<64>* psymval,
1355 unsigned char* view,
1356 elfcpp::Elf_types<64>::Elf_Addr address,
1359 if (this->skip_call_tls_get_addr_)
1361 if (r_type != elfcpp::R_X86_64_PLT32
1363 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1365 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1366 _("missing expected TLS relocation"));
1370 this->skip_call_tls_get_addr_ = false;
1375 // Pick the value to use for symbols defined in shared objects.
1376 Symbol_value<64> symval;
1378 && (gsym->is_from_dynobj()
1379 || (parameters->output_is_shared()
1380 && gsym->is_preemptible()))
1381 && gsym->has_plt_offset())
1383 symval.set_output_value(target->plt_section()->address()
1384 + gsym->plt_offset());
1388 const Sized_relobj<64, false>* object = relinfo->object;
1389 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1391 // Get the GOT offset if needed.
1392 // The GOT pointer points to the end of the GOT section.
1393 // We need to subtract the size of the GOT section to get
1394 // the actual offset to use in the relocation.
1395 bool have_got_offset = false;
1396 unsigned int got_offset = 0;
1399 case elfcpp::R_X86_64_GOT32:
1400 case elfcpp::R_X86_64_GOT64:
1401 case elfcpp::R_X86_64_GOTPLT64:
1402 case elfcpp::R_X86_64_GOTPCREL:
1403 case elfcpp::R_X86_64_GOTPCREL64:
1406 gold_assert(gsym->has_got_offset());
1407 got_offset = gsym->got_offset() - target->got_size();
1411 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1412 gold_assert(object->local_has_got_offset(r_sym));
1413 got_offset = object->local_got_offset(r_sym) - target->got_size();
1415 have_got_offset = true;
1424 case elfcpp::R_X86_64_NONE:
1425 case elfcpp::R_386_GNU_VTINHERIT:
1426 case elfcpp::R_386_GNU_VTENTRY:
1429 case elfcpp::R_X86_64_64:
1430 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1433 case elfcpp::R_X86_64_PC64:
1434 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1438 case elfcpp::R_X86_64_32:
1439 // FIXME: we need to verify that value + addend fits into 32 bits:
1440 // uint64_t x = value + addend;
1441 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1442 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1443 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1446 case elfcpp::R_X86_64_32S:
1447 // FIXME: we need to verify that value + addend fits into 32 bits:
1448 // int64_t x = value + addend; // note this quantity is signed!
1449 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1450 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1453 case elfcpp::R_X86_64_PC32:
1454 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1458 case elfcpp::R_X86_64_16:
1459 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1462 case elfcpp::R_X86_64_PC16:
1463 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1467 case elfcpp::R_X86_64_8:
1468 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1471 case elfcpp::R_X86_64_PC8:
1472 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1476 case elfcpp::R_X86_64_PLT32:
1477 gold_assert(gsym == NULL
1478 || gsym->has_plt_offset()
1479 || gsym->final_value_is_known());
1480 // Note: while this code looks the same as for R_X86_64_PC32, it
1481 // behaves differently because psymval was set to point to
1482 // the PLT entry, rather than the symbol, in Scan::global().
1483 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1487 case elfcpp::R_X86_64_PLTOFF64:
1490 gold_assert(gsym->has_plt_offset()
1491 || gsym->final_value_is_known());
1492 elfcpp::Elf_types<64>::Elf_Addr got_address;
1493 got_address = target->got_section(NULL, NULL)->address();
1494 Relocate_functions<64, false>::rela64(view, object, psymval,
1495 addend - got_address);
1498 case elfcpp::R_X86_64_GOT32:
1499 gold_assert(have_got_offset);
1500 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1503 case elfcpp::R_X86_64_GOTPC32:
1506 elfcpp::Elf_types<64>::Elf_Addr value;
1507 value = target->got_plt_section()->address();
1508 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1512 case elfcpp::R_X86_64_GOT64:
1513 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1514 // Since we always add a PLT entry, this is equivalent.
1515 case elfcpp::R_X86_64_GOTPLT64:
1516 gold_assert(have_got_offset);
1517 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1520 case elfcpp::R_X86_64_GOTPC64:
1523 elfcpp::Elf_types<64>::Elf_Addr value;
1524 value = target->got_plt_section()->address();
1525 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1529 case elfcpp::R_X86_64_GOTOFF64:
1531 elfcpp::Elf_types<64>::Elf_Addr value;
1532 value = (psymval->value(object, 0)
1533 - target->got_plt_section()->address());
1534 Relocate_functions<64, false>::rela64(view, value, addend);
1538 case elfcpp::R_X86_64_GOTPCREL:
1540 gold_assert(have_got_offset);
1541 elfcpp::Elf_types<64>::Elf_Addr value;
1542 value = target->got_plt_section()->address() + got_offset;
1543 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1547 case elfcpp::R_X86_64_GOTPCREL64:
1549 gold_assert(have_got_offset);
1550 elfcpp::Elf_types<64>::Elf_Addr value;
1551 value = target->got_plt_section()->address() + got_offset;
1552 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1556 case elfcpp::R_X86_64_COPY:
1557 case elfcpp::R_X86_64_GLOB_DAT:
1558 case elfcpp::R_X86_64_JUMP_SLOT:
1559 case elfcpp::R_X86_64_RELATIVE:
1560 // These are outstanding tls relocs, which are unexpected when linking
1561 case elfcpp::R_X86_64_TPOFF64:
1562 case elfcpp::R_X86_64_DTPMOD64:
1563 case elfcpp::R_X86_64_TLSDESC:
1564 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1565 _("unexpected reloc %u in object file"),
1569 // These are initial tls relocs, which are expected when linking
1570 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1571 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1572 case elfcpp::R_X86_64_TLSDESC_CALL:
1573 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1574 case elfcpp::R_X86_64_DTPOFF32:
1575 case elfcpp::R_X86_64_DTPOFF64:
1576 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1577 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1578 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1579 view, address, view_size);
1582 case elfcpp::R_X86_64_SIZE32:
1583 case elfcpp::R_X86_64_SIZE64:
1585 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1586 _("unsupported reloc %u"),
1594 // Perform a TLS relocation.
1597 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1598 Target_x86_64* target,
1600 const elfcpp::Rela<64, false>& rela,
1601 unsigned int r_type,
1602 const Sized_symbol<64>* gsym,
1603 const Symbol_value<64>* psymval,
1604 unsigned char* view,
1605 elfcpp::Elf_types<64>::Elf_Addr address,
1608 Output_segment* tls_segment = relinfo->layout->tls_segment();
1610 const Sized_relobj<64, false>* object = relinfo->object;
1611 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1613 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1615 const bool is_final = (gsym == NULL
1616 ? !parameters->output_is_position_independent()
1617 : gsym->final_value_is_known());
1618 const tls::Tls_optimization optimized_type
1619 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1622 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1623 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1624 case elfcpp::R_X86_64_TLSDESC_CALL:
1625 if (optimized_type == tls::TLSOPT_TO_LE)
1627 gold_assert(tls_segment != NULL);
1628 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1629 rela, r_type, value, view,
1635 unsigned int got_offset;
1638 gold_assert(gsym->has_tls_got_offset(true));
1639 got_offset = gsym->tls_got_offset(true) - target->got_size();
1643 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1644 gold_assert(object->local_has_tls_got_offset(r_sym, true));
1645 got_offset = (object->local_tls_got_offset(r_sym, true)
1646 - target->got_size());
1648 if (optimized_type == tls::TLSOPT_TO_IE)
1650 gold_assert(tls_segment != NULL);
1651 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1652 got_offset, view, view_size);
1655 else if (optimized_type == tls::TLSOPT_NONE)
1657 // Relocate the field with the offset of the pair of GOT
1659 value = target->got_plt_section()->address() + got_offset;
1660 Relocate_functions<64, false>::pcrela32(view, value, addend,
1665 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1666 _("unsupported reloc %u"), r_type);
1669 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1670 if (optimized_type == tls::TLSOPT_TO_LE)
1672 gold_assert(tls_segment != NULL);
1673 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1674 value, view, view_size);
1677 else if (optimized_type == tls::TLSOPT_NONE)
1679 // Relocate the field with the offset of the GOT entry for
1680 // the module index.
1681 unsigned int got_offset;
1682 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1683 - target->got_size());
1684 value = target->got_plt_section()->address() + got_offset;
1685 Relocate_functions<64, false>::pcrela32(view, value, addend,
1689 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1690 _("unsupported reloc %u"), r_type);
1693 case elfcpp::R_X86_64_DTPOFF32:
1694 gold_assert(tls_segment != NULL);
1695 if (optimized_type == tls::TLSOPT_TO_LE)
1696 value -= tls_segment->memsz();
1697 Relocate_functions<64, false>::rela32(view, value, 0);
1700 case elfcpp::R_X86_64_DTPOFF64:
1701 gold_assert(tls_segment != NULL);
1702 if (optimized_type == tls::TLSOPT_TO_LE)
1703 value -= tls_segment->memsz();
1704 Relocate_functions<64, false>::rela64(view, value, 0);
1707 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1708 if (optimized_type == tls::TLSOPT_TO_LE)
1710 gold_assert(tls_segment != NULL);
1711 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1712 rela, r_type, value, view,
1716 else if (optimized_type == tls::TLSOPT_NONE)
1718 // Relocate the field with the offset of the GOT entry for
1719 // the tp-relative offset of the symbol.
1720 unsigned int got_offset;
1723 gold_assert(gsym->has_got_offset());
1724 got_offset = gsym->got_offset() - target->got_size();
1728 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1729 gold_assert(object->local_has_got_offset(r_sym));
1730 got_offset = (object->local_got_offset(r_sym)
1731 - target->got_size());
1733 value = target->got_plt_section()->address() + got_offset;
1734 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1737 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1738 _("unsupported reloc type %u"),
1742 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1743 value -= tls_segment->memsz();
1744 Relocate_functions<64, false>::rela32(view, value, 0);
1749 // Do a relocation in which we convert a TLS General-Dynamic to an
1753 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1755 Output_segment* tls_segment,
1756 const elfcpp::Rela<64, false>& rela,
1758 elfcpp::Elf_types<64>::Elf_Addr value,
1759 unsigned char* view,
1762 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1763 // .word 0x6666; rex64; call __tls_get_addr
1764 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1766 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1767 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1769 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1770 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1771 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1772 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1774 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1776 value -= tls_segment->memsz();
1777 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1779 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1781 this->skip_call_tls_get_addr_ = true;
1784 // Do a relocation in which we convert a TLS General-Dynamic to a
1788 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1790 Output_segment* tls_segment,
1791 const elfcpp::Rela<64, false>& rela,
1793 elfcpp::Elf_types<64>::Elf_Addr value,
1794 unsigned char* view,
1797 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1798 // .word 0x6666; rex64; call __tls_get_addr
1799 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1801 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1802 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1804 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1805 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1806 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1807 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1809 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1811 value -= tls_segment->memsz();
1812 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1814 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1816 this->skip_call_tls_get_addr_ = true;
1820 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1823 const elfcpp::Rela<64, false>& rela,
1825 elfcpp::Elf_types<64>::Elf_Addr,
1826 unsigned char* view,
1829 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1830 // ... leq foo@dtpoff(%rax),%reg
1831 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1833 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1834 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1836 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1837 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1839 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1841 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1843 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1845 this->skip_call_tls_get_addr_ = true;
1848 // Do a relocation in which we convert a TLS Initial-Exec to a
1852 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1854 Output_segment* tls_segment,
1855 const elfcpp::Rela<64, false>& rela,
1857 elfcpp::Elf_types<64>::Elf_Addr value,
1858 unsigned char* view,
1861 // We need to examine the opcodes to figure out which instruction we
1864 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1865 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1867 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1868 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1870 unsigned char op1 = view[-3];
1871 unsigned char op2 = view[-2];
1872 unsigned char op3 = view[-1];
1873 unsigned char reg = op3 >> 3;
1881 view[-1] = 0xc0 | reg;
1885 // Special handling for %rsp.
1889 view[-1] = 0xc0 | reg;
1897 view[-1] = 0x80 | reg | (reg << 3);
1900 value -= tls_segment->memsz();
1901 Relocate_functions<64, false>::rela32(view, value, 0);
1904 // Relocate section data.
1907 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1908 unsigned int sh_type,
1909 const unsigned char* prelocs,
1911 Output_section* output_section,
1912 bool needs_special_offset_handling,
1913 unsigned char* view,
1914 elfcpp::Elf_types<64>::Elf_Addr address,
1917 gold_assert(sh_type == elfcpp::SHT_RELA);
1919 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1920 Target_x86_64::Relocate>(
1926 needs_special_offset_handling,
1932 // Return the value to use for a dynamic which requires special
1933 // treatment. This is how we support equality comparisons of function
1934 // pointers across shared library boundaries, as described in the
1935 // processor specific ABI supplement.
1938 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1940 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1941 return this->plt_section()->address() + gsym->plt_offset();
1944 // Return a string used to fill a code section with nops to take up
1945 // the specified length.
1948 Target_x86_64::do_code_fill(off_t length)
1952 // Build a jmpq instruction to skip over the bytes.
1953 unsigned char jmp[5];
1955 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1956 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1957 + std::string(length - 5, '\0'));
1960 // Nop sequences of various lengths.
1961 const char nop1[1] = { 0x90 }; // nop
1962 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1963 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1964 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1965 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1966 0x00 }; // leal 0(%esi,1),%esi
1967 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1969 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1971 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1972 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1973 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1974 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1976 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1977 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1979 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1980 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1982 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1983 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1984 0x00, 0x00, 0x00, 0x00 };
1985 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1986 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1987 0x27, 0x00, 0x00, 0x00,
1989 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1990 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1991 0xbc, 0x27, 0x00, 0x00,
1993 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1994 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1995 0x90, 0x90, 0x90, 0x90,
1998 const char* nops[16] = {
2000 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2001 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2004 return std::string(nops[length], length);
2007 // The selector for x86_64 object files.
2009 class Target_selector_x86_64 : public Target_selector
2012 Target_selector_x86_64()
2013 : Target_selector(elfcpp::EM_X86_64, 64, false)
2017 recognize(int machine, int osabi, int abiversion);
2020 Target_x86_64* target_;
2023 // Recognize an x86_64 object file when we already know that the machine
2024 // number is EM_X86_64.
2027 Target_selector_x86_64::recognize(int, int, int)
2029 if (this->target_ == NULL)
2030 this->target_ = new Target_x86_64();
2031 return this->target_;
2034 Target_selector_x86_64 target_selector_x86_64;
2036 } // End anonymous namespace.