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), saw_tls_block_reloc_(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_;
292 // This is set if we see a relocation which could load the address
293 // of the TLS block. Whether we see such a relocation determines
294 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
295 // in debugging sections.
296 bool saw_tls_block_reloc_;
299 // A class which returns the size required for a relocation type,
300 // used while scanning relocs during a relocatable link.
301 class Relocatable_size_for_reloc
305 get_size_for_reloc(unsigned int, Relobj*);
308 // Adjust TLS relocation type based on the options and whether this
309 // is a local symbol.
310 static tls::Tls_optimization
311 optimize_tls_reloc(bool is_final, int r_type);
313 // Get the GOT section, creating it if necessary.
314 Output_data_got<64, false>*
315 got_section(Symbol_table*, Layout*);
317 // Get the GOT PLT section.
319 got_plt_section() const
321 gold_assert(this->got_plt_ != NULL);
322 return this->got_plt_;
325 // Create the PLT section.
327 make_plt_section(Symbol_table* symtab, Layout* layout);
329 // Create a PLT entry for a global symbol.
331 make_plt_entry(Symbol_table*, Layout*, Symbol*);
333 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
335 define_tls_base_symbol(Symbol_table*, Layout*);
337 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
339 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
341 // Create a GOT entry for the TLS module index.
343 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
344 Sized_relobj<64, false>* object);
346 // Get the PLT section.
347 Output_data_plt_x86_64*
350 gold_assert(this->plt_ != NULL);
354 // Get the dynamic reloc section, creating it if necessary.
356 rela_dyn_section(Layout*);
358 // Return true if the symbol may need a COPY relocation.
359 // References from an executable object to non-function symbols
360 // defined in a dynamic object may need a COPY relocation.
362 may_need_copy_reloc(Symbol* gsym)
364 return (!parameters->options().shared()
365 && gsym->is_from_dynobj()
366 && gsym->type() != elfcpp::STT_FUNC);
369 // Add a potential copy relocation.
371 copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object,
372 unsigned int shndx, Output_section* output_section,
373 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
375 this->copy_relocs_.copy_reloc(symtab, layout,
376 symtab->get_sized_symbol<64>(sym),
377 object, shndx, output_section,
378 reloc, this->rela_dyn_section(layout));
381 // Information about this specific target which we pass to the
382 // general Target structure.
383 static const Target::Target_info x86_64_info;
387 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
388 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
389 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
390 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
394 Output_data_got<64, false>* got_;
396 Output_data_plt_x86_64* plt_;
397 // The GOT PLT section.
398 Output_data_space* got_plt_;
399 // The dynamic reloc section.
400 Reloc_section* rela_dyn_;
401 // Relocs saved to avoid a COPY reloc.
402 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
403 // Space for variables copied with a COPY reloc.
404 Output_data_space* dynbss_;
405 // Offset of the GOT entry for the TLS module index.
406 unsigned int got_mod_index_offset_;
407 // True if the _TLS_MODULE_BASE_ symbol has been defined.
408 bool tls_base_symbol_defined_;
411 const Target::Target_info Target_x86_64::x86_64_info =
414 false, // is_big_endian
415 elfcpp::EM_X86_64, // machine_code
416 false, // has_make_symbol
417 false, // has_resolve
418 true, // has_code_fill
419 true, // is_default_stack_executable
421 "/lib/ld64.so.1", // program interpreter
422 0x400000, // default_text_segment_address
423 0x1000, // abi_pagesize (overridable by -z max-page-size)
424 0x1000 // common_pagesize (overridable by -z common-page-size)
427 // Get the GOT section, creating it if necessary.
429 Output_data_got<64, false>*
430 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
432 if (this->got_ == NULL)
434 gold_assert(symtab != NULL && layout != NULL);
436 this->got_ = new Output_data_got<64, false>();
439 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
441 | elfcpp::SHF_WRITE),
445 // The old GNU linker creates a .got.plt section. We just
446 // create another set of data in the .got section. Note that we
447 // always create a PLT if we create a GOT, although the PLT
449 this->got_plt_ = new Output_data_space(8);
450 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
452 | elfcpp::SHF_WRITE),
456 // The first three entries are reserved.
457 this->got_plt_->set_current_data_size(3 * 8);
459 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
460 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
462 0, 0, elfcpp::STT_OBJECT,
464 elfcpp::STV_HIDDEN, 0,
471 // Get the dynamic reloc section, creating it if necessary.
473 Target_x86_64::Reloc_section*
474 Target_x86_64::rela_dyn_section(Layout* layout)
476 if (this->rela_dyn_ == NULL)
478 gold_assert(layout != NULL);
479 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
480 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
481 elfcpp::SHF_ALLOC, this->rela_dyn_);
483 return this->rela_dyn_;
486 // A class to handle the PLT data.
488 class Output_data_plt_x86_64 : public Output_section_data
491 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
493 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
496 // Add an entry to the PLT.
498 add_entry(Symbol* gsym);
500 // Add the reserved TLSDESC_PLT entry to the PLT.
502 reserve_tlsdesc_entry(unsigned int got_offset)
503 { this->tlsdesc_got_offset_ = got_offset; }
505 // Return true if a TLSDESC_PLT entry has been reserved.
507 has_tlsdesc_entry() const
508 { return this->tlsdesc_got_offset_ != -1U; }
510 // Return the GOT offset for the reserved TLSDESC_PLT entry.
512 get_tlsdesc_got_offset() const
513 { return this->tlsdesc_got_offset_; }
515 // Return the offset of the reserved TLSDESC_PLT entry.
517 get_tlsdesc_plt_offset() const
518 { return (this->count_ + 1) * plt_entry_size; }
520 // Return the .rel.plt section data.
523 { return this->rel_; }
527 do_adjust_output_section(Output_section* os);
530 // The size of an entry in the PLT.
531 static const int plt_entry_size = 16;
533 // The first entry in the PLT.
534 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
535 // procedure linkage table for both programs and shared objects."
536 static unsigned char first_plt_entry[plt_entry_size];
538 // Other entries in the PLT for an executable.
539 static unsigned char plt_entry[plt_entry_size];
541 // The reserved TLSDESC entry in the PLT for an executable.
542 static unsigned char tlsdesc_plt_entry[plt_entry_size];
544 // Set the final size.
546 set_final_data_size();
548 // Write out the PLT data.
550 do_write(Output_file*);
552 // The reloc section.
555 Output_data_got<64, false>* got_;
556 // The .got.plt section.
557 Output_data_space* got_plt_;
558 // The number of PLT entries.
560 // Offset of the reserved TLSDESC_GOT entry when needed.
561 unsigned int tlsdesc_got_offset_;
564 // Create the PLT section. The ordinary .got section is an argument,
565 // since we need to refer to the start. We also create our own .got
566 // section just for PLT entries.
568 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
569 Output_data_got<64, false>* got,
570 Output_data_space* got_plt)
571 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
572 tlsdesc_got_offset_(-1U)
574 this->rel_ = new Reloc_section(false);
575 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
576 elfcpp::SHF_ALLOC, this->rel_);
580 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
582 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
583 // linker, and so do we.
587 // Add an entry to the PLT.
590 Output_data_plt_x86_64::add_entry(Symbol* gsym)
592 gold_assert(!gsym->has_plt_offset());
594 // Note that when setting the PLT offset we skip the initial
595 // reserved PLT entry.
596 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
600 section_offset_type got_offset = this->got_plt_->current_data_size();
602 // Every PLT entry needs a GOT entry which points back to the PLT
603 // entry (this will be changed by the dynamic linker, normally
604 // lazily when the function is called).
605 this->got_plt_->set_current_data_size(got_offset + 8);
607 // Every PLT entry needs a reloc.
608 gsym->set_needs_dynsym_entry();
609 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
612 // Note that we don't need to save the symbol. The contents of the
613 // PLT are independent of which symbols are used. The symbols only
614 // appear in the relocations.
617 // Set the final size.
619 Output_data_plt_x86_64::set_final_data_size()
621 unsigned int count = this->count_;
622 if (this->has_tlsdesc_entry())
624 this->set_data_size((count + 1) * plt_entry_size);
627 // The first entry in the PLT for an executable.
629 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
631 // From AMD64 ABI Draft 0.98, page 76
632 0xff, 0x35, // pushq contents of memory address
633 0, 0, 0, 0, // replaced with address of .got + 8
634 0xff, 0x25, // jmp indirect
635 0, 0, 0, 0, // replaced with address of .got + 16
636 0x90, 0x90, 0x90, 0x90 // noop (x4)
639 // Subsequent entries in the PLT for an executable.
641 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
643 // From AMD64 ABI Draft 0.98, page 76
644 0xff, 0x25, // jmpq indirect
645 0, 0, 0, 0, // replaced with address of symbol in .got
646 0x68, // pushq immediate
647 0, 0, 0, 0, // replaced with offset into relocation table
648 0xe9, // jmpq relative
649 0, 0, 0, 0 // replaced with offset to start of .plt
652 // The reserved TLSDESC entry in the PLT for an executable.
654 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
656 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
657 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
658 0xff, 0x35, // pushq x(%rip)
659 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
660 0xff, 0x25, // jmpq *y(%rip)
661 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
666 // Write out the PLT. This uses the hand-coded instructions above,
667 // and adjusts them as needed. This is specified by the AMD64 ABI.
670 Output_data_plt_x86_64::do_write(Output_file* of)
672 const off_t offset = this->offset();
673 const section_size_type oview_size =
674 convert_to_section_size_type(this->data_size());
675 unsigned char* const oview = of->get_output_view(offset, oview_size);
677 const off_t got_file_offset = this->got_plt_->offset();
678 const section_size_type got_size =
679 convert_to_section_size_type(this->got_plt_->data_size());
680 unsigned char* const got_view = of->get_output_view(got_file_offset,
683 unsigned char* pov = oview;
685 // The base address of the .plt section.
686 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
687 // The base address of the .got section.
688 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
689 // The base address of the PLT portion of the .got section,
690 // which is where the GOT pointer will point, and where the
691 // three reserved GOT entries are located.
692 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
694 memcpy(pov, first_plt_entry, plt_entry_size);
695 // We do a jmp relative to the PC at the end of this instruction.
696 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
698 - (plt_address + 6)));
699 elfcpp::Swap<32, false>::writeval(pov + 8,
701 - (plt_address + 12)));
702 pov += plt_entry_size;
704 unsigned char* got_pov = got_view;
706 memset(got_pov, 0, 24);
709 unsigned int plt_offset = plt_entry_size;
710 unsigned int got_offset = 24;
711 const unsigned int count = this->count_;
712 for (unsigned int plt_index = 0;
715 pov += plt_entry_size,
717 plt_offset += plt_entry_size,
720 // Set and adjust the PLT entry itself.
721 memcpy(pov, plt_entry, plt_entry_size);
722 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
723 (got_address + got_offset
724 - (plt_address + plt_offset
727 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
728 elfcpp::Swap<32, false>::writeval(pov + 12,
729 - (plt_offset + plt_entry_size));
731 // Set the entry in the GOT.
732 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
735 if (this->has_tlsdesc_entry())
737 // Set and adjust the reserved TLSDESC PLT entry.
738 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
739 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
740 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
742 - (plt_address + plt_offset
744 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
747 - (plt_address + plt_offset
749 pov += plt_entry_size;
752 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
753 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
755 of->write_output_view(offset, oview_size, oview);
756 of->write_output_view(got_file_offset, got_size, got_view);
759 // Create the PLT section.
762 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
764 if (this->plt_ == NULL)
766 // Create the GOT sections first.
767 this->got_section(symtab, layout);
769 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
771 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
773 | elfcpp::SHF_EXECINSTR),
778 // Create a PLT entry for a global symbol.
781 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
784 if (gsym->has_plt_offset())
787 if (this->plt_ == NULL)
788 this->make_plt_section(symtab, layout);
790 this->plt_->add_entry(gsym);
793 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
796 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
798 if (this->tls_base_symbol_defined_)
801 Output_segment* tls_segment = layout->tls_segment();
802 if (tls_segment != NULL)
804 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
808 elfcpp::STV_HIDDEN, 0,
809 Symbol::SEGMENT_END, true);
811 this->tls_base_symbol_defined_ = true;
814 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
817 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
820 if (this->plt_ == NULL)
821 this->make_plt_section(symtab, layout);
823 if (!this->plt_->has_tlsdesc_entry())
825 // Allocate the TLSDESC_GOT entry.
826 Output_data_got<64, false>* got = this->got_section(symtab, layout);
827 unsigned int got_offset = got->add_constant(0);
829 // Allocate the TLSDESC_PLT entry.
830 this->plt_->reserve_tlsdesc_entry(got_offset);
834 // Create a GOT entry for the TLS module index.
837 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
838 Sized_relobj<64, false>* object)
840 if (this->got_mod_index_offset_ == -1U)
842 gold_assert(symtab != NULL && layout != NULL && object != NULL);
843 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
844 Output_data_got<64, false>* got = this->got_section(symtab, layout);
845 unsigned int got_offset = got->add_constant(0);
846 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
848 got->add_constant(0);
849 this->got_mod_index_offset_ = got_offset;
851 return this->got_mod_index_offset_;
854 // Optimize the TLS relocation type based on what we know about the
855 // symbol. IS_FINAL is true if the final address of this symbol is
856 // known at link time.
858 tls::Tls_optimization
859 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
861 // If we are generating a shared library, then we can't do anything
863 if (parameters->options().shared())
864 return tls::TLSOPT_NONE;
868 case elfcpp::R_X86_64_TLSGD:
869 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
870 case elfcpp::R_X86_64_TLSDESC_CALL:
871 // These are General-Dynamic which permits fully general TLS
872 // access. Since we know that we are generating an executable,
873 // we can convert this to Initial-Exec. If we also know that
874 // this is a local symbol, we can further switch to Local-Exec.
876 return tls::TLSOPT_TO_LE;
877 return tls::TLSOPT_TO_IE;
879 case elfcpp::R_X86_64_TLSLD:
880 // This is Local-Dynamic, which refers to a local symbol in the
881 // dynamic TLS block. Since we know that we generating an
882 // executable, we can switch to Local-Exec.
883 return tls::TLSOPT_TO_LE;
885 case elfcpp::R_X86_64_DTPOFF32:
886 case elfcpp::R_X86_64_DTPOFF64:
887 // Another Local-Dynamic reloc.
888 return tls::TLSOPT_TO_LE;
890 case elfcpp::R_X86_64_GOTTPOFF:
891 // These are Initial-Exec relocs which get the thread offset
892 // from the GOT. If we know that we are linking against the
893 // local symbol, we can switch to Local-Exec, which links the
894 // thread offset into the instruction.
896 return tls::TLSOPT_TO_LE;
897 return tls::TLSOPT_NONE;
899 case elfcpp::R_X86_64_TPOFF32:
900 // When we already have Local-Exec, there is nothing further we
902 return tls::TLSOPT_NONE;
909 // Report an unsupported relocation against a local symbol.
912 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
915 gold_error(_("%s: unsupported reloc %u against local symbol"),
916 object->name().c_str(), r_type);
919 // We are about to emit a dynamic relocation of type R_TYPE. If the
920 // dynamic linker does not support it, issue an error. The GNU linker
921 // only issues a non-PIC error for an allocated read-only section.
922 // Here we know the section is allocated, but we don't know that it is
923 // read-only. But we check for all the relocation types which the
924 // glibc dynamic linker supports, so it seems appropriate to issue an
925 // error even if the section is not read-only.
928 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
932 // These are the relocation types supported by glibc for x86_64.
933 case elfcpp::R_X86_64_RELATIVE:
934 case elfcpp::R_X86_64_GLOB_DAT:
935 case elfcpp::R_X86_64_JUMP_SLOT:
936 case elfcpp::R_X86_64_DTPMOD64:
937 case elfcpp::R_X86_64_DTPOFF64:
938 case elfcpp::R_X86_64_TPOFF64:
939 case elfcpp::R_X86_64_64:
940 case elfcpp::R_X86_64_32:
941 case elfcpp::R_X86_64_PC32:
942 case elfcpp::R_X86_64_COPY:
946 // This prevents us from issuing more than one error per reloc
947 // section. But we can still wind up issuing more than one
948 // error per object file.
949 if (this->issued_non_pic_error_)
951 object->error(_("requires unsupported dynamic reloc; "
952 "recompile with -fPIC"));
953 this->issued_non_pic_error_ = true;
956 case elfcpp::R_X86_64_NONE:
961 // Scan a relocation for a local symbol.
964 Target_x86_64::Scan::local(const General_options&,
965 Symbol_table* symtab,
967 Target_x86_64* target,
968 Sized_relobj<64, false>* object,
969 unsigned int data_shndx,
970 Output_section* output_section,
971 const elfcpp::Rela<64, false>& reloc,
973 const elfcpp::Sym<64, false>& lsym)
977 case elfcpp::R_X86_64_NONE:
978 case elfcpp::R_386_GNU_VTINHERIT:
979 case elfcpp::R_386_GNU_VTENTRY:
982 case elfcpp::R_X86_64_64:
983 // If building a shared library (or a position-independent
984 // executable), we need to create a dynamic relocation for this
985 // location. The relocation applied at link time will apply the
986 // link-time value, so we flag the location with an
987 // R_X86_64_RELATIVE relocation so the dynamic loader can
988 // relocate it easily.
989 if (parameters->options().output_is_position_independent())
991 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
992 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
993 rela_dyn->add_local_relative(object, r_sym,
994 elfcpp::R_X86_64_RELATIVE,
995 output_section, data_shndx,
996 reloc.get_r_offset(),
997 reloc.get_r_addend());
1001 case elfcpp::R_X86_64_32:
1002 case elfcpp::R_X86_64_32S:
1003 case elfcpp::R_X86_64_16:
1004 case elfcpp::R_X86_64_8:
1005 // If building a shared library (or a position-independent
1006 // executable), we need to create a dynamic relocation for this
1007 // location. We can't use an R_X86_64_RELATIVE relocation
1008 // because that is always a 64-bit relocation.
1009 if (parameters->options().output_is_position_independent())
1011 this->check_non_pic(object, r_type);
1013 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1014 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1015 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1016 rela_dyn->add_local(object, r_sym, r_type, output_section,
1017 data_shndx, reloc.get_r_offset(),
1018 reloc.get_r_addend());
1021 gold_assert(lsym.get_st_value() == 0);
1022 unsigned int shndx = lsym.get_st_shndx();
1024 shndx = object->adjust_sym_shndx(r_sym, shndx,
1027 object->error(_("section symbol %u has bad shndx %u"),
1030 rela_dyn->add_local_section(object, shndx,
1031 r_type, output_section,
1032 data_shndx, reloc.get_r_offset(),
1033 reloc.get_r_addend());
1038 case elfcpp::R_X86_64_PC64:
1039 case elfcpp::R_X86_64_PC32:
1040 case elfcpp::R_X86_64_PC16:
1041 case elfcpp::R_X86_64_PC8:
1044 case elfcpp::R_X86_64_PLT32:
1045 // Since we know this is a local symbol, we can handle this as a
1049 case elfcpp::R_X86_64_GOTPC32:
1050 case elfcpp::R_X86_64_GOTOFF64:
1051 case elfcpp::R_X86_64_GOTPC64:
1052 case elfcpp::R_X86_64_PLTOFF64:
1053 // We need a GOT section.
1054 target->got_section(symtab, layout);
1055 // For PLTOFF64, we'd normally want a PLT section, but since we
1056 // know this is a local symbol, no PLT is needed.
1059 case elfcpp::R_X86_64_GOT64:
1060 case elfcpp::R_X86_64_GOT32:
1061 case elfcpp::R_X86_64_GOTPCREL64:
1062 case elfcpp::R_X86_64_GOTPCREL:
1063 case elfcpp::R_X86_64_GOTPLT64:
1065 // The symbol requires a GOT entry.
1066 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1067 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1068 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1070 // If we are generating a shared object, we need to add a
1071 // dynamic relocation for this symbol's GOT entry.
1072 if (parameters->options().output_is_position_independent())
1074 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1075 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1076 if (r_type != elfcpp::R_X86_64_GOT32)
1077 rela_dyn->add_local_relative(
1078 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1079 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1082 this->check_non_pic(object, r_type);
1084 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1085 rela_dyn->add_local(
1086 object, r_sym, r_type, got,
1087 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1091 // For GOTPLT64, we'd normally want a PLT section, but since
1092 // we know this is a local symbol, no PLT is needed.
1096 case elfcpp::R_X86_64_COPY:
1097 case elfcpp::R_X86_64_GLOB_DAT:
1098 case elfcpp::R_X86_64_JUMP_SLOT:
1099 case elfcpp::R_X86_64_RELATIVE:
1100 // These are outstanding tls relocs, which are unexpected when linking
1101 case elfcpp::R_X86_64_TPOFF64:
1102 case elfcpp::R_X86_64_DTPMOD64:
1103 case elfcpp::R_X86_64_TLSDESC:
1104 gold_error(_("%s: unexpected reloc %u in object file"),
1105 object->name().c_str(), r_type);
1108 // These are initial tls relocs, which are expected when linking
1109 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1110 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1111 case elfcpp::R_X86_64_TLSDESC_CALL:
1112 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1113 case elfcpp::R_X86_64_DTPOFF32:
1114 case elfcpp::R_X86_64_DTPOFF64:
1115 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1116 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1118 bool output_is_shared = parameters->options().shared();
1119 const tls::Tls_optimization optimized_type
1120 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1123 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1124 if (optimized_type == tls::TLSOPT_NONE)
1126 // Create a pair of GOT entries for the module index and
1127 // dtv-relative offset.
1128 Output_data_got<64, false>* got
1129 = target->got_section(symtab, layout);
1130 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1131 unsigned int shndx = lsym.get_st_shndx();
1133 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1135 object->error(_("local symbol %u has bad shndx %u"),
1138 got->add_local_pair_with_rela(object, r_sym,
1141 target->rela_dyn_section(layout),
1142 elfcpp::R_X86_64_DTPMOD64, 0);
1144 else if (optimized_type != tls::TLSOPT_TO_LE)
1145 unsupported_reloc_local(object, r_type);
1148 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1149 target->define_tls_base_symbol(symtab, layout);
1150 if (optimized_type == tls::TLSOPT_NONE)
1152 // Create reserved PLT and GOT entries for the resolver.
1153 target->reserve_tlsdesc_entries(symtab, layout);
1155 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1156 Output_data_got<64, false>* got
1157 = target->got_section(symtab, layout);
1158 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1159 unsigned int shndx = lsym.get_st_shndx();
1161 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1163 object->error(_("local symbol %u has bad shndx %u"),
1166 got->add_local_pair_with_rela(object, r_sym,
1169 target->rela_dyn_section(layout),
1170 elfcpp::R_X86_64_TLSDESC, 0);
1172 else if (optimized_type != tls::TLSOPT_TO_LE)
1173 unsupported_reloc_local(object, r_type);
1176 case elfcpp::R_X86_64_TLSDESC_CALL:
1179 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1180 if (optimized_type == tls::TLSOPT_NONE)
1182 // Create a GOT entry for the module index.
1183 target->got_mod_index_entry(symtab, layout, object);
1185 else if (optimized_type != tls::TLSOPT_TO_LE)
1186 unsupported_reloc_local(object, r_type);
1189 case elfcpp::R_X86_64_DTPOFF32:
1190 case elfcpp::R_X86_64_DTPOFF64:
1193 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1194 layout->set_has_static_tls();
1195 if (optimized_type == tls::TLSOPT_NONE)
1197 // Create a GOT entry for the tp-relative offset.
1198 Output_data_got<64, false>* got
1199 = target->got_section(symtab, layout);
1200 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1201 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1202 target->rela_dyn_section(layout),
1203 elfcpp::R_X86_64_TPOFF64);
1205 else if (optimized_type != tls::TLSOPT_TO_LE)
1206 unsupported_reloc_local(object, r_type);
1209 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1210 layout->set_has_static_tls();
1211 if (output_is_shared)
1212 unsupported_reloc_local(object, r_type);
1221 case elfcpp::R_X86_64_SIZE32:
1222 case elfcpp::R_X86_64_SIZE64:
1224 gold_error(_("%s: unsupported reloc %u against local symbol"),
1225 object->name().c_str(), r_type);
1231 // Report an unsupported relocation against a global symbol.
1234 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1235 unsigned int r_type,
1238 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1239 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1242 // Scan a relocation for a global symbol.
1245 Target_x86_64::Scan::global(const General_options&,
1246 Symbol_table* symtab,
1248 Target_x86_64* target,
1249 Sized_relobj<64, false>* object,
1250 unsigned int data_shndx,
1251 Output_section* output_section,
1252 const elfcpp::Rela<64, false>& reloc,
1253 unsigned int r_type,
1258 case elfcpp::R_X86_64_NONE:
1259 case elfcpp::R_386_GNU_VTINHERIT:
1260 case elfcpp::R_386_GNU_VTENTRY:
1263 case elfcpp::R_X86_64_64:
1264 case elfcpp::R_X86_64_32:
1265 case elfcpp::R_X86_64_32S:
1266 case elfcpp::R_X86_64_16:
1267 case elfcpp::R_X86_64_8:
1269 // Make a PLT entry if necessary.
1270 if (gsym->needs_plt_entry())
1272 target->make_plt_entry(symtab, layout, gsym);
1273 // Since this is not a PC-relative relocation, we may be
1274 // taking the address of a function. In that case we need to
1275 // set the entry in the dynamic symbol table to the address of
1277 if (gsym->is_from_dynobj() && !parameters->options().shared())
1278 gsym->set_needs_dynsym_value();
1280 // Make a dynamic relocation if necessary.
1281 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1283 if (target->may_need_copy_reloc(gsym))
1285 target->copy_reloc(symtab, layout, object,
1286 data_shndx, output_section, gsym, reloc);
1288 else if (r_type == elfcpp::R_X86_64_64
1289 && gsym->can_use_relative_reloc(false))
1291 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1292 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1293 output_section, object,
1294 data_shndx, reloc.get_r_offset(),
1295 reloc.get_r_addend());
1299 this->check_non_pic(object, r_type);
1300 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1301 rela_dyn->add_global(gsym, r_type, output_section, object,
1302 data_shndx, reloc.get_r_offset(),
1303 reloc.get_r_addend());
1309 case elfcpp::R_X86_64_PC64:
1310 case elfcpp::R_X86_64_PC32:
1311 case elfcpp::R_X86_64_PC16:
1312 case elfcpp::R_X86_64_PC8:
1314 // Make a PLT entry if necessary.
1315 if (gsym->needs_plt_entry())
1316 target->make_plt_entry(symtab, layout, gsym);
1317 // Make a dynamic relocation if necessary.
1318 int flags = Symbol::NON_PIC_REF;
1319 if (gsym->type() == elfcpp::STT_FUNC)
1320 flags |= Symbol::FUNCTION_CALL;
1321 if (gsym->needs_dynamic_reloc(flags))
1323 if (target->may_need_copy_reloc(gsym))
1325 target->copy_reloc(symtab, layout, object,
1326 data_shndx, output_section, gsym, reloc);
1330 this->check_non_pic(object, r_type);
1331 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1332 rela_dyn->add_global(gsym, r_type, output_section, object,
1333 data_shndx, reloc.get_r_offset(),
1334 reloc.get_r_addend());
1340 case elfcpp::R_X86_64_GOT64:
1341 case elfcpp::R_X86_64_GOT32:
1342 case elfcpp::R_X86_64_GOTPCREL64:
1343 case elfcpp::R_X86_64_GOTPCREL:
1344 case elfcpp::R_X86_64_GOTPLT64:
1346 // The symbol requires a GOT entry.
1347 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1348 if (gsym->final_value_is_known())
1349 got->add_global(gsym, GOT_TYPE_STANDARD);
1352 // If this symbol is not fully resolved, we need to add a
1353 // dynamic relocation for it.
1354 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1355 if (gsym->is_from_dynobj()
1356 || gsym->is_undefined()
1357 || gsym->is_preemptible())
1358 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1359 elfcpp::R_X86_64_GLOB_DAT);
1362 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1363 rela_dyn->add_global_relative(
1364 gsym, elfcpp::R_X86_64_RELATIVE, got,
1365 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1368 // For GOTPLT64, we also need a PLT entry (but only if the
1369 // symbol is not fully resolved).
1370 if (r_type == elfcpp::R_X86_64_GOTPLT64
1371 && !gsym->final_value_is_known())
1372 target->make_plt_entry(symtab, layout, gsym);
1376 case elfcpp::R_X86_64_PLT32:
1377 // If the symbol is fully resolved, this is just a PC32 reloc.
1378 // Otherwise we need a PLT entry.
1379 if (gsym->final_value_is_known())
1381 // If building a shared library, we can also skip the PLT entry
1382 // if the symbol is defined in the output file and is protected
1384 if (gsym->is_defined()
1385 && !gsym->is_from_dynobj()
1386 && !gsym->is_preemptible())
1388 target->make_plt_entry(symtab, layout, gsym);
1391 case elfcpp::R_X86_64_GOTPC32:
1392 case elfcpp::R_X86_64_GOTOFF64:
1393 case elfcpp::R_X86_64_GOTPC64:
1394 case elfcpp::R_X86_64_PLTOFF64:
1395 // We need a GOT section.
1396 target->got_section(symtab, layout);
1397 // For PLTOFF64, we also need a PLT entry (but only if the
1398 // symbol is not fully resolved).
1399 if (r_type == elfcpp::R_X86_64_PLTOFF64
1400 && !gsym->final_value_is_known())
1401 target->make_plt_entry(symtab, layout, gsym);
1404 case elfcpp::R_X86_64_COPY:
1405 case elfcpp::R_X86_64_GLOB_DAT:
1406 case elfcpp::R_X86_64_JUMP_SLOT:
1407 case elfcpp::R_X86_64_RELATIVE:
1408 // These are outstanding tls relocs, which are unexpected when linking
1409 case elfcpp::R_X86_64_TPOFF64:
1410 case elfcpp::R_X86_64_DTPMOD64:
1411 case elfcpp::R_X86_64_TLSDESC:
1412 gold_error(_("%s: unexpected reloc %u in object file"),
1413 object->name().c_str(), r_type);
1416 // These are initial tls relocs, which are expected for global()
1417 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1418 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1419 case elfcpp::R_X86_64_TLSDESC_CALL:
1420 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1421 case elfcpp::R_X86_64_DTPOFF32:
1422 case elfcpp::R_X86_64_DTPOFF64:
1423 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1424 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1426 const bool is_final = gsym->final_value_is_known();
1427 const tls::Tls_optimization optimized_type
1428 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1431 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1432 if (optimized_type == tls::TLSOPT_NONE)
1434 // Create a pair of GOT entries for the module index and
1435 // dtv-relative offset.
1436 Output_data_got<64, false>* got
1437 = target->got_section(symtab, layout);
1438 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1439 target->rela_dyn_section(layout),
1440 elfcpp::R_X86_64_DTPMOD64,
1441 elfcpp::R_X86_64_DTPOFF64);
1443 else if (optimized_type == tls::TLSOPT_TO_IE)
1445 // Create a GOT entry for the tp-relative offset.
1446 Output_data_got<64, false>* got
1447 = target->got_section(symtab, layout);
1448 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1449 target->rela_dyn_section(layout),
1450 elfcpp::R_X86_64_TPOFF64);
1452 else if (optimized_type != tls::TLSOPT_TO_LE)
1453 unsupported_reloc_global(object, r_type, gsym);
1456 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1457 target->define_tls_base_symbol(symtab, layout);
1458 if (optimized_type == tls::TLSOPT_NONE)
1460 // Create reserved PLT and GOT entries for the resolver.
1461 target->reserve_tlsdesc_entries(symtab, layout);
1463 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1464 Output_data_got<64, false>* got
1465 = target->got_section(symtab, layout);
1466 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1467 target->rela_dyn_section(layout),
1468 elfcpp::R_X86_64_TLSDESC, 0);
1470 else if (optimized_type == tls::TLSOPT_TO_IE)
1472 // Create a GOT entry for the tp-relative offset.
1473 Output_data_got<64, false>* got
1474 = target->got_section(symtab, layout);
1475 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1476 target->rela_dyn_section(layout),
1477 elfcpp::R_X86_64_TPOFF64);
1479 else if (optimized_type != tls::TLSOPT_TO_LE)
1480 unsupported_reloc_global(object, r_type, gsym);
1483 case elfcpp::R_X86_64_TLSDESC_CALL:
1486 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1487 if (optimized_type == tls::TLSOPT_NONE)
1489 // Create a GOT entry for the module index.
1490 target->got_mod_index_entry(symtab, layout, object);
1492 else if (optimized_type != tls::TLSOPT_TO_LE)
1493 unsupported_reloc_global(object, r_type, gsym);
1496 case elfcpp::R_X86_64_DTPOFF32:
1497 case elfcpp::R_X86_64_DTPOFF64:
1500 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1501 layout->set_has_static_tls();
1502 if (optimized_type == tls::TLSOPT_NONE)
1504 // Create a GOT entry for the tp-relative offset.
1505 Output_data_got<64, false>* got
1506 = target->got_section(symtab, layout);
1507 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1508 target->rela_dyn_section(layout),
1509 elfcpp::R_X86_64_TPOFF64);
1511 else if (optimized_type != tls::TLSOPT_TO_LE)
1512 unsupported_reloc_global(object, r_type, gsym);
1515 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1516 layout->set_has_static_tls();
1517 if (parameters->options().shared())
1518 unsupported_reloc_local(object, r_type);
1527 case elfcpp::R_X86_64_SIZE32:
1528 case elfcpp::R_X86_64_SIZE64:
1530 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1531 object->name().c_str(), r_type,
1532 gsym->demangled_name().c_str());
1537 // Scan relocations for a section.
1540 Target_x86_64::scan_relocs(const General_options& options,
1541 Symbol_table* symtab,
1543 Sized_relobj<64, false>* object,
1544 unsigned int data_shndx,
1545 unsigned int sh_type,
1546 const unsigned char* prelocs,
1548 Output_section* output_section,
1549 bool needs_special_offset_handling,
1550 size_t local_symbol_count,
1551 const unsigned char* plocal_symbols)
1553 if (sh_type == elfcpp::SHT_REL)
1555 gold_error(_("%s: unsupported REL reloc section"),
1556 object->name().c_str());
1560 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1561 Target_x86_64::Scan>(
1571 needs_special_offset_handling,
1576 // Finalize the sections.
1579 Target_x86_64::do_finalize_sections(Layout* layout)
1581 // Fill in some more dynamic tags.
1582 Output_data_dynamic* const odyn = layout->dynamic_data();
1585 if (this->got_plt_ != NULL)
1586 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1588 if (this->plt_ != NULL)
1590 const Output_data* od = this->plt_->rel_plt();
1591 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1592 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1593 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1594 if (this->plt_->has_tlsdesc_entry())
1596 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1597 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1598 this->got_->finalize_data_size();
1599 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1600 this->plt_, plt_offset);
1601 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1602 this->got_, got_offset);
1606 if (this->rela_dyn_ != NULL)
1608 const Output_data* od = this->rela_dyn_;
1609 odyn->add_section_address(elfcpp::DT_RELA, od);
1610 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1611 odyn->add_constant(elfcpp::DT_RELAENT,
1612 elfcpp::Elf_sizes<64>::rela_size);
1615 if (!parameters->options().shared())
1617 // The value of the DT_DEBUG tag is filled in by the dynamic
1618 // linker at run time, and used by the debugger.
1619 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1623 // Emit any relocs we saved in an attempt to avoid generating COPY
1625 if (this->copy_relocs_.any_saved_relocs())
1626 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1629 // Perform a relocation.
1632 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1633 Target_x86_64* target,
1635 const elfcpp::Rela<64, false>& rela,
1636 unsigned int r_type,
1637 const Sized_symbol<64>* gsym,
1638 const Symbol_value<64>* psymval,
1639 unsigned char* view,
1640 elfcpp::Elf_types<64>::Elf_Addr address,
1641 section_size_type view_size)
1643 if (this->skip_call_tls_get_addr_)
1645 if (r_type != elfcpp::R_X86_64_PLT32
1647 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1649 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1650 _("missing expected TLS relocation"));
1654 this->skip_call_tls_get_addr_ = false;
1659 // Pick the value to use for symbols defined in shared objects.
1660 Symbol_value<64> symval;
1662 && (gsym->is_from_dynobj()
1663 || (parameters->options().shared()
1664 && (gsym->is_undefined() || gsym->is_preemptible())))
1665 && gsym->has_plt_offset())
1667 symval.set_output_value(target->plt_section()->address()
1668 + gsym->plt_offset());
1672 const Sized_relobj<64, false>* object = relinfo->object;
1673 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1675 // Get the GOT offset if needed.
1676 // The GOT pointer points to the end of the GOT section.
1677 // We need to subtract the size of the GOT section to get
1678 // the actual offset to use in the relocation.
1679 bool have_got_offset = false;
1680 unsigned int got_offset = 0;
1683 case elfcpp::R_X86_64_GOT32:
1684 case elfcpp::R_X86_64_GOT64:
1685 case elfcpp::R_X86_64_GOTPLT64:
1686 case elfcpp::R_X86_64_GOTPCREL:
1687 case elfcpp::R_X86_64_GOTPCREL64:
1690 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1691 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1695 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1696 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1697 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1698 - target->got_size());
1700 have_got_offset = true;
1709 case elfcpp::R_X86_64_NONE:
1710 case elfcpp::R_386_GNU_VTINHERIT:
1711 case elfcpp::R_386_GNU_VTENTRY:
1714 case elfcpp::R_X86_64_64:
1715 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1718 case elfcpp::R_X86_64_PC64:
1719 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1723 case elfcpp::R_X86_64_32:
1724 // FIXME: we need to verify that value + addend fits into 32 bits:
1725 // uint64_t x = value + addend;
1726 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1727 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1728 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1731 case elfcpp::R_X86_64_32S:
1732 // FIXME: we need to verify that value + addend fits into 32 bits:
1733 // int64_t x = value + addend; // note this quantity is signed!
1734 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1735 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1738 case elfcpp::R_X86_64_PC32:
1739 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1743 case elfcpp::R_X86_64_16:
1744 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1747 case elfcpp::R_X86_64_PC16:
1748 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1752 case elfcpp::R_X86_64_8:
1753 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1756 case elfcpp::R_X86_64_PC8:
1757 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1761 case elfcpp::R_X86_64_PLT32:
1762 gold_assert(gsym == NULL
1763 || gsym->has_plt_offset()
1764 || gsym->final_value_is_known()
1765 || (gsym->is_defined()
1766 && !gsym->is_from_dynobj()
1767 && !gsym->is_preemptible()));
1768 // Note: while this code looks the same as for R_X86_64_PC32, it
1769 // behaves differently because psymval was set to point to
1770 // the PLT entry, rather than the symbol, in Scan::global().
1771 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1775 case elfcpp::R_X86_64_PLTOFF64:
1778 gold_assert(gsym->has_plt_offset()
1779 || gsym->final_value_is_known());
1780 elfcpp::Elf_types<64>::Elf_Addr got_address;
1781 got_address = target->got_section(NULL, NULL)->address();
1782 Relocate_functions<64, false>::rela64(view, object, psymval,
1783 addend - got_address);
1786 case elfcpp::R_X86_64_GOT32:
1787 gold_assert(have_got_offset);
1788 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1791 case elfcpp::R_X86_64_GOTPC32:
1794 elfcpp::Elf_types<64>::Elf_Addr value;
1795 value = target->got_plt_section()->address();
1796 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1800 case elfcpp::R_X86_64_GOT64:
1801 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1802 // Since we always add a PLT entry, this is equivalent.
1803 case elfcpp::R_X86_64_GOTPLT64:
1804 gold_assert(have_got_offset);
1805 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1808 case elfcpp::R_X86_64_GOTPC64:
1811 elfcpp::Elf_types<64>::Elf_Addr value;
1812 value = target->got_plt_section()->address();
1813 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1817 case elfcpp::R_X86_64_GOTOFF64:
1819 elfcpp::Elf_types<64>::Elf_Addr value;
1820 value = (psymval->value(object, 0)
1821 - target->got_plt_section()->address());
1822 Relocate_functions<64, false>::rela64(view, value, addend);
1826 case elfcpp::R_X86_64_GOTPCREL:
1828 gold_assert(have_got_offset);
1829 elfcpp::Elf_types<64>::Elf_Addr value;
1830 value = target->got_plt_section()->address() + got_offset;
1831 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1835 case elfcpp::R_X86_64_GOTPCREL64:
1837 gold_assert(have_got_offset);
1838 elfcpp::Elf_types<64>::Elf_Addr value;
1839 value = target->got_plt_section()->address() + got_offset;
1840 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1844 case elfcpp::R_X86_64_COPY:
1845 case elfcpp::R_X86_64_GLOB_DAT:
1846 case elfcpp::R_X86_64_JUMP_SLOT:
1847 case elfcpp::R_X86_64_RELATIVE:
1848 // These are outstanding tls relocs, which are unexpected when linking
1849 case elfcpp::R_X86_64_TPOFF64:
1850 case elfcpp::R_X86_64_DTPMOD64:
1851 case elfcpp::R_X86_64_TLSDESC:
1852 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1853 _("unexpected reloc %u in object file"),
1857 // These are initial tls relocs, which are expected when linking
1858 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1859 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1860 case elfcpp::R_X86_64_TLSDESC_CALL:
1861 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1862 case elfcpp::R_X86_64_DTPOFF32:
1863 case elfcpp::R_X86_64_DTPOFF64:
1864 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1865 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1866 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1867 view, address, view_size);
1870 case elfcpp::R_X86_64_SIZE32:
1871 case elfcpp::R_X86_64_SIZE64:
1873 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1874 _("unsupported reloc %u"),
1882 // Perform a TLS relocation.
1885 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1886 Target_x86_64* target,
1888 const elfcpp::Rela<64, false>& rela,
1889 unsigned int r_type,
1890 const Sized_symbol<64>* gsym,
1891 const Symbol_value<64>* psymval,
1892 unsigned char* view,
1893 elfcpp::Elf_types<64>::Elf_Addr address,
1894 section_size_type view_size)
1896 Output_segment* tls_segment = relinfo->layout->tls_segment();
1898 const Sized_relobj<64, false>* object = relinfo->object;
1899 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1901 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1903 const bool is_final = (gsym == NULL
1904 ? !parameters->options().output_is_position_independent()
1905 : gsym->final_value_is_known());
1906 const tls::Tls_optimization optimized_type
1907 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1910 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1911 this->saw_tls_block_reloc_ = true;
1912 if (optimized_type == tls::TLSOPT_TO_LE)
1914 gold_assert(tls_segment != NULL);
1915 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1916 rela, r_type, value, view,
1922 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1923 ? GOT_TYPE_TLS_OFFSET
1924 : GOT_TYPE_TLS_PAIR);
1925 unsigned int got_offset;
1928 gold_assert(gsym->has_got_offset(got_type));
1929 got_offset = gsym->got_offset(got_type) - target->got_size();
1933 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1934 gold_assert(object->local_has_got_offset(r_sym, got_type));
1935 got_offset = (object->local_got_offset(r_sym, got_type)
1936 - target->got_size());
1938 if (optimized_type == tls::TLSOPT_TO_IE)
1940 gold_assert(tls_segment != NULL);
1941 value = target->got_plt_section()->address() + got_offset;
1942 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1943 value, view, address, view_size);
1946 else if (optimized_type == tls::TLSOPT_NONE)
1948 // Relocate the field with the offset of the pair of GOT
1950 value = target->got_plt_section()->address() + got_offset;
1951 Relocate_functions<64, false>::pcrela32(view, value, addend,
1956 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1957 _("unsupported reloc %u"), r_type);
1960 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1961 case elfcpp::R_X86_64_TLSDESC_CALL:
1962 this->saw_tls_block_reloc_ = true;
1963 if (optimized_type == tls::TLSOPT_TO_LE)
1965 gold_assert(tls_segment != NULL);
1966 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1967 rela, r_type, value, view,
1973 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1974 ? GOT_TYPE_TLS_OFFSET
1975 : GOT_TYPE_TLS_DESC);
1976 unsigned int got_offset;
1979 gold_assert(gsym->has_got_offset(got_type));
1980 got_offset = gsym->got_offset(got_type) - target->got_size();
1984 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1985 gold_assert(object->local_has_got_offset(r_sym, got_type));
1986 got_offset = (object->local_got_offset(r_sym, got_type)
1987 - target->got_size());
1989 if (optimized_type == tls::TLSOPT_TO_IE)
1991 gold_assert(tls_segment != NULL);
1992 value = target->got_plt_section()->address() + got_offset;
1993 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
1994 rela, r_type, value, view, address,
1998 else if (optimized_type == tls::TLSOPT_NONE)
2000 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2002 // Relocate the field with the offset of the pair of GOT
2004 value = target->got_plt_section()->address() + got_offset;
2005 Relocate_functions<64, false>::pcrela32(view, value, addend,
2011 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2012 _("unsupported reloc %u"), r_type);
2015 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2016 this->saw_tls_block_reloc_ = true;
2017 if (optimized_type == tls::TLSOPT_TO_LE)
2019 gold_assert(tls_segment != NULL);
2020 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2021 value, view, view_size);
2024 else if (optimized_type == tls::TLSOPT_NONE)
2026 // Relocate the field with the offset of the GOT entry for
2027 // the module index.
2028 unsigned int got_offset;
2029 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2030 - target->got_size());
2031 value = target->got_plt_section()->address() + got_offset;
2032 Relocate_functions<64, false>::pcrela32(view, value, addend,
2036 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2037 _("unsupported reloc %u"), r_type);
2040 case elfcpp::R_X86_64_DTPOFF32:
2041 gold_assert(tls_segment != NULL);
2042 if (optimized_type == tls::TLSOPT_TO_LE)
2044 // This relocation type is used in debugging information.
2045 // In that case we need to not optimize the value. If we
2046 // haven't seen a TLSLD reloc, then we assume we should not
2047 // optimize this reloc.
2048 if (this->saw_tls_block_reloc_)
2049 value -= tls_segment->memsz();
2051 Relocate_functions<64, false>::rela32(view, value, 0);
2054 case elfcpp::R_X86_64_DTPOFF64:
2055 gold_assert(tls_segment != NULL);
2056 if (optimized_type == tls::TLSOPT_TO_LE)
2058 // See R_X86_64_DTPOFF32, just above, for why we test this.
2059 if (this->saw_tls_block_reloc_)
2060 value -= tls_segment->memsz();
2062 Relocate_functions<64, false>::rela64(view, value, 0);
2065 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2066 if (optimized_type == tls::TLSOPT_TO_LE)
2068 gold_assert(tls_segment != NULL);
2069 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2070 rela, r_type, value, view,
2074 else if (optimized_type == tls::TLSOPT_NONE)
2076 // Relocate the field with the offset of the GOT entry for
2077 // the tp-relative offset of the symbol.
2078 unsigned int got_offset;
2081 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2082 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2083 - target->got_size());
2087 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2088 gold_assert(object->local_has_got_offset(r_sym,
2089 GOT_TYPE_TLS_OFFSET));
2090 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2091 - target->got_size());
2093 value = target->got_plt_section()->address() + got_offset;
2094 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2097 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2098 _("unsupported reloc type %u"),
2102 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2103 value -= tls_segment->memsz();
2104 Relocate_functions<64, false>::rela32(view, value, 0);
2109 // Do a relocation in which we convert a TLS General-Dynamic to an
2113 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2116 const elfcpp::Rela<64, false>& rela,
2118 elfcpp::Elf_types<64>::Elf_Addr value,
2119 unsigned char* view,
2120 elfcpp::Elf_types<64>::Elf_Addr address,
2121 section_size_type view_size)
2123 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2124 // .word 0x6666; rex64; call __tls_get_addr
2125 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2127 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2128 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2130 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2131 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2132 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2133 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2135 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2137 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2138 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2140 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2142 this->skip_call_tls_get_addr_ = true;
2145 // Do a relocation in which we convert a TLS General-Dynamic to a
2149 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2151 Output_segment* tls_segment,
2152 const elfcpp::Rela<64, false>& rela,
2154 elfcpp::Elf_types<64>::Elf_Addr value,
2155 unsigned char* view,
2156 section_size_type view_size)
2158 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2159 // .word 0x6666; rex64; call __tls_get_addr
2160 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2162 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2163 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2165 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2166 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2167 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2168 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2170 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2172 value -= tls_segment->memsz();
2173 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2175 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2177 this->skip_call_tls_get_addr_ = true;
2180 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2183 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2184 const Relocate_info<64, false>* relinfo,
2187 const elfcpp::Rela<64, false>& rela,
2188 unsigned int r_type,
2189 elfcpp::Elf_types<64>::Elf_Addr value,
2190 unsigned char* view,
2191 elfcpp::Elf_types<64>::Elf_Addr address,
2192 section_size_type view_size)
2194 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2196 // leaq foo@tlsdesc(%rip), %rax
2197 // ==> movq foo@gottpoff(%rip), %rax
2198 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2199 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2200 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2201 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2203 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2204 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2208 // call *foo@tlscall(%rax)
2210 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2211 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2212 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2213 view[0] == 0xff && view[1] == 0x10);
2219 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2222 Target_x86_64::Relocate::tls_desc_gd_to_le(
2223 const Relocate_info<64, false>* relinfo,
2225 Output_segment* tls_segment,
2226 const elfcpp::Rela<64, false>& rela,
2227 unsigned int r_type,
2228 elfcpp::Elf_types<64>::Elf_Addr value,
2229 unsigned char* view,
2230 section_size_type view_size)
2232 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2234 // leaq foo@tlsdesc(%rip), %rax
2235 // ==> movq foo@tpoff, %rax
2236 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2237 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2238 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2239 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2242 value -= tls_segment->memsz();
2243 Relocate_functions<64, false>::rela32(view, value, 0);
2247 // call *foo@tlscall(%rax)
2249 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2250 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2251 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2252 view[0] == 0xff && view[1] == 0x10);
2259 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2262 const elfcpp::Rela<64, false>& rela,
2264 elfcpp::Elf_types<64>::Elf_Addr,
2265 unsigned char* view,
2266 section_size_type view_size)
2268 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2269 // ... leq foo@dtpoff(%rax),%reg
2270 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2272 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2273 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2275 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2276 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2278 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2280 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2282 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2284 this->skip_call_tls_get_addr_ = true;
2287 // Do a relocation in which we convert a TLS Initial-Exec to a
2291 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2293 Output_segment* tls_segment,
2294 const elfcpp::Rela<64, false>& rela,
2296 elfcpp::Elf_types<64>::Elf_Addr value,
2297 unsigned char* view,
2298 section_size_type view_size)
2300 // We need to examine the opcodes to figure out which instruction we
2303 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2304 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2306 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2307 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2309 unsigned char op1 = view[-3];
2310 unsigned char op2 = view[-2];
2311 unsigned char op3 = view[-1];
2312 unsigned char reg = op3 >> 3;
2320 view[-1] = 0xc0 | reg;
2324 // Special handling for %rsp.
2328 view[-1] = 0xc0 | reg;
2336 view[-1] = 0x80 | reg | (reg << 3);
2339 value -= tls_segment->memsz();
2340 Relocate_functions<64, false>::rela32(view, value, 0);
2343 // Relocate section data.
2346 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2347 unsigned int sh_type,
2348 const unsigned char* prelocs,
2350 Output_section* output_section,
2351 bool needs_special_offset_handling,
2352 unsigned char* view,
2353 elfcpp::Elf_types<64>::Elf_Addr address,
2354 section_size_type view_size)
2356 gold_assert(sh_type == elfcpp::SHT_RELA);
2358 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2359 Target_x86_64::Relocate>(
2365 needs_special_offset_handling,
2371 // Return the size of a relocation while scanning during a relocatable
2375 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2376 unsigned int r_type,
2381 case elfcpp::R_X86_64_NONE:
2382 case elfcpp::R_386_GNU_VTINHERIT:
2383 case elfcpp::R_386_GNU_VTENTRY:
2384 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2385 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2386 case elfcpp::R_X86_64_TLSDESC_CALL:
2387 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2388 case elfcpp::R_X86_64_DTPOFF32:
2389 case elfcpp::R_X86_64_DTPOFF64:
2390 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2391 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2394 case elfcpp::R_X86_64_64:
2395 case elfcpp::R_X86_64_PC64:
2396 case elfcpp::R_X86_64_GOTOFF64:
2397 case elfcpp::R_X86_64_GOTPC64:
2398 case elfcpp::R_X86_64_PLTOFF64:
2399 case elfcpp::R_X86_64_GOT64:
2400 case elfcpp::R_X86_64_GOTPCREL64:
2401 case elfcpp::R_X86_64_GOTPCREL:
2402 case elfcpp::R_X86_64_GOTPLT64:
2405 case elfcpp::R_X86_64_32:
2406 case elfcpp::R_X86_64_32S:
2407 case elfcpp::R_X86_64_PC32:
2408 case elfcpp::R_X86_64_PLT32:
2409 case elfcpp::R_X86_64_GOTPC32:
2410 case elfcpp::R_X86_64_GOT32:
2413 case elfcpp::R_X86_64_16:
2414 case elfcpp::R_X86_64_PC16:
2417 case elfcpp::R_X86_64_8:
2418 case elfcpp::R_X86_64_PC8:
2421 case elfcpp::R_X86_64_COPY:
2422 case elfcpp::R_X86_64_GLOB_DAT:
2423 case elfcpp::R_X86_64_JUMP_SLOT:
2424 case elfcpp::R_X86_64_RELATIVE:
2425 // These are outstanding tls relocs, which are unexpected when linking
2426 case elfcpp::R_X86_64_TPOFF64:
2427 case elfcpp::R_X86_64_DTPMOD64:
2428 case elfcpp::R_X86_64_TLSDESC:
2429 object->error(_("unexpected reloc %u in object file"), r_type);
2432 case elfcpp::R_X86_64_SIZE32:
2433 case elfcpp::R_X86_64_SIZE64:
2435 object->error(_("unsupported reloc %u against local symbol"), r_type);
2440 // Scan the relocs during a relocatable link.
2443 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2444 Symbol_table* symtab,
2446 Sized_relobj<64, false>* object,
2447 unsigned int data_shndx,
2448 unsigned int sh_type,
2449 const unsigned char* prelocs,
2451 Output_section* output_section,
2452 bool needs_special_offset_handling,
2453 size_t local_symbol_count,
2454 const unsigned char* plocal_symbols,
2455 Relocatable_relocs* rr)
2457 gold_assert(sh_type == elfcpp::SHT_RELA);
2459 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2460 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2462 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2463 Scan_relocatable_relocs>(
2472 needs_special_offset_handling,
2478 // Relocate a section during a relocatable link.
2481 Target_x86_64::relocate_for_relocatable(
2482 const Relocate_info<64, false>* relinfo,
2483 unsigned int sh_type,
2484 const unsigned char* prelocs,
2486 Output_section* output_section,
2487 off_t offset_in_output_section,
2488 const Relocatable_relocs* rr,
2489 unsigned char* view,
2490 elfcpp::Elf_types<64>::Elf_Addr view_address,
2491 section_size_type view_size,
2492 unsigned char* reloc_view,
2493 section_size_type reloc_view_size)
2495 gold_assert(sh_type == elfcpp::SHT_RELA);
2497 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2502 offset_in_output_section,
2511 // Return the value to use for a dynamic which requires special
2512 // treatment. This is how we support equality comparisons of function
2513 // pointers across shared library boundaries, as described in the
2514 // processor specific ABI supplement.
2517 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2519 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2520 return this->plt_section()->address() + gsym->plt_offset();
2523 // Return a string used to fill a code section with nops to take up
2524 // the specified length.
2527 Target_x86_64::do_code_fill(section_size_type length) const
2531 // Build a jmpq instruction to skip over the bytes.
2532 unsigned char jmp[5];
2534 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2535 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2536 + std::string(length - 5, '\0'));
2539 // Nop sequences of various lengths.
2540 const char nop1[1] = { 0x90 }; // nop
2541 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2542 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2543 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2544 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2545 0x00 }; // leal 0(%esi,1),%esi
2546 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2548 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2550 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2551 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2552 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2553 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2555 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2556 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2558 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2559 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2561 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2562 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2563 0x00, 0x00, 0x00, 0x00 };
2564 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2565 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2566 0x27, 0x00, 0x00, 0x00,
2568 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2569 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2570 0xbc, 0x27, 0x00, 0x00,
2572 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2573 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2574 0x90, 0x90, 0x90, 0x90,
2577 const char* nops[16] = {
2579 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2580 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2583 return std::string(nops[length], length);
2586 // The selector for x86_64 object files.
2588 class Target_selector_x86_64 : public Target_selector
2591 Target_selector_x86_64()
2592 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2596 do_instantiate_target()
2597 { return new Target_x86_64(); }
2600 Target_selector_x86_64 target_selector_x86_64;
2602 } // End anonymous namespace.