1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2017 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 elfcpp::Elf_types<64>::Elf_Addr got_addr,
482 elfcpp::Elf_types<64>::Elf_Addr plt_addr);
485 do_fill_plt_entry(unsigned char* pov,
486 elfcpp::Elf_types<64>::Elf_Addr got_address,
487 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 elfcpp::Elf_types<64>::Elf_Addr got_address,
495 elfcpp::Elf_types<64>::Elf_Addr plt_address,
496 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 elfcpp::Elf_types<64>::Elf_Addr got_address,
503 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 // We can't do predefined symbols because they may become undefined
1050 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1051 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1052 || gsym->is_undefined()
1053 || gsym->is_predefined()
1054 || gsym->is_from_dynobj()
1055 || gsym->is_preemptible())
1057 // If we are building a shared object and the symbol is protected, we may
1058 // need to go through the GOT.
1059 if (parameters->options().shared()
1060 && gsym->visibility() == elfcpp::STV_PROTECTED)
1062 // We cannot convert references to the _DYNAMIC symbol.
1063 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1065 // Check for a MOV opcode.
1066 return (*view)[r_offset - 2] == 0x8b;
1070 // callq *foo@GOTPCRELX(%rip) to
1072 // and jmpq *foo@GOTPCRELX(%rip) to
1075 template<class View_type>
1077 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1078 size_t r_offset, View_type* view)
1080 gold_assert(gsym != NULL);
1081 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1082 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1084 // We cannot convert references to IFUNC symbols, or to symbols that
1085 // are not local to the current module.
1086 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1087 || gsym->is_undefined ()
1088 || gsym->is_from_dynobj()
1089 || gsym->is_preemptible())
1091 // Check for a CALLQ or JMPQ opcode.
1092 return ((*view)[r_offset - 2] == 0xff
1093 && ((*view)[r_offset - 1] == 0x15
1094 || (*view)[r_offset - 1] == 0x25));
1097 // Adjust TLS relocation type based on the options and whether this
1098 // is a local symbol.
1099 static tls::Tls_optimization
1100 optimize_tls_reloc(bool is_final, int r_type);
1102 // Get the GOT section, creating it if necessary.
1103 Output_data_got<64, false>*
1104 got_section(Symbol_table*, Layout*);
1106 // Get the GOT PLT section.
1107 Output_data_got_plt_x86_64*
1108 got_plt_section() const
1110 gold_assert(this->got_plt_ != NULL);
1111 return this->got_plt_;
1114 // Get the GOT section for TLSDESC entries.
1115 Output_data_got<64, false>*
1116 got_tlsdesc_section() const
1118 gold_assert(this->got_tlsdesc_ != NULL);
1119 return this->got_tlsdesc_;
1122 // Create the PLT section.
1124 make_plt_section(Symbol_table* symtab, Layout* layout);
1126 // Create a PLT entry for a global symbol.
1128 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1130 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1132 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1133 Sized_relobj_file<size, false>* relobj,
1134 unsigned int local_sym_index);
1136 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1138 define_tls_base_symbol(Symbol_table*, Layout*);
1140 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1142 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1144 // Create a GOT entry for the TLS module index.
1146 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1147 Sized_relobj_file<size, false>* object);
1149 // Get the PLT section.
1150 Output_data_plt_x86_64<size>*
1153 gold_assert(this->plt_ != NULL);
1157 // Get the dynamic reloc section, creating it if necessary.
1159 rela_dyn_section(Layout*);
1161 // Get the section to use for TLSDESC relocations.
1163 rela_tlsdesc_section(Layout*) const;
1165 // Get the section to use for IRELATIVE relocations.
1167 rela_irelative_section(Layout*);
1169 // Add a potential copy relocation.
1171 copy_reloc(Symbol_table* symtab, Layout* layout,
1172 Sized_relobj_file<size, false>* object,
1173 unsigned int shndx, Output_section* output_section,
1174 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1176 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1177 this->copy_relocs_.copy_reloc(symtab, layout,
1178 symtab->get_sized_symbol<size>(sym),
1179 object, shndx, output_section,
1180 r_type, reloc.get_r_offset(),
1181 reloc.get_r_addend(),
1182 this->rela_dyn_section(layout));
1185 // Information about this specific target which we pass to the
1186 // general Target structure.
1187 static const Target::Target_info x86_64_info;
1189 // The types of GOT entries needed for this platform.
1190 // These values are exposed to the ABI in an incremental link.
1191 // Do not renumber existing values without changing the version
1192 // number of the .gnu_incremental_inputs section.
1195 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1196 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1197 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1198 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1201 // This type is used as the argument to the target specific
1202 // relocation routines. The only target specific reloc is
1203 // R_X86_64_TLSDESC against a local symbol.
1206 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1207 : object(a_object), r_sym(a_r_sym)
1210 // The object in which the local symbol is defined.
1211 Sized_relobj_file<size, false>* object;
1212 // The local symbol index in the object.
1217 Output_data_got<64, false>* got_;
1219 Output_data_plt_x86_64<size>* plt_;
1220 // The GOT PLT section.
1221 Output_data_got_plt_x86_64* got_plt_;
1222 // The GOT section for IRELATIVE relocations.
1223 Output_data_space* got_irelative_;
1224 // The GOT section for TLSDESC relocations.
1225 Output_data_got<64, false>* got_tlsdesc_;
1226 // The _GLOBAL_OFFSET_TABLE_ symbol.
1227 Symbol* global_offset_table_;
1228 // The dynamic reloc section.
1229 Reloc_section* rela_dyn_;
1230 // The section to use for IRELATIVE relocs.
1231 Reloc_section* rela_irelative_;
1232 // Relocs saved to avoid a COPY reloc.
1233 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1234 // Offset of the GOT entry for the TLS module index.
1235 unsigned int got_mod_index_offset_;
1236 // We handle R_X86_64_TLSDESC against a local symbol as a target
1237 // specific relocation. Here we store the object and local symbol
1238 // index for the relocation.
1239 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1240 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1241 bool tls_base_symbol_defined_;
1245 const Target::Target_info Target_x86_64<64>::x86_64_info =
1248 false, // is_big_endian
1249 elfcpp::EM_X86_64, // machine_code
1250 false, // has_make_symbol
1251 false, // has_resolve
1252 true, // has_code_fill
1253 true, // is_default_stack_executable
1254 true, // can_icf_inline_merge_sections
1256 "/lib/ld64.so.1", // program interpreter
1257 0x400000, // default_text_segment_address
1258 0x1000, // abi_pagesize (overridable by -z max-page-size)
1259 0x1000, // common_pagesize (overridable by -z common-page-size)
1260 false, // isolate_execinstr
1262 elfcpp::SHN_UNDEF, // small_common_shndx
1263 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1264 0, // small_common_section_flags
1265 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1266 NULL, // attributes_section
1267 NULL, // attributes_vendor
1268 "_start", // entry_symbol_name
1269 32, // hash_entry_size
1273 const Target::Target_info Target_x86_64<32>::x86_64_info =
1276 false, // is_big_endian
1277 elfcpp::EM_X86_64, // machine_code
1278 false, // has_make_symbol
1279 false, // has_resolve
1280 true, // has_code_fill
1281 true, // is_default_stack_executable
1282 true, // can_icf_inline_merge_sections
1284 "/libx32/ldx32.so.1", // program interpreter
1285 0x400000, // default_text_segment_address
1286 0x1000, // abi_pagesize (overridable by -z max-page-size)
1287 0x1000, // common_pagesize (overridable by -z common-page-size)
1288 false, // isolate_execinstr
1290 elfcpp::SHN_UNDEF, // small_common_shndx
1291 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1292 0, // small_common_section_flags
1293 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1294 NULL, // attributes_section
1295 NULL, // attributes_vendor
1296 "_start", // entry_symbol_name
1297 32, // hash_entry_size
1300 // This is called when a new output section is created. This is where
1301 // we handle the SHF_X86_64_LARGE.
1305 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1307 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1308 os->set_is_large_section();
1311 // Get the GOT section, creating it if necessary.
1314 Output_data_got<64, false>*
1315 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1317 if (this->got_ == NULL)
1319 gold_assert(symtab != NULL && layout != NULL);
1321 // When using -z now, we can treat .got.plt as a relro section.
1322 // Without -z now, it is modified after program startup by lazy
1324 bool is_got_plt_relro = parameters->options().now();
1325 Output_section_order got_order = (is_got_plt_relro
1327 : ORDER_RELRO_LAST);
1328 Output_section_order got_plt_order = (is_got_plt_relro
1330 : ORDER_NON_RELRO_FIRST);
1332 this->got_ = new Output_data_got<64, false>();
1334 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1336 | elfcpp::SHF_WRITE),
1337 this->got_, got_order, true);
1339 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1340 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1342 | elfcpp::SHF_WRITE),
1343 this->got_plt_, got_plt_order,
1346 // The first three entries are reserved.
1347 this->got_plt_->set_current_data_size(3 * 8);
1349 if (!is_got_plt_relro)
1351 // Those bytes can go into the relro segment.
1352 layout->increase_relro(3 * 8);
1355 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1356 this->global_offset_table_ =
1357 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1358 Symbol_table::PREDEFINED,
1360 0, 0, elfcpp::STT_OBJECT,
1362 elfcpp::STV_HIDDEN, 0,
1365 // If there are any IRELATIVE relocations, they get GOT entries
1366 // in .got.plt after the jump slot entries.
1367 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1368 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1370 | elfcpp::SHF_WRITE),
1371 this->got_irelative_,
1372 got_plt_order, is_got_plt_relro);
1374 // If there are any TLSDESC relocations, they get GOT entries in
1375 // .got.plt after the jump slot and IRELATIVE entries.
1376 this->got_tlsdesc_ = new Output_data_got<64, false>();
1377 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1379 | elfcpp::SHF_WRITE),
1381 got_plt_order, is_got_plt_relro);
1387 // Get the dynamic reloc section, creating it if necessary.
1390 typename Target_x86_64<size>::Reloc_section*
1391 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1393 if (this->rela_dyn_ == NULL)
1395 gold_assert(layout != NULL);
1396 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1397 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1398 elfcpp::SHF_ALLOC, this->rela_dyn_,
1399 ORDER_DYNAMIC_RELOCS, false);
1401 return this->rela_dyn_;
1404 // Get the section to use for IRELATIVE relocs, creating it if
1405 // necessary. These go in .rela.dyn, but only after all other dynamic
1406 // relocations. They need to follow the other dynamic relocations so
1407 // that they can refer to global variables initialized by those
1411 typename Target_x86_64<size>::Reloc_section*
1412 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1414 if (this->rela_irelative_ == NULL)
1416 // Make sure we have already created the dynamic reloc section.
1417 this->rela_dyn_section(layout);
1418 this->rela_irelative_ = new Reloc_section(false);
1419 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1420 elfcpp::SHF_ALLOC, this->rela_irelative_,
1421 ORDER_DYNAMIC_RELOCS, false);
1422 gold_assert(this->rela_dyn_->output_section()
1423 == this->rela_irelative_->output_section());
1425 return this->rela_irelative_;
1428 // Write the first three reserved words of the .got.plt section.
1429 // The remainder of the section is written while writing the PLT
1430 // in Output_data_plt_i386::do_write.
1433 Output_data_got_plt_x86_64::do_write(Output_file* of)
1435 // The first entry in the GOT is the address of the .dynamic section
1436 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1437 // We saved space for them when we created the section in
1438 // Target_x86_64::got_section.
1439 const off_t got_file_offset = this->offset();
1440 gold_assert(this->data_size() >= 24);
1441 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1442 Output_section* dynamic = this->layout_->dynamic_section();
1443 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1444 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1445 memset(got_view + 8, 0, 16);
1446 of->write_output_view(got_file_offset, 24, got_view);
1449 // Initialize the PLT section.
1453 Output_data_plt_x86_64<size>::init(Layout* layout)
1455 this->rel_ = new Reloc_section(false);
1456 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1457 elfcpp::SHF_ALLOC, this->rel_,
1458 ORDER_DYNAMIC_PLT_RELOCS, false);
1463 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1465 os->set_entsize(this->get_plt_entry_size());
1468 // Add an entry to the PLT.
1472 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1475 gold_assert(!gsym->has_plt_offset());
1477 unsigned int plt_index;
1479 section_offset_type got_offset;
1481 unsigned int* pcount;
1482 unsigned int offset;
1483 unsigned int reserved;
1484 Output_section_data_build* got;
1485 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1486 && gsym->can_use_relative_reloc(false))
1488 pcount = &this->irelative_count_;
1491 got = this->got_irelative_;
1495 pcount = &this->count_;
1498 got = this->got_plt_;
1501 if (!this->is_data_size_valid())
1503 // Note that when setting the PLT offset for a non-IRELATIVE
1504 // entry we skip the initial reserved PLT entry.
1505 plt_index = *pcount + offset;
1506 plt_offset = plt_index * this->get_plt_entry_size();
1510 got_offset = (plt_index - offset + reserved) * 8;
1511 gold_assert(got_offset == got->current_data_size());
1513 // Every PLT entry needs a GOT entry which points back to the PLT
1514 // entry (this will be changed by the dynamic linker, normally
1515 // lazily when the function is called).
1516 got->set_current_data_size(got_offset + 8);
1520 // FIXME: This is probably not correct for IRELATIVE relocs.
1522 // For incremental updates, find an available slot.
1523 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1524 this->get_plt_entry_size(), 0);
1525 if (plt_offset == -1)
1526 gold_fallback(_("out of patch space (PLT);"
1527 " relink with --incremental-full"));
1529 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1530 // can be calculated from the PLT index, adjusting for the three
1531 // reserved entries at the beginning of the GOT.
1532 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1533 got_offset = (plt_index - offset + reserved) * 8;
1536 gsym->set_plt_offset(plt_offset);
1538 // Every PLT entry needs a reloc.
1539 this->add_relocation(symtab, layout, gsym, got_offset);
1541 // Note that we don't need to save the symbol. The contents of the
1542 // PLT are independent of which symbols are used. The symbols only
1543 // appear in the relocations.
1546 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1551 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1552 Symbol_table* symtab,
1554 Sized_relobj_file<size, false>* relobj,
1555 unsigned int local_sym_index)
1557 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1558 ++this->irelative_count_;
1560 section_offset_type got_offset = this->got_irelative_->current_data_size();
1562 // Every PLT entry needs a GOT entry which points back to the PLT
1564 this->got_irelative_->set_current_data_size(got_offset + 8);
1566 // Every PLT entry needs a reloc.
1567 Reloc_section* rela = this->rela_irelative(symtab, layout);
1568 rela->add_symbolless_local_addend(relobj, local_sym_index,
1569 elfcpp::R_X86_64_IRELATIVE,
1570 this->got_irelative_, got_offset, 0);
1575 // Add the relocation for a PLT entry.
1579 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1582 unsigned int got_offset)
1584 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1585 && gsym->can_use_relative_reloc(false))
1587 Reloc_section* rela = this->rela_irelative(symtab, layout);
1588 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1589 this->got_irelative_, got_offset, 0);
1593 gsym->set_needs_dynsym_entry();
1594 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1599 // Return where the TLSDESC relocations should go, creating it if
1600 // necessary. These follow the JUMP_SLOT relocations.
1603 typename Output_data_plt_x86_64<size>::Reloc_section*
1604 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1606 if (this->tlsdesc_rel_ == NULL)
1608 this->tlsdesc_rel_ = new Reloc_section(false);
1609 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1610 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1611 ORDER_DYNAMIC_PLT_RELOCS, false);
1612 gold_assert(this->tlsdesc_rel_->output_section()
1613 == this->rel_->output_section());
1615 return this->tlsdesc_rel_;
1618 // Return where the IRELATIVE relocations should go in the PLT. These
1619 // follow the JUMP_SLOT and the TLSDESC relocations.
1622 typename Output_data_plt_x86_64<size>::Reloc_section*
1623 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1626 if (this->irelative_rel_ == NULL)
1628 // Make sure we have a place for the TLSDESC relocations, in
1629 // case we see any later on.
1630 this->rela_tlsdesc(layout);
1631 this->irelative_rel_ = new Reloc_section(false);
1632 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1633 elfcpp::SHF_ALLOC, this->irelative_rel_,
1634 ORDER_DYNAMIC_PLT_RELOCS, false);
1635 gold_assert(this->irelative_rel_->output_section()
1636 == this->rel_->output_section());
1638 if (parameters->doing_static_link())
1640 // A statically linked executable will only have a .rela.plt
1641 // section to hold R_X86_64_IRELATIVE relocs for
1642 // STT_GNU_IFUNC symbols. The library will use these
1643 // symbols to locate the IRELATIVE relocs at program startup
1645 symtab->define_in_output_data("__rela_iplt_start", NULL,
1646 Symbol_table::PREDEFINED,
1647 this->irelative_rel_, 0, 0,
1648 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1649 elfcpp::STV_HIDDEN, 0, false, true);
1650 symtab->define_in_output_data("__rela_iplt_end", NULL,
1651 Symbol_table::PREDEFINED,
1652 this->irelative_rel_, 0, 0,
1653 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1654 elfcpp::STV_HIDDEN, 0, true, true);
1657 return this->irelative_rel_;
1660 // Return the PLT address to use for a global symbol.
1664 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1666 uint64_t offset = 0;
1667 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1668 && gsym->can_use_relative_reloc(false))
1669 offset = (this->count_ + 1) * this->get_plt_entry_size();
1670 return this->address() + offset + gsym->plt_offset();
1673 // Return the PLT address to use for a local symbol. These are always
1674 // IRELATIVE relocs.
1678 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1681 return (this->address()
1682 + (this->count_ + 1) * this->get_plt_entry_size()
1683 + object->local_plt_offset(r_sym));
1686 // Set the final size.
1689 Output_data_plt_x86_64<size>::set_final_data_size()
1691 // Number of regular and IFUNC PLT entries, plus the first entry.
1692 unsigned int count = this->count_ + this->irelative_count_ + 1;
1693 // Count the TLSDESC entry, if present.
1694 if (this->has_tlsdesc_entry())
1696 this->set_data_size(count * this->get_plt_entry_size());
1699 // The first entry in the PLT for an executable.
1703 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1705 // From AMD64 ABI Draft 0.98, page 76
1706 0xff, 0x35, // pushq contents of memory address
1707 0, 0, 0, 0, // replaced with address of .got + 8
1708 0xff, 0x25, // jmp indirect
1709 0, 0, 0, 0, // replaced with address of .got + 16
1710 0x90, 0x90, 0x90, 0x90 // noop (x4)
1715 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1717 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1718 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1720 memcpy(pov, first_plt_entry, plt_entry_size);
1721 // We do a jmp relative to the PC at the end of this instruction.
1722 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1724 - (plt_address + 6)));
1725 elfcpp::Swap<32, false>::writeval(pov + 8,
1727 - (plt_address + 12)));
1730 // Subsequent entries in the PLT for an executable.
1734 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1736 // From AMD64 ABI Draft 0.98, page 76
1737 0xff, 0x25, // jmpq indirect
1738 0, 0, 0, 0, // replaced with address of symbol in .got
1739 0x68, // pushq immediate
1740 0, 0, 0, 0, // replaced with offset into relocation table
1741 0xe9, // jmpq relative
1742 0, 0, 0, 0 // replaced with offset to start of .plt
1747 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1749 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1750 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1751 unsigned int got_offset,
1752 unsigned int plt_offset,
1753 unsigned int plt_index)
1755 // Check PC-relative offset overflow in PLT entry.
1756 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1757 - (plt_address + plt_offset + 6));
1758 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1759 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1762 memcpy(pov, plt_entry, plt_entry_size);
1763 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1764 plt_got_pcrel_offset);
1766 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1767 elfcpp::Swap<32, false>::writeval(pov + 12,
1768 - (plt_offset + plt_entry_size));
1773 // The reserved TLSDESC entry in the PLT for an executable.
1777 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1779 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1780 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1781 0xff, 0x35, // pushq x(%rip)
1782 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1783 0xff, 0x25, // jmpq *y(%rip)
1784 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1791 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1793 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1794 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1795 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1796 unsigned int tlsdesc_got_offset,
1797 unsigned int plt_offset)
1799 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1800 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1802 - (plt_address + plt_offset
1804 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1806 + tlsdesc_got_offset
1807 - (plt_address + plt_offset
1811 // Return the APLT address to use for a global symbol (for -z bndplt).
1814 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
1816 uint64_t offset = this->aplt_offset_;
1817 // Convert the PLT offset into an APLT offset.
1818 unsigned int plt_offset = gsym->plt_offset();
1819 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1820 && gsym->can_use_relative_reloc(false))
1821 offset += this->regular_count() * aplt_entry_size;
1823 plt_offset -= plt_entry_size;
1824 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
1825 return this->address() + offset + plt_offset;
1828 // Return the PLT address to use for a local symbol. These are always
1829 // IRELATIVE relocs.
1832 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
1835 // Convert the PLT offset into an APLT offset.
1836 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size)
1837 / (plt_entry_size / aplt_entry_size));
1838 return (this->address()
1839 + this->aplt_offset_
1840 + this->regular_count() * aplt_entry_size
1844 // Set the final size.
1846 Output_data_plt_x86_64_bnd::set_final_data_size()
1848 // Number of regular and IFUNC PLT entries.
1849 unsigned int count = this->entry_count();
1850 // Count the first entry and the TLSDESC entry, if present.
1851 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
1852 unsigned int plt_size = (count + extra) * plt_entry_size;
1853 // Offset of the APLT.
1854 this->aplt_offset_ = plt_size;
1855 // Size of the APLT.
1856 plt_size += count * aplt_entry_size;
1857 this->set_data_size(plt_size);
1860 // The first entry in the BND PLT.
1863 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
1865 // From AMD64 ABI Draft 0.98, page 76
1866 0xff, 0x35, // pushq contents of memory address
1867 0, 0, 0, 0, // replaced with address of .got + 8
1868 0xf2, 0xff, 0x25, // bnd jmp indirect
1869 0, 0, 0, 0, // replaced with address of .got + 16
1870 0x0f, 0x1f, 0x00 // nop
1874 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
1876 elfcpp::Elf_types<64>::Elf_Addr got_address,
1877 elfcpp::Elf_types<64>::Elf_Addr plt_address)
1879 memcpy(pov, first_plt_entry, plt_entry_size);
1880 // We do a jmp relative to the PC at the end of this instruction.
1881 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1883 - (plt_address + 6)));
1884 elfcpp::Swap<32, false>::writeval(pov + 9,
1886 - (plt_address + 13)));
1889 // Subsequent entries in the BND PLT.
1892 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
1894 // From AMD64 ABI Draft 0.99.8, page 139
1895 0x68, // pushq immediate
1896 0, 0, 0, 0, // replaced with offset into relocation table
1897 0xf2, 0xe9, // bnd jmpq relative
1898 0, 0, 0, 0, // replaced with offset to start of .plt
1899 0x0f, 0x1f, 0x44, 0, 0 // nop
1902 // Entries in the BND Additional PLT.
1905 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
1907 // From AMD64 ABI Draft 0.99.8, page 139
1908 0xf2, 0xff, 0x25, // bnd jmpq indirect
1909 0, 0, 0, 0, // replaced with address of symbol in .got
1914 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
1916 elfcpp::Elf_types<64>::Elf_Addr,
1917 elfcpp::Elf_types<64>::Elf_Addr,
1919 unsigned int plt_offset,
1920 unsigned int plt_index)
1922 memcpy(pov, plt_entry, plt_entry_size);
1923 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
1924 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
1929 Output_data_plt_x86_64_bnd::fill_aplt_entry(
1931 elfcpp::Elf_types<64>::Elf_Addr got_address,
1932 elfcpp::Elf_types<64>::Elf_Addr plt_address,
1933 unsigned int got_offset,
1934 unsigned int plt_offset,
1935 unsigned int plt_index)
1937 // Check PC-relative offset overflow in PLT entry.
1938 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1939 - (plt_address + plt_offset + 7));
1940 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1941 gold_error(_("PC-relative offset overflow in APLT entry %d"),
1944 memcpy(pov, aplt_entry, aplt_entry_size);
1945 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
1948 // The reserved TLSDESC entry in the PLT for an executable.
1951 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
1953 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1954 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1955 0xff, 0x35, // pushq x(%rip)
1956 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1957 0xf2, 0xff, 0x25, // jmpq *y(%rip)
1958 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1959 0x0f, 0x1f, 0 // nop
1963 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
1965 elfcpp::Elf_types<64>::Elf_Addr got_address,
1966 elfcpp::Elf_types<64>::Elf_Addr plt_address,
1967 elfcpp::Elf_types<64>::Elf_Addr got_base,
1968 unsigned int tlsdesc_got_offset,
1969 unsigned int plt_offset)
1971 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1972 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1974 - (plt_address + plt_offset
1976 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
1978 + tlsdesc_got_offset
1979 - (plt_address + plt_offset
1983 // The .eh_frame unwind information for the PLT.
1987 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1990 'z', // Augmentation: augmentation size included.
1991 'R', // Augmentation: FDE encoding included.
1992 '\0', // End of augmentation string.
1993 1, // Code alignment factor.
1994 0x78, // Data alignment factor.
1995 16, // Return address column.
1996 1, // Augmentation size.
1997 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1998 | elfcpp::DW_EH_PE_sdata4),
1999 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2000 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2001 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2007 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2009 0, 0, 0, 0, // Replaced with offset to .plt.
2010 0, 0, 0, 0, // Replaced with size of .plt.
2011 0, // Augmentation size.
2012 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2013 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2014 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2015 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2016 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2017 11, // Block length.
2018 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2019 elfcpp::DW_OP_breg16, 0, // Push %rip.
2020 elfcpp::DW_OP_lit15, // Push 0xf.
2021 elfcpp::DW_OP_and, // & (%rip & 0xf).
2022 elfcpp::DW_OP_lit11, // Push 0xb.
2023 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2024 elfcpp::DW_OP_lit3, // Push 3.
2025 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2026 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2027 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2033 // The .eh_frame unwind information for the BND PLT.
2035 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2037 0, 0, 0, 0, // Replaced with offset to .plt.
2038 0, 0, 0, 0, // Replaced with size of .plt.
2039 0, // Augmentation size.
2040 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2041 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2042 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2043 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2044 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2045 11, // Block length.
2046 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2047 elfcpp::DW_OP_breg16, 0, // Push %rip.
2048 elfcpp::DW_OP_lit15, // Push 0xf.
2049 elfcpp::DW_OP_and, // & (%rip & 0xf).
2050 elfcpp::DW_OP_lit5, // Push 5.
2051 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
2052 elfcpp::DW_OP_lit3, // Push 3.
2053 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
2054 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2055 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2061 // Write out the PLT. This uses the hand-coded instructions above,
2062 // and adjusts them as needed. This is specified by the AMD64 ABI.
2066 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2068 const off_t offset = this->offset();
2069 const section_size_type oview_size =
2070 convert_to_section_size_type(this->data_size());
2071 unsigned char* const oview = of->get_output_view(offset, oview_size);
2073 const off_t got_file_offset = this->got_plt_->offset();
2074 gold_assert(parameters->incremental_update()
2075 || (got_file_offset + this->got_plt_->data_size()
2076 == this->got_irelative_->offset()));
2077 const section_size_type got_size =
2078 convert_to_section_size_type(this->got_plt_->data_size()
2079 + this->got_irelative_->data_size());
2080 unsigned char* const got_view = of->get_output_view(got_file_offset,
2083 unsigned char* pov = oview;
2085 // The base address of the .plt section.
2086 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2087 // The base address of the .got section.
2088 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2089 // The base address of the PLT portion of the .got section,
2090 // which is where the GOT pointer will point, and where the
2091 // three reserved GOT entries are located.
2092 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2093 = this->got_plt_->address();
2095 this->fill_first_plt_entry(pov, got_address, plt_address);
2096 pov += this->get_plt_entry_size();
2098 // The first three entries in the GOT are reserved, and are written
2099 // by Output_data_got_plt_x86_64::do_write.
2100 unsigned char* got_pov = got_view + 24;
2102 unsigned int plt_offset = this->get_plt_entry_size();
2103 unsigned int got_offset = 24;
2104 const unsigned int count = this->count_ + this->irelative_count_;
2105 for (unsigned int plt_index = 0;
2108 pov += this->get_plt_entry_size(),
2110 plt_offset += this->get_plt_entry_size(),
2113 // Set and adjust the PLT entry itself.
2114 unsigned int lazy_offset = this->fill_plt_entry(pov,
2115 got_address, plt_address,
2116 got_offset, plt_offset,
2119 // Set the entry in the GOT.
2120 elfcpp::Swap<64, false>::writeval(got_pov,
2121 plt_address + plt_offset + lazy_offset);
2124 if (this->has_tlsdesc_entry())
2126 // Set and adjust the reserved TLSDESC PLT entry.
2127 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2128 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2129 tlsdesc_got_offset, plt_offset);
2130 pov += this->get_plt_entry_size();
2133 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2134 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2136 of->write_output_view(offset, oview_size, oview);
2137 of->write_output_view(got_file_offset, got_size, got_view);
2140 // Write out the BND PLT.
2143 Output_data_plt_x86_64_bnd::do_write(Output_file* of)
2145 const off_t offset = this->offset();
2146 const section_size_type oview_size =
2147 convert_to_section_size_type(this->data_size());
2148 unsigned char* const oview = of->get_output_view(offset, oview_size);
2150 Output_data_got<64, false>* got = this->got();
2151 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2152 Output_data_space* got_irelative = this->got_irelative();
2154 const off_t got_file_offset = got_plt->offset();
2155 gold_assert(parameters->incremental_update()
2156 || (got_file_offset + got_plt->data_size()
2157 == got_irelative->offset()));
2158 const section_size_type got_size =
2159 convert_to_section_size_type(got_plt->data_size()
2160 + got_irelative->data_size());
2161 unsigned char* const got_view = of->get_output_view(got_file_offset,
2164 unsigned char* pov = oview;
2166 // The base address of the .plt section.
2167 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2168 // The base address of the .got section.
2169 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2170 // The base address of the PLT portion of the .got section,
2171 // which is where the GOT pointer will point, and where the
2172 // three reserved GOT entries are located.
2173 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2175 this->fill_first_plt_entry(pov, got_address, plt_address);
2176 pov += plt_entry_size;
2178 // The first three entries in the GOT are reserved, and are written
2179 // by Output_data_got_plt_x86_64::do_write.
2180 unsigned char* got_pov = got_view + 24;
2182 unsigned int plt_offset = plt_entry_size;
2183 unsigned int got_offset = 24;
2184 const unsigned int count = this->entry_count();
2185 for (unsigned int plt_index = 0;
2188 pov += plt_entry_size,
2190 plt_offset += plt_entry_size,
2193 // Set and adjust the PLT entry itself.
2194 unsigned int lazy_offset = this->fill_plt_entry(pov,
2195 got_address, plt_address,
2196 got_offset, plt_offset,
2199 // Set the entry in the GOT.
2200 elfcpp::Swap<64, false>::writeval(got_pov,
2201 plt_address + plt_offset + lazy_offset);
2204 if (this->has_tlsdesc_entry())
2206 // Set and adjust the reserved TLSDESC PLT entry.
2207 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2208 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2209 tlsdesc_got_offset, plt_offset);
2210 pov += this->get_plt_entry_size();
2213 // Write the additional PLT.
2215 for (unsigned int plt_index = 0;
2218 pov += aplt_entry_size,
2219 plt_offset += aplt_entry_size,
2222 // Set and adjust the PLT entry itself.
2223 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2224 plt_offset, plt_index);
2227 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2228 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2230 of->write_output_view(offset, oview_size, oview);
2231 of->write_output_view(got_file_offset, got_size, got_view);
2234 // Create the PLT section.
2238 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2240 if (this->plt_ == NULL)
2242 // Create the GOT sections first.
2243 this->got_section(symtab, layout);
2245 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2246 this->got_irelative_);
2248 // Add unwind information if requested.
2249 if (parameters->options().ld_generated_unwind_info())
2250 this->plt_->add_eh_frame(layout);
2252 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2254 | elfcpp::SHF_EXECINSTR),
2255 this->plt_, ORDER_PLT, false);
2257 // Make the sh_info field of .rela.plt point to .plt.
2258 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2259 rela_plt_os->set_info_section(this->plt_->output_section());
2264 Output_data_plt_x86_64<32>*
2265 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2266 Output_data_got<64, false>* got,
2267 Output_data_got_plt_x86_64* got_plt,
2268 Output_data_space* got_irelative)
2270 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2275 Output_data_plt_x86_64<64>*
2276 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2277 Output_data_got<64, false>* got,
2278 Output_data_got_plt_x86_64* got_plt,
2279 Output_data_space* got_irelative)
2281 if (parameters->options().bndplt())
2282 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2285 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2290 Output_data_plt_x86_64<32>*
2291 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2292 Output_data_got<64, false>* got,
2293 Output_data_got_plt_x86_64* got_plt,
2294 Output_data_space* got_irelative,
2295 unsigned int plt_count)
2297 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2303 Output_data_plt_x86_64<64>*
2304 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2305 Output_data_got<64, false>* got,
2306 Output_data_got_plt_x86_64* got_plt,
2307 Output_data_space* got_irelative,
2308 unsigned int plt_count)
2310 if (parameters->options().bndplt())
2311 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2312 got_irelative, plt_count);
2314 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2319 // Return the section for TLSDESC relocations.
2322 typename Target_x86_64<size>::Reloc_section*
2323 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2325 return this->plt_section()->rela_tlsdesc(layout);
2328 // Create a PLT entry for a global symbol.
2332 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2335 if (gsym->has_plt_offset())
2338 if (this->plt_ == NULL)
2339 this->make_plt_section(symtab, layout);
2341 this->plt_->add_entry(symtab, layout, gsym);
2344 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2348 Target_x86_64<size>::make_local_ifunc_plt_entry(
2349 Symbol_table* symtab, Layout* layout,
2350 Sized_relobj_file<size, false>* relobj,
2351 unsigned int local_sym_index)
2353 if (relobj->local_has_plt_offset(local_sym_index))
2355 if (this->plt_ == NULL)
2356 this->make_plt_section(symtab, layout);
2357 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2360 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2363 // Return the number of entries in the PLT.
2367 Target_x86_64<size>::plt_entry_count() const
2369 if (this->plt_ == NULL)
2371 return this->plt_->entry_count();
2374 // Return the offset of the first non-reserved PLT entry.
2378 Target_x86_64<size>::first_plt_entry_offset() const
2380 if (this->plt_ == NULL)
2382 return this->plt_->first_plt_entry_offset();
2385 // Return the size of each PLT entry.
2389 Target_x86_64<size>::plt_entry_size() const
2391 if (this->plt_ == NULL)
2393 return this->plt_->get_plt_entry_size();
2396 // Create the GOT and PLT sections for an incremental update.
2399 Output_data_got_base*
2400 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
2402 unsigned int got_count,
2403 unsigned int plt_count)
2405 gold_assert(this->got_ == NULL);
2407 this->got_ = new Output_data_got<64, false>(got_count * 8);
2408 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2410 | elfcpp::SHF_WRITE),
2411 this->got_, ORDER_RELRO_LAST,
2414 // Add the three reserved entries.
2415 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
2416 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2418 | elfcpp::SHF_WRITE),
2419 this->got_plt_, ORDER_NON_RELRO_FIRST,
2422 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2423 this->global_offset_table_ =
2424 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2425 Symbol_table::PREDEFINED,
2427 0, 0, elfcpp::STT_OBJECT,
2429 elfcpp::STV_HIDDEN, 0,
2432 // If there are any TLSDESC relocations, they get GOT entries in
2433 // .got.plt after the jump slot entries.
2434 // FIXME: Get the count for TLSDESC entries.
2435 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
2436 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2437 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2439 ORDER_NON_RELRO_FIRST, false);
2441 // If there are any IRELATIVE relocations, they get GOT entries in
2442 // .got.plt after the jump slot and TLSDESC entries.
2443 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2444 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2445 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2446 this->got_irelative_,
2447 ORDER_NON_RELRO_FIRST, false);
2449 // Create the PLT section.
2450 this->plt_ = this->make_data_plt(layout, this->got_,
2452 this->got_irelative_,
2455 // Add unwind information if requested.
2456 if (parameters->options().ld_generated_unwind_info())
2457 this->plt_->add_eh_frame(layout);
2459 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2460 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2461 this->plt_, ORDER_PLT, false);
2463 // Make the sh_info field of .rela.plt point to .plt.
2464 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2465 rela_plt_os->set_info_section(this->plt_->output_section());
2467 // Create the rela_dyn section.
2468 this->rela_dyn_section(layout);
2473 // Reserve a GOT entry for a local symbol, and regenerate any
2474 // necessary dynamic relocations.
2478 Target_x86_64<size>::reserve_local_got_entry(
2479 unsigned int got_index,
2480 Sized_relobj<size, false>* obj,
2482 unsigned int got_type)
2484 unsigned int got_offset = got_index * 8;
2485 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2487 this->got_->reserve_local(got_index, obj, r_sym, got_type);
2490 case GOT_TYPE_STANDARD:
2491 if (parameters->options().output_is_position_independent())
2492 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
2493 this->got_, got_offset, 0, false);
2495 case GOT_TYPE_TLS_OFFSET:
2496 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
2497 this->got_, got_offset, 0);
2499 case GOT_TYPE_TLS_PAIR:
2500 this->got_->reserve_slot(got_index + 1);
2501 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
2502 this->got_, got_offset, 0);
2504 case GOT_TYPE_TLS_DESC:
2505 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2506 // this->got_->reserve_slot(got_index + 1);
2507 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2508 // this->got_, got_offset, 0);
2515 // Reserve a GOT entry for a global symbol, and regenerate any
2516 // necessary dynamic relocations.
2520 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
2522 unsigned int got_type)
2524 unsigned int got_offset = got_index * 8;
2525 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2527 this->got_->reserve_global(got_index, gsym, got_type);
2530 case GOT_TYPE_STANDARD:
2531 if (!gsym->final_value_is_known())
2533 if (gsym->is_from_dynobj()
2534 || gsym->is_undefined()
2535 || gsym->is_preemptible()
2536 || gsym->type() == elfcpp::STT_GNU_IFUNC)
2537 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
2538 this->got_, got_offset, 0);
2540 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2541 this->got_, got_offset, 0, false);
2544 case GOT_TYPE_TLS_OFFSET:
2545 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
2546 this->got_, got_offset, 0, false);
2548 case GOT_TYPE_TLS_PAIR:
2549 this->got_->reserve_slot(got_index + 1);
2550 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
2551 this->got_, got_offset, 0, false);
2552 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
2553 this->got_, got_offset + 8, 0, false);
2555 case GOT_TYPE_TLS_DESC:
2556 this->got_->reserve_slot(got_index + 1);
2557 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2558 this->got_, got_offset, 0, false);
2565 // Register an existing PLT entry for a global symbol.
2569 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2571 unsigned int plt_index,
2574 gold_assert(this->plt_ != NULL);
2575 gold_assert(!gsym->has_plt_offset());
2577 this->plt_->reserve_slot(plt_index);
2579 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2581 unsigned int got_offset = (plt_index + 3) * 8;
2582 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2585 // Force a COPY relocation for a given symbol.
2589 Target_x86_64<size>::emit_copy_reloc(
2590 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2592 this->copy_relocs_.emit_copy_reloc(symtab,
2593 symtab->get_sized_symbol<size>(sym),
2596 this->rela_dyn_section(NULL));
2599 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2603 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2606 if (this->tls_base_symbol_defined_)
2609 Output_segment* tls_segment = layout->tls_segment();
2610 if (tls_segment != NULL)
2612 bool is_exec = parameters->options().output_is_executable();
2613 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2614 Symbol_table::PREDEFINED,
2618 elfcpp::STV_HIDDEN, 0,
2620 ? Symbol::SEGMENT_END
2621 : Symbol::SEGMENT_START),
2624 this->tls_base_symbol_defined_ = true;
2627 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2631 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2634 if (this->plt_ == NULL)
2635 this->make_plt_section(symtab, layout);
2637 if (!this->plt_->has_tlsdesc_entry())
2639 // Allocate the TLSDESC_GOT entry.
2640 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2641 unsigned int got_offset = got->add_constant(0);
2643 // Allocate the TLSDESC_PLT entry.
2644 this->plt_->reserve_tlsdesc_entry(got_offset);
2648 // Create a GOT entry for the TLS module index.
2652 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2653 Sized_relobj_file<size, false>* object)
2655 if (this->got_mod_index_offset_ == -1U)
2657 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2658 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2659 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2660 unsigned int got_offset = got->add_constant(0);
2661 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2663 got->add_constant(0);
2664 this->got_mod_index_offset_ = got_offset;
2666 return this->got_mod_index_offset_;
2669 // Optimize the TLS relocation type based on what we know about the
2670 // symbol. IS_FINAL is true if the final address of this symbol is
2671 // known at link time.
2674 tls::Tls_optimization
2675 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2677 // If we are generating a shared library, then we can't do anything
2679 if (parameters->options().shared())
2680 return tls::TLSOPT_NONE;
2684 case elfcpp::R_X86_64_TLSGD:
2685 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2686 case elfcpp::R_X86_64_TLSDESC_CALL:
2687 // These are General-Dynamic which permits fully general TLS
2688 // access. Since we know that we are generating an executable,
2689 // we can convert this to Initial-Exec. If we also know that
2690 // this is a local symbol, we can further switch to Local-Exec.
2692 return tls::TLSOPT_TO_LE;
2693 return tls::TLSOPT_TO_IE;
2695 case elfcpp::R_X86_64_TLSLD:
2696 // This is Local-Dynamic, which refers to a local symbol in the
2697 // dynamic TLS block. Since we know that we generating an
2698 // executable, we can switch to Local-Exec.
2699 return tls::TLSOPT_TO_LE;
2701 case elfcpp::R_X86_64_DTPOFF32:
2702 case elfcpp::R_X86_64_DTPOFF64:
2703 // Another Local-Dynamic reloc.
2704 return tls::TLSOPT_TO_LE;
2706 case elfcpp::R_X86_64_GOTTPOFF:
2707 // These are Initial-Exec relocs which get the thread offset
2708 // from the GOT. If we know that we are linking against the
2709 // local symbol, we can switch to Local-Exec, which links the
2710 // thread offset into the instruction.
2712 return tls::TLSOPT_TO_LE;
2713 return tls::TLSOPT_NONE;
2715 case elfcpp::R_X86_64_TPOFF32:
2716 // When we already have Local-Exec, there is nothing further we
2718 return tls::TLSOPT_NONE;
2725 // Get the Reference_flags for a particular relocation.
2729 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2733 case elfcpp::R_X86_64_NONE:
2734 case elfcpp::R_X86_64_GNU_VTINHERIT:
2735 case elfcpp::R_X86_64_GNU_VTENTRY:
2736 case elfcpp::R_X86_64_GOTPC32:
2737 case elfcpp::R_X86_64_GOTPC64:
2738 // No symbol reference.
2741 case elfcpp::R_X86_64_64:
2742 case elfcpp::R_X86_64_32:
2743 case elfcpp::R_X86_64_32S:
2744 case elfcpp::R_X86_64_16:
2745 case elfcpp::R_X86_64_8:
2746 return Symbol::ABSOLUTE_REF;
2748 case elfcpp::R_X86_64_PC64:
2749 case elfcpp::R_X86_64_PC32:
2750 case elfcpp::R_X86_64_PC32_BND:
2751 case elfcpp::R_X86_64_PC16:
2752 case elfcpp::R_X86_64_PC8:
2753 case elfcpp::R_X86_64_GOTOFF64:
2754 return Symbol::RELATIVE_REF;
2756 case elfcpp::R_X86_64_PLT32:
2757 case elfcpp::R_X86_64_PLT32_BND:
2758 case elfcpp::R_X86_64_PLTOFF64:
2759 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2761 case elfcpp::R_X86_64_GOT64:
2762 case elfcpp::R_X86_64_GOT32:
2763 case elfcpp::R_X86_64_GOTPCREL64:
2764 case elfcpp::R_X86_64_GOTPCREL:
2765 case elfcpp::R_X86_64_GOTPCRELX:
2766 case elfcpp::R_X86_64_REX_GOTPCRELX:
2767 case elfcpp::R_X86_64_GOTPLT64:
2769 return Symbol::ABSOLUTE_REF;
2771 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2772 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2773 case elfcpp::R_X86_64_TLSDESC_CALL:
2774 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2775 case elfcpp::R_X86_64_DTPOFF32:
2776 case elfcpp::R_X86_64_DTPOFF64:
2777 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2778 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2779 return Symbol::TLS_REF;
2781 case elfcpp::R_X86_64_COPY:
2782 case elfcpp::R_X86_64_GLOB_DAT:
2783 case elfcpp::R_X86_64_JUMP_SLOT:
2784 case elfcpp::R_X86_64_RELATIVE:
2785 case elfcpp::R_X86_64_IRELATIVE:
2786 case elfcpp::R_X86_64_TPOFF64:
2787 case elfcpp::R_X86_64_DTPMOD64:
2788 case elfcpp::R_X86_64_TLSDESC:
2789 case elfcpp::R_X86_64_SIZE32:
2790 case elfcpp::R_X86_64_SIZE64:
2792 // Not expected. We will give an error later.
2797 // Report an unsupported relocation against a local symbol.
2801 Target_x86_64<size>::Scan::unsupported_reloc_local(
2802 Sized_relobj_file<size, false>* object,
2803 unsigned int r_type)
2805 gold_error(_("%s: unsupported reloc %u against local symbol"),
2806 object->name().c_str(), r_type);
2809 // We are about to emit a dynamic relocation of type R_TYPE. If the
2810 // dynamic linker does not support it, issue an error. The GNU linker
2811 // only issues a non-PIC error for an allocated read-only section.
2812 // Here we know the section is allocated, but we don't know that it is
2813 // read-only. But we check for all the relocation types which the
2814 // glibc dynamic linker supports, so it seems appropriate to issue an
2815 // error even if the section is not read-only. If GSYM is not NULL,
2816 // it is the symbol the relocation is against; if it is NULL, the
2817 // relocation is against a local symbol.
2821 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2826 // These are the relocation types supported by glibc for x86_64
2827 // which should always work.
2828 case elfcpp::R_X86_64_RELATIVE:
2829 case elfcpp::R_X86_64_IRELATIVE:
2830 case elfcpp::R_X86_64_GLOB_DAT:
2831 case elfcpp::R_X86_64_JUMP_SLOT:
2832 case elfcpp::R_X86_64_DTPMOD64:
2833 case elfcpp::R_X86_64_DTPOFF64:
2834 case elfcpp::R_X86_64_TPOFF64:
2835 case elfcpp::R_X86_64_64:
2836 case elfcpp::R_X86_64_COPY:
2839 // glibc supports these reloc types, but they can overflow.
2840 case elfcpp::R_X86_64_PC32:
2841 case elfcpp::R_X86_64_PC32_BND:
2842 // A PC relative reference is OK against a local symbol or if
2843 // the symbol is defined locally.
2845 || (!gsym->is_from_dynobj()
2846 && !gsym->is_undefined()
2847 && !gsym->is_preemptible()))
2850 case elfcpp::R_X86_64_32:
2851 // R_X86_64_32 is OK for x32.
2852 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2854 if (this->issued_non_pic_error_)
2856 gold_assert(parameters->options().output_is_position_independent());
2858 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2859 "overflow at runtime; recompile with -fPIC"));
2865 case elfcpp::R_X86_64_32:
2866 r_name = "R_X86_64_32";
2868 case elfcpp::R_X86_64_PC32:
2869 r_name = "R_X86_64_PC32";
2871 case elfcpp::R_X86_64_PC32_BND:
2872 r_name = "R_X86_64_PC32_BND";
2878 object->error(_("requires dynamic %s reloc against '%s' "
2879 "which may overflow at runtime; recompile "
2881 r_name, gsym->name());
2883 this->issued_non_pic_error_ = true;
2887 // This prevents us from issuing more than one error per reloc
2888 // section. But we can still wind up issuing more than one
2889 // error per object file.
2890 if (this->issued_non_pic_error_)
2892 gold_assert(parameters->options().output_is_position_independent());
2893 object->error(_("requires unsupported dynamic reloc %u; "
2894 "recompile with -fPIC"),
2896 this->issued_non_pic_error_ = true;
2899 case elfcpp::R_X86_64_NONE:
2904 // Return whether we need to make a PLT entry for a relocation of the
2905 // given type against a STT_GNU_IFUNC symbol.
2909 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2910 Sized_relobj_file<size, false>* object,
2911 unsigned int r_type)
2913 int flags = Scan::get_reference_flags(r_type);
2914 if (flags & Symbol::TLS_REF)
2915 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2916 object->name().c_str(), r_type);
2920 // Scan a relocation for a local symbol.
2924 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2926 Target_x86_64<size>* target,
2927 Sized_relobj_file<size, false>* object,
2928 unsigned int data_shndx,
2929 Output_section* output_section,
2930 const elfcpp::Rela<size, false>& reloc,
2931 unsigned int r_type,
2932 const elfcpp::Sym<size, false>& lsym,
2938 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2939 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2940 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2942 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2943 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2948 case elfcpp::R_X86_64_NONE:
2949 case elfcpp::R_X86_64_GNU_VTINHERIT:
2950 case elfcpp::R_X86_64_GNU_VTENTRY:
2953 case elfcpp::R_X86_64_64:
2954 // If building a shared library (or a position-independent
2955 // executable), we need to create a dynamic relocation for this
2956 // location. The relocation applied at link time will apply the
2957 // link-time value, so we flag the location with an
2958 // R_X86_64_RELATIVE relocation so the dynamic loader can
2959 // relocate it easily.
2960 if (parameters->options().output_is_position_independent())
2962 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2963 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2964 rela_dyn->add_local_relative(object, r_sym,
2966 ? elfcpp::R_X86_64_RELATIVE64
2967 : elfcpp::R_X86_64_RELATIVE),
2968 output_section, data_shndx,
2969 reloc.get_r_offset(),
2970 reloc.get_r_addend(), is_ifunc);
2974 case elfcpp::R_X86_64_32:
2975 case elfcpp::R_X86_64_32S:
2976 case elfcpp::R_X86_64_16:
2977 case elfcpp::R_X86_64_8:
2978 // If building a shared library (or a position-independent
2979 // executable), we need to create a dynamic relocation for this
2980 // location. We can't use an R_X86_64_RELATIVE relocation
2981 // because that is always a 64-bit relocation.
2982 if (parameters->options().output_is_position_independent())
2984 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2985 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2987 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2988 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2989 rela_dyn->add_local_relative(object, r_sym,
2990 elfcpp::R_X86_64_RELATIVE,
2991 output_section, data_shndx,
2992 reloc.get_r_offset(),
2993 reloc.get_r_addend(), is_ifunc);
2997 this->check_non_pic(object, r_type, NULL);
2999 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3000 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3001 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3002 rela_dyn->add_local(object, r_sym, r_type, output_section,
3003 data_shndx, reloc.get_r_offset(),
3004 reloc.get_r_addend());
3007 gold_assert(lsym.get_st_value() == 0);
3008 unsigned int shndx = lsym.get_st_shndx();
3010 shndx = object->adjust_sym_shndx(r_sym, shndx,
3013 object->error(_("section symbol %u has bad shndx %u"),
3016 rela_dyn->add_local_section(object, shndx,
3017 r_type, output_section,
3018 data_shndx, reloc.get_r_offset(),
3019 reloc.get_r_addend());
3024 case elfcpp::R_X86_64_PC64:
3025 case elfcpp::R_X86_64_PC32:
3026 case elfcpp::R_X86_64_PC32_BND:
3027 case elfcpp::R_X86_64_PC16:
3028 case elfcpp::R_X86_64_PC8:
3031 case elfcpp::R_X86_64_PLT32:
3032 case elfcpp::R_X86_64_PLT32_BND:
3033 // Since we know this is a local symbol, we can handle this as a
3037 case elfcpp::R_X86_64_GOTPC32:
3038 case elfcpp::R_X86_64_GOTOFF64:
3039 case elfcpp::R_X86_64_GOTPC64:
3040 case elfcpp::R_X86_64_PLTOFF64:
3041 // We need a GOT section.
3042 target->got_section(symtab, layout);
3043 // For PLTOFF64, we'd normally want a PLT section, but since we
3044 // know this is a local symbol, no PLT is needed.
3047 case elfcpp::R_X86_64_GOT64:
3048 case elfcpp::R_X86_64_GOT32:
3049 case elfcpp::R_X86_64_GOTPCREL64:
3050 case elfcpp::R_X86_64_GOTPCREL:
3051 case elfcpp::R_X86_64_GOTPCRELX:
3052 case elfcpp::R_X86_64_REX_GOTPCRELX:
3053 case elfcpp::R_X86_64_GOTPLT64:
3055 // The symbol requires a GOT section.
3056 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3058 // If the relocation symbol isn't IFUNC,
3059 // and is local, then we will convert
3060 // mov foo@GOTPCREL(%rip), %reg
3061 // to lea foo(%rip), %reg.
3062 // in Relocate::relocate.
3063 if ((r_type == elfcpp::R_X86_64_GOTPCREL
3064 || r_type == elfcpp::R_X86_64_GOTPCRELX
3065 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3066 && reloc.get_r_offset() >= 2
3069 section_size_type stype;
3070 const unsigned char* view = object->section_contents(data_shndx,
3072 if (view[reloc.get_r_offset() - 2] == 0x8b)
3077 // The symbol requires a GOT entry.
3078 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3080 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3081 // lets function pointers compare correctly with shared
3082 // libraries. Otherwise we would need an IRELATIVE reloc.
3085 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3087 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3090 // If we are generating a shared object, we need to add a
3091 // dynamic relocation for this symbol's GOT entry.
3092 if (parameters->options().output_is_position_independent())
3094 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3095 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3096 if (r_type != elfcpp::R_X86_64_GOT32)
3098 unsigned int got_offset =
3099 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3100 rela_dyn->add_local_relative(object, r_sym,
3101 elfcpp::R_X86_64_RELATIVE,
3102 got, got_offset, 0, is_ifunc);
3106 this->check_non_pic(object, r_type, NULL);
3108 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3109 rela_dyn->add_local(
3110 object, r_sym, r_type, got,
3111 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3115 // For GOTPLT64, we'd normally want a PLT section, but since
3116 // we know this is a local symbol, no PLT is needed.
3120 case elfcpp::R_X86_64_COPY:
3121 case elfcpp::R_X86_64_GLOB_DAT:
3122 case elfcpp::R_X86_64_JUMP_SLOT:
3123 case elfcpp::R_X86_64_RELATIVE:
3124 case elfcpp::R_X86_64_IRELATIVE:
3125 // These are outstanding tls relocs, which are unexpected when linking
3126 case elfcpp::R_X86_64_TPOFF64:
3127 case elfcpp::R_X86_64_DTPMOD64:
3128 case elfcpp::R_X86_64_TLSDESC:
3129 gold_error(_("%s: unexpected reloc %u in object file"),
3130 object->name().c_str(), r_type);
3133 // These are initial tls relocs, which are expected when linking
3134 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3135 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3136 case elfcpp::R_X86_64_TLSDESC_CALL:
3137 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3138 case elfcpp::R_X86_64_DTPOFF32:
3139 case elfcpp::R_X86_64_DTPOFF64:
3140 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3141 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3143 bool output_is_shared = parameters->options().shared();
3144 const tls::Tls_optimization optimized_type
3145 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3149 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3150 if (optimized_type == tls::TLSOPT_NONE)
3152 // Create a pair of GOT entries for the module index and
3153 // dtv-relative offset.
3154 Output_data_got<64, false>* got
3155 = target->got_section(symtab, layout);
3156 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3157 unsigned int shndx = lsym.get_st_shndx();
3159 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3161 object->error(_("local symbol %u has bad shndx %u"),
3164 got->add_local_pair_with_rel(object, r_sym,
3167 target->rela_dyn_section(layout),
3168 elfcpp::R_X86_64_DTPMOD64);
3170 else if (optimized_type != tls::TLSOPT_TO_LE)
3171 unsupported_reloc_local(object, r_type);
3174 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3175 target->define_tls_base_symbol(symtab, layout);
3176 if (optimized_type == tls::TLSOPT_NONE)
3178 // Create reserved PLT and GOT entries for the resolver.
3179 target->reserve_tlsdesc_entries(symtab, layout);
3181 // Generate a double GOT entry with an
3182 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3183 // is resolved lazily, so the GOT entry needs to be in
3184 // an area in .got.plt, not .got. Call got_section to
3185 // make sure the section has been created.
3186 target->got_section(symtab, layout);
3187 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3188 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3189 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3191 unsigned int got_offset = got->add_constant(0);
3192 got->add_constant(0);
3193 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3195 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3196 // We store the arguments we need in a vector, and
3197 // use the index into the vector as the parameter
3198 // to pass to the target specific routines.
3199 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3200 void* arg = reinterpret_cast<void*>(intarg);
3201 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3202 got, got_offset, 0);
3205 else if (optimized_type != tls::TLSOPT_TO_LE)
3206 unsupported_reloc_local(object, r_type);
3209 case elfcpp::R_X86_64_TLSDESC_CALL:
3212 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3213 if (optimized_type == tls::TLSOPT_NONE)
3215 // Create a GOT entry for the module index.
3216 target->got_mod_index_entry(symtab, layout, object);
3218 else if (optimized_type != tls::TLSOPT_TO_LE)
3219 unsupported_reloc_local(object, r_type);
3222 case elfcpp::R_X86_64_DTPOFF32:
3223 case elfcpp::R_X86_64_DTPOFF64:
3226 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3227 layout->set_has_static_tls();
3228 if (optimized_type == tls::TLSOPT_NONE)
3230 // Create a GOT entry for the tp-relative offset.
3231 Output_data_got<64, false>* got
3232 = target->got_section(symtab, layout);
3233 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3234 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3235 target->rela_dyn_section(layout),
3236 elfcpp::R_X86_64_TPOFF64);
3238 else if (optimized_type != tls::TLSOPT_TO_LE)
3239 unsupported_reloc_local(object, r_type);
3242 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3243 layout->set_has_static_tls();
3244 if (output_is_shared)
3245 unsupported_reloc_local(object, r_type);
3254 case elfcpp::R_X86_64_SIZE32:
3255 case elfcpp::R_X86_64_SIZE64:
3257 gold_error(_("%s: unsupported reloc %u against local symbol"),
3258 object->name().c_str(), r_type);
3264 // Report an unsupported relocation against a global symbol.
3268 Target_x86_64<size>::Scan::unsupported_reloc_global(
3269 Sized_relobj_file<size, false>* object,
3270 unsigned int r_type,
3273 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3274 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3277 // Returns true if this relocation type could be that of a function pointer.
3280 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
3284 case elfcpp::R_X86_64_64:
3285 case elfcpp::R_X86_64_32:
3286 case elfcpp::R_X86_64_32S:
3287 case elfcpp::R_X86_64_16:
3288 case elfcpp::R_X86_64_8:
3289 case elfcpp::R_X86_64_GOT64:
3290 case elfcpp::R_X86_64_GOT32:
3291 case elfcpp::R_X86_64_GOTPCREL64:
3292 case elfcpp::R_X86_64_GOTPCREL:
3293 case elfcpp::R_X86_64_GOTPCRELX:
3294 case elfcpp::R_X86_64_REX_GOTPCRELX:
3295 case elfcpp::R_X86_64_GOTPLT64:
3303 // For safe ICF, scan a relocation for a local symbol to check if it
3304 // corresponds to a function pointer being taken. In that case mark
3305 // the function whose pointer was taken as not foldable.
3309 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3312 Target_x86_64<size>* ,
3313 Sized_relobj_file<size, false>* ,
3316 const elfcpp::Rela<size, false>& ,
3317 unsigned int r_type,
3318 const elfcpp::Sym<size, false>&)
3320 // When building a shared library, do not fold any local symbols as it is
3321 // not possible to distinguish pointer taken versus a call by looking at
3322 // the relocation types.
3323 return (parameters->options().shared()
3324 || possible_function_pointer_reloc(r_type));
3327 // For safe ICF, scan a relocation for a global symbol to check if it
3328 // corresponds to a function pointer being taken. In that case mark
3329 // the function whose pointer was taken as not foldable.
3333 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3336 Target_x86_64<size>* ,
3337 Sized_relobj_file<size, false>* ,
3340 const elfcpp::Rela<size, false>& ,
3341 unsigned int r_type,
3344 // When building a shared library, do not fold symbols whose visibility
3345 // is hidden, internal or protected.
3346 return ((parameters->options().shared()
3347 && (gsym->visibility() == elfcpp::STV_INTERNAL
3348 || gsym->visibility() == elfcpp::STV_PROTECTED
3349 || gsym->visibility() == elfcpp::STV_HIDDEN))
3350 || possible_function_pointer_reloc(r_type));
3353 // Scan a relocation for a global symbol.
3357 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3359 Target_x86_64<size>* target,
3360 Sized_relobj_file<size, false>* object,
3361 unsigned int data_shndx,
3362 Output_section* output_section,
3363 const elfcpp::Rela<size, false>& reloc,
3364 unsigned int r_type,
3367 // A STT_GNU_IFUNC symbol may require a PLT entry.
3368 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3369 && this->reloc_needs_plt_for_ifunc(object, r_type))
3370 target->make_plt_entry(symtab, layout, gsym);
3374 case elfcpp::R_X86_64_NONE:
3375 case elfcpp::R_X86_64_GNU_VTINHERIT:
3376 case elfcpp::R_X86_64_GNU_VTENTRY:
3379 case elfcpp::R_X86_64_64:
3380 case elfcpp::R_X86_64_32:
3381 case elfcpp::R_X86_64_32S:
3382 case elfcpp::R_X86_64_16:
3383 case elfcpp::R_X86_64_8:
3385 // Make a PLT entry if necessary.
3386 if (gsym->needs_plt_entry())
3388 target->make_plt_entry(symtab, layout, gsym);
3389 // Since this is not a PC-relative relocation, we may be
3390 // taking the address of a function. In that case we need to
3391 // set the entry in the dynamic symbol table to the address of
3393 if (gsym->is_from_dynobj() && !parameters->options().shared())
3394 gsym->set_needs_dynsym_value();
3396 // Make a dynamic relocation if necessary.
3397 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3399 if (!parameters->options().output_is_position_independent()
3400 && gsym->may_need_copy_reloc())
3402 target->copy_reloc(symtab, layout, object,
3403 data_shndx, output_section, gsym, reloc);
3405 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3406 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3407 && gsym->type() == elfcpp::STT_GNU_IFUNC
3408 && gsym->can_use_relative_reloc(false)
3409 && !gsym->is_from_dynobj()
3410 && !gsym->is_undefined()
3411 && !gsym->is_preemptible())
3413 // Use an IRELATIVE reloc for a locally defined
3414 // STT_GNU_IFUNC symbol. This makes a function
3415 // address in a PIE executable match the address in a
3416 // shared library that it links against.
3417 Reloc_section* rela_dyn =
3418 target->rela_irelative_section(layout);
3419 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3420 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3421 output_section, object,
3423 reloc.get_r_offset(),
3424 reloc.get_r_addend());
3426 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3427 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3428 && gsym->can_use_relative_reloc(false))
3430 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3431 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3432 output_section, object,
3434 reloc.get_r_offset(),
3435 reloc.get_r_addend(), false);
3439 this->check_non_pic(object, r_type, gsym);
3440 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3441 rela_dyn->add_global(gsym, r_type, output_section, object,
3442 data_shndx, reloc.get_r_offset(),
3443 reloc.get_r_addend());
3449 case elfcpp::R_X86_64_PC64:
3450 case elfcpp::R_X86_64_PC32:
3451 case elfcpp::R_X86_64_PC32_BND:
3452 case elfcpp::R_X86_64_PC16:
3453 case elfcpp::R_X86_64_PC8:
3455 // Make a PLT entry if necessary.
3456 if (gsym->needs_plt_entry())
3457 target->make_plt_entry(symtab, layout, gsym);
3458 // Make a dynamic relocation if necessary.
3459 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3461 if (parameters->options().output_is_executable()
3462 && gsym->may_need_copy_reloc())
3464 target->copy_reloc(symtab, layout, object,
3465 data_shndx, output_section, gsym, reloc);
3469 this->check_non_pic(object, r_type, gsym);
3470 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3471 rela_dyn->add_global(gsym, r_type, output_section, object,
3472 data_shndx, reloc.get_r_offset(),
3473 reloc.get_r_addend());
3479 case elfcpp::R_X86_64_GOT64:
3480 case elfcpp::R_X86_64_GOT32:
3481 case elfcpp::R_X86_64_GOTPCREL64:
3482 case elfcpp::R_X86_64_GOTPCREL:
3483 case elfcpp::R_X86_64_GOTPCRELX:
3484 case elfcpp::R_X86_64_REX_GOTPCRELX:
3485 case elfcpp::R_X86_64_GOTPLT64:
3487 // The symbol requires a GOT entry.
3488 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3490 // If we convert this from
3491 // mov foo@GOTPCREL(%rip), %reg
3492 // to lea foo(%rip), %reg.
3495 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3497 // in Relocate::relocate, then there is nothing to do here.
3499 Lazy_view<size> view(object, data_shndx);
3500 size_t r_offset = reloc.get_r_offset();
3502 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3507 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3512 if (gsym->final_value_is_known())
3514 // For a STT_GNU_IFUNC symbol we want the PLT address.
3515 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3516 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3518 got->add_global(gsym, GOT_TYPE_STANDARD);
3522 // If this symbol is not fully resolved, we need to add a
3523 // dynamic relocation for it.
3524 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3526 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3528 // 1) The symbol may be defined in some other module.
3530 // 2) We are building a shared library and this is a
3531 // protected symbol; using GLOB_DAT means that the dynamic
3532 // linker can use the address of the PLT in the main
3533 // executable when appropriate so that function address
3534 // comparisons work.
3536 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3537 // code, again so that function address comparisons work.
3538 if (gsym->is_from_dynobj()
3539 || gsym->is_undefined()
3540 || gsym->is_preemptible()
3541 || (gsym->visibility() == elfcpp::STV_PROTECTED
3542 && parameters->options().shared())
3543 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3544 && parameters->options().output_is_position_independent()))
3545 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3546 elfcpp::R_X86_64_GLOB_DAT);
3549 // For a STT_GNU_IFUNC symbol we want to write the PLT
3550 // offset into the GOT, so that function pointer
3551 // comparisons work correctly.
3553 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3554 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3557 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3558 // Tell the dynamic linker to use the PLT address
3559 // when resolving relocations.
3560 if (gsym->is_from_dynobj()
3561 && !parameters->options().shared())
3562 gsym->set_needs_dynsym_value();
3566 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3567 rela_dyn->add_global_relative(gsym,
3568 elfcpp::R_X86_64_RELATIVE,
3569 got, got_off, 0, false);
3576 case elfcpp::R_X86_64_PLT32:
3577 case elfcpp::R_X86_64_PLT32_BND:
3578 // If the symbol is fully resolved, this is just a PC32 reloc.
3579 // Otherwise we need a PLT entry.
3580 if (gsym->final_value_is_known())
3582 // If building a shared library, we can also skip the PLT entry
3583 // if the symbol is defined in the output file and is protected
3585 if (gsym->is_defined()
3586 && !gsym->is_from_dynobj()
3587 && !gsym->is_preemptible())
3589 target->make_plt_entry(symtab, layout, gsym);
3592 case elfcpp::R_X86_64_GOTPC32:
3593 case elfcpp::R_X86_64_GOTOFF64:
3594 case elfcpp::R_X86_64_GOTPC64:
3595 case elfcpp::R_X86_64_PLTOFF64:
3596 // We need a GOT section.
3597 target->got_section(symtab, layout);
3598 // For PLTOFF64, we also need a PLT entry (but only if the
3599 // symbol is not fully resolved).
3600 if (r_type == elfcpp::R_X86_64_PLTOFF64
3601 && !gsym->final_value_is_known())
3602 target->make_plt_entry(symtab, layout, gsym);
3605 case elfcpp::R_X86_64_COPY:
3606 case elfcpp::R_X86_64_GLOB_DAT:
3607 case elfcpp::R_X86_64_JUMP_SLOT:
3608 case elfcpp::R_X86_64_RELATIVE:
3609 case elfcpp::R_X86_64_IRELATIVE:
3610 // These are outstanding tls relocs, which are unexpected when linking
3611 case elfcpp::R_X86_64_TPOFF64:
3612 case elfcpp::R_X86_64_DTPMOD64:
3613 case elfcpp::R_X86_64_TLSDESC:
3614 gold_error(_("%s: unexpected reloc %u in object file"),
3615 object->name().c_str(), r_type);
3618 // These are initial tls relocs, which are expected for global()
3619 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3620 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3621 case elfcpp::R_X86_64_TLSDESC_CALL:
3622 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3623 case elfcpp::R_X86_64_DTPOFF32:
3624 case elfcpp::R_X86_64_DTPOFF64:
3625 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3626 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3628 // For the Initial-Exec model, we can treat undef symbols as final
3629 // when building an executable.
3630 const bool is_final = (gsym->final_value_is_known() ||
3631 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3632 gsym->is_undefined() &&
3633 parameters->options().output_is_executable()));
3634 const tls::Tls_optimization optimized_type
3635 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3638 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3639 if (optimized_type == tls::TLSOPT_NONE)
3641 // Create a pair of GOT entries for the module index and
3642 // dtv-relative offset.
3643 Output_data_got<64, false>* got
3644 = target->got_section(symtab, layout);
3645 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3646 target->rela_dyn_section(layout),
3647 elfcpp::R_X86_64_DTPMOD64,
3648 elfcpp::R_X86_64_DTPOFF64);
3650 else if (optimized_type == tls::TLSOPT_TO_IE)
3652 // Create a GOT entry for the tp-relative offset.
3653 Output_data_got<64, false>* got
3654 = target->got_section(symtab, layout);
3655 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3656 target->rela_dyn_section(layout),
3657 elfcpp::R_X86_64_TPOFF64);
3659 else if (optimized_type != tls::TLSOPT_TO_LE)
3660 unsupported_reloc_global(object, r_type, gsym);
3663 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3664 target->define_tls_base_symbol(symtab, layout);
3665 if (optimized_type == tls::TLSOPT_NONE)
3667 // Create reserved PLT and GOT entries for the resolver.
3668 target->reserve_tlsdesc_entries(symtab, layout);
3670 // Create a double GOT entry with an R_X86_64_TLSDESC
3671 // reloc. The R_X86_64_TLSDESC reloc is resolved
3672 // lazily, so the GOT entry needs to be in an area in
3673 // .got.plt, not .got. Call got_section to make sure
3674 // the section has been created.
3675 target->got_section(symtab, layout);
3676 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3677 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3678 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3679 elfcpp::R_X86_64_TLSDESC, 0);
3681 else if (optimized_type == tls::TLSOPT_TO_IE)
3683 // Create a GOT entry for the tp-relative offset.
3684 Output_data_got<64, false>* got
3685 = target->got_section(symtab, layout);
3686 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3687 target->rela_dyn_section(layout),
3688 elfcpp::R_X86_64_TPOFF64);
3690 else if (optimized_type != tls::TLSOPT_TO_LE)
3691 unsupported_reloc_global(object, r_type, gsym);
3694 case elfcpp::R_X86_64_TLSDESC_CALL:
3697 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3698 if (optimized_type == tls::TLSOPT_NONE)
3700 // Create a GOT entry for the module index.
3701 target->got_mod_index_entry(symtab, layout, object);
3703 else if (optimized_type != tls::TLSOPT_TO_LE)
3704 unsupported_reloc_global(object, r_type, gsym);
3707 case elfcpp::R_X86_64_DTPOFF32:
3708 case elfcpp::R_X86_64_DTPOFF64:
3711 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3712 layout->set_has_static_tls();
3713 if (optimized_type == tls::TLSOPT_NONE)
3715 // Create a GOT entry for the tp-relative offset.
3716 Output_data_got<64, false>* got
3717 = target->got_section(symtab, layout);
3718 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3719 target->rela_dyn_section(layout),
3720 elfcpp::R_X86_64_TPOFF64);
3722 else if (optimized_type != tls::TLSOPT_TO_LE)
3723 unsupported_reloc_global(object, r_type, gsym);
3726 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3727 layout->set_has_static_tls();
3728 if (parameters->options().shared())
3729 unsupported_reloc_global(object, r_type, gsym);
3738 case elfcpp::R_X86_64_SIZE32:
3739 case elfcpp::R_X86_64_SIZE64:
3741 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3742 object->name().c_str(), r_type,
3743 gsym->demangled_name().c_str());
3750 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3752 Sized_relobj_file<size, false>* object,
3753 unsigned int data_shndx,
3754 unsigned int sh_type,
3755 const unsigned char* prelocs,
3757 Output_section* output_section,
3758 bool needs_special_offset_handling,
3759 size_t local_symbol_count,
3760 const unsigned char* plocal_symbols)
3762 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3765 if (sh_type == elfcpp::SHT_REL)
3770 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3780 needs_special_offset_handling,
3785 // Scan relocations for a section.
3789 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3791 Sized_relobj_file<size, false>* object,
3792 unsigned int data_shndx,
3793 unsigned int sh_type,
3794 const unsigned char* prelocs,
3796 Output_section* output_section,
3797 bool needs_special_offset_handling,
3798 size_t local_symbol_count,
3799 const unsigned char* plocal_symbols)
3801 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3804 if (sh_type == elfcpp::SHT_REL)
3806 gold_error(_("%s: unsupported REL reloc section"),
3807 object->name().c_str());
3811 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3820 needs_special_offset_handling,
3825 // Finalize the sections.
3829 Target_x86_64<size>::do_finalize_sections(
3831 const Input_objects*,
3832 Symbol_table* symtab)
3834 const Reloc_section* rel_plt = (this->plt_ == NULL
3836 : this->plt_->rela_plt());
3837 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3838 this->rela_dyn_, true, false);
3840 // Fill in some more dynamic tags.
3841 Output_data_dynamic* const odyn = layout->dynamic_data();
3844 if (this->plt_ != NULL
3845 && this->plt_->output_section() != NULL
3846 && this->plt_->has_tlsdesc_entry())
3848 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3849 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3850 this->got_->finalize_data_size();
3851 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3852 this->plt_, plt_offset);
3853 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3854 this->got_, got_offset);
3858 // Emit any relocs we saved in an attempt to avoid generating COPY
3860 if (this->copy_relocs_.any_saved_relocs())
3861 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3863 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3864 // the .got.plt section.
3865 Symbol* sym = this->global_offset_table_;
3868 uint64_t data_size = this->got_plt_->current_data_size();
3869 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3872 if (parameters->doing_static_link()
3873 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3875 // If linking statically, make sure that the __rela_iplt symbols
3876 // were defined if necessary, even if we didn't create a PLT.
3877 static const Define_symbol_in_segment syms[] =
3880 "__rela_iplt_start", // name
3881 elfcpp::PT_LOAD, // segment_type
3882 elfcpp::PF_W, // segment_flags_set
3883 elfcpp::PF(0), // segment_flags_clear
3886 elfcpp::STT_NOTYPE, // type
3887 elfcpp::STB_GLOBAL, // binding
3888 elfcpp::STV_HIDDEN, // visibility
3890 Symbol::SEGMENT_START, // offset_from_base
3894 "__rela_iplt_end", // name
3895 elfcpp::PT_LOAD, // segment_type
3896 elfcpp::PF_W, // segment_flags_set
3897 elfcpp::PF(0), // segment_flags_clear
3900 elfcpp::STT_NOTYPE, // type
3901 elfcpp::STB_GLOBAL, // binding
3902 elfcpp::STV_HIDDEN, // visibility
3904 Symbol::SEGMENT_START, // offset_from_base
3909 symtab->define_symbols(layout, 2, syms,
3910 layout->script_options()->saw_sections_clause());
3914 // For x32, we need to handle PC-relative relocations using full 64-bit
3915 // arithmetic, so that we can detect relocation overflows properly.
3916 // This class overrides the pcrela32_check methods from the defaults in
3917 // Relocate_functions in reloc.h.
3920 class X86_64_relocate_functions : public Relocate_functions<size, false>
3923 typedef Relocate_functions<size, false> Base;
3925 // Do a simple PC relative relocation with the addend in the
3927 static inline typename Base::Reloc_status
3928 pcrela32_check(unsigned char* view,
3929 typename elfcpp::Elf_types<64>::Elf_Addr value,
3930 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3931 typename elfcpp::Elf_types<64>::Elf_Addr address)
3933 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3934 Valtype* wv = reinterpret_cast<Valtype*>(view);
3935 value = value + addend - address;
3936 elfcpp::Swap<32, false>::writeval(wv, value);
3937 return (Bits<32>::has_overflow(value)
3938 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3941 // Do a simple PC relative relocation with a Symbol_value with the
3942 // addend in the relocation.
3943 static inline typename Base::Reloc_status
3944 pcrela32_check(unsigned char* view,
3945 const Sized_relobj_file<size, false>* object,
3946 const Symbol_value<size>* psymval,
3947 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3948 typename elfcpp::Elf_types<64>::Elf_Addr address)
3950 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3951 Valtype* wv = reinterpret_cast<Valtype*>(view);
3952 typename elfcpp::Elf_types<64>::Elf_Addr value;
3954 value = psymval->value(object, addend);
3957 // For negative addends, get the symbol value without
3958 // the addend, then add the addend using 64-bit arithmetic.
3959 value = psymval->value(object, 0);
3963 elfcpp::Swap<32, false>::writeval(wv, value);
3964 return (Bits<32>::has_overflow(value)
3965 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3969 // Perform a relocation.
3973 Target_x86_64<size>::Relocate::relocate(
3974 const Relocate_info<size, false>* relinfo,
3976 Target_x86_64<size>* target,
3979 const unsigned char* preloc,
3980 const Sized_symbol<size>* gsym,
3981 const Symbol_value<size>* psymval,
3982 unsigned char* view,
3983 typename elfcpp::Elf_types<size>::Elf_Addr address,
3984 section_size_type view_size)
3986 typedef X86_64_relocate_functions<size> Reloc_funcs;
3987 const elfcpp::Rela<size, false> rela(preloc);
3988 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3990 if (this->skip_call_tls_get_addr_)
3992 if ((r_type != elfcpp::R_X86_64_PLT32
3993 && r_type != elfcpp::R_X86_64_GOTPCREL
3994 && r_type != elfcpp::R_X86_64_GOTPCRELX
3995 && r_type != elfcpp::R_X86_64_PLT32_BND
3996 && r_type != elfcpp::R_X86_64_PC32_BND
3997 && r_type != elfcpp::R_X86_64_PC32)
3999 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4001 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4002 _("missing expected TLS relocation"));
4003 this->skip_call_tls_get_addr_ = false;
4007 this->skip_call_tls_get_addr_ = false;
4015 const Sized_relobj_file<size, false>* object = relinfo->object;
4017 // Pick the value to use for symbols defined in the PLT.
4018 Symbol_value<size> symval;
4020 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4022 symval.set_output_value(target->plt_address_for_global(gsym));
4025 else if (gsym == NULL && psymval->is_ifunc_symbol())
4027 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4028 if (object->local_has_plt_offset(r_sym))
4030 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4035 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4037 // Get the GOT offset if needed.
4038 // The GOT pointer points to the end of the GOT section.
4039 // We need to subtract the size of the GOT section to get
4040 // the actual offset to use in the relocation.
4041 bool have_got_offset = false;
4042 // Since the actual offset is always negative, we use signed int to
4043 // support 64-bit GOT relocations.
4047 case elfcpp::R_X86_64_GOT32:
4048 case elfcpp::R_X86_64_GOT64:
4049 case elfcpp::R_X86_64_GOTPLT64:
4050 case elfcpp::R_X86_64_GOTPCREL64:
4053 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4054 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4058 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4059 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4060 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4061 - target->got_size());
4063 have_got_offset = true;
4070 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4074 case elfcpp::R_X86_64_NONE:
4075 case elfcpp::R_X86_64_GNU_VTINHERIT:
4076 case elfcpp::R_X86_64_GNU_VTENTRY:
4079 case elfcpp::R_X86_64_64:
4080 Reloc_funcs::rela64(view, object, psymval, addend);
4083 case elfcpp::R_X86_64_PC64:
4084 Reloc_funcs::pcrela64(view, object, psymval, addend,
4088 case elfcpp::R_X86_64_32:
4089 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4090 Reloc_funcs::CHECK_UNSIGNED);
4093 case elfcpp::R_X86_64_32S:
4094 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4095 Reloc_funcs::CHECK_SIGNED);
4098 case elfcpp::R_X86_64_PC32:
4099 case elfcpp::R_X86_64_PC32_BND:
4100 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4104 case elfcpp::R_X86_64_16:
4105 Reloc_funcs::rela16(view, object, psymval, addend);
4108 case elfcpp::R_X86_64_PC16:
4109 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4112 case elfcpp::R_X86_64_8:
4113 Reloc_funcs::rela8(view, object, psymval, addend);
4116 case elfcpp::R_X86_64_PC8:
4117 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4120 case elfcpp::R_X86_64_PLT32:
4121 case elfcpp::R_X86_64_PLT32_BND:
4122 gold_assert(gsym == NULL
4123 || gsym->has_plt_offset()
4124 || gsym->final_value_is_known()
4125 || (gsym->is_defined()
4126 && !gsym->is_from_dynobj()
4127 && !gsym->is_preemptible()));
4128 // Note: while this code looks the same as for R_X86_64_PC32, it
4129 // behaves differently because psymval was set to point to
4130 // the PLT entry, rather than the symbol, in Scan::global().
4131 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4135 case elfcpp::R_X86_64_PLTOFF64:
4138 gold_assert(gsym->has_plt_offset()
4139 || gsym->final_value_is_known());
4140 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4141 // This is the address of GLOBAL_OFFSET_TABLE.
4142 got_address = target->got_plt_section()->address();
4143 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4147 case elfcpp::R_X86_64_GOT32:
4148 gold_assert(have_got_offset);
4149 Reloc_funcs::rela32(view, got_offset, addend);
4152 case elfcpp::R_X86_64_GOTPC32:
4155 typename elfcpp::Elf_types<size>::Elf_Addr value;
4156 value = target->got_plt_section()->address();
4157 Reloc_funcs::pcrela32_check(view, value, addend, address);
4161 case elfcpp::R_X86_64_GOT64:
4162 case elfcpp::R_X86_64_GOTPLT64:
4163 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
4165 gold_assert(have_got_offset);
4166 Reloc_funcs::rela64(view, got_offset, addend);
4169 case elfcpp::R_X86_64_GOTPC64:
4172 typename elfcpp::Elf_types<size>::Elf_Addr value;
4173 value = target->got_plt_section()->address();
4174 Reloc_funcs::pcrela64(view, value, addend, address);
4178 case elfcpp::R_X86_64_GOTOFF64:
4180 typename elfcpp::Elf_types<size>::Elf_Addr value;
4181 value = (psymval->value(object, 0)
4182 - target->got_plt_section()->address());
4183 Reloc_funcs::rela64(view, value, addend);
4187 case elfcpp::R_X86_64_GOTPCREL:
4188 case elfcpp::R_X86_64_GOTPCRELX:
4189 case elfcpp::R_X86_64_REX_GOTPCRELX:
4192 // mov foo@GOTPCREL(%rip), %reg
4193 // to lea foo(%rip), %reg.
4196 && rela.get_r_offset() >= 2
4198 && !psymval->is_ifunc_symbol())
4200 && rela.get_r_offset() >= 2
4201 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4205 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4208 // callq *foo@GOTPCRELX(%rip) to
4210 // and jmpq *foo@GOTPCRELX(%rip) to
4213 else if (gsym != NULL
4214 && rela.get_r_offset() >= 2
4215 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4219 if (view[-1] == 0x15)
4221 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4222 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4225 // Convert GOTPCRELX to 32-bit pc relative reloc.
4226 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4230 // Convert jmpq *foo@GOTPCRELX(%rip) to
4233 // The opcode of direct jmpq is 0xe9.
4235 // The opcode of nop is 0x90.
4237 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4238 // relative and since the instruction following the jmpq is now
4239 // the nop, offset the address by 1 byte. The start of the
4240 // relocation also moves ahead by 1 byte.
4241 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4249 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4250 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4251 - target->got_size());
4255 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4256 gold_assert(object->local_has_got_offset(r_sym,
4257 GOT_TYPE_STANDARD));
4258 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4259 - target->got_size());
4261 typename elfcpp::Elf_types<size>::Elf_Addr value;
4262 value = target->got_plt_section()->address() + got_offset;
4263 Reloc_funcs::pcrela32_check(view, value, addend, address);
4268 case elfcpp::R_X86_64_GOTPCREL64:
4270 gold_assert(have_got_offset);
4271 typename elfcpp::Elf_types<size>::Elf_Addr value;
4272 value = target->got_plt_section()->address() + got_offset;
4273 Reloc_funcs::pcrela64(view, value, addend, address);
4277 case elfcpp::R_X86_64_COPY:
4278 case elfcpp::R_X86_64_GLOB_DAT:
4279 case elfcpp::R_X86_64_JUMP_SLOT:
4280 case elfcpp::R_X86_64_RELATIVE:
4281 case elfcpp::R_X86_64_IRELATIVE:
4282 // These are outstanding tls relocs, which are unexpected when linking
4283 case elfcpp::R_X86_64_TPOFF64:
4284 case elfcpp::R_X86_64_DTPMOD64:
4285 case elfcpp::R_X86_64_TLSDESC:
4286 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4287 _("unexpected reloc %u in object file"),
4291 // These are initial tls relocs, which are expected when linking
4292 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4293 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4294 case elfcpp::R_X86_64_TLSDESC_CALL:
4295 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4296 case elfcpp::R_X86_64_DTPOFF32:
4297 case elfcpp::R_X86_64_DTPOFF64:
4298 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4299 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4300 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4301 view, address, view_size);
4304 case elfcpp::R_X86_64_SIZE32:
4305 case elfcpp::R_X86_64_SIZE64:
4307 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4308 _("unsupported reloc %u"),
4313 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4317 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4318 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4319 _("relocation overflow: "
4320 "reference to local symbol %u in %s"),
4321 r_sym, object->name().c_str());
4323 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4325 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4326 _("relocation overflow: "
4327 "reference to '%s' defined in %s"),
4329 gsym->object()->name().c_str());
4333 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4334 _("relocation overflow: reference to '%s'"),
4342 // Perform a TLS relocation.
4346 Target_x86_64<size>::Relocate::relocate_tls(
4347 const Relocate_info<size, false>* relinfo,
4348 Target_x86_64<size>* target,
4350 const elfcpp::Rela<size, false>& rela,
4351 unsigned int r_type,
4352 const Sized_symbol<size>* gsym,
4353 const Symbol_value<size>* psymval,
4354 unsigned char* view,
4355 typename elfcpp::Elf_types<size>::Elf_Addr address,
4356 section_size_type view_size)
4358 Output_segment* tls_segment = relinfo->layout->tls_segment();
4360 const Sized_relobj_file<size, false>* object = relinfo->object;
4361 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4362 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4363 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4365 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4367 const bool is_final = (gsym == NULL
4368 ? !parameters->options().shared()
4369 : gsym->final_value_is_known());
4370 tls::Tls_optimization optimized_type
4371 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4374 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4375 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4377 // If this code sequence is used in a non-executable section,
4378 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4379 // on the assumption that it's being used by itself in a debug
4380 // section. Therefore, in the unlikely event that the code
4381 // sequence appears in a non-executable section, we simply
4382 // leave it unoptimized.
4383 optimized_type = tls::TLSOPT_NONE;
4385 if (optimized_type == tls::TLSOPT_TO_LE)
4387 if (tls_segment == NULL)
4389 gold_assert(parameters->errors()->error_count() > 0
4390 || issue_undefined_symbol_error(gsym));
4393 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4394 rela, r_type, value, view,
4400 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4401 ? GOT_TYPE_TLS_OFFSET
4402 : GOT_TYPE_TLS_PAIR);
4403 unsigned int got_offset;
4406 gold_assert(gsym->has_got_offset(got_type));
4407 got_offset = gsym->got_offset(got_type) - target->got_size();
4411 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4412 gold_assert(object->local_has_got_offset(r_sym, got_type));
4413 got_offset = (object->local_got_offset(r_sym, got_type)
4414 - target->got_size());
4416 if (optimized_type == tls::TLSOPT_TO_IE)
4418 value = target->got_plt_section()->address() + got_offset;
4419 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4420 value, view, address, view_size);
4423 else if (optimized_type == tls::TLSOPT_NONE)
4425 // Relocate the field with the offset of the pair of GOT
4427 value = target->got_plt_section()->address() + got_offset;
4428 Relocate_functions<size, false>::pcrela32(view, value, addend,
4433 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4434 _("unsupported reloc %u"), r_type);
4437 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4438 case elfcpp::R_X86_64_TLSDESC_CALL:
4439 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4441 // See above comment for R_X86_64_TLSGD.
4442 optimized_type = tls::TLSOPT_NONE;
4444 if (optimized_type == tls::TLSOPT_TO_LE)
4446 if (tls_segment == NULL)
4448 gold_assert(parameters->errors()->error_count() > 0
4449 || issue_undefined_symbol_error(gsym));
4452 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4453 rela, r_type, value, view,
4459 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4460 ? GOT_TYPE_TLS_OFFSET
4461 : GOT_TYPE_TLS_DESC);
4462 unsigned int got_offset = 0;
4463 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4464 && optimized_type == tls::TLSOPT_NONE)
4466 // We created GOT entries in the .got.tlsdesc portion of
4467 // the .got.plt section, but the offset stored in the
4468 // symbol is the offset within .got.tlsdesc.
4469 got_offset = (target->got_size()
4470 + target->got_plt_section()->data_size());
4474 gold_assert(gsym->has_got_offset(got_type));
4475 got_offset += gsym->got_offset(got_type) - target->got_size();
4479 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4480 gold_assert(object->local_has_got_offset(r_sym, got_type));
4481 got_offset += (object->local_got_offset(r_sym, got_type)
4482 - target->got_size());
4484 if (optimized_type == tls::TLSOPT_TO_IE)
4486 value = target->got_plt_section()->address() + got_offset;
4487 this->tls_desc_gd_to_ie(relinfo, relnum,
4488 rela, r_type, value, view, address,
4492 else if (optimized_type == tls::TLSOPT_NONE)
4494 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4496 // Relocate the field with the offset of the pair of GOT
4498 value = target->got_plt_section()->address() + got_offset;
4499 Relocate_functions<size, false>::pcrela32(view, value, addend,
4505 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4506 _("unsupported reloc %u"), r_type);
4509 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4510 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4512 // See above comment for R_X86_64_TLSGD.
4513 optimized_type = tls::TLSOPT_NONE;
4515 if (optimized_type == tls::TLSOPT_TO_LE)
4517 if (tls_segment == NULL)
4519 gold_assert(parameters->errors()->error_count() > 0
4520 || issue_undefined_symbol_error(gsym));
4523 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4524 value, view, view_size);
4527 else if (optimized_type == tls::TLSOPT_NONE)
4529 // Relocate the field with the offset of the GOT entry for
4530 // the module index.
4531 unsigned int got_offset;
4532 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4533 - target->got_size());
4534 value = target->got_plt_section()->address() + got_offset;
4535 Relocate_functions<size, false>::pcrela32(view, value, addend,
4539 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4540 _("unsupported reloc %u"), r_type);
4543 case elfcpp::R_X86_64_DTPOFF32:
4544 // This relocation type is used in debugging information.
4545 // In that case we need to not optimize the value. If the
4546 // section is not executable, then we assume we should not
4547 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4548 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4550 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4552 if (tls_segment == NULL)
4554 gold_assert(parameters->errors()->error_count() > 0
4555 || issue_undefined_symbol_error(gsym));
4558 value -= tls_segment->memsz();
4560 Relocate_functions<size, false>::rela32(view, value, addend);
4563 case elfcpp::R_X86_64_DTPOFF64:
4564 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4565 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4567 if (tls_segment == NULL)
4569 gold_assert(parameters->errors()->error_count() > 0
4570 || issue_undefined_symbol_error(gsym));
4573 value -= tls_segment->memsz();
4575 Relocate_functions<size, false>::rela64(view, value, addend);
4578 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4580 && gsym->is_undefined()
4581 && parameters->options().output_is_executable())
4583 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4585 r_type, value, view,
4589 else if (optimized_type == tls::TLSOPT_TO_LE)
4591 if (tls_segment == NULL)
4593 gold_assert(parameters->errors()->error_count() > 0
4594 || issue_undefined_symbol_error(gsym));
4597 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4599 r_type, value, view,
4603 else if (optimized_type == tls::TLSOPT_NONE)
4605 // Relocate the field with the offset of the GOT entry for
4606 // the tp-relative offset of the symbol.
4607 unsigned int got_offset;
4610 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4611 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4612 - target->got_size());
4616 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4617 gold_assert(object->local_has_got_offset(r_sym,
4618 GOT_TYPE_TLS_OFFSET));
4619 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4620 - target->got_size());
4622 value = target->got_plt_section()->address() + got_offset;
4623 Relocate_functions<size, false>::pcrela32(view, value, addend,
4627 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4628 _("unsupported reloc type %u"),
4632 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4633 if (tls_segment == NULL)
4635 gold_assert(parameters->errors()->error_count() > 0
4636 || issue_undefined_symbol_error(gsym));
4639 value -= tls_segment->memsz();
4640 Relocate_functions<size, false>::rela32(view, value, addend);
4645 // Do a relocation in which we convert a TLS General-Dynamic to an
4650 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4651 const Relocate_info<size, false>* relinfo,
4653 const elfcpp::Rela<size, false>& rela,
4655 typename elfcpp::Elf_types<size>::Elf_Addr value,
4656 unsigned char* view,
4657 typename elfcpp::Elf_types<size>::Elf_Addr address,
4658 section_size_type view_size)
4661 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4662 // .word 0x6666; rex64; call __tls_get_addr@PLT
4663 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4664 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4665 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4666 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4668 // leaq foo@tlsgd(%rip),%rdi;
4669 // .word 0x6666; rex64; call __tls_get_addr@PLT
4670 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4671 // leaq foo@tlsgd(%rip),%rdi;
4672 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4673 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4675 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4676 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4677 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4678 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4682 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4684 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4685 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4686 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4691 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4693 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4694 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4695 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4699 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4700 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4703 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4705 this->skip_call_tls_get_addr_ = true;
4708 // Do a relocation in which we convert a TLS General-Dynamic to a
4713 Target_x86_64<size>::Relocate::tls_gd_to_le(
4714 const Relocate_info<size, false>* relinfo,
4716 Output_segment* tls_segment,
4717 const elfcpp::Rela<size, false>& rela,
4719 typename elfcpp::Elf_types<size>::Elf_Addr value,
4720 unsigned char* view,
4721 section_size_type view_size)
4724 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4725 // .word 0x6666; rex64; call __tls_get_addr@PLT
4726 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4727 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4728 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4729 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4731 // leaq foo@tlsgd(%rip),%rdi;
4732 // .word 0x6666; rex64; call __tls_get_addr@PLT
4733 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4734 // leaq foo@tlsgd(%rip),%rdi;
4735 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4736 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4738 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4739 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4740 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4741 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4745 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4747 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4748 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4749 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4754 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4756 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4757 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4759 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4763 value -= tls_segment->memsz();
4764 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4766 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4768 this->skip_call_tls_get_addr_ = true;
4771 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4775 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4776 const Relocate_info<size, false>* relinfo,
4778 const elfcpp::Rela<size, false>& rela,
4779 unsigned int r_type,
4780 typename elfcpp::Elf_types<size>::Elf_Addr value,
4781 unsigned char* view,
4782 typename elfcpp::Elf_types<size>::Elf_Addr address,
4783 section_size_type view_size)
4785 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4787 // leaq foo@tlsdesc(%rip), %rax
4788 // ==> movq foo@gottpoff(%rip), %rax
4789 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4790 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4791 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4792 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4794 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4795 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4799 // call *foo@tlscall(%rax)
4801 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4802 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4803 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4804 view[0] == 0xff && view[1] == 0x10);
4810 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4814 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4815 const Relocate_info<size, false>* relinfo,
4817 Output_segment* tls_segment,
4818 const elfcpp::Rela<size, false>& rela,
4819 unsigned int r_type,
4820 typename elfcpp::Elf_types<size>::Elf_Addr value,
4821 unsigned char* view,
4822 section_size_type view_size)
4824 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4826 // leaq foo@tlsdesc(%rip), %rax
4827 // ==> movq foo@tpoff, %rax
4828 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4829 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4830 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4831 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4834 value -= tls_segment->memsz();
4835 Relocate_functions<size, false>::rela32(view, value, 0);
4839 // call *foo@tlscall(%rax)
4841 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4842 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4843 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4844 view[0] == 0xff && view[1] == 0x10);
4852 Target_x86_64<size>::Relocate::tls_ld_to_le(
4853 const Relocate_info<size, false>* relinfo,
4856 const elfcpp::Rela<size, false>& rela,
4858 typename elfcpp::Elf_types<size>::Elf_Addr,
4859 unsigned char* view,
4860 section_size_type view_size)
4862 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4864 // ... leq foo@dtpoff(%rax),%reg
4865 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4867 // ... leq foo@dtpoff(%rax),%reg
4868 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4869 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
4871 // ... leq foo@dtpoff(%rax),%reg
4872 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4874 // ... leq foo@dtpoff(%rax),%reg
4875 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4877 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4878 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4880 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4881 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4883 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4884 view[4] == 0xe8 || view[4] == 0xff);
4886 if (view[4] == 0xe8)
4889 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4891 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4896 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
4899 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
4903 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4905 this->skip_call_tls_get_addr_ = true;
4908 // Do a relocation in which we convert a TLS Initial-Exec to a
4913 Target_x86_64<size>::Relocate::tls_ie_to_le(
4914 const Relocate_info<size, false>* relinfo,
4916 Output_segment* tls_segment,
4917 const elfcpp::Rela<size, false>& rela,
4919 typename elfcpp::Elf_types<size>::Elf_Addr value,
4920 unsigned char* view,
4921 section_size_type view_size)
4923 // We need to examine the opcodes to figure out which instruction we
4926 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4927 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4929 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4930 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4932 unsigned char op1 = view[-3];
4933 unsigned char op2 = view[-2];
4934 unsigned char op3 = view[-1];
4935 unsigned char reg = op3 >> 3;
4942 else if (size == 32 && op1 == 0x44)
4945 view[-1] = 0xc0 | reg;
4949 // Special handling for %rsp.
4952 else if (size == 32 && op1 == 0x44)
4955 view[-1] = 0xc0 | reg;
4962 else if (size == 32 && op1 == 0x44)
4965 view[-1] = 0x80 | reg | (reg << 3);
4968 if (tls_segment != NULL)
4969 value -= tls_segment->memsz();
4970 Relocate_functions<size, false>::rela32(view, value, 0);
4973 // Relocate section data.
4977 Target_x86_64<size>::relocate_section(
4978 const Relocate_info<size, false>* relinfo,
4979 unsigned int sh_type,
4980 const unsigned char* prelocs,
4982 Output_section* output_section,
4983 bool needs_special_offset_handling,
4984 unsigned char* view,
4985 typename elfcpp::Elf_types<size>::Elf_Addr address,
4986 section_size_type view_size,
4987 const Reloc_symbol_changes* reloc_symbol_changes)
4989 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4992 gold_assert(sh_type == elfcpp::SHT_RELA);
4994 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
4995 gold::Default_comdat_behavior, Classify_reloc>(
5001 needs_special_offset_handling,
5005 reloc_symbol_changes);
5008 // Apply an incremental relocation. Incremental relocations always refer
5009 // to global symbols.
5013 Target_x86_64<size>::apply_relocation(
5014 const Relocate_info<size, false>* relinfo,
5015 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5016 unsigned int r_type,
5017 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5019 unsigned char* view,
5020 typename elfcpp::Elf_types<size>::Elf_Addr address,
5021 section_size_type view_size)
5023 gold::apply_relocation<size, false, Target_x86_64<size>,
5024 typename Target_x86_64<size>::Relocate>(
5036 // Scan the relocs during a relocatable link.
5040 Target_x86_64<size>::scan_relocatable_relocs(
5041 Symbol_table* symtab,
5043 Sized_relobj_file<size, false>* object,
5044 unsigned int data_shndx,
5045 unsigned int sh_type,
5046 const unsigned char* prelocs,
5048 Output_section* output_section,
5049 bool needs_special_offset_handling,
5050 size_t local_symbol_count,
5051 const unsigned char* plocal_symbols,
5052 Relocatable_relocs* rr)
5054 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5056 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5057 Scan_relocatable_relocs;
5059 gold_assert(sh_type == elfcpp::SHT_RELA);
5061 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5069 needs_special_offset_handling,
5075 // Scan the relocs for --emit-relocs.
5079 Target_x86_64<size>::emit_relocs_scan(
5080 Symbol_table* symtab,
5082 Sized_relobj_file<size, false>* object,
5083 unsigned int data_shndx,
5084 unsigned int sh_type,
5085 const unsigned char* prelocs,
5087 Output_section* output_section,
5088 bool needs_special_offset_handling,
5089 size_t local_symbol_count,
5090 const unsigned char* plocal_syms,
5091 Relocatable_relocs* rr)
5093 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5095 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5096 Emit_relocs_strategy;
5098 gold_assert(sh_type == elfcpp::SHT_RELA);
5100 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5108 needs_special_offset_handling,
5114 // Relocate a section during a relocatable link.
5118 Target_x86_64<size>::relocate_relocs(
5119 const Relocate_info<size, false>* relinfo,
5120 unsigned int sh_type,
5121 const unsigned char* prelocs,
5123 Output_section* output_section,
5124 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5125 unsigned char* view,
5126 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5127 section_size_type view_size,
5128 unsigned char* reloc_view,
5129 section_size_type reloc_view_size)
5131 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5134 gold_assert(sh_type == elfcpp::SHT_RELA);
5136 gold::relocate_relocs<size, false, Classify_reloc>(
5141 offset_in_output_section,
5149 // Return the value to use for a dynamic which requires special
5150 // treatment. This is how we support equality comparisons of function
5151 // pointers across shared library boundaries, as described in the
5152 // processor specific ABI supplement.
5156 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5158 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5159 return this->plt_address_for_global(gsym);
5162 // Return a string used to fill a code section with nops to take up
5163 // the specified length.
5167 Target_x86_64<size>::do_code_fill(section_size_type length) const
5171 // Build a jmpq instruction to skip over the bytes.
5172 unsigned char jmp[5];
5174 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5175 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5176 + std::string(length - 5, static_cast<char>(0x90)));
5179 // Nop sequences of various lengths.
5180 const char nop1[1] = { '\x90' }; // nop
5181 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5182 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5183 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5185 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5187 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5188 '\x44', '\x00', '\x00' };
5189 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5190 '\x00', '\x00', '\x00',
5192 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5193 '\x00', '\x00', '\x00',
5195 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5196 '\x84', '\x00', '\x00',
5197 '\x00', '\x00', '\x00' };
5198 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5199 '\x1f', '\x84', '\x00',
5200 '\x00', '\x00', '\x00',
5202 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5203 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5204 '\x00', '\x00', '\x00',
5206 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5207 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5208 '\x84', '\x00', '\x00',
5209 '\x00', '\x00', '\x00' };
5210 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5211 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5212 '\x1f', '\x84', '\x00',
5213 '\x00', '\x00', '\x00',
5215 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5216 '\x66', '\x66', '\x2e', // data16
5217 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5218 '\x00', '\x00', '\x00',
5220 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5221 '\x66', '\x66', '\x66', // data16; data16
5222 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5223 '\x84', '\x00', '\x00',
5224 '\x00', '\x00', '\x00' };
5226 const char* nops[16] = {
5228 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5229 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5232 return std::string(nops[length], length);
5235 // Return the addend to use for a target specific relocation. The
5236 // only target specific relocation is R_X86_64_TLSDESC for a local
5237 // symbol. We want to set the addend is the offset of the local
5238 // symbol in the TLS segment.
5242 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5245 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5246 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5247 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5248 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5249 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5250 gold_assert(psymval->is_tls_symbol());
5251 // The value of a TLS symbol is the offset in the TLS segment.
5252 return psymval->value(ti.object, 0);
5255 // Return the value to use for the base of a DW_EH_PE_datarel offset
5256 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5257 // assembler can not write out the difference between two labels in
5258 // different sections, so instead of using a pc-relative value they
5259 // use an offset from the GOT.
5263 Target_x86_64<size>::do_ehframe_datarel_base() const
5265 gold_assert(this->global_offset_table_ != NULL);
5266 Symbol* sym = this->global_offset_table_;
5267 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5268 return ssym->value();
5271 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5272 // compiled with -fsplit-stack. The function calls non-split-stack
5273 // code. We have to change the function so that it always ensures
5274 // that it has enough stack space to run some random function.
5276 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5277 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5278 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5280 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5281 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5282 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5286 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5287 section_offset_type fnoffset,
5288 section_size_type fnsize,
5289 const unsigned char*,
5291 unsigned char* view,
5292 section_size_type view_size,
5294 std::string* to) const
5296 const char* const cmp_insn = reinterpret_cast<const char*>
5297 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5298 const char* const lea_r10_insn = reinterpret_cast<const char*>
5299 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5300 const char* const lea_r11_insn = reinterpret_cast<const char*>
5301 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5303 const size_t cmp_insn_len =
5304 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5305 const size_t lea_r10_insn_len =
5306 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5307 const size_t lea_r11_insn_len =
5308 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5309 const size_t nop_len = (size == 32 ? 7 : 8);
5311 // The function starts with a comparison of the stack pointer and a
5312 // field in the TCB. This is followed by a jump.
5315 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5316 && fnsize > nop_len + 1)
5318 // We will call __morestack if the carry flag is set after this
5319 // comparison. We turn the comparison into an stc instruction
5321 view[fnoffset] = '\xf9';
5322 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5324 // lea NN(%rsp),%r10
5325 // lea NN(%rsp),%r11
5326 else if ((this->match_view(view, view_size, fnoffset,
5327 lea_r10_insn, lea_r10_insn_len)
5328 || this->match_view(view, view_size, fnoffset,
5329 lea_r11_insn, lea_r11_insn_len))
5332 // This is loading an offset from the stack pointer for a
5333 // comparison. The offset is negative, so we decrease the
5334 // offset by the amount of space we need for the stack. This
5335 // means we will avoid calling __morestack if there happens to
5336 // be plenty of space on the stack already.
5337 unsigned char* pval = view + fnoffset + 4;
5338 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5339 val -= parameters->options().split_stack_adjust_size();
5340 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5344 if (!object->has_no_split_stack())
5345 object->error(_("failed to match split-stack sequence at "
5346 "section %u offset %0zx"),
5347 shndx, static_cast<size_t>(fnoffset));
5351 // We have to change the function so that it calls
5352 // __morestack_non_split instead of __morestack. The former will
5353 // allocate additional stack space.
5354 *from = "__morestack";
5355 *to = "__morestack_non_split";
5358 // The selector for x86_64 object files. Note this is never instantiated
5359 // directly. It's only used in Target_selector_x86_64_nacl, below.
5362 class Target_selector_x86_64 : public Target_selector_freebsd
5365 Target_selector_x86_64()
5366 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5368 ? "elf64-x86-64" : "elf32-x86-64"),
5370 ? "elf64-x86-64-freebsd"
5371 : "elf32-x86-64-freebsd"),
5372 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5376 do_instantiate_target()
5377 { return new Target_x86_64<size>(); }
5381 // NaCl variant. It uses different PLT contents.
5384 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5387 Output_data_plt_x86_64_nacl(Layout* layout,
5388 Output_data_got<64, false>* got,
5389 Output_data_got_plt_x86_64* got_plt,
5390 Output_data_space* got_irelative)
5391 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5392 got, got_plt, got_irelative)
5395 Output_data_plt_x86_64_nacl(Layout* layout,
5396 Output_data_got<64, false>* got,
5397 Output_data_got_plt_x86_64* got_plt,
5398 Output_data_space* got_irelative,
5399 unsigned int plt_count)
5400 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5401 got, got_plt, got_irelative,
5406 virtual unsigned int
5407 do_get_plt_entry_size() const
5408 { return plt_entry_size; }
5411 do_add_eh_frame(Layout* layout)
5413 layout->add_eh_frame_for_plt(this,
5414 this->plt_eh_frame_cie,
5415 this->plt_eh_frame_cie_size,
5417 plt_eh_frame_fde_size);
5421 do_fill_first_plt_entry(unsigned char* pov,
5422 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5423 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5425 virtual unsigned int
5426 do_fill_plt_entry(unsigned char* pov,
5427 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5428 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5429 unsigned int got_offset,
5430 unsigned int plt_offset,
5431 unsigned int plt_index);
5434 do_fill_tlsdesc_entry(unsigned char* pov,
5435 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5436 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5437 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5438 unsigned int tlsdesc_got_offset,
5439 unsigned int plt_offset);
5442 // The size of an entry in the PLT.
5443 static const int plt_entry_size = 64;
5445 // The first entry in the PLT.
5446 static const unsigned char first_plt_entry[plt_entry_size];
5448 // Other entries in the PLT for an executable.
5449 static const unsigned char plt_entry[plt_entry_size];
5451 // The reserved TLSDESC entry in the PLT for an executable.
5452 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5454 // The .eh_frame unwind information for the PLT.
5455 static const int plt_eh_frame_fde_size = 32;
5456 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5460 class Target_x86_64_nacl : public Target_x86_64<size>
5463 Target_x86_64_nacl()
5464 : Target_x86_64<size>(&x86_64_nacl_info)
5467 virtual Output_data_plt_x86_64<size>*
5468 do_make_data_plt(Layout* layout,
5469 Output_data_got<64, false>* got,
5470 Output_data_got_plt_x86_64* got_plt,
5471 Output_data_space* got_irelative)
5473 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5477 virtual Output_data_plt_x86_64<size>*
5478 do_make_data_plt(Layout* layout,
5479 Output_data_got<64, false>* got,
5480 Output_data_got_plt_x86_64* got_plt,
5481 Output_data_space* got_irelative,
5482 unsigned int plt_count)
5484 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5490 do_code_fill(section_size_type length) const;
5493 static const Target::Target_info x86_64_nacl_info;
5497 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5500 false, // is_big_endian
5501 elfcpp::EM_X86_64, // machine_code
5502 false, // has_make_symbol
5503 false, // has_resolve
5504 true, // has_code_fill
5505 true, // is_default_stack_executable
5506 true, // can_icf_inline_merge_sections
5508 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5509 0x20000, // default_text_segment_address
5510 0x10000, // abi_pagesize (overridable by -z max-page-size)
5511 0x10000, // common_pagesize (overridable by -z common-page-size)
5512 true, // isolate_execinstr
5513 0x10000000, // rosegment_gap
5514 elfcpp::SHN_UNDEF, // small_common_shndx
5515 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5516 0, // small_common_section_flags
5517 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5518 NULL, // attributes_section
5519 NULL, // attributes_vendor
5520 "_start", // entry_symbol_name
5521 32, // hash_entry_size
5525 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5528 false, // is_big_endian
5529 elfcpp::EM_X86_64, // machine_code
5530 false, // has_make_symbol
5531 false, // has_resolve
5532 true, // has_code_fill
5533 true, // is_default_stack_executable
5534 true, // can_icf_inline_merge_sections
5536 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5537 0x20000, // default_text_segment_address
5538 0x10000, // abi_pagesize (overridable by -z max-page-size)
5539 0x10000, // common_pagesize (overridable by -z common-page-size)
5540 true, // isolate_execinstr
5541 0x10000000, // rosegment_gap
5542 elfcpp::SHN_UNDEF, // small_common_shndx
5543 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5544 0, // small_common_section_flags
5545 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5546 NULL, // attributes_section
5547 NULL, // attributes_vendor
5548 "_start", // entry_symbol_name
5549 32, // hash_entry_size
5552 #define NACLMASK 0xe0 // 32-byte alignment mask.
5554 // The first entry in the PLT.
5558 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5560 0xff, 0x35, // pushq contents of memory address
5561 0, 0, 0, 0, // replaced with address of .got + 8
5562 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5563 0, 0, 0, 0, // replaced with address of .got + 16
5564 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5565 0x4d, 0x01, 0xfb, // add %r15, %r11
5566 0x41, 0xff, 0xe3, // jmpq *%r11
5568 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5569 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5571 // 32 bytes of nop to pad out to the standard size
5572 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5573 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5574 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5575 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5576 0x66, // excess data32 prefix
5582 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5584 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5585 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5587 memcpy(pov, first_plt_entry, plt_entry_size);
5588 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5590 - (plt_address + 2 + 4)));
5591 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5593 - (plt_address + 9 + 4)));
5596 // Subsequent entries in the PLT.
5600 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
5602 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5603 0, 0, 0, 0, // replaced with address of symbol in .got
5604 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5605 0x4d, 0x01, 0xfb, // add %r15, %r11
5606 0x41, 0xff, 0xe3, // jmpq *%r11
5608 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5609 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5610 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5612 // Lazy GOT entries point here (32-byte aligned).
5613 0x68, // pushq immediate
5614 0, 0, 0, 0, // replaced with index into relocation table
5615 0xe9, // jmp relative
5616 0, 0, 0, 0, // replaced with offset to start of .plt0
5618 // 22 bytes of nop to pad out to the standard size.
5619 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5620 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5621 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5626 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
5628 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5629 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5630 unsigned int got_offset,
5631 unsigned int plt_offset,
5632 unsigned int plt_index)
5634 memcpy(pov, plt_entry, plt_entry_size);
5635 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
5636 (got_address + got_offset
5637 - (plt_address + plt_offset
5640 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
5641 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
5642 - (plt_offset + 38 + 4));
5647 // The reserved TLSDESC entry in the PLT.
5651 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
5653 0xff, 0x35, // pushq x(%rip)
5654 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5655 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5656 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5657 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5658 0x4d, 0x01, 0xfb, // add %r15, %r11
5659 0x41, 0xff, 0xe3, // jmpq *%r11
5661 // 41 bytes of nop to pad out to the standard size.
5662 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5663 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5664 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5665 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5666 0x66, 0x66, // excess data32 prefixes
5667 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5672 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5674 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5675 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5676 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5677 unsigned int tlsdesc_got_offset,
5678 unsigned int plt_offset)
5680 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5681 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5683 - (plt_address + plt_offset
5685 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5687 + tlsdesc_got_offset
5688 - (plt_address + plt_offset
5692 // The .eh_frame unwind information for the PLT.
5696 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5698 0, 0, 0, 0, // Replaced with offset to .plt.
5699 0, 0, 0, 0, // Replaced with size of .plt.
5700 0, // Augmentation size.
5701 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5702 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5703 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5704 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5705 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5706 13, // Block length.
5707 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5708 elfcpp::DW_OP_breg16, 0, // Push %rip.
5709 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5710 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5711 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5712 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5713 elfcpp::DW_OP_lit3, // Push 3.
5714 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5715 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5716 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5720 // Return a string used to fill a code section with nops.
5721 // For NaCl, long NOPs are only valid if they do not cross
5722 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5725 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5727 return std::string(length, static_cast<char>(0x90));
5730 // The selector for x86_64-nacl object files.
5733 class Target_selector_x86_64_nacl
5734 : public Target_selector_nacl<Target_selector_x86_64<size>,
5735 Target_x86_64_nacl<size> >
5738 Target_selector_x86_64_nacl()
5739 : Target_selector_nacl<Target_selector_x86_64<size>,
5740 Target_x86_64_nacl<size> >("x86-64",
5742 ? "elf64-x86-64-nacl"
5743 : "elf32-x86-64-nacl",
5746 : "elf32_x86_64_nacl")
5750 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5751 Target_selector_x86_64_nacl<32> target_selector_x32;
5753 } // End anonymous namespace.