1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2018 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];
545 // We use this PLT when Indirect Branch Tracking (IBT) is enabled.
548 class Output_data_plt_x86_64_ibt : public Output_data_plt_x86_64<size>
551 Output_data_plt_x86_64_ibt(Layout* layout,
552 Output_data_got<64, false>* got,
553 Output_data_got_plt_x86_64* got_plt,
554 Output_data_space* got_irelative)
555 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
556 got, got_plt, got_irelative),
560 Output_data_plt_x86_64_ibt(Layout* layout,
561 Output_data_got<64, false>* got,
562 Output_data_got_plt_x86_64* got_plt,
563 Output_data_space* got_irelative,
564 unsigned int plt_count)
565 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
566 got, got_plt, got_irelative,
573 do_get_plt_entry_size() const
574 { return plt_entry_size; }
576 // Return the PLT address to use for a global symbol.
578 do_address_for_global(const Symbol*);
580 // Return the PLT address to use for a local symbol.
582 do_address_for_local(const Relobj*, unsigned int symndx);
585 do_add_eh_frame(Layout* layout)
587 layout->add_eh_frame_for_plt(this,
588 this->plt_eh_frame_cie,
589 this->plt_eh_frame_cie_size,
591 plt_eh_frame_fde_size);
595 do_fill_first_plt_entry(unsigned char* pov,
596 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
597 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
600 do_fill_plt_entry(unsigned char* pov,
601 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
602 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
603 unsigned int got_offset,
604 unsigned int plt_offset,
605 unsigned int plt_index);
608 do_fill_tlsdesc_entry(unsigned char* pov,
609 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
610 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
611 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
612 unsigned int tlsdesc_got_offset,
613 unsigned int plt_offset);
616 fill_aplt_entry(unsigned char* pov,
617 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
618 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
619 unsigned int got_offset,
620 unsigned int plt_offset,
621 unsigned int plt_index);
624 // Set the final size.
626 set_final_data_size();
628 // Write out the BND PLT data.
630 do_write(Output_file*);
632 // Offset of the Additional PLT (if using -z bndplt).
633 unsigned int aplt_offset_;
635 // The size of an entry in the PLT.
636 static const int plt_entry_size = 16;
638 // The size of an entry in the additional PLT.
639 static const int aplt_entry_size = 16;
641 // The first entry in the PLT.
642 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
643 // procedure linkage table for both programs and shared objects."
644 static const unsigned char first_plt_entry[plt_entry_size];
646 // Other entries in the PLT for an executable.
647 static const unsigned char plt_entry[plt_entry_size];
649 // Entries in the additional PLT.
650 static const unsigned char aplt_entry[aplt_entry_size];
652 // The reserved TLSDESC entry in the PLT for an executable.
653 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
655 // The .eh_frame unwind information for the PLT.
656 static const int plt_eh_frame_fde_size = 32;
657 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
664 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
665 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
669 operator[](size_t offset)
671 if (this->view_ == NULL)
672 this->view_ = this->object_->section_contents(this->data_shndx_,
675 if (offset >= this->view_size_)
677 return this->view_[offset];
681 Sized_relobj_file<size, false>* object_;
682 unsigned int data_shndx_;
683 const unsigned char* view_;
684 section_size_type view_size_;
687 // The x86_64 target class.
689 // http://www.x86-64.org/documentation/abi.pdf
690 // TLS info comes from
691 // http://people.redhat.com/drepper/tls.pdf
692 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
695 class Target_x86_64 : public Sized_target<size, false>
698 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
699 // uses only Elf64_Rela relocation entries with explicit addends."
700 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
702 Target_x86_64(const Target::Target_info* info = &x86_64_info)
703 : Sized_target<size, false>(info),
704 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
705 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
706 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
707 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
708 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
709 feature_1_(0), object_feature_1_(0), seen_first_object_(false)
712 // Hook for a new output section.
714 do_new_output_section(Output_section*) const;
716 // Scan the relocations to look for symbol adjustments.
718 gc_process_relocs(Symbol_table* symtab,
720 Sized_relobj_file<size, false>* object,
721 unsigned int data_shndx,
722 unsigned int sh_type,
723 const unsigned char* prelocs,
725 Output_section* output_section,
726 bool needs_special_offset_handling,
727 size_t local_symbol_count,
728 const unsigned char* plocal_symbols);
730 // Scan the relocations to look for symbol adjustments.
732 scan_relocs(Symbol_table* symtab,
734 Sized_relobj_file<size, false>* object,
735 unsigned int data_shndx,
736 unsigned int sh_type,
737 const unsigned char* prelocs,
739 Output_section* output_section,
740 bool needs_special_offset_handling,
741 size_t local_symbol_count,
742 const unsigned char* plocal_symbols);
744 // Finalize the sections.
746 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
748 // Return the value to use for a dynamic which requires special
751 do_dynsym_value(const Symbol*) const;
753 // Relocate a section.
755 relocate_section(const Relocate_info<size, false>*,
756 unsigned int sh_type,
757 const unsigned char* prelocs,
759 Output_section* output_section,
760 bool needs_special_offset_handling,
762 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
763 section_size_type view_size,
764 const Reloc_symbol_changes*);
766 // Scan the relocs during a relocatable link.
768 scan_relocatable_relocs(Symbol_table* symtab,
770 Sized_relobj_file<size, false>* object,
771 unsigned int data_shndx,
772 unsigned int sh_type,
773 const unsigned char* prelocs,
775 Output_section* output_section,
776 bool needs_special_offset_handling,
777 size_t local_symbol_count,
778 const unsigned char* plocal_symbols,
779 Relocatable_relocs*);
781 // Scan the relocs for --emit-relocs.
783 emit_relocs_scan(Symbol_table* symtab,
785 Sized_relobj_file<size, false>* object,
786 unsigned int data_shndx,
787 unsigned int sh_type,
788 const unsigned char* prelocs,
790 Output_section* output_section,
791 bool needs_special_offset_handling,
792 size_t local_symbol_count,
793 const unsigned char* plocal_syms,
794 Relocatable_relocs* rr);
796 // Emit relocations for a section.
799 const Relocate_info<size, false>*,
800 unsigned int sh_type,
801 const unsigned char* prelocs,
803 Output_section* output_section,
804 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
806 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
807 section_size_type view_size,
808 unsigned char* reloc_view,
809 section_size_type reloc_view_size);
811 // Return a string used to fill a code section with nops.
813 do_code_fill(section_size_type length) const;
815 // Return whether SYM is defined by the ABI.
817 do_is_defined_by_abi(const Symbol* sym) const
818 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
820 // Return the symbol index to use for a target specific relocation.
821 // The only target specific relocation is R_X86_64_TLSDESC for a
822 // local symbol, which is an absolute reloc.
824 do_reloc_symbol_index(void*, unsigned int r_type) const
826 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
830 // Return the addend to use for a target specific relocation.
832 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
834 // Return the PLT section.
836 do_plt_address_for_global(const Symbol* gsym) const
837 { return this->plt_section()->address_for_global(gsym); }
840 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
841 { return this->plt_section()->address_for_local(relobj, symndx); }
843 // This function should be defined in targets that can use relocation
844 // types to determine (implemented in local_reloc_may_be_function_pointer
845 // and global_reloc_may_be_function_pointer)
846 // if a function's pointer is taken. ICF uses this in safe mode to only
847 // fold those functions whose pointer is defintely not taken. For x86_64
848 // pie binaries, safe ICF cannot be done by looking at only relocation
849 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
850 // opcode is checked as well to distinguish a function call from taking
851 // a function's pointer.
853 do_can_check_for_function_pointers() const
856 // Return the base for a DW_EH_PE_datarel encoding.
858 do_ehframe_datarel_base() const;
860 // Adjust -fsplit-stack code which calls non-split-stack code.
862 do_calls_non_split(Relobj* object, unsigned int shndx,
863 section_offset_type fnoffset, section_size_type fnsize,
864 const unsigned char* prelocs, size_t reloc_count,
865 unsigned char* view, section_size_type view_size,
866 std::string* from, std::string* to) const;
868 // Return the size of the GOT section.
872 gold_assert(this->got_ != NULL);
873 return this->got_->data_size();
876 // Return the number of entries in the GOT.
878 got_entry_count() const
880 if (this->got_ == NULL)
882 return this->got_size() / 8;
885 // Return the number of entries in the PLT.
887 plt_entry_count() const;
889 // Return the offset of the first non-reserved PLT entry.
891 first_plt_entry_offset() const;
893 // Return the size of each PLT entry.
895 plt_entry_size() const;
897 // Return the size of each GOT entry.
899 got_entry_size() const
902 // Create the GOT section for an incremental update.
903 Output_data_got_base*
904 init_got_plt_for_update(Symbol_table* symtab,
906 unsigned int got_count,
907 unsigned int plt_count);
909 // Reserve a GOT entry for a local symbol, and regenerate any
910 // necessary dynamic relocations.
912 reserve_local_got_entry(unsigned int got_index,
913 Sized_relobj<size, false>* obj,
915 unsigned int got_type);
917 // Reserve a GOT entry for a global symbol, and regenerate any
918 // necessary dynamic relocations.
920 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
921 unsigned int got_type);
923 // Register an existing PLT entry for a global symbol.
925 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
928 // Force a COPY relocation for a given symbol.
930 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
932 // Apply an incremental relocation.
934 apply_relocation(const Relocate_info<size, false>* relinfo,
935 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
937 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
940 typename elfcpp::Elf_types<size>::Elf_Addr address,
941 section_size_type view_size);
943 // Add a new reloc argument, returning the index in the vector.
945 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
947 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
948 return this->tlsdesc_reloc_info_.size() - 1;
951 Output_data_plt_x86_64<size>*
952 make_data_plt(Layout* layout,
953 Output_data_got<64, false>* got,
954 Output_data_got_plt_x86_64* got_plt,
955 Output_data_space* got_irelative)
957 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
960 Output_data_plt_x86_64<size>*
961 make_data_plt(Layout* layout,
962 Output_data_got<64, false>* got,
963 Output_data_got_plt_x86_64* got_plt,
964 Output_data_space* got_irelative,
965 unsigned int plt_count)
967 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
971 virtual Output_data_plt_x86_64<size>*
972 do_make_data_plt(Layout* layout,
973 Output_data_got<64, false>* got,
974 Output_data_got_plt_x86_64* got_plt,
975 Output_data_space* got_irelative);
977 virtual Output_data_plt_x86_64<size>*
978 do_make_data_plt(Layout* layout,
979 Output_data_got<64, false>* got,
980 Output_data_got_plt_x86_64* got_plt,
981 Output_data_space* got_irelative,
982 unsigned int plt_count);
985 // The class which scans relocations.
990 : issued_non_pic_error_(false)
994 get_reference_flags(unsigned int r_type);
997 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
998 Sized_relobj_file<size, false>* object,
999 unsigned int data_shndx,
1000 Output_section* output_section,
1001 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
1002 const elfcpp::Sym<size, false>& lsym,
1006 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
1007 Sized_relobj_file<size, false>* object,
1008 unsigned int data_shndx,
1009 Output_section* output_section,
1010 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
1014 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
1015 Target_x86_64* target,
1016 Sized_relobj_file<size, false>* object,
1017 unsigned int data_shndx,
1018 Output_section* output_section,
1019 const elfcpp::Rela<size, false>& reloc,
1020 unsigned int r_type,
1021 const elfcpp::Sym<size, false>& lsym);
1024 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
1025 Target_x86_64* target,
1026 Sized_relobj_file<size, false>* object,
1027 unsigned int data_shndx,
1028 Output_section* output_section,
1029 const elfcpp::Rela<size, false>& reloc,
1030 unsigned int r_type,
1035 unsupported_reloc_local(Sized_relobj_file<size, false>*,
1036 unsigned int r_type);
1039 unsupported_reloc_global(Sized_relobj_file<size, false>*,
1040 unsigned int r_type, Symbol*);
1043 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
1046 possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
1047 unsigned int src_indx,
1048 unsigned int r_offset,
1049 unsigned int r_type);
1052 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
1053 unsigned int r_type);
1055 // Whether we have issued an error about a non-PIC compilation.
1056 bool issued_non_pic_error_;
1059 // The class which implements relocation.
1064 : skip_call_tls_get_addr_(false)
1069 if (this->skip_call_tls_get_addr_)
1071 // FIXME: This needs to specify the location somehow.
1072 gold_error(_("missing expected TLS relocation"));
1076 // Do a relocation. Return false if the caller should not issue
1077 // any warnings about this relocation.
1079 relocate(const Relocate_info<size, false>*, unsigned int,
1080 Target_x86_64*, Output_section*, size_t, const unsigned char*,
1081 const Sized_symbol<size>*, const Symbol_value<size>*,
1082 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1086 // Do a TLS relocation.
1088 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
1089 size_t relnum, const elfcpp::Rela<size, false>&,
1090 unsigned int r_type, const Sized_symbol<size>*,
1091 const Symbol_value<size>*,
1092 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1095 // Do a TLS General-Dynamic to Initial-Exec transition.
1097 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1098 const elfcpp::Rela<size, false>&, unsigned int r_type,
1099 typename elfcpp::Elf_types<size>::Elf_Addr value,
1100 unsigned char* view,
1101 typename elfcpp::Elf_types<size>::Elf_Addr,
1102 section_size_type view_size);
1104 // Do a TLS General-Dynamic to Local-Exec transition.
1106 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1107 Output_segment* tls_segment,
1108 const elfcpp::Rela<size, false>&, unsigned int r_type,
1109 typename elfcpp::Elf_types<size>::Elf_Addr value,
1110 unsigned char* view,
1111 section_size_type view_size);
1113 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1115 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1116 const elfcpp::Rela<size, false>&, unsigned int r_type,
1117 typename elfcpp::Elf_types<size>::Elf_Addr value,
1118 unsigned char* view,
1119 typename elfcpp::Elf_types<size>::Elf_Addr,
1120 section_size_type view_size);
1122 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1124 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1125 Output_segment* tls_segment,
1126 const elfcpp::Rela<size, false>&, unsigned int r_type,
1127 typename elfcpp::Elf_types<size>::Elf_Addr value,
1128 unsigned char* view,
1129 section_size_type view_size);
1131 // Do a TLS Local-Dynamic to Local-Exec transition.
1133 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
1134 Output_segment* tls_segment,
1135 const elfcpp::Rela<size, false>&, unsigned int r_type,
1136 typename elfcpp::Elf_types<size>::Elf_Addr value,
1137 unsigned char* view,
1138 section_size_type view_size);
1140 // Do a TLS Initial-Exec to Local-Exec transition.
1142 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
1143 Output_segment* tls_segment,
1144 const elfcpp::Rela<size, false>&, unsigned int r_type,
1145 typename elfcpp::Elf_types<size>::Elf_Addr value,
1146 unsigned char* view,
1147 section_size_type view_size);
1149 // This is set if we should skip the next reloc, which should be a
1150 // PLT32 reloc against ___tls_get_addr.
1151 bool skip_call_tls_get_addr_;
1154 // Check if relocation against this symbol is a candidate for
1156 // mov foo@GOTPCREL(%rip), %reg
1157 // to lea foo(%rip), %reg.
1158 template<class View_type>
1160 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
1161 size_t r_offset, View_type* view)
1163 gold_assert(gsym != NULL);
1164 // We cannot do the conversion unless it's one of these relocations.
1165 if (r_type != elfcpp::R_X86_64_GOTPCREL
1166 && r_type != elfcpp::R_X86_64_GOTPCRELX
1167 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
1169 // We cannot convert references to IFUNC symbols, or to symbols that
1170 // are not local to the current module.
1171 // We can't do predefined symbols because they may become undefined
1172 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1173 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1174 || gsym->is_undefined()
1175 || gsym->is_predefined()
1176 || gsym->is_from_dynobj()
1177 || gsym->is_preemptible())
1179 // If we are building a shared object and the symbol is protected, we may
1180 // need to go through the GOT.
1181 if (parameters->options().shared()
1182 && gsym->visibility() == elfcpp::STV_PROTECTED)
1184 // We cannot convert references to the _DYNAMIC symbol.
1185 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1187 // Check for a MOV opcode.
1188 return (*view)[r_offset - 2] == 0x8b;
1192 // callq *foo@GOTPCRELX(%rip) to
1194 // and jmpq *foo@GOTPCRELX(%rip) to
1197 template<class View_type>
1199 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1200 size_t r_offset, View_type* view)
1202 gold_assert(gsym != NULL);
1203 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1204 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1206 // We cannot convert references to IFUNC symbols, or to symbols that
1207 // are not local to the current module.
1208 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1209 || gsym->is_undefined ()
1210 || gsym->is_from_dynobj()
1211 || gsym->is_preemptible())
1213 // Check for a CALLQ or JMPQ opcode.
1214 return ((*view)[r_offset - 2] == 0xff
1215 && ((*view)[r_offset - 1] == 0x15
1216 || (*view)[r_offset - 1] == 0x25));
1219 // Adjust TLS relocation type based on the options and whether this
1220 // is a local symbol.
1221 static tls::Tls_optimization
1222 optimize_tls_reloc(bool is_final, int r_type);
1224 // Get the GOT section, creating it if necessary.
1225 Output_data_got<64, false>*
1226 got_section(Symbol_table*, Layout*);
1228 // Get the GOT PLT section.
1229 Output_data_got_plt_x86_64*
1230 got_plt_section() const
1232 gold_assert(this->got_plt_ != NULL);
1233 return this->got_plt_;
1236 // Get the GOT section for TLSDESC entries.
1237 Output_data_got<64, false>*
1238 got_tlsdesc_section() const
1240 gold_assert(this->got_tlsdesc_ != NULL);
1241 return this->got_tlsdesc_;
1244 // Create the PLT section.
1246 make_plt_section(Symbol_table* symtab, Layout* layout);
1248 // Create a PLT entry for a global symbol.
1250 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1252 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1254 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1255 Sized_relobj_file<size, false>* relobj,
1256 unsigned int local_sym_index);
1258 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1260 define_tls_base_symbol(Symbol_table*, Layout*);
1262 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1264 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1266 // Create a GOT entry for the TLS module index.
1268 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1269 Sized_relobj_file<size, false>* object);
1271 // Get the PLT section.
1272 Output_data_plt_x86_64<size>*
1275 gold_assert(this->plt_ != NULL);
1279 // Get the dynamic reloc section, creating it if necessary.
1281 rela_dyn_section(Layout*);
1283 // Get the section to use for TLSDESC relocations.
1285 rela_tlsdesc_section(Layout*) const;
1287 // Get the section to use for IRELATIVE relocations.
1289 rela_irelative_section(Layout*);
1291 // Add a potential copy relocation.
1293 copy_reloc(Symbol_table* symtab, Layout* layout,
1294 Sized_relobj_file<size, false>* object,
1295 unsigned int shndx, Output_section* output_section,
1296 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1298 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1299 this->copy_relocs_.copy_reloc(symtab, layout,
1300 symtab->get_sized_symbol<size>(sym),
1301 object, shndx, output_section,
1302 r_type, reloc.get_r_offset(),
1303 reloc.get_r_addend(),
1304 this->rela_dyn_section(layout));
1307 // Record a target-specific program property in the .note.gnu.property
1310 record_gnu_property(int, int, size_t, const unsigned char*, const Object*);
1312 // Merge the target-specific program properties from the current object.
1314 merge_gnu_properties(const Object*);
1316 // Finalize the target-specific program properties and add them back to
1319 do_finalize_gnu_properties(Layout*) const;
1321 // Information about this specific target which we pass to the
1322 // general Target structure.
1323 static const Target::Target_info x86_64_info;
1325 // The types of GOT entries needed for this platform.
1326 // These values are exposed to the ABI in an incremental link.
1327 // Do not renumber existing values without changing the version
1328 // number of the .gnu_incremental_inputs section.
1331 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1332 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1333 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1334 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1337 // This type is used as the argument to the target specific
1338 // relocation routines. The only target specific reloc is
1339 // R_X86_64_TLSDESC against a local symbol.
1342 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1343 : object(a_object), r_sym(a_r_sym)
1346 // The object in which the local symbol is defined.
1347 Sized_relobj_file<size, false>* object;
1348 // The local symbol index in the object.
1353 Output_data_got<64, false>* got_;
1355 Output_data_plt_x86_64<size>* plt_;
1356 // The GOT PLT section.
1357 Output_data_got_plt_x86_64* got_plt_;
1358 // The GOT section for IRELATIVE relocations.
1359 Output_data_space* got_irelative_;
1360 // The GOT section for TLSDESC relocations.
1361 Output_data_got<64, false>* got_tlsdesc_;
1362 // The _GLOBAL_OFFSET_TABLE_ symbol.
1363 Symbol* global_offset_table_;
1364 // The dynamic reloc section.
1365 Reloc_section* rela_dyn_;
1366 // The section to use for IRELATIVE relocs.
1367 Reloc_section* rela_irelative_;
1368 // Relocs saved to avoid a COPY reloc.
1369 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1370 // Offset of the GOT entry for the TLS module index.
1371 unsigned int got_mod_index_offset_;
1372 // We handle R_X86_64_TLSDESC against a local symbol as a target
1373 // specific relocation. Here we store the object and local symbol
1374 // index for the relocation.
1375 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1376 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1377 bool tls_base_symbol_defined_;
1378 // Target-specific program properties, from .note.gnu.property section.
1379 // Each bit represents a specific feature.
1380 uint32_t isa_1_used_;
1381 uint32_t isa_1_needed_;
1382 uint32_t feature_1_;
1383 // Target-specific properties from the current object.
1384 // These bits get ANDed into FEATURE_1_ after all properties for the object
1385 // have been processed.
1386 uint32_t object_feature_1_;
1387 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1388 bool seen_first_object_;
1392 const Target::Target_info Target_x86_64<64>::x86_64_info =
1395 false, // is_big_endian
1396 elfcpp::EM_X86_64, // machine_code
1397 false, // has_make_symbol
1398 false, // has_resolve
1399 true, // has_code_fill
1400 true, // is_default_stack_executable
1401 true, // can_icf_inline_merge_sections
1403 "/lib/ld64.so.1", // program interpreter
1404 0x400000, // default_text_segment_address
1405 0x1000, // abi_pagesize (overridable by -z max-page-size)
1406 0x1000, // common_pagesize (overridable by -z common-page-size)
1407 false, // isolate_execinstr
1409 elfcpp::SHN_UNDEF, // small_common_shndx
1410 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1411 0, // small_common_section_flags
1412 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1413 NULL, // attributes_section
1414 NULL, // attributes_vendor
1415 "_start", // entry_symbol_name
1416 32, // hash_entry_size
1417 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1421 const Target::Target_info Target_x86_64<32>::x86_64_info =
1424 false, // is_big_endian
1425 elfcpp::EM_X86_64, // machine_code
1426 false, // has_make_symbol
1427 false, // has_resolve
1428 true, // has_code_fill
1429 true, // is_default_stack_executable
1430 true, // can_icf_inline_merge_sections
1432 "/libx32/ldx32.so.1", // program interpreter
1433 0x400000, // default_text_segment_address
1434 0x1000, // abi_pagesize (overridable by -z max-page-size)
1435 0x1000, // common_pagesize (overridable by -z common-page-size)
1436 false, // isolate_execinstr
1438 elfcpp::SHN_UNDEF, // small_common_shndx
1439 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1440 0, // small_common_section_flags
1441 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1442 NULL, // attributes_section
1443 NULL, // attributes_vendor
1444 "_start", // entry_symbol_name
1445 32, // hash_entry_size
1446 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1449 // This is called when a new output section is created. This is where
1450 // we handle the SHF_X86_64_LARGE.
1454 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1456 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1457 os->set_is_large_section();
1460 // Get the GOT section, creating it if necessary.
1463 Output_data_got<64, false>*
1464 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1466 if (this->got_ == NULL)
1468 gold_assert(symtab != NULL && layout != NULL);
1470 // When using -z now, we can treat .got.plt as a relro section.
1471 // Without -z now, it is modified after program startup by lazy
1473 bool is_got_plt_relro = parameters->options().now();
1474 Output_section_order got_order = (is_got_plt_relro
1476 : ORDER_RELRO_LAST);
1477 Output_section_order got_plt_order = (is_got_plt_relro
1479 : ORDER_NON_RELRO_FIRST);
1481 this->got_ = new Output_data_got<64, false>();
1483 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1485 | elfcpp::SHF_WRITE),
1486 this->got_, got_order, true);
1488 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1489 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1491 | elfcpp::SHF_WRITE),
1492 this->got_plt_, got_plt_order,
1495 // The first three entries are reserved.
1496 this->got_plt_->set_current_data_size(3 * 8);
1498 if (!is_got_plt_relro)
1500 // Those bytes can go into the relro segment.
1501 layout->increase_relro(3 * 8);
1504 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1505 this->global_offset_table_ =
1506 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1507 Symbol_table::PREDEFINED,
1509 0, 0, elfcpp::STT_OBJECT,
1511 elfcpp::STV_HIDDEN, 0,
1514 // If there are any IRELATIVE relocations, they get GOT entries
1515 // in .got.plt after the jump slot entries.
1516 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1517 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1519 | elfcpp::SHF_WRITE),
1520 this->got_irelative_,
1521 got_plt_order, is_got_plt_relro);
1523 // If there are any TLSDESC relocations, they get GOT entries in
1524 // .got.plt after the jump slot and IRELATIVE entries.
1525 this->got_tlsdesc_ = new Output_data_got<64, false>();
1526 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1528 | elfcpp::SHF_WRITE),
1530 got_plt_order, is_got_plt_relro);
1536 // Get the dynamic reloc section, creating it if necessary.
1539 typename Target_x86_64<size>::Reloc_section*
1540 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1542 if (this->rela_dyn_ == NULL)
1544 gold_assert(layout != NULL);
1545 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1546 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1547 elfcpp::SHF_ALLOC, this->rela_dyn_,
1548 ORDER_DYNAMIC_RELOCS, false);
1550 return this->rela_dyn_;
1553 // Get the section to use for IRELATIVE relocs, creating it if
1554 // necessary. These go in .rela.dyn, but only after all other dynamic
1555 // relocations. They need to follow the other dynamic relocations so
1556 // that they can refer to global variables initialized by those
1560 typename Target_x86_64<size>::Reloc_section*
1561 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1563 if (this->rela_irelative_ == NULL)
1565 // Make sure we have already created the dynamic reloc section.
1566 this->rela_dyn_section(layout);
1567 this->rela_irelative_ = new Reloc_section(false);
1568 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1569 elfcpp::SHF_ALLOC, this->rela_irelative_,
1570 ORDER_DYNAMIC_RELOCS, false);
1571 gold_assert(this->rela_dyn_->output_section()
1572 == this->rela_irelative_->output_section());
1574 return this->rela_irelative_;
1577 // Record a target-specific program property from the .note.gnu.property
1581 Target_x86_64<size>::record_gnu_property(
1583 size_t pr_datasz, const unsigned char* pr_data,
1584 const Object* object)
1590 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1591 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1592 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1595 gold_warning(_("%s: corrupt .note.gnu.property section "
1596 "(pr_datasz for property %d is not 4)"),
1597 object->name().c_str(), pr_type);
1600 val = elfcpp::Swap<32, false>::readval(pr_data);
1603 gold_warning(_("%s: unknown program property type 0x%x "
1604 "in .note.gnu.property section"),
1605 object->name().c_str(), pr_type);
1611 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1612 this->isa_1_used_ |= val;
1614 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1615 this->isa_1_needed_ |= val;
1617 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1618 // If we see multiple feature props in one object, OR them together.
1619 this->object_feature_1_ |= val;
1624 // Merge the target-specific program properties from the current object.
1627 Target_x86_64<size>::merge_gnu_properties(const Object*)
1629 if (this->seen_first_object_)
1630 this->feature_1_ &= this->object_feature_1_;
1633 this->feature_1_ = this->object_feature_1_;
1634 this->seen_first_object_ = true;
1636 this->object_feature_1_ = 0;
1640 add_property(Layout* layout, unsigned int pr_type, uint32_t val)
1642 unsigned char buf[4];
1643 elfcpp::Swap<32, false>::writeval(buf, val);
1644 layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
1647 // Finalize the target-specific program properties and add them back to
1651 Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const
1653 if (this->isa_1_used_ != 0)
1654 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
1656 if (this->isa_1_needed_ != 0)
1657 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
1658 this->isa_1_needed_);
1659 if (this->feature_1_ != 0)
1660 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
1664 // Write the first three reserved words of the .got.plt section.
1665 // The remainder of the section is written while writing the PLT
1666 // in Output_data_plt_i386::do_write.
1669 Output_data_got_plt_x86_64::do_write(Output_file* of)
1671 // The first entry in the GOT is the address of the .dynamic section
1672 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1673 // We saved space for them when we created the section in
1674 // Target_x86_64::got_section.
1675 const off_t got_file_offset = this->offset();
1676 gold_assert(this->data_size() >= 24);
1677 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1678 Output_section* dynamic = this->layout_->dynamic_section();
1679 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1680 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1681 memset(got_view + 8, 0, 16);
1682 of->write_output_view(got_file_offset, 24, got_view);
1685 // Initialize the PLT section.
1689 Output_data_plt_x86_64<size>::init(Layout* layout)
1691 this->rel_ = new Reloc_section(false);
1692 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1693 elfcpp::SHF_ALLOC, this->rel_,
1694 ORDER_DYNAMIC_PLT_RELOCS, false);
1699 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1701 os->set_entsize(this->get_plt_entry_size());
1704 // Add an entry to the PLT.
1708 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1711 gold_assert(!gsym->has_plt_offset());
1713 unsigned int plt_index;
1715 section_offset_type got_offset;
1717 unsigned int* pcount;
1718 unsigned int offset;
1719 unsigned int reserved;
1720 Output_section_data_build* got;
1721 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1722 && gsym->can_use_relative_reloc(false))
1724 pcount = &this->irelative_count_;
1727 got = this->got_irelative_;
1731 pcount = &this->count_;
1734 got = this->got_plt_;
1737 if (!this->is_data_size_valid())
1739 // Note that when setting the PLT offset for a non-IRELATIVE
1740 // entry we skip the initial reserved PLT entry.
1741 plt_index = *pcount + offset;
1742 plt_offset = plt_index * this->get_plt_entry_size();
1746 got_offset = (plt_index - offset + reserved) * 8;
1747 gold_assert(got_offset == got->current_data_size());
1749 // Every PLT entry needs a GOT entry which points back to the PLT
1750 // entry (this will be changed by the dynamic linker, normally
1751 // lazily when the function is called).
1752 got->set_current_data_size(got_offset + 8);
1756 // FIXME: This is probably not correct for IRELATIVE relocs.
1758 // For incremental updates, find an available slot.
1759 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1760 this->get_plt_entry_size(), 0);
1761 if (plt_offset == -1)
1762 gold_fallback(_("out of patch space (PLT);"
1763 " relink with --incremental-full"));
1765 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1766 // can be calculated from the PLT index, adjusting for the three
1767 // reserved entries at the beginning of the GOT.
1768 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1769 got_offset = (plt_index - offset + reserved) * 8;
1772 gsym->set_plt_offset(plt_offset);
1774 // Every PLT entry needs a reloc.
1775 this->add_relocation(symtab, layout, gsym, got_offset);
1777 // Note that we don't need to save the symbol. The contents of the
1778 // PLT are independent of which symbols are used. The symbols only
1779 // appear in the relocations.
1782 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1787 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1788 Symbol_table* symtab,
1790 Sized_relobj_file<size, false>* relobj,
1791 unsigned int local_sym_index)
1793 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1794 ++this->irelative_count_;
1796 section_offset_type got_offset = this->got_irelative_->current_data_size();
1798 // Every PLT entry needs a GOT entry which points back to the PLT
1800 this->got_irelative_->set_current_data_size(got_offset + 8);
1802 // Every PLT entry needs a reloc.
1803 Reloc_section* rela = this->rela_irelative(symtab, layout);
1804 rela->add_symbolless_local_addend(relobj, local_sym_index,
1805 elfcpp::R_X86_64_IRELATIVE,
1806 this->got_irelative_, got_offset, 0);
1811 // Add the relocation for a PLT entry.
1815 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1818 unsigned int got_offset)
1820 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1821 && gsym->can_use_relative_reloc(false))
1823 Reloc_section* rela = this->rela_irelative(symtab, layout);
1824 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1825 this->got_irelative_, got_offset, 0);
1829 gsym->set_needs_dynsym_entry();
1830 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1835 // Return where the TLSDESC relocations should go, creating it if
1836 // necessary. These follow the JUMP_SLOT relocations.
1839 typename Output_data_plt_x86_64<size>::Reloc_section*
1840 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1842 if (this->tlsdesc_rel_ == NULL)
1844 this->tlsdesc_rel_ = new Reloc_section(false);
1845 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1846 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1847 ORDER_DYNAMIC_PLT_RELOCS, false);
1848 gold_assert(this->tlsdesc_rel_->output_section()
1849 == this->rel_->output_section());
1851 return this->tlsdesc_rel_;
1854 // Return where the IRELATIVE relocations should go in the PLT. These
1855 // follow the JUMP_SLOT and the TLSDESC relocations.
1858 typename Output_data_plt_x86_64<size>::Reloc_section*
1859 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1862 if (this->irelative_rel_ == NULL)
1864 // Make sure we have a place for the TLSDESC relocations, in
1865 // case we see any later on.
1866 this->rela_tlsdesc(layout);
1867 this->irelative_rel_ = new Reloc_section(false);
1868 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1869 elfcpp::SHF_ALLOC, this->irelative_rel_,
1870 ORDER_DYNAMIC_PLT_RELOCS, false);
1871 gold_assert(this->irelative_rel_->output_section()
1872 == this->rel_->output_section());
1874 if (parameters->doing_static_link())
1876 // A statically linked executable will only have a .rela.plt
1877 // section to hold R_X86_64_IRELATIVE relocs for
1878 // STT_GNU_IFUNC symbols. The library will use these
1879 // symbols to locate the IRELATIVE relocs at program startup
1881 symtab->define_in_output_data("__rela_iplt_start", NULL,
1882 Symbol_table::PREDEFINED,
1883 this->irelative_rel_, 0, 0,
1884 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1885 elfcpp::STV_HIDDEN, 0, false, true);
1886 symtab->define_in_output_data("__rela_iplt_end", NULL,
1887 Symbol_table::PREDEFINED,
1888 this->irelative_rel_, 0, 0,
1889 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1890 elfcpp::STV_HIDDEN, 0, true, true);
1893 return this->irelative_rel_;
1896 // Return the PLT address to use for a global symbol.
1900 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1902 uint64_t offset = 0;
1903 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1904 && gsym->can_use_relative_reloc(false))
1905 offset = (this->count_ + 1) * this->get_plt_entry_size();
1906 return this->address() + offset + gsym->plt_offset();
1909 // Return the PLT address to use for a local symbol. These are always
1910 // IRELATIVE relocs.
1914 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1917 return (this->address()
1918 + (this->count_ + 1) * this->get_plt_entry_size()
1919 + object->local_plt_offset(r_sym));
1922 // Set the final size.
1925 Output_data_plt_x86_64<size>::set_final_data_size()
1927 // Number of regular and IFUNC PLT entries, plus the first entry.
1928 unsigned int count = this->count_ + this->irelative_count_ + 1;
1929 // Count the TLSDESC entry, if present.
1930 if (this->has_tlsdesc_entry())
1932 this->set_data_size(count * this->get_plt_entry_size());
1935 // The first entry in the PLT for an executable.
1939 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1941 // From AMD64 ABI Draft 0.98, page 76
1942 0xff, 0x35, // pushq contents of memory address
1943 0, 0, 0, 0, // replaced with address of .got + 8
1944 0xff, 0x25, // jmp indirect
1945 0, 0, 0, 0, // replaced with address of .got + 16
1946 0x90, 0x90, 0x90, 0x90 // noop (x4)
1951 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1953 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1954 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1956 memcpy(pov, first_plt_entry, plt_entry_size);
1957 // We do a jmp relative to the PC at the end of this instruction.
1958 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1960 - (plt_address + 6)));
1961 elfcpp::Swap<32, false>::writeval(pov + 8,
1963 - (plt_address + 12)));
1966 // Subsequent entries in the PLT for an executable.
1970 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1972 // From AMD64 ABI Draft 0.98, page 76
1973 0xff, 0x25, // jmpq indirect
1974 0, 0, 0, 0, // replaced with address of symbol in .got
1975 0x68, // pushq immediate
1976 0, 0, 0, 0, // replaced with offset into relocation table
1977 0xe9, // jmpq relative
1978 0, 0, 0, 0 // replaced with offset to start of .plt
1983 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1985 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1986 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1987 unsigned int got_offset,
1988 unsigned int plt_offset,
1989 unsigned int plt_index)
1991 // Check PC-relative offset overflow in PLT entry.
1992 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1993 - (plt_address + plt_offset + 6));
1994 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1995 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1998 memcpy(pov, plt_entry, plt_entry_size);
1999 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2000 plt_got_pcrel_offset);
2002 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
2003 elfcpp::Swap<32, false>::writeval(pov + 12,
2004 - (plt_offset + plt_entry_size));
2009 // The reserved TLSDESC entry in the PLT for an executable.
2013 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
2015 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2016 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2017 0xff, 0x35, // pushq x(%rip)
2018 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2019 0xff, 0x25, // jmpq *y(%rip)
2020 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2027 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
2029 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2030 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2031 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2032 unsigned int tlsdesc_got_offset,
2033 unsigned int plt_offset)
2035 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2036 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2038 - (plt_address + plt_offset
2040 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
2042 + tlsdesc_got_offset
2043 - (plt_address + plt_offset
2047 // Return the APLT address to use for a global symbol (for -z bndplt).
2050 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
2052 uint64_t offset = this->aplt_offset_;
2053 // Convert the PLT offset into an APLT offset.
2054 unsigned int plt_offset = gsym->plt_offset();
2055 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2056 && gsym->can_use_relative_reloc(false))
2057 offset += this->regular_count() * aplt_entry_size;
2059 plt_offset -= plt_entry_size;
2060 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
2061 return this->address() + offset + plt_offset;
2064 // Return the PLT address to use for a local symbol. These are always
2065 // IRELATIVE relocs.
2068 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
2071 // Convert the PLT offset into an APLT offset.
2072 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size)
2073 / (plt_entry_size / aplt_entry_size));
2074 return (this->address()
2075 + this->aplt_offset_
2076 + this->regular_count() * aplt_entry_size
2080 // Set the final size.
2082 Output_data_plt_x86_64_bnd::set_final_data_size()
2084 // Number of regular and IFUNC PLT entries.
2085 unsigned int count = this->entry_count();
2086 // Count the first entry and the TLSDESC entry, if present.
2087 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2088 unsigned int plt_size = (count + extra) * plt_entry_size;
2089 // Offset of the APLT.
2090 this->aplt_offset_ = plt_size;
2091 // Size of the APLT.
2092 plt_size += count * aplt_entry_size;
2093 this->set_data_size(plt_size);
2096 // The first entry in the BND PLT.
2099 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
2101 // From AMD64 ABI Draft 0.98, page 76
2102 0xff, 0x35, // pushq contents of memory address
2103 0, 0, 0, 0, // replaced with address of .got + 8
2104 0xf2, 0xff, 0x25, // bnd jmp indirect
2105 0, 0, 0, 0, // replaced with address of .got + 16
2106 0x0f, 0x1f, 0x00 // nop
2110 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
2112 elfcpp::Elf_types<64>::Elf_Addr got_address,
2113 elfcpp::Elf_types<64>::Elf_Addr plt_address)
2115 memcpy(pov, first_plt_entry, plt_entry_size);
2116 // We do a jmp relative to the PC at the end of this instruction.
2117 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2119 - (plt_address + 6)));
2120 elfcpp::Swap<32, false>::writeval(pov + 9,
2122 - (plt_address + 13)));
2125 // Subsequent entries in the BND PLT.
2128 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
2130 // From AMD64 ABI Draft 0.99.8, page 139
2131 0x68, // pushq immediate
2132 0, 0, 0, 0, // replaced with offset into relocation table
2133 0xf2, 0xe9, // bnd jmpq relative
2134 0, 0, 0, 0, // replaced with offset to start of .plt
2135 0x0f, 0x1f, 0x44, 0, 0 // nop
2138 // Entries in the BND Additional PLT.
2141 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
2143 // From AMD64 ABI Draft 0.99.8, page 139
2144 0xf2, 0xff, 0x25, // bnd jmpq indirect
2145 0, 0, 0, 0, // replaced with address of symbol in .got
2150 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
2152 elfcpp::Elf_types<64>::Elf_Addr,
2153 elfcpp::Elf_types<64>::Elf_Addr,
2155 unsigned int plt_offset,
2156 unsigned int plt_index)
2158 memcpy(pov, plt_entry, plt_entry_size);
2159 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
2160 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
2165 Output_data_plt_x86_64_bnd::fill_aplt_entry(
2167 elfcpp::Elf_types<64>::Elf_Addr got_address,
2168 elfcpp::Elf_types<64>::Elf_Addr plt_address,
2169 unsigned int got_offset,
2170 unsigned int plt_offset,
2171 unsigned int plt_index)
2173 // Check PC-relative offset overflow in PLT entry.
2174 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2175 - (plt_address + plt_offset + 7));
2176 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2177 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2180 memcpy(pov, aplt_entry, aplt_entry_size);
2181 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
2184 // The reserved TLSDESC entry in the PLT for an executable.
2187 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
2189 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2190 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2191 0xff, 0x35, // pushq x(%rip)
2192 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2193 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2194 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2195 0x0f, 0x1f, 0 // nop
2199 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
2201 elfcpp::Elf_types<64>::Elf_Addr got_address,
2202 elfcpp::Elf_types<64>::Elf_Addr plt_address,
2203 elfcpp::Elf_types<64>::Elf_Addr got_base,
2204 unsigned int tlsdesc_got_offset,
2205 unsigned int plt_offset)
2207 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2208 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2210 - (plt_address + plt_offset
2212 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
2214 + tlsdesc_got_offset
2215 - (plt_address + plt_offset
2219 // Return the APLT address to use for a global symbol (for IBT).
2223 Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym)
2225 uint64_t offset = this->aplt_offset_;
2226 // Convert the PLT offset into an APLT offset.
2227 unsigned int plt_offset = gsym->plt_offset();
2228 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2229 && gsym->can_use_relative_reloc(false))
2230 offset += this->regular_count() * aplt_entry_size;
2232 plt_offset -= plt_entry_size;
2233 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
2234 return this->address() + offset + plt_offset;
2237 // Return the PLT address to use for a local symbol. These are always
2238 // IRELATIVE relocs.
2242 Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object,
2245 // Convert the PLT offset into an APLT offset.
2246 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size)
2247 / (plt_entry_size / aplt_entry_size));
2248 return (this->address()
2249 + this->aplt_offset_
2250 + this->regular_count() * aplt_entry_size
2254 // Set the final size.
2258 Output_data_plt_x86_64_ibt<size>::set_final_data_size()
2260 // Number of regular and IFUNC PLT entries.
2261 unsigned int count = this->entry_count();
2262 // Count the first entry and the TLSDESC entry, if present.
2263 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2264 unsigned int plt_size = (count + extra) * plt_entry_size;
2265 // Offset of the APLT.
2266 this->aplt_offset_ = plt_size;
2267 // Size of the APLT.
2268 plt_size += count * aplt_entry_size;
2269 this->set_data_size(plt_size);
2272 // The first entry in the IBT PLT.
2276 Output_data_plt_x86_64_ibt<32>::first_plt_entry[plt_entry_size] =
2278 // MPX isn't supported for x32, so we don't need the BND prefix.
2279 // From AMD64 ABI Draft 0.98, page 76
2280 0xff, 0x35, // pushq contents of memory address
2281 0, 0, 0, 0, // replaced with address of .got + 8
2282 0xff, 0x25, // jmp indirect
2283 0, 0, 0, 0, // replaced with address of .got + 16
2284 0x90, 0x90, 0x90, 0x90 // noop (x4)
2289 Output_data_plt_x86_64_ibt<64>::first_plt_entry[plt_entry_size] =
2291 // Use the BND prefix so that IBT is compatible with MPX.
2292 0xff, 0x35, // pushq contents of memory address
2293 0, 0, 0, 0, // replaced with address of .got + 8
2294 0xf2, 0xff, 0x25, // bnd jmp indirect
2295 0, 0, 0, 0, // replaced with address of .got + 16
2296 0x0f, 0x1f, 0x00 // nop
2301 Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry(
2303 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2304 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
2306 // Offsets to the addresses needing relocation.
2307 const unsigned int roff1 = 2;
2308 const unsigned int roff2 = (size == 32) ? 8 : 9;
2310 memcpy(pov, first_plt_entry, plt_entry_size);
2311 // We do a jmp relative to the PC at the end of this instruction.
2312 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1,
2314 - (plt_address + roff1 + 4)));
2315 elfcpp::Swap<32, false>::writeval(pov + roff2,
2317 - (plt_address + roff2 + 4)));
2320 // Subsequent entries in the IBT PLT.
2324 Output_data_plt_x86_64_ibt<32>::plt_entry[plt_entry_size] =
2326 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2327 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2328 0x68, // pushq immediate
2329 0, 0, 0, 0, // replaced with offset into relocation table
2330 0xe9, // jmpq relative
2331 0, 0, 0, 0, // replaced with offset to start of .plt
2337 Output_data_plt_x86_64_ibt<64>::plt_entry[plt_entry_size] =
2339 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2340 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2341 0x68, // pushq immediate
2342 0, 0, 0, 0, // replaced with offset into relocation table
2343 0xf2, 0xe9, // bnd jmpq relative
2344 0, 0, 0, 0, // replaced with offset to start of .plt
2348 // Entries in the IBT Additional PLT.
2352 Output_data_plt_x86_64_ibt<32>::aplt_entry[aplt_entry_size] =
2354 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2355 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2356 0xff, 0x25, // jmpq indirect
2357 0, 0, 0, 0, // replaced with address of symbol in .got
2358 0x0f, 0x1f, 0x04, 0x00, // nop
2364 Output_data_plt_x86_64_ibt<64>::aplt_entry[aplt_entry_size] =
2366 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2367 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2368 0xf2, 0xff, 0x25, // bnd jmpq indirect
2369 0, 0, 0, 0, // replaced with address of symbol in .got
2370 0x0f, 0x1f, 0x04, 0x00, // nop
2376 Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry(
2378 typename elfcpp::Elf_types<size>::Elf_Addr,
2379 typename elfcpp::Elf_types<size>::Elf_Addr,
2381 unsigned int plt_offset,
2382 unsigned int plt_index)
2384 // Offsets to the addresses needing relocation.
2385 const unsigned int roff1 = 5;
2386 const unsigned int roff2 = (size == 32) ? 10 : 11;
2388 memcpy(pov, plt_entry, plt_entry_size);
2389 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index);
2390 elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4));
2396 Output_data_plt_x86_64_ibt<size>::fill_aplt_entry(
2398 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2399 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2400 unsigned int got_offset,
2401 unsigned int plt_offset,
2402 unsigned int plt_index)
2404 // Offset to the address needing relocation.
2405 const unsigned int roff = (size == 32) ? 6 : 7;
2407 // Check PC-relative offset overflow in PLT entry.
2408 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2409 - (plt_address + plt_offset + roff + 4));
2410 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2411 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2414 memcpy(pov, aplt_entry, aplt_entry_size);
2415 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset);
2418 // The reserved TLSDESC entry in the IBT PLT for an executable.
2422 Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] =
2424 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2425 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2426 0xff, 0x35, // pushq x(%rip)
2427 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2428 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2429 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2430 0x0f, 0x1f, 0 // nop
2435 Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry(
2437 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2438 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2439 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2440 unsigned int tlsdesc_got_offset,
2441 unsigned int plt_offset)
2443 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2444 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2446 - (plt_address + plt_offset
2448 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
2450 + tlsdesc_got_offset
2451 - (plt_address + plt_offset
2455 // The .eh_frame unwind information for the PLT.
2459 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
2462 'z', // Augmentation: augmentation size included.
2463 'R', // Augmentation: FDE encoding included.
2464 '\0', // End of augmentation string.
2465 1, // Code alignment factor.
2466 0x78, // Data alignment factor.
2467 16, // Return address column.
2468 1, // Augmentation size.
2469 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
2470 | elfcpp::DW_EH_PE_sdata4),
2471 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2472 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2473 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2479 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2481 0, 0, 0, 0, // Replaced with offset to .plt.
2482 0, 0, 0, 0, // Replaced with size of .plt.
2483 0, // Augmentation size.
2484 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2485 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2486 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2487 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2488 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2489 11, // Block length.
2490 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2491 elfcpp::DW_OP_breg16, 0, // Push %rip.
2492 elfcpp::DW_OP_lit15, // Push 0xf.
2493 elfcpp::DW_OP_and, // & (%rip & 0xf).
2494 elfcpp::DW_OP_lit11, // Push 0xb.
2495 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2496 elfcpp::DW_OP_lit3, // Push 3.
2497 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2498 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2499 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2505 // The .eh_frame unwind information for the BND PLT.
2507 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2509 0, 0, 0, 0, // Replaced with offset to .plt.
2510 0, 0, 0, 0, // Replaced with size of .plt.
2511 0, // Augmentation size.
2512 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2513 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2514 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2515 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2516 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2517 11, // Block length.
2518 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2519 elfcpp::DW_OP_breg16, 0, // Push %rip.
2520 elfcpp::DW_OP_lit15, // Push 0xf.
2521 elfcpp::DW_OP_and, // & (%rip & 0xf).
2522 elfcpp::DW_OP_lit5, // Push 5.
2523 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
2524 elfcpp::DW_OP_lit3, // Push 3.
2525 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
2526 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2527 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2533 // The .eh_frame unwind information for the BND PLT.
2536 Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2538 0, 0, 0, 0, // Replaced with offset to .plt.
2539 0, 0, 0, 0, // Replaced with size of .plt.
2540 0, // Augmentation size.
2541 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2542 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2543 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2544 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2545 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2546 11, // Block length.
2547 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2548 elfcpp::DW_OP_breg16, 0, // Push %rip.
2549 elfcpp::DW_OP_lit15, // Push 0xf.
2550 elfcpp::DW_OP_and, // & (%rip & 0xf).
2551 elfcpp::DW_OP_lit9, // Push 9.
2552 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9)
2553 elfcpp::DW_OP_lit3, // Push 3.
2554 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3)
2555 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2556 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2562 // Write out the PLT. This uses the hand-coded instructions above,
2563 // and adjusts them as needed. This is specified by the AMD64 ABI.
2567 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2569 const off_t offset = this->offset();
2570 const section_size_type oview_size =
2571 convert_to_section_size_type(this->data_size());
2572 unsigned char* const oview = of->get_output_view(offset, oview_size);
2574 const off_t got_file_offset = this->got_plt_->offset();
2575 gold_assert(parameters->incremental_update()
2576 || (got_file_offset + this->got_plt_->data_size()
2577 == this->got_irelative_->offset()));
2578 const section_size_type got_size =
2579 convert_to_section_size_type(this->got_plt_->data_size()
2580 + this->got_irelative_->data_size());
2581 unsigned char* const got_view = of->get_output_view(got_file_offset,
2584 unsigned char* pov = oview;
2586 // The base address of the .plt section.
2587 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2588 // The base address of the .got section.
2589 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2590 // The base address of the PLT portion of the .got section,
2591 // which is where the GOT pointer will point, and where the
2592 // three reserved GOT entries are located.
2593 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2594 = this->got_plt_->address();
2596 this->fill_first_plt_entry(pov, got_address, plt_address);
2597 pov += this->get_plt_entry_size();
2599 // The first three entries in the GOT are reserved, and are written
2600 // by Output_data_got_plt_x86_64::do_write.
2601 unsigned char* got_pov = got_view + 24;
2603 unsigned int plt_offset = this->get_plt_entry_size();
2604 unsigned int got_offset = 24;
2605 const unsigned int count = this->count_ + this->irelative_count_;
2606 for (unsigned int plt_index = 0;
2609 pov += this->get_plt_entry_size(),
2611 plt_offset += this->get_plt_entry_size(),
2614 // Set and adjust the PLT entry itself.
2615 unsigned int lazy_offset = this->fill_plt_entry(pov,
2616 got_address, plt_address,
2617 got_offset, plt_offset,
2620 // Set the entry in the GOT.
2621 elfcpp::Swap<64, false>::writeval(got_pov,
2622 plt_address + plt_offset + lazy_offset);
2625 if (this->has_tlsdesc_entry())
2627 // Set and adjust the reserved TLSDESC PLT entry.
2628 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2629 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2630 tlsdesc_got_offset, plt_offset);
2631 pov += this->get_plt_entry_size();
2634 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2635 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2637 of->write_output_view(offset, oview_size, oview);
2638 of->write_output_view(got_file_offset, got_size, got_view);
2641 // Write out the BND PLT.
2644 Output_data_plt_x86_64_bnd::do_write(Output_file* of)
2646 const off_t offset = this->offset();
2647 const section_size_type oview_size =
2648 convert_to_section_size_type(this->data_size());
2649 unsigned char* const oview = of->get_output_view(offset, oview_size);
2651 Output_data_got<64, false>* got = this->got();
2652 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2653 Output_data_space* got_irelative = this->got_irelative();
2655 const off_t got_file_offset = got_plt->offset();
2656 gold_assert(parameters->incremental_update()
2657 || (got_file_offset + got_plt->data_size()
2658 == got_irelative->offset()));
2659 const section_size_type got_size =
2660 convert_to_section_size_type(got_plt->data_size()
2661 + got_irelative->data_size());
2662 unsigned char* const got_view = of->get_output_view(got_file_offset,
2665 unsigned char* pov = oview;
2667 // The base address of the .plt section.
2668 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2669 // The base address of the .got section.
2670 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2671 // The base address of the PLT portion of the .got section,
2672 // which is where the GOT pointer will point, and where the
2673 // three reserved GOT entries are located.
2674 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2676 this->fill_first_plt_entry(pov, got_address, plt_address);
2677 pov += plt_entry_size;
2679 // The first three entries in the GOT are reserved, and are written
2680 // by Output_data_got_plt_x86_64::do_write.
2681 unsigned char* got_pov = got_view + 24;
2683 unsigned int plt_offset = plt_entry_size;
2684 unsigned int got_offset = 24;
2685 const unsigned int count = this->entry_count();
2686 for (unsigned int plt_index = 0;
2689 pov += plt_entry_size,
2691 plt_offset += plt_entry_size,
2694 // Set and adjust the PLT entry itself.
2695 unsigned int lazy_offset = this->fill_plt_entry(pov,
2696 got_address, plt_address,
2697 got_offset, plt_offset,
2700 // Set the entry in the GOT.
2701 elfcpp::Swap<64, false>::writeval(got_pov,
2702 plt_address + plt_offset + lazy_offset);
2705 if (this->has_tlsdesc_entry())
2707 // Set and adjust the reserved TLSDESC PLT entry.
2708 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2709 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2710 tlsdesc_got_offset, plt_offset);
2711 pov += this->get_plt_entry_size();
2714 // Write the additional PLT.
2716 for (unsigned int plt_index = 0;
2719 pov += aplt_entry_size,
2720 plt_offset += aplt_entry_size,
2723 // Set and adjust the APLT entry.
2724 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2725 plt_offset, plt_index);
2728 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2729 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2731 of->write_output_view(offset, oview_size, oview);
2732 of->write_output_view(got_file_offset, got_size, got_view);
2735 // Write out the IBT PLT.
2739 Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of)
2741 const off_t offset = this->offset();
2742 const section_size_type oview_size =
2743 convert_to_section_size_type(this->data_size());
2744 unsigned char* const oview = of->get_output_view(offset, oview_size);
2746 Output_data_got<64, false>* got = this->got();
2747 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2748 Output_data_space* got_irelative = this->got_irelative();
2750 const off_t got_file_offset = got_plt->offset();
2751 gold_assert(parameters->incremental_update()
2752 || (got_file_offset + got_plt->data_size()
2753 == got_irelative->offset()));
2754 const section_size_type got_size =
2755 convert_to_section_size_type(got_plt->data_size()
2756 + got_irelative->data_size());
2757 unsigned char* const got_view = of->get_output_view(got_file_offset,
2760 unsigned char* pov = oview;
2762 // The base address of the .plt section.
2763 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2764 // The base address of the .got section.
2765 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2766 // The base address of the PLT portion of the .got section,
2767 // which is where the GOT pointer will point, and where the
2768 // three reserved GOT entries are located.
2769 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2771 this->fill_first_plt_entry(pov, got_address, plt_address);
2772 pov += plt_entry_size;
2774 // The first three entries in the GOT are reserved, and are written
2775 // by Output_data_got_plt_x86_64::do_write.
2776 unsigned char* got_pov = got_view + 24;
2778 unsigned int plt_offset = plt_entry_size;
2779 unsigned int got_offset = 24;
2780 const unsigned int count = this->entry_count();
2781 for (unsigned int plt_index = 0;
2784 pov += plt_entry_size,
2786 plt_offset += plt_entry_size,
2789 // Set and adjust the PLT entry itself.
2790 unsigned int lazy_offset = this->fill_plt_entry(pov,
2791 got_address, plt_address,
2792 got_offset, plt_offset,
2795 // Set the entry in the GOT.
2796 elfcpp::Swap<64, false>::writeval(got_pov,
2797 plt_address + plt_offset + lazy_offset);
2800 if (this->has_tlsdesc_entry())
2802 // Set and adjust the reserved TLSDESC PLT entry.
2803 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2804 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2805 tlsdesc_got_offset, plt_offset);
2806 pov += this->get_plt_entry_size();
2809 // Write the additional PLT.
2811 for (unsigned int plt_index = 0;
2814 pov += aplt_entry_size,
2815 plt_offset += aplt_entry_size,
2818 // Set and adjust the APLT entry.
2819 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2820 plt_offset, plt_index);
2823 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2824 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2826 of->write_output_view(offset, oview_size, oview);
2827 of->write_output_view(got_file_offset, got_size, got_view);
2830 // Create the PLT section.
2834 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2836 if (this->plt_ == NULL)
2838 // Create the GOT sections first.
2839 this->got_section(symtab, layout);
2841 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2842 this->got_irelative_);
2844 // Add unwind information if requested.
2845 if (parameters->options().ld_generated_unwind_info())
2846 this->plt_->add_eh_frame(layout);
2848 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2850 | elfcpp::SHF_EXECINSTR),
2851 this->plt_, ORDER_PLT, false);
2853 // Make the sh_info field of .rela.plt point to .plt.
2854 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2855 rela_plt_os->set_info_section(this->plt_->output_section());
2860 Output_data_plt_x86_64<32>*
2861 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2862 Output_data_got<64, false>* got,
2863 Output_data_got_plt_x86_64* got_plt,
2864 Output_data_space* got_irelative)
2866 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2867 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2869 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2874 Output_data_plt_x86_64<64>*
2875 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2876 Output_data_got<64, false>* got,
2877 Output_data_got_plt_x86_64* got_plt,
2878 Output_data_space* got_irelative)
2880 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2881 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2883 else if (parameters->options().bndplt())
2884 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2887 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2892 Output_data_plt_x86_64<32>*
2893 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2894 Output_data_got<64, false>* got,
2895 Output_data_got_plt_x86_64* got_plt,
2896 Output_data_space* got_irelative,
2897 unsigned int plt_count)
2899 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2900 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2901 got_irelative, plt_count);
2902 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2903 got_irelative, plt_count);
2907 Output_data_plt_x86_64<64>*
2908 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2909 Output_data_got<64, false>* got,
2910 Output_data_got_plt_x86_64* got_plt,
2911 Output_data_space* got_irelative,
2912 unsigned int plt_count)
2914 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2915 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2916 got_irelative, plt_count);
2917 else if (parameters->options().bndplt())
2918 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2919 got_irelative, plt_count);
2921 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2926 // Return the section for TLSDESC relocations.
2929 typename Target_x86_64<size>::Reloc_section*
2930 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2932 return this->plt_section()->rela_tlsdesc(layout);
2935 // Create a PLT entry for a global symbol.
2939 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2942 if (gsym->has_plt_offset())
2945 if (this->plt_ == NULL)
2946 this->make_plt_section(symtab, layout);
2948 this->plt_->add_entry(symtab, layout, gsym);
2951 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2955 Target_x86_64<size>::make_local_ifunc_plt_entry(
2956 Symbol_table* symtab, Layout* layout,
2957 Sized_relobj_file<size, false>* relobj,
2958 unsigned int local_sym_index)
2960 if (relobj->local_has_plt_offset(local_sym_index))
2962 if (this->plt_ == NULL)
2963 this->make_plt_section(symtab, layout);
2964 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2967 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2970 // Return the number of entries in the PLT.
2974 Target_x86_64<size>::plt_entry_count() const
2976 if (this->plt_ == NULL)
2978 return this->plt_->entry_count();
2981 // Return the offset of the first non-reserved PLT entry.
2985 Target_x86_64<size>::first_plt_entry_offset() const
2987 if (this->plt_ == NULL)
2989 return this->plt_->first_plt_entry_offset();
2992 // Return the size of each PLT entry.
2996 Target_x86_64<size>::plt_entry_size() const
2998 if (this->plt_ == NULL)
3000 return this->plt_->get_plt_entry_size();
3003 // Create the GOT and PLT sections for an incremental update.
3006 Output_data_got_base*
3007 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
3009 unsigned int got_count,
3010 unsigned int plt_count)
3012 gold_assert(this->got_ == NULL);
3014 this->got_ = new Output_data_got<64, false>(got_count * 8);
3015 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3017 | elfcpp::SHF_WRITE),
3018 this->got_, ORDER_RELRO_LAST,
3021 // Add the three reserved entries.
3022 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
3023 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3025 | elfcpp::SHF_WRITE),
3026 this->got_plt_, ORDER_NON_RELRO_FIRST,
3029 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3030 this->global_offset_table_ =
3031 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3032 Symbol_table::PREDEFINED,
3034 0, 0, elfcpp::STT_OBJECT,
3036 elfcpp::STV_HIDDEN, 0,
3039 // If there are any TLSDESC relocations, they get GOT entries in
3040 // .got.plt after the jump slot entries.
3041 // FIXME: Get the count for TLSDESC entries.
3042 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
3043 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3044 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3046 ORDER_NON_RELRO_FIRST, false);
3048 // If there are any IRELATIVE relocations, they get GOT entries in
3049 // .got.plt after the jump slot and TLSDESC entries.
3050 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
3051 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3052 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3053 this->got_irelative_,
3054 ORDER_NON_RELRO_FIRST, false);
3056 // Create the PLT section.
3057 this->plt_ = this->make_data_plt(layout, this->got_,
3059 this->got_irelative_,
3062 // Add unwind information if requested.
3063 if (parameters->options().ld_generated_unwind_info())
3064 this->plt_->add_eh_frame(layout);
3066 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3067 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
3068 this->plt_, ORDER_PLT, false);
3070 // Make the sh_info field of .rela.plt point to .plt.
3071 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
3072 rela_plt_os->set_info_section(this->plt_->output_section());
3074 // Create the rela_dyn section.
3075 this->rela_dyn_section(layout);
3080 // Reserve a GOT entry for a local symbol, and regenerate any
3081 // necessary dynamic relocations.
3085 Target_x86_64<size>::reserve_local_got_entry(
3086 unsigned int got_index,
3087 Sized_relobj<size, false>* obj,
3089 unsigned int got_type)
3091 unsigned int got_offset = got_index * 8;
3092 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
3094 this->got_->reserve_local(got_index, obj, r_sym, got_type);
3097 case GOT_TYPE_STANDARD:
3098 if (parameters->options().output_is_position_independent())
3099 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
3100 this->got_, got_offset, 0, false);
3102 case GOT_TYPE_TLS_OFFSET:
3103 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
3104 this->got_, got_offset, 0);
3106 case GOT_TYPE_TLS_PAIR:
3107 this->got_->reserve_slot(got_index + 1);
3108 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
3109 this->got_, got_offset, 0);
3111 case GOT_TYPE_TLS_DESC:
3112 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
3113 // this->got_->reserve_slot(got_index + 1);
3114 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3115 // this->got_, got_offset, 0);
3122 // Reserve a GOT entry for a global symbol, and regenerate any
3123 // necessary dynamic relocations.
3127 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
3129 unsigned int got_type)
3131 unsigned int got_offset = got_index * 8;
3132 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
3134 this->got_->reserve_global(got_index, gsym, got_type);
3137 case GOT_TYPE_STANDARD:
3138 if (!gsym->final_value_is_known())
3140 if (gsym->is_from_dynobj()
3141 || gsym->is_undefined()
3142 || gsym->is_preemptible()
3143 || gsym->type() == elfcpp::STT_GNU_IFUNC)
3144 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
3145 this->got_, got_offset, 0);
3147 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3148 this->got_, got_offset, 0, false);
3151 case GOT_TYPE_TLS_OFFSET:
3152 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
3153 this->got_, got_offset, 0, false);
3155 case GOT_TYPE_TLS_PAIR:
3156 this->got_->reserve_slot(got_index + 1);
3157 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
3158 this->got_, got_offset, 0, false);
3159 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
3160 this->got_, got_offset + 8, 0, false);
3162 case GOT_TYPE_TLS_DESC:
3163 this->got_->reserve_slot(got_index + 1);
3164 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
3165 this->got_, got_offset, 0, false);
3172 // Register an existing PLT entry for a global symbol.
3176 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
3178 unsigned int plt_index,
3181 gold_assert(this->plt_ != NULL);
3182 gold_assert(!gsym->has_plt_offset());
3184 this->plt_->reserve_slot(plt_index);
3186 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
3188 unsigned int got_offset = (plt_index + 3) * 8;
3189 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
3192 // Force a COPY relocation for a given symbol.
3196 Target_x86_64<size>::emit_copy_reloc(
3197 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
3199 this->copy_relocs_.emit_copy_reloc(symtab,
3200 symtab->get_sized_symbol<size>(sym),
3203 this->rela_dyn_section(NULL));
3206 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3210 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
3213 if (this->tls_base_symbol_defined_)
3216 Output_segment* tls_segment = layout->tls_segment();
3217 if (tls_segment != NULL)
3219 bool is_exec = parameters->options().output_is_executable();
3220 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
3221 Symbol_table::PREDEFINED,
3225 elfcpp::STV_HIDDEN, 0,
3227 ? Symbol::SEGMENT_END
3228 : Symbol::SEGMENT_START),
3231 this->tls_base_symbol_defined_ = true;
3234 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3238 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
3241 if (this->plt_ == NULL)
3242 this->make_plt_section(symtab, layout);
3244 if (!this->plt_->has_tlsdesc_entry())
3246 // Allocate the TLSDESC_GOT entry.
3247 Output_data_got<64, false>* got = this->got_section(symtab, layout);
3248 unsigned int got_offset = got->add_constant(0);
3250 // Allocate the TLSDESC_PLT entry.
3251 this->plt_->reserve_tlsdesc_entry(got_offset);
3255 // Create a GOT entry for the TLS module index.
3259 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3260 Sized_relobj_file<size, false>* object)
3262 if (this->got_mod_index_offset_ == -1U)
3264 gold_assert(symtab != NULL && layout != NULL && object != NULL);
3265 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
3266 Output_data_got<64, false>* got = this->got_section(symtab, layout);
3267 unsigned int got_offset = got->add_constant(0);
3268 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
3270 got->add_constant(0);
3271 this->got_mod_index_offset_ = got_offset;
3273 return this->got_mod_index_offset_;
3276 // Optimize the TLS relocation type based on what we know about the
3277 // symbol. IS_FINAL is true if the final address of this symbol is
3278 // known at link time.
3281 tls::Tls_optimization
3282 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
3284 // If we are generating a shared library, then we can't do anything
3286 if (parameters->options().shared())
3287 return tls::TLSOPT_NONE;
3291 case elfcpp::R_X86_64_TLSGD:
3292 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3293 case elfcpp::R_X86_64_TLSDESC_CALL:
3294 // These are General-Dynamic which permits fully general TLS
3295 // access. Since we know that we are generating an executable,
3296 // we can convert this to Initial-Exec. If we also know that
3297 // this is a local symbol, we can further switch to Local-Exec.
3299 return tls::TLSOPT_TO_LE;
3300 return tls::TLSOPT_TO_IE;
3302 case elfcpp::R_X86_64_TLSLD:
3303 // This is Local-Dynamic, which refers to a local symbol in the
3304 // dynamic TLS block. Since we know that we generating an
3305 // executable, we can switch to Local-Exec.
3306 return tls::TLSOPT_TO_LE;
3308 case elfcpp::R_X86_64_DTPOFF32:
3309 case elfcpp::R_X86_64_DTPOFF64:
3310 // Another Local-Dynamic reloc.
3311 return tls::TLSOPT_TO_LE;
3313 case elfcpp::R_X86_64_GOTTPOFF:
3314 // These are Initial-Exec relocs which get the thread offset
3315 // from the GOT. If we know that we are linking against the
3316 // local symbol, we can switch to Local-Exec, which links the
3317 // thread offset into the instruction.
3319 return tls::TLSOPT_TO_LE;
3320 return tls::TLSOPT_NONE;
3322 case elfcpp::R_X86_64_TPOFF32:
3323 // When we already have Local-Exec, there is nothing further we
3325 return tls::TLSOPT_NONE;
3332 // Get the Reference_flags for a particular relocation.
3336 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
3340 case elfcpp::R_X86_64_NONE:
3341 case elfcpp::R_X86_64_GNU_VTINHERIT:
3342 case elfcpp::R_X86_64_GNU_VTENTRY:
3343 case elfcpp::R_X86_64_GOTPC32:
3344 case elfcpp::R_X86_64_GOTPC64:
3345 // No symbol reference.
3348 case elfcpp::R_X86_64_64:
3349 case elfcpp::R_X86_64_32:
3350 case elfcpp::R_X86_64_32S:
3351 case elfcpp::R_X86_64_16:
3352 case elfcpp::R_X86_64_8:
3353 return Symbol::ABSOLUTE_REF;
3355 case elfcpp::R_X86_64_PC64:
3356 case elfcpp::R_X86_64_PC32:
3357 case elfcpp::R_X86_64_PC32_BND:
3358 case elfcpp::R_X86_64_PC16:
3359 case elfcpp::R_X86_64_PC8:
3360 case elfcpp::R_X86_64_GOTOFF64:
3361 return Symbol::RELATIVE_REF;
3363 case elfcpp::R_X86_64_PLT32:
3364 case elfcpp::R_X86_64_PLT32_BND:
3365 case elfcpp::R_X86_64_PLTOFF64:
3366 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
3368 case elfcpp::R_X86_64_GOT64:
3369 case elfcpp::R_X86_64_GOT32:
3370 case elfcpp::R_X86_64_GOTPCREL64:
3371 case elfcpp::R_X86_64_GOTPCREL:
3372 case elfcpp::R_X86_64_GOTPCRELX:
3373 case elfcpp::R_X86_64_REX_GOTPCRELX:
3374 case elfcpp::R_X86_64_GOTPLT64:
3376 return Symbol::ABSOLUTE_REF;
3378 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3379 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3380 case elfcpp::R_X86_64_TLSDESC_CALL:
3381 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3382 case elfcpp::R_X86_64_DTPOFF32:
3383 case elfcpp::R_X86_64_DTPOFF64:
3384 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3385 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3386 return Symbol::TLS_REF;
3388 case elfcpp::R_X86_64_COPY:
3389 case elfcpp::R_X86_64_GLOB_DAT:
3390 case elfcpp::R_X86_64_JUMP_SLOT:
3391 case elfcpp::R_X86_64_RELATIVE:
3392 case elfcpp::R_X86_64_IRELATIVE:
3393 case elfcpp::R_X86_64_TPOFF64:
3394 case elfcpp::R_X86_64_DTPMOD64:
3395 case elfcpp::R_X86_64_TLSDESC:
3396 case elfcpp::R_X86_64_SIZE32:
3397 case elfcpp::R_X86_64_SIZE64:
3399 // Not expected. We will give an error later.
3404 // Report an unsupported relocation against a local symbol.
3408 Target_x86_64<size>::Scan::unsupported_reloc_local(
3409 Sized_relobj_file<size, false>* object,
3410 unsigned int r_type)
3412 gold_error(_("%s: unsupported reloc %u against local symbol"),
3413 object->name().c_str(), r_type);
3416 // We are about to emit a dynamic relocation of type R_TYPE. If the
3417 // dynamic linker does not support it, issue an error. The GNU linker
3418 // only issues a non-PIC error for an allocated read-only section.
3419 // Here we know the section is allocated, but we don't know that it is
3420 // read-only. But we check for all the relocation types which the
3421 // glibc dynamic linker supports, so it seems appropriate to issue an
3422 // error even if the section is not read-only. If GSYM is not NULL,
3423 // it is the symbol the relocation is against; if it is NULL, the
3424 // relocation is against a local symbol.
3428 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
3433 // These are the relocation types supported by glibc for x86_64
3434 // which should always work.
3435 case elfcpp::R_X86_64_RELATIVE:
3436 case elfcpp::R_X86_64_IRELATIVE:
3437 case elfcpp::R_X86_64_GLOB_DAT:
3438 case elfcpp::R_X86_64_JUMP_SLOT:
3439 case elfcpp::R_X86_64_DTPMOD64:
3440 case elfcpp::R_X86_64_DTPOFF64:
3441 case elfcpp::R_X86_64_TPOFF64:
3442 case elfcpp::R_X86_64_64:
3443 case elfcpp::R_X86_64_COPY:
3446 // glibc supports these reloc types, but they can overflow.
3447 case elfcpp::R_X86_64_PC32:
3448 case elfcpp::R_X86_64_PC32_BND:
3449 // A PC relative reference is OK against a local symbol or if
3450 // the symbol is defined locally.
3452 || (!gsym->is_from_dynobj()
3453 && !gsym->is_undefined()
3454 && !gsym->is_preemptible()))
3457 case elfcpp::R_X86_64_32:
3458 // R_X86_64_32 is OK for x32.
3459 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3461 if (this->issued_non_pic_error_)
3463 gold_assert(parameters->options().output_is_position_independent());
3465 object->error(_("requires dynamic R_X86_64_32 reloc which may "
3466 "overflow at runtime; recompile with -fPIC"));
3472 case elfcpp::R_X86_64_32:
3473 r_name = "R_X86_64_32";
3475 case elfcpp::R_X86_64_PC32:
3476 r_name = "R_X86_64_PC32";
3478 case elfcpp::R_X86_64_PC32_BND:
3479 r_name = "R_X86_64_PC32_BND";
3485 object->error(_("requires dynamic %s reloc against '%s' "
3486 "which may overflow at runtime; recompile "
3488 r_name, gsym->name());
3490 this->issued_non_pic_error_ = true;
3494 // This prevents us from issuing more than one error per reloc
3495 // section. But we can still wind up issuing more than one
3496 // error per object file.
3497 if (this->issued_non_pic_error_)
3499 gold_assert(parameters->options().output_is_position_independent());
3500 object->error(_("requires unsupported dynamic reloc %u; "
3501 "recompile with -fPIC"),
3503 this->issued_non_pic_error_ = true;
3506 case elfcpp::R_X86_64_NONE:
3511 // Return whether we need to make a PLT entry for a relocation of the
3512 // given type against a STT_GNU_IFUNC symbol.
3516 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
3517 Sized_relobj_file<size, false>* object,
3518 unsigned int r_type)
3520 int flags = Scan::get_reference_flags(r_type);
3521 if (flags & Symbol::TLS_REF)
3522 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3523 object->name().c_str(), r_type);
3527 // Scan a relocation for a local symbol.
3531 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
3533 Target_x86_64<size>* target,
3534 Sized_relobj_file<size, false>* object,
3535 unsigned int data_shndx,
3536 Output_section* output_section,
3537 const elfcpp::Rela<size, false>& reloc,
3538 unsigned int r_type,
3539 const elfcpp::Sym<size, false>& lsym,
3545 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3546 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3547 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3549 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3550 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3555 case elfcpp::R_X86_64_NONE:
3556 case elfcpp::R_X86_64_GNU_VTINHERIT:
3557 case elfcpp::R_X86_64_GNU_VTENTRY:
3560 case elfcpp::R_X86_64_64:
3561 // If building a shared library (or a position-independent
3562 // executable), we need to create a dynamic relocation for this
3563 // location. The relocation applied at link time will apply the
3564 // link-time value, so we flag the location with an
3565 // R_X86_64_RELATIVE relocation so the dynamic loader can
3566 // relocate it easily.
3567 if (parameters->options().output_is_position_independent())
3569 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3570 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3571 rela_dyn->add_local_relative(object, r_sym,
3573 ? elfcpp::R_X86_64_RELATIVE64
3574 : elfcpp::R_X86_64_RELATIVE),
3575 output_section, data_shndx,
3576 reloc.get_r_offset(),
3577 reloc.get_r_addend(), is_ifunc);
3581 case elfcpp::R_X86_64_32:
3582 case elfcpp::R_X86_64_32S:
3583 case elfcpp::R_X86_64_16:
3584 case elfcpp::R_X86_64_8:
3585 // If building a shared library (or a position-independent
3586 // executable), we need to create a dynamic relocation for this
3587 // location. We can't use an R_X86_64_RELATIVE relocation
3588 // because that is always a 64-bit relocation.
3589 if (parameters->options().output_is_position_independent())
3591 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3592 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3594 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3595 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3596 rela_dyn->add_local_relative(object, r_sym,
3597 elfcpp::R_X86_64_RELATIVE,
3598 output_section, data_shndx,
3599 reloc.get_r_offset(),
3600 reloc.get_r_addend(), is_ifunc);
3604 this->check_non_pic(object, r_type, NULL);
3606 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3607 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3608 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3609 rela_dyn->add_local(object, r_sym, r_type, output_section,
3610 data_shndx, reloc.get_r_offset(),
3611 reloc.get_r_addend());
3614 gold_assert(lsym.get_st_value() == 0);
3615 unsigned int shndx = lsym.get_st_shndx();
3617 shndx = object->adjust_sym_shndx(r_sym, shndx,
3620 object->error(_("section symbol %u has bad shndx %u"),
3623 rela_dyn->add_local_section(object, shndx,
3624 r_type, output_section,
3625 data_shndx, reloc.get_r_offset(),
3626 reloc.get_r_addend());
3631 case elfcpp::R_X86_64_PC64:
3632 case elfcpp::R_X86_64_PC32:
3633 case elfcpp::R_X86_64_PC32_BND:
3634 case elfcpp::R_X86_64_PC16:
3635 case elfcpp::R_X86_64_PC8:
3638 case elfcpp::R_X86_64_PLT32:
3639 case elfcpp::R_X86_64_PLT32_BND:
3640 // Since we know this is a local symbol, we can handle this as a
3644 case elfcpp::R_X86_64_GOTPC32:
3645 case elfcpp::R_X86_64_GOTOFF64:
3646 case elfcpp::R_X86_64_GOTPC64:
3647 case elfcpp::R_X86_64_PLTOFF64:
3648 // We need a GOT section.
3649 target->got_section(symtab, layout);
3650 // For PLTOFF64, we'd normally want a PLT section, but since we
3651 // know this is a local symbol, no PLT is needed.
3654 case elfcpp::R_X86_64_GOT64:
3655 case elfcpp::R_X86_64_GOT32:
3656 case elfcpp::R_X86_64_GOTPCREL64:
3657 case elfcpp::R_X86_64_GOTPCREL:
3658 case elfcpp::R_X86_64_GOTPCRELX:
3659 case elfcpp::R_X86_64_REX_GOTPCRELX:
3660 case elfcpp::R_X86_64_GOTPLT64:
3662 // The symbol requires a GOT section.
3663 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3665 // If the relocation symbol isn't IFUNC,
3666 // and is local, then we will convert
3667 // mov foo@GOTPCREL(%rip), %reg
3668 // to lea foo(%rip), %reg.
3669 // in Relocate::relocate.
3670 if (!parameters->incremental()
3671 && (r_type == elfcpp::R_X86_64_GOTPCREL
3672 || r_type == elfcpp::R_X86_64_GOTPCRELX
3673 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3674 && reloc.get_r_offset() >= 2
3677 section_size_type stype;
3678 const unsigned char* view = object->section_contents(data_shndx,
3680 if (view[reloc.get_r_offset() - 2] == 0x8b)
3684 // The symbol requires a GOT entry.
3685 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3687 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3688 // lets function pointers compare correctly with shared
3689 // libraries. Otherwise we would need an IRELATIVE reloc.
3692 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3694 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3697 // If we are generating a shared object, we need to add a
3698 // dynamic relocation for this symbol's GOT entry.
3699 if (parameters->options().output_is_position_independent())
3701 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3702 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3703 if (r_type != elfcpp::R_X86_64_GOT32)
3705 unsigned int got_offset =
3706 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3707 rela_dyn->add_local_relative(object, r_sym,
3708 elfcpp::R_X86_64_RELATIVE,
3709 got, got_offset, 0, is_ifunc);
3713 this->check_non_pic(object, r_type, NULL);
3715 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3716 rela_dyn->add_local(
3717 object, r_sym, r_type, got,
3718 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3722 // For GOTPLT64, we'd normally want a PLT section, but since
3723 // we know this is a local symbol, no PLT is needed.
3727 case elfcpp::R_X86_64_COPY:
3728 case elfcpp::R_X86_64_GLOB_DAT:
3729 case elfcpp::R_X86_64_JUMP_SLOT:
3730 case elfcpp::R_X86_64_RELATIVE:
3731 case elfcpp::R_X86_64_IRELATIVE:
3732 // These are outstanding tls relocs, which are unexpected when linking
3733 case elfcpp::R_X86_64_TPOFF64:
3734 case elfcpp::R_X86_64_DTPMOD64:
3735 case elfcpp::R_X86_64_TLSDESC:
3736 gold_error(_("%s: unexpected reloc %u in object file"),
3737 object->name().c_str(), r_type);
3740 // These are initial tls relocs, which are expected when linking
3741 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3742 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3743 case elfcpp::R_X86_64_TLSDESC_CALL:
3744 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3745 case elfcpp::R_X86_64_DTPOFF32:
3746 case elfcpp::R_X86_64_DTPOFF64:
3747 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3748 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3750 bool output_is_shared = parameters->options().shared();
3751 const tls::Tls_optimization optimized_type
3752 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3756 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3757 if (optimized_type == tls::TLSOPT_NONE)
3759 // Create a pair of GOT entries for the module index and
3760 // dtv-relative offset.
3761 Output_data_got<64, false>* got
3762 = target->got_section(symtab, layout);
3763 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3764 unsigned int shndx = lsym.get_st_shndx();
3766 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3768 object->error(_("local symbol %u has bad shndx %u"),
3771 got->add_local_pair_with_rel(object, r_sym,
3774 target->rela_dyn_section(layout),
3775 elfcpp::R_X86_64_DTPMOD64);
3777 else if (optimized_type != tls::TLSOPT_TO_LE)
3778 unsupported_reloc_local(object, r_type);
3781 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3782 target->define_tls_base_symbol(symtab, layout);
3783 if (optimized_type == tls::TLSOPT_NONE)
3785 // Create reserved PLT and GOT entries for the resolver.
3786 target->reserve_tlsdesc_entries(symtab, layout);
3788 // Generate a double GOT entry with an
3789 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3790 // is resolved lazily, so the GOT entry needs to be in
3791 // an area in .got.plt, not .got. Call got_section to
3792 // make sure the section has been created.
3793 target->got_section(symtab, layout);
3794 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3795 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3796 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3798 unsigned int got_offset = got->add_constant(0);
3799 got->add_constant(0);
3800 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3802 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3803 // We store the arguments we need in a vector, and
3804 // use the index into the vector as the parameter
3805 // to pass to the target specific routines.
3806 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3807 void* arg = reinterpret_cast<void*>(intarg);
3808 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3809 got, got_offset, 0);
3812 else if (optimized_type != tls::TLSOPT_TO_LE)
3813 unsupported_reloc_local(object, r_type);
3816 case elfcpp::R_X86_64_TLSDESC_CALL:
3819 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3820 if (optimized_type == tls::TLSOPT_NONE)
3822 // Create a GOT entry for the module index.
3823 target->got_mod_index_entry(symtab, layout, object);
3825 else if (optimized_type != tls::TLSOPT_TO_LE)
3826 unsupported_reloc_local(object, r_type);
3829 case elfcpp::R_X86_64_DTPOFF32:
3830 case elfcpp::R_X86_64_DTPOFF64:
3833 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3834 layout->set_has_static_tls();
3835 if (optimized_type == tls::TLSOPT_NONE)
3837 // Create a GOT entry for the tp-relative offset.
3838 Output_data_got<64, false>* got
3839 = target->got_section(symtab, layout);
3840 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3841 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3842 target->rela_dyn_section(layout),
3843 elfcpp::R_X86_64_TPOFF64);
3845 else if (optimized_type != tls::TLSOPT_TO_LE)
3846 unsupported_reloc_local(object, r_type);
3849 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3850 layout->set_has_static_tls();
3851 if (output_is_shared)
3852 unsupported_reloc_local(object, r_type);
3861 case elfcpp::R_X86_64_SIZE32:
3862 case elfcpp::R_X86_64_SIZE64:
3864 gold_error(_("%s: unsupported reloc %u against local symbol"),
3865 object->name().c_str(), r_type);
3871 // Report an unsupported relocation against a global symbol.
3875 Target_x86_64<size>::Scan::unsupported_reloc_global(
3876 Sized_relobj_file<size, false>* object,
3877 unsigned int r_type,
3880 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3881 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3884 // Returns true if this relocation type could be that of a function pointer.
3887 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3888 Sized_relobj_file<size, false>* src_obj,
3889 unsigned int src_indx,
3890 unsigned int r_offset,
3891 unsigned int r_type)
3895 case elfcpp::R_X86_64_64:
3896 case elfcpp::R_X86_64_32:
3897 case elfcpp::R_X86_64_32S:
3898 case elfcpp::R_X86_64_16:
3899 case elfcpp::R_X86_64_8:
3900 case elfcpp::R_X86_64_GOT64:
3901 case elfcpp::R_X86_64_GOT32:
3902 case elfcpp::R_X86_64_GOTPCREL64:
3903 case elfcpp::R_X86_64_GOTPCREL:
3904 case elfcpp::R_X86_64_GOTPCRELX:
3905 case elfcpp::R_X86_64_REX_GOTPCRELX:
3906 case elfcpp::R_X86_64_GOTPLT64:
3910 case elfcpp::R_X86_64_PC32:
3912 // This relocation may be used both for function calls and
3913 // for taking address of a function. We distinguish between
3914 // them by checking the opcodes.
3915 uint64_t sh_flags = src_obj->section_flags(src_indx);
3916 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3919 section_size_type stype;
3920 const unsigned char* view = src_obj->section_contents(src_indx,
3926 && view[r_offset - 1] == 0xe8)
3931 && view[r_offset - 1] == 0xe9)
3934 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3936 && view[r_offset - 2] == 0x0f
3937 && view[r_offset - 1] >= 0x80
3938 && view[r_offset - 1] <= 0x8f)
3942 // Be conservative and treat all others as function pointers.
3949 // For safe ICF, scan a relocation for a local symbol to check if it
3950 // corresponds to a function pointer being taken. In that case mark
3951 // the function whose pointer was taken as not foldable.
3955 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3958 Target_x86_64<size>* ,
3959 Sized_relobj_file<size, false>* src_obj,
3960 unsigned int src_indx,
3962 const elfcpp::Rela<size, false>& reloc,
3963 unsigned int r_type,
3964 const elfcpp::Sym<size, false>&)
3966 // When building a shared library, do not fold any local symbols as it is
3967 // not possible to distinguish pointer taken versus a call by looking at
3968 // the relocation types.
3969 if (parameters->options().shared())
3972 return possible_function_pointer_reloc(src_obj, src_indx,
3973 reloc.get_r_offset(), r_type);
3976 // For safe ICF, scan a relocation for a global symbol to check if it
3977 // corresponds to a function pointer being taken. In that case mark
3978 // the function whose pointer was taken as not foldable.
3982 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3985 Target_x86_64<size>* ,
3986 Sized_relobj_file<size, false>* src_obj,
3987 unsigned int src_indx,
3989 const elfcpp::Rela<size, false>& reloc,
3990 unsigned int r_type,
3993 // When building a shared library, do not fold symbols whose visibility
3994 // is hidden, internal or protected.
3995 if (parameters->options().shared()
3996 && (gsym->visibility() == elfcpp::STV_INTERNAL
3997 || gsym->visibility() == elfcpp::STV_PROTECTED
3998 || gsym->visibility() == elfcpp::STV_HIDDEN))
4001 return possible_function_pointer_reloc(src_obj, src_indx,
4002 reloc.get_r_offset(), r_type);
4005 // Scan a relocation for a global symbol.
4009 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
4011 Target_x86_64<size>* target,
4012 Sized_relobj_file<size, false>* object,
4013 unsigned int data_shndx,
4014 Output_section* output_section,
4015 const elfcpp::Rela<size, false>& reloc,
4016 unsigned int r_type,
4019 // A STT_GNU_IFUNC symbol may require a PLT entry.
4020 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4021 && this->reloc_needs_plt_for_ifunc(object, r_type))
4022 target->make_plt_entry(symtab, layout, gsym);
4026 case elfcpp::R_X86_64_NONE:
4027 case elfcpp::R_X86_64_GNU_VTINHERIT:
4028 case elfcpp::R_X86_64_GNU_VTENTRY:
4031 case elfcpp::R_X86_64_64:
4032 case elfcpp::R_X86_64_32:
4033 case elfcpp::R_X86_64_32S:
4034 case elfcpp::R_X86_64_16:
4035 case elfcpp::R_X86_64_8:
4037 // Make a PLT entry if necessary.
4038 if (gsym->needs_plt_entry())
4040 target->make_plt_entry(symtab, layout, gsym);
4041 // Since this is not a PC-relative relocation, we may be
4042 // taking the address of a function. In that case we need to
4043 // set the entry in the dynamic symbol table to the address of
4045 if (gsym->is_from_dynobj() && !parameters->options().shared())
4046 gsym->set_needs_dynsym_value();
4048 // Make a dynamic relocation if necessary.
4049 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4051 if (!parameters->options().output_is_position_independent()
4052 && gsym->may_need_copy_reloc())
4054 target->copy_reloc(symtab, layout, object,
4055 data_shndx, output_section, gsym, reloc);
4057 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
4058 || (size == 32 && r_type == elfcpp::R_X86_64_32))
4059 && gsym->type() == elfcpp::STT_GNU_IFUNC
4060 && gsym->can_use_relative_reloc(false)
4061 && !gsym->is_from_dynobj()
4062 && !gsym->is_undefined()
4063 && !gsym->is_preemptible())
4065 // Use an IRELATIVE reloc for a locally defined
4066 // STT_GNU_IFUNC symbol. This makes a function
4067 // address in a PIE executable match the address in a
4068 // shared library that it links against.
4069 Reloc_section* rela_dyn =
4070 target->rela_irelative_section(layout);
4071 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
4072 rela_dyn->add_symbolless_global_addend(gsym, r_type,
4073 output_section, object,
4075 reloc.get_r_offset(),
4076 reloc.get_r_addend());
4078 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
4079 || (size == 32 && r_type == elfcpp::R_X86_64_32))
4080 && gsym->can_use_relative_reloc(false))
4082 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4083 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
4084 output_section, object,
4086 reloc.get_r_offset(),
4087 reloc.get_r_addend(), false);
4091 this->check_non_pic(object, r_type, gsym);
4092 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4093 rela_dyn->add_global(gsym, r_type, output_section, object,
4094 data_shndx, reloc.get_r_offset(),
4095 reloc.get_r_addend());
4101 case elfcpp::R_X86_64_PC64:
4102 case elfcpp::R_X86_64_PC32:
4103 case elfcpp::R_X86_64_PC32_BND:
4104 case elfcpp::R_X86_64_PC16:
4105 case elfcpp::R_X86_64_PC8:
4107 // Make a PLT entry if necessary.
4108 if (gsym->needs_plt_entry())
4109 target->make_plt_entry(symtab, layout, gsym);
4110 // Make a dynamic relocation if necessary.
4111 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4113 if (parameters->options().output_is_executable()
4114 && gsym->may_need_copy_reloc())
4116 target->copy_reloc(symtab, layout, object,
4117 data_shndx, output_section, gsym, reloc);
4121 this->check_non_pic(object, r_type, gsym);
4122 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4123 rela_dyn->add_global(gsym, r_type, output_section, object,
4124 data_shndx, reloc.get_r_offset(),
4125 reloc.get_r_addend());
4131 case elfcpp::R_X86_64_GOT64:
4132 case elfcpp::R_X86_64_GOT32:
4133 case elfcpp::R_X86_64_GOTPCREL64:
4134 case elfcpp::R_X86_64_GOTPCREL:
4135 case elfcpp::R_X86_64_GOTPCRELX:
4136 case elfcpp::R_X86_64_REX_GOTPCRELX:
4137 case elfcpp::R_X86_64_GOTPLT64:
4139 // The symbol requires a GOT entry.
4140 Output_data_got<64, false>* got = target->got_section(symtab, layout);
4142 // If we convert this from
4143 // mov foo@GOTPCREL(%rip), %reg
4144 // to lea foo(%rip), %reg.
4147 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
4149 // in Relocate::relocate, then there is nothing to do here.
4150 // We cannot make these optimizations in incremental linking mode,
4151 // because we look at the opcode to decide whether or not to make
4152 // change, and during an incremental update, the change may have
4153 // already been applied.
4155 Lazy_view<size> view(object, data_shndx);
4156 size_t r_offset = reloc.get_r_offset();
4157 if (!parameters->incremental()
4159 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4163 if (!parameters->incremental()
4165 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
4170 if (gsym->final_value_is_known())
4172 // For a STT_GNU_IFUNC symbol we want the PLT address.
4173 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4174 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4176 got->add_global(gsym, GOT_TYPE_STANDARD);
4180 // If this symbol is not fully resolved, we need to add a
4181 // dynamic relocation for it.
4182 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4184 // Use a GLOB_DAT rather than a RELATIVE reloc if:
4186 // 1) The symbol may be defined in some other module.
4188 // 2) We are building a shared library and this is a
4189 // protected symbol; using GLOB_DAT means that the dynamic
4190 // linker can use the address of the PLT in the main
4191 // executable when appropriate so that function address
4192 // comparisons work.
4194 // 3) This is a STT_GNU_IFUNC symbol in position dependent
4195 // code, again so that function address comparisons work.
4196 if (gsym->is_from_dynobj()
4197 || gsym->is_undefined()
4198 || gsym->is_preemptible()
4199 || (gsym->visibility() == elfcpp::STV_PROTECTED
4200 && parameters->options().shared())
4201 || (gsym->type() == elfcpp::STT_GNU_IFUNC
4202 && parameters->options().output_is_position_independent()))
4203 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
4204 elfcpp::R_X86_64_GLOB_DAT);
4207 // For a STT_GNU_IFUNC symbol we want to write the PLT
4208 // offset into the GOT, so that function pointer
4209 // comparisons work correctly.
4211 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
4212 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
4215 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4216 // Tell the dynamic linker to use the PLT address
4217 // when resolving relocations.
4218 if (gsym->is_from_dynobj()
4219 && !parameters->options().shared())
4220 gsym->set_needs_dynsym_value();
4224 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
4225 rela_dyn->add_global_relative(gsym,
4226 elfcpp::R_X86_64_RELATIVE,
4227 got, got_off, 0, false);
4234 case elfcpp::R_X86_64_PLT32:
4235 case elfcpp::R_X86_64_PLT32_BND:
4236 // If the symbol is fully resolved, this is just a PC32 reloc.
4237 // Otherwise we need a PLT entry.
4238 if (gsym->final_value_is_known())
4240 // If building a shared library, we can also skip the PLT entry
4241 // if the symbol is defined in the output file and is protected
4243 if (gsym->is_defined()
4244 && !gsym->is_from_dynobj()
4245 && !gsym->is_preemptible())
4247 target->make_plt_entry(symtab, layout, gsym);
4250 case elfcpp::R_X86_64_GOTPC32:
4251 case elfcpp::R_X86_64_GOTOFF64:
4252 case elfcpp::R_X86_64_GOTPC64:
4253 case elfcpp::R_X86_64_PLTOFF64:
4254 // We need a GOT section.
4255 target->got_section(symtab, layout);
4256 // For PLTOFF64, we also need a PLT entry (but only if the
4257 // symbol is not fully resolved).
4258 if (r_type == elfcpp::R_X86_64_PLTOFF64
4259 && !gsym->final_value_is_known())
4260 target->make_plt_entry(symtab, layout, gsym);
4263 case elfcpp::R_X86_64_COPY:
4264 case elfcpp::R_X86_64_GLOB_DAT:
4265 case elfcpp::R_X86_64_JUMP_SLOT:
4266 case elfcpp::R_X86_64_RELATIVE:
4267 case elfcpp::R_X86_64_IRELATIVE:
4268 // These are outstanding tls relocs, which are unexpected when linking
4269 case elfcpp::R_X86_64_TPOFF64:
4270 case elfcpp::R_X86_64_DTPMOD64:
4271 case elfcpp::R_X86_64_TLSDESC:
4272 gold_error(_("%s: unexpected reloc %u in object file"),
4273 object->name().c_str(), r_type);
4276 // These are initial tls relocs, which are expected for global()
4277 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4278 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4279 case elfcpp::R_X86_64_TLSDESC_CALL:
4280 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4281 case elfcpp::R_X86_64_DTPOFF32:
4282 case elfcpp::R_X86_64_DTPOFF64:
4283 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4284 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4286 // For the Initial-Exec model, we can treat undef symbols as final
4287 // when building an executable.
4288 const bool is_final = (gsym->final_value_is_known() ||
4289 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
4290 gsym->is_undefined() &&
4291 parameters->options().output_is_executable()));
4292 const tls::Tls_optimization optimized_type
4293 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4296 case elfcpp::R_X86_64_TLSGD: // General-dynamic
4297 if (optimized_type == tls::TLSOPT_NONE)
4299 // Create a pair of GOT entries for the module index and
4300 // dtv-relative offset.
4301 Output_data_got<64, false>* got
4302 = target->got_section(symtab, layout);
4303 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
4304 target->rela_dyn_section(layout),
4305 elfcpp::R_X86_64_DTPMOD64,
4306 elfcpp::R_X86_64_DTPOFF64);
4308 else if (optimized_type == tls::TLSOPT_TO_IE)
4310 // Create a GOT entry for the tp-relative offset.
4311 Output_data_got<64, false>* got
4312 = target->got_section(symtab, layout);
4313 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4314 target->rela_dyn_section(layout),
4315 elfcpp::R_X86_64_TPOFF64);
4317 else if (optimized_type != tls::TLSOPT_TO_LE)
4318 unsupported_reloc_global(object, r_type, gsym);
4321 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
4322 target->define_tls_base_symbol(symtab, layout);
4323 if (optimized_type == tls::TLSOPT_NONE)
4325 // Create reserved PLT and GOT entries for the resolver.
4326 target->reserve_tlsdesc_entries(symtab, layout);
4328 // Create a double GOT entry with an R_X86_64_TLSDESC
4329 // reloc. The R_X86_64_TLSDESC reloc is resolved
4330 // lazily, so the GOT entry needs to be in an area in
4331 // .got.plt, not .got. Call got_section to make sure
4332 // the section has been created.
4333 target->got_section(symtab, layout);
4334 Output_data_got<64, false>* got = target->got_tlsdesc_section();
4335 Reloc_section* rt = target->rela_tlsdesc_section(layout);
4336 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
4337 elfcpp::R_X86_64_TLSDESC, 0);
4339 else if (optimized_type == tls::TLSOPT_TO_IE)
4341 // Create a GOT entry for the tp-relative offset.
4342 Output_data_got<64, false>* got
4343 = target->got_section(symtab, layout);
4344 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4345 target->rela_dyn_section(layout),
4346 elfcpp::R_X86_64_TPOFF64);
4348 else if (optimized_type != tls::TLSOPT_TO_LE)
4349 unsupported_reloc_global(object, r_type, gsym);
4352 case elfcpp::R_X86_64_TLSDESC_CALL:
4355 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4356 if (optimized_type == tls::TLSOPT_NONE)
4358 // Create a GOT entry for the module index.
4359 target->got_mod_index_entry(symtab, layout, object);
4361 else if (optimized_type != tls::TLSOPT_TO_LE)
4362 unsupported_reloc_global(object, r_type, gsym);
4365 case elfcpp::R_X86_64_DTPOFF32:
4366 case elfcpp::R_X86_64_DTPOFF64:
4369 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4370 layout->set_has_static_tls();
4371 if (optimized_type == tls::TLSOPT_NONE)
4373 // Create a GOT entry for the tp-relative offset.
4374 Output_data_got<64, false>* got
4375 = target->got_section(symtab, layout);
4376 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4377 target->rela_dyn_section(layout),
4378 elfcpp::R_X86_64_TPOFF64);
4380 else if (optimized_type != tls::TLSOPT_TO_LE)
4381 unsupported_reloc_global(object, r_type, gsym);
4384 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4385 layout->set_has_static_tls();
4386 if (parameters->options().shared())
4387 unsupported_reloc_global(object, r_type, gsym);
4396 case elfcpp::R_X86_64_SIZE32:
4397 case elfcpp::R_X86_64_SIZE64:
4399 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4400 object->name().c_str(), r_type,
4401 gsym->demangled_name().c_str());
4408 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
4410 Sized_relobj_file<size, false>* object,
4411 unsigned int data_shndx,
4412 unsigned int sh_type,
4413 const unsigned char* prelocs,
4415 Output_section* output_section,
4416 bool needs_special_offset_handling,
4417 size_t local_symbol_count,
4418 const unsigned char* plocal_symbols)
4420 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4423 if (sh_type == elfcpp::SHT_REL)
4428 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
4438 needs_special_offset_handling,
4443 // Scan relocations for a section.
4447 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
4449 Sized_relobj_file<size, false>* object,
4450 unsigned int data_shndx,
4451 unsigned int sh_type,
4452 const unsigned char* prelocs,
4454 Output_section* output_section,
4455 bool needs_special_offset_handling,
4456 size_t local_symbol_count,
4457 const unsigned char* plocal_symbols)
4459 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4462 if (sh_type == elfcpp::SHT_REL)
4464 gold_error(_("%s: unsupported REL reloc section"),
4465 object->name().c_str());
4469 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
4478 needs_special_offset_handling,
4483 // Finalize the sections.
4487 Target_x86_64<size>::do_finalize_sections(
4489 const Input_objects*,
4490 Symbol_table* symtab)
4492 const Reloc_section* rel_plt = (this->plt_ == NULL
4494 : this->plt_->rela_plt());
4495 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4496 this->rela_dyn_, true, false);
4498 // Fill in some more dynamic tags.
4499 Output_data_dynamic* const odyn = layout->dynamic_data();
4502 if (this->plt_ != NULL
4503 && this->plt_->output_section() != NULL
4504 && this->plt_->has_tlsdesc_entry())
4506 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
4507 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
4508 this->got_->finalize_data_size();
4509 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
4510 this->plt_, plt_offset);
4511 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
4512 this->got_, got_offset);
4516 // Emit any relocs we saved in an attempt to avoid generating COPY
4518 if (this->copy_relocs_.any_saved_relocs())
4519 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4521 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4522 // the .got.plt section.
4523 Symbol* sym = this->global_offset_table_;
4526 uint64_t data_size = this->got_plt_->current_data_size();
4527 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4530 if (parameters->doing_static_link()
4531 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4533 // If linking statically, make sure that the __rela_iplt symbols
4534 // were defined if necessary, even if we didn't create a PLT.
4535 static const Define_symbol_in_segment syms[] =
4538 "__rela_iplt_start", // name
4539 elfcpp::PT_LOAD, // segment_type
4540 elfcpp::PF_W, // segment_flags_set
4541 elfcpp::PF(0), // segment_flags_clear
4544 elfcpp::STT_NOTYPE, // type
4545 elfcpp::STB_GLOBAL, // binding
4546 elfcpp::STV_HIDDEN, // visibility
4548 Symbol::SEGMENT_START, // offset_from_base
4552 "__rela_iplt_end", // name
4553 elfcpp::PT_LOAD, // segment_type
4554 elfcpp::PF_W, // segment_flags_set
4555 elfcpp::PF(0), // segment_flags_clear
4558 elfcpp::STT_NOTYPE, // type
4559 elfcpp::STB_GLOBAL, // binding
4560 elfcpp::STV_HIDDEN, // visibility
4562 Symbol::SEGMENT_START, // offset_from_base
4567 symtab->define_symbols(layout, 2, syms,
4568 layout->script_options()->saw_sections_clause());
4572 // For x32, we need to handle PC-relative relocations using full 64-bit
4573 // arithmetic, so that we can detect relocation overflows properly.
4574 // This class overrides the pcrela32_check methods from the defaults in
4575 // Relocate_functions in reloc.h.
4578 class X86_64_relocate_functions : public Relocate_functions<size, false>
4581 typedef Relocate_functions<size, false> Base;
4583 // Do a simple PC relative relocation with the addend in the
4585 static inline typename Base::Reloc_status
4586 pcrela32_check(unsigned char* view,
4587 typename elfcpp::Elf_types<64>::Elf_Addr value,
4588 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4589 typename elfcpp::Elf_types<64>::Elf_Addr address)
4591 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4592 Valtype* wv = reinterpret_cast<Valtype*>(view);
4593 value = value + addend - address;
4594 elfcpp::Swap<32, false>::writeval(wv, value);
4595 return (Bits<32>::has_overflow(value)
4596 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4599 // Do a simple PC relative relocation with a Symbol_value with the
4600 // addend in the relocation.
4601 static inline typename Base::Reloc_status
4602 pcrela32_check(unsigned char* view,
4603 const Sized_relobj_file<size, false>* object,
4604 const Symbol_value<size>* psymval,
4605 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4606 typename elfcpp::Elf_types<64>::Elf_Addr address)
4608 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4609 Valtype* wv = reinterpret_cast<Valtype*>(view);
4610 typename elfcpp::Elf_types<64>::Elf_Addr value;
4612 value = psymval->value(object, addend);
4615 // For negative addends, get the symbol value without
4616 // the addend, then add the addend using 64-bit arithmetic.
4617 value = psymval->value(object, 0);
4621 elfcpp::Swap<32, false>::writeval(wv, value);
4622 return (Bits<32>::has_overflow(value)
4623 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4627 // Perform a relocation.
4631 Target_x86_64<size>::Relocate::relocate(
4632 const Relocate_info<size, false>* relinfo,
4634 Target_x86_64<size>* target,
4637 const unsigned char* preloc,
4638 const Sized_symbol<size>* gsym,
4639 const Symbol_value<size>* psymval,
4640 unsigned char* view,
4641 typename elfcpp::Elf_types<size>::Elf_Addr address,
4642 section_size_type view_size)
4644 typedef X86_64_relocate_functions<size> Reloc_funcs;
4645 const elfcpp::Rela<size, false> rela(preloc);
4646 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4648 if (this->skip_call_tls_get_addr_)
4650 if ((r_type != elfcpp::R_X86_64_PLT32
4651 && r_type != elfcpp::R_X86_64_GOTPCREL
4652 && r_type != elfcpp::R_X86_64_GOTPCRELX
4653 && r_type != elfcpp::R_X86_64_PLT32_BND
4654 && r_type != elfcpp::R_X86_64_PC32_BND
4655 && r_type != elfcpp::R_X86_64_PC32)
4657 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4659 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4660 _("missing expected TLS relocation"));
4661 this->skip_call_tls_get_addr_ = false;
4665 this->skip_call_tls_get_addr_ = false;
4673 const Sized_relobj_file<size, false>* object = relinfo->object;
4675 // Pick the value to use for symbols defined in the PLT.
4676 Symbol_value<size> symval;
4678 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4680 symval.set_output_value(target->plt_address_for_global(gsym));
4683 else if (gsym == NULL && psymval->is_ifunc_symbol())
4685 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4686 if (object->local_has_plt_offset(r_sym))
4688 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4693 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4695 // Get the GOT offset if needed.
4696 // The GOT pointer points to the end of the GOT section.
4697 // We need to subtract the size of the GOT section to get
4698 // the actual offset to use in the relocation.
4699 bool have_got_offset = false;
4700 // Since the actual offset is always negative, we use signed int to
4701 // support 64-bit GOT relocations.
4705 case elfcpp::R_X86_64_GOT32:
4706 case elfcpp::R_X86_64_GOT64:
4707 case elfcpp::R_X86_64_GOTPLT64:
4708 case elfcpp::R_X86_64_GOTPCREL64:
4711 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4712 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4716 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4717 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4718 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4719 - target->got_size());
4721 have_got_offset = true;
4728 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4732 case elfcpp::R_X86_64_NONE:
4733 case elfcpp::R_X86_64_GNU_VTINHERIT:
4734 case elfcpp::R_X86_64_GNU_VTENTRY:
4737 case elfcpp::R_X86_64_64:
4738 Reloc_funcs::rela64(view, object, psymval, addend);
4741 case elfcpp::R_X86_64_PC64:
4742 Reloc_funcs::pcrela64(view, object, psymval, addend,
4746 case elfcpp::R_X86_64_32:
4747 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4748 Reloc_funcs::CHECK_UNSIGNED);
4751 case elfcpp::R_X86_64_32S:
4752 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4753 Reloc_funcs::CHECK_SIGNED);
4756 case elfcpp::R_X86_64_PC32:
4757 case elfcpp::R_X86_64_PC32_BND:
4758 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4762 case elfcpp::R_X86_64_16:
4763 Reloc_funcs::rela16(view, object, psymval, addend);
4766 case elfcpp::R_X86_64_PC16:
4767 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4770 case elfcpp::R_X86_64_8:
4771 Reloc_funcs::rela8(view, object, psymval, addend);
4774 case elfcpp::R_X86_64_PC8:
4775 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4778 case elfcpp::R_X86_64_PLT32:
4779 case elfcpp::R_X86_64_PLT32_BND:
4780 gold_assert(gsym == NULL
4781 || gsym->has_plt_offset()
4782 || gsym->final_value_is_known()
4783 || (gsym->is_defined()
4784 && !gsym->is_from_dynobj()
4785 && !gsym->is_preemptible()));
4786 // Note: while this code looks the same as for R_X86_64_PC32, it
4787 // behaves differently because psymval was set to point to
4788 // the PLT entry, rather than the symbol, in Scan::global().
4789 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4793 case elfcpp::R_X86_64_PLTOFF64:
4796 gold_assert(gsym->has_plt_offset()
4797 || gsym->final_value_is_known());
4798 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4799 // This is the address of GLOBAL_OFFSET_TABLE.
4800 got_address = target->got_plt_section()->address();
4801 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4805 case elfcpp::R_X86_64_GOT32:
4806 gold_assert(have_got_offset);
4807 Reloc_funcs::rela32(view, got_offset, addend);
4810 case elfcpp::R_X86_64_GOTPC32:
4813 typename elfcpp::Elf_types<size>::Elf_Addr value;
4814 value = target->got_plt_section()->address();
4815 Reloc_funcs::pcrela32_check(view, value, addend, address);
4819 case elfcpp::R_X86_64_GOT64:
4820 case elfcpp::R_X86_64_GOTPLT64:
4821 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4823 gold_assert(have_got_offset);
4824 Reloc_funcs::rela64(view, got_offset, addend);
4827 case elfcpp::R_X86_64_GOTPC64:
4830 typename elfcpp::Elf_types<size>::Elf_Addr value;
4831 value = target->got_plt_section()->address();
4832 Reloc_funcs::pcrela64(view, value, addend, address);
4836 case elfcpp::R_X86_64_GOTOFF64:
4838 typename elfcpp::Elf_types<size>::Elf_Addr value;
4839 value = (psymval->value(object, 0)
4840 - target->got_plt_section()->address());
4841 Reloc_funcs::rela64(view, value, addend);
4845 case elfcpp::R_X86_64_GOTPCREL:
4846 case elfcpp::R_X86_64_GOTPCRELX:
4847 case elfcpp::R_X86_64_REX_GOTPCRELX:
4850 // mov foo@GOTPCREL(%rip), %reg
4851 // to lea foo(%rip), %reg.
4853 if (!parameters->incremental()
4855 && rela.get_r_offset() >= 2
4857 && !psymval->is_ifunc_symbol())
4859 && rela.get_r_offset() >= 2
4860 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4864 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4867 // callq *foo@GOTPCRELX(%rip) to
4869 // and jmpq *foo@GOTPCRELX(%rip) to
4872 else if (!parameters->incremental()
4874 && rela.get_r_offset() >= 2
4875 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4879 if (view[-1] == 0x15)
4881 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4882 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4885 // Convert GOTPCRELX to 32-bit pc relative reloc.
4886 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4890 // Convert jmpq *foo@GOTPCRELX(%rip) to
4893 // The opcode of direct jmpq is 0xe9.
4895 // The opcode of nop is 0x90.
4897 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4898 // relative and since the instruction following the jmpq is now
4899 // the nop, offset the address by 1 byte. The start of the
4900 // relocation also moves ahead by 1 byte.
4901 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4909 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4910 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4911 - target->got_size());
4915 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4916 gold_assert(object->local_has_got_offset(r_sym,
4917 GOT_TYPE_STANDARD));
4918 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4919 - target->got_size());
4921 typename elfcpp::Elf_types<size>::Elf_Addr value;
4922 value = target->got_plt_section()->address() + got_offset;
4923 Reloc_funcs::pcrela32_check(view, value, addend, address);
4928 case elfcpp::R_X86_64_GOTPCREL64:
4930 gold_assert(have_got_offset);
4931 typename elfcpp::Elf_types<size>::Elf_Addr value;
4932 value = target->got_plt_section()->address() + got_offset;
4933 Reloc_funcs::pcrela64(view, value, addend, address);
4937 case elfcpp::R_X86_64_COPY:
4938 case elfcpp::R_X86_64_GLOB_DAT:
4939 case elfcpp::R_X86_64_JUMP_SLOT:
4940 case elfcpp::R_X86_64_RELATIVE:
4941 case elfcpp::R_X86_64_IRELATIVE:
4942 // These are outstanding tls relocs, which are unexpected when linking
4943 case elfcpp::R_X86_64_TPOFF64:
4944 case elfcpp::R_X86_64_DTPMOD64:
4945 case elfcpp::R_X86_64_TLSDESC:
4946 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4947 _("unexpected reloc %u in object file"),
4951 // These are initial tls relocs, which are expected when linking
4952 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4953 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4954 case elfcpp::R_X86_64_TLSDESC_CALL:
4955 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4956 case elfcpp::R_X86_64_DTPOFF32:
4957 case elfcpp::R_X86_64_DTPOFF64:
4958 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4959 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4960 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4961 view, address, view_size);
4964 case elfcpp::R_X86_64_SIZE32:
4965 case elfcpp::R_X86_64_SIZE64:
4967 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4968 _("unsupported reloc %u"),
4973 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4977 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4978 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4979 _("relocation overflow: "
4980 "reference to local symbol %u in %s"),
4981 r_sym, object->name().c_str());
4983 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4985 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4986 _("relocation overflow: "
4987 "reference to '%s' defined in %s"),
4989 gsym->object()->name().c_str());
4993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4994 _("relocation overflow: reference to '%s'"),
5002 // Perform a TLS relocation.
5006 Target_x86_64<size>::Relocate::relocate_tls(
5007 const Relocate_info<size, false>* relinfo,
5008 Target_x86_64<size>* target,
5010 const elfcpp::Rela<size, false>& rela,
5011 unsigned int r_type,
5012 const Sized_symbol<size>* gsym,
5013 const Symbol_value<size>* psymval,
5014 unsigned char* view,
5015 typename elfcpp::Elf_types<size>::Elf_Addr address,
5016 section_size_type view_size)
5018 Output_segment* tls_segment = relinfo->layout->tls_segment();
5020 const Sized_relobj_file<size, false>* object = relinfo->object;
5021 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5022 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
5023 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
5025 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
5027 const bool is_final = (gsym == NULL
5028 ? !parameters->options().shared()
5029 : gsym->final_value_is_known());
5030 tls::Tls_optimization optimized_type
5031 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
5034 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
5035 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5037 // If this code sequence is used in a non-executable section,
5038 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
5039 // on the assumption that it's being used by itself in a debug
5040 // section. Therefore, in the unlikely event that the code
5041 // sequence appears in a non-executable section, we simply
5042 // leave it unoptimized.
5043 optimized_type = tls::TLSOPT_NONE;
5045 if (optimized_type == tls::TLSOPT_TO_LE)
5047 if (tls_segment == NULL)
5049 gold_assert(parameters->errors()->error_count() > 0
5050 || issue_undefined_symbol_error(gsym));
5053 this->tls_gd_to_le(relinfo, relnum, tls_segment,
5054 rela, r_type, value, view,
5060 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
5061 ? GOT_TYPE_TLS_OFFSET
5062 : GOT_TYPE_TLS_PAIR);
5063 unsigned int got_offset;
5066 gold_assert(gsym->has_got_offset(got_type));
5067 got_offset = gsym->got_offset(got_type) - target->got_size();
5071 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5072 gold_assert(object->local_has_got_offset(r_sym, got_type));
5073 got_offset = (object->local_got_offset(r_sym, got_type)
5074 - target->got_size());
5076 if (optimized_type == tls::TLSOPT_TO_IE)
5078 value = target->got_plt_section()->address() + got_offset;
5079 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
5080 value, view, address, view_size);
5083 else if (optimized_type == tls::TLSOPT_NONE)
5085 // Relocate the field with the offset of the pair of GOT
5087 value = target->got_plt_section()->address() + got_offset;
5088 Relocate_functions<size, false>::pcrela32(view, value, addend,
5093 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5094 _("unsupported reloc %u"), r_type);
5097 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
5098 case elfcpp::R_X86_64_TLSDESC_CALL:
5099 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5101 // See above comment for R_X86_64_TLSGD.
5102 optimized_type = tls::TLSOPT_NONE;
5104 if (optimized_type == tls::TLSOPT_TO_LE)
5106 if (tls_segment == NULL)
5108 gold_assert(parameters->errors()->error_count() > 0
5109 || issue_undefined_symbol_error(gsym));
5112 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
5113 rela, r_type, value, view,
5119 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
5120 ? GOT_TYPE_TLS_OFFSET
5121 : GOT_TYPE_TLS_DESC);
5122 unsigned int got_offset = 0;
5123 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
5124 && optimized_type == tls::TLSOPT_NONE)
5126 // We created GOT entries in the .got.tlsdesc portion of
5127 // the .got.plt section, but the offset stored in the
5128 // symbol is the offset within .got.tlsdesc.
5129 got_offset = (target->got_size()
5130 + target->got_plt_section()->data_size());
5134 gold_assert(gsym->has_got_offset(got_type));
5135 got_offset += gsym->got_offset(got_type) - target->got_size();
5139 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5140 gold_assert(object->local_has_got_offset(r_sym, got_type));
5141 got_offset += (object->local_got_offset(r_sym, got_type)
5142 - target->got_size());
5144 if (optimized_type == tls::TLSOPT_TO_IE)
5146 value = target->got_plt_section()->address() + got_offset;
5147 this->tls_desc_gd_to_ie(relinfo, relnum,
5148 rela, r_type, value, view, address,
5152 else if (optimized_type == tls::TLSOPT_NONE)
5154 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5156 // Relocate the field with the offset of the pair of GOT
5158 value = target->got_plt_section()->address() + got_offset;
5159 Relocate_functions<size, false>::pcrela32(view, value, addend,
5165 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5166 _("unsupported reloc %u"), r_type);
5169 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
5170 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5172 // See above comment for R_X86_64_TLSGD.
5173 optimized_type = tls::TLSOPT_NONE;
5175 if (optimized_type == tls::TLSOPT_TO_LE)
5177 if (tls_segment == NULL)
5179 gold_assert(parameters->errors()->error_count() > 0
5180 || issue_undefined_symbol_error(gsym));
5183 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
5184 value, view, view_size);
5187 else if (optimized_type == tls::TLSOPT_NONE)
5189 // Relocate the field with the offset of the GOT entry for
5190 // the module index.
5191 unsigned int got_offset;
5192 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
5193 - target->got_size());
5194 value = target->got_plt_section()->address() + got_offset;
5195 Relocate_functions<size, false>::pcrela32(view, value, addend,
5199 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5200 _("unsupported reloc %u"), r_type);
5203 case elfcpp::R_X86_64_DTPOFF32:
5204 // This relocation type is used in debugging information.
5205 // In that case we need to not optimize the value. If the
5206 // section is not executable, then we assume we should not
5207 // optimize this reloc. See comments above for R_X86_64_TLSGD,
5208 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
5210 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
5212 if (tls_segment == NULL)
5214 gold_assert(parameters->errors()->error_count() > 0
5215 || issue_undefined_symbol_error(gsym));
5218 value -= tls_segment->memsz();
5220 Relocate_functions<size, false>::rela32(view, value, addend);
5223 case elfcpp::R_X86_64_DTPOFF64:
5224 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
5225 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
5227 if (tls_segment == NULL)
5229 gold_assert(parameters->errors()->error_count() > 0
5230 || issue_undefined_symbol_error(gsym));
5233 value -= tls_segment->memsz();
5235 Relocate_functions<size, false>::rela64(view, value, addend);
5238 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
5240 && gsym->is_undefined()
5241 && parameters->options().output_is_executable())
5243 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
5245 r_type, value, view,
5249 else if (optimized_type == tls::TLSOPT_TO_LE)
5251 if (tls_segment == NULL)
5253 gold_assert(parameters->errors()->error_count() > 0
5254 || issue_undefined_symbol_error(gsym));
5257 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
5259 r_type, value, view,
5263 else if (optimized_type == tls::TLSOPT_NONE)
5265 // Relocate the field with the offset of the GOT entry for
5266 // the tp-relative offset of the symbol.
5267 unsigned int got_offset;
5270 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
5271 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
5272 - target->got_size());
5276 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5277 gold_assert(object->local_has_got_offset(r_sym,
5278 GOT_TYPE_TLS_OFFSET));
5279 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
5280 - target->got_size());
5282 value = target->got_plt_section()->address() + got_offset;
5283 Relocate_functions<size, false>::pcrela32(view, value, addend,
5287 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5288 _("unsupported reloc type %u"),
5292 case elfcpp::R_X86_64_TPOFF32: // Local-exec
5293 if (tls_segment == NULL)
5295 gold_assert(parameters->errors()->error_count() > 0
5296 || issue_undefined_symbol_error(gsym));
5299 value -= tls_segment->memsz();
5300 Relocate_functions<size, false>::rela32(view, value, addend);
5305 // Do a relocation in which we convert a TLS General-Dynamic to an
5310 Target_x86_64<size>::Relocate::tls_gd_to_ie(
5311 const Relocate_info<size, false>* relinfo,
5313 const elfcpp::Rela<size, false>& rela,
5315 typename elfcpp::Elf_types<size>::Elf_Addr value,
5316 unsigned char* view,
5317 typename elfcpp::Elf_types<size>::Elf_Addr address,
5318 section_size_type view_size)
5321 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5322 // .word 0x6666; rex64; call __tls_get_addr@PLT
5323 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5324 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5325 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5326 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5328 // leaq foo@tlsgd(%rip),%rdi;
5329 // .word 0x6666; rex64; call __tls_get_addr@PLT
5330 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5331 // leaq foo@tlsgd(%rip),%rdi;
5332 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5333 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5335 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5336 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5337 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5338 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5342 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5344 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5345 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5346 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5351 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5353 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5354 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5355 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5359 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5360 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
5363 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5365 this->skip_call_tls_get_addr_ = true;
5368 // Do a relocation in which we convert a TLS General-Dynamic to a
5373 Target_x86_64<size>::Relocate::tls_gd_to_le(
5374 const Relocate_info<size, false>* relinfo,
5376 Output_segment* tls_segment,
5377 const elfcpp::Rela<size, false>& rela,
5379 typename elfcpp::Elf_types<size>::Elf_Addr value,
5380 unsigned char* view,
5381 section_size_type view_size)
5384 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5385 // .word 0x6666; rex64; call __tls_get_addr@PLT
5386 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5387 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5388 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5389 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5391 // leaq foo@tlsgd(%rip),%rdi;
5392 // .word 0x6666; rex64; call __tls_get_addr@PLT
5393 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5394 // leaq foo@tlsgd(%rip),%rdi;
5395 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5396 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5398 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5399 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5400 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5401 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5405 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5407 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5408 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5409 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5414 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5416 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5417 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5419 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5423 value -= tls_segment->memsz();
5424 Relocate_functions<size, false>::rela32(view + 8, value, 0);
5426 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5428 this->skip_call_tls_get_addr_ = true;
5431 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5435 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
5436 const Relocate_info<size, false>* relinfo,
5438 const elfcpp::Rela<size, false>& rela,
5439 unsigned int r_type,
5440 typename elfcpp::Elf_types<size>::Elf_Addr value,
5441 unsigned char* view,
5442 typename elfcpp::Elf_types<size>::Elf_Addr address,
5443 section_size_type view_size)
5445 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5447 // leaq foo@tlsdesc(%rip), %rax
5448 // ==> movq foo@gottpoff(%rip), %rax
5449 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5450 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5451 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5452 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
5454 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5455 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
5459 // call *foo@tlscall(%rax)
5461 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5462 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5463 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5464 view[0] == 0xff && view[1] == 0x10);
5470 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5474 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
5475 const Relocate_info<size, false>* relinfo,
5477 Output_segment* tls_segment,
5478 const elfcpp::Rela<size, false>& rela,
5479 unsigned int r_type,
5480 typename elfcpp::Elf_types<size>::Elf_Addr value,
5481 unsigned char* view,
5482 section_size_type view_size)
5484 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5486 // leaq foo@tlsdesc(%rip), %rax
5487 // ==> movq foo@tpoff, %rax
5488 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5489 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5490 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5491 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
5494 value -= tls_segment->memsz();
5495 Relocate_functions<size, false>::rela32(view, value, 0);
5499 // call *foo@tlscall(%rax)
5501 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5502 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5503 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5504 view[0] == 0xff && view[1] == 0x10);
5512 Target_x86_64<size>::Relocate::tls_ld_to_le(
5513 const Relocate_info<size, false>* relinfo,
5516 const elfcpp::Rela<size, false>& rela,
5518 typename elfcpp::Elf_types<size>::Elf_Addr,
5519 unsigned char* view,
5520 section_size_type view_size)
5522 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5524 // ... leq foo@dtpoff(%rax),%reg
5525 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5527 // ... leq foo@dtpoff(%rax),%reg
5528 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5529 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5531 // ... leq foo@dtpoff(%rax),%reg
5532 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5534 // ... leq foo@dtpoff(%rax),%reg
5535 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5537 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5538 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
5540 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5541 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
5543 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5544 view[4] == 0xe8 || view[4] == 0xff);
5546 if (view[4] == 0xe8)
5549 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5551 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5556 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5559 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5563 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5565 this->skip_call_tls_get_addr_ = true;
5568 // Do a relocation in which we convert a TLS Initial-Exec to a
5573 Target_x86_64<size>::Relocate::tls_ie_to_le(
5574 const Relocate_info<size, false>* relinfo,
5576 Output_segment* tls_segment,
5577 const elfcpp::Rela<size, false>& rela,
5579 typename elfcpp::Elf_types<size>::Elf_Addr value,
5580 unsigned char* view,
5581 section_size_type view_size)
5583 // We need to examine the opcodes to figure out which instruction we
5586 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5587 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5589 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5590 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5592 unsigned char op1 = view[-3];
5593 unsigned char op2 = view[-2];
5594 unsigned char op3 = view[-1];
5595 unsigned char reg = op3 >> 3;
5602 else if (size == 32 && op1 == 0x44)
5605 view[-1] = 0xc0 | reg;
5609 // Special handling for %rsp.
5612 else if (size == 32 && op1 == 0x44)
5615 view[-1] = 0xc0 | reg;
5622 else if (size == 32 && op1 == 0x44)
5625 view[-1] = 0x80 | reg | (reg << 3);
5628 if (tls_segment != NULL)
5629 value -= tls_segment->memsz();
5630 Relocate_functions<size, false>::rela32(view, value, 0);
5633 // Relocate section data.
5637 Target_x86_64<size>::relocate_section(
5638 const Relocate_info<size, false>* relinfo,
5639 unsigned int sh_type,
5640 const unsigned char* prelocs,
5642 Output_section* output_section,
5643 bool needs_special_offset_handling,
5644 unsigned char* view,
5645 typename elfcpp::Elf_types<size>::Elf_Addr address,
5646 section_size_type view_size,
5647 const Reloc_symbol_changes* reloc_symbol_changes)
5649 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5652 gold_assert(sh_type == elfcpp::SHT_RELA);
5654 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5655 gold::Default_comdat_behavior, Classify_reloc>(
5661 needs_special_offset_handling,
5665 reloc_symbol_changes);
5668 // Apply an incremental relocation. Incremental relocations always refer
5669 // to global symbols.
5673 Target_x86_64<size>::apply_relocation(
5674 const Relocate_info<size, false>* relinfo,
5675 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5676 unsigned int r_type,
5677 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5679 unsigned char* view,
5680 typename elfcpp::Elf_types<size>::Elf_Addr address,
5681 section_size_type view_size)
5683 gold::apply_relocation<size, false, Target_x86_64<size>,
5684 typename Target_x86_64<size>::Relocate>(
5696 // Scan the relocs during a relocatable link.
5700 Target_x86_64<size>::scan_relocatable_relocs(
5701 Symbol_table* symtab,
5703 Sized_relobj_file<size, false>* object,
5704 unsigned int data_shndx,
5705 unsigned int sh_type,
5706 const unsigned char* prelocs,
5708 Output_section* output_section,
5709 bool needs_special_offset_handling,
5710 size_t local_symbol_count,
5711 const unsigned char* plocal_symbols,
5712 Relocatable_relocs* rr)
5714 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5716 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5717 Scan_relocatable_relocs;
5719 gold_assert(sh_type == elfcpp::SHT_RELA);
5721 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5729 needs_special_offset_handling,
5735 // Scan the relocs for --emit-relocs.
5739 Target_x86_64<size>::emit_relocs_scan(
5740 Symbol_table* symtab,
5742 Sized_relobj_file<size, false>* object,
5743 unsigned int data_shndx,
5744 unsigned int sh_type,
5745 const unsigned char* prelocs,
5747 Output_section* output_section,
5748 bool needs_special_offset_handling,
5749 size_t local_symbol_count,
5750 const unsigned char* plocal_syms,
5751 Relocatable_relocs* rr)
5753 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5755 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5756 Emit_relocs_strategy;
5758 gold_assert(sh_type == elfcpp::SHT_RELA);
5760 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5768 needs_special_offset_handling,
5774 // Relocate a section during a relocatable link.
5778 Target_x86_64<size>::relocate_relocs(
5779 const Relocate_info<size, false>* relinfo,
5780 unsigned int sh_type,
5781 const unsigned char* prelocs,
5783 Output_section* output_section,
5784 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5785 unsigned char* view,
5786 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5787 section_size_type view_size,
5788 unsigned char* reloc_view,
5789 section_size_type reloc_view_size)
5791 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5794 gold_assert(sh_type == elfcpp::SHT_RELA);
5796 gold::relocate_relocs<size, false, Classify_reloc>(
5801 offset_in_output_section,
5809 // Return the value to use for a dynamic which requires special
5810 // treatment. This is how we support equality comparisons of function
5811 // pointers across shared library boundaries, as described in the
5812 // processor specific ABI supplement.
5816 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5818 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5819 return this->plt_address_for_global(gsym);
5822 // Return a string used to fill a code section with nops to take up
5823 // the specified length.
5827 Target_x86_64<size>::do_code_fill(section_size_type length) const
5831 // Build a jmpq instruction to skip over the bytes.
5832 unsigned char jmp[5];
5834 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5835 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5836 + std::string(length - 5, static_cast<char>(0x90)));
5839 // Nop sequences of various lengths.
5840 const char nop1[1] = { '\x90' }; // nop
5841 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5842 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5843 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5845 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5847 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5848 '\x44', '\x00', '\x00' };
5849 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5850 '\x00', '\x00', '\x00',
5852 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5853 '\x00', '\x00', '\x00',
5855 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5856 '\x84', '\x00', '\x00',
5857 '\x00', '\x00', '\x00' };
5858 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5859 '\x1f', '\x84', '\x00',
5860 '\x00', '\x00', '\x00',
5862 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5863 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5864 '\x00', '\x00', '\x00',
5866 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5867 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5868 '\x84', '\x00', '\x00',
5869 '\x00', '\x00', '\x00' };
5870 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5871 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5872 '\x1f', '\x84', '\x00',
5873 '\x00', '\x00', '\x00',
5875 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5876 '\x66', '\x66', '\x2e', // data16
5877 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5878 '\x00', '\x00', '\x00',
5880 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5881 '\x66', '\x66', '\x66', // data16; data16
5882 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5883 '\x84', '\x00', '\x00',
5884 '\x00', '\x00', '\x00' };
5886 const char* nops[16] = {
5888 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5889 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5892 return std::string(nops[length], length);
5895 // Return the addend to use for a target specific relocation. The
5896 // only target specific relocation is R_X86_64_TLSDESC for a local
5897 // symbol. We want to set the addend is the offset of the local
5898 // symbol in the TLS segment.
5902 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5905 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5906 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5907 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5908 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5909 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5910 gold_assert(psymval->is_tls_symbol());
5911 // The value of a TLS symbol is the offset in the TLS segment.
5912 return psymval->value(ti.object, 0);
5915 // Return the value to use for the base of a DW_EH_PE_datarel offset
5916 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5917 // assembler can not write out the difference between two labels in
5918 // different sections, so instead of using a pc-relative value they
5919 // use an offset from the GOT.
5923 Target_x86_64<size>::do_ehframe_datarel_base() const
5925 gold_assert(this->global_offset_table_ != NULL);
5926 Symbol* sym = this->global_offset_table_;
5927 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5928 return ssym->value();
5931 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5932 // compiled with -fsplit-stack. The function calls non-split-stack
5933 // code. We have to change the function so that it always ensures
5934 // that it has enough stack space to run some random function.
5936 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5937 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5938 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5940 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5941 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5942 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5946 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5947 section_offset_type fnoffset,
5948 section_size_type fnsize,
5949 const unsigned char*,
5951 unsigned char* view,
5952 section_size_type view_size,
5954 std::string* to) const
5956 const char* const cmp_insn = reinterpret_cast<const char*>
5957 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5958 const char* const lea_r10_insn = reinterpret_cast<const char*>
5959 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5960 const char* const lea_r11_insn = reinterpret_cast<const char*>
5961 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5963 const size_t cmp_insn_len =
5964 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5965 const size_t lea_r10_insn_len =
5966 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5967 const size_t lea_r11_insn_len =
5968 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5969 const size_t nop_len = (size == 32 ? 7 : 8);
5971 // The function starts with a comparison of the stack pointer and a
5972 // field in the TCB. This is followed by a jump.
5975 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5976 && fnsize > nop_len + 1)
5978 // We will call __morestack if the carry flag is set after this
5979 // comparison. We turn the comparison into an stc instruction
5981 view[fnoffset] = '\xf9';
5982 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5984 // lea NN(%rsp),%r10
5985 // lea NN(%rsp),%r11
5986 else if ((this->match_view(view, view_size, fnoffset,
5987 lea_r10_insn, lea_r10_insn_len)
5988 || this->match_view(view, view_size, fnoffset,
5989 lea_r11_insn, lea_r11_insn_len))
5992 // This is loading an offset from the stack pointer for a
5993 // comparison. The offset is negative, so we decrease the
5994 // offset by the amount of space we need for the stack. This
5995 // means we will avoid calling __morestack if there happens to
5996 // be plenty of space on the stack already.
5997 unsigned char* pval = view + fnoffset + 4;
5998 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5999 val -= parameters->options().split_stack_adjust_size();
6000 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
6004 if (!object->has_no_split_stack())
6005 object->error(_("failed to match split-stack sequence at "
6006 "section %u offset %0zx"),
6007 shndx, static_cast<size_t>(fnoffset));
6011 // We have to change the function so that it calls
6012 // __morestack_non_split instead of __morestack. The former will
6013 // allocate additional stack space.
6014 *from = "__morestack";
6015 *to = "__morestack_non_split";
6018 // The selector for x86_64 object files. Note this is never instantiated
6019 // directly. It's only used in Target_selector_x86_64_nacl, below.
6022 class Target_selector_x86_64 : public Target_selector_freebsd
6025 Target_selector_x86_64()
6026 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
6028 ? "elf64-x86-64" : "elf32-x86-64"),
6030 ? "elf64-x86-64-freebsd"
6031 : "elf32-x86-64-freebsd"),
6032 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
6036 do_instantiate_target()
6037 { return new Target_x86_64<size>(); }
6041 // NaCl variant. It uses different PLT contents.
6044 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
6047 Output_data_plt_x86_64_nacl(Layout* layout,
6048 Output_data_got<64, false>* got,
6049 Output_data_got_plt_x86_64* got_plt,
6050 Output_data_space* got_irelative)
6051 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
6052 got, got_plt, got_irelative)
6055 Output_data_plt_x86_64_nacl(Layout* layout,
6056 Output_data_got<64, false>* got,
6057 Output_data_got_plt_x86_64* got_plt,
6058 Output_data_space* got_irelative,
6059 unsigned int plt_count)
6060 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
6061 got, got_plt, got_irelative,
6066 virtual unsigned int
6067 do_get_plt_entry_size() const
6068 { return plt_entry_size; }
6071 do_add_eh_frame(Layout* layout)
6073 layout->add_eh_frame_for_plt(this,
6074 this->plt_eh_frame_cie,
6075 this->plt_eh_frame_cie_size,
6077 plt_eh_frame_fde_size);
6081 do_fill_first_plt_entry(unsigned char* pov,
6082 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
6083 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
6085 virtual unsigned int
6086 do_fill_plt_entry(unsigned char* pov,
6087 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6088 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6089 unsigned int got_offset,
6090 unsigned int plt_offset,
6091 unsigned int plt_index);
6094 do_fill_tlsdesc_entry(unsigned char* pov,
6095 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6096 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6097 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6098 unsigned int tlsdesc_got_offset,
6099 unsigned int plt_offset);
6102 // The size of an entry in the PLT.
6103 static const int plt_entry_size = 64;
6105 // The first entry in the PLT.
6106 static const unsigned char first_plt_entry[plt_entry_size];
6108 // Other entries in the PLT for an executable.
6109 static const unsigned char plt_entry[plt_entry_size];
6111 // The reserved TLSDESC entry in the PLT for an executable.
6112 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
6114 // The .eh_frame unwind information for the PLT.
6115 static const int plt_eh_frame_fde_size = 32;
6116 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
6120 class Target_x86_64_nacl : public Target_x86_64<size>
6123 Target_x86_64_nacl()
6124 : Target_x86_64<size>(&x86_64_nacl_info)
6127 virtual Output_data_plt_x86_64<size>*
6128 do_make_data_plt(Layout* layout,
6129 Output_data_got<64, false>* got,
6130 Output_data_got_plt_x86_64* got_plt,
6131 Output_data_space* got_irelative)
6133 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
6137 virtual Output_data_plt_x86_64<size>*
6138 do_make_data_plt(Layout* layout,
6139 Output_data_got<64, false>* got,
6140 Output_data_got_plt_x86_64* got_plt,
6141 Output_data_space* got_irelative,
6142 unsigned int plt_count)
6144 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
6150 do_code_fill(section_size_type length) const;
6153 static const Target::Target_info x86_64_nacl_info;
6157 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
6160 false, // is_big_endian
6161 elfcpp::EM_X86_64, // machine_code
6162 false, // has_make_symbol
6163 false, // has_resolve
6164 true, // has_code_fill
6165 true, // is_default_stack_executable
6166 true, // can_icf_inline_merge_sections
6168 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
6169 0x20000, // default_text_segment_address
6170 0x10000, // abi_pagesize (overridable by -z max-page-size)
6171 0x10000, // common_pagesize (overridable by -z common-page-size)
6172 true, // isolate_execinstr
6173 0x10000000, // rosegment_gap
6174 elfcpp::SHN_UNDEF, // small_common_shndx
6175 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
6176 0, // small_common_section_flags
6177 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
6178 NULL, // attributes_section
6179 NULL, // attributes_vendor
6180 "_start", // entry_symbol_name
6181 32, // hash_entry_size
6182 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
6186 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
6189 false, // is_big_endian
6190 elfcpp::EM_X86_64, // machine_code
6191 false, // has_make_symbol
6192 false, // has_resolve
6193 true, // has_code_fill
6194 true, // is_default_stack_executable
6195 true, // can_icf_inline_merge_sections
6197 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
6198 0x20000, // default_text_segment_address
6199 0x10000, // abi_pagesize (overridable by -z max-page-size)
6200 0x10000, // common_pagesize (overridable by -z common-page-size)
6201 true, // isolate_execinstr
6202 0x10000000, // rosegment_gap
6203 elfcpp::SHN_UNDEF, // small_common_shndx
6204 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
6205 0, // small_common_section_flags
6206 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
6207 NULL, // attributes_section
6208 NULL, // attributes_vendor
6209 "_start", // entry_symbol_name
6210 32, // hash_entry_size
6211 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
6214 #define NACLMASK 0xe0 // 32-byte alignment mask.
6216 // The first entry in the PLT.
6220 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
6222 0xff, 0x35, // pushq contents of memory address
6223 0, 0, 0, 0, // replaced with address of .got + 8
6224 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
6225 0, 0, 0, 0, // replaced with address of .got + 16
6226 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6227 0x4d, 0x01, 0xfb, // add %r15, %r11
6228 0x41, 0xff, 0xe3, // jmpq *%r11
6230 // 9-byte nop sequence to pad out to the next 32-byte boundary.
6231 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
6233 // 32 bytes of nop to pad out to the standard size
6234 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6235 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6236 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6237 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6238 0x66, // excess data32 prefix
6244 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
6246 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6247 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
6249 memcpy(pov, first_plt_entry, plt_entry_size);
6250 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6252 - (plt_address + 2 + 4)));
6253 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6255 - (plt_address + 9 + 4)));
6258 // Subsequent entries in the PLT.
6262 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
6264 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6265 0, 0, 0, 0, // replaced with address of symbol in .got
6266 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6267 0x4d, 0x01, 0xfb, // add %r15, %r11
6268 0x41, 0xff, 0xe3, // jmpq *%r11
6270 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6271 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6272 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6274 // Lazy GOT entries point here (32-byte aligned).
6275 0x68, // pushq immediate
6276 0, 0, 0, 0, // replaced with index into relocation table
6277 0xe9, // jmp relative
6278 0, 0, 0, 0, // replaced with offset to start of .plt0
6280 // 22 bytes of nop to pad out to the standard size.
6281 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6282 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6283 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6288 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
6290 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6291 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6292 unsigned int got_offset,
6293 unsigned int plt_offset,
6294 unsigned int plt_index)
6296 memcpy(pov, plt_entry, plt_entry_size);
6297 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
6298 (got_address + got_offset
6299 - (plt_address + plt_offset
6302 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
6303 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
6304 - (plt_offset + 38 + 4));
6309 // The reserved TLSDESC entry in the PLT.
6313 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
6315 0xff, 0x35, // pushq x(%rip)
6316 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6317 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6318 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6319 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6320 0x4d, 0x01, 0xfb, // add %r15, %r11
6321 0x41, 0xff, 0xe3, // jmpq *%r11
6323 // 41 bytes of nop to pad out to the standard size.
6324 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6325 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6326 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6327 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6328 0x66, 0x66, // excess data32 prefixes
6329 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6334 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
6336 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6337 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6338 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6339 unsigned int tlsdesc_got_offset,
6340 unsigned int plt_offset)
6342 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
6343 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6345 - (plt_address + plt_offset
6347 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6349 + tlsdesc_got_offset
6350 - (plt_address + plt_offset
6354 // The .eh_frame unwind information for the PLT.
6358 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
6360 0, 0, 0, 0, // Replaced with offset to .plt.
6361 0, 0, 0, 0, // Replaced with size of .plt.
6362 0, // Augmentation size.
6363 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
6364 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
6365 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
6366 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
6367 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
6368 13, // Block length.
6369 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
6370 elfcpp::DW_OP_breg16, 0, // Push %rip.
6371 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
6372 elfcpp::DW_OP_and, // & (%rip & 0x3f).
6373 elfcpp::DW_OP_const1u, 37, // Push 0x25.
6374 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
6375 elfcpp::DW_OP_lit3, // Push 3.
6376 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
6377 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6378 elfcpp::DW_CFA_nop, // Align to 32 bytes.
6382 // Return a string used to fill a code section with nops.
6383 // For NaCl, long NOPs are only valid if they do not cross
6384 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6387 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
6389 return std::string(length, static_cast<char>(0x90));
6392 // The selector for x86_64-nacl object files.
6395 class Target_selector_x86_64_nacl
6396 : public Target_selector_nacl<Target_selector_x86_64<size>,
6397 Target_x86_64_nacl<size> >
6400 Target_selector_x86_64_nacl()
6401 : Target_selector_nacl<Target_selector_x86_64<size>,
6402 Target_x86_64_nacl<size> >("x86-64",
6404 ? "elf64-x86-64-nacl"
6405 : "elf32-x86-64-nacl",
6408 : "elf32_x86_64_nacl")
6412 Target_selector_x86_64_nacl<64> target_selector_x86_64;
6413 Target_selector_x86_64_nacl<32> target_selector_x32;
6415 } // End anonymous namespace.