1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 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 gc_process_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 // Scan the relocations to look for symbol adjustments.
86 scan_relocs(const General_options& options,
89 Sized_relobj<64, false>* object,
90 unsigned int data_shndx,
92 const unsigned char* prelocs,
94 Output_section* output_section,
95 bool needs_special_offset_handling,
96 size_t local_symbol_count,
97 const unsigned char* plocal_symbols);
99 // Finalize the sections.
101 do_finalize_sections(Layout*);
103 // Return the value to use for a dynamic which requires special
106 do_dynsym_value(const Symbol*) const;
108 // Relocate a section.
110 relocate_section(const Relocate_info<64, false>*,
111 unsigned int sh_type,
112 const unsigned char* prelocs,
114 Output_section* output_section,
115 bool needs_special_offset_handling,
117 elfcpp::Elf_types<64>::Elf_Addr view_address,
118 section_size_type view_size);
120 // Scan the relocs during a relocatable link.
122 scan_relocatable_relocs(const General_options& options,
123 Symbol_table* symtab,
125 Sized_relobj<64, false>* object,
126 unsigned int data_shndx,
127 unsigned int sh_type,
128 const unsigned char* prelocs,
130 Output_section* output_section,
131 bool needs_special_offset_handling,
132 size_t local_symbol_count,
133 const unsigned char* plocal_symbols,
134 Relocatable_relocs*);
136 // Relocate a section during a relocatable link.
138 relocate_for_relocatable(const Relocate_info<64, false>*,
139 unsigned int sh_type,
140 const unsigned char* prelocs,
142 Output_section* output_section,
143 off_t offset_in_output_section,
144 const Relocatable_relocs*,
146 elfcpp::Elf_types<64>::Elf_Addr view_address,
147 section_size_type view_size,
148 unsigned char* reloc_view,
149 section_size_type reloc_view_size);
151 // Return a string used to fill a code section with nops.
153 do_code_fill(section_size_type length) const;
155 // Return whether SYM is defined by the ABI.
157 do_is_defined_by_abi(const Symbol* sym) const
158 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
160 // Return the size of the GOT section.
164 gold_assert(this->got_ != NULL);
165 return this->got_->data_size();
169 // The class which scans relocations.
174 : issued_non_pic_error_(false)
178 local(const General_options& options, Symbol_table* symtab,
179 Layout* layout, Target_x86_64* target,
180 Sized_relobj<64, false>* object,
181 unsigned int data_shndx,
182 Output_section* output_section,
183 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
184 const elfcpp::Sym<64, false>& lsym);
187 global(const General_options& options, Symbol_table* symtab,
188 Layout* layout, Target_x86_64* target,
189 Sized_relobj<64, false>* object,
190 unsigned int data_shndx,
191 Output_section* output_section,
192 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
197 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
200 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
204 check_non_pic(Relobj*, unsigned int r_type);
206 // Whether we have issued an error about a non-PIC compilation.
207 bool issued_non_pic_error_;
210 // The class which implements relocation.
215 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
220 if (this->skip_call_tls_get_addr_)
222 // FIXME: This needs to specify the location somehow.
223 gold_error(_("missing expected TLS relocation"));
227 // Do a relocation. Return false if the caller should not issue
228 // any warnings about this relocation.
230 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
231 const elfcpp::Rela<64, false>&,
232 unsigned int r_type, const Sized_symbol<64>*,
233 const Symbol_value<64>*,
234 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
238 // Do a TLS relocation.
240 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
241 size_t relnum, const elfcpp::Rela<64, false>&,
242 unsigned int r_type, const Sized_symbol<64>*,
243 const Symbol_value<64>*,
244 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
247 // Do a TLS General-Dynamic to Initial-Exec transition.
249 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
250 Output_segment* tls_segment,
251 const elfcpp::Rela<64, false>&, unsigned int r_type,
252 elfcpp::Elf_types<64>::Elf_Addr value,
254 elfcpp::Elf_types<64>::Elf_Addr,
255 section_size_type view_size);
257 // Do a TLS General-Dynamic to Local-Exec transition.
259 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
260 Output_segment* tls_segment,
261 const elfcpp::Rela<64, false>&, unsigned int r_type,
262 elfcpp::Elf_types<64>::Elf_Addr value,
264 section_size_type view_size);
266 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
268 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
269 Output_segment* tls_segment,
270 const elfcpp::Rela<64, false>&, unsigned int r_type,
271 elfcpp::Elf_types<64>::Elf_Addr value,
273 elfcpp::Elf_types<64>::Elf_Addr,
274 section_size_type view_size);
276 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
278 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
279 Output_segment* tls_segment,
280 const elfcpp::Rela<64, false>&, unsigned int r_type,
281 elfcpp::Elf_types<64>::Elf_Addr value,
283 section_size_type view_size);
285 // Do a TLS Local-Dynamic to Local-Exec transition.
287 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
288 Output_segment* tls_segment,
289 const elfcpp::Rela<64, false>&, unsigned int r_type,
290 elfcpp::Elf_types<64>::Elf_Addr value,
292 section_size_type view_size);
294 // Do a TLS Initial-Exec to Local-Exec transition.
296 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
297 Output_segment* tls_segment,
298 const elfcpp::Rela<64, false>&, unsigned int r_type,
299 elfcpp::Elf_types<64>::Elf_Addr value,
301 section_size_type view_size);
303 // This is set if we should skip the next reloc, which should be a
304 // PLT32 reloc against ___tls_get_addr.
305 bool skip_call_tls_get_addr_;
307 // This is set if we see a relocation which could load the address
308 // of the TLS block. Whether we see such a relocation determines
309 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
310 // in debugging sections.
311 bool saw_tls_block_reloc_;
314 // A class which returns the size required for a relocation type,
315 // used while scanning relocs during a relocatable link.
316 class Relocatable_size_for_reloc
320 get_size_for_reloc(unsigned int, Relobj*);
323 // Adjust TLS relocation type based on the options and whether this
324 // is a local symbol.
325 static tls::Tls_optimization
326 optimize_tls_reloc(bool is_final, int r_type);
328 // Get the GOT section, creating it if necessary.
329 Output_data_got<64, false>*
330 got_section(Symbol_table*, Layout*);
332 // Get the GOT PLT section.
334 got_plt_section() const
336 gold_assert(this->got_plt_ != NULL);
337 return this->got_plt_;
340 // Create the PLT section.
342 make_plt_section(Symbol_table* symtab, Layout* layout);
344 // Create a PLT entry for a global symbol.
346 make_plt_entry(Symbol_table*, Layout*, Symbol*);
348 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
350 define_tls_base_symbol(Symbol_table*, Layout*);
352 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
354 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
356 // Create a GOT entry for the TLS module index.
358 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
359 Sized_relobj<64, false>* object);
361 // Get the PLT section.
362 Output_data_plt_x86_64*
365 gold_assert(this->plt_ != NULL);
369 // Get the dynamic reloc section, creating it if necessary.
371 rela_dyn_section(Layout*);
373 // Return true if the symbol may need a COPY relocation.
374 // References from an executable object to non-function symbols
375 // defined in a dynamic object may need a COPY relocation.
377 may_need_copy_reloc(Symbol* gsym)
379 return (!parameters->options().shared()
380 && gsym->is_from_dynobj()
381 && gsym->type() != elfcpp::STT_FUNC);
384 // Add a potential copy relocation.
386 copy_reloc(Symbol_table* symtab, Layout* layout,
387 Sized_relobj<64, false>* object,
388 unsigned int shndx, Output_section* output_section,
389 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
391 this->copy_relocs_.copy_reloc(symtab, layout,
392 symtab->get_sized_symbol<64>(sym),
393 object, shndx, output_section,
394 reloc, this->rela_dyn_section(layout));
397 // Information about this specific target which we pass to the
398 // general Target structure.
399 static const Target::Target_info x86_64_info;
403 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
404 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
405 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
406 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
410 Output_data_got<64, false>* got_;
412 Output_data_plt_x86_64* plt_;
413 // The GOT PLT section.
414 Output_data_space* got_plt_;
415 // The dynamic reloc section.
416 Reloc_section* rela_dyn_;
417 // Relocs saved to avoid a COPY reloc.
418 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
419 // Space for variables copied with a COPY reloc.
420 Output_data_space* dynbss_;
421 // Offset of the GOT entry for the TLS module index.
422 unsigned int got_mod_index_offset_;
423 // True if the _TLS_MODULE_BASE_ symbol has been defined.
424 bool tls_base_symbol_defined_;
427 const Target::Target_info Target_x86_64::x86_64_info =
430 false, // is_big_endian
431 elfcpp::EM_X86_64, // machine_code
432 false, // has_make_symbol
433 false, // has_resolve
434 true, // has_code_fill
435 true, // is_default_stack_executable
437 "/lib/ld64.so.1", // program interpreter
438 0x400000, // default_text_segment_address
439 0x1000, // abi_pagesize (overridable by -z max-page-size)
440 0x1000 // common_pagesize (overridable by -z common-page-size)
443 // Get the GOT section, creating it if necessary.
445 Output_data_got<64, false>*
446 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
448 if (this->got_ == NULL)
450 gold_assert(symtab != NULL && layout != NULL);
452 this->got_ = new Output_data_got<64, false>();
455 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
457 | elfcpp::SHF_WRITE),
461 // The old GNU linker creates a .got.plt section. We just
462 // create another set of data in the .got section. Note that we
463 // always create a PLT if we create a GOT, although the PLT
465 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
466 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
468 | elfcpp::SHF_WRITE),
472 // The first three entries are reserved.
473 this->got_plt_->set_current_data_size(3 * 8);
475 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
476 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
478 0, 0, elfcpp::STT_OBJECT,
480 elfcpp::STV_HIDDEN, 0,
487 // Get the dynamic reloc section, creating it if necessary.
489 Target_x86_64::Reloc_section*
490 Target_x86_64::rela_dyn_section(Layout* layout)
492 if (this->rela_dyn_ == NULL)
494 gold_assert(layout != NULL);
495 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
496 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
497 elfcpp::SHF_ALLOC, this->rela_dyn_);
499 return this->rela_dyn_;
502 // A class to handle the PLT data.
504 class Output_data_plt_x86_64 : public Output_section_data
507 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
509 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
512 // Add an entry to the PLT.
514 add_entry(Symbol* gsym);
516 // Add the reserved TLSDESC_PLT entry to the PLT.
518 reserve_tlsdesc_entry(unsigned int got_offset)
519 { this->tlsdesc_got_offset_ = got_offset; }
521 // Return true if a TLSDESC_PLT entry has been reserved.
523 has_tlsdesc_entry() const
524 { return this->tlsdesc_got_offset_ != -1U; }
526 // Return the GOT offset for the reserved TLSDESC_PLT entry.
528 get_tlsdesc_got_offset() const
529 { return this->tlsdesc_got_offset_; }
531 // Return the offset of the reserved TLSDESC_PLT entry.
533 get_tlsdesc_plt_offset() const
534 { return (this->count_ + 1) * plt_entry_size; }
536 // Return the .rel.plt section data.
539 { return this->rel_; }
543 do_adjust_output_section(Output_section* os);
545 // Write to a map file.
547 do_print_to_mapfile(Mapfile* mapfile) const
548 { mapfile->print_output_data(this, _("** PLT")); }
551 // The size of an entry in the PLT.
552 static const int plt_entry_size = 16;
554 // The first entry in the PLT.
555 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
556 // procedure linkage table for both programs and shared objects."
557 static unsigned char first_plt_entry[plt_entry_size];
559 // Other entries in the PLT for an executable.
560 static unsigned char plt_entry[plt_entry_size];
562 // The reserved TLSDESC entry in the PLT for an executable.
563 static unsigned char tlsdesc_plt_entry[plt_entry_size];
565 // Set the final size.
567 set_final_data_size();
569 // Write out the PLT data.
571 do_write(Output_file*);
573 // The reloc section.
576 Output_data_got<64, false>* got_;
577 // The .got.plt section.
578 Output_data_space* got_plt_;
579 // The number of PLT entries.
581 // Offset of the reserved TLSDESC_GOT entry when needed.
582 unsigned int tlsdesc_got_offset_;
585 // Create the PLT section. The ordinary .got section is an argument,
586 // since we need to refer to the start. We also create our own .got
587 // section just for PLT entries.
589 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
590 Output_data_got<64, false>* got,
591 Output_data_space* got_plt)
592 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
593 tlsdesc_got_offset_(-1U)
595 this->rel_ = new Reloc_section(false);
596 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
597 elfcpp::SHF_ALLOC, this->rel_);
601 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
603 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
604 // linker, and so do we.
608 // Add an entry to the PLT.
611 Output_data_plt_x86_64::add_entry(Symbol* gsym)
613 gold_assert(!gsym->has_plt_offset());
615 // Note that when setting the PLT offset we skip the initial
616 // reserved PLT entry.
617 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
621 section_offset_type got_offset = this->got_plt_->current_data_size();
623 // Every PLT entry needs a GOT entry which points back to the PLT
624 // entry (this will be changed by the dynamic linker, normally
625 // lazily when the function is called).
626 this->got_plt_->set_current_data_size(got_offset + 8);
628 // Every PLT entry needs a reloc.
629 gsym->set_needs_dynsym_entry();
630 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
633 // Note that we don't need to save the symbol. The contents of the
634 // PLT are independent of which symbols are used. The symbols only
635 // appear in the relocations.
638 // Set the final size.
640 Output_data_plt_x86_64::set_final_data_size()
642 unsigned int count = this->count_;
643 if (this->has_tlsdesc_entry())
645 this->set_data_size((count + 1) * plt_entry_size);
648 // The first entry in the PLT for an executable.
650 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
652 // From AMD64 ABI Draft 0.98, page 76
653 0xff, 0x35, // pushq contents of memory address
654 0, 0, 0, 0, // replaced with address of .got + 8
655 0xff, 0x25, // jmp indirect
656 0, 0, 0, 0, // replaced with address of .got + 16
657 0x90, 0x90, 0x90, 0x90 // noop (x4)
660 // Subsequent entries in the PLT for an executable.
662 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
664 // From AMD64 ABI Draft 0.98, page 76
665 0xff, 0x25, // jmpq indirect
666 0, 0, 0, 0, // replaced with address of symbol in .got
667 0x68, // pushq immediate
668 0, 0, 0, 0, // replaced with offset into relocation table
669 0xe9, // jmpq relative
670 0, 0, 0, 0 // replaced with offset to start of .plt
673 // The reserved TLSDESC entry in the PLT for an executable.
675 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
677 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
678 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
679 0xff, 0x35, // pushq x(%rip)
680 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
681 0xff, 0x25, // jmpq *y(%rip)
682 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
687 // Write out the PLT. This uses the hand-coded instructions above,
688 // and adjusts them as needed. This is specified by the AMD64 ABI.
691 Output_data_plt_x86_64::do_write(Output_file* of)
693 const off_t offset = this->offset();
694 const section_size_type oview_size =
695 convert_to_section_size_type(this->data_size());
696 unsigned char* const oview = of->get_output_view(offset, oview_size);
698 const off_t got_file_offset = this->got_plt_->offset();
699 const section_size_type got_size =
700 convert_to_section_size_type(this->got_plt_->data_size());
701 unsigned char* const got_view = of->get_output_view(got_file_offset,
704 unsigned char* pov = oview;
706 // The base address of the .plt section.
707 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
708 // The base address of the .got section.
709 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
710 // The base address of the PLT portion of the .got section,
711 // which is where the GOT pointer will point, and where the
712 // three reserved GOT entries are located.
713 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
715 memcpy(pov, first_plt_entry, plt_entry_size);
716 // We do a jmp relative to the PC at the end of this instruction.
717 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
719 - (plt_address + 6)));
720 elfcpp::Swap<32, false>::writeval(pov + 8,
722 - (plt_address + 12)));
723 pov += plt_entry_size;
725 unsigned char* got_pov = got_view;
727 memset(got_pov, 0, 24);
730 unsigned int plt_offset = plt_entry_size;
731 unsigned int got_offset = 24;
732 const unsigned int count = this->count_;
733 for (unsigned int plt_index = 0;
736 pov += plt_entry_size,
738 plt_offset += plt_entry_size,
741 // Set and adjust the PLT entry itself.
742 memcpy(pov, plt_entry, plt_entry_size);
743 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
744 (got_address + got_offset
745 - (plt_address + plt_offset
748 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
749 elfcpp::Swap<32, false>::writeval(pov + 12,
750 - (plt_offset + plt_entry_size));
752 // Set the entry in the GOT.
753 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
756 if (this->has_tlsdesc_entry())
758 // Set and adjust the reserved TLSDESC PLT entry.
759 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
760 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
761 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
763 - (plt_address + plt_offset
765 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
768 - (plt_address + plt_offset
770 pov += plt_entry_size;
773 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
774 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
776 of->write_output_view(offset, oview_size, oview);
777 of->write_output_view(got_file_offset, got_size, got_view);
780 // Create the PLT section.
783 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
785 if (this->plt_ == NULL)
787 // Create the GOT sections first.
788 this->got_section(symtab, layout);
790 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
792 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
794 | elfcpp::SHF_EXECINSTR),
799 // Create a PLT entry for a global symbol.
802 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
805 if (gsym->has_plt_offset())
808 if (this->plt_ == NULL)
809 this->make_plt_section(symtab, layout);
811 this->plt_->add_entry(gsym);
814 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
817 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
819 if (this->tls_base_symbol_defined_)
822 Output_segment* tls_segment = layout->tls_segment();
823 if (tls_segment != NULL)
825 bool is_exec = parameters->options().output_is_executable();
826 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
830 elfcpp::STV_HIDDEN, 0,
832 ? Symbol::SEGMENT_END
833 : Symbol::SEGMENT_START),
836 this->tls_base_symbol_defined_ = true;
839 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
842 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
845 if (this->plt_ == NULL)
846 this->make_plt_section(symtab, layout);
848 if (!this->plt_->has_tlsdesc_entry())
850 // Allocate the TLSDESC_GOT entry.
851 Output_data_got<64, false>* got = this->got_section(symtab, layout);
852 unsigned int got_offset = got->add_constant(0);
854 // Allocate the TLSDESC_PLT entry.
855 this->plt_->reserve_tlsdesc_entry(got_offset);
859 // Create a GOT entry for the TLS module index.
862 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
863 Sized_relobj<64, false>* object)
865 if (this->got_mod_index_offset_ == -1U)
867 gold_assert(symtab != NULL && layout != NULL && object != NULL);
868 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
869 Output_data_got<64, false>* got = this->got_section(symtab, layout);
870 unsigned int got_offset = got->add_constant(0);
871 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
873 got->add_constant(0);
874 this->got_mod_index_offset_ = got_offset;
876 return this->got_mod_index_offset_;
879 // Optimize the TLS relocation type based on what we know about the
880 // symbol. IS_FINAL is true if the final address of this symbol is
881 // known at link time.
883 tls::Tls_optimization
884 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
886 // If we are generating a shared library, then we can't do anything
888 if (parameters->options().shared())
889 return tls::TLSOPT_NONE;
893 case elfcpp::R_X86_64_TLSGD:
894 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
895 case elfcpp::R_X86_64_TLSDESC_CALL:
896 // These are General-Dynamic which permits fully general TLS
897 // access. Since we know that we are generating an executable,
898 // we can convert this to Initial-Exec. If we also know that
899 // this is a local symbol, we can further switch to Local-Exec.
901 return tls::TLSOPT_TO_LE;
902 return tls::TLSOPT_TO_IE;
904 case elfcpp::R_X86_64_TLSLD:
905 // This is Local-Dynamic, which refers to a local symbol in the
906 // dynamic TLS block. Since we know that we generating an
907 // executable, we can switch to Local-Exec.
908 return tls::TLSOPT_TO_LE;
910 case elfcpp::R_X86_64_DTPOFF32:
911 case elfcpp::R_X86_64_DTPOFF64:
912 // Another Local-Dynamic reloc.
913 return tls::TLSOPT_TO_LE;
915 case elfcpp::R_X86_64_GOTTPOFF:
916 // These are Initial-Exec relocs which get the thread offset
917 // from the GOT. If we know that we are linking against the
918 // local symbol, we can switch to Local-Exec, which links the
919 // thread offset into the instruction.
921 return tls::TLSOPT_TO_LE;
922 return tls::TLSOPT_NONE;
924 case elfcpp::R_X86_64_TPOFF32:
925 // When we already have Local-Exec, there is nothing further we
927 return tls::TLSOPT_NONE;
934 // Report an unsupported relocation against a local symbol.
937 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
940 gold_error(_("%s: unsupported reloc %u against local symbol"),
941 object->name().c_str(), r_type);
944 // We are about to emit a dynamic relocation of type R_TYPE. If the
945 // dynamic linker does not support it, issue an error. The GNU linker
946 // only issues a non-PIC error for an allocated read-only section.
947 // Here we know the section is allocated, but we don't know that it is
948 // read-only. But we check for all the relocation types which the
949 // glibc dynamic linker supports, so it seems appropriate to issue an
950 // error even if the section is not read-only.
953 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
957 // These are the relocation types supported by glibc for x86_64.
958 case elfcpp::R_X86_64_RELATIVE:
959 case elfcpp::R_X86_64_GLOB_DAT:
960 case elfcpp::R_X86_64_JUMP_SLOT:
961 case elfcpp::R_X86_64_DTPMOD64:
962 case elfcpp::R_X86_64_DTPOFF64:
963 case elfcpp::R_X86_64_TPOFF64:
964 case elfcpp::R_X86_64_64:
965 case elfcpp::R_X86_64_32:
966 case elfcpp::R_X86_64_PC32:
967 case elfcpp::R_X86_64_COPY:
971 // This prevents us from issuing more than one error per reloc
972 // section. But we can still wind up issuing more than one
973 // error per object file.
974 if (this->issued_non_pic_error_)
976 object->error(_("requires unsupported dynamic reloc; "
977 "recompile with -fPIC"));
978 this->issued_non_pic_error_ = true;
981 case elfcpp::R_X86_64_NONE:
986 // Scan a relocation for a local symbol.
989 Target_x86_64::Scan::local(const General_options&,
990 Symbol_table* symtab,
992 Target_x86_64* target,
993 Sized_relobj<64, false>* object,
994 unsigned int data_shndx,
995 Output_section* output_section,
996 const elfcpp::Rela<64, false>& reloc,
998 const elfcpp::Sym<64, false>& lsym)
1002 case elfcpp::R_X86_64_NONE:
1003 case elfcpp::R_386_GNU_VTINHERIT:
1004 case elfcpp::R_386_GNU_VTENTRY:
1007 case elfcpp::R_X86_64_64:
1008 // If building a shared library (or a position-independent
1009 // executable), we need to create a dynamic relocation for this
1010 // location. The relocation applied at link time will apply the
1011 // link-time value, so we flag the location with an
1012 // R_X86_64_RELATIVE relocation so the dynamic loader can
1013 // relocate it easily.
1014 if (parameters->options().output_is_position_independent())
1016 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1017 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1018 rela_dyn->add_local_relative(object, r_sym,
1019 elfcpp::R_X86_64_RELATIVE,
1020 output_section, data_shndx,
1021 reloc.get_r_offset(),
1022 reloc.get_r_addend());
1026 case elfcpp::R_X86_64_32:
1027 case elfcpp::R_X86_64_32S:
1028 case elfcpp::R_X86_64_16:
1029 case elfcpp::R_X86_64_8:
1030 // If building a shared library (or a position-independent
1031 // executable), we need to create a dynamic relocation for this
1032 // location. We can't use an R_X86_64_RELATIVE relocation
1033 // because that is always a 64-bit relocation.
1034 if (parameters->options().output_is_position_independent())
1036 this->check_non_pic(object, r_type);
1038 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1039 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1040 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1041 rela_dyn->add_local(object, r_sym, r_type, output_section,
1042 data_shndx, reloc.get_r_offset(),
1043 reloc.get_r_addend());
1046 gold_assert(lsym.get_st_value() == 0);
1047 unsigned int shndx = lsym.get_st_shndx();
1049 shndx = object->adjust_sym_shndx(r_sym, shndx,
1052 object->error(_("section symbol %u has bad shndx %u"),
1055 rela_dyn->add_local_section(object, shndx,
1056 r_type, output_section,
1057 data_shndx, reloc.get_r_offset(),
1058 reloc.get_r_addend());
1063 case elfcpp::R_X86_64_PC64:
1064 case elfcpp::R_X86_64_PC32:
1065 case elfcpp::R_X86_64_PC16:
1066 case elfcpp::R_X86_64_PC8:
1069 case elfcpp::R_X86_64_PLT32:
1070 // Since we know this is a local symbol, we can handle this as a
1074 case elfcpp::R_X86_64_GOTPC32:
1075 case elfcpp::R_X86_64_GOTOFF64:
1076 case elfcpp::R_X86_64_GOTPC64:
1077 case elfcpp::R_X86_64_PLTOFF64:
1078 // We need a GOT section.
1079 target->got_section(symtab, layout);
1080 // For PLTOFF64, we'd normally want a PLT section, but since we
1081 // know this is a local symbol, no PLT is needed.
1084 case elfcpp::R_X86_64_GOT64:
1085 case elfcpp::R_X86_64_GOT32:
1086 case elfcpp::R_X86_64_GOTPCREL64:
1087 case elfcpp::R_X86_64_GOTPCREL:
1088 case elfcpp::R_X86_64_GOTPLT64:
1090 // The symbol requires a GOT entry.
1091 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1092 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1093 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1095 // If we are generating a shared object, we need to add a
1096 // dynamic relocation for this symbol's GOT entry.
1097 if (parameters->options().output_is_position_independent())
1099 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1100 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1101 if (r_type != elfcpp::R_X86_64_GOT32)
1102 rela_dyn->add_local_relative(
1103 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1104 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1107 this->check_non_pic(object, r_type);
1109 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1110 rela_dyn->add_local(
1111 object, r_sym, r_type, got,
1112 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1116 // For GOTPLT64, we'd normally want a PLT section, but since
1117 // we know this is a local symbol, no PLT is needed.
1121 case elfcpp::R_X86_64_COPY:
1122 case elfcpp::R_X86_64_GLOB_DAT:
1123 case elfcpp::R_X86_64_JUMP_SLOT:
1124 case elfcpp::R_X86_64_RELATIVE:
1125 // These are outstanding tls relocs, which are unexpected when linking
1126 case elfcpp::R_X86_64_TPOFF64:
1127 case elfcpp::R_X86_64_DTPMOD64:
1128 case elfcpp::R_X86_64_TLSDESC:
1129 gold_error(_("%s: unexpected reloc %u in object file"),
1130 object->name().c_str(), r_type);
1133 // These are initial tls relocs, which are expected when linking
1134 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1135 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1136 case elfcpp::R_X86_64_TLSDESC_CALL:
1137 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1138 case elfcpp::R_X86_64_DTPOFF32:
1139 case elfcpp::R_X86_64_DTPOFF64:
1140 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1141 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1143 bool output_is_shared = parameters->options().shared();
1144 const tls::Tls_optimization optimized_type
1145 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1148 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1149 if (optimized_type == tls::TLSOPT_NONE)
1151 // Create a pair of GOT entries for the module index and
1152 // dtv-relative offset.
1153 Output_data_got<64, false>* got
1154 = target->got_section(symtab, layout);
1155 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1156 unsigned int shndx = lsym.get_st_shndx();
1158 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1160 object->error(_("local symbol %u has bad shndx %u"),
1163 got->add_local_pair_with_rela(object, r_sym,
1166 target->rela_dyn_section(layout),
1167 elfcpp::R_X86_64_DTPMOD64, 0);
1169 else if (optimized_type != tls::TLSOPT_TO_LE)
1170 unsupported_reloc_local(object, r_type);
1173 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1174 target->define_tls_base_symbol(symtab, layout);
1175 if (optimized_type == tls::TLSOPT_NONE)
1177 // Create reserved PLT and GOT entries for the resolver.
1178 target->reserve_tlsdesc_entries(symtab, layout);
1180 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1181 Output_data_got<64, false>* got
1182 = target->got_section(symtab, layout);
1183 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1184 unsigned int shndx = lsym.get_st_shndx();
1186 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1188 object->error(_("local symbol %u has bad shndx %u"),
1191 got->add_local_pair_with_rela(object, r_sym,
1194 target->rela_dyn_section(layout),
1195 elfcpp::R_X86_64_TLSDESC, 0);
1197 else if (optimized_type != tls::TLSOPT_TO_LE)
1198 unsupported_reloc_local(object, r_type);
1201 case elfcpp::R_X86_64_TLSDESC_CALL:
1204 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1205 if (optimized_type == tls::TLSOPT_NONE)
1207 // Create a GOT entry for the module index.
1208 target->got_mod_index_entry(symtab, layout, object);
1210 else if (optimized_type != tls::TLSOPT_TO_LE)
1211 unsupported_reloc_local(object, r_type);
1214 case elfcpp::R_X86_64_DTPOFF32:
1215 case elfcpp::R_X86_64_DTPOFF64:
1218 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1219 layout->set_has_static_tls();
1220 if (optimized_type == tls::TLSOPT_NONE)
1222 // Create a GOT entry for the tp-relative offset.
1223 Output_data_got<64, false>* got
1224 = target->got_section(symtab, layout);
1225 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1226 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1227 target->rela_dyn_section(layout),
1228 elfcpp::R_X86_64_TPOFF64);
1230 else if (optimized_type != tls::TLSOPT_TO_LE)
1231 unsupported_reloc_local(object, r_type);
1234 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1235 layout->set_has_static_tls();
1236 if (output_is_shared)
1237 unsupported_reloc_local(object, r_type);
1246 case elfcpp::R_X86_64_SIZE32:
1247 case elfcpp::R_X86_64_SIZE64:
1249 gold_error(_("%s: unsupported reloc %u against local symbol"),
1250 object->name().c_str(), r_type);
1256 // Report an unsupported relocation against a global symbol.
1259 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1260 unsigned int r_type,
1263 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1264 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1267 // Scan a relocation for a global symbol.
1270 Target_x86_64::Scan::global(const General_options&,
1271 Symbol_table* symtab,
1273 Target_x86_64* target,
1274 Sized_relobj<64, false>* object,
1275 unsigned int data_shndx,
1276 Output_section* output_section,
1277 const elfcpp::Rela<64, false>& reloc,
1278 unsigned int r_type,
1283 case elfcpp::R_X86_64_NONE:
1284 case elfcpp::R_386_GNU_VTINHERIT:
1285 case elfcpp::R_386_GNU_VTENTRY:
1288 case elfcpp::R_X86_64_64:
1289 case elfcpp::R_X86_64_32:
1290 case elfcpp::R_X86_64_32S:
1291 case elfcpp::R_X86_64_16:
1292 case elfcpp::R_X86_64_8:
1294 // Make a PLT entry if necessary.
1295 if (gsym->needs_plt_entry())
1297 target->make_plt_entry(symtab, layout, gsym);
1298 // Since this is not a PC-relative relocation, we may be
1299 // taking the address of a function. In that case we need to
1300 // set the entry in the dynamic symbol table to the address of
1302 if (gsym->is_from_dynobj() && !parameters->options().shared())
1303 gsym->set_needs_dynsym_value();
1305 // Make a dynamic relocation if necessary.
1306 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1308 if (target->may_need_copy_reloc(gsym))
1310 target->copy_reloc(symtab, layout, object,
1311 data_shndx, output_section, gsym, reloc);
1313 else if (r_type == elfcpp::R_X86_64_64
1314 && gsym->can_use_relative_reloc(false))
1316 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1317 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1318 output_section, object,
1319 data_shndx, reloc.get_r_offset(),
1320 reloc.get_r_addend());
1324 this->check_non_pic(object, r_type);
1325 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1326 rela_dyn->add_global(gsym, r_type, output_section, object,
1327 data_shndx, reloc.get_r_offset(),
1328 reloc.get_r_addend());
1334 case elfcpp::R_X86_64_PC64:
1335 case elfcpp::R_X86_64_PC32:
1336 case elfcpp::R_X86_64_PC16:
1337 case elfcpp::R_X86_64_PC8:
1339 // Make a PLT entry if necessary.
1340 if (gsym->needs_plt_entry())
1341 target->make_plt_entry(symtab, layout, gsym);
1342 // Make a dynamic relocation if necessary.
1343 int flags = Symbol::NON_PIC_REF;
1344 if (gsym->type() == elfcpp::STT_FUNC)
1345 flags |= Symbol::FUNCTION_CALL;
1346 if (gsym->needs_dynamic_reloc(flags))
1348 if (target->may_need_copy_reloc(gsym))
1350 target->copy_reloc(symtab, layout, object,
1351 data_shndx, output_section, gsym, reloc);
1355 this->check_non_pic(object, r_type);
1356 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1357 rela_dyn->add_global(gsym, r_type, output_section, object,
1358 data_shndx, reloc.get_r_offset(),
1359 reloc.get_r_addend());
1365 case elfcpp::R_X86_64_GOT64:
1366 case elfcpp::R_X86_64_GOT32:
1367 case elfcpp::R_X86_64_GOTPCREL64:
1368 case elfcpp::R_X86_64_GOTPCREL:
1369 case elfcpp::R_X86_64_GOTPLT64:
1371 // The symbol requires a GOT entry.
1372 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1373 if (gsym->final_value_is_known())
1374 got->add_global(gsym, GOT_TYPE_STANDARD);
1377 // If this symbol is not fully resolved, we need to add a
1378 // dynamic relocation for it.
1379 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1380 if (gsym->is_from_dynobj()
1381 || gsym->is_undefined()
1382 || gsym->is_preemptible())
1383 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1384 elfcpp::R_X86_64_GLOB_DAT);
1387 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1388 rela_dyn->add_global_relative(
1389 gsym, elfcpp::R_X86_64_RELATIVE, got,
1390 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1393 // For GOTPLT64, we also need a PLT entry (but only if the
1394 // symbol is not fully resolved).
1395 if (r_type == elfcpp::R_X86_64_GOTPLT64
1396 && !gsym->final_value_is_known())
1397 target->make_plt_entry(symtab, layout, gsym);
1401 case elfcpp::R_X86_64_PLT32:
1402 // If the symbol is fully resolved, this is just a PC32 reloc.
1403 // Otherwise we need a PLT entry.
1404 if (gsym->final_value_is_known())
1406 // If building a shared library, we can also skip the PLT entry
1407 // if the symbol is defined in the output file and is protected
1409 if (gsym->is_defined()
1410 && !gsym->is_from_dynobj()
1411 && !gsym->is_preemptible())
1413 target->make_plt_entry(symtab, layout, gsym);
1416 case elfcpp::R_X86_64_GOTPC32:
1417 case elfcpp::R_X86_64_GOTOFF64:
1418 case elfcpp::R_X86_64_GOTPC64:
1419 case elfcpp::R_X86_64_PLTOFF64:
1420 // We need a GOT section.
1421 target->got_section(symtab, layout);
1422 // For PLTOFF64, we also need a PLT entry (but only if the
1423 // symbol is not fully resolved).
1424 if (r_type == elfcpp::R_X86_64_PLTOFF64
1425 && !gsym->final_value_is_known())
1426 target->make_plt_entry(symtab, layout, gsym);
1429 case elfcpp::R_X86_64_COPY:
1430 case elfcpp::R_X86_64_GLOB_DAT:
1431 case elfcpp::R_X86_64_JUMP_SLOT:
1432 case elfcpp::R_X86_64_RELATIVE:
1433 // These are outstanding tls relocs, which are unexpected when linking
1434 case elfcpp::R_X86_64_TPOFF64:
1435 case elfcpp::R_X86_64_DTPMOD64:
1436 case elfcpp::R_X86_64_TLSDESC:
1437 gold_error(_("%s: unexpected reloc %u in object file"),
1438 object->name().c_str(), r_type);
1441 // These are initial tls relocs, which are expected for global()
1442 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1443 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1444 case elfcpp::R_X86_64_TLSDESC_CALL:
1445 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1446 case elfcpp::R_X86_64_DTPOFF32:
1447 case elfcpp::R_X86_64_DTPOFF64:
1448 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1449 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1451 const bool is_final = gsym->final_value_is_known();
1452 const tls::Tls_optimization optimized_type
1453 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1456 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1457 if (optimized_type == tls::TLSOPT_NONE)
1459 // Create a pair of GOT entries for the module index and
1460 // dtv-relative offset.
1461 Output_data_got<64, false>* got
1462 = target->got_section(symtab, layout);
1463 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1464 target->rela_dyn_section(layout),
1465 elfcpp::R_X86_64_DTPMOD64,
1466 elfcpp::R_X86_64_DTPOFF64);
1468 else if (optimized_type == tls::TLSOPT_TO_IE)
1470 // Create a GOT entry for the tp-relative offset.
1471 Output_data_got<64, false>* got
1472 = target->got_section(symtab, layout);
1473 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1474 target->rela_dyn_section(layout),
1475 elfcpp::R_X86_64_TPOFF64);
1477 else if (optimized_type != tls::TLSOPT_TO_LE)
1478 unsupported_reloc_global(object, r_type, gsym);
1481 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1482 target->define_tls_base_symbol(symtab, layout);
1483 if (optimized_type == tls::TLSOPT_NONE)
1485 // Create reserved PLT and GOT entries for the resolver.
1486 target->reserve_tlsdesc_entries(symtab, layout);
1488 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1489 Output_data_got<64, false>* got
1490 = target->got_section(symtab, layout);
1491 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1492 target->rela_dyn_section(layout),
1493 elfcpp::R_X86_64_TLSDESC, 0);
1495 else if (optimized_type == tls::TLSOPT_TO_IE)
1497 // Create a GOT entry for the tp-relative offset.
1498 Output_data_got<64, false>* got
1499 = target->got_section(symtab, layout);
1500 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1501 target->rela_dyn_section(layout),
1502 elfcpp::R_X86_64_TPOFF64);
1504 else if (optimized_type != tls::TLSOPT_TO_LE)
1505 unsupported_reloc_global(object, r_type, gsym);
1508 case elfcpp::R_X86_64_TLSDESC_CALL:
1511 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1512 if (optimized_type == tls::TLSOPT_NONE)
1514 // Create a GOT entry for the module index.
1515 target->got_mod_index_entry(symtab, layout, object);
1517 else if (optimized_type != tls::TLSOPT_TO_LE)
1518 unsupported_reloc_global(object, r_type, gsym);
1521 case elfcpp::R_X86_64_DTPOFF32:
1522 case elfcpp::R_X86_64_DTPOFF64:
1525 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1526 layout->set_has_static_tls();
1527 if (optimized_type == tls::TLSOPT_NONE)
1529 // Create a GOT entry for the tp-relative offset.
1530 Output_data_got<64, false>* got
1531 = target->got_section(symtab, layout);
1532 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1533 target->rela_dyn_section(layout),
1534 elfcpp::R_X86_64_TPOFF64);
1536 else if (optimized_type != tls::TLSOPT_TO_LE)
1537 unsupported_reloc_global(object, r_type, gsym);
1540 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1541 layout->set_has_static_tls();
1542 if (parameters->options().shared())
1543 unsupported_reloc_local(object, r_type);
1552 case elfcpp::R_X86_64_SIZE32:
1553 case elfcpp::R_X86_64_SIZE64:
1555 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1556 object->name().c_str(), r_type,
1557 gsym->demangled_name().c_str());
1563 Target_x86_64::gc_process_relocs(const General_options& options,
1564 Symbol_table* symtab,
1566 Sized_relobj<64, false>* object,
1567 unsigned int data_shndx,
1568 unsigned int sh_type,
1569 const unsigned char* prelocs,
1571 Output_section* output_section,
1572 bool needs_special_offset_handling,
1573 size_t local_symbol_count,
1574 const unsigned char* plocal_symbols)
1577 if (sh_type == elfcpp::SHT_REL)
1582 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1583 Target_x86_64::Scan>(
1593 needs_special_offset_handling,
1598 // Scan relocations for a section.
1601 Target_x86_64::scan_relocs(const General_options& options,
1602 Symbol_table* symtab,
1604 Sized_relobj<64, false>* object,
1605 unsigned int data_shndx,
1606 unsigned int sh_type,
1607 const unsigned char* prelocs,
1609 Output_section* output_section,
1610 bool needs_special_offset_handling,
1611 size_t local_symbol_count,
1612 const unsigned char* plocal_symbols)
1614 if (sh_type == elfcpp::SHT_REL)
1616 gold_error(_("%s: unsupported REL reloc section"),
1617 object->name().c_str());
1621 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1622 Target_x86_64::Scan>(
1632 needs_special_offset_handling,
1637 // Finalize the sections.
1640 Target_x86_64::do_finalize_sections(Layout* layout)
1642 // Fill in some more dynamic tags.
1643 Output_data_dynamic* const odyn = layout->dynamic_data();
1646 if (this->got_plt_ != NULL)
1647 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1649 if (this->plt_ != NULL)
1651 const Output_data* od = this->plt_->rel_plt();
1652 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1653 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1654 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1655 if (this->plt_->has_tlsdesc_entry())
1657 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1658 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1659 this->got_->finalize_data_size();
1660 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1661 this->plt_, plt_offset);
1662 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1663 this->got_, got_offset);
1667 if (this->rela_dyn_ != NULL)
1669 const Output_data* od = this->rela_dyn_;
1670 odyn->add_section_address(elfcpp::DT_RELA, od);
1671 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1672 odyn->add_constant(elfcpp::DT_RELAENT,
1673 elfcpp::Elf_sizes<64>::rela_size);
1676 if (!parameters->options().shared())
1678 // The value of the DT_DEBUG tag is filled in by the dynamic
1679 // linker at run time, and used by the debugger.
1680 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1684 // Emit any relocs we saved in an attempt to avoid generating COPY
1686 if (this->copy_relocs_.any_saved_relocs())
1687 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1690 // Perform a relocation.
1693 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1694 Target_x86_64* target,
1696 const elfcpp::Rela<64, false>& rela,
1697 unsigned int r_type,
1698 const Sized_symbol<64>* gsym,
1699 const Symbol_value<64>* psymval,
1700 unsigned char* view,
1701 elfcpp::Elf_types<64>::Elf_Addr address,
1702 section_size_type view_size)
1704 if (this->skip_call_tls_get_addr_)
1706 if (r_type != elfcpp::R_X86_64_PLT32
1708 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1710 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1711 _("missing expected TLS relocation"));
1715 this->skip_call_tls_get_addr_ = false;
1720 // Pick the value to use for symbols defined in shared objects.
1721 Symbol_value<64> symval;
1723 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1724 || r_type == elfcpp::R_X86_64_PC32
1725 || r_type == elfcpp::R_X86_64_PC16
1726 || r_type == elfcpp::R_X86_64_PC8))
1728 symval.set_output_value(target->plt_section()->address()
1729 + gsym->plt_offset());
1733 const Sized_relobj<64, false>* object = relinfo->object;
1734 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1736 // Get the GOT offset if needed.
1737 // The GOT pointer points to the end of the GOT section.
1738 // We need to subtract the size of the GOT section to get
1739 // the actual offset to use in the relocation.
1740 bool have_got_offset = false;
1741 unsigned int got_offset = 0;
1744 case elfcpp::R_X86_64_GOT32:
1745 case elfcpp::R_X86_64_GOT64:
1746 case elfcpp::R_X86_64_GOTPLT64:
1747 case elfcpp::R_X86_64_GOTPCREL:
1748 case elfcpp::R_X86_64_GOTPCREL64:
1751 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1752 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1756 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1757 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1758 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1759 - target->got_size());
1761 have_got_offset = true;
1770 case elfcpp::R_X86_64_NONE:
1771 case elfcpp::R_386_GNU_VTINHERIT:
1772 case elfcpp::R_386_GNU_VTENTRY:
1775 case elfcpp::R_X86_64_64:
1776 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1779 case elfcpp::R_X86_64_PC64:
1780 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1784 case elfcpp::R_X86_64_32:
1785 // FIXME: we need to verify that value + addend fits into 32 bits:
1786 // uint64_t x = value + addend;
1787 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1788 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1789 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1792 case elfcpp::R_X86_64_32S:
1793 // FIXME: we need to verify that value + addend fits into 32 bits:
1794 // int64_t x = value + addend; // note this quantity is signed!
1795 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1796 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1799 case elfcpp::R_X86_64_PC32:
1800 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1804 case elfcpp::R_X86_64_16:
1805 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1808 case elfcpp::R_X86_64_PC16:
1809 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1813 case elfcpp::R_X86_64_8:
1814 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1817 case elfcpp::R_X86_64_PC8:
1818 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1822 case elfcpp::R_X86_64_PLT32:
1823 gold_assert(gsym == NULL
1824 || gsym->has_plt_offset()
1825 || gsym->final_value_is_known()
1826 || (gsym->is_defined()
1827 && !gsym->is_from_dynobj()
1828 && !gsym->is_preemptible()));
1829 // Note: while this code looks the same as for R_X86_64_PC32, it
1830 // behaves differently because psymval was set to point to
1831 // the PLT entry, rather than the symbol, in Scan::global().
1832 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1836 case elfcpp::R_X86_64_PLTOFF64:
1839 gold_assert(gsym->has_plt_offset()
1840 || gsym->final_value_is_known());
1841 elfcpp::Elf_types<64>::Elf_Addr got_address;
1842 got_address = target->got_section(NULL, NULL)->address();
1843 Relocate_functions<64, false>::rela64(view, object, psymval,
1844 addend - got_address);
1847 case elfcpp::R_X86_64_GOT32:
1848 gold_assert(have_got_offset);
1849 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1852 case elfcpp::R_X86_64_GOTPC32:
1855 elfcpp::Elf_types<64>::Elf_Addr value;
1856 value = target->got_plt_section()->address();
1857 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1861 case elfcpp::R_X86_64_GOT64:
1862 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1863 // Since we always add a PLT entry, this is equivalent.
1864 case elfcpp::R_X86_64_GOTPLT64:
1865 gold_assert(have_got_offset);
1866 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1869 case elfcpp::R_X86_64_GOTPC64:
1872 elfcpp::Elf_types<64>::Elf_Addr value;
1873 value = target->got_plt_section()->address();
1874 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1878 case elfcpp::R_X86_64_GOTOFF64:
1880 elfcpp::Elf_types<64>::Elf_Addr value;
1881 value = (psymval->value(object, 0)
1882 - target->got_plt_section()->address());
1883 Relocate_functions<64, false>::rela64(view, value, addend);
1887 case elfcpp::R_X86_64_GOTPCREL:
1889 gold_assert(have_got_offset);
1890 elfcpp::Elf_types<64>::Elf_Addr value;
1891 value = target->got_plt_section()->address() + got_offset;
1892 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1896 case elfcpp::R_X86_64_GOTPCREL64:
1898 gold_assert(have_got_offset);
1899 elfcpp::Elf_types<64>::Elf_Addr value;
1900 value = target->got_plt_section()->address() + got_offset;
1901 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1905 case elfcpp::R_X86_64_COPY:
1906 case elfcpp::R_X86_64_GLOB_DAT:
1907 case elfcpp::R_X86_64_JUMP_SLOT:
1908 case elfcpp::R_X86_64_RELATIVE:
1909 // These are outstanding tls relocs, which are unexpected when linking
1910 case elfcpp::R_X86_64_TPOFF64:
1911 case elfcpp::R_X86_64_DTPMOD64:
1912 case elfcpp::R_X86_64_TLSDESC:
1913 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1914 _("unexpected reloc %u in object file"),
1918 // These are initial tls relocs, which are expected when linking
1919 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1920 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1921 case elfcpp::R_X86_64_TLSDESC_CALL:
1922 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1923 case elfcpp::R_X86_64_DTPOFF32:
1924 case elfcpp::R_X86_64_DTPOFF64:
1925 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1926 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1927 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1928 view, address, view_size);
1931 case elfcpp::R_X86_64_SIZE32:
1932 case elfcpp::R_X86_64_SIZE64:
1934 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1935 _("unsupported reloc %u"),
1943 // Perform a TLS relocation.
1946 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1947 Target_x86_64* target,
1949 const elfcpp::Rela<64, false>& rela,
1950 unsigned int r_type,
1951 const Sized_symbol<64>* gsym,
1952 const Symbol_value<64>* psymval,
1953 unsigned char* view,
1954 elfcpp::Elf_types<64>::Elf_Addr address,
1955 section_size_type view_size)
1957 Output_segment* tls_segment = relinfo->layout->tls_segment();
1959 const Sized_relobj<64, false>* object = relinfo->object;
1960 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1962 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1964 const bool is_final = (gsym == NULL
1965 ? !parameters->options().output_is_position_independent()
1966 : gsym->final_value_is_known());
1967 const tls::Tls_optimization optimized_type
1968 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1971 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1972 this->saw_tls_block_reloc_ = true;
1973 if (optimized_type == tls::TLSOPT_TO_LE)
1975 gold_assert(tls_segment != NULL);
1976 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1977 rela, r_type, value, view,
1983 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1984 ? GOT_TYPE_TLS_OFFSET
1985 : GOT_TYPE_TLS_PAIR);
1986 unsigned int got_offset;
1989 gold_assert(gsym->has_got_offset(got_type));
1990 got_offset = gsym->got_offset(got_type) - target->got_size();
1994 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1995 gold_assert(object->local_has_got_offset(r_sym, got_type));
1996 got_offset = (object->local_got_offset(r_sym, got_type)
1997 - target->got_size());
1999 if (optimized_type == tls::TLSOPT_TO_IE)
2001 gold_assert(tls_segment != NULL);
2002 value = target->got_plt_section()->address() + got_offset;
2003 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2004 value, view, address, view_size);
2007 else if (optimized_type == tls::TLSOPT_NONE)
2009 // Relocate the field with the offset of the pair of GOT
2011 value = target->got_plt_section()->address() + got_offset;
2012 Relocate_functions<64, false>::pcrela32(view, value, addend,
2017 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2018 _("unsupported reloc %u"), r_type);
2021 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2022 case elfcpp::R_X86_64_TLSDESC_CALL:
2023 this->saw_tls_block_reloc_ = true;
2024 if (optimized_type == tls::TLSOPT_TO_LE)
2026 gold_assert(tls_segment != NULL);
2027 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2028 rela, r_type, value, view,
2034 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2035 ? GOT_TYPE_TLS_OFFSET
2036 : GOT_TYPE_TLS_DESC);
2037 unsigned int got_offset;
2040 gold_assert(gsym->has_got_offset(got_type));
2041 got_offset = gsym->got_offset(got_type) - target->got_size();
2045 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2046 gold_assert(object->local_has_got_offset(r_sym, got_type));
2047 got_offset = (object->local_got_offset(r_sym, got_type)
2048 - target->got_size());
2050 if (optimized_type == tls::TLSOPT_TO_IE)
2052 gold_assert(tls_segment != NULL);
2053 value = target->got_plt_section()->address() + got_offset;
2054 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2055 rela, r_type, value, view, address,
2059 else if (optimized_type == tls::TLSOPT_NONE)
2061 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2063 // Relocate the field with the offset of the pair of GOT
2065 value = target->got_plt_section()->address() + got_offset;
2066 Relocate_functions<64, false>::pcrela32(view, value, addend,
2072 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2073 _("unsupported reloc %u"), r_type);
2076 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2077 this->saw_tls_block_reloc_ = true;
2078 if (optimized_type == tls::TLSOPT_TO_LE)
2080 gold_assert(tls_segment != NULL);
2081 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2082 value, view, view_size);
2085 else if (optimized_type == tls::TLSOPT_NONE)
2087 // Relocate the field with the offset of the GOT entry for
2088 // the module index.
2089 unsigned int got_offset;
2090 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2091 - target->got_size());
2092 value = target->got_plt_section()->address() + got_offset;
2093 Relocate_functions<64, false>::pcrela32(view, value, addend,
2097 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2098 _("unsupported reloc %u"), r_type);
2101 case elfcpp::R_X86_64_DTPOFF32:
2102 gold_assert(tls_segment != NULL);
2103 if (optimized_type == tls::TLSOPT_TO_LE)
2105 // This relocation type is used in debugging information.
2106 // In that case we need to not optimize the value. If we
2107 // haven't seen a TLSLD reloc, then we assume we should not
2108 // optimize this reloc.
2109 if (this->saw_tls_block_reloc_)
2110 value -= tls_segment->memsz();
2112 Relocate_functions<64, false>::rela32(view, value, addend);
2115 case elfcpp::R_X86_64_DTPOFF64:
2116 gold_assert(tls_segment != NULL);
2117 if (optimized_type == tls::TLSOPT_TO_LE)
2119 // See R_X86_64_DTPOFF32, just above, for why we test this.
2120 if (this->saw_tls_block_reloc_)
2121 value -= tls_segment->memsz();
2123 Relocate_functions<64, false>::rela64(view, value, addend);
2126 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2127 if (optimized_type == tls::TLSOPT_TO_LE)
2129 gold_assert(tls_segment != NULL);
2130 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2131 rela, r_type, value, view,
2135 else if (optimized_type == tls::TLSOPT_NONE)
2137 // Relocate the field with the offset of the GOT entry for
2138 // the tp-relative offset of the symbol.
2139 unsigned int got_offset;
2142 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2143 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2144 - target->got_size());
2148 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2149 gold_assert(object->local_has_got_offset(r_sym,
2150 GOT_TYPE_TLS_OFFSET));
2151 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2152 - target->got_size());
2154 value = target->got_plt_section()->address() + got_offset;
2155 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2158 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2159 _("unsupported reloc type %u"),
2163 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2164 value -= tls_segment->memsz();
2165 Relocate_functions<64, false>::rela32(view, value, addend);
2170 // Do a relocation in which we convert a TLS General-Dynamic to an
2174 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2177 const elfcpp::Rela<64, false>& rela,
2179 elfcpp::Elf_types<64>::Elf_Addr value,
2180 unsigned char* view,
2181 elfcpp::Elf_types<64>::Elf_Addr address,
2182 section_size_type view_size)
2184 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2185 // .word 0x6666; rex64; call __tls_get_addr
2186 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2188 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2189 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2191 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2192 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2193 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2194 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2196 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2198 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2199 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2201 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2203 this->skip_call_tls_get_addr_ = true;
2206 // Do a relocation in which we convert a TLS General-Dynamic to a
2210 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2212 Output_segment* tls_segment,
2213 const elfcpp::Rela<64, false>& rela,
2215 elfcpp::Elf_types<64>::Elf_Addr value,
2216 unsigned char* view,
2217 section_size_type view_size)
2219 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2220 // .word 0x6666; rex64; call __tls_get_addr
2221 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2223 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2224 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2226 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2227 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2228 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2229 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2231 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2233 value -= tls_segment->memsz();
2234 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2236 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2238 this->skip_call_tls_get_addr_ = true;
2241 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2244 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2245 const Relocate_info<64, false>* relinfo,
2248 const elfcpp::Rela<64, false>& rela,
2249 unsigned int r_type,
2250 elfcpp::Elf_types<64>::Elf_Addr value,
2251 unsigned char* view,
2252 elfcpp::Elf_types<64>::Elf_Addr address,
2253 section_size_type view_size)
2255 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2257 // leaq foo@tlsdesc(%rip), %rax
2258 // ==> movq foo@gottpoff(%rip), %rax
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);
2261 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2262 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2264 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2265 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2269 // call *foo@tlscall(%rax)
2271 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2272 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2273 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2274 view[0] == 0xff && view[1] == 0x10);
2280 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2283 Target_x86_64::Relocate::tls_desc_gd_to_le(
2284 const Relocate_info<64, false>* relinfo,
2286 Output_segment* tls_segment,
2287 const elfcpp::Rela<64, false>& rela,
2288 unsigned int r_type,
2289 elfcpp::Elf_types<64>::Elf_Addr value,
2290 unsigned char* view,
2291 section_size_type view_size)
2293 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2295 // leaq foo@tlsdesc(%rip), %rax
2296 // ==> movq foo@tpoff, %rax
2297 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2298 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2299 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2300 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2303 value -= tls_segment->memsz();
2304 Relocate_functions<64, false>::rela32(view, value, 0);
2308 // call *foo@tlscall(%rax)
2310 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2311 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2312 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2313 view[0] == 0xff && view[1] == 0x10);
2320 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2323 const elfcpp::Rela<64, false>& rela,
2325 elfcpp::Elf_types<64>::Elf_Addr,
2326 unsigned char* view,
2327 section_size_type view_size)
2329 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2330 // ... leq foo@dtpoff(%rax),%reg
2331 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2333 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2334 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2336 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2337 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2339 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2341 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2343 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2345 this->skip_call_tls_get_addr_ = true;
2348 // Do a relocation in which we convert a TLS Initial-Exec to a
2352 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2354 Output_segment* tls_segment,
2355 const elfcpp::Rela<64, false>& rela,
2357 elfcpp::Elf_types<64>::Elf_Addr value,
2358 unsigned char* view,
2359 section_size_type view_size)
2361 // We need to examine the opcodes to figure out which instruction we
2364 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2365 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2367 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2368 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2370 unsigned char op1 = view[-3];
2371 unsigned char op2 = view[-2];
2372 unsigned char op3 = view[-1];
2373 unsigned char reg = op3 >> 3;
2381 view[-1] = 0xc0 | reg;
2385 // Special handling for %rsp.
2389 view[-1] = 0xc0 | reg;
2397 view[-1] = 0x80 | reg | (reg << 3);
2400 value -= tls_segment->memsz();
2401 Relocate_functions<64, false>::rela32(view, value, 0);
2404 // Relocate section data.
2407 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2408 unsigned int sh_type,
2409 const unsigned char* prelocs,
2411 Output_section* output_section,
2412 bool needs_special_offset_handling,
2413 unsigned char* view,
2414 elfcpp::Elf_types<64>::Elf_Addr address,
2415 section_size_type view_size)
2417 gold_assert(sh_type == elfcpp::SHT_RELA);
2419 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2420 Target_x86_64::Relocate>(
2426 needs_special_offset_handling,
2432 // Return the size of a relocation while scanning during a relocatable
2436 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2437 unsigned int r_type,
2442 case elfcpp::R_X86_64_NONE:
2443 case elfcpp::R_386_GNU_VTINHERIT:
2444 case elfcpp::R_386_GNU_VTENTRY:
2445 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2446 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2447 case elfcpp::R_X86_64_TLSDESC_CALL:
2448 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2449 case elfcpp::R_X86_64_DTPOFF32:
2450 case elfcpp::R_X86_64_DTPOFF64:
2451 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2452 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2455 case elfcpp::R_X86_64_64:
2456 case elfcpp::R_X86_64_PC64:
2457 case elfcpp::R_X86_64_GOTOFF64:
2458 case elfcpp::R_X86_64_GOTPC64:
2459 case elfcpp::R_X86_64_PLTOFF64:
2460 case elfcpp::R_X86_64_GOT64:
2461 case elfcpp::R_X86_64_GOTPCREL64:
2462 case elfcpp::R_X86_64_GOTPCREL:
2463 case elfcpp::R_X86_64_GOTPLT64:
2466 case elfcpp::R_X86_64_32:
2467 case elfcpp::R_X86_64_32S:
2468 case elfcpp::R_X86_64_PC32:
2469 case elfcpp::R_X86_64_PLT32:
2470 case elfcpp::R_X86_64_GOTPC32:
2471 case elfcpp::R_X86_64_GOT32:
2474 case elfcpp::R_X86_64_16:
2475 case elfcpp::R_X86_64_PC16:
2478 case elfcpp::R_X86_64_8:
2479 case elfcpp::R_X86_64_PC8:
2482 case elfcpp::R_X86_64_COPY:
2483 case elfcpp::R_X86_64_GLOB_DAT:
2484 case elfcpp::R_X86_64_JUMP_SLOT:
2485 case elfcpp::R_X86_64_RELATIVE:
2486 // These are outstanding tls relocs, which are unexpected when linking
2487 case elfcpp::R_X86_64_TPOFF64:
2488 case elfcpp::R_X86_64_DTPMOD64:
2489 case elfcpp::R_X86_64_TLSDESC:
2490 object->error(_("unexpected reloc %u in object file"), r_type);
2493 case elfcpp::R_X86_64_SIZE32:
2494 case elfcpp::R_X86_64_SIZE64:
2496 object->error(_("unsupported reloc %u against local symbol"), r_type);
2501 // Scan the relocs during a relocatable link.
2504 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2505 Symbol_table* symtab,
2507 Sized_relobj<64, false>* object,
2508 unsigned int data_shndx,
2509 unsigned int sh_type,
2510 const unsigned char* prelocs,
2512 Output_section* output_section,
2513 bool needs_special_offset_handling,
2514 size_t local_symbol_count,
2515 const unsigned char* plocal_symbols,
2516 Relocatable_relocs* rr)
2518 gold_assert(sh_type == elfcpp::SHT_RELA);
2520 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2521 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2523 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2524 Scan_relocatable_relocs>(
2533 needs_special_offset_handling,
2539 // Relocate a section during a relocatable link.
2542 Target_x86_64::relocate_for_relocatable(
2543 const Relocate_info<64, false>* relinfo,
2544 unsigned int sh_type,
2545 const unsigned char* prelocs,
2547 Output_section* output_section,
2548 off_t offset_in_output_section,
2549 const Relocatable_relocs* rr,
2550 unsigned char* view,
2551 elfcpp::Elf_types<64>::Elf_Addr view_address,
2552 section_size_type view_size,
2553 unsigned char* reloc_view,
2554 section_size_type reloc_view_size)
2556 gold_assert(sh_type == elfcpp::SHT_RELA);
2558 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2563 offset_in_output_section,
2572 // Return the value to use for a dynamic which requires special
2573 // treatment. This is how we support equality comparisons of function
2574 // pointers across shared library boundaries, as described in the
2575 // processor specific ABI supplement.
2578 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2580 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2581 return this->plt_section()->address() + gsym->plt_offset();
2584 // Return a string used to fill a code section with nops to take up
2585 // the specified length.
2588 Target_x86_64::do_code_fill(section_size_type length) const
2592 // Build a jmpq instruction to skip over the bytes.
2593 unsigned char jmp[5];
2595 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2596 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2597 + std::string(length - 5, '\0'));
2600 // Nop sequences of various lengths.
2601 const char nop1[1] = { 0x90 }; // nop
2602 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2603 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2604 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2605 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2606 0x00 }; // leal 0(%esi,1),%esi
2607 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2609 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2611 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2612 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2613 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2614 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2616 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2617 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2619 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2620 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2622 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2623 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2624 0x00, 0x00, 0x00, 0x00 };
2625 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2626 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2627 0x27, 0x00, 0x00, 0x00,
2629 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2630 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2631 0xbc, 0x27, 0x00, 0x00,
2633 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2634 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2635 0x90, 0x90, 0x90, 0x90,
2638 const char* nops[16] = {
2640 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2641 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2644 return std::string(nops[length], length);
2647 // The selector for x86_64 object files.
2649 class Target_selector_x86_64 : public Target_selector
2652 Target_selector_x86_64()
2653 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2657 do_instantiate_target()
2658 { return new Target_x86_64(); }
2661 Target_selector_x86_64 target_selector_x86_64;
2663 } // End anonymous namespace.