1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64 : public Output_section_data_build
56 Output_data_got_plt_x86_64(Layout* layout)
57 : Output_section_data_build(8),
61 Output_data_got_plt_x86_64(Layout* layout, off_t data_size)
62 : Output_section_data_build(data_size, 8),
67 // Write out the PLT data.
69 do_write(Output_file*);
71 // Write to a map file.
73 do_print_to_mapfile(Mapfile* mapfile) const
74 { mapfile->print_output_data(this, "** GOT PLT"); }
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
88 class Output_data_plt_x86_64 : public Output_section_data
91 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
93 Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
94 Output_data_got<64, false>* got,
95 Output_data_got_plt_x86_64* got_plt,
96 Output_data_space* got_irelative)
97 : Output_section_data(addralign), tlsdesc_rel_(NULL),
98 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
99 got_irelative_(got_irelative), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout); }
103 Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
104 Output_data_got<64, false>* got,
105 Output_data_got_plt_x86_64* got_plt,
106 Output_data_space* got_irelative,
107 unsigned int plt_count)
108 : Output_section_data((plt_count + 1) * plt_entry_size,
109 plt_entry_size, false),
110 tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
111 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
116 // Initialize the free list and reserve the first entry.
117 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
118 this->free_list_.remove(0, plt_entry_size);
121 // Initialize the PLT section.
123 init(Layout* layout);
125 // Add an entry to the PLT.
127 add_entry(Symbol_table*, Layout*, Symbol* gsym);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
131 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
132 Sized_relobj_file<size, false>* relobj,
133 unsigned int local_sym_index);
135 // Add the relocation for a PLT entry.
137 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
138 unsigned int got_offset);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
142 reserve_tlsdesc_entry(unsigned int got_offset)
143 { this->tlsdesc_got_offset_ = got_offset; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_ != -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
157 get_tlsdesc_plt_offset() const
159 return ((this->count_ + this->irelative_count_ + 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
166 { return this->rel_; }
168 // Return where the TLSDESC relocations should go.
170 rela_tlsdesc(Layout*);
172 // Return where the IRELATIVE relocations should go in the PLT
175 rela_irelative(Symbol_table*, Layout*);
177 // Return whether we created a section for IRELATIVE relocations.
179 has_irelative_section() const
180 { return this->irelative_rel_ != NULL; }
182 // Return the number of PLT entries.
185 { return this->count_ + this->irelative_count_; }
187 // Return the offset of the first non-reserved PLT entry.
189 first_plt_entry_offset()
190 { return this->get_plt_entry_size(); }
192 // Return the size of a PLT entry.
194 get_plt_entry_size() const
195 { return this->do_get_plt_entry_size(); }
197 // Reserve a slot in the PLT for an existing symbol in an incremental update.
199 reserve_slot(unsigned int plt_index)
201 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
202 (plt_index + 2) * this->get_plt_entry_size());
205 // Return the PLT address to use for a global symbol.
207 address_for_global(const Symbol*);
209 // Return the PLT address to use for a local symbol.
211 address_for_local(const Relobj*, unsigned int symndx);
213 // Add .eh_frame information for the PLT.
215 add_eh_frame(Layout* layout)
216 { this->do_add_eh_frame(layout); }
219 // Fill in the first PLT entry.
221 fill_first_plt_entry(unsigned char* pov,
222 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
223 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
224 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
226 // Fill in a normal PLT entry. Returns the offset into the entry that
227 // should be the initial GOT slot value.
229 fill_plt_entry(unsigned char* pov,
230 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
231 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
232 unsigned int got_offset,
233 unsigned int plt_offset,
234 unsigned int plt_index)
236 return this->do_fill_plt_entry(pov, got_address, plt_address,
237 got_offset, plt_offset, plt_index);
240 // Fill in the reserved TLSDESC PLT entry.
242 fill_tlsdesc_entry(unsigned char* pov,
243 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
244 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
245 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
246 unsigned int tlsdesc_got_offset,
247 unsigned int plt_offset)
249 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
250 tlsdesc_got_offset, plt_offset);
254 do_get_plt_entry_size() const = 0;
257 do_fill_first_plt_entry(unsigned char* pov,
258 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
259 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
263 do_fill_plt_entry(unsigned char* pov,
264 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
265 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
266 unsigned int got_offset,
267 unsigned int plt_offset,
268 unsigned int plt_index) = 0;
271 do_fill_tlsdesc_entry(unsigned char* pov,
272 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
273 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
274 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
275 unsigned int tlsdesc_got_offset,
276 unsigned int plt_offset) = 0;
279 do_add_eh_frame(Layout* layout) = 0;
282 do_adjust_output_section(Output_section* os);
284 // Write to a map file.
286 do_print_to_mapfile(Mapfile* mapfile) const
287 { mapfile->print_output_data(this, _("** PLT")); }
289 // The CIE of the .eh_frame unwind information for the PLT.
290 static const int plt_eh_frame_cie_size = 16;
291 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
294 // Set the final size.
296 set_final_data_size();
298 // Write out the PLT data.
300 do_write(Output_file*);
302 // The reloc section.
304 // The TLSDESC relocs, if necessary. These must follow the regular
306 Reloc_section* tlsdesc_rel_;
307 // The IRELATIVE relocs, if necessary. These must follow the
308 // regular PLT relocations and the TLSDESC relocations.
309 Reloc_section* irelative_rel_;
311 Output_data_got<64, false>* got_;
312 // The .got.plt section.
313 Output_data_got_plt_x86_64* got_plt_;
314 // The part of the .got.plt section used for IRELATIVE relocs.
315 Output_data_space* got_irelative_;
316 // The number of PLT entries.
318 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
319 // follow the regular PLT entries.
320 unsigned int irelative_count_;
321 // Offset of the reserved TLSDESC_GOT entry when needed.
322 unsigned int tlsdesc_got_offset_;
323 // List of available regions within the section, for incremental
325 Free_list free_list_;
329 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
332 Output_data_plt_x86_64_standard(Layout* layout,
333 Output_data_got<64, false>* got,
334 Output_data_got_plt_x86_64* got_plt,
335 Output_data_space* got_irelative)
336 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
337 got, got_plt, got_irelative)
340 Output_data_plt_x86_64_standard(Layout* layout,
341 Output_data_got<64, false>* got,
342 Output_data_got_plt_x86_64* got_plt,
343 Output_data_space* got_irelative,
344 unsigned int plt_count)
345 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
346 got, got_plt, got_irelative,
352 do_get_plt_entry_size() const
353 { return plt_entry_size; }
356 do_add_eh_frame(Layout* layout)
358 layout->add_eh_frame_for_plt(this,
359 this->plt_eh_frame_cie,
360 this->plt_eh_frame_cie_size,
362 plt_eh_frame_fde_size);
366 do_fill_first_plt_entry(unsigned char* pov,
367 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
368 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
371 do_fill_plt_entry(unsigned char* pov,
372 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
373 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
374 unsigned int got_offset,
375 unsigned int plt_offset,
376 unsigned int plt_index);
379 do_fill_tlsdesc_entry(unsigned char* pov,
380 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
381 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
382 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
383 unsigned int tlsdesc_got_offset,
384 unsigned int plt_offset);
387 // The size of an entry in the PLT.
388 static const int plt_entry_size = 16;
390 // The first entry in the PLT.
391 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
392 // procedure linkage table for both programs and shared objects."
393 static const unsigned char first_plt_entry[plt_entry_size];
395 // Other entries in the PLT for an executable.
396 static const unsigned char plt_entry[plt_entry_size];
398 // The reserved TLSDESC entry in the PLT for an executable.
399 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
401 // The .eh_frame unwind information for the PLT.
402 static const int plt_eh_frame_fde_size = 32;
403 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
406 // The x86_64 target class.
408 // http://www.x86-64.org/documentation/abi.pdf
409 // TLS info comes from
410 // http://people.redhat.com/drepper/tls.pdf
411 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
414 class Target_x86_64 : public Sized_target<size, false>
417 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
418 // uses only Elf64_Rela relocation entries with explicit addends."
419 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
421 Target_x86_64(const Target::Target_info* info = &x86_64_info)
422 : Sized_target<size, false>(info),
423 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
424 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
425 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
426 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
427 tls_base_symbol_defined_(false)
430 // Hook for a new output section.
432 do_new_output_section(Output_section*) const;
434 // Scan the relocations to look for symbol adjustments.
436 gc_process_relocs(Symbol_table* symtab,
438 Sized_relobj_file<size, false>* object,
439 unsigned int data_shndx,
440 unsigned int sh_type,
441 const unsigned char* prelocs,
443 Output_section* output_section,
444 bool needs_special_offset_handling,
445 size_t local_symbol_count,
446 const unsigned char* plocal_symbols);
448 // Scan the relocations to look for symbol adjustments.
450 scan_relocs(Symbol_table* symtab,
452 Sized_relobj_file<size, false>* object,
453 unsigned int data_shndx,
454 unsigned int sh_type,
455 const unsigned char* prelocs,
457 Output_section* output_section,
458 bool needs_special_offset_handling,
459 size_t local_symbol_count,
460 const unsigned char* plocal_symbols);
462 // Finalize the sections.
464 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
466 // Return the value to use for a dynamic which requires special
469 do_dynsym_value(const Symbol*) const;
471 // Relocate a section.
473 relocate_section(const Relocate_info<size, false>*,
474 unsigned int sh_type,
475 const unsigned char* prelocs,
477 Output_section* output_section,
478 bool needs_special_offset_handling,
480 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
481 section_size_type view_size,
482 const Reloc_symbol_changes*);
484 // Scan the relocs during a relocatable link.
486 scan_relocatable_relocs(Symbol_table* symtab,
488 Sized_relobj_file<size, false>* object,
489 unsigned int data_shndx,
490 unsigned int sh_type,
491 const unsigned char* prelocs,
493 Output_section* output_section,
494 bool needs_special_offset_handling,
495 size_t local_symbol_count,
496 const unsigned char* plocal_symbols,
497 Relocatable_relocs*);
499 // Scan the relocs for --emit-relocs.
501 emit_relocs_scan(Symbol_table* symtab,
503 Sized_relobj_file<size, false>* object,
504 unsigned int data_shndx,
505 unsigned int sh_type,
506 const unsigned char* prelocs,
508 Output_section* output_section,
509 bool needs_special_offset_handling,
510 size_t local_symbol_count,
511 const unsigned char* plocal_syms,
512 Relocatable_relocs* rr);
514 // Emit relocations for a section.
517 const Relocate_info<size, false>*,
518 unsigned int sh_type,
519 const unsigned char* prelocs,
521 Output_section* output_section,
522 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
524 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
525 section_size_type view_size,
526 unsigned char* reloc_view,
527 section_size_type reloc_view_size);
529 // Return a string used to fill a code section with nops.
531 do_code_fill(section_size_type length) const;
533 // Return whether SYM is defined by the ABI.
535 do_is_defined_by_abi(const Symbol* sym) const
536 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
538 // Return the symbol index to use for a target specific relocation.
539 // The only target specific relocation is R_X86_64_TLSDESC for a
540 // local symbol, which is an absolute reloc.
542 do_reloc_symbol_index(void*, unsigned int r_type) const
544 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
548 // Return the addend to use for a target specific relocation.
550 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
552 // Return the PLT section.
554 do_plt_address_for_global(const Symbol* gsym) const
555 { return this->plt_section()->address_for_global(gsym); }
558 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
559 { return this->plt_section()->address_for_local(relobj, symndx); }
561 // This function should be defined in targets that can use relocation
562 // types to determine (implemented in local_reloc_may_be_function_pointer
563 // and global_reloc_may_be_function_pointer)
564 // if a function's pointer is taken. ICF uses this in safe mode to only
565 // fold those functions whose pointer is defintely not taken. For x86_64
566 // pie binaries, safe ICF cannot be done by looking at relocation types.
568 do_can_check_for_function_pointers() const
569 { return !parameters->options().pie(); }
571 // Return the base for a DW_EH_PE_datarel encoding.
573 do_ehframe_datarel_base() const;
575 // Adjust -fsplit-stack code which calls non-split-stack code.
577 do_calls_non_split(Relobj* object, unsigned int shndx,
578 section_offset_type fnoffset, section_size_type fnsize,
579 const unsigned char* prelocs, size_t reloc_count,
580 unsigned char* view, section_size_type view_size,
581 std::string* from, std::string* to) const;
583 // Return the size of the GOT section.
587 gold_assert(this->got_ != NULL);
588 return this->got_->data_size();
591 // Return the number of entries in the GOT.
593 got_entry_count() const
595 if (this->got_ == NULL)
597 return this->got_size() / 8;
600 // Return the number of entries in the PLT.
602 plt_entry_count() const;
604 // Return the offset of the first non-reserved PLT entry.
606 first_plt_entry_offset() const;
608 // Return the size of each PLT entry.
610 plt_entry_size() const;
612 // Return the size of each GOT entry.
614 got_entry_size() const
617 // Create the GOT section for an incremental update.
618 Output_data_got_base*
619 init_got_plt_for_update(Symbol_table* symtab,
621 unsigned int got_count,
622 unsigned int plt_count);
624 // Reserve a GOT entry for a local symbol, and regenerate any
625 // necessary dynamic relocations.
627 reserve_local_got_entry(unsigned int got_index,
628 Sized_relobj<size, false>* obj,
630 unsigned int got_type);
632 // Reserve a GOT entry for a global symbol, and regenerate any
633 // necessary dynamic relocations.
635 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
636 unsigned int got_type);
638 // Register an existing PLT entry for a global symbol.
640 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
643 // Force a COPY relocation for a given symbol.
645 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
647 // Apply an incremental relocation.
649 apply_relocation(const Relocate_info<size, false>* relinfo,
650 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
652 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
655 typename elfcpp::Elf_types<size>::Elf_Addr address,
656 section_size_type view_size);
658 // Add a new reloc argument, returning the index in the vector.
660 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
662 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
663 return this->tlsdesc_reloc_info_.size() - 1;
666 Output_data_plt_x86_64<size>*
667 make_data_plt(Layout* layout,
668 Output_data_got<64, false>* got,
669 Output_data_got_plt_x86_64* got_plt,
670 Output_data_space* got_irelative)
672 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
675 Output_data_plt_x86_64<size>*
676 make_data_plt(Layout* layout,
677 Output_data_got<64, false>* got,
678 Output_data_got_plt_x86_64* got_plt,
679 Output_data_space* got_irelative,
680 unsigned int plt_count)
682 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
686 virtual Output_data_plt_x86_64<size>*
687 do_make_data_plt(Layout* layout,
688 Output_data_got<64, false>* got,
689 Output_data_got_plt_x86_64* got_plt,
690 Output_data_space* got_irelative)
692 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
696 virtual Output_data_plt_x86_64<size>*
697 do_make_data_plt(Layout* layout,
698 Output_data_got<64, false>* got,
699 Output_data_got_plt_x86_64* got_plt,
700 Output_data_space* got_irelative,
701 unsigned int plt_count)
703 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
709 // The class which scans relocations.
714 : issued_non_pic_error_(false)
718 get_reference_flags(unsigned int r_type);
721 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
722 Sized_relobj_file<size, false>* object,
723 unsigned int data_shndx,
724 Output_section* output_section,
725 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
726 const elfcpp::Sym<size, false>& lsym,
730 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
731 Sized_relobj_file<size, false>* object,
732 unsigned int data_shndx,
733 Output_section* output_section,
734 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
738 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
739 Target_x86_64* target,
740 Sized_relobj_file<size, false>* object,
741 unsigned int data_shndx,
742 Output_section* output_section,
743 const elfcpp::Rela<size, false>& reloc,
745 const elfcpp::Sym<size, false>& lsym);
748 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
749 Target_x86_64* target,
750 Sized_relobj_file<size, false>* object,
751 unsigned int data_shndx,
752 Output_section* output_section,
753 const elfcpp::Rela<size, false>& reloc,
759 unsupported_reloc_local(Sized_relobj_file<size, false>*,
760 unsigned int r_type);
763 unsupported_reloc_global(Sized_relobj_file<size, false>*,
764 unsigned int r_type, Symbol*);
767 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
770 possible_function_pointer_reloc(unsigned int r_type);
773 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
774 unsigned int r_type);
776 // Whether we have issued an error about a non-PIC compilation.
777 bool issued_non_pic_error_;
780 // The class which implements relocation.
785 : skip_call_tls_get_addr_(false)
790 if (this->skip_call_tls_get_addr_)
792 // FIXME: This needs to specify the location somehow.
793 gold_error(_("missing expected TLS relocation"));
797 // Do a relocation. Return false if the caller should not issue
798 // any warnings about this relocation.
800 relocate(const Relocate_info<size, false>*, unsigned int,
801 Target_x86_64*, Output_section*, size_t, const unsigned char*,
802 const Sized_symbol<size>*, const Symbol_value<size>*,
803 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
807 // Do a TLS relocation.
809 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
810 size_t relnum, const elfcpp::Rela<size, false>&,
811 unsigned int r_type, const Sized_symbol<size>*,
812 const Symbol_value<size>*,
813 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
816 // Do a TLS General-Dynamic to Initial-Exec transition.
818 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
819 const elfcpp::Rela<size, false>&, unsigned int r_type,
820 typename elfcpp::Elf_types<size>::Elf_Addr value,
822 typename elfcpp::Elf_types<size>::Elf_Addr,
823 section_size_type view_size);
825 // Do a TLS General-Dynamic to Local-Exec transition.
827 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
828 Output_segment* tls_segment,
829 const elfcpp::Rela<size, false>&, unsigned int r_type,
830 typename elfcpp::Elf_types<size>::Elf_Addr value,
832 section_size_type view_size);
834 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
836 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
837 const elfcpp::Rela<size, false>&, unsigned int r_type,
838 typename elfcpp::Elf_types<size>::Elf_Addr value,
840 typename elfcpp::Elf_types<size>::Elf_Addr,
841 section_size_type view_size);
843 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
845 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
846 Output_segment* tls_segment,
847 const elfcpp::Rela<size, false>&, unsigned int r_type,
848 typename elfcpp::Elf_types<size>::Elf_Addr value,
850 section_size_type view_size);
852 // Do a TLS Local-Dynamic to Local-Exec transition.
854 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
855 Output_segment* tls_segment,
856 const elfcpp::Rela<size, false>&, unsigned int r_type,
857 typename elfcpp::Elf_types<size>::Elf_Addr value,
859 section_size_type view_size);
861 // Do a TLS Initial-Exec to Local-Exec transition.
863 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
864 Output_segment* tls_segment,
865 const elfcpp::Rela<size, false>&, unsigned int r_type,
866 typename elfcpp::Elf_types<size>::Elf_Addr value,
868 section_size_type view_size);
870 // This is set if we should skip the next reloc, which should be a
871 // PLT32 reloc against ___tls_get_addr.
872 bool skip_call_tls_get_addr_;
875 // Check if relocation against this symbol is a candidate for
877 // mov foo@GOTPCREL(%rip), %reg
878 // to lea foo(%rip), %reg.
880 can_convert_mov_to_lea(const Symbol* gsym)
882 gold_assert(gsym != NULL);
883 return (gsym->type() != elfcpp::STT_GNU_IFUNC
884 && !gsym->is_undefined ()
885 && !gsym->is_from_dynobj()
886 && !gsym->is_preemptible()
887 && (!parameters->options().shared()
888 || (gsym->visibility() != elfcpp::STV_DEFAULT
889 && gsym->visibility() != elfcpp::STV_PROTECTED)
890 || parameters->options().Bsymbolic())
891 && strcmp(gsym->name(), "_DYNAMIC") != 0);
894 // Adjust TLS relocation type based on the options and whether this
895 // is a local symbol.
896 static tls::Tls_optimization
897 optimize_tls_reloc(bool is_final, int r_type);
899 // Get the GOT section, creating it if necessary.
900 Output_data_got<64, false>*
901 got_section(Symbol_table*, Layout*);
903 // Get the GOT PLT section.
904 Output_data_got_plt_x86_64*
905 got_plt_section() const
907 gold_assert(this->got_plt_ != NULL);
908 return this->got_plt_;
911 // Get the GOT section for TLSDESC entries.
912 Output_data_got<64, false>*
913 got_tlsdesc_section() const
915 gold_assert(this->got_tlsdesc_ != NULL);
916 return this->got_tlsdesc_;
919 // Create the PLT section.
921 make_plt_section(Symbol_table* symtab, Layout* layout);
923 // Create a PLT entry for a global symbol.
925 make_plt_entry(Symbol_table*, Layout*, Symbol*);
927 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
929 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
930 Sized_relobj_file<size, false>* relobj,
931 unsigned int local_sym_index);
933 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
935 define_tls_base_symbol(Symbol_table*, Layout*);
937 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
939 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
941 // Create a GOT entry for the TLS module index.
943 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
944 Sized_relobj_file<size, false>* object);
946 // Get the PLT section.
947 Output_data_plt_x86_64<size>*
950 gold_assert(this->plt_ != NULL);
954 // Get the dynamic reloc section, creating it if necessary.
956 rela_dyn_section(Layout*);
958 // Get the section to use for TLSDESC relocations.
960 rela_tlsdesc_section(Layout*) const;
962 // Get the section to use for IRELATIVE relocations.
964 rela_irelative_section(Layout*);
966 // Add a potential copy relocation.
968 copy_reloc(Symbol_table* symtab, Layout* layout,
969 Sized_relobj_file<size, false>* object,
970 unsigned int shndx, Output_section* output_section,
971 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
973 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
974 this->copy_relocs_.copy_reloc(symtab, layout,
975 symtab->get_sized_symbol<size>(sym),
976 object, shndx, output_section,
977 r_type, reloc.get_r_offset(),
978 reloc.get_r_addend(),
979 this->rela_dyn_section(layout));
982 // Information about this specific target which we pass to the
983 // general Target structure.
984 static const Target::Target_info x86_64_info;
986 // The types of GOT entries needed for this platform.
987 // These values are exposed to the ABI in an incremental link.
988 // Do not renumber existing values without changing the version
989 // number of the .gnu_incremental_inputs section.
992 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
993 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
994 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
995 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
998 // This type is used as the argument to the target specific
999 // relocation routines. The only target specific reloc is
1000 // R_X86_64_TLSDESC against a local symbol.
1003 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1004 : object(a_object), r_sym(a_r_sym)
1007 // The object in which the local symbol is defined.
1008 Sized_relobj_file<size, false>* object;
1009 // The local symbol index in the object.
1014 Output_data_got<64, false>* got_;
1016 Output_data_plt_x86_64<size>* plt_;
1017 // The GOT PLT section.
1018 Output_data_got_plt_x86_64* got_plt_;
1019 // The GOT section for IRELATIVE relocations.
1020 Output_data_space* got_irelative_;
1021 // The GOT section for TLSDESC relocations.
1022 Output_data_got<64, false>* got_tlsdesc_;
1023 // The _GLOBAL_OFFSET_TABLE_ symbol.
1024 Symbol* global_offset_table_;
1025 // The dynamic reloc section.
1026 Reloc_section* rela_dyn_;
1027 // The section to use for IRELATIVE relocs.
1028 Reloc_section* rela_irelative_;
1029 // Relocs saved to avoid a COPY reloc.
1030 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1031 // Offset of the GOT entry for the TLS module index.
1032 unsigned int got_mod_index_offset_;
1033 // We handle R_X86_64_TLSDESC against a local symbol as a target
1034 // specific relocation. Here we store the object and local symbol
1035 // index for the relocation.
1036 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1037 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1038 bool tls_base_symbol_defined_;
1042 const Target::Target_info Target_x86_64<64>::x86_64_info =
1045 false, // is_big_endian
1046 elfcpp::EM_X86_64, // machine_code
1047 false, // has_make_symbol
1048 false, // has_resolve
1049 true, // has_code_fill
1050 true, // is_default_stack_executable
1051 true, // can_icf_inline_merge_sections
1053 "/lib/ld64.so.1", // program interpreter
1054 0x400000, // default_text_segment_address
1055 0x1000, // abi_pagesize (overridable by -z max-page-size)
1056 0x1000, // common_pagesize (overridable by -z common-page-size)
1057 false, // isolate_execinstr
1059 elfcpp::SHN_UNDEF, // small_common_shndx
1060 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1061 0, // small_common_section_flags
1062 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1063 NULL, // attributes_section
1064 NULL, // attributes_vendor
1065 "_start", // entry_symbol_name
1066 32, // hash_entry_size
1070 const Target::Target_info Target_x86_64<32>::x86_64_info =
1073 false, // is_big_endian
1074 elfcpp::EM_X86_64, // machine_code
1075 false, // has_make_symbol
1076 false, // has_resolve
1077 true, // has_code_fill
1078 true, // is_default_stack_executable
1079 true, // can_icf_inline_merge_sections
1081 "/libx32/ldx32.so.1", // program interpreter
1082 0x400000, // default_text_segment_address
1083 0x1000, // abi_pagesize (overridable by -z max-page-size)
1084 0x1000, // common_pagesize (overridable by -z common-page-size)
1085 false, // isolate_execinstr
1087 elfcpp::SHN_UNDEF, // small_common_shndx
1088 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1089 0, // small_common_section_flags
1090 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1091 NULL, // attributes_section
1092 NULL, // attributes_vendor
1093 "_start", // entry_symbol_name
1094 32, // hash_entry_size
1097 // This is called when a new output section is created. This is where
1098 // we handle the SHF_X86_64_LARGE.
1102 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1104 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1105 os->set_is_large_section();
1108 // Get the GOT section, creating it if necessary.
1111 Output_data_got<64, false>*
1112 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1114 if (this->got_ == NULL)
1116 gold_assert(symtab != NULL && layout != NULL);
1118 // When using -z now, we can treat .got.plt as a relro section.
1119 // Without -z now, it is modified after program startup by lazy
1121 bool is_got_plt_relro = parameters->options().now();
1122 Output_section_order got_order = (is_got_plt_relro
1124 : ORDER_RELRO_LAST);
1125 Output_section_order got_plt_order = (is_got_plt_relro
1127 : ORDER_NON_RELRO_FIRST);
1129 this->got_ = new Output_data_got<64, false>();
1131 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1133 | elfcpp::SHF_WRITE),
1134 this->got_, got_order, true);
1136 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1137 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1139 | elfcpp::SHF_WRITE),
1140 this->got_plt_, got_plt_order,
1143 // The first three entries are reserved.
1144 this->got_plt_->set_current_data_size(3 * 8);
1146 if (!is_got_plt_relro)
1148 // Those bytes can go into the relro segment.
1149 layout->increase_relro(3 * 8);
1152 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1153 this->global_offset_table_ =
1154 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1155 Symbol_table::PREDEFINED,
1157 0, 0, elfcpp::STT_OBJECT,
1159 elfcpp::STV_HIDDEN, 0,
1162 // If there are any IRELATIVE relocations, they get GOT entries
1163 // in .got.plt after the jump slot entries.
1164 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1165 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1167 | elfcpp::SHF_WRITE),
1168 this->got_irelative_,
1169 got_plt_order, is_got_plt_relro);
1171 // If there are any TLSDESC relocations, they get GOT entries in
1172 // .got.plt after the jump slot and IRELATIVE entries.
1173 this->got_tlsdesc_ = new Output_data_got<64, false>();
1174 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1176 | elfcpp::SHF_WRITE),
1178 got_plt_order, is_got_plt_relro);
1184 // Get the dynamic reloc section, creating it if necessary.
1187 typename Target_x86_64<size>::Reloc_section*
1188 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1190 if (this->rela_dyn_ == NULL)
1192 gold_assert(layout != NULL);
1193 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1194 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1195 elfcpp::SHF_ALLOC, this->rela_dyn_,
1196 ORDER_DYNAMIC_RELOCS, false);
1198 return this->rela_dyn_;
1201 // Get the section to use for IRELATIVE relocs, creating it if
1202 // necessary. These go in .rela.dyn, but only after all other dynamic
1203 // relocations. They need to follow the other dynamic relocations so
1204 // that they can refer to global variables initialized by those
1208 typename Target_x86_64<size>::Reloc_section*
1209 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1211 if (this->rela_irelative_ == NULL)
1213 // Make sure we have already created the dynamic reloc section.
1214 this->rela_dyn_section(layout);
1215 this->rela_irelative_ = new Reloc_section(false);
1216 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1217 elfcpp::SHF_ALLOC, this->rela_irelative_,
1218 ORDER_DYNAMIC_RELOCS, false);
1219 gold_assert(this->rela_dyn_->output_section()
1220 == this->rela_irelative_->output_section());
1222 return this->rela_irelative_;
1225 // Write the first three reserved words of the .got.plt section.
1226 // The remainder of the section is written while writing the PLT
1227 // in Output_data_plt_i386::do_write.
1230 Output_data_got_plt_x86_64::do_write(Output_file* of)
1232 // The first entry in the GOT is the address of the .dynamic section
1233 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1234 // We saved space for them when we created the section in
1235 // Target_x86_64::got_section.
1236 const off_t got_file_offset = this->offset();
1237 gold_assert(this->data_size() >= 24);
1238 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1239 Output_section* dynamic = this->layout_->dynamic_section();
1240 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1241 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1242 memset(got_view + 8, 0, 16);
1243 of->write_output_view(got_file_offset, 24, got_view);
1246 // Initialize the PLT section.
1250 Output_data_plt_x86_64<size>::init(Layout* layout)
1252 this->rel_ = new Reloc_section(false);
1253 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1254 elfcpp::SHF_ALLOC, this->rel_,
1255 ORDER_DYNAMIC_PLT_RELOCS, false);
1260 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1262 os->set_entsize(this->get_plt_entry_size());
1265 // Add an entry to the PLT.
1269 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1272 gold_assert(!gsym->has_plt_offset());
1274 unsigned int plt_index;
1276 section_offset_type got_offset;
1278 unsigned int* pcount;
1279 unsigned int offset;
1280 unsigned int reserved;
1281 Output_section_data_build* got;
1282 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1283 && gsym->can_use_relative_reloc(false))
1285 pcount = &this->irelative_count_;
1288 got = this->got_irelative_;
1292 pcount = &this->count_;
1295 got = this->got_plt_;
1298 if (!this->is_data_size_valid())
1300 // Note that when setting the PLT offset for a non-IRELATIVE
1301 // entry we skip the initial reserved PLT entry.
1302 plt_index = *pcount + offset;
1303 plt_offset = plt_index * this->get_plt_entry_size();
1307 got_offset = (plt_index - offset + reserved) * 8;
1308 gold_assert(got_offset == got->current_data_size());
1310 // Every PLT entry needs a GOT entry which points back to the PLT
1311 // entry (this will be changed by the dynamic linker, normally
1312 // lazily when the function is called).
1313 got->set_current_data_size(got_offset + 8);
1317 // FIXME: This is probably not correct for IRELATIVE relocs.
1319 // For incremental updates, find an available slot.
1320 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1321 this->get_plt_entry_size(), 0);
1322 if (plt_offset == -1)
1323 gold_fallback(_("out of patch space (PLT);"
1324 " relink with --incremental-full"));
1326 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1327 // can be calculated from the PLT index, adjusting for the three
1328 // reserved entries at the beginning of the GOT.
1329 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1330 got_offset = (plt_index - offset + reserved) * 8;
1333 gsym->set_plt_offset(plt_offset);
1335 // Every PLT entry needs a reloc.
1336 this->add_relocation(symtab, layout, gsym, got_offset);
1338 // Note that we don't need to save the symbol. The contents of the
1339 // PLT are independent of which symbols are used. The symbols only
1340 // appear in the relocations.
1343 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1348 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1349 Symbol_table* symtab,
1351 Sized_relobj_file<size, false>* relobj,
1352 unsigned int local_sym_index)
1354 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1355 ++this->irelative_count_;
1357 section_offset_type got_offset = this->got_irelative_->current_data_size();
1359 // Every PLT entry needs a GOT entry which points back to the PLT
1361 this->got_irelative_->set_current_data_size(got_offset + 8);
1363 // Every PLT entry needs a reloc.
1364 Reloc_section* rela = this->rela_irelative(symtab, layout);
1365 rela->add_symbolless_local_addend(relobj, local_sym_index,
1366 elfcpp::R_X86_64_IRELATIVE,
1367 this->got_irelative_, got_offset, 0);
1372 // Add the relocation for a PLT entry.
1376 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1379 unsigned int got_offset)
1381 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1382 && gsym->can_use_relative_reloc(false))
1384 Reloc_section* rela = this->rela_irelative(symtab, layout);
1385 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1386 this->got_irelative_, got_offset, 0);
1390 gsym->set_needs_dynsym_entry();
1391 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1396 // Return where the TLSDESC relocations should go, creating it if
1397 // necessary. These follow the JUMP_SLOT relocations.
1400 typename Output_data_plt_x86_64<size>::Reloc_section*
1401 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1403 if (this->tlsdesc_rel_ == NULL)
1405 this->tlsdesc_rel_ = new Reloc_section(false);
1406 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1407 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1408 ORDER_DYNAMIC_PLT_RELOCS, false);
1409 gold_assert(this->tlsdesc_rel_->output_section()
1410 == this->rel_->output_section());
1412 return this->tlsdesc_rel_;
1415 // Return where the IRELATIVE relocations should go in the PLT. These
1416 // follow the JUMP_SLOT and the TLSDESC relocations.
1419 typename Output_data_plt_x86_64<size>::Reloc_section*
1420 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1423 if (this->irelative_rel_ == NULL)
1425 // Make sure we have a place for the TLSDESC relocations, in
1426 // case we see any later on.
1427 this->rela_tlsdesc(layout);
1428 this->irelative_rel_ = new Reloc_section(false);
1429 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1430 elfcpp::SHF_ALLOC, this->irelative_rel_,
1431 ORDER_DYNAMIC_PLT_RELOCS, false);
1432 gold_assert(this->irelative_rel_->output_section()
1433 == this->rel_->output_section());
1435 if (parameters->doing_static_link())
1437 // A statically linked executable will only have a .rela.plt
1438 // section to hold R_X86_64_IRELATIVE relocs for
1439 // STT_GNU_IFUNC symbols. The library will use these
1440 // symbols to locate the IRELATIVE relocs at program startup
1442 symtab->define_in_output_data("__rela_iplt_start", NULL,
1443 Symbol_table::PREDEFINED,
1444 this->irelative_rel_, 0, 0,
1445 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1446 elfcpp::STV_HIDDEN, 0, false, true);
1447 symtab->define_in_output_data("__rela_iplt_end", NULL,
1448 Symbol_table::PREDEFINED,
1449 this->irelative_rel_, 0, 0,
1450 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1451 elfcpp::STV_HIDDEN, 0, true, true);
1454 return this->irelative_rel_;
1457 // Return the PLT address to use for a global symbol.
1461 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1463 uint64_t offset = 0;
1464 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1465 && gsym->can_use_relative_reloc(false))
1466 offset = (this->count_ + 1) * this->get_plt_entry_size();
1467 return this->address() + offset + gsym->plt_offset();
1470 // Return the PLT address to use for a local symbol. These are always
1471 // IRELATIVE relocs.
1475 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
1478 return (this->address()
1479 + (this->count_ + 1) * this->get_plt_entry_size()
1480 + object->local_plt_offset(r_sym));
1483 // Set the final size.
1486 Output_data_plt_x86_64<size>::set_final_data_size()
1488 unsigned int count = this->count_ + this->irelative_count_;
1489 if (this->has_tlsdesc_entry())
1491 this->set_data_size((count + 1) * this->get_plt_entry_size());
1494 // The first entry in the PLT for an executable.
1498 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1500 // From AMD64 ABI Draft 0.98, page 76
1501 0xff, 0x35, // pushq contents of memory address
1502 0, 0, 0, 0, // replaced with address of .got + 8
1503 0xff, 0x25, // jmp indirect
1504 0, 0, 0, 0, // replaced with address of .got + 16
1505 0x90, 0x90, 0x90, 0x90 // noop (x4)
1510 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1512 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1513 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1515 memcpy(pov, first_plt_entry, plt_entry_size);
1516 // We do a jmp relative to the PC at the end of this instruction.
1517 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1519 - (plt_address + 6)));
1520 elfcpp::Swap<32, false>::writeval(pov + 8,
1522 - (plt_address + 12)));
1525 // Subsequent entries in the PLT for an executable.
1529 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1531 // From AMD64 ABI Draft 0.98, page 76
1532 0xff, 0x25, // jmpq indirect
1533 0, 0, 0, 0, // replaced with address of symbol in .got
1534 0x68, // pushq immediate
1535 0, 0, 0, 0, // replaced with offset into relocation table
1536 0xe9, // jmpq relative
1537 0, 0, 0, 0 // replaced with offset to start of .plt
1542 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1544 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1545 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1546 unsigned int got_offset,
1547 unsigned int plt_offset,
1548 unsigned int plt_index)
1550 // Check PC-relative offset overflow in PLT entry.
1551 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1552 - (plt_address + plt_offset + 6));
1553 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1554 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1557 memcpy(pov, plt_entry, plt_entry_size);
1558 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1559 plt_got_pcrel_offset);
1561 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1562 elfcpp::Swap<32, false>::writeval(pov + 12,
1563 - (plt_offset + plt_entry_size));
1568 // The reserved TLSDESC entry in the PLT for an executable.
1572 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1574 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1575 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1576 0xff, 0x35, // pushq x(%rip)
1577 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1578 0xff, 0x25, // jmpq *y(%rip)
1579 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1586 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1588 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1589 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1590 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1591 unsigned int tlsdesc_got_offset,
1592 unsigned int plt_offset)
1594 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1595 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1597 - (plt_address + plt_offset
1599 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1601 + tlsdesc_got_offset
1602 - (plt_address + plt_offset
1606 // The .eh_frame unwind information for the PLT.
1610 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1613 'z', // Augmentation: augmentation size included.
1614 'R', // Augmentation: FDE encoding included.
1615 '\0', // End of augmentation string.
1616 1, // Code alignment factor.
1617 0x78, // Data alignment factor.
1618 16, // Return address column.
1619 1, // Augmentation size.
1620 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1621 | elfcpp::DW_EH_PE_sdata4),
1622 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1623 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1624 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1630 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1632 0, 0, 0, 0, // Replaced with offset to .plt.
1633 0, 0, 0, 0, // Replaced with size of .plt.
1634 0, // Augmentation size.
1635 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1636 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1637 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1638 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1639 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1640 11, // Block length.
1641 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1642 elfcpp::DW_OP_breg16, 0, // Push %rip.
1643 elfcpp::DW_OP_lit15, // Push 0xf.
1644 elfcpp::DW_OP_and, // & (%rip & 0xf).
1645 elfcpp::DW_OP_lit11, // Push 0xb.
1646 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1647 elfcpp::DW_OP_lit3, // Push 3.
1648 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1649 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1650 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1656 // Write out the PLT. This uses the hand-coded instructions above,
1657 // and adjusts them as needed. This is specified by the AMD64 ABI.
1661 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1663 const off_t offset = this->offset();
1664 const section_size_type oview_size =
1665 convert_to_section_size_type(this->data_size());
1666 unsigned char* const oview = of->get_output_view(offset, oview_size);
1668 const off_t got_file_offset = this->got_plt_->offset();
1669 gold_assert(parameters->incremental_update()
1670 || (got_file_offset + this->got_plt_->data_size()
1671 == this->got_irelative_->offset()));
1672 const section_size_type got_size =
1673 convert_to_section_size_type(this->got_plt_->data_size()
1674 + this->got_irelative_->data_size());
1675 unsigned char* const got_view = of->get_output_view(got_file_offset,
1678 unsigned char* pov = oview;
1680 // The base address of the .plt section.
1681 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1682 // The base address of the .got section.
1683 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1684 // The base address of the PLT portion of the .got section,
1685 // which is where the GOT pointer will point, and where the
1686 // three reserved GOT entries are located.
1687 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1688 = this->got_plt_->address();
1690 this->fill_first_plt_entry(pov, got_address, plt_address);
1691 pov += this->get_plt_entry_size();
1693 // The first three entries in the GOT are reserved, and are written
1694 // by Output_data_got_plt_x86_64::do_write.
1695 unsigned char* got_pov = got_view + 24;
1697 unsigned int plt_offset = this->get_plt_entry_size();
1698 unsigned int got_offset = 24;
1699 const unsigned int count = this->count_ + this->irelative_count_;
1700 for (unsigned int plt_index = 0;
1703 pov += this->get_plt_entry_size(),
1705 plt_offset += this->get_plt_entry_size(),
1708 // Set and adjust the PLT entry itself.
1709 unsigned int lazy_offset = this->fill_plt_entry(pov,
1710 got_address, plt_address,
1711 got_offset, plt_offset,
1714 // Set the entry in the GOT.
1715 elfcpp::Swap<64, false>::writeval(got_pov,
1716 plt_address + plt_offset + lazy_offset);
1719 if (this->has_tlsdesc_entry())
1721 // Set and adjust the reserved TLSDESC PLT entry.
1722 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1723 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
1724 tlsdesc_got_offset, plt_offset);
1725 pov += this->get_plt_entry_size();
1728 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1729 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1731 of->write_output_view(offset, oview_size, oview);
1732 of->write_output_view(got_file_offset, got_size, got_view);
1735 // Create the PLT section.
1739 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1741 if (this->plt_ == NULL)
1743 // Create the GOT sections first.
1744 this->got_section(symtab, layout);
1746 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
1747 this->got_irelative_);
1749 // Add unwind information if requested.
1750 if (parameters->options().ld_generated_unwind_info())
1751 this->plt_->add_eh_frame(layout);
1753 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1755 | elfcpp::SHF_EXECINSTR),
1756 this->plt_, ORDER_PLT, false);
1758 // Make the sh_info field of .rela.plt point to .plt.
1759 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1760 rela_plt_os->set_info_section(this->plt_->output_section());
1764 // Return the section for TLSDESC relocations.
1767 typename Target_x86_64<size>::Reloc_section*
1768 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1770 return this->plt_section()->rela_tlsdesc(layout);
1773 // Create a PLT entry for a global symbol.
1777 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1780 if (gsym->has_plt_offset())
1783 if (this->plt_ == NULL)
1784 this->make_plt_section(symtab, layout);
1786 this->plt_->add_entry(symtab, layout, gsym);
1789 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1793 Target_x86_64<size>::make_local_ifunc_plt_entry(
1794 Symbol_table* symtab, Layout* layout,
1795 Sized_relobj_file<size, false>* relobj,
1796 unsigned int local_sym_index)
1798 if (relobj->local_has_plt_offset(local_sym_index))
1800 if (this->plt_ == NULL)
1801 this->make_plt_section(symtab, layout);
1802 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1805 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1808 // Return the number of entries in the PLT.
1812 Target_x86_64<size>::plt_entry_count() const
1814 if (this->plt_ == NULL)
1816 return this->plt_->entry_count();
1819 // Return the offset of the first non-reserved PLT entry.
1823 Target_x86_64<size>::first_plt_entry_offset() const
1825 return this->plt_->first_plt_entry_offset();
1828 // Return the size of each PLT entry.
1832 Target_x86_64<size>::plt_entry_size() const
1834 return this->plt_->get_plt_entry_size();
1837 // Create the GOT and PLT sections for an incremental update.
1840 Output_data_got_base*
1841 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1843 unsigned int got_count,
1844 unsigned int plt_count)
1846 gold_assert(this->got_ == NULL);
1848 this->got_ = new Output_data_got<64, false>(got_count * 8);
1849 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1851 | elfcpp::SHF_WRITE),
1852 this->got_, ORDER_RELRO_LAST,
1855 // Add the three reserved entries.
1856 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
1857 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1859 | elfcpp::SHF_WRITE),
1860 this->got_plt_, ORDER_NON_RELRO_FIRST,
1863 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1864 this->global_offset_table_ =
1865 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1866 Symbol_table::PREDEFINED,
1868 0, 0, elfcpp::STT_OBJECT,
1870 elfcpp::STV_HIDDEN, 0,
1873 // If there are any TLSDESC relocations, they get GOT entries in
1874 // .got.plt after the jump slot entries.
1875 // FIXME: Get the count for TLSDESC entries.
1876 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1877 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1878 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1880 ORDER_NON_RELRO_FIRST, false);
1882 // If there are any IRELATIVE relocations, they get GOT entries in
1883 // .got.plt after the jump slot and TLSDESC entries.
1884 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1885 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1886 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1887 this->got_irelative_,
1888 ORDER_NON_RELRO_FIRST, false);
1890 // Create the PLT section.
1891 this->plt_ = this->make_data_plt(layout, this->got_,
1893 this->got_irelative_,
1896 // Add unwind information if requested.
1897 if (parameters->options().ld_generated_unwind_info())
1898 this->plt_->add_eh_frame(layout);
1900 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1901 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1902 this->plt_, ORDER_PLT, false);
1904 // Make the sh_info field of .rela.plt point to .plt.
1905 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1906 rela_plt_os->set_info_section(this->plt_->output_section());
1908 // Create the rela_dyn section.
1909 this->rela_dyn_section(layout);
1914 // Reserve a GOT entry for a local symbol, and regenerate any
1915 // necessary dynamic relocations.
1919 Target_x86_64<size>::reserve_local_got_entry(
1920 unsigned int got_index,
1921 Sized_relobj<size, false>* obj,
1923 unsigned int got_type)
1925 unsigned int got_offset = got_index * 8;
1926 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1928 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1931 case GOT_TYPE_STANDARD:
1932 if (parameters->options().output_is_position_independent())
1933 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1934 this->got_, got_offset, 0, false);
1936 case GOT_TYPE_TLS_OFFSET:
1937 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1938 this->got_, got_offset, 0);
1940 case GOT_TYPE_TLS_PAIR:
1941 this->got_->reserve_slot(got_index + 1);
1942 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1943 this->got_, got_offset, 0);
1945 case GOT_TYPE_TLS_DESC:
1946 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1947 // this->got_->reserve_slot(got_index + 1);
1948 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1949 // this->got_, got_offset, 0);
1956 // Reserve a GOT entry for a global symbol, and regenerate any
1957 // necessary dynamic relocations.
1961 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1963 unsigned int got_type)
1965 unsigned int got_offset = got_index * 8;
1966 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1968 this->got_->reserve_global(got_index, gsym, got_type);
1971 case GOT_TYPE_STANDARD:
1972 if (!gsym->final_value_is_known())
1974 if (gsym->is_from_dynobj()
1975 || gsym->is_undefined()
1976 || gsym->is_preemptible()
1977 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1978 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1979 this->got_, got_offset, 0);
1981 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1982 this->got_, got_offset, 0, false);
1985 case GOT_TYPE_TLS_OFFSET:
1986 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1987 this->got_, got_offset, 0, false);
1989 case GOT_TYPE_TLS_PAIR:
1990 this->got_->reserve_slot(got_index + 1);
1991 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1992 this->got_, got_offset, 0, false);
1993 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1994 this->got_, got_offset + 8, 0, false);
1996 case GOT_TYPE_TLS_DESC:
1997 this->got_->reserve_slot(got_index + 1);
1998 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1999 this->got_, got_offset, 0, false);
2006 // Register an existing PLT entry for a global symbol.
2010 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2012 unsigned int plt_index,
2015 gold_assert(this->plt_ != NULL);
2016 gold_assert(!gsym->has_plt_offset());
2018 this->plt_->reserve_slot(plt_index);
2020 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2022 unsigned int got_offset = (plt_index + 3) * 8;
2023 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2026 // Force a COPY relocation for a given symbol.
2030 Target_x86_64<size>::emit_copy_reloc(
2031 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2033 this->copy_relocs_.emit_copy_reloc(symtab,
2034 symtab->get_sized_symbol<size>(sym),
2037 this->rela_dyn_section(NULL));
2040 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2044 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2047 if (this->tls_base_symbol_defined_)
2050 Output_segment* tls_segment = layout->tls_segment();
2051 if (tls_segment != NULL)
2053 bool is_exec = parameters->options().output_is_executable();
2054 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2055 Symbol_table::PREDEFINED,
2059 elfcpp::STV_HIDDEN, 0,
2061 ? Symbol::SEGMENT_END
2062 : Symbol::SEGMENT_START),
2065 this->tls_base_symbol_defined_ = true;
2068 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2072 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2075 if (this->plt_ == NULL)
2076 this->make_plt_section(symtab, layout);
2078 if (!this->plt_->has_tlsdesc_entry())
2080 // Allocate the TLSDESC_GOT entry.
2081 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2082 unsigned int got_offset = got->add_constant(0);
2084 // Allocate the TLSDESC_PLT entry.
2085 this->plt_->reserve_tlsdesc_entry(got_offset);
2089 // Create a GOT entry for the TLS module index.
2093 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2094 Sized_relobj_file<size, false>* object)
2096 if (this->got_mod_index_offset_ == -1U)
2098 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2099 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2100 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2101 unsigned int got_offset = got->add_constant(0);
2102 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2104 got->add_constant(0);
2105 this->got_mod_index_offset_ = got_offset;
2107 return this->got_mod_index_offset_;
2110 // Optimize the TLS relocation type based on what we know about the
2111 // symbol. IS_FINAL is true if the final address of this symbol is
2112 // known at link time.
2115 tls::Tls_optimization
2116 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2118 // If we are generating a shared library, then we can't do anything
2120 if (parameters->options().shared())
2121 return tls::TLSOPT_NONE;
2125 case elfcpp::R_X86_64_TLSGD:
2126 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2127 case elfcpp::R_X86_64_TLSDESC_CALL:
2128 // These are General-Dynamic which permits fully general TLS
2129 // access. Since we know that we are generating an executable,
2130 // we can convert this to Initial-Exec. If we also know that
2131 // this is a local symbol, we can further switch to Local-Exec.
2133 return tls::TLSOPT_TO_LE;
2134 return tls::TLSOPT_TO_IE;
2136 case elfcpp::R_X86_64_TLSLD:
2137 // This is Local-Dynamic, which refers to a local symbol in the
2138 // dynamic TLS block. Since we know that we generating an
2139 // executable, we can switch to Local-Exec.
2140 return tls::TLSOPT_TO_LE;
2142 case elfcpp::R_X86_64_DTPOFF32:
2143 case elfcpp::R_X86_64_DTPOFF64:
2144 // Another Local-Dynamic reloc.
2145 return tls::TLSOPT_TO_LE;
2147 case elfcpp::R_X86_64_GOTTPOFF:
2148 // These are Initial-Exec relocs which get the thread offset
2149 // from the GOT. If we know that we are linking against the
2150 // local symbol, we can switch to Local-Exec, which links the
2151 // thread offset into the instruction.
2153 return tls::TLSOPT_TO_LE;
2154 return tls::TLSOPT_NONE;
2156 case elfcpp::R_X86_64_TPOFF32:
2157 // When we already have Local-Exec, there is nothing further we
2159 return tls::TLSOPT_NONE;
2166 // Get the Reference_flags for a particular relocation.
2170 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2174 case elfcpp::R_X86_64_NONE:
2175 case elfcpp::R_X86_64_GNU_VTINHERIT:
2176 case elfcpp::R_X86_64_GNU_VTENTRY:
2177 case elfcpp::R_X86_64_GOTPC32:
2178 case elfcpp::R_X86_64_GOTPC64:
2179 // No symbol reference.
2182 case elfcpp::R_X86_64_64:
2183 case elfcpp::R_X86_64_32:
2184 case elfcpp::R_X86_64_32S:
2185 case elfcpp::R_X86_64_16:
2186 case elfcpp::R_X86_64_8:
2187 return Symbol::ABSOLUTE_REF;
2189 case elfcpp::R_X86_64_PC64:
2190 case elfcpp::R_X86_64_PC32:
2191 case elfcpp::R_X86_64_PC32_BND:
2192 case elfcpp::R_X86_64_PC16:
2193 case elfcpp::R_X86_64_PC8:
2194 case elfcpp::R_X86_64_GOTOFF64:
2195 return Symbol::RELATIVE_REF;
2197 case elfcpp::R_X86_64_PLT32:
2198 case elfcpp::R_X86_64_PLT32_BND:
2199 case elfcpp::R_X86_64_PLTOFF64:
2200 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2202 case elfcpp::R_X86_64_GOT64:
2203 case elfcpp::R_X86_64_GOT32:
2204 case elfcpp::R_X86_64_GOTPCREL64:
2205 case elfcpp::R_X86_64_GOTPCREL:
2206 case elfcpp::R_X86_64_GOTPCRELX:
2207 case elfcpp::R_X86_64_REX_GOTPCRELX:
2208 case elfcpp::R_X86_64_GOTPLT64:
2210 return Symbol::ABSOLUTE_REF;
2212 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2213 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2214 case elfcpp::R_X86_64_TLSDESC_CALL:
2215 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2216 case elfcpp::R_X86_64_DTPOFF32:
2217 case elfcpp::R_X86_64_DTPOFF64:
2218 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2219 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2220 return Symbol::TLS_REF;
2222 case elfcpp::R_X86_64_COPY:
2223 case elfcpp::R_X86_64_GLOB_DAT:
2224 case elfcpp::R_X86_64_JUMP_SLOT:
2225 case elfcpp::R_X86_64_RELATIVE:
2226 case elfcpp::R_X86_64_IRELATIVE:
2227 case elfcpp::R_X86_64_TPOFF64:
2228 case elfcpp::R_X86_64_DTPMOD64:
2229 case elfcpp::R_X86_64_TLSDESC:
2230 case elfcpp::R_X86_64_SIZE32:
2231 case elfcpp::R_X86_64_SIZE64:
2233 // Not expected. We will give an error later.
2238 // Report an unsupported relocation against a local symbol.
2242 Target_x86_64<size>::Scan::unsupported_reloc_local(
2243 Sized_relobj_file<size, false>* object,
2244 unsigned int r_type)
2246 gold_error(_("%s: unsupported reloc %u against local symbol"),
2247 object->name().c_str(), r_type);
2250 // We are about to emit a dynamic relocation of type R_TYPE. If the
2251 // dynamic linker does not support it, issue an error. The GNU linker
2252 // only issues a non-PIC error for an allocated read-only section.
2253 // Here we know the section is allocated, but we don't know that it is
2254 // read-only. But we check for all the relocation types which the
2255 // glibc dynamic linker supports, so it seems appropriate to issue an
2256 // error even if the section is not read-only. If GSYM is not NULL,
2257 // it is the symbol the relocation is against; if it is NULL, the
2258 // relocation is against a local symbol.
2262 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2267 // These are the relocation types supported by glibc for x86_64
2268 // which should always work.
2269 case elfcpp::R_X86_64_RELATIVE:
2270 case elfcpp::R_X86_64_IRELATIVE:
2271 case elfcpp::R_X86_64_GLOB_DAT:
2272 case elfcpp::R_X86_64_JUMP_SLOT:
2273 case elfcpp::R_X86_64_DTPMOD64:
2274 case elfcpp::R_X86_64_DTPOFF64:
2275 case elfcpp::R_X86_64_TPOFF64:
2276 case elfcpp::R_X86_64_64:
2277 case elfcpp::R_X86_64_COPY:
2280 // glibc supports these reloc types, but they can overflow.
2281 case elfcpp::R_X86_64_PC32:
2282 case elfcpp::R_X86_64_PC32_BND:
2283 // A PC relative reference is OK against a local symbol or if
2284 // the symbol is defined locally.
2286 || (!gsym->is_from_dynobj()
2287 && !gsym->is_undefined()
2288 && !gsym->is_preemptible()))
2291 case elfcpp::R_X86_64_32:
2292 // R_X86_64_32 is OK for x32.
2293 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2295 if (this->issued_non_pic_error_)
2297 gold_assert(parameters->options().output_is_position_independent());
2299 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2300 "overflow at runtime; recompile with -fPIC"));
2306 case elfcpp::R_X86_64_32:
2307 r_name = "R_X86_64_32";
2309 case elfcpp::R_X86_64_PC32:
2310 r_name = "R_X86_64_PC32";
2312 case elfcpp::R_X86_64_PC32_BND:
2313 r_name = "R_X86_64_PC32_BND";
2319 object->error(_("requires dynamic %s reloc against '%s' "
2320 "which may overflow at runtime; recompile "
2322 r_name, gsym->name());
2324 this->issued_non_pic_error_ = true;
2328 // This prevents us from issuing more than one error per reloc
2329 // section. But we can still wind up issuing more than one
2330 // error per object file.
2331 if (this->issued_non_pic_error_)
2333 gold_assert(parameters->options().output_is_position_independent());
2334 object->error(_("requires unsupported dynamic reloc %u; "
2335 "recompile with -fPIC"),
2337 this->issued_non_pic_error_ = true;
2340 case elfcpp::R_X86_64_NONE:
2345 // Return whether we need to make a PLT entry for a relocation of the
2346 // given type against a STT_GNU_IFUNC symbol.
2350 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2351 Sized_relobj_file<size, false>* object,
2352 unsigned int r_type)
2354 int flags = Scan::get_reference_flags(r_type);
2355 if (flags & Symbol::TLS_REF)
2356 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2357 object->name().c_str(), r_type);
2361 // Scan a relocation for a local symbol.
2365 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2367 Target_x86_64<size>* target,
2368 Sized_relobj_file<size, false>* object,
2369 unsigned int data_shndx,
2370 Output_section* output_section,
2371 const elfcpp::Rela<size, false>& reloc,
2372 unsigned int r_type,
2373 const elfcpp::Sym<size, false>& lsym,
2379 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2380 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2381 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2383 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2384 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2389 case elfcpp::R_X86_64_NONE:
2390 case elfcpp::R_X86_64_GNU_VTINHERIT:
2391 case elfcpp::R_X86_64_GNU_VTENTRY:
2394 case elfcpp::R_X86_64_64:
2395 // If building a shared library (or a position-independent
2396 // executable), we need to create a dynamic relocation for this
2397 // location. The relocation applied at link time will apply the
2398 // link-time value, so we flag the location with an
2399 // R_X86_64_RELATIVE relocation so the dynamic loader can
2400 // relocate it easily.
2401 if (parameters->options().output_is_position_independent())
2403 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2404 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2405 rela_dyn->add_local_relative(object, r_sym,
2407 ? elfcpp::R_X86_64_RELATIVE64
2408 : elfcpp::R_X86_64_RELATIVE),
2409 output_section, data_shndx,
2410 reloc.get_r_offset(),
2411 reloc.get_r_addend(), is_ifunc);
2415 case elfcpp::R_X86_64_32:
2416 case elfcpp::R_X86_64_32S:
2417 case elfcpp::R_X86_64_16:
2418 case elfcpp::R_X86_64_8:
2419 // If building a shared library (or a position-independent
2420 // executable), we need to create a dynamic relocation for this
2421 // location. We can't use an R_X86_64_RELATIVE relocation
2422 // because that is always a 64-bit relocation.
2423 if (parameters->options().output_is_position_independent())
2425 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2426 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2428 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2429 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2430 rela_dyn->add_local_relative(object, r_sym,
2431 elfcpp::R_X86_64_RELATIVE,
2432 output_section, data_shndx,
2433 reloc.get_r_offset(),
2434 reloc.get_r_addend(), is_ifunc);
2438 this->check_non_pic(object, r_type, NULL);
2440 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2441 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2442 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2443 rela_dyn->add_local(object, r_sym, r_type, output_section,
2444 data_shndx, reloc.get_r_offset(),
2445 reloc.get_r_addend());
2448 gold_assert(lsym.get_st_value() == 0);
2449 unsigned int shndx = lsym.get_st_shndx();
2451 shndx = object->adjust_sym_shndx(r_sym, shndx,
2454 object->error(_("section symbol %u has bad shndx %u"),
2457 rela_dyn->add_local_section(object, shndx,
2458 r_type, output_section,
2459 data_shndx, reloc.get_r_offset(),
2460 reloc.get_r_addend());
2465 case elfcpp::R_X86_64_PC64:
2466 case elfcpp::R_X86_64_PC32:
2467 case elfcpp::R_X86_64_PC32_BND:
2468 case elfcpp::R_X86_64_PC16:
2469 case elfcpp::R_X86_64_PC8:
2472 case elfcpp::R_X86_64_PLT32:
2473 case elfcpp::R_X86_64_PLT32_BND:
2474 // Since we know this is a local symbol, we can handle this as a
2478 case elfcpp::R_X86_64_GOTPC32:
2479 case elfcpp::R_X86_64_GOTOFF64:
2480 case elfcpp::R_X86_64_GOTPC64:
2481 case elfcpp::R_X86_64_PLTOFF64:
2482 // We need a GOT section.
2483 target->got_section(symtab, layout);
2484 // For PLTOFF64, we'd normally want a PLT section, but since we
2485 // know this is a local symbol, no PLT is needed.
2488 case elfcpp::R_X86_64_GOT64:
2489 case elfcpp::R_X86_64_GOT32:
2490 case elfcpp::R_X86_64_GOTPCREL64:
2491 case elfcpp::R_X86_64_GOTPCREL:
2492 case elfcpp::R_X86_64_GOTPCRELX:
2493 case elfcpp::R_X86_64_REX_GOTPCRELX:
2494 case elfcpp::R_X86_64_GOTPLT64:
2496 // The symbol requires a GOT section.
2497 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2499 // If the relocation symbol isn't IFUNC,
2500 // and is local, then we will convert
2501 // mov foo@GOTPCREL(%rip), %reg
2502 // to lea foo(%rip), %reg.
2503 // in Relocate::relocate.
2504 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2505 || r_type == elfcpp::R_X86_64_GOTPCRELX
2506 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2507 && reloc.get_r_offset() >= 2
2510 section_size_type stype;
2511 const unsigned char* view = object->section_contents(data_shndx,
2513 if (view[reloc.get_r_offset() - 2] == 0x8b)
2518 // The symbol requires a GOT entry.
2519 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2521 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2522 // lets function pointers compare correctly with shared
2523 // libraries. Otherwise we would need an IRELATIVE reloc.
2526 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2528 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2531 // If we are generating a shared object, we need to add a
2532 // dynamic relocation for this symbol's GOT entry.
2533 if (parameters->options().output_is_position_independent())
2535 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2536 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2537 if (r_type != elfcpp::R_X86_64_GOT32)
2539 unsigned int got_offset =
2540 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2541 rela_dyn->add_local_relative(object, r_sym,
2542 elfcpp::R_X86_64_RELATIVE,
2543 got, got_offset, 0, is_ifunc);
2547 this->check_non_pic(object, r_type, NULL);
2549 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2550 rela_dyn->add_local(
2551 object, r_sym, r_type, got,
2552 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2556 // For GOTPLT64, we'd normally want a PLT section, but since
2557 // we know this is a local symbol, no PLT is needed.
2561 case elfcpp::R_X86_64_COPY:
2562 case elfcpp::R_X86_64_GLOB_DAT:
2563 case elfcpp::R_X86_64_JUMP_SLOT:
2564 case elfcpp::R_X86_64_RELATIVE:
2565 case elfcpp::R_X86_64_IRELATIVE:
2566 // These are outstanding tls relocs, which are unexpected when linking
2567 case elfcpp::R_X86_64_TPOFF64:
2568 case elfcpp::R_X86_64_DTPMOD64:
2569 case elfcpp::R_X86_64_TLSDESC:
2570 gold_error(_("%s: unexpected reloc %u in object file"),
2571 object->name().c_str(), r_type);
2574 // These are initial tls relocs, which are expected when linking
2575 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2576 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2577 case elfcpp::R_X86_64_TLSDESC_CALL:
2578 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2579 case elfcpp::R_X86_64_DTPOFF32:
2580 case elfcpp::R_X86_64_DTPOFF64:
2581 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2582 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2584 bool output_is_shared = parameters->options().shared();
2585 const tls::Tls_optimization optimized_type
2586 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2590 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2591 if (optimized_type == tls::TLSOPT_NONE)
2593 // Create a pair of GOT entries for the module index and
2594 // dtv-relative offset.
2595 Output_data_got<64, false>* got
2596 = target->got_section(symtab, layout);
2597 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2598 unsigned int shndx = lsym.get_st_shndx();
2600 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2602 object->error(_("local symbol %u has bad shndx %u"),
2605 got->add_local_pair_with_rel(object, r_sym,
2608 target->rela_dyn_section(layout),
2609 elfcpp::R_X86_64_DTPMOD64);
2611 else if (optimized_type != tls::TLSOPT_TO_LE)
2612 unsupported_reloc_local(object, r_type);
2615 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2616 target->define_tls_base_symbol(symtab, layout);
2617 if (optimized_type == tls::TLSOPT_NONE)
2619 // Create reserved PLT and GOT entries for the resolver.
2620 target->reserve_tlsdesc_entries(symtab, layout);
2622 // Generate a double GOT entry with an
2623 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2624 // is resolved lazily, so the GOT entry needs to be in
2625 // an area in .got.plt, not .got. Call got_section to
2626 // make sure the section has been created.
2627 target->got_section(symtab, layout);
2628 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2629 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2630 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2632 unsigned int got_offset = got->add_constant(0);
2633 got->add_constant(0);
2634 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2636 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2637 // We store the arguments we need in a vector, and
2638 // use the index into the vector as the parameter
2639 // to pass to the target specific routines.
2640 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2641 void* arg = reinterpret_cast<void*>(intarg);
2642 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2643 got, got_offset, 0);
2646 else if (optimized_type != tls::TLSOPT_TO_LE)
2647 unsupported_reloc_local(object, r_type);
2650 case elfcpp::R_X86_64_TLSDESC_CALL:
2653 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2654 if (optimized_type == tls::TLSOPT_NONE)
2656 // Create a GOT entry for the module index.
2657 target->got_mod_index_entry(symtab, layout, object);
2659 else if (optimized_type != tls::TLSOPT_TO_LE)
2660 unsupported_reloc_local(object, r_type);
2663 case elfcpp::R_X86_64_DTPOFF32:
2664 case elfcpp::R_X86_64_DTPOFF64:
2667 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2668 layout->set_has_static_tls();
2669 if (optimized_type == tls::TLSOPT_NONE)
2671 // Create a GOT entry for the tp-relative offset.
2672 Output_data_got<64, false>* got
2673 = target->got_section(symtab, layout);
2674 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2675 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2676 target->rela_dyn_section(layout),
2677 elfcpp::R_X86_64_TPOFF64);
2679 else if (optimized_type != tls::TLSOPT_TO_LE)
2680 unsupported_reloc_local(object, r_type);
2683 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2684 layout->set_has_static_tls();
2685 if (output_is_shared)
2686 unsupported_reloc_local(object, r_type);
2695 case elfcpp::R_X86_64_SIZE32:
2696 case elfcpp::R_X86_64_SIZE64:
2698 gold_error(_("%s: unsupported reloc %u against local symbol"),
2699 object->name().c_str(), r_type);
2705 // Report an unsupported relocation against a global symbol.
2709 Target_x86_64<size>::Scan::unsupported_reloc_global(
2710 Sized_relobj_file<size, false>* object,
2711 unsigned int r_type,
2714 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2715 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2718 // Returns true if this relocation type could be that of a function pointer.
2721 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2725 case elfcpp::R_X86_64_64:
2726 case elfcpp::R_X86_64_32:
2727 case elfcpp::R_X86_64_32S:
2728 case elfcpp::R_X86_64_16:
2729 case elfcpp::R_X86_64_8:
2730 case elfcpp::R_X86_64_GOT64:
2731 case elfcpp::R_X86_64_GOT32:
2732 case elfcpp::R_X86_64_GOTPCREL64:
2733 case elfcpp::R_X86_64_GOTPCREL:
2734 case elfcpp::R_X86_64_GOTPCRELX:
2735 case elfcpp::R_X86_64_REX_GOTPCRELX:
2736 case elfcpp::R_X86_64_GOTPLT64:
2744 // For safe ICF, scan a relocation for a local symbol to check if it
2745 // corresponds to a function pointer being taken. In that case mark
2746 // the function whose pointer was taken as not foldable.
2750 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2753 Target_x86_64<size>* ,
2754 Sized_relobj_file<size, false>* ,
2757 const elfcpp::Rela<size, false>& ,
2758 unsigned int r_type,
2759 const elfcpp::Sym<size, false>&)
2761 // When building a shared library, do not fold any local symbols as it is
2762 // not possible to distinguish pointer taken versus a call by looking at
2763 // the relocation types.
2764 return (parameters->options().shared()
2765 || possible_function_pointer_reloc(r_type));
2768 // For safe ICF, scan a relocation for a global symbol to check if it
2769 // corresponds to a function pointer being taken. In that case mark
2770 // the function whose pointer was taken as not foldable.
2774 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2777 Target_x86_64<size>* ,
2778 Sized_relobj_file<size, false>* ,
2781 const elfcpp::Rela<size, false>& ,
2782 unsigned int r_type,
2785 // When building a shared library, do not fold symbols whose visibility
2786 // is hidden, internal or protected.
2787 return ((parameters->options().shared()
2788 && (gsym->visibility() == elfcpp::STV_INTERNAL
2789 || gsym->visibility() == elfcpp::STV_PROTECTED
2790 || gsym->visibility() == elfcpp::STV_HIDDEN))
2791 || possible_function_pointer_reloc(r_type));
2794 // Scan a relocation for a global symbol.
2798 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2800 Target_x86_64<size>* target,
2801 Sized_relobj_file<size, false>* object,
2802 unsigned int data_shndx,
2803 Output_section* output_section,
2804 const elfcpp::Rela<size, false>& reloc,
2805 unsigned int r_type,
2808 // A STT_GNU_IFUNC symbol may require a PLT entry.
2809 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2810 && this->reloc_needs_plt_for_ifunc(object, r_type))
2811 target->make_plt_entry(symtab, layout, gsym);
2815 case elfcpp::R_X86_64_NONE:
2816 case elfcpp::R_X86_64_GNU_VTINHERIT:
2817 case elfcpp::R_X86_64_GNU_VTENTRY:
2820 case elfcpp::R_X86_64_64:
2821 case elfcpp::R_X86_64_32:
2822 case elfcpp::R_X86_64_32S:
2823 case elfcpp::R_X86_64_16:
2824 case elfcpp::R_X86_64_8:
2826 // Make a PLT entry if necessary.
2827 if (gsym->needs_plt_entry())
2829 target->make_plt_entry(symtab, layout, gsym);
2830 // Since this is not a PC-relative relocation, we may be
2831 // taking the address of a function. In that case we need to
2832 // set the entry in the dynamic symbol table to the address of
2834 if (gsym->is_from_dynobj() && !parameters->options().shared())
2835 gsym->set_needs_dynsym_value();
2837 // Make a dynamic relocation if necessary.
2838 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2840 if (!parameters->options().output_is_position_independent()
2841 && gsym->may_need_copy_reloc())
2843 target->copy_reloc(symtab, layout, object,
2844 data_shndx, output_section, gsym, reloc);
2846 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2847 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2848 && gsym->type() == elfcpp::STT_GNU_IFUNC
2849 && gsym->can_use_relative_reloc(false)
2850 && !gsym->is_from_dynobj()
2851 && !gsym->is_undefined()
2852 && !gsym->is_preemptible())
2854 // Use an IRELATIVE reloc for a locally defined
2855 // STT_GNU_IFUNC symbol. This makes a function
2856 // address in a PIE executable match the address in a
2857 // shared library that it links against.
2858 Reloc_section* rela_dyn =
2859 target->rela_irelative_section(layout);
2860 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2861 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2862 output_section, object,
2864 reloc.get_r_offset(),
2865 reloc.get_r_addend());
2867 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2868 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2869 && gsym->can_use_relative_reloc(false))
2871 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2872 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2873 output_section, object,
2875 reloc.get_r_offset(),
2876 reloc.get_r_addend(), false);
2880 this->check_non_pic(object, r_type, gsym);
2881 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2882 rela_dyn->add_global(gsym, r_type, output_section, object,
2883 data_shndx, reloc.get_r_offset(),
2884 reloc.get_r_addend());
2890 case elfcpp::R_X86_64_PC64:
2891 case elfcpp::R_X86_64_PC32:
2892 case elfcpp::R_X86_64_PC32_BND:
2893 case elfcpp::R_X86_64_PC16:
2894 case elfcpp::R_X86_64_PC8:
2896 // Make a PLT entry if necessary.
2897 if (gsym->needs_plt_entry())
2898 target->make_plt_entry(symtab, layout, gsym);
2899 // Make a dynamic relocation if necessary.
2900 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2902 if (parameters->options().output_is_executable()
2903 && gsym->may_need_copy_reloc())
2905 target->copy_reloc(symtab, layout, object,
2906 data_shndx, output_section, gsym, reloc);
2910 this->check_non_pic(object, r_type, gsym);
2911 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2912 rela_dyn->add_global(gsym, r_type, output_section, object,
2913 data_shndx, reloc.get_r_offset(),
2914 reloc.get_r_addend());
2920 case elfcpp::R_X86_64_GOT64:
2921 case elfcpp::R_X86_64_GOT32:
2922 case elfcpp::R_X86_64_GOTPCREL64:
2923 case elfcpp::R_X86_64_GOTPCREL:
2924 case elfcpp::R_X86_64_GOTPCRELX:
2925 case elfcpp::R_X86_64_REX_GOTPCRELX:
2926 case elfcpp::R_X86_64_GOTPLT64:
2928 // The symbol requires a GOT entry.
2929 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2931 // If we convert this from
2932 // mov foo@GOTPCREL(%rip), %reg
2933 // to lea foo(%rip), %reg.
2934 // in Relocate::relocate, then there is nothing to do here.
2935 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2936 || r_type == elfcpp::R_X86_64_GOTPCRELX
2937 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2938 && reloc.get_r_offset() >= 2
2939 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
2941 section_size_type stype;
2942 const unsigned char* view = object->section_contents(data_shndx,
2944 if (view[reloc.get_r_offset() - 2] == 0x8b)
2948 if (gsym->final_value_is_known())
2950 // For a STT_GNU_IFUNC symbol we want the PLT address.
2951 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2952 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2954 got->add_global(gsym, GOT_TYPE_STANDARD);
2958 // If this symbol is not fully resolved, we need to add a
2959 // dynamic relocation for it.
2960 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2962 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2964 // 1) The symbol may be defined in some other module.
2966 // 2) We are building a shared library and this is a
2967 // protected symbol; using GLOB_DAT means that the dynamic
2968 // linker can use the address of the PLT in the main
2969 // executable when appropriate so that function address
2970 // comparisons work.
2972 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2973 // code, again so that function address comparisons work.
2974 if (gsym->is_from_dynobj()
2975 || gsym->is_undefined()
2976 || gsym->is_preemptible()
2977 || (gsym->visibility() == elfcpp::STV_PROTECTED
2978 && parameters->options().shared())
2979 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2980 && parameters->options().output_is_position_independent()))
2981 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2982 elfcpp::R_X86_64_GLOB_DAT);
2985 // For a STT_GNU_IFUNC symbol we want to write the PLT
2986 // offset into the GOT, so that function pointer
2987 // comparisons work correctly.
2989 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2990 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2993 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2994 // Tell the dynamic linker to use the PLT address
2995 // when resolving relocations.
2996 if (gsym->is_from_dynobj()
2997 && !parameters->options().shared())
2998 gsym->set_needs_dynsym_value();
3002 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3003 rela_dyn->add_global_relative(gsym,
3004 elfcpp::R_X86_64_RELATIVE,
3005 got, got_off, 0, false);
3012 case elfcpp::R_X86_64_PLT32:
3013 case elfcpp::R_X86_64_PLT32_BND:
3014 // If the symbol is fully resolved, this is just a PC32 reloc.
3015 // Otherwise we need a PLT entry.
3016 if (gsym->final_value_is_known())
3018 // If building a shared library, we can also skip the PLT entry
3019 // if the symbol is defined in the output file and is protected
3021 if (gsym->is_defined()
3022 && !gsym->is_from_dynobj()
3023 && !gsym->is_preemptible())
3025 target->make_plt_entry(symtab, layout, gsym);
3028 case elfcpp::R_X86_64_GOTPC32:
3029 case elfcpp::R_X86_64_GOTOFF64:
3030 case elfcpp::R_X86_64_GOTPC64:
3031 case elfcpp::R_X86_64_PLTOFF64:
3032 // We need a GOT section.
3033 target->got_section(symtab, layout);
3034 // For PLTOFF64, we also need a PLT entry (but only if the
3035 // symbol is not fully resolved).
3036 if (r_type == elfcpp::R_X86_64_PLTOFF64
3037 && !gsym->final_value_is_known())
3038 target->make_plt_entry(symtab, layout, gsym);
3041 case elfcpp::R_X86_64_COPY:
3042 case elfcpp::R_X86_64_GLOB_DAT:
3043 case elfcpp::R_X86_64_JUMP_SLOT:
3044 case elfcpp::R_X86_64_RELATIVE:
3045 case elfcpp::R_X86_64_IRELATIVE:
3046 // These are outstanding tls relocs, which are unexpected when linking
3047 case elfcpp::R_X86_64_TPOFF64:
3048 case elfcpp::R_X86_64_DTPMOD64:
3049 case elfcpp::R_X86_64_TLSDESC:
3050 gold_error(_("%s: unexpected reloc %u in object file"),
3051 object->name().c_str(), r_type);
3054 // These are initial tls relocs, which are expected for global()
3055 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3056 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3057 case elfcpp::R_X86_64_TLSDESC_CALL:
3058 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3059 case elfcpp::R_X86_64_DTPOFF32:
3060 case elfcpp::R_X86_64_DTPOFF64:
3061 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3062 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3064 // For the Initial-Exec model, we can treat undef symbols as final
3065 // when building an executable.
3066 const bool is_final = (gsym->final_value_is_known() ||
3067 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3068 gsym->is_undefined() &&
3069 parameters->options().output_is_executable()));
3070 const tls::Tls_optimization optimized_type
3071 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3074 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3075 if (optimized_type == tls::TLSOPT_NONE)
3077 // Create a pair of GOT entries for the module index and
3078 // dtv-relative offset.
3079 Output_data_got<64, false>* got
3080 = target->got_section(symtab, layout);
3081 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3082 target->rela_dyn_section(layout),
3083 elfcpp::R_X86_64_DTPMOD64,
3084 elfcpp::R_X86_64_DTPOFF64);
3086 else if (optimized_type == tls::TLSOPT_TO_IE)
3088 // Create a GOT entry for the tp-relative offset.
3089 Output_data_got<64, false>* got
3090 = target->got_section(symtab, layout);
3091 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3092 target->rela_dyn_section(layout),
3093 elfcpp::R_X86_64_TPOFF64);
3095 else if (optimized_type != tls::TLSOPT_TO_LE)
3096 unsupported_reloc_global(object, r_type, gsym);
3099 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3100 target->define_tls_base_symbol(symtab, layout);
3101 if (optimized_type == tls::TLSOPT_NONE)
3103 // Create reserved PLT and GOT entries for the resolver.
3104 target->reserve_tlsdesc_entries(symtab, layout);
3106 // Create a double GOT entry with an R_X86_64_TLSDESC
3107 // reloc. The R_X86_64_TLSDESC reloc is resolved
3108 // lazily, so the GOT entry needs to be in an area in
3109 // .got.plt, not .got. Call got_section to make sure
3110 // the section has been created.
3111 target->got_section(symtab, layout);
3112 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3113 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3114 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3115 elfcpp::R_X86_64_TLSDESC, 0);
3117 else if (optimized_type == tls::TLSOPT_TO_IE)
3119 // Create a GOT entry for the tp-relative offset.
3120 Output_data_got<64, false>* got
3121 = target->got_section(symtab, layout);
3122 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3123 target->rela_dyn_section(layout),
3124 elfcpp::R_X86_64_TPOFF64);
3126 else if (optimized_type != tls::TLSOPT_TO_LE)
3127 unsupported_reloc_global(object, r_type, gsym);
3130 case elfcpp::R_X86_64_TLSDESC_CALL:
3133 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3134 if (optimized_type == tls::TLSOPT_NONE)
3136 // Create a GOT entry for the module index.
3137 target->got_mod_index_entry(symtab, layout, object);
3139 else if (optimized_type != tls::TLSOPT_TO_LE)
3140 unsupported_reloc_global(object, r_type, gsym);
3143 case elfcpp::R_X86_64_DTPOFF32:
3144 case elfcpp::R_X86_64_DTPOFF64:
3147 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3148 layout->set_has_static_tls();
3149 if (optimized_type == tls::TLSOPT_NONE)
3151 // Create a GOT entry for the tp-relative offset.
3152 Output_data_got<64, false>* got
3153 = target->got_section(symtab, layout);
3154 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3155 target->rela_dyn_section(layout),
3156 elfcpp::R_X86_64_TPOFF64);
3158 else if (optimized_type != tls::TLSOPT_TO_LE)
3159 unsupported_reloc_global(object, r_type, gsym);
3162 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3163 layout->set_has_static_tls();
3164 if (parameters->options().shared())
3165 unsupported_reloc_global(object, r_type, gsym);
3174 case elfcpp::R_X86_64_SIZE32:
3175 case elfcpp::R_X86_64_SIZE64:
3177 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3178 object->name().c_str(), r_type,
3179 gsym->demangled_name().c_str());
3186 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3188 Sized_relobj_file<size, false>* object,
3189 unsigned int data_shndx,
3190 unsigned int sh_type,
3191 const unsigned char* prelocs,
3193 Output_section* output_section,
3194 bool needs_special_offset_handling,
3195 size_t local_symbol_count,
3196 const unsigned char* plocal_symbols)
3198 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3201 if (sh_type == elfcpp::SHT_REL)
3206 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3216 needs_special_offset_handling,
3221 // Scan relocations for a section.
3225 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3227 Sized_relobj_file<size, false>* object,
3228 unsigned int data_shndx,
3229 unsigned int sh_type,
3230 const unsigned char* prelocs,
3232 Output_section* output_section,
3233 bool needs_special_offset_handling,
3234 size_t local_symbol_count,
3235 const unsigned char* plocal_symbols)
3237 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3240 if (sh_type == elfcpp::SHT_REL)
3242 gold_error(_("%s: unsupported REL reloc section"),
3243 object->name().c_str());
3247 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3256 needs_special_offset_handling,
3261 // Finalize the sections.
3265 Target_x86_64<size>::do_finalize_sections(
3267 const Input_objects*,
3268 Symbol_table* symtab)
3270 const Reloc_section* rel_plt = (this->plt_ == NULL
3272 : this->plt_->rela_plt());
3273 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3274 this->rela_dyn_, true, false);
3276 // Fill in some more dynamic tags.
3277 Output_data_dynamic* const odyn = layout->dynamic_data();
3280 if (this->plt_ != NULL
3281 && this->plt_->output_section() != NULL
3282 && this->plt_->has_tlsdesc_entry())
3284 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3285 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3286 this->got_->finalize_data_size();
3287 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3288 this->plt_, plt_offset);
3289 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3290 this->got_, got_offset);
3294 // Emit any relocs we saved in an attempt to avoid generating COPY
3296 if (this->copy_relocs_.any_saved_relocs())
3297 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3299 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3300 // the .got.plt section.
3301 Symbol* sym = this->global_offset_table_;
3304 uint64_t data_size = this->got_plt_->current_data_size();
3305 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3308 if (parameters->doing_static_link()
3309 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3311 // If linking statically, make sure that the __rela_iplt symbols
3312 // were defined if necessary, even if we didn't create a PLT.
3313 static const Define_symbol_in_segment syms[] =
3316 "__rela_iplt_start", // name
3317 elfcpp::PT_LOAD, // segment_type
3318 elfcpp::PF_W, // segment_flags_set
3319 elfcpp::PF(0), // segment_flags_clear
3322 elfcpp::STT_NOTYPE, // type
3323 elfcpp::STB_GLOBAL, // binding
3324 elfcpp::STV_HIDDEN, // visibility
3326 Symbol::SEGMENT_START, // offset_from_base
3330 "__rela_iplt_end", // name
3331 elfcpp::PT_LOAD, // segment_type
3332 elfcpp::PF_W, // segment_flags_set
3333 elfcpp::PF(0), // segment_flags_clear
3336 elfcpp::STT_NOTYPE, // type
3337 elfcpp::STB_GLOBAL, // binding
3338 elfcpp::STV_HIDDEN, // visibility
3340 Symbol::SEGMENT_START, // offset_from_base
3345 symtab->define_symbols(layout, 2, syms,
3346 layout->script_options()->saw_sections_clause());
3350 // Perform a relocation.
3354 Target_x86_64<size>::Relocate::relocate(
3355 const Relocate_info<size, false>* relinfo,
3357 Target_x86_64<size>* target,
3360 const unsigned char* preloc,
3361 const Sized_symbol<size>* gsym,
3362 const Symbol_value<size>* psymval,
3363 unsigned char* view,
3364 typename elfcpp::Elf_types<size>::Elf_Addr address,
3365 section_size_type view_size)
3367 typedef Relocate_functions<size, false> Reloc_funcs;
3368 const elfcpp::Rela<size, false> rela(preloc);
3369 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3371 if (this->skip_call_tls_get_addr_)
3373 if ((r_type != elfcpp::R_X86_64_PLT32
3374 && r_type != elfcpp::R_X86_64_PLT32_BND
3375 && r_type != elfcpp::R_X86_64_PC32_BND
3376 && r_type != elfcpp::R_X86_64_PC32)
3378 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3380 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3381 _("missing expected TLS relocation"));
3385 this->skip_call_tls_get_addr_ = false;
3393 const Sized_relobj_file<size, false>* object = relinfo->object;
3395 // Pick the value to use for symbols defined in the PLT.
3396 Symbol_value<size> symval;
3398 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3400 symval.set_output_value(target->plt_address_for_global(gsym));
3403 else if (gsym == NULL && psymval->is_ifunc_symbol())
3405 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3406 if (object->local_has_plt_offset(r_sym))
3408 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3413 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3415 // Get the GOT offset if needed.
3416 // The GOT pointer points to the end of the GOT section.
3417 // We need to subtract the size of the GOT section to get
3418 // the actual offset to use in the relocation.
3419 bool have_got_offset = false;
3420 // Since the actual offset is always negative, we use signed int to
3421 // support 64-bit GOT relocations.
3425 case elfcpp::R_X86_64_GOT32:
3426 case elfcpp::R_X86_64_GOT64:
3427 case elfcpp::R_X86_64_GOTPLT64:
3428 case elfcpp::R_X86_64_GOTPCREL64:
3431 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3432 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3436 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3437 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3438 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3439 - target->got_size());
3441 have_got_offset = true;
3448 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
3452 case elfcpp::R_X86_64_NONE:
3453 case elfcpp::R_X86_64_GNU_VTINHERIT:
3454 case elfcpp::R_X86_64_GNU_VTENTRY:
3457 case elfcpp::R_X86_64_64:
3458 Reloc_funcs::rela64(view, object, psymval, addend);
3461 case elfcpp::R_X86_64_PC64:
3462 Reloc_funcs::pcrela64(view, object, psymval, addend,
3466 case elfcpp::R_X86_64_32:
3467 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
3468 Reloc_funcs::CHECK_UNSIGNED);
3471 case elfcpp::R_X86_64_32S:
3472 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
3473 Reloc_funcs::CHECK_SIGNED);
3476 case elfcpp::R_X86_64_PC32:
3477 case elfcpp::R_X86_64_PC32_BND:
3478 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
3479 address, Reloc_funcs::CHECK_SIGNED);
3482 case elfcpp::R_X86_64_16:
3483 Reloc_funcs::rela16(view, object, psymval, addend);
3486 case elfcpp::R_X86_64_PC16:
3487 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
3490 case elfcpp::R_X86_64_8:
3491 Reloc_funcs::rela8(view, object, psymval, addend);
3494 case elfcpp::R_X86_64_PC8:
3495 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
3498 case elfcpp::R_X86_64_PLT32:
3499 case elfcpp::R_X86_64_PLT32_BND:
3500 gold_assert(gsym == NULL
3501 || gsym->has_plt_offset()
3502 || gsym->final_value_is_known()
3503 || (gsym->is_defined()
3504 && !gsym->is_from_dynobj()
3505 && !gsym->is_preemptible()));
3506 // Note: while this code looks the same as for R_X86_64_PC32, it
3507 // behaves differently because psymval was set to point to
3508 // the PLT entry, rather than the symbol, in Scan::global().
3509 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
3510 address, Reloc_funcs::CHECK_SIGNED);
3513 case elfcpp::R_X86_64_PLTOFF64:
3516 gold_assert(gsym->has_plt_offset()
3517 || gsym->final_value_is_known());
3518 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3519 // This is the address of GLOBAL_OFFSET_TABLE.
3520 got_address = target->got_plt_section()->address();
3521 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
3525 case elfcpp::R_X86_64_GOT32:
3526 gold_assert(have_got_offset);
3527 Reloc_funcs::rela32(view, got_offset, addend);
3530 case elfcpp::R_X86_64_GOTPC32:
3533 typename elfcpp::Elf_types<size>::Elf_Addr value;
3534 value = target->got_plt_section()->address();
3535 Reloc_funcs::pcrela32(view, value, addend, address);
3539 case elfcpp::R_X86_64_GOT64:
3540 case elfcpp::R_X86_64_GOTPLT64:
3541 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3543 gold_assert(have_got_offset);
3544 Reloc_funcs::rela64(view, got_offset, addend);
3547 case elfcpp::R_X86_64_GOTPC64:
3550 typename elfcpp::Elf_types<size>::Elf_Addr value;
3551 value = target->got_plt_section()->address();
3552 Reloc_funcs::pcrela64(view, value, addend, address);
3556 case elfcpp::R_X86_64_GOTOFF64:
3558 typename elfcpp::Elf_types<size>::Elf_Addr value;
3559 value = (psymval->value(object, 0)
3560 - target->got_plt_section()->address());
3561 Reloc_funcs::rela64(view, value, addend);
3565 case elfcpp::R_X86_64_GOTPCREL:
3566 case elfcpp::R_X86_64_GOTPCRELX:
3567 case elfcpp::R_X86_64_REX_GOTPCRELX:
3570 // mov foo@GOTPCREL(%rip), %reg
3571 // to lea foo(%rip), %reg.
3573 if (rela.get_r_offset() >= 2
3575 && ((gsym == NULL && !psymval->is_ifunc_symbol())
3577 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
3580 Reloc_funcs::pcrela32(view, object, psymval, addend,
3587 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3588 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3592 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3593 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3594 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3595 - target->got_size());
3597 typename elfcpp::Elf_types<size>::Elf_Addr value;
3598 value = target->got_plt_section()->address() + got_offset;
3599 Reloc_funcs::pcrela32(view, value, addend, address);
3604 case elfcpp::R_X86_64_GOTPCREL64:
3606 gold_assert(have_got_offset);
3607 typename elfcpp::Elf_types<size>::Elf_Addr value;
3608 value = target->got_plt_section()->address() + got_offset;
3609 Reloc_funcs::pcrela64(view, value, addend, address);
3613 case elfcpp::R_X86_64_COPY:
3614 case elfcpp::R_X86_64_GLOB_DAT:
3615 case elfcpp::R_X86_64_JUMP_SLOT:
3616 case elfcpp::R_X86_64_RELATIVE:
3617 case elfcpp::R_X86_64_IRELATIVE:
3618 // These are outstanding tls relocs, which are unexpected when linking
3619 case elfcpp::R_X86_64_TPOFF64:
3620 case elfcpp::R_X86_64_DTPMOD64:
3621 case elfcpp::R_X86_64_TLSDESC:
3622 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3623 _("unexpected reloc %u in object file"),
3627 // These are initial tls relocs, which are expected when linking
3628 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3629 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3630 case elfcpp::R_X86_64_TLSDESC_CALL:
3631 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3632 case elfcpp::R_X86_64_DTPOFF32:
3633 case elfcpp::R_X86_64_DTPOFF64:
3634 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3635 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3636 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3637 view, address, view_size);
3640 case elfcpp::R_X86_64_SIZE32:
3641 case elfcpp::R_X86_64_SIZE64:
3643 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3644 _("unsupported reloc %u"),
3649 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
3650 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3651 _("relocation overflow"));
3656 // Perform a TLS relocation.
3660 Target_x86_64<size>::Relocate::relocate_tls(
3661 const Relocate_info<size, false>* relinfo,
3662 Target_x86_64<size>* target,
3664 const elfcpp::Rela<size, false>& rela,
3665 unsigned int r_type,
3666 const Sized_symbol<size>* gsym,
3667 const Symbol_value<size>* psymval,
3668 unsigned char* view,
3669 typename elfcpp::Elf_types<size>::Elf_Addr address,
3670 section_size_type view_size)
3672 Output_segment* tls_segment = relinfo->layout->tls_segment();
3674 const Sized_relobj_file<size, false>* object = relinfo->object;
3675 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3676 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3677 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3679 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3681 const bool is_final = (gsym == NULL
3682 ? !parameters->options().shared()
3683 : gsym->final_value_is_known());
3684 tls::Tls_optimization optimized_type
3685 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3688 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3689 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3691 // If this code sequence is used in a non-executable section,
3692 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3693 // on the assumption that it's being used by itself in a debug
3694 // section. Therefore, in the unlikely event that the code
3695 // sequence appears in a non-executable section, we simply
3696 // leave it unoptimized.
3697 optimized_type = tls::TLSOPT_NONE;
3699 if (optimized_type == tls::TLSOPT_TO_LE)
3701 if (tls_segment == NULL)
3703 gold_assert(parameters->errors()->error_count() > 0
3704 || issue_undefined_symbol_error(gsym));
3707 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3708 rela, r_type, value, view,
3714 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3715 ? GOT_TYPE_TLS_OFFSET
3716 : GOT_TYPE_TLS_PAIR);
3717 unsigned int got_offset;
3720 gold_assert(gsym->has_got_offset(got_type));
3721 got_offset = gsym->got_offset(got_type) - target->got_size();
3725 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3726 gold_assert(object->local_has_got_offset(r_sym, got_type));
3727 got_offset = (object->local_got_offset(r_sym, got_type)
3728 - target->got_size());
3730 if (optimized_type == tls::TLSOPT_TO_IE)
3732 value = target->got_plt_section()->address() + got_offset;
3733 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
3734 value, view, address, view_size);
3737 else if (optimized_type == tls::TLSOPT_NONE)
3739 // Relocate the field with the offset of the pair of GOT
3741 value = target->got_plt_section()->address() + got_offset;
3742 Relocate_functions<size, false>::pcrela32(view, value, addend,
3747 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3748 _("unsupported reloc %u"), r_type);
3751 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3752 case elfcpp::R_X86_64_TLSDESC_CALL:
3753 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3755 // See above comment for R_X86_64_TLSGD.
3756 optimized_type = tls::TLSOPT_NONE;
3758 if (optimized_type == tls::TLSOPT_TO_LE)
3760 if (tls_segment == NULL)
3762 gold_assert(parameters->errors()->error_count() > 0
3763 || issue_undefined_symbol_error(gsym));
3766 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3767 rela, r_type, value, view,
3773 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3774 ? GOT_TYPE_TLS_OFFSET
3775 : GOT_TYPE_TLS_DESC);
3776 unsigned int got_offset = 0;
3777 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3778 && optimized_type == tls::TLSOPT_NONE)
3780 // We created GOT entries in the .got.tlsdesc portion of
3781 // the .got.plt section, but the offset stored in the
3782 // symbol is the offset within .got.tlsdesc.
3783 got_offset = (target->got_size()
3784 + target->got_plt_section()->data_size());
3788 gold_assert(gsym->has_got_offset(got_type));
3789 got_offset += gsym->got_offset(got_type) - target->got_size();
3793 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3794 gold_assert(object->local_has_got_offset(r_sym, got_type));
3795 got_offset += (object->local_got_offset(r_sym, got_type)
3796 - target->got_size());
3798 if (optimized_type == tls::TLSOPT_TO_IE)
3800 value = target->got_plt_section()->address() + got_offset;
3801 this->tls_desc_gd_to_ie(relinfo, relnum,
3802 rela, r_type, value, view, address,
3806 else if (optimized_type == tls::TLSOPT_NONE)
3808 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3810 // Relocate the field with the offset of the pair of GOT
3812 value = target->got_plt_section()->address() + got_offset;
3813 Relocate_functions<size, false>::pcrela32(view, value, addend,
3819 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3820 _("unsupported reloc %u"), r_type);
3823 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3824 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3826 // See above comment for R_X86_64_TLSGD.
3827 optimized_type = tls::TLSOPT_NONE;
3829 if (optimized_type == tls::TLSOPT_TO_LE)
3831 if (tls_segment == NULL)
3833 gold_assert(parameters->errors()->error_count() > 0
3834 || issue_undefined_symbol_error(gsym));
3837 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3838 value, view, view_size);
3841 else if (optimized_type == tls::TLSOPT_NONE)
3843 // Relocate the field with the offset of the GOT entry for
3844 // the module index.
3845 unsigned int got_offset;
3846 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3847 - target->got_size());
3848 value = target->got_plt_section()->address() + got_offset;
3849 Relocate_functions<size, false>::pcrela32(view, value, addend,
3853 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3854 _("unsupported reloc %u"), r_type);
3857 case elfcpp::R_X86_64_DTPOFF32:
3858 // This relocation type is used in debugging information.
3859 // In that case we need to not optimize the value. If the
3860 // section is not executable, then we assume we should not
3861 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3862 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3864 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3866 if (tls_segment == NULL)
3868 gold_assert(parameters->errors()->error_count() > 0
3869 || issue_undefined_symbol_error(gsym));
3872 value -= tls_segment->memsz();
3874 Relocate_functions<size, false>::rela32(view, value, addend);
3877 case elfcpp::R_X86_64_DTPOFF64:
3878 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3879 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3881 if (tls_segment == NULL)
3883 gold_assert(parameters->errors()->error_count() > 0
3884 || issue_undefined_symbol_error(gsym));
3887 value -= tls_segment->memsz();
3889 Relocate_functions<size, false>::rela64(view, value, addend);
3892 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3894 && gsym->is_undefined()
3895 && parameters->options().output_is_executable())
3897 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3899 r_type, value, view,
3903 else if (optimized_type == tls::TLSOPT_TO_LE)
3905 if (tls_segment == NULL)
3907 gold_assert(parameters->errors()->error_count() > 0
3908 || issue_undefined_symbol_error(gsym));
3911 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3913 r_type, value, view,
3917 else if (optimized_type == tls::TLSOPT_NONE)
3919 // Relocate the field with the offset of the GOT entry for
3920 // the tp-relative offset of the symbol.
3921 unsigned int got_offset;
3924 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3925 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3926 - target->got_size());
3930 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3931 gold_assert(object->local_has_got_offset(r_sym,
3932 GOT_TYPE_TLS_OFFSET));
3933 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3934 - target->got_size());
3936 value = target->got_plt_section()->address() + got_offset;
3937 Relocate_functions<size, false>::pcrela32(view, value, addend,
3941 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3942 _("unsupported reloc type %u"),
3946 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3947 if (tls_segment == NULL)
3949 gold_assert(parameters->errors()->error_count() > 0
3950 || issue_undefined_symbol_error(gsym));
3953 value -= tls_segment->memsz();
3954 Relocate_functions<size, false>::rela32(view, value, addend);
3959 // Do a relocation in which we convert a TLS General-Dynamic to an
3964 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3965 const Relocate_info<size, false>* relinfo,
3967 const elfcpp::Rela<size, false>& rela,
3969 typename elfcpp::Elf_types<size>::Elf_Addr value,
3970 unsigned char* view,
3971 typename elfcpp::Elf_types<size>::Elf_Addr address,
3972 section_size_type view_size)
3975 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3976 // .word 0x6666; rex64; call __tls_get_addr
3977 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3979 // leaq foo@tlsgd(%rip),%rdi;
3980 // .word 0x6666; rex64; call __tls_get_addr
3981 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3983 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3984 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3985 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3989 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3991 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3992 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3993 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3998 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4000 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4001 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4002 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4006 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4007 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4010 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4012 this->skip_call_tls_get_addr_ = true;
4015 // Do a relocation in which we convert a TLS General-Dynamic to a
4020 Target_x86_64<size>::Relocate::tls_gd_to_le(
4021 const Relocate_info<size, false>* relinfo,
4023 Output_segment* tls_segment,
4024 const elfcpp::Rela<size, false>& rela,
4026 typename elfcpp::Elf_types<size>::Elf_Addr value,
4027 unsigned char* view,
4028 section_size_type view_size)
4031 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4032 // .word 0x6666; rex64; call __tls_get_addr
4033 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4035 // leaq foo@tlsgd(%rip),%rdi;
4036 // .word 0x6666; rex64; call __tls_get_addr
4037 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4039 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4040 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4041 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
4045 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4047 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4048 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4049 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4054 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4056 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4057 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4059 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4063 value -= tls_segment->memsz();
4064 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4066 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4068 this->skip_call_tls_get_addr_ = true;
4071 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4075 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4076 const Relocate_info<size, false>* relinfo,
4078 const elfcpp::Rela<size, false>& rela,
4079 unsigned int r_type,
4080 typename elfcpp::Elf_types<size>::Elf_Addr value,
4081 unsigned char* view,
4082 typename elfcpp::Elf_types<size>::Elf_Addr address,
4083 section_size_type view_size)
4085 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4087 // leaq foo@tlsdesc(%rip), %rax
4088 // ==> movq foo@gottpoff(%rip), %rax
4089 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4090 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4091 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4092 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4094 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4095 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4099 // call *foo@tlscall(%rax)
4101 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4102 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4103 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4104 view[0] == 0xff && view[1] == 0x10);
4110 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4114 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4115 const Relocate_info<size, false>* relinfo,
4117 Output_segment* tls_segment,
4118 const elfcpp::Rela<size, false>& rela,
4119 unsigned int r_type,
4120 typename elfcpp::Elf_types<size>::Elf_Addr value,
4121 unsigned char* view,
4122 section_size_type view_size)
4124 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4126 // leaq foo@tlsdesc(%rip), %rax
4127 // ==> movq foo@tpoff, %rax
4128 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4129 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4130 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4131 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4134 value -= tls_segment->memsz();
4135 Relocate_functions<size, false>::rela32(view, value, 0);
4139 // call *foo@tlscall(%rax)
4141 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4142 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4143 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4144 view[0] == 0xff && view[1] == 0x10);
4152 Target_x86_64<size>::Relocate::tls_ld_to_le(
4153 const Relocate_info<size, false>* relinfo,
4156 const elfcpp::Rela<size, false>& rela,
4158 typename elfcpp::Elf_types<size>::Elf_Addr,
4159 unsigned char* view,
4160 section_size_type view_size)
4162 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4164 // ... leq foo@dtpoff(%rax),%reg
4165 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4167 // ... leq foo@dtpoff(%rax),%reg
4168 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4170 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4171 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4173 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4174 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4176 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4179 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4181 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4183 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4185 this->skip_call_tls_get_addr_ = true;
4188 // Do a relocation in which we convert a TLS Initial-Exec to a
4193 Target_x86_64<size>::Relocate::tls_ie_to_le(
4194 const Relocate_info<size, false>* relinfo,
4196 Output_segment* tls_segment,
4197 const elfcpp::Rela<size, false>& rela,
4199 typename elfcpp::Elf_types<size>::Elf_Addr value,
4200 unsigned char* view,
4201 section_size_type view_size)
4203 // We need to examine the opcodes to figure out which instruction we
4206 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4207 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4210 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4212 unsigned char op1 = view[-3];
4213 unsigned char op2 = view[-2];
4214 unsigned char op3 = view[-1];
4215 unsigned char reg = op3 >> 3;
4222 else if (size == 32 && op1 == 0x44)
4225 view[-1] = 0xc0 | reg;
4229 // Special handling for %rsp.
4232 else if (size == 32 && op1 == 0x44)
4235 view[-1] = 0xc0 | reg;
4242 else if (size == 32 && op1 == 0x44)
4245 view[-1] = 0x80 | reg | (reg << 3);
4248 if (tls_segment != NULL)
4249 value -= tls_segment->memsz();
4250 Relocate_functions<size, false>::rela32(view, value, 0);
4253 // Relocate section data.
4257 Target_x86_64<size>::relocate_section(
4258 const Relocate_info<size, false>* relinfo,
4259 unsigned int sh_type,
4260 const unsigned char* prelocs,
4262 Output_section* output_section,
4263 bool needs_special_offset_handling,
4264 unsigned char* view,
4265 typename elfcpp::Elf_types<size>::Elf_Addr address,
4266 section_size_type view_size,
4267 const Reloc_symbol_changes* reloc_symbol_changes)
4269 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4272 gold_assert(sh_type == elfcpp::SHT_RELA);
4274 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
4275 gold::Default_comdat_behavior, Classify_reloc>(
4281 needs_special_offset_handling,
4285 reloc_symbol_changes);
4288 // Apply an incremental relocation. Incremental relocations always refer
4289 // to global symbols.
4293 Target_x86_64<size>::apply_relocation(
4294 const Relocate_info<size, false>* relinfo,
4295 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4296 unsigned int r_type,
4297 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4299 unsigned char* view,
4300 typename elfcpp::Elf_types<size>::Elf_Addr address,
4301 section_size_type view_size)
4303 gold::apply_relocation<size, false, Target_x86_64<size>,
4304 typename Target_x86_64<size>::Relocate>(
4316 // Scan the relocs during a relocatable link.
4320 Target_x86_64<size>::scan_relocatable_relocs(
4321 Symbol_table* symtab,
4323 Sized_relobj_file<size, false>* object,
4324 unsigned int data_shndx,
4325 unsigned int sh_type,
4326 const unsigned char* prelocs,
4328 Output_section* output_section,
4329 bool needs_special_offset_handling,
4330 size_t local_symbol_count,
4331 const unsigned char* plocal_symbols,
4332 Relocatable_relocs* rr)
4334 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4336 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
4337 Scan_relocatable_relocs;
4339 gold_assert(sh_type == elfcpp::SHT_RELA);
4341 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
4349 needs_special_offset_handling,
4355 // Scan the relocs for --emit-relocs.
4359 Target_x86_64<size>::emit_relocs_scan(
4360 Symbol_table* symtab,
4362 Sized_relobj_file<size, false>* object,
4363 unsigned int data_shndx,
4364 unsigned int sh_type,
4365 const unsigned char* prelocs,
4367 Output_section* output_section,
4368 bool needs_special_offset_handling,
4369 size_t local_symbol_count,
4370 const unsigned char* plocal_syms,
4371 Relocatable_relocs* rr)
4373 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4375 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
4376 Emit_relocs_strategy;
4378 gold_assert(sh_type == elfcpp::SHT_RELA);
4380 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
4388 needs_special_offset_handling,
4394 // Relocate a section during a relocatable link.
4398 Target_x86_64<size>::relocate_relocs(
4399 const Relocate_info<size, false>* relinfo,
4400 unsigned int sh_type,
4401 const unsigned char* prelocs,
4403 Output_section* output_section,
4404 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4405 unsigned char* view,
4406 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4407 section_size_type view_size,
4408 unsigned char* reloc_view,
4409 section_size_type reloc_view_size)
4411 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4414 gold_assert(sh_type == elfcpp::SHT_RELA);
4416 gold::relocate_relocs<size, false, Classify_reloc>(
4421 offset_in_output_section,
4429 // Return the value to use for a dynamic which requires special
4430 // treatment. This is how we support equality comparisons of function
4431 // pointers across shared library boundaries, as described in the
4432 // processor specific ABI supplement.
4436 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4438 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4439 return this->plt_address_for_global(gsym);
4442 // Return a string used to fill a code section with nops to take up
4443 // the specified length.
4447 Target_x86_64<size>::do_code_fill(section_size_type length) const
4451 // Build a jmpq instruction to skip over the bytes.
4452 unsigned char jmp[5];
4454 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4455 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4456 + std::string(length - 5, static_cast<char>(0x90)));
4459 // Nop sequences of various lengths.
4460 const char nop1[1] = { '\x90' }; // nop
4461 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4462 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4463 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4465 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4467 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4468 '\x44', '\x00', '\x00' };
4469 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4470 '\x00', '\x00', '\x00',
4472 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4473 '\x00', '\x00', '\x00',
4475 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4476 '\x84', '\x00', '\x00',
4477 '\x00', '\x00', '\x00' };
4478 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4479 '\x1f', '\x84', '\x00',
4480 '\x00', '\x00', '\x00',
4482 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4483 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4484 '\x00', '\x00', '\x00',
4486 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4487 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4488 '\x84', '\x00', '\x00',
4489 '\x00', '\x00', '\x00' };
4490 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4491 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4492 '\x1f', '\x84', '\x00',
4493 '\x00', '\x00', '\x00',
4495 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4496 '\x66', '\x66', '\x2e', // data16
4497 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4498 '\x00', '\x00', '\x00',
4500 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4501 '\x66', '\x66', '\x66', // data16; data16
4502 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4503 '\x84', '\x00', '\x00',
4504 '\x00', '\x00', '\x00' };
4506 const char* nops[16] = {
4508 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4509 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4512 return std::string(nops[length], length);
4515 // Return the addend to use for a target specific relocation. The
4516 // only target specific relocation is R_X86_64_TLSDESC for a local
4517 // symbol. We want to set the addend is the offset of the local
4518 // symbol in the TLS segment.
4522 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4525 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4526 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4527 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4528 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4529 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4530 gold_assert(psymval->is_tls_symbol());
4531 // The value of a TLS symbol is the offset in the TLS segment.
4532 return psymval->value(ti.object, 0);
4535 // Return the value to use for the base of a DW_EH_PE_datarel offset
4536 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4537 // assembler can not write out the difference between two labels in
4538 // different sections, so instead of using a pc-relative value they
4539 // use an offset from the GOT.
4543 Target_x86_64<size>::do_ehframe_datarel_base() const
4545 gold_assert(this->global_offset_table_ != NULL);
4546 Symbol* sym = this->global_offset_table_;
4547 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4548 return ssym->value();
4551 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4552 // compiled with -fsplit-stack. The function calls non-split-stack
4553 // code. We have to change the function so that it always ensures
4554 // that it has enough stack space to run some random function.
4556 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
4557 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
4558 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
4560 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4561 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
4562 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4566 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4567 section_offset_type fnoffset,
4568 section_size_type fnsize,
4569 const unsigned char*,
4571 unsigned char* view,
4572 section_size_type view_size,
4574 std::string* to) const
4576 const char* const cmp_insn = reinterpret_cast<const char*>
4577 (size == 32 ? cmp_insn_32 : cmp_insn_64);
4578 const char* const lea_r10_insn = reinterpret_cast<const char*>
4579 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
4580 const char* const lea_r11_insn = reinterpret_cast<const char*>
4581 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
4583 const size_t cmp_insn_len =
4584 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
4585 const size_t lea_r10_insn_len =
4586 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
4587 const size_t lea_r11_insn_len =
4588 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
4589 const size_t nop_len = (size == 32 ? 7 : 8);
4591 // The function starts with a comparison of the stack pointer and a
4592 // field in the TCB. This is followed by a jump.
4595 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
4596 && fnsize > nop_len + 1)
4598 // We will call __morestack if the carry flag is set after this
4599 // comparison. We turn the comparison into an stc instruction
4601 view[fnoffset] = '\xf9';
4602 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
4604 // lea NN(%rsp),%r10
4605 // lea NN(%rsp),%r11
4606 else if ((this->match_view(view, view_size, fnoffset,
4607 lea_r10_insn, lea_r10_insn_len)
4608 || this->match_view(view, view_size, fnoffset,
4609 lea_r11_insn, lea_r11_insn_len))
4612 // This is loading an offset from the stack pointer for a
4613 // comparison. The offset is negative, so we decrease the
4614 // offset by the amount of space we need for the stack. This
4615 // means we will avoid calling __morestack if there happens to
4616 // be plenty of space on the stack already.
4617 unsigned char* pval = view + fnoffset + 4;
4618 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4619 val -= parameters->options().split_stack_adjust_size();
4620 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4624 if (!object->has_no_split_stack())
4625 object->error(_("failed to match split-stack sequence at "
4626 "section %u offset %0zx"),
4627 shndx, static_cast<size_t>(fnoffset));
4631 // We have to change the function so that it calls
4632 // __morestack_non_split instead of __morestack. The former will
4633 // allocate additional stack space.
4634 *from = "__morestack";
4635 *to = "__morestack_non_split";
4638 // The selector for x86_64 object files. Note this is never instantiated
4639 // directly. It's only used in Target_selector_x86_64_nacl, below.
4642 class Target_selector_x86_64 : public Target_selector_freebsd
4645 Target_selector_x86_64()
4646 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4648 ? "elf64-x86-64" : "elf32-x86-64"),
4650 ? "elf64-x86-64-freebsd"
4651 : "elf32-x86-64-freebsd"),
4652 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4656 do_instantiate_target()
4657 { return new Target_x86_64<size>(); }
4661 // NaCl variant. It uses different PLT contents.
4664 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4667 Output_data_plt_x86_64_nacl(Layout* layout,
4668 Output_data_got<64, false>* got,
4669 Output_data_got_plt_x86_64* got_plt,
4670 Output_data_space* got_irelative)
4671 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4672 got, got_plt, got_irelative)
4675 Output_data_plt_x86_64_nacl(Layout* layout,
4676 Output_data_got<64, false>* got,
4677 Output_data_got_plt_x86_64* got_plt,
4678 Output_data_space* got_irelative,
4679 unsigned int plt_count)
4680 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4681 got, got_plt, got_irelative,
4686 virtual unsigned int
4687 do_get_plt_entry_size() const
4688 { return plt_entry_size; }
4691 do_add_eh_frame(Layout* layout)
4693 layout->add_eh_frame_for_plt(this,
4694 this->plt_eh_frame_cie,
4695 this->plt_eh_frame_cie_size,
4697 plt_eh_frame_fde_size);
4701 do_fill_first_plt_entry(unsigned char* pov,
4702 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4703 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4705 virtual unsigned int
4706 do_fill_plt_entry(unsigned char* pov,
4707 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4708 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4709 unsigned int got_offset,
4710 unsigned int plt_offset,
4711 unsigned int plt_index);
4714 do_fill_tlsdesc_entry(unsigned char* pov,
4715 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4716 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4717 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4718 unsigned int tlsdesc_got_offset,
4719 unsigned int plt_offset);
4722 // The size of an entry in the PLT.
4723 static const int plt_entry_size = 64;
4725 // The first entry in the PLT.
4726 static const unsigned char first_plt_entry[plt_entry_size];
4728 // Other entries in the PLT for an executable.
4729 static const unsigned char plt_entry[plt_entry_size];
4731 // The reserved TLSDESC entry in the PLT for an executable.
4732 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4734 // The .eh_frame unwind information for the PLT.
4735 static const int plt_eh_frame_fde_size = 32;
4736 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4740 class Target_x86_64_nacl : public Target_x86_64<size>
4743 Target_x86_64_nacl()
4744 : Target_x86_64<size>(&x86_64_nacl_info)
4747 virtual Output_data_plt_x86_64<size>*
4748 do_make_data_plt(Layout* layout,
4749 Output_data_got<64, false>* got,
4750 Output_data_got_plt_x86_64* got_plt,
4751 Output_data_space* got_irelative)
4753 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4757 virtual Output_data_plt_x86_64<size>*
4758 do_make_data_plt(Layout* layout,
4759 Output_data_got<64, false>* got,
4760 Output_data_got_plt_x86_64* got_plt,
4761 Output_data_space* got_irelative,
4762 unsigned int plt_count)
4764 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4770 do_code_fill(section_size_type length) const;
4773 static const Target::Target_info x86_64_nacl_info;
4777 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4780 false, // is_big_endian
4781 elfcpp::EM_X86_64, // machine_code
4782 false, // has_make_symbol
4783 false, // has_resolve
4784 true, // has_code_fill
4785 true, // is_default_stack_executable
4786 true, // can_icf_inline_merge_sections
4788 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4789 0x20000, // default_text_segment_address
4790 0x10000, // abi_pagesize (overridable by -z max-page-size)
4791 0x10000, // common_pagesize (overridable by -z common-page-size)
4792 true, // isolate_execinstr
4793 0x10000000, // rosegment_gap
4794 elfcpp::SHN_UNDEF, // small_common_shndx
4795 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4796 0, // small_common_section_flags
4797 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4798 NULL, // attributes_section
4799 NULL, // attributes_vendor
4800 "_start", // entry_symbol_name
4801 32, // hash_entry_size
4805 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4808 false, // is_big_endian
4809 elfcpp::EM_X86_64, // machine_code
4810 false, // has_make_symbol
4811 false, // has_resolve
4812 true, // has_code_fill
4813 true, // is_default_stack_executable
4814 true, // can_icf_inline_merge_sections
4816 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4817 0x20000, // default_text_segment_address
4818 0x10000, // abi_pagesize (overridable by -z max-page-size)
4819 0x10000, // common_pagesize (overridable by -z common-page-size)
4820 true, // isolate_execinstr
4821 0x10000000, // rosegment_gap
4822 elfcpp::SHN_UNDEF, // small_common_shndx
4823 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4824 0, // small_common_section_flags
4825 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4826 NULL, // attributes_section
4827 NULL, // attributes_vendor
4828 "_start", // entry_symbol_name
4829 32, // hash_entry_size
4832 #define NACLMASK 0xe0 // 32-byte alignment mask.
4834 // The first entry in the PLT.
4838 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4840 0xff, 0x35, // pushq contents of memory address
4841 0, 0, 0, 0, // replaced with address of .got + 8
4842 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4843 0, 0, 0, 0, // replaced with address of .got + 16
4844 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4845 0x4d, 0x01, 0xfb, // add %r15, %r11
4846 0x41, 0xff, 0xe3, // jmpq *%r11
4848 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4849 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4851 // 32 bytes of nop to pad out to the standard size
4852 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4853 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4854 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4855 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4856 0x66, // excess data32 prefix
4862 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4864 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4865 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4867 memcpy(pov, first_plt_entry, plt_entry_size);
4868 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4870 - (plt_address + 2 + 4)));
4871 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4873 - (plt_address + 9 + 4)));
4876 // Subsequent entries in the PLT.
4880 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4882 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4883 0, 0, 0, 0, // replaced with address of symbol in .got
4884 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4885 0x4d, 0x01, 0xfb, // add %r15, %r11
4886 0x41, 0xff, 0xe3, // jmpq *%r11
4888 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4889 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4890 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4892 // Lazy GOT entries point here (32-byte aligned).
4893 0x68, // pushq immediate
4894 0, 0, 0, 0, // replaced with index into relocation table
4895 0xe9, // jmp relative
4896 0, 0, 0, 0, // replaced with offset to start of .plt0
4898 // 22 bytes of nop to pad out to the standard size.
4899 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4900 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4901 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4906 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4908 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4909 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4910 unsigned int got_offset,
4911 unsigned int plt_offset,
4912 unsigned int plt_index)
4914 memcpy(pov, plt_entry, plt_entry_size);
4915 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4916 (got_address + got_offset
4917 - (plt_address + plt_offset
4920 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4921 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4922 - (plt_offset + 38 + 4));
4927 // The reserved TLSDESC entry in the PLT.
4931 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4933 0xff, 0x35, // pushq x(%rip)
4934 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4935 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4936 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4937 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4938 0x4d, 0x01, 0xfb, // add %r15, %r11
4939 0x41, 0xff, 0xe3, // jmpq *%r11
4941 // 41 bytes of nop to pad out to the standard size.
4942 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4943 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4944 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4945 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4946 0x66, 0x66, // excess data32 prefixes
4947 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4952 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
4954 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4955 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4956 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4957 unsigned int tlsdesc_got_offset,
4958 unsigned int plt_offset)
4960 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
4961 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4963 - (plt_address + plt_offset
4965 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4967 + tlsdesc_got_offset
4968 - (plt_address + plt_offset
4972 // The .eh_frame unwind information for the PLT.
4976 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4978 0, 0, 0, 0, // Replaced with offset to .plt.
4979 0, 0, 0, 0, // Replaced with size of .plt.
4980 0, // Augmentation size.
4981 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
4982 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
4983 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
4984 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
4985 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
4986 13, // Block length.
4987 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
4988 elfcpp::DW_OP_breg16, 0, // Push %rip.
4989 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
4990 elfcpp::DW_OP_and, // & (%rip & 0x3f).
4991 elfcpp::DW_OP_const1u, 37, // Push 0x25.
4992 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
4993 elfcpp::DW_OP_lit3, // Push 3.
4994 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
4995 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
4996 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5000 // Return a string used to fill a code section with nops.
5001 // For NaCl, long NOPs are only valid if they do not cross
5002 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5005 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5007 return std::string(length, static_cast<char>(0x90));
5010 // The selector for x86_64-nacl object files.
5013 class Target_selector_x86_64_nacl
5014 : public Target_selector_nacl<Target_selector_x86_64<size>,
5015 Target_x86_64_nacl<size> >
5018 Target_selector_x86_64_nacl()
5019 : Target_selector_nacl<Target_selector_x86_64<size>,
5020 Target_x86_64_nacl<size> >("x86-64",
5022 ? "elf64-x86-64-nacl"
5023 : "elf32-x86-64-nacl",
5026 : "elf32_x86_64_nacl")
5030 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5031 Target_selector_x86_64_nacl<32> target_selector_x32;
5033 } // End anonymous namespace.