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 Output_segment* tls_segment,
820 const elfcpp::Rela<size, false>&, unsigned int r_type,
821 typename elfcpp::Elf_types<size>::Elf_Addr value,
823 typename elfcpp::Elf_types<size>::Elf_Addr,
824 section_size_type view_size);
826 // Do a TLS General-Dynamic to Local-Exec transition.
828 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
829 Output_segment* tls_segment,
830 const elfcpp::Rela<size, false>&, unsigned int r_type,
831 typename elfcpp::Elf_types<size>::Elf_Addr value,
833 section_size_type view_size);
835 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
837 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
838 Output_segment* tls_segment,
839 const elfcpp::Rela<size, false>&, unsigned int r_type,
840 typename elfcpp::Elf_types<size>::Elf_Addr value,
842 typename elfcpp::Elf_types<size>::Elf_Addr,
843 section_size_type view_size);
845 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
847 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
848 Output_segment* tls_segment,
849 const elfcpp::Rela<size, false>&, unsigned int r_type,
850 typename elfcpp::Elf_types<size>::Elf_Addr value,
852 section_size_type view_size);
854 // Do a TLS Local-Dynamic to Local-Exec transition.
856 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
857 Output_segment* tls_segment,
858 const elfcpp::Rela<size, false>&, unsigned int r_type,
859 typename elfcpp::Elf_types<size>::Elf_Addr value,
861 section_size_type view_size);
863 // Do a TLS Initial-Exec to Local-Exec transition.
865 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
866 Output_segment* tls_segment,
867 const elfcpp::Rela<size, false>&, unsigned int r_type,
868 typename elfcpp::Elf_types<size>::Elf_Addr value,
870 section_size_type view_size);
872 // This is set if we should skip the next reloc, which should be a
873 // PLT32 reloc against ___tls_get_addr.
874 bool skip_call_tls_get_addr_;
877 // Check if relocation against this symbol is a candidate for
879 // mov foo@GOTPCREL(%rip), %reg
880 // to lea foo(%rip), %reg.
882 can_convert_mov_to_lea(const Symbol* gsym)
884 gold_assert(gsym != NULL);
885 return (gsym->type() != elfcpp::STT_GNU_IFUNC
886 && !gsym->is_undefined ()
887 && !gsym->is_from_dynobj()
888 && !gsym->is_preemptible()
889 && (!parameters->options().shared()
890 || (gsym->visibility() != elfcpp::STV_DEFAULT
891 && gsym->visibility() != elfcpp::STV_PROTECTED)
892 || parameters->options().Bsymbolic())
893 && strcmp(gsym->name(), "_DYNAMIC") != 0);
896 // Adjust TLS relocation type based on the options and whether this
897 // is a local symbol.
898 static tls::Tls_optimization
899 optimize_tls_reloc(bool is_final, int r_type);
901 // Get the GOT section, creating it if necessary.
902 Output_data_got<64, false>*
903 got_section(Symbol_table*, Layout*);
905 // Get the GOT PLT section.
906 Output_data_got_plt_x86_64*
907 got_plt_section() const
909 gold_assert(this->got_plt_ != NULL);
910 return this->got_plt_;
913 // Get the GOT section for TLSDESC entries.
914 Output_data_got<64, false>*
915 got_tlsdesc_section() const
917 gold_assert(this->got_tlsdesc_ != NULL);
918 return this->got_tlsdesc_;
921 // Create the PLT section.
923 make_plt_section(Symbol_table* symtab, Layout* layout);
925 // Create a PLT entry for a global symbol.
927 make_plt_entry(Symbol_table*, Layout*, Symbol*);
929 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
931 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
932 Sized_relobj_file<size, false>* relobj,
933 unsigned int local_sym_index);
935 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
937 define_tls_base_symbol(Symbol_table*, Layout*);
939 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
941 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
943 // Create a GOT entry for the TLS module index.
945 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
946 Sized_relobj_file<size, false>* object);
948 // Get the PLT section.
949 Output_data_plt_x86_64<size>*
952 gold_assert(this->plt_ != NULL);
956 // Get the dynamic reloc section, creating it if necessary.
958 rela_dyn_section(Layout*);
960 // Get the section to use for TLSDESC relocations.
962 rela_tlsdesc_section(Layout*) const;
964 // Get the section to use for IRELATIVE relocations.
966 rela_irelative_section(Layout*);
968 // Add a potential copy relocation.
970 copy_reloc(Symbol_table* symtab, Layout* layout,
971 Sized_relobj_file<size, false>* object,
972 unsigned int shndx, Output_section* output_section,
973 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
975 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
976 this->copy_relocs_.copy_reloc(symtab, layout,
977 symtab->get_sized_symbol<size>(sym),
978 object, shndx, output_section,
979 r_type, reloc.get_r_offset(),
980 reloc.get_r_addend(),
981 this->rela_dyn_section(layout));
984 // Information about this specific target which we pass to the
985 // general Target structure.
986 static const Target::Target_info x86_64_info;
988 // The types of GOT entries needed for this platform.
989 // These values are exposed to the ABI in an incremental link.
990 // Do not renumber existing values without changing the version
991 // number of the .gnu_incremental_inputs section.
994 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
995 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
996 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
997 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1000 // This type is used as the argument to the target specific
1001 // relocation routines. The only target specific reloc is
1002 // R_X86_64_TLSDESC against a local symbol.
1005 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1006 : object(a_object), r_sym(a_r_sym)
1009 // The object in which the local symbol is defined.
1010 Sized_relobj_file<size, false>* object;
1011 // The local symbol index in the object.
1016 Output_data_got<64, false>* got_;
1018 Output_data_plt_x86_64<size>* plt_;
1019 // The GOT PLT section.
1020 Output_data_got_plt_x86_64* got_plt_;
1021 // The GOT section for IRELATIVE relocations.
1022 Output_data_space* got_irelative_;
1023 // The GOT section for TLSDESC relocations.
1024 Output_data_got<64, false>* got_tlsdesc_;
1025 // The _GLOBAL_OFFSET_TABLE_ symbol.
1026 Symbol* global_offset_table_;
1027 // The dynamic reloc section.
1028 Reloc_section* rela_dyn_;
1029 // The section to use for IRELATIVE relocs.
1030 Reloc_section* rela_irelative_;
1031 // Relocs saved to avoid a COPY reloc.
1032 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1033 // Offset of the GOT entry for the TLS module index.
1034 unsigned int got_mod_index_offset_;
1035 // We handle R_X86_64_TLSDESC against a local symbol as a target
1036 // specific relocation. Here we store the object and local symbol
1037 // index for the relocation.
1038 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1039 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1040 bool tls_base_symbol_defined_;
1044 const Target::Target_info Target_x86_64<64>::x86_64_info =
1047 false, // is_big_endian
1048 elfcpp::EM_X86_64, // machine_code
1049 false, // has_make_symbol
1050 false, // has_resolve
1051 true, // has_code_fill
1052 true, // is_default_stack_executable
1053 true, // can_icf_inline_merge_sections
1055 "/lib/ld64.so.1", // program interpreter
1056 0x400000, // default_text_segment_address
1057 0x1000, // abi_pagesize (overridable by -z max-page-size)
1058 0x1000, // common_pagesize (overridable by -z common-page-size)
1059 false, // isolate_execinstr
1061 elfcpp::SHN_UNDEF, // small_common_shndx
1062 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1063 0, // small_common_section_flags
1064 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1065 NULL, // attributes_section
1066 NULL, // attributes_vendor
1067 "_start", // entry_symbol_name
1068 32, // hash_entry_size
1072 const Target::Target_info Target_x86_64<32>::x86_64_info =
1075 false, // is_big_endian
1076 elfcpp::EM_X86_64, // machine_code
1077 false, // has_make_symbol
1078 false, // has_resolve
1079 true, // has_code_fill
1080 true, // is_default_stack_executable
1081 true, // can_icf_inline_merge_sections
1083 "/libx32/ldx32.so.1", // program interpreter
1084 0x400000, // default_text_segment_address
1085 0x1000, // abi_pagesize (overridable by -z max-page-size)
1086 0x1000, // common_pagesize (overridable by -z common-page-size)
1087 false, // isolate_execinstr
1089 elfcpp::SHN_UNDEF, // small_common_shndx
1090 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1091 0, // small_common_section_flags
1092 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1093 NULL, // attributes_section
1094 NULL, // attributes_vendor
1095 "_start", // entry_symbol_name
1096 32, // hash_entry_size
1099 // This is called when a new output section is created. This is where
1100 // we handle the SHF_X86_64_LARGE.
1104 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1106 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1107 os->set_is_large_section();
1110 // Get the GOT section, creating it if necessary.
1113 Output_data_got<64, false>*
1114 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1116 if (this->got_ == NULL)
1118 gold_assert(symtab != NULL && layout != NULL);
1120 // When using -z now, we can treat .got.plt as a relro section.
1121 // Without -z now, it is modified after program startup by lazy
1123 bool is_got_plt_relro = parameters->options().now();
1124 Output_section_order got_order = (is_got_plt_relro
1126 : ORDER_RELRO_LAST);
1127 Output_section_order got_plt_order = (is_got_plt_relro
1129 : ORDER_NON_RELRO_FIRST);
1131 this->got_ = new Output_data_got<64, false>();
1133 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1135 | elfcpp::SHF_WRITE),
1136 this->got_, got_order, true);
1138 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1139 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1141 | elfcpp::SHF_WRITE),
1142 this->got_plt_, got_plt_order,
1145 // The first three entries are reserved.
1146 this->got_plt_->set_current_data_size(3 * 8);
1148 if (!is_got_plt_relro)
1150 // Those bytes can go into the relro segment.
1151 layout->increase_relro(3 * 8);
1154 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1155 this->global_offset_table_ =
1156 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1157 Symbol_table::PREDEFINED,
1159 0, 0, elfcpp::STT_OBJECT,
1161 elfcpp::STV_HIDDEN, 0,
1164 // If there are any IRELATIVE relocations, they get GOT entries
1165 // in .got.plt after the jump slot entries.
1166 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1167 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1169 | elfcpp::SHF_WRITE),
1170 this->got_irelative_,
1171 got_plt_order, is_got_plt_relro);
1173 // If there are any TLSDESC relocations, they get GOT entries in
1174 // .got.plt after the jump slot and IRELATIVE entries.
1175 this->got_tlsdesc_ = new Output_data_got<64, false>();
1176 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1178 | elfcpp::SHF_WRITE),
1180 got_plt_order, is_got_plt_relro);
1186 // Get the dynamic reloc section, creating it if necessary.
1189 typename Target_x86_64<size>::Reloc_section*
1190 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1192 if (this->rela_dyn_ == NULL)
1194 gold_assert(layout != NULL);
1195 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1196 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1197 elfcpp::SHF_ALLOC, this->rela_dyn_,
1198 ORDER_DYNAMIC_RELOCS, false);
1200 return this->rela_dyn_;
1203 // Get the section to use for IRELATIVE relocs, creating it if
1204 // necessary. These go in .rela.dyn, but only after all other dynamic
1205 // relocations. They need to follow the other dynamic relocations so
1206 // that they can refer to global variables initialized by those
1210 typename Target_x86_64<size>::Reloc_section*
1211 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1213 if (this->rela_irelative_ == NULL)
1215 // Make sure we have already created the dynamic reloc section.
1216 this->rela_dyn_section(layout);
1217 this->rela_irelative_ = new Reloc_section(false);
1218 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1219 elfcpp::SHF_ALLOC, this->rela_irelative_,
1220 ORDER_DYNAMIC_RELOCS, false);
1221 gold_assert(this->rela_dyn_->output_section()
1222 == this->rela_irelative_->output_section());
1224 return this->rela_irelative_;
1227 // Write the first three reserved words of the .got.plt section.
1228 // The remainder of the section is written while writing the PLT
1229 // in Output_data_plt_i386::do_write.
1232 Output_data_got_plt_x86_64::do_write(Output_file* of)
1234 // The first entry in the GOT is the address of the .dynamic section
1235 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1236 // We saved space for them when we created the section in
1237 // Target_x86_64::got_section.
1238 const off_t got_file_offset = this->offset();
1239 gold_assert(this->data_size() >= 24);
1240 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1241 Output_section* dynamic = this->layout_->dynamic_section();
1242 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1243 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1244 memset(got_view + 8, 0, 16);
1245 of->write_output_view(got_file_offset, 24, got_view);
1248 // Initialize the PLT section.
1252 Output_data_plt_x86_64<size>::init(Layout* layout)
1254 this->rel_ = new Reloc_section(false);
1255 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1256 elfcpp::SHF_ALLOC, this->rel_,
1257 ORDER_DYNAMIC_PLT_RELOCS, false);
1262 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1264 os->set_entsize(this->get_plt_entry_size());
1267 // Add an entry to the PLT.
1271 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1274 gold_assert(!gsym->has_plt_offset());
1276 unsigned int plt_index;
1278 section_offset_type got_offset;
1280 unsigned int* pcount;
1281 unsigned int offset;
1282 unsigned int reserved;
1283 Output_section_data_build* got;
1284 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1285 && gsym->can_use_relative_reloc(false))
1287 pcount = &this->irelative_count_;
1290 got = this->got_irelative_;
1294 pcount = &this->count_;
1297 got = this->got_plt_;
1300 if (!this->is_data_size_valid())
1302 // Note that when setting the PLT offset for a non-IRELATIVE
1303 // entry we skip the initial reserved PLT entry.
1304 plt_index = *pcount + offset;
1305 plt_offset = plt_index * this->get_plt_entry_size();
1309 got_offset = (plt_index - offset + reserved) * 8;
1310 gold_assert(got_offset == got->current_data_size());
1312 // Every PLT entry needs a GOT entry which points back to the PLT
1313 // entry (this will be changed by the dynamic linker, normally
1314 // lazily when the function is called).
1315 got->set_current_data_size(got_offset + 8);
1319 // FIXME: This is probably not correct for IRELATIVE relocs.
1321 // For incremental updates, find an available slot.
1322 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1323 this->get_plt_entry_size(), 0);
1324 if (plt_offset == -1)
1325 gold_fallback(_("out of patch space (PLT);"
1326 " relink with --incremental-full"));
1328 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1329 // can be calculated from the PLT index, adjusting for the three
1330 // reserved entries at the beginning of the GOT.
1331 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1332 got_offset = (plt_index - offset + reserved) * 8;
1335 gsym->set_plt_offset(plt_offset);
1337 // Every PLT entry needs a reloc.
1338 this->add_relocation(symtab, layout, gsym, got_offset);
1340 // Note that we don't need to save the symbol. The contents of the
1341 // PLT are independent of which symbols are used. The symbols only
1342 // appear in the relocations.
1345 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1350 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1351 Symbol_table* symtab,
1353 Sized_relobj_file<size, false>* relobj,
1354 unsigned int local_sym_index)
1356 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1357 ++this->irelative_count_;
1359 section_offset_type got_offset = this->got_irelative_->current_data_size();
1361 // Every PLT entry needs a GOT entry which points back to the PLT
1363 this->got_irelative_->set_current_data_size(got_offset + 8);
1365 // Every PLT entry needs a reloc.
1366 Reloc_section* rela = this->rela_irelative(symtab, layout);
1367 rela->add_symbolless_local_addend(relobj, local_sym_index,
1368 elfcpp::R_X86_64_IRELATIVE,
1369 this->got_irelative_, got_offset, 0);
1374 // Add the relocation for a PLT entry.
1378 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1381 unsigned int got_offset)
1383 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1384 && gsym->can_use_relative_reloc(false))
1386 Reloc_section* rela = this->rela_irelative(symtab, layout);
1387 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1388 this->got_irelative_, got_offset, 0);
1392 gsym->set_needs_dynsym_entry();
1393 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1398 // Return where the TLSDESC relocations should go, creating it if
1399 // necessary. These follow the JUMP_SLOT relocations.
1402 typename Output_data_plt_x86_64<size>::Reloc_section*
1403 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1405 if (this->tlsdesc_rel_ == NULL)
1407 this->tlsdesc_rel_ = new Reloc_section(false);
1408 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1409 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1410 ORDER_DYNAMIC_PLT_RELOCS, false);
1411 gold_assert(this->tlsdesc_rel_->output_section()
1412 == this->rel_->output_section());
1414 return this->tlsdesc_rel_;
1417 // Return where the IRELATIVE relocations should go in the PLT. These
1418 // follow the JUMP_SLOT and the TLSDESC relocations.
1421 typename Output_data_plt_x86_64<size>::Reloc_section*
1422 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1425 if (this->irelative_rel_ == NULL)
1427 // Make sure we have a place for the TLSDESC relocations, in
1428 // case we see any later on.
1429 this->rela_tlsdesc(layout);
1430 this->irelative_rel_ = new Reloc_section(false);
1431 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1432 elfcpp::SHF_ALLOC, this->irelative_rel_,
1433 ORDER_DYNAMIC_PLT_RELOCS, false);
1434 gold_assert(this->irelative_rel_->output_section()
1435 == this->rel_->output_section());
1437 if (parameters->doing_static_link())
1439 // A statically linked executable will only have a .rela.plt
1440 // section to hold R_X86_64_IRELATIVE relocs for
1441 // STT_GNU_IFUNC symbols. The library will use these
1442 // symbols to locate the IRELATIVE relocs at program startup
1444 symtab->define_in_output_data("__rela_iplt_start", NULL,
1445 Symbol_table::PREDEFINED,
1446 this->irelative_rel_, 0, 0,
1447 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1448 elfcpp::STV_HIDDEN, 0, false, true);
1449 symtab->define_in_output_data("__rela_iplt_end", NULL,
1450 Symbol_table::PREDEFINED,
1451 this->irelative_rel_, 0, 0,
1452 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1453 elfcpp::STV_HIDDEN, 0, true, true);
1456 return this->irelative_rel_;
1459 // Return the PLT address to use for a global symbol.
1463 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1465 uint64_t offset = 0;
1466 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1467 && gsym->can_use_relative_reloc(false))
1468 offset = (this->count_ + 1) * this->get_plt_entry_size();
1469 return this->address() + offset + gsym->plt_offset();
1472 // Return the PLT address to use for a local symbol. These are always
1473 // IRELATIVE relocs.
1477 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
1480 return (this->address()
1481 + (this->count_ + 1) * this->get_plt_entry_size()
1482 + object->local_plt_offset(r_sym));
1485 // Set the final size.
1488 Output_data_plt_x86_64<size>::set_final_data_size()
1490 unsigned int count = this->count_ + this->irelative_count_;
1491 if (this->has_tlsdesc_entry())
1493 this->set_data_size((count + 1) * this->get_plt_entry_size());
1496 // The first entry in the PLT for an executable.
1500 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1502 // From AMD64 ABI Draft 0.98, page 76
1503 0xff, 0x35, // pushq contents of memory address
1504 0, 0, 0, 0, // replaced with address of .got + 8
1505 0xff, 0x25, // jmp indirect
1506 0, 0, 0, 0, // replaced with address of .got + 16
1507 0x90, 0x90, 0x90, 0x90 // noop (x4)
1512 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1514 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1515 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1517 memcpy(pov, first_plt_entry, plt_entry_size);
1518 // We do a jmp relative to the PC at the end of this instruction.
1519 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1521 - (plt_address + 6)));
1522 elfcpp::Swap<32, false>::writeval(pov + 8,
1524 - (plt_address + 12)));
1527 // Subsequent entries in the PLT for an executable.
1531 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1533 // From AMD64 ABI Draft 0.98, page 76
1534 0xff, 0x25, // jmpq indirect
1535 0, 0, 0, 0, // replaced with address of symbol in .got
1536 0x68, // pushq immediate
1537 0, 0, 0, 0, // replaced with offset into relocation table
1538 0xe9, // jmpq relative
1539 0, 0, 0, 0 // replaced with offset to start of .plt
1544 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1546 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1547 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1548 unsigned int got_offset,
1549 unsigned int plt_offset,
1550 unsigned int plt_index)
1552 // Check PC-relative offset overflow in PLT entry.
1553 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1554 - (plt_address + plt_offset + 6));
1555 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1556 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1559 memcpy(pov, plt_entry, plt_entry_size);
1560 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1561 plt_got_pcrel_offset);
1563 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1564 elfcpp::Swap<32, false>::writeval(pov + 12,
1565 - (plt_offset + plt_entry_size));
1570 // The reserved TLSDESC entry in the PLT for an executable.
1574 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1576 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1577 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1578 0xff, 0x35, // pushq x(%rip)
1579 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1580 0xff, 0x25, // jmpq *y(%rip)
1581 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1588 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1590 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1591 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1592 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1593 unsigned int tlsdesc_got_offset,
1594 unsigned int plt_offset)
1596 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1597 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1599 - (plt_address + plt_offset
1601 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1603 + tlsdesc_got_offset
1604 - (plt_address + plt_offset
1608 // The .eh_frame unwind information for the PLT.
1612 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1615 'z', // Augmentation: augmentation size included.
1616 'R', // Augmentation: FDE encoding included.
1617 '\0', // End of augmentation string.
1618 1, // Code alignment factor.
1619 0x78, // Data alignment factor.
1620 16, // Return address column.
1621 1, // Augmentation size.
1622 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1623 | elfcpp::DW_EH_PE_sdata4),
1624 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1625 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1626 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1632 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1634 0, 0, 0, 0, // Replaced with offset to .plt.
1635 0, 0, 0, 0, // Replaced with size of .plt.
1636 0, // Augmentation size.
1637 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1638 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1639 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1640 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1641 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1642 11, // Block length.
1643 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1644 elfcpp::DW_OP_breg16, 0, // Push %rip.
1645 elfcpp::DW_OP_lit15, // Push 0xf.
1646 elfcpp::DW_OP_and, // & (%rip & 0xf).
1647 elfcpp::DW_OP_lit11, // Push 0xb.
1648 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1649 elfcpp::DW_OP_lit3, // Push 3.
1650 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1651 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1652 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1658 // Write out the PLT. This uses the hand-coded instructions above,
1659 // and adjusts them as needed. This is specified by the AMD64 ABI.
1663 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1665 const off_t offset = this->offset();
1666 const section_size_type oview_size =
1667 convert_to_section_size_type(this->data_size());
1668 unsigned char* const oview = of->get_output_view(offset, oview_size);
1670 const off_t got_file_offset = this->got_plt_->offset();
1671 gold_assert(parameters->incremental_update()
1672 || (got_file_offset + this->got_plt_->data_size()
1673 == this->got_irelative_->offset()));
1674 const section_size_type got_size =
1675 convert_to_section_size_type(this->got_plt_->data_size()
1676 + this->got_irelative_->data_size());
1677 unsigned char* const got_view = of->get_output_view(got_file_offset,
1680 unsigned char* pov = oview;
1682 // The base address of the .plt section.
1683 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1684 // The base address of the .got section.
1685 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1686 // The base address of the PLT portion of the .got section,
1687 // which is where the GOT pointer will point, and where the
1688 // three reserved GOT entries are located.
1689 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1690 = this->got_plt_->address();
1692 this->fill_first_plt_entry(pov, got_address, plt_address);
1693 pov += this->get_plt_entry_size();
1695 // The first three entries in the GOT are reserved, and are written
1696 // by Output_data_got_plt_x86_64::do_write.
1697 unsigned char* got_pov = got_view + 24;
1699 unsigned int plt_offset = this->get_plt_entry_size();
1700 unsigned int got_offset = 24;
1701 const unsigned int count = this->count_ + this->irelative_count_;
1702 for (unsigned int plt_index = 0;
1705 pov += this->get_plt_entry_size(),
1707 plt_offset += this->get_plt_entry_size(),
1710 // Set and adjust the PLT entry itself.
1711 unsigned int lazy_offset = this->fill_plt_entry(pov,
1712 got_address, plt_address,
1713 got_offset, plt_offset,
1716 // Set the entry in the GOT.
1717 elfcpp::Swap<64, false>::writeval(got_pov,
1718 plt_address + plt_offset + lazy_offset);
1721 if (this->has_tlsdesc_entry())
1723 // Set and adjust the reserved TLSDESC PLT entry.
1724 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1725 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
1726 tlsdesc_got_offset, plt_offset);
1727 pov += this->get_plt_entry_size();
1730 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1731 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1733 of->write_output_view(offset, oview_size, oview);
1734 of->write_output_view(got_file_offset, got_size, got_view);
1737 // Create the PLT section.
1741 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1743 if (this->plt_ == NULL)
1745 // Create the GOT sections first.
1746 this->got_section(symtab, layout);
1748 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
1749 this->got_irelative_);
1751 // Add unwind information if requested.
1752 if (parameters->options().ld_generated_unwind_info())
1753 this->plt_->add_eh_frame(layout);
1755 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1757 | elfcpp::SHF_EXECINSTR),
1758 this->plt_, ORDER_PLT, false);
1760 // Make the sh_info field of .rela.plt point to .plt.
1761 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1762 rela_plt_os->set_info_section(this->plt_->output_section());
1766 // Return the section for TLSDESC relocations.
1769 typename Target_x86_64<size>::Reloc_section*
1770 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1772 return this->plt_section()->rela_tlsdesc(layout);
1775 // Create a PLT entry for a global symbol.
1779 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1782 if (gsym->has_plt_offset())
1785 if (this->plt_ == NULL)
1786 this->make_plt_section(symtab, layout);
1788 this->plt_->add_entry(symtab, layout, gsym);
1791 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1795 Target_x86_64<size>::make_local_ifunc_plt_entry(
1796 Symbol_table* symtab, Layout* layout,
1797 Sized_relobj_file<size, false>* relobj,
1798 unsigned int local_sym_index)
1800 if (relobj->local_has_plt_offset(local_sym_index))
1802 if (this->plt_ == NULL)
1803 this->make_plt_section(symtab, layout);
1804 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1807 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1810 // Return the number of entries in the PLT.
1814 Target_x86_64<size>::plt_entry_count() const
1816 if (this->plt_ == NULL)
1818 return this->plt_->entry_count();
1821 // Return the offset of the first non-reserved PLT entry.
1825 Target_x86_64<size>::first_plt_entry_offset() const
1827 return this->plt_->first_plt_entry_offset();
1830 // Return the size of each PLT entry.
1834 Target_x86_64<size>::plt_entry_size() const
1836 return this->plt_->get_plt_entry_size();
1839 // Create the GOT and PLT sections for an incremental update.
1842 Output_data_got_base*
1843 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1845 unsigned int got_count,
1846 unsigned int plt_count)
1848 gold_assert(this->got_ == NULL);
1850 this->got_ = new Output_data_got<64, false>(got_count * 8);
1851 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1853 | elfcpp::SHF_WRITE),
1854 this->got_, ORDER_RELRO_LAST,
1857 // Add the three reserved entries.
1858 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
1859 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1861 | elfcpp::SHF_WRITE),
1862 this->got_plt_, ORDER_NON_RELRO_FIRST,
1865 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1866 this->global_offset_table_ =
1867 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1868 Symbol_table::PREDEFINED,
1870 0, 0, elfcpp::STT_OBJECT,
1872 elfcpp::STV_HIDDEN, 0,
1875 // If there are any TLSDESC relocations, they get GOT entries in
1876 // .got.plt after the jump slot entries.
1877 // FIXME: Get the count for TLSDESC entries.
1878 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1879 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1880 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1882 ORDER_NON_RELRO_FIRST, false);
1884 // If there are any IRELATIVE relocations, they get GOT entries in
1885 // .got.plt after the jump slot and TLSDESC entries.
1886 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1887 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1888 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1889 this->got_irelative_,
1890 ORDER_NON_RELRO_FIRST, false);
1892 // Create the PLT section.
1893 this->plt_ = this->make_data_plt(layout, this->got_,
1895 this->got_irelative_,
1898 // Add unwind information if requested.
1899 if (parameters->options().ld_generated_unwind_info())
1900 this->plt_->add_eh_frame(layout);
1902 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1903 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1904 this->plt_, ORDER_PLT, false);
1906 // Make the sh_info field of .rela.plt point to .plt.
1907 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1908 rela_plt_os->set_info_section(this->plt_->output_section());
1910 // Create the rela_dyn section.
1911 this->rela_dyn_section(layout);
1916 // Reserve a GOT entry for a local symbol, and regenerate any
1917 // necessary dynamic relocations.
1921 Target_x86_64<size>::reserve_local_got_entry(
1922 unsigned int got_index,
1923 Sized_relobj<size, false>* obj,
1925 unsigned int got_type)
1927 unsigned int got_offset = got_index * 8;
1928 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1930 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1933 case GOT_TYPE_STANDARD:
1934 if (parameters->options().output_is_position_independent())
1935 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1936 this->got_, got_offset, 0, false);
1938 case GOT_TYPE_TLS_OFFSET:
1939 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1940 this->got_, got_offset, 0);
1942 case GOT_TYPE_TLS_PAIR:
1943 this->got_->reserve_slot(got_index + 1);
1944 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1945 this->got_, got_offset, 0);
1947 case GOT_TYPE_TLS_DESC:
1948 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1949 // this->got_->reserve_slot(got_index + 1);
1950 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1951 // this->got_, got_offset, 0);
1958 // Reserve a GOT entry for a global symbol, and regenerate any
1959 // necessary dynamic relocations.
1963 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1965 unsigned int got_type)
1967 unsigned int got_offset = got_index * 8;
1968 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1970 this->got_->reserve_global(got_index, gsym, got_type);
1973 case GOT_TYPE_STANDARD:
1974 if (!gsym->final_value_is_known())
1976 if (gsym->is_from_dynobj()
1977 || gsym->is_undefined()
1978 || gsym->is_preemptible()
1979 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1980 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1981 this->got_, got_offset, 0);
1983 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1984 this->got_, got_offset, 0, false);
1987 case GOT_TYPE_TLS_OFFSET:
1988 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1989 this->got_, got_offset, 0, false);
1991 case GOT_TYPE_TLS_PAIR:
1992 this->got_->reserve_slot(got_index + 1);
1993 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1994 this->got_, got_offset, 0, false);
1995 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1996 this->got_, got_offset + 8, 0, false);
1998 case GOT_TYPE_TLS_DESC:
1999 this->got_->reserve_slot(got_index + 1);
2000 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2001 this->got_, got_offset, 0, false);
2008 // Register an existing PLT entry for a global symbol.
2012 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2014 unsigned int plt_index,
2017 gold_assert(this->plt_ != NULL);
2018 gold_assert(!gsym->has_plt_offset());
2020 this->plt_->reserve_slot(plt_index);
2022 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2024 unsigned int got_offset = (plt_index + 3) * 8;
2025 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2028 // Force a COPY relocation for a given symbol.
2032 Target_x86_64<size>::emit_copy_reloc(
2033 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2035 this->copy_relocs_.emit_copy_reloc(symtab,
2036 symtab->get_sized_symbol<size>(sym),
2039 this->rela_dyn_section(NULL));
2042 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2046 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2049 if (this->tls_base_symbol_defined_)
2052 Output_segment* tls_segment = layout->tls_segment();
2053 if (tls_segment != NULL)
2055 bool is_exec = parameters->options().output_is_executable();
2056 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2057 Symbol_table::PREDEFINED,
2061 elfcpp::STV_HIDDEN, 0,
2063 ? Symbol::SEGMENT_END
2064 : Symbol::SEGMENT_START),
2067 this->tls_base_symbol_defined_ = true;
2070 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2074 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2077 if (this->plt_ == NULL)
2078 this->make_plt_section(symtab, layout);
2080 if (!this->plt_->has_tlsdesc_entry())
2082 // Allocate the TLSDESC_GOT entry.
2083 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2084 unsigned int got_offset = got->add_constant(0);
2086 // Allocate the TLSDESC_PLT entry.
2087 this->plt_->reserve_tlsdesc_entry(got_offset);
2091 // Create a GOT entry for the TLS module index.
2095 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2096 Sized_relobj_file<size, false>* object)
2098 if (this->got_mod_index_offset_ == -1U)
2100 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2101 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2102 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2103 unsigned int got_offset = got->add_constant(0);
2104 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2106 got->add_constant(0);
2107 this->got_mod_index_offset_ = got_offset;
2109 return this->got_mod_index_offset_;
2112 // Optimize the TLS relocation type based on what we know about the
2113 // symbol. IS_FINAL is true if the final address of this symbol is
2114 // known at link time.
2117 tls::Tls_optimization
2118 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2120 // If we are generating a shared library, then we can't do anything
2122 if (parameters->options().shared())
2123 return tls::TLSOPT_NONE;
2127 case elfcpp::R_X86_64_TLSGD:
2128 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2129 case elfcpp::R_X86_64_TLSDESC_CALL:
2130 // These are General-Dynamic which permits fully general TLS
2131 // access. Since we know that we are generating an executable,
2132 // we can convert this to Initial-Exec. If we also know that
2133 // this is a local symbol, we can further switch to Local-Exec.
2135 return tls::TLSOPT_TO_LE;
2136 return tls::TLSOPT_TO_IE;
2138 case elfcpp::R_X86_64_TLSLD:
2139 // This is Local-Dynamic, which refers to a local symbol in the
2140 // dynamic TLS block. Since we know that we generating an
2141 // executable, we can switch to Local-Exec.
2142 return tls::TLSOPT_TO_LE;
2144 case elfcpp::R_X86_64_DTPOFF32:
2145 case elfcpp::R_X86_64_DTPOFF64:
2146 // Another Local-Dynamic reloc.
2147 return tls::TLSOPT_TO_LE;
2149 case elfcpp::R_X86_64_GOTTPOFF:
2150 // These are Initial-Exec relocs which get the thread offset
2151 // from the GOT. If we know that we are linking against the
2152 // local symbol, we can switch to Local-Exec, which links the
2153 // thread offset into the instruction.
2155 return tls::TLSOPT_TO_LE;
2156 return tls::TLSOPT_NONE;
2158 case elfcpp::R_X86_64_TPOFF32:
2159 // When we already have Local-Exec, there is nothing further we
2161 return tls::TLSOPT_NONE;
2168 // Get the Reference_flags for a particular relocation.
2172 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2176 case elfcpp::R_X86_64_NONE:
2177 case elfcpp::R_X86_64_GNU_VTINHERIT:
2178 case elfcpp::R_X86_64_GNU_VTENTRY:
2179 case elfcpp::R_X86_64_GOTPC32:
2180 case elfcpp::R_X86_64_GOTPC64:
2181 // No symbol reference.
2184 case elfcpp::R_X86_64_64:
2185 case elfcpp::R_X86_64_32:
2186 case elfcpp::R_X86_64_32S:
2187 case elfcpp::R_X86_64_16:
2188 case elfcpp::R_X86_64_8:
2189 return Symbol::ABSOLUTE_REF;
2191 case elfcpp::R_X86_64_PC64:
2192 case elfcpp::R_X86_64_PC32:
2193 case elfcpp::R_X86_64_PC32_BND:
2194 case elfcpp::R_X86_64_PC16:
2195 case elfcpp::R_X86_64_PC8:
2196 case elfcpp::R_X86_64_GOTOFF64:
2197 return Symbol::RELATIVE_REF;
2199 case elfcpp::R_X86_64_PLT32:
2200 case elfcpp::R_X86_64_PLT32_BND:
2201 case elfcpp::R_X86_64_PLTOFF64:
2202 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2204 case elfcpp::R_X86_64_GOT64:
2205 case elfcpp::R_X86_64_GOT32:
2206 case elfcpp::R_X86_64_GOTPCREL64:
2207 case elfcpp::R_X86_64_GOTPCREL:
2208 case elfcpp::R_X86_64_GOTPCRELX:
2209 case elfcpp::R_X86_64_REX_GOTPCRELX:
2210 case elfcpp::R_X86_64_GOTPLT64:
2212 return Symbol::ABSOLUTE_REF;
2214 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2215 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2216 case elfcpp::R_X86_64_TLSDESC_CALL:
2217 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2218 case elfcpp::R_X86_64_DTPOFF32:
2219 case elfcpp::R_X86_64_DTPOFF64:
2220 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2221 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2222 return Symbol::TLS_REF;
2224 case elfcpp::R_X86_64_COPY:
2225 case elfcpp::R_X86_64_GLOB_DAT:
2226 case elfcpp::R_X86_64_JUMP_SLOT:
2227 case elfcpp::R_X86_64_RELATIVE:
2228 case elfcpp::R_X86_64_IRELATIVE:
2229 case elfcpp::R_X86_64_TPOFF64:
2230 case elfcpp::R_X86_64_DTPMOD64:
2231 case elfcpp::R_X86_64_TLSDESC:
2232 case elfcpp::R_X86_64_SIZE32:
2233 case elfcpp::R_X86_64_SIZE64:
2235 // Not expected. We will give an error later.
2240 // Report an unsupported relocation against a local symbol.
2244 Target_x86_64<size>::Scan::unsupported_reloc_local(
2245 Sized_relobj_file<size, false>* object,
2246 unsigned int r_type)
2248 gold_error(_("%s: unsupported reloc %u against local symbol"),
2249 object->name().c_str(), r_type);
2252 // We are about to emit a dynamic relocation of type R_TYPE. If the
2253 // dynamic linker does not support it, issue an error. The GNU linker
2254 // only issues a non-PIC error for an allocated read-only section.
2255 // Here we know the section is allocated, but we don't know that it is
2256 // read-only. But we check for all the relocation types which the
2257 // glibc dynamic linker supports, so it seems appropriate to issue an
2258 // error even if the section is not read-only. If GSYM is not NULL,
2259 // it is the symbol the relocation is against; if it is NULL, the
2260 // relocation is against a local symbol.
2264 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2269 // These are the relocation types supported by glibc for x86_64
2270 // which should always work.
2271 case elfcpp::R_X86_64_RELATIVE:
2272 case elfcpp::R_X86_64_IRELATIVE:
2273 case elfcpp::R_X86_64_GLOB_DAT:
2274 case elfcpp::R_X86_64_JUMP_SLOT:
2275 case elfcpp::R_X86_64_DTPMOD64:
2276 case elfcpp::R_X86_64_DTPOFF64:
2277 case elfcpp::R_X86_64_TPOFF64:
2278 case elfcpp::R_X86_64_64:
2279 case elfcpp::R_X86_64_COPY:
2282 // glibc supports these reloc types, but they can overflow.
2283 case elfcpp::R_X86_64_PC32:
2284 case elfcpp::R_X86_64_PC32_BND:
2285 // A PC relative reference is OK against a local symbol or if
2286 // the symbol is defined locally.
2288 || (!gsym->is_from_dynobj()
2289 && !gsym->is_undefined()
2290 && !gsym->is_preemptible()))
2293 case elfcpp::R_X86_64_32:
2294 // R_X86_64_32 is OK for x32.
2295 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2297 if (this->issued_non_pic_error_)
2299 gold_assert(parameters->options().output_is_position_independent());
2301 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2302 "overflow at runtime; recompile with -fPIC"));
2308 case elfcpp::R_X86_64_32:
2309 r_name = "R_X86_64_32";
2311 case elfcpp::R_X86_64_PC32:
2312 r_name = "R_X86_64_PC32";
2314 case elfcpp::R_X86_64_PC32_BND:
2315 r_name = "R_X86_64_PC32_BND";
2321 object->error(_("requires dynamic %s reloc against '%s' "
2322 "which may overflow at runtime; recompile "
2324 r_name, gsym->name());
2326 this->issued_non_pic_error_ = true;
2330 // This prevents us from issuing more than one error per reloc
2331 // section. But we can still wind up issuing more than one
2332 // error per object file.
2333 if (this->issued_non_pic_error_)
2335 gold_assert(parameters->options().output_is_position_independent());
2336 object->error(_("requires unsupported dynamic reloc %u; "
2337 "recompile with -fPIC"),
2339 this->issued_non_pic_error_ = true;
2342 case elfcpp::R_X86_64_NONE:
2347 // Return whether we need to make a PLT entry for a relocation of the
2348 // given type against a STT_GNU_IFUNC symbol.
2352 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2353 Sized_relobj_file<size, false>* object,
2354 unsigned int r_type)
2356 int flags = Scan::get_reference_flags(r_type);
2357 if (flags & Symbol::TLS_REF)
2358 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2359 object->name().c_str(), r_type);
2363 // Scan a relocation for a local symbol.
2367 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2369 Target_x86_64<size>* target,
2370 Sized_relobj_file<size, false>* object,
2371 unsigned int data_shndx,
2372 Output_section* output_section,
2373 const elfcpp::Rela<size, false>& reloc,
2374 unsigned int r_type,
2375 const elfcpp::Sym<size, false>& lsym,
2381 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2382 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2383 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2385 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2386 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2391 case elfcpp::R_X86_64_NONE:
2392 case elfcpp::R_X86_64_GNU_VTINHERIT:
2393 case elfcpp::R_X86_64_GNU_VTENTRY:
2396 case elfcpp::R_X86_64_64:
2397 // If building a shared library (or a position-independent
2398 // executable), we need to create a dynamic relocation for this
2399 // location. The relocation applied at link time will apply the
2400 // link-time value, so we flag the location with an
2401 // R_X86_64_RELATIVE relocation so the dynamic loader can
2402 // relocate it easily.
2403 if (parameters->options().output_is_position_independent())
2405 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2406 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2407 rela_dyn->add_local_relative(object, r_sym,
2409 ? elfcpp::R_X86_64_RELATIVE64
2410 : elfcpp::R_X86_64_RELATIVE),
2411 output_section, data_shndx,
2412 reloc.get_r_offset(),
2413 reloc.get_r_addend(), is_ifunc);
2417 case elfcpp::R_X86_64_32:
2418 case elfcpp::R_X86_64_32S:
2419 case elfcpp::R_X86_64_16:
2420 case elfcpp::R_X86_64_8:
2421 // If building a shared library (or a position-independent
2422 // executable), we need to create a dynamic relocation for this
2423 // location. We can't use an R_X86_64_RELATIVE relocation
2424 // because that is always a 64-bit relocation.
2425 if (parameters->options().output_is_position_independent())
2427 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2428 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2430 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2431 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2432 rela_dyn->add_local_relative(object, r_sym,
2433 elfcpp::R_X86_64_RELATIVE,
2434 output_section, data_shndx,
2435 reloc.get_r_offset(),
2436 reloc.get_r_addend(), is_ifunc);
2440 this->check_non_pic(object, r_type, NULL);
2442 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2443 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2444 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2445 rela_dyn->add_local(object, r_sym, r_type, output_section,
2446 data_shndx, reloc.get_r_offset(),
2447 reloc.get_r_addend());
2450 gold_assert(lsym.get_st_value() == 0);
2451 unsigned int shndx = lsym.get_st_shndx();
2453 shndx = object->adjust_sym_shndx(r_sym, shndx,
2456 object->error(_("section symbol %u has bad shndx %u"),
2459 rela_dyn->add_local_section(object, shndx,
2460 r_type, output_section,
2461 data_shndx, reloc.get_r_offset(),
2462 reloc.get_r_addend());
2467 case elfcpp::R_X86_64_PC64:
2468 case elfcpp::R_X86_64_PC32:
2469 case elfcpp::R_X86_64_PC32_BND:
2470 case elfcpp::R_X86_64_PC16:
2471 case elfcpp::R_X86_64_PC8:
2474 case elfcpp::R_X86_64_PLT32:
2475 case elfcpp::R_X86_64_PLT32_BND:
2476 // Since we know this is a local symbol, we can handle this as a
2480 case elfcpp::R_X86_64_GOTPC32:
2481 case elfcpp::R_X86_64_GOTOFF64:
2482 case elfcpp::R_X86_64_GOTPC64:
2483 case elfcpp::R_X86_64_PLTOFF64:
2484 // We need a GOT section.
2485 target->got_section(symtab, layout);
2486 // For PLTOFF64, we'd normally want a PLT section, but since we
2487 // know this is a local symbol, no PLT is needed.
2490 case elfcpp::R_X86_64_GOT64:
2491 case elfcpp::R_X86_64_GOT32:
2492 case elfcpp::R_X86_64_GOTPCREL64:
2493 case elfcpp::R_X86_64_GOTPCREL:
2494 case elfcpp::R_X86_64_GOTPCRELX:
2495 case elfcpp::R_X86_64_REX_GOTPCRELX:
2496 case elfcpp::R_X86_64_GOTPLT64:
2498 // The symbol requires a GOT section.
2499 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2501 // If the relocation symbol isn't IFUNC,
2502 // and is local, then we will convert
2503 // mov foo@GOTPCREL(%rip), %reg
2504 // to lea foo(%rip), %reg.
2505 // in Relocate::relocate.
2506 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2507 || r_type == elfcpp::R_X86_64_GOTPCRELX
2508 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2509 && reloc.get_r_offset() >= 2
2512 section_size_type stype;
2513 const unsigned char* view = object->section_contents(data_shndx,
2515 if (view[reloc.get_r_offset() - 2] == 0x8b)
2520 // The symbol requires a GOT entry.
2521 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2523 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2524 // lets function pointers compare correctly with shared
2525 // libraries. Otherwise we would need an IRELATIVE reloc.
2528 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2530 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2533 // If we are generating a shared object, we need to add a
2534 // dynamic relocation for this symbol's GOT entry.
2535 if (parameters->options().output_is_position_independent())
2537 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2538 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2539 if (r_type != elfcpp::R_X86_64_GOT32)
2541 unsigned int got_offset =
2542 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2543 rela_dyn->add_local_relative(object, r_sym,
2544 elfcpp::R_X86_64_RELATIVE,
2545 got, got_offset, 0, is_ifunc);
2549 this->check_non_pic(object, r_type, NULL);
2551 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2552 rela_dyn->add_local(
2553 object, r_sym, r_type, got,
2554 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2558 // For GOTPLT64, we'd normally want a PLT section, but since
2559 // we know this is a local symbol, no PLT is needed.
2563 case elfcpp::R_X86_64_COPY:
2564 case elfcpp::R_X86_64_GLOB_DAT:
2565 case elfcpp::R_X86_64_JUMP_SLOT:
2566 case elfcpp::R_X86_64_RELATIVE:
2567 case elfcpp::R_X86_64_IRELATIVE:
2568 // These are outstanding tls relocs, which are unexpected when linking
2569 case elfcpp::R_X86_64_TPOFF64:
2570 case elfcpp::R_X86_64_DTPMOD64:
2571 case elfcpp::R_X86_64_TLSDESC:
2572 gold_error(_("%s: unexpected reloc %u in object file"),
2573 object->name().c_str(), r_type);
2576 // These are initial tls relocs, which are expected when linking
2577 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2578 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2579 case elfcpp::R_X86_64_TLSDESC_CALL:
2580 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2581 case elfcpp::R_X86_64_DTPOFF32:
2582 case elfcpp::R_X86_64_DTPOFF64:
2583 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2584 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2586 bool output_is_shared = parameters->options().shared();
2587 const tls::Tls_optimization optimized_type
2588 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2592 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2593 if (optimized_type == tls::TLSOPT_NONE)
2595 // Create a pair of GOT entries for the module index and
2596 // dtv-relative offset.
2597 Output_data_got<64, false>* got
2598 = target->got_section(symtab, layout);
2599 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2600 unsigned int shndx = lsym.get_st_shndx();
2602 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2604 object->error(_("local symbol %u has bad shndx %u"),
2607 got->add_local_pair_with_rel(object, r_sym,
2610 target->rela_dyn_section(layout),
2611 elfcpp::R_X86_64_DTPMOD64);
2613 else if (optimized_type != tls::TLSOPT_TO_LE)
2614 unsupported_reloc_local(object, r_type);
2617 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2618 target->define_tls_base_symbol(symtab, layout);
2619 if (optimized_type == tls::TLSOPT_NONE)
2621 // Create reserved PLT and GOT entries for the resolver.
2622 target->reserve_tlsdesc_entries(symtab, layout);
2624 // Generate a double GOT entry with an
2625 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2626 // is resolved lazily, so the GOT entry needs to be in
2627 // an area in .got.plt, not .got. Call got_section to
2628 // make sure the section has been created.
2629 target->got_section(symtab, layout);
2630 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2631 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2632 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2634 unsigned int got_offset = got->add_constant(0);
2635 got->add_constant(0);
2636 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2638 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2639 // We store the arguments we need in a vector, and
2640 // use the index into the vector as the parameter
2641 // to pass to the target specific routines.
2642 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2643 void* arg = reinterpret_cast<void*>(intarg);
2644 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2645 got, got_offset, 0);
2648 else if (optimized_type != tls::TLSOPT_TO_LE)
2649 unsupported_reloc_local(object, r_type);
2652 case elfcpp::R_X86_64_TLSDESC_CALL:
2655 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2656 if (optimized_type == tls::TLSOPT_NONE)
2658 // Create a GOT entry for the module index.
2659 target->got_mod_index_entry(symtab, layout, object);
2661 else if (optimized_type != tls::TLSOPT_TO_LE)
2662 unsupported_reloc_local(object, r_type);
2665 case elfcpp::R_X86_64_DTPOFF32:
2666 case elfcpp::R_X86_64_DTPOFF64:
2669 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2670 layout->set_has_static_tls();
2671 if (optimized_type == tls::TLSOPT_NONE)
2673 // Create a GOT entry for the tp-relative offset.
2674 Output_data_got<64, false>* got
2675 = target->got_section(symtab, layout);
2676 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2677 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2678 target->rela_dyn_section(layout),
2679 elfcpp::R_X86_64_TPOFF64);
2681 else if (optimized_type != tls::TLSOPT_TO_LE)
2682 unsupported_reloc_local(object, r_type);
2685 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2686 layout->set_has_static_tls();
2687 if (output_is_shared)
2688 unsupported_reloc_local(object, r_type);
2697 case elfcpp::R_X86_64_SIZE32:
2698 case elfcpp::R_X86_64_SIZE64:
2700 gold_error(_("%s: unsupported reloc %u against local symbol"),
2701 object->name().c_str(), r_type);
2707 // Report an unsupported relocation against a global symbol.
2711 Target_x86_64<size>::Scan::unsupported_reloc_global(
2712 Sized_relobj_file<size, false>* object,
2713 unsigned int r_type,
2716 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2717 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2720 // Returns true if this relocation type could be that of a function pointer.
2723 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2727 case elfcpp::R_X86_64_64:
2728 case elfcpp::R_X86_64_32:
2729 case elfcpp::R_X86_64_32S:
2730 case elfcpp::R_X86_64_16:
2731 case elfcpp::R_X86_64_8:
2732 case elfcpp::R_X86_64_GOT64:
2733 case elfcpp::R_X86_64_GOT32:
2734 case elfcpp::R_X86_64_GOTPCREL64:
2735 case elfcpp::R_X86_64_GOTPCREL:
2736 case elfcpp::R_X86_64_GOTPCRELX:
2737 case elfcpp::R_X86_64_REX_GOTPCRELX:
2738 case elfcpp::R_X86_64_GOTPLT64:
2746 // For safe ICF, scan a relocation for a local symbol to check if it
2747 // corresponds to a function pointer being taken. In that case mark
2748 // the function whose pointer was taken as not foldable.
2752 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2755 Target_x86_64<size>* ,
2756 Sized_relobj_file<size, false>* ,
2759 const elfcpp::Rela<size, false>& ,
2760 unsigned int r_type,
2761 const elfcpp::Sym<size, false>&)
2763 // When building a shared library, do not fold any local symbols as it is
2764 // not possible to distinguish pointer taken versus a call by looking at
2765 // the relocation types.
2766 return (parameters->options().shared()
2767 || possible_function_pointer_reloc(r_type));
2770 // For safe ICF, scan a relocation for a global symbol to check if it
2771 // corresponds to a function pointer being taken. In that case mark
2772 // the function whose pointer was taken as not foldable.
2776 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2779 Target_x86_64<size>* ,
2780 Sized_relobj_file<size, false>* ,
2783 const elfcpp::Rela<size, false>& ,
2784 unsigned int r_type,
2787 // When building a shared library, do not fold symbols whose visibility
2788 // is hidden, internal or protected.
2789 return ((parameters->options().shared()
2790 && (gsym->visibility() == elfcpp::STV_INTERNAL
2791 || gsym->visibility() == elfcpp::STV_PROTECTED
2792 || gsym->visibility() == elfcpp::STV_HIDDEN))
2793 || possible_function_pointer_reloc(r_type));
2796 // Scan a relocation for a global symbol.
2800 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2802 Target_x86_64<size>* target,
2803 Sized_relobj_file<size, false>* object,
2804 unsigned int data_shndx,
2805 Output_section* output_section,
2806 const elfcpp::Rela<size, false>& reloc,
2807 unsigned int r_type,
2810 // A STT_GNU_IFUNC symbol may require a PLT entry.
2811 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2812 && this->reloc_needs_plt_for_ifunc(object, r_type))
2813 target->make_plt_entry(symtab, layout, gsym);
2817 case elfcpp::R_X86_64_NONE:
2818 case elfcpp::R_X86_64_GNU_VTINHERIT:
2819 case elfcpp::R_X86_64_GNU_VTENTRY:
2822 case elfcpp::R_X86_64_64:
2823 case elfcpp::R_X86_64_32:
2824 case elfcpp::R_X86_64_32S:
2825 case elfcpp::R_X86_64_16:
2826 case elfcpp::R_X86_64_8:
2828 // Make a PLT entry if necessary.
2829 if (gsym->needs_plt_entry())
2831 target->make_plt_entry(symtab, layout, gsym);
2832 // Since this is not a PC-relative relocation, we may be
2833 // taking the address of a function. In that case we need to
2834 // set the entry in the dynamic symbol table to the address of
2836 if (gsym->is_from_dynobj() && !parameters->options().shared())
2837 gsym->set_needs_dynsym_value();
2839 // Make a dynamic relocation if necessary.
2840 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2842 if (!parameters->options().output_is_position_independent()
2843 && gsym->may_need_copy_reloc())
2845 target->copy_reloc(symtab, layout, object,
2846 data_shndx, output_section, gsym, reloc);
2848 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2849 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2850 && gsym->type() == elfcpp::STT_GNU_IFUNC
2851 && gsym->can_use_relative_reloc(false)
2852 && !gsym->is_from_dynobj()
2853 && !gsym->is_undefined()
2854 && !gsym->is_preemptible())
2856 // Use an IRELATIVE reloc for a locally defined
2857 // STT_GNU_IFUNC symbol. This makes a function
2858 // address in a PIE executable match the address in a
2859 // shared library that it links against.
2860 Reloc_section* rela_dyn =
2861 target->rela_irelative_section(layout);
2862 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2863 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2864 output_section, object,
2866 reloc.get_r_offset(),
2867 reloc.get_r_addend());
2869 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2870 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2871 && gsym->can_use_relative_reloc(false))
2873 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2874 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2875 output_section, object,
2877 reloc.get_r_offset(),
2878 reloc.get_r_addend(), false);
2882 this->check_non_pic(object, r_type, gsym);
2883 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2884 rela_dyn->add_global(gsym, r_type, output_section, object,
2885 data_shndx, reloc.get_r_offset(),
2886 reloc.get_r_addend());
2892 case elfcpp::R_X86_64_PC64:
2893 case elfcpp::R_X86_64_PC32:
2894 case elfcpp::R_X86_64_PC32_BND:
2895 case elfcpp::R_X86_64_PC16:
2896 case elfcpp::R_X86_64_PC8:
2898 // Make a PLT entry if necessary.
2899 if (gsym->needs_plt_entry())
2900 target->make_plt_entry(symtab, layout, gsym);
2901 // Make a dynamic relocation if necessary.
2902 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2904 if (parameters->options().output_is_executable()
2905 && gsym->may_need_copy_reloc())
2907 target->copy_reloc(symtab, layout, object,
2908 data_shndx, output_section, gsym, reloc);
2912 this->check_non_pic(object, r_type, gsym);
2913 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2914 rela_dyn->add_global(gsym, r_type, output_section, object,
2915 data_shndx, reloc.get_r_offset(),
2916 reloc.get_r_addend());
2922 case elfcpp::R_X86_64_GOT64:
2923 case elfcpp::R_X86_64_GOT32:
2924 case elfcpp::R_X86_64_GOTPCREL64:
2925 case elfcpp::R_X86_64_GOTPCREL:
2926 case elfcpp::R_X86_64_GOTPCRELX:
2927 case elfcpp::R_X86_64_REX_GOTPCRELX:
2928 case elfcpp::R_X86_64_GOTPLT64:
2930 // The symbol requires a GOT entry.
2931 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2933 // If we convert this from
2934 // mov foo@GOTPCREL(%rip), %reg
2935 // to lea foo(%rip), %reg.
2936 // in Relocate::relocate, then there is nothing to do here.
2937 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2938 || r_type == elfcpp::R_X86_64_GOTPCRELX
2939 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2940 && reloc.get_r_offset() >= 2
2941 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
2943 section_size_type stype;
2944 const unsigned char* view = object->section_contents(data_shndx,
2946 if (view[reloc.get_r_offset() - 2] == 0x8b)
2950 if (gsym->final_value_is_known())
2952 // For a STT_GNU_IFUNC symbol we want the PLT address.
2953 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2954 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2956 got->add_global(gsym, GOT_TYPE_STANDARD);
2960 // If this symbol is not fully resolved, we need to add a
2961 // dynamic relocation for it.
2962 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2964 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2966 // 1) The symbol may be defined in some other module.
2968 // 2) We are building a shared library and this is a
2969 // protected symbol; using GLOB_DAT means that the dynamic
2970 // linker can use the address of the PLT in the main
2971 // executable when appropriate so that function address
2972 // comparisons work.
2974 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2975 // code, again so that function address comparisons work.
2976 if (gsym->is_from_dynobj()
2977 || gsym->is_undefined()
2978 || gsym->is_preemptible()
2979 || (gsym->visibility() == elfcpp::STV_PROTECTED
2980 && parameters->options().shared())
2981 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2982 && parameters->options().output_is_position_independent()))
2983 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2984 elfcpp::R_X86_64_GLOB_DAT);
2987 // For a STT_GNU_IFUNC symbol we want to write the PLT
2988 // offset into the GOT, so that function pointer
2989 // comparisons work correctly.
2991 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2992 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2995 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2996 // Tell the dynamic linker to use the PLT address
2997 // when resolving relocations.
2998 if (gsym->is_from_dynobj()
2999 && !parameters->options().shared())
3000 gsym->set_needs_dynsym_value();
3004 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3005 rela_dyn->add_global_relative(gsym,
3006 elfcpp::R_X86_64_RELATIVE,
3007 got, got_off, 0, false);
3014 case elfcpp::R_X86_64_PLT32:
3015 case elfcpp::R_X86_64_PLT32_BND:
3016 // If the symbol is fully resolved, this is just a PC32 reloc.
3017 // Otherwise we need a PLT entry.
3018 if (gsym->final_value_is_known())
3020 // If building a shared library, we can also skip the PLT entry
3021 // if the symbol is defined in the output file and is protected
3023 if (gsym->is_defined()
3024 && !gsym->is_from_dynobj()
3025 && !gsym->is_preemptible())
3027 target->make_plt_entry(symtab, layout, gsym);
3030 case elfcpp::R_X86_64_GOTPC32:
3031 case elfcpp::R_X86_64_GOTOFF64:
3032 case elfcpp::R_X86_64_GOTPC64:
3033 case elfcpp::R_X86_64_PLTOFF64:
3034 // We need a GOT section.
3035 target->got_section(symtab, layout);
3036 // For PLTOFF64, we also need a PLT entry (but only if the
3037 // symbol is not fully resolved).
3038 if (r_type == elfcpp::R_X86_64_PLTOFF64
3039 && !gsym->final_value_is_known())
3040 target->make_plt_entry(symtab, layout, gsym);
3043 case elfcpp::R_X86_64_COPY:
3044 case elfcpp::R_X86_64_GLOB_DAT:
3045 case elfcpp::R_X86_64_JUMP_SLOT:
3046 case elfcpp::R_X86_64_RELATIVE:
3047 case elfcpp::R_X86_64_IRELATIVE:
3048 // These are outstanding tls relocs, which are unexpected when linking
3049 case elfcpp::R_X86_64_TPOFF64:
3050 case elfcpp::R_X86_64_DTPMOD64:
3051 case elfcpp::R_X86_64_TLSDESC:
3052 gold_error(_("%s: unexpected reloc %u in object file"),
3053 object->name().c_str(), r_type);
3056 // These are initial tls relocs, which are expected for global()
3057 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3058 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3059 case elfcpp::R_X86_64_TLSDESC_CALL:
3060 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3061 case elfcpp::R_X86_64_DTPOFF32:
3062 case elfcpp::R_X86_64_DTPOFF64:
3063 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3064 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3066 // For the Initial-Exec model, we can treat undef symbols as final
3067 // when building an executable.
3068 const bool is_final = (gsym->final_value_is_known() ||
3069 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3070 gsym->is_undefined() &&
3071 parameters->options().output_is_executable()));
3072 const tls::Tls_optimization optimized_type
3073 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3076 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3077 if (optimized_type == tls::TLSOPT_NONE)
3079 // Create a pair of GOT entries for the module index and
3080 // dtv-relative offset.
3081 Output_data_got<64, false>* got
3082 = target->got_section(symtab, layout);
3083 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3084 target->rela_dyn_section(layout),
3085 elfcpp::R_X86_64_DTPMOD64,
3086 elfcpp::R_X86_64_DTPOFF64);
3088 else if (optimized_type == tls::TLSOPT_TO_IE)
3090 // Create a GOT entry for the tp-relative offset.
3091 Output_data_got<64, false>* got
3092 = target->got_section(symtab, layout);
3093 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3094 target->rela_dyn_section(layout),
3095 elfcpp::R_X86_64_TPOFF64);
3097 else if (optimized_type != tls::TLSOPT_TO_LE)
3098 unsupported_reloc_global(object, r_type, gsym);
3101 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3102 target->define_tls_base_symbol(symtab, layout);
3103 if (optimized_type == tls::TLSOPT_NONE)
3105 // Create reserved PLT and GOT entries for the resolver.
3106 target->reserve_tlsdesc_entries(symtab, layout);
3108 // Create a double GOT entry with an R_X86_64_TLSDESC
3109 // reloc. The R_X86_64_TLSDESC reloc is resolved
3110 // lazily, so the GOT entry needs to be in an area in
3111 // .got.plt, not .got. Call got_section to make sure
3112 // the section has been created.
3113 target->got_section(symtab, layout);
3114 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3115 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3116 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3117 elfcpp::R_X86_64_TLSDESC, 0);
3119 else if (optimized_type == tls::TLSOPT_TO_IE)
3121 // Create a GOT entry for the tp-relative offset.
3122 Output_data_got<64, false>* got
3123 = target->got_section(symtab, layout);
3124 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3125 target->rela_dyn_section(layout),
3126 elfcpp::R_X86_64_TPOFF64);
3128 else if (optimized_type != tls::TLSOPT_TO_LE)
3129 unsupported_reloc_global(object, r_type, gsym);
3132 case elfcpp::R_X86_64_TLSDESC_CALL:
3135 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3136 if (optimized_type == tls::TLSOPT_NONE)
3138 // Create a GOT entry for the module index.
3139 target->got_mod_index_entry(symtab, layout, object);
3141 else if (optimized_type != tls::TLSOPT_TO_LE)
3142 unsupported_reloc_global(object, r_type, gsym);
3145 case elfcpp::R_X86_64_DTPOFF32:
3146 case elfcpp::R_X86_64_DTPOFF64:
3149 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3150 layout->set_has_static_tls();
3151 if (optimized_type == tls::TLSOPT_NONE)
3153 // Create a GOT entry for the tp-relative offset.
3154 Output_data_got<64, false>* got
3155 = target->got_section(symtab, layout);
3156 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3157 target->rela_dyn_section(layout),
3158 elfcpp::R_X86_64_TPOFF64);
3160 else if (optimized_type != tls::TLSOPT_TO_LE)
3161 unsupported_reloc_global(object, r_type, gsym);
3164 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3165 layout->set_has_static_tls();
3166 if (parameters->options().shared())
3167 unsupported_reloc_global(object, r_type, gsym);
3176 case elfcpp::R_X86_64_SIZE32:
3177 case elfcpp::R_X86_64_SIZE64:
3179 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3180 object->name().c_str(), r_type,
3181 gsym->demangled_name().c_str());
3188 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3190 Sized_relobj_file<size, false>* object,
3191 unsigned int data_shndx,
3192 unsigned int sh_type,
3193 const unsigned char* prelocs,
3195 Output_section* output_section,
3196 bool needs_special_offset_handling,
3197 size_t local_symbol_count,
3198 const unsigned char* plocal_symbols)
3200 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3203 if (sh_type == elfcpp::SHT_REL)
3208 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3218 needs_special_offset_handling,
3223 // Scan relocations for a section.
3227 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3229 Sized_relobj_file<size, false>* object,
3230 unsigned int data_shndx,
3231 unsigned int sh_type,
3232 const unsigned char* prelocs,
3234 Output_section* output_section,
3235 bool needs_special_offset_handling,
3236 size_t local_symbol_count,
3237 const unsigned char* plocal_symbols)
3239 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3242 if (sh_type == elfcpp::SHT_REL)
3244 gold_error(_("%s: unsupported REL reloc section"),
3245 object->name().c_str());
3249 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3258 needs_special_offset_handling,
3263 // Finalize the sections.
3267 Target_x86_64<size>::do_finalize_sections(
3269 const Input_objects*,
3270 Symbol_table* symtab)
3272 const Reloc_section* rel_plt = (this->plt_ == NULL
3274 : this->plt_->rela_plt());
3275 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3276 this->rela_dyn_, true, false);
3278 // Fill in some more dynamic tags.
3279 Output_data_dynamic* const odyn = layout->dynamic_data();
3282 if (this->plt_ != NULL
3283 && this->plt_->output_section() != NULL
3284 && this->plt_->has_tlsdesc_entry())
3286 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3287 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3288 this->got_->finalize_data_size();
3289 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3290 this->plt_, plt_offset);
3291 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3292 this->got_, got_offset);
3296 // Emit any relocs we saved in an attempt to avoid generating COPY
3298 if (this->copy_relocs_.any_saved_relocs())
3299 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3301 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3302 // the .got.plt section.
3303 Symbol* sym = this->global_offset_table_;
3306 uint64_t data_size = this->got_plt_->current_data_size();
3307 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3310 if (parameters->doing_static_link()
3311 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3313 // If linking statically, make sure that the __rela_iplt symbols
3314 // were defined if necessary, even if we didn't create a PLT.
3315 static const Define_symbol_in_segment syms[] =
3318 "__rela_iplt_start", // name
3319 elfcpp::PT_LOAD, // segment_type
3320 elfcpp::PF_W, // segment_flags_set
3321 elfcpp::PF(0), // segment_flags_clear
3324 elfcpp::STT_NOTYPE, // type
3325 elfcpp::STB_GLOBAL, // binding
3326 elfcpp::STV_HIDDEN, // visibility
3328 Symbol::SEGMENT_START, // offset_from_base
3332 "__rela_iplt_end", // name
3333 elfcpp::PT_LOAD, // segment_type
3334 elfcpp::PF_W, // segment_flags_set
3335 elfcpp::PF(0), // segment_flags_clear
3338 elfcpp::STT_NOTYPE, // type
3339 elfcpp::STB_GLOBAL, // binding
3340 elfcpp::STV_HIDDEN, // visibility
3342 Symbol::SEGMENT_START, // offset_from_base
3347 symtab->define_symbols(layout, 2, syms,
3348 layout->script_options()->saw_sections_clause());
3352 // Perform a relocation.
3356 Target_x86_64<size>::Relocate::relocate(
3357 const Relocate_info<size, false>* relinfo,
3359 Target_x86_64<size>* target,
3362 const unsigned char* preloc,
3363 const Sized_symbol<size>* gsym,
3364 const Symbol_value<size>* psymval,
3365 unsigned char* view,
3366 typename elfcpp::Elf_types<size>::Elf_Addr address,
3367 section_size_type view_size)
3369 const elfcpp::Rela<size, false> rela(preloc);
3370 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3372 if (this->skip_call_tls_get_addr_)
3374 if ((r_type != elfcpp::R_X86_64_PLT32
3375 && r_type != elfcpp::R_X86_64_PLT32_BND
3376 && r_type != elfcpp::R_X86_64_PC32_BND
3377 && r_type != elfcpp::R_X86_64_PC32)
3379 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3381 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3382 _("missing expected TLS relocation"));
3386 this->skip_call_tls_get_addr_ = false;
3394 const Sized_relobj_file<size, false>* object = relinfo->object;
3396 // Pick the value to use for symbols defined in the PLT.
3397 Symbol_value<size> symval;
3399 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3401 symval.set_output_value(target->plt_address_for_global(gsym));
3404 else if (gsym == NULL && psymval->is_ifunc_symbol())
3406 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3407 if (object->local_has_plt_offset(r_sym))
3409 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3414 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3416 // Get the GOT offset if needed.
3417 // The GOT pointer points to the end of the GOT section.
3418 // We need to subtract the size of the GOT section to get
3419 // the actual offset to use in the relocation.
3420 bool have_got_offset = false;
3421 // Since the actual offset is always negative, we use signed int to
3422 // support 64-bit GOT relocations.
3426 case elfcpp::R_X86_64_GOT32:
3427 case elfcpp::R_X86_64_GOT64:
3428 case elfcpp::R_X86_64_GOTPLT64:
3429 case elfcpp::R_X86_64_GOTPCREL64:
3432 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3433 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3437 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3438 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3439 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3440 - target->got_size());
3442 have_got_offset = true;
3451 case elfcpp::R_X86_64_NONE:
3452 case elfcpp::R_X86_64_GNU_VTINHERIT:
3453 case elfcpp::R_X86_64_GNU_VTENTRY:
3456 case elfcpp::R_X86_64_64:
3457 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
3460 case elfcpp::R_X86_64_PC64:
3461 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
3465 case elfcpp::R_X86_64_32:
3466 // FIXME: we need to verify that value + addend fits into 32 bits:
3467 // uint64_t x = value + addend;
3468 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3469 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3470 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3473 case elfcpp::R_X86_64_32S:
3474 // FIXME: we need to verify that value + addend fits into 32 bits:
3475 // int64_t x = value + addend; // note this quantity is signed!
3476 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3477 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3480 case elfcpp::R_X86_64_PC32:
3481 case elfcpp::R_X86_64_PC32_BND:
3482 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3486 case elfcpp::R_X86_64_16:
3487 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
3490 case elfcpp::R_X86_64_PC16:
3491 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3495 case elfcpp::R_X86_64_8:
3496 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
3499 case elfcpp::R_X86_64_PC8:
3500 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3504 case elfcpp::R_X86_64_PLT32:
3505 case elfcpp::R_X86_64_PLT32_BND:
3506 gold_assert(gsym == NULL
3507 || gsym->has_plt_offset()
3508 || gsym->final_value_is_known()
3509 || (gsym->is_defined()
3510 && !gsym->is_from_dynobj()
3511 && !gsym->is_preemptible()));
3512 // Note: while this code looks the same as for R_X86_64_PC32, it
3513 // behaves differently because psymval was set to point to
3514 // the PLT entry, rather than the symbol, in Scan::global().
3515 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3519 case elfcpp::R_X86_64_PLTOFF64:
3522 gold_assert(gsym->has_plt_offset()
3523 || gsym->final_value_is_known());
3524 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3525 // This is the address of GLOBAL_OFFSET_TABLE.
3526 got_address = target->got_plt_section()->address();
3527 Relocate_functions<size, false>::rela64(view, object, psymval,
3528 addend - got_address);
3532 case elfcpp::R_X86_64_GOT32:
3533 gold_assert(have_got_offset);
3534 Relocate_functions<size, false>::rela32(view, got_offset, addend);
3537 case elfcpp::R_X86_64_GOTPC32:
3540 typename elfcpp::Elf_types<size>::Elf_Addr value;
3541 value = target->got_plt_section()->address();
3542 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3546 case elfcpp::R_X86_64_GOT64:
3547 case elfcpp::R_X86_64_GOTPLT64:
3548 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3550 gold_assert(have_got_offset);
3551 Relocate_functions<size, false>::rela64(view, got_offset, addend);
3554 case elfcpp::R_X86_64_GOTPC64:
3557 typename elfcpp::Elf_types<size>::Elf_Addr value;
3558 value = target->got_plt_section()->address();
3559 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3563 case elfcpp::R_X86_64_GOTOFF64:
3565 typename elfcpp::Elf_types<size>::Elf_Addr value;
3566 value = (psymval->value(object, 0)
3567 - target->got_plt_section()->address());
3568 Relocate_functions<size, false>::rela64(view, value, addend);
3572 case elfcpp::R_X86_64_GOTPCREL:
3573 case elfcpp::R_X86_64_GOTPCRELX:
3574 case elfcpp::R_X86_64_REX_GOTPCRELX:
3577 // mov foo@GOTPCREL(%rip), %reg
3578 // to lea foo(%rip), %reg.
3580 if (rela.get_r_offset() >= 2
3582 && ((gsym == NULL && !psymval->is_ifunc_symbol())
3584 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
3587 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3594 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3595 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3599 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3600 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3601 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3602 - target->got_size());
3604 typename elfcpp::Elf_types<size>::Elf_Addr value;
3605 value = target->got_plt_section()->address() + got_offset;
3606 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3611 case elfcpp::R_X86_64_GOTPCREL64:
3613 gold_assert(have_got_offset);
3614 typename elfcpp::Elf_types<size>::Elf_Addr value;
3615 value = target->got_plt_section()->address() + got_offset;
3616 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3620 case elfcpp::R_X86_64_COPY:
3621 case elfcpp::R_X86_64_GLOB_DAT:
3622 case elfcpp::R_X86_64_JUMP_SLOT:
3623 case elfcpp::R_X86_64_RELATIVE:
3624 case elfcpp::R_X86_64_IRELATIVE:
3625 // These are outstanding tls relocs, which are unexpected when linking
3626 case elfcpp::R_X86_64_TPOFF64:
3627 case elfcpp::R_X86_64_DTPMOD64:
3628 case elfcpp::R_X86_64_TLSDESC:
3629 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3630 _("unexpected reloc %u in object file"),
3634 // These are initial tls relocs, which are expected when linking
3635 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3636 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3637 case elfcpp::R_X86_64_TLSDESC_CALL:
3638 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3639 case elfcpp::R_X86_64_DTPOFF32:
3640 case elfcpp::R_X86_64_DTPOFF64:
3641 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3642 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3643 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3644 view, address, view_size);
3647 case elfcpp::R_X86_64_SIZE32:
3648 case elfcpp::R_X86_64_SIZE64:
3650 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3651 _("unsupported reloc %u"),
3659 // Perform a TLS relocation.
3663 Target_x86_64<size>::Relocate::relocate_tls(
3664 const Relocate_info<size, false>* relinfo,
3665 Target_x86_64<size>* target,
3667 const elfcpp::Rela<size, false>& rela,
3668 unsigned int r_type,
3669 const Sized_symbol<size>* gsym,
3670 const Symbol_value<size>* psymval,
3671 unsigned char* view,
3672 typename elfcpp::Elf_types<size>::Elf_Addr address,
3673 section_size_type view_size)
3675 Output_segment* tls_segment = relinfo->layout->tls_segment();
3677 const Sized_relobj_file<size, false>* object = relinfo->object;
3678 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3679 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3680 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3682 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3684 const bool is_final = (gsym == NULL
3685 ? !parameters->options().shared()
3686 : gsym->final_value_is_known());
3687 tls::Tls_optimization optimized_type
3688 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3691 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3692 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3694 // If this code sequence is used in a non-executable section,
3695 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3696 // on the assumption that it's being used by itself in a debug
3697 // section. Therefore, in the unlikely event that the code
3698 // sequence appears in a non-executable section, we simply
3699 // leave it unoptimized.
3700 optimized_type = tls::TLSOPT_NONE;
3702 if (optimized_type == tls::TLSOPT_TO_LE)
3704 if (tls_segment == NULL)
3706 gold_assert(parameters->errors()->error_count() > 0
3707 || issue_undefined_symbol_error(gsym));
3710 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3711 rela, r_type, value, view,
3717 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3718 ? GOT_TYPE_TLS_OFFSET
3719 : GOT_TYPE_TLS_PAIR);
3720 unsigned int got_offset;
3723 gold_assert(gsym->has_got_offset(got_type));
3724 got_offset = gsym->got_offset(got_type) - target->got_size();
3728 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3729 gold_assert(object->local_has_got_offset(r_sym, got_type));
3730 got_offset = (object->local_got_offset(r_sym, got_type)
3731 - target->got_size());
3733 if (optimized_type == tls::TLSOPT_TO_IE)
3735 value = target->got_plt_section()->address() + got_offset;
3736 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3737 value, view, address, view_size);
3740 else if (optimized_type == tls::TLSOPT_NONE)
3742 // Relocate the field with the offset of the pair of GOT
3744 value = target->got_plt_section()->address() + got_offset;
3745 Relocate_functions<size, false>::pcrela32(view, value, addend,
3750 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3751 _("unsupported reloc %u"), r_type);
3754 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3755 case elfcpp::R_X86_64_TLSDESC_CALL:
3756 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3758 // See above comment for R_X86_64_TLSGD.
3759 optimized_type = tls::TLSOPT_NONE;
3761 if (optimized_type == tls::TLSOPT_TO_LE)
3763 if (tls_segment == NULL)
3765 gold_assert(parameters->errors()->error_count() > 0
3766 || issue_undefined_symbol_error(gsym));
3769 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3770 rela, r_type, value, view,
3776 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3777 ? GOT_TYPE_TLS_OFFSET
3778 : GOT_TYPE_TLS_DESC);
3779 unsigned int got_offset = 0;
3780 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3781 && optimized_type == tls::TLSOPT_NONE)
3783 // We created GOT entries in the .got.tlsdesc portion of
3784 // the .got.plt section, but the offset stored in the
3785 // symbol is the offset within .got.tlsdesc.
3786 got_offset = (target->got_size()
3787 + target->got_plt_section()->data_size());
3791 gold_assert(gsym->has_got_offset(got_type));
3792 got_offset += gsym->got_offset(got_type) - target->got_size();
3796 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3797 gold_assert(object->local_has_got_offset(r_sym, got_type));
3798 got_offset += (object->local_got_offset(r_sym, got_type)
3799 - target->got_size());
3801 if (optimized_type == tls::TLSOPT_TO_IE)
3803 if (tls_segment == NULL)
3805 gold_assert(parameters->errors()->error_count() > 0
3806 || issue_undefined_symbol_error(gsym));
3809 value = target->got_plt_section()->address() + got_offset;
3810 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3811 rela, r_type, value, view, address,
3815 else if (optimized_type == tls::TLSOPT_NONE)
3817 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3819 // Relocate the field with the offset of the pair of GOT
3821 value = target->got_plt_section()->address() + got_offset;
3822 Relocate_functions<size, false>::pcrela32(view, value, addend,
3828 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3829 _("unsupported reloc %u"), r_type);
3832 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3833 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3835 // See above comment for R_X86_64_TLSGD.
3836 optimized_type = tls::TLSOPT_NONE;
3838 if (optimized_type == tls::TLSOPT_TO_LE)
3840 if (tls_segment == NULL)
3842 gold_assert(parameters->errors()->error_count() > 0
3843 || issue_undefined_symbol_error(gsym));
3846 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3847 value, view, view_size);
3850 else if (optimized_type == tls::TLSOPT_NONE)
3852 // Relocate the field with the offset of the GOT entry for
3853 // the module index.
3854 unsigned int got_offset;
3855 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3856 - target->got_size());
3857 value = target->got_plt_section()->address() + got_offset;
3858 Relocate_functions<size, false>::pcrela32(view, value, addend,
3862 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3863 _("unsupported reloc %u"), r_type);
3866 case elfcpp::R_X86_64_DTPOFF32:
3867 // This relocation type is used in debugging information.
3868 // In that case we need to not optimize the value. If the
3869 // section is not executable, then we assume we should not
3870 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3871 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3873 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3875 if (tls_segment == NULL)
3877 gold_assert(parameters->errors()->error_count() > 0
3878 || issue_undefined_symbol_error(gsym));
3881 value -= tls_segment->memsz();
3883 Relocate_functions<size, false>::rela32(view, value, addend);
3886 case elfcpp::R_X86_64_DTPOFF64:
3887 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3888 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3890 if (tls_segment == NULL)
3892 gold_assert(parameters->errors()->error_count() > 0
3893 || issue_undefined_symbol_error(gsym));
3896 value -= tls_segment->memsz();
3898 Relocate_functions<size, false>::rela64(view, value, addend);
3901 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3903 && gsym->is_undefined()
3904 && parameters->options().output_is_executable())
3906 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3908 r_type, value, view,
3912 else if (optimized_type == tls::TLSOPT_TO_LE)
3914 if (tls_segment == NULL)
3916 gold_assert(parameters->errors()->error_count() > 0
3917 || issue_undefined_symbol_error(gsym));
3920 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3922 r_type, value, view,
3926 else if (optimized_type == tls::TLSOPT_NONE)
3928 // Relocate the field with the offset of the GOT entry for
3929 // the tp-relative offset of the symbol.
3930 unsigned int got_offset;
3933 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3934 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3935 - target->got_size());
3939 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3940 gold_assert(object->local_has_got_offset(r_sym,
3941 GOT_TYPE_TLS_OFFSET));
3942 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3943 - target->got_size());
3945 value = target->got_plt_section()->address() + got_offset;
3946 Relocate_functions<size, false>::pcrela32(view, value, addend,
3950 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3951 _("unsupported reloc type %u"),
3955 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3956 if (tls_segment == NULL)
3958 gold_assert(parameters->errors()->error_count() > 0
3959 || issue_undefined_symbol_error(gsym));
3962 value -= tls_segment->memsz();
3963 Relocate_functions<size, false>::rela32(view, value, addend);
3968 // Do a relocation in which we convert a TLS General-Dynamic to an
3973 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3974 const Relocate_info<size, false>* relinfo,
3977 const elfcpp::Rela<size, false>& rela,
3979 typename elfcpp::Elf_types<size>::Elf_Addr value,
3980 unsigned char* view,
3981 typename elfcpp::Elf_types<size>::Elf_Addr address,
3982 section_size_type view_size)
3985 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3986 // .word 0x6666; rex64; call __tls_get_addr
3987 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3989 // leaq foo@tlsgd(%rip),%rdi;
3990 // .word 0x6666; rex64; call __tls_get_addr
3991 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3993 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3994 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3995 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3999 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4001 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4002 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4003 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4008 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4010 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4011 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4012 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4016 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4017 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4020 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4022 this->skip_call_tls_get_addr_ = true;
4025 // Do a relocation in which we convert a TLS General-Dynamic to a
4030 Target_x86_64<size>::Relocate::tls_gd_to_le(
4031 const Relocate_info<size, false>* relinfo,
4033 Output_segment* tls_segment,
4034 const elfcpp::Rela<size, false>& rela,
4036 typename elfcpp::Elf_types<size>::Elf_Addr value,
4037 unsigned char* view,
4038 section_size_type view_size)
4041 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4042 // .word 0x6666; rex64; call __tls_get_addr
4043 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4045 // leaq foo@tlsgd(%rip),%rdi;
4046 // .word 0x6666; rex64; call __tls_get_addr
4047 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4049 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4050 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4051 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
4055 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4057 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4058 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4059 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4064 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4066 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4067 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4069 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4073 value -= tls_segment->memsz();
4074 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4076 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4078 this->skip_call_tls_get_addr_ = true;
4081 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4085 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4086 const Relocate_info<size, false>* relinfo,
4089 const elfcpp::Rela<size, false>& rela,
4090 unsigned int r_type,
4091 typename elfcpp::Elf_types<size>::Elf_Addr value,
4092 unsigned char* view,
4093 typename elfcpp::Elf_types<size>::Elf_Addr address,
4094 section_size_type view_size)
4096 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4098 // leaq foo@tlsdesc(%rip), %rax
4099 // ==> movq foo@gottpoff(%rip), %rax
4100 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4101 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4102 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4103 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4105 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4106 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4110 // call *foo@tlscall(%rax)
4112 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4113 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4114 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4115 view[0] == 0xff && view[1] == 0x10);
4121 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4125 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4126 const Relocate_info<size, false>* relinfo,
4128 Output_segment* tls_segment,
4129 const elfcpp::Rela<size, false>& rela,
4130 unsigned int r_type,
4131 typename elfcpp::Elf_types<size>::Elf_Addr value,
4132 unsigned char* view,
4133 section_size_type view_size)
4135 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4137 // leaq foo@tlsdesc(%rip), %rax
4138 // ==> movq foo@tpoff, %rax
4139 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4140 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4141 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4142 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4145 value -= tls_segment->memsz();
4146 Relocate_functions<size, false>::rela32(view, value, 0);
4150 // call *foo@tlscall(%rax)
4152 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4153 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4154 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4155 view[0] == 0xff && view[1] == 0x10);
4163 Target_x86_64<size>::Relocate::tls_ld_to_le(
4164 const Relocate_info<size, false>* relinfo,
4167 const elfcpp::Rela<size, false>& rela,
4169 typename elfcpp::Elf_types<size>::Elf_Addr,
4170 unsigned char* view,
4171 section_size_type view_size)
4173 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4175 // ... leq foo@dtpoff(%rax),%reg
4176 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4178 // ... leq foo@dtpoff(%rax),%reg
4179 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4181 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4182 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4184 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4185 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4187 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4190 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4192 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4194 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4196 this->skip_call_tls_get_addr_ = true;
4199 // Do a relocation in which we convert a TLS Initial-Exec to a
4204 Target_x86_64<size>::Relocate::tls_ie_to_le(
4205 const Relocate_info<size, false>* relinfo,
4207 Output_segment* tls_segment,
4208 const elfcpp::Rela<size, false>& rela,
4210 typename elfcpp::Elf_types<size>::Elf_Addr value,
4211 unsigned char* view,
4212 section_size_type view_size)
4214 // We need to examine the opcodes to figure out which instruction we
4217 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4218 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4220 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4221 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4223 unsigned char op1 = view[-3];
4224 unsigned char op2 = view[-2];
4225 unsigned char op3 = view[-1];
4226 unsigned char reg = op3 >> 3;
4233 else if (size == 32 && op1 == 0x44)
4236 view[-1] = 0xc0 | reg;
4240 // Special handling for %rsp.
4243 else if (size == 32 && op1 == 0x44)
4246 view[-1] = 0xc0 | reg;
4253 else if (size == 32 && op1 == 0x44)
4256 view[-1] = 0x80 | reg | (reg << 3);
4259 if (tls_segment != NULL)
4260 value -= tls_segment->memsz();
4261 Relocate_functions<size, false>::rela32(view, value, 0);
4264 // Relocate section data.
4268 Target_x86_64<size>::relocate_section(
4269 const Relocate_info<size, false>* relinfo,
4270 unsigned int sh_type,
4271 const unsigned char* prelocs,
4273 Output_section* output_section,
4274 bool needs_special_offset_handling,
4275 unsigned char* view,
4276 typename elfcpp::Elf_types<size>::Elf_Addr address,
4277 section_size_type view_size,
4278 const Reloc_symbol_changes* reloc_symbol_changes)
4280 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4283 gold_assert(sh_type == elfcpp::SHT_RELA);
4285 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
4286 gold::Default_comdat_behavior, Classify_reloc>(
4292 needs_special_offset_handling,
4296 reloc_symbol_changes);
4299 // Apply an incremental relocation. Incremental relocations always refer
4300 // to global symbols.
4304 Target_x86_64<size>::apply_relocation(
4305 const Relocate_info<size, false>* relinfo,
4306 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4307 unsigned int r_type,
4308 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4310 unsigned char* view,
4311 typename elfcpp::Elf_types<size>::Elf_Addr address,
4312 section_size_type view_size)
4314 gold::apply_relocation<size, false, Target_x86_64<size>,
4315 typename Target_x86_64<size>::Relocate>(
4327 // Scan the relocs during a relocatable link.
4331 Target_x86_64<size>::scan_relocatable_relocs(
4332 Symbol_table* symtab,
4334 Sized_relobj_file<size, false>* object,
4335 unsigned int data_shndx,
4336 unsigned int sh_type,
4337 const unsigned char* prelocs,
4339 Output_section* output_section,
4340 bool needs_special_offset_handling,
4341 size_t local_symbol_count,
4342 const unsigned char* plocal_symbols,
4343 Relocatable_relocs* rr)
4345 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4347 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
4348 Scan_relocatable_relocs;
4350 gold_assert(sh_type == elfcpp::SHT_RELA);
4352 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
4360 needs_special_offset_handling,
4366 // Scan the relocs for --emit-relocs.
4370 Target_x86_64<size>::emit_relocs_scan(
4371 Symbol_table* symtab,
4373 Sized_relobj_file<size, false>* object,
4374 unsigned int data_shndx,
4375 unsigned int sh_type,
4376 const unsigned char* prelocs,
4378 Output_section* output_section,
4379 bool needs_special_offset_handling,
4380 size_t local_symbol_count,
4381 const unsigned char* plocal_syms,
4382 Relocatable_relocs* rr)
4384 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4386 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
4387 Emit_relocs_strategy;
4389 gold_assert(sh_type == elfcpp::SHT_RELA);
4391 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
4399 needs_special_offset_handling,
4405 // Relocate a section during a relocatable link.
4409 Target_x86_64<size>::relocate_relocs(
4410 const Relocate_info<size, false>* relinfo,
4411 unsigned int sh_type,
4412 const unsigned char* prelocs,
4414 Output_section* output_section,
4415 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4416 unsigned char* view,
4417 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4418 section_size_type view_size,
4419 unsigned char* reloc_view,
4420 section_size_type reloc_view_size)
4422 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4425 gold_assert(sh_type == elfcpp::SHT_RELA);
4427 gold::relocate_relocs<size, false, Classify_reloc>(
4432 offset_in_output_section,
4440 // Return the value to use for a dynamic which requires special
4441 // treatment. This is how we support equality comparisons of function
4442 // pointers across shared library boundaries, as described in the
4443 // processor specific ABI supplement.
4447 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4449 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4450 return this->plt_address_for_global(gsym);
4453 // Return a string used to fill a code section with nops to take up
4454 // the specified length.
4458 Target_x86_64<size>::do_code_fill(section_size_type length) const
4462 // Build a jmpq instruction to skip over the bytes.
4463 unsigned char jmp[5];
4465 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4466 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4467 + std::string(length - 5, static_cast<char>(0x90)));
4470 // Nop sequences of various lengths.
4471 const char nop1[1] = { '\x90' }; // nop
4472 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4473 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4474 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4476 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4478 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4479 '\x44', '\x00', '\x00' };
4480 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4481 '\x00', '\x00', '\x00',
4483 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4484 '\x00', '\x00', '\x00',
4486 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4487 '\x84', '\x00', '\x00',
4488 '\x00', '\x00', '\x00' };
4489 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4490 '\x1f', '\x84', '\x00',
4491 '\x00', '\x00', '\x00',
4493 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4494 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4495 '\x00', '\x00', '\x00',
4497 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4498 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4499 '\x84', '\x00', '\x00',
4500 '\x00', '\x00', '\x00' };
4501 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4502 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4503 '\x1f', '\x84', '\x00',
4504 '\x00', '\x00', '\x00',
4506 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4507 '\x66', '\x66', '\x2e', // data16
4508 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4509 '\x00', '\x00', '\x00',
4511 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4512 '\x66', '\x66', '\x66', // data16; data16
4513 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4514 '\x84', '\x00', '\x00',
4515 '\x00', '\x00', '\x00' };
4517 const char* nops[16] = {
4519 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4520 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4523 return std::string(nops[length], length);
4526 // Return the addend to use for a target specific relocation. The
4527 // only target specific relocation is R_X86_64_TLSDESC for a local
4528 // symbol. We want to set the addend is the offset of the local
4529 // symbol in the TLS segment.
4533 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4536 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4537 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4538 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4539 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4540 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4541 gold_assert(psymval->is_tls_symbol());
4542 // The value of a TLS symbol is the offset in the TLS segment.
4543 return psymval->value(ti.object, 0);
4546 // Return the value to use for the base of a DW_EH_PE_datarel offset
4547 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4548 // assembler can not write out the difference between two labels in
4549 // different sections, so instead of using a pc-relative value they
4550 // use an offset from the GOT.
4554 Target_x86_64<size>::do_ehframe_datarel_base() const
4556 gold_assert(this->global_offset_table_ != NULL);
4557 Symbol* sym = this->global_offset_table_;
4558 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4559 return ssym->value();
4562 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4563 // compiled with -fsplit-stack. The function calls non-split-stack
4564 // code. We have to change the function so that it always ensures
4565 // that it has enough stack space to run some random function.
4567 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
4568 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
4569 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
4571 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4572 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
4573 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4577 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4578 section_offset_type fnoffset,
4579 section_size_type fnsize,
4580 const unsigned char*,
4582 unsigned char* view,
4583 section_size_type view_size,
4585 std::string* to) const
4587 const char* const cmp_insn = reinterpret_cast<const char*>
4588 (size == 32 ? cmp_insn_32 : cmp_insn_64);
4589 const char* const lea_r10_insn = reinterpret_cast<const char*>
4590 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
4591 const char* const lea_r11_insn = reinterpret_cast<const char*>
4592 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
4594 const size_t cmp_insn_len =
4595 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
4596 const size_t lea_r10_insn_len =
4597 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
4598 const size_t lea_r11_insn_len =
4599 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
4600 const size_t nop_len = (size == 32 ? 7 : 8);
4602 // The function starts with a comparison of the stack pointer and a
4603 // field in the TCB. This is followed by a jump.
4606 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
4607 && fnsize > nop_len + 1)
4609 // We will call __morestack if the carry flag is set after this
4610 // comparison. We turn the comparison into an stc instruction
4612 view[fnoffset] = '\xf9';
4613 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
4615 // lea NN(%rsp),%r10
4616 // lea NN(%rsp),%r11
4617 else if ((this->match_view(view, view_size, fnoffset,
4618 lea_r10_insn, lea_r10_insn_len)
4619 || this->match_view(view, view_size, fnoffset,
4620 lea_r11_insn, lea_r11_insn_len))
4623 // This is loading an offset from the stack pointer for a
4624 // comparison. The offset is negative, so we decrease the
4625 // offset by the amount of space we need for the stack. This
4626 // means we will avoid calling __morestack if there happens to
4627 // be plenty of space on the stack already.
4628 unsigned char* pval = view + fnoffset + 4;
4629 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4630 val -= parameters->options().split_stack_adjust_size();
4631 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4635 if (!object->has_no_split_stack())
4636 object->error(_("failed to match split-stack sequence at "
4637 "section %u offset %0zx"),
4638 shndx, static_cast<size_t>(fnoffset));
4642 // We have to change the function so that it calls
4643 // __morestack_non_split instead of __morestack. The former will
4644 // allocate additional stack space.
4645 *from = "__morestack";
4646 *to = "__morestack_non_split";
4649 // The selector for x86_64 object files. Note this is never instantiated
4650 // directly. It's only used in Target_selector_x86_64_nacl, below.
4653 class Target_selector_x86_64 : public Target_selector_freebsd
4656 Target_selector_x86_64()
4657 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4659 ? "elf64-x86-64" : "elf32-x86-64"),
4661 ? "elf64-x86-64-freebsd"
4662 : "elf32-x86-64-freebsd"),
4663 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4667 do_instantiate_target()
4668 { return new Target_x86_64<size>(); }
4672 // NaCl variant. It uses different PLT contents.
4675 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4678 Output_data_plt_x86_64_nacl(Layout* layout,
4679 Output_data_got<64, false>* got,
4680 Output_data_got_plt_x86_64* got_plt,
4681 Output_data_space* got_irelative)
4682 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4683 got, got_plt, got_irelative)
4686 Output_data_plt_x86_64_nacl(Layout* layout,
4687 Output_data_got<64, false>* got,
4688 Output_data_got_plt_x86_64* got_plt,
4689 Output_data_space* got_irelative,
4690 unsigned int plt_count)
4691 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4692 got, got_plt, got_irelative,
4697 virtual unsigned int
4698 do_get_plt_entry_size() const
4699 { return plt_entry_size; }
4702 do_add_eh_frame(Layout* layout)
4704 layout->add_eh_frame_for_plt(this,
4705 this->plt_eh_frame_cie,
4706 this->plt_eh_frame_cie_size,
4708 plt_eh_frame_fde_size);
4712 do_fill_first_plt_entry(unsigned char* pov,
4713 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4714 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4716 virtual unsigned int
4717 do_fill_plt_entry(unsigned char* pov,
4718 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4719 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4720 unsigned int got_offset,
4721 unsigned int plt_offset,
4722 unsigned int plt_index);
4725 do_fill_tlsdesc_entry(unsigned char* pov,
4726 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4727 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4728 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4729 unsigned int tlsdesc_got_offset,
4730 unsigned int plt_offset);
4733 // The size of an entry in the PLT.
4734 static const int plt_entry_size = 64;
4736 // The first entry in the PLT.
4737 static const unsigned char first_plt_entry[plt_entry_size];
4739 // Other entries in the PLT for an executable.
4740 static const unsigned char plt_entry[plt_entry_size];
4742 // The reserved TLSDESC entry in the PLT for an executable.
4743 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4745 // The .eh_frame unwind information for the PLT.
4746 static const int plt_eh_frame_fde_size = 32;
4747 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4751 class Target_x86_64_nacl : public Target_x86_64<size>
4754 Target_x86_64_nacl()
4755 : Target_x86_64<size>(&x86_64_nacl_info)
4758 virtual Output_data_plt_x86_64<size>*
4759 do_make_data_plt(Layout* layout,
4760 Output_data_got<64, false>* got,
4761 Output_data_got_plt_x86_64* got_plt,
4762 Output_data_space* got_irelative)
4764 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4768 virtual Output_data_plt_x86_64<size>*
4769 do_make_data_plt(Layout* layout,
4770 Output_data_got<64, false>* got,
4771 Output_data_got_plt_x86_64* got_plt,
4772 Output_data_space* got_irelative,
4773 unsigned int plt_count)
4775 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4781 do_code_fill(section_size_type length) const;
4784 static const Target::Target_info x86_64_nacl_info;
4788 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4791 false, // is_big_endian
4792 elfcpp::EM_X86_64, // machine_code
4793 false, // has_make_symbol
4794 false, // has_resolve
4795 true, // has_code_fill
4796 true, // is_default_stack_executable
4797 true, // can_icf_inline_merge_sections
4799 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4800 0x20000, // default_text_segment_address
4801 0x10000, // abi_pagesize (overridable by -z max-page-size)
4802 0x10000, // common_pagesize (overridable by -z common-page-size)
4803 true, // isolate_execinstr
4804 0x10000000, // rosegment_gap
4805 elfcpp::SHN_UNDEF, // small_common_shndx
4806 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4807 0, // small_common_section_flags
4808 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4809 NULL, // attributes_section
4810 NULL, // attributes_vendor
4811 "_start", // entry_symbol_name
4812 32, // hash_entry_size
4816 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4819 false, // is_big_endian
4820 elfcpp::EM_X86_64, // machine_code
4821 false, // has_make_symbol
4822 false, // has_resolve
4823 true, // has_code_fill
4824 true, // is_default_stack_executable
4825 true, // can_icf_inline_merge_sections
4827 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4828 0x20000, // default_text_segment_address
4829 0x10000, // abi_pagesize (overridable by -z max-page-size)
4830 0x10000, // common_pagesize (overridable by -z common-page-size)
4831 true, // isolate_execinstr
4832 0x10000000, // rosegment_gap
4833 elfcpp::SHN_UNDEF, // small_common_shndx
4834 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4835 0, // small_common_section_flags
4836 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4837 NULL, // attributes_section
4838 NULL, // attributes_vendor
4839 "_start", // entry_symbol_name
4840 32, // hash_entry_size
4843 #define NACLMASK 0xe0 // 32-byte alignment mask.
4845 // The first entry in the PLT.
4849 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4851 0xff, 0x35, // pushq contents of memory address
4852 0, 0, 0, 0, // replaced with address of .got + 8
4853 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4854 0, 0, 0, 0, // replaced with address of .got + 16
4855 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4856 0x4d, 0x01, 0xfb, // add %r15, %r11
4857 0x41, 0xff, 0xe3, // jmpq *%r11
4859 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4860 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4862 // 32 bytes of nop to pad out to the standard size
4863 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4864 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4865 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4866 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4867 0x66, // excess data32 prefix
4873 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4875 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4876 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4878 memcpy(pov, first_plt_entry, plt_entry_size);
4879 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4881 - (plt_address + 2 + 4)));
4882 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4884 - (plt_address + 9 + 4)));
4887 // Subsequent entries in the PLT.
4891 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4893 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4894 0, 0, 0, 0, // replaced with address of symbol in .got
4895 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4896 0x4d, 0x01, 0xfb, // add %r15, %r11
4897 0x41, 0xff, 0xe3, // jmpq *%r11
4899 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4900 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4901 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4903 // Lazy GOT entries point here (32-byte aligned).
4904 0x68, // pushq immediate
4905 0, 0, 0, 0, // replaced with index into relocation table
4906 0xe9, // jmp relative
4907 0, 0, 0, 0, // replaced with offset to start of .plt0
4909 // 22 bytes of nop to pad out to the standard size.
4910 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4911 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4912 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4917 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4919 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4920 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4921 unsigned int got_offset,
4922 unsigned int plt_offset,
4923 unsigned int plt_index)
4925 memcpy(pov, plt_entry, plt_entry_size);
4926 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4927 (got_address + got_offset
4928 - (plt_address + plt_offset
4931 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4932 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4933 - (plt_offset + 38 + 4));
4938 // The reserved TLSDESC entry in the PLT.
4942 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4944 0xff, 0x35, // pushq x(%rip)
4945 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4946 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4947 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4948 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4949 0x4d, 0x01, 0xfb, // add %r15, %r11
4950 0x41, 0xff, 0xe3, // jmpq *%r11
4952 // 41 bytes of nop to pad out to the standard size.
4953 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4954 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4955 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4956 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4957 0x66, 0x66, // excess data32 prefixes
4958 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4963 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
4965 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4966 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4967 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4968 unsigned int tlsdesc_got_offset,
4969 unsigned int plt_offset)
4971 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
4972 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4974 - (plt_address + plt_offset
4976 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4978 + tlsdesc_got_offset
4979 - (plt_address + plt_offset
4983 // The .eh_frame unwind information for the PLT.
4987 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4989 0, 0, 0, 0, // Replaced with offset to .plt.
4990 0, 0, 0, 0, // Replaced with size of .plt.
4991 0, // Augmentation size.
4992 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
4993 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
4994 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
4995 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
4996 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
4997 13, // Block length.
4998 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
4999 elfcpp::DW_OP_breg16, 0, // Push %rip.
5000 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5001 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5002 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5003 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5004 elfcpp::DW_OP_lit3, // Push 3.
5005 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5006 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5007 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5011 // Return a string used to fill a code section with nops.
5012 // For NaCl, long NOPs are only valid if they do not cross
5013 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5016 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5018 return std::string(length, static_cast<char>(0x90));
5021 // The selector for x86_64-nacl object files.
5024 class Target_selector_x86_64_nacl
5025 : public Target_selector_nacl<Target_selector_x86_64<size>,
5026 Target_x86_64_nacl<size> >
5029 Target_selector_x86_64_nacl()
5030 : Target_selector_nacl<Target_selector_x86_64<size>,
5031 Target_x86_64_nacl<size> >("x86-64",
5033 ? "elf64-x86-64-nacl"
5034 : "elf32-x86-64-nacl",
5037 : "elf32_x86_64_nacl")
5041 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5042 Target_selector_x86_64_nacl<32> target_selector_x32;
5044 } // End anonymous namespace.