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,
110 section_size_type view_size);
112 // Return a string used to fill a code section with nops.
114 do_code_fill(section_size_type 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,
196 // Do a TLS General-Dynamic to Local-Exec transition.
198 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
199 Output_segment* tls_segment,
200 const elfcpp::Rela<64, false>&, unsigned int r_type,
201 elfcpp::Elf_types<64>::Elf_Addr value,
203 section_size_type view_size);
205 // Do a TLS General-Dynamic to Local-Exec transition.
207 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
208 Output_segment* tls_segment,
209 const elfcpp::Rela<64, false>&, unsigned int r_type,
210 elfcpp::Elf_types<64>::Elf_Addr value,
212 section_size_type view_size);
214 // Do a TLS Local-Dynamic to Local-Exec transition.
216 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
217 Output_segment* tls_segment,
218 const elfcpp::Rela<64, false>&, unsigned int r_type,
219 elfcpp::Elf_types<64>::Elf_Addr value,
221 section_size_type view_size);
223 // Do a TLS Initial-Exec to Local-Exec transition.
225 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
226 Output_segment* tls_segment,
227 const elfcpp::Rela<64, false>&, unsigned int r_type,
228 elfcpp::Elf_types<64>::Elf_Addr value,
230 section_size_type view_size);
232 // This is set if we should skip the next reloc, which should be a
233 // PLT32 reloc against ___tls_get_addr.
234 bool skip_call_tls_get_addr_;
237 // Adjust TLS relocation type based on the options and whether this
238 // is a local symbol.
239 static tls::Tls_optimization
240 optimize_tls_reloc(bool is_final, int r_type);
242 // Get the GOT section, creating it if necessary.
243 Output_data_got<64, false>*
244 got_section(Symbol_table*, Layout*);
246 // Get the GOT PLT section.
248 got_plt_section() const
250 gold_assert(this->got_plt_ != NULL);
251 return this->got_plt_;
254 // Create a PLT entry for a global symbol.
256 make_plt_entry(Symbol_table*, Layout*, Symbol*);
258 // Create a GOT entry for the TLS module index.
260 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
261 Sized_relobj<64, false>* object);
263 // Get the PLT section.
264 Output_data_plt_x86_64*
267 gold_assert(this->plt_ != NULL);
271 // Get the dynamic reloc section, creating it if necessary.
273 rela_dyn_section(Layout*);
275 // Return true if the symbol may need a COPY relocation.
276 // References from an executable object to non-function symbols
277 // defined in a dynamic object may need a COPY relocation.
279 may_need_copy_reloc(Symbol* gsym)
281 return (!parameters->output_is_shared()
282 && gsym->is_from_dynobj()
283 && gsym->type() != elfcpp::STT_FUNC);
286 // Copy a relocation against a global symbol.
288 copy_reloc(const General_options*, Symbol_table*, Layout*,
289 Sized_relobj<64, false>*, unsigned int,
290 Output_section*, Symbol*, const elfcpp::Rela<64, false>&);
292 // Information about this specific target which we pass to the
293 // general Target structure.
294 static const Target::Target_info x86_64_info;
297 Output_data_got<64, false>* got_;
299 Output_data_plt_x86_64* plt_;
300 // The GOT PLT section.
301 Output_data_space* got_plt_;
302 // The dynamic reloc section.
303 Reloc_section* rela_dyn_;
304 // Relocs saved to avoid a COPY reloc.
305 Copy_relocs<64, false>* copy_relocs_;
306 // Space for variables copied with a COPY reloc.
307 Output_data_space* dynbss_;
308 // Offset of the GOT entry for the TLS module index;
309 unsigned int got_mod_index_offset_;
312 const Target::Target_info Target_x86_64::x86_64_info =
315 false, // is_big_endian
316 elfcpp::EM_X86_64, // machine_code
317 false, // has_make_symbol
318 false, // has_resolve
319 true, // has_code_fill
320 true, // is_default_stack_executable
321 "/lib/ld64.so.1", // program interpreter
322 0x400000, // default_text_segment_address
323 0x1000, // abi_pagesize
324 0x1000 // common_pagesize
327 // Get the GOT section, creating it if necessary.
329 Output_data_got<64, false>*
330 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
332 if (this->got_ == NULL)
334 gold_assert(symtab != NULL && layout != NULL);
336 this->got_ = new Output_data_got<64, false>();
338 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
339 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
342 // The old GNU linker creates a .got.plt section. We just
343 // create another set of data in the .got section. Note that we
344 // always create a PLT if we create a GOT, although the PLT
346 this->got_plt_ = new Output_data_space(8);
347 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
348 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
351 // The first three entries are reserved.
352 this->got_plt_->set_current_data_size(3 * 8);
354 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
355 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
357 0, 0, elfcpp::STT_OBJECT,
359 elfcpp::STV_HIDDEN, 0,
366 // Get the dynamic reloc section, creating it if necessary.
368 Target_x86_64::Reloc_section*
369 Target_x86_64::rela_dyn_section(Layout* layout)
371 if (this->rela_dyn_ == NULL)
373 gold_assert(layout != NULL);
374 this->rela_dyn_ = new Reloc_section();
375 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
376 elfcpp::SHF_ALLOC, this->rela_dyn_);
378 return this->rela_dyn_;
381 // A class to handle the PLT data.
383 class Output_data_plt_x86_64 : public Output_section_data
386 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
388 Output_data_plt_x86_64(Layout*, Output_data_space*);
390 // Add an entry to the PLT.
392 add_entry(Symbol* gsym);
394 // Return the .rel.plt section data.
397 { return this->rel_; }
401 do_adjust_output_section(Output_section* os);
404 // The size of an entry in the PLT.
405 static const int plt_entry_size = 16;
407 // The first entry in the PLT.
408 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
409 // procedure linkage table for both programs and shared objects."
410 static unsigned char first_plt_entry[plt_entry_size];
412 // Other entries in the PLT for an executable.
413 static unsigned char plt_entry[plt_entry_size];
415 // Set the final size.
417 set_final_data_size()
418 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
420 // Write out the PLT data.
422 do_write(Output_file*);
424 // The reloc section.
426 // The .got.plt section.
427 Output_data_space* got_plt_;
428 // The number of PLT entries.
432 // Create the PLT section. The ordinary .got section is an argument,
433 // since we need to refer to the start. We also create our own .got
434 // section just for PLT entries.
436 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
437 Output_data_space* got_plt)
438 : Output_section_data(8), got_plt_(got_plt), count_(0)
440 this->rel_ = new Reloc_section();
441 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
442 elfcpp::SHF_ALLOC, this->rel_);
446 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
448 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
449 // linker, and so do we.
453 // Add an entry to the PLT.
456 Output_data_plt_x86_64::add_entry(Symbol* gsym)
458 gold_assert(!gsym->has_plt_offset());
460 // Note that when setting the PLT offset we skip the initial
461 // reserved PLT entry.
462 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
466 section_offset_type got_offset = this->got_plt_->current_data_size();
468 // Every PLT entry needs a GOT entry which points back to the PLT
469 // entry (this will be changed by the dynamic linker, normally
470 // lazily when the function is called).
471 this->got_plt_->set_current_data_size(got_offset + 8);
473 // Every PLT entry needs a reloc.
474 gsym->set_needs_dynsym_entry();
475 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
478 // Note that we don't need to save the symbol. The contents of the
479 // PLT are independent of which symbols are used. The symbols only
480 // appear in the relocations.
483 // The first entry in the PLT for an executable.
485 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
487 // From AMD64 ABI Draft 0.98, page 76
488 0xff, 0x35, // pushq contents of memory address
489 0, 0, 0, 0, // replaced with address of .got + 8
490 0xff, 0x25, // jmp indirect
491 0, 0, 0, 0, // replaced with address of .got + 16
492 0x90, 0x90, 0x90, 0x90 // noop (x4)
495 // Subsequent entries in the PLT for an executable.
497 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
499 // From AMD64 ABI Draft 0.98, page 76
500 0xff, 0x25, // jmpq indirect
501 0, 0, 0, 0, // replaced with address of symbol in .got
502 0x68, // pushq immediate
503 0, 0, 0, 0, // replaced with offset into relocation table
504 0xe9, // jmpq relative
505 0, 0, 0, 0 // replaced with offset to start of .plt
508 // Write out the PLT. This uses the hand-coded instructions above,
509 // and adjusts them as needed. This is specified by the AMD64 ABI.
512 Output_data_plt_x86_64::do_write(Output_file* of)
514 const off_t offset = this->offset();
515 const section_size_type oview_size =
516 convert_to_section_size_type(this->data_size());
517 unsigned char* const oview = of->get_output_view(offset, oview_size);
519 const off_t got_file_offset = this->got_plt_->offset();
520 const section_size_type got_size =
521 convert_to_section_size_type(this->got_plt_->data_size());
522 unsigned char* const got_view = of->get_output_view(got_file_offset,
525 unsigned char* pov = oview;
527 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
528 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
530 memcpy(pov, first_plt_entry, plt_entry_size);
531 // We do a jmp relative to the PC at the end of this instruction.
532 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
533 - (plt_address + 6));
534 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
535 - (plt_address + 12));
536 pov += plt_entry_size;
538 unsigned char* got_pov = got_view;
540 memset(got_pov, 0, 24);
543 unsigned int plt_offset = plt_entry_size;
544 unsigned int got_offset = 24;
545 const unsigned int count = this->count_;
546 for (unsigned int plt_index = 0;
549 pov += plt_entry_size,
551 plt_offset += plt_entry_size,
554 // Set and adjust the PLT entry itself.
555 memcpy(pov, plt_entry, plt_entry_size);
556 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
557 (got_address + got_offset
558 - (plt_address + plt_offset
561 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
562 elfcpp::Swap<32, false>::writeval(pov + 12,
563 - (plt_offset + plt_entry_size));
565 // Set the entry in the GOT.
566 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
569 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
570 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
572 of->write_output_view(offset, oview_size, oview);
573 of->write_output_view(got_file_offset, got_size, got_view);
576 // Create a PLT entry for a global symbol.
579 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
582 if (gsym->has_plt_offset())
585 if (this->plt_ == NULL)
587 // Create the GOT sections first.
588 this->got_section(symtab, layout);
590 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
591 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
593 | elfcpp::SHF_EXECINSTR),
597 this->plt_->add_entry(gsym);
600 // Create a GOT entry for the TLS module index.
603 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
604 Sized_relobj<64, false>* object)
606 if (this->got_mod_index_offset_ == -1U)
608 gold_assert(symtab != NULL && layout != NULL && object != NULL);
609 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
610 Output_data_got<64, false>* got = this->got_section(symtab, layout);
611 unsigned int got_offset = got->add_constant(0);
612 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
614 got->add_constant(0);
615 this->got_mod_index_offset_ = got_offset;
617 return this->got_mod_index_offset_;
620 // Handle a relocation against a non-function symbol defined in a
621 // dynamic object. The traditional way to handle this is to generate
622 // a COPY relocation to copy the variable at runtime from the shared
623 // object into the executable's data segment. However, this is
624 // undesirable in general, as if the size of the object changes in the
625 // dynamic object, the executable will no longer work correctly. If
626 // this relocation is in a writable section, then we can create a
627 // dynamic reloc and the dynamic linker will resolve it to the correct
628 // address at runtime. However, we do not want do that if the
629 // relocation is in a read-only section, as it would prevent the
630 // readonly segment from being shared. And if we have to eventually
631 // generate a COPY reloc, then any dynamic relocations will be
632 // useless. So this means that if this is a writable section, we need
633 // to save the relocation until we see whether we have to create a
634 // COPY relocation for this symbol for any other relocation.
637 Target_x86_64::copy_reloc(const General_options* options,
638 Symbol_table* symtab,
640 Sized_relobj<64, false>* object,
641 unsigned int data_shndx,
642 Output_section* output_section,
644 const elfcpp::Rela<64, false>& rela)
646 Sized_symbol<64>* ssym;
647 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
650 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
653 // So far we do not need a COPY reloc. Save this relocation.
654 // If it turns out that we never need a COPY reloc for this
655 // symbol, then we will emit the relocation.
656 if (this->copy_relocs_ == NULL)
657 this->copy_relocs_ = new Copy_relocs<64, false>();
658 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
662 // Allocate space for this symbol in the .bss section.
664 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
666 // There is no defined way to determine the required alignment
667 // of the symbol. We pick the alignment based on the size. We
668 // set an arbitrary maximum of 256.
670 for (align = 1; align < 512; align <<= 1)
671 if ((symsize & align) != 0)
674 if (this->dynbss_ == NULL)
676 this->dynbss_ = new Output_data_space(align);
677 layout->add_output_section_data(".bss",
680 | elfcpp::SHF_WRITE),
684 Output_data_space* dynbss = this->dynbss_;
686 if (align > dynbss->addralign())
687 dynbss->set_space_alignment(align);
689 section_size_type dynbss_size = dynbss->current_data_size();
690 dynbss_size = align_address(dynbss_size, align);
691 section_size_type offset = dynbss_size;
692 dynbss->set_current_data_size(dynbss_size + symsize);
694 symtab->define_with_copy_reloc(ssym, dynbss, offset);
696 // Add the COPY reloc.
697 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
698 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
703 // Optimize the TLS relocation type based on what we know about the
704 // symbol. IS_FINAL is true if the final address of this symbol is
705 // known at link time.
707 tls::Tls_optimization
708 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
710 // If we are generating a shared library, then we can't do anything
712 if (parameters->output_is_shared())
713 return tls::TLSOPT_NONE;
717 case elfcpp::R_X86_64_TLSGD:
718 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
719 case elfcpp::R_X86_64_TLSDESC_CALL:
720 // These are General-Dynamic which permits fully general TLS
721 // access. Since we know that we are generating an executable,
722 // we can convert this to Initial-Exec. If we also know that
723 // this is a local symbol, we can further switch to Local-Exec.
725 return tls::TLSOPT_TO_LE;
726 return tls::TLSOPT_TO_IE;
728 case elfcpp::R_X86_64_TLSLD:
729 // This is Local-Dynamic, which refers to a local symbol in the
730 // dynamic TLS block. Since we know that we generating an
731 // executable, we can switch to Local-Exec.
732 return tls::TLSOPT_TO_LE;
734 case elfcpp::R_X86_64_DTPOFF32:
735 case elfcpp::R_X86_64_DTPOFF64:
736 // Another Local-Dynamic reloc.
737 return tls::TLSOPT_TO_LE;
739 case elfcpp::R_X86_64_GOTTPOFF:
740 // These are Initial-Exec relocs which get the thread offset
741 // from the GOT. If we know that we are linking against the
742 // local symbol, we can switch to Local-Exec, which links the
743 // thread offset into the instruction.
745 return tls::TLSOPT_TO_LE;
746 return tls::TLSOPT_NONE;
748 case elfcpp::R_X86_64_TPOFF32:
749 // When we already have Local-Exec, there is nothing further we
751 return tls::TLSOPT_NONE;
758 // Report an unsupported relocation against a local symbol.
761 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
764 gold_error(_("%s: unsupported reloc %u against local symbol"),
765 object->name().c_str(), r_type);
768 // Scan a relocation for a local symbol.
771 Target_x86_64::Scan::local(const General_options&,
772 Symbol_table* symtab,
774 Target_x86_64* target,
775 Sized_relobj<64, false>* object,
776 unsigned int data_shndx,
777 Output_section* output_section,
778 const elfcpp::Rela<64, false>& reloc,
780 const elfcpp::Sym<64, false>& lsym)
784 case elfcpp::R_X86_64_NONE:
785 case elfcpp::R_386_GNU_VTINHERIT:
786 case elfcpp::R_386_GNU_VTENTRY:
789 case elfcpp::R_X86_64_64:
790 // If building a shared library (or a position-independent
791 // executable), we need to create a dynamic relocation for
792 // this location. The relocation applied at link time will
793 // apply the link-time value, so we flag the location with
794 // an R_386_RELATIVE relocation so the dynamic loader can
795 // relocate it easily.
796 if (parameters->output_is_position_independent())
798 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
799 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
800 rela_dyn->add_local_relative(object, r_sym,
801 elfcpp::R_X86_64_RELATIVE,
802 output_section, data_shndx,
803 reloc.get_r_offset(),
804 reloc.get_r_addend());
808 case elfcpp::R_X86_64_32:
809 case elfcpp::R_X86_64_32S:
810 case elfcpp::R_X86_64_16:
811 case elfcpp::R_X86_64_8:
812 // If building a shared library (or a position-independent
813 // executable), we need to create a dynamic relocation for
814 // this location. The relocation applied at link time will
815 // apply the link-time value, so we flag the location with
816 // an R_386_RELATIVE relocation so the dynamic loader can
817 // relocate it easily.
818 if (parameters->output_is_position_independent())
820 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
821 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
822 rela_dyn->add_local(object, r_sym, r_type, output_section,
823 data_shndx, reloc.get_r_offset(),
824 reloc.get_r_addend());
828 case elfcpp::R_X86_64_PC64:
829 case elfcpp::R_X86_64_PC32:
830 case elfcpp::R_X86_64_PC16:
831 case elfcpp::R_X86_64_PC8:
834 case elfcpp::R_X86_64_PLT32:
835 // Since we know this is a local symbol, we can handle this as a
839 case elfcpp::R_X86_64_GOTPC32:
840 case elfcpp::R_X86_64_GOTOFF64:
841 case elfcpp::R_X86_64_GOTPC64:
842 case elfcpp::R_X86_64_PLTOFF64:
843 // We need a GOT section.
844 target->got_section(symtab, layout);
845 // For PLTOFF64, we'd normally want a PLT section, but since we
846 // know this is a local symbol, no PLT is needed.
849 case elfcpp::R_X86_64_GOT64:
850 case elfcpp::R_X86_64_GOT32:
851 case elfcpp::R_X86_64_GOTPCREL64:
852 case elfcpp::R_X86_64_GOTPCREL:
853 case elfcpp::R_X86_64_GOTPLT64:
855 // The symbol requires a GOT entry.
856 Output_data_got<64, false>* got = target->got_section(symtab, layout);
857 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
858 if (got->add_local(object, r_sym))
860 // If we are generating a shared object, we need to add a
861 // dynamic relocation for this symbol's GOT entry.
862 if (parameters->output_is_position_independent())
864 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
865 // R_X86_64_RELATIVE assumes a 64-bit relocation.
866 if (r_type != elfcpp::R_X86_64_GOT32)
867 rela_dyn->add_local_relative(object, r_sym,
868 elfcpp::R_X86_64_RELATIVE, got,
869 object->local_got_offset(r_sym),
872 rela_dyn->add_local(object, r_sym, r_type,
873 got, object->local_got_offset(r_sym), 0);
876 // For GOTPLT64, we'd normally want a PLT section, but since
877 // we know this is a local symbol, no PLT is needed.
881 case elfcpp::R_X86_64_COPY:
882 case elfcpp::R_X86_64_GLOB_DAT:
883 case elfcpp::R_X86_64_JUMP_SLOT:
884 case elfcpp::R_X86_64_RELATIVE:
885 // These are outstanding tls relocs, which are unexpected when linking
886 case elfcpp::R_X86_64_TPOFF64:
887 case elfcpp::R_X86_64_DTPMOD64:
888 case elfcpp::R_X86_64_TLSDESC:
889 gold_error(_("%s: unexpected reloc %u in object file"),
890 object->name().c_str(), r_type);
893 // These are initial tls relocs, which are expected when linking
894 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
895 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
896 case elfcpp::R_X86_64_TLSDESC_CALL:
897 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
898 case elfcpp::R_X86_64_DTPOFF32:
899 case elfcpp::R_X86_64_DTPOFF64:
900 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
901 case elfcpp::R_X86_64_TPOFF32: // Local-exec
903 bool output_is_shared = parameters->output_is_shared();
904 const tls::Tls_optimization optimized_type
905 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
908 case elfcpp::R_X86_64_TLSGD: // General-dynamic
909 if (optimized_type == tls::TLSOPT_NONE)
911 // Create a pair of GOT entries for the module index and
912 // dtv-relative offset.
913 Output_data_got<64, false>* got
914 = target->got_section(symtab, layout);
915 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
916 got->add_local_tls_with_rela(object, r_sym,
917 lsym.get_st_shndx(), true,
918 target->rela_dyn_section(layout),
919 elfcpp::R_X86_64_DTPMOD64);
921 else if (optimized_type != tls::TLSOPT_TO_LE)
922 unsupported_reloc_local(object, r_type);
925 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
926 case elfcpp::R_X86_64_TLSDESC_CALL:
927 // FIXME: If not relaxing to LE, we need to generate
928 // a GOT entry with a R_x86_64_TLSDESC reloc.
929 if (optimized_type != tls::TLSOPT_TO_LE)
930 unsupported_reloc_local(object, r_type);
933 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
934 if (optimized_type == tls::TLSOPT_NONE)
936 // Create a GOT entry for the module index.
937 target->got_mod_index_entry(symtab, layout, object);
939 else if (optimized_type != tls::TLSOPT_TO_LE)
940 unsupported_reloc_local(object, r_type);
943 case elfcpp::R_X86_64_DTPOFF32:
944 case elfcpp::R_X86_64_DTPOFF64:
947 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
948 layout->set_has_static_tls();
949 if (optimized_type == tls::TLSOPT_NONE)
951 // Create a GOT entry for the tp-relative offset.
952 Output_data_got<64, false>* got
953 = target->got_section(symtab, layout);
954 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
955 got->add_local_with_rela(object, r_sym,
956 target->rela_dyn_section(layout),
957 elfcpp::R_X86_64_TPOFF64);
959 else if (optimized_type != tls::TLSOPT_TO_LE)
960 unsupported_reloc_local(object, r_type);
963 case elfcpp::R_X86_64_TPOFF32: // Local-exec
964 layout->set_has_static_tls();
965 if (output_is_shared)
966 unsupported_reloc_local(object, r_type);
975 case elfcpp::R_X86_64_SIZE32:
976 case elfcpp::R_X86_64_SIZE64:
978 gold_error(_("%s: unsupported reloc %u against local symbol"),
979 object->name().c_str(), r_type);
985 // Report an unsupported relocation against a global symbol.
988 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
992 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
993 object->name().c_str(), r_type, gsym->demangled_name().c_str());
996 // Scan a relocation for a global symbol.
999 Target_x86_64::Scan::global(const General_options& options,
1000 Symbol_table* symtab,
1002 Target_x86_64* target,
1003 Sized_relobj<64, false>* object,
1004 unsigned int data_shndx,
1005 Output_section* output_section,
1006 const elfcpp::Rela<64, false>& reloc,
1007 unsigned int r_type,
1012 case elfcpp::R_X86_64_NONE:
1013 case elfcpp::R_386_GNU_VTINHERIT:
1014 case elfcpp::R_386_GNU_VTENTRY:
1017 case elfcpp::R_X86_64_64:
1018 case elfcpp::R_X86_64_32:
1019 case elfcpp::R_X86_64_32S:
1020 case elfcpp::R_X86_64_16:
1021 case elfcpp::R_X86_64_8:
1023 // Make a PLT entry if necessary.
1024 if (gsym->needs_plt_entry())
1026 target->make_plt_entry(symtab, layout, gsym);
1027 // Since this is not a PC-relative relocation, we may be
1028 // taking the address of a function. In that case we need to
1029 // set the entry in the dynamic symbol table to the address of
1031 if (gsym->is_from_dynobj() && !parameters->output_is_shared())
1032 gsym->set_needs_dynsym_value();
1034 // Make a dynamic relocation if necessary.
1035 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1037 if (target->may_need_copy_reloc(gsym))
1039 target->copy_reloc(&options, symtab, layout, object,
1040 data_shndx, output_section, gsym, reloc);
1042 else if (r_type == elfcpp::R_X86_64_64
1043 && gsym->can_use_relative_reloc(false))
1045 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1046 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1047 output_section, object,
1048 data_shndx, reloc.get_r_offset(),
1049 reloc.get_r_addend());
1053 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1054 rela_dyn->add_global(gsym, r_type, output_section, object,
1055 data_shndx, reloc.get_r_offset(),
1056 reloc.get_r_addend());
1062 case elfcpp::R_X86_64_PC64:
1063 case elfcpp::R_X86_64_PC32:
1064 case elfcpp::R_X86_64_PC16:
1065 case elfcpp::R_X86_64_PC8:
1067 // Make a PLT entry if necessary.
1068 if (gsym->needs_plt_entry())
1069 target->make_plt_entry(symtab, layout, gsym);
1070 // Make a dynamic relocation if necessary.
1071 int flags = Symbol::NON_PIC_REF;
1072 if (gsym->type() == elfcpp::STT_FUNC)
1073 flags |= Symbol::FUNCTION_CALL;
1074 if (gsym->needs_dynamic_reloc(flags))
1076 if (target->may_need_copy_reloc(gsym))
1078 target->copy_reloc(&options, symtab, layout, object,
1079 data_shndx, output_section, gsym, reloc);
1083 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1084 rela_dyn->add_global(gsym, r_type, output_section, object,
1085 data_shndx, reloc.get_r_offset(),
1086 reloc.get_r_addend());
1092 case elfcpp::R_X86_64_GOT64:
1093 case elfcpp::R_X86_64_GOT32:
1094 case elfcpp::R_X86_64_GOTPCREL64:
1095 case elfcpp::R_X86_64_GOTPCREL:
1096 case elfcpp::R_X86_64_GOTPLT64:
1098 // The symbol requires a GOT entry.
1099 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1100 if (gsym->final_value_is_known())
1101 got->add_global(gsym);
1104 // If this symbol is not fully resolved, we need to add a
1105 // dynamic relocation for it.
1106 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1107 if (gsym->is_from_dynobj() || gsym->is_preemptible())
1108 got->add_global_with_rela(gsym, rela_dyn,
1109 elfcpp::R_X86_64_GLOB_DAT);
1112 if (got->add_global(gsym))
1113 rela_dyn->add_global_relative(gsym,
1114 elfcpp::R_X86_64_RELATIVE,
1115 got, gsym->got_offset(), 0);
1118 // For GOTPLT64, we also need a PLT entry (but only if the
1119 // symbol is not fully resolved).
1120 if (r_type == elfcpp::R_X86_64_GOTPLT64
1121 && !gsym->final_value_is_known())
1122 target->make_plt_entry(symtab, layout, gsym);
1126 case elfcpp::R_X86_64_PLT32:
1127 // If the symbol is fully resolved, this is just a PC32 reloc.
1128 // Otherwise we need a PLT entry.
1129 if (gsym->final_value_is_known())
1131 // If building a shared library, we can also skip the PLT entry
1132 // if the symbol is defined in the output file and is protected
1134 if (gsym->is_defined()
1135 && !gsym->is_from_dynobj()
1136 && !gsym->is_preemptible())
1138 target->make_plt_entry(symtab, layout, gsym);
1141 case elfcpp::R_X86_64_GOTPC32:
1142 case elfcpp::R_X86_64_GOTOFF64:
1143 case elfcpp::R_X86_64_GOTPC64:
1144 case elfcpp::R_X86_64_PLTOFF64:
1145 // We need a GOT section.
1146 target->got_section(symtab, layout);
1147 // For PLTOFF64, we also need a PLT entry (but only if the
1148 // symbol is not fully resolved).
1149 if (r_type == elfcpp::R_X86_64_PLTOFF64
1150 && !gsym->final_value_is_known())
1151 target->make_plt_entry(symtab, layout, gsym);
1154 case elfcpp::R_X86_64_COPY:
1155 case elfcpp::R_X86_64_GLOB_DAT:
1156 case elfcpp::R_X86_64_JUMP_SLOT:
1157 case elfcpp::R_X86_64_RELATIVE:
1158 // These are outstanding tls relocs, which are unexpected when linking
1159 case elfcpp::R_X86_64_TPOFF64:
1160 case elfcpp::R_X86_64_DTPMOD64:
1161 case elfcpp::R_X86_64_TLSDESC:
1162 gold_error(_("%s: unexpected reloc %u in object file"),
1163 object->name().c_str(), r_type);
1166 // These are initial tls relocs, which are expected for global()
1167 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1168 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1169 case elfcpp::R_X86_64_TLSDESC_CALL:
1170 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1171 case elfcpp::R_X86_64_DTPOFF32:
1172 case elfcpp::R_X86_64_DTPOFF64:
1173 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1174 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1176 const bool is_final = gsym->final_value_is_known();
1177 const tls::Tls_optimization optimized_type
1178 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1181 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1182 if (optimized_type == tls::TLSOPT_NONE)
1184 // Create a pair of GOT entries for the module index and
1185 // dtv-relative offset.
1186 Output_data_got<64, false>* got
1187 = target->got_section(symtab, layout);
1188 got->add_global_tls_with_rela(gsym,
1189 target->rela_dyn_section(layout),
1190 elfcpp::R_X86_64_DTPMOD64,
1191 elfcpp::R_X86_64_DTPOFF64);
1193 else if (optimized_type == tls::TLSOPT_TO_IE)
1195 // Create a GOT entry for the tp-relative offset.
1196 Output_data_got<64, false>* got
1197 = target->got_section(symtab, layout);
1198 got->add_global_with_rela(gsym,
1199 target->rela_dyn_section(layout),
1200 elfcpp::R_X86_64_TPOFF64);
1202 else if (optimized_type != tls::TLSOPT_TO_LE)
1203 unsupported_reloc_global(object, r_type, gsym);
1206 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1207 case elfcpp::R_X86_64_TLSDESC_CALL:
1208 // FIXME: If not relaxing to LE, we need to generate
1209 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1210 if (optimized_type != tls::TLSOPT_TO_LE)
1211 unsupported_reloc_global(object, r_type, gsym);
1214 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1215 if (optimized_type == tls::TLSOPT_NONE)
1217 // Create a GOT entry for the module index.
1218 target->got_mod_index_entry(symtab, layout, object);
1220 else if (optimized_type != tls::TLSOPT_TO_LE)
1221 unsupported_reloc_global(object, r_type, gsym);
1224 case elfcpp::R_X86_64_DTPOFF32:
1225 case elfcpp::R_X86_64_DTPOFF64:
1228 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1229 layout->set_has_static_tls();
1230 if (optimized_type == tls::TLSOPT_NONE)
1232 // Create a GOT entry for the tp-relative offset.
1233 Output_data_got<64, false>* got
1234 = target->got_section(symtab, layout);
1235 got->add_global_with_rela(gsym,
1236 target->rela_dyn_section(layout),
1237 elfcpp::R_X86_64_TPOFF64);
1239 else if (optimized_type != tls::TLSOPT_TO_LE)
1240 unsupported_reloc_global(object, r_type, gsym);
1243 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1244 layout->set_has_static_tls();
1245 if (parameters->output_is_shared())
1246 unsupported_reloc_local(object, r_type);
1255 case elfcpp::R_X86_64_SIZE32:
1256 case elfcpp::R_X86_64_SIZE64:
1258 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1259 object->name().c_str(), r_type,
1260 gsym->demangled_name().c_str());
1265 // Scan relocations for a section.
1268 Target_x86_64::scan_relocs(const General_options& options,
1269 Symbol_table* symtab,
1271 Sized_relobj<64, false>* object,
1272 unsigned int data_shndx,
1273 unsigned int sh_type,
1274 const unsigned char* prelocs,
1276 Output_section* output_section,
1277 bool needs_special_offset_handling,
1278 size_t local_symbol_count,
1279 const unsigned char* plocal_symbols)
1281 if (sh_type == elfcpp::SHT_REL)
1283 gold_error(_("%s: unsupported REL reloc section"),
1284 object->name().c_str());
1288 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1289 Target_x86_64::Scan>(
1299 needs_special_offset_handling,
1304 // Finalize the sections.
1307 Target_x86_64::do_finalize_sections(Layout* layout)
1309 // Fill in some more dynamic tags.
1310 Output_data_dynamic* const odyn = layout->dynamic_data();
1313 if (this->got_plt_ != NULL)
1314 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1316 if (this->plt_ != NULL)
1318 const Output_data* od = this->plt_->rel_plt();
1319 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1320 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1321 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1324 if (this->rela_dyn_ != NULL)
1326 const Output_data* od = this->rela_dyn_;
1327 odyn->add_section_address(elfcpp::DT_RELA, od);
1328 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1329 odyn->add_constant(elfcpp::DT_RELAENT,
1330 elfcpp::Elf_sizes<64>::rela_size);
1333 if (!parameters->output_is_shared())
1335 // The value of the DT_DEBUG tag is filled in by the dynamic
1336 // linker at run time, and used by the debugger.
1337 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1341 // Emit any relocs we saved in an attempt to avoid generating COPY
1343 if (this->copy_relocs_ == NULL)
1345 if (this->copy_relocs_->any_to_emit())
1347 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1348 this->copy_relocs_->emit(rela_dyn);
1350 delete this->copy_relocs_;
1351 this->copy_relocs_ = NULL;
1354 // Perform a relocation.
1357 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1358 Target_x86_64* target,
1360 const elfcpp::Rela<64, false>& rela,
1361 unsigned int r_type,
1362 const Sized_symbol<64>* gsym,
1363 const Symbol_value<64>* psymval,
1364 unsigned char* view,
1365 elfcpp::Elf_types<64>::Elf_Addr address,
1366 section_size_type view_size)
1368 if (this->skip_call_tls_get_addr_)
1370 if (r_type != elfcpp::R_X86_64_PLT32
1372 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1374 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1375 _("missing expected TLS relocation"));
1379 this->skip_call_tls_get_addr_ = false;
1384 // Pick the value to use for symbols defined in shared objects.
1385 Symbol_value<64> symval;
1387 && (gsym->is_from_dynobj()
1388 || (parameters->output_is_shared()
1389 && gsym->is_preemptible()))
1390 && gsym->has_plt_offset())
1392 symval.set_output_value(target->plt_section()->address()
1393 + gsym->plt_offset());
1397 const Sized_relobj<64, false>* object = relinfo->object;
1398 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1400 // Get the GOT offset if needed.
1401 // The GOT pointer points to the end of the GOT section.
1402 // We need to subtract the size of the GOT section to get
1403 // the actual offset to use in the relocation.
1404 bool have_got_offset = false;
1405 unsigned int got_offset = 0;
1408 case elfcpp::R_X86_64_GOT32:
1409 case elfcpp::R_X86_64_GOT64:
1410 case elfcpp::R_X86_64_GOTPLT64:
1411 case elfcpp::R_X86_64_GOTPCREL:
1412 case elfcpp::R_X86_64_GOTPCREL64:
1415 gold_assert(gsym->has_got_offset());
1416 got_offset = gsym->got_offset() - target->got_size();
1420 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1421 gold_assert(object->local_has_got_offset(r_sym));
1422 got_offset = object->local_got_offset(r_sym) - target->got_size();
1424 have_got_offset = true;
1433 case elfcpp::R_X86_64_NONE:
1434 case elfcpp::R_386_GNU_VTINHERIT:
1435 case elfcpp::R_386_GNU_VTENTRY:
1438 case elfcpp::R_X86_64_64:
1439 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1442 case elfcpp::R_X86_64_PC64:
1443 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1447 case elfcpp::R_X86_64_32:
1448 // FIXME: we need to verify that value + addend fits into 32 bits:
1449 // uint64_t x = value + addend;
1450 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1451 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1452 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1455 case elfcpp::R_X86_64_32S:
1456 // FIXME: we need to verify that value + addend fits into 32 bits:
1457 // int64_t x = value + addend; // note this quantity is signed!
1458 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1459 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1462 case elfcpp::R_X86_64_PC32:
1463 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1467 case elfcpp::R_X86_64_16:
1468 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1471 case elfcpp::R_X86_64_PC16:
1472 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1476 case elfcpp::R_X86_64_8:
1477 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1480 case elfcpp::R_X86_64_PC8:
1481 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1485 case elfcpp::R_X86_64_PLT32:
1486 gold_assert(gsym == NULL
1487 || gsym->has_plt_offset()
1488 || gsym->final_value_is_known()
1489 || (gsym->is_defined()
1490 && !gsym->is_from_dynobj()
1491 && !gsym->is_preemptible()));
1492 // Note: while this code looks the same as for R_X86_64_PC32, it
1493 // behaves differently because psymval was set to point to
1494 // the PLT entry, rather than the symbol, in Scan::global().
1495 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1499 case elfcpp::R_X86_64_PLTOFF64:
1502 gold_assert(gsym->has_plt_offset()
1503 || gsym->final_value_is_known());
1504 elfcpp::Elf_types<64>::Elf_Addr got_address;
1505 got_address = target->got_section(NULL, NULL)->address();
1506 Relocate_functions<64, false>::rela64(view, object, psymval,
1507 addend - got_address);
1510 case elfcpp::R_X86_64_GOT32:
1511 gold_assert(have_got_offset);
1512 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1515 case elfcpp::R_X86_64_GOTPC32:
1518 elfcpp::Elf_types<64>::Elf_Addr value;
1519 value = target->got_plt_section()->address();
1520 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1524 case elfcpp::R_X86_64_GOT64:
1525 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1526 // Since we always add a PLT entry, this is equivalent.
1527 case elfcpp::R_X86_64_GOTPLT64:
1528 gold_assert(have_got_offset);
1529 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1532 case elfcpp::R_X86_64_GOTPC64:
1535 elfcpp::Elf_types<64>::Elf_Addr value;
1536 value = target->got_plt_section()->address();
1537 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1541 case elfcpp::R_X86_64_GOTOFF64:
1543 elfcpp::Elf_types<64>::Elf_Addr value;
1544 value = (psymval->value(object, 0)
1545 - target->got_plt_section()->address());
1546 Relocate_functions<64, false>::rela64(view, value, addend);
1550 case elfcpp::R_X86_64_GOTPCREL:
1552 gold_assert(have_got_offset);
1553 elfcpp::Elf_types<64>::Elf_Addr value;
1554 value = target->got_plt_section()->address() + got_offset;
1555 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1559 case elfcpp::R_X86_64_GOTPCREL64:
1561 gold_assert(have_got_offset);
1562 elfcpp::Elf_types<64>::Elf_Addr value;
1563 value = target->got_plt_section()->address() + got_offset;
1564 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1568 case elfcpp::R_X86_64_COPY:
1569 case elfcpp::R_X86_64_GLOB_DAT:
1570 case elfcpp::R_X86_64_JUMP_SLOT:
1571 case elfcpp::R_X86_64_RELATIVE:
1572 // These are outstanding tls relocs, which are unexpected when linking
1573 case elfcpp::R_X86_64_TPOFF64:
1574 case elfcpp::R_X86_64_DTPMOD64:
1575 case elfcpp::R_X86_64_TLSDESC:
1576 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1577 _("unexpected reloc %u in object file"),
1581 // These are initial tls relocs, which are expected when linking
1582 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1583 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1584 case elfcpp::R_X86_64_TLSDESC_CALL:
1585 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1586 case elfcpp::R_X86_64_DTPOFF32:
1587 case elfcpp::R_X86_64_DTPOFF64:
1588 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1589 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1590 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1591 view, address, view_size);
1594 case elfcpp::R_X86_64_SIZE32:
1595 case elfcpp::R_X86_64_SIZE64:
1597 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1598 _("unsupported reloc %u"),
1606 // Perform a TLS relocation.
1609 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1610 Target_x86_64* target,
1612 const elfcpp::Rela<64, false>& rela,
1613 unsigned int r_type,
1614 const Sized_symbol<64>* gsym,
1615 const Symbol_value<64>* psymval,
1616 unsigned char* view,
1617 elfcpp::Elf_types<64>::Elf_Addr address,
1618 section_size_type view_size)
1620 Output_segment* tls_segment = relinfo->layout->tls_segment();
1622 const Sized_relobj<64, false>* object = relinfo->object;
1623 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1625 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1627 const bool is_final = (gsym == NULL
1628 ? !parameters->output_is_position_independent()
1629 : gsym->final_value_is_known());
1630 const tls::Tls_optimization optimized_type
1631 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1634 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1635 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1636 case elfcpp::R_X86_64_TLSDESC_CALL:
1637 if (optimized_type == tls::TLSOPT_TO_LE)
1639 gold_assert(tls_segment != NULL);
1640 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1641 rela, r_type, value, view,
1647 unsigned int got_offset;
1650 gold_assert(gsym->has_tls_got_offset(true));
1651 got_offset = gsym->tls_got_offset(true) - target->got_size();
1655 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1656 gold_assert(object->local_has_tls_got_offset(r_sym, true));
1657 got_offset = (object->local_tls_got_offset(r_sym, true)
1658 - target->got_size());
1660 if (optimized_type == tls::TLSOPT_TO_IE)
1662 gold_assert(tls_segment != NULL);
1663 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1664 got_offset, view, view_size);
1667 else if (optimized_type == tls::TLSOPT_NONE)
1669 // Relocate the field with the offset of the pair of GOT
1671 value = target->got_plt_section()->address() + got_offset;
1672 Relocate_functions<64, false>::pcrela32(view, value, addend,
1677 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1678 _("unsupported reloc %u"), r_type);
1681 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1682 if (optimized_type == tls::TLSOPT_TO_LE)
1684 gold_assert(tls_segment != NULL);
1685 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1686 value, view, view_size);
1689 else if (optimized_type == tls::TLSOPT_NONE)
1691 // Relocate the field with the offset of the GOT entry for
1692 // the module index.
1693 unsigned int got_offset;
1694 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1695 - target->got_size());
1696 value = target->got_plt_section()->address() + got_offset;
1697 Relocate_functions<64, false>::pcrela32(view, value, addend,
1701 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1702 _("unsupported reloc %u"), r_type);
1705 case elfcpp::R_X86_64_DTPOFF32:
1706 gold_assert(tls_segment != NULL);
1707 if (optimized_type == tls::TLSOPT_TO_LE)
1708 value -= tls_segment->memsz();
1709 Relocate_functions<64, false>::rela32(view, value, 0);
1712 case elfcpp::R_X86_64_DTPOFF64:
1713 gold_assert(tls_segment != NULL);
1714 if (optimized_type == tls::TLSOPT_TO_LE)
1715 value -= tls_segment->memsz();
1716 Relocate_functions<64, false>::rela64(view, value, 0);
1719 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1720 if (optimized_type == tls::TLSOPT_TO_LE)
1722 gold_assert(tls_segment != NULL);
1723 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1724 rela, r_type, value, view,
1728 else if (optimized_type == tls::TLSOPT_NONE)
1730 // Relocate the field with the offset of the GOT entry for
1731 // the tp-relative offset of the symbol.
1732 unsigned int got_offset;
1735 gold_assert(gsym->has_got_offset());
1736 got_offset = gsym->got_offset() - target->got_size();
1740 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1741 gold_assert(object->local_has_got_offset(r_sym));
1742 got_offset = (object->local_got_offset(r_sym)
1743 - target->got_size());
1745 value = target->got_plt_section()->address() + got_offset;
1746 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1749 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1750 _("unsupported reloc type %u"),
1754 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1755 value -= tls_segment->memsz();
1756 Relocate_functions<64, false>::rela32(view, value, 0);
1761 // Do a relocation in which we convert a TLS General-Dynamic to an
1765 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1767 Output_segment* tls_segment,
1768 const elfcpp::Rela<64, false>& rela,
1770 elfcpp::Elf_types<64>::Elf_Addr value,
1771 unsigned char* view,
1772 section_size_type view_size)
1774 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1775 // .word 0x6666; rex64; call __tls_get_addr
1776 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1778 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1779 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1781 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1782 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1783 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1784 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1786 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1788 value -= tls_segment->memsz();
1789 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1791 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1793 this->skip_call_tls_get_addr_ = true;
1796 // Do a relocation in which we convert a TLS General-Dynamic to a
1800 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1802 Output_segment* tls_segment,
1803 const elfcpp::Rela<64, false>& rela,
1805 elfcpp::Elf_types<64>::Elf_Addr value,
1806 unsigned char* view,
1807 section_size_type view_size)
1809 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1810 // .word 0x6666; rex64; call __tls_get_addr
1811 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1813 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1814 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1816 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1817 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1818 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1819 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1821 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1823 value -= tls_segment->memsz();
1824 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1826 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1828 this->skip_call_tls_get_addr_ = true;
1832 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1835 const elfcpp::Rela<64, false>& rela,
1837 elfcpp::Elf_types<64>::Elf_Addr,
1838 unsigned char* view,
1839 section_size_type view_size)
1841 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1842 // ... leq foo@dtpoff(%rax),%reg
1843 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1845 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1846 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1848 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1849 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1851 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1853 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1855 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1857 this->skip_call_tls_get_addr_ = true;
1860 // Do a relocation in which we convert a TLS Initial-Exec to a
1864 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1866 Output_segment* tls_segment,
1867 const elfcpp::Rela<64, false>& rela,
1869 elfcpp::Elf_types<64>::Elf_Addr value,
1870 unsigned char* view,
1871 section_size_type view_size)
1873 // We need to examine the opcodes to figure out which instruction we
1876 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1877 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1879 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1880 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1882 unsigned char op1 = view[-3];
1883 unsigned char op2 = view[-2];
1884 unsigned char op3 = view[-1];
1885 unsigned char reg = op3 >> 3;
1893 view[-1] = 0xc0 | reg;
1897 // Special handling for %rsp.
1901 view[-1] = 0xc0 | reg;
1909 view[-1] = 0x80 | reg | (reg << 3);
1912 value -= tls_segment->memsz();
1913 Relocate_functions<64, false>::rela32(view, value, 0);
1916 // Relocate section data.
1919 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1920 unsigned int sh_type,
1921 const unsigned char* prelocs,
1923 Output_section* output_section,
1924 bool needs_special_offset_handling,
1925 unsigned char* view,
1926 elfcpp::Elf_types<64>::Elf_Addr address,
1927 section_size_type view_size)
1929 gold_assert(sh_type == elfcpp::SHT_RELA);
1931 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1932 Target_x86_64::Relocate>(
1938 needs_special_offset_handling,
1944 // Return the value to use for a dynamic which requires special
1945 // treatment. This is how we support equality comparisons of function
1946 // pointers across shared library boundaries, as described in the
1947 // processor specific ABI supplement.
1950 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1952 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1953 return this->plt_section()->address() + gsym->plt_offset();
1956 // Return a string used to fill a code section with nops to take up
1957 // the specified length.
1960 Target_x86_64::do_code_fill(section_size_type length)
1964 // Build a jmpq instruction to skip over the bytes.
1965 unsigned char jmp[5];
1967 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1968 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1969 + std::string(length - 5, '\0'));
1972 // Nop sequences of various lengths.
1973 const char nop1[1] = { 0x90 }; // nop
1974 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1975 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1976 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1977 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1978 0x00 }; // leal 0(%esi,1),%esi
1979 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1981 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1983 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1984 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1985 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1986 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1988 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1989 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1991 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1992 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1994 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1995 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1996 0x00, 0x00, 0x00, 0x00 };
1997 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1998 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1999 0x27, 0x00, 0x00, 0x00,
2001 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2002 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2003 0xbc, 0x27, 0x00, 0x00,
2005 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2006 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2007 0x90, 0x90, 0x90, 0x90,
2010 const char* nops[16] = {
2012 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2013 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2016 return std::string(nops[length], length);
2019 // The selector for x86_64 object files.
2021 class Target_selector_x86_64 : public Target_selector
2024 Target_selector_x86_64()
2025 : Target_selector(elfcpp::EM_X86_64, 64, false)
2029 recognize(int machine, int osabi, int abiversion);
2032 Target_x86_64* target_;
2035 // Recognize an x86_64 object file when we already know that the machine
2036 // number is EM_X86_64.
2039 Target_selector_x86_64::recognize(int, int, int)
2041 if (this->target_ == NULL)
2042 this->target_ = new Target_x86_64();
2043 return this->target_;
2046 Target_selector_x86_64 target_selector_x86_64;
2048 } // End anonymous namespace.