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();
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();
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<32>::Elf_Addr plt_address = this->address();
676 // The base address of the .got section.
677 elfcpp::Elf_types<32>::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<32>::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, got_address + 8
686 - (plt_address + 6));
687 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
688 - (plt_address + 12));
689 pov += plt_entry_size;
691 unsigned char* got_pov = got_view;
693 memset(got_pov, 0, 24);
696 unsigned int plt_offset = plt_entry_size;
697 unsigned int got_offset = 24;
698 const unsigned int count = this->count_;
699 for (unsigned int plt_index = 0;
702 pov += plt_entry_size,
704 plt_offset += plt_entry_size,
707 // Set and adjust the PLT entry itself.
708 memcpy(pov, plt_entry, plt_entry_size);
709 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
710 (got_address + got_offset
711 - (plt_address + plt_offset
714 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
715 elfcpp::Swap<32, false>::writeval(pov + 12,
716 - (plt_offset + plt_entry_size));
718 // Set the entry in the GOT.
719 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
722 if (this->has_tlsdesc_entry())
724 // Set and adjust the reserved TLSDESC PLT entry.
725 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
726 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
727 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
729 - (plt_address + plt_offset
731 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
734 - (plt_address + plt_offset
736 pov += plt_entry_size;
739 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
740 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
742 of->write_output_view(offset, oview_size, oview);
743 of->write_output_view(got_file_offset, got_size, got_view);
746 // Create the PLT section.
749 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
751 if (this->plt_ == NULL)
753 // Create the GOT sections first.
754 this->got_section(symtab, layout);
756 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
758 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
760 | elfcpp::SHF_EXECINSTR),
765 // Create a PLT entry for a global symbol.
768 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
771 if (gsym->has_plt_offset())
774 if (this->plt_ == NULL)
775 this->make_plt_section(symtab, layout);
777 this->plt_->add_entry(gsym);
780 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
783 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
785 if (this->tls_base_symbol_defined_)
788 Output_segment* tls_segment = layout->tls_segment();
789 if (tls_segment != NULL)
791 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
795 elfcpp::STV_HIDDEN, 0,
796 Symbol::SEGMENT_END, true);
798 this->tls_base_symbol_defined_ = true;
801 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
804 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
807 if (this->plt_ == NULL)
808 this->make_plt_section(symtab, layout);
810 if (!this->plt_->has_tlsdesc_entry())
812 // Allocate the TLSDESC_GOT entry.
813 Output_data_got<64, false>* got = this->got_section(symtab, layout);
814 unsigned int got_offset = got->add_constant(0);
816 // Allocate the TLSDESC_PLT entry.
817 this->plt_->reserve_tlsdesc_entry(got_offset);
821 // Create a GOT entry for the TLS module index.
824 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
825 Sized_relobj<64, false>* object)
827 if (this->got_mod_index_offset_ == -1U)
829 gold_assert(symtab != NULL && layout != NULL && object != NULL);
830 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
831 Output_data_got<64, false>* got = this->got_section(symtab, layout);
832 unsigned int got_offset = got->add_constant(0);
833 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
835 got->add_constant(0);
836 this->got_mod_index_offset_ = got_offset;
838 return this->got_mod_index_offset_;
841 // Optimize the TLS relocation type based on what we know about the
842 // symbol. IS_FINAL is true if the final address of this symbol is
843 // known at link time.
845 tls::Tls_optimization
846 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
848 // If we are generating a shared library, then we can't do anything
850 if (parameters->options().shared())
851 return tls::TLSOPT_NONE;
855 case elfcpp::R_X86_64_TLSGD:
856 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
857 case elfcpp::R_X86_64_TLSDESC_CALL:
858 // These are General-Dynamic which permits fully general TLS
859 // access. Since we know that we are generating an executable,
860 // we can convert this to Initial-Exec. If we also know that
861 // this is a local symbol, we can further switch to Local-Exec.
863 return tls::TLSOPT_TO_LE;
864 return tls::TLSOPT_TO_IE;
866 case elfcpp::R_X86_64_TLSLD:
867 // This is Local-Dynamic, which refers to a local symbol in the
868 // dynamic TLS block. Since we know that we generating an
869 // executable, we can switch to Local-Exec.
870 return tls::TLSOPT_TO_LE;
872 case elfcpp::R_X86_64_DTPOFF32:
873 case elfcpp::R_X86_64_DTPOFF64:
874 // Another Local-Dynamic reloc.
875 return tls::TLSOPT_TO_LE;
877 case elfcpp::R_X86_64_GOTTPOFF:
878 // These are Initial-Exec relocs which get the thread offset
879 // from the GOT. If we know that we are linking against the
880 // local symbol, we can switch to Local-Exec, which links the
881 // thread offset into the instruction.
883 return tls::TLSOPT_TO_LE;
884 return tls::TLSOPT_NONE;
886 case elfcpp::R_X86_64_TPOFF32:
887 // When we already have Local-Exec, there is nothing further we
889 return tls::TLSOPT_NONE;
896 // Report an unsupported relocation against a local symbol.
899 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
902 gold_error(_("%s: unsupported reloc %u against local symbol"),
903 object->name().c_str(), r_type);
906 // We are about to emit a dynamic relocation of type R_TYPE. If the
907 // dynamic linker does not support it, issue an error. The GNU linker
908 // only issues a non-PIC error for an allocated read-only section.
909 // Here we know the section is allocated, but we don't know that it is
910 // read-only. But we check for all the relocation types which the
911 // glibc dynamic linker supports, so it seems appropriate to issue an
912 // error even if the section is not read-only.
915 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
919 // These are the relocation types supported by glibc for x86_64.
920 case elfcpp::R_X86_64_RELATIVE:
921 case elfcpp::R_X86_64_GLOB_DAT:
922 case elfcpp::R_X86_64_JUMP_SLOT:
923 case elfcpp::R_X86_64_DTPMOD64:
924 case elfcpp::R_X86_64_DTPOFF64:
925 case elfcpp::R_X86_64_TPOFF64:
926 case elfcpp::R_X86_64_64:
927 case elfcpp::R_X86_64_32:
928 case elfcpp::R_X86_64_PC32:
929 case elfcpp::R_X86_64_COPY:
933 // This prevents us from issuing more than one error per reloc
934 // section. But we can still wind up issuing more than one
935 // error per object file.
936 if (this->issued_non_pic_error_)
938 object->error(_("requires unsupported dynamic reloc; "
939 "recompile with -fPIC"));
940 this->issued_non_pic_error_ = true;
943 case elfcpp::R_X86_64_NONE:
948 // Scan a relocation for a local symbol.
951 Target_x86_64::Scan::local(const General_options&,
952 Symbol_table* symtab,
954 Target_x86_64* target,
955 Sized_relobj<64, false>* object,
956 unsigned int data_shndx,
957 Output_section* output_section,
958 const elfcpp::Rela<64, false>& reloc,
960 const elfcpp::Sym<64, false>& lsym)
964 case elfcpp::R_X86_64_NONE:
965 case elfcpp::R_386_GNU_VTINHERIT:
966 case elfcpp::R_386_GNU_VTENTRY:
969 case elfcpp::R_X86_64_64:
970 // If building a shared library (or a position-independent
971 // executable), we need to create a dynamic relocation for this
972 // location. The relocation applied at link time will apply the
973 // link-time value, so we flag the location with an
974 // R_X86_64_RELATIVE relocation so the dynamic loader can
975 // relocate it easily.
976 if (parameters->options().output_is_position_independent())
978 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
979 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
980 rela_dyn->add_local_relative(object, r_sym,
981 elfcpp::R_X86_64_RELATIVE,
982 output_section, data_shndx,
983 reloc.get_r_offset(),
984 reloc.get_r_addend());
988 case elfcpp::R_X86_64_32:
989 case elfcpp::R_X86_64_32S:
990 case elfcpp::R_X86_64_16:
991 case elfcpp::R_X86_64_8:
992 // If building a shared library (or a position-independent
993 // executable), we need to create a dynamic relocation for this
994 // location. We can't use an R_X86_64_RELATIVE relocation
995 // because that is always a 64-bit relocation.
996 if (parameters->options().output_is_position_independent())
998 this->check_non_pic(object, r_type);
1000 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1001 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1003 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1004 rela_dyn->add_local(object, r_sym, r_type, output_section,
1005 data_shndx, reloc.get_r_offset(),
1006 reloc.get_r_addend());
1010 gold_assert(lsym.get_st_value() == 0);
1011 rela_dyn->add_local_section(object, lsym.get_st_shndx(),
1012 r_type, output_section,
1013 data_shndx, reloc.get_r_offset(),
1014 reloc.get_r_addend());
1019 case elfcpp::R_X86_64_PC64:
1020 case elfcpp::R_X86_64_PC32:
1021 case elfcpp::R_X86_64_PC16:
1022 case elfcpp::R_X86_64_PC8:
1025 case elfcpp::R_X86_64_PLT32:
1026 // Since we know this is a local symbol, we can handle this as a
1030 case elfcpp::R_X86_64_GOTPC32:
1031 case elfcpp::R_X86_64_GOTOFF64:
1032 case elfcpp::R_X86_64_GOTPC64:
1033 case elfcpp::R_X86_64_PLTOFF64:
1034 // We need a GOT section.
1035 target->got_section(symtab, layout);
1036 // For PLTOFF64, we'd normally want a PLT section, but since we
1037 // know this is a local symbol, no PLT is needed.
1040 case elfcpp::R_X86_64_GOT64:
1041 case elfcpp::R_X86_64_GOT32:
1042 case elfcpp::R_X86_64_GOTPCREL64:
1043 case elfcpp::R_X86_64_GOTPCREL:
1044 case elfcpp::R_X86_64_GOTPLT64:
1046 // The symbol requires a GOT entry.
1047 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1048 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1049 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1051 // If we are generating a shared object, we need to add a
1052 // dynamic relocation for this symbol's GOT entry.
1053 if (parameters->options().output_is_position_independent())
1055 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1056 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1057 if (r_type != elfcpp::R_X86_64_GOT32)
1058 rela_dyn->add_local_relative(
1059 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1060 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1063 this->check_non_pic(object, r_type);
1065 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1066 rela_dyn->add_local(
1067 object, r_sym, r_type, got,
1068 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1072 // For GOTPLT64, we'd normally want a PLT section, but since
1073 // we know this is a local symbol, no PLT is needed.
1077 case elfcpp::R_X86_64_COPY:
1078 case elfcpp::R_X86_64_GLOB_DAT:
1079 case elfcpp::R_X86_64_JUMP_SLOT:
1080 case elfcpp::R_X86_64_RELATIVE:
1081 // These are outstanding tls relocs, which are unexpected when linking
1082 case elfcpp::R_X86_64_TPOFF64:
1083 case elfcpp::R_X86_64_DTPMOD64:
1084 case elfcpp::R_X86_64_TLSDESC:
1085 gold_error(_("%s: unexpected reloc %u in object file"),
1086 object->name().c_str(), r_type);
1089 // These are initial tls relocs, which are expected when linking
1090 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1091 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1092 case elfcpp::R_X86_64_TLSDESC_CALL:
1093 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1094 case elfcpp::R_X86_64_DTPOFF32:
1095 case elfcpp::R_X86_64_DTPOFF64:
1096 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1097 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1099 bool output_is_shared = parameters->options().shared();
1100 const tls::Tls_optimization optimized_type
1101 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1104 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1105 if (optimized_type == tls::TLSOPT_NONE)
1107 // Create a pair of GOT entries for the module index and
1108 // dtv-relative offset.
1109 Output_data_got<64, false>* got
1110 = target->got_section(symtab, layout);
1111 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1112 got->add_local_pair_with_rela(object, r_sym,
1113 lsym.get_st_shndx(),
1115 target->rela_dyn_section(layout),
1116 elfcpp::R_X86_64_DTPMOD64, 0);
1118 else if (optimized_type != tls::TLSOPT_TO_LE)
1119 unsupported_reloc_local(object, r_type);
1122 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1123 target->define_tls_base_symbol(symtab, layout);
1124 if (optimized_type == tls::TLSOPT_NONE)
1126 // Create reserved PLT and GOT entries for the resolver.
1127 target->reserve_tlsdesc_entries(symtab, layout);
1129 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1130 Output_data_got<64, false>* got
1131 = target->got_section(symtab, layout);
1132 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1133 got->add_local_pair_with_rela(object, r_sym,
1134 lsym.get_st_shndx(),
1136 target->rela_dyn_section(layout),
1137 elfcpp::R_X86_64_TLSDESC, 0);
1139 else if (optimized_type != tls::TLSOPT_TO_LE)
1140 unsupported_reloc_local(object, r_type);
1143 case elfcpp::R_X86_64_TLSDESC_CALL:
1146 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1147 if (optimized_type == tls::TLSOPT_NONE)
1149 // Create a GOT entry for the module index.
1150 target->got_mod_index_entry(symtab, layout, object);
1152 else if (optimized_type != tls::TLSOPT_TO_LE)
1153 unsupported_reloc_local(object, r_type);
1156 case elfcpp::R_X86_64_DTPOFF32:
1157 case elfcpp::R_X86_64_DTPOFF64:
1160 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1161 layout->set_has_static_tls();
1162 if (optimized_type == tls::TLSOPT_NONE)
1164 // Create a GOT entry for the tp-relative offset.
1165 Output_data_got<64, false>* got
1166 = target->got_section(symtab, layout);
1167 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1168 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1169 target->rela_dyn_section(layout),
1170 elfcpp::R_X86_64_TPOFF64);
1172 else if (optimized_type != tls::TLSOPT_TO_LE)
1173 unsupported_reloc_local(object, r_type);
1176 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1177 layout->set_has_static_tls();
1178 if (output_is_shared)
1179 unsupported_reloc_local(object, r_type);
1188 case elfcpp::R_X86_64_SIZE32:
1189 case elfcpp::R_X86_64_SIZE64:
1191 gold_error(_("%s: unsupported reloc %u against local symbol"),
1192 object->name().c_str(), r_type);
1198 // Report an unsupported relocation against a global symbol.
1201 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1202 unsigned int r_type,
1205 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1206 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1209 // Scan a relocation for a global symbol.
1212 Target_x86_64::Scan::global(const General_options&,
1213 Symbol_table* symtab,
1215 Target_x86_64* target,
1216 Sized_relobj<64, false>* object,
1217 unsigned int data_shndx,
1218 Output_section* output_section,
1219 const elfcpp::Rela<64, false>& reloc,
1220 unsigned int r_type,
1225 case elfcpp::R_X86_64_NONE:
1226 case elfcpp::R_386_GNU_VTINHERIT:
1227 case elfcpp::R_386_GNU_VTENTRY:
1230 case elfcpp::R_X86_64_64:
1231 case elfcpp::R_X86_64_32:
1232 case elfcpp::R_X86_64_32S:
1233 case elfcpp::R_X86_64_16:
1234 case elfcpp::R_X86_64_8:
1236 // Make a PLT entry if necessary.
1237 if (gsym->needs_plt_entry())
1239 target->make_plt_entry(symtab, layout, gsym);
1240 // Since this is not a PC-relative relocation, we may be
1241 // taking the address of a function. In that case we need to
1242 // set the entry in the dynamic symbol table to the address of
1244 if (gsym->is_from_dynobj() && !parameters->options().shared())
1245 gsym->set_needs_dynsym_value();
1247 // Make a dynamic relocation if necessary.
1248 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1250 if (target->may_need_copy_reloc(gsym))
1252 target->copy_reloc(symtab, layout, object,
1253 data_shndx, output_section, gsym, reloc);
1255 else if (r_type == elfcpp::R_X86_64_64
1256 && gsym->can_use_relative_reloc(false))
1258 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1259 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1260 output_section, object,
1261 data_shndx, reloc.get_r_offset(),
1262 reloc.get_r_addend());
1266 this->check_non_pic(object, r_type);
1267 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1268 rela_dyn->add_global(gsym, r_type, output_section, object,
1269 data_shndx, reloc.get_r_offset(),
1270 reloc.get_r_addend());
1276 case elfcpp::R_X86_64_PC64:
1277 case elfcpp::R_X86_64_PC32:
1278 case elfcpp::R_X86_64_PC16:
1279 case elfcpp::R_X86_64_PC8:
1281 // Make a PLT entry if necessary.
1282 if (gsym->needs_plt_entry())
1283 target->make_plt_entry(symtab, layout, gsym);
1284 // Make a dynamic relocation if necessary.
1285 int flags = Symbol::NON_PIC_REF;
1286 if (gsym->type() == elfcpp::STT_FUNC)
1287 flags |= Symbol::FUNCTION_CALL;
1288 if (gsym->needs_dynamic_reloc(flags))
1290 if (target->may_need_copy_reloc(gsym))
1292 target->copy_reloc(symtab, layout, object,
1293 data_shndx, output_section, gsym, reloc);
1297 this->check_non_pic(object, r_type);
1298 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1299 rela_dyn->add_global(gsym, r_type, output_section, object,
1300 data_shndx, reloc.get_r_offset(),
1301 reloc.get_r_addend());
1307 case elfcpp::R_X86_64_GOT64:
1308 case elfcpp::R_X86_64_GOT32:
1309 case elfcpp::R_X86_64_GOTPCREL64:
1310 case elfcpp::R_X86_64_GOTPCREL:
1311 case elfcpp::R_X86_64_GOTPLT64:
1313 // The symbol requires a GOT entry.
1314 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1315 if (gsym->final_value_is_known())
1316 got->add_global(gsym, GOT_TYPE_STANDARD);
1319 // If this symbol is not fully resolved, we need to add a
1320 // dynamic relocation for it.
1321 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1322 if (gsym->is_from_dynobj()
1323 || gsym->is_undefined()
1324 || gsym->is_preemptible())
1325 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1326 elfcpp::R_X86_64_GLOB_DAT);
1329 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1330 rela_dyn->add_global_relative(
1331 gsym, elfcpp::R_X86_64_RELATIVE, got,
1332 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1335 // For GOTPLT64, we also need a PLT entry (but only if the
1336 // symbol is not fully resolved).
1337 if (r_type == elfcpp::R_X86_64_GOTPLT64
1338 && !gsym->final_value_is_known())
1339 target->make_plt_entry(symtab, layout, gsym);
1343 case elfcpp::R_X86_64_PLT32:
1344 // If the symbol is fully resolved, this is just a PC32 reloc.
1345 // Otherwise we need a PLT entry.
1346 if (gsym->final_value_is_known())
1348 // If building a shared library, we can also skip the PLT entry
1349 // if the symbol is defined in the output file and is protected
1351 if (gsym->is_defined()
1352 && !gsym->is_from_dynobj()
1353 && !gsym->is_preemptible())
1355 target->make_plt_entry(symtab, layout, gsym);
1358 case elfcpp::R_X86_64_GOTPC32:
1359 case elfcpp::R_X86_64_GOTOFF64:
1360 case elfcpp::R_X86_64_GOTPC64:
1361 case elfcpp::R_X86_64_PLTOFF64:
1362 // We need a GOT section.
1363 target->got_section(symtab, layout);
1364 // For PLTOFF64, we also need a PLT entry (but only if the
1365 // symbol is not fully resolved).
1366 if (r_type == elfcpp::R_X86_64_PLTOFF64
1367 && !gsym->final_value_is_known())
1368 target->make_plt_entry(symtab, layout, gsym);
1371 case elfcpp::R_X86_64_COPY:
1372 case elfcpp::R_X86_64_GLOB_DAT:
1373 case elfcpp::R_X86_64_JUMP_SLOT:
1374 case elfcpp::R_X86_64_RELATIVE:
1375 // These are outstanding tls relocs, which are unexpected when linking
1376 case elfcpp::R_X86_64_TPOFF64:
1377 case elfcpp::R_X86_64_DTPMOD64:
1378 case elfcpp::R_X86_64_TLSDESC:
1379 gold_error(_("%s: unexpected reloc %u in object file"),
1380 object->name().c_str(), r_type);
1383 // These are initial tls relocs, which are expected for global()
1384 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1385 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1386 case elfcpp::R_X86_64_TLSDESC_CALL:
1387 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1388 case elfcpp::R_X86_64_DTPOFF32:
1389 case elfcpp::R_X86_64_DTPOFF64:
1390 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1391 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1393 const bool is_final = gsym->final_value_is_known();
1394 const tls::Tls_optimization optimized_type
1395 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1398 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1399 if (optimized_type == tls::TLSOPT_NONE)
1401 // Create a pair of GOT entries for the module index and
1402 // dtv-relative offset.
1403 Output_data_got<64, false>* got
1404 = target->got_section(symtab, layout);
1405 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1406 target->rela_dyn_section(layout),
1407 elfcpp::R_X86_64_DTPMOD64,
1408 elfcpp::R_X86_64_DTPOFF64);
1410 else if (optimized_type == tls::TLSOPT_TO_IE)
1412 // Create a GOT entry for the tp-relative offset.
1413 Output_data_got<64, false>* got
1414 = target->got_section(symtab, layout);
1415 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1416 target->rela_dyn_section(layout),
1417 elfcpp::R_X86_64_TPOFF64);
1419 else if (optimized_type != tls::TLSOPT_TO_LE)
1420 unsupported_reloc_global(object, r_type, gsym);
1423 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1424 target->define_tls_base_symbol(symtab, layout);
1425 if (optimized_type == tls::TLSOPT_NONE)
1427 // Create reserved PLT and GOT entries for the resolver.
1428 target->reserve_tlsdesc_entries(symtab, layout);
1430 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1431 Output_data_got<64, false>* got
1432 = target->got_section(symtab, layout);
1433 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1434 target->rela_dyn_section(layout),
1435 elfcpp::R_X86_64_TLSDESC, 0);
1437 else if (optimized_type == tls::TLSOPT_TO_IE)
1439 // Create a GOT entry for the tp-relative offset.
1440 Output_data_got<64, false>* got
1441 = target->got_section(symtab, layout);
1442 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1443 target->rela_dyn_section(layout),
1444 elfcpp::R_X86_64_TPOFF64);
1446 else if (optimized_type != tls::TLSOPT_TO_LE)
1447 unsupported_reloc_global(object, r_type, gsym);
1450 case elfcpp::R_X86_64_TLSDESC_CALL:
1453 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1454 if (optimized_type == tls::TLSOPT_NONE)
1456 // Create a GOT entry for the module index.
1457 target->got_mod_index_entry(symtab, layout, object);
1459 else if (optimized_type != tls::TLSOPT_TO_LE)
1460 unsupported_reloc_global(object, r_type, gsym);
1463 case elfcpp::R_X86_64_DTPOFF32:
1464 case elfcpp::R_X86_64_DTPOFF64:
1467 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1468 layout->set_has_static_tls();
1469 if (optimized_type == tls::TLSOPT_NONE)
1471 // Create a GOT entry for the tp-relative offset.
1472 Output_data_got<64, false>* got
1473 = target->got_section(symtab, layout);
1474 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1475 target->rela_dyn_section(layout),
1476 elfcpp::R_X86_64_TPOFF64);
1478 else if (optimized_type != tls::TLSOPT_TO_LE)
1479 unsupported_reloc_global(object, r_type, gsym);
1482 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1483 layout->set_has_static_tls();
1484 if (parameters->options().shared())
1485 unsupported_reloc_local(object, r_type);
1494 case elfcpp::R_X86_64_SIZE32:
1495 case elfcpp::R_X86_64_SIZE64:
1497 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1498 object->name().c_str(), r_type,
1499 gsym->demangled_name().c_str());
1504 // Scan relocations for a section.
1507 Target_x86_64::scan_relocs(const General_options& options,
1508 Symbol_table* symtab,
1510 Sized_relobj<64, false>* object,
1511 unsigned int data_shndx,
1512 unsigned int sh_type,
1513 const unsigned char* prelocs,
1515 Output_section* output_section,
1516 bool needs_special_offset_handling,
1517 size_t local_symbol_count,
1518 const unsigned char* plocal_symbols)
1520 if (sh_type == elfcpp::SHT_REL)
1522 gold_error(_("%s: unsupported REL reloc section"),
1523 object->name().c_str());
1527 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1528 Target_x86_64::Scan>(
1538 needs_special_offset_handling,
1543 // Finalize the sections.
1546 Target_x86_64::do_finalize_sections(Layout* layout)
1548 // Fill in some more dynamic tags.
1549 Output_data_dynamic* const odyn = layout->dynamic_data();
1552 if (this->got_plt_ != NULL)
1553 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1555 if (this->plt_ != NULL)
1557 const Output_data* od = this->plt_->rel_plt();
1558 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1559 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1560 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1561 if (this->plt_->has_tlsdesc_entry())
1563 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1564 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1565 this->got_->finalize_data_size();
1566 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1567 this->plt_, plt_offset);
1568 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1569 this->got_, got_offset);
1573 if (this->rela_dyn_ != NULL)
1575 const Output_data* od = this->rela_dyn_;
1576 odyn->add_section_address(elfcpp::DT_RELA, od);
1577 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1578 odyn->add_constant(elfcpp::DT_RELAENT,
1579 elfcpp::Elf_sizes<64>::rela_size);
1582 if (!parameters->options().shared())
1584 // The value of the DT_DEBUG tag is filled in by the dynamic
1585 // linker at run time, and used by the debugger.
1586 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1590 // Emit any relocs we saved in an attempt to avoid generating COPY
1592 if (this->copy_relocs_.any_saved_relocs())
1593 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1596 // Perform a relocation.
1599 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1600 Target_x86_64* target,
1602 const elfcpp::Rela<64, false>& rela,
1603 unsigned int r_type,
1604 const Sized_symbol<64>* gsym,
1605 const Symbol_value<64>* psymval,
1606 unsigned char* view,
1607 elfcpp::Elf_types<64>::Elf_Addr address,
1608 section_size_type view_size)
1610 if (this->skip_call_tls_get_addr_)
1612 if (r_type != elfcpp::R_X86_64_PLT32
1614 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1616 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1617 _("missing expected TLS relocation"));
1621 this->skip_call_tls_get_addr_ = false;
1626 // Pick the value to use for symbols defined in shared objects.
1627 Symbol_value<64> symval;
1629 && (gsym->is_from_dynobj()
1630 || (parameters->options().shared()
1631 && (gsym->is_undefined() || gsym->is_preemptible())))
1632 && gsym->has_plt_offset())
1634 symval.set_output_value(target->plt_section()->address()
1635 + gsym->plt_offset());
1639 const Sized_relobj<64, false>* object = relinfo->object;
1640 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1642 // Get the GOT offset if needed.
1643 // The GOT pointer points to the end of the GOT section.
1644 // We need to subtract the size of the GOT section to get
1645 // the actual offset to use in the relocation.
1646 bool have_got_offset = false;
1647 unsigned int got_offset = 0;
1650 case elfcpp::R_X86_64_GOT32:
1651 case elfcpp::R_X86_64_GOT64:
1652 case elfcpp::R_X86_64_GOTPLT64:
1653 case elfcpp::R_X86_64_GOTPCREL:
1654 case elfcpp::R_X86_64_GOTPCREL64:
1657 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1658 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1662 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1663 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1664 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1665 - target->got_size());
1667 have_got_offset = true;
1676 case elfcpp::R_X86_64_NONE:
1677 case elfcpp::R_386_GNU_VTINHERIT:
1678 case elfcpp::R_386_GNU_VTENTRY:
1681 case elfcpp::R_X86_64_64:
1682 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1685 case elfcpp::R_X86_64_PC64:
1686 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1690 case elfcpp::R_X86_64_32:
1691 // FIXME: we need to verify that value + addend fits into 32 bits:
1692 // uint64_t x = value + addend;
1693 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1694 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1695 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1698 case elfcpp::R_X86_64_32S:
1699 // FIXME: we need to verify that value + addend fits into 32 bits:
1700 // int64_t x = value + addend; // note this quantity is signed!
1701 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1702 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1705 case elfcpp::R_X86_64_PC32:
1706 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1710 case elfcpp::R_X86_64_16:
1711 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1714 case elfcpp::R_X86_64_PC16:
1715 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1719 case elfcpp::R_X86_64_8:
1720 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1723 case elfcpp::R_X86_64_PC8:
1724 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1728 case elfcpp::R_X86_64_PLT32:
1729 gold_assert(gsym == NULL
1730 || gsym->has_plt_offset()
1731 || gsym->final_value_is_known()
1732 || (gsym->is_defined()
1733 && !gsym->is_from_dynobj()
1734 && !gsym->is_preemptible()));
1735 // Note: while this code looks the same as for R_X86_64_PC32, it
1736 // behaves differently because psymval was set to point to
1737 // the PLT entry, rather than the symbol, in Scan::global().
1738 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1742 case elfcpp::R_X86_64_PLTOFF64:
1745 gold_assert(gsym->has_plt_offset()
1746 || gsym->final_value_is_known());
1747 elfcpp::Elf_types<64>::Elf_Addr got_address;
1748 got_address = target->got_section(NULL, NULL)->address();
1749 Relocate_functions<64, false>::rela64(view, object, psymval,
1750 addend - got_address);
1753 case elfcpp::R_X86_64_GOT32:
1754 gold_assert(have_got_offset);
1755 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1758 case elfcpp::R_X86_64_GOTPC32:
1761 elfcpp::Elf_types<64>::Elf_Addr value;
1762 value = target->got_plt_section()->address();
1763 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1767 case elfcpp::R_X86_64_GOT64:
1768 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1769 // Since we always add a PLT entry, this is equivalent.
1770 case elfcpp::R_X86_64_GOTPLT64:
1771 gold_assert(have_got_offset);
1772 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1775 case elfcpp::R_X86_64_GOTPC64:
1778 elfcpp::Elf_types<64>::Elf_Addr value;
1779 value = target->got_plt_section()->address();
1780 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1784 case elfcpp::R_X86_64_GOTOFF64:
1786 elfcpp::Elf_types<64>::Elf_Addr value;
1787 value = (psymval->value(object, 0)
1788 - target->got_plt_section()->address());
1789 Relocate_functions<64, false>::rela64(view, value, addend);
1793 case elfcpp::R_X86_64_GOTPCREL:
1795 gold_assert(have_got_offset);
1796 elfcpp::Elf_types<64>::Elf_Addr value;
1797 value = target->got_plt_section()->address() + got_offset;
1798 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1802 case elfcpp::R_X86_64_GOTPCREL64:
1804 gold_assert(have_got_offset);
1805 elfcpp::Elf_types<64>::Elf_Addr value;
1806 value = target->got_plt_section()->address() + got_offset;
1807 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1811 case elfcpp::R_X86_64_COPY:
1812 case elfcpp::R_X86_64_GLOB_DAT:
1813 case elfcpp::R_X86_64_JUMP_SLOT:
1814 case elfcpp::R_X86_64_RELATIVE:
1815 // These are outstanding tls relocs, which are unexpected when linking
1816 case elfcpp::R_X86_64_TPOFF64:
1817 case elfcpp::R_X86_64_DTPMOD64:
1818 case elfcpp::R_X86_64_TLSDESC:
1819 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1820 _("unexpected reloc %u in object file"),
1824 // These are initial tls relocs, which are expected when linking
1825 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1826 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1827 case elfcpp::R_X86_64_TLSDESC_CALL:
1828 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1829 case elfcpp::R_X86_64_DTPOFF32:
1830 case elfcpp::R_X86_64_DTPOFF64:
1831 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1832 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1833 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1834 view, address, view_size);
1837 case elfcpp::R_X86_64_SIZE32:
1838 case elfcpp::R_X86_64_SIZE64:
1840 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1841 _("unsupported reloc %u"),
1849 // Perform a TLS relocation.
1852 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1853 Target_x86_64* target,
1855 const elfcpp::Rela<64, false>& rela,
1856 unsigned int r_type,
1857 const Sized_symbol<64>* gsym,
1858 const Symbol_value<64>* psymval,
1859 unsigned char* view,
1860 elfcpp::Elf_types<64>::Elf_Addr address,
1861 section_size_type view_size)
1863 Output_segment* tls_segment = relinfo->layout->tls_segment();
1865 const Sized_relobj<64, false>* object = relinfo->object;
1866 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1868 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1870 const bool is_final = (gsym == NULL
1871 ? !parameters->options().output_is_position_independent()
1872 : gsym->final_value_is_known());
1873 const tls::Tls_optimization optimized_type
1874 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1877 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1878 if (optimized_type == tls::TLSOPT_TO_LE)
1880 gold_assert(tls_segment != NULL);
1881 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1882 rela, r_type, value, view,
1888 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1889 ? GOT_TYPE_TLS_OFFSET
1890 : GOT_TYPE_TLS_PAIR);
1891 unsigned int got_offset;
1894 gold_assert(gsym->has_got_offset(got_type));
1895 got_offset = gsym->got_offset(got_type) - target->got_size();
1899 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1900 gold_assert(object->local_has_got_offset(r_sym, got_type));
1901 got_offset = (object->local_got_offset(r_sym, got_type)
1902 - target->got_size());
1904 if (optimized_type == tls::TLSOPT_TO_IE)
1906 gold_assert(tls_segment != NULL);
1907 value = target->got_plt_section()->address() + got_offset;
1908 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1909 value, view, address, view_size);
1912 else if (optimized_type == tls::TLSOPT_NONE)
1914 // Relocate the field with the offset of the pair of GOT
1916 value = target->got_plt_section()->address() + got_offset;
1917 Relocate_functions<64, false>::pcrela32(view, value, addend,
1922 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1923 _("unsupported reloc %u"), r_type);
1926 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1927 case elfcpp::R_X86_64_TLSDESC_CALL:
1928 if (optimized_type == tls::TLSOPT_TO_LE)
1930 gold_assert(tls_segment != NULL);
1931 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1932 rela, r_type, value, view,
1938 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1939 ? GOT_TYPE_TLS_OFFSET
1940 : GOT_TYPE_TLS_DESC);
1941 unsigned int got_offset;
1944 gold_assert(gsym->has_got_offset(got_type));
1945 got_offset = gsym->got_offset(got_type) - target->got_size();
1949 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1950 gold_assert(object->local_has_got_offset(r_sym, got_type));
1951 got_offset = (object->local_got_offset(r_sym, got_type)
1952 - target->got_size());
1954 if (optimized_type == tls::TLSOPT_TO_IE)
1956 gold_assert(tls_segment != NULL);
1957 value = target->got_plt_section()->address() + got_offset;
1958 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
1959 rela, r_type, value, view, address,
1963 else if (optimized_type == tls::TLSOPT_NONE)
1965 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
1967 // Relocate the field with the offset of the pair of GOT
1969 value = target->got_plt_section()->address() + got_offset;
1970 Relocate_functions<64, false>::pcrela32(view, value, addend,
1976 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1977 _("unsupported reloc %u"), r_type);
1980 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1981 if (optimized_type == tls::TLSOPT_TO_LE)
1983 gold_assert(tls_segment != NULL);
1984 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1985 value, view, view_size);
1988 else if (optimized_type == tls::TLSOPT_NONE)
1990 // Relocate the field with the offset of the GOT entry for
1991 // the module index.
1992 unsigned int got_offset;
1993 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1994 - target->got_size());
1995 value = target->got_plt_section()->address() + got_offset;
1996 Relocate_functions<64, false>::pcrela32(view, value, addend,
2000 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2001 _("unsupported reloc %u"), r_type);
2004 case elfcpp::R_X86_64_DTPOFF32:
2005 gold_assert(tls_segment != NULL);
2006 if (optimized_type == tls::TLSOPT_TO_LE)
2007 value -= tls_segment->memsz();
2008 Relocate_functions<64, false>::rela32(view, value, 0);
2011 case elfcpp::R_X86_64_DTPOFF64:
2012 gold_assert(tls_segment != NULL);
2013 if (optimized_type == tls::TLSOPT_TO_LE)
2014 value -= tls_segment->memsz();
2015 Relocate_functions<64, false>::rela64(view, value, 0);
2018 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2019 if (optimized_type == tls::TLSOPT_TO_LE)
2021 gold_assert(tls_segment != NULL);
2022 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2023 rela, r_type, value, view,
2027 else if (optimized_type == tls::TLSOPT_NONE)
2029 // Relocate the field with the offset of the GOT entry for
2030 // the tp-relative offset of the symbol.
2031 unsigned int got_offset;
2034 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2035 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2036 - target->got_size());
2040 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2041 gold_assert(object->local_has_got_offset(r_sym,
2042 GOT_TYPE_TLS_OFFSET));
2043 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2044 - target->got_size());
2046 value = target->got_plt_section()->address() + got_offset;
2047 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2050 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2051 _("unsupported reloc type %u"),
2055 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2056 value -= tls_segment->memsz();
2057 Relocate_functions<64, false>::rela32(view, value, 0);
2062 // Do a relocation in which we convert a TLS General-Dynamic to an
2066 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2069 const elfcpp::Rela<64, false>& rela,
2071 elfcpp::Elf_types<64>::Elf_Addr value,
2072 unsigned char* view,
2073 elfcpp::Elf_types<64>::Elf_Addr address,
2074 section_size_type view_size)
2076 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2077 // .word 0x6666; rex64; call __tls_get_addr
2078 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2080 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2081 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2083 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2084 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2085 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2086 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2088 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2090 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2091 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2093 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2095 this->skip_call_tls_get_addr_ = true;
2098 // Do a relocation in which we convert a TLS General-Dynamic to a
2102 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2104 Output_segment* tls_segment,
2105 const elfcpp::Rela<64, false>& rela,
2107 elfcpp::Elf_types<64>::Elf_Addr value,
2108 unsigned char* view,
2109 section_size_type view_size)
2111 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2112 // .word 0x6666; rex64; call __tls_get_addr
2113 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2115 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2116 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2118 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2119 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2120 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2121 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2123 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2125 value -= tls_segment->memsz();
2126 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2128 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2130 this->skip_call_tls_get_addr_ = true;
2133 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2136 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2137 const Relocate_info<64, false>* relinfo,
2140 const elfcpp::Rela<64, false>& rela,
2141 unsigned int r_type,
2142 elfcpp::Elf_types<64>::Elf_Addr value,
2143 unsigned char* view,
2144 elfcpp::Elf_types<64>::Elf_Addr address,
2145 section_size_type view_size)
2147 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2149 // leaq foo@tlsdesc(%rip), %rax
2150 // ==> movq foo@gottpoff(%rip), %rax
2151 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2152 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2153 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2154 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2156 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2157 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2161 // call *foo@tlscall(%rax)
2163 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2164 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2165 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2166 view[0] == 0xff && view[1] == 0x10);
2172 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2175 Target_x86_64::Relocate::tls_desc_gd_to_le(
2176 const Relocate_info<64, false>* relinfo,
2178 Output_segment* tls_segment,
2179 const elfcpp::Rela<64, false>& rela,
2180 unsigned int r_type,
2181 elfcpp::Elf_types<64>::Elf_Addr value,
2182 unsigned char* view,
2183 section_size_type view_size)
2185 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2187 // leaq foo@tlsdesc(%rip), %rax
2188 // ==> movq foo@tpoff, %rax
2189 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2190 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2191 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2192 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2195 value -= tls_segment->memsz();
2196 Relocate_functions<64, false>::rela32(view, value, 0);
2200 // call *foo@tlscall(%rax)
2202 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2203 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2204 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2205 view[0] == 0xff && view[1] == 0x10);
2212 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2215 const elfcpp::Rela<64, false>& rela,
2217 elfcpp::Elf_types<64>::Elf_Addr,
2218 unsigned char* view,
2219 section_size_type view_size)
2221 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2222 // ... leq foo@dtpoff(%rax),%reg
2223 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2225 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2226 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2228 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2229 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2231 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2233 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2235 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2237 this->skip_call_tls_get_addr_ = true;
2240 // Do a relocation in which we convert a TLS Initial-Exec to a
2244 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2246 Output_segment* tls_segment,
2247 const elfcpp::Rela<64, false>& rela,
2249 elfcpp::Elf_types<64>::Elf_Addr value,
2250 unsigned char* view,
2251 section_size_type view_size)
2253 // We need to examine the opcodes to figure out which instruction we
2256 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2257 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2259 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2260 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2262 unsigned char op1 = view[-3];
2263 unsigned char op2 = view[-2];
2264 unsigned char op3 = view[-1];
2265 unsigned char reg = op3 >> 3;
2273 view[-1] = 0xc0 | reg;
2277 // Special handling for %rsp.
2281 view[-1] = 0xc0 | reg;
2289 view[-1] = 0x80 | reg | (reg << 3);
2292 value -= tls_segment->memsz();
2293 Relocate_functions<64, false>::rela32(view, value, 0);
2296 // Relocate section data.
2299 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2300 unsigned int sh_type,
2301 const unsigned char* prelocs,
2303 Output_section* output_section,
2304 bool needs_special_offset_handling,
2305 unsigned char* view,
2306 elfcpp::Elf_types<64>::Elf_Addr address,
2307 section_size_type view_size)
2309 gold_assert(sh_type == elfcpp::SHT_RELA);
2311 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2312 Target_x86_64::Relocate>(
2318 needs_special_offset_handling,
2324 // Return the size of a relocation while scanning during a relocatable
2328 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2329 unsigned int r_type,
2334 case elfcpp::R_X86_64_NONE:
2335 case elfcpp::R_386_GNU_VTINHERIT:
2336 case elfcpp::R_386_GNU_VTENTRY:
2337 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2338 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2339 case elfcpp::R_X86_64_TLSDESC_CALL:
2340 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2341 case elfcpp::R_X86_64_DTPOFF32:
2342 case elfcpp::R_X86_64_DTPOFF64:
2343 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2344 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2347 case elfcpp::R_X86_64_64:
2348 case elfcpp::R_X86_64_PC64:
2349 case elfcpp::R_X86_64_GOTOFF64:
2350 case elfcpp::R_X86_64_GOTPC64:
2351 case elfcpp::R_X86_64_PLTOFF64:
2352 case elfcpp::R_X86_64_GOT64:
2353 case elfcpp::R_X86_64_GOTPCREL64:
2354 case elfcpp::R_X86_64_GOTPCREL:
2355 case elfcpp::R_X86_64_GOTPLT64:
2358 case elfcpp::R_X86_64_32:
2359 case elfcpp::R_X86_64_32S:
2360 case elfcpp::R_X86_64_PC32:
2361 case elfcpp::R_X86_64_PLT32:
2362 case elfcpp::R_X86_64_GOTPC32:
2363 case elfcpp::R_X86_64_GOT32:
2366 case elfcpp::R_X86_64_16:
2367 case elfcpp::R_X86_64_PC16:
2370 case elfcpp::R_X86_64_8:
2371 case elfcpp::R_X86_64_PC8:
2374 case elfcpp::R_X86_64_COPY:
2375 case elfcpp::R_X86_64_GLOB_DAT:
2376 case elfcpp::R_X86_64_JUMP_SLOT:
2377 case elfcpp::R_X86_64_RELATIVE:
2378 // These are outstanding tls relocs, which are unexpected when linking
2379 case elfcpp::R_X86_64_TPOFF64:
2380 case elfcpp::R_X86_64_DTPMOD64:
2381 case elfcpp::R_X86_64_TLSDESC:
2382 object->error(_("unexpected reloc %u in object file"), r_type);
2385 case elfcpp::R_X86_64_SIZE32:
2386 case elfcpp::R_X86_64_SIZE64:
2388 object->error(_("unsupported reloc %u against local symbol"), r_type);
2393 // Scan the relocs during a relocatable link.
2396 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2397 Symbol_table* symtab,
2399 Sized_relobj<64, false>* object,
2400 unsigned int data_shndx,
2401 unsigned int sh_type,
2402 const unsigned char* prelocs,
2404 Output_section* output_section,
2405 bool needs_special_offset_handling,
2406 size_t local_symbol_count,
2407 const unsigned char* plocal_symbols,
2408 Relocatable_relocs* rr)
2410 gold_assert(sh_type == elfcpp::SHT_RELA);
2412 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2413 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2415 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2416 Scan_relocatable_relocs>(
2425 needs_special_offset_handling,
2431 // Relocate a section during a relocatable link.
2434 Target_x86_64::relocate_for_relocatable(
2435 const Relocate_info<64, false>* relinfo,
2436 unsigned int sh_type,
2437 const unsigned char* prelocs,
2439 Output_section* output_section,
2440 off_t offset_in_output_section,
2441 const Relocatable_relocs* rr,
2442 unsigned char* view,
2443 elfcpp::Elf_types<64>::Elf_Addr view_address,
2444 section_size_type view_size,
2445 unsigned char* reloc_view,
2446 section_size_type reloc_view_size)
2448 gold_assert(sh_type == elfcpp::SHT_RELA);
2450 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2455 offset_in_output_section,
2464 // Return the value to use for a dynamic which requires special
2465 // treatment. This is how we support equality comparisons of function
2466 // pointers across shared library boundaries, as described in the
2467 // processor specific ABI supplement.
2470 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2472 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2473 return this->plt_section()->address() + gsym->plt_offset();
2476 // Return a string used to fill a code section with nops to take up
2477 // the specified length.
2480 Target_x86_64::do_code_fill(section_size_type length) const
2484 // Build a jmpq instruction to skip over the bytes.
2485 unsigned char jmp[5];
2487 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2488 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2489 + std::string(length - 5, '\0'));
2492 // Nop sequences of various lengths.
2493 const char nop1[1] = { 0x90 }; // nop
2494 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2495 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2496 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2497 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2498 0x00 }; // leal 0(%esi,1),%esi
2499 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2501 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2503 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2504 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2505 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2506 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2508 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2509 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2511 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2512 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2514 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2515 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2516 0x00, 0x00, 0x00, 0x00 };
2517 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2518 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2519 0x27, 0x00, 0x00, 0x00,
2521 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2522 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2523 0xbc, 0x27, 0x00, 0x00,
2525 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2526 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2527 0x90, 0x90, 0x90, 0x90,
2530 const char* nops[16] = {
2532 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2533 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2536 return std::string(nops[length], length);
2539 // The selector for x86_64 object files.
2541 class Target_selector_x86_64 : public Target_selector
2544 Target_selector_x86_64()
2545 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2549 do_instantiate_target()
2550 { return new Target_x86_64(); }
2553 Target_selector_x86_64 target_selector_x86_64;
2555 } // End anonymous namespace.