1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 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 modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
46 class Output_data_plt_x86_64;
48 // The x86_64 target class.
50 // http://www.x86-64.org/documentation/abi.pdf
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_x86_64 : public Sized_target<64, false>
58 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
59 // uses only Elf64_Rela relocation entries with explicit addends."
60 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
63 : Sized_target<64, false>(&x86_64_info),
64 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
65 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
66 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
69 // Scan the relocations to look for symbol adjustments.
71 scan_relocs(const General_options& options,
74 Sized_relobj<64, false>* object,
75 unsigned int data_shndx,
77 const unsigned char* prelocs,
79 Output_section* output_section,
80 bool needs_special_offset_handling,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols);
84 // Finalize the sections.
86 do_finalize_sections(Layout*);
88 // Return the value to use for a dynamic which requires special
91 do_dynsym_value(const Symbol*) const;
93 // Relocate a section.
95 relocate_section(const Relocate_info<64, false>*,
97 const unsigned char* prelocs,
99 Output_section* output_section,
100 bool needs_special_offset_handling,
102 elfcpp::Elf_types<64>::Elf_Addr view_address,
103 section_size_type view_size);
105 // Scan the relocs during a relocatable link.
107 scan_relocatable_relocs(const General_options& options,
108 Symbol_table* symtab,
110 Sized_relobj<64, false>* object,
111 unsigned int data_shndx,
112 unsigned int sh_type,
113 const unsigned char* prelocs,
115 Output_section* output_section,
116 bool needs_special_offset_handling,
117 size_t local_symbol_count,
118 const unsigned char* plocal_symbols,
119 Relocatable_relocs*);
121 // Relocate a section during a relocatable link.
123 relocate_for_relocatable(const Relocate_info<64, false>*,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
127 Output_section* output_section,
128 off_t offset_in_output_section,
129 const Relocatable_relocs*,
131 elfcpp::Elf_types<64>::Elf_Addr view_address,
132 section_size_type view_size,
133 unsigned char* reloc_view,
134 section_size_type reloc_view_size);
136 // Return a string used to fill a code section with nops.
138 do_code_fill(section_size_type length) const;
140 // Return whether SYM is defined by the ABI.
142 do_is_defined_by_abi(Symbol* sym) const
143 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
145 // Return the size of the GOT section.
149 gold_assert(this->got_ != NULL);
150 return this->got_->data_size();
154 // The class which scans relocations.
159 : issued_non_pic_error_(false)
163 local(const General_options& options, Symbol_table* symtab,
164 Layout* layout, Target_x86_64* target,
165 Sized_relobj<64, false>* object,
166 unsigned int data_shndx,
167 Output_section* output_section,
168 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
169 const elfcpp::Sym<64, false>& lsym);
172 global(const General_options& options, Symbol_table* symtab,
173 Layout* layout, Target_x86_64* target,
174 Sized_relobj<64, false>* object,
175 unsigned int data_shndx,
176 Output_section* output_section,
177 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
182 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
185 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
189 check_non_pic(Relobj*, unsigned int r_type);
191 // Whether we have issued an error about a non-PIC compilation.
192 bool issued_non_pic_error_;
195 // The class which implements relocation.
200 : skip_call_tls_get_addr_(false)
205 if (this->skip_call_tls_get_addr_)
207 // FIXME: This needs to specify the location somehow.
208 gold_error(_("missing expected TLS relocation"));
212 // Do a relocation. Return false if the caller should not issue
213 // any warnings about this relocation.
215 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
216 const elfcpp::Rela<64, false>&,
217 unsigned int r_type, const Sized_symbol<64>*,
218 const Symbol_value<64>*,
219 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
223 // Do a TLS relocation.
225 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
226 size_t relnum, const elfcpp::Rela<64, false>&,
227 unsigned int r_type, const Sized_symbol<64>*,
228 const Symbol_value<64>*,
229 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
232 // Do a TLS General-Dynamic to Initial-Exec transition.
234 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
235 Output_segment* tls_segment,
236 const elfcpp::Rela<64, false>&, unsigned int r_type,
237 elfcpp::Elf_types<64>::Elf_Addr value,
239 elfcpp::Elf_types<64>::Elf_Addr,
240 section_size_type view_size);
242 // Do a TLS General-Dynamic to Local-Exec transition.
244 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
245 Output_segment* tls_segment,
246 const elfcpp::Rela<64, false>&, unsigned int r_type,
247 elfcpp::Elf_types<64>::Elf_Addr value,
249 section_size_type view_size);
251 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
253 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
254 Output_segment* tls_segment,
255 const elfcpp::Rela<64, false>&, unsigned int r_type,
256 elfcpp::Elf_types<64>::Elf_Addr value,
258 elfcpp::Elf_types<64>::Elf_Addr,
259 section_size_type view_size);
261 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
263 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
264 Output_segment* tls_segment,
265 const elfcpp::Rela<64, false>&, unsigned int r_type,
266 elfcpp::Elf_types<64>::Elf_Addr value,
268 section_size_type view_size);
270 // Do a TLS Local-Dynamic to Local-Exec transition.
272 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
273 Output_segment* tls_segment,
274 const elfcpp::Rela<64, false>&, unsigned int r_type,
275 elfcpp::Elf_types<64>::Elf_Addr value,
277 section_size_type view_size);
279 // Do a TLS Initial-Exec to Local-Exec transition.
281 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
282 Output_segment* tls_segment,
283 const elfcpp::Rela<64, false>&, unsigned int r_type,
284 elfcpp::Elf_types<64>::Elf_Addr value,
286 section_size_type view_size);
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_;
293 // A class which returns the size required for a relocation type,
294 // used while scanning relocs during a relocatable link.
295 class Relocatable_size_for_reloc
299 get_size_for_reloc(unsigned int, Relobj*);
302 // Adjust TLS relocation type based on the options and whether this
303 // is a local symbol.
304 static tls::Tls_optimization
305 optimize_tls_reloc(bool is_final, int r_type);
307 // Get the GOT section, creating it if necessary.
308 Output_data_got<64, false>*
309 got_section(Symbol_table*, Layout*);
311 // Get the GOT PLT section.
313 got_plt_section() const
315 gold_assert(this->got_plt_ != NULL);
316 return this->got_plt_;
319 // Create the PLT section.
321 make_plt_section(Symbol_table* symtab, Layout* layout);
323 // Create a PLT entry for a global symbol.
325 make_plt_entry(Symbol_table*, Layout*, Symbol*);
327 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
329 define_tls_base_symbol(Symbol_table*, Layout*);
331 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
333 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
335 // Create a GOT entry for the TLS module index.
337 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
338 Sized_relobj<64, false>* object);
340 // Get the PLT section.
341 Output_data_plt_x86_64*
344 gold_assert(this->plt_ != NULL);
348 // Get the dynamic reloc section, creating it if necessary.
350 rela_dyn_section(Layout*);
352 // Return true if the symbol may need a COPY relocation.
353 // References from an executable object to non-function symbols
354 // defined in a dynamic object may need a COPY relocation.
356 may_need_copy_reloc(Symbol* gsym)
358 return (!parameters->options().shared()
359 && gsym->is_from_dynobj()
360 && gsym->type() != elfcpp::STT_FUNC);
363 // Add a potential copy relocation.
365 copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object,
366 unsigned int shndx, Output_section* output_section,
367 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
369 this->copy_relocs_.copy_reloc(symtab, layout,
370 symtab->get_sized_symbol<64>(sym),
371 object, shndx, output_section,
372 reloc, this->rela_dyn_section(layout));
375 // Information about this specific target which we pass to the
376 // general Target structure.
377 static const Target::Target_info x86_64_info;
381 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
382 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
383 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
384 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
388 Output_data_got<64, false>* got_;
390 Output_data_plt_x86_64* plt_;
391 // The GOT PLT section.
392 Output_data_space* got_plt_;
393 // The dynamic reloc section.
394 Reloc_section* rela_dyn_;
395 // Relocs saved to avoid a COPY reloc.
396 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
397 // Space for variables copied with a COPY reloc.
398 Output_data_space* dynbss_;
399 // Offset of the GOT entry for the TLS module index.
400 unsigned int got_mod_index_offset_;
401 // True if the _TLS_MODULE_BASE_ symbol has been defined.
402 bool tls_base_symbol_defined_;
405 const Target::Target_info Target_x86_64::x86_64_info =
408 false, // is_big_endian
409 elfcpp::EM_X86_64, // machine_code
410 false, // has_make_symbol
411 false, // has_resolve
412 true, // has_code_fill
413 true, // is_default_stack_executable
415 "/lib/ld64.so.1", // program interpreter
416 0x400000, // default_text_segment_address
417 0x1000, // abi_pagesize (overridable by -z max-page-size)
418 0x1000 // common_pagesize (overridable by -z common-page-size)
421 // Get the GOT section, creating it if necessary.
423 Output_data_got<64, false>*
424 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
426 if (this->got_ == NULL)
428 gold_assert(symtab != NULL && layout != NULL);
430 this->got_ = new Output_data_got<64, false>();
432 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
433 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
436 // The old GNU linker creates a .got.plt section. We just
437 // create another set of data in the .got section. Note that we
438 // always create a PLT if we create a GOT, although the PLT
440 this->got_plt_ = new Output_data_space(8);
441 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
442 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
445 // The first three entries are reserved.
446 this->got_plt_->set_current_data_size(3 * 8);
448 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
449 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
451 0, 0, elfcpp::STT_OBJECT,
453 elfcpp::STV_HIDDEN, 0,
460 // Get the dynamic reloc section, creating it if necessary.
462 Target_x86_64::Reloc_section*
463 Target_x86_64::rela_dyn_section(Layout* layout)
465 if (this->rela_dyn_ == NULL)
467 gold_assert(layout != NULL);
468 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
469 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
470 elfcpp::SHF_ALLOC, this->rela_dyn_);
472 return this->rela_dyn_;
475 // A class to handle the PLT data.
477 class Output_data_plt_x86_64 : public Output_section_data
480 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
482 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
485 // Add an entry to the PLT.
487 add_entry(Symbol* gsym);
489 // Add the reserved TLSDESC_PLT entry to the PLT.
491 reserve_tlsdesc_entry(unsigned int got_offset)
492 { this->tlsdesc_got_offset_ = got_offset; }
494 // Return true if a TLSDESC_PLT entry has been reserved.
496 has_tlsdesc_entry() const
497 { return this->tlsdesc_got_offset_ != -1U; }
499 // Return the GOT offset for the reserved TLSDESC_PLT entry.
501 get_tlsdesc_got_offset() const
502 { return this->tlsdesc_got_offset_; }
504 // Return the offset of the reserved TLSDESC_PLT entry.
506 get_tlsdesc_plt_offset() const
507 { return (this->count_ + 1) * plt_entry_size; }
509 // Return the .rel.plt section data.
512 { return this->rel_; }
516 do_adjust_output_section(Output_section* os);
519 // The size of an entry in the PLT.
520 static const int plt_entry_size = 16;
522 // The first entry in the PLT.
523 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
524 // procedure linkage table for both programs and shared objects."
525 static unsigned char first_plt_entry[plt_entry_size];
527 // Other entries in the PLT for an executable.
528 static unsigned char plt_entry[plt_entry_size];
530 // The reserved TLSDESC entry in the PLT for an executable.
531 static unsigned char tlsdesc_plt_entry[plt_entry_size];
533 // Set the final size.
535 set_final_data_size();
537 // Write out the PLT data.
539 do_write(Output_file*);
541 // The reloc section.
544 Output_data_got<64, false>* got_;
545 // The .got.plt section.
546 Output_data_space* got_plt_;
547 // The number of PLT entries.
549 // Offset of the reserved TLSDESC_GOT entry when needed.
550 unsigned int tlsdesc_got_offset_;
553 // Create the PLT section. The ordinary .got section is an argument,
554 // since we need to refer to the start. We also create our own .got
555 // section just for PLT entries.
557 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
558 Output_data_got<64, false>* got,
559 Output_data_space* got_plt)
560 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
561 tlsdesc_got_offset_(-1U)
563 this->rel_ = new Reloc_section(false);
564 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
565 elfcpp::SHF_ALLOC, this->rel_);
569 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
571 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
572 // linker, and so do we.
576 // Add an entry to the PLT.
579 Output_data_plt_x86_64::add_entry(Symbol* gsym)
581 gold_assert(!gsym->has_plt_offset());
583 // Note that when setting the PLT offset we skip the initial
584 // reserved PLT entry.
585 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
589 section_offset_type got_offset = this->got_plt_->current_data_size();
591 // Every PLT entry needs a GOT entry which points back to the PLT
592 // entry (this will be changed by the dynamic linker, normally
593 // lazily when the function is called).
594 this->got_plt_->set_current_data_size(got_offset + 8);
596 // Every PLT entry needs a reloc.
597 gsym->set_needs_dynsym_entry();
598 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
601 // Note that we don't need to save the symbol. The contents of the
602 // PLT are independent of which symbols are used. The symbols only
603 // appear in the relocations.
606 // Set the final size.
608 Output_data_plt_x86_64::set_final_data_size()
610 unsigned int count = this->count_;
611 if (this->has_tlsdesc_entry())
613 this->set_data_size((count + 1) * plt_entry_size);
616 // The first entry in the PLT for an executable.
618 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
620 // From AMD64 ABI Draft 0.98, page 76
621 0xff, 0x35, // pushq contents of memory address
622 0, 0, 0, 0, // replaced with address of .got + 8
623 0xff, 0x25, // jmp indirect
624 0, 0, 0, 0, // replaced with address of .got + 16
625 0x90, 0x90, 0x90, 0x90 // noop (x4)
628 // Subsequent entries in the PLT for an executable.
630 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
632 // From AMD64 ABI Draft 0.98, page 76
633 0xff, 0x25, // jmpq indirect
634 0, 0, 0, 0, // replaced with address of symbol in .got
635 0x68, // pushq immediate
636 0, 0, 0, 0, // replaced with offset into relocation table
637 0xe9, // jmpq relative
638 0, 0, 0, 0 // replaced with offset to start of .plt
641 // The reserved TLSDESC entry in the PLT for an executable.
643 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
645 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
646 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
647 0xff, 0x35, // pushq x(%rip)
648 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
649 0xff, 0x25, // jmpq *y(%rip)
650 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
655 // Write out the PLT. This uses the hand-coded instructions above,
656 // and adjusts them as needed. This is specified by the AMD64 ABI.
659 Output_data_plt_x86_64::do_write(Output_file* of)
661 const off_t offset = this->offset();
662 const section_size_type oview_size =
663 convert_to_section_size_type(this->data_size());
664 unsigned char* const oview = of->get_output_view(offset, oview_size);
666 const off_t got_file_offset = this->got_plt_->offset();
667 const section_size_type got_size =
668 convert_to_section_size_type(this->got_plt_->data_size());
669 unsigned char* const got_view = of->get_output_view(got_file_offset,
672 unsigned char* pov = oview;
674 // The base address of the .plt section.
675 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
676 // The base address of the .got section.
677 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
678 // The base address of the PLT portion of the .got section,
679 // which is where the GOT pointer will point, and where the
680 // three reserved GOT entries are located.
681 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
683 memcpy(pov, first_plt_entry, plt_entry_size);
684 // We do a jmp relative to the PC at the end of this instruction.
685 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
687 - (plt_address + 6)));
688 elfcpp::Swap<32, false>::writeval(pov + 8,
690 - (plt_address + 12)));
691 pov += plt_entry_size;
693 unsigned char* got_pov = got_view;
695 memset(got_pov, 0, 24);
698 unsigned int plt_offset = plt_entry_size;
699 unsigned int got_offset = 24;
700 const unsigned int count = this->count_;
701 for (unsigned int plt_index = 0;
704 pov += plt_entry_size,
706 plt_offset += plt_entry_size,
709 // Set and adjust the PLT entry itself.
710 memcpy(pov, plt_entry, plt_entry_size);
711 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
712 (got_address + got_offset
713 - (plt_address + plt_offset
716 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
717 elfcpp::Swap<32, false>::writeval(pov + 12,
718 - (plt_offset + plt_entry_size));
720 // Set the entry in the GOT.
721 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
724 if (this->has_tlsdesc_entry())
726 // Set and adjust the reserved TLSDESC PLT entry.
727 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
728 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
729 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
731 - (plt_address + plt_offset
733 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
736 - (plt_address + plt_offset
738 pov += plt_entry_size;
741 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
742 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
744 of->write_output_view(offset, oview_size, oview);
745 of->write_output_view(got_file_offset, got_size, got_view);
748 // Create the PLT section.
751 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
753 if (this->plt_ == NULL)
755 // Create the GOT sections first.
756 this->got_section(symtab, layout);
758 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
760 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
762 | elfcpp::SHF_EXECINSTR),
767 // Create a PLT entry for a global symbol.
770 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
773 if (gsym->has_plt_offset())
776 if (this->plt_ == NULL)
777 this->make_plt_section(symtab, layout);
779 this->plt_->add_entry(gsym);
782 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
785 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
787 if (this->tls_base_symbol_defined_)
790 Output_segment* tls_segment = layout->tls_segment();
791 if (tls_segment != NULL)
793 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
797 elfcpp::STV_HIDDEN, 0,
798 Symbol::SEGMENT_END, true);
800 this->tls_base_symbol_defined_ = true;
803 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
806 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
809 if (this->plt_ == NULL)
810 this->make_plt_section(symtab, layout);
812 if (!this->plt_->has_tlsdesc_entry())
814 // Allocate the TLSDESC_GOT entry.
815 Output_data_got<64, false>* got = this->got_section(symtab, layout);
816 unsigned int got_offset = got->add_constant(0);
818 // Allocate the TLSDESC_PLT entry.
819 this->plt_->reserve_tlsdesc_entry(got_offset);
823 // Create a GOT entry for the TLS module index.
826 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
827 Sized_relobj<64, false>* object)
829 if (this->got_mod_index_offset_ == -1U)
831 gold_assert(symtab != NULL && layout != NULL && object != NULL);
832 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
833 Output_data_got<64, false>* got = this->got_section(symtab, layout);
834 unsigned int got_offset = got->add_constant(0);
835 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
837 got->add_constant(0);
838 this->got_mod_index_offset_ = got_offset;
840 return this->got_mod_index_offset_;
843 // Optimize the TLS relocation type based on what we know about the
844 // symbol. IS_FINAL is true if the final address of this symbol is
845 // known at link time.
847 tls::Tls_optimization
848 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
850 // If we are generating a shared library, then we can't do anything
852 if (parameters->options().shared())
853 return tls::TLSOPT_NONE;
857 case elfcpp::R_X86_64_TLSGD:
858 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
859 case elfcpp::R_X86_64_TLSDESC_CALL:
860 // These are General-Dynamic which permits fully general TLS
861 // access. Since we know that we are generating an executable,
862 // we can convert this to Initial-Exec. If we also know that
863 // this is a local symbol, we can further switch to Local-Exec.
865 return tls::TLSOPT_TO_LE;
866 return tls::TLSOPT_TO_IE;
868 case elfcpp::R_X86_64_TLSLD:
869 // This is Local-Dynamic, which refers to a local symbol in the
870 // dynamic TLS block. Since we know that we generating an
871 // executable, we can switch to Local-Exec.
872 return tls::TLSOPT_TO_LE;
874 case elfcpp::R_X86_64_DTPOFF32:
875 case elfcpp::R_X86_64_DTPOFF64:
876 // Another Local-Dynamic reloc.
877 return tls::TLSOPT_TO_LE;
879 case elfcpp::R_X86_64_GOTTPOFF:
880 // These are Initial-Exec relocs which get the thread offset
881 // from the GOT. If we know that we are linking against the
882 // local symbol, we can switch to Local-Exec, which links the
883 // thread offset into the instruction.
885 return tls::TLSOPT_TO_LE;
886 return tls::TLSOPT_NONE;
888 case elfcpp::R_X86_64_TPOFF32:
889 // When we already have Local-Exec, there is nothing further we
891 return tls::TLSOPT_NONE;
898 // Report an unsupported relocation against a local symbol.
901 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
904 gold_error(_("%s: unsupported reloc %u against local symbol"),
905 object->name().c_str(), r_type);
908 // We are about to emit a dynamic relocation of type R_TYPE. If the
909 // dynamic linker does not support it, issue an error. The GNU linker
910 // only issues a non-PIC error for an allocated read-only section.
911 // Here we know the section is allocated, but we don't know that it is
912 // read-only. But we check for all the relocation types which the
913 // glibc dynamic linker supports, so it seems appropriate to issue an
914 // error even if the section is not read-only.
917 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
921 // These are the relocation types supported by glibc for x86_64.
922 case elfcpp::R_X86_64_RELATIVE:
923 case elfcpp::R_X86_64_GLOB_DAT:
924 case elfcpp::R_X86_64_JUMP_SLOT:
925 case elfcpp::R_X86_64_DTPMOD64:
926 case elfcpp::R_X86_64_DTPOFF64:
927 case elfcpp::R_X86_64_TPOFF64:
928 case elfcpp::R_X86_64_64:
929 case elfcpp::R_X86_64_32:
930 case elfcpp::R_X86_64_PC32:
931 case elfcpp::R_X86_64_COPY:
935 // This prevents us from issuing more than one error per reloc
936 // section. But we can still wind up issuing more than one
937 // error per object file.
938 if (this->issued_non_pic_error_)
940 object->error(_("requires unsupported dynamic reloc; "
941 "recompile with -fPIC"));
942 this->issued_non_pic_error_ = true;
945 case elfcpp::R_X86_64_NONE:
950 // Scan a relocation for a local symbol.
953 Target_x86_64::Scan::local(const General_options&,
954 Symbol_table* symtab,
956 Target_x86_64* target,
957 Sized_relobj<64, false>* object,
958 unsigned int data_shndx,
959 Output_section* output_section,
960 const elfcpp::Rela<64, false>& reloc,
962 const elfcpp::Sym<64, false>& lsym)
966 case elfcpp::R_X86_64_NONE:
967 case elfcpp::R_386_GNU_VTINHERIT:
968 case elfcpp::R_386_GNU_VTENTRY:
971 case elfcpp::R_X86_64_64:
972 // If building a shared library (or a position-independent
973 // executable), we need to create a dynamic relocation for this
974 // location. The relocation applied at link time will apply the
975 // link-time value, so we flag the location with an
976 // R_X86_64_RELATIVE relocation so the dynamic loader can
977 // relocate it easily.
978 if (parameters->options().output_is_position_independent())
980 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
981 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
982 rela_dyn->add_local_relative(object, r_sym,
983 elfcpp::R_X86_64_RELATIVE,
984 output_section, data_shndx,
985 reloc.get_r_offset(),
986 reloc.get_r_addend());
990 case elfcpp::R_X86_64_32:
991 case elfcpp::R_X86_64_32S:
992 case elfcpp::R_X86_64_16:
993 case elfcpp::R_X86_64_8:
994 // If building a shared library (or a position-independent
995 // executable), we need to create a dynamic relocation for this
996 // location. We can't use an R_X86_64_RELATIVE relocation
997 // because that is always a 64-bit relocation.
998 if (parameters->options().output_is_position_independent())
1000 this->check_non_pic(object, r_type);
1002 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1003 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1004 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1005 rela_dyn->add_local(object, r_sym, r_type, output_section,
1006 data_shndx, reloc.get_r_offset(),
1007 reloc.get_r_addend());
1010 gold_assert(lsym.get_st_value() == 0);
1011 unsigned int shndx = lsym.get_st_shndx();
1013 shndx = object->adjust_sym_shndx(r_sym, shndx,
1016 object->error(_("section symbol %u has bad shndx %u"),
1019 rela_dyn->add_local_section(object, shndx,
1020 r_type, output_section,
1021 data_shndx, reloc.get_r_offset(),
1022 reloc.get_r_addend());
1027 case elfcpp::R_X86_64_PC64:
1028 case elfcpp::R_X86_64_PC32:
1029 case elfcpp::R_X86_64_PC16:
1030 case elfcpp::R_X86_64_PC8:
1033 case elfcpp::R_X86_64_PLT32:
1034 // Since we know this is a local symbol, we can handle this as a
1038 case elfcpp::R_X86_64_GOTPC32:
1039 case elfcpp::R_X86_64_GOTOFF64:
1040 case elfcpp::R_X86_64_GOTPC64:
1041 case elfcpp::R_X86_64_PLTOFF64:
1042 // We need a GOT section.
1043 target->got_section(symtab, layout);
1044 // For PLTOFF64, we'd normally want a PLT section, but since we
1045 // know this is a local symbol, no PLT is needed.
1048 case elfcpp::R_X86_64_GOT64:
1049 case elfcpp::R_X86_64_GOT32:
1050 case elfcpp::R_X86_64_GOTPCREL64:
1051 case elfcpp::R_X86_64_GOTPCREL:
1052 case elfcpp::R_X86_64_GOTPLT64:
1054 // The symbol requires a GOT entry.
1055 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1056 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1057 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1059 // If we are generating a shared object, we need to add a
1060 // dynamic relocation for this symbol's GOT entry.
1061 if (parameters->options().output_is_position_independent())
1063 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1064 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1065 if (r_type != elfcpp::R_X86_64_GOT32)
1066 rela_dyn->add_local_relative(
1067 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1068 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1071 this->check_non_pic(object, r_type);
1073 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1074 rela_dyn->add_local(
1075 object, r_sym, r_type, got,
1076 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1080 // For GOTPLT64, we'd normally want a PLT section, but since
1081 // we know this is a local symbol, no PLT is needed.
1085 case elfcpp::R_X86_64_COPY:
1086 case elfcpp::R_X86_64_GLOB_DAT:
1087 case elfcpp::R_X86_64_JUMP_SLOT:
1088 case elfcpp::R_X86_64_RELATIVE:
1089 // These are outstanding tls relocs, which are unexpected when linking
1090 case elfcpp::R_X86_64_TPOFF64:
1091 case elfcpp::R_X86_64_DTPMOD64:
1092 case elfcpp::R_X86_64_TLSDESC:
1093 gold_error(_("%s: unexpected reloc %u in object file"),
1094 object->name().c_str(), r_type);
1097 // These are initial tls relocs, which are expected when linking
1098 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1099 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1100 case elfcpp::R_X86_64_TLSDESC_CALL:
1101 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1102 case elfcpp::R_X86_64_DTPOFF32:
1103 case elfcpp::R_X86_64_DTPOFF64:
1104 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1105 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1107 bool output_is_shared = parameters->options().shared();
1108 const tls::Tls_optimization optimized_type
1109 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1112 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1113 if (optimized_type == tls::TLSOPT_NONE)
1115 // Create a pair of GOT entries for the module index and
1116 // dtv-relative offset.
1117 Output_data_got<64, false>* got
1118 = target->got_section(symtab, layout);
1119 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1120 unsigned int shndx = lsym.get_st_shndx();
1122 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1124 object->error(_("local symbol %u has bad shndx %u"),
1127 got->add_local_pair_with_rela(object, r_sym,
1130 target->rela_dyn_section(layout),
1131 elfcpp::R_X86_64_DTPMOD64, 0);
1133 else if (optimized_type != tls::TLSOPT_TO_LE)
1134 unsupported_reloc_local(object, r_type);
1137 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1138 target->define_tls_base_symbol(symtab, layout);
1139 if (optimized_type == tls::TLSOPT_NONE)
1141 // Create reserved PLT and GOT entries for the resolver.
1142 target->reserve_tlsdesc_entries(symtab, layout);
1144 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1145 Output_data_got<64, false>* got
1146 = target->got_section(symtab, layout);
1147 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1148 unsigned int shndx = lsym.get_st_shndx();
1150 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1152 object->error(_("local symbol %u has bad shndx %u"),
1155 got->add_local_pair_with_rela(object, r_sym,
1158 target->rela_dyn_section(layout),
1159 elfcpp::R_X86_64_TLSDESC, 0);
1161 else if (optimized_type != tls::TLSOPT_TO_LE)
1162 unsupported_reloc_local(object, r_type);
1165 case elfcpp::R_X86_64_TLSDESC_CALL:
1168 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1169 if (optimized_type == tls::TLSOPT_NONE)
1171 // Create a GOT entry for the module index.
1172 target->got_mod_index_entry(symtab, layout, object);
1174 else if (optimized_type != tls::TLSOPT_TO_LE)
1175 unsupported_reloc_local(object, r_type);
1178 case elfcpp::R_X86_64_DTPOFF32:
1179 case elfcpp::R_X86_64_DTPOFF64:
1182 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1183 layout->set_has_static_tls();
1184 if (optimized_type == tls::TLSOPT_NONE)
1186 // Create a GOT entry for the tp-relative offset.
1187 Output_data_got<64, false>* got
1188 = target->got_section(symtab, layout);
1189 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1190 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1191 target->rela_dyn_section(layout),
1192 elfcpp::R_X86_64_TPOFF64);
1194 else if (optimized_type != tls::TLSOPT_TO_LE)
1195 unsupported_reloc_local(object, r_type);
1198 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1199 layout->set_has_static_tls();
1200 if (output_is_shared)
1201 unsupported_reloc_local(object, r_type);
1210 case elfcpp::R_X86_64_SIZE32:
1211 case elfcpp::R_X86_64_SIZE64:
1213 gold_error(_("%s: unsupported reloc %u against local symbol"),
1214 object->name().c_str(), r_type);
1220 // Report an unsupported relocation against a global symbol.
1223 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1224 unsigned int r_type,
1227 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1228 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1231 // Scan a relocation for a global symbol.
1234 Target_x86_64::Scan::global(const General_options&,
1235 Symbol_table* symtab,
1237 Target_x86_64* target,
1238 Sized_relobj<64, false>* object,
1239 unsigned int data_shndx,
1240 Output_section* output_section,
1241 const elfcpp::Rela<64, false>& reloc,
1242 unsigned int r_type,
1247 case elfcpp::R_X86_64_NONE:
1248 case elfcpp::R_386_GNU_VTINHERIT:
1249 case elfcpp::R_386_GNU_VTENTRY:
1252 case elfcpp::R_X86_64_64:
1253 case elfcpp::R_X86_64_32:
1254 case elfcpp::R_X86_64_32S:
1255 case elfcpp::R_X86_64_16:
1256 case elfcpp::R_X86_64_8:
1258 // Make a PLT entry if necessary.
1259 if (gsym->needs_plt_entry())
1261 target->make_plt_entry(symtab, layout, gsym);
1262 // Since this is not a PC-relative relocation, we may be
1263 // taking the address of a function. In that case we need to
1264 // set the entry in the dynamic symbol table to the address of
1266 if (gsym->is_from_dynobj() && !parameters->options().shared())
1267 gsym->set_needs_dynsym_value();
1269 // Make a dynamic relocation if necessary.
1270 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1272 if (target->may_need_copy_reloc(gsym))
1274 target->copy_reloc(symtab, layout, object,
1275 data_shndx, output_section, gsym, reloc);
1277 else if (r_type == elfcpp::R_X86_64_64
1278 && gsym->can_use_relative_reloc(false))
1280 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1281 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1282 output_section, object,
1283 data_shndx, reloc.get_r_offset(),
1284 reloc.get_r_addend());
1288 this->check_non_pic(object, r_type);
1289 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1290 rela_dyn->add_global(gsym, r_type, output_section, object,
1291 data_shndx, reloc.get_r_offset(),
1292 reloc.get_r_addend());
1298 case elfcpp::R_X86_64_PC64:
1299 case elfcpp::R_X86_64_PC32:
1300 case elfcpp::R_X86_64_PC16:
1301 case elfcpp::R_X86_64_PC8:
1303 // Make a PLT entry if necessary.
1304 if (gsym->needs_plt_entry())
1305 target->make_plt_entry(symtab, layout, gsym);
1306 // Make a dynamic relocation if necessary.
1307 int flags = Symbol::NON_PIC_REF;
1308 if (gsym->type() == elfcpp::STT_FUNC)
1309 flags |= Symbol::FUNCTION_CALL;
1310 if (gsym->needs_dynamic_reloc(flags))
1312 if (target->may_need_copy_reloc(gsym))
1314 target->copy_reloc(symtab, layout, object,
1315 data_shndx, output_section, gsym, reloc);
1319 this->check_non_pic(object, r_type);
1320 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1321 rela_dyn->add_global(gsym, r_type, output_section, object,
1322 data_shndx, reloc.get_r_offset(),
1323 reloc.get_r_addend());
1329 case elfcpp::R_X86_64_GOT64:
1330 case elfcpp::R_X86_64_GOT32:
1331 case elfcpp::R_X86_64_GOTPCREL64:
1332 case elfcpp::R_X86_64_GOTPCREL:
1333 case elfcpp::R_X86_64_GOTPLT64:
1335 // The symbol requires a GOT entry.
1336 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1337 if (gsym->final_value_is_known())
1338 got->add_global(gsym, GOT_TYPE_STANDARD);
1341 // If this symbol is not fully resolved, we need to add a
1342 // dynamic relocation for it.
1343 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1344 if (gsym->is_from_dynobj()
1345 || gsym->is_undefined()
1346 || gsym->is_preemptible())
1347 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1348 elfcpp::R_X86_64_GLOB_DAT);
1351 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1352 rela_dyn->add_global_relative(
1353 gsym, elfcpp::R_X86_64_RELATIVE, got,
1354 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1357 // For GOTPLT64, we also need a PLT entry (but only if the
1358 // symbol is not fully resolved).
1359 if (r_type == elfcpp::R_X86_64_GOTPLT64
1360 && !gsym->final_value_is_known())
1361 target->make_plt_entry(symtab, layout, gsym);
1365 case elfcpp::R_X86_64_PLT32:
1366 // If the symbol is fully resolved, this is just a PC32 reloc.
1367 // Otherwise we need a PLT entry.
1368 if (gsym->final_value_is_known())
1370 // If building a shared library, we can also skip the PLT entry
1371 // if the symbol is defined in the output file and is protected
1373 if (gsym->is_defined()
1374 && !gsym->is_from_dynobj()
1375 && !gsym->is_preemptible())
1377 target->make_plt_entry(symtab, layout, gsym);
1380 case elfcpp::R_X86_64_GOTPC32:
1381 case elfcpp::R_X86_64_GOTOFF64:
1382 case elfcpp::R_X86_64_GOTPC64:
1383 case elfcpp::R_X86_64_PLTOFF64:
1384 // We need a GOT section.
1385 target->got_section(symtab, layout);
1386 // For PLTOFF64, we also need a PLT entry (but only if the
1387 // symbol is not fully resolved).
1388 if (r_type == elfcpp::R_X86_64_PLTOFF64
1389 && !gsym->final_value_is_known())
1390 target->make_plt_entry(symtab, layout, gsym);
1393 case elfcpp::R_X86_64_COPY:
1394 case elfcpp::R_X86_64_GLOB_DAT:
1395 case elfcpp::R_X86_64_JUMP_SLOT:
1396 case elfcpp::R_X86_64_RELATIVE:
1397 // These are outstanding tls relocs, which are unexpected when linking
1398 case elfcpp::R_X86_64_TPOFF64:
1399 case elfcpp::R_X86_64_DTPMOD64:
1400 case elfcpp::R_X86_64_TLSDESC:
1401 gold_error(_("%s: unexpected reloc %u in object file"),
1402 object->name().c_str(), r_type);
1405 // These are initial tls relocs, which are expected for global()
1406 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1407 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1408 case elfcpp::R_X86_64_TLSDESC_CALL:
1409 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1410 case elfcpp::R_X86_64_DTPOFF32:
1411 case elfcpp::R_X86_64_DTPOFF64:
1412 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1413 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1415 const bool is_final = gsym->final_value_is_known();
1416 const tls::Tls_optimization optimized_type
1417 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1420 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1421 if (optimized_type == tls::TLSOPT_NONE)
1423 // Create a pair of GOT entries for the module index and
1424 // dtv-relative offset.
1425 Output_data_got<64, false>* got
1426 = target->got_section(symtab, layout);
1427 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1428 target->rela_dyn_section(layout),
1429 elfcpp::R_X86_64_DTPMOD64,
1430 elfcpp::R_X86_64_DTPOFF64);
1432 else if (optimized_type == tls::TLSOPT_TO_IE)
1434 // Create a GOT entry for the tp-relative offset.
1435 Output_data_got<64, false>* got
1436 = target->got_section(symtab, layout);
1437 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1438 target->rela_dyn_section(layout),
1439 elfcpp::R_X86_64_TPOFF64);
1441 else if (optimized_type != tls::TLSOPT_TO_LE)
1442 unsupported_reloc_global(object, r_type, gsym);
1445 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1446 target->define_tls_base_symbol(symtab, layout);
1447 if (optimized_type == tls::TLSOPT_NONE)
1449 // Create reserved PLT and GOT entries for the resolver.
1450 target->reserve_tlsdesc_entries(symtab, layout);
1452 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1453 Output_data_got<64, false>* got
1454 = target->got_section(symtab, layout);
1455 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1456 target->rela_dyn_section(layout),
1457 elfcpp::R_X86_64_TLSDESC, 0);
1459 else if (optimized_type == tls::TLSOPT_TO_IE)
1461 // Create a GOT entry for the tp-relative offset.
1462 Output_data_got<64, false>* got
1463 = target->got_section(symtab, layout);
1464 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1465 target->rela_dyn_section(layout),
1466 elfcpp::R_X86_64_TPOFF64);
1468 else if (optimized_type != tls::TLSOPT_TO_LE)
1469 unsupported_reloc_global(object, r_type, gsym);
1472 case elfcpp::R_X86_64_TLSDESC_CALL:
1475 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1476 if (optimized_type == tls::TLSOPT_NONE)
1478 // Create a GOT entry for the module index.
1479 target->got_mod_index_entry(symtab, layout, object);
1481 else if (optimized_type != tls::TLSOPT_TO_LE)
1482 unsupported_reloc_global(object, r_type, gsym);
1485 case elfcpp::R_X86_64_DTPOFF32:
1486 case elfcpp::R_X86_64_DTPOFF64:
1489 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1490 layout->set_has_static_tls();
1491 if (optimized_type == tls::TLSOPT_NONE)
1493 // Create a GOT entry for the tp-relative offset.
1494 Output_data_got<64, false>* got
1495 = target->got_section(symtab, layout);
1496 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1497 target->rela_dyn_section(layout),
1498 elfcpp::R_X86_64_TPOFF64);
1500 else if (optimized_type != tls::TLSOPT_TO_LE)
1501 unsupported_reloc_global(object, r_type, gsym);
1504 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1505 layout->set_has_static_tls();
1506 if (parameters->options().shared())
1507 unsupported_reloc_local(object, r_type);
1516 case elfcpp::R_X86_64_SIZE32:
1517 case elfcpp::R_X86_64_SIZE64:
1519 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1520 object->name().c_str(), r_type,
1521 gsym->demangled_name().c_str());
1526 // Scan relocations for a section.
1529 Target_x86_64::scan_relocs(const General_options& options,
1530 Symbol_table* symtab,
1532 Sized_relobj<64, false>* object,
1533 unsigned int data_shndx,
1534 unsigned int sh_type,
1535 const unsigned char* prelocs,
1537 Output_section* output_section,
1538 bool needs_special_offset_handling,
1539 size_t local_symbol_count,
1540 const unsigned char* plocal_symbols)
1542 if (sh_type == elfcpp::SHT_REL)
1544 gold_error(_("%s: unsupported REL reloc section"),
1545 object->name().c_str());
1549 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1550 Target_x86_64::Scan>(
1560 needs_special_offset_handling,
1565 // Finalize the sections.
1568 Target_x86_64::do_finalize_sections(Layout* layout)
1570 // Fill in some more dynamic tags.
1571 Output_data_dynamic* const odyn = layout->dynamic_data();
1574 if (this->got_plt_ != NULL)
1575 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1577 if (this->plt_ != NULL)
1579 const Output_data* od = this->plt_->rel_plt();
1580 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1581 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1582 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1583 if (this->plt_->has_tlsdesc_entry())
1585 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1586 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1587 this->got_->finalize_data_size();
1588 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1589 this->plt_, plt_offset);
1590 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1591 this->got_, got_offset);
1595 if (this->rela_dyn_ != NULL)
1597 const Output_data* od = this->rela_dyn_;
1598 odyn->add_section_address(elfcpp::DT_RELA, od);
1599 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1600 odyn->add_constant(elfcpp::DT_RELAENT,
1601 elfcpp::Elf_sizes<64>::rela_size);
1604 if (!parameters->options().shared())
1606 // The value of the DT_DEBUG tag is filled in by the dynamic
1607 // linker at run time, and used by the debugger.
1608 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1612 // Emit any relocs we saved in an attempt to avoid generating COPY
1614 if (this->copy_relocs_.any_saved_relocs())
1615 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1618 // Perform a relocation.
1621 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1622 Target_x86_64* target,
1624 const elfcpp::Rela<64, false>& rela,
1625 unsigned int r_type,
1626 const Sized_symbol<64>* gsym,
1627 const Symbol_value<64>* psymval,
1628 unsigned char* view,
1629 elfcpp::Elf_types<64>::Elf_Addr address,
1630 section_size_type view_size)
1632 if (this->skip_call_tls_get_addr_)
1634 if (r_type != elfcpp::R_X86_64_PLT32
1636 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1638 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1639 _("missing expected TLS relocation"));
1643 this->skip_call_tls_get_addr_ = false;
1648 // Pick the value to use for symbols defined in shared objects.
1649 Symbol_value<64> symval;
1651 && (gsym->is_from_dynobj()
1652 || (parameters->options().shared()
1653 && (gsym->is_undefined() || gsym->is_preemptible())))
1654 && gsym->has_plt_offset())
1656 symval.set_output_value(target->plt_section()->address()
1657 + gsym->plt_offset());
1661 const Sized_relobj<64, false>* object = relinfo->object;
1662 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1664 // Get the GOT offset if needed.
1665 // The GOT pointer points to the end of the GOT section.
1666 // We need to subtract the size of the GOT section to get
1667 // the actual offset to use in the relocation.
1668 bool have_got_offset = false;
1669 unsigned int got_offset = 0;
1672 case elfcpp::R_X86_64_GOT32:
1673 case elfcpp::R_X86_64_GOT64:
1674 case elfcpp::R_X86_64_GOTPLT64:
1675 case elfcpp::R_X86_64_GOTPCREL:
1676 case elfcpp::R_X86_64_GOTPCREL64:
1679 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1680 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1684 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1685 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1686 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1687 - target->got_size());
1689 have_got_offset = true;
1698 case elfcpp::R_X86_64_NONE:
1699 case elfcpp::R_386_GNU_VTINHERIT:
1700 case elfcpp::R_386_GNU_VTENTRY:
1703 case elfcpp::R_X86_64_64:
1704 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1707 case elfcpp::R_X86_64_PC64:
1708 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1712 case elfcpp::R_X86_64_32:
1713 // FIXME: we need to verify that value + addend fits into 32 bits:
1714 // uint64_t x = value + addend;
1715 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1716 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1717 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1720 case elfcpp::R_X86_64_32S:
1721 // FIXME: we need to verify that value + addend fits into 32 bits:
1722 // int64_t x = value + addend; // note this quantity is signed!
1723 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1724 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1727 case elfcpp::R_X86_64_PC32:
1728 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1732 case elfcpp::R_X86_64_16:
1733 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1736 case elfcpp::R_X86_64_PC16:
1737 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1741 case elfcpp::R_X86_64_8:
1742 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1745 case elfcpp::R_X86_64_PC8:
1746 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1750 case elfcpp::R_X86_64_PLT32:
1751 gold_assert(gsym == NULL
1752 || gsym->has_plt_offset()
1753 || gsym->final_value_is_known()
1754 || (gsym->is_defined()
1755 && !gsym->is_from_dynobj()
1756 && !gsym->is_preemptible()));
1757 // Note: while this code looks the same as for R_X86_64_PC32, it
1758 // behaves differently because psymval was set to point to
1759 // the PLT entry, rather than the symbol, in Scan::global().
1760 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1764 case elfcpp::R_X86_64_PLTOFF64:
1767 gold_assert(gsym->has_plt_offset()
1768 || gsym->final_value_is_known());
1769 elfcpp::Elf_types<64>::Elf_Addr got_address;
1770 got_address = target->got_section(NULL, NULL)->address();
1771 Relocate_functions<64, false>::rela64(view, object, psymval,
1772 addend - got_address);
1775 case elfcpp::R_X86_64_GOT32:
1776 gold_assert(have_got_offset);
1777 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1780 case elfcpp::R_X86_64_GOTPC32:
1783 elfcpp::Elf_types<64>::Elf_Addr value;
1784 value = target->got_plt_section()->address();
1785 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1789 case elfcpp::R_X86_64_GOT64:
1790 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1791 // Since we always add a PLT entry, this is equivalent.
1792 case elfcpp::R_X86_64_GOTPLT64:
1793 gold_assert(have_got_offset);
1794 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1797 case elfcpp::R_X86_64_GOTPC64:
1800 elfcpp::Elf_types<64>::Elf_Addr value;
1801 value = target->got_plt_section()->address();
1802 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1806 case elfcpp::R_X86_64_GOTOFF64:
1808 elfcpp::Elf_types<64>::Elf_Addr value;
1809 value = (psymval->value(object, 0)
1810 - target->got_plt_section()->address());
1811 Relocate_functions<64, false>::rela64(view, value, addend);
1815 case elfcpp::R_X86_64_GOTPCREL:
1817 gold_assert(have_got_offset);
1818 elfcpp::Elf_types<64>::Elf_Addr value;
1819 value = target->got_plt_section()->address() + got_offset;
1820 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1824 case elfcpp::R_X86_64_GOTPCREL64:
1826 gold_assert(have_got_offset);
1827 elfcpp::Elf_types<64>::Elf_Addr value;
1828 value = target->got_plt_section()->address() + got_offset;
1829 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1833 case elfcpp::R_X86_64_COPY:
1834 case elfcpp::R_X86_64_GLOB_DAT:
1835 case elfcpp::R_X86_64_JUMP_SLOT:
1836 case elfcpp::R_X86_64_RELATIVE:
1837 // These are outstanding tls relocs, which are unexpected when linking
1838 case elfcpp::R_X86_64_TPOFF64:
1839 case elfcpp::R_X86_64_DTPMOD64:
1840 case elfcpp::R_X86_64_TLSDESC:
1841 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1842 _("unexpected reloc %u in object file"),
1846 // These are initial tls relocs, which are expected when linking
1847 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1848 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1849 case elfcpp::R_X86_64_TLSDESC_CALL:
1850 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1851 case elfcpp::R_X86_64_DTPOFF32:
1852 case elfcpp::R_X86_64_DTPOFF64:
1853 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1854 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1855 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1856 view, address, view_size);
1859 case elfcpp::R_X86_64_SIZE32:
1860 case elfcpp::R_X86_64_SIZE64:
1862 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1863 _("unsupported reloc %u"),
1871 // Perform a TLS relocation.
1874 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1875 Target_x86_64* target,
1877 const elfcpp::Rela<64, false>& rela,
1878 unsigned int r_type,
1879 const Sized_symbol<64>* gsym,
1880 const Symbol_value<64>* psymval,
1881 unsigned char* view,
1882 elfcpp::Elf_types<64>::Elf_Addr address,
1883 section_size_type view_size)
1885 Output_segment* tls_segment = relinfo->layout->tls_segment();
1887 const Sized_relobj<64, false>* object = relinfo->object;
1888 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1890 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1892 const bool is_final = (gsym == NULL
1893 ? !parameters->options().output_is_position_independent()
1894 : gsym->final_value_is_known());
1895 const tls::Tls_optimization optimized_type
1896 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1899 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1900 if (optimized_type == tls::TLSOPT_TO_LE)
1902 gold_assert(tls_segment != NULL);
1903 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1904 rela, r_type, value, view,
1910 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1911 ? GOT_TYPE_TLS_OFFSET
1912 : GOT_TYPE_TLS_PAIR);
1913 unsigned int got_offset;
1916 gold_assert(gsym->has_got_offset(got_type));
1917 got_offset = gsym->got_offset(got_type) - target->got_size();
1921 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1922 gold_assert(object->local_has_got_offset(r_sym, got_type));
1923 got_offset = (object->local_got_offset(r_sym, got_type)
1924 - target->got_size());
1926 if (optimized_type == tls::TLSOPT_TO_IE)
1928 gold_assert(tls_segment != NULL);
1929 value = target->got_plt_section()->address() + got_offset;
1930 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1931 value, view, address, view_size);
1934 else if (optimized_type == tls::TLSOPT_NONE)
1936 // Relocate the field with the offset of the pair of GOT
1938 value = target->got_plt_section()->address() + got_offset;
1939 Relocate_functions<64, false>::pcrela32(view, value, addend,
1944 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1945 _("unsupported reloc %u"), r_type);
1948 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1949 case elfcpp::R_X86_64_TLSDESC_CALL:
1950 if (optimized_type == tls::TLSOPT_TO_LE)
1952 gold_assert(tls_segment != NULL);
1953 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1954 rela, r_type, value, view,
1960 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1961 ? GOT_TYPE_TLS_OFFSET
1962 : GOT_TYPE_TLS_DESC);
1963 unsigned int got_offset;
1966 gold_assert(gsym->has_got_offset(got_type));
1967 got_offset = gsym->got_offset(got_type) - target->got_size();
1971 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1972 gold_assert(object->local_has_got_offset(r_sym, got_type));
1973 got_offset = (object->local_got_offset(r_sym, got_type)
1974 - target->got_size());
1976 if (optimized_type == tls::TLSOPT_TO_IE)
1978 gold_assert(tls_segment != NULL);
1979 value = target->got_plt_section()->address() + got_offset;
1980 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
1981 rela, r_type, value, view, address,
1985 else if (optimized_type == tls::TLSOPT_NONE)
1987 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
1989 // Relocate the field with the offset of the pair of GOT
1991 value = target->got_plt_section()->address() + got_offset;
1992 Relocate_functions<64, false>::pcrela32(view, value, addend,
1998 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1999 _("unsupported reloc %u"), r_type);
2002 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2003 if (optimized_type == tls::TLSOPT_TO_LE)
2005 gold_assert(tls_segment != NULL);
2006 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2007 value, view, view_size);
2010 else if (optimized_type == tls::TLSOPT_NONE)
2012 // Relocate the field with the offset of the GOT entry for
2013 // the module index.
2014 unsigned int got_offset;
2015 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2016 - target->got_size());
2017 value = target->got_plt_section()->address() + got_offset;
2018 Relocate_functions<64, false>::pcrela32(view, value, addend,
2022 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2023 _("unsupported reloc %u"), r_type);
2026 case elfcpp::R_X86_64_DTPOFF32:
2027 gold_assert(tls_segment != NULL);
2028 if (optimized_type == tls::TLSOPT_TO_LE)
2029 value -= tls_segment->memsz();
2030 Relocate_functions<64, false>::rela32(view, value, 0);
2033 case elfcpp::R_X86_64_DTPOFF64:
2034 gold_assert(tls_segment != NULL);
2035 if (optimized_type == tls::TLSOPT_TO_LE)
2036 value -= tls_segment->memsz();
2037 Relocate_functions<64, false>::rela64(view, value, 0);
2040 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2041 if (optimized_type == tls::TLSOPT_TO_LE)
2043 gold_assert(tls_segment != NULL);
2044 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2045 rela, r_type, value, view,
2049 else if (optimized_type == tls::TLSOPT_NONE)
2051 // Relocate the field with the offset of the GOT entry for
2052 // the tp-relative offset of the symbol.
2053 unsigned int got_offset;
2056 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2057 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2058 - target->got_size());
2062 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2063 gold_assert(object->local_has_got_offset(r_sym,
2064 GOT_TYPE_TLS_OFFSET));
2065 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2066 - target->got_size());
2068 value = target->got_plt_section()->address() + got_offset;
2069 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2072 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2073 _("unsupported reloc type %u"),
2077 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2078 value -= tls_segment->memsz();
2079 Relocate_functions<64, false>::rela32(view, value, 0);
2084 // Do a relocation in which we convert a TLS General-Dynamic to an
2088 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2091 const elfcpp::Rela<64, false>& rela,
2093 elfcpp::Elf_types<64>::Elf_Addr value,
2094 unsigned char* view,
2095 elfcpp::Elf_types<64>::Elf_Addr address,
2096 section_size_type view_size)
2098 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2099 // .word 0x6666; rex64; call __tls_get_addr
2100 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2102 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2103 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2105 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2106 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2107 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2108 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2110 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2112 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2113 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2115 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2117 this->skip_call_tls_get_addr_ = true;
2120 // Do a relocation in which we convert a TLS General-Dynamic to a
2124 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2126 Output_segment* tls_segment,
2127 const elfcpp::Rela<64, false>& rela,
2129 elfcpp::Elf_types<64>::Elf_Addr value,
2130 unsigned char* view,
2131 section_size_type view_size)
2133 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2134 // .word 0x6666; rex64; call __tls_get_addr
2135 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2137 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2138 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2140 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2141 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2142 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2143 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2145 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2147 value -= tls_segment->memsz();
2148 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2150 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2152 this->skip_call_tls_get_addr_ = true;
2155 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2158 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2159 const Relocate_info<64, false>* relinfo,
2162 const elfcpp::Rela<64, false>& rela,
2163 unsigned int r_type,
2164 elfcpp::Elf_types<64>::Elf_Addr value,
2165 unsigned char* view,
2166 elfcpp::Elf_types<64>::Elf_Addr address,
2167 section_size_type view_size)
2169 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2171 // leaq foo@tlsdesc(%rip), %rax
2172 // ==> movq foo@gottpoff(%rip), %rax
2173 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2174 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2175 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2176 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2178 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2179 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2183 // call *foo@tlscall(%rax)
2185 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2186 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2187 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2188 view[0] == 0xff && view[1] == 0x10);
2194 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2197 Target_x86_64::Relocate::tls_desc_gd_to_le(
2198 const Relocate_info<64, false>* relinfo,
2200 Output_segment* tls_segment,
2201 const elfcpp::Rela<64, false>& rela,
2202 unsigned int r_type,
2203 elfcpp::Elf_types<64>::Elf_Addr value,
2204 unsigned char* view,
2205 section_size_type view_size)
2207 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2209 // leaq foo@tlsdesc(%rip), %rax
2210 // ==> movq foo@tpoff, %rax
2211 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2212 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2213 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2214 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2217 value -= tls_segment->memsz();
2218 Relocate_functions<64, false>::rela32(view, value, 0);
2222 // call *foo@tlscall(%rax)
2224 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2225 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2226 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2227 view[0] == 0xff && view[1] == 0x10);
2234 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2237 const elfcpp::Rela<64, false>& rela,
2239 elfcpp::Elf_types<64>::Elf_Addr,
2240 unsigned char* view,
2241 section_size_type view_size)
2243 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2244 // ... leq foo@dtpoff(%rax),%reg
2245 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2247 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2248 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2250 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2251 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2253 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2255 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2257 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2259 this->skip_call_tls_get_addr_ = true;
2262 // Do a relocation in which we convert a TLS Initial-Exec to a
2266 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2268 Output_segment* tls_segment,
2269 const elfcpp::Rela<64, false>& rela,
2271 elfcpp::Elf_types<64>::Elf_Addr value,
2272 unsigned char* view,
2273 section_size_type view_size)
2275 // We need to examine the opcodes to figure out which instruction we
2278 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2279 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2281 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2282 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2284 unsigned char op1 = view[-3];
2285 unsigned char op2 = view[-2];
2286 unsigned char op3 = view[-1];
2287 unsigned char reg = op3 >> 3;
2295 view[-1] = 0xc0 | reg;
2299 // Special handling for %rsp.
2303 view[-1] = 0xc0 | reg;
2311 view[-1] = 0x80 | reg | (reg << 3);
2314 value -= tls_segment->memsz();
2315 Relocate_functions<64, false>::rela32(view, value, 0);
2318 // Relocate section data.
2321 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2322 unsigned int sh_type,
2323 const unsigned char* prelocs,
2325 Output_section* output_section,
2326 bool needs_special_offset_handling,
2327 unsigned char* view,
2328 elfcpp::Elf_types<64>::Elf_Addr address,
2329 section_size_type view_size)
2331 gold_assert(sh_type == elfcpp::SHT_RELA);
2333 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2334 Target_x86_64::Relocate>(
2340 needs_special_offset_handling,
2346 // Return the size of a relocation while scanning during a relocatable
2350 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2351 unsigned int r_type,
2356 case elfcpp::R_X86_64_NONE:
2357 case elfcpp::R_386_GNU_VTINHERIT:
2358 case elfcpp::R_386_GNU_VTENTRY:
2359 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2360 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2361 case elfcpp::R_X86_64_TLSDESC_CALL:
2362 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2363 case elfcpp::R_X86_64_DTPOFF32:
2364 case elfcpp::R_X86_64_DTPOFF64:
2365 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2366 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2369 case elfcpp::R_X86_64_64:
2370 case elfcpp::R_X86_64_PC64:
2371 case elfcpp::R_X86_64_GOTOFF64:
2372 case elfcpp::R_X86_64_GOTPC64:
2373 case elfcpp::R_X86_64_PLTOFF64:
2374 case elfcpp::R_X86_64_GOT64:
2375 case elfcpp::R_X86_64_GOTPCREL64:
2376 case elfcpp::R_X86_64_GOTPCREL:
2377 case elfcpp::R_X86_64_GOTPLT64:
2380 case elfcpp::R_X86_64_32:
2381 case elfcpp::R_X86_64_32S:
2382 case elfcpp::R_X86_64_PC32:
2383 case elfcpp::R_X86_64_PLT32:
2384 case elfcpp::R_X86_64_GOTPC32:
2385 case elfcpp::R_X86_64_GOT32:
2388 case elfcpp::R_X86_64_16:
2389 case elfcpp::R_X86_64_PC16:
2392 case elfcpp::R_X86_64_8:
2393 case elfcpp::R_X86_64_PC8:
2396 case elfcpp::R_X86_64_COPY:
2397 case elfcpp::R_X86_64_GLOB_DAT:
2398 case elfcpp::R_X86_64_JUMP_SLOT:
2399 case elfcpp::R_X86_64_RELATIVE:
2400 // These are outstanding tls relocs, which are unexpected when linking
2401 case elfcpp::R_X86_64_TPOFF64:
2402 case elfcpp::R_X86_64_DTPMOD64:
2403 case elfcpp::R_X86_64_TLSDESC:
2404 object->error(_("unexpected reloc %u in object file"), r_type);
2407 case elfcpp::R_X86_64_SIZE32:
2408 case elfcpp::R_X86_64_SIZE64:
2410 object->error(_("unsupported reloc %u against local symbol"), r_type);
2415 // Scan the relocs during a relocatable link.
2418 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2419 Symbol_table* symtab,
2421 Sized_relobj<64, false>* object,
2422 unsigned int data_shndx,
2423 unsigned int sh_type,
2424 const unsigned char* prelocs,
2426 Output_section* output_section,
2427 bool needs_special_offset_handling,
2428 size_t local_symbol_count,
2429 const unsigned char* plocal_symbols,
2430 Relocatable_relocs* rr)
2432 gold_assert(sh_type == elfcpp::SHT_RELA);
2434 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2435 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2437 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2438 Scan_relocatable_relocs>(
2447 needs_special_offset_handling,
2453 // Relocate a section during a relocatable link.
2456 Target_x86_64::relocate_for_relocatable(
2457 const Relocate_info<64, false>* relinfo,
2458 unsigned int sh_type,
2459 const unsigned char* prelocs,
2461 Output_section* output_section,
2462 off_t offset_in_output_section,
2463 const Relocatable_relocs* rr,
2464 unsigned char* view,
2465 elfcpp::Elf_types<64>::Elf_Addr view_address,
2466 section_size_type view_size,
2467 unsigned char* reloc_view,
2468 section_size_type reloc_view_size)
2470 gold_assert(sh_type == elfcpp::SHT_RELA);
2472 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2477 offset_in_output_section,
2486 // Return the value to use for a dynamic which requires special
2487 // treatment. This is how we support equality comparisons of function
2488 // pointers across shared library boundaries, as described in the
2489 // processor specific ABI supplement.
2492 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2494 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2495 return this->plt_section()->address() + gsym->plt_offset();
2498 // Return a string used to fill a code section with nops to take up
2499 // the specified length.
2502 Target_x86_64::do_code_fill(section_size_type length) const
2506 // Build a jmpq instruction to skip over the bytes.
2507 unsigned char jmp[5];
2509 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2510 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2511 + std::string(length - 5, '\0'));
2514 // Nop sequences of various lengths.
2515 const char nop1[1] = { 0x90 }; // nop
2516 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2517 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2518 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2519 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2520 0x00 }; // leal 0(%esi,1),%esi
2521 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2523 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2525 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2526 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2527 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2528 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2530 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2531 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2533 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2534 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2536 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2537 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2538 0x00, 0x00, 0x00, 0x00 };
2539 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2540 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2541 0x27, 0x00, 0x00, 0x00,
2543 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2544 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2545 0xbc, 0x27, 0x00, 0x00,
2547 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2548 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2549 0x90, 0x90, 0x90, 0x90,
2552 const char* nops[16] = {
2554 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2555 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2558 return std::string(nops[length], length);
2561 // The selector for x86_64 object files.
2563 class Target_selector_x86_64 : public Target_selector
2566 Target_selector_x86_64()
2567 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2571 do_instantiate_target()
2572 { return new Target_x86_64(); }
2575 Target_selector_x86_64 target_selector_x86_64;
2577 } // End anonymous namespace.