1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
51 // A class to handle the PLT data.
54 class Output_data_plt_x86_64 : public Output_section_data
57 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
59 Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got,
60 Output_data_space* got_plt,
61 Output_data_space* got_irelative)
62 : Output_section_data(16), layout_(layout), tlsdesc_rel_(NULL),
63 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
64 got_irelative_(got_irelative), count_(0), irelative_count_(0),
65 tlsdesc_got_offset_(-1U), free_list_()
66 { this->init(layout); }
68 Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got,
69 Output_data_space* got_plt,
70 Output_data_space* got_irelative,
71 unsigned int plt_count)
72 : Output_section_data((plt_count + 1) * plt_entry_size, 16, false),
73 layout_(layout), tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
74 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
75 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
79 // Initialize the free list and reserve the first entry.
80 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
81 this->free_list_.remove(0, plt_entry_size);
84 // Initialize the PLT section.
88 // Add an entry to the PLT.
90 add_entry(Symbol_table*, Layout*, Symbol* gsym);
92 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
94 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
95 Sized_relobj_file<size, false>* relobj,
96 unsigned int local_sym_index);
98 // Add the relocation for a PLT entry.
100 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
101 unsigned int got_offset);
103 // Add the reserved TLSDESC_PLT entry to the PLT.
105 reserve_tlsdesc_entry(unsigned int got_offset)
106 { this->tlsdesc_got_offset_ = got_offset; }
108 // Return true if a TLSDESC_PLT entry has been reserved.
110 has_tlsdesc_entry() const
111 { return this->tlsdesc_got_offset_ != -1U; }
113 // Return the GOT offset for the reserved TLSDESC_PLT entry.
115 get_tlsdesc_got_offset() const
116 { return this->tlsdesc_got_offset_; }
118 // Return the offset of the reserved TLSDESC_PLT entry.
120 get_tlsdesc_plt_offset() const
121 { return (this->count_ + this->irelative_count_ + 1) * plt_entry_size; }
123 // Return the .rela.plt section data.
126 { return this->rel_; }
128 // Return where the TLSDESC relocations should go.
130 rela_tlsdesc(Layout*);
132 // Return where the IRELATIVE relocations should go in the PLT
135 rela_irelative(Symbol_table*, Layout*);
137 // Return whether we created a section for IRELATIVE relocations.
139 has_irelative_section() const
140 { return this->irelative_rel_ != NULL; }
142 // Return the number of PLT entries.
145 { return this->count_ + this->irelative_count_; }
147 // Return the offset of the first non-reserved PLT entry.
149 first_plt_entry_offset()
150 { return plt_entry_size; }
152 // Return the size of a PLT entry.
155 { return plt_entry_size; }
157 // Reserve a slot in the PLT for an existing symbol in an incremental update.
159 reserve_slot(unsigned int plt_index)
161 this->free_list_.remove((plt_index + 1) * plt_entry_size,
162 (plt_index + 2) * plt_entry_size);
165 // Return the PLT address to use for a global symbol.
167 address_for_global(const Symbol*);
169 // Return the PLT address to use for a local symbol.
171 address_for_local(const Relobj*, unsigned int symndx);
175 do_adjust_output_section(Output_section* os);
177 // Write to a map file.
179 do_print_to_mapfile(Mapfile* mapfile) const
180 { mapfile->print_output_data(this, _("** PLT")); }
183 // The size of an entry in the PLT.
184 static const int plt_entry_size = 16;
186 // The first entry in the PLT.
187 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
188 // procedure linkage table for both programs and shared objects."
189 static const unsigned char first_plt_entry[plt_entry_size];
191 // Other entries in the PLT for an executable.
192 static const unsigned char plt_entry[plt_entry_size];
194 // The reserved TLSDESC entry in the PLT for an executable.
195 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
197 // The .eh_frame unwind information for the PLT.
198 static const int plt_eh_frame_cie_size = 16;
199 static const int plt_eh_frame_fde_size = 32;
200 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
201 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
203 // Set the final size.
205 set_final_data_size();
207 // Write out the PLT data.
209 do_write(Output_file*);
211 // A pointer to the Layout class, so that we can find the .dynamic
212 // section when we write out the GOT PLT section.
214 // The reloc section.
216 // The TLSDESC relocs, if necessary. These must follow the regular
218 Reloc_section* tlsdesc_rel_;
219 // The IRELATIVE relocs, if necessary. These must follow the
220 // regular PLT relocations and the TLSDESC relocations.
221 Reloc_section* irelative_rel_;
223 Output_data_got<64, false>* got_;
224 // The .got.plt section.
225 Output_data_space* got_plt_;
226 // The part of the .got.plt section used for IRELATIVE relocs.
227 Output_data_space* got_irelative_;
228 // The number of PLT entries.
230 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
231 // follow the regular PLT entries.
232 unsigned int irelative_count_;
233 // Offset of the reserved TLSDESC_GOT entry when needed.
234 unsigned int tlsdesc_got_offset_;
235 // List of available regions within the section, for incremental
237 Free_list free_list_;
240 // The x86_64 target class.
242 // http://www.x86-64.org/documentation/abi.pdf
243 // TLS info comes from
244 // http://people.redhat.com/drepper/tls.pdf
245 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
248 class Target_x86_64 : public Sized_target<size, false>
251 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
252 // uses only Elf64_Rela relocation entries with explicit addends."
253 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
256 : Sized_target<size, false>(&x86_64_info),
257 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
258 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
259 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
260 dynbss_(NULL), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
261 tls_base_symbol_defined_(false)
264 // Hook for a new output section.
266 do_new_output_section(Output_section*) const;
268 // Scan the relocations to look for symbol adjustments.
270 gc_process_relocs(Symbol_table* symtab,
272 Sized_relobj_file<size, false>* object,
273 unsigned int data_shndx,
274 unsigned int sh_type,
275 const unsigned char* prelocs,
277 Output_section* output_section,
278 bool needs_special_offset_handling,
279 size_t local_symbol_count,
280 const unsigned char* plocal_symbols);
282 // Scan the relocations to look for symbol adjustments.
284 scan_relocs(Symbol_table* symtab,
286 Sized_relobj_file<size, false>* object,
287 unsigned int data_shndx,
288 unsigned int sh_type,
289 const unsigned char* prelocs,
291 Output_section* output_section,
292 bool needs_special_offset_handling,
293 size_t local_symbol_count,
294 const unsigned char* plocal_symbols);
296 // Finalize the sections.
298 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
300 // Return the value to use for a dynamic which requires special
303 do_dynsym_value(const Symbol*) const;
305 // Relocate a section.
307 relocate_section(const Relocate_info<size, false>*,
308 unsigned int sh_type,
309 const unsigned char* prelocs,
311 Output_section* output_section,
312 bool needs_special_offset_handling,
314 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
315 section_size_type view_size,
316 const Reloc_symbol_changes*);
318 // Scan the relocs during a relocatable link.
320 scan_relocatable_relocs(Symbol_table* symtab,
322 Sized_relobj_file<size, false>* object,
323 unsigned int data_shndx,
324 unsigned int sh_type,
325 const unsigned char* prelocs,
327 Output_section* output_section,
328 bool needs_special_offset_handling,
329 size_t local_symbol_count,
330 const unsigned char* plocal_symbols,
331 Relocatable_relocs*);
333 // Relocate a section during a relocatable link.
335 relocate_for_relocatable(
336 const Relocate_info<size, false>*,
337 unsigned int sh_type,
338 const unsigned char* prelocs,
340 Output_section* output_section,
341 off_t offset_in_output_section,
342 const Relocatable_relocs*,
344 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
345 section_size_type view_size,
346 unsigned char* reloc_view,
347 section_size_type reloc_view_size);
349 // Return a string used to fill a code section with nops.
351 do_code_fill(section_size_type length) const;
353 // Return whether SYM is defined by the ABI.
355 do_is_defined_by_abi(const Symbol* sym) const
356 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
358 // Return the symbol index to use for a target specific relocation.
359 // The only target specific relocation is R_X86_64_TLSDESC for a
360 // local symbol, which is an absolute reloc.
362 do_reloc_symbol_index(void*, unsigned int r_type) const
364 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
368 // Return the addend to use for a target specific relocation.
370 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
372 // Return the PLT section.
374 do_plt_address_for_global(const Symbol* gsym) const
375 { return this->plt_section()->address_for_global(gsym); }
378 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
379 { return this->plt_section()->address_for_local(relobj, symndx); }
381 // This function should be defined in targets that can use relocation
382 // types to determine (implemented in local_reloc_may_be_function_pointer
383 // and global_reloc_may_be_function_pointer)
384 // if a function's pointer is taken. ICF uses this in safe mode to only
385 // fold those functions whose pointer is defintely not taken. For x86_64
386 // pie binaries, safe ICF cannot be done by looking at relocation types.
388 do_can_check_for_function_pointers() const
389 { return !parameters->options().pie(); }
391 // Return the base for a DW_EH_PE_datarel encoding.
393 do_ehframe_datarel_base() const;
395 // Adjust -fsplit-stack code which calls non-split-stack code.
397 do_calls_non_split(Relobj* object, unsigned int shndx,
398 section_offset_type fnoffset, section_size_type fnsize,
399 unsigned char* view, section_size_type view_size,
400 std::string* from, std::string* to) const;
402 // Return the size of the GOT section.
406 gold_assert(this->got_ != NULL);
407 return this->got_->data_size();
410 // Return the number of entries in the GOT.
412 got_entry_count() const
414 if (this->got_ == NULL)
416 return this->got_size() / 8;
419 // Return the number of entries in the PLT.
421 plt_entry_count() const;
423 // Return the offset of the first non-reserved PLT entry.
425 first_plt_entry_offset() const;
427 // Return the size of each PLT entry.
429 plt_entry_size() const;
431 // Create the GOT section for an incremental update.
432 Output_data_got_base*
433 init_got_plt_for_update(Symbol_table* symtab,
435 unsigned int got_count,
436 unsigned int plt_count);
438 // Reserve a GOT entry for a local symbol, and regenerate any
439 // necessary dynamic relocations.
441 reserve_local_got_entry(unsigned int got_index,
442 Sized_relobj<size, false>* obj,
444 unsigned int got_type);
446 // Reserve a GOT entry for a global symbol, and regenerate any
447 // necessary dynamic relocations.
449 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
450 unsigned int got_type);
452 // Register an existing PLT entry for a global symbol.
454 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
457 // Force a COPY relocation for a given symbol.
459 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
461 // Apply an incremental relocation.
463 apply_relocation(const Relocate_info<size, false>* relinfo,
464 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
466 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
469 typename elfcpp::Elf_types<size>::Elf_Addr address,
470 section_size_type view_size);
472 // Add a new reloc argument, returning the index in the vector.
474 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
476 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
477 return this->tlsdesc_reloc_info_.size() - 1;
481 // The class which scans relocations.
486 : issued_non_pic_error_(false)
490 get_reference_flags(unsigned int r_type);
493 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
494 Sized_relobj_file<size, false>* object,
495 unsigned int data_shndx,
496 Output_section* output_section,
497 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
498 const elfcpp::Sym<size, false>& lsym);
501 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
502 Sized_relobj_file<size, false>* object,
503 unsigned int data_shndx,
504 Output_section* output_section,
505 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
509 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
510 Target_x86_64* target,
511 Sized_relobj_file<size, false>* object,
512 unsigned int data_shndx,
513 Output_section* output_section,
514 const elfcpp::Rela<size, false>& reloc,
516 const elfcpp::Sym<size, false>& lsym);
519 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
520 Target_x86_64* target,
521 Sized_relobj_file<size, false>* object,
522 unsigned int data_shndx,
523 Output_section* output_section,
524 const elfcpp::Rela<size, false>& reloc,
530 unsupported_reloc_local(Sized_relobj_file<size, false>*,
531 unsigned int r_type);
534 unsupported_reloc_global(Sized_relobj_file<size, false>*,
535 unsigned int r_type, Symbol*);
538 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
541 possible_function_pointer_reloc(unsigned int r_type);
544 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
545 unsigned int r_type);
547 // Whether we have issued an error about a non-PIC compilation.
548 bool issued_non_pic_error_;
551 // The class which implements relocation.
556 : skip_call_tls_get_addr_(false)
561 if (this->skip_call_tls_get_addr_)
563 // FIXME: This needs to specify the location somehow.
564 gold_error(_("missing expected TLS relocation"));
568 // Do a relocation. Return false if the caller should not issue
569 // any warnings about this relocation.
571 relocate(const Relocate_info<size, false>*, Target_x86_64*,
573 size_t relnum, const elfcpp::Rela<size, false>&,
574 unsigned int r_type, const Sized_symbol<size>*,
575 const Symbol_value<size>*,
576 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
580 // Do a TLS relocation.
582 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
583 size_t relnum, const elfcpp::Rela<size, false>&,
584 unsigned int r_type, const Sized_symbol<size>*,
585 const Symbol_value<size>*,
586 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
589 // Do a TLS General-Dynamic to Initial-Exec transition.
591 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
592 Output_segment* tls_segment,
593 const elfcpp::Rela<size, false>&, unsigned int r_type,
594 typename elfcpp::Elf_types<size>::Elf_Addr value,
596 typename elfcpp::Elf_types<size>::Elf_Addr,
597 section_size_type view_size);
599 // Do a TLS General-Dynamic to Local-Exec transition.
601 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
602 Output_segment* tls_segment,
603 const elfcpp::Rela<size, false>&, unsigned int r_type,
604 typename elfcpp::Elf_types<size>::Elf_Addr value,
606 section_size_type view_size);
608 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
610 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
611 Output_segment* tls_segment,
612 const elfcpp::Rela<size, false>&, unsigned int r_type,
613 typename elfcpp::Elf_types<size>::Elf_Addr value,
615 typename elfcpp::Elf_types<size>::Elf_Addr,
616 section_size_type view_size);
618 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
620 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
621 Output_segment* tls_segment,
622 const elfcpp::Rela<size, false>&, unsigned int r_type,
623 typename elfcpp::Elf_types<size>::Elf_Addr value,
625 section_size_type view_size);
627 // Do a TLS Local-Dynamic to Local-Exec transition.
629 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
630 Output_segment* tls_segment,
631 const elfcpp::Rela<size, false>&, unsigned int r_type,
632 typename elfcpp::Elf_types<size>::Elf_Addr value,
634 section_size_type view_size);
636 // Do a TLS Initial-Exec to Local-Exec transition.
638 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
639 Output_segment* tls_segment,
640 const elfcpp::Rela<size, false>&, unsigned int r_type,
641 typename elfcpp::Elf_types<size>::Elf_Addr value,
643 section_size_type view_size);
645 // This is set if we should skip the next reloc, which should be a
646 // PLT32 reloc against ___tls_get_addr.
647 bool skip_call_tls_get_addr_;
650 // A class which returns the size required for a relocation type,
651 // used while scanning relocs during a relocatable link.
652 class Relocatable_size_for_reloc
656 get_size_for_reloc(unsigned int, Relobj*);
659 // Adjust TLS relocation type based on the options and whether this
660 // is a local symbol.
661 static tls::Tls_optimization
662 optimize_tls_reloc(bool is_final, int r_type);
664 // Get the GOT section, creating it if necessary.
665 Output_data_got<64, false>*
666 got_section(Symbol_table*, Layout*);
668 // Get the GOT PLT section.
670 got_plt_section() const
672 gold_assert(this->got_plt_ != NULL);
673 return this->got_plt_;
676 // Get the GOT section for TLSDESC entries.
677 Output_data_got<64, false>*
678 got_tlsdesc_section() const
680 gold_assert(this->got_tlsdesc_ != NULL);
681 return this->got_tlsdesc_;
684 // Create the PLT section.
686 make_plt_section(Symbol_table* symtab, Layout* layout);
688 // Create a PLT entry for a global symbol.
690 make_plt_entry(Symbol_table*, Layout*, Symbol*);
692 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
694 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
695 Sized_relobj_file<size, false>* relobj,
696 unsigned int local_sym_index);
698 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
700 define_tls_base_symbol(Symbol_table*, Layout*);
702 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
704 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
706 // Create a GOT entry for the TLS module index.
708 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
709 Sized_relobj_file<size, false>* object);
711 // Get the PLT section.
712 Output_data_plt_x86_64<size>*
715 gold_assert(this->plt_ != NULL);
719 // Get the dynamic reloc section, creating it if necessary.
721 rela_dyn_section(Layout*);
723 // Get the section to use for TLSDESC relocations.
725 rela_tlsdesc_section(Layout*) const;
727 // Get the section to use for IRELATIVE relocations.
729 rela_irelative_section(Layout*);
731 // Add a potential copy relocation.
733 copy_reloc(Symbol_table* symtab, Layout* layout,
734 Sized_relobj_file<size, false>* object,
735 unsigned int shndx, Output_section* output_section,
736 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
738 this->copy_relocs_.copy_reloc(symtab, layout,
739 symtab->get_sized_symbol<size>(sym),
740 object, shndx, output_section,
741 reloc, this->rela_dyn_section(layout));
744 // Information about this specific target which we pass to the
745 // general Target structure.
746 static const Target::Target_info x86_64_info;
748 // The types of GOT entries needed for this platform.
749 // These values are exposed to the ABI in an incremental link.
750 // Do not renumber existing values without changing the version
751 // number of the .gnu_incremental_inputs section.
754 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
755 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
756 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
757 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
760 // This type is used as the argument to the target specific
761 // relocation routines. The only target specific reloc is
762 // R_X86_64_TLSDESC against a local symbol.
765 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
766 : object(a_object), r_sym(a_r_sym)
769 // The object in which the local symbol is defined.
770 Sized_relobj_file<size, false>* object;
771 // The local symbol index in the object.
776 Output_data_got<64, false>* got_;
778 Output_data_plt_x86_64<size>* plt_;
779 // The GOT PLT section.
780 Output_data_space* got_plt_;
781 // The GOT section for IRELATIVE relocations.
782 Output_data_space* got_irelative_;
783 // The GOT section for TLSDESC relocations.
784 Output_data_got<64, false>* got_tlsdesc_;
785 // The _GLOBAL_OFFSET_TABLE_ symbol.
786 Symbol* global_offset_table_;
787 // The dynamic reloc section.
788 Reloc_section* rela_dyn_;
789 // The section to use for IRELATIVE relocs.
790 Reloc_section* rela_irelative_;
791 // Relocs saved to avoid a COPY reloc.
792 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
793 // Space for variables copied with a COPY reloc.
794 Output_data_space* dynbss_;
795 // Offset of the GOT entry for the TLS module index.
796 unsigned int got_mod_index_offset_;
797 // We handle R_X86_64_TLSDESC against a local symbol as a target
798 // specific relocation. Here we store the object and local symbol
799 // index for the relocation.
800 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
801 // True if the _TLS_MODULE_BASE_ symbol has been defined.
802 bool tls_base_symbol_defined_;
806 const Target::Target_info Target_x86_64<64>::x86_64_info =
809 false, // is_big_endian
810 elfcpp::EM_X86_64, // machine_code
811 false, // has_make_symbol
812 false, // has_resolve
813 true, // has_code_fill
814 true, // is_default_stack_executable
815 true, // can_icf_inline_merge_sections
817 "/lib/ld64.so.1", // program interpreter
818 0x400000, // default_text_segment_address
819 0x1000, // abi_pagesize (overridable by -z max-page-size)
820 0x1000, // common_pagesize (overridable by -z common-page-size)
821 elfcpp::SHN_UNDEF, // small_common_shndx
822 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
823 0, // small_common_section_flags
824 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
825 NULL, // attributes_section
826 NULL // attributes_vendor
830 const Target::Target_info Target_x86_64<32>::x86_64_info =
833 false, // is_big_endian
834 elfcpp::EM_X86_64, // machine_code
835 false, // has_make_symbol
836 false, // has_resolve
837 true, // has_code_fill
838 true, // is_default_stack_executable
839 true, // can_icf_inline_merge_sections
841 "/libx32/ldx32.so.1", // program interpreter
842 0x400000, // default_text_segment_address
843 0x1000, // abi_pagesize (overridable by -z max-page-size)
844 0x1000, // common_pagesize (overridable by -z common-page-size)
845 elfcpp::SHN_UNDEF, // small_common_shndx
846 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
847 0, // small_common_section_flags
848 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
849 NULL, // attributes_section
850 NULL // attributes_vendor
853 // This is called when a new output section is created. This is where
854 // we handle the SHF_X86_64_LARGE.
858 Target_x86_64<size>::do_new_output_section(Output_section* os) const
860 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
861 os->set_is_large_section();
864 // Get the GOT section, creating it if necessary.
867 Output_data_got<64, false>*
868 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
870 if (this->got_ == NULL)
872 gold_assert(symtab != NULL && layout != NULL);
874 // When using -z now, we can treat .got.plt as a relro section.
875 // Without -z now, it is modified after program startup by lazy
877 bool is_got_plt_relro = parameters->options().now();
878 Output_section_order got_order = (is_got_plt_relro
881 Output_section_order got_plt_order = (is_got_plt_relro
883 : ORDER_NON_RELRO_FIRST);
885 this->got_ = new Output_data_got<64, false>();
887 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
889 | elfcpp::SHF_WRITE),
890 this->got_, got_order, true);
892 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
893 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
895 | elfcpp::SHF_WRITE),
896 this->got_plt_, got_plt_order,
899 // The first three entries are reserved.
900 this->got_plt_->set_current_data_size(3 * 8);
902 if (!is_got_plt_relro)
904 // Those bytes can go into the relro segment.
905 layout->increase_relro(3 * 8);
908 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
909 this->global_offset_table_ =
910 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
911 Symbol_table::PREDEFINED,
913 0, 0, elfcpp::STT_OBJECT,
915 elfcpp::STV_HIDDEN, 0,
918 // If there are any IRELATIVE relocations, they get GOT entries
919 // in .got.plt after the jump slot entries.
920 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
921 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
923 | elfcpp::SHF_WRITE),
924 this->got_irelative_,
925 got_plt_order, is_got_plt_relro);
927 // If there are any TLSDESC relocations, they get GOT entries in
928 // .got.plt after the jump slot and IRELATIVE entries.
929 this->got_tlsdesc_ = new Output_data_got<64, false>();
930 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
932 | elfcpp::SHF_WRITE),
934 got_plt_order, is_got_plt_relro);
940 // Get the dynamic reloc section, creating it if necessary.
943 typename Target_x86_64<size>::Reloc_section*
944 Target_x86_64<size>::rela_dyn_section(Layout* layout)
946 if (this->rela_dyn_ == NULL)
948 gold_assert(layout != NULL);
949 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
950 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
951 elfcpp::SHF_ALLOC, this->rela_dyn_,
952 ORDER_DYNAMIC_RELOCS, false);
954 return this->rela_dyn_;
957 // Get the section to use for IRELATIVE relocs, creating it if
958 // necessary. These go in .rela.dyn, but only after all other dynamic
959 // relocations. They need to follow the other dynamic relocations so
960 // that they can refer to global variables initialized by those
964 typename Target_x86_64<size>::Reloc_section*
965 Target_x86_64<size>::rela_irelative_section(Layout* layout)
967 if (this->rela_irelative_ == NULL)
969 // Make sure we have already created the dynamic reloc section.
970 this->rela_dyn_section(layout);
971 this->rela_irelative_ = new Reloc_section(false);
972 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
973 elfcpp::SHF_ALLOC, this->rela_irelative_,
974 ORDER_DYNAMIC_RELOCS, false);
975 gold_assert(this->rela_dyn_->output_section()
976 == this->rela_irelative_->output_section());
978 return this->rela_irelative_;
981 // Initialize the PLT section.
985 Output_data_plt_x86_64<size>::init(Layout* layout)
987 this->rel_ = new Reloc_section(false);
988 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
989 elfcpp::SHF_ALLOC, this->rel_,
990 ORDER_DYNAMIC_PLT_RELOCS, false);
992 // Add unwind information if requested.
993 if (parameters->options().ld_generated_unwind_info())
994 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
995 plt_eh_frame_fde, plt_eh_frame_fde_size);
1000 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1002 os->set_entsize(plt_entry_size);
1005 // Add an entry to the PLT.
1009 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1012 gold_assert(!gsym->has_plt_offset());
1014 unsigned int plt_index;
1016 section_offset_type got_offset;
1018 unsigned int* pcount;
1019 unsigned int offset;
1020 unsigned int reserved;
1021 Output_data_space* got;
1022 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1023 && gsym->can_use_relative_reloc(false))
1025 pcount = &this->irelative_count_;
1028 got = this->got_irelative_;
1032 pcount = &this->count_;
1035 got = this->got_plt_;
1038 if (!this->is_data_size_valid())
1040 // Note that when setting the PLT offset for a non-IRELATIVE
1041 // entry we skip the initial reserved PLT entry.
1042 plt_index = *pcount + offset;
1043 plt_offset = plt_index * plt_entry_size;
1047 got_offset = (plt_index - offset + reserved) * 8;
1048 gold_assert(got_offset == got->current_data_size());
1050 // Every PLT entry needs a GOT entry which points back to the PLT
1051 // entry (this will be changed by the dynamic linker, normally
1052 // lazily when the function is called).
1053 got->set_current_data_size(got_offset + 8);
1057 // FIXME: This is probably not correct for IRELATIVE relocs.
1059 // For incremental updates, find an available slot.
1060 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
1061 if (plt_offset == -1)
1062 gold_fallback(_("out of patch space (PLT);"
1063 " relink with --incremental-full"));
1065 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1066 // can be calculated from the PLT index, adjusting for the three
1067 // reserved entries at the beginning of the GOT.
1068 plt_index = plt_offset / plt_entry_size - 1;
1069 got_offset = (plt_index - offset + reserved) * 8;
1072 gsym->set_plt_offset(plt_offset);
1074 // Every PLT entry needs a reloc.
1075 this->add_relocation(symtab, layout, gsym, got_offset);
1077 // Note that we don't need to save the symbol. The contents of the
1078 // PLT are independent of which symbols are used. The symbols only
1079 // appear in the relocations.
1082 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1087 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1088 Symbol_table* symtab,
1090 Sized_relobj_file<size, false>* relobj,
1091 unsigned int local_sym_index)
1093 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
1094 ++this->irelative_count_;
1096 section_offset_type got_offset = this->got_irelative_->current_data_size();
1098 // Every PLT entry needs a GOT entry which points back to the PLT
1100 this->got_irelative_->set_current_data_size(got_offset + 8);
1102 // Every PLT entry needs a reloc.
1103 Reloc_section* rela = this->rela_irelative(symtab, layout);
1104 rela->add_symbolless_local_addend(relobj, local_sym_index,
1105 elfcpp::R_X86_64_IRELATIVE,
1106 this->got_irelative_, got_offset, 0);
1111 // Add the relocation for a PLT entry.
1115 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1118 unsigned int got_offset)
1120 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1121 && gsym->can_use_relative_reloc(false))
1123 Reloc_section* rela = this->rela_irelative(symtab, layout);
1124 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1125 this->got_irelative_, got_offset, 0);
1129 gsym->set_needs_dynsym_entry();
1130 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1135 // Return where the TLSDESC relocations should go, creating it if
1136 // necessary. These follow the JUMP_SLOT relocations.
1139 typename Output_data_plt_x86_64<size>::Reloc_section*
1140 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1142 if (this->tlsdesc_rel_ == NULL)
1144 this->tlsdesc_rel_ = new Reloc_section(false);
1145 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1146 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1147 ORDER_DYNAMIC_PLT_RELOCS, false);
1148 gold_assert(this->tlsdesc_rel_->output_section()
1149 == this->rel_->output_section());
1151 return this->tlsdesc_rel_;
1154 // Return where the IRELATIVE relocations should go in the PLT. These
1155 // follow the JUMP_SLOT and the TLSDESC relocations.
1158 typename Output_data_plt_x86_64<size>::Reloc_section*
1159 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1162 if (this->irelative_rel_ == NULL)
1164 // Make sure we have a place for the TLSDESC relocations, in
1165 // case we see any later on.
1166 this->rela_tlsdesc(layout);
1167 this->irelative_rel_ = new Reloc_section(false);
1168 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1169 elfcpp::SHF_ALLOC, this->irelative_rel_,
1170 ORDER_DYNAMIC_PLT_RELOCS, false);
1171 gold_assert(this->irelative_rel_->output_section()
1172 == this->rel_->output_section());
1174 if (parameters->doing_static_link())
1176 // A statically linked executable will only have a .rela.plt
1177 // section to hold R_X86_64_IRELATIVE relocs for
1178 // STT_GNU_IFUNC symbols. The library will use these
1179 // symbols to locate the IRELATIVE relocs at program startup
1181 symtab->define_in_output_data("__rela_iplt_start", NULL,
1182 Symbol_table::PREDEFINED,
1183 this->irelative_rel_, 0, 0,
1184 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1185 elfcpp::STV_HIDDEN, 0, false, true);
1186 symtab->define_in_output_data("__rela_iplt_end", NULL,
1187 Symbol_table::PREDEFINED,
1188 this->irelative_rel_, 0, 0,
1189 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1190 elfcpp::STV_HIDDEN, 0, true, true);
1193 return this->irelative_rel_;
1196 // Return the PLT address to use for a global symbol.
1200 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1202 uint64_t offset = 0;
1203 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1204 && gsym->can_use_relative_reloc(false))
1205 offset = (this->count_ + 1) * plt_entry_size;
1206 return this->address() + offset;
1209 // Return the PLT address to use for a local symbol. These are always
1210 // IRELATIVE relocs.
1214 Output_data_plt_x86_64<size>::address_for_local(const Relobj*, unsigned int)
1216 return this->address() + (this->count_ + 1) * plt_entry_size;
1219 // Set the final size.
1222 Output_data_plt_x86_64<size>::set_final_data_size()
1224 unsigned int count = this->count_ + this->irelative_count_;
1225 if (this->has_tlsdesc_entry())
1227 this->set_data_size((count + 1) * plt_entry_size);
1230 // The first entry in the PLT for an executable.
1234 Output_data_plt_x86_64<size>::first_plt_entry[plt_entry_size] =
1236 // From AMD64 ABI Draft 0.98, page 76
1237 0xff, 0x35, // pushq contents of memory address
1238 0, 0, 0, 0, // replaced with address of .got + 8
1239 0xff, 0x25, // jmp indirect
1240 0, 0, 0, 0, // replaced with address of .got + 16
1241 0x90, 0x90, 0x90, 0x90 // noop (x4)
1244 // Subsequent entries in the PLT for an executable.
1248 Output_data_plt_x86_64<size>::plt_entry[plt_entry_size] =
1250 // From AMD64 ABI Draft 0.98, page 76
1251 0xff, 0x25, // jmpq indirect
1252 0, 0, 0, 0, // replaced with address of symbol in .got
1253 0x68, // pushq immediate
1254 0, 0, 0, 0, // replaced with offset into relocation table
1255 0xe9, // jmpq relative
1256 0, 0, 0, 0 // replaced with offset to start of .plt
1259 // The reserved TLSDESC entry in the PLT for an executable.
1263 Output_data_plt_x86_64<size>::tlsdesc_plt_entry[plt_entry_size] =
1265 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1266 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1267 0xff, 0x35, // pushq x(%rip)
1268 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1269 0xff, 0x25, // jmpq *y(%rip)
1270 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1275 // The .eh_frame unwind information for the PLT.
1279 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1282 'z', // Augmentation: augmentation size included.
1283 'R', // Augmentation: FDE encoding included.
1284 '\0', // End of augmentation string.
1285 1, // Code alignment factor.
1286 0x78, // Data alignment factor.
1287 16, // Return address column.
1288 1, // Augmentation size.
1289 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1290 | elfcpp::DW_EH_PE_sdata4),
1291 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1292 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1293 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1299 Output_data_plt_x86_64<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1301 0, 0, 0, 0, // Replaced with offset to .plt.
1302 0, 0, 0, 0, // Replaced with size of .plt.
1303 0, // Augmentation size.
1304 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1305 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1306 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1307 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1308 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1309 11, // Block length.
1310 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1311 elfcpp::DW_OP_breg16, 0, // Push %rip.
1312 elfcpp::DW_OP_lit15, // Push 0xf.
1313 elfcpp::DW_OP_and, // & (%rip & 0xf).
1314 elfcpp::DW_OP_lit11, // Push 0xb.
1315 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1316 elfcpp::DW_OP_lit3, // Push 3.
1317 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1318 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1319 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1325 // Write out the PLT. This uses the hand-coded instructions above,
1326 // and adjusts them as needed. This is specified by the AMD64 ABI.
1330 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1332 const off_t offset = this->offset();
1333 const section_size_type oview_size =
1334 convert_to_section_size_type(this->data_size());
1335 unsigned char* const oview = of->get_output_view(offset, oview_size);
1337 const off_t got_file_offset = this->got_plt_->offset();
1338 gold_assert(parameters->incremental_update()
1339 || (got_file_offset + this->got_plt_->data_size()
1340 == this->got_irelative_->offset()));
1341 const section_size_type got_size =
1342 convert_to_section_size_type(this->got_plt_->data_size()
1343 + this->got_irelative_->data_size());
1344 unsigned char* const got_view = of->get_output_view(got_file_offset,
1347 unsigned char* pov = oview;
1349 // The base address of the .plt section.
1350 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1351 // The base address of the .got section.
1352 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1353 // The base address of the PLT portion of the .got section,
1354 // which is where the GOT pointer will point, and where the
1355 // three reserved GOT entries are located.
1356 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1357 = this->got_plt_->address();
1359 memcpy(pov, first_plt_entry, plt_entry_size);
1360 // We do a jmp relative to the PC at the end of this instruction.
1361 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1363 - (plt_address + 6)));
1364 elfcpp::Swap<32, false>::writeval(pov + 8,
1366 - (plt_address + 12)));
1367 pov += plt_entry_size;
1369 unsigned char* got_pov = got_view;
1371 // The first entry in the GOT is the address of the .dynamic section
1372 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1373 // We saved space for them when we created the section in
1374 // Target_x86_64::got_section.
1375 Output_section* dynamic = this->layout_->dynamic_section();
1376 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1377 elfcpp::Swap<64, false>::writeval(got_pov, dynamic_addr);
1379 memset(got_pov, 0, 16);
1382 unsigned int plt_offset = plt_entry_size;
1383 unsigned int got_offset = 24;
1384 const unsigned int count = this->count_ + this->irelative_count_;
1385 for (unsigned int plt_index = 0;
1388 pov += plt_entry_size,
1390 plt_offset += plt_entry_size,
1393 // Set and adjust the PLT entry itself.
1394 memcpy(pov, plt_entry, plt_entry_size);
1395 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1396 (got_address + got_offset
1397 - (plt_address + plt_offset
1400 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1401 elfcpp::Swap<32, false>::writeval(pov + 12,
1402 - (plt_offset + plt_entry_size));
1404 // Set the entry in the GOT.
1405 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1408 if (this->has_tlsdesc_entry())
1410 // Set and adjust the reserved TLSDESC PLT entry.
1411 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1412 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1413 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1415 - (plt_address + plt_offset
1417 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1419 + tlsdesc_got_offset
1420 - (plt_address + plt_offset
1422 pov += plt_entry_size;
1425 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1426 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1428 of->write_output_view(offset, oview_size, oview);
1429 of->write_output_view(got_file_offset, got_size, got_view);
1432 // Create the PLT section.
1436 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1438 if (this->plt_ == NULL)
1440 // Create the GOT sections first.
1441 this->got_section(symtab, layout);
1443 this->plt_ = new Output_data_plt_x86_64<size>(layout, this->got_,
1445 this->got_irelative_);
1446 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1448 | elfcpp::SHF_EXECINSTR),
1449 this->plt_, ORDER_PLT, false);
1451 // Make the sh_info field of .rela.plt point to .plt.
1452 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1453 rela_plt_os->set_info_section(this->plt_->output_section());
1457 // Return the section for TLSDESC relocations.
1460 typename Target_x86_64<size>::Reloc_section*
1461 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1463 return this->plt_section()->rela_tlsdesc(layout);
1466 // Create a PLT entry for a global symbol.
1470 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1473 if (gsym->has_plt_offset())
1476 if (this->plt_ == NULL)
1477 this->make_plt_section(symtab, layout);
1479 this->plt_->add_entry(symtab, layout, gsym);
1482 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1486 Target_x86_64<size>::make_local_ifunc_plt_entry(
1487 Symbol_table* symtab, Layout* layout,
1488 Sized_relobj_file<size, false>* relobj,
1489 unsigned int local_sym_index)
1491 if (relobj->local_has_plt_offset(local_sym_index))
1493 if (this->plt_ == NULL)
1494 this->make_plt_section(symtab, layout);
1495 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1498 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1501 // Return the number of entries in the PLT.
1505 Target_x86_64<size>::plt_entry_count() const
1507 if (this->plt_ == NULL)
1509 return this->plt_->entry_count();
1512 // Return the offset of the first non-reserved PLT entry.
1516 Target_x86_64<size>::first_plt_entry_offset() const
1518 return Output_data_plt_x86_64<size>::first_plt_entry_offset();
1521 // Return the size of each PLT entry.
1525 Target_x86_64<size>::plt_entry_size() const
1527 return Output_data_plt_x86_64<size>::get_plt_entry_size();
1530 // Create the GOT and PLT sections for an incremental update.
1533 Output_data_got_base*
1534 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1536 unsigned int got_count,
1537 unsigned int plt_count)
1539 gold_assert(this->got_ == NULL);
1541 this->got_ = new Output_data_got<64, false>(got_count * 8);
1542 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1544 | elfcpp::SHF_WRITE),
1545 this->got_, ORDER_RELRO_LAST,
1548 // Add the three reserved entries.
1549 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1550 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1552 | elfcpp::SHF_WRITE),
1553 this->got_plt_, ORDER_NON_RELRO_FIRST,
1556 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1557 this->global_offset_table_ =
1558 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1559 Symbol_table::PREDEFINED,
1561 0, 0, elfcpp::STT_OBJECT,
1563 elfcpp::STV_HIDDEN, 0,
1566 // If there are any TLSDESC relocations, they get GOT entries in
1567 // .got.plt after the jump slot entries.
1568 // FIXME: Get the count for TLSDESC entries.
1569 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1570 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1571 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1573 ORDER_NON_RELRO_FIRST, false);
1575 // If there are any IRELATIVE relocations, they get GOT entries in
1576 // .got.plt after the jump slot and TLSDESC entries.
1577 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1578 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1579 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1580 this->got_irelative_,
1581 ORDER_NON_RELRO_FIRST, false);
1583 // Create the PLT section.
1584 this->plt_ = new Output_data_plt_x86_64<size>(layout, this->got_,
1586 this->got_irelative_,
1588 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1589 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1590 this->plt_, ORDER_PLT, false);
1592 // Make the sh_info field of .rela.plt point to .plt.
1593 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1594 rela_plt_os->set_info_section(this->plt_->output_section());
1596 // Create the rela_dyn section.
1597 this->rela_dyn_section(layout);
1602 // Reserve a GOT entry for a local symbol, and regenerate any
1603 // necessary dynamic relocations.
1607 Target_x86_64<size>::reserve_local_got_entry(
1608 unsigned int got_index,
1609 Sized_relobj<size, false>* obj,
1611 unsigned int got_type)
1613 unsigned int got_offset = got_index * 8;
1614 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1616 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1619 case GOT_TYPE_STANDARD:
1620 if (parameters->options().output_is_position_independent())
1621 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1622 this->got_, got_offset, 0, false);
1624 case GOT_TYPE_TLS_OFFSET:
1625 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1626 this->got_, got_offset, 0);
1628 case GOT_TYPE_TLS_PAIR:
1629 this->got_->reserve_slot(got_index + 1);
1630 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1631 this->got_, got_offset, 0);
1633 case GOT_TYPE_TLS_DESC:
1634 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1635 // this->got_->reserve_slot(got_index + 1);
1636 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1637 // this->got_, got_offset, 0);
1644 // Reserve a GOT entry for a global symbol, and regenerate any
1645 // necessary dynamic relocations.
1649 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1651 unsigned int got_type)
1653 unsigned int got_offset = got_index * 8;
1654 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1656 this->got_->reserve_global(got_index, gsym, got_type);
1659 case GOT_TYPE_STANDARD:
1660 if (!gsym->final_value_is_known())
1662 if (gsym->is_from_dynobj()
1663 || gsym->is_undefined()
1664 || gsym->is_preemptible()
1665 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1666 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1667 this->got_, got_offset, 0);
1669 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1670 this->got_, got_offset, 0);
1673 case GOT_TYPE_TLS_OFFSET:
1674 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1675 this->got_, got_offset, 0);
1677 case GOT_TYPE_TLS_PAIR:
1678 this->got_->reserve_slot(got_index + 1);
1679 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1680 this->got_, got_offset, 0);
1681 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1682 this->got_, got_offset + 8, 0);
1684 case GOT_TYPE_TLS_DESC:
1685 this->got_->reserve_slot(got_index + 1);
1686 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1687 this->got_, got_offset, 0);
1694 // Register an existing PLT entry for a global symbol.
1698 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
1700 unsigned int plt_index,
1703 gold_assert(this->plt_ != NULL);
1704 gold_assert(!gsym->has_plt_offset());
1706 this->plt_->reserve_slot(plt_index);
1708 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1710 unsigned int got_offset = (plt_index + 3) * 8;
1711 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
1714 // Force a COPY relocation for a given symbol.
1718 Target_x86_64<size>::emit_copy_reloc(
1719 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1721 this->copy_relocs_.emit_copy_reloc(symtab,
1722 symtab->get_sized_symbol<size>(sym),
1725 this->rela_dyn_section(NULL));
1728 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1732 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
1735 if (this->tls_base_symbol_defined_)
1738 Output_segment* tls_segment = layout->tls_segment();
1739 if (tls_segment != NULL)
1741 bool is_exec = parameters->options().output_is_executable();
1742 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1743 Symbol_table::PREDEFINED,
1747 elfcpp::STV_HIDDEN, 0,
1749 ? Symbol::SEGMENT_END
1750 : Symbol::SEGMENT_START),
1753 this->tls_base_symbol_defined_ = true;
1756 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1760 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
1763 if (this->plt_ == NULL)
1764 this->make_plt_section(symtab, layout);
1766 if (!this->plt_->has_tlsdesc_entry())
1768 // Allocate the TLSDESC_GOT entry.
1769 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1770 unsigned int got_offset = got->add_constant(0);
1772 // Allocate the TLSDESC_PLT entry.
1773 this->plt_->reserve_tlsdesc_entry(got_offset);
1777 // Create a GOT entry for the TLS module index.
1781 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1782 Sized_relobj_file<size, false>* object)
1784 if (this->got_mod_index_offset_ == -1U)
1786 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1787 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1788 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1789 unsigned int got_offset = got->add_constant(0);
1790 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1792 got->add_constant(0);
1793 this->got_mod_index_offset_ = got_offset;
1795 return this->got_mod_index_offset_;
1798 // Optimize the TLS relocation type based on what we know about the
1799 // symbol. IS_FINAL is true if the final address of this symbol is
1800 // known at link time.
1803 tls::Tls_optimization
1804 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
1806 // If we are generating a shared library, then we can't do anything
1808 if (parameters->options().shared())
1809 return tls::TLSOPT_NONE;
1813 case elfcpp::R_X86_64_TLSGD:
1814 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1815 case elfcpp::R_X86_64_TLSDESC_CALL:
1816 // These are General-Dynamic which permits fully general TLS
1817 // access. Since we know that we are generating an executable,
1818 // we can convert this to Initial-Exec. If we also know that
1819 // this is a local symbol, we can further switch to Local-Exec.
1821 return tls::TLSOPT_TO_LE;
1822 return tls::TLSOPT_TO_IE;
1824 case elfcpp::R_X86_64_TLSLD:
1825 // This is Local-Dynamic, which refers to a local symbol in the
1826 // dynamic TLS block. Since we know that we generating an
1827 // executable, we can switch to Local-Exec.
1828 return tls::TLSOPT_TO_LE;
1830 case elfcpp::R_X86_64_DTPOFF32:
1831 case elfcpp::R_X86_64_DTPOFF64:
1832 // Another Local-Dynamic reloc.
1833 return tls::TLSOPT_TO_LE;
1835 case elfcpp::R_X86_64_GOTTPOFF:
1836 // These are Initial-Exec relocs which get the thread offset
1837 // from the GOT. If we know that we are linking against the
1838 // local symbol, we can switch to Local-Exec, which links the
1839 // thread offset into the instruction.
1841 return tls::TLSOPT_TO_LE;
1842 return tls::TLSOPT_NONE;
1844 case elfcpp::R_X86_64_TPOFF32:
1845 // When we already have Local-Exec, there is nothing further we
1847 return tls::TLSOPT_NONE;
1854 // Get the Reference_flags for a particular relocation.
1858 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
1862 case elfcpp::R_X86_64_NONE:
1863 case elfcpp::R_X86_64_GNU_VTINHERIT:
1864 case elfcpp::R_X86_64_GNU_VTENTRY:
1865 case elfcpp::R_X86_64_GOTPC32:
1866 case elfcpp::R_X86_64_GOTPC64:
1867 // No symbol reference.
1870 case elfcpp::R_X86_64_64:
1871 case elfcpp::R_X86_64_32:
1872 case elfcpp::R_X86_64_32S:
1873 case elfcpp::R_X86_64_16:
1874 case elfcpp::R_X86_64_8:
1875 return Symbol::ABSOLUTE_REF;
1877 case elfcpp::R_X86_64_PC64:
1878 case elfcpp::R_X86_64_PC32:
1879 case elfcpp::R_X86_64_PC16:
1880 case elfcpp::R_X86_64_PC8:
1881 case elfcpp::R_X86_64_GOTOFF64:
1882 return Symbol::RELATIVE_REF;
1884 case elfcpp::R_X86_64_PLT32:
1885 case elfcpp::R_X86_64_PLTOFF64:
1886 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1888 case elfcpp::R_X86_64_GOT64:
1889 case elfcpp::R_X86_64_GOT32:
1890 case elfcpp::R_X86_64_GOTPCREL64:
1891 case elfcpp::R_X86_64_GOTPCREL:
1892 case elfcpp::R_X86_64_GOTPLT64:
1894 return Symbol::ABSOLUTE_REF;
1896 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1897 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1898 case elfcpp::R_X86_64_TLSDESC_CALL:
1899 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1900 case elfcpp::R_X86_64_DTPOFF32:
1901 case elfcpp::R_X86_64_DTPOFF64:
1902 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1903 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1904 return Symbol::TLS_REF;
1906 case elfcpp::R_X86_64_COPY:
1907 case elfcpp::R_X86_64_GLOB_DAT:
1908 case elfcpp::R_X86_64_JUMP_SLOT:
1909 case elfcpp::R_X86_64_RELATIVE:
1910 case elfcpp::R_X86_64_IRELATIVE:
1911 case elfcpp::R_X86_64_TPOFF64:
1912 case elfcpp::R_X86_64_DTPMOD64:
1913 case elfcpp::R_X86_64_TLSDESC:
1914 case elfcpp::R_X86_64_SIZE32:
1915 case elfcpp::R_X86_64_SIZE64:
1917 // Not expected. We will give an error later.
1922 // Report an unsupported relocation against a local symbol.
1926 Target_x86_64<size>::Scan::unsupported_reloc_local(
1927 Sized_relobj_file<size, false>* object,
1928 unsigned int r_type)
1930 gold_error(_("%s: unsupported reloc %u against local symbol"),
1931 object->name().c_str(), r_type);
1934 // We are about to emit a dynamic relocation of type R_TYPE. If the
1935 // dynamic linker does not support it, issue an error. The GNU linker
1936 // only issues a non-PIC error for an allocated read-only section.
1937 // Here we know the section is allocated, but we don't know that it is
1938 // read-only. But we check for all the relocation types which the
1939 // glibc dynamic linker supports, so it seems appropriate to issue an
1940 // error even if the section is not read-only. If GSYM is not NULL,
1941 // it is the symbol the relocation is against; if it is NULL, the
1942 // relocation is against a local symbol.
1946 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1951 // These are the relocation types supported by glibc for x86_64
1952 // which should always work.
1953 case elfcpp::R_X86_64_RELATIVE:
1954 case elfcpp::R_X86_64_IRELATIVE:
1955 case elfcpp::R_X86_64_GLOB_DAT:
1956 case elfcpp::R_X86_64_JUMP_SLOT:
1957 case elfcpp::R_X86_64_DTPMOD64:
1958 case elfcpp::R_X86_64_DTPOFF64:
1959 case elfcpp::R_X86_64_TPOFF64:
1960 case elfcpp::R_X86_64_64:
1961 case elfcpp::R_X86_64_COPY:
1964 // glibc supports these reloc types, but they can overflow.
1965 case elfcpp::R_X86_64_PC32:
1966 // A PC relative reference is OK against a local symbol or if
1967 // the symbol is defined locally.
1969 || (!gsym->is_from_dynobj()
1970 && !gsym->is_undefined()
1971 && !gsym->is_preemptible()))
1974 case elfcpp::R_X86_64_32:
1975 // R_X86_64_32 is OK for x32.
1976 if (size == 32 && r_type == elfcpp::R_X86_64_32)
1978 if (this->issued_non_pic_error_)
1980 gold_assert(parameters->options().output_is_position_independent());
1982 object->error(_("requires dynamic R_X86_64_32 reloc which may "
1983 "overflow at runtime; recompile with -fPIC"));
1985 object->error(_("requires dynamic %s reloc against '%s' which may "
1986 "overflow at runtime; recompile with -fPIC"),
1987 (r_type == elfcpp::R_X86_64_32
1991 this->issued_non_pic_error_ = true;
1995 // This prevents us from issuing more than one error per reloc
1996 // section. But we can still wind up issuing more than one
1997 // error per object file.
1998 if (this->issued_non_pic_error_)
2000 gold_assert(parameters->options().output_is_position_independent());
2001 object->error(_("requires unsupported dynamic reloc %u; "
2002 "recompile with -fPIC"),
2004 this->issued_non_pic_error_ = true;
2007 case elfcpp::R_X86_64_NONE:
2012 // Return whether we need to make a PLT entry for a relocation of the
2013 // given type against a STT_GNU_IFUNC symbol.
2017 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2018 Sized_relobj_file<size, false>* object,
2019 unsigned int r_type)
2021 int flags = Scan::get_reference_flags(r_type);
2022 if (flags & Symbol::TLS_REF)
2023 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2024 object->name().c_str(), r_type);
2028 // Scan a relocation for a local symbol.
2032 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2034 Target_x86_64<size>* target,
2035 Sized_relobj_file<size, false>* object,
2036 unsigned int data_shndx,
2037 Output_section* output_section,
2038 const elfcpp::Rela<size, false>& reloc,
2039 unsigned int r_type,
2040 const elfcpp::Sym<size, false>& lsym)
2042 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2043 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2044 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2046 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2047 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2052 case elfcpp::R_X86_64_NONE:
2053 case elfcpp::R_X86_64_GNU_VTINHERIT:
2054 case elfcpp::R_X86_64_GNU_VTENTRY:
2057 case elfcpp::R_X86_64_64:
2058 // If building a shared library (or a position-independent
2059 // executable), we need to create a dynamic relocation for this
2060 // location. The relocation applied at link time will apply the
2061 // link-time value, so we flag the location with an
2062 // R_X86_64_RELATIVE relocation so the dynamic loader can
2063 // relocate it easily.
2064 if (parameters->options().output_is_position_independent())
2066 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2067 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2068 rela_dyn->add_local_relative(object, r_sym,
2069 elfcpp::R_X86_64_RELATIVE,
2070 output_section, data_shndx,
2071 reloc.get_r_offset(),
2072 reloc.get_r_addend(), is_ifunc);
2076 case elfcpp::R_X86_64_32:
2077 case elfcpp::R_X86_64_32S:
2078 case elfcpp::R_X86_64_16:
2079 case elfcpp::R_X86_64_8:
2080 // If building a shared library (or a position-independent
2081 // executable), we need to create a dynamic relocation for this
2082 // location. We can't use an R_X86_64_RELATIVE relocation
2083 // because that is always a 64-bit relocation.
2084 if (parameters->options().output_is_position_independent())
2086 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2087 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2089 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2090 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2091 rela_dyn->add_local_relative(object, r_sym,
2092 elfcpp::R_X86_64_RELATIVE,
2093 output_section, data_shndx,
2094 reloc.get_r_offset(),
2095 reloc.get_r_addend(), is_ifunc);
2099 this->check_non_pic(object, r_type, NULL);
2101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2102 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2103 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2104 rela_dyn->add_local(object, r_sym, r_type, output_section,
2105 data_shndx, reloc.get_r_offset(),
2106 reloc.get_r_addend());
2109 gold_assert(lsym.get_st_value() == 0);
2110 unsigned int shndx = lsym.get_st_shndx();
2112 shndx = object->adjust_sym_shndx(r_sym, shndx,
2115 object->error(_("section symbol %u has bad shndx %u"),
2118 rela_dyn->add_local_section(object, shndx,
2119 r_type, output_section,
2120 data_shndx, reloc.get_r_offset(),
2121 reloc.get_r_addend());
2126 case elfcpp::R_X86_64_PC64:
2127 case elfcpp::R_X86_64_PC32:
2128 case elfcpp::R_X86_64_PC16:
2129 case elfcpp::R_X86_64_PC8:
2132 case elfcpp::R_X86_64_PLT32:
2133 // Since we know this is a local symbol, we can handle this as a
2137 case elfcpp::R_X86_64_GOTPC32:
2138 case elfcpp::R_X86_64_GOTOFF64:
2139 case elfcpp::R_X86_64_GOTPC64:
2140 case elfcpp::R_X86_64_PLTOFF64:
2141 // We need a GOT section.
2142 target->got_section(symtab, layout);
2143 // For PLTOFF64, we'd normally want a PLT section, but since we
2144 // know this is a local symbol, no PLT is needed.
2147 case elfcpp::R_X86_64_GOT64:
2148 case elfcpp::R_X86_64_GOT32:
2149 case elfcpp::R_X86_64_GOTPCREL64:
2150 case elfcpp::R_X86_64_GOTPCREL:
2151 case elfcpp::R_X86_64_GOTPLT64:
2153 // The symbol requires a GOT entry.
2154 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2155 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2157 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2158 // lets function pointers compare correctly with shared
2159 // libraries. Otherwise we would need an IRELATIVE reloc.
2162 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2164 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2167 // If we are generating a shared object, we need to add a
2168 // dynamic relocation for this symbol's GOT entry.
2169 if (parameters->options().output_is_position_independent())
2171 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2172 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2173 if (r_type != elfcpp::R_X86_64_GOT32)
2175 unsigned int got_offset =
2176 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2177 rela_dyn->add_local_relative(object, r_sym,
2178 elfcpp::R_X86_64_RELATIVE,
2179 got, got_offset, 0, is_ifunc);
2183 this->check_non_pic(object, r_type, NULL);
2185 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2186 rela_dyn->add_local(
2187 object, r_sym, r_type, got,
2188 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2192 // For GOTPLT64, we'd normally want a PLT section, but since
2193 // we know this is a local symbol, no PLT is needed.
2197 case elfcpp::R_X86_64_COPY:
2198 case elfcpp::R_X86_64_GLOB_DAT:
2199 case elfcpp::R_X86_64_JUMP_SLOT:
2200 case elfcpp::R_X86_64_RELATIVE:
2201 case elfcpp::R_X86_64_IRELATIVE:
2202 // These are outstanding tls relocs, which are unexpected when linking
2203 case elfcpp::R_X86_64_TPOFF64:
2204 case elfcpp::R_X86_64_DTPMOD64:
2205 case elfcpp::R_X86_64_TLSDESC:
2206 gold_error(_("%s: unexpected reloc %u in object file"),
2207 object->name().c_str(), r_type);
2210 // These are initial tls relocs, which are expected when linking
2211 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2212 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2213 case elfcpp::R_X86_64_TLSDESC_CALL:
2214 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2215 case elfcpp::R_X86_64_DTPOFF32:
2216 case elfcpp::R_X86_64_DTPOFF64:
2217 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2218 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2220 bool output_is_shared = parameters->options().shared();
2221 const tls::Tls_optimization optimized_type
2222 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2226 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2227 if (optimized_type == tls::TLSOPT_NONE)
2229 // Create a pair of GOT entries for the module index and
2230 // dtv-relative offset.
2231 Output_data_got<64, false>* got
2232 = target->got_section(symtab, layout);
2233 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2234 unsigned int shndx = lsym.get_st_shndx();
2236 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2238 object->error(_("local symbol %u has bad shndx %u"),
2241 got->add_local_pair_with_rel(object, r_sym,
2244 target->rela_dyn_section(layout),
2245 elfcpp::R_X86_64_DTPMOD64, 0);
2247 else if (optimized_type != tls::TLSOPT_TO_LE)
2248 unsupported_reloc_local(object, r_type);
2251 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2252 target->define_tls_base_symbol(symtab, layout);
2253 if (optimized_type == tls::TLSOPT_NONE)
2255 // Create reserved PLT and GOT entries for the resolver.
2256 target->reserve_tlsdesc_entries(symtab, layout);
2258 // Generate a double GOT entry with an
2259 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2260 // is resolved lazily, so the GOT entry needs to be in
2261 // an area in .got.plt, not .got. Call got_section to
2262 // make sure the section has been created.
2263 target->got_section(symtab, layout);
2264 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2265 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2266 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2268 unsigned int got_offset = got->add_constant(0);
2269 got->add_constant(0);
2270 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2272 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2273 // We store the arguments we need in a vector, and
2274 // use the index into the vector as the parameter
2275 // to pass to the target specific routines.
2276 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2277 void* arg = reinterpret_cast<void*>(intarg);
2278 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2279 got, got_offset, 0);
2282 else if (optimized_type != tls::TLSOPT_TO_LE)
2283 unsupported_reloc_local(object, r_type);
2286 case elfcpp::R_X86_64_TLSDESC_CALL:
2289 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2290 if (optimized_type == tls::TLSOPT_NONE)
2292 // Create a GOT entry for the module index.
2293 target->got_mod_index_entry(symtab, layout, object);
2295 else if (optimized_type != tls::TLSOPT_TO_LE)
2296 unsupported_reloc_local(object, r_type);
2299 case elfcpp::R_X86_64_DTPOFF32:
2300 case elfcpp::R_X86_64_DTPOFF64:
2303 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2304 layout->set_has_static_tls();
2305 if (optimized_type == tls::TLSOPT_NONE)
2307 // Create a GOT entry for the tp-relative offset.
2308 Output_data_got<64, false>* got
2309 = target->got_section(symtab, layout);
2310 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2311 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2312 target->rela_dyn_section(layout),
2313 elfcpp::R_X86_64_TPOFF64);
2315 else if (optimized_type != tls::TLSOPT_TO_LE)
2316 unsupported_reloc_local(object, r_type);
2319 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2320 layout->set_has_static_tls();
2321 if (output_is_shared)
2322 unsupported_reloc_local(object, r_type);
2331 case elfcpp::R_X86_64_SIZE32:
2332 case elfcpp::R_X86_64_SIZE64:
2334 gold_error(_("%s: unsupported reloc %u against local symbol"),
2335 object->name().c_str(), r_type);
2341 // Report an unsupported relocation against a global symbol.
2345 Target_x86_64<size>::Scan::unsupported_reloc_global(
2346 Sized_relobj_file<size, false>* object,
2347 unsigned int r_type,
2350 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2351 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2354 // Returns true if this relocation type could be that of a function pointer.
2357 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2361 case elfcpp::R_X86_64_64:
2362 case elfcpp::R_X86_64_32:
2363 case elfcpp::R_X86_64_32S:
2364 case elfcpp::R_X86_64_16:
2365 case elfcpp::R_X86_64_8:
2366 case elfcpp::R_X86_64_GOT64:
2367 case elfcpp::R_X86_64_GOT32:
2368 case elfcpp::R_X86_64_GOTPCREL64:
2369 case elfcpp::R_X86_64_GOTPCREL:
2370 case elfcpp::R_X86_64_GOTPLT64:
2378 // For safe ICF, scan a relocation for a local symbol to check if it
2379 // corresponds to a function pointer being taken. In that case mark
2380 // the function whose pointer was taken as not foldable.
2384 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2387 Target_x86_64<size>* ,
2388 Sized_relobj_file<size, false>* ,
2391 const elfcpp::Rela<size, false>& ,
2392 unsigned int r_type,
2393 const elfcpp::Sym<size, false>&)
2395 // When building a shared library, do not fold any local symbols as it is
2396 // not possible to distinguish pointer taken versus a call by looking at
2397 // the relocation types.
2398 return (parameters->options().shared()
2399 || possible_function_pointer_reloc(r_type));
2402 // For safe ICF, scan a relocation for a global symbol to check if it
2403 // corresponds to a function pointer being taken. In that case mark
2404 // the function whose pointer was taken as not foldable.
2408 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2411 Target_x86_64<size>* ,
2412 Sized_relobj_file<size, false>* ,
2415 const elfcpp::Rela<size, false>& ,
2416 unsigned int r_type,
2419 // When building a shared library, do not fold symbols whose visibility
2420 // is hidden, internal or protected.
2421 return ((parameters->options().shared()
2422 && (gsym->visibility() == elfcpp::STV_INTERNAL
2423 || gsym->visibility() == elfcpp::STV_PROTECTED
2424 || gsym->visibility() == elfcpp::STV_HIDDEN))
2425 || possible_function_pointer_reloc(r_type));
2428 // Scan a relocation for a global symbol.
2432 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2434 Target_x86_64<size>* target,
2435 Sized_relobj_file<size, false>* object,
2436 unsigned int data_shndx,
2437 Output_section* output_section,
2438 const elfcpp::Rela<size, false>& reloc,
2439 unsigned int r_type,
2442 // A STT_GNU_IFUNC symbol may require a PLT entry.
2443 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2444 && this->reloc_needs_plt_for_ifunc(object, r_type))
2445 target->make_plt_entry(symtab, layout, gsym);
2449 case elfcpp::R_X86_64_NONE:
2450 case elfcpp::R_X86_64_GNU_VTINHERIT:
2451 case elfcpp::R_X86_64_GNU_VTENTRY:
2454 case elfcpp::R_X86_64_64:
2455 case elfcpp::R_X86_64_32:
2456 case elfcpp::R_X86_64_32S:
2457 case elfcpp::R_X86_64_16:
2458 case elfcpp::R_X86_64_8:
2460 // Make a PLT entry if necessary.
2461 if (gsym->needs_plt_entry())
2463 target->make_plt_entry(symtab, layout, gsym);
2464 // Since this is not a PC-relative relocation, we may be
2465 // taking the address of a function. In that case we need to
2466 // set the entry in the dynamic symbol table to the address of
2468 if (gsym->is_from_dynobj() && !parameters->options().shared())
2469 gsym->set_needs_dynsym_value();
2471 // Make a dynamic relocation if necessary.
2472 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2474 if (gsym->may_need_copy_reloc())
2476 target->copy_reloc(symtab, layout, object,
2477 data_shndx, output_section, gsym, reloc);
2479 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2480 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2481 && gsym->type() == elfcpp::STT_GNU_IFUNC
2482 && gsym->can_use_relative_reloc(false)
2483 && !gsym->is_from_dynobj()
2484 && !gsym->is_undefined()
2485 && !gsym->is_preemptible())
2487 // Use an IRELATIVE reloc for a locally defined
2488 // STT_GNU_IFUNC symbol. This makes a function
2489 // address in a PIE executable match the address in a
2490 // shared library that it links against.
2491 Reloc_section* rela_dyn =
2492 target->rela_irelative_section(layout);
2493 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2494 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2495 output_section, object,
2497 reloc.get_r_offset(),
2498 reloc.get_r_addend());
2500 else if (r_type == elfcpp::R_X86_64_64
2501 && gsym->can_use_relative_reloc(false))
2503 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2504 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2505 output_section, object,
2507 reloc.get_r_offset(),
2508 reloc.get_r_addend());
2512 this->check_non_pic(object, r_type, gsym);
2513 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2514 rela_dyn->add_global(gsym, r_type, output_section, object,
2515 data_shndx, reloc.get_r_offset(),
2516 reloc.get_r_addend());
2522 case elfcpp::R_X86_64_PC64:
2523 case elfcpp::R_X86_64_PC32:
2524 case elfcpp::R_X86_64_PC16:
2525 case elfcpp::R_X86_64_PC8:
2527 // Make a PLT entry if necessary.
2528 if (gsym->needs_plt_entry())
2529 target->make_plt_entry(symtab, layout, gsym);
2530 // Make a dynamic relocation if necessary.
2531 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2533 if (gsym->may_need_copy_reloc())
2535 target->copy_reloc(symtab, layout, object,
2536 data_shndx, output_section, gsym, reloc);
2540 this->check_non_pic(object, r_type, gsym);
2541 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2542 rela_dyn->add_global(gsym, r_type, output_section, object,
2543 data_shndx, reloc.get_r_offset(),
2544 reloc.get_r_addend());
2550 case elfcpp::R_X86_64_GOT64:
2551 case elfcpp::R_X86_64_GOT32:
2552 case elfcpp::R_X86_64_GOTPCREL64:
2553 case elfcpp::R_X86_64_GOTPCREL:
2554 case elfcpp::R_X86_64_GOTPLT64:
2556 // The symbol requires a GOT entry.
2557 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2558 if (gsym->final_value_is_known())
2560 // For a STT_GNU_IFUNC symbol we want the PLT address.
2561 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2562 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2564 got->add_global(gsym, GOT_TYPE_STANDARD);
2568 // If this symbol is not fully resolved, we need to add a
2569 // dynamic relocation for it.
2570 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2572 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2574 // 1) The symbol may be defined in some other module.
2576 // 2) We are building a shared library and this is a
2577 // protected symbol; using GLOB_DAT means that the dynamic
2578 // linker can use the address of the PLT in the main
2579 // executable when appropriate so that function address
2580 // comparisons work.
2582 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2583 // code, again so that function address comparisons work.
2584 if (gsym->is_from_dynobj()
2585 || gsym->is_undefined()
2586 || gsym->is_preemptible()
2587 || (gsym->visibility() == elfcpp::STV_PROTECTED
2588 && parameters->options().shared())
2589 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2590 && parameters->options().output_is_position_independent()))
2591 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2592 elfcpp::R_X86_64_GLOB_DAT);
2595 // For a STT_GNU_IFUNC symbol we want to write the PLT
2596 // offset into the GOT, so that function pointer
2597 // comparisons work correctly.
2599 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2600 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2603 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2604 // Tell the dynamic linker to use the PLT address
2605 // when resolving relocations.
2606 if (gsym->is_from_dynobj()
2607 && !parameters->options().shared())
2608 gsym->set_needs_dynsym_value();
2612 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2613 rela_dyn->add_global_relative(gsym,
2614 elfcpp::R_X86_64_RELATIVE,
2619 // For GOTPLT64, we also need a PLT entry (but only if the
2620 // symbol is not fully resolved).
2621 if (r_type == elfcpp::R_X86_64_GOTPLT64
2622 && !gsym->final_value_is_known())
2623 target->make_plt_entry(symtab, layout, gsym);
2627 case elfcpp::R_X86_64_PLT32:
2628 // If the symbol is fully resolved, this is just a PC32 reloc.
2629 // Otherwise we need a PLT entry.
2630 if (gsym->final_value_is_known())
2632 // If building a shared library, we can also skip the PLT entry
2633 // if the symbol is defined in the output file and is protected
2635 if (gsym->is_defined()
2636 && !gsym->is_from_dynobj()
2637 && !gsym->is_preemptible())
2639 target->make_plt_entry(symtab, layout, gsym);
2642 case elfcpp::R_X86_64_GOTPC32:
2643 case elfcpp::R_X86_64_GOTOFF64:
2644 case elfcpp::R_X86_64_GOTPC64:
2645 case elfcpp::R_X86_64_PLTOFF64:
2646 // We need a GOT section.
2647 target->got_section(symtab, layout);
2648 // For PLTOFF64, we also need a PLT entry (but only if the
2649 // symbol is not fully resolved).
2650 if (r_type == elfcpp::R_X86_64_PLTOFF64
2651 && !gsym->final_value_is_known())
2652 target->make_plt_entry(symtab, layout, gsym);
2655 case elfcpp::R_X86_64_COPY:
2656 case elfcpp::R_X86_64_GLOB_DAT:
2657 case elfcpp::R_X86_64_JUMP_SLOT:
2658 case elfcpp::R_X86_64_RELATIVE:
2659 case elfcpp::R_X86_64_IRELATIVE:
2660 // These are outstanding tls relocs, which are unexpected when linking
2661 case elfcpp::R_X86_64_TPOFF64:
2662 case elfcpp::R_X86_64_DTPMOD64:
2663 case elfcpp::R_X86_64_TLSDESC:
2664 gold_error(_("%s: unexpected reloc %u in object file"),
2665 object->name().c_str(), r_type);
2668 // These are initial tls relocs, which are expected for global()
2669 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2670 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2671 case elfcpp::R_X86_64_TLSDESC_CALL:
2672 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2673 case elfcpp::R_X86_64_DTPOFF32:
2674 case elfcpp::R_X86_64_DTPOFF64:
2675 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2676 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2678 const bool is_final = gsym->final_value_is_known();
2679 const tls::Tls_optimization optimized_type
2680 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2683 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2684 if (optimized_type == tls::TLSOPT_NONE)
2686 // Create a pair of GOT entries for the module index and
2687 // dtv-relative offset.
2688 Output_data_got<64, false>* got
2689 = target->got_section(symtab, layout);
2690 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2691 target->rela_dyn_section(layout),
2692 elfcpp::R_X86_64_DTPMOD64,
2693 elfcpp::R_X86_64_DTPOFF64);
2695 else if (optimized_type == tls::TLSOPT_TO_IE)
2697 // Create a GOT entry for the tp-relative offset.
2698 Output_data_got<64, false>* got
2699 = target->got_section(symtab, layout);
2700 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2701 target->rela_dyn_section(layout),
2702 elfcpp::R_X86_64_TPOFF64);
2704 else if (optimized_type != tls::TLSOPT_TO_LE)
2705 unsupported_reloc_global(object, r_type, gsym);
2708 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2709 target->define_tls_base_symbol(symtab, layout);
2710 if (optimized_type == tls::TLSOPT_NONE)
2712 // Create reserved PLT and GOT entries for the resolver.
2713 target->reserve_tlsdesc_entries(symtab, layout);
2715 // Create a double GOT entry with an R_X86_64_TLSDESC
2716 // reloc. The R_X86_64_TLSDESC reloc is resolved
2717 // lazily, so the GOT entry needs to be in an area in
2718 // .got.plt, not .got. Call got_section to make sure
2719 // the section has been created.
2720 target->got_section(symtab, layout);
2721 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2722 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2723 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2724 elfcpp::R_X86_64_TLSDESC, 0);
2726 else if (optimized_type == tls::TLSOPT_TO_IE)
2728 // Create a GOT entry for the tp-relative offset.
2729 Output_data_got<64, false>* got
2730 = target->got_section(symtab, layout);
2731 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2732 target->rela_dyn_section(layout),
2733 elfcpp::R_X86_64_TPOFF64);
2735 else if (optimized_type != tls::TLSOPT_TO_LE)
2736 unsupported_reloc_global(object, r_type, gsym);
2739 case elfcpp::R_X86_64_TLSDESC_CALL:
2742 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2743 if (optimized_type == tls::TLSOPT_NONE)
2745 // Create a GOT entry for the module index.
2746 target->got_mod_index_entry(symtab, layout, object);
2748 else if (optimized_type != tls::TLSOPT_TO_LE)
2749 unsupported_reloc_global(object, r_type, gsym);
2752 case elfcpp::R_X86_64_DTPOFF32:
2753 case elfcpp::R_X86_64_DTPOFF64:
2756 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2757 layout->set_has_static_tls();
2758 if (optimized_type == tls::TLSOPT_NONE)
2760 // Create a GOT entry for the tp-relative offset.
2761 Output_data_got<64, false>* got
2762 = target->got_section(symtab, layout);
2763 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2764 target->rela_dyn_section(layout),
2765 elfcpp::R_X86_64_TPOFF64);
2767 else if (optimized_type != tls::TLSOPT_TO_LE)
2768 unsupported_reloc_global(object, r_type, gsym);
2771 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2772 layout->set_has_static_tls();
2773 if (parameters->options().shared())
2774 unsupported_reloc_local(object, r_type);
2783 case elfcpp::R_X86_64_SIZE32:
2784 case elfcpp::R_X86_64_SIZE64:
2786 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2787 object->name().c_str(), r_type,
2788 gsym->demangled_name().c_str());
2795 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
2797 Sized_relobj_file<size, false>* object,
2798 unsigned int data_shndx,
2799 unsigned int sh_type,
2800 const unsigned char* prelocs,
2802 Output_section* output_section,
2803 bool needs_special_offset_handling,
2804 size_t local_symbol_count,
2805 const unsigned char* plocal_symbols)
2808 if (sh_type == elfcpp::SHT_REL)
2813 gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
2814 typename Target_x86_64<size>::Scan,
2815 typename Target_x86_64<size>::Relocatable_size_for_reloc>(
2824 needs_special_offset_handling,
2829 // Scan relocations for a section.
2833 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
2835 Sized_relobj_file<size, false>* object,
2836 unsigned int data_shndx,
2837 unsigned int sh_type,
2838 const unsigned char* prelocs,
2840 Output_section* output_section,
2841 bool needs_special_offset_handling,
2842 size_t local_symbol_count,
2843 const unsigned char* plocal_symbols)
2845 if (sh_type == elfcpp::SHT_REL)
2847 gold_error(_("%s: unsupported REL reloc section"),
2848 object->name().c_str());
2852 gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
2853 typename Target_x86_64<size>::Scan>(
2862 needs_special_offset_handling,
2867 // Finalize the sections.
2871 Target_x86_64<size>::do_finalize_sections(
2873 const Input_objects*,
2874 Symbol_table* symtab)
2876 const Reloc_section* rel_plt = (this->plt_ == NULL
2878 : this->plt_->rela_plt());
2879 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2880 this->rela_dyn_, true, false);
2882 // Fill in some more dynamic tags.
2883 Output_data_dynamic* const odyn = layout->dynamic_data();
2886 if (this->plt_ != NULL
2887 && this->plt_->output_section() != NULL
2888 && this->plt_->has_tlsdesc_entry())
2890 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2891 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2892 this->got_->finalize_data_size();
2893 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2894 this->plt_, plt_offset);
2895 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2896 this->got_, got_offset);
2900 // Emit any relocs we saved in an attempt to avoid generating COPY
2902 if (this->copy_relocs_.any_saved_relocs())
2903 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2905 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2906 // the .got.plt section.
2907 Symbol* sym = this->global_offset_table_;
2910 uint64_t data_size = this->got_plt_->current_data_size();
2911 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
2914 if (parameters->doing_static_link()
2915 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2917 // If linking statically, make sure that the __rela_iplt symbols
2918 // were defined if necessary, even if we didn't create a PLT.
2919 static const Define_symbol_in_segment syms[] =
2922 "__rela_iplt_start", // name
2923 elfcpp::PT_LOAD, // segment_type
2924 elfcpp::PF_W, // segment_flags_set
2925 elfcpp::PF(0), // segment_flags_clear
2928 elfcpp::STT_NOTYPE, // type
2929 elfcpp::STB_GLOBAL, // binding
2930 elfcpp::STV_HIDDEN, // visibility
2932 Symbol::SEGMENT_START, // offset_from_base
2936 "__rela_iplt_end", // name
2937 elfcpp::PT_LOAD, // segment_type
2938 elfcpp::PF_W, // segment_flags_set
2939 elfcpp::PF(0), // segment_flags_clear
2942 elfcpp::STT_NOTYPE, // type
2943 elfcpp::STB_GLOBAL, // binding
2944 elfcpp::STV_HIDDEN, // visibility
2946 Symbol::SEGMENT_START, // offset_from_base
2951 symtab->define_symbols(layout, 2, syms,
2952 layout->script_options()->saw_sections_clause());
2956 // Perform a relocation.
2960 Target_x86_64<size>::Relocate::relocate(
2961 const Relocate_info<size, false>* relinfo,
2962 Target_x86_64<size>* target,
2965 const elfcpp::Rela<size, false>& rela,
2966 unsigned int r_type,
2967 const Sized_symbol<size>* gsym,
2968 const Symbol_value<size>* psymval,
2969 unsigned char* view,
2970 typename elfcpp::Elf_types<size>::Elf_Addr address,
2971 section_size_type view_size)
2973 if (this->skip_call_tls_get_addr_)
2975 if ((r_type != elfcpp::R_X86_64_PLT32
2976 && r_type != elfcpp::R_X86_64_PC32)
2978 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2980 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2981 _("missing expected TLS relocation"));
2985 this->skip_call_tls_get_addr_ = false;
2990 const Sized_relobj_file<size, false>* object = relinfo->object;
2992 // Pick the value to use for symbols defined in the PLT.
2993 Symbol_value<size> symval;
2995 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2997 symval.set_output_value(target->plt_address_for_global(gsym)
2998 + gsym->plt_offset());
3001 else if (gsym == NULL && psymval->is_ifunc_symbol())
3003 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3004 if (object->local_has_plt_offset(r_sym))
3006 symval.set_output_value(target->plt_address_for_local(object, r_sym)
3007 + object->local_plt_offset(r_sym));
3012 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3014 // Get the GOT offset if needed.
3015 // The GOT pointer points to the end of the GOT section.
3016 // We need to subtract the size of the GOT section to get
3017 // the actual offset to use in the relocation.
3018 bool have_got_offset = false;
3019 unsigned int got_offset = 0;
3022 case elfcpp::R_X86_64_GOT32:
3023 case elfcpp::R_X86_64_GOT64:
3024 case elfcpp::R_X86_64_GOTPLT64:
3025 case elfcpp::R_X86_64_GOTPCREL:
3026 case elfcpp::R_X86_64_GOTPCREL64:
3029 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3030 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3034 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3035 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3036 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3037 - target->got_size());
3039 have_got_offset = true;
3048 case elfcpp::R_X86_64_NONE:
3049 case elfcpp::R_X86_64_GNU_VTINHERIT:
3050 case elfcpp::R_X86_64_GNU_VTENTRY:
3053 case elfcpp::R_X86_64_64:
3054 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
3057 case elfcpp::R_X86_64_PC64:
3058 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
3062 case elfcpp::R_X86_64_32:
3063 // FIXME: we need to verify that value + addend fits into 32 bits:
3064 // uint64_t x = value + addend;
3065 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3066 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3067 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3070 case elfcpp::R_X86_64_32S:
3071 // FIXME: we need to verify that value + addend fits into 32 bits:
3072 // int64_t x = value + addend; // note this quantity is signed!
3073 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3074 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3077 case elfcpp::R_X86_64_PC32:
3078 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3082 case elfcpp::R_X86_64_16:
3083 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
3086 case elfcpp::R_X86_64_PC16:
3087 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3091 case elfcpp::R_X86_64_8:
3092 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
3095 case elfcpp::R_X86_64_PC8:
3096 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3100 case elfcpp::R_X86_64_PLT32:
3101 gold_assert(gsym == NULL
3102 || gsym->has_plt_offset()
3103 || gsym->final_value_is_known()
3104 || (gsym->is_defined()
3105 && !gsym->is_from_dynobj()
3106 && !gsym->is_preemptible()));
3107 // Note: while this code looks the same as for R_X86_64_PC32, it
3108 // behaves differently because psymval was set to point to
3109 // the PLT entry, rather than the symbol, in Scan::global().
3110 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3114 case elfcpp::R_X86_64_PLTOFF64:
3117 gold_assert(gsym->has_plt_offset()
3118 || gsym->final_value_is_known());
3119 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3120 got_address = target->got_section(NULL, NULL)->address();
3121 Relocate_functions<size, false>::rela64(view, object, psymval,
3122 addend - got_address);
3125 case elfcpp::R_X86_64_GOT32:
3126 gold_assert(have_got_offset);
3127 Relocate_functions<size, false>::rela32(view, got_offset, addend);
3130 case elfcpp::R_X86_64_GOTPC32:
3133 typename elfcpp::Elf_types<size>::Elf_Addr value;
3134 value = target->got_plt_section()->address();
3135 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3139 case elfcpp::R_X86_64_GOT64:
3140 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3141 // Since we always add a PLT entry, this is equivalent.
3142 case elfcpp::R_X86_64_GOTPLT64:
3143 gold_assert(have_got_offset);
3144 Relocate_functions<size, false>::rela64(view, got_offset, addend);
3147 case elfcpp::R_X86_64_GOTPC64:
3150 typename elfcpp::Elf_types<size>::Elf_Addr value;
3151 value = target->got_plt_section()->address();
3152 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3156 case elfcpp::R_X86_64_GOTOFF64:
3158 typename elfcpp::Elf_types<size>::Elf_Addr value;
3159 value = (psymval->value(object, 0)
3160 - target->got_plt_section()->address());
3161 Relocate_functions<size, false>::rela64(view, value, addend);
3165 case elfcpp::R_X86_64_GOTPCREL:
3167 gold_assert(have_got_offset);
3168 typename elfcpp::Elf_types<size>::Elf_Addr value;
3169 value = target->got_plt_section()->address() + got_offset;
3170 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3174 case elfcpp::R_X86_64_GOTPCREL64:
3176 gold_assert(have_got_offset);
3177 typename elfcpp::Elf_types<size>::Elf_Addr value;
3178 value = target->got_plt_section()->address() + got_offset;
3179 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3183 case elfcpp::R_X86_64_COPY:
3184 case elfcpp::R_X86_64_GLOB_DAT:
3185 case elfcpp::R_X86_64_JUMP_SLOT:
3186 case elfcpp::R_X86_64_RELATIVE:
3187 case elfcpp::R_X86_64_IRELATIVE:
3188 // These are outstanding tls relocs, which are unexpected when linking
3189 case elfcpp::R_X86_64_TPOFF64:
3190 case elfcpp::R_X86_64_DTPMOD64:
3191 case elfcpp::R_X86_64_TLSDESC:
3192 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3193 _("unexpected reloc %u in object file"),
3197 // These are initial tls relocs, which are expected when linking
3198 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3199 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3200 case elfcpp::R_X86_64_TLSDESC_CALL:
3201 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3202 case elfcpp::R_X86_64_DTPOFF32:
3203 case elfcpp::R_X86_64_DTPOFF64:
3204 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3205 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3206 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3207 view, address, view_size);
3210 case elfcpp::R_X86_64_SIZE32:
3211 case elfcpp::R_X86_64_SIZE64:
3213 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3214 _("unsupported reloc %u"),
3222 // Perform a TLS relocation.
3226 Target_x86_64<size>::Relocate::relocate_tls(
3227 const Relocate_info<size, false>* relinfo,
3228 Target_x86_64<size>* target,
3230 const elfcpp::Rela<size, false>& rela,
3231 unsigned int r_type,
3232 const Sized_symbol<size>* gsym,
3233 const Symbol_value<size>* psymval,
3234 unsigned char* view,
3235 typename elfcpp::Elf_types<size>::Elf_Addr address,
3236 section_size_type view_size)
3238 Output_segment* tls_segment = relinfo->layout->tls_segment();
3240 const Sized_relobj_file<size, false>* object = relinfo->object;
3241 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3242 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3243 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3245 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3247 const bool is_final = (gsym == NULL
3248 ? !parameters->options().shared()
3249 : gsym->final_value_is_known());
3250 tls::Tls_optimization optimized_type
3251 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3254 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3255 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3257 // If this code sequence is used in a non-executable section,
3258 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3259 // on the assumption that it's being used by itself in a debug
3260 // section. Therefore, in the unlikely event that the code
3261 // sequence appears in a non-executable section, we simply
3262 // leave it unoptimized.
3263 optimized_type = tls::TLSOPT_NONE;
3265 if (optimized_type == tls::TLSOPT_TO_LE)
3267 if (tls_segment == NULL)
3269 gold_assert(parameters->errors()->error_count() > 0
3270 || issue_undefined_symbol_error(gsym));
3273 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3274 rela, r_type, value, view,
3280 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3281 ? GOT_TYPE_TLS_OFFSET
3282 : GOT_TYPE_TLS_PAIR);
3283 unsigned int got_offset;
3286 gold_assert(gsym->has_got_offset(got_type));
3287 got_offset = gsym->got_offset(got_type) - target->got_size();
3291 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3292 gold_assert(object->local_has_got_offset(r_sym, got_type));
3293 got_offset = (object->local_got_offset(r_sym, got_type)
3294 - target->got_size());
3296 if (optimized_type == tls::TLSOPT_TO_IE)
3298 value = target->got_plt_section()->address() + got_offset;
3299 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3300 value, view, address, view_size);
3303 else if (optimized_type == tls::TLSOPT_NONE)
3305 // Relocate the field with the offset of the pair of GOT
3307 value = target->got_plt_section()->address() + got_offset;
3308 Relocate_functions<size, false>::pcrela32(view, value, addend,
3313 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3314 _("unsupported reloc %u"), r_type);
3317 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3318 case elfcpp::R_X86_64_TLSDESC_CALL:
3319 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3321 // See above comment for R_X86_64_TLSGD.
3322 optimized_type = tls::TLSOPT_NONE;
3324 if (optimized_type == tls::TLSOPT_TO_LE)
3326 if (tls_segment == NULL)
3328 gold_assert(parameters->errors()->error_count() > 0
3329 || issue_undefined_symbol_error(gsym));
3332 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3333 rela, r_type, value, view,
3339 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3340 ? GOT_TYPE_TLS_OFFSET
3341 : GOT_TYPE_TLS_DESC);
3342 unsigned int got_offset = 0;
3343 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3344 && optimized_type == tls::TLSOPT_NONE)
3346 // We created GOT entries in the .got.tlsdesc portion of
3347 // the .got.plt section, but the offset stored in the
3348 // symbol is the offset within .got.tlsdesc.
3349 got_offset = (target->got_size()
3350 + target->got_plt_section()->data_size());
3354 gold_assert(gsym->has_got_offset(got_type));
3355 got_offset += gsym->got_offset(got_type) - target->got_size();
3359 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3360 gold_assert(object->local_has_got_offset(r_sym, got_type));
3361 got_offset += (object->local_got_offset(r_sym, got_type)
3362 - target->got_size());
3364 if (optimized_type == tls::TLSOPT_TO_IE)
3366 if (tls_segment == NULL)
3368 gold_assert(parameters->errors()->error_count() > 0
3369 || issue_undefined_symbol_error(gsym));
3372 value = target->got_plt_section()->address() + got_offset;
3373 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3374 rela, r_type, value, view, address,
3378 else if (optimized_type == tls::TLSOPT_NONE)
3380 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3382 // Relocate the field with the offset of the pair of GOT
3384 value = target->got_plt_section()->address() + got_offset;
3385 Relocate_functions<size, false>::pcrela32(view, value, addend,
3391 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3392 _("unsupported reloc %u"), r_type);
3395 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3396 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3398 // See above comment for R_X86_64_TLSGD.
3399 optimized_type = tls::TLSOPT_NONE;
3401 if (optimized_type == tls::TLSOPT_TO_LE)
3403 if (tls_segment == NULL)
3405 gold_assert(parameters->errors()->error_count() > 0
3406 || issue_undefined_symbol_error(gsym));
3409 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3410 value, view, view_size);
3413 else if (optimized_type == tls::TLSOPT_NONE)
3415 // Relocate the field with the offset of the GOT entry for
3416 // the module index.
3417 unsigned int got_offset;
3418 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3419 - target->got_size());
3420 value = target->got_plt_section()->address() + got_offset;
3421 Relocate_functions<size, false>::pcrela32(view, value, addend,
3425 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3426 _("unsupported reloc %u"), r_type);
3429 case elfcpp::R_X86_64_DTPOFF32:
3430 // This relocation type is used in debugging information.
3431 // In that case we need to not optimize the value. If the
3432 // section is not executable, then we assume we should not
3433 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3434 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3436 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3438 if (tls_segment == NULL)
3440 gold_assert(parameters->errors()->error_count() > 0
3441 || issue_undefined_symbol_error(gsym));
3444 value -= tls_segment->memsz();
3446 Relocate_functions<size, false>::rela32(view, value, addend);
3449 case elfcpp::R_X86_64_DTPOFF64:
3450 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3451 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3453 if (tls_segment == NULL)
3455 gold_assert(parameters->errors()->error_count() > 0
3456 || issue_undefined_symbol_error(gsym));
3459 value -= tls_segment->memsz();
3461 Relocate_functions<size, false>::rela64(view, value, addend);
3464 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3465 if (optimized_type == tls::TLSOPT_TO_LE)
3467 if (tls_segment == NULL)
3469 gold_assert(parameters->errors()->error_count() > 0
3470 || issue_undefined_symbol_error(gsym));
3473 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3475 r_type, value, view,
3479 else if (optimized_type == tls::TLSOPT_NONE)
3481 // Relocate the field with the offset of the GOT entry for
3482 // the tp-relative offset of the symbol.
3483 unsigned int got_offset;
3486 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3487 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3488 - target->got_size());
3492 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3493 gold_assert(object->local_has_got_offset(r_sym,
3494 GOT_TYPE_TLS_OFFSET));
3495 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3496 - target->got_size());
3498 value = target->got_plt_section()->address() + got_offset;
3499 Relocate_functions<size, false>::pcrela32(view, value, addend,
3503 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3504 _("unsupported reloc type %u"),
3508 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3509 if (tls_segment == NULL)
3511 gold_assert(parameters->errors()->error_count() > 0
3512 || issue_undefined_symbol_error(gsym));
3515 value -= tls_segment->memsz();
3516 Relocate_functions<size, false>::rela32(view, value, addend);
3521 // Do a relocation in which we convert a TLS General-Dynamic to an
3526 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3527 const Relocate_info<size, false>* relinfo,
3530 const elfcpp::Rela<size, false>& rela,
3532 typename elfcpp::Elf_types<size>::Elf_Addr value,
3533 unsigned char* view,
3534 typename elfcpp::Elf_types<size>::Elf_Addr address,
3535 section_size_type view_size)
3538 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3539 // .word 0x6666; rex64; call __tls_get_addr
3540 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3542 // leaq foo@tlsgd(%rip),%rdi;
3543 // .word 0x6666; rex64; call __tls_get_addr
3544 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3546 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3547 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3548 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3552 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3554 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3555 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3556 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3561 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3563 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3564 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
3565 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3569 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3570 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
3573 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3575 this->skip_call_tls_get_addr_ = true;
3578 // Do a relocation in which we convert a TLS General-Dynamic to a
3583 Target_x86_64<size>::Relocate::tls_gd_to_le(
3584 const Relocate_info<size, false>* relinfo,
3586 Output_segment* tls_segment,
3587 const elfcpp::Rela<size, false>& rela,
3589 typename elfcpp::Elf_types<size>::Elf_Addr value,
3590 unsigned char* view,
3591 section_size_type view_size)
3594 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3595 // .word 0x6666; rex64; call __tls_get_addr
3596 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3598 // leaq foo@tlsgd(%rip),%rdi;
3599 // .word 0x6666; rex64; call __tls_get_addr
3600 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
3602 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3603 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3604 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3608 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3610 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3611 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3612 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3617 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3619 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3620 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
3622 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3626 value -= tls_segment->memsz();
3627 Relocate_functions<size, false>::rela32(view + 8, value, 0);
3629 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3631 this->skip_call_tls_get_addr_ = true;
3634 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3638 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
3639 const Relocate_info<size, false>* relinfo,
3642 const elfcpp::Rela<size, false>& rela,
3643 unsigned int r_type,
3644 typename elfcpp::Elf_types<size>::Elf_Addr value,
3645 unsigned char* view,
3646 typename elfcpp::Elf_types<size>::Elf_Addr address,
3647 section_size_type view_size)
3649 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3651 // leaq foo@tlsdesc(%rip), %rax
3652 // ==> movq foo@gottpoff(%rip), %rax
3653 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3654 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3655 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3656 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3658 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3659 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3663 // call *foo@tlscall(%rax)
3665 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3666 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3667 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3668 view[0] == 0xff && view[1] == 0x10);
3674 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3678 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
3679 const Relocate_info<size, false>* relinfo,
3681 Output_segment* tls_segment,
3682 const elfcpp::Rela<size, false>& rela,
3683 unsigned int r_type,
3684 typename elfcpp::Elf_types<size>::Elf_Addr value,
3685 unsigned char* view,
3686 section_size_type view_size)
3688 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3690 // leaq foo@tlsdesc(%rip), %rax
3691 // ==> movq foo@tpoff, %rax
3692 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3693 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3694 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3695 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3698 value -= tls_segment->memsz();
3699 Relocate_functions<size, false>::rela32(view, value, 0);
3703 // call *foo@tlscall(%rax)
3705 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3706 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3707 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3708 view[0] == 0xff && view[1] == 0x10);
3716 Target_x86_64<size>::Relocate::tls_ld_to_le(
3717 const Relocate_info<size, false>* relinfo,
3720 const elfcpp::Rela<size, false>& rela,
3722 typename elfcpp::Elf_types<size>::Elf_Addr,
3723 unsigned char* view,
3724 section_size_type view_size)
3726 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3727 // ... leq foo@dtpoff(%rax),%reg
3728 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3730 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3731 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3733 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3734 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3736 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3738 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3740 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3742 this->skip_call_tls_get_addr_ = true;
3745 // Do a relocation in which we convert a TLS Initial-Exec to a
3750 Target_x86_64<size>::Relocate::tls_ie_to_le(
3751 const Relocate_info<size, false>* relinfo,
3753 Output_segment* tls_segment,
3754 const elfcpp::Rela<size, false>& rela,
3756 typename elfcpp::Elf_types<size>::Elf_Addr value,
3757 unsigned char* view,
3758 section_size_type view_size)
3760 // We need to examine the opcodes to figure out which instruction we
3763 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3764 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3766 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3767 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3769 unsigned char op1 = view[-3];
3770 unsigned char op2 = view[-2];
3771 unsigned char op3 = view[-1];
3772 unsigned char reg = op3 >> 3;
3780 view[-1] = 0xc0 | reg;
3784 // Special handling for %rsp.
3788 view[-1] = 0xc0 | reg;
3796 view[-1] = 0x80 | reg | (reg << 3);
3799 value -= tls_segment->memsz();
3800 Relocate_functions<size, false>::rela32(view, value, 0);
3803 // Relocate section data.
3807 Target_x86_64<size>::relocate_section(
3808 const Relocate_info<size, false>* relinfo,
3809 unsigned int sh_type,
3810 const unsigned char* prelocs,
3812 Output_section* output_section,
3813 bool needs_special_offset_handling,
3814 unsigned char* view,
3815 typename elfcpp::Elf_types<size>::Elf_Addr address,
3816 section_size_type view_size,
3817 const Reloc_symbol_changes* reloc_symbol_changes)
3819 gold_assert(sh_type == elfcpp::SHT_RELA);
3821 gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3822 typename Target_x86_64<size>::Relocate>(
3828 needs_special_offset_handling,
3832 reloc_symbol_changes);
3835 // Apply an incremental relocation. Incremental relocations always refer
3836 // to global symbols.
3840 Target_x86_64<size>::apply_relocation(
3841 const Relocate_info<size, false>* relinfo,
3842 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
3843 unsigned int r_type,
3844 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
3846 unsigned char* view,
3847 typename elfcpp::Elf_types<size>::Elf_Addr address,
3848 section_size_type view_size)
3850 gold::apply_relocation<size, false, Target_x86_64<size>,
3851 typename Target_x86_64<size>::Relocate>(
3863 // Return the size of a relocation while scanning during a relocatable
3868 Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
3869 unsigned int r_type,
3874 case elfcpp::R_X86_64_NONE:
3875 case elfcpp::R_X86_64_GNU_VTINHERIT:
3876 case elfcpp::R_X86_64_GNU_VTENTRY:
3877 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3878 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3879 case elfcpp::R_X86_64_TLSDESC_CALL:
3880 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3881 case elfcpp::R_X86_64_DTPOFF32:
3882 case elfcpp::R_X86_64_DTPOFF64:
3883 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3884 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3887 case elfcpp::R_X86_64_64:
3888 case elfcpp::R_X86_64_PC64:
3889 case elfcpp::R_X86_64_GOTOFF64:
3890 case elfcpp::R_X86_64_GOTPC64:
3891 case elfcpp::R_X86_64_PLTOFF64:
3892 case elfcpp::R_X86_64_GOT64:
3893 case elfcpp::R_X86_64_GOTPCREL64:
3894 case elfcpp::R_X86_64_GOTPCREL:
3895 case elfcpp::R_X86_64_GOTPLT64:
3898 case elfcpp::R_X86_64_32:
3899 case elfcpp::R_X86_64_32S:
3900 case elfcpp::R_X86_64_PC32:
3901 case elfcpp::R_X86_64_PLT32:
3902 case elfcpp::R_X86_64_GOTPC32:
3903 case elfcpp::R_X86_64_GOT32:
3906 case elfcpp::R_X86_64_16:
3907 case elfcpp::R_X86_64_PC16:
3910 case elfcpp::R_X86_64_8:
3911 case elfcpp::R_X86_64_PC8:
3914 case elfcpp::R_X86_64_COPY:
3915 case elfcpp::R_X86_64_GLOB_DAT:
3916 case elfcpp::R_X86_64_JUMP_SLOT:
3917 case elfcpp::R_X86_64_RELATIVE:
3918 case elfcpp::R_X86_64_IRELATIVE:
3919 // These are outstanding tls relocs, which are unexpected when linking
3920 case elfcpp::R_X86_64_TPOFF64:
3921 case elfcpp::R_X86_64_DTPMOD64:
3922 case elfcpp::R_X86_64_TLSDESC:
3923 object->error(_("unexpected reloc %u in object file"), r_type);
3926 case elfcpp::R_X86_64_SIZE32:
3927 case elfcpp::R_X86_64_SIZE64:
3929 object->error(_("unsupported reloc %u against local symbol"), r_type);
3934 // Scan the relocs during a relocatable link.
3938 Target_x86_64<size>::scan_relocatable_relocs(
3939 Symbol_table* symtab,
3941 Sized_relobj_file<size, false>* object,
3942 unsigned int data_shndx,
3943 unsigned int sh_type,
3944 const unsigned char* prelocs,
3946 Output_section* output_section,
3947 bool needs_special_offset_handling,
3948 size_t local_symbol_count,
3949 const unsigned char* plocal_symbols,
3950 Relocatable_relocs* rr)
3952 gold_assert(sh_type == elfcpp::SHT_RELA);
3954 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3955 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3957 gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
3958 Scan_relocatable_relocs>(
3966 needs_special_offset_handling,
3972 // Relocate a section during a relocatable link.
3976 Target_x86_64<size>::relocate_for_relocatable(
3977 const Relocate_info<size, false>* relinfo,
3978 unsigned int sh_type,
3979 const unsigned char* prelocs,
3981 Output_section* output_section,
3982 off_t offset_in_output_section,
3983 const Relocatable_relocs* rr,
3984 unsigned char* view,
3985 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3986 section_size_type view_size,
3987 unsigned char* reloc_view,
3988 section_size_type reloc_view_size)
3990 gold_assert(sh_type == elfcpp::SHT_RELA);
3992 gold::relocate_for_relocatable<size, false, elfcpp::SHT_RELA>(
3997 offset_in_output_section,
4006 // Return the value to use for a dynamic which requires special
4007 // treatment. This is how we support equality comparisons of function
4008 // pointers across shared library boundaries, as described in the
4009 // processor specific ABI supplement.
4013 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4015 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4016 return this->plt_address_for_global(gsym) + gsym->plt_offset();
4019 // Return a string used to fill a code section with nops to take up
4020 // the specified length.
4024 Target_x86_64<size>::do_code_fill(section_size_type length) const
4028 // Build a jmpq instruction to skip over the bytes.
4029 unsigned char jmp[5];
4031 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4032 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4033 + std::string(length - 5, static_cast<char>(0x90)));
4036 // Nop sequences of various lengths.
4037 const char nop1[1] = { '\x90' }; // nop
4038 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4039 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4040 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4042 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4044 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4045 '\x44', '\x00', '\x00' };
4046 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4047 '\x00', '\x00', '\x00',
4049 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4050 '\x00', '\x00', '\x00',
4052 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4053 '\x84', '\x00', '\x00',
4054 '\x00', '\x00', '\x00' };
4055 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4056 '\x1f', '\x84', '\x00',
4057 '\x00', '\x00', '\x00',
4059 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4060 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4061 '\x00', '\x00', '\x00',
4063 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4064 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4065 '\x84', '\x00', '\x00',
4066 '\x00', '\x00', '\x00' };
4067 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4068 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4069 '\x1f', '\x84', '\x00',
4070 '\x00', '\x00', '\x00',
4072 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4073 '\x66', '\x66', '\x2e', // data16
4074 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4075 '\x00', '\x00', '\x00',
4077 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4078 '\x66', '\x66', '\x66', // data16; data16
4079 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4080 '\x84', '\x00', '\x00',
4081 '\x00', '\x00', '\x00' };
4083 const char* nops[16] = {
4085 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4086 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4089 return std::string(nops[length], length);
4092 // Return the addend to use for a target specific relocation. The
4093 // only target specific relocation is R_X86_64_TLSDESC for a local
4094 // symbol. We want to set the addend is the offset of the local
4095 // symbol in the TLS segment.
4099 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4102 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4103 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4104 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4105 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4106 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4107 gold_assert(psymval->is_tls_symbol());
4108 // The value of a TLS symbol is the offset in the TLS segment.
4109 return psymval->value(ti.object, 0);
4112 // Return the value to use for the base of a DW_EH_PE_datarel offset
4113 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4114 // assembler can not write out the difference between two labels in
4115 // different sections, so instead of using a pc-relative value they
4116 // use an offset from the GOT.
4120 Target_x86_64<size>::do_ehframe_datarel_base() const
4122 gold_assert(this->global_offset_table_ != NULL);
4123 Symbol* sym = this->global_offset_table_;
4124 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4125 return ssym->value();
4128 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4129 // compiled with -fsplit-stack. The function calls non-split-stack
4130 // code. We have to change the function so that it always ensures
4131 // that it has enough stack space to run some random function.
4135 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4136 section_offset_type fnoffset,
4137 section_size_type fnsize,
4138 unsigned char* view,
4139 section_size_type view_size,
4141 std::string* to) const
4143 // The function starts with a comparison of the stack pointer and a
4144 // field in the TCB. This is followed by a jump.
4147 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
4150 // We will call __morestack if the carry flag is set after this
4151 // comparison. We turn the comparison into an stc instruction
4153 view[fnoffset] = '\xf9';
4154 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
4156 // lea NN(%rsp),%r10
4157 // lea NN(%rsp),%r11
4158 else if ((this->match_view(view, view_size, fnoffset,
4159 "\x4c\x8d\x94\x24", 4)
4160 || this->match_view(view, view_size, fnoffset,
4161 "\x4c\x8d\x9c\x24", 4))
4164 // This is loading an offset from the stack pointer for a
4165 // comparison. The offset is negative, so we decrease the
4166 // offset by the amount of space we need for the stack. This
4167 // means we will avoid calling __morestack if there happens to
4168 // be plenty of space on the stack already.
4169 unsigned char* pval = view + fnoffset + 4;
4170 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4171 val -= parameters->options().split_stack_adjust_size();
4172 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4176 if (!object->has_no_split_stack())
4177 object->error(_("failed to match split-stack sequence at "
4178 "section %u offset %0zx"),
4179 shndx, static_cast<size_t>(fnoffset));
4183 // We have to change the function so that it calls
4184 // __morestack_non_split instead of __morestack. The former will
4185 // allocate additional stack space.
4186 *from = "__morestack";
4187 *to = "__morestack_non_split";
4190 // The selector for x86_64 object files.
4193 class Target_selector_x86_64 : public Target_selector_freebsd
4196 Target_selector_x86_64()
4197 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4199 ? "elf64-x86-64" : "elf32-x86-64"),
4201 ? "elf64-x86-64-freebsd"
4202 : "elf32-x86-64-freebsd"),
4203 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4207 do_instantiate_target()
4208 { return new Target_x86_64<size>(); }
4212 Target_selector_x86_64<64> target_selector_x86_64;
4213 Target_selector_x86_64<32> target_selector_x32;
4215 } // End anonymous namespace.