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 layout->set_has_static_tls();
946 if (optimized_type == tls::TLSOPT_NONE)
948 // Create a GOT entry for the tp-relative offset.
949 Output_data_got<64, false>* got
950 = target->got_section(symtab, layout);
951 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
952 got->add_local_with_rela(object, r_sym,
953 target->rela_dyn_section(layout),
954 elfcpp::R_X86_64_TPOFF64);
956 else if (optimized_type != tls::TLSOPT_TO_LE)
957 unsupported_reloc_local(object, r_type);
960 case elfcpp::R_X86_64_TPOFF32: // Local-exec
961 layout->set_has_static_tls();
962 if (output_is_shared)
963 unsupported_reloc_local(object, r_type);
972 case elfcpp::R_X86_64_SIZE32:
973 case elfcpp::R_X86_64_SIZE64:
975 gold_error(_("%s: unsupported reloc %u against local symbol"),
976 object->name().c_str(), r_type);
982 // Report an unsupported relocation against a global symbol.
985 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
989 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
990 object->name().c_str(), r_type, gsym->demangled_name().c_str());
993 // Scan a relocation for a global symbol.
996 Target_x86_64::Scan::global(const General_options& options,
997 Symbol_table* symtab,
999 Target_x86_64* target,
1000 Sized_relobj<64, false>* object,
1001 unsigned int data_shndx,
1002 Output_section* output_section,
1003 const elfcpp::Rela<64, false>& reloc,
1004 unsigned int r_type,
1009 case elfcpp::R_X86_64_NONE:
1010 case elfcpp::R_386_GNU_VTINHERIT:
1011 case elfcpp::R_386_GNU_VTENTRY:
1014 case elfcpp::R_X86_64_64:
1015 case elfcpp::R_X86_64_32:
1016 case elfcpp::R_X86_64_32S:
1017 case elfcpp::R_X86_64_16:
1018 case elfcpp::R_X86_64_8:
1020 // Make a PLT entry if necessary.
1021 if (gsym->needs_plt_entry())
1023 target->make_plt_entry(symtab, layout, gsym);
1024 // Since this is not a PC-relative relocation, we may be
1025 // taking the address of a function. In that case we need to
1026 // set the entry in the dynamic symbol table to the address of
1028 if (gsym->is_from_dynobj())
1029 gsym->set_needs_dynsym_value();
1031 // Make a dynamic relocation if necessary.
1032 if (gsym->needs_dynamic_reloc(true, false))
1034 if (target->may_need_copy_reloc(gsym))
1036 target->copy_reloc(&options, symtab, layout, object,
1037 data_shndx, output_section, gsym, reloc);
1039 else if (r_type == elfcpp::R_X86_64_64
1040 && gsym->can_use_relative_reloc(false))
1042 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1043 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1044 output_section, object,
1045 data_shndx, reloc.get_r_offset(),
1046 reloc.get_r_addend());
1050 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1051 rela_dyn->add_global(gsym, r_type, output_section, object,
1052 data_shndx, reloc.get_r_offset(),
1053 reloc.get_r_addend());
1059 case elfcpp::R_X86_64_PC64:
1060 case elfcpp::R_X86_64_PC32:
1061 case elfcpp::R_X86_64_PC16:
1062 case elfcpp::R_X86_64_PC8:
1064 // Make a PLT entry if necessary.
1065 if (gsym->needs_plt_entry())
1066 target->make_plt_entry(symtab, layout, gsym);
1067 // Make a dynamic relocation if necessary.
1068 bool is_function_call = (gsym->type() == elfcpp::STT_FUNC);
1069 if (gsym->needs_dynamic_reloc(false, is_function_call))
1071 if (target->may_need_copy_reloc(gsym))
1073 target->copy_reloc(&options, symtab, layout, object,
1074 data_shndx, output_section, gsym, reloc);
1078 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1079 rela_dyn->add_global(gsym, r_type, output_section, object,
1080 data_shndx, reloc.get_r_offset(),
1081 reloc.get_r_addend());
1087 case elfcpp::R_X86_64_GOT64:
1088 case elfcpp::R_X86_64_GOT32:
1089 case elfcpp::R_X86_64_GOTPCREL64:
1090 case elfcpp::R_X86_64_GOTPCREL:
1091 case elfcpp::R_X86_64_GOTPLT64:
1093 // The symbol requires a GOT entry.
1094 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1095 if (gsym->final_value_is_known())
1096 got->add_global(gsym);
1099 // If this symbol is not fully resolved, we need to add a
1100 // dynamic relocation for it.
1101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1102 if (gsym->is_from_dynobj() || gsym->is_preemptible())
1103 got->add_global_with_rela(gsym, rela_dyn,
1104 elfcpp::R_X86_64_GLOB_DAT);
1107 if (got->add_global(gsym))
1108 rela_dyn->add_global_relative(gsym,
1109 elfcpp::R_X86_64_RELATIVE,
1110 got, gsym->got_offset(), 0);
1113 // For GOTPLT64, we also need a PLT entry (but only if the
1114 // symbol is not fully resolved).
1115 if (r_type == elfcpp::R_X86_64_GOTPLT64
1116 && !gsym->final_value_is_known())
1117 target->make_plt_entry(symtab, layout, gsym);
1121 case elfcpp::R_X86_64_PLT32:
1122 // If the symbol is fully resolved, this is just a PC32 reloc.
1123 // Otherwise we need a PLT entry.
1124 if (gsym->final_value_is_known())
1126 // If building a shared library, we can also skip the PLT entry
1127 // if the symbol is defined in the output file and is protected
1129 if (gsym->is_defined()
1130 && !gsym->is_from_dynobj()
1131 && !gsym->is_preemptible())
1133 target->make_plt_entry(symtab, layout, gsym);
1136 case elfcpp::R_X86_64_GOTPC32:
1137 case elfcpp::R_X86_64_GOTOFF64:
1138 case elfcpp::R_X86_64_GOTPC64:
1139 case elfcpp::R_X86_64_PLTOFF64:
1140 // We need a GOT section.
1141 target->got_section(symtab, layout);
1142 // For PLTOFF64, we also need a PLT entry (but only if the
1143 // symbol is not fully resolved).
1144 if (r_type == elfcpp::R_X86_64_PLTOFF64
1145 && !gsym->final_value_is_known())
1146 target->make_plt_entry(symtab, layout, gsym);
1149 case elfcpp::R_X86_64_COPY:
1150 case elfcpp::R_X86_64_GLOB_DAT:
1151 case elfcpp::R_X86_64_JUMP_SLOT:
1152 case elfcpp::R_X86_64_RELATIVE:
1153 // These are outstanding tls relocs, which are unexpected when linking
1154 case elfcpp::R_X86_64_TPOFF64:
1155 case elfcpp::R_X86_64_DTPMOD64:
1156 case elfcpp::R_X86_64_TLSDESC:
1157 gold_error(_("%s: unexpected reloc %u in object file"),
1158 object->name().c_str(), r_type);
1161 // These are initial tls relocs, which are expected for global()
1162 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1163 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1164 case elfcpp::R_X86_64_TLSDESC_CALL:
1165 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1166 case elfcpp::R_X86_64_DTPOFF32:
1167 case elfcpp::R_X86_64_DTPOFF64:
1168 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1169 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1171 const bool is_final = gsym->final_value_is_known();
1172 const tls::Tls_optimization optimized_type
1173 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1176 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1177 if (optimized_type == tls::TLSOPT_NONE)
1179 // Create a pair of GOT entries for the module index and
1180 // dtv-relative offset.
1181 Output_data_got<64, false>* got
1182 = target->got_section(symtab, layout);
1183 got->add_global_tls_with_rela(gsym,
1184 target->rela_dyn_section(layout),
1185 elfcpp::R_X86_64_DTPMOD64,
1186 elfcpp::R_X86_64_DTPOFF64);
1188 else if (optimized_type == tls::TLSOPT_TO_IE)
1190 // Create a GOT entry for the tp-relative offset.
1191 Output_data_got<64, false>* got
1192 = target->got_section(symtab, layout);
1193 got->add_global_with_rela(gsym,
1194 target->rela_dyn_section(layout),
1195 elfcpp::R_X86_64_TPOFF64);
1197 else if (optimized_type != tls::TLSOPT_TO_LE)
1198 unsupported_reloc_global(object, r_type, gsym);
1201 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1202 case elfcpp::R_X86_64_TLSDESC_CALL:
1203 // FIXME: If not relaxing to LE, we need to generate
1204 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1205 if (optimized_type != tls::TLSOPT_TO_LE)
1206 unsupported_reloc_global(object, r_type, gsym);
1209 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1210 if (optimized_type == tls::TLSOPT_NONE)
1212 // Create a GOT entry for the module index.
1213 target->got_mod_index_entry(symtab, layout, object);
1215 else if (optimized_type != tls::TLSOPT_TO_LE)
1216 unsupported_reloc_global(object, r_type, gsym);
1219 case elfcpp::R_X86_64_DTPOFF32:
1220 case elfcpp::R_X86_64_DTPOFF64:
1223 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1224 layout->set_has_static_tls();
1225 if (optimized_type == tls::TLSOPT_NONE)
1227 // Create a GOT entry for the tp-relative offset.
1228 Output_data_got<64, false>* got
1229 = target->got_section(symtab, layout);
1230 got->add_global_with_rela(gsym,
1231 target->rela_dyn_section(layout),
1232 elfcpp::R_X86_64_TPOFF64);
1234 else if (optimized_type != tls::TLSOPT_TO_LE)
1235 unsupported_reloc_global(object, r_type, gsym);
1238 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1239 layout->set_has_static_tls();
1240 if (parameters->output_is_shared())
1241 unsupported_reloc_local(object, r_type);
1250 case elfcpp::R_X86_64_SIZE32:
1251 case elfcpp::R_X86_64_SIZE64:
1253 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1254 object->name().c_str(), r_type,
1255 gsym->demangled_name().c_str());
1260 // Scan relocations for a section.
1263 Target_x86_64::scan_relocs(const General_options& options,
1264 Symbol_table* symtab,
1266 Sized_relobj<64, false>* object,
1267 unsigned int data_shndx,
1268 unsigned int sh_type,
1269 const unsigned char* prelocs,
1271 Output_section* output_section,
1272 bool needs_special_offset_handling,
1273 size_t local_symbol_count,
1274 const unsigned char* plocal_symbols)
1276 if (sh_type == elfcpp::SHT_REL)
1278 gold_error(_("%s: unsupported REL reloc section"),
1279 object->name().c_str());
1283 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1284 Target_x86_64::Scan>(
1294 needs_special_offset_handling,
1299 // Finalize the sections.
1302 Target_x86_64::do_finalize_sections(Layout* layout)
1304 // Fill in some more dynamic tags.
1305 Output_data_dynamic* const odyn = layout->dynamic_data();
1308 if (this->got_plt_ != NULL)
1309 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1311 if (this->plt_ != NULL)
1313 const Output_data* od = this->plt_->rel_plt();
1314 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1315 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1316 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1319 if (this->rela_dyn_ != NULL)
1321 const Output_data* od = this->rela_dyn_;
1322 odyn->add_section_address(elfcpp::DT_RELA, od);
1323 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1324 odyn->add_constant(elfcpp::DT_RELAENT,
1325 elfcpp::Elf_sizes<64>::rela_size);
1328 if (!parameters->output_is_shared())
1330 // The value of the DT_DEBUG tag is filled in by the dynamic
1331 // linker at run time, and used by the debugger.
1332 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1336 // Emit any relocs we saved in an attempt to avoid generating COPY
1338 if (this->copy_relocs_ == NULL)
1340 if (this->copy_relocs_->any_to_emit())
1342 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1343 this->copy_relocs_->emit(rela_dyn);
1345 delete this->copy_relocs_;
1346 this->copy_relocs_ = NULL;
1349 // Perform a relocation.
1352 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1353 Target_x86_64* target,
1355 const elfcpp::Rela<64, false>& rela,
1356 unsigned int r_type,
1357 const Sized_symbol<64>* gsym,
1358 const Symbol_value<64>* psymval,
1359 unsigned char* view,
1360 elfcpp::Elf_types<64>::Elf_Addr address,
1363 if (this->skip_call_tls_get_addr_)
1365 if (r_type != elfcpp::R_X86_64_PLT32
1367 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1369 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1370 _("missing expected TLS relocation"));
1374 this->skip_call_tls_get_addr_ = false;
1379 // Pick the value to use for symbols defined in shared objects.
1380 Symbol_value<64> symval;
1382 && (gsym->is_from_dynobj()
1383 || (parameters->output_is_shared()
1384 && gsym->is_preemptible()))
1385 && gsym->has_plt_offset())
1387 symval.set_output_value(target->plt_section()->address()
1388 + gsym->plt_offset());
1392 const Sized_relobj<64, false>* object = relinfo->object;
1393 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1395 // Get the GOT offset if needed.
1396 // The GOT pointer points to the end of the GOT section.
1397 // We need to subtract the size of the GOT section to get
1398 // the actual offset to use in the relocation.
1399 bool have_got_offset = false;
1400 unsigned int got_offset = 0;
1403 case elfcpp::R_X86_64_GOT32:
1404 case elfcpp::R_X86_64_GOT64:
1405 case elfcpp::R_X86_64_GOTPLT64:
1406 case elfcpp::R_X86_64_GOTPCREL:
1407 case elfcpp::R_X86_64_GOTPCREL64:
1410 gold_assert(gsym->has_got_offset());
1411 got_offset = gsym->got_offset() - target->got_size();
1415 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1416 gold_assert(object->local_has_got_offset(r_sym));
1417 got_offset = object->local_got_offset(r_sym) - target->got_size();
1419 have_got_offset = true;
1428 case elfcpp::R_X86_64_NONE:
1429 case elfcpp::R_386_GNU_VTINHERIT:
1430 case elfcpp::R_386_GNU_VTENTRY:
1433 case elfcpp::R_X86_64_64:
1434 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1437 case elfcpp::R_X86_64_PC64:
1438 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1442 case elfcpp::R_X86_64_32:
1443 // FIXME: we need to verify that value + addend fits into 32 bits:
1444 // uint64_t x = value + addend;
1445 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1446 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1447 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1450 case elfcpp::R_X86_64_32S:
1451 // FIXME: we need to verify that value + addend fits into 32 bits:
1452 // int64_t x = value + addend; // note this quantity is signed!
1453 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1454 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1457 case elfcpp::R_X86_64_PC32:
1458 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1462 case elfcpp::R_X86_64_16:
1463 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1466 case elfcpp::R_X86_64_PC16:
1467 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1471 case elfcpp::R_X86_64_8:
1472 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1475 case elfcpp::R_X86_64_PC8:
1476 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1480 case elfcpp::R_X86_64_PLT32:
1481 gold_assert(gsym == NULL
1482 || gsym->has_plt_offset()
1483 || gsym->final_value_is_known());
1484 // Note: while this code looks the same as for R_X86_64_PC32, it
1485 // behaves differently because psymval was set to point to
1486 // the PLT entry, rather than the symbol, in Scan::global().
1487 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1491 case elfcpp::R_X86_64_PLTOFF64:
1494 gold_assert(gsym->has_plt_offset()
1495 || gsym->final_value_is_known());
1496 elfcpp::Elf_types<64>::Elf_Addr got_address;
1497 got_address = target->got_section(NULL, NULL)->address();
1498 Relocate_functions<64, false>::rela64(view, object, psymval,
1499 addend - got_address);
1502 case elfcpp::R_X86_64_GOT32:
1503 gold_assert(have_got_offset);
1504 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1507 case elfcpp::R_X86_64_GOTPC32:
1510 elfcpp::Elf_types<64>::Elf_Addr value;
1511 value = target->got_plt_section()->address();
1512 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1516 case elfcpp::R_X86_64_GOT64:
1517 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1518 // Since we always add a PLT entry, this is equivalent.
1519 case elfcpp::R_X86_64_GOTPLT64:
1520 gold_assert(have_got_offset);
1521 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1524 case elfcpp::R_X86_64_GOTPC64:
1527 elfcpp::Elf_types<64>::Elf_Addr value;
1528 value = target->got_plt_section()->address();
1529 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1533 case elfcpp::R_X86_64_GOTOFF64:
1535 elfcpp::Elf_types<64>::Elf_Addr value;
1536 value = (psymval->value(object, 0)
1537 - target->got_plt_section()->address());
1538 Relocate_functions<64, false>::rela64(view, value, addend);
1542 case elfcpp::R_X86_64_GOTPCREL:
1544 gold_assert(have_got_offset);
1545 elfcpp::Elf_types<64>::Elf_Addr value;
1546 value = target->got_plt_section()->address() + got_offset;
1547 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1551 case elfcpp::R_X86_64_GOTPCREL64:
1553 gold_assert(have_got_offset);
1554 elfcpp::Elf_types<64>::Elf_Addr value;
1555 value = target->got_plt_section()->address() + got_offset;
1556 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1560 case elfcpp::R_X86_64_COPY:
1561 case elfcpp::R_X86_64_GLOB_DAT:
1562 case elfcpp::R_X86_64_JUMP_SLOT:
1563 case elfcpp::R_X86_64_RELATIVE:
1564 // These are outstanding tls relocs, which are unexpected when linking
1565 case elfcpp::R_X86_64_TPOFF64:
1566 case elfcpp::R_X86_64_DTPMOD64:
1567 case elfcpp::R_X86_64_TLSDESC:
1568 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1569 _("unexpected reloc %u in object file"),
1573 // These are initial tls relocs, which are expected when linking
1574 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1575 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1576 case elfcpp::R_X86_64_TLSDESC_CALL:
1577 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1578 case elfcpp::R_X86_64_DTPOFF32:
1579 case elfcpp::R_X86_64_DTPOFF64:
1580 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1581 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1582 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1583 view, address, view_size);
1586 case elfcpp::R_X86_64_SIZE32:
1587 case elfcpp::R_X86_64_SIZE64:
1589 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1590 _("unsupported reloc %u"),
1598 // Perform a TLS relocation.
1601 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1602 Target_x86_64* target,
1604 const elfcpp::Rela<64, false>& rela,
1605 unsigned int r_type,
1606 const Sized_symbol<64>* gsym,
1607 const Symbol_value<64>* psymval,
1608 unsigned char* view,
1609 elfcpp::Elf_types<64>::Elf_Addr address,
1612 Output_segment* tls_segment = relinfo->layout->tls_segment();
1614 const Sized_relobj<64, false>* object = relinfo->object;
1615 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1617 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1619 const bool is_final = (gsym == NULL
1620 ? !parameters->output_is_position_independent()
1621 : gsym->final_value_is_known());
1622 const tls::Tls_optimization optimized_type
1623 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1626 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1627 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1628 case elfcpp::R_X86_64_TLSDESC_CALL:
1629 if (optimized_type == tls::TLSOPT_TO_LE)
1631 gold_assert(tls_segment != NULL);
1632 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1633 rela, r_type, value, view,
1639 unsigned int got_offset;
1642 gold_assert(gsym->has_tls_got_offset(true));
1643 got_offset = gsym->tls_got_offset(true) - target->got_size();
1647 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1648 gold_assert(object->local_has_tls_got_offset(r_sym, true));
1649 got_offset = (object->local_tls_got_offset(r_sym, true)
1650 - target->got_size());
1652 if (optimized_type == tls::TLSOPT_TO_IE)
1654 gold_assert(tls_segment != NULL);
1655 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1656 got_offset, view, view_size);
1659 else if (optimized_type == tls::TLSOPT_NONE)
1661 // Relocate the field with the offset of the pair of GOT
1663 value = target->got_plt_section()->address() + got_offset;
1664 Relocate_functions<64, false>::pcrela32(view, value, addend,
1669 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1670 _("unsupported reloc %u"), r_type);
1673 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1674 if (optimized_type == tls::TLSOPT_TO_LE)
1676 gold_assert(tls_segment != NULL);
1677 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1678 value, view, view_size);
1681 else if (optimized_type == tls::TLSOPT_NONE)
1683 // Relocate the field with the offset of the GOT entry for
1684 // the module index.
1685 unsigned int got_offset;
1686 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1687 - target->got_size());
1688 value = target->got_plt_section()->address() + got_offset;
1689 Relocate_functions<64, false>::pcrela32(view, value, addend,
1693 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1694 _("unsupported reloc %u"), r_type);
1697 case elfcpp::R_X86_64_DTPOFF32:
1698 gold_assert(tls_segment != NULL);
1699 if (optimized_type == tls::TLSOPT_TO_LE)
1700 value -= tls_segment->memsz();
1701 Relocate_functions<64, false>::rela32(view, value, 0);
1704 case elfcpp::R_X86_64_DTPOFF64:
1705 gold_assert(tls_segment != NULL);
1706 if (optimized_type == tls::TLSOPT_TO_LE)
1707 value -= tls_segment->memsz();
1708 Relocate_functions<64, false>::rela64(view, value, 0);
1711 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1712 if (optimized_type == tls::TLSOPT_TO_LE)
1714 gold_assert(tls_segment != NULL);
1715 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1716 rela, r_type, value, view,
1720 else if (optimized_type == tls::TLSOPT_NONE)
1722 // Relocate the field with the offset of the GOT entry for
1723 // the tp-relative offset of the symbol.
1724 unsigned int got_offset;
1727 gold_assert(gsym->has_got_offset());
1728 got_offset = gsym->got_offset() - target->got_size();
1732 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1733 gold_assert(object->local_has_got_offset(r_sym));
1734 got_offset = (object->local_got_offset(r_sym)
1735 - target->got_size());
1737 value = target->got_plt_section()->address() + got_offset;
1738 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1741 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1742 _("unsupported reloc type %u"),
1746 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1747 value -= tls_segment->memsz();
1748 Relocate_functions<64, false>::rela32(view, value, 0);
1753 // Do a relocation in which we convert a TLS General-Dynamic to an
1757 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1759 Output_segment* tls_segment,
1760 const elfcpp::Rela<64, false>& rela,
1762 elfcpp::Elf_types<64>::Elf_Addr value,
1763 unsigned char* view,
1766 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1767 // .word 0x6666; rex64; call __tls_get_addr
1768 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1770 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1771 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1773 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1774 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1775 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1776 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1778 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1780 value -= tls_segment->memsz();
1781 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1783 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1785 this->skip_call_tls_get_addr_ = true;
1788 // Do a relocation in which we convert a TLS General-Dynamic to a
1792 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1794 Output_segment* tls_segment,
1795 const elfcpp::Rela<64, false>& rela,
1797 elfcpp::Elf_types<64>::Elf_Addr value,
1798 unsigned char* view,
1801 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1802 // .word 0x6666; rex64; call __tls_get_addr
1803 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1805 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1806 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1808 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1809 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1810 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1811 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1813 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1815 value -= tls_segment->memsz();
1816 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1818 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1820 this->skip_call_tls_get_addr_ = true;
1824 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1827 const elfcpp::Rela<64, false>& rela,
1829 elfcpp::Elf_types<64>::Elf_Addr,
1830 unsigned char* view,
1833 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1834 // ... leq foo@dtpoff(%rax),%reg
1835 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1837 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1838 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1840 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1841 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1843 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1845 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1847 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1849 this->skip_call_tls_get_addr_ = true;
1852 // Do a relocation in which we convert a TLS Initial-Exec to a
1856 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1858 Output_segment* tls_segment,
1859 const elfcpp::Rela<64, false>& rela,
1861 elfcpp::Elf_types<64>::Elf_Addr value,
1862 unsigned char* view,
1865 // We need to examine the opcodes to figure out which instruction we
1868 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1869 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1871 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1872 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1874 unsigned char op1 = view[-3];
1875 unsigned char op2 = view[-2];
1876 unsigned char op3 = view[-1];
1877 unsigned char reg = op3 >> 3;
1885 view[-1] = 0xc0 | reg;
1889 // Special handling for %rsp.
1893 view[-1] = 0xc0 | reg;
1901 view[-1] = 0x80 | reg | (reg << 3);
1904 value -= tls_segment->memsz();
1905 Relocate_functions<64, false>::rela32(view, value, 0);
1908 // Relocate section data.
1911 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1912 unsigned int sh_type,
1913 const unsigned char* prelocs,
1915 Output_section* output_section,
1916 bool needs_special_offset_handling,
1917 unsigned char* view,
1918 elfcpp::Elf_types<64>::Elf_Addr address,
1921 gold_assert(sh_type == elfcpp::SHT_RELA);
1923 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1924 Target_x86_64::Relocate>(
1930 needs_special_offset_handling,
1936 // Return the value to use for a dynamic which requires special
1937 // treatment. This is how we support equality comparisons of function
1938 // pointers across shared library boundaries, as described in the
1939 // processor specific ABI supplement.
1942 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1944 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1945 return this->plt_section()->address() + gsym->plt_offset();
1948 // Return a string used to fill a code section with nops to take up
1949 // the specified length.
1952 Target_x86_64::do_code_fill(off_t length)
1956 // Build a jmpq instruction to skip over the bytes.
1957 unsigned char jmp[5];
1959 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1960 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1961 + std::string(length - 5, '\0'));
1964 // Nop sequences of various lengths.
1965 const char nop1[1] = { 0x90 }; // nop
1966 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1967 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1968 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1969 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1970 0x00 }; // leal 0(%esi,1),%esi
1971 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1973 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1975 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1976 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1977 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1978 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1980 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1981 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1983 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1984 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1986 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1987 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1988 0x00, 0x00, 0x00, 0x00 };
1989 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1990 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1991 0x27, 0x00, 0x00, 0x00,
1993 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1994 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1995 0xbc, 0x27, 0x00, 0x00,
1997 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1998 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1999 0x90, 0x90, 0x90, 0x90,
2002 const char* nops[16] = {
2004 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2005 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2008 return std::string(nops[length], length);
2011 // The selector for x86_64 object files.
2013 class Target_selector_x86_64 : public Target_selector
2016 Target_selector_x86_64()
2017 : Target_selector(elfcpp::EM_X86_64, 64, false)
2021 recognize(int machine, int osabi, int abiversion);
2024 Target_x86_64* target_;
2027 // Recognize an x86_64 object file when we already know that the machine
2028 // number is EM_X86_64.
2031 Target_selector_x86_64::recognize(int, int, int)
2033 if (this->target_ == NULL)
2034 this->target_ = new Target_x86_64();
2035 return this->target_;
2038 Target_selector_x86_64 target_selector_x86_64;
2040 } // End anonymous namespace.