1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64 : public Output_section_data_build
56 Output_data_got_plt_x86_64(Layout* layout)
57 : Output_section_data_build(8),
61 Output_data_got_plt_x86_64(Layout* layout, off_t data_size)
62 : Output_section_data_build(data_size, 8),
67 // Write out the PLT data.
69 do_write(Output_file*);
71 // Write to a map file.
73 do_print_to_mapfile(Mapfile* mapfile) const
74 { mapfile->print_output_data(this, "** GOT PLT"); }
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
88 class Output_data_plt_x86_64 : public Output_section_data
91 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
93 Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
94 Output_data_got<64, false>* got,
95 Output_data_got_plt_x86_64* got_plt,
96 Output_data_space* got_irelative)
97 : Output_section_data(addralign), tlsdesc_rel_(NULL),
98 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
99 got_irelative_(got_irelative), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout); }
103 Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
104 Output_data_got<64, false>* got,
105 Output_data_got_plt_x86_64* got_plt,
106 Output_data_space* got_irelative,
107 unsigned int plt_count)
108 : Output_section_data((plt_count + 1) * plt_entry_size,
109 plt_entry_size, false),
110 tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
111 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
116 // Initialize the free list and reserve the first entry.
117 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
118 this->free_list_.remove(0, plt_entry_size);
121 // Initialize the PLT section.
123 init(Layout* layout);
125 // Add an entry to the PLT.
127 add_entry(Symbol_table*, Layout*, Symbol* gsym);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
131 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
132 Sized_relobj_file<size, false>* relobj,
133 unsigned int local_sym_index);
135 // Add the relocation for a PLT entry.
137 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
138 unsigned int got_offset);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
142 reserve_tlsdesc_entry(unsigned int got_offset)
143 { this->tlsdesc_got_offset_ = got_offset; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_ != -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
157 get_tlsdesc_plt_offset() const
159 return ((this->count_ + this->irelative_count_ + 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
166 { return this->rel_; }
168 // Return where the TLSDESC relocations should go.
170 rela_tlsdesc(Layout*);
172 // Return where the IRELATIVE relocations should go in the PLT
175 rela_irelative(Symbol_table*, Layout*);
177 // Return whether we created a section for IRELATIVE relocations.
179 has_irelative_section() const
180 { return this->irelative_rel_ != NULL; }
182 // Get count of regular PLT entries.
184 regular_count() const
185 { return this->count_; }
187 // Return the total number of PLT entries.
190 { return this->count_ + this->irelative_count_; }
192 // Return the offset of the first non-reserved PLT entry.
194 first_plt_entry_offset()
195 { return this->get_plt_entry_size(); }
197 // Return the size of a PLT entry.
199 get_plt_entry_size() const
200 { return this->do_get_plt_entry_size(); }
202 // Reserve a slot in the PLT for an existing symbol in an incremental update.
204 reserve_slot(unsigned int plt_index)
206 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
207 (plt_index + 2) * this->get_plt_entry_size());
210 // Return the PLT address to use for a global symbol.
212 address_for_global(const Symbol* sym)
213 { return do_address_for_global(sym); }
215 // Return the PLT address to use for a local symbol.
217 address_for_local(const Relobj* obj, unsigned int symndx)
218 { return do_address_for_local(obj, symndx); }
220 // Add .eh_frame information for the PLT.
222 add_eh_frame(Layout* layout)
223 { this->do_add_eh_frame(layout); }
226 Output_data_got<64, false>*
228 { return this->got_; }
230 Output_data_got_plt_x86_64*
232 { return this->got_plt_; }
235 got_irelative() const
236 { return this->got_irelative_; }
238 // Fill in the first PLT entry.
240 fill_first_plt_entry(unsigned char* pov,
241 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
242 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
243 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
245 // Fill in a normal PLT entry. Returns the offset into the entry that
246 // should be the initial GOT slot value.
248 fill_plt_entry(unsigned char* pov,
249 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
250 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
251 unsigned int got_offset,
252 unsigned int plt_offset,
253 unsigned int plt_index)
255 return this->do_fill_plt_entry(pov, got_address, plt_address,
256 got_offset, plt_offset, plt_index);
259 // Fill in the reserved TLSDESC PLT entry.
261 fill_tlsdesc_entry(unsigned char* pov,
262 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
263 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
264 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
265 unsigned int tlsdesc_got_offset,
266 unsigned int plt_offset)
268 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
269 tlsdesc_got_offset, plt_offset);
273 do_get_plt_entry_size() const = 0;
276 do_fill_first_plt_entry(unsigned char* pov,
277 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
278 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
282 do_fill_plt_entry(unsigned char* pov,
283 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
284 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
285 unsigned int got_offset,
286 unsigned int plt_offset,
287 unsigned int plt_index) = 0;
290 do_fill_tlsdesc_entry(unsigned char* pov,
291 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
292 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
293 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
294 unsigned int tlsdesc_got_offset,
295 unsigned int plt_offset) = 0;
297 // Return the PLT address to use for a global symbol.
299 do_address_for_global(const Symbol* sym);
301 // Return the PLT address to use for a local symbol.
303 do_address_for_local(const Relobj* obj, unsigned int symndx);
306 do_add_eh_frame(Layout* layout) = 0;
309 do_adjust_output_section(Output_section* os);
311 // Write to a map file.
313 do_print_to_mapfile(Mapfile* mapfile) const
314 { mapfile->print_output_data(this, _("** PLT")); }
316 // The CIE of the .eh_frame unwind information for the PLT.
317 static const int plt_eh_frame_cie_size = 16;
318 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
321 // Set the final size.
323 set_final_data_size();
325 // Write out the PLT data.
327 do_write(Output_file*);
329 // The reloc section.
331 // The TLSDESC relocs, if necessary. These must follow the regular
333 Reloc_section* tlsdesc_rel_;
334 // The IRELATIVE relocs, if necessary. These must follow the
335 // regular PLT relocations and the TLSDESC relocations.
336 Reloc_section* irelative_rel_;
338 Output_data_got<64, false>* got_;
339 // The .got.plt section.
340 Output_data_got_plt_x86_64* got_plt_;
341 // The part of the .got.plt section used for IRELATIVE relocs.
342 Output_data_space* got_irelative_;
343 // The number of PLT entries.
345 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
346 // follow the regular PLT entries.
347 unsigned int irelative_count_;
348 // Offset of the reserved TLSDESC_GOT entry when needed.
349 unsigned int tlsdesc_got_offset_;
350 // List of available regions within the section, for incremental
352 Free_list free_list_;
356 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
359 Output_data_plt_x86_64_standard(Layout* layout,
360 Output_data_got<64, false>* got,
361 Output_data_got_plt_x86_64* got_plt,
362 Output_data_space* got_irelative)
363 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
364 got, got_plt, got_irelative)
367 Output_data_plt_x86_64_standard(Layout* layout,
368 Output_data_got<64, false>* got,
369 Output_data_got_plt_x86_64* got_plt,
370 Output_data_space* got_irelative,
371 unsigned int plt_count)
372 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
373 got, got_plt, got_irelative,
379 do_get_plt_entry_size() const
380 { return plt_entry_size; }
383 do_add_eh_frame(Layout* layout)
385 layout->add_eh_frame_for_plt(this,
386 this->plt_eh_frame_cie,
387 this->plt_eh_frame_cie_size,
389 plt_eh_frame_fde_size);
393 do_fill_first_plt_entry(unsigned char* pov,
394 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
395 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
398 do_fill_plt_entry(unsigned char* pov,
399 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
400 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
401 unsigned int got_offset,
402 unsigned int plt_offset,
403 unsigned int plt_index);
406 do_fill_tlsdesc_entry(unsigned char* pov,
407 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
408 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
409 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
410 unsigned int tlsdesc_got_offset,
411 unsigned int plt_offset);
414 // The size of an entry in the PLT.
415 static const int plt_entry_size = 16;
417 // The first entry in the PLT.
418 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
419 // procedure linkage table for both programs and shared objects."
420 static const unsigned char first_plt_entry[plt_entry_size];
422 // Other entries in the PLT for an executable.
423 static const unsigned char plt_entry[plt_entry_size];
425 // The reserved TLSDESC entry in the PLT for an executable.
426 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
428 // The .eh_frame unwind information for the PLT.
429 static const int plt_eh_frame_fde_size = 32;
430 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
433 class Output_data_plt_x86_64_bnd : public Output_data_plt_x86_64<64>
436 Output_data_plt_x86_64_bnd(Layout* layout,
437 Output_data_got<64, false>* got,
438 Output_data_got_plt_x86_64* got_plt,
439 Output_data_space* got_irelative)
440 : Output_data_plt_x86_64<64>(layout, plt_entry_size,
441 got, got_plt, got_irelative),
445 Output_data_plt_x86_64_bnd(Layout* layout,
446 Output_data_got<64, false>* got,
447 Output_data_got_plt_x86_64* got_plt,
448 Output_data_space* got_irelative,
449 unsigned int plt_count)
450 : Output_data_plt_x86_64<64>(layout, plt_entry_size,
451 got, got_plt, got_irelative,
458 do_get_plt_entry_size() const
459 { return plt_entry_size; }
461 // Return the PLT address to use for a global symbol.
463 do_address_for_global(const Symbol*);
465 // Return the PLT address to use for a local symbol.
467 do_address_for_local(const Relobj*, unsigned int symndx);
470 do_add_eh_frame(Layout* layout)
472 layout->add_eh_frame_for_plt(this,
473 this->plt_eh_frame_cie,
474 this->plt_eh_frame_cie_size,
476 plt_eh_frame_fde_size);
480 do_fill_first_plt_entry(unsigned char* pov,
481 typename elfcpp::Elf_types<64>::Elf_Addr got_addr,
482 typename elfcpp::Elf_types<64>::Elf_Addr plt_addr);
485 do_fill_plt_entry(unsigned char* pov,
486 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
487 typename elfcpp::Elf_types<64>::Elf_Addr plt_address,
488 unsigned int got_offset,
489 unsigned int plt_offset,
490 unsigned int plt_index);
493 do_fill_tlsdesc_entry(unsigned char* pov,
494 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
495 typename elfcpp::Elf_types<64>::Elf_Addr plt_address,
496 typename elfcpp::Elf_types<64>::Elf_Addr got_base,
497 unsigned int tlsdesc_got_offset,
498 unsigned int plt_offset);
501 fill_aplt_entry(unsigned char* pov,
502 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
503 typename elfcpp::Elf_types<64>::Elf_Addr plt_address,
504 unsigned int got_offset,
505 unsigned int plt_offset,
506 unsigned int plt_index);
509 // Set the final size.
511 set_final_data_size();
513 // Write out the BND PLT data.
515 do_write(Output_file*);
517 // Offset of the Additional PLT (if using -z bndplt).
518 unsigned int aplt_offset_;
520 // The size of an entry in the PLT.
521 static const int plt_entry_size = 16;
523 // The size of an entry in the additional PLT.
524 static const int aplt_entry_size = 8;
526 // The first entry in the PLT.
527 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
528 // procedure linkage table for both programs and shared objects."
529 static const unsigned char first_plt_entry[plt_entry_size];
531 // Other entries in the PLT for an executable.
532 static const unsigned char plt_entry[plt_entry_size];
534 // Entries in the additional PLT.
535 static const unsigned char aplt_entry[aplt_entry_size];
537 // The reserved TLSDESC entry in the PLT for an executable.
538 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
540 // The .eh_frame unwind information for the PLT.
541 static const int plt_eh_frame_fde_size = 32;
542 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
549 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
550 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
554 operator[](size_t offset)
556 if (this->view_ == NULL)
557 this->view_ = this->object_->section_contents(this->data_shndx_,
560 if (offset >= this->view_size_)
562 return this->view_[offset];
566 Sized_relobj_file<size, false>* object_;
567 unsigned int data_shndx_;
568 const unsigned char* view_;
569 section_size_type view_size_;
572 // The x86_64 target class.
574 // http://www.x86-64.org/documentation/abi.pdf
575 // TLS info comes from
576 // http://people.redhat.com/drepper/tls.pdf
577 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
580 class Target_x86_64 : public Sized_target<size, false>
583 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
584 // uses only Elf64_Rela relocation entries with explicit addends."
585 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
587 Target_x86_64(const Target::Target_info* info = &x86_64_info)
588 : Sized_target<size, false>(info),
589 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
590 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
591 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
592 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
593 tls_base_symbol_defined_(false)
596 // Hook for a new output section.
598 do_new_output_section(Output_section*) const;
600 // Scan the relocations to look for symbol adjustments.
602 gc_process_relocs(Symbol_table* symtab,
604 Sized_relobj_file<size, false>* object,
605 unsigned int data_shndx,
606 unsigned int sh_type,
607 const unsigned char* prelocs,
609 Output_section* output_section,
610 bool needs_special_offset_handling,
611 size_t local_symbol_count,
612 const unsigned char* plocal_symbols);
614 // Scan the relocations to look for symbol adjustments.
616 scan_relocs(Symbol_table* symtab,
618 Sized_relobj_file<size, false>* object,
619 unsigned int data_shndx,
620 unsigned int sh_type,
621 const unsigned char* prelocs,
623 Output_section* output_section,
624 bool needs_special_offset_handling,
625 size_t local_symbol_count,
626 const unsigned char* plocal_symbols);
628 // Finalize the sections.
630 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
632 // Return the value to use for a dynamic which requires special
635 do_dynsym_value(const Symbol*) const;
637 // Relocate a section.
639 relocate_section(const Relocate_info<size, false>*,
640 unsigned int sh_type,
641 const unsigned char* prelocs,
643 Output_section* output_section,
644 bool needs_special_offset_handling,
646 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
647 section_size_type view_size,
648 const Reloc_symbol_changes*);
650 // Scan the relocs during a relocatable link.
652 scan_relocatable_relocs(Symbol_table* symtab,
654 Sized_relobj_file<size, false>* object,
655 unsigned int data_shndx,
656 unsigned int sh_type,
657 const unsigned char* prelocs,
659 Output_section* output_section,
660 bool needs_special_offset_handling,
661 size_t local_symbol_count,
662 const unsigned char* plocal_symbols,
663 Relocatable_relocs*);
665 // Scan the relocs for --emit-relocs.
667 emit_relocs_scan(Symbol_table* symtab,
669 Sized_relobj_file<size, false>* object,
670 unsigned int data_shndx,
671 unsigned int sh_type,
672 const unsigned char* prelocs,
674 Output_section* output_section,
675 bool needs_special_offset_handling,
676 size_t local_symbol_count,
677 const unsigned char* plocal_syms,
678 Relocatable_relocs* rr);
680 // Emit relocations for a section.
683 const Relocate_info<size, false>*,
684 unsigned int sh_type,
685 const unsigned char* prelocs,
687 Output_section* output_section,
688 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
690 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
691 section_size_type view_size,
692 unsigned char* reloc_view,
693 section_size_type reloc_view_size);
695 // Return a string used to fill a code section with nops.
697 do_code_fill(section_size_type length) const;
699 // Return whether SYM is defined by the ABI.
701 do_is_defined_by_abi(const Symbol* sym) const
702 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
704 // Return the symbol index to use for a target specific relocation.
705 // The only target specific relocation is R_X86_64_TLSDESC for a
706 // local symbol, which is an absolute reloc.
708 do_reloc_symbol_index(void*, unsigned int r_type) const
710 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
714 // Return the addend to use for a target specific relocation.
716 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
718 // Return the PLT section.
720 do_plt_address_for_global(const Symbol* gsym) const
721 { return this->plt_section()->address_for_global(gsym); }
724 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
725 { return this->plt_section()->address_for_local(relobj, symndx); }
727 // This function should be defined in targets that can use relocation
728 // types to determine (implemented in local_reloc_may_be_function_pointer
729 // and global_reloc_may_be_function_pointer)
730 // if a function's pointer is taken. ICF uses this in safe mode to only
731 // fold those functions whose pointer is defintely not taken. For x86_64
732 // pie binaries, safe ICF cannot be done by looking at relocation types.
734 do_can_check_for_function_pointers() const
735 { return !parameters->options().pie(); }
737 // Return the base for a DW_EH_PE_datarel encoding.
739 do_ehframe_datarel_base() const;
741 // Adjust -fsplit-stack code which calls non-split-stack code.
743 do_calls_non_split(Relobj* object, unsigned int shndx,
744 section_offset_type fnoffset, section_size_type fnsize,
745 const unsigned char* prelocs, size_t reloc_count,
746 unsigned char* view, section_size_type view_size,
747 std::string* from, std::string* to) const;
749 // Return the size of the GOT section.
753 gold_assert(this->got_ != NULL);
754 return this->got_->data_size();
757 // Return the number of entries in the GOT.
759 got_entry_count() const
761 if (this->got_ == NULL)
763 return this->got_size() / 8;
766 // Return the number of entries in the PLT.
768 plt_entry_count() const;
770 // Return the offset of the first non-reserved PLT entry.
772 first_plt_entry_offset() const;
774 // Return the size of each PLT entry.
776 plt_entry_size() const;
778 // Return the size of each GOT entry.
780 got_entry_size() const
783 // Create the GOT section for an incremental update.
784 Output_data_got_base*
785 init_got_plt_for_update(Symbol_table* symtab,
787 unsigned int got_count,
788 unsigned int plt_count);
790 // Reserve a GOT entry for a local symbol, and regenerate any
791 // necessary dynamic relocations.
793 reserve_local_got_entry(unsigned int got_index,
794 Sized_relobj<size, false>* obj,
796 unsigned int got_type);
798 // Reserve a GOT entry for a global symbol, and regenerate any
799 // necessary dynamic relocations.
801 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
802 unsigned int got_type);
804 // Register an existing PLT entry for a global symbol.
806 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
809 // Force a COPY relocation for a given symbol.
811 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
813 // Apply an incremental relocation.
815 apply_relocation(const Relocate_info<size, false>* relinfo,
816 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
818 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
821 typename elfcpp::Elf_types<size>::Elf_Addr address,
822 section_size_type view_size);
824 // Add a new reloc argument, returning the index in the vector.
826 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
828 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
829 return this->tlsdesc_reloc_info_.size() - 1;
832 Output_data_plt_x86_64<size>*
833 make_data_plt(Layout* layout,
834 Output_data_got<64, false>* got,
835 Output_data_got_plt_x86_64* got_plt,
836 Output_data_space* got_irelative)
838 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
841 Output_data_plt_x86_64<size>*
842 make_data_plt(Layout* layout,
843 Output_data_got<64, false>* got,
844 Output_data_got_plt_x86_64* got_plt,
845 Output_data_space* got_irelative,
846 unsigned int plt_count)
848 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
852 virtual Output_data_plt_x86_64<size>*
853 do_make_data_plt(Layout* layout,
854 Output_data_got<64, false>* got,
855 Output_data_got_plt_x86_64* got_plt,
856 Output_data_space* got_irelative);
858 virtual Output_data_plt_x86_64<size>*
859 do_make_data_plt(Layout* layout,
860 Output_data_got<64, false>* got,
861 Output_data_got_plt_x86_64* got_plt,
862 Output_data_space* got_irelative,
863 unsigned int plt_count);
866 // The class which scans relocations.
871 : issued_non_pic_error_(false)
875 get_reference_flags(unsigned int r_type);
878 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
879 Sized_relobj_file<size, false>* object,
880 unsigned int data_shndx,
881 Output_section* output_section,
882 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
883 const elfcpp::Sym<size, false>& lsym,
887 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
888 Sized_relobj_file<size, false>* object,
889 unsigned int data_shndx,
890 Output_section* output_section,
891 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
895 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
896 Target_x86_64* target,
897 Sized_relobj_file<size, false>* object,
898 unsigned int data_shndx,
899 Output_section* output_section,
900 const elfcpp::Rela<size, false>& reloc,
902 const elfcpp::Sym<size, false>& lsym);
905 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
906 Target_x86_64* target,
907 Sized_relobj_file<size, false>* object,
908 unsigned int data_shndx,
909 Output_section* output_section,
910 const elfcpp::Rela<size, false>& reloc,
916 unsupported_reloc_local(Sized_relobj_file<size, false>*,
917 unsigned int r_type);
920 unsupported_reloc_global(Sized_relobj_file<size, false>*,
921 unsigned int r_type, Symbol*);
924 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
927 possible_function_pointer_reloc(unsigned int r_type);
930 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
931 unsigned int r_type);
933 // Whether we have issued an error about a non-PIC compilation.
934 bool issued_non_pic_error_;
937 // The class which implements relocation.
942 : skip_call_tls_get_addr_(false)
947 if (this->skip_call_tls_get_addr_)
949 // FIXME: This needs to specify the location somehow.
950 gold_error(_("missing expected TLS relocation"));
954 // Do a relocation. Return false if the caller should not issue
955 // any warnings about this relocation.
957 relocate(const Relocate_info<size, false>*, unsigned int,
958 Target_x86_64*, Output_section*, size_t, const unsigned char*,
959 const Sized_symbol<size>*, const Symbol_value<size>*,
960 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
964 // Do a TLS relocation.
966 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
967 size_t relnum, const elfcpp::Rela<size, false>&,
968 unsigned int r_type, const Sized_symbol<size>*,
969 const Symbol_value<size>*,
970 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
973 // Do a TLS General-Dynamic to Initial-Exec transition.
975 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
976 const elfcpp::Rela<size, false>&, unsigned int r_type,
977 typename elfcpp::Elf_types<size>::Elf_Addr value,
979 typename elfcpp::Elf_types<size>::Elf_Addr,
980 section_size_type view_size);
982 // Do a TLS General-Dynamic to Local-Exec transition.
984 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
985 Output_segment* tls_segment,
986 const elfcpp::Rela<size, false>&, unsigned int r_type,
987 typename elfcpp::Elf_types<size>::Elf_Addr value,
989 section_size_type view_size);
991 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
993 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
994 const elfcpp::Rela<size, false>&, unsigned int r_type,
995 typename elfcpp::Elf_types<size>::Elf_Addr value,
997 typename elfcpp::Elf_types<size>::Elf_Addr,
998 section_size_type view_size);
1000 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1002 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1003 Output_segment* tls_segment,
1004 const elfcpp::Rela<size, false>&, unsigned int r_type,
1005 typename elfcpp::Elf_types<size>::Elf_Addr value,
1006 unsigned char* view,
1007 section_size_type view_size);
1009 // Do a TLS Local-Dynamic to Local-Exec transition.
1011 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
1012 Output_segment* tls_segment,
1013 const elfcpp::Rela<size, false>&, unsigned int r_type,
1014 typename elfcpp::Elf_types<size>::Elf_Addr value,
1015 unsigned char* view,
1016 section_size_type view_size);
1018 // Do a TLS Initial-Exec to Local-Exec transition.
1020 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
1021 Output_segment* tls_segment,
1022 const elfcpp::Rela<size, false>&, unsigned int r_type,
1023 typename elfcpp::Elf_types<size>::Elf_Addr value,
1024 unsigned char* view,
1025 section_size_type view_size);
1027 // This is set if we should skip the next reloc, which should be a
1028 // PLT32 reloc against ___tls_get_addr.
1029 bool skip_call_tls_get_addr_;
1032 // Check if relocation against this symbol is a candidate for
1034 // mov foo@GOTPCREL(%rip), %reg
1035 // to lea foo(%rip), %reg.
1036 template<class View_type>
1038 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
1039 size_t r_offset, View_type* view)
1041 gold_assert(gsym != NULL);
1042 // We cannot do the conversion unless it's one of these relocations.
1043 if (r_type != elfcpp::R_X86_64_GOTPCREL
1044 && r_type != elfcpp::R_X86_64_GOTPCRELX
1045 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
1047 // We cannot convert references to IFUNC symbols, or to symbols that
1048 // are not local to the current module.
1049 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1050 || gsym->is_undefined ()
1051 || gsym->is_from_dynobj()
1052 || gsym->is_preemptible())
1054 // If we are building a shared object and the symbol is protected, we may
1055 // need to go through the GOT.
1056 if (parameters->options().shared()
1057 && gsym->visibility() == elfcpp::STV_PROTECTED)
1059 // We cannot convert references to the _DYNAMIC symbol.
1060 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1062 // Check for a MOV opcode.
1063 return (*view)[r_offset - 2] == 0x8b;
1067 // callq *foo@GOTPCRELX(%rip) to
1069 // and jmpq *foo@GOTPCRELX(%rip) to
1072 template<class View_type>
1074 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1075 size_t r_offset, View_type* view)
1077 gold_assert(gsym != NULL);
1078 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1079 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1081 // We cannot convert references to IFUNC symbols, or to symbols that
1082 // are not local to the current module.
1083 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1084 || gsym->is_undefined ()
1085 || gsym->is_from_dynobj()
1086 || gsym->is_preemptible())
1088 // Check for a CALLQ or JMPQ opcode.
1089 return ((*view)[r_offset - 2] == 0xff
1090 && ((*view)[r_offset - 1] == 0x15
1091 || (*view)[r_offset - 1] == 0x25));
1094 // Adjust TLS relocation type based on the options and whether this
1095 // is a local symbol.
1096 static tls::Tls_optimization
1097 optimize_tls_reloc(bool is_final, int r_type);
1099 // Get the GOT section, creating it if necessary.
1100 Output_data_got<64, false>*
1101 got_section(Symbol_table*, Layout*);
1103 // Get the GOT PLT section.
1104 Output_data_got_plt_x86_64*
1105 got_plt_section() const
1107 gold_assert(this->got_plt_ != NULL);
1108 return this->got_plt_;
1111 // Get the GOT section for TLSDESC entries.
1112 Output_data_got<64, false>*
1113 got_tlsdesc_section() const
1115 gold_assert(this->got_tlsdesc_ != NULL);
1116 return this->got_tlsdesc_;
1119 // Create the PLT section.
1121 make_plt_section(Symbol_table* symtab, Layout* layout);
1123 // Create a PLT entry for a global symbol.
1125 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1127 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1129 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1130 Sized_relobj_file<size, false>* relobj,
1131 unsigned int local_sym_index);
1133 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1135 define_tls_base_symbol(Symbol_table*, Layout*);
1137 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1139 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1141 // Create a GOT entry for the TLS module index.
1143 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1144 Sized_relobj_file<size, false>* object);
1146 // Get the PLT section.
1147 Output_data_plt_x86_64<size>*
1150 gold_assert(this->plt_ != NULL);
1154 // Get the dynamic reloc section, creating it if necessary.
1156 rela_dyn_section(Layout*);
1158 // Get the section to use for TLSDESC relocations.
1160 rela_tlsdesc_section(Layout*) const;
1162 // Get the section to use for IRELATIVE relocations.
1164 rela_irelative_section(Layout*);
1166 // Add a potential copy relocation.
1168 copy_reloc(Symbol_table* symtab, Layout* layout,
1169 Sized_relobj_file<size, false>* object,
1170 unsigned int shndx, Output_section* output_section,
1171 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1173 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1174 this->copy_relocs_.copy_reloc(symtab, layout,
1175 symtab->get_sized_symbol<size>(sym),
1176 object, shndx, output_section,
1177 r_type, reloc.get_r_offset(),
1178 reloc.get_r_addend(),
1179 this->rela_dyn_section(layout));
1182 // Information about this specific target which we pass to the
1183 // general Target structure.
1184 static const Target::Target_info x86_64_info;
1186 // The types of GOT entries needed for this platform.
1187 // These values are exposed to the ABI in an incremental link.
1188 // Do not renumber existing values without changing the version
1189 // number of the .gnu_incremental_inputs section.
1192 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1193 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1194 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1195 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1198 // This type is used as the argument to the target specific
1199 // relocation routines. The only target specific reloc is
1200 // R_X86_64_TLSDESC against a local symbol.
1203 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1204 : object(a_object), r_sym(a_r_sym)
1207 // The object in which the local symbol is defined.
1208 Sized_relobj_file<size, false>* object;
1209 // The local symbol index in the object.
1214 Output_data_got<64, false>* got_;
1216 Output_data_plt_x86_64<size>* plt_;
1217 // The GOT PLT section.
1218 Output_data_got_plt_x86_64* got_plt_;
1219 // The GOT section for IRELATIVE relocations.
1220 Output_data_space* got_irelative_;
1221 // The GOT section for TLSDESC relocations.
1222 Output_data_got<64, false>* got_tlsdesc_;
1223 // The _GLOBAL_OFFSET_TABLE_ symbol.
1224 Symbol* global_offset_table_;
1225 // The dynamic reloc section.
1226 Reloc_section* rela_dyn_;
1227 // The section to use for IRELATIVE relocs.
1228 Reloc_section* rela_irelative_;
1229 // Relocs saved to avoid a COPY reloc.
1230 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1231 // Offset of the GOT entry for the TLS module index.
1232 unsigned int got_mod_index_offset_;
1233 // We handle R_X86_64_TLSDESC against a local symbol as a target
1234 // specific relocation. Here we store the object and local symbol
1235 // index for the relocation.
1236 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1237 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1238 bool tls_base_symbol_defined_;
1242 const Target::Target_info Target_x86_64<64>::x86_64_info =
1245 false, // is_big_endian
1246 elfcpp::EM_X86_64, // machine_code
1247 false, // has_make_symbol
1248 false, // has_resolve
1249 true, // has_code_fill
1250 true, // is_default_stack_executable
1251 true, // can_icf_inline_merge_sections
1253 "/lib/ld64.so.1", // program interpreter
1254 0x400000, // default_text_segment_address
1255 0x1000, // abi_pagesize (overridable by -z max-page-size)
1256 0x1000, // common_pagesize (overridable by -z common-page-size)
1257 false, // isolate_execinstr
1259 elfcpp::SHN_UNDEF, // small_common_shndx
1260 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1261 0, // small_common_section_flags
1262 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1263 NULL, // attributes_section
1264 NULL, // attributes_vendor
1265 "_start", // entry_symbol_name
1266 32, // hash_entry_size
1270 const Target::Target_info Target_x86_64<32>::x86_64_info =
1273 false, // is_big_endian
1274 elfcpp::EM_X86_64, // machine_code
1275 false, // has_make_symbol
1276 false, // has_resolve
1277 true, // has_code_fill
1278 true, // is_default_stack_executable
1279 true, // can_icf_inline_merge_sections
1281 "/libx32/ldx32.so.1", // program interpreter
1282 0x400000, // default_text_segment_address
1283 0x1000, // abi_pagesize (overridable by -z max-page-size)
1284 0x1000, // common_pagesize (overridable by -z common-page-size)
1285 false, // isolate_execinstr
1287 elfcpp::SHN_UNDEF, // small_common_shndx
1288 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1289 0, // small_common_section_flags
1290 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1291 NULL, // attributes_section
1292 NULL, // attributes_vendor
1293 "_start", // entry_symbol_name
1294 32, // hash_entry_size
1297 // This is called when a new output section is created. This is where
1298 // we handle the SHF_X86_64_LARGE.
1302 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1304 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1305 os->set_is_large_section();
1308 // Get the GOT section, creating it if necessary.
1311 Output_data_got<64, false>*
1312 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1314 if (this->got_ == NULL)
1316 gold_assert(symtab != NULL && layout != NULL);
1318 // When using -z now, we can treat .got.plt as a relro section.
1319 // Without -z now, it is modified after program startup by lazy
1321 bool is_got_plt_relro = parameters->options().now();
1322 Output_section_order got_order = (is_got_plt_relro
1324 : ORDER_RELRO_LAST);
1325 Output_section_order got_plt_order = (is_got_plt_relro
1327 : ORDER_NON_RELRO_FIRST);
1329 this->got_ = new Output_data_got<64, false>();
1331 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1333 | elfcpp::SHF_WRITE),
1334 this->got_, got_order, true);
1336 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1337 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1339 | elfcpp::SHF_WRITE),
1340 this->got_plt_, got_plt_order,
1343 // The first three entries are reserved.
1344 this->got_plt_->set_current_data_size(3 * 8);
1346 if (!is_got_plt_relro)
1348 // Those bytes can go into the relro segment.
1349 layout->increase_relro(3 * 8);
1352 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1353 this->global_offset_table_ =
1354 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1355 Symbol_table::PREDEFINED,
1357 0, 0, elfcpp::STT_OBJECT,
1359 elfcpp::STV_HIDDEN, 0,
1362 // If there are any IRELATIVE relocations, they get GOT entries
1363 // in .got.plt after the jump slot entries.
1364 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1365 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1367 | elfcpp::SHF_WRITE),
1368 this->got_irelative_,
1369 got_plt_order, is_got_plt_relro);
1371 // If there are any TLSDESC relocations, they get GOT entries in
1372 // .got.plt after the jump slot and IRELATIVE entries.
1373 this->got_tlsdesc_ = new Output_data_got<64, false>();
1374 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1376 | elfcpp::SHF_WRITE),
1378 got_plt_order, is_got_plt_relro);
1384 // Get the dynamic reloc section, creating it if necessary.
1387 typename Target_x86_64<size>::Reloc_section*
1388 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1390 if (this->rela_dyn_ == NULL)
1392 gold_assert(layout != NULL);
1393 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1394 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1395 elfcpp::SHF_ALLOC, this->rela_dyn_,
1396 ORDER_DYNAMIC_RELOCS, false);
1398 return this->rela_dyn_;
1401 // Get the section to use for IRELATIVE relocs, creating it if
1402 // necessary. These go in .rela.dyn, but only after all other dynamic
1403 // relocations. They need to follow the other dynamic relocations so
1404 // that they can refer to global variables initialized by those
1408 typename Target_x86_64<size>::Reloc_section*
1409 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1411 if (this->rela_irelative_ == NULL)
1413 // Make sure we have already created the dynamic reloc section.
1414 this->rela_dyn_section(layout);
1415 this->rela_irelative_ = new Reloc_section(false);
1416 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1417 elfcpp::SHF_ALLOC, this->rela_irelative_,
1418 ORDER_DYNAMIC_RELOCS, false);
1419 gold_assert(this->rela_dyn_->output_section()
1420 == this->rela_irelative_->output_section());
1422 return this->rela_irelative_;
1425 // Write the first three reserved words of the .got.plt section.
1426 // The remainder of the section is written while writing the PLT
1427 // in Output_data_plt_i386::do_write.
1430 Output_data_got_plt_x86_64::do_write(Output_file* of)
1432 // The first entry in the GOT is the address of the .dynamic section
1433 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1434 // We saved space for them when we created the section in
1435 // Target_x86_64::got_section.
1436 const off_t got_file_offset = this->offset();
1437 gold_assert(this->data_size() >= 24);
1438 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1439 Output_section* dynamic = this->layout_->dynamic_section();
1440 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1441 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1442 memset(got_view + 8, 0, 16);
1443 of->write_output_view(got_file_offset, 24, got_view);
1446 // Initialize the PLT section.
1450 Output_data_plt_x86_64<size>::init(Layout* layout)
1452 this->rel_ = new Reloc_section(false);
1453 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1454 elfcpp::SHF_ALLOC, this->rel_,
1455 ORDER_DYNAMIC_PLT_RELOCS, false);
1460 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1462 os->set_entsize(this->get_plt_entry_size());
1465 // Add an entry to the PLT.
1469 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1472 gold_assert(!gsym->has_plt_offset());
1474 unsigned int plt_index;
1476 section_offset_type got_offset;
1478 unsigned int* pcount;
1479 unsigned int offset;
1480 unsigned int reserved;
1481 Output_section_data_build* got;
1482 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1483 && gsym->can_use_relative_reloc(false))
1485 pcount = &this->irelative_count_;
1488 got = this->got_irelative_;
1492 pcount = &this->count_;
1495 got = this->got_plt_;
1498 if (!this->is_data_size_valid())
1500 // Note that when setting the PLT offset for a non-IRELATIVE
1501 // entry we skip the initial reserved PLT entry.
1502 plt_index = *pcount + offset;
1503 plt_offset = plt_index * this->get_plt_entry_size();
1507 got_offset = (plt_index - offset + reserved) * 8;
1508 gold_assert(got_offset == got->current_data_size());
1510 // Every PLT entry needs a GOT entry which points back to the PLT
1511 // entry (this will be changed by the dynamic linker, normally
1512 // lazily when the function is called).
1513 got->set_current_data_size(got_offset + 8);
1517 // FIXME: This is probably not correct for IRELATIVE relocs.
1519 // For incremental updates, find an available slot.
1520 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1521 this->get_plt_entry_size(), 0);
1522 if (plt_offset == -1)
1523 gold_fallback(_("out of patch space (PLT);"
1524 " relink with --incremental-full"));
1526 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1527 // can be calculated from the PLT index, adjusting for the three
1528 // reserved entries at the beginning of the GOT.
1529 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1530 got_offset = (plt_index - offset + reserved) * 8;
1533 gsym->set_plt_offset(plt_offset);
1535 // Every PLT entry needs a reloc.
1536 this->add_relocation(symtab, layout, gsym, got_offset);
1538 // Note that we don't need to save the symbol. The contents of the
1539 // PLT are independent of which symbols are used. The symbols only
1540 // appear in the relocations.
1543 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1548 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1549 Symbol_table* symtab,
1551 Sized_relobj_file<size, false>* relobj,
1552 unsigned int local_sym_index)
1554 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1555 ++this->irelative_count_;
1557 section_offset_type got_offset = this->got_irelative_->current_data_size();
1559 // Every PLT entry needs a GOT entry which points back to the PLT
1561 this->got_irelative_->set_current_data_size(got_offset + 8);
1563 // Every PLT entry needs a reloc.
1564 Reloc_section* rela = this->rela_irelative(symtab, layout);
1565 rela->add_symbolless_local_addend(relobj, local_sym_index,
1566 elfcpp::R_X86_64_IRELATIVE,
1567 this->got_irelative_, got_offset, 0);
1572 // Add the relocation for a PLT entry.
1576 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1579 unsigned int got_offset)
1581 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1582 && gsym->can_use_relative_reloc(false))
1584 Reloc_section* rela = this->rela_irelative(symtab, layout);
1585 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1586 this->got_irelative_, got_offset, 0);
1590 gsym->set_needs_dynsym_entry();
1591 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1596 // Return where the TLSDESC relocations should go, creating it if
1597 // necessary. These follow the JUMP_SLOT relocations.
1600 typename Output_data_plt_x86_64<size>::Reloc_section*
1601 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1603 if (this->tlsdesc_rel_ == NULL)
1605 this->tlsdesc_rel_ = new Reloc_section(false);
1606 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1607 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1608 ORDER_DYNAMIC_PLT_RELOCS, false);
1609 gold_assert(this->tlsdesc_rel_->output_section()
1610 == this->rel_->output_section());
1612 return this->tlsdesc_rel_;
1615 // Return where the IRELATIVE relocations should go in the PLT. These
1616 // follow the JUMP_SLOT and the TLSDESC relocations.
1619 typename Output_data_plt_x86_64<size>::Reloc_section*
1620 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1623 if (this->irelative_rel_ == NULL)
1625 // Make sure we have a place for the TLSDESC relocations, in
1626 // case we see any later on.
1627 this->rela_tlsdesc(layout);
1628 this->irelative_rel_ = new Reloc_section(false);
1629 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1630 elfcpp::SHF_ALLOC, this->irelative_rel_,
1631 ORDER_DYNAMIC_PLT_RELOCS, false);
1632 gold_assert(this->irelative_rel_->output_section()
1633 == this->rel_->output_section());
1635 if (parameters->doing_static_link())
1637 // A statically linked executable will only have a .rela.plt
1638 // section to hold R_X86_64_IRELATIVE relocs for
1639 // STT_GNU_IFUNC symbols. The library will use these
1640 // symbols to locate the IRELATIVE relocs at program startup
1642 symtab->define_in_output_data("__rela_iplt_start", NULL,
1643 Symbol_table::PREDEFINED,
1644 this->irelative_rel_, 0, 0,
1645 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1646 elfcpp::STV_HIDDEN, 0, false, true);
1647 symtab->define_in_output_data("__rela_iplt_end", NULL,
1648 Symbol_table::PREDEFINED,
1649 this->irelative_rel_, 0, 0,
1650 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1651 elfcpp::STV_HIDDEN, 0, true, true);
1654 return this->irelative_rel_;
1657 // Return the PLT address to use for a global symbol.
1661 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1663 uint64_t offset = 0;
1664 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1665 && gsym->can_use_relative_reloc(false))
1666 offset = (this->count_ + 1) * this->get_plt_entry_size();
1667 return this->address() + offset + gsym->plt_offset();
1670 // Return the PLT address to use for a local symbol. These are always
1671 // IRELATIVE relocs.
1675 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1678 return (this->address()
1679 + (this->count_ + 1) * this->get_plt_entry_size()
1680 + object->local_plt_offset(r_sym));
1683 // Set the final size.
1686 Output_data_plt_x86_64<size>::set_final_data_size()
1688 // Number of regular and IFUNC PLT entries, plus the first entry.
1689 unsigned int count = this->count_ + this->irelative_count_ + 1;
1690 // Count the TLSDESC entry, if present.
1691 if (this->has_tlsdesc_entry())
1693 this->set_data_size(count * this->get_plt_entry_size());
1696 // The first entry in the PLT for an executable.
1700 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1702 // From AMD64 ABI Draft 0.98, page 76
1703 0xff, 0x35, // pushq contents of memory address
1704 0, 0, 0, 0, // replaced with address of .got + 8
1705 0xff, 0x25, // jmp indirect
1706 0, 0, 0, 0, // replaced with address of .got + 16
1707 0x90, 0x90, 0x90, 0x90 // noop (x4)
1712 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1714 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1715 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1717 memcpy(pov, first_plt_entry, plt_entry_size);
1718 // We do a jmp relative to the PC at the end of this instruction.
1719 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1721 - (plt_address + 6)));
1722 elfcpp::Swap<32, false>::writeval(pov + 8,
1724 - (plt_address + 12)));
1727 // Subsequent entries in the PLT for an executable.
1731 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1733 // From AMD64 ABI Draft 0.98, page 76
1734 0xff, 0x25, // jmpq indirect
1735 0, 0, 0, 0, // replaced with address of symbol in .got
1736 0x68, // pushq immediate
1737 0, 0, 0, 0, // replaced with offset into relocation table
1738 0xe9, // jmpq relative
1739 0, 0, 0, 0 // replaced with offset to start of .plt
1744 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1746 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1747 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1748 unsigned int got_offset,
1749 unsigned int plt_offset,
1750 unsigned int plt_index)
1752 // Check PC-relative offset overflow in PLT entry.
1753 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1754 - (plt_address + plt_offset + 6));
1755 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1756 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1759 memcpy(pov, plt_entry, plt_entry_size);
1760 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1761 plt_got_pcrel_offset);
1763 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1764 elfcpp::Swap<32, false>::writeval(pov + 12,
1765 - (plt_offset + plt_entry_size));
1770 // The reserved TLSDESC entry in the PLT for an executable.
1774 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1776 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1777 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1778 0xff, 0x35, // pushq x(%rip)
1779 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1780 0xff, 0x25, // jmpq *y(%rip)
1781 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1788 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1790 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1791 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1792 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1793 unsigned int tlsdesc_got_offset,
1794 unsigned int plt_offset)
1796 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1797 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1799 - (plt_address + plt_offset
1801 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1803 + tlsdesc_got_offset
1804 - (plt_address + plt_offset
1808 // Return the APLT address to use for a global symbol (for -z bndplt).
1811 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
1813 uint64_t offset = this->aplt_offset_;
1814 // Convert the PLT offset into an APLT offset.
1815 unsigned int plt_offset = gsym->plt_offset();
1816 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1817 && gsym->can_use_relative_reloc(false))
1818 offset += this->regular_count() * aplt_entry_size;
1820 plt_offset -= plt_entry_size;
1821 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
1822 return this->address() + offset + plt_offset;
1825 // Return the PLT address to use for a local symbol. These are always
1826 // IRELATIVE relocs.
1829 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
1832 // Convert the PLT offset into an APLT offset.
1833 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size)
1834 / (plt_entry_size / aplt_entry_size));
1835 return (this->address()
1836 + this->aplt_offset_
1837 + this->regular_count() * aplt_entry_size
1841 // Set the final size.
1843 Output_data_plt_x86_64_bnd::set_final_data_size()
1845 // Number of regular and IFUNC PLT entries.
1846 unsigned int count = this->entry_count();
1847 // Count the first entry and the TLSDESC entry, if present.
1848 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
1849 unsigned int plt_size = (count + extra) * plt_entry_size;
1850 // Offset of the APLT.
1851 this->aplt_offset_ = plt_size;
1852 // Size of the APLT.
1853 plt_size += count * aplt_entry_size;
1854 this->set_data_size(plt_size);
1857 // The first entry in the BND PLT.
1860 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
1862 // From AMD64 ABI Draft 0.98, page 76
1863 0xff, 0x35, // pushq contents of memory address
1864 0, 0, 0, 0, // replaced with address of .got + 8
1865 0xf2, 0xff, 0x25, // bnd jmp indirect
1866 0, 0, 0, 0, // replaced with address of .got + 16
1867 0x0f, 0x1f, 0x00 // nop
1871 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
1873 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
1874 typename elfcpp::Elf_types<64>::Elf_Addr plt_address)
1876 memcpy(pov, first_plt_entry, plt_entry_size);
1877 // We do a jmp relative to the PC at the end of this instruction.
1878 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1880 - (plt_address + 6)));
1881 elfcpp::Swap<32, false>::writeval(pov + 9,
1883 - (plt_address + 13)));
1886 // Subsequent entries in the BND PLT.
1889 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
1891 // From AMD64 ABI Draft 0.99.8, page 139
1892 0x68, // pushq immediate
1893 0, 0, 0, 0, // replaced with offset into relocation table
1894 0xf2, 0xe9, // bnd jmpq relative
1895 0, 0, 0, 0, // replaced with offset to start of .plt
1896 0x0f, 0x1f, 0x44, 0, 0 // nop
1899 // Entries in the BND Additional PLT.
1902 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
1904 // From AMD64 ABI Draft 0.99.8, page 139
1905 0xf2, 0xff, 0x25, // bnd jmpq indirect
1906 0, 0, 0, 0, // replaced with address of symbol in .got
1911 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
1913 typename elfcpp::Elf_types<64>::Elf_Addr,
1914 typename elfcpp::Elf_types<64>::Elf_Addr,
1916 unsigned int plt_offset,
1917 unsigned int plt_index)
1919 memcpy(pov, plt_entry, plt_entry_size);
1920 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
1921 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
1926 Output_data_plt_x86_64_bnd::fill_aplt_entry(
1928 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
1929 typename elfcpp::Elf_types<64>::Elf_Addr plt_address,
1930 unsigned int got_offset,
1931 unsigned int plt_offset,
1932 unsigned int plt_index)
1934 // Check PC-relative offset overflow in PLT entry.
1935 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1936 - (plt_address + plt_offset + 7));
1937 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1938 gold_error(_("PC-relative offset overflow in APLT entry %d"),
1941 memcpy(pov, aplt_entry, aplt_entry_size);
1942 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
1945 // The reserved TLSDESC entry in the PLT for an executable.
1948 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
1950 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1951 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1952 0xff, 0x35, // pushq x(%rip)
1953 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1954 0xf2, 0xff, 0x25, // jmpq *y(%rip)
1955 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1956 0x0f, 0x1f, 0 // nop
1960 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
1962 typename elfcpp::Elf_types<64>::Elf_Addr got_address,
1963 typename elfcpp::Elf_types<64>::Elf_Addr plt_address,
1964 typename elfcpp::Elf_types<64>::Elf_Addr got_base,
1965 unsigned int tlsdesc_got_offset,
1966 unsigned int plt_offset)
1968 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1969 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1971 - (plt_address + plt_offset
1973 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
1975 + tlsdesc_got_offset
1976 - (plt_address + plt_offset
1980 // The .eh_frame unwind information for the PLT.
1984 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1987 'z', // Augmentation: augmentation size included.
1988 'R', // Augmentation: FDE encoding included.
1989 '\0', // End of augmentation string.
1990 1, // Code alignment factor.
1991 0x78, // Data alignment factor.
1992 16, // Return address column.
1993 1, // Augmentation size.
1994 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1995 | elfcpp::DW_EH_PE_sdata4),
1996 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1997 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1998 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2004 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2006 0, 0, 0, 0, // Replaced with offset to .plt.
2007 0, 0, 0, 0, // Replaced with size of .plt.
2008 0, // Augmentation size.
2009 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2010 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2011 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2012 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2013 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2014 11, // Block length.
2015 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2016 elfcpp::DW_OP_breg16, 0, // Push %rip.
2017 elfcpp::DW_OP_lit15, // Push 0xf.
2018 elfcpp::DW_OP_and, // & (%rip & 0xf).
2019 elfcpp::DW_OP_lit11, // Push 0xb.
2020 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2021 elfcpp::DW_OP_lit3, // Push 3.
2022 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2023 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2024 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2030 // The .eh_frame unwind information for the BND PLT.
2032 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2034 0, 0, 0, 0, // Replaced with offset to .plt.
2035 0, 0, 0, 0, // Replaced with size of .plt.
2036 0, // Augmentation size.
2037 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2038 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2039 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2040 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2041 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2042 11, // Block length.
2043 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2044 elfcpp::DW_OP_breg16, 0, // Push %rip.
2045 elfcpp::DW_OP_lit15, // Push 0xf.
2046 elfcpp::DW_OP_and, // & (%rip & 0xf).
2047 elfcpp::DW_OP_lit5, // Push 5.
2048 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
2049 elfcpp::DW_OP_lit3, // Push 3.
2050 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
2051 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2052 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2058 // Write out the PLT. This uses the hand-coded instructions above,
2059 // and adjusts them as needed. This is specified by the AMD64 ABI.
2063 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2065 const off_t offset = this->offset();
2066 const section_size_type oview_size =
2067 convert_to_section_size_type(this->data_size());
2068 unsigned char* const oview = of->get_output_view(offset, oview_size);
2070 const off_t got_file_offset = this->got_plt_->offset();
2071 gold_assert(parameters->incremental_update()
2072 || (got_file_offset + this->got_plt_->data_size()
2073 == this->got_irelative_->offset()));
2074 const section_size_type got_size =
2075 convert_to_section_size_type(this->got_plt_->data_size()
2076 + this->got_irelative_->data_size());
2077 unsigned char* const got_view = of->get_output_view(got_file_offset,
2080 unsigned char* pov = oview;
2082 // The base address of the .plt section.
2083 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2084 // The base address of the .got section.
2085 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2086 // The base address of the PLT portion of the .got section,
2087 // which is where the GOT pointer will point, and where the
2088 // three reserved GOT entries are located.
2089 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2090 = this->got_plt_->address();
2092 this->fill_first_plt_entry(pov, got_address, plt_address);
2093 pov += this->get_plt_entry_size();
2095 // The first three entries in the GOT are reserved, and are written
2096 // by Output_data_got_plt_x86_64::do_write.
2097 unsigned char* got_pov = got_view + 24;
2099 unsigned int plt_offset = this->get_plt_entry_size();
2100 unsigned int got_offset = 24;
2101 const unsigned int count = this->count_ + this->irelative_count_;
2102 for (unsigned int plt_index = 0;
2105 pov += this->get_plt_entry_size(),
2107 plt_offset += this->get_plt_entry_size(),
2110 // Set and adjust the PLT entry itself.
2111 unsigned int lazy_offset = this->fill_plt_entry(pov,
2112 got_address, plt_address,
2113 got_offset, plt_offset,
2116 // Set the entry in the GOT.
2117 elfcpp::Swap<64, false>::writeval(got_pov,
2118 plt_address + plt_offset + lazy_offset);
2121 if (this->has_tlsdesc_entry())
2123 // Set and adjust the reserved TLSDESC PLT entry.
2124 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2125 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2126 tlsdesc_got_offset, plt_offset);
2127 pov += this->get_plt_entry_size();
2130 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2131 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2133 of->write_output_view(offset, oview_size, oview);
2134 of->write_output_view(got_file_offset, got_size, got_view);
2137 // Write out the BND PLT.
2140 Output_data_plt_x86_64_bnd::do_write(Output_file* of)
2142 const off_t offset = this->offset();
2143 const section_size_type oview_size =
2144 convert_to_section_size_type(this->data_size());
2145 unsigned char* const oview = of->get_output_view(offset, oview_size);
2147 Output_data_got<64, false>* got = this->got();
2148 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2149 Output_data_space* got_irelative = this->got_irelative();
2151 const off_t got_file_offset = got_plt->offset();
2152 gold_assert(parameters->incremental_update()
2153 || (got_file_offset + got_plt->data_size()
2154 == got_irelative->offset()));
2155 const section_size_type got_size =
2156 convert_to_section_size_type(got_plt->data_size()
2157 + got_irelative->data_size());
2158 unsigned char* const got_view = of->get_output_view(got_file_offset,
2161 unsigned char* pov = oview;
2163 // The base address of the .plt section.
2164 typename elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2165 // The base address of the .got section.
2166 typename elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2167 // The base address of the PLT portion of the .got section,
2168 // which is where the GOT pointer will point, and where the
2169 // three reserved GOT entries are located.
2170 typename elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2172 this->fill_first_plt_entry(pov, got_address, plt_address);
2173 pov += plt_entry_size;
2175 // The first three entries in the GOT are reserved, and are written
2176 // by Output_data_got_plt_x86_64::do_write.
2177 unsigned char* got_pov = got_view + 24;
2179 unsigned int plt_offset = plt_entry_size;
2180 unsigned int got_offset = 24;
2181 const unsigned int count = this->entry_count();
2182 for (unsigned int plt_index = 0;
2185 pov += plt_entry_size,
2187 plt_offset += plt_entry_size,
2190 // Set and adjust the PLT entry itself.
2191 unsigned int lazy_offset = this->fill_plt_entry(pov,
2192 got_address, plt_address,
2193 got_offset, plt_offset,
2196 // Set the entry in the GOT.
2197 elfcpp::Swap<64, false>::writeval(got_pov,
2198 plt_address + plt_offset + lazy_offset);
2201 if (this->has_tlsdesc_entry())
2203 // Set and adjust the reserved TLSDESC PLT entry.
2204 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2205 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2206 tlsdesc_got_offset, plt_offset);
2207 pov += this->get_plt_entry_size();
2210 // Write the additional PLT.
2212 for (unsigned int plt_index = 0;
2215 pov += aplt_entry_size,
2216 plt_offset += aplt_entry_size,
2219 // Set and adjust the PLT entry itself.
2220 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2221 plt_offset, plt_index);
2224 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2225 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2227 of->write_output_view(offset, oview_size, oview);
2228 of->write_output_view(got_file_offset, got_size, got_view);
2231 // Create the PLT section.
2235 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2237 if (this->plt_ == NULL)
2239 // Create the GOT sections first.
2240 this->got_section(symtab, layout);
2242 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2243 this->got_irelative_);
2245 // Add unwind information if requested.
2246 if (parameters->options().ld_generated_unwind_info())
2247 this->plt_->add_eh_frame(layout);
2249 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2251 | elfcpp::SHF_EXECINSTR),
2252 this->plt_, ORDER_PLT, false);
2254 // Make the sh_info field of .rela.plt point to .plt.
2255 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2256 rela_plt_os->set_info_section(this->plt_->output_section());
2261 Output_data_plt_x86_64<32>*
2262 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2263 Output_data_got<64, false>* got,
2264 Output_data_got_plt_x86_64* got_plt,
2265 Output_data_space* got_irelative)
2267 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2272 Output_data_plt_x86_64<64>*
2273 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2274 Output_data_got<64, false>* got,
2275 Output_data_got_plt_x86_64* got_plt,
2276 Output_data_space* got_irelative)
2278 if (parameters->options().bndplt())
2279 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2282 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2287 Output_data_plt_x86_64<32>*
2288 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2289 Output_data_got<64, false>* got,
2290 Output_data_got_plt_x86_64* got_plt,
2291 Output_data_space* got_irelative,
2292 unsigned int plt_count)
2294 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2300 Output_data_plt_x86_64<64>*
2301 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2302 Output_data_got<64, false>* got,
2303 Output_data_got_plt_x86_64* got_plt,
2304 Output_data_space* got_irelative,
2305 unsigned int plt_count)
2307 if (parameters->options().bndplt())
2308 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2309 got_irelative, plt_count);
2311 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2316 // Return the section for TLSDESC relocations.
2319 typename Target_x86_64<size>::Reloc_section*
2320 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2322 return this->plt_section()->rela_tlsdesc(layout);
2325 // Create a PLT entry for a global symbol.
2329 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2332 if (gsym->has_plt_offset())
2335 if (this->plt_ == NULL)
2336 this->make_plt_section(symtab, layout);
2338 this->plt_->add_entry(symtab, layout, gsym);
2341 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2345 Target_x86_64<size>::make_local_ifunc_plt_entry(
2346 Symbol_table* symtab, Layout* layout,
2347 Sized_relobj_file<size, false>* relobj,
2348 unsigned int local_sym_index)
2350 if (relobj->local_has_plt_offset(local_sym_index))
2352 if (this->plt_ == NULL)
2353 this->make_plt_section(symtab, layout);
2354 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2357 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2360 // Return the number of entries in the PLT.
2364 Target_x86_64<size>::plt_entry_count() const
2366 if (this->plt_ == NULL)
2368 return this->plt_->entry_count();
2371 // Return the offset of the first non-reserved PLT entry.
2375 Target_x86_64<size>::first_plt_entry_offset() const
2377 if (this->plt_ == NULL)
2379 return this->plt_->first_plt_entry_offset();
2382 // Return the size of each PLT entry.
2386 Target_x86_64<size>::plt_entry_size() const
2388 if (this->plt_ == NULL)
2390 return this->plt_->get_plt_entry_size();
2393 // Create the GOT and PLT sections for an incremental update.
2396 Output_data_got_base*
2397 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
2399 unsigned int got_count,
2400 unsigned int plt_count)
2402 gold_assert(this->got_ == NULL);
2404 this->got_ = new Output_data_got<64, false>(got_count * 8);
2405 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2407 | elfcpp::SHF_WRITE),
2408 this->got_, ORDER_RELRO_LAST,
2411 // Add the three reserved entries.
2412 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
2413 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2415 | elfcpp::SHF_WRITE),
2416 this->got_plt_, ORDER_NON_RELRO_FIRST,
2419 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2420 this->global_offset_table_ =
2421 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2422 Symbol_table::PREDEFINED,
2424 0, 0, elfcpp::STT_OBJECT,
2426 elfcpp::STV_HIDDEN, 0,
2429 // If there are any TLSDESC relocations, they get GOT entries in
2430 // .got.plt after the jump slot entries.
2431 // FIXME: Get the count for TLSDESC entries.
2432 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
2433 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2434 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2436 ORDER_NON_RELRO_FIRST, false);
2438 // If there are any IRELATIVE relocations, they get GOT entries in
2439 // .got.plt after the jump slot and TLSDESC entries.
2440 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2441 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2442 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2443 this->got_irelative_,
2444 ORDER_NON_RELRO_FIRST, false);
2446 // Create the PLT section.
2447 this->plt_ = this->make_data_plt(layout, this->got_,
2449 this->got_irelative_,
2452 // Add unwind information if requested.
2453 if (parameters->options().ld_generated_unwind_info())
2454 this->plt_->add_eh_frame(layout);
2456 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2457 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2458 this->plt_, ORDER_PLT, false);
2460 // Make the sh_info field of .rela.plt point to .plt.
2461 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2462 rela_plt_os->set_info_section(this->plt_->output_section());
2464 // Create the rela_dyn section.
2465 this->rela_dyn_section(layout);
2470 // Reserve a GOT entry for a local symbol, and regenerate any
2471 // necessary dynamic relocations.
2475 Target_x86_64<size>::reserve_local_got_entry(
2476 unsigned int got_index,
2477 Sized_relobj<size, false>* obj,
2479 unsigned int got_type)
2481 unsigned int got_offset = got_index * 8;
2482 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2484 this->got_->reserve_local(got_index, obj, r_sym, got_type);
2487 case GOT_TYPE_STANDARD:
2488 if (parameters->options().output_is_position_independent())
2489 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
2490 this->got_, got_offset, 0, false);
2492 case GOT_TYPE_TLS_OFFSET:
2493 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
2494 this->got_, got_offset, 0);
2496 case GOT_TYPE_TLS_PAIR:
2497 this->got_->reserve_slot(got_index + 1);
2498 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
2499 this->got_, got_offset, 0);
2501 case GOT_TYPE_TLS_DESC:
2502 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2503 // this->got_->reserve_slot(got_index + 1);
2504 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2505 // this->got_, got_offset, 0);
2512 // Reserve a GOT entry for a global symbol, and regenerate any
2513 // necessary dynamic relocations.
2517 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
2519 unsigned int got_type)
2521 unsigned int got_offset = got_index * 8;
2522 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2524 this->got_->reserve_global(got_index, gsym, got_type);
2527 case GOT_TYPE_STANDARD:
2528 if (!gsym->final_value_is_known())
2530 if (gsym->is_from_dynobj()
2531 || gsym->is_undefined()
2532 || gsym->is_preemptible()
2533 || gsym->type() == elfcpp::STT_GNU_IFUNC)
2534 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
2535 this->got_, got_offset, 0);
2537 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2538 this->got_, got_offset, 0, false);
2541 case GOT_TYPE_TLS_OFFSET:
2542 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
2543 this->got_, got_offset, 0, false);
2545 case GOT_TYPE_TLS_PAIR:
2546 this->got_->reserve_slot(got_index + 1);
2547 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
2548 this->got_, got_offset, 0, false);
2549 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
2550 this->got_, got_offset + 8, 0, false);
2552 case GOT_TYPE_TLS_DESC:
2553 this->got_->reserve_slot(got_index + 1);
2554 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2555 this->got_, got_offset, 0, false);
2562 // Register an existing PLT entry for a global symbol.
2566 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2568 unsigned int plt_index,
2571 gold_assert(this->plt_ != NULL);
2572 gold_assert(!gsym->has_plt_offset());
2574 this->plt_->reserve_slot(plt_index);
2576 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2578 unsigned int got_offset = (plt_index + 3) * 8;
2579 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2582 // Force a COPY relocation for a given symbol.
2586 Target_x86_64<size>::emit_copy_reloc(
2587 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2589 this->copy_relocs_.emit_copy_reloc(symtab,
2590 symtab->get_sized_symbol<size>(sym),
2593 this->rela_dyn_section(NULL));
2596 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2600 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2603 if (this->tls_base_symbol_defined_)
2606 Output_segment* tls_segment = layout->tls_segment();
2607 if (tls_segment != NULL)
2609 bool is_exec = parameters->options().output_is_executable();
2610 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2611 Symbol_table::PREDEFINED,
2615 elfcpp::STV_HIDDEN, 0,
2617 ? Symbol::SEGMENT_END
2618 : Symbol::SEGMENT_START),
2621 this->tls_base_symbol_defined_ = true;
2624 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2628 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2631 if (this->plt_ == NULL)
2632 this->make_plt_section(symtab, layout);
2634 if (!this->plt_->has_tlsdesc_entry())
2636 // Allocate the TLSDESC_GOT entry.
2637 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2638 unsigned int got_offset = got->add_constant(0);
2640 // Allocate the TLSDESC_PLT entry.
2641 this->plt_->reserve_tlsdesc_entry(got_offset);
2645 // Create a GOT entry for the TLS module index.
2649 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2650 Sized_relobj_file<size, false>* object)
2652 if (this->got_mod_index_offset_ == -1U)
2654 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2655 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2656 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2657 unsigned int got_offset = got->add_constant(0);
2658 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2660 got->add_constant(0);
2661 this->got_mod_index_offset_ = got_offset;
2663 return this->got_mod_index_offset_;
2666 // Optimize the TLS relocation type based on what we know about the
2667 // symbol. IS_FINAL is true if the final address of this symbol is
2668 // known at link time.
2671 tls::Tls_optimization
2672 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2674 // If we are generating a shared library, then we can't do anything
2676 if (parameters->options().shared())
2677 return tls::TLSOPT_NONE;
2681 case elfcpp::R_X86_64_TLSGD:
2682 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2683 case elfcpp::R_X86_64_TLSDESC_CALL:
2684 // These are General-Dynamic which permits fully general TLS
2685 // access. Since we know that we are generating an executable,
2686 // we can convert this to Initial-Exec. If we also know that
2687 // this is a local symbol, we can further switch to Local-Exec.
2689 return tls::TLSOPT_TO_LE;
2690 return tls::TLSOPT_TO_IE;
2692 case elfcpp::R_X86_64_TLSLD:
2693 // This is Local-Dynamic, which refers to a local symbol in the
2694 // dynamic TLS block. Since we know that we generating an
2695 // executable, we can switch to Local-Exec.
2696 return tls::TLSOPT_TO_LE;
2698 case elfcpp::R_X86_64_DTPOFF32:
2699 case elfcpp::R_X86_64_DTPOFF64:
2700 // Another Local-Dynamic reloc.
2701 return tls::TLSOPT_TO_LE;
2703 case elfcpp::R_X86_64_GOTTPOFF:
2704 // These are Initial-Exec relocs which get the thread offset
2705 // from the GOT. If we know that we are linking against the
2706 // local symbol, we can switch to Local-Exec, which links the
2707 // thread offset into the instruction.
2709 return tls::TLSOPT_TO_LE;
2710 return tls::TLSOPT_NONE;
2712 case elfcpp::R_X86_64_TPOFF32:
2713 // When we already have Local-Exec, there is nothing further we
2715 return tls::TLSOPT_NONE;
2722 // Get the Reference_flags for a particular relocation.
2726 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2730 case elfcpp::R_X86_64_NONE:
2731 case elfcpp::R_X86_64_GNU_VTINHERIT:
2732 case elfcpp::R_X86_64_GNU_VTENTRY:
2733 case elfcpp::R_X86_64_GOTPC32:
2734 case elfcpp::R_X86_64_GOTPC64:
2735 // No symbol reference.
2738 case elfcpp::R_X86_64_64:
2739 case elfcpp::R_X86_64_32:
2740 case elfcpp::R_X86_64_32S:
2741 case elfcpp::R_X86_64_16:
2742 case elfcpp::R_X86_64_8:
2743 return Symbol::ABSOLUTE_REF;
2745 case elfcpp::R_X86_64_PC64:
2746 case elfcpp::R_X86_64_PC32:
2747 case elfcpp::R_X86_64_PC32_BND:
2748 case elfcpp::R_X86_64_PC16:
2749 case elfcpp::R_X86_64_PC8:
2750 case elfcpp::R_X86_64_GOTOFF64:
2751 return Symbol::RELATIVE_REF;
2753 case elfcpp::R_X86_64_PLT32:
2754 case elfcpp::R_X86_64_PLT32_BND:
2755 case elfcpp::R_X86_64_PLTOFF64:
2756 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2758 case elfcpp::R_X86_64_GOT64:
2759 case elfcpp::R_X86_64_GOT32:
2760 case elfcpp::R_X86_64_GOTPCREL64:
2761 case elfcpp::R_X86_64_GOTPCREL:
2762 case elfcpp::R_X86_64_GOTPCRELX:
2763 case elfcpp::R_X86_64_REX_GOTPCRELX:
2764 case elfcpp::R_X86_64_GOTPLT64:
2766 return Symbol::ABSOLUTE_REF;
2768 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2769 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2770 case elfcpp::R_X86_64_TLSDESC_CALL:
2771 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2772 case elfcpp::R_X86_64_DTPOFF32:
2773 case elfcpp::R_X86_64_DTPOFF64:
2774 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2775 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2776 return Symbol::TLS_REF;
2778 case elfcpp::R_X86_64_COPY:
2779 case elfcpp::R_X86_64_GLOB_DAT:
2780 case elfcpp::R_X86_64_JUMP_SLOT:
2781 case elfcpp::R_X86_64_RELATIVE:
2782 case elfcpp::R_X86_64_IRELATIVE:
2783 case elfcpp::R_X86_64_TPOFF64:
2784 case elfcpp::R_X86_64_DTPMOD64:
2785 case elfcpp::R_X86_64_TLSDESC:
2786 case elfcpp::R_X86_64_SIZE32:
2787 case elfcpp::R_X86_64_SIZE64:
2789 // Not expected. We will give an error later.
2794 // Report an unsupported relocation against a local symbol.
2798 Target_x86_64<size>::Scan::unsupported_reloc_local(
2799 Sized_relobj_file<size, false>* object,
2800 unsigned int r_type)
2802 gold_error(_("%s: unsupported reloc %u against local symbol"),
2803 object->name().c_str(), r_type);
2806 // We are about to emit a dynamic relocation of type R_TYPE. If the
2807 // dynamic linker does not support it, issue an error. The GNU linker
2808 // only issues a non-PIC error for an allocated read-only section.
2809 // Here we know the section is allocated, but we don't know that it is
2810 // read-only. But we check for all the relocation types which the
2811 // glibc dynamic linker supports, so it seems appropriate to issue an
2812 // error even if the section is not read-only. If GSYM is not NULL,
2813 // it is the symbol the relocation is against; if it is NULL, the
2814 // relocation is against a local symbol.
2818 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2823 // These are the relocation types supported by glibc for x86_64
2824 // which should always work.
2825 case elfcpp::R_X86_64_RELATIVE:
2826 case elfcpp::R_X86_64_IRELATIVE:
2827 case elfcpp::R_X86_64_GLOB_DAT:
2828 case elfcpp::R_X86_64_JUMP_SLOT:
2829 case elfcpp::R_X86_64_DTPMOD64:
2830 case elfcpp::R_X86_64_DTPOFF64:
2831 case elfcpp::R_X86_64_TPOFF64:
2832 case elfcpp::R_X86_64_64:
2833 case elfcpp::R_X86_64_COPY:
2836 // glibc supports these reloc types, but they can overflow.
2837 case elfcpp::R_X86_64_PC32:
2838 case elfcpp::R_X86_64_PC32_BND:
2839 // A PC relative reference is OK against a local symbol or if
2840 // the symbol is defined locally.
2842 || (!gsym->is_from_dynobj()
2843 && !gsym->is_undefined()
2844 && !gsym->is_preemptible()))
2847 case elfcpp::R_X86_64_32:
2848 // R_X86_64_32 is OK for x32.
2849 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2851 if (this->issued_non_pic_error_)
2853 gold_assert(parameters->options().output_is_position_independent());
2855 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2856 "overflow at runtime; recompile with -fPIC"));
2862 case elfcpp::R_X86_64_32:
2863 r_name = "R_X86_64_32";
2865 case elfcpp::R_X86_64_PC32:
2866 r_name = "R_X86_64_PC32";
2868 case elfcpp::R_X86_64_PC32_BND:
2869 r_name = "R_X86_64_PC32_BND";
2875 object->error(_("requires dynamic %s reloc against '%s' "
2876 "which may overflow at runtime; recompile "
2878 r_name, gsym->name());
2880 this->issued_non_pic_error_ = true;
2884 // This prevents us from issuing more than one error per reloc
2885 // section. But we can still wind up issuing more than one
2886 // error per object file.
2887 if (this->issued_non_pic_error_)
2889 gold_assert(parameters->options().output_is_position_independent());
2890 object->error(_("requires unsupported dynamic reloc %u; "
2891 "recompile with -fPIC"),
2893 this->issued_non_pic_error_ = true;
2896 case elfcpp::R_X86_64_NONE:
2901 // Return whether we need to make a PLT entry for a relocation of the
2902 // given type against a STT_GNU_IFUNC symbol.
2906 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2907 Sized_relobj_file<size, false>* object,
2908 unsigned int r_type)
2910 int flags = Scan::get_reference_flags(r_type);
2911 if (flags & Symbol::TLS_REF)
2912 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2913 object->name().c_str(), r_type);
2917 // Scan a relocation for a local symbol.
2921 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2923 Target_x86_64<size>* target,
2924 Sized_relobj_file<size, false>* object,
2925 unsigned int data_shndx,
2926 Output_section* output_section,
2927 const elfcpp::Rela<size, false>& reloc,
2928 unsigned int r_type,
2929 const elfcpp::Sym<size, false>& lsym,
2935 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2936 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2937 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2939 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2940 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2945 case elfcpp::R_X86_64_NONE:
2946 case elfcpp::R_X86_64_GNU_VTINHERIT:
2947 case elfcpp::R_X86_64_GNU_VTENTRY:
2950 case elfcpp::R_X86_64_64:
2951 // If building a shared library (or a position-independent
2952 // executable), we need to create a dynamic relocation for this
2953 // location. The relocation applied at link time will apply the
2954 // link-time value, so we flag the location with an
2955 // R_X86_64_RELATIVE relocation so the dynamic loader can
2956 // relocate it easily.
2957 if (parameters->options().output_is_position_independent())
2959 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2960 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2961 rela_dyn->add_local_relative(object, r_sym,
2963 ? elfcpp::R_X86_64_RELATIVE64
2964 : elfcpp::R_X86_64_RELATIVE),
2965 output_section, data_shndx,
2966 reloc.get_r_offset(),
2967 reloc.get_r_addend(), is_ifunc);
2971 case elfcpp::R_X86_64_32:
2972 case elfcpp::R_X86_64_32S:
2973 case elfcpp::R_X86_64_16:
2974 case elfcpp::R_X86_64_8:
2975 // If building a shared library (or a position-independent
2976 // executable), we need to create a dynamic relocation for this
2977 // location. We can't use an R_X86_64_RELATIVE relocation
2978 // because that is always a 64-bit relocation.
2979 if (parameters->options().output_is_position_independent())
2981 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2982 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2984 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2985 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2986 rela_dyn->add_local_relative(object, r_sym,
2987 elfcpp::R_X86_64_RELATIVE,
2988 output_section, data_shndx,
2989 reloc.get_r_offset(),
2990 reloc.get_r_addend(), is_ifunc);
2994 this->check_non_pic(object, r_type, NULL);
2996 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2997 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2998 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2999 rela_dyn->add_local(object, r_sym, r_type, output_section,
3000 data_shndx, reloc.get_r_offset(),
3001 reloc.get_r_addend());
3004 gold_assert(lsym.get_st_value() == 0);
3005 unsigned int shndx = lsym.get_st_shndx();
3007 shndx = object->adjust_sym_shndx(r_sym, shndx,
3010 object->error(_("section symbol %u has bad shndx %u"),
3013 rela_dyn->add_local_section(object, shndx,
3014 r_type, output_section,
3015 data_shndx, reloc.get_r_offset(),
3016 reloc.get_r_addend());
3021 case elfcpp::R_X86_64_PC64:
3022 case elfcpp::R_X86_64_PC32:
3023 case elfcpp::R_X86_64_PC32_BND:
3024 case elfcpp::R_X86_64_PC16:
3025 case elfcpp::R_X86_64_PC8:
3028 case elfcpp::R_X86_64_PLT32:
3029 case elfcpp::R_X86_64_PLT32_BND:
3030 // Since we know this is a local symbol, we can handle this as a
3034 case elfcpp::R_X86_64_GOTPC32:
3035 case elfcpp::R_X86_64_GOTOFF64:
3036 case elfcpp::R_X86_64_GOTPC64:
3037 case elfcpp::R_X86_64_PLTOFF64:
3038 // We need a GOT section.
3039 target->got_section(symtab, layout);
3040 // For PLTOFF64, we'd normally want a PLT section, but since we
3041 // know this is a local symbol, no PLT is needed.
3044 case elfcpp::R_X86_64_GOT64:
3045 case elfcpp::R_X86_64_GOT32:
3046 case elfcpp::R_X86_64_GOTPCREL64:
3047 case elfcpp::R_X86_64_GOTPCREL:
3048 case elfcpp::R_X86_64_GOTPCRELX:
3049 case elfcpp::R_X86_64_REX_GOTPCRELX:
3050 case elfcpp::R_X86_64_GOTPLT64:
3052 // The symbol requires a GOT section.
3053 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3055 // If the relocation symbol isn't IFUNC,
3056 // and is local, then we will convert
3057 // mov foo@GOTPCREL(%rip), %reg
3058 // to lea foo(%rip), %reg.
3059 // in Relocate::relocate.
3060 if ((r_type == elfcpp::R_X86_64_GOTPCREL
3061 || r_type == elfcpp::R_X86_64_GOTPCRELX
3062 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3063 && reloc.get_r_offset() >= 2
3066 section_size_type stype;
3067 const unsigned char* view = object->section_contents(data_shndx,
3069 if (view[reloc.get_r_offset() - 2] == 0x8b)
3074 // The symbol requires a GOT entry.
3075 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3077 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3078 // lets function pointers compare correctly with shared
3079 // libraries. Otherwise we would need an IRELATIVE reloc.
3082 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3084 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3087 // If we are generating a shared object, we need to add a
3088 // dynamic relocation for this symbol's GOT entry.
3089 if (parameters->options().output_is_position_independent())
3091 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3092 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3093 if (r_type != elfcpp::R_X86_64_GOT32)
3095 unsigned int got_offset =
3096 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3097 rela_dyn->add_local_relative(object, r_sym,
3098 elfcpp::R_X86_64_RELATIVE,
3099 got, got_offset, 0, is_ifunc);
3103 this->check_non_pic(object, r_type, NULL);
3105 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3106 rela_dyn->add_local(
3107 object, r_sym, r_type, got,
3108 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3112 // For GOTPLT64, we'd normally want a PLT section, but since
3113 // we know this is a local symbol, no PLT is needed.
3117 case elfcpp::R_X86_64_COPY:
3118 case elfcpp::R_X86_64_GLOB_DAT:
3119 case elfcpp::R_X86_64_JUMP_SLOT:
3120 case elfcpp::R_X86_64_RELATIVE:
3121 case elfcpp::R_X86_64_IRELATIVE:
3122 // These are outstanding tls relocs, which are unexpected when linking
3123 case elfcpp::R_X86_64_TPOFF64:
3124 case elfcpp::R_X86_64_DTPMOD64:
3125 case elfcpp::R_X86_64_TLSDESC:
3126 gold_error(_("%s: unexpected reloc %u in object file"),
3127 object->name().c_str(), r_type);
3130 // These are initial tls relocs, which are expected when linking
3131 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3132 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3133 case elfcpp::R_X86_64_TLSDESC_CALL:
3134 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3135 case elfcpp::R_X86_64_DTPOFF32:
3136 case elfcpp::R_X86_64_DTPOFF64:
3137 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3138 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3140 bool output_is_shared = parameters->options().shared();
3141 const tls::Tls_optimization optimized_type
3142 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3146 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3147 if (optimized_type == tls::TLSOPT_NONE)
3149 // Create a pair of GOT entries for the module index and
3150 // dtv-relative offset.
3151 Output_data_got<64, false>* got
3152 = target->got_section(symtab, layout);
3153 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3154 unsigned int shndx = lsym.get_st_shndx();
3156 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3158 object->error(_("local symbol %u has bad shndx %u"),
3161 got->add_local_pair_with_rel(object, r_sym,
3164 target->rela_dyn_section(layout),
3165 elfcpp::R_X86_64_DTPMOD64);
3167 else if (optimized_type != tls::TLSOPT_TO_LE)
3168 unsupported_reloc_local(object, r_type);
3171 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3172 target->define_tls_base_symbol(symtab, layout);
3173 if (optimized_type == tls::TLSOPT_NONE)
3175 // Create reserved PLT and GOT entries for the resolver.
3176 target->reserve_tlsdesc_entries(symtab, layout);
3178 // Generate a double GOT entry with an
3179 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3180 // is resolved lazily, so the GOT entry needs to be in
3181 // an area in .got.plt, not .got. Call got_section to
3182 // make sure the section has been created.
3183 target->got_section(symtab, layout);
3184 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3185 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3186 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3188 unsigned int got_offset = got->add_constant(0);
3189 got->add_constant(0);
3190 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3192 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3193 // We store the arguments we need in a vector, and
3194 // use the index into the vector as the parameter
3195 // to pass to the target specific routines.
3196 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3197 void* arg = reinterpret_cast<void*>(intarg);
3198 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3199 got, got_offset, 0);
3202 else if (optimized_type != tls::TLSOPT_TO_LE)
3203 unsupported_reloc_local(object, r_type);
3206 case elfcpp::R_X86_64_TLSDESC_CALL:
3209 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3210 if (optimized_type == tls::TLSOPT_NONE)
3212 // Create a GOT entry for the module index.
3213 target->got_mod_index_entry(symtab, layout, object);
3215 else if (optimized_type != tls::TLSOPT_TO_LE)
3216 unsupported_reloc_local(object, r_type);
3219 case elfcpp::R_X86_64_DTPOFF32:
3220 case elfcpp::R_X86_64_DTPOFF64:
3223 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3224 layout->set_has_static_tls();
3225 if (optimized_type == tls::TLSOPT_NONE)
3227 // Create a GOT entry for the tp-relative offset.
3228 Output_data_got<64, false>* got
3229 = target->got_section(symtab, layout);
3230 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3231 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3232 target->rela_dyn_section(layout),
3233 elfcpp::R_X86_64_TPOFF64);
3235 else if (optimized_type != tls::TLSOPT_TO_LE)
3236 unsupported_reloc_local(object, r_type);
3239 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3240 layout->set_has_static_tls();
3241 if (output_is_shared)
3242 unsupported_reloc_local(object, r_type);
3251 case elfcpp::R_X86_64_SIZE32:
3252 case elfcpp::R_X86_64_SIZE64:
3254 gold_error(_("%s: unsupported reloc %u against local symbol"),
3255 object->name().c_str(), r_type);
3261 // Report an unsupported relocation against a global symbol.
3265 Target_x86_64<size>::Scan::unsupported_reloc_global(
3266 Sized_relobj_file<size, false>* object,
3267 unsigned int r_type,
3270 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3271 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3274 // Returns true if this relocation type could be that of a function pointer.
3277 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
3281 case elfcpp::R_X86_64_64:
3282 case elfcpp::R_X86_64_32:
3283 case elfcpp::R_X86_64_32S:
3284 case elfcpp::R_X86_64_16:
3285 case elfcpp::R_X86_64_8:
3286 case elfcpp::R_X86_64_GOT64:
3287 case elfcpp::R_X86_64_GOT32:
3288 case elfcpp::R_X86_64_GOTPCREL64:
3289 case elfcpp::R_X86_64_GOTPCREL:
3290 case elfcpp::R_X86_64_GOTPCRELX:
3291 case elfcpp::R_X86_64_REX_GOTPCRELX:
3292 case elfcpp::R_X86_64_GOTPLT64:
3300 // For safe ICF, scan a relocation for a local symbol to check if it
3301 // corresponds to a function pointer being taken. In that case mark
3302 // the function whose pointer was taken as not foldable.
3306 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3309 Target_x86_64<size>* ,
3310 Sized_relobj_file<size, false>* ,
3313 const elfcpp::Rela<size, false>& ,
3314 unsigned int r_type,
3315 const elfcpp::Sym<size, false>&)
3317 // When building a shared library, do not fold any local symbols as it is
3318 // not possible to distinguish pointer taken versus a call by looking at
3319 // the relocation types.
3320 return (parameters->options().shared()
3321 || possible_function_pointer_reloc(r_type));
3324 // For safe ICF, scan a relocation for a global symbol to check if it
3325 // corresponds to a function pointer being taken. In that case mark
3326 // the function whose pointer was taken as not foldable.
3330 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3333 Target_x86_64<size>* ,
3334 Sized_relobj_file<size, false>* ,
3337 const elfcpp::Rela<size, false>& ,
3338 unsigned int r_type,
3341 // When building a shared library, do not fold symbols whose visibility
3342 // is hidden, internal or protected.
3343 return ((parameters->options().shared()
3344 && (gsym->visibility() == elfcpp::STV_INTERNAL
3345 || gsym->visibility() == elfcpp::STV_PROTECTED
3346 || gsym->visibility() == elfcpp::STV_HIDDEN))
3347 || possible_function_pointer_reloc(r_type));
3350 // Scan a relocation for a global symbol.
3354 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3356 Target_x86_64<size>* target,
3357 Sized_relobj_file<size, false>* object,
3358 unsigned int data_shndx,
3359 Output_section* output_section,
3360 const elfcpp::Rela<size, false>& reloc,
3361 unsigned int r_type,
3364 // A STT_GNU_IFUNC symbol may require a PLT entry.
3365 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3366 && this->reloc_needs_plt_for_ifunc(object, r_type))
3367 target->make_plt_entry(symtab, layout, gsym);
3371 case elfcpp::R_X86_64_NONE:
3372 case elfcpp::R_X86_64_GNU_VTINHERIT:
3373 case elfcpp::R_X86_64_GNU_VTENTRY:
3376 case elfcpp::R_X86_64_64:
3377 case elfcpp::R_X86_64_32:
3378 case elfcpp::R_X86_64_32S:
3379 case elfcpp::R_X86_64_16:
3380 case elfcpp::R_X86_64_8:
3382 // Make a PLT entry if necessary.
3383 if (gsym->needs_plt_entry())
3385 target->make_plt_entry(symtab, layout, gsym);
3386 // Since this is not a PC-relative relocation, we may be
3387 // taking the address of a function. In that case we need to
3388 // set the entry in the dynamic symbol table to the address of
3390 if (gsym->is_from_dynobj() && !parameters->options().shared())
3391 gsym->set_needs_dynsym_value();
3393 // Make a dynamic relocation if necessary.
3394 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3396 if (!parameters->options().output_is_position_independent()
3397 && gsym->may_need_copy_reloc())
3399 target->copy_reloc(symtab, layout, object,
3400 data_shndx, output_section, gsym, reloc);
3402 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3403 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3404 && gsym->type() == elfcpp::STT_GNU_IFUNC
3405 && gsym->can_use_relative_reloc(false)
3406 && !gsym->is_from_dynobj()
3407 && !gsym->is_undefined()
3408 && !gsym->is_preemptible())
3410 // Use an IRELATIVE reloc for a locally defined
3411 // STT_GNU_IFUNC symbol. This makes a function
3412 // address in a PIE executable match the address in a
3413 // shared library that it links against.
3414 Reloc_section* rela_dyn =
3415 target->rela_irelative_section(layout);
3416 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3417 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3418 output_section, object,
3420 reloc.get_r_offset(),
3421 reloc.get_r_addend());
3423 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3424 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3425 && gsym->can_use_relative_reloc(false))
3427 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3428 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3429 output_section, object,
3431 reloc.get_r_offset(),
3432 reloc.get_r_addend(), false);
3436 this->check_non_pic(object, r_type, gsym);
3437 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3438 rela_dyn->add_global(gsym, r_type, output_section, object,
3439 data_shndx, reloc.get_r_offset(),
3440 reloc.get_r_addend());
3446 case elfcpp::R_X86_64_PC64:
3447 case elfcpp::R_X86_64_PC32:
3448 case elfcpp::R_X86_64_PC32_BND:
3449 case elfcpp::R_X86_64_PC16:
3450 case elfcpp::R_X86_64_PC8:
3452 // Make a PLT entry if necessary.
3453 if (gsym->needs_plt_entry())
3454 target->make_plt_entry(symtab, layout, gsym);
3455 // Make a dynamic relocation if necessary.
3456 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3458 if (parameters->options().output_is_executable()
3459 && gsym->may_need_copy_reloc())
3461 target->copy_reloc(symtab, layout, object,
3462 data_shndx, output_section, gsym, reloc);
3466 this->check_non_pic(object, r_type, gsym);
3467 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3468 rela_dyn->add_global(gsym, r_type, output_section, object,
3469 data_shndx, reloc.get_r_offset(),
3470 reloc.get_r_addend());
3476 case elfcpp::R_X86_64_GOT64:
3477 case elfcpp::R_X86_64_GOT32:
3478 case elfcpp::R_X86_64_GOTPCREL64:
3479 case elfcpp::R_X86_64_GOTPCREL:
3480 case elfcpp::R_X86_64_GOTPCRELX:
3481 case elfcpp::R_X86_64_REX_GOTPCRELX:
3482 case elfcpp::R_X86_64_GOTPLT64:
3484 // The symbol requires a GOT entry.
3485 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3487 // If we convert this from
3488 // mov foo@GOTPCREL(%rip), %reg
3489 // to lea foo(%rip), %reg.
3492 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3494 // in Relocate::relocate, then there is nothing to do here.
3496 Lazy_view<size> view(object, data_shndx);
3497 size_t r_offset = reloc.get_r_offset();
3499 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3504 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3509 if (gsym->final_value_is_known())
3511 // For a STT_GNU_IFUNC symbol we want the PLT address.
3512 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3513 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3515 got->add_global(gsym, GOT_TYPE_STANDARD);
3519 // If this symbol is not fully resolved, we need to add a
3520 // dynamic relocation for it.
3521 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3523 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3525 // 1) The symbol may be defined in some other module.
3527 // 2) We are building a shared library and this is a
3528 // protected symbol; using GLOB_DAT means that the dynamic
3529 // linker can use the address of the PLT in the main
3530 // executable when appropriate so that function address
3531 // comparisons work.
3533 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3534 // code, again so that function address comparisons work.
3535 if (gsym->is_from_dynobj()
3536 || gsym->is_undefined()
3537 || gsym->is_preemptible()
3538 || (gsym->visibility() == elfcpp::STV_PROTECTED
3539 && parameters->options().shared())
3540 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3541 && parameters->options().output_is_position_independent()))
3542 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3543 elfcpp::R_X86_64_GLOB_DAT);
3546 // For a STT_GNU_IFUNC symbol we want to write the PLT
3547 // offset into the GOT, so that function pointer
3548 // comparisons work correctly.
3550 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3551 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3554 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3555 // Tell the dynamic linker to use the PLT address
3556 // when resolving relocations.
3557 if (gsym->is_from_dynobj()
3558 && !parameters->options().shared())
3559 gsym->set_needs_dynsym_value();
3563 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3564 rela_dyn->add_global_relative(gsym,
3565 elfcpp::R_X86_64_RELATIVE,
3566 got, got_off, 0, false);
3573 case elfcpp::R_X86_64_PLT32:
3574 case elfcpp::R_X86_64_PLT32_BND:
3575 // If the symbol is fully resolved, this is just a PC32 reloc.
3576 // Otherwise we need a PLT entry.
3577 if (gsym->final_value_is_known())
3579 // If building a shared library, we can also skip the PLT entry
3580 // if the symbol is defined in the output file and is protected
3582 if (gsym->is_defined()
3583 && !gsym->is_from_dynobj()
3584 && !gsym->is_preemptible())
3586 target->make_plt_entry(symtab, layout, gsym);
3589 case elfcpp::R_X86_64_GOTPC32:
3590 case elfcpp::R_X86_64_GOTOFF64:
3591 case elfcpp::R_X86_64_GOTPC64:
3592 case elfcpp::R_X86_64_PLTOFF64:
3593 // We need a GOT section.
3594 target->got_section(symtab, layout);
3595 // For PLTOFF64, we also need a PLT entry (but only if the
3596 // symbol is not fully resolved).
3597 if (r_type == elfcpp::R_X86_64_PLTOFF64
3598 && !gsym->final_value_is_known())
3599 target->make_plt_entry(symtab, layout, gsym);
3602 case elfcpp::R_X86_64_COPY:
3603 case elfcpp::R_X86_64_GLOB_DAT:
3604 case elfcpp::R_X86_64_JUMP_SLOT:
3605 case elfcpp::R_X86_64_RELATIVE:
3606 case elfcpp::R_X86_64_IRELATIVE:
3607 // These are outstanding tls relocs, which are unexpected when linking
3608 case elfcpp::R_X86_64_TPOFF64:
3609 case elfcpp::R_X86_64_DTPMOD64:
3610 case elfcpp::R_X86_64_TLSDESC:
3611 gold_error(_("%s: unexpected reloc %u in object file"),
3612 object->name().c_str(), r_type);
3615 // These are initial tls relocs, which are expected for global()
3616 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3617 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3618 case elfcpp::R_X86_64_TLSDESC_CALL:
3619 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3620 case elfcpp::R_X86_64_DTPOFF32:
3621 case elfcpp::R_X86_64_DTPOFF64:
3622 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3623 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3625 // For the Initial-Exec model, we can treat undef symbols as final
3626 // when building an executable.
3627 const bool is_final = (gsym->final_value_is_known() ||
3628 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3629 gsym->is_undefined() &&
3630 parameters->options().output_is_executable()));
3631 const tls::Tls_optimization optimized_type
3632 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3635 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3636 if (optimized_type == tls::TLSOPT_NONE)
3638 // Create a pair of GOT entries for the module index and
3639 // dtv-relative offset.
3640 Output_data_got<64, false>* got
3641 = target->got_section(symtab, layout);
3642 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3643 target->rela_dyn_section(layout),
3644 elfcpp::R_X86_64_DTPMOD64,
3645 elfcpp::R_X86_64_DTPOFF64);
3647 else if (optimized_type == tls::TLSOPT_TO_IE)
3649 // Create a GOT entry for the tp-relative offset.
3650 Output_data_got<64, false>* got
3651 = target->got_section(symtab, layout);
3652 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3653 target->rela_dyn_section(layout),
3654 elfcpp::R_X86_64_TPOFF64);
3656 else if (optimized_type != tls::TLSOPT_TO_LE)
3657 unsupported_reloc_global(object, r_type, gsym);
3660 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3661 target->define_tls_base_symbol(symtab, layout);
3662 if (optimized_type == tls::TLSOPT_NONE)
3664 // Create reserved PLT and GOT entries for the resolver.
3665 target->reserve_tlsdesc_entries(symtab, layout);
3667 // Create a double GOT entry with an R_X86_64_TLSDESC
3668 // reloc. The R_X86_64_TLSDESC reloc is resolved
3669 // lazily, so the GOT entry needs to be in an area in
3670 // .got.plt, not .got. Call got_section to make sure
3671 // the section has been created.
3672 target->got_section(symtab, layout);
3673 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3674 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3675 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3676 elfcpp::R_X86_64_TLSDESC, 0);
3678 else if (optimized_type == tls::TLSOPT_TO_IE)
3680 // Create a GOT entry for the tp-relative offset.
3681 Output_data_got<64, false>* got
3682 = target->got_section(symtab, layout);
3683 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3684 target->rela_dyn_section(layout),
3685 elfcpp::R_X86_64_TPOFF64);
3687 else if (optimized_type != tls::TLSOPT_TO_LE)
3688 unsupported_reloc_global(object, r_type, gsym);
3691 case elfcpp::R_X86_64_TLSDESC_CALL:
3694 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3695 if (optimized_type == tls::TLSOPT_NONE)
3697 // Create a GOT entry for the module index.
3698 target->got_mod_index_entry(symtab, layout, object);
3700 else if (optimized_type != tls::TLSOPT_TO_LE)
3701 unsupported_reloc_global(object, r_type, gsym);
3704 case elfcpp::R_X86_64_DTPOFF32:
3705 case elfcpp::R_X86_64_DTPOFF64:
3708 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3709 layout->set_has_static_tls();
3710 if (optimized_type == tls::TLSOPT_NONE)
3712 // Create a GOT entry for the tp-relative offset.
3713 Output_data_got<64, false>* got
3714 = target->got_section(symtab, layout);
3715 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3716 target->rela_dyn_section(layout),
3717 elfcpp::R_X86_64_TPOFF64);
3719 else if (optimized_type != tls::TLSOPT_TO_LE)
3720 unsupported_reloc_global(object, r_type, gsym);
3723 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3724 layout->set_has_static_tls();
3725 if (parameters->options().shared())
3726 unsupported_reloc_global(object, r_type, gsym);
3735 case elfcpp::R_X86_64_SIZE32:
3736 case elfcpp::R_X86_64_SIZE64:
3738 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3739 object->name().c_str(), r_type,
3740 gsym->demangled_name().c_str());
3747 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3749 Sized_relobj_file<size, false>* object,
3750 unsigned int data_shndx,
3751 unsigned int sh_type,
3752 const unsigned char* prelocs,
3754 Output_section* output_section,
3755 bool needs_special_offset_handling,
3756 size_t local_symbol_count,
3757 const unsigned char* plocal_symbols)
3759 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3762 if (sh_type == elfcpp::SHT_REL)
3767 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3777 needs_special_offset_handling,
3782 // Scan relocations for a section.
3786 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3788 Sized_relobj_file<size, false>* object,
3789 unsigned int data_shndx,
3790 unsigned int sh_type,
3791 const unsigned char* prelocs,
3793 Output_section* output_section,
3794 bool needs_special_offset_handling,
3795 size_t local_symbol_count,
3796 const unsigned char* plocal_symbols)
3798 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3801 if (sh_type == elfcpp::SHT_REL)
3803 gold_error(_("%s: unsupported REL reloc section"),
3804 object->name().c_str());
3808 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3817 needs_special_offset_handling,
3822 // Finalize the sections.
3826 Target_x86_64<size>::do_finalize_sections(
3828 const Input_objects*,
3829 Symbol_table* symtab)
3831 const Reloc_section* rel_plt = (this->plt_ == NULL
3833 : this->plt_->rela_plt());
3834 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3835 this->rela_dyn_, true, false);
3837 // Fill in some more dynamic tags.
3838 Output_data_dynamic* const odyn = layout->dynamic_data();
3841 if (this->plt_ != NULL
3842 && this->plt_->output_section() != NULL
3843 && this->plt_->has_tlsdesc_entry())
3845 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3846 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3847 this->got_->finalize_data_size();
3848 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3849 this->plt_, plt_offset);
3850 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3851 this->got_, got_offset);
3855 // Emit any relocs we saved in an attempt to avoid generating COPY
3857 if (this->copy_relocs_.any_saved_relocs())
3858 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3860 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3861 // the .got.plt section.
3862 Symbol* sym = this->global_offset_table_;
3865 uint64_t data_size = this->got_plt_->current_data_size();
3866 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3869 if (parameters->doing_static_link()
3870 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3872 // If linking statically, make sure that the __rela_iplt symbols
3873 // were defined if necessary, even if we didn't create a PLT.
3874 static const Define_symbol_in_segment syms[] =
3877 "__rela_iplt_start", // name
3878 elfcpp::PT_LOAD, // segment_type
3879 elfcpp::PF_W, // segment_flags_set
3880 elfcpp::PF(0), // segment_flags_clear
3883 elfcpp::STT_NOTYPE, // type
3884 elfcpp::STB_GLOBAL, // binding
3885 elfcpp::STV_HIDDEN, // visibility
3887 Symbol::SEGMENT_START, // offset_from_base
3891 "__rela_iplt_end", // name
3892 elfcpp::PT_LOAD, // segment_type
3893 elfcpp::PF_W, // segment_flags_set
3894 elfcpp::PF(0), // segment_flags_clear
3897 elfcpp::STT_NOTYPE, // type
3898 elfcpp::STB_GLOBAL, // binding
3899 elfcpp::STV_HIDDEN, // visibility
3901 Symbol::SEGMENT_START, // offset_from_base
3906 symtab->define_symbols(layout, 2, syms,
3907 layout->script_options()->saw_sections_clause());
3911 // For x32, we need to handle PC-relative relocations using full 64-bit
3912 // arithmetic, so that we can detect relocation overflows properly.
3913 // This class overrides the pcrela32_check methods from the defaults in
3914 // Relocate_functions in reloc.h.
3917 class X86_64_relocate_functions : public Relocate_functions<size, false>
3920 typedef Relocate_functions<size, false> Base;
3922 // Do a simple PC relative relocation with the addend in the
3924 static inline typename Base::Reloc_status
3925 pcrela32_check(unsigned char* view,
3926 typename elfcpp::Elf_types<64>::Elf_Addr value,
3927 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3928 typename elfcpp::Elf_types<64>::Elf_Addr address)
3930 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3931 Valtype* wv = reinterpret_cast<Valtype*>(view);
3932 value = value + addend - address;
3933 elfcpp::Swap<32, false>::writeval(wv, value);
3934 return (Bits<32>::has_overflow(value)
3935 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3938 // Do a simple PC relative relocation with a Symbol_value with the
3939 // addend in the relocation.
3940 static inline typename Base::Reloc_status
3941 pcrela32_check(unsigned char* view,
3942 const Sized_relobj_file<size, false>* object,
3943 const Symbol_value<size>* psymval,
3944 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3945 typename elfcpp::Elf_types<64>::Elf_Addr address)
3947 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3948 Valtype* wv = reinterpret_cast<Valtype*>(view);
3949 typename elfcpp::Elf_types<64>::Elf_Addr value;
3951 value = psymval->value(object, addend);
3954 // For negative addends, get the symbol value without
3955 // the addend, then add the addend using 64-bit arithmetic.
3956 value = psymval->value(object, 0);
3960 elfcpp::Swap<32, false>::writeval(wv, value);
3961 return (Bits<32>::has_overflow(value)
3962 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3966 // Perform a relocation.
3970 Target_x86_64<size>::Relocate::relocate(
3971 const Relocate_info<size, false>* relinfo,
3973 Target_x86_64<size>* target,
3976 const unsigned char* preloc,
3977 const Sized_symbol<size>* gsym,
3978 const Symbol_value<size>* psymval,
3979 unsigned char* view,
3980 typename elfcpp::Elf_types<size>::Elf_Addr address,
3981 section_size_type view_size)
3983 typedef X86_64_relocate_functions<size> Reloc_funcs;
3984 const elfcpp::Rela<size, false> rela(preloc);
3985 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3987 if (this->skip_call_tls_get_addr_)
3989 if ((r_type != elfcpp::R_X86_64_PLT32
3990 && r_type != elfcpp::R_X86_64_GOTPCREL
3991 && r_type != elfcpp::R_X86_64_GOTPCRELX
3992 && r_type != elfcpp::R_X86_64_PLT32_BND
3993 && r_type != elfcpp::R_X86_64_PC32_BND
3994 && r_type != elfcpp::R_X86_64_PC32)
3996 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3998 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3999 _("missing expected TLS relocation"));
4000 this->skip_call_tls_get_addr_ = false;
4004 this->skip_call_tls_get_addr_ = false;
4012 const Sized_relobj_file<size, false>* object = relinfo->object;
4014 // Pick the value to use for symbols defined in the PLT.
4015 Symbol_value<size> symval;
4017 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4019 symval.set_output_value(target->plt_address_for_global(gsym));
4022 else if (gsym == NULL && psymval->is_ifunc_symbol())
4024 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4025 if (object->local_has_plt_offset(r_sym))
4027 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4032 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4034 // Get the GOT offset if needed.
4035 // The GOT pointer points to the end of the GOT section.
4036 // We need to subtract the size of the GOT section to get
4037 // the actual offset to use in the relocation.
4038 bool have_got_offset = false;
4039 // Since the actual offset is always negative, we use signed int to
4040 // support 64-bit GOT relocations.
4044 case elfcpp::R_X86_64_GOT32:
4045 case elfcpp::R_X86_64_GOT64:
4046 case elfcpp::R_X86_64_GOTPLT64:
4047 case elfcpp::R_X86_64_GOTPCREL64:
4050 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4051 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4055 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4056 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4057 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4058 - target->got_size());
4060 have_got_offset = true;
4067 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4071 case elfcpp::R_X86_64_NONE:
4072 case elfcpp::R_X86_64_GNU_VTINHERIT:
4073 case elfcpp::R_X86_64_GNU_VTENTRY:
4076 case elfcpp::R_X86_64_64:
4077 Reloc_funcs::rela64(view, object, psymval, addend);
4080 case elfcpp::R_X86_64_PC64:
4081 Reloc_funcs::pcrela64(view, object, psymval, addend,
4085 case elfcpp::R_X86_64_32:
4086 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4087 Reloc_funcs::CHECK_UNSIGNED);
4090 case elfcpp::R_X86_64_32S:
4091 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4092 Reloc_funcs::CHECK_SIGNED);
4095 case elfcpp::R_X86_64_PC32:
4096 case elfcpp::R_X86_64_PC32_BND:
4097 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4101 case elfcpp::R_X86_64_16:
4102 Reloc_funcs::rela16(view, object, psymval, addend);
4105 case elfcpp::R_X86_64_PC16:
4106 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4109 case elfcpp::R_X86_64_8:
4110 Reloc_funcs::rela8(view, object, psymval, addend);
4113 case elfcpp::R_X86_64_PC8:
4114 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4117 case elfcpp::R_X86_64_PLT32:
4118 case elfcpp::R_X86_64_PLT32_BND:
4119 gold_assert(gsym == NULL
4120 || gsym->has_plt_offset()
4121 || gsym->final_value_is_known()
4122 || (gsym->is_defined()
4123 && !gsym->is_from_dynobj()
4124 && !gsym->is_preemptible()));
4125 // Note: while this code looks the same as for R_X86_64_PC32, it
4126 // behaves differently because psymval was set to point to
4127 // the PLT entry, rather than the symbol, in Scan::global().
4128 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4132 case elfcpp::R_X86_64_PLTOFF64:
4135 gold_assert(gsym->has_plt_offset()
4136 || gsym->final_value_is_known());
4137 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4138 // This is the address of GLOBAL_OFFSET_TABLE.
4139 got_address = target->got_plt_section()->address();
4140 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4144 case elfcpp::R_X86_64_GOT32:
4145 gold_assert(have_got_offset);
4146 Reloc_funcs::rela32(view, got_offset, addend);
4149 case elfcpp::R_X86_64_GOTPC32:
4152 typename elfcpp::Elf_types<size>::Elf_Addr value;
4153 value = target->got_plt_section()->address();
4154 Reloc_funcs::pcrela32_check(view, value, addend, address);
4158 case elfcpp::R_X86_64_GOT64:
4159 case elfcpp::R_X86_64_GOTPLT64:
4160 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
4162 gold_assert(have_got_offset);
4163 Reloc_funcs::rela64(view, got_offset, addend);
4166 case elfcpp::R_X86_64_GOTPC64:
4169 typename elfcpp::Elf_types<size>::Elf_Addr value;
4170 value = target->got_plt_section()->address();
4171 Reloc_funcs::pcrela64(view, value, addend, address);
4175 case elfcpp::R_X86_64_GOTOFF64:
4177 typename elfcpp::Elf_types<size>::Elf_Addr value;
4178 value = (psymval->value(object, 0)
4179 - target->got_plt_section()->address());
4180 Reloc_funcs::rela64(view, value, addend);
4184 case elfcpp::R_X86_64_GOTPCREL:
4185 case elfcpp::R_X86_64_GOTPCRELX:
4186 case elfcpp::R_X86_64_REX_GOTPCRELX:
4189 // mov foo@GOTPCREL(%rip), %reg
4190 // to lea foo(%rip), %reg.
4193 && rela.get_r_offset() >= 2
4195 && !psymval->is_ifunc_symbol())
4197 && rela.get_r_offset() >= 2
4198 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4202 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4205 // callq *foo@GOTPCRELX(%rip) to
4207 // and jmpq *foo@GOTPCRELX(%rip) to
4210 else if (gsym != NULL
4211 && rela.get_r_offset() >= 2
4212 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4216 if (view[-1] == 0x15)
4218 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4219 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4222 // Convert GOTPCRELX to 32-bit pc relative reloc.
4223 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4227 // Convert jmpq *foo@GOTPCRELX(%rip) to
4230 // The opcode of direct jmpq is 0xe9.
4232 // The opcode of nop is 0x90.
4234 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4235 // relative and since the instruction following the jmpq is now
4236 // the nop, offset the address by 1 byte. The start of the
4237 // relocation also moves ahead by 1 byte.
4238 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4246 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4247 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4251 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4252 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4253 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4254 - target->got_size());
4256 typename elfcpp::Elf_types<size>::Elf_Addr value;
4257 value = target->got_plt_section()->address() + got_offset;
4258 Reloc_funcs::pcrela32_check(view, value, addend, address);
4263 case elfcpp::R_X86_64_GOTPCREL64:
4265 gold_assert(have_got_offset);
4266 typename elfcpp::Elf_types<size>::Elf_Addr value;
4267 value = target->got_plt_section()->address() + got_offset;
4268 Reloc_funcs::pcrela64(view, value, addend, address);
4272 case elfcpp::R_X86_64_COPY:
4273 case elfcpp::R_X86_64_GLOB_DAT:
4274 case elfcpp::R_X86_64_JUMP_SLOT:
4275 case elfcpp::R_X86_64_RELATIVE:
4276 case elfcpp::R_X86_64_IRELATIVE:
4277 // These are outstanding tls relocs, which are unexpected when linking
4278 case elfcpp::R_X86_64_TPOFF64:
4279 case elfcpp::R_X86_64_DTPMOD64:
4280 case elfcpp::R_X86_64_TLSDESC:
4281 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4282 _("unexpected reloc %u in object file"),
4286 // These are initial tls relocs, which are expected when linking
4287 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4288 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4289 case elfcpp::R_X86_64_TLSDESC_CALL:
4290 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4291 case elfcpp::R_X86_64_DTPOFF32:
4292 case elfcpp::R_X86_64_DTPOFF64:
4293 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4294 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4295 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4296 view, address, view_size);
4299 case elfcpp::R_X86_64_SIZE32:
4300 case elfcpp::R_X86_64_SIZE64:
4302 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4303 _("unsupported reloc %u"),
4308 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4312 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4313 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4314 _("relocation overflow: "
4315 "reference to local symbol %u in %s"),
4316 r_sym, object->name().c_str());
4318 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4320 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4321 _("relocation overflow: "
4322 "reference to '%s' defined in %s"),
4324 gsym->object()->name().c_str());
4328 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4329 _("relocation overflow: reference to '%s'"),
4337 // Perform a TLS relocation.
4341 Target_x86_64<size>::Relocate::relocate_tls(
4342 const Relocate_info<size, false>* relinfo,
4343 Target_x86_64<size>* target,
4345 const elfcpp::Rela<size, false>& rela,
4346 unsigned int r_type,
4347 const Sized_symbol<size>* gsym,
4348 const Symbol_value<size>* psymval,
4349 unsigned char* view,
4350 typename elfcpp::Elf_types<size>::Elf_Addr address,
4351 section_size_type view_size)
4353 Output_segment* tls_segment = relinfo->layout->tls_segment();
4355 const Sized_relobj_file<size, false>* object = relinfo->object;
4356 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4357 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4358 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4360 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4362 const bool is_final = (gsym == NULL
4363 ? !parameters->options().shared()
4364 : gsym->final_value_is_known());
4365 tls::Tls_optimization optimized_type
4366 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4369 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4370 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4372 // If this code sequence is used in a non-executable section,
4373 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4374 // on the assumption that it's being used by itself in a debug
4375 // section. Therefore, in the unlikely event that the code
4376 // sequence appears in a non-executable section, we simply
4377 // leave it unoptimized.
4378 optimized_type = tls::TLSOPT_NONE;
4380 if (optimized_type == tls::TLSOPT_TO_LE)
4382 if (tls_segment == NULL)
4384 gold_assert(parameters->errors()->error_count() > 0
4385 || issue_undefined_symbol_error(gsym));
4388 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4389 rela, r_type, value, view,
4395 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4396 ? GOT_TYPE_TLS_OFFSET
4397 : GOT_TYPE_TLS_PAIR);
4398 unsigned int got_offset;
4401 gold_assert(gsym->has_got_offset(got_type));
4402 got_offset = gsym->got_offset(got_type) - target->got_size();
4406 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4407 gold_assert(object->local_has_got_offset(r_sym, got_type));
4408 got_offset = (object->local_got_offset(r_sym, got_type)
4409 - target->got_size());
4411 if (optimized_type == tls::TLSOPT_TO_IE)
4413 value = target->got_plt_section()->address() + got_offset;
4414 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4415 value, view, address, view_size);
4418 else if (optimized_type == tls::TLSOPT_NONE)
4420 // Relocate the field with the offset of the pair of GOT
4422 value = target->got_plt_section()->address() + got_offset;
4423 Relocate_functions<size, false>::pcrela32(view, value, addend,
4428 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4429 _("unsupported reloc %u"), r_type);
4432 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4433 case elfcpp::R_X86_64_TLSDESC_CALL:
4434 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4436 // See above comment for R_X86_64_TLSGD.
4437 optimized_type = tls::TLSOPT_NONE;
4439 if (optimized_type == tls::TLSOPT_TO_LE)
4441 if (tls_segment == NULL)
4443 gold_assert(parameters->errors()->error_count() > 0
4444 || issue_undefined_symbol_error(gsym));
4447 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4448 rela, r_type, value, view,
4454 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4455 ? GOT_TYPE_TLS_OFFSET
4456 : GOT_TYPE_TLS_DESC);
4457 unsigned int got_offset = 0;
4458 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4459 && optimized_type == tls::TLSOPT_NONE)
4461 // We created GOT entries in the .got.tlsdesc portion of
4462 // the .got.plt section, but the offset stored in the
4463 // symbol is the offset within .got.tlsdesc.
4464 got_offset = (target->got_size()
4465 + target->got_plt_section()->data_size());
4469 gold_assert(gsym->has_got_offset(got_type));
4470 got_offset += gsym->got_offset(got_type) - target->got_size();
4474 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4475 gold_assert(object->local_has_got_offset(r_sym, got_type));
4476 got_offset += (object->local_got_offset(r_sym, got_type)
4477 - target->got_size());
4479 if (optimized_type == tls::TLSOPT_TO_IE)
4481 value = target->got_plt_section()->address() + got_offset;
4482 this->tls_desc_gd_to_ie(relinfo, relnum,
4483 rela, r_type, value, view, address,
4487 else if (optimized_type == tls::TLSOPT_NONE)
4489 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4491 // Relocate the field with the offset of the pair of GOT
4493 value = target->got_plt_section()->address() + got_offset;
4494 Relocate_functions<size, false>::pcrela32(view, value, addend,
4500 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4501 _("unsupported reloc %u"), r_type);
4504 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4505 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4507 // See above comment for R_X86_64_TLSGD.
4508 optimized_type = tls::TLSOPT_NONE;
4510 if (optimized_type == tls::TLSOPT_TO_LE)
4512 if (tls_segment == NULL)
4514 gold_assert(parameters->errors()->error_count() > 0
4515 || issue_undefined_symbol_error(gsym));
4518 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4519 value, view, view_size);
4522 else if (optimized_type == tls::TLSOPT_NONE)
4524 // Relocate the field with the offset of the GOT entry for
4525 // the module index.
4526 unsigned int got_offset;
4527 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4528 - target->got_size());
4529 value = target->got_plt_section()->address() + got_offset;
4530 Relocate_functions<size, false>::pcrela32(view, value, addend,
4534 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4535 _("unsupported reloc %u"), r_type);
4538 case elfcpp::R_X86_64_DTPOFF32:
4539 // This relocation type is used in debugging information.
4540 // In that case we need to not optimize the value. If the
4541 // section is not executable, then we assume we should not
4542 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4543 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4545 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4547 if (tls_segment == NULL)
4549 gold_assert(parameters->errors()->error_count() > 0
4550 || issue_undefined_symbol_error(gsym));
4553 value -= tls_segment->memsz();
4555 Relocate_functions<size, false>::rela32(view, value, addend);
4558 case elfcpp::R_X86_64_DTPOFF64:
4559 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4560 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4562 if (tls_segment == NULL)
4564 gold_assert(parameters->errors()->error_count() > 0
4565 || issue_undefined_symbol_error(gsym));
4568 value -= tls_segment->memsz();
4570 Relocate_functions<size, false>::rela64(view, value, addend);
4573 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4575 && gsym->is_undefined()
4576 && parameters->options().output_is_executable())
4578 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4580 r_type, value, view,
4584 else if (optimized_type == tls::TLSOPT_TO_LE)
4586 if (tls_segment == NULL)
4588 gold_assert(parameters->errors()->error_count() > 0
4589 || issue_undefined_symbol_error(gsym));
4592 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4594 r_type, value, view,
4598 else if (optimized_type == tls::TLSOPT_NONE)
4600 // Relocate the field with the offset of the GOT entry for
4601 // the tp-relative offset of the symbol.
4602 unsigned int got_offset;
4605 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4606 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4607 - target->got_size());
4611 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4612 gold_assert(object->local_has_got_offset(r_sym,
4613 GOT_TYPE_TLS_OFFSET));
4614 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4615 - target->got_size());
4617 value = target->got_plt_section()->address() + got_offset;
4618 Relocate_functions<size, false>::pcrela32(view, value, addend,
4622 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4623 _("unsupported reloc type %u"),
4627 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4628 if (tls_segment == NULL)
4630 gold_assert(parameters->errors()->error_count() > 0
4631 || issue_undefined_symbol_error(gsym));
4634 value -= tls_segment->memsz();
4635 Relocate_functions<size, false>::rela32(view, value, addend);
4640 // Do a relocation in which we convert a TLS General-Dynamic to an
4645 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4646 const Relocate_info<size, false>* relinfo,
4648 const elfcpp::Rela<size, false>& rela,
4650 typename elfcpp::Elf_types<size>::Elf_Addr value,
4651 unsigned char* view,
4652 typename elfcpp::Elf_types<size>::Elf_Addr address,
4653 section_size_type view_size)
4656 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4657 // .word 0x6666; rex64; call __tls_get_addr@PLT
4658 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4659 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4660 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4661 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4663 // leaq foo@tlsgd(%rip),%rdi;
4664 // .word 0x6666; rex64; call __tls_get_addr@PLT
4665 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4666 // leaq foo@tlsgd(%rip),%rdi;
4667 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4668 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4670 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4671 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4672 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4673 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4677 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4679 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4680 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4681 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4686 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4688 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4689 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4690 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4694 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4695 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4698 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4700 this->skip_call_tls_get_addr_ = true;
4703 // Do a relocation in which we convert a TLS General-Dynamic to a
4708 Target_x86_64<size>::Relocate::tls_gd_to_le(
4709 const Relocate_info<size, false>* relinfo,
4711 Output_segment* tls_segment,
4712 const elfcpp::Rela<size, false>& rela,
4714 typename elfcpp::Elf_types<size>::Elf_Addr value,
4715 unsigned char* view,
4716 section_size_type view_size)
4719 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4720 // .word 0x6666; rex64; call __tls_get_addr@PLT
4721 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4722 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4723 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4724 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4726 // leaq foo@tlsgd(%rip),%rdi;
4727 // .word 0x6666; rex64; call __tls_get_addr@PLT
4728 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4729 // leaq foo@tlsgd(%rip),%rdi;
4730 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4731 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4733 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4734 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4735 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4736 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4740 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4742 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4743 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4744 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4749 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4751 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4752 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4754 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4758 value -= tls_segment->memsz();
4759 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4761 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4763 this->skip_call_tls_get_addr_ = true;
4766 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4770 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4771 const Relocate_info<size, false>* relinfo,
4773 const elfcpp::Rela<size, false>& rela,
4774 unsigned int r_type,
4775 typename elfcpp::Elf_types<size>::Elf_Addr value,
4776 unsigned char* view,
4777 typename elfcpp::Elf_types<size>::Elf_Addr address,
4778 section_size_type view_size)
4780 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4782 // leaq foo@tlsdesc(%rip), %rax
4783 // ==> movq foo@gottpoff(%rip), %rax
4784 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4785 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4786 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4787 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4789 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4790 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4794 // call *foo@tlscall(%rax)
4796 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4797 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4798 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4799 view[0] == 0xff && view[1] == 0x10);
4805 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4809 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4810 const Relocate_info<size, false>* relinfo,
4812 Output_segment* tls_segment,
4813 const elfcpp::Rela<size, false>& rela,
4814 unsigned int r_type,
4815 typename elfcpp::Elf_types<size>::Elf_Addr value,
4816 unsigned char* view,
4817 section_size_type view_size)
4819 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4821 // leaq foo@tlsdesc(%rip), %rax
4822 // ==> movq foo@tpoff, %rax
4823 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4824 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4825 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4826 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4829 value -= tls_segment->memsz();
4830 Relocate_functions<size, false>::rela32(view, value, 0);
4834 // call *foo@tlscall(%rax)
4836 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4837 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4838 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4839 view[0] == 0xff && view[1] == 0x10);
4847 Target_x86_64<size>::Relocate::tls_ld_to_le(
4848 const Relocate_info<size, false>* relinfo,
4851 const elfcpp::Rela<size, false>& rela,
4853 typename elfcpp::Elf_types<size>::Elf_Addr,
4854 unsigned char* view,
4855 section_size_type view_size)
4857 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4859 // ... leq foo@dtpoff(%rax),%reg
4860 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4862 // ... leq foo@dtpoff(%rax),%reg
4863 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4864 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
4866 // ... leq foo@dtpoff(%rax),%reg
4867 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4869 // ... leq foo@dtpoff(%rax),%reg
4870 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4872 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4873 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4875 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4876 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4878 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4879 view[4] == 0xe8 || view[4] == 0xff);
4881 if (view[4] == 0xe8)
4884 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4886 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4891 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
4894 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
4898 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4900 this->skip_call_tls_get_addr_ = true;
4903 // Do a relocation in which we convert a TLS Initial-Exec to a
4908 Target_x86_64<size>::Relocate::tls_ie_to_le(
4909 const Relocate_info<size, false>* relinfo,
4911 Output_segment* tls_segment,
4912 const elfcpp::Rela<size, false>& rela,
4914 typename elfcpp::Elf_types<size>::Elf_Addr value,
4915 unsigned char* view,
4916 section_size_type view_size)
4918 // We need to examine the opcodes to figure out which instruction we
4921 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4922 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4924 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4925 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4927 unsigned char op1 = view[-3];
4928 unsigned char op2 = view[-2];
4929 unsigned char op3 = view[-1];
4930 unsigned char reg = op3 >> 3;
4937 else if (size == 32 && op1 == 0x44)
4940 view[-1] = 0xc0 | reg;
4944 // Special handling for %rsp.
4947 else if (size == 32 && op1 == 0x44)
4950 view[-1] = 0xc0 | reg;
4957 else if (size == 32 && op1 == 0x44)
4960 view[-1] = 0x80 | reg | (reg << 3);
4963 if (tls_segment != NULL)
4964 value -= tls_segment->memsz();
4965 Relocate_functions<size, false>::rela32(view, value, 0);
4968 // Relocate section data.
4972 Target_x86_64<size>::relocate_section(
4973 const Relocate_info<size, false>* relinfo,
4974 unsigned int sh_type,
4975 const unsigned char* prelocs,
4977 Output_section* output_section,
4978 bool needs_special_offset_handling,
4979 unsigned char* view,
4980 typename elfcpp::Elf_types<size>::Elf_Addr address,
4981 section_size_type view_size,
4982 const Reloc_symbol_changes* reloc_symbol_changes)
4984 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4987 gold_assert(sh_type == elfcpp::SHT_RELA);
4989 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
4990 gold::Default_comdat_behavior, Classify_reloc>(
4996 needs_special_offset_handling,
5000 reloc_symbol_changes);
5003 // Apply an incremental relocation. Incremental relocations always refer
5004 // to global symbols.
5008 Target_x86_64<size>::apply_relocation(
5009 const Relocate_info<size, false>* relinfo,
5010 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5011 unsigned int r_type,
5012 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5014 unsigned char* view,
5015 typename elfcpp::Elf_types<size>::Elf_Addr address,
5016 section_size_type view_size)
5018 gold::apply_relocation<size, false, Target_x86_64<size>,
5019 typename Target_x86_64<size>::Relocate>(
5031 // Scan the relocs during a relocatable link.
5035 Target_x86_64<size>::scan_relocatable_relocs(
5036 Symbol_table* symtab,
5038 Sized_relobj_file<size, false>* object,
5039 unsigned int data_shndx,
5040 unsigned int sh_type,
5041 const unsigned char* prelocs,
5043 Output_section* output_section,
5044 bool needs_special_offset_handling,
5045 size_t local_symbol_count,
5046 const unsigned char* plocal_symbols,
5047 Relocatable_relocs* rr)
5049 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5051 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5052 Scan_relocatable_relocs;
5054 gold_assert(sh_type == elfcpp::SHT_RELA);
5056 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5064 needs_special_offset_handling,
5070 // Scan the relocs for --emit-relocs.
5074 Target_x86_64<size>::emit_relocs_scan(
5075 Symbol_table* symtab,
5077 Sized_relobj_file<size, false>* object,
5078 unsigned int data_shndx,
5079 unsigned int sh_type,
5080 const unsigned char* prelocs,
5082 Output_section* output_section,
5083 bool needs_special_offset_handling,
5084 size_t local_symbol_count,
5085 const unsigned char* plocal_syms,
5086 Relocatable_relocs* rr)
5088 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5090 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5091 Emit_relocs_strategy;
5093 gold_assert(sh_type == elfcpp::SHT_RELA);
5095 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5103 needs_special_offset_handling,
5109 // Relocate a section during a relocatable link.
5113 Target_x86_64<size>::relocate_relocs(
5114 const Relocate_info<size, false>* relinfo,
5115 unsigned int sh_type,
5116 const unsigned char* prelocs,
5118 Output_section* output_section,
5119 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5120 unsigned char* view,
5121 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5122 section_size_type view_size,
5123 unsigned char* reloc_view,
5124 section_size_type reloc_view_size)
5126 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5129 gold_assert(sh_type == elfcpp::SHT_RELA);
5131 gold::relocate_relocs<size, false, Classify_reloc>(
5136 offset_in_output_section,
5144 // Return the value to use for a dynamic which requires special
5145 // treatment. This is how we support equality comparisons of function
5146 // pointers across shared library boundaries, as described in the
5147 // processor specific ABI supplement.
5151 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5153 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5154 return this->plt_address_for_global(gsym);
5157 // Return a string used to fill a code section with nops to take up
5158 // the specified length.
5162 Target_x86_64<size>::do_code_fill(section_size_type length) const
5166 // Build a jmpq instruction to skip over the bytes.
5167 unsigned char jmp[5];
5169 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5170 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5171 + std::string(length - 5, static_cast<char>(0x90)));
5174 // Nop sequences of various lengths.
5175 const char nop1[1] = { '\x90' }; // nop
5176 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5177 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5178 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5180 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5182 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5183 '\x44', '\x00', '\x00' };
5184 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5185 '\x00', '\x00', '\x00',
5187 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5188 '\x00', '\x00', '\x00',
5190 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5191 '\x84', '\x00', '\x00',
5192 '\x00', '\x00', '\x00' };
5193 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5194 '\x1f', '\x84', '\x00',
5195 '\x00', '\x00', '\x00',
5197 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5198 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5199 '\x00', '\x00', '\x00',
5201 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5202 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5203 '\x84', '\x00', '\x00',
5204 '\x00', '\x00', '\x00' };
5205 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5206 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5207 '\x1f', '\x84', '\x00',
5208 '\x00', '\x00', '\x00',
5210 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5211 '\x66', '\x66', '\x2e', // data16
5212 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5213 '\x00', '\x00', '\x00',
5215 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5216 '\x66', '\x66', '\x66', // data16; data16
5217 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5218 '\x84', '\x00', '\x00',
5219 '\x00', '\x00', '\x00' };
5221 const char* nops[16] = {
5223 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5224 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5227 return std::string(nops[length], length);
5230 // Return the addend to use for a target specific relocation. The
5231 // only target specific relocation is R_X86_64_TLSDESC for a local
5232 // symbol. We want to set the addend is the offset of the local
5233 // symbol in the TLS segment.
5237 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5240 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5241 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5242 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5243 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5244 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5245 gold_assert(psymval->is_tls_symbol());
5246 // The value of a TLS symbol is the offset in the TLS segment.
5247 return psymval->value(ti.object, 0);
5250 // Return the value to use for the base of a DW_EH_PE_datarel offset
5251 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5252 // assembler can not write out the difference between two labels in
5253 // different sections, so instead of using a pc-relative value they
5254 // use an offset from the GOT.
5258 Target_x86_64<size>::do_ehframe_datarel_base() const
5260 gold_assert(this->global_offset_table_ != NULL);
5261 Symbol* sym = this->global_offset_table_;
5262 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5263 return ssym->value();
5266 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5267 // compiled with -fsplit-stack. The function calls non-split-stack
5268 // code. We have to change the function so that it always ensures
5269 // that it has enough stack space to run some random function.
5271 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5272 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5273 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5275 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5276 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5277 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5281 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5282 section_offset_type fnoffset,
5283 section_size_type fnsize,
5284 const unsigned char*,
5286 unsigned char* view,
5287 section_size_type view_size,
5289 std::string* to) const
5291 const char* const cmp_insn = reinterpret_cast<const char*>
5292 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5293 const char* const lea_r10_insn = reinterpret_cast<const char*>
5294 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5295 const char* const lea_r11_insn = reinterpret_cast<const char*>
5296 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5298 const size_t cmp_insn_len =
5299 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5300 const size_t lea_r10_insn_len =
5301 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5302 const size_t lea_r11_insn_len =
5303 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5304 const size_t nop_len = (size == 32 ? 7 : 8);
5306 // The function starts with a comparison of the stack pointer and a
5307 // field in the TCB. This is followed by a jump.
5310 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5311 && fnsize > nop_len + 1)
5313 // We will call __morestack if the carry flag is set after this
5314 // comparison. We turn the comparison into an stc instruction
5316 view[fnoffset] = '\xf9';
5317 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5319 // lea NN(%rsp),%r10
5320 // lea NN(%rsp),%r11
5321 else if ((this->match_view(view, view_size, fnoffset,
5322 lea_r10_insn, lea_r10_insn_len)
5323 || this->match_view(view, view_size, fnoffset,
5324 lea_r11_insn, lea_r11_insn_len))
5327 // This is loading an offset from the stack pointer for a
5328 // comparison. The offset is negative, so we decrease the
5329 // offset by the amount of space we need for the stack. This
5330 // means we will avoid calling __morestack if there happens to
5331 // be plenty of space on the stack already.
5332 unsigned char* pval = view + fnoffset + 4;
5333 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5334 val -= parameters->options().split_stack_adjust_size();
5335 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5339 if (!object->has_no_split_stack())
5340 object->error(_("failed to match split-stack sequence at "
5341 "section %u offset %0zx"),
5342 shndx, static_cast<size_t>(fnoffset));
5346 // We have to change the function so that it calls
5347 // __morestack_non_split instead of __morestack. The former will
5348 // allocate additional stack space.
5349 *from = "__morestack";
5350 *to = "__morestack_non_split";
5353 // The selector for x86_64 object files. Note this is never instantiated
5354 // directly. It's only used in Target_selector_x86_64_nacl, below.
5357 class Target_selector_x86_64 : public Target_selector_freebsd
5360 Target_selector_x86_64()
5361 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5363 ? "elf64-x86-64" : "elf32-x86-64"),
5365 ? "elf64-x86-64-freebsd"
5366 : "elf32-x86-64-freebsd"),
5367 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5371 do_instantiate_target()
5372 { return new Target_x86_64<size>(); }
5376 // NaCl variant. It uses different PLT contents.
5379 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5382 Output_data_plt_x86_64_nacl(Layout* layout,
5383 Output_data_got<64, false>* got,
5384 Output_data_got_plt_x86_64* got_plt,
5385 Output_data_space* got_irelative)
5386 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5387 got, got_plt, got_irelative)
5390 Output_data_plt_x86_64_nacl(Layout* layout,
5391 Output_data_got<64, false>* got,
5392 Output_data_got_plt_x86_64* got_plt,
5393 Output_data_space* got_irelative,
5394 unsigned int plt_count)
5395 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5396 got, got_plt, got_irelative,
5401 virtual unsigned int
5402 do_get_plt_entry_size() const
5403 { return plt_entry_size; }
5406 do_add_eh_frame(Layout* layout)
5408 layout->add_eh_frame_for_plt(this,
5409 this->plt_eh_frame_cie,
5410 this->plt_eh_frame_cie_size,
5412 plt_eh_frame_fde_size);
5416 do_fill_first_plt_entry(unsigned char* pov,
5417 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5418 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5420 virtual unsigned int
5421 do_fill_plt_entry(unsigned char* pov,
5422 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5423 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5424 unsigned int got_offset,
5425 unsigned int plt_offset,
5426 unsigned int plt_index);
5429 do_fill_tlsdesc_entry(unsigned char* pov,
5430 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5431 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5432 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5433 unsigned int tlsdesc_got_offset,
5434 unsigned int plt_offset);
5437 // The size of an entry in the PLT.
5438 static const int plt_entry_size = 64;
5440 // The first entry in the PLT.
5441 static const unsigned char first_plt_entry[plt_entry_size];
5443 // Other entries in the PLT for an executable.
5444 static const unsigned char plt_entry[plt_entry_size];
5446 // The reserved TLSDESC entry in the PLT for an executable.
5447 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5449 // The .eh_frame unwind information for the PLT.
5450 static const int plt_eh_frame_fde_size = 32;
5451 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5455 class Target_x86_64_nacl : public Target_x86_64<size>
5458 Target_x86_64_nacl()
5459 : Target_x86_64<size>(&x86_64_nacl_info)
5462 virtual Output_data_plt_x86_64<size>*
5463 do_make_data_plt(Layout* layout,
5464 Output_data_got<64, false>* got,
5465 Output_data_got_plt_x86_64* got_plt,
5466 Output_data_space* got_irelative)
5468 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5472 virtual Output_data_plt_x86_64<size>*
5473 do_make_data_plt(Layout* layout,
5474 Output_data_got<64, false>* got,
5475 Output_data_got_plt_x86_64* got_plt,
5476 Output_data_space* got_irelative,
5477 unsigned int plt_count)
5479 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5485 do_code_fill(section_size_type length) const;
5488 static const Target::Target_info x86_64_nacl_info;
5492 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5495 false, // is_big_endian
5496 elfcpp::EM_X86_64, // machine_code
5497 false, // has_make_symbol
5498 false, // has_resolve
5499 true, // has_code_fill
5500 true, // is_default_stack_executable
5501 true, // can_icf_inline_merge_sections
5503 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5504 0x20000, // default_text_segment_address
5505 0x10000, // abi_pagesize (overridable by -z max-page-size)
5506 0x10000, // common_pagesize (overridable by -z common-page-size)
5507 true, // isolate_execinstr
5508 0x10000000, // rosegment_gap
5509 elfcpp::SHN_UNDEF, // small_common_shndx
5510 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5511 0, // small_common_section_flags
5512 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5513 NULL, // attributes_section
5514 NULL, // attributes_vendor
5515 "_start", // entry_symbol_name
5516 32, // hash_entry_size
5520 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5523 false, // is_big_endian
5524 elfcpp::EM_X86_64, // machine_code
5525 false, // has_make_symbol
5526 false, // has_resolve
5527 true, // has_code_fill
5528 true, // is_default_stack_executable
5529 true, // can_icf_inline_merge_sections
5531 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5532 0x20000, // default_text_segment_address
5533 0x10000, // abi_pagesize (overridable by -z max-page-size)
5534 0x10000, // common_pagesize (overridable by -z common-page-size)
5535 true, // isolate_execinstr
5536 0x10000000, // rosegment_gap
5537 elfcpp::SHN_UNDEF, // small_common_shndx
5538 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5539 0, // small_common_section_flags
5540 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5541 NULL, // attributes_section
5542 NULL, // attributes_vendor
5543 "_start", // entry_symbol_name
5544 32, // hash_entry_size
5547 #define NACLMASK 0xe0 // 32-byte alignment mask.
5549 // The first entry in the PLT.
5553 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5555 0xff, 0x35, // pushq contents of memory address
5556 0, 0, 0, 0, // replaced with address of .got + 8
5557 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5558 0, 0, 0, 0, // replaced with address of .got + 16
5559 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5560 0x4d, 0x01, 0xfb, // add %r15, %r11
5561 0x41, 0xff, 0xe3, // jmpq *%r11
5563 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5564 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5566 // 32 bytes of nop to pad out to the standard size
5567 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5568 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5569 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5570 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5571 0x66, // excess data32 prefix
5577 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5579 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5580 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5582 memcpy(pov, first_plt_entry, plt_entry_size);
5583 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5585 - (plt_address + 2 + 4)));
5586 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5588 - (plt_address + 9 + 4)));
5591 // Subsequent entries in the PLT.
5595 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
5597 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5598 0, 0, 0, 0, // replaced with address of symbol in .got
5599 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5600 0x4d, 0x01, 0xfb, // add %r15, %r11
5601 0x41, 0xff, 0xe3, // jmpq *%r11
5603 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5604 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5605 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5607 // Lazy GOT entries point here (32-byte aligned).
5608 0x68, // pushq immediate
5609 0, 0, 0, 0, // replaced with index into relocation table
5610 0xe9, // jmp relative
5611 0, 0, 0, 0, // replaced with offset to start of .plt0
5613 // 22 bytes of nop to pad out to the standard size.
5614 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5615 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5616 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5621 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
5623 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5624 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5625 unsigned int got_offset,
5626 unsigned int plt_offset,
5627 unsigned int plt_index)
5629 memcpy(pov, plt_entry, plt_entry_size);
5630 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
5631 (got_address + got_offset
5632 - (plt_address + plt_offset
5635 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
5636 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
5637 - (plt_offset + 38 + 4));
5642 // The reserved TLSDESC entry in the PLT.
5646 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
5648 0xff, 0x35, // pushq x(%rip)
5649 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5650 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5651 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5652 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5653 0x4d, 0x01, 0xfb, // add %r15, %r11
5654 0x41, 0xff, 0xe3, // jmpq *%r11
5656 // 41 bytes of nop to pad out to the standard size.
5657 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5658 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5659 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5660 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5661 0x66, 0x66, // excess data32 prefixes
5662 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5667 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5669 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5670 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5671 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5672 unsigned int tlsdesc_got_offset,
5673 unsigned int plt_offset)
5675 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5676 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5678 - (plt_address + plt_offset
5680 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5682 + tlsdesc_got_offset
5683 - (plt_address + plt_offset
5687 // The .eh_frame unwind information for the PLT.
5691 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5693 0, 0, 0, 0, // Replaced with offset to .plt.
5694 0, 0, 0, 0, // Replaced with size of .plt.
5695 0, // Augmentation size.
5696 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5697 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5698 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5699 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5700 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5701 13, // Block length.
5702 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5703 elfcpp::DW_OP_breg16, 0, // Push %rip.
5704 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5705 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5706 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5707 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5708 elfcpp::DW_OP_lit3, // Push 3.
5709 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5710 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5711 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5715 // Return a string used to fill a code section with nops.
5716 // For NaCl, long NOPs are only valid if they do not cross
5717 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5720 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5722 return std::string(length, static_cast<char>(0x90));
5725 // The selector for x86_64-nacl object files.
5728 class Target_selector_x86_64_nacl
5729 : public Target_selector_nacl<Target_selector_x86_64<size>,
5730 Target_x86_64_nacl<size> >
5733 Target_selector_x86_64_nacl()
5734 : Target_selector_nacl<Target_selector_x86_64<size>,
5735 Target_x86_64_nacl<size> >("x86-64",
5737 ? "elf64-x86-64-nacl"
5738 : "elf32-x86-64-nacl",
5741 : "elf32_x86_64_nacl")
5745 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5746 Target_selector_x86_64_nacl<32> target_selector_x32;
5748 } // End anonymous namespace.