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 // Emit relocations for a section.
502 const Relocate_info<size, false>*,
503 unsigned int sh_type,
504 const unsigned char* prelocs,
506 Output_section* output_section,
507 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
509 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
510 section_size_type view_size,
511 unsigned char* reloc_view,
512 section_size_type reloc_view_size);
514 // Return a string used to fill a code section with nops.
516 do_code_fill(section_size_type length) const;
518 // Return whether SYM is defined by the ABI.
520 do_is_defined_by_abi(const Symbol* sym) const
521 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
523 // Return the symbol index to use for a target specific relocation.
524 // The only target specific relocation is R_X86_64_TLSDESC for a
525 // local symbol, which is an absolute reloc.
527 do_reloc_symbol_index(void*, unsigned int r_type) const
529 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
533 // Return the addend to use for a target specific relocation.
535 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
537 // Return the PLT section.
539 do_plt_address_for_global(const Symbol* gsym) const
540 { return this->plt_section()->address_for_global(gsym); }
543 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
544 { return this->plt_section()->address_for_local(relobj, symndx); }
546 // This function should be defined in targets that can use relocation
547 // types to determine (implemented in local_reloc_may_be_function_pointer
548 // and global_reloc_may_be_function_pointer)
549 // if a function's pointer is taken. ICF uses this in safe mode to only
550 // fold those functions whose pointer is defintely not taken. For x86_64
551 // pie binaries, safe ICF cannot be done by looking at relocation types.
553 do_can_check_for_function_pointers() const
554 { return !parameters->options().pie(); }
556 // Return the base for a DW_EH_PE_datarel encoding.
558 do_ehframe_datarel_base() const;
560 // Adjust -fsplit-stack code which calls non-split-stack code.
562 do_calls_non_split(Relobj* object, unsigned int shndx,
563 section_offset_type fnoffset, section_size_type fnsize,
564 const unsigned char* prelocs, size_t reloc_count,
565 unsigned char* view, section_size_type view_size,
566 std::string* from, std::string* to) const;
568 // Return the size of the GOT section.
572 gold_assert(this->got_ != NULL);
573 return this->got_->data_size();
576 // Return the number of entries in the GOT.
578 got_entry_count() const
580 if (this->got_ == NULL)
582 return this->got_size() / 8;
585 // Return the number of entries in the PLT.
587 plt_entry_count() const;
589 // Return the offset of the first non-reserved PLT entry.
591 first_plt_entry_offset() const;
593 // Return the size of each PLT entry.
595 plt_entry_size() const;
597 // Return the size of each GOT entry.
599 got_entry_size() const
602 // Create the GOT section for an incremental update.
603 Output_data_got_base*
604 init_got_plt_for_update(Symbol_table* symtab,
606 unsigned int got_count,
607 unsigned int plt_count);
609 // Reserve a GOT entry for a local symbol, and regenerate any
610 // necessary dynamic relocations.
612 reserve_local_got_entry(unsigned int got_index,
613 Sized_relobj<size, false>* obj,
615 unsigned int got_type);
617 // Reserve a GOT entry for a global symbol, and regenerate any
618 // necessary dynamic relocations.
620 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
621 unsigned int got_type);
623 // Register an existing PLT entry for a global symbol.
625 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
628 // Force a COPY relocation for a given symbol.
630 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
632 // Apply an incremental relocation.
634 apply_relocation(const Relocate_info<size, false>* relinfo,
635 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
637 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
640 typename elfcpp::Elf_types<size>::Elf_Addr address,
641 section_size_type view_size);
643 // Add a new reloc argument, returning the index in the vector.
645 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
647 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
648 return this->tlsdesc_reloc_info_.size() - 1;
651 Output_data_plt_x86_64<size>*
652 make_data_plt(Layout* layout,
653 Output_data_got<64, false>* got,
654 Output_data_got_plt_x86_64* got_plt,
655 Output_data_space* got_irelative)
657 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
660 Output_data_plt_x86_64<size>*
661 make_data_plt(Layout* layout,
662 Output_data_got<64, false>* got,
663 Output_data_got_plt_x86_64* got_plt,
664 Output_data_space* got_irelative,
665 unsigned int plt_count)
667 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
671 virtual Output_data_plt_x86_64<size>*
672 do_make_data_plt(Layout* layout,
673 Output_data_got<64, false>* got,
674 Output_data_got_plt_x86_64* got_plt,
675 Output_data_space* got_irelative)
677 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
681 virtual Output_data_plt_x86_64<size>*
682 do_make_data_plt(Layout* layout,
683 Output_data_got<64, false>* got,
684 Output_data_got_plt_x86_64* got_plt,
685 Output_data_space* got_irelative,
686 unsigned int plt_count)
688 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
694 // The class which scans relocations.
699 : issued_non_pic_error_(false)
703 get_reference_flags(unsigned int r_type);
706 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
707 Sized_relobj_file<size, false>* object,
708 unsigned int data_shndx,
709 Output_section* output_section,
710 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
711 const elfcpp::Sym<size, false>& lsym,
715 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
716 Sized_relobj_file<size, false>* object,
717 unsigned int data_shndx,
718 Output_section* output_section,
719 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
723 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
724 Target_x86_64* target,
725 Sized_relobj_file<size, false>* object,
726 unsigned int data_shndx,
727 Output_section* output_section,
728 const elfcpp::Rela<size, false>& reloc,
730 const elfcpp::Sym<size, false>& lsym);
733 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
734 Target_x86_64* target,
735 Sized_relobj_file<size, false>* object,
736 unsigned int data_shndx,
737 Output_section* output_section,
738 const elfcpp::Rela<size, false>& reloc,
744 unsupported_reloc_local(Sized_relobj_file<size, false>*,
745 unsigned int r_type);
748 unsupported_reloc_global(Sized_relobj_file<size, false>*,
749 unsigned int r_type, Symbol*);
752 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
755 possible_function_pointer_reloc(unsigned int r_type);
758 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
759 unsigned int r_type);
761 // Whether we have issued an error about a non-PIC compilation.
762 bool issued_non_pic_error_;
765 // The class which implements relocation.
770 : skip_call_tls_get_addr_(false)
775 if (this->skip_call_tls_get_addr_)
777 // FIXME: This needs to specify the location somehow.
778 gold_error(_("missing expected TLS relocation"));
782 // Do a relocation. Return false if the caller should not issue
783 // any warnings about this relocation.
785 relocate(const Relocate_info<size, false>*, unsigned int,
786 Target_x86_64*, Output_section*, size_t, const unsigned char*,
787 const Sized_symbol<size>*, const Symbol_value<size>*,
788 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
792 // Do a TLS relocation.
794 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
795 size_t relnum, const elfcpp::Rela<size, false>&,
796 unsigned int r_type, const Sized_symbol<size>*,
797 const Symbol_value<size>*,
798 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
801 // Do a TLS General-Dynamic to Initial-Exec transition.
803 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
804 Output_segment* tls_segment,
805 const elfcpp::Rela<size, false>&, unsigned int r_type,
806 typename elfcpp::Elf_types<size>::Elf_Addr value,
808 typename elfcpp::Elf_types<size>::Elf_Addr,
809 section_size_type view_size);
811 // Do a TLS General-Dynamic to Local-Exec transition.
813 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
814 Output_segment* tls_segment,
815 const elfcpp::Rela<size, false>&, unsigned int r_type,
816 typename elfcpp::Elf_types<size>::Elf_Addr value,
818 section_size_type view_size);
820 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
822 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
823 Output_segment* tls_segment,
824 const elfcpp::Rela<size, false>&, unsigned int r_type,
825 typename elfcpp::Elf_types<size>::Elf_Addr value,
827 typename elfcpp::Elf_types<size>::Elf_Addr,
828 section_size_type view_size);
830 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
832 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
833 Output_segment* tls_segment,
834 const elfcpp::Rela<size, false>&, unsigned int r_type,
835 typename elfcpp::Elf_types<size>::Elf_Addr value,
837 section_size_type view_size);
839 // Do a TLS Local-Dynamic to Local-Exec transition.
841 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
842 Output_segment* tls_segment,
843 const elfcpp::Rela<size, false>&, unsigned int r_type,
844 typename elfcpp::Elf_types<size>::Elf_Addr value,
846 section_size_type view_size);
848 // Do a TLS Initial-Exec to Local-Exec transition.
850 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
851 Output_segment* tls_segment,
852 const elfcpp::Rela<size, false>&, unsigned int r_type,
853 typename elfcpp::Elf_types<size>::Elf_Addr value,
855 section_size_type view_size);
857 // This is set if we should skip the next reloc, which should be a
858 // PLT32 reloc against ___tls_get_addr.
859 bool skip_call_tls_get_addr_;
862 // A class which returns the size required for a relocation type,
863 // used while scanning relocs during a relocatable link.
864 class Relocatable_size_for_reloc
868 get_size_for_reloc(unsigned int, Relobj*);
871 // Check if relocation against this symbol is a candidate for
873 // mov foo@GOTPCREL(%rip), %reg
874 // to lea foo(%rip), %reg.
876 can_convert_mov_to_lea(const Symbol* gsym)
878 gold_assert(gsym != NULL);
879 return (gsym->type() != elfcpp::STT_GNU_IFUNC
880 && !gsym->is_undefined ()
881 && !gsym->is_from_dynobj()
882 && !gsym->is_preemptible()
883 && (!parameters->options().shared()
884 || (gsym->visibility() != elfcpp::STV_DEFAULT
885 && gsym->visibility() != elfcpp::STV_PROTECTED)
886 || parameters->options().Bsymbolic())
887 && strcmp(gsym->name(), "_DYNAMIC") != 0);
890 // Adjust TLS relocation type based on the options and whether this
891 // is a local symbol.
892 static tls::Tls_optimization
893 optimize_tls_reloc(bool is_final, int r_type);
895 // Get the GOT section, creating it if necessary.
896 Output_data_got<64, false>*
897 got_section(Symbol_table*, Layout*);
899 // Get the GOT PLT section.
900 Output_data_got_plt_x86_64*
901 got_plt_section() const
903 gold_assert(this->got_plt_ != NULL);
904 return this->got_plt_;
907 // Get the GOT section for TLSDESC entries.
908 Output_data_got<64, false>*
909 got_tlsdesc_section() const
911 gold_assert(this->got_tlsdesc_ != NULL);
912 return this->got_tlsdesc_;
915 // Create the PLT section.
917 make_plt_section(Symbol_table* symtab, Layout* layout);
919 // Create a PLT entry for a global symbol.
921 make_plt_entry(Symbol_table*, Layout*, Symbol*);
923 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
925 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
926 Sized_relobj_file<size, false>* relobj,
927 unsigned int local_sym_index);
929 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
931 define_tls_base_symbol(Symbol_table*, Layout*);
933 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
935 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
937 // Create a GOT entry for the TLS module index.
939 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
940 Sized_relobj_file<size, false>* object);
942 // Get the PLT section.
943 Output_data_plt_x86_64<size>*
946 gold_assert(this->plt_ != NULL);
950 // Get the dynamic reloc section, creating it if necessary.
952 rela_dyn_section(Layout*);
954 // Get the section to use for TLSDESC relocations.
956 rela_tlsdesc_section(Layout*) const;
958 // Get the section to use for IRELATIVE relocations.
960 rela_irelative_section(Layout*);
962 // Add a potential copy relocation.
964 copy_reloc(Symbol_table* symtab, Layout* layout,
965 Sized_relobj_file<size, false>* object,
966 unsigned int shndx, Output_section* output_section,
967 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
969 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
970 this->copy_relocs_.copy_reloc(symtab, layout,
971 symtab->get_sized_symbol<size>(sym),
972 object, shndx, output_section,
973 r_type, reloc.get_r_offset(),
974 reloc.get_r_addend(),
975 this->rela_dyn_section(layout));
978 // Information about this specific target which we pass to the
979 // general Target structure.
980 static const Target::Target_info x86_64_info;
982 // The types of GOT entries needed for this platform.
983 // These values are exposed to the ABI in an incremental link.
984 // Do not renumber existing values without changing the version
985 // number of the .gnu_incremental_inputs section.
988 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
989 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
990 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
991 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
994 // This type is used as the argument to the target specific
995 // relocation routines. The only target specific reloc is
996 // R_X86_64_TLSDESC against a local symbol.
999 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1000 : object(a_object), r_sym(a_r_sym)
1003 // The object in which the local symbol is defined.
1004 Sized_relobj_file<size, false>* object;
1005 // The local symbol index in the object.
1010 Output_data_got<64, false>* got_;
1012 Output_data_plt_x86_64<size>* plt_;
1013 // The GOT PLT section.
1014 Output_data_got_plt_x86_64* got_plt_;
1015 // The GOT section for IRELATIVE relocations.
1016 Output_data_space* got_irelative_;
1017 // The GOT section for TLSDESC relocations.
1018 Output_data_got<64, false>* got_tlsdesc_;
1019 // The _GLOBAL_OFFSET_TABLE_ symbol.
1020 Symbol* global_offset_table_;
1021 // The dynamic reloc section.
1022 Reloc_section* rela_dyn_;
1023 // The section to use for IRELATIVE relocs.
1024 Reloc_section* rela_irelative_;
1025 // Relocs saved to avoid a COPY reloc.
1026 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1027 // Offset of the GOT entry for the TLS module index.
1028 unsigned int got_mod_index_offset_;
1029 // We handle R_X86_64_TLSDESC against a local symbol as a target
1030 // specific relocation. Here we store the object and local symbol
1031 // index for the relocation.
1032 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1033 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1034 bool tls_base_symbol_defined_;
1038 const Target::Target_info Target_x86_64<64>::x86_64_info =
1041 false, // is_big_endian
1042 elfcpp::EM_X86_64, // machine_code
1043 false, // has_make_symbol
1044 false, // has_resolve
1045 true, // has_code_fill
1046 true, // is_default_stack_executable
1047 true, // can_icf_inline_merge_sections
1049 "/lib/ld64.so.1", // program interpreter
1050 0x400000, // default_text_segment_address
1051 0x1000, // abi_pagesize (overridable by -z max-page-size)
1052 0x1000, // common_pagesize (overridable by -z common-page-size)
1053 false, // isolate_execinstr
1055 elfcpp::SHN_UNDEF, // small_common_shndx
1056 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1057 0, // small_common_section_flags
1058 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1059 NULL, // attributes_section
1060 NULL, // attributes_vendor
1061 "_start", // entry_symbol_name
1062 32, // hash_entry_size
1066 const Target::Target_info Target_x86_64<32>::x86_64_info =
1069 false, // is_big_endian
1070 elfcpp::EM_X86_64, // machine_code
1071 false, // has_make_symbol
1072 false, // has_resolve
1073 true, // has_code_fill
1074 true, // is_default_stack_executable
1075 true, // can_icf_inline_merge_sections
1077 "/libx32/ldx32.so.1", // program interpreter
1078 0x400000, // default_text_segment_address
1079 0x1000, // abi_pagesize (overridable by -z max-page-size)
1080 0x1000, // common_pagesize (overridable by -z common-page-size)
1081 false, // isolate_execinstr
1083 elfcpp::SHN_UNDEF, // small_common_shndx
1084 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1085 0, // small_common_section_flags
1086 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1087 NULL, // attributes_section
1088 NULL, // attributes_vendor
1089 "_start", // entry_symbol_name
1090 32, // hash_entry_size
1093 // This is called when a new output section is created. This is where
1094 // we handle the SHF_X86_64_LARGE.
1098 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1100 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1101 os->set_is_large_section();
1104 // Get the GOT section, creating it if necessary.
1107 Output_data_got<64, false>*
1108 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1110 if (this->got_ == NULL)
1112 gold_assert(symtab != NULL && layout != NULL);
1114 // When using -z now, we can treat .got.plt as a relro section.
1115 // Without -z now, it is modified after program startup by lazy
1117 bool is_got_plt_relro = parameters->options().now();
1118 Output_section_order got_order = (is_got_plt_relro
1120 : ORDER_RELRO_LAST);
1121 Output_section_order got_plt_order = (is_got_plt_relro
1123 : ORDER_NON_RELRO_FIRST);
1125 this->got_ = new Output_data_got<64, false>();
1127 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1129 | elfcpp::SHF_WRITE),
1130 this->got_, got_order, true);
1132 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1133 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1135 | elfcpp::SHF_WRITE),
1136 this->got_plt_, got_plt_order,
1139 // The first three entries are reserved.
1140 this->got_plt_->set_current_data_size(3 * 8);
1142 if (!is_got_plt_relro)
1144 // Those bytes can go into the relro segment.
1145 layout->increase_relro(3 * 8);
1148 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1149 this->global_offset_table_ =
1150 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1151 Symbol_table::PREDEFINED,
1153 0, 0, elfcpp::STT_OBJECT,
1155 elfcpp::STV_HIDDEN, 0,
1158 // If there are any IRELATIVE relocations, they get GOT entries
1159 // in .got.plt after the jump slot entries.
1160 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1161 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1163 | elfcpp::SHF_WRITE),
1164 this->got_irelative_,
1165 got_plt_order, is_got_plt_relro);
1167 // If there are any TLSDESC relocations, they get GOT entries in
1168 // .got.plt after the jump slot and IRELATIVE entries.
1169 this->got_tlsdesc_ = new Output_data_got<64, false>();
1170 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1172 | elfcpp::SHF_WRITE),
1174 got_plt_order, is_got_plt_relro);
1180 // Get the dynamic reloc section, creating it if necessary.
1183 typename Target_x86_64<size>::Reloc_section*
1184 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1186 if (this->rela_dyn_ == NULL)
1188 gold_assert(layout != NULL);
1189 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1190 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1191 elfcpp::SHF_ALLOC, this->rela_dyn_,
1192 ORDER_DYNAMIC_RELOCS, false);
1194 return this->rela_dyn_;
1197 // Get the section to use for IRELATIVE relocs, creating it if
1198 // necessary. These go in .rela.dyn, but only after all other dynamic
1199 // relocations. They need to follow the other dynamic relocations so
1200 // that they can refer to global variables initialized by those
1204 typename Target_x86_64<size>::Reloc_section*
1205 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1207 if (this->rela_irelative_ == NULL)
1209 // Make sure we have already created the dynamic reloc section.
1210 this->rela_dyn_section(layout);
1211 this->rela_irelative_ = new Reloc_section(false);
1212 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1213 elfcpp::SHF_ALLOC, this->rela_irelative_,
1214 ORDER_DYNAMIC_RELOCS, false);
1215 gold_assert(this->rela_dyn_->output_section()
1216 == this->rela_irelative_->output_section());
1218 return this->rela_irelative_;
1221 // Write the first three reserved words of the .got.plt section.
1222 // The remainder of the section is written while writing the PLT
1223 // in Output_data_plt_i386::do_write.
1226 Output_data_got_plt_x86_64::do_write(Output_file* of)
1228 // The first entry in the GOT is the address of the .dynamic section
1229 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1230 // We saved space for them when we created the section in
1231 // Target_x86_64::got_section.
1232 const off_t got_file_offset = this->offset();
1233 gold_assert(this->data_size() >= 24);
1234 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1235 Output_section* dynamic = this->layout_->dynamic_section();
1236 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1237 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1238 memset(got_view + 8, 0, 16);
1239 of->write_output_view(got_file_offset, 24, got_view);
1242 // Initialize the PLT section.
1246 Output_data_plt_x86_64<size>::init(Layout* layout)
1248 this->rel_ = new Reloc_section(false);
1249 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1250 elfcpp::SHF_ALLOC, this->rel_,
1251 ORDER_DYNAMIC_PLT_RELOCS, false);
1256 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1258 os->set_entsize(this->get_plt_entry_size());
1261 // Add an entry to the PLT.
1265 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1268 gold_assert(!gsym->has_plt_offset());
1270 unsigned int plt_index;
1272 section_offset_type got_offset;
1274 unsigned int* pcount;
1275 unsigned int offset;
1276 unsigned int reserved;
1277 Output_section_data_build* got;
1278 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1279 && gsym->can_use_relative_reloc(false))
1281 pcount = &this->irelative_count_;
1284 got = this->got_irelative_;
1288 pcount = &this->count_;
1291 got = this->got_plt_;
1294 if (!this->is_data_size_valid())
1296 // Note that when setting the PLT offset for a non-IRELATIVE
1297 // entry we skip the initial reserved PLT entry.
1298 plt_index = *pcount + offset;
1299 plt_offset = plt_index * this->get_plt_entry_size();
1303 got_offset = (plt_index - offset + reserved) * 8;
1304 gold_assert(got_offset == got->current_data_size());
1306 // Every PLT entry needs a GOT entry which points back to the PLT
1307 // entry (this will be changed by the dynamic linker, normally
1308 // lazily when the function is called).
1309 got->set_current_data_size(got_offset + 8);
1313 // FIXME: This is probably not correct for IRELATIVE relocs.
1315 // For incremental updates, find an available slot.
1316 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1317 this->get_plt_entry_size(), 0);
1318 if (plt_offset == -1)
1319 gold_fallback(_("out of patch space (PLT);"
1320 " relink with --incremental-full"));
1322 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1323 // can be calculated from the PLT index, adjusting for the three
1324 // reserved entries at the beginning of the GOT.
1325 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1326 got_offset = (plt_index - offset + reserved) * 8;
1329 gsym->set_plt_offset(plt_offset);
1331 // Every PLT entry needs a reloc.
1332 this->add_relocation(symtab, layout, gsym, got_offset);
1334 // Note that we don't need to save the symbol. The contents of the
1335 // PLT are independent of which symbols are used. The symbols only
1336 // appear in the relocations.
1339 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1344 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1345 Symbol_table* symtab,
1347 Sized_relobj_file<size, false>* relobj,
1348 unsigned int local_sym_index)
1350 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1351 ++this->irelative_count_;
1353 section_offset_type got_offset = this->got_irelative_->current_data_size();
1355 // Every PLT entry needs a GOT entry which points back to the PLT
1357 this->got_irelative_->set_current_data_size(got_offset + 8);
1359 // Every PLT entry needs a reloc.
1360 Reloc_section* rela = this->rela_irelative(symtab, layout);
1361 rela->add_symbolless_local_addend(relobj, local_sym_index,
1362 elfcpp::R_X86_64_IRELATIVE,
1363 this->got_irelative_, got_offset, 0);
1368 // Add the relocation for a PLT entry.
1372 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1375 unsigned int got_offset)
1377 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1378 && gsym->can_use_relative_reloc(false))
1380 Reloc_section* rela = this->rela_irelative(symtab, layout);
1381 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1382 this->got_irelative_, got_offset, 0);
1386 gsym->set_needs_dynsym_entry();
1387 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1392 // Return where the TLSDESC relocations should go, creating it if
1393 // necessary. These follow the JUMP_SLOT relocations.
1396 typename Output_data_plt_x86_64<size>::Reloc_section*
1397 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1399 if (this->tlsdesc_rel_ == NULL)
1401 this->tlsdesc_rel_ = new Reloc_section(false);
1402 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1403 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1404 ORDER_DYNAMIC_PLT_RELOCS, false);
1405 gold_assert(this->tlsdesc_rel_->output_section()
1406 == this->rel_->output_section());
1408 return this->tlsdesc_rel_;
1411 // Return where the IRELATIVE relocations should go in the PLT. These
1412 // follow the JUMP_SLOT and the TLSDESC relocations.
1415 typename Output_data_plt_x86_64<size>::Reloc_section*
1416 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1419 if (this->irelative_rel_ == NULL)
1421 // Make sure we have a place for the TLSDESC relocations, in
1422 // case we see any later on.
1423 this->rela_tlsdesc(layout);
1424 this->irelative_rel_ = new Reloc_section(false);
1425 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1426 elfcpp::SHF_ALLOC, this->irelative_rel_,
1427 ORDER_DYNAMIC_PLT_RELOCS, false);
1428 gold_assert(this->irelative_rel_->output_section()
1429 == this->rel_->output_section());
1431 if (parameters->doing_static_link())
1433 // A statically linked executable will only have a .rela.plt
1434 // section to hold R_X86_64_IRELATIVE relocs for
1435 // STT_GNU_IFUNC symbols. The library will use these
1436 // symbols to locate the IRELATIVE relocs at program startup
1438 symtab->define_in_output_data("__rela_iplt_start", NULL,
1439 Symbol_table::PREDEFINED,
1440 this->irelative_rel_, 0, 0,
1441 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1442 elfcpp::STV_HIDDEN, 0, false, true);
1443 symtab->define_in_output_data("__rela_iplt_end", NULL,
1444 Symbol_table::PREDEFINED,
1445 this->irelative_rel_, 0, 0,
1446 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1447 elfcpp::STV_HIDDEN, 0, true, true);
1450 return this->irelative_rel_;
1453 // Return the PLT address to use for a global symbol.
1457 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1459 uint64_t offset = 0;
1460 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1461 && gsym->can_use_relative_reloc(false))
1462 offset = (this->count_ + 1) * this->get_plt_entry_size();
1463 return this->address() + offset + gsym->plt_offset();
1466 // Return the PLT address to use for a local symbol. These are always
1467 // IRELATIVE relocs.
1471 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
1474 return (this->address()
1475 + (this->count_ + 1) * this->get_plt_entry_size()
1476 + object->local_plt_offset(r_sym));
1479 // Set the final size.
1482 Output_data_plt_x86_64<size>::set_final_data_size()
1484 unsigned int count = this->count_ + this->irelative_count_;
1485 if (this->has_tlsdesc_entry())
1487 this->set_data_size((count + 1) * this->get_plt_entry_size());
1490 // The first entry in the PLT for an executable.
1494 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1496 // From AMD64 ABI Draft 0.98, page 76
1497 0xff, 0x35, // pushq contents of memory address
1498 0, 0, 0, 0, // replaced with address of .got + 8
1499 0xff, 0x25, // jmp indirect
1500 0, 0, 0, 0, // replaced with address of .got + 16
1501 0x90, 0x90, 0x90, 0x90 // noop (x4)
1506 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1508 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1509 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1511 memcpy(pov, first_plt_entry, plt_entry_size);
1512 // We do a jmp relative to the PC at the end of this instruction.
1513 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1515 - (plt_address + 6)));
1516 elfcpp::Swap<32, false>::writeval(pov + 8,
1518 - (plt_address + 12)));
1521 // Subsequent entries in the PLT for an executable.
1525 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1527 // From AMD64 ABI Draft 0.98, page 76
1528 0xff, 0x25, // jmpq indirect
1529 0, 0, 0, 0, // replaced with address of symbol in .got
1530 0x68, // pushq immediate
1531 0, 0, 0, 0, // replaced with offset into relocation table
1532 0xe9, // jmpq relative
1533 0, 0, 0, 0 // replaced with offset to start of .plt
1538 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1540 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1541 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1542 unsigned int got_offset,
1543 unsigned int plt_offset,
1544 unsigned int plt_index)
1546 // Check PC-relative offset overflow in PLT entry.
1547 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1548 - (plt_address + plt_offset + 6));
1549 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1550 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1553 memcpy(pov, plt_entry, plt_entry_size);
1554 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1555 plt_got_pcrel_offset);
1557 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1558 elfcpp::Swap<32, false>::writeval(pov + 12,
1559 - (plt_offset + plt_entry_size));
1564 // The reserved TLSDESC entry in the PLT for an executable.
1568 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1570 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1571 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1572 0xff, 0x35, // pushq x(%rip)
1573 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1574 0xff, 0x25, // jmpq *y(%rip)
1575 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1582 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1584 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1585 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1586 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1587 unsigned int tlsdesc_got_offset,
1588 unsigned int plt_offset)
1590 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1591 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1593 - (plt_address + plt_offset
1595 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1597 + tlsdesc_got_offset
1598 - (plt_address + plt_offset
1602 // The .eh_frame unwind information for the PLT.
1606 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1609 'z', // Augmentation: augmentation size included.
1610 'R', // Augmentation: FDE encoding included.
1611 '\0', // End of augmentation string.
1612 1, // Code alignment factor.
1613 0x78, // Data alignment factor.
1614 16, // Return address column.
1615 1, // Augmentation size.
1616 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1617 | elfcpp::DW_EH_PE_sdata4),
1618 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1619 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1620 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1626 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1628 0, 0, 0, 0, // Replaced with offset to .plt.
1629 0, 0, 0, 0, // Replaced with size of .plt.
1630 0, // Augmentation size.
1631 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1632 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1633 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1634 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1635 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1636 11, // Block length.
1637 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1638 elfcpp::DW_OP_breg16, 0, // Push %rip.
1639 elfcpp::DW_OP_lit15, // Push 0xf.
1640 elfcpp::DW_OP_and, // & (%rip & 0xf).
1641 elfcpp::DW_OP_lit11, // Push 0xb.
1642 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1643 elfcpp::DW_OP_lit3, // Push 3.
1644 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1645 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1646 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1652 // Write out the PLT. This uses the hand-coded instructions above,
1653 // and adjusts them as needed. This is specified by the AMD64 ABI.
1657 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1659 const off_t offset = this->offset();
1660 const section_size_type oview_size =
1661 convert_to_section_size_type(this->data_size());
1662 unsigned char* const oview = of->get_output_view(offset, oview_size);
1664 const off_t got_file_offset = this->got_plt_->offset();
1665 gold_assert(parameters->incremental_update()
1666 || (got_file_offset + this->got_plt_->data_size()
1667 == this->got_irelative_->offset()));
1668 const section_size_type got_size =
1669 convert_to_section_size_type(this->got_plt_->data_size()
1670 + this->got_irelative_->data_size());
1671 unsigned char* const got_view = of->get_output_view(got_file_offset,
1674 unsigned char* pov = oview;
1676 // The base address of the .plt section.
1677 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1678 // The base address of the .got section.
1679 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1680 // The base address of the PLT portion of the .got section,
1681 // which is where the GOT pointer will point, and where the
1682 // three reserved GOT entries are located.
1683 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1684 = this->got_plt_->address();
1686 this->fill_first_plt_entry(pov, got_address, plt_address);
1687 pov += this->get_plt_entry_size();
1689 // The first three entries in the GOT are reserved, and are written
1690 // by Output_data_got_plt_x86_64::do_write.
1691 unsigned char* got_pov = got_view + 24;
1693 unsigned int plt_offset = this->get_plt_entry_size();
1694 unsigned int got_offset = 24;
1695 const unsigned int count = this->count_ + this->irelative_count_;
1696 for (unsigned int plt_index = 0;
1699 pov += this->get_plt_entry_size(),
1701 plt_offset += this->get_plt_entry_size(),
1704 // Set and adjust the PLT entry itself.
1705 unsigned int lazy_offset = this->fill_plt_entry(pov,
1706 got_address, plt_address,
1707 got_offset, plt_offset,
1710 // Set the entry in the GOT.
1711 elfcpp::Swap<64, false>::writeval(got_pov,
1712 plt_address + plt_offset + lazy_offset);
1715 if (this->has_tlsdesc_entry())
1717 // Set and adjust the reserved TLSDESC PLT entry.
1718 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1719 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
1720 tlsdesc_got_offset, plt_offset);
1721 pov += this->get_plt_entry_size();
1724 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1725 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1727 of->write_output_view(offset, oview_size, oview);
1728 of->write_output_view(got_file_offset, got_size, got_view);
1731 // Create the PLT section.
1735 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1737 if (this->plt_ == NULL)
1739 // Create the GOT sections first.
1740 this->got_section(symtab, layout);
1742 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
1743 this->got_irelative_);
1745 // Add unwind information if requested.
1746 if (parameters->options().ld_generated_unwind_info())
1747 this->plt_->add_eh_frame(layout);
1749 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1751 | elfcpp::SHF_EXECINSTR),
1752 this->plt_, ORDER_PLT, false);
1754 // Make the sh_info field of .rela.plt point to .plt.
1755 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1756 rela_plt_os->set_info_section(this->plt_->output_section());
1760 // Return the section for TLSDESC relocations.
1763 typename Target_x86_64<size>::Reloc_section*
1764 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1766 return this->plt_section()->rela_tlsdesc(layout);
1769 // Create a PLT entry for a global symbol.
1773 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1776 if (gsym->has_plt_offset())
1779 if (this->plt_ == NULL)
1780 this->make_plt_section(symtab, layout);
1782 this->plt_->add_entry(symtab, layout, gsym);
1785 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1789 Target_x86_64<size>::make_local_ifunc_plt_entry(
1790 Symbol_table* symtab, Layout* layout,
1791 Sized_relobj_file<size, false>* relobj,
1792 unsigned int local_sym_index)
1794 if (relobj->local_has_plt_offset(local_sym_index))
1796 if (this->plt_ == NULL)
1797 this->make_plt_section(symtab, layout);
1798 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1801 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1804 // Return the number of entries in the PLT.
1808 Target_x86_64<size>::plt_entry_count() const
1810 if (this->plt_ == NULL)
1812 return this->plt_->entry_count();
1815 // Return the offset of the first non-reserved PLT entry.
1819 Target_x86_64<size>::first_plt_entry_offset() const
1821 return this->plt_->first_plt_entry_offset();
1824 // Return the size of each PLT entry.
1828 Target_x86_64<size>::plt_entry_size() const
1830 return this->plt_->get_plt_entry_size();
1833 // Create the GOT and PLT sections for an incremental update.
1836 Output_data_got_base*
1837 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1839 unsigned int got_count,
1840 unsigned int plt_count)
1842 gold_assert(this->got_ == NULL);
1844 this->got_ = new Output_data_got<64, false>(got_count * 8);
1845 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1847 | elfcpp::SHF_WRITE),
1848 this->got_, ORDER_RELRO_LAST,
1851 // Add the three reserved entries.
1852 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
1853 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1855 | elfcpp::SHF_WRITE),
1856 this->got_plt_, ORDER_NON_RELRO_FIRST,
1859 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1860 this->global_offset_table_ =
1861 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1862 Symbol_table::PREDEFINED,
1864 0, 0, elfcpp::STT_OBJECT,
1866 elfcpp::STV_HIDDEN, 0,
1869 // If there are any TLSDESC relocations, they get GOT entries in
1870 // .got.plt after the jump slot entries.
1871 // FIXME: Get the count for TLSDESC entries.
1872 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1873 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1874 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1876 ORDER_NON_RELRO_FIRST, false);
1878 // If there are any IRELATIVE relocations, they get GOT entries in
1879 // .got.plt after the jump slot and TLSDESC entries.
1880 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1881 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1882 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1883 this->got_irelative_,
1884 ORDER_NON_RELRO_FIRST, false);
1886 // Create the PLT section.
1887 this->plt_ = this->make_data_plt(layout, this->got_,
1889 this->got_irelative_,
1892 // Add unwind information if requested.
1893 if (parameters->options().ld_generated_unwind_info())
1894 this->plt_->add_eh_frame(layout);
1896 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1897 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1898 this->plt_, ORDER_PLT, false);
1900 // Make the sh_info field of .rela.plt point to .plt.
1901 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1902 rela_plt_os->set_info_section(this->plt_->output_section());
1904 // Create the rela_dyn section.
1905 this->rela_dyn_section(layout);
1910 // Reserve a GOT entry for a local symbol, and regenerate any
1911 // necessary dynamic relocations.
1915 Target_x86_64<size>::reserve_local_got_entry(
1916 unsigned int got_index,
1917 Sized_relobj<size, false>* obj,
1919 unsigned int got_type)
1921 unsigned int got_offset = got_index * 8;
1922 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1924 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1927 case GOT_TYPE_STANDARD:
1928 if (parameters->options().output_is_position_independent())
1929 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1930 this->got_, got_offset, 0, false);
1932 case GOT_TYPE_TLS_OFFSET:
1933 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1934 this->got_, got_offset, 0);
1936 case GOT_TYPE_TLS_PAIR:
1937 this->got_->reserve_slot(got_index + 1);
1938 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1939 this->got_, got_offset, 0);
1941 case GOT_TYPE_TLS_DESC:
1942 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1943 // this->got_->reserve_slot(got_index + 1);
1944 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1945 // this->got_, got_offset, 0);
1952 // Reserve a GOT entry for a global symbol, and regenerate any
1953 // necessary dynamic relocations.
1957 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1959 unsigned int got_type)
1961 unsigned int got_offset = got_index * 8;
1962 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1964 this->got_->reserve_global(got_index, gsym, got_type);
1967 case GOT_TYPE_STANDARD:
1968 if (!gsym->final_value_is_known())
1970 if (gsym->is_from_dynobj()
1971 || gsym->is_undefined()
1972 || gsym->is_preemptible()
1973 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1974 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1975 this->got_, got_offset, 0);
1977 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1978 this->got_, got_offset, 0, false);
1981 case GOT_TYPE_TLS_OFFSET:
1982 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1983 this->got_, got_offset, 0, false);
1985 case GOT_TYPE_TLS_PAIR:
1986 this->got_->reserve_slot(got_index + 1);
1987 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1988 this->got_, got_offset, 0, false);
1989 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1990 this->got_, got_offset + 8, 0, false);
1992 case GOT_TYPE_TLS_DESC:
1993 this->got_->reserve_slot(got_index + 1);
1994 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1995 this->got_, got_offset, 0, false);
2002 // Register an existing PLT entry for a global symbol.
2006 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2008 unsigned int plt_index,
2011 gold_assert(this->plt_ != NULL);
2012 gold_assert(!gsym->has_plt_offset());
2014 this->plt_->reserve_slot(plt_index);
2016 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2018 unsigned int got_offset = (plt_index + 3) * 8;
2019 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2022 // Force a COPY relocation for a given symbol.
2026 Target_x86_64<size>::emit_copy_reloc(
2027 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2029 this->copy_relocs_.emit_copy_reloc(symtab,
2030 symtab->get_sized_symbol<size>(sym),
2033 this->rela_dyn_section(NULL));
2036 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2040 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2043 if (this->tls_base_symbol_defined_)
2046 Output_segment* tls_segment = layout->tls_segment();
2047 if (tls_segment != NULL)
2049 bool is_exec = parameters->options().output_is_executable();
2050 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2051 Symbol_table::PREDEFINED,
2055 elfcpp::STV_HIDDEN, 0,
2057 ? Symbol::SEGMENT_END
2058 : Symbol::SEGMENT_START),
2061 this->tls_base_symbol_defined_ = true;
2064 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2068 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2071 if (this->plt_ == NULL)
2072 this->make_plt_section(symtab, layout);
2074 if (!this->plt_->has_tlsdesc_entry())
2076 // Allocate the TLSDESC_GOT entry.
2077 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2078 unsigned int got_offset = got->add_constant(0);
2080 // Allocate the TLSDESC_PLT entry.
2081 this->plt_->reserve_tlsdesc_entry(got_offset);
2085 // Create a GOT entry for the TLS module index.
2089 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2090 Sized_relobj_file<size, false>* object)
2092 if (this->got_mod_index_offset_ == -1U)
2094 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2095 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2096 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2097 unsigned int got_offset = got->add_constant(0);
2098 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2100 got->add_constant(0);
2101 this->got_mod_index_offset_ = got_offset;
2103 return this->got_mod_index_offset_;
2106 // Optimize the TLS relocation type based on what we know about the
2107 // symbol. IS_FINAL is true if the final address of this symbol is
2108 // known at link time.
2111 tls::Tls_optimization
2112 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2114 // If we are generating a shared library, then we can't do anything
2116 if (parameters->options().shared())
2117 return tls::TLSOPT_NONE;
2121 case elfcpp::R_X86_64_TLSGD:
2122 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2123 case elfcpp::R_X86_64_TLSDESC_CALL:
2124 // These are General-Dynamic which permits fully general TLS
2125 // access. Since we know that we are generating an executable,
2126 // we can convert this to Initial-Exec. If we also know that
2127 // this is a local symbol, we can further switch to Local-Exec.
2129 return tls::TLSOPT_TO_LE;
2130 return tls::TLSOPT_TO_IE;
2132 case elfcpp::R_X86_64_TLSLD:
2133 // This is Local-Dynamic, which refers to a local symbol in the
2134 // dynamic TLS block. Since we know that we generating an
2135 // executable, we can switch to Local-Exec.
2136 return tls::TLSOPT_TO_LE;
2138 case elfcpp::R_X86_64_DTPOFF32:
2139 case elfcpp::R_X86_64_DTPOFF64:
2140 // Another Local-Dynamic reloc.
2141 return tls::TLSOPT_TO_LE;
2143 case elfcpp::R_X86_64_GOTTPOFF:
2144 // These are Initial-Exec relocs which get the thread offset
2145 // from the GOT. If we know that we are linking against the
2146 // local symbol, we can switch to Local-Exec, which links the
2147 // thread offset into the instruction.
2149 return tls::TLSOPT_TO_LE;
2150 return tls::TLSOPT_NONE;
2152 case elfcpp::R_X86_64_TPOFF32:
2153 // When we already have Local-Exec, there is nothing further we
2155 return tls::TLSOPT_NONE;
2162 // Get the Reference_flags for a particular relocation.
2166 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2170 case elfcpp::R_X86_64_NONE:
2171 case elfcpp::R_X86_64_GNU_VTINHERIT:
2172 case elfcpp::R_X86_64_GNU_VTENTRY:
2173 case elfcpp::R_X86_64_GOTPC32:
2174 case elfcpp::R_X86_64_GOTPC64:
2175 // No symbol reference.
2178 case elfcpp::R_X86_64_64:
2179 case elfcpp::R_X86_64_32:
2180 case elfcpp::R_X86_64_32S:
2181 case elfcpp::R_X86_64_16:
2182 case elfcpp::R_X86_64_8:
2183 return Symbol::ABSOLUTE_REF;
2185 case elfcpp::R_X86_64_PC64:
2186 case elfcpp::R_X86_64_PC32:
2187 case elfcpp::R_X86_64_PC32_BND:
2188 case elfcpp::R_X86_64_PC16:
2189 case elfcpp::R_X86_64_PC8:
2190 case elfcpp::R_X86_64_GOTOFF64:
2191 return Symbol::RELATIVE_REF;
2193 case elfcpp::R_X86_64_PLT32:
2194 case elfcpp::R_X86_64_PLT32_BND:
2195 case elfcpp::R_X86_64_PLTOFF64:
2196 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2198 case elfcpp::R_X86_64_GOT64:
2199 case elfcpp::R_X86_64_GOT32:
2200 case elfcpp::R_X86_64_GOTPCREL64:
2201 case elfcpp::R_X86_64_GOTPCREL:
2202 case elfcpp::R_X86_64_GOTPCRELX:
2203 case elfcpp::R_X86_64_REX_GOTPCRELX:
2204 case elfcpp::R_X86_64_GOTPLT64:
2206 return Symbol::ABSOLUTE_REF;
2208 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2209 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2210 case elfcpp::R_X86_64_TLSDESC_CALL:
2211 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2212 case elfcpp::R_X86_64_DTPOFF32:
2213 case elfcpp::R_X86_64_DTPOFF64:
2214 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2215 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2216 return Symbol::TLS_REF;
2218 case elfcpp::R_X86_64_COPY:
2219 case elfcpp::R_X86_64_GLOB_DAT:
2220 case elfcpp::R_X86_64_JUMP_SLOT:
2221 case elfcpp::R_X86_64_RELATIVE:
2222 case elfcpp::R_X86_64_IRELATIVE:
2223 case elfcpp::R_X86_64_TPOFF64:
2224 case elfcpp::R_X86_64_DTPMOD64:
2225 case elfcpp::R_X86_64_TLSDESC:
2226 case elfcpp::R_X86_64_SIZE32:
2227 case elfcpp::R_X86_64_SIZE64:
2229 // Not expected. We will give an error later.
2234 // Report an unsupported relocation against a local symbol.
2238 Target_x86_64<size>::Scan::unsupported_reloc_local(
2239 Sized_relobj_file<size, false>* object,
2240 unsigned int r_type)
2242 gold_error(_("%s: unsupported reloc %u against local symbol"),
2243 object->name().c_str(), r_type);
2246 // We are about to emit a dynamic relocation of type R_TYPE. If the
2247 // dynamic linker does not support it, issue an error. The GNU linker
2248 // only issues a non-PIC error for an allocated read-only section.
2249 // Here we know the section is allocated, but we don't know that it is
2250 // read-only. But we check for all the relocation types which the
2251 // glibc dynamic linker supports, so it seems appropriate to issue an
2252 // error even if the section is not read-only. If GSYM is not NULL,
2253 // it is the symbol the relocation is against; if it is NULL, the
2254 // relocation is against a local symbol.
2258 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2263 // These are the relocation types supported by glibc for x86_64
2264 // which should always work.
2265 case elfcpp::R_X86_64_RELATIVE:
2266 case elfcpp::R_X86_64_IRELATIVE:
2267 case elfcpp::R_X86_64_GLOB_DAT:
2268 case elfcpp::R_X86_64_JUMP_SLOT:
2269 case elfcpp::R_X86_64_DTPMOD64:
2270 case elfcpp::R_X86_64_DTPOFF64:
2271 case elfcpp::R_X86_64_TPOFF64:
2272 case elfcpp::R_X86_64_64:
2273 case elfcpp::R_X86_64_COPY:
2276 // glibc supports these reloc types, but they can overflow.
2277 case elfcpp::R_X86_64_PC32:
2278 case elfcpp::R_X86_64_PC32_BND:
2279 // A PC relative reference is OK against a local symbol or if
2280 // the symbol is defined locally.
2282 || (!gsym->is_from_dynobj()
2283 && !gsym->is_undefined()
2284 && !gsym->is_preemptible()))
2287 case elfcpp::R_X86_64_32:
2288 // R_X86_64_32 is OK for x32.
2289 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2291 if (this->issued_non_pic_error_)
2293 gold_assert(parameters->options().output_is_position_independent());
2295 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2296 "overflow at runtime; recompile with -fPIC"));
2302 case elfcpp::R_X86_64_32:
2303 r_name = "R_X86_64_32";
2305 case elfcpp::R_X86_64_PC32:
2306 r_name = "R_X86_64_PC32";
2308 case elfcpp::R_X86_64_PC32_BND:
2309 r_name = "R_X86_64_PC32_BND";
2315 object->error(_("requires dynamic %s reloc against '%s' "
2316 "which may overflow at runtime; recompile "
2318 r_name, gsym->name());
2320 this->issued_non_pic_error_ = true;
2324 // This prevents us from issuing more than one error per reloc
2325 // section. But we can still wind up issuing more than one
2326 // error per object file.
2327 if (this->issued_non_pic_error_)
2329 gold_assert(parameters->options().output_is_position_independent());
2330 object->error(_("requires unsupported dynamic reloc %u; "
2331 "recompile with -fPIC"),
2333 this->issued_non_pic_error_ = true;
2336 case elfcpp::R_X86_64_NONE:
2341 // Return whether we need to make a PLT entry for a relocation of the
2342 // given type against a STT_GNU_IFUNC symbol.
2346 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2347 Sized_relobj_file<size, false>* object,
2348 unsigned int r_type)
2350 int flags = Scan::get_reference_flags(r_type);
2351 if (flags & Symbol::TLS_REF)
2352 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2353 object->name().c_str(), r_type);
2357 // Scan a relocation for a local symbol.
2361 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2363 Target_x86_64<size>* target,
2364 Sized_relobj_file<size, false>* object,
2365 unsigned int data_shndx,
2366 Output_section* output_section,
2367 const elfcpp::Rela<size, false>& reloc,
2368 unsigned int r_type,
2369 const elfcpp::Sym<size, false>& lsym,
2375 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2376 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2377 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2379 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2380 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2385 case elfcpp::R_X86_64_NONE:
2386 case elfcpp::R_X86_64_GNU_VTINHERIT:
2387 case elfcpp::R_X86_64_GNU_VTENTRY:
2390 case elfcpp::R_X86_64_64:
2391 // If building a shared library (or a position-independent
2392 // executable), we need to create a dynamic relocation for this
2393 // location. The relocation applied at link time will apply the
2394 // link-time value, so we flag the location with an
2395 // R_X86_64_RELATIVE relocation so the dynamic loader can
2396 // relocate it easily.
2397 if (parameters->options().output_is_position_independent())
2399 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2400 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2401 rela_dyn->add_local_relative(object, r_sym,
2403 ? elfcpp::R_X86_64_RELATIVE64
2404 : elfcpp::R_X86_64_RELATIVE),
2405 output_section, data_shndx,
2406 reloc.get_r_offset(),
2407 reloc.get_r_addend(), is_ifunc);
2411 case elfcpp::R_X86_64_32:
2412 case elfcpp::R_X86_64_32S:
2413 case elfcpp::R_X86_64_16:
2414 case elfcpp::R_X86_64_8:
2415 // If building a shared library (or a position-independent
2416 // executable), we need to create a dynamic relocation for this
2417 // location. We can't use an R_X86_64_RELATIVE relocation
2418 // because that is always a 64-bit relocation.
2419 if (parameters->options().output_is_position_independent())
2421 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2422 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2424 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2425 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2426 rela_dyn->add_local_relative(object, r_sym,
2427 elfcpp::R_X86_64_RELATIVE,
2428 output_section, data_shndx,
2429 reloc.get_r_offset(),
2430 reloc.get_r_addend(), is_ifunc);
2434 this->check_non_pic(object, r_type, NULL);
2436 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2437 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2438 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2439 rela_dyn->add_local(object, r_sym, r_type, output_section,
2440 data_shndx, reloc.get_r_offset(),
2441 reloc.get_r_addend());
2444 gold_assert(lsym.get_st_value() == 0);
2445 unsigned int shndx = lsym.get_st_shndx();
2447 shndx = object->adjust_sym_shndx(r_sym, shndx,
2450 object->error(_("section symbol %u has bad shndx %u"),
2453 rela_dyn->add_local_section(object, shndx,
2454 r_type, output_section,
2455 data_shndx, reloc.get_r_offset(),
2456 reloc.get_r_addend());
2461 case elfcpp::R_X86_64_PC64:
2462 case elfcpp::R_X86_64_PC32:
2463 case elfcpp::R_X86_64_PC32_BND:
2464 case elfcpp::R_X86_64_PC16:
2465 case elfcpp::R_X86_64_PC8:
2468 case elfcpp::R_X86_64_PLT32:
2469 case elfcpp::R_X86_64_PLT32_BND:
2470 // Since we know this is a local symbol, we can handle this as a
2474 case elfcpp::R_X86_64_GOTPC32:
2475 case elfcpp::R_X86_64_GOTOFF64:
2476 case elfcpp::R_X86_64_GOTPC64:
2477 case elfcpp::R_X86_64_PLTOFF64:
2478 // We need a GOT section.
2479 target->got_section(symtab, layout);
2480 // For PLTOFF64, we'd normally want a PLT section, but since we
2481 // know this is a local symbol, no PLT is needed.
2484 case elfcpp::R_X86_64_GOT64:
2485 case elfcpp::R_X86_64_GOT32:
2486 case elfcpp::R_X86_64_GOTPCREL64:
2487 case elfcpp::R_X86_64_GOTPCREL:
2488 case elfcpp::R_X86_64_GOTPCRELX:
2489 case elfcpp::R_X86_64_REX_GOTPCRELX:
2490 case elfcpp::R_X86_64_GOTPLT64:
2492 // The symbol requires a GOT section.
2493 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2495 // If the relocation symbol isn't IFUNC,
2496 // and is local, then we will convert
2497 // mov foo@GOTPCREL(%rip), %reg
2498 // to lea foo(%rip), %reg.
2499 // in Relocate::relocate.
2500 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2501 || r_type == elfcpp::R_X86_64_GOTPCRELX
2502 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2503 && reloc.get_r_offset() >= 2
2506 section_size_type stype;
2507 const unsigned char* view = object->section_contents(data_shndx,
2509 if (view[reloc.get_r_offset() - 2] == 0x8b)
2514 // The symbol requires a GOT entry.
2515 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2517 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2518 // lets function pointers compare correctly with shared
2519 // libraries. Otherwise we would need an IRELATIVE reloc.
2522 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2524 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2527 // If we are generating a shared object, we need to add a
2528 // dynamic relocation for this symbol's GOT entry.
2529 if (parameters->options().output_is_position_independent())
2531 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2532 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2533 if (r_type != elfcpp::R_X86_64_GOT32)
2535 unsigned int got_offset =
2536 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2537 rela_dyn->add_local_relative(object, r_sym,
2538 elfcpp::R_X86_64_RELATIVE,
2539 got, got_offset, 0, is_ifunc);
2543 this->check_non_pic(object, r_type, NULL);
2545 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2546 rela_dyn->add_local(
2547 object, r_sym, r_type, got,
2548 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2552 // For GOTPLT64, we'd normally want a PLT section, but since
2553 // we know this is a local symbol, no PLT is needed.
2557 case elfcpp::R_X86_64_COPY:
2558 case elfcpp::R_X86_64_GLOB_DAT:
2559 case elfcpp::R_X86_64_JUMP_SLOT:
2560 case elfcpp::R_X86_64_RELATIVE:
2561 case elfcpp::R_X86_64_IRELATIVE:
2562 // These are outstanding tls relocs, which are unexpected when linking
2563 case elfcpp::R_X86_64_TPOFF64:
2564 case elfcpp::R_X86_64_DTPMOD64:
2565 case elfcpp::R_X86_64_TLSDESC:
2566 gold_error(_("%s: unexpected reloc %u in object file"),
2567 object->name().c_str(), r_type);
2570 // These are initial tls relocs, which are expected when linking
2571 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2572 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2573 case elfcpp::R_X86_64_TLSDESC_CALL:
2574 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2575 case elfcpp::R_X86_64_DTPOFF32:
2576 case elfcpp::R_X86_64_DTPOFF64:
2577 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2578 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2580 bool output_is_shared = parameters->options().shared();
2581 const tls::Tls_optimization optimized_type
2582 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2586 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2587 if (optimized_type == tls::TLSOPT_NONE)
2589 // Create a pair of GOT entries for the module index and
2590 // dtv-relative offset.
2591 Output_data_got<64, false>* got
2592 = target->got_section(symtab, layout);
2593 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2594 unsigned int shndx = lsym.get_st_shndx();
2596 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2598 object->error(_("local symbol %u has bad shndx %u"),
2601 got->add_local_pair_with_rel(object, r_sym,
2604 target->rela_dyn_section(layout),
2605 elfcpp::R_X86_64_DTPMOD64);
2607 else if (optimized_type != tls::TLSOPT_TO_LE)
2608 unsupported_reloc_local(object, r_type);
2611 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2612 target->define_tls_base_symbol(symtab, layout);
2613 if (optimized_type == tls::TLSOPT_NONE)
2615 // Create reserved PLT and GOT entries for the resolver.
2616 target->reserve_tlsdesc_entries(symtab, layout);
2618 // Generate a double GOT entry with an
2619 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2620 // is resolved lazily, so the GOT entry needs to be in
2621 // an area in .got.plt, not .got. Call got_section to
2622 // make sure the section has been created.
2623 target->got_section(symtab, layout);
2624 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2625 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2626 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2628 unsigned int got_offset = got->add_constant(0);
2629 got->add_constant(0);
2630 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2632 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2633 // We store the arguments we need in a vector, and
2634 // use the index into the vector as the parameter
2635 // to pass to the target specific routines.
2636 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2637 void* arg = reinterpret_cast<void*>(intarg);
2638 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2639 got, got_offset, 0);
2642 else if (optimized_type != tls::TLSOPT_TO_LE)
2643 unsupported_reloc_local(object, r_type);
2646 case elfcpp::R_X86_64_TLSDESC_CALL:
2649 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2650 if (optimized_type == tls::TLSOPT_NONE)
2652 // Create a GOT entry for the module index.
2653 target->got_mod_index_entry(symtab, layout, object);
2655 else if (optimized_type != tls::TLSOPT_TO_LE)
2656 unsupported_reloc_local(object, r_type);
2659 case elfcpp::R_X86_64_DTPOFF32:
2660 case elfcpp::R_X86_64_DTPOFF64:
2663 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2664 layout->set_has_static_tls();
2665 if (optimized_type == tls::TLSOPT_NONE)
2667 // Create a GOT entry for the tp-relative offset.
2668 Output_data_got<64, false>* got
2669 = target->got_section(symtab, layout);
2670 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2671 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2672 target->rela_dyn_section(layout),
2673 elfcpp::R_X86_64_TPOFF64);
2675 else if (optimized_type != tls::TLSOPT_TO_LE)
2676 unsupported_reloc_local(object, r_type);
2679 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2680 layout->set_has_static_tls();
2681 if (output_is_shared)
2682 unsupported_reloc_local(object, r_type);
2691 case elfcpp::R_X86_64_SIZE32:
2692 case elfcpp::R_X86_64_SIZE64:
2694 gold_error(_("%s: unsupported reloc %u against local symbol"),
2695 object->name().c_str(), r_type);
2701 // Report an unsupported relocation against a global symbol.
2705 Target_x86_64<size>::Scan::unsupported_reloc_global(
2706 Sized_relobj_file<size, false>* object,
2707 unsigned int r_type,
2710 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2711 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2714 // Returns true if this relocation type could be that of a function pointer.
2717 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2721 case elfcpp::R_X86_64_64:
2722 case elfcpp::R_X86_64_32:
2723 case elfcpp::R_X86_64_32S:
2724 case elfcpp::R_X86_64_16:
2725 case elfcpp::R_X86_64_8:
2726 case elfcpp::R_X86_64_GOT64:
2727 case elfcpp::R_X86_64_GOT32:
2728 case elfcpp::R_X86_64_GOTPCREL64:
2729 case elfcpp::R_X86_64_GOTPCREL:
2730 case elfcpp::R_X86_64_GOTPCRELX:
2731 case elfcpp::R_X86_64_REX_GOTPCRELX:
2732 case elfcpp::R_X86_64_GOTPLT64:
2740 // For safe ICF, scan a relocation for a local symbol to check if it
2741 // corresponds to a function pointer being taken. In that case mark
2742 // the function whose pointer was taken as not foldable.
2746 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2749 Target_x86_64<size>* ,
2750 Sized_relobj_file<size, false>* ,
2753 const elfcpp::Rela<size, false>& ,
2754 unsigned int r_type,
2755 const elfcpp::Sym<size, false>&)
2757 // When building a shared library, do not fold any local symbols as it is
2758 // not possible to distinguish pointer taken versus a call by looking at
2759 // the relocation types.
2760 return (parameters->options().shared()
2761 || possible_function_pointer_reloc(r_type));
2764 // For safe ICF, scan a relocation for a global symbol to check if it
2765 // corresponds to a function pointer being taken. In that case mark
2766 // the function whose pointer was taken as not foldable.
2770 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2773 Target_x86_64<size>* ,
2774 Sized_relobj_file<size, false>* ,
2777 const elfcpp::Rela<size, false>& ,
2778 unsigned int r_type,
2781 // When building a shared library, do not fold symbols whose visibility
2782 // is hidden, internal or protected.
2783 return ((parameters->options().shared()
2784 && (gsym->visibility() == elfcpp::STV_INTERNAL
2785 || gsym->visibility() == elfcpp::STV_PROTECTED
2786 || gsym->visibility() == elfcpp::STV_HIDDEN))
2787 || possible_function_pointer_reloc(r_type));
2790 // Scan a relocation for a global symbol.
2794 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2796 Target_x86_64<size>* target,
2797 Sized_relobj_file<size, false>* object,
2798 unsigned int data_shndx,
2799 Output_section* output_section,
2800 const elfcpp::Rela<size, false>& reloc,
2801 unsigned int r_type,
2804 // A STT_GNU_IFUNC symbol may require a PLT entry.
2805 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2806 && this->reloc_needs_plt_for_ifunc(object, r_type))
2807 target->make_plt_entry(symtab, layout, gsym);
2811 case elfcpp::R_X86_64_NONE:
2812 case elfcpp::R_X86_64_GNU_VTINHERIT:
2813 case elfcpp::R_X86_64_GNU_VTENTRY:
2816 case elfcpp::R_X86_64_64:
2817 case elfcpp::R_X86_64_32:
2818 case elfcpp::R_X86_64_32S:
2819 case elfcpp::R_X86_64_16:
2820 case elfcpp::R_X86_64_8:
2822 // Make a PLT entry if necessary.
2823 if (gsym->needs_plt_entry())
2825 target->make_plt_entry(symtab, layout, gsym);
2826 // Since this is not a PC-relative relocation, we may be
2827 // taking the address of a function. In that case we need to
2828 // set the entry in the dynamic symbol table to the address of
2830 if (gsym->is_from_dynobj() && !parameters->options().shared())
2831 gsym->set_needs_dynsym_value();
2833 // Make a dynamic relocation if necessary.
2834 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2836 if (!parameters->options().output_is_position_independent()
2837 && gsym->may_need_copy_reloc())
2839 target->copy_reloc(symtab, layout, object,
2840 data_shndx, output_section, gsym, reloc);
2842 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2843 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2844 && gsym->type() == elfcpp::STT_GNU_IFUNC
2845 && gsym->can_use_relative_reloc(false)
2846 && !gsym->is_from_dynobj()
2847 && !gsym->is_undefined()
2848 && !gsym->is_preemptible())
2850 // Use an IRELATIVE reloc for a locally defined
2851 // STT_GNU_IFUNC symbol. This makes a function
2852 // address in a PIE executable match the address in a
2853 // shared library that it links against.
2854 Reloc_section* rela_dyn =
2855 target->rela_irelative_section(layout);
2856 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2857 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2858 output_section, object,
2860 reloc.get_r_offset(),
2861 reloc.get_r_addend());
2863 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2864 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2865 && gsym->can_use_relative_reloc(false))
2867 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2868 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2869 output_section, object,
2871 reloc.get_r_offset(),
2872 reloc.get_r_addend(), false);
2876 this->check_non_pic(object, r_type, gsym);
2877 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2878 rela_dyn->add_global(gsym, r_type, output_section, object,
2879 data_shndx, reloc.get_r_offset(),
2880 reloc.get_r_addend());
2886 case elfcpp::R_X86_64_PC64:
2887 case elfcpp::R_X86_64_PC32:
2888 case elfcpp::R_X86_64_PC32_BND:
2889 case elfcpp::R_X86_64_PC16:
2890 case elfcpp::R_X86_64_PC8:
2892 // Make a PLT entry if necessary.
2893 if (gsym->needs_plt_entry())
2894 target->make_plt_entry(symtab, layout, gsym);
2895 // Make a dynamic relocation if necessary.
2896 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2898 if (parameters->options().output_is_executable()
2899 && gsym->may_need_copy_reloc())
2901 target->copy_reloc(symtab, layout, object,
2902 data_shndx, output_section, gsym, reloc);
2906 this->check_non_pic(object, r_type, gsym);
2907 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2908 rela_dyn->add_global(gsym, r_type, output_section, object,
2909 data_shndx, reloc.get_r_offset(),
2910 reloc.get_r_addend());
2916 case elfcpp::R_X86_64_GOT64:
2917 case elfcpp::R_X86_64_GOT32:
2918 case elfcpp::R_X86_64_GOTPCREL64:
2919 case elfcpp::R_X86_64_GOTPCREL:
2920 case elfcpp::R_X86_64_GOTPCRELX:
2921 case elfcpp::R_X86_64_REX_GOTPCRELX:
2922 case elfcpp::R_X86_64_GOTPLT64:
2924 // The symbol requires a GOT entry.
2925 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2927 // If we convert this from
2928 // mov foo@GOTPCREL(%rip), %reg
2929 // to lea foo(%rip), %reg.
2930 // in Relocate::relocate, then there is nothing to do here.
2931 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2932 || r_type == elfcpp::R_X86_64_GOTPCRELX
2933 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2934 && reloc.get_r_offset() >= 2
2935 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
2937 section_size_type stype;
2938 const unsigned char* view = object->section_contents(data_shndx,
2940 if (view[reloc.get_r_offset() - 2] == 0x8b)
2944 if (gsym->final_value_is_known())
2946 // For a STT_GNU_IFUNC symbol we want the PLT address.
2947 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2948 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2950 got->add_global(gsym, GOT_TYPE_STANDARD);
2954 // If this symbol is not fully resolved, we need to add a
2955 // dynamic relocation for it.
2956 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2958 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2960 // 1) The symbol may be defined in some other module.
2962 // 2) We are building a shared library and this is a
2963 // protected symbol; using GLOB_DAT means that the dynamic
2964 // linker can use the address of the PLT in the main
2965 // executable when appropriate so that function address
2966 // comparisons work.
2968 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2969 // code, again so that function address comparisons work.
2970 if (gsym->is_from_dynobj()
2971 || gsym->is_undefined()
2972 || gsym->is_preemptible()
2973 || (gsym->visibility() == elfcpp::STV_PROTECTED
2974 && parameters->options().shared())
2975 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2976 && parameters->options().output_is_position_independent()))
2977 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2978 elfcpp::R_X86_64_GLOB_DAT);
2981 // For a STT_GNU_IFUNC symbol we want to write the PLT
2982 // offset into the GOT, so that function pointer
2983 // comparisons work correctly.
2985 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2986 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2989 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2990 // Tell the dynamic linker to use the PLT address
2991 // when resolving relocations.
2992 if (gsym->is_from_dynobj()
2993 && !parameters->options().shared())
2994 gsym->set_needs_dynsym_value();
2998 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2999 rela_dyn->add_global_relative(gsym,
3000 elfcpp::R_X86_64_RELATIVE,
3001 got, got_off, 0, false);
3008 case elfcpp::R_X86_64_PLT32:
3009 case elfcpp::R_X86_64_PLT32_BND:
3010 // If the symbol is fully resolved, this is just a PC32 reloc.
3011 // Otherwise we need a PLT entry.
3012 if (gsym->final_value_is_known())
3014 // If building a shared library, we can also skip the PLT entry
3015 // if the symbol is defined in the output file and is protected
3017 if (gsym->is_defined()
3018 && !gsym->is_from_dynobj()
3019 && !gsym->is_preemptible())
3021 target->make_plt_entry(symtab, layout, gsym);
3024 case elfcpp::R_X86_64_GOTPC32:
3025 case elfcpp::R_X86_64_GOTOFF64:
3026 case elfcpp::R_X86_64_GOTPC64:
3027 case elfcpp::R_X86_64_PLTOFF64:
3028 // We need a GOT section.
3029 target->got_section(symtab, layout);
3030 // For PLTOFF64, we also need a PLT entry (but only if the
3031 // symbol is not fully resolved).
3032 if (r_type == elfcpp::R_X86_64_PLTOFF64
3033 && !gsym->final_value_is_known())
3034 target->make_plt_entry(symtab, layout, gsym);
3037 case elfcpp::R_X86_64_COPY:
3038 case elfcpp::R_X86_64_GLOB_DAT:
3039 case elfcpp::R_X86_64_JUMP_SLOT:
3040 case elfcpp::R_X86_64_RELATIVE:
3041 case elfcpp::R_X86_64_IRELATIVE:
3042 // These are outstanding tls relocs, which are unexpected when linking
3043 case elfcpp::R_X86_64_TPOFF64:
3044 case elfcpp::R_X86_64_DTPMOD64:
3045 case elfcpp::R_X86_64_TLSDESC:
3046 gold_error(_("%s: unexpected reloc %u in object file"),
3047 object->name().c_str(), r_type);
3050 // These are initial tls relocs, which are expected for global()
3051 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3052 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3053 case elfcpp::R_X86_64_TLSDESC_CALL:
3054 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3055 case elfcpp::R_X86_64_DTPOFF32:
3056 case elfcpp::R_X86_64_DTPOFF64:
3057 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3058 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3060 // For the Initial-Exec model, we can treat undef symbols as final
3061 // when building an executable.
3062 const bool is_final = (gsym->final_value_is_known() ||
3063 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3064 gsym->is_undefined() &&
3065 parameters->options().output_is_executable()));
3066 const tls::Tls_optimization optimized_type
3067 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3070 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3071 if (optimized_type == tls::TLSOPT_NONE)
3073 // Create a pair of GOT entries for the module index and
3074 // dtv-relative offset.
3075 Output_data_got<64, false>* got
3076 = target->got_section(symtab, layout);
3077 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3078 target->rela_dyn_section(layout),
3079 elfcpp::R_X86_64_DTPMOD64,
3080 elfcpp::R_X86_64_DTPOFF64);
3082 else if (optimized_type == tls::TLSOPT_TO_IE)
3084 // Create a GOT entry for the tp-relative offset.
3085 Output_data_got<64, false>* got
3086 = target->got_section(symtab, layout);
3087 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3088 target->rela_dyn_section(layout),
3089 elfcpp::R_X86_64_TPOFF64);
3091 else if (optimized_type != tls::TLSOPT_TO_LE)
3092 unsupported_reloc_global(object, r_type, gsym);
3095 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3096 target->define_tls_base_symbol(symtab, layout);
3097 if (optimized_type == tls::TLSOPT_NONE)
3099 // Create reserved PLT and GOT entries for the resolver.
3100 target->reserve_tlsdesc_entries(symtab, layout);
3102 // Create a double GOT entry with an R_X86_64_TLSDESC
3103 // reloc. The R_X86_64_TLSDESC reloc is resolved
3104 // lazily, so the GOT entry needs to be in an area in
3105 // .got.plt, not .got. Call got_section to make sure
3106 // the section has been created.
3107 target->got_section(symtab, layout);
3108 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3109 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3110 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3111 elfcpp::R_X86_64_TLSDESC, 0);
3113 else if (optimized_type == tls::TLSOPT_TO_IE)
3115 // Create a GOT entry for the tp-relative offset.
3116 Output_data_got<64, false>* got
3117 = target->got_section(symtab, layout);
3118 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3119 target->rela_dyn_section(layout),
3120 elfcpp::R_X86_64_TPOFF64);
3122 else if (optimized_type != tls::TLSOPT_TO_LE)
3123 unsupported_reloc_global(object, r_type, gsym);
3126 case elfcpp::R_X86_64_TLSDESC_CALL:
3129 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3130 if (optimized_type == tls::TLSOPT_NONE)
3132 // Create a GOT entry for the module index.
3133 target->got_mod_index_entry(symtab, layout, object);
3135 else if (optimized_type != tls::TLSOPT_TO_LE)
3136 unsupported_reloc_global(object, r_type, gsym);
3139 case elfcpp::R_X86_64_DTPOFF32:
3140 case elfcpp::R_X86_64_DTPOFF64:
3143 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3144 layout->set_has_static_tls();
3145 if (optimized_type == tls::TLSOPT_NONE)
3147 // Create a GOT entry for the tp-relative offset.
3148 Output_data_got<64, false>* got
3149 = target->got_section(symtab, layout);
3150 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3151 target->rela_dyn_section(layout),
3152 elfcpp::R_X86_64_TPOFF64);
3154 else if (optimized_type != tls::TLSOPT_TO_LE)
3155 unsupported_reloc_global(object, r_type, gsym);
3158 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3159 layout->set_has_static_tls();
3160 if (parameters->options().shared())
3161 unsupported_reloc_global(object, r_type, gsym);
3170 case elfcpp::R_X86_64_SIZE32:
3171 case elfcpp::R_X86_64_SIZE64:
3173 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3174 object->name().c_str(), r_type,
3175 gsym->demangled_name().c_str());
3182 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3184 Sized_relobj_file<size, false>* object,
3185 unsigned int data_shndx,
3186 unsigned int sh_type,
3187 const unsigned char* prelocs,
3189 Output_section* output_section,
3190 bool needs_special_offset_handling,
3191 size_t local_symbol_count,
3192 const unsigned char* plocal_symbols)
3195 if (sh_type == elfcpp::SHT_REL)
3200 gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3201 typename Target_x86_64<size>::Scan,
3202 typename Target_x86_64<size>::Relocatable_size_for_reloc>(
3211 needs_special_offset_handling,
3216 // Scan relocations for a section.
3220 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3222 Sized_relobj_file<size, false>* object,
3223 unsigned int data_shndx,
3224 unsigned int sh_type,
3225 const unsigned char* prelocs,
3227 Output_section* output_section,
3228 bool needs_special_offset_handling,
3229 size_t local_symbol_count,
3230 const unsigned char* plocal_symbols)
3232 if (sh_type == elfcpp::SHT_REL)
3234 gold_error(_("%s: unsupported REL reloc section"),
3235 object->name().c_str());
3239 gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3240 typename Target_x86_64<size>::Scan>(
3249 needs_special_offset_handling,
3254 // Finalize the sections.
3258 Target_x86_64<size>::do_finalize_sections(
3260 const Input_objects*,
3261 Symbol_table* symtab)
3263 const Reloc_section* rel_plt = (this->plt_ == NULL
3265 : this->plt_->rela_plt());
3266 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3267 this->rela_dyn_, true, false);
3269 // Fill in some more dynamic tags.
3270 Output_data_dynamic* const odyn = layout->dynamic_data();
3273 if (this->plt_ != NULL
3274 && this->plt_->output_section() != NULL
3275 && this->plt_->has_tlsdesc_entry())
3277 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3278 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3279 this->got_->finalize_data_size();
3280 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3281 this->plt_, plt_offset);
3282 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3283 this->got_, got_offset);
3287 // Emit any relocs we saved in an attempt to avoid generating COPY
3289 if (this->copy_relocs_.any_saved_relocs())
3290 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3292 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3293 // the .got.plt section.
3294 Symbol* sym = this->global_offset_table_;
3297 uint64_t data_size = this->got_plt_->current_data_size();
3298 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3301 if (parameters->doing_static_link()
3302 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3304 // If linking statically, make sure that the __rela_iplt symbols
3305 // were defined if necessary, even if we didn't create a PLT.
3306 static const Define_symbol_in_segment syms[] =
3309 "__rela_iplt_start", // name
3310 elfcpp::PT_LOAD, // segment_type
3311 elfcpp::PF_W, // segment_flags_set
3312 elfcpp::PF(0), // segment_flags_clear
3315 elfcpp::STT_NOTYPE, // type
3316 elfcpp::STB_GLOBAL, // binding
3317 elfcpp::STV_HIDDEN, // visibility
3319 Symbol::SEGMENT_START, // offset_from_base
3323 "__rela_iplt_end", // name
3324 elfcpp::PT_LOAD, // segment_type
3325 elfcpp::PF_W, // segment_flags_set
3326 elfcpp::PF(0), // segment_flags_clear
3329 elfcpp::STT_NOTYPE, // type
3330 elfcpp::STB_GLOBAL, // binding
3331 elfcpp::STV_HIDDEN, // visibility
3333 Symbol::SEGMENT_START, // offset_from_base
3338 symtab->define_symbols(layout, 2, syms,
3339 layout->script_options()->saw_sections_clause());
3343 // Perform a relocation.
3347 Target_x86_64<size>::Relocate::relocate(
3348 const Relocate_info<size, false>* relinfo,
3350 Target_x86_64<size>* target,
3353 const unsigned char* preloc,
3354 const Sized_symbol<size>* gsym,
3355 const Symbol_value<size>* psymval,
3356 unsigned char* view,
3357 typename elfcpp::Elf_types<size>::Elf_Addr address,
3358 section_size_type view_size)
3360 const elfcpp::Rela<size, false> rela(preloc);
3361 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3363 if (this->skip_call_tls_get_addr_)
3365 if ((r_type != elfcpp::R_X86_64_PLT32
3366 && r_type != elfcpp::R_X86_64_PLT32_BND
3367 && r_type != elfcpp::R_X86_64_PC32_BND
3368 && r_type != elfcpp::R_X86_64_PC32)
3370 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3372 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3373 _("missing expected TLS relocation"));
3377 this->skip_call_tls_get_addr_ = false;
3385 const Sized_relobj_file<size, false>* object = relinfo->object;
3387 // Pick the value to use for symbols defined in the PLT.
3388 Symbol_value<size> symval;
3390 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3392 symval.set_output_value(target->plt_address_for_global(gsym));
3395 else if (gsym == NULL && psymval->is_ifunc_symbol())
3397 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3398 if (object->local_has_plt_offset(r_sym))
3400 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3405 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3407 // Get the GOT offset if needed.
3408 // The GOT pointer points to the end of the GOT section.
3409 // We need to subtract the size of the GOT section to get
3410 // the actual offset to use in the relocation.
3411 bool have_got_offset = false;
3412 // Since the actual offset is always negative, we use signed int to
3413 // support 64-bit GOT relocations.
3417 case elfcpp::R_X86_64_GOT32:
3418 case elfcpp::R_X86_64_GOT64:
3419 case elfcpp::R_X86_64_GOTPLT64:
3420 case elfcpp::R_X86_64_GOTPCREL64:
3423 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3424 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3428 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3429 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3430 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3431 - target->got_size());
3433 have_got_offset = true;
3442 case elfcpp::R_X86_64_NONE:
3443 case elfcpp::R_X86_64_GNU_VTINHERIT:
3444 case elfcpp::R_X86_64_GNU_VTENTRY:
3447 case elfcpp::R_X86_64_64:
3448 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
3451 case elfcpp::R_X86_64_PC64:
3452 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
3456 case elfcpp::R_X86_64_32:
3457 // FIXME: we need to verify that value + addend fits into 32 bits:
3458 // uint64_t x = value + addend;
3459 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3460 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3461 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3464 case elfcpp::R_X86_64_32S:
3465 // FIXME: we need to verify that value + addend fits into 32 bits:
3466 // int64_t x = value + addend; // note this quantity is signed!
3467 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3468 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3471 case elfcpp::R_X86_64_PC32:
3472 case elfcpp::R_X86_64_PC32_BND:
3473 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3477 case elfcpp::R_X86_64_16:
3478 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
3481 case elfcpp::R_X86_64_PC16:
3482 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3486 case elfcpp::R_X86_64_8:
3487 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
3490 case elfcpp::R_X86_64_PC8:
3491 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3495 case elfcpp::R_X86_64_PLT32:
3496 case elfcpp::R_X86_64_PLT32_BND:
3497 gold_assert(gsym == NULL
3498 || gsym->has_plt_offset()
3499 || gsym->final_value_is_known()
3500 || (gsym->is_defined()
3501 && !gsym->is_from_dynobj()
3502 && !gsym->is_preemptible()));
3503 // Note: while this code looks the same as for R_X86_64_PC32, it
3504 // behaves differently because psymval was set to point to
3505 // the PLT entry, rather than the symbol, in Scan::global().
3506 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3510 case elfcpp::R_X86_64_PLTOFF64:
3513 gold_assert(gsym->has_plt_offset()
3514 || gsym->final_value_is_known());
3515 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3516 // This is the address of GLOBAL_OFFSET_TABLE.
3517 got_address = target->got_plt_section()->address();
3518 Relocate_functions<size, false>::rela64(view, object, psymval,
3519 addend - got_address);
3523 case elfcpp::R_X86_64_GOT32:
3524 gold_assert(have_got_offset);
3525 Relocate_functions<size, false>::rela32(view, got_offset, addend);
3528 case elfcpp::R_X86_64_GOTPC32:
3531 typename elfcpp::Elf_types<size>::Elf_Addr value;
3532 value = target->got_plt_section()->address();
3533 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3537 case elfcpp::R_X86_64_GOT64:
3538 case elfcpp::R_X86_64_GOTPLT64:
3539 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3541 gold_assert(have_got_offset);
3542 Relocate_functions<size, false>::rela64(view, got_offset, addend);
3545 case elfcpp::R_X86_64_GOTPC64:
3548 typename elfcpp::Elf_types<size>::Elf_Addr value;
3549 value = target->got_plt_section()->address();
3550 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3554 case elfcpp::R_X86_64_GOTOFF64:
3556 typename elfcpp::Elf_types<size>::Elf_Addr value;
3557 value = (psymval->value(object, 0)
3558 - target->got_plt_section()->address());
3559 Relocate_functions<size, false>::rela64(view, value, addend);
3563 case elfcpp::R_X86_64_GOTPCREL:
3564 case elfcpp::R_X86_64_GOTPCRELX:
3565 case elfcpp::R_X86_64_REX_GOTPCRELX:
3568 // mov foo@GOTPCREL(%rip), %reg
3569 // to lea foo(%rip), %reg.
3571 if (rela.get_r_offset() >= 2
3573 && ((gsym == NULL && !psymval->is_ifunc_symbol())
3575 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
3578 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3585 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3586 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3590 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3591 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3592 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3593 - target->got_size());
3595 typename elfcpp::Elf_types<size>::Elf_Addr value;
3596 value = target->got_plt_section()->address() + got_offset;
3597 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3602 case elfcpp::R_X86_64_GOTPCREL64:
3604 gold_assert(have_got_offset);
3605 typename elfcpp::Elf_types<size>::Elf_Addr value;
3606 value = target->got_plt_section()->address() + got_offset;
3607 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3611 case elfcpp::R_X86_64_COPY:
3612 case elfcpp::R_X86_64_GLOB_DAT:
3613 case elfcpp::R_X86_64_JUMP_SLOT:
3614 case elfcpp::R_X86_64_RELATIVE:
3615 case elfcpp::R_X86_64_IRELATIVE:
3616 // These are outstanding tls relocs, which are unexpected when linking
3617 case elfcpp::R_X86_64_TPOFF64:
3618 case elfcpp::R_X86_64_DTPMOD64:
3619 case elfcpp::R_X86_64_TLSDESC:
3620 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3621 _("unexpected reloc %u in object file"),
3625 // These are initial tls relocs, which are expected when linking
3626 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3627 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3628 case elfcpp::R_X86_64_TLSDESC_CALL:
3629 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3630 case elfcpp::R_X86_64_DTPOFF32:
3631 case elfcpp::R_X86_64_DTPOFF64:
3632 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3633 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3634 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3635 view, address, view_size);
3638 case elfcpp::R_X86_64_SIZE32:
3639 case elfcpp::R_X86_64_SIZE64:
3641 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3642 _("unsupported reloc %u"),
3650 // Perform a TLS relocation.
3654 Target_x86_64<size>::Relocate::relocate_tls(
3655 const Relocate_info<size, false>* relinfo,
3656 Target_x86_64<size>* target,
3658 const elfcpp::Rela<size, false>& rela,
3659 unsigned int r_type,
3660 const Sized_symbol<size>* gsym,
3661 const Symbol_value<size>* psymval,
3662 unsigned char* view,
3663 typename elfcpp::Elf_types<size>::Elf_Addr address,
3664 section_size_type view_size)
3666 Output_segment* tls_segment = relinfo->layout->tls_segment();
3668 const Sized_relobj_file<size, false>* object = relinfo->object;
3669 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3670 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3671 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3673 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3675 const bool is_final = (gsym == NULL
3676 ? !parameters->options().shared()
3677 : gsym->final_value_is_known());
3678 tls::Tls_optimization optimized_type
3679 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3682 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3683 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3685 // If this code sequence is used in a non-executable section,
3686 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3687 // on the assumption that it's being used by itself in a debug
3688 // section. Therefore, in the unlikely event that the code
3689 // sequence appears in a non-executable section, we simply
3690 // leave it unoptimized.
3691 optimized_type = tls::TLSOPT_NONE;
3693 if (optimized_type == tls::TLSOPT_TO_LE)
3695 if (tls_segment == NULL)
3697 gold_assert(parameters->errors()->error_count() > 0
3698 || issue_undefined_symbol_error(gsym));
3701 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3702 rela, r_type, value, view,
3708 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3709 ? GOT_TYPE_TLS_OFFSET
3710 : GOT_TYPE_TLS_PAIR);
3711 unsigned int got_offset;
3714 gold_assert(gsym->has_got_offset(got_type));
3715 got_offset = gsym->got_offset(got_type) - target->got_size();
3719 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3720 gold_assert(object->local_has_got_offset(r_sym, got_type));
3721 got_offset = (object->local_got_offset(r_sym, got_type)
3722 - target->got_size());
3724 if (optimized_type == tls::TLSOPT_TO_IE)
3726 value = target->got_plt_section()->address() + got_offset;
3727 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3728 value, view, address, view_size);
3731 else if (optimized_type == tls::TLSOPT_NONE)
3733 // Relocate the field with the offset of the pair of GOT
3735 value = target->got_plt_section()->address() + got_offset;
3736 Relocate_functions<size, false>::pcrela32(view, value, addend,
3741 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3742 _("unsupported reloc %u"), r_type);
3745 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3746 case elfcpp::R_X86_64_TLSDESC_CALL:
3747 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3749 // See above comment for R_X86_64_TLSGD.
3750 optimized_type = tls::TLSOPT_NONE;
3752 if (optimized_type == tls::TLSOPT_TO_LE)
3754 if (tls_segment == NULL)
3756 gold_assert(parameters->errors()->error_count() > 0
3757 || issue_undefined_symbol_error(gsym));
3760 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3761 rela, r_type, value, view,
3767 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3768 ? GOT_TYPE_TLS_OFFSET
3769 : GOT_TYPE_TLS_DESC);
3770 unsigned int got_offset = 0;
3771 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3772 && optimized_type == tls::TLSOPT_NONE)
3774 // We created GOT entries in the .got.tlsdesc portion of
3775 // the .got.plt section, but the offset stored in the
3776 // symbol is the offset within .got.tlsdesc.
3777 got_offset = (target->got_size()
3778 + target->got_plt_section()->data_size());
3782 gold_assert(gsym->has_got_offset(got_type));
3783 got_offset += gsym->got_offset(got_type) - target->got_size();
3787 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3788 gold_assert(object->local_has_got_offset(r_sym, got_type));
3789 got_offset += (object->local_got_offset(r_sym, got_type)
3790 - target->got_size());
3792 if (optimized_type == tls::TLSOPT_TO_IE)
3794 if (tls_segment == NULL)
3796 gold_assert(parameters->errors()->error_count() > 0
3797 || issue_undefined_symbol_error(gsym));
3800 value = target->got_plt_section()->address() + got_offset;
3801 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3802 rela, r_type, value, view, address,
3806 else if (optimized_type == tls::TLSOPT_NONE)
3808 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3810 // Relocate the field with the offset of the pair of GOT
3812 value = target->got_plt_section()->address() + got_offset;
3813 Relocate_functions<size, false>::pcrela32(view, value, addend,
3819 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3820 _("unsupported reloc %u"), r_type);
3823 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3824 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3826 // See above comment for R_X86_64_TLSGD.
3827 optimized_type = tls::TLSOPT_NONE;
3829 if (optimized_type == tls::TLSOPT_TO_LE)
3831 if (tls_segment == NULL)
3833 gold_assert(parameters->errors()->error_count() > 0
3834 || issue_undefined_symbol_error(gsym));
3837 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3838 value, view, view_size);
3841 else if (optimized_type == tls::TLSOPT_NONE)
3843 // Relocate the field with the offset of the GOT entry for
3844 // the module index.
3845 unsigned int got_offset;
3846 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3847 - target->got_size());
3848 value = target->got_plt_section()->address() + got_offset;
3849 Relocate_functions<size, false>::pcrela32(view, value, addend,
3853 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3854 _("unsupported reloc %u"), r_type);
3857 case elfcpp::R_X86_64_DTPOFF32:
3858 // This relocation type is used in debugging information.
3859 // In that case we need to not optimize the value. If the
3860 // section is not executable, then we assume we should not
3861 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3862 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3864 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3866 if (tls_segment == NULL)
3868 gold_assert(parameters->errors()->error_count() > 0
3869 || issue_undefined_symbol_error(gsym));
3872 value -= tls_segment->memsz();
3874 Relocate_functions<size, false>::rela32(view, value, addend);
3877 case elfcpp::R_X86_64_DTPOFF64:
3878 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3879 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3881 if (tls_segment == NULL)
3883 gold_assert(parameters->errors()->error_count() > 0
3884 || issue_undefined_symbol_error(gsym));
3887 value -= tls_segment->memsz();
3889 Relocate_functions<size, false>::rela64(view, value, addend);
3892 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3894 && gsym->is_undefined()
3895 && parameters->options().output_is_executable())
3897 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3899 r_type, value, view,
3903 else if (optimized_type == tls::TLSOPT_TO_LE)
3905 if (tls_segment == NULL)
3907 gold_assert(parameters->errors()->error_count() > 0
3908 || issue_undefined_symbol_error(gsym));
3911 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3913 r_type, value, view,
3917 else if (optimized_type == tls::TLSOPT_NONE)
3919 // Relocate the field with the offset of the GOT entry for
3920 // the tp-relative offset of the symbol.
3921 unsigned int got_offset;
3924 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3925 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3926 - target->got_size());
3930 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3931 gold_assert(object->local_has_got_offset(r_sym,
3932 GOT_TYPE_TLS_OFFSET));
3933 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3934 - target->got_size());
3936 value = target->got_plt_section()->address() + got_offset;
3937 Relocate_functions<size, false>::pcrela32(view, value, addend,
3941 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3942 _("unsupported reloc type %u"),
3946 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3947 if (tls_segment == NULL)
3949 gold_assert(parameters->errors()->error_count() > 0
3950 || issue_undefined_symbol_error(gsym));
3953 value -= tls_segment->memsz();
3954 Relocate_functions<size, false>::rela32(view, value, addend);
3959 // Do a relocation in which we convert a TLS General-Dynamic to an
3964 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3965 const Relocate_info<size, false>* relinfo,
3968 const elfcpp::Rela<size, false>& rela,
3970 typename elfcpp::Elf_types<size>::Elf_Addr value,
3971 unsigned char* view,
3972 typename elfcpp::Elf_types<size>::Elf_Addr address,
3973 section_size_type view_size)
3976 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3977 // .word 0x6666; rex64; call __tls_get_addr
3978 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3980 // leaq foo@tlsgd(%rip),%rdi;
3981 // .word 0x6666; rex64; call __tls_get_addr
3982 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3984 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3985 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3986 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3990 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3992 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3993 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3994 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\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 - 3, "\x48\x8d\x3d", 3) == 0));
4003 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4007 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4008 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4011 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4013 this->skip_call_tls_get_addr_ = true;
4016 // Do a relocation in which we convert a TLS General-Dynamic to a
4021 Target_x86_64<size>::Relocate::tls_gd_to_le(
4022 const Relocate_info<size, false>* relinfo,
4024 Output_segment* tls_segment,
4025 const elfcpp::Rela<size, false>& rela,
4027 typename elfcpp::Elf_types<size>::Elf_Addr value,
4028 unsigned char* view,
4029 section_size_type view_size)
4032 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4033 // .word 0x6666; rex64; call __tls_get_addr
4034 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4036 // leaq foo@tlsgd(%rip),%rdi;
4037 // .word 0x6666; rex64; call __tls_get_addr
4038 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4040 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4041 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4042 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
4046 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4048 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4049 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4050 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\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 - 3, "\x48\x8d\x3d", 3) == 0));
4060 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4064 value -= tls_segment->memsz();
4065 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4067 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4069 this->skip_call_tls_get_addr_ = true;
4072 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4076 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4077 const Relocate_info<size, false>* relinfo,
4080 const elfcpp::Rela<size, false>& rela,
4081 unsigned int r_type,
4082 typename elfcpp::Elf_types<size>::Elf_Addr value,
4083 unsigned char* view,
4084 typename elfcpp::Elf_types<size>::Elf_Addr address,
4085 section_size_type view_size)
4087 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4089 // leaq foo@tlsdesc(%rip), %rax
4090 // ==> movq foo@gottpoff(%rip), %rax
4091 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4092 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4093 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4094 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4096 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4097 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4101 // call *foo@tlscall(%rax)
4103 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4104 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4105 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4106 view[0] == 0xff && view[1] == 0x10);
4112 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4116 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4117 const Relocate_info<size, false>* relinfo,
4119 Output_segment* tls_segment,
4120 const elfcpp::Rela<size, false>& rela,
4121 unsigned int r_type,
4122 typename elfcpp::Elf_types<size>::Elf_Addr value,
4123 unsigned char* view,
4124 section_size_type view_size)
4126 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4128 // leaq foo@tlsdesc(%rip), %rax
4129 // ==> movq foo@tpoff, %rax
4130 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4131 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4132 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4133 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4136 value -= tls_segment->memsz();
4137 Relocate_functions<size, false>::rela32(view, value, 0);
4141 // call *foo@tlscall(%rax)
4143 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4144 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4145 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4146 view[0] == 0xff && view[1] == 0x10);
4154 Target_x86_64<size>::Relocate::tls_ld_to_le(
4155 const Relocate_info<size, false>* relinfo,
4158 const elfcpp::Rela<size, false>& rela,
4160 typename elfcpp::Elf_types<size>::Elf_Addr,
4161 unsigned char* view,
4162 section_size_type view_size)
4164 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4166 // ... leq foo@dtpoff(%rax),%reg
4167 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4169 // ... leq foo@dtpoff(%rax),%reg
4170 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4172 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4173 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4175 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4176 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4178 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4181 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4183 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4185 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4187 this->skip_call_tls_get_addr_ = true;
4190 // Do a relocation in which we convert a TLS Initial-Exec to a
4195 Target_x86_64<size>::Relocate::tls_ie_to_le(
4196 const Relocate_info<size, false>* relinfo,
4198 Output_segment* tls_segment,
4199 const elfcpp::Rela<size, false>& rela,
4201 typename elfcpp::Elf_types<size>::Elf_Addr value,
4202 unsigned char* view,
4203 section_size_type view_size)
4205 // We need to examine the opcodes to figure out which instruction we
4208 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4209 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4211 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4212 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4214 unsigned char op1 = view[-3];
4215 unsigned char op2 = view[-2];
4216 unsigned char op3 = view[-1];
4217 unsigned char reg = op3 >> 3;
4224 else if (size == 32 && op1 == 0x44)
4227 view[-1] = 0xc0 | reg;
4231 // Special handling for %rsp.
4234 else if (size == 32 && op1 == 0x44)
4237 view[-1] = 0xc0 | reg;
4244 else if (size == 32 && op1 == 0x44)
4247 view[-1] = 0x80 | reg | (reg << 3);
4250 if (tls_segment != NULL)
4251 value -= tls_segment->memsz();
4252 Relocate_functions<size, false>::rela32(view, value, 0);
4255 // Relocate section data.
4259 Target_x86_64<size>::relocate_section(
4260 const Relocate_info<size, false>* relinfo,
4261 unsigned int sh_type,
4262 const unsigned char* prelocs,
4264 Output_section* output_section,
4265 bool needs_special_offset_handling,
4266 unsigned char* view,
4267 typename elfcpp::Elf_types<size>::Elf_Addr address,
4268 section_size_type view_size,
4269 const Reloc_symbol_changes* reloc_symbol_changes)
4271 gold_assert(sh_type == elfcpp::SHT_RELA);
4273 gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
4274 typename Target_x86_64<size>::Relocate,
4275 gold::Default_comdat_behavior>(
4281 needs_special_offset_handling,
4285 reloc_symbol_changes);
4288 // Apply an incremental relocation. Incremental relocations always refer
4289 // to global symbols.
4293 Target_x86_64<size>::apply_relocation(
4294 const Relocate_info<size, false>* relinfo,
4295 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4296 unsigned int r_type,
4297 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4299 unsigned char* view,
4300 typename elfcpp::Elf_types<size>::Elf_Addr address,
4301 section_size_type view_size)
4303 gold::apply_relocation<size, false, Target_x86_64<size>,
4304 typename Target_x86_64<size>::Relocate>(
4316 // Return the size of a relocation while scanning during a relocatable
4321 Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
4322 unsigned int r_type,
4327 case elfcpp::R_X86_64_NONE:
4328 case elfcpp::R_X86_64_GNU_VTINHERIT:
4329 case elfcpp::R_X86_64_GNU_VTENTRY:
4330 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4331 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4332 case elfcpp::R_X86_64_TLSDESC_CALL:
4333 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4334 case elfcpp::R_X86_64_DTPOFF32:
4335 case elfcpp::R_X86_64_DTPOFF64:
4336 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4337 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4340 case elfcpp::R_X86_64_64:
4341 case elfcpp::R_X86_64_PC64:
4342 case elfcpp::R_X86_64_GOTOFF64:
4343 case elfcpp::R_X86_64_GOTPC64:
4344 case elfcpp::R_X86_64_PLTOFF64:
4345 case elfcpp::R_X86_64_GOT64:
4346 case elfcpp::R_X86_64_GOTPCREL64:
4347 case elfcpp::R_X86_64_GOTPCREL:
4348 case elfcpp::R_X86_64_GOTPCRELX:
4349 case elfcpp::R_X86_64_REX_GOTPCRELX:
4350 case elfcpp::R_X86_64_GOTPLT64:
4353 case elfcpp::R_X86_64_32:
4354 case elfcpp::R_X86_64_32S:
4355 case elfcpp::R_X86_64_PC32:
4356 case elfcpp::R_X86_64_PC32_BND:
4357 case elfcpp::R_X86_64_PLT32:
4358 case elfcpp::R_X86_64_PLT32_BND:
4359 case elfcpp::R_X86_64_GOTPC32:
4360 case elfcpp::R_X86_64_GOT32:
4363 case elfcpp::R_X86_64_16:
4364 case elfcpp::R_X86_64_PC16:
4367 case elfcpp::R_X86_64_8:
4368 case elfcpp::R_X86_64_PC8:
4371 case elfcpp::R_X86_64_COPY:
4372 case elfcpp::R_X86_64_GLOB_DAT:
4373 case elfcpp::R_X86_64_JUMP_SLOT:
4374 case elfcpp::R_X86_64_RELATIVE:
4375 case elfcpp::R_X86_64_IRELATIVE:
4376 // These are outstanding tls relocs, which are unexpected when linking
4377 case elfcpp::R_X86_64_TPOFF64:
4378 case elfcpp::R_X86_64_DTPMOD64:
4379 case elfcpp::R_X86_64_TLSDESC:
4380 object->error(_("unexpected reloc %u in object file"), r_type);
4383 case elfcpp::R_X86_64_SIZE32:
4384 case elfcpp::R_X86_64_SIZE64:
4386 object->error(_("unsupported reloc %u against local symbol"), r_type);
4391 // Scan the relocs during a relocatable link.
4395 Target_x86_64<size>::scan_relocatable_relocs(
4396 Symbol_table* symtab,
4398 Sized_relobj_file<size, false>* object,
4399 unsigned int data_shndx,
4400 unsigned int sh_type,
4401 const unsigned char* prelocs,
4403 Output_section* output_section,
4404 bool needs_special_offset_handling,
4405 size_t local_symbol_count,
4406 const unsigned char* plocal_symbols,
4407 Relocatable_relocs* rr)
4409 gold_assert(sh_type == elfcpp::SHT_RELA);
4411 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4412 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4414 gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
4415 Scan_relocatable_relocs>(
4423 needs_special_offset_handling,
4429 // Relocate a section during a relocatable link.
4433 Target_x86_64<size>::relocate_relocs(
4434 const Relocate_info<size, false>* relinfo,
4435 unsigned int sh_type,
4436 const unsigned char* prelocs,
4438 Output_section* output_section,
4439 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4440 unsigned char* view,
4441 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4442 section_size_type view_size,
4443 unsigned char* reloc_view,
4444 section_size_type reloc_view_size)
4446 gold_assert(sh_type == elfcpp::SHT_RELA);
4448 gold::relocate_relocs<size, false, elfcpp::SHT_RELA>(
4453 offset_in_output_section,
4461 // Return the value to use for a dynamic which requires special
4462 // treatment. This is how we support equality comparisons of function
4463 // pointers across shared library boundaries, as described in the
4464 // processor specific ABI supplement.
4468 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4470 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4471 return this->plt_address_for_global(gsym);
4474 // Return a string used to fill a code section with nops to take up
4475 // the specified length.
4479 Target_x86_64<size>::do_code_fill(section_size_type length) const
4483 // Build a jmpq instruction to skip over the bytes.
4484 unsigned char jmp[5];
4486 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4487 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4488 + std::string(length - 5, static_cast<char>(0x90)));
4491 // Nop sequences of various lengths.
4492 const char nop1[1] = { '\x90' }; // nop
4493 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4494 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4495 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4497 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4499 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4500 '\x44', '\x00', '\x00' };
4501 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4502 '\x00', '\x00', '\x00',
4504 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4505 '\x00', '\x00', '\x00',
4507 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4508 '\x84', '\x00', '\x00',
4509 '\x00', '\x00', '\x00' };
4510 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4511 '\x1f', '\x84', '\x00',
4512 '\x00', '\x00', '\x00',
4514 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4515 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4516 '\x00', '\x00', '\x00',
4518 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4519 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4520 '\x84', '\x00', '\x00',
4521 '\x00', '\x00', '\x00' };
4522 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4523 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4524 '\x1f', '\x84', '\x00',
4525 '\x00', '\x00', '\x00',
4527 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4528 '\x66', '\x66', '\x2e', // data16
4529 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4530 '\x00', '\x00', '\x00',
4532 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4533 '\x66', '\x66', '\x66', // data16; data16
4534 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4535 '\x84', '\x00', '\x00',
4536 '\x00', '\x00', '\x00' };
4538 const char* nops[16] = {
4540 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4541 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4544 return std::string(nops[length], length);
4547 // Return the addend to use for a target specific relocation. The
4548 // only target specific relocation is R_X86_64_TLSDESC for a local
4549 // symbol. We want to set the addend is the offset of the local
4550 // symbol in the TLS segment.
4554 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4557 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4558 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4559 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4560 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4561 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4562 gold_assert(psymval->is_tls_symbol());
4563 // The value of a TLS symbol is the offset in the TLS segment.
4564 return psymval->value(ti.object, 0);
4567 // Return the value to use for the base of a DW_EH_PE_datarel offset
4568 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4569 // assembler can not write out the difference between two labels in
4570 // different sections, so instead of using a pc-relative value they
4571 // use an offset from the GOT.
4575 Target_x86_64<size>::do_ehframe_datarel_base() const
4577 gold_assert(this->global_offset_table_ != NULL);
4578 Symbol* sym = this->global_offset_table_;
4579 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4580 return ssym->value();
4583 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4584 // compiled with -fsplit-stack. The function calls non-split-stack
4585 // code. We have to change the function so that it always ensures
4586 // that it has enough stack space to run some random function.
4588 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
4589 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
4590 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
4592 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4593 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
4594 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4598 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4599 section_offset_type fnoffset,
4600 section_size_type fnsize,
4601 const unsigned char*,
4603 unsigned char* view,
4604 section_size_type view_size,
4606 std::string* to) const
4608 const char* const cmp_insn = reinterpret_cast<const char*>
4609 (size == 32 ? cmp_insn_32 : cmp_insn_64);
4610 const char* const lea_r10_insn = reinterpret_cast<const char*>
4611 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
4612 const char* const lea_r11_insn = reinterpret_cast<const char*>
4613 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
4615 const size_t cmp_insn_len =
4616 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
4617 const size_t lea_r10_insn_len =
4618 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
4619 const size_t lea_r11_insn_len =
4620 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
4621 const size_t nop_len = (size == 32 ? 7 : 8);
4623 // The function starts with a comparison of the stack pointer and a
4624 // field in the TCB. This is followed by a jump.
4627 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
4628 && fnsize > nop_len + 1)
4630 // We will call __morestack if the carry flag is set after this
4631 // comparison. We turn the comparison into an stc instruction
4633 view[fnoffset] = '\xf9';
4634 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
4636 // lea NN(%rsp),%r10
4637 // lea NN(%rsp),%r11
4638 else if ((this->match_view(view, view_size, fnoffset,
4639 lea_r10_insn, lea_r10_insn_len)
4640 || this->match_view(view, view_size, fnoffset,
4641 lea_r11_insn, lea_r11_insn_len))
4644 // This is loading an offset from the stack pointer for a
4645 // comparison. The offset is negative, so we decrease the
4646 // offset by the amount of space we need for the stack. This
4647 // means we will avoid calling __morestack if there happens to
4648 // be plenty of space on the stack already.
4649 unsigned char* pval = view + fnoffset + 4;
4650 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4651 val -= parameters->options().split_stack_adjust_size();
4652 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4656 if (!object->has_no_split_stack())
4657 object->error(_("failed to match split-stack sequence at "
4658 "section %u offset %0zx"),
4659 shndx, static_cast<size_t>(fnoffset));
4663 // We have to change the function so that it calls
4664 // __morestack_non_split instead of __morestack. The former will
4665 // allocate additional stack space.
4666 *from = "__morestack";
4667 *to = "__morestack_non_split";
4670 // The selector for x86_64 object files. Note this is never instantiated
4671 // directly. It's only used in Target_selector_x86_64_nacl, below.
4674 class Target_selector_x86_64 : public Target_selector_freebsd
4677 Target_selector_x86_64()
4678 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4680 ? "elf64-x86-64" : "elf32-x86-64"),
4682 ? "elf64-x86-64-freebsd"
4683 : "elf32-x86-64-freebsd"),
4684 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4688 do_instantiate_target()
4689 { return new Target_x86_64<size>(); }
4693 // NaCl variant. It uses different PLT contents.
4696 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4699 Output_data_plt_x86_64_nacl(Layout* layout,
4700 Output_data_got<64, false>* got,
4701 Output_data_got_plt_x86_64* got_plt,
4702 Output_data_space* got_irelative)
4703 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4704 got, got_plt, got_irelative)
4707 Output_data_plt_x86_64_nacl(Layout* layout,
4708 Output_data_got<64, false>* got,
4709 Output_data_got_plt_x86_64* got_plt,
4710 Output_data_space* got_irelative,
4711 unsigned int plt_count)
4712 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4713 got, got_plt, got_irelative,
4718 virtual unsigned int
4719 do_get_plt_entry_size() const
4720 { return plt_entry_size; }
4723 do_add_eh_frame(Layout* layout)
4725 layout->add_eh_frame_for_plt(this,
4726 this->plt_eh_frame_cie,
4727 this->plt_eh_frame_cie_size,
4729 plt_eh_frame_fde_size);
4733 do_fill_first_plt_entry(unsigned char* pov,
4734 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4735 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4737 virtual unsigned int
4738 do_fill_plt_entry(unsigned char* pov,
4739 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4740 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4741 unsigned int got_offset,
4742 unsigned int plt_offset,
4743 unsigned int plt_index);
4746 do_fill_tlsdesc_entry(unsigned char* pov,
4747 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4748 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4749 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4750 unsigned int tlsdesc_got_offset,
4751 unsigned int plt_offset);
4754 // The size of an entry in the PLT.
4755 static const int plt_entry_size = 64;
4757 // The first entry in the PLT.
4758 static const unsigned char first_plt_entry[plt_entry_size];
4760 // Other entries in the PLT for an executable.
4761 static const unsigned char plt_entry[plt_entry_size];
4763 // The reserved TLSDESC entry in the PLT for an executable.
4764 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4766 // The .eh_frame unwind information for the PLT.
4767 static const int plt_eh_frame_fde_size = 32;
4768 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4772 class Target_x86_64_nacl : public Target_x86_64<size>
4775 Target_x86_64_nacl()
4776 : Target_x86_64<size>(&x86_64_nacl_info)
4779 virtual Output_data_plt_x86_64<size>*
4780 do_make_data_plt(Layout* layout,
4781 Output_data_got<64, false>* got,
4782 Output_data_got_plt_x86_64* got_plt,
4783 Output_data_space* got_irelative)
4785 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4789 virtual Output_data_plt_x86_64<size>*
4790 do_make_data_plt(Layout* layout,
4791 Output_data_got<64, false>* got,
4792 Output_data_got_plt_x86_64* got_plt,
4793 Output_data_space* got_irelative,
4794 unsigned int plt_count)
4796 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4802 do_code_fill(section_size_type length) const;
4805 static const Target::Target_info x86_64_nacl_info;
4809 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4812 false, // is_big_endian
4813 elfcpp::EM_X86_64, // machine_code
4814 false, // has_make_symbol
4815 false, // has_resolve
4816 true, // has_code_fill
4817 true, // is_default_stack_executable
4818 true, // can_icf_inline_merge_sections
4820 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4821 0x20000, // default_text_segment_address
4822 0x10000, // abi_pagesize (overridable by -z max-page-size)
4823 0x10000, // common_pagesize (overridable by -z common-page-size)
4824 true, // isolate_execinstr
4825 0x10000000, // rosegment_gap
4826 elfcpp::SHN_UNDEF, // small_common_shndx
4827 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4828 0, // small_common_section_flags
4829 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4830 NULL, // attributes_section
4831 NULL, // attributes_vendor
4832 "_start", // entry_symbol_name
4833 32, // hash_entry_size
4837 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4840 false, // is_big_endian
4841 elfcpp::EM_X86_64, // machine_code
4842 false, // has_make_symbol
4843 false, // has_resolve
4844 true, // has_code_fill
4845 true, // is_default_stack_executable
4846 true, // can_icf_inline_merge_sections
4848 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4849 0x20000, // default_text_segment_address
4850 0x10000, // abi_pagesize (overridable by -z max-page-size)
4851 0x10000, // common_pagesize (overridable by -z common-page-size)
4852 true, // isolate_execinstr
4853 0x10000000, // rosegment_gap
4854 elfcpp::SHN_UNDEF, // small_common_shndx
4855 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4856 0, // small_common_section_flags
4857 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4858 NULL, // attributes_section
4859 NULL, // attributes_vendor
4860 "_start", // entry_symbol_name
4861 32, // hash_entry_size
4864 #define NACLMASK 0xe0 // 32-byte alignment mask.
4866 // The first entry in the PLT.
4870 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4872 0xff, 0x35, // pushq contents of memory address
4873 0, 0, 0, 0, // replaced with address of .got + 8
4874 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4875 0, 0, 0, 0, // replaced with address of .got + 16
4876 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4877 0x4d, 0x01, 0xfb, // add %r15, %r11
4878 0x41, 0xff, 0xe3, // jmpq *%r11
4880 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4881 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4883 // 32 bytes of nop to pad out to the standard size
4884 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4885 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4886 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4887 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4888 0x66, // excess data32 prefix
4894 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4896 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4897 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4899 memcpy(pov, first_plt_entry, plt_entry_size);
4900 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4902 - (plt_address + 2 + 4)));
4903 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4905 - (plt_address + 9 + 4)));
4908 // Subsequent entries in the PLT.
4912 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4914 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4915 0, 0, 0, 0, // replaced with address of symbol in .got
4916 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4917 0x4d, 0x01, 0xfb, // add %r15, %r11
4918 0x41, 0xff, 0xe3, // jmpq *%r11
4920 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4921 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4922 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4924 // Lazy GOT entries point here (32-byte aligned).
4925 0x68, // pushq immediate
4926 0, 0, 0, 0, // replaced with index into relocation table
4927 0xe9, // jmp relative
4928 0, 0, 0, 0, // replaced with offset to start of .plt0
4930 // 22 bytes of nop to pad out to the standard size.
4931 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4932 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4933 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4938 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4940 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4941 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4942 unsigned int got_offset,
4943 unsigned int plt_offset,
4944 unsigned int plt_index)
4946 memcpy(pov, plt_entry, plt_entry_size);
4947 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4948 (got_address + got_offset
4949 - (plt_address + plt_offset
4952 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4953 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4954 - (plt_offset + 38 + 4));
4959 // The reserved TLSDESC entry in the PLT.
4963 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4965 0xff, 0x35, // pushq x(%rip)
4966 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4967 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4968 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4969 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4970 0x4d, 0x01, 0xfb, // add %r15, %r11
4971 0x41, 0xff, 0xe3, // jmpq *%r11
4973 // 41 bytes of nop to pad out to the standard size.
4974 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4975 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4976 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4977 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4978 0x66, 0x66, // excess data32 prefixes
4979 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4984 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
4986 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4987 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4988 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4989 unsigned int tlsdesc_got_offset,
4990 unsigned int plt_offset)
4992 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
4993 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4995 - (plt_address + plt_offset
4997 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4999 + tlsdesc_got_offset
5000 - (plt_address + plt_offset
5004 // The .eh_frame unwind information for the PLT.
5008 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5010 0, 0, 0, 0, // Replaced with offset to .plt.
5011 0, 0, 0, 0, // Replaced with size of .plt.
5012 0, // Augmentation size.
5013 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5014 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5015 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5016 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5017 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5018 13, // Block length.
5019 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5020 elfcpp::DW_OP_breg16, 0, // Push %rip.
5021 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5022 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5023 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5024 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5025 elfcpp::DW_OP_lit3, // Push 3.
5026 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5027 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5028 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5032 // Return a string used to fill a code section with nops.
5033 // For NaCl, long NOPs are only valid if they do not cross
5034 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5037 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5039 return std::string(length, static_cast<char>(0x90));
5042 // The selector for x86_64-nacl object files.
5045 class Target_selector_x86_64_nacl
5046 : public Target_selector_nacl<Target_selector_x86_64<size>,
5047 Target_x86_64_nacl<size> >
5050 Target_selector_x86_64_nacl()
5051 : Target_selector_nacl<Target_selector_x86_64<size>,
5052 Target_x86_64_nacl<size> >("x86-64",
5054 ? "elf64-x86-64-nacl"
5055 : "elf32-x86-64-nacl",
5058 : "elf32_x86_64_nacl")
5062 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5063 Target_selector_x86_64_nacl<32> target_selector_x32;
5065 } // End anonymous namespace.