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>();
438 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
439 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
442 // The old GNU linker creates a .got.plt section. We just
443 // create another set of data in the .got section. Note that we
444 // always create a PLT if we create a GOT, although the PLT
446 this->got_plt_ = new Output_data_space(8);
447 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
448 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
451 // The first three entries are reserved.
452 this->got_plt_->set_current_data_size(3 * 8);
454 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
455 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
457 0, 0, elfcpp::STT_OBJECT,
459 elfcpp::STV_HIDDEN, 0,
466 // Get the dynamic reloc section, creating it if necessary.
468 Target_x86_64::Reloc_section*
469 Target_x86_64::rela_dyn_section(Layout* layout)
471 if (this->rela_dyn_ == NULL)
473 gold_assert(layout != NULL);
474 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
475 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
476 elfcpp::SHF_ALLOC, this->rela_dyn_);
478 return this->rela_dyn_;
481 // A class to handle the PLT data.
483 class Output_data_plt_x86_64 : public Output_section_data
486 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
488 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
491 // Add an entry to the PLT.
493 add_entry(Symbol* gsym);
495 // Add the reserved TLSDESC_PLT entry to the PLT.
497 reserve_tlsdesc_entry(unsigned int got_offset)
498 { this->tlsdesc_got_offset_ = got_offset; }
500 // Return true if a TLSDESC_PLT entry has been reserved.
502 has_tlsdesc_entry() const
503 { return this->tlsdesc_got_offset_ != -1U; }
505 // Return the GOT offset for the reserved TLSDESC_PLT entry.
507 get_tlsdesc_got_offset() const
508 { return this->tlsdesc_got_offset_; }
510 // Return the offset of the reserved TLSDESC_PLT entry.
512 get_tlsdesc_plt_offset() const
513 { return (this->count_ + 1) * plt_entry_size; }
515 // Return the .rel.plt section data.
518 { return this->rel_; }
522 do_adjust_output_section(Output_section* os);
525 // The size of an entry in the PLT.
526 static const int plt_entry_size = 16;
528 // The first entry in the PLT.
529 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
530 // procedure linkage table for both programs and shared objects."
531 static unsigned char first_plt_entry[plt_entry_size];
533 // Other entries in the PLT for an executable.
534 static unsigned char plt_entry[plt_entry_size];
536 // The reserved TLSDESC entry in the PLT for an executable.
537 static unsigned char tlsdesc_plt_entry[plt_entry_size];
539 // Set the final size.
541 set_final_data_size();
543 // Write out the PLT data.
545 do_write(Output_file*);
547 // The reloc section.
550 Output_data_got<64, false>* got_;
551 // The .got.plt section.
552 Output_data_space* got_plt_;
553 // The number of PLT entries.
555 // Offset of the reserved TLSDESC_GOT entry when needed.
556 unsigned int tlsdesc_got_offset_;
559 // Create the PLT section. The ordinary .got section is an argument,
560 // since we need to refer to the start. We also create our own .got
561 // section just for PLT entries.
563 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
564 Output_data_got<64, false>* got,
565 Output_data_space* got_plt)
566 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
567 tlsdesc_got_offset_(-1U)
569 this->rel_ = new Reloc_section(false);
570 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
571 elfcpp::SHF_ALLOC, this->rel_);
575 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
577 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
578 // linker, and so do we.
582 // Add an entry to the PLT.
585 Output_data_plt_x86_64::add_entry(Symbol* gsym)
587 gold_assert(!gsym->has_plt_offset());
589 // Note that when setting the PLT offset we skip the initial
590 // reserved PLT entry.
591 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
595 section_offset_type got_offset = this->got_plt_->current_data_size();
597 // Every PLT entry needs a GOT entry which points back to the PLT
598 // entry (this will be changed by the dynamic linker, normally
599 // lazily when the function is called).
600 this->got_plt_->set_current_data_size(got_offset + 8);
602 // Every PLT entry needs a reloc.
603 gsym->set_needs_dynsym_entry();
604 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
607 // Note that we don't need to save the symbol. The contents of the
608 // PLT are independent of which symbols are used. The symbols only
609 // appear in the relocations.
612 // Set the final size.
614 Output_data_plt_x86_64::set_final_data_size()
616 unsigned int count = this->count_;
617 if (this->has_tlsdesc_entry())
619 this->set_data_size((count + 1) * plt_entry_size);
622 // The first entry in the PLT for an executable.
624 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
626 // From AMD64 ABI Draft 0.98, page 76
627 0xff, 0x35, // pushq contents of memory address
628 0, 0, 0, 0, // replaced with address of .got + 8
629 0xff, 0x25, // jmp indirect
630 0, 0, 0, 0, // replaced with address of .got + 16
631 0x90, 0x90, 0x90, 0x90 // noop (x4)
634 // Subsequent entries in the PLT for an executable.
636 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
638 // From AMD64 ABI Draft 0.98, page 76
639 0xff, 0x25, // jmpq indirect
640 0, 0, 0, 0, // replaced with address of symbol in .got
641 0x68, // pushq immediate
642 0, 0, 0, 0, // replaced with offset into relocation table
643 0xe9, // jmpq relative
644 0, 0, 0, 0 // replaced with offset to start of .plt
647 // The reserved TLSDESC entry in the PLT for an executable.
649 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
651 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
652 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
653 0xff, 0x35, // pushq x(%rip)
654 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
655 0xff, 0x25, // jmpq *y(%rip)
656 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
661 // Write out the PLT. This uses the hand-coded instructions above,
662 // and adjusts them as needed. This is specified by the AMD64 ABI.
665 Output_data_plt_x86_64::do_write(Output_file* of)
667 const off_t offset = this->offset();
668 const section_size_type oview_size =
669 convert_to_section_size_type(this->data_size());
670 unsigned char* const oview = of->get_output_view(offset, oview_size);
672 const off_t got_file_offset = this->got_plt_->offset();
673 const section_size_type got_size =
674 convert_to_section_size_type(this->got_plt_->data_size());
675 unsigned char* const got_view = of->get_output_view(got_file_offset,
678 unsigned char* pov = oview;
680 // The base address of the .plt section.
681 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
682 // The base address of the .got section.
683 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
684 // The base address of the PLT portion of the .got section,
685 // which is where the GOT pointer will point, and where the
686 // three reserved GOT entries are located.
687 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
689 memcpy(pov, first_plt_entry, plt_entry_size);
690 // We do a jmp relative to the PC at the end of this instruction.
691 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
693 - (plt_address + 6)));
694 elfcpp::Swap<32, false>::writeval(pov + 8,
696 - (plt_address + 12)));
697 pov += plt_entry_size;
699 unsigned char* got_pov = got_view;
701 memset(got_pov, 0, 24);
704 unsigned int plt_offset = plt_entry_size;
705 unsigned int got_offset = 24;
706 const unsigned int count = this->count_;
707 for (unsigned int plt_index = 0;
710 pov += plt_entry_size,
712 plt_offset += plt_entry_size,
715 // Set and adjust the PLT entry itself.
716 memcpy(pov, plt_entry, plt_entry_size);
717 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
718 (got_address + got_offset
719 - (plt_address + plt_offset
722 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
723 elfcpp::Swap<32, false>::writeval(pov + 12,
724 - (plt_offset + plt_entry_size));
726 // Set the entry in the GOT.
727 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
730 if (this->has_tlsdesc_entry())
732 // Set and adjust the reserved TLSDESC PLT entry.
733 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
734 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
735 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
737 - (plt_address + plt_offset
739 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
742 - (plt_address + plt_offset
744 pov += plt_entry_size;
747 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
748 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
750 of->write_output_view(offset, oview_size, oview);
751 of->write_output_view(got_file_offset, got_size, got_view);
754 // Create the PLT section.
757 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
759 if (this->plt_ == NULL)
761 // Create the GOT sections first.
762 this->got_section(symtab, layout);
764 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
766 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
768 | elfcpp::SHF_EXECINSTR),
773 // Create a PLT entry for a global symbol.
776 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
779 if (gsym->has_plt_offset())
782 if (this->plt_ == NULL)
783 this->make_plt_section(symtab, layout);
785 this->plt_->add_entry(gsym);
788 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
791 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
793 if (this->tls_base_symbol_defined_)
796 Output_segment* tls_segment = layout->tls_segment();
797 if (tls_segment != NULL)
799 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
803 elfcpp::STV_HIDDEN, 0,
804 Symbol::SEGMENT_END, true);
806 this->tls_base_symbol_defined_ = true;
809 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
812 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
815 if (this->plt_ == NULL)
816 this->make_plt_section(symtab, layout);
818 if (!this->plt_->has_tlsdesc_entry())
820 // Allocate the TLSDESC_GOT entry.
821 Output_data_got<64, false>* got = this->got_section(symtab, layout);
822 unsigned int got_offset = got->add_constant(0);
824 // Allocate the TLSDESC_PLT entry.
825 this->plt_->reserve_tlsdesc_entry(got_offset);
829 // Create a GOT entry for the TLS module index.
832 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
833 Sized_relobj<64, false>* object)
835 if (this->got_mod_index_offset_ == -1U)
837 gold_assert(symtab != NULL && layout != NULL && object != NULL);
838 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
839 Output_data_got<64, false>* got = this->got_section(symtab, layout);
840 unsigned int got_offset = got->add_constant(0);
841 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
843 got->add_constant(0);
844 this->got_mod_index_offset_ = got_offset;
846 return this->got_mod_index_offset_;
849 // Optimize the TLS relocation type based on what we know about the
850 // symbol. IS_FINAL is true if the final address of this symbol is
851 // known at link time.
853 tls::Tls_optimization
854 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
856 // If we are generating a shared library, then we can't do anything
858 if (parameters->options().shared())
859 return tls::TLSOPT_NONE;
863 case elfcpp::R_X86_64_TLSGD:
864 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
865 case elfcpp::R_X86_64_TLSDESC_CALL:
866 // These are General-Dynamic which permits fully general TLS
867 // access. Since we know that we are generating an executable,
868 // we can convert this to Initial-Exec. If we also know that
869 // this is a local symbol, we can further switch to Local-Exec.
871 return tls::TLSOPT_TO_LE;
872 return tls::TLSOPT_TO_IE;
874 case elfcpp::R_X86_64_TLSLD:
875 // This is Local-Dynamic, which refers to a local symbol in the
876 // dynamic TLS block. Since we know that we generating an
877 // executable, we can switch to Local-Exec.
878 return tls::TLSOPT_TO_LE;
880 case elfcpp::R_X86_64_DTPOFF32:
881 case elfcpp::R_X86_64_DTPOFF64:
882 // Another Local-Dynamic reloc.
883 return tls::TLSOPT_TO_LE;
885 case elfcpp::R_X86_64_GOTTPOFF:
886 // These are Initial-Exec relocs which get the thread offset
887 // from the GOT. If we know that we are linking against the
888 // local symbol, we can switch to Local-Exec, which links the
889 // thread offset into the instruction.
891 return tls::TLSOPT_TO_LE;
892 return tls::TLSOPT_NONE;
894 case elfcpp::R_X86_64_TPOFF32:
895 // When we already have Local-Exec, there is nothing further we
897 return tls::TLSOPT_NONE;
904 // Report an unsupported relocation against a local symbol.
907 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
910 gold_error(_("%s: unsupported reloc %u against local symbol"),
911 object->name().c_str(), r_type);
914 // We are about to emit a dynamic relocation of type R_TYPE. If the
915 // dynamic linker does not support it, issue an error. The GNU linker
916 // only issues a non-PIC error for an allocated read-only section.
917 // Here we know the section is allocated, but we don't know that it is
918 // read-only. But we check for all the relocation types which the
919 // glibc dynamic linker supports, so it seems appropriate to issue an
920 // error even if the section is not read-only.
923 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
927 // These are the relocation types supported by glibc for x86_64.
928 case elfcpp::R_X86_64_RELATIVE:
929 case elfcpp::R_X86_64_GLOB_DAT:
930 case elfcpp::R_X86_64_JUMP_SLOT:
931 case elfcpp::R_X86_64_DTPMOD64:
932 case elfcpp::R_X86_64_DTPOFF64:
933 case elfcpp::R_X86_64_TPOFF64:
934 case elfcpp::R_X86_64_64:
935 case elfcpp::R_X86_64_32:
936 case elfcpp::R_X86_64_PC32:
937 case elfcpp::R_X86_64_COPY:
941 // This prevents us from issuing more than one error per reloc
942 // section. But we can still wind up issuing more than one
943 // error per object file.
944 if (this->issued_non_pic_error_)
946 object->error(_("requires unsupported dynamic reloc; "
947 "recompile with -fPIC"));
948 this->issued_non_pic_error_ = true;
951 case elfcpp::R_X86_64_NONE:
956 // Scan a relocation for a local symbol.
959 Target_x86_64::Scan::local(const General_options&,
960 Symbol_table* symtab,
962 Target_x86_64* target,
963 Sized_relobj<64, false>* object,
964 unsigned int data_shndx,
965 Output_section* output_section,
966 const elfcpp::Rela<64, false>& reloc,
968 const elfcpp::Sym<64, false>& lsym)
972 case elfcpp::R_X86_64_NONE:
973 case elfcpp::R_386_GNU_VTINHERIT:
974 case elfcpp::R_386_GNU_VTENTRY:
977 case elfcpp::R_X86_64_64:
978 // If building a shared library (or a position-independent
979 // executable), we need to create a dynamic relocation for this
980 // location. The relocation applied at link time will apply the
981 // link-time value, so we flag the location with an
982 // R_X86_64_RELATIVE relocation so the dynamic loader can
983 // relocate it easily.
984 if (parameters->options().output_is_position_independent())
986 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
987 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
988 rela_dyn->add_local_relative(object, r_sym,
989 elfcpp::R_X86_64_RELATIVE,
990 output_section, data_shndx,
991 reloc.get_r_offset(),
992 reloc.get_r_addend());
996 case elfcpp::R_X86_64_32:
997 case elfcpp::R_X86_64_32S:
998 case elfcpp::R_X86_64_16:
999 case elfcpp::R_X86_64_8:
1000 // If building a shared library (or a position-independent
1001 // executable), we need to create a dynamic relocation for this
1002 // location. We can't use an R_X86_64_RELATIVE relocation
1003 // because that is always a 64-bit relocation.
1004 if (parameters->options().output_is_position_independent())
1006 this->check_non_pic(object, r_type);
1008 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1009 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1010 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1011 rela_dyn->add_local(object, r_sym, r_type, output_section,
1012 data_shndx, reloc.get_r_offset(),
1013 reloc.get_r_addend());
1016 gold_assert(lsym.get_st_value() == 0);
1017 unsigned int shndx = lsym.get_st_shndx();
1019 shndx = object->adjust_sym_shndx(r_sym, shndx,
1022 object->error(_("section symbol %u has bad shndx %u"),
1025 rela_dyn->add_local_section(object, shndx,
1026 r_type, output_section,
1027 data_shndx, reloc.get_r_offset(),
1028 reloc.get_r_addend());
1033 case elfcpp::R_X86_64_PC64:
1034 case elfcpp::R_X86_64_PC32:
1035 case elfcpp::R_X86_64_PC16:
1036 case elfcpp::R_X86_64_PC8:
1039 case elfcpp::R_X86_64_PLT32:
1040 // Since we know this is a local symbol, we can handle this as a
1044 case elfcpp::R_X86_64_GOTPC32:
1045 case elfcpp::R_X86_64_GOTOFF64:
1046 case elfcpp::R_X86_64_GOTPC64:
1047 case elfcpp::R_X86_64_PLTOFF64:
1048 // We need a GOT section.
1049 target->got_section(symtab, layout);
1050 // For PLTOFF64, we'd normally want a PLT section, but since we
1051 // know this is a local symbol, no PLT is needed.
1054 case elfcpp::R_X86_64_GOT64:
1055 case elfcpp::R_X86_64_GOT32:
1056 case elfcpp::R_X86_64_GOTPCREL64:
1057 case elfcpp::R_X86_64_GOTPCREL:
1058 case elfcpp::R_X86_64_GOTPLT64:
1060 // The symbol requires a GOT entry.
1061 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1062 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1063 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1065 // If we are generating a shared object, we need to add a
1066 // dynamic relocation for this symbol's GOT entry.
1067 if (parameters->options().output_is_position_independent())
1069 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1070 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1071 if (r_type != elfcpp::R_X86_64_GOT32)
1072 rela_dyn->add_local_relative(
1073 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1074 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1077 this->check_non_pic(object, r_type);
1079 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1080 rela_dyn->add_local(
1081 object, r_sym, r_type, got,
1082 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1086 // For GOTPLT64, we'd normally want a PLT section, but since
1087 // we know this is a local symbol, no PLT is needed.
1091 case elfcpp::R_X86_64_COPY:
1092 case elfcpp::R_X86_64_GLOB_DAT:
1093 case elfcpp::R_X86_64_JUMP_SLOT:
1094 case elfcpp::R_X86_64_RELATIVE:
1095 // These are outstanding tls relocs, which are unexpected when linking
1096 case elfcpp::R_X86_64_TPOFF64:
1097 case elfcpp::R_X86_64_DTPMOD64:
1098 case elfcpp::R_X86_64_TLSDESC:
1099 gold_error(_("%s: unexpected reloc %u in object file"),
1100 object->name().c_str(), r_type);
1103 // These are initial tls relocs, which are expected when linking
1104 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1105 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1106 case elfcpp::R_X86_64_TLSDESC_CALL:
1107 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1108 case elfcpp::R_X86_64_DTPOFF32:
1109 case elfcpp::R_X86_64_DTPOFF64:
1110 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1111 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1113 bool output_is_shared = parameters->options().shared();
1114 const tls::Tls_optimization optimized_type
1115 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1118 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1119 if (optimized_type == tls::TLSOPT_NONE)
1121 // Create a pair of GOT entries for the module index and
1122 // dtv-relative offset.
1123 Output_data_got<64, false>* got
1124 = target->got_section(symtab, layout);
1125 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1126 unsigned int shndx = lsym.get_st_shndx();
1128 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1130 object->error(_("local symbol %u has bad shndx %u"),
1133 got->add_local_pair_with_rela(object, r_sym,
1136 target->rela_dyn_section(layout),
1137 elfcpp::R_X86_64_DTPMOD64, 0);
1139 else if (optimized_type != tls::TLSOPT_TO_LE)
1140 unsupported_reloc_local(object, r_type);
1143 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1144 target->define_tls_base_symbol(symtab, layout);
1145 if (optimized_type == tls::TLSOPT_NONE)
1147 // Create reserved PLT and GOT entries for the resolver.
1148 target->reserve_tlsdesc_entries(symtab, layout);
1150 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1151 Output_data_got<64, false>* got
1152 = target->got_section(symtab, layout);
1153 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1154 unsigned int shndx = lsym.get_st_shndx();
1156 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1158 object->error(_("local symbol %u has bad shndx %u"),
1161 got->add_local_pair_with_rela(object, r_sym,
1164 target->rela_dyn_section(layout),
1165 elfcpp::R_X86_64_TLSDESC, 0);
1167 else if (optimized_type != tls::TLSOPT_TO_LE)
1168 unsupported_reloc_local(object, r_type);
1171 case elfcpp::R_X86_64_TLSDESC_CALL:
1174 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1175 if (optimized_type == tls::TLSOPT_NONE)
1177 // Create a GOT entry for the module index.
1178 target->got_mod_index_entry(symtab, layout, object);
1180 else if (optimized_type != tls::TLSOPT_TO_LE)
1181 unsupported_reloc_local(object, r_type);
1184 case elfcpp::R_X86_64_DTPOFF32:
1185 case elfcpp::R_X86_64_DTPOFF64:
1188 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1189 layout->set_has_static_tls();
1190 if (optimized_type == tls::TLSOPT_NONE)
1192 // Create a GOT entry for the tp-relative offset.
1193 Output_data_got<64, false>* got
1194 = target->got_section(symtab, layout);
1195 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1196 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1197 target->rela_dyn_section(layout),
1198 elfcpp::R_X86_64_TPOFF64);
1200 else if (optimized_type != tls::TLSOPT_TO_LE)
1201 unsupported_reloc_local(object, r_type);
1204 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1205 layout->set_has_static_tls();
1206 if (output_is_shared)
1207 unsupported_reloc_local(object, r_type);
1216 case elfcpp::R_X86_64_SIZE32:
1217 case elfcpp::R_X86_64_SIZE64:
1219 gold_error(_("%s: unsupported reloc %u against local symbol"),
1220 object->name().c_str(), r_type);
1226 // Report an unsupported relocation against a global symbol.
1229 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1230 unsigned int r_type,
1233 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1234 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1237 // Scan a relocation for a global symbol.
1240 Target_x86_64::Scan::global(const General_options&,
1241 Symbol_table* symtab,
1243 Target_x86_64* target,
1244 Sized_relobj<64, false>* object,
1245 unsigned int data_shndx,
1246 Output_section* output_section,
1247 const elfcpp::Rela<64, false>& reloc,
1248 unsigned int r_type,
1253 case elfcpp::R_X86_64_NONE:
1254 case elfcpp::R_386_GNU_VTINHERIT:
1255 case elfcpp::R_386_GNU_VTENTRY:
1258 case elfcpp::R_X86_64_64:
1259 case elfcpp::R_X86_64_32:
1260 case elfcpp::R_X86_64_32S:
1261 case elfcpp::R_X86_64_16:
1262 case elfcpp::R_X86_64_8:
1264 // Make a PLT entry if necessary.
1265 if (gsym->needs_plt_entry())
1267 target->make_plt_entry(symtab, layout, gsym);
1268 // Since this is not a PC-relative relocation, we may be
1269 // taking the address of a function. In that case we need to
1270 // set the entry in the dynamic symbol table to the address of
1272 if (gsym->is_from_dynobj() && !parameters->options().shared())
1273 gsym->set_needs_dynsym_value();
1275 // Make a dynamic relocation if necessary.
1276 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1278 if (target->may_need_copy_reloc(gsym))
1280 target->copy_reloc(symtab, layout, object,
1281 data_shndx, output_section, gsym, reloc);
1283 else if (r_type == elfcpp::R_X86_64_64
1284 && gsym->can_use_relative_reloc(false))
1286 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1287 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1288 output_section, object,
1289 data_shndx, reloc.get_r_offset(),
1290 reloc.get_r_addend());
1294 this->check_non_pic(object, r_type);
1295 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1296 rela_dyn->add_global(gsym, r_type, output_section, object,
1297 data_shndx, reloc.get_r_offset(),
1298 reloc.get_r_addend());
1304 case elfcpp::R_X86_64_PC64:
1305 case elfcpp::R_X86_64_PC32:
1306 case elfcpp::R_X86_64_PC16:
1307 case elfcpp::R_X86_64_PC8:
1309 // Make a PLT entry if necessary.
1310 if (gsym->needs_plt_entry())
1311 target->make_plt_entry(symtab, layout, gsym);
1312 // Make a dynamic relocation if necessary.
1313 int flags = Symbol::NON_PIC_REF;
1314 if (gsym->type() == elfcpp::STT_FUNC)
1315 flags |= Symbol::FUNCTION_CALL;
1316 if (gsym->needs_dynamic_reloc(flags))
1318 if (target->may_need_copy_reloc(gsym))
1320 target->copy_reloc(symtab, layout, object,
1321 data_shndx, output_section, gsym, reloc);
1325 this->check_non_pic(object, r_type);
1326 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1327 rela_dyn->add_global(gsym, r_type, output_section, object,
1328 data_shndx, reloc.get_r_offset(),
1329 reloc.get_r_addend());
1335 case elfcpp::R_X86_64_GOT64:
1336 case elfcpp::R_X86_64_GOT32:
1337 case elfcpp::R_X86_64_GOTPCREL64:
1338 case elfcpp::R_X86_64_GOTPCREL:
1339 case elfcpp::R_X86_64_GOTPLT64:
1341 // The symbol requires a GOT entry.
1342 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1343 if (gsym->final_value_is_known())
1344 got->add_global(gsym, GOT_TYPE_STANDARD);
1347 // If this symbol is not fully resolved, we need to add a
1348 // dynamic relocation for it.
1349 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1350 if (gsym->is_from_dynobj()
1351 || gsym->is_undefined()
1352 || gsym->is_preemptible())
1353 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1354 elfcpp::R_X86_64_GLOB_DAT);
1357 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1358 rela_dyn->add_global_relative(
1359 gsym, elfcpp::R_X86_64_RELATIVE, got,
1360 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1363 // For GOTPLT64, we also need a PLT entry (but only if the
1364 // symbol is not fully resolved).
1365 if (r_type == elfcpp::R_X86_64_GOTPLT64
1366 && !gsym->final_value_is_known())
1367 target->make_plt_entry(symtab, layout, gsym);
1371 case elfcpp::R_X86_64_PLT32:
1372 // If the symbol is fully resolved, this is just a PC32 reloc.
1373 // Otherwise we need a PLT entry.
1374 if (gsym->final_value_is_known())
1376 // If building a shared library, we can also skip the PLT entry
1377 // if the symbol is defined in the output file and is protected
1379 if (gsym->is_defined()
1380 && !gsym->is_from_dynobj()
1381 && !gsym->is_preemptible())
1383 target->make_plt_entry(symtab, layout, gsym);
1386 case elfcpp::R_X86_64_GOTPC32:
1387 case elfcpp::R_X86_64_GOTOFF64:
1388 case elfcpp::R_X86_64_GOTPC64:
1389 case elfcpp::R_X86_64_PLTOFF64:
1390 // We need a GOT section.
1391 target->got_section(symtab, layout);
1392 // For PLTOFF64, we also need a PLT entry (but only if the
1393 // symbol is not fully resolved).
1394 if (r_type == elfcpp::R_X86_64_PLTOFF64
1395 && !gsym->final_value_is_known())
1396 target->make_plt_entry(symtab, layout, gsym);
1399 case elfcpp::R_X86_64_COPY:
1400 case elfcpp::R_X86_64_GLOB_DAT:
1401 case elfcpp::R_X86_64_JUMP_SLOT:
1402 case elfcpp::R_X86_64_RELATIVE:
1403 // These are outstanding tls relocs, which are unexpected when linking
1404 case elfcpp::R_X86_64_TPOFF64:
1405 case elfcpp::R_X86_64_DTPMOD64:
1406 case elfcpp::R_X86_64_TLSDESC:
1407 gold_error(_("%s: unexpected reloc %u in object file"),
1408 object->name().c_str(), r_type);
1411 // These are initial tls relocs, which are expected for global()
1412 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1413 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1414 case elfcpp::R_X86_64_TLSDESC_CALL:
1415 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1416 case elfcpp::R_X86_64_DTPOFF32:
1417 case elfcpp::R_X86_64_DTPOFF64:
1418 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1419 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1421 const bool is_final = gsym->final_value_is_known();
1422 const tls::Tls_optimization optimized_type
1423 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1426 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1427 if (optimized_type == tls::TLSOPT_NONE)
1429 // Create a pair of GOT entries for the module index and
1430 // dtv-relative offset.
1431 Output_data_got<64, false>* got
1432 = target->got_section(symtab, layout);
1433 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1434 target->rela_dyn_section(layout),
1435 elfcpp::R_X86_64_DTPMOD64,
1436 elfcpp::R_X86_64_DTPOFF64);
1438 else if (optimized_type == tls::TLSOPT_TO_IE)
1440 // Create a GOT entry for the tp-relative offset.
1441 Output_data_got<64, false>* got
1442 = target->got_section(symtab, layout);
1443 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1444 target->rela_dyn_section(layout),
1445 elfcpp::R_X86_64_TPOFF64);
1447 else if (optimized_type != tls::TLSOPT_TO_LE)
1448 unsupported_reloc_global(object, r_type, gsym);
1451 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1452 target->define_tls_base_symbol(symtab, layout);
1453 if (optimized_type == tls::TLSOPT_NONE)
1455 // Create reserved PLT and GOT entries for the resolver.
1456 target->reserve_tlsdesc_entries(symtab, layout);
1458 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1459 Output_data_got<64, false>* got
1460 = target->got_section(symtab, layout);
1461 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1462 target->rela_dyn_section(layout),
1463 elfcpp::R_X86_64_TLSDESC, 0);
1465 else if (optimized_type == tls::TLSOPT_TO_IE)
1467 // Create a GOT entry for the tp-relative offset.
1468 Output_data_got<64, false>* got
1469 = target->got_section(symtab, layout);
1470 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1471 target->rela_dyn_section(layout),
1472 elfcpp::R_X86_64_TPOFF64);
1474 else if (optimized_type != tls::TLSOPT_TO_LE)
1475 unsupported_reloc_global(object, r_type, gsym);
1478 case elfcpp::R_X86_64_TLSDESC_CALL:
1481 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1482 if (optimized_type == tls::TLSOPT_NONE)
1484 // Create a GOT entry for the module index.
1485 target->got_mod_index_entry(symtab, layout, object);
1487 else if (optimized_type != tls::TLSOPT_TO_LE)
1488 unsupported_reloc_global(object, r_type, gsym);
1491 case elfcpp::R_X86_64_DTPOFF32:
1492 case elfcpp::R_X86_64_DTPOFF64:
1495 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1496 layout->set_has_static_tls();
1497 if (optimized_type == tls::TLSOPT_NONE)
1499 // Create a GOT entry for the tp-relative offset.
1500 Output_data_got<64, false>* got
1501 = target->got_section(symtab, layout);
1502 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1503 target->rela_dyn_section(layout),
1504 elfcpp::R_X86_64_TPOFF64);
1506 else if (optimized_type != tls::TLSOPT_TO_LE)
1507 unsupported_reloc_global(object, r_type, gsym);
1510 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1511 layout->set_has_static_tls();
1512 if (parameters->options().shared())
1513 unsupported_reloc_local(object, r_type);
1522 case elfcpp::R_X86_64_SIZE32:
1523 case elfcpp::R_X86_64_SIZE64:
1525 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1526 object->name().c_str(), r_type,
1527 gsym->demangled_name().c_str());
1532 // Scan relocations for a section.
1535 Target_x86_64::scan_relocs(const General_options& options,
1536 Symbol_table* symtab,
1538 Sized_relobj<64, false>* object,
1539 unsigned int data_shndx,
1540 unsigned int sh_type,
1541 const unsigned char* prelocs,
1543 Output_section* output_section,
1544 bool needs_special_offset_handling,
1545 size_t local_symbol_count,
1546 const unsigned char* plocal_symbols)
1548 if (sh_type == elfcpp::SHT_REL)
1550 gold_error(_("%s: unsupported REL reloc section"),
1551 object->name().c_str());
1555 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1556 Target_x86_64::Scan>(
1566 needs_special_offset_handling,
1571 // Finalize the sections.
1574 Target_x86_64::do_finalize_sections(Layout* layout)
1576 // Fill in some more dynamic tags.
1577 Output_data_dynamic* const odyn = layout->dynamic_data();
1580 if (this->got_plt_ != NULL)
1581 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1583 if (this->plt_ != NULL)
1585 const Output_data* od = this->plt_->rel_plt();
1586 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1587 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1588 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1589 if (this->plt_->has_tlsdesc_entry())
1591 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1592 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1593 this->got_->finalize_data_size();
1594 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1595 this->plt_, plt_offset);
1596 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1597 this->got_, got_offset);
1601 if (this->rela_dyn_ != NULL)
1603 const Output_data* od = this->rela_dyn_;
1604 odyn->add_section_address(elfcpp::DT_RELA, od);
1605 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1606 odyn->add_constant(elfcpp::DT_RELAENT,
1607 elfcpp::Elf_sizes<64>::rela_size);
1610 if (!parameters->options().shared())
1612 // The value of the DT_DEBUG tag is filled in by the dynamic
1613 // linker at run time, and used by the debugger.
1614 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1618 // Emit any relocs we saved in an attempt to avoid generating COPY
1620 if (this->copy_relocs_.any_saved_relocs())
1621 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1624 // Perform a relocation.
1627 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1628 Target_x86_64* target,
1630 const elfcpp::Rela<64, false>& rela,
1631 unsigned int r_type,
1632 const Sized_symbol<64>* gsym,
1633 const Symbol_value<64>* psymval,
1634 unsigned char* view,
1635 elfcpp::Elf_types<64>::Elf_Addr address,
1636 section_size_type view_size)
1638 if (this->skip_call_tls_get_addr_)
1640 if (r_type != elfcpp::R_X86_64_PLT32
1642 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1644 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1645 _("missing expected TLS relocation"));
1649 this->skip_call_tls_get_addr_ = false;
1654 // Pick the value to use for symbols defined in shared objects.
1655 Symbol_value<64> symval;
1657 && (gsym->is_from_dynobj()
1658 || (parameters->options().shared()
1659 && (gsym->is_undefined() || gsym->is_preemptible())))
1660 && gsym->has_plt_offset())
1662 symval.set_output_value(target->plt_section()->address()
1663 + gsym->plt_offset());
1667 const Sized_relobj<64, false>* object = relinfo->object;
1668 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1670 // Get the GOT offset if needed.
1671 // The GOT pointer points to the end of the GOT section.
1672 // We need to subtract the size of the GOT section to get
1673 // the actual offset to use in the relocation.
1674 bool have_got_offset = false;
1675 unsigned int got_offset = 0;
1678 case elfcpp::R_X86_64_GOT32:
1679 case elfcpp::R_X86_64_GOT64:
1680 case elfcpp::R_X86_64_GOTPLT64:
1681 case elfcpp::R_X86_64_GOTPCREL:
1682 case elfcpp::R_X86_64_GOTPCREL64:
1685 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1686 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1690 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1691 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1692 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1693 - target->got_size());
1695 have_got_offset = true;
1704 case elfcpp::R_X86_64_NONE:
1705 case elfcpp::R_386_GNU_VTINHERIT:
1706 case elfcpp::R_386_GNU_VTENTRY:
1709 case elfcpp::R_X86_64_64:
1710 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1713 case elfcpp::R_X86_64_PC64:
1714 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1718 case elfcpp::R_X86_64_32:
1719 // FIXME: we need to verify that value + addend fits into 32 bits:
1720 // uint64_t x = value + addend;
1721 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1722 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1723 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1726 case elfcpp::R_X86_64_32S:
1727 // FIXME: we need to verify that value + addend fits into 32 bits:
1728 // int64_t x = value + addend; // note this quantity is signed!
1729 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1730 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1733 case elfcpp::R_X86_64_PC32:
1734 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1738 case elfcpp::R_X86_64_16:
1739 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1742 case elfcpp::R_X86_64_PC16:
1743 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1747 case elfcpp::R_X86_64_8:
1748 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1751 case elfcpp::R_X86_64_PC8:
1752 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1756 case elfcpp::R_X86_64_PLT32:
1757 gold_assert(gsym == NULL
1758 || gsym->has_plt_offset()
1759 || gsym->final_value_is_known()
1760 || (gsym->is_defined()
1761 && !gsym->is_from_dynobj()
1762 && !gsym->is_preemptible()));
1763 // Note: while this code looks the same as for R_X86_64_PC32, it
1764 // behaves differently because psymval was set to point to
1765 // the PLT entry, rather than the symbol, in Scan::global().
1766 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1770 case elfcpp::R_X86_64_PLTOFF64:
1773 gold_assert(gsym->has_plt_offset()
1774 || gsym->final_value_is_known());
1775 elfcpp::Elf_types<64>::Elf_Addr got_address;
1776 got_address = target->got_section(NULL, NULL)->address();
1777 Relocate_functions<64, false>::rela64(view, object, psymval,
1778 addend - got_address);
1781 case elfcpp::R_X86_64_GOT32:
1782 gold_assert(have_got_offset);
1783 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1786 case elfcpp::R_X86_64_GOTPC32:
1789 elfcpp::Elf_types<64>::Elf_Addr value;
1790 value = target->got_plt_section()->address();
1791 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1795 case elfcpp::R_X86_64_GOT64:
1796 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1797 // Since we always add a PLT entry, this is equivalent.
1798 case elfcpp::R_X86_64_GOTPLT64:
1799 gold_assert(have_got_offset);
1800 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1803 case elfcpp::R_X86_64_GOTPC64:
1806 elfcpp::Elf_types<64>::Elf_Addr value;
1807 value = target->got_plt_section()->address();
1808 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1812 case elfcpp::R_X86_64_GOTOFF64:
1814 elfcpp::Elf_types<64>::Elf_Addr value;
1815 value = (psymval->value(object, 0)
1816 - target->got_plt_section()->address());
1817 Relocate_functions<64, false>::rela64(view, value, addend);
1821 case elfcpp::R_X86_64_GOTPCREL:
1823 gold_assert(have_got_offset);
1824 elfcpp::Elf_types<64>::Elf_Addr value;
1825 value = target->got_plt_section()->address() + got_offset;
1826 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1830 case elfcpp::R_X86_64_GOTPCREL64:
1832 gold_assert(have_got_offset);
1833 elfcpp::Elf_types<64>::Elf_Addr value;
1834 value = target->got_plt_section()->address() + got_offset;
1835 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1839 case elfcpp::R_X86_64_COPY:
1840 case elfcpp::R_X86_64_GLOB_DAT:
1841 case elfcpp::R_X86_64_JUMP_SLOT:
1842 case elfcpp::R_X86_64_RELATIVE:
1843 // These are outstanding tls relocs, which are unexpected when linking
1844 case elfcpp::R_X86_64_TPOFF64:
1845 case elfcpp::R_X86_64_DTPMOD64:
1846 case elfcpp::R_X86_64_TLSDESC:
1847 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1848 _("unexpected reloc %u in object file"),
1852 // These are initial tls relocs, which are expected when linking
1853 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1854 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1855 case elfcpp::R_X86_64_TLSDESC_CALL:
1856 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1857 case elfcpp::R_X86_64_DTPOFF32:
1858 case elfcpp::R_X86_64_DTPOFF64:
1859 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1860 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1861 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1862 view, address, view_size);
1865 case elfcpp::R_X86_64_SIZE32:
1866 case elfcpp::R_X86_64_SIZE64:
1868 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1869 _("unsupported reloc %u"),
1877 // Perform a TLS relocation.
1880 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1881 Target_x86_64* target,
1883 const elfcpp::Rela<64, false>& rela,
1884 unsigned int r_type,
1885 const Sized_symbol<64>* gsym,
1886 const Symbol_value<64>* psymval,
1887 unsigned char* view,
1888 elfcpp::Elf_types<64>::Elf_Addr address,
1889 section_size_type view_size)
1891 Output_segment* tls_segment = relinfo->layout->tls_segment();
1893 const Sized_relobj<64, false>* object = relinfo->object;
1894 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1896 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1898 const bool is_final = (gsym == NULL
1899 ? !parameters->options().output_is_position_independent()
1900 : gsym->final_value_is_known());
1901 const tls::Tls_optimization optimized_type
1902 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1905 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1906 this->saw_tls_block_reloc_ = true;
1907 if (optimized_type == tls::TLSOPT_TO_LE)
1909 gold_assert(tls_segment != NULL);
1910 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1911 rela, r_type, value, view,
1917 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1918 ? GOT_TYPE_TLS_OFFSET
1919 : GOT_TYPE_TLS_PAIR);
1920 unsigned int got_offset;
1923 gold_assert(gsym->has_got_offset(got_type));
1924 got_offset = gsym->got_offset(got_type) - target->got_size();
1928 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1929 gold_assert(object->local_has_got_offset(r_sym, got_type));
1930 got_offset = (object->local_got_offset(r_sym, got_type)
1931 - target->got_size());
1933 if (optimized_type == tls::TLSOPT_TO_IE)
1935 gold_assert(tls_segment != NULL);
1936 value = target->got_plt_section()->address() + got_offset;
1937 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1938 value, view, address, view_size);
1941 else if (optimized_type == tls::TLSOPT_NONE)
1943 // Relocate the field with the offset of the pair of GOT
1945 value = target->got_plt_section()->address() + got_offset;
1946 Relocate_functions<64, false>::pcrela32(view, value, addend,
1951 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1952 _("unsupported reloc %u"), r_type);
1955 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1956 case elfcpp::R_X86_64_TLSDESC_CALL:
1957 this->saw_tls_block_reloc_ = true;
1958 if (optimized_type == tls::TLSOPT_TO_LE)
1960 gold_assert(tls_segment != NULL);
1961 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1962 rela, r_type, value, view,
1968 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1969 ? GOT_TYPE_TLS_OFFSET
1970 : GOT_TYPE_TLS_DESC);
1971 unsigned int got_offset;
1974 gold_assert(gsym->has_got_offset(got_type));
1975 got_offset = gsym->got_offset(got_type) - target->got_size();
1979 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1980 gold_assert(object->local_has_got_offset(r_sym, got_type));
1981 got_offset = (object->local_got_offset(r_sym, got_type)
1982 - target->got_size());
1984 if (optimized_type == tls::TLSOPT_TO_IE)
1986 gold_assert(tls_segment != NULL);
1987 value = target->got_plt_section()->address() + got_offset;
1988 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
1989 rela, r_type, value, view, address,
1993 else if (optimized_type == tls::TLSOPT_NONE)
1995 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
1997 // Relocate the field with the offset of the pair of GOT
1999 value = target->got_plt_section()->address() + got_offset;
2000 Relocate_functions<64, false>::pcrela32(view, value, addend,
2006 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2007 _("unsupported reloc %u"), r_type);
2010 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2011 this->saw_tls_block_reloc_ = true;
2012 if (optimized_type == tls::TLSOPT_TO_LE)
2014 gold_assert(tls_segment != NULL);
2015 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2016 value, view, view_size);
2019 else if (optimized_type == tls::TLSOPT_NONE)
2021 // Relocate the field with the offset of the GOT entry for
2022 // the module index.
2023 unsigned int got_offset;
2024 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2025 - target->got_size());
2026 value = target->got_plt_section()->address() + got_offset;
2027 Relocate_functions<64, false>::pcrela32(view, value, addend,
2031 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2032 _("unsupported reloc %u"), r_type);
2035 case elfcpp::R_X86_64_DTPOFF32:
2036 gold_assert(tls_segment != NULL);
2037 if (optimized_type == tls::TLSOPT_TO_LE)
2039 // This relocation type is used in debugging information.
2040 // In that case we need to not optimize the value. If we
2041 // haven't seen a TLSLD reloc, then we assume we should not
2042 // optimize this reloc.
2043 if (this->saw_tls_block_reloc_)
2044 value -= tls_segment->memsz();
2046 Relocate_functions<64, false>::rela32(view, value, 0);
2049 case elfcpp::R_X86_64_DTPOFF64:
2050 gold_assert(tls_segment != NULL);
2051 if (optimized_type == tls::TLSOPT_TO_LE)
2053 // See R_X86_64_DTPOFF32, just above, for why we test this.
2054 if (this->saw_tls_block_reloc_)
2055 value -= tls_segment->memsz();
2057 Relocate_functions<64, false>::rela64(view, value, 0);
2060 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2061 if (optimized_type == tls::TLSOPT_TO_LE)
2063 gold_assert(tls_segment != NULL);
2064 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2065 rela, r_type, value, view,
2069 else if (optimized_type == tls::TLSOPT_NONE)
2071 // Relocate the field with the offset of the GOT entry for
2072 // the tp-relative offset of the symbol.
2073 unsigned int got_offset;
2076 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2077 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2078 - target->got_size());
2082 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2083 gold_assert(object->local_has_got_offset(r_sym,
2084 GOT_TYPE_TLS_OFFSET));
2085 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2086 - target->got_size());
2088 value = target->got_plt_section()->address() + got_offset;
2089 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2092 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2093 _("unsupported reloc type %u"),
2097 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2098 value -= tls_segment->memsz();
2099 Relocate_functions<64, false>::rela32(view, value, 0);
2104 // Do a relocation in which we convert a TLS General-Dynamic to an
2108 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2111 const elfcpp::Rela<64, false>& rela,
2113 elfcpp::Elf_types<64>::Elf_Addr value,
2114 unsigned char* view,
2115 elfcpp::Elf_types<64>::Elf_Addr address,
2116 section_size_type view_size)
2118 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2119 // .word 0x6666; rex64; call __tls_get_addr
2120 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2122 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2123 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2125 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2126 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2127 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2128 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2130 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2132 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2133 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2135 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2137 this->skip_call_tls_get_addr_ = true;
2140 // Do a relocation in which we convert a TLS General-Dynamic to a
2144 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2146 Output_segment* tls_segment,
2147 const elfcpp::Rela<64, false>& rela,
2149 elfcpp::Elf_types<64>::Elf_Addr value,
2150 unsigned char* view,
2151 section_size_type view_size)
2153 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2154 // .word 0x6666; rex64; call __tls_get_addr
2155 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2157 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2158 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2160 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2161 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2162 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2163 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2165 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2167 value -= tls_segment->memsz();
2168 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2170 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2172 this->skip_call_tls_get_addr_ = true;
2175 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2178 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2179 const Relocate_info<64, false>* relinfo,
2182 const elfcpp::Rela<64, false>& rela,
2183 unsigned int r_type,
2184 elfcpp::Elf_types<64>::Elf_Addr value,
2185 unsigned char* view,
2186 elfcpp::Elf_types<64>::Elf_Addr address,
2187 section_size_type view_size)
2189 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2191 // leaq foo@tlsdesc(%rip), %rax
2192 // ==> movq foo@gottpoff(%rip), %rax
2193 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2194 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2195 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2196 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2198 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2199 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2203 // call *foo@tlscall(%rax)
2205 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2206 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2207 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2208 view[0] == 0xff && view[1] == 0x10);
2214 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2217 Target_x86_64::Relocate::tls_desc_gd_to_le(
2218 const Relocate_info<64, false>* relinfo,
2220 Output_segment* tls_segment,
2221 const elfcpp::Rela<64, false>& rela,
2222 unsigned int r_type,
2223 elfcpp::Elf_types<64>::Elf_Addr value,
2224 unsigned char* view,
2225 section_size_type view_size)
2227 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2229 // leaq foo@tlsdesc(%rip), %rax
2230 // ==> movq foo@tpoff, %rax
2231 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2232 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2233 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2234 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2237 value -= tls_segment->memsz();
2238 Relocate_functions<64, false>::rela32(view, value, 0);
2242 // call *foo@tlscall(%rax)
2244 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2245 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2246 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2247 view[0] == 0xff && view[1] == 0x10);
2254 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2257 const elfcpp::Rela<64, false>& rela,
2259 elfcpp::Elf_types<64>::Elf_Addr,
2260 unsigned char* view,
2261 section_size_type view_size)
2263 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2264 // ... leq foo@dtpoff(%rax),%reg
2265 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2267 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2268 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2270 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2271 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2273 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2275 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2277 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2279 this->skip_call_tls_get_addr_ = true;
2282 // Do a relocation in which we convert a TLS Initial-Exec to a
2286 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2288 Output_segment* tls_segment,
2289 const elfcpp::Rela<64, false>& rela,
2291 elfcpp::Elf_types<64>::Elf_Addr value,
2292 unsigned char* view,
2293 section_size_type view_size)
2295 // We need to examine the opcodes to figure out which instruction we
2298 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2299 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2301 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2302 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2304 unsigned char op1 = view[-3];
2305 unsigned char op2 = view[-2];
2306 unsigned char op3 = view[-1];
2307 unsigned char reg = op3 >> 3;
2315 view[-1] = 0xc0 | reg;
2319 // Special handling for %rsp.
2323 view[-1] = 0xc0 | reg;
2331 view[-1] = 0x80 | reg | (reg << 3);
2334 value -= tls_segment->memsz();
2335 Relocate_functions<64, false>::rela32(view, value, 0);
2338 // Relocate section data.
2341 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2342 unsigned int sh_type,
2343 const unsigned char* prelocs,
2345 Output_section* output_section,
2346 bool needs_special_offset_handling,
2347 unsigned char* view,
2348 elfcpp::Elf_types<64>::Elf_Addr address,
2349 section_size_type view_size)
2351 gold_assert(sh_type == elfcpp::SHT_RELA);
2353 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2354 Target_x86_64::Relocate>(
2360 needs_special_offset_handling,
2366 // Return the size of a relocation while scanning during a relocatable
2370 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2371 unsigned int r_type,
2376 case elfcpp::R_X86_64_NONE:
2377 case elfcpp::R_386_GNU_VTINHERIT:
2378 case elfcpp::R_386_GNU_VTENTRY:
2379 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2380 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2381 case elfcpp::R_X86_64_TLSDESC_CALL:
2382 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2383 case elfcpp::R_X86_64_DTPOFF32:
2384 case elfcpp::R_X86_64_DTPOFF64:
2385 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2386 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2389 case elfcpp::R_X86_64_64:
2390 case elfcpp::R_X86_64_PC64:
2391 case elfcpp::R_X86_64_GOTOFF64:
2392 case elfcpp::R_X86_64_GOTPC64:
2393 case elfcpp::R_X86_64_PLTOFF64:
2394 case elfcpp::R_X86_64_GOT64:
2395 case elfcpp::R_X86_64_GOTPCREL64:
2396 case elfcpp::R_X86_64_GOTPCREL:
2397 case elfcpp::R_X86_64_GOTPLT64:
2400 case elfcpp::R_X86_64_32:
2401 case elfcpp::R_X86_64_32S:
2402 case elfcpp::R_X86_64_PC32:
2403 case elfcpp::R_X86_64_PLT32:
2404 case elfcpp::R_X86_64_GOTPC32:
2405 case elfcpp::R_X86_64_GOT32:
2408 case elfcpp::R_X86_64_16:
2409 case elfcpp::R_X86_64_PC16:
2412 case elfcpp::R_X86_64_8:
2413 case elfcpp::R_X86_64_PC8:
2416 case elfcpp::R_X86_64_COPY:
2417 case elfcpp::R_X86_64_GLOB_DAT:
2418 case elfcpp::R_X86_64_JUMP_SLOT:
2419 case elfcpp::R_X86_64_RELATIVE:
2420 // These are outstanding tls relocs, which are unexpected when linking
2421 case elfcpp::R_X86_64_TPOFF64:
2422 case elfcpp::R_X86_64_DTPMOD64:
2423 case elfcpp::R_X86_64_TLSDESC:
2424 object->error(_("unexpected reloc %u in object file"), r_type);
2427 case elfcpp::R_X86_64_SIZE32:
2428 case elfcpp::R_X86_64_SIZE64:
2430 object->error(_("unsupported reloc %u against local symbol"), r_type);
2435 // Scan the relocs during a relocatable link.
2438 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2439 Symbol_table* symtab,
2441 Sized_relobj<64, false>* object,
2442 unsigned int data_shndx,
2443 unsigned int sh_type,
2444 const unsigned char* prelocs,
2446 Output_section* output_section,
2447 bool needs_special_offset_handling,
2448 size_t local_symbol_count,
2449 const unsigned char* plocal_symbols,
2450 Relocatable_relocs* rr)
2452 gold_assert(sh_type == elfcpp::SHT_RELA);
2454 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2455 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2457 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2458 Scan_relocatable_relocs>(
2467 needs_special_offset_handling,
2473 // Relocate a section during a relocatable link.
2476 Target_x86_64::relocate_for_relocatable(
2477 const Relocate_info<64, false>* relinfo,
2478 unsigned int sh_type,
2479 const unsigned char* prelocs,
2481 Output_section* output_section,
2482 off_t offset_in_output_section,
2483 const Relocatable_relocs* rr,
2484 unsigned char* view,
2485 elfcpp::Elf_types<64>::Elf_Addr view_address,
2486 section_size_type view_size,
2487 unsigned char* reloc_view,
2488 section_size_type reloc_view_size)
2490 gold_assert(sh_type == elfcpp::SHT_RELA);
2492 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2497 offset_in_output_section,
2506 // Return the value to use for a dynamic which requires special
2507 // treatment. This is how we support equality comparisons of function
2508 // pointers across shared library boundaries, as described in the
2509 // processor specific ABI supplement.
2512 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2514 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2515 return this->plt_section()->address() + gsym->plt_offset();
2518 // Return a string used to fill a code section with nops to take up
2519 // the specified length.
2522 Target_x86_64::do_code_fill(section_size_type length) const
2526 // Build a jmpq instruction to skip over the bytes.
2527 unsigned char jmp[5];
2529 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2530 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2531 + std::string(length - 5, '\0'));
2534 // Nop sequences of various lengths.
2535 const char nop1[1] = { 0x90 }; // nop
2536 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2537 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2538 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2539 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2540 0x00 }; // leal 0(%esi,1),%esi
2541 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2543 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2545 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2546 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2547 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2548 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2550 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2551 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2553 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2554 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2556 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2557 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2558 0x00, 0x00, 0x00, 0x00 };
2559 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2560 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2561 0x27, 0x00, 0x00, 0x00,
2563 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2564 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2565 0xbc, 0x27, 0x00, 0x00,
2567 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2568 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2569 0x90, 0x90, 0x90, 0x90,
2572 const char* nops[16] = {
2574 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2575 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2578 return std::string(nops[length], length);
2581 // The selector for x86_64 object files.
2583 class Target_selector_x86_64 : public Target_selector
2586 Target_selector_x86_64()
2587 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2591 do_instantiate_target()
2592 { return new Target_x86_64(); }
2595 Target_selector_x86_64 target_selector_x86_64;
2597 } // End anonymous namespace.