1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2015 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,
508 const Relocatable_relocs*,
510 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
511 section_size_type view_size,
512 unsigned char* reloc_view,
513 section_size_type reloc_view_size);
515 // Return a string used to fill a code section with nops.
517 do_code_fill(section_size_type length) const;
519 // Return whether SYM is defined by the ABI.
521 do_is_defined_by_abi(const Symbol* sym) const
522 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
524 // Return the symbol index to use for a target specific relocation.
525 // The only target specific relocation is R_X86_64_TLSDESC for a
526 // local symbol, which is an absolute reloc.
528 do_reloc_symbol_index(void*, unsigned int r_type) const
530 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
534 // Return the addend to use for a target specific relocation.
536 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
538 // Return the PLT section.
540 do_plt_address_for_global(const Symbol* gsym) const
541 { return this->plt_section()->address_for_global(gsym); }
544 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
545 { return this->plt_section()->address_for_local(relobj, symndx); }
547 // This function should be defined in targets that can use relocation
548 // types to determine (implemented in local_reloc_may_be_function_pointer
549 // and global_reloc_may_be_function_pointer)
550 // if a function's pointer is taken. ICF uses this in safe mode to only
551 // fold those functions whose pointer is defintely not taken. For x86_64
552 // pie binaries, safe ICF cannot be done by looking at relocation types.
554 do_can_check_for_function_pointers() const
555 { return !parameters->options().pie(); }
557 // Return the base for a DW_EH_PE_datarel encoding.
559 do_ehframe_datarel_base() const;
561 // Adjust -fsplit-stack code which calls non-split-stack code.
563 do_calls_non_split(Relobj* object, unsigned int shndx,
564 section_offset_type fnoffset, section_size_type fnsize,
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>*, Target_x86_64*,
787 size_t relnum, const elfcpp::Rela<size, false>&,
788 unsigned int r_type, const Sized_symbol<size>*,
789 const Symbol_value<size>*,
790 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
794 // Do a TLS relocation.
796 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
797 size_t relnum, const elfcpp::Rela<size, false>&,
798 unsigned int r_type, const Sized_symbol<size>*,
799 const Symbol_value<size>*,
800 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
803 // Do a TLS General-Dynamic to Initial-Exec transition.
805 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
806 Output_segment* tls_segment,
807 const elfcpp::Rela<size, false>&, unsigned int r_type,
808 typename elfcpp::Elf_types<size>::Elf_Addr value,
810 typename elfcpp::Elf_types<size>::Elf_Addr,
811 section_size_type view_size);
813 // Do a TLS General-Dynamic to Local-Exec transition.
815 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
816 Output_segment* tls_segment,
817 const elfcpp::Rela<size, false>&, unsigned int r_type,
818 typename elfcpp::Elf_types<size>::Elf_Addr value,
820 section_size_type view_size);
822 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
824 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
825 Output_segment* tls_segment,
826 const elfcpp::Rela<size, false>&, unsigned int r_type,
827 typename elfcpp::Elf_types<size>::Elf_Addr value,
829 typename elfcpp::Elf_types<size>::Elf_Addr,
830 section_size_type view_size);
832 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
834 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
835 Output_segment* tls_segment,
836 const elfcpp::Rela<size, false>&, unsigned int r_type,
837 typename elfcpp::Elf_types<size>::Elf_Addr value,
839 section_size_type view_size);
841 // Do a TLS Local-Dynamic to Local-Exec transition.
843 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
844 Output_segment* tls_segment,
845 const elfcpp::Rela<size, false>&, unsigned int r_type,
846 typename elfcpp::Elf_types<size>::Elf_Addr value,
848 section_size_type view_size);
850 // Do a TLS Initial-Exec to Local-Exec transition.
852 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
853 Output_segment* tls_segment,
854 const elfcpp::Rela<size, false>&, unsigned int r_type,
855 typename elfcpp::Elf_types<size>::Elf_Addr value,
857 section_size_type view_size);
859 // This is set if we should skip the next reloc, which should be a
860 // PLT32 reloc against ___tls_get_addr.
861 bool skip_call_tls_get_addr_;
864 // A class which returns the size required for a relocation type,
865 // used while scanning relocs during a relocatable link.
866 class Relocatable_size_for_reloc
870 get_size_for_reloc(unsigned int, Relobj*);
873 // Check if relocation against this symbol is a candidate for
875 // mov foo@GOTPCREL(%rip), %reg
876 // to lea foo(%rip), %reg.
878 can_convert_mov_to_lea(const Symbol* gsym)
880 gold_assert(gsym != NULL);
881 return (gsym->type() != elfcpp::STT_GNU_IFUNC
882 && !gsym->is_undefined ()
883 && !gsym->is_from_dynobj()
884 && !gsym->is_preemptible()
885 && (!parameters->options().shared()
886 || (gsym->visibility() != elfcpp::STV_DEFAULT
887 && gsym->visibility() != elfcpp::STV_PROTECTED)
888 || parameters->options().Bsymbolic())
889 && strcmp(gsym->name(), "_DYNAMIC") != 0);
892 // Adjust TLS relocation type based on the options and whether this
893 // is a local symbol.
894 static tls::Tls_optimization
895 optimize_tls_reloc(bool is_final, int r_type);
897 // Get the GOT section, creating it if necessary.
898 Output_data_got<64, false>*
899 got_section(Symbol_table*, Layout*);
901 // Get the GOT PLT section.
902 Output_data_got_plt_x86_64*
903 got_plt_section() const
905 gold_assert(this->got_plt_ != NULL);
906 return this->got_plt_;
909 // Get the GOT section for TLSDESC entries.
910 Output_data_got<64, false>*
911 got_tlsdesc_section() const
913 gold_assert(this->got_tlsdesc_ != NULL);
914 return this->got_tlsdesc_;
917 // Create the PLT section.
919 make_plt_section(Symbol_table* symtab, Layout* layout);
921 // Create a PLT entry for a global symbol.
923 make_plt_entry(Symbol_table*, Layout*, Symbol*);
925 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
927 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
928 Sized_relobj_file<size, false>* relobj,
929 unsigned int local_sym_index);
931 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
933 define_tls_base_symbol(Symbol_table*, Layout*);
935 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
937 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
939 // Create a GOT entry for the TLS module index.
941 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
942 Sized_relobj_file<size, false>* object);
944 // Get the PLT section.
945 Output_data_plt_x86_64<size>*
948 gold_assert(this->plt_ != NULL);
952 // Get the dynamic reloc section, creating it if necessary.
954 rela_dyn_section(Layout*);
956 // Get the section to use for TLSDESC relocations.
958 rela_tlsdesc_section(Layout*) const;
960 // Get the section to use for IRELATIVE relocations.
962 rela_irelative_section(Layout*);
964 // Add a potential copy relocation.
966 copy_reloc(Symbol_table* symtab, Layout* layout,
967 Sized_relobj_file<size, false>* object,
968 unsigned int shndx, Output_section* output_section,
969 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
971 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
972 this->copy_relocs_.copy_reloc(symtab, layout,
973 symtab->get_sized_symbol<size>(sym),
974 object, shndx, output_section,
975 r_type, reloc.get_r_offset(),
976 reloc.get_r_addend(),
977 this->rela_dyn_section(layout));
980 // Information about this specific target which we pass to the
981 // general Target structure.
982 static const Target::Target_info x86_64_info;
984 // The types of GOT entries needed for this platform.
985 // These values are exposed to the ABI in an incremental link.
986 // Do not renumber existing values without changing the version
987 // number of the .gnu_incremental_inputs section.
990 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
991 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
992 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
993 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
996 // This type is used as the argument to the target specific
997 // relocation routines. The only target specific reloc is
998 // R_X86_64_TLSDESC against a local symbol.
1001 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1002 : object(a_object), r_sym(a_r_sym)
1005 // The object in which the local symbol is defined.
1006 Sized_relobj_file<size, false>* object;
1007 // The local symbol index in the object.
1012 Output_data_got<64, false>* got_;
1014 Output_data_plt_x86_64<size>* plt_;
1015 // The GOT PLT section.
1016 Output_data_got_plt_x86_64* got_plt_;
1017 // The GOT section for IRELATIVE relocations.
1018 Output_data_space* got_irelative_;
1019 // The GOT section for TLSDESC relocations.
1020 Output_data_got<64, false>* got_tlsdesc_;
1021 // The _GLOBAL_OFFSET_TABLE_ symbol.
1022 Symbol* global_offset_table_;
1023 // The dynamic reloc section.
1024 Reloc_section* rela_dyn_;
1025 // The section to use for IRELATIVE relocs.
1026 Reloc_section* rela_irelative_;
1027 // Relocs saved to avoid a COPY reloc.
1028 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1029 // Offset of the GOT entry for the TLS module index.
1030 unsigned int got_mod_index_offset_;
1031 // We handle R_X86_64_TLSDESC against a local symbol as a target
1032 // specific relocation. Here we store the object and local symbol
1033 // index for the relocation.
1034 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1035 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1036 bool tls_base_symbol_defined_;
1040 const Target::Target_info Target_x86_64<64>::x86_64_info =
1043 false, // is_big_endian
1044 elfcpp::EM_X86_64, // machine_code
1045 false, // has_make_symbol
1046 false, // has_resolve
1047 true, // has_code_fill
1048 true, // is_default_stack_executable
1049 true, // can_icf_inline_merge_sections
1051 "/lib/ld64.so.1", // program interpreter
1052 0x400000, // default_text_segment_address
1053 0x1000, // abi_pagesize (overridable by -z max-page-size)
1054 0x1000, // common_pagesize (overridable by -z common-page-size)
1055 false, // isolate_execinstr
1057 elfcpp::SHN_UNDEF, // small_common_shndx
1058 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1059 0, // small_common_section_flags
1060 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1061 NULL, // attributes_section
1062 NULL, // attributes_vendor
1063 "_start", // entry_symbol_name
1064 32, // hash_entry_size
1068 const Target::Target_info Target_x86_64<32>::x86_64_info =
1071 false, // is_big_endian
1072 elfcpp::EM_X86_64, // machine_code
1073 false, // has_make_symbol
1074 false, // has_resolve
1075 true, // has_code_fill
1076 true, // is_default_stack_executable
1077 true, // can_icf_inline_merge_sections
1079 "/libx32/ldx32.so.1", // program interpreter
1080 0x400000, // default_text_segment_address
1081 0x1000, // abi_pagesize (overridable by -z max-page-size)
1082 0x1000, // common_pagesize (overridable by -z common-page-size)
1083 false, // isolate_execinstr
1085 elfcpp::SHN_UNDEF, // small_common_shndx
1086 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1087 0, // small_common_section_flags
1088 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1089 NULL, // attributes_section
1090 NULL, // attributes_vendor
1091 "_start", // entry_symbol_name
1092 32, // hash_entry_size
1095 // This is called when a new output section is created. This is where
1096 // we handle the SHF_X86_64_LARGE.
1100 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1102 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1103 os->set_is_large_section();
1106 // Get the GOT section, creating it if necessary.
1109 Output_data_got<64, false>*
1110 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1112 if (this->got_ == NULL)
1114 gold_assert(symtab != NULL && layout != NULL);
1116 // When using -z now, we can treat .got.plt as a relro section.
1117 // Without -z now, it is modified after program startup by lazy
1119 bool is_got_plt_relro = parameters->options().now();
1120 Output_section_order got_order = (is_got_plt_relro
1122 : ORDER_RELRO_LAST);
1123 Output_section_order got_plt_order = (is_got_plt_relro
1125 : ORDER_NON_RELRO_FIRST);
1127 this->got_ = new Output_data_got<64, false>();
1129 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1131 | elfcpp::SHF_WRITE),
1132 this->got_, got_order, true);
1134 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1135 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1137 | elfcpp::SHF_WRITE),
1138 this->got_plt_, got_plt_order,
1141 // The first three entries are reserved.
1142 this->got_plt_->set_current_data_size(3 * 8);
1144 if (!is_got_plt_relro)
1146 // Those bytes can go into the relro segment.
1147 layout->increase_relro(3 * 8);
1150 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1151 this->global_offset_table_ =
1152 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1153 Symbol_table::PREDEFINED,
1155 0, 0, elfcpp::STT_OBJECT,
1157 elfcpp::STV_HIDDEN, 0,
1160 // If there are any IRELATIVE relocations, they get GOT entries
1161 // in .got.plt after the jump slot entries.
1162 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1163 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1165 | elfcpp::SHF_WRITE),
1166 this->got_irelative_,
1167 got_plt_order, is_got_plt_relro);
1169 // If there are any TLSDESC relocations, they get GOT entries in
1170 // .got.plt after the jump slot and IRELATIVE entries.
1171 this->got_tlsdesc_ = new Output_data_got<64, false>();
1172 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1174 | elfcpp::SHF_WRITE),
1176 got_plt_order, is_got_plt_relro);
1182 // Get the dynamic reloc section, creating it if necessary.
1185 typename Target_x86_64<size>::Reloc_section*
1186 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1188 if (this->rela_dyn_ == NULL)
1190 gold_assert(layout != NULL);
1191 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1192 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1193 elfcpp::SHF_ALLOC, this->rela_dyn_,
1194 ORDER_DYNAMIC_RELOCS, false);
1196 return this->rela_dyn_;
1199 // Get the section to use for IRELATIVE relocs, creating it if
1200 // necessary. These go in .rela.dyn, but only after all other dynamic
1201 // relocations. They need to follow the other dynamic relocations so
1202 // that they can refer to global variables initialized by those
1206 typename Target_x86_64<size>::Reloc_section*
1207 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1209 if (this->rela_irelative_ == NULL)
1211 // Make sure we have already created the dynamic reloc section.
1212 this->rela_dyn_section(layout);
1213 this->rela_irelative_ = new Reloc_section(false);
1214 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1215 elfcpp::SHF_ALLOC, this->rela_irelative_,
1216 ORDER_DYNAMIC_RELOCS, false);
1217 gold_assert(this->rela_dyn_->output_section()
1218 == this->rela_irelative_->output_section());
1220 return this->rela_irelative_;
1223 // Write the first three reserved words of the .got.plt section.
1224 // The remainder of the section is written while writing the PLT
1225 // in Output_data_plt_i386::do_write.
1228 Output_data_got_plt_x86_64::do_write(Output_file* of)
1230 // The first entry in the GOT is the address of the .dynamic section
1231 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1232 // We saved space for them when we created the section in
1233 // Target_x86_64::got_section.
1234 const off_t got_file_offset = this->offset();
1235 gold_assert(this->data_size() >= 24);
1236 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1237 Output_section* dynamic = this->layout_->dynamic_section();
1238 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1239 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1240 memset(got_view + 8, 0, 16);
1241 of->write_output_view(got_file_offset, 24, got_view);
1244 // Initialize the PLT section.
1248 Output_data_plt_x86_64<size>::init(Layout* layout)
1250 this->rel_ = new Reloc_section(false);
1251 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1252 elfcpp::SHF_ALLOC, this->rel_,
1253 ORDER_DYNAMIC_PLT_RELOCS, false);
1258 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1260 os->set_entsize(this->get_plt_entry_size());
1263 // Add an entry to the PLT.
1267 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1270 gold_assert(!gsym->has_plt_offset());
1272 unsigned int plt_index;
1274 section_offset_type got_offset;
1276 unsigned int* pcount;
1277 unsigned int offset;
1278 unsigned int reserved;
1279 Output_section_data_build* got;
1280 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1281 && gsym->can_use_relative_reloc(false))
1283 pcount = &this->irelative_count_;
1286 got = this->got_irelative_;
1290 pcount = &this->count_;
1293 got = this->got_plt_;
1296 if (!this->is_data_size_valid())
1298 // Note that when setting the PLT offset for a non-IRELATIVE
1299 // entry we skip the initial reserved PLT entry.
1300 plt_index = *pcount + offset;
1301 plt_offset = plt_index * this->get_plt_entry_size();
1305 got_offset = (plt_index - offset + reserved) * 8;
1306 gold_assert(got_offset == got->current_data_size());
1308 // Every PLT entry needs a GOT entry which points back to the PLT
1309 // entry (this will be changed by the dynamic linker, normally
1310 // lazily when the function is called).
1311 got->set_current_data_size(got_offset + 8);
1315 // FIXME: This is probably not correct for IRELATIVE relocs.
1317 // For incremental updates, find an available slot.
1318 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1319 this->get_plt_entry_size(), 0);
1320 if (plt_offset == -1)
1321 gold_fallback(_("out of patch space (PLT);"
1322 " relink with --incremental-full"));
1324 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1325 // can be calculated from the PLT index, adjusting for the three
1326 // reserved entries at the beginning of the GOT.
1327 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1328 got_offset = (plt_index - offset + reserved) * 8;
1331 gsym->set_plt_offset(plt_offset);
1333 // Every PLT entry needs a reloc.
1334 this->add_relocation(symtab, layout, gsym, got_offset);
1336 // Note that we don't need to save the symbol. The contents of the
1337 // PLT are independent of which symbols are used. The symbols only
1338 // appear in the relocations.
1341 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1346 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1347 Symbol_table* symtab,
1349 Sized_relobj_file<size, false>* relobj,
1350 unsigned int local_sym_index)
1352 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1353 ++this->irelative_count_;
1355 section_offset_type got_offset = this->got_irelative_->current_data_size();
1357 // Every PLT entry needs a GOT entry which points back to the PLT
1359 this->got_irelative_->set_current_data_size(got_offset + 8);
1361 // Every PLT entry needs a reloc.
1362 Reloc_section* rela = this->rela_irelative(symtab, layout);
1363 rela->add_symbolless_local_addend(relobj, local_sym_index,
1364 elfcpp::R_X86_64_IRELATIVE,
1365 this->got_irelative_, got_offset, 0);
1370 // Add the relocation for a PLT entry.
1374 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1377 unsigned int got_offset)
1379 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1380 && gsym->can_use_relative_reloc(false))
1382 Reloc_section* rela = this->rela_irelative(symtab, layout);
1383 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1384 this->got_irelative_, got_offset, 0);
1388 gsym->set_needs_dynsym_entry();
1389 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1394 // Return where the TLSDESC relocations should go, creating it if
1395 // necessary. These follow the JUMP_SLOT relocations.
1398 typename Output_data_plt_x86_64<size>::Reloc_section*
1399 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1401 if (this->tlsdesc_rel_ == NULL)
1403 this->tlsdesc_rel_ = new Reloc_section(false);
1404 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1405 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1406 ORDER_DYNAMIC_PLT_RELOCS, false);
1407 gold_assert(this->tlsdesc_rel_->output_section()
1408 == this->rel_->output_section());
1410 return this->tlsdesc_rel_;
1413 // Return where the IRELATIVE relocations should go in the PLT. These
1414 // follow the JUMP_SLOT and the TLSDESC relocations.
1417 typename Output_data_plt_x86_64<size>::Reloc_section*
1418 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1421 if (this->irelative_rel_ == NULL)
1423 // Make sure we have a place for the TLSDESC relocations, in
1424 // case we see any later on.
1425 this->rela_tlsdesc(layout);
1426 this->irelative_rel_ = new Reloc_section(false);
1427 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1428 elfcpp::SHF_ALLOC, this->irelative_rel_,
1429 ORDER_DYNAMIC_PLT_RELOCS, false);
1430 gold_assert(this->irelative_rel_->output_section()
1431 == this->rel_->output_section());
1433 if (parameters->doing_static_link())
1435 // A statically linked executable will only have a .rela.plt
1436 // section to hold R_X86_64_IRELATIVE relocs for
1437 // STT_GNU_IFUNC symbols. The library will use these
1438 // symbols to locate the IRELATIVE relocs at program startup
1440 symtab->define_in_output_data("__rela_iplt_start", NULL,
1441 Symbol_table::PREDEFINED,
1442 this->irelative_rel_, 0, 0,
1443 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1444 elfcpp::STV_HIDDEN, 0, false, true);
1445 symtab->define_in_output_data("__rela_iplt_end", NULL,
1446 Symbol_table::PREDEFINED,
1447 this->irelative_rel_, 0, 0,
1448 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1449 elfcpp::STV_HIDDEN, 0, true, true);
1452 return this->irelative_rel_;
1455 // Return the PLT address to use for a global symbol.
1459 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1461 uint64_t offset = 0;
1462 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1463 && gsym->can_use_relative_reloc(false))
1464 offset = (this->count_ + 1) * this->get_plt_entry_size();
1465 return this->address() + offset + gsym->plt_offset();
1468 // Return the PLT address to use for a local symbol. These are always
1469 // IRELATIVE relocs.
1473 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
1476 return (this->address()
1477 + (this->count_ + 1) * this->get_plt_entry_size()
1478 + object->local_plt_offset(r_sym));
1481 // Set the final size.
1484 Output_data_plt_x86_64<size>::set_final_data_size()
1486 unsigned int count = this->count_ + this->irelative_count_;
1487 if (this->has_tlsdesc_entry())
1489 this->set_data_size((count + 1) * this->get_plt_entry_size());
1492 // The first entry in the PLT for an executable.
1496 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1498 // From AMD64 ABI Draft 0.98, page 76
1499 0xff, 0x35, // pushq contents of memory address
1500 0, 0, 0, 0, // replaced with address of .got + 8
1501 0xff, 0x25, // jmp indirect
1502 0, 0, 0, 0, // replaced with address of .got + 16
1503 0x90, 0x90, 0x90, 0x90 // noop (x4)
1508 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1510 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1511 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1513 memcpy(pov, first_plt_entry, plt_entry_size);
1514 // We do a jmp relative to the PC at the end of this instruction.
1515 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1517 - (plt_address + 6)));
1518 elfcpp::Swap<32, false>::writeval(pov + 8,
1520 - (plt_address + 12)));
1523 // Subsequent entries in the PLT for an executable.
1527 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1529 // From AMD64 ABI Draft 0.98, page 76
1530 0xff, 0x25, // jmpq indirect
1531 0, 0, 0, 0, // replaced with address of symbol in .got
1532 0x68, // pushq immediate
1533 0, 0, 0, 0, // replaced with offset into relocation table
1534 0xe9, // jmpq relative
1535 0, 0, 0, 0 // replaced with offset to start of .plt
1540 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1542 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1543 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1544 unsigned int got_offset,
1545 unsigned int plt_offset,
1546 unsigned int plt_index)
1548 // Check PC-relative offset overflow in PLT entry.
1549 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1550 - (plt_address + plt_offset + 6));
1551 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1552 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1555 memcpy(pov, plt_entry, plt_entry_size);
1556 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1557 plt_got_pcrel_offset);
1559 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1560 elfcpp::Swap<32, false>::writeval(pov + 12,
1561 - (plt_offset + plt_entry_size));
1566 // The reserved TLSDESC entry in the PLT for an executable.
1570 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1572 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1573 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1574 0xff, 0x35, // pushq x(%rip)
1575 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1576 0xff, 0x25, // jmpq *y(%rip)
1577 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1584 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1586 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1587 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1588 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1589 unsigned int tlsdesc_got_offset,
1590 unsigned int plt_offset)
1592 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1593 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1595 - (plt_address + plt_offset
1597 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1599 + tlsdesc_got_offset
1600 - (plt_address + plt_offset
1604 // The .eh_frame unwind information for the PLT.
1608 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1611 'z', // Augmentation: augmentation size included.
1612 'R', // Augmentation: FDE encoding included.
1613 '\0', // End of augmentation string.
1614 1, // Code alignment factor.
1615 0x78, // Data alignment factor.
1616 16, // Return address column.
1617 1, // Augmentation size.
1618 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1619 | elfcpp::DW_EH_PE_sdata4),
1620 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1621 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1622 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1628 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1630 0, 0, 0, 0, // Replaced with offset to .plt.
1631 0, 0, 0, 0, // Replaced with size of .plt.
1632 0, // Augmentation size.
1633 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1634 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1635 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1636 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1637 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1638 11, // Block length.
1639 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1640 elfcpp::DW_OP_breg16, 0, // Push %rip.
1641 elfcpp::DW_OP_lit15, // Push 0xf.
1642 elfcpp::DW_OP_and, // & (%rip & 0xf).
1643 elfcpp::DW_OP_lit11, // Push 0xb.
1644 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1645 elfcpp::DW_OP_lit3, // Push 3.
1646 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1647 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1648 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1654 // Write out the PLT. This uses the hand-coded instructions above,
1655 // and adjusts them as needed. This is specified by the AMD64 ABI.
1659 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1661 const off_t offset = this->offset();
1662 const section_size_type oview_size =
1663 convert_to_section_size_type(this->data_size());
1664 unsigned char* const oview = of->get_output_view(offset, oview_size);
1666 const off_t got_file_offset = this->got_plt_->offset();
1667 gold_assert(parameters->incremental_update()
1668 || (got_file_offset + this->got_plt_->data_size()
1669 == this->got_irelative_->offset()));
1670 const section_size_type got_size =
1671 convert_to_section_size_type(this->got_plt_->data_size()
1672 + this->got_irelative_->data_size());
1673 unsigned char* const got_view = of->get_output_view(got_file_offset,
1676 unsigned char* pov = oview;
1678 // The base address of the .plt section.
1679 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1680 // The base address of the .got section.
1681 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1682 // The base address of the PLT portion of the .got section,
1683 // which is where the GOT pointer will point, and where the
1684 // three reserved GOT entries are located.
1685 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1686 = this->got_plt_->address();
1688 this->fill_first_plt_entry(pov, got_address, plt_address);
1689 pov += this->get_plt_entry_size();
1691 // The first three entries in the GOT are reserved, and are written
1692 // by Output_data_got_plt_x86_64::do_write.
1693 unsigned char* got_pov = got_view + 24;
1695 unsigned int plt_offset = this->get_plt_entry_size();
1696 unsigned int got_offset = 24;
1697 const unsigned int count = this->count_ + this->irelative_count_;
1698 for (unsigned int plt_index = 0;
1701 pov += this->get_plt_entry_size(),
1703 plt_offset += this->get_plt_entry_size(),
1706 // Set and adjust the PLT entry itself.
1707 unsigned int lazy_offset = this->fill_plt_entry(pov,
1708 got_address, plt_address,
1709 got_offset, plt_offset,
1712 // Set the entry in the GOT.
1713 elfcpp::Swap<64, false>::writeval(got_pov,
1714 plt_address + plt_offset + lazy_offset);
1717 if (this->has_tlsdesc_entry())
1719 // Set and adjust the reserved TLSDESC PLT entry.
1720 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1721 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
1722 tlsdesc_got_offset, plt_offset);
1723 pov += this->get_plt_entry_size();
1726 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1727 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1729 of->write_output_view(offset, oview_size, oview);
1730 of->write_output_view(got_file_offset, got_size, got_view);
1733 // Create the PLT section.
1737 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1739 if (this->plt_ == NULL)
1741 // Create the GOT sections first.
1742 this->got_section(symtab, layout);
1744 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
1745 this->got_irelative_);
1747 // Add unwind information if requested.
1748 if (parameters->options().ld_generated_unwind_info())
1749 this->plt_->add_eh_frame(layout);
1751 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1753 | elfcpp::SHF_EXECINSTR),
1754 this->plt_, ORDER_PLT, false);
1756 // Make the sh_info field of .rela.plt point to .plt.
1757 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1758 rela_plt_os->set_info_section(this->plt_->output_section());
1762 // Return the section for TLSDESC relocations.
1765 typename Target_x86_64<size>::Reloc_section*
1766 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1768 return this->plt_section()->rela_tlsdesc(layout);
1771 // Create a PLT entry for a global symbol.
1775 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1778 if (gsym->has_plt_offset())
1781 if (this->plt_ == NULL)
1782 this->make_plt_section(symtab, layout);
1784 this->plt_->add_entry(symtab, layout, gsym);
1787 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1791 Target_x86_64<size>::make_local_ifunc_plt_entry(
1792 Symbol_table* symtab, Layout* layout,
1793 Sized_relobj_file<size, false>* relobj,
1794 unsigned int local_sym_index)
1796 if (relobj->local_has_plt_offset(local_sym_index))
1798 if (this->plt_ == NULL)
1799 this->make_plt_section(symtab, layout);
1800 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1803 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1806 // Return the number of entries in the PLT.
1810 Target_x86_64<size>::plt_entry_count() const
1812 if (this->plt_ == NULL)
1814 return this->plt_->entry_count();
1817 // Return the offset of the first non-reserved PLT entry.
1821 Target_x86_64<size>::first_plt_entry_offset() const
1823 return this->plt_->first_plt_entry_offset();
1826 // Return the size of each PLT entry.
1830 Target_x86_64<size>::plt_entry_size() const
1832 return this->plt_->get_plt_entry_size();
1835 // Create the GOT and PLT sections for an incremental update.
1838 Output_data_got_base*
1839 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1841 unsigned int got_count,
1842 unsigned int plt_count)
1844 gold_assert(this->got_ == NULL);
1846 this->got_ = new Output_data_got<64, false>(got_count * 8);
1847 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1849 | elfcpp::SHF_WRITE),
1850 this->got_, ORDER_RELRO_LAST,
1853 // Add the three reserved entries.
1854 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
1855 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1857 | elfcpp::SHF_WRITE),
1858 this->got_plt_, ORDER_NON_RELRO_FIRST,
1861 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1862 this->global_offset_table_ =
1863 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1864 Symbol_table::PREDEFINED,
1866 0, 0, elfcpp::STT_OBJECT,
1868 elfcpp::STV_HIDDEN, 0,
1871 // If there are any TLSDESC relocations, they get GOT entries in
1872 // .got.plt after the jump slot entries.
1873 // FIXME: Get the count for TLSDESC entries.
1874 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1875 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1876 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1878 ORDER_NON_RELRO_FIRST, false);
1880 // If there are any IRELATIVE relocations, they get GOT entries in
1881 // .got.plt after the jump slot and TLSDESC entries.
1882 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1883 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1884 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1885 this->got_irelative_,
1886 ORDER_NON_RELRO_FIRST, false);
1888 // Create the PLT section.
1889 this->plt_ = this->make_data_plt(layout, this->got_,
1891 this->got_irelative_,
1894 // Add unwind information if requested.
1895 if (parameters->options().ld_generated_unwind_info())
1896 this->plt_->add_eh_frame(layout);
1898 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1899 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1900 this->plt_, ORDER_PLT, false);
1902 // Make the sh_info field of .rela.plt point to .plt.
1903 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1904 rela_plt_os->set_info_section(this->plt_->output_section());
1906 // Create the rela_dyn section.
1907 this->rela_dyn_section(layout);
1912 // Reserve a GOT entry for a local symbol, and regenerate any
1913 // necessary dynamic relocations.
1917 Target_x86_64<size>::reserve_local_got_entry(
1918 unsigned int got_index,
1919 Sized_relobj<size, false>* obj,
1921 unsigned int got_type)
1923 unsigned int got_offset = got_index * 8;
1924 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1926 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1929 case GOT_TYPE_STANDARD:
1930 if (parameters->options().output_is_position_independent())
1931 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1932 this->got_, got_offset, 0, false);
1934 case GOT_TYPE_TLS_OFFSET:
1935 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1936 this->got_, got_offset, 0);
1938 case GOT_TYPE_TLS_PAIR:
1939 this->got_->reserve_slot(got_index + 1);
1940 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1941 this->got_, got_offset, 0);
1943 case GOT_TYPE_TLS_DESC:
1944 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1945 // this->got_->reserve_slot(got_index + 1);
1946 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1947 // this->got_, got_offset, 0);
1954 // Reserve a GOT entry for a global symbol, and regenerate any
1955 // necessary dynamic relocations.
1959 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1961 unsigned int got_type)
1963 unsigned int got_offset = got_index * 8;
1964 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1966 this->got_->reserve_global(got_index, gsym, got_type);
1969 case GOT_TYPE_STANDARD:
1970 if (!gsym->final_value_is_known())
1972 if (gsym->is_from_dynobj()
1973 || gsym->is_undefined()
1974 || gsym->is_preemptible()
1975 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1976 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1977 this->got_, got_offset, 0);
1979 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1980 this->got_, got_offset, 0, false);
1983 case GOT_TYPE_TLS_OFFSET:
1984 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1985 this->got_, got_offset, 0, false);
1987 case GOT_TYPE_TLS_PAIR:
1988 this->got_->reserve_slot(got_index + 1);
1989 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1990 this->got_, got_offset, 0, false);
1991 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1992 this->got_, got_offset + 8, 0, false);
1994 case GOT_TYPE_TLS_DESC:
1995 this->got_->reserve_slot(got_index + 1);
1996 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1997 this->got_, got_offset, 0, false);
2004 // Register an existing PLT entry for a global symbol.
2008 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2010 unsigned int plt_index,
2013 gold_assert(this->plt_ != NULL);
2014 gold_assert(!gsym->has_plt_offset());
2016 this->plt_->reserve_slot(plt_index);
2018 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2020 unsigned int got_offset = (plt_index + 3) * 8;
2021 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2024 // Force a COPY relocation for a given symbol.
2028 Target_x86_64<size>::emit_copy_reloc(
2029 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2031 this->copy_relocs_.emit_copy_reloc(symtab,
2032 symtab->get_sized_symbol<size>(sym),
2035 this->rela_dyn_section(NULL));
2038 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2042 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2045 if (this->tls_base_symbol_defined_)
2048 Output_segment* tls_segment = layout->tls_segment();
2049 if (tls_segment != NULL)
2051 bool is_exec = parameters->options().output_is_executable();
2052 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2053 Symbol_table::PREDEFINED,
2057 elfcpp::STV_HIDDEN, 0,
2059 ? Symbol::SEGMENT_END
2060 : Symbol::SEGMENT_START),
2063 this->tls_base_symbol_defined_ = true;
2066 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2070 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2073 if (this->plt_ == NULL)
2074 this->make_plt_section(symtab, layout);
2076 if (!this->plt_->has_tlsdesc_entry())
2078 // Allocate the TLSDESC_GOT entry.
2079 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2080 unsigned int got_offset = got->add_constant(0);
2082 // Allocate the TLSDESC_PLT entry.
2083 this->plt_->reserve_tlsdesc_entry(got_offset);
2087 // Create a GOT entry for the TLS module index.
2091 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2092 Sized_relobj_file<size, false>* object)
2094 if (this->got_mod_index_offset_ == -1U)
2096 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2097 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2098 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2099 unsigned int got_offset = got->add_constant(0);
2100 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2102 got->add_constant(0);
2103 this->got_mod_index_offset_ = got_offset;
2105 return this->got_mod_index_offset_;
2108 // Optimize the TLS relocation type based on what we know about the
2109 // symbol. IS_FINAL is true if the final address of this symbol is
2110 // known at link time.
2113 tls::Tls_optimization
2114 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2116 // If we are generating a shared library, then we can't do anything
2118 if (parameters->options().shared())
2119 return tls::TLSOPT_NONE;
2123 case elfcpp::R_X86_64_TLSGD:
2124 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2125 case elfcpp::R_X86_64_TLSDESC_CALL:
2126 // These are General-Dynamic which permits fully general TLS
2127 // access. Since we know that we are generating an executable,
2128 // we can convert this to Initial-Exec. If we also know that
2129 // this is a local symbol, we can further switch to Local-Exec.
2131 return tls::TLSOPT_TO_LE;
2132 return tls::TLSOPT_TO_IE;
2134 case elfcpp::R_X86_64_TLSLD:
2135 // This is Local-Dynamic, which refers to a local symbol in the
2136 // dynamic TLS block. Since we know that we generating an
2137 // executable, we can switch to Local-Exec.
2138 return tls::TLSOPT_TO_LE;
2140 case elfcpp::R_X86_64_DTPOFF32:
2141 case elfcpp::R_X86_64_DTPOFF64:
2142 // Another Local-Dynamic reloc.
2143 return tls::TLSOPT_TO_LE;
2145 case elfcpp::R_X86_64_GOTTPOFF:
2146 // These are Initial-Exec relocs which get the thread offset
2147 // from the GOT. If we know that we are linking against the
2148 // local symbol, we can switch to Local-Exec, which links the
2149 // thread offset into the instruction.
2151 return tls::TLSOPT_TO_LE;
2152 return tls::TLSOPT_NONE;
2154 case elfcpp::R_X86_64_TPOFF32:
2155 // When we already have Local-Exec, there is nothing further we
2157 return tls::TLSOPT_NONE;
2164 // Get the Reference_flags for a particular relocation.
2168 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2172 case elfcpp::R_X86_64_NONE:
2173 case elfcpp::R_X86_64_GNU_VTINHERIT:
2174 case elfcpp::R_X86_64_GNU_VTENTRY:
2175 case elfcpp::R_X86_64_GOTPC32:
2176 case elfcpp::R_X86_64_GOTPC64:
2177 // No symbol reference.
2180 case elfcpp::R_X86_64_64:
2181 case elfcpp::R_X86_64_32:
2182 case elfcpp::R_X86_64_32S:
2183 case elfcpp::R_X86_64_16:
2184 case elfcpp::R_X86_64_8:
2185 return Symbol::ABSOLUTE_REF;
2187 case elfcpp::R_X86_64_PC64:
2188 case elfcpp::R_X86_64_PC32:
2189 case elfcpp::R_X86_64_PC32_BND:
2190 case elfcpp::R_X86_64_PC16:
2191 case elfcpp::R_X86_64_PC8:
2192 case elfcpp::R_X86_64_GOTOFF64:
2193 return Symbol::RELATIVE_REF;
2195 case elfcpp::R_X86_64_PLT32:
2196 case elfcpp::R_X86_64_PLT32_BND:
2197 case elfcpp::R_X86_64_PLTOFF64:
2198 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2200 case elfcpp::R_X86_64_GOT64:
2201 case elfcpp::R_X86_64_GOT32:
2202 case elfcpp::R_X86_64_GOTPCREL64:
2203 case elfcpp::R_X86_64_GOTPCREL:
2204 case elfcpp::R_X86_64_GOTPCRELX:
2205 case elfcpp::R_X86_64_REX_GOTPCRELX:
2206 case elfcpp::R_X86_64_GOTPLT64:
2208 return Symbol::ABSOLUTE_REF;
2210 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2211 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2212 case elfcpp::R_X86_64_TLSDESC_CALL:
2213 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2214 case elfcpp::R_X86_64_DTPOFF32:
2215 case elfcpp::R_X86_64_DTPOFF64:
2216 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2217 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2218 return Symbol::TLS_REF;
2220 case elfcpp::R_X86_64_COPY:
2221 case elfcpp::R_X86_64_GLOB_DAT:
2222 case elfcpp::R_X86_64_JUMP_SLOT:
2223 case elfcpp::R_X86_64_RELATIVE:
2224 case elfcpp::R_X86_64_IRELATIVE:
2225 case elfcpp::R_X86_64_TPOFF64:
2226 case elfcpp::R_X86_64_DTPMOD64:
2227 case elfcpp::R_X86_64_TLSDESC:
2228 case elfcpp::R_X86_64_SIZE32:
2229 case elfcpp::R_X86_64_SIZE64:
2231 // Not expected. We will give an error later.
2236 // Report an unsupported relocation against a local symbol.
2240 Target_x86_64<size>::Scan::unsupported_reloc_local(
2241 Sized_relobj_file<size, false>* object,
2242 unsigned int r_type)
2244 gold_error(_("%s: unsupported reloc %u against local symbol"),
2245 object->name().c_str(), r_type);
2248 // We are about to emit a dynamic relocation of type R_TYPE. If the
2249 // dynamic linker does not support it, issue an error. The GNU linker
2250 // only issues a non-PIC error for an allocated read-only section.
2251 // Here we know the section is allocated, but we don't know that it is
2252 // read-only. But we check for all the relocation types which the
2253 // glibc dynamic linker supports, so it seems appropriate to issue an
2254 // error even if the section is not read-only. If GSYM is not NULL,
2255 // it is the symbol the relocation is against; if it is NULL, the
2256 // relocation is against a local symbol.
2260 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2265 // These are the relocation types supported by glibc for x86_64
2266 // which should always work.
2267 case elfcpp::R_X86_64_RELATIVE:
2268 case elfcpp::R_X86_64_IRELATIVE:
2269 case elfcpp::R_X86_64_GLOB_DAT:
2270 case elfcpp::R_X86_64_JUMP_SLOT:
2271 case elfcpp::R_X86_64_DTPMOD64:
2272 case elfcpp::R_X86_64_DTPOFF64:
2273 case elfcpp::R_X86_64_TPOFF64:
2274 case elfcpp::R_X86_64_64:
2275 case elfcpp::R_X86_64_COPY:
2278 // glibc supports these reloc types, but they can overflow.
2279 case elfcpp::R_X86_64_PC32:
2280 case elfcpp::R_X86_64_PC32_BND:
2281 // A PC relative reference is OK against a local symbol or if
2282 // the symbol is defined locally.
2284 || (!gsym->is_from_dynobj()
2285 && !gsym->is_undefined()
2286 && !gsym->is_preemptible()))
2289 case elfcpp::R_X86_64_32:
2290 // R_X86_64_32 is OK for x32.
2291 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2293 if (this->issued_non_pic_error_)
2295 gold_assert(parameters->options().output_is_position_independent());
2297 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2298 "overflow at runtime; recompile with -fPIC"));
2304 case elfcpp::R_X86_64_32:
2305 r_name = "R_X86_64_32";
2307 case elfcpp::R_X86_64_PC32:
2308 r_name = "R_X86_64_PC32";
2310 case elfcpp::R_X86_64_PC32_BND:
2311 r_name = "R_X86_64_PC32_BND";
2317 object->error(_("requires dynamic %s reloc against '%s' "
2318 "which may overflow at runtime; recompile "
2320 r_name, gsym->name());
2322 this->issued_non_pic_error_ = true;
2326 // This prevents us from issuing more than one error per reloc
2327 // section. But we can still wind up issuing more than one
2328 // error per object file.
2329 if (this->issued_non_pic_error_)
2331 gold_assert(parameters->options().output_is_position_independent());
2332 object->error(_("requires unsupported dynamic reloc %u; "
2333 "recompile with -fPIC"),
2335 this->issued_non_pic_error_ = true;
2338 case elfcpp::R_X86_64_NONE:
2343 // Return whether we need to make a PLT entry for a relocation of the
2344 // given type against a STT_GNU_IFUNC symbol.
2348 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2349 Sized_relobj_file<size, false>* object,
2350 unsigned int r_type)
2352 int flags = Scan::get_reference_flags(r_type);
2353 if (flags & Symbol::TLS_REF)
2354 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2355 object->name().c_str(), r_type);
2359 // Scan a relocation for a local symbol.
2363 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2365 Target_x86_64<size>* target,
2366 Sized_relobj_file<size, false>* object,
2367 unsigned int data_shndx,
2368 Output_section* output_section,
2369 const elfcpp::Rela<size, false>& reloc,
2370 unsigned int r_type,
2371 const elfcpp::Sym<size, false>& lsym,
2377 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2378 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2379 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2381 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2382 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2387 case elfcpp::R_X86_64_NONE:
2388 case elfcpp::R_X86_64_GNU_VTINHERIT:
2389 case elfcpp::R_X86_64_GNU_VTENTRY:
2392 case elfcpp::R_X86_64_64:
2393 // If building a shared library (or a position-independent
2394 // executable), we need to create a dynamic relocation for this
2395 // location. The relocation applied at link time will apply the
2396 // link-time value, so we flag the location with an
2397 // R_X86_64_RELATIVE relocation so the dynamic loader can
2398 // relocate it easily.
2399 if (parameters->options().output_is_position_independent())
2401 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2402 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2403 rela_dyn->add_local_relative(object, r_sym,
2405 ? elfcpp::R_X86_64_RELATIVE64
2406 : elfcpp::R_X86_64_RELATIVE),
2407 output_section, data_shndx,
2408 reloc.get_r_offset(),
2409 reloc.get_r_addend(), is_ifunc);
2413 case elfcpp::R_X86_64_32:
2414 case elfcpp::R_X86_64_32S:
2415 case elfcpp::R_X86_64_16:
2416 case elfcpp::R_X86_64_8:
2417 // If building a shared library (or a position-independent
2418 // executable), we need to create a dynamic relocation for this
2419 // location. We can't use an R_X86_64_RELATIVE relocation
2420 // because that is always a 64-bit relocation.
2421 if (parameters->options().output_is_position_independent())
2423 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2424 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2426 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2427 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2428 rela_dyn->add_local_relative(object, r_sym,
2429 elfcpp::R_X86_64_RELATIVE,
2430 output_section, data_shndx,
2431 reloc.get_r_offset(),
2432 reloc.get_r_addend(), is_ifunc);
2436 this->check_non_pic(object, r_type, NULL);
2438 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2439 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2440 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2441 rela_dyn->add_local(object, r_sym, r_type, output_section,
2442 data_shndx, reloc.get_r_offset(),
2443 reloc.get_r_addend());
2446 gold_assert(lsym.get_st_value() == 0);
2447 unsigned int shndx = lsym.get_st_shndx();
2449 shndx = object->adjust_sym_shndx(r_sym, shndx,
2452 object->error(_("section symbol %u has bad shndx %u"),
2455 rela_dyn->add_local_section(object, shndx,
2456 r_type, output_section,
2457 data_shndx, reloc.get_r_offset(),
2458 reloc.get_r_addend());
2463 case elfcpp::R_X86_64_PC64:
2464 case elfcpp::R_X86_64_PC32:
2465 case elfcpp::R_X86_64_PC32_BND:
2466 case elfcpp::R_X86_64_PC16:
2467 case elfcpp::R_X86_64_PC8:
2470 case elfcpp::R_X86_64_PLT32:
2471 case elfcpp::R_X86_64_PLT32_BND:
2472 // Since we know this is a local symbol, we can handle this as a
2476 case elfcpp::R_X86_64_GOTPC32:
2477 case elfcpp::R_X86_64_GOTOFF64:
2478 case elfcpp::R_X86_64_GOTPC64:
2479 case elfcpp::R_X86_64_PLTOFF64:
2480 // We need a GOT section.
2481 target->got_section(symtab, layout);
2482 // For PLTOFF64, we'd normally want a PLT section, but since we
2483 // know this is a local symbol, no PLT is needed.
2486 case elfcpp::R_X86_64_GOT64:
2487 case elfcpp::R_X86_64_GOT32:
2488 case elfcpp::R_X86_64_GOTPCREL64:
2489 case elfcpp::R_X86_64_GOTPCREL:
2490 case elfcpp::R_X86_64_GOTPCRELX:
2491 case elfcpp::R_X86_64_REX_GOTPCRELX:
2492 case elfcpp::R_X86_64_GOTPLT64:
2494 // The symbol requires a GOT section.
2495 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2497 // If the relocation symbol isn't IFUNC,
2498 // and is local, then we will convert
2499 // mov foo@GOTPCREL(%rip), %reg
2500 // to lea foo(%rip), %reg.
2501 // in Relocate::relocate.
2502 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2503 || r_type == elfcpp::R_X86_64_GOTPCRELX
2504 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2505 && reloc.get_r_offset() >= 2
2508 section_size_type stype;
2509 const unsigned char* view = object->section_contents(data_shndx,
2511 if (view[reloc.get_r_offset() - 2] == 0x8b)
2516 // The symbol requires a GOT entry.
2517 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2519 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2520 // lets function pointers compare correctly with shared
2521 // libraries. Otherwise we would need an IRELATIVE reloc.
2524 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2526 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2529 // If we are generating a shared object, we need to add a
2530 // dynamic relocation for this symbol's GOT entry.
2531 if (parameters->options().output_is_position_independent())
2533 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2534 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2535 if (r_type != elfcpp::R_X86_64_GOT32)
2537 unsigned int got_offset =
2538 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2539 rela_dyn->add_local_relative(object, r_sym,
2540 elfcpp::R_X86_64_RELATIVE,
2541 got, got_offset, 0, is_ifunc);
2545 this->check_non_pic(object, r_type, NULL);
2547 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2548 rela_dyn->add_local(
2549 object, r_sym, r_type, got,
2550 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2554 // For GOTPLT64, we'd normally want a PLT section, but since
2555 // we know this is a local symbol, no PLT is needed.
2559 case elfcpp::R_X86_64_COPY:
2560 case elfcpp::R_X86_64_GLOB_DAT:
2561 case elfcpp::R_X86_64_JUMP_SLOT:
2562 case elfcpp::R_X86_64_RELATIVE:
2563 case elfcpp::R_X86_64_IRELATIVE:
2564 // These are outstanding tls relocs, which are unexpected when linking
2565 case elfcpp::R_X86_64_TPOFF64:
2566 case elfcpp::R_X86_64_DTPMOD64:
2567 case elfcpp::R_X86_64_TLSDESC:
2568 gold_error(_("%s: unexpected reloc %u in object file"),
2569 object->name().c_str(), r_type);
2572 // These are initial tls relocs, which are expected when linking
2573 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2574 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2575 case elfcpp::R_X86_64_TLSDESC_CALL:
2576 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2577 case elfcpp::R_X86_64_DTPOFF32:
2578 case elfcpp::R_X86_64_DTPOFF64:
2579 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2580 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2582 bool output_is_shared = parameters->options().shared();
2583 const tls::Tls_optimization optimized_type
2584 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2588 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2589 if (optimized_type == tls::TLSOPT_NONE)
2591 // Create a pair of GOT entries for the module index and
2592 // dtv-relative offset.
2593 Output_data_got<64, false>* got
2594 = target->got_section(symtab, layout);
2595 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2596 unsigned int shndx = lsym.get_st_shndx();
2598 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2600 object->error(_("local symbol %u has bad shndx %u"),
2603 got->add_local_pair_with_rel(object, r_sym,
2606 target->rela_dyn_section(layout),
2607 elfcpp::R_X86_64_DTPMOD64);
2609 else if (optimized_type != tls::TLSOPT_TO_LE)
2610 unsupported_reloc_local(object, r_type);
2613 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2614 target->define_tls_base_symbol(symtab, layout);
2615 if (optimized_type == tls::TLSOPT_NONE)
2617 // Create reserved PLT and GOT entries for the resolver.
2618 target->reserve_tlsdesc_entries(symtab, layout);
2620 // Generate a double GOT entry with an
2621 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2622 // is resolved lazily, so the GOT entry needs to be in
2623 // an area in .got.plt, not .got. Call got_section to
2624 // make sure the section has been created.
2625 target->got_section(symtab, layout);
2626 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2627 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2628 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2630 unsigned int got_offset = got->add_constant(0);
2631 got->add_constant(0);
2632 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2634 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2635 // We store the arguments we need in a vector, and
2636 // use the index into the vector as the parameter
2637 // to pass to the target specific routines.
2638 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2639 void* arg = reinterpret_cast<void*>(intarg);
2640 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2641 got, got_offset, 0);
2644 else if (optimized_type != tls::TLSOPT_TO_LE)
2645 unsupported_reloc_local(object, r_type);
2648 case elfcpp::R_X86_64_TLSDESC_CALL:
2651 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2652 if (optimized_type == tls::TLSOPT_NONE)
2654 // Create a GOT entry for the module index.
2655 target->got_mod_index_entry(symtab, layout, object);
2657 else if (optimized_type != tls::TLSOPT_TO_LE)
2658 unsupported_reloc_local(object, r_type);
2661 case elfcpp::R_X86_64_DTPOFF32:
2662 case elfcpp::R_X86_64_DTPOFF64:
2665 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2666 layout->set_has_static_tls();
2667 if (optimized_type == tls::TLSOPT_NONE)
2669 // Create a GOT entry for the tp-relative offset.
2670 Output_data_got<64, false>* got
2671 = target->got_section(symtab, layout);
2672 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2673 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2674 target->rela_dyn_section(layout),
2675 elfcpp::R_X86_64_TPOFF64);
2677 else if (optimized_type != tls::TLSOPT_TO_LE)
2678 unsupported_reloc_local(object, r_type);
2681 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2682 layout->set_has_static_tls();
2683 if (output_is_shared)
2684 unsupported_reloc_local(object, r_type);
2693 case elfcpp::R_X86_64_SIZE32:
2694 case elfcpp::R_X86_64_SIZE64:
2696 gold_error(_("%s: unsupported reloc %u against local symbol"),
2697 object->name().c_str(), r_type);
2703 // Report an unsupported relocation against a global symbol.
2707 Target_x86_64<size>::Scan::unsupported_reloc_global(
2708 Sized_relobj_file<size, false>* object,
2709 unsigned int r_type,
2712 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2713 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2716 // Returns true if this relocation type could be that of a function pointer.
2719 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2723 case elfcpp::R_X86_64_64:
2724 case elfcpp::R_X86_64_32:
2725 case elfcpp::R_X86_64_32S:
2726 case elfcpp::R_X86_64_16:
2727 case elfcpp::R_X86_64_8:
2728 case elfcpp::R_X86_64_GOT64:
2729 case elfcpp::R_X86_64_GOT32:
2730 case elfcpp::R_X86_64_GOTPCREL64:
2731 case elfcpp::R_X86_64_GOTPCREL:
2732 case elfcpp::R_X86_64_GOTPCRELX:
2733 case elfcpp::R_X86_64_REX_GOTPCRELX:
2734 case elfcpp::R_X86_64_GOTPLT64:
2742 // For safe ICF, scan a relocation for a local symbol to check if it
2743 // corresponds to a function pointer being taken. In that case mark
2744 // the function whose pointer was taken as not foldable.
2748 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2751 Target_x86_64<size>* ,
2752 Sized_relobj_file<size, false>* ,
2755 const elfcpp::Rela<size, false>& ,
2756 unsigned int r_type,
2757 const elfcpp::Sym<size, false>&)
2759 // When building a shared library, do not fold any local symbols as it is
2760 // not possible to distinguish pointer taken versus a call by looking at
2761 // the relocation types.
2762 return (parameters->options().shared()
2763 || possible_function_pointer_reloc(r_type));
2766 // For safe ICF, scan a relocation for a global symbol to check if it
2767 // corresponds to a function pointer being taken. In that case mark
2768 // the function whose pointer was taken as not foldable.
2772 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2775 Target_x86_64<size>* ,
2776 Sized_relobj_file<size, false>* ,
2779 const elfcpp::Rela<size, false>& ,
2780 unsigned int r_type,
2783 // When building a shared library, do not fold symbols whose visibility
2784 // is hidden, internal or protected.
2785 return ((parameters->options().shared()
2786 && (gsym->visibility() == elfcpp::STV_INTERNAL
2787 || gsym->visibility() == elfcpp::STV_PROTECTED
2788 || gsym->visibility() == elfcpp::STV_HIDDEN))
2789 || possible_function_pointer_reloc(r_type));
2792 // Scan a relocation for a global symbol.
2796 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2798 Target_x86_64<size>* target,
2799 Sized_relobj_file<size, false>* object,
2800 unsigned int data_shndx,
2801 Output_section* output_section,
2802 const elfcpp::Rela<size, false>& reloc,
2803 unsigned int r_type,
2806 // A STT_GNU_IFUNC symbol may require a PLT entry.
2807 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2808 && this->reloc_needs_plt_for_ifunc(object, r_type))
2809 target->make_plt_entry(symtab, layout, gsym);
2813 case elfcpp::R_X86_64_NONE:
2814 case elfcpp::R_X86_64_GNU_VTINHERIT:
2815 case elfcpp::R_X86_64_GNU_VTENTRY:
2818 case elfcpp::R_X86_64_64:
2819 case elfcpp::R_X86_64_32:
2820 case elfcpp::R_X86_64_32S:
2821 case elfcpp::R_X86_64_16:
2822 case elfcpp::R_X86_64_8:
2824 // Make a PLT entry if necessary.
2825 if (gsym->needs_plt_entry())
2827 target->make_plt_entry(symtab, layout, gsym);
2828 // Since this is not a PC-relative relocation, we may be
2829 // taking the address of a function. In that case we need to
2830 // set the entry in the dynamic symbol table to the address of
2832 if (gsym->is_from_dynobj() && !parameters->options().shared())
2833 gsym->set_needs_dynsym_value();
2835 // Make a dynamic relocation if necessary.
2836 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2838 if (!parameters->options().output_is_position_independent()
2839 && gsym->may_need_copy_reloc())
2841 target->copy_reloc(symtab, layout, object,
2842 data_shndx, output_section, gsym, reloc);
2844 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2845 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2846 && gsym->type() == elfcpp::STT_GNU_IFUNC
2847 && gsym->can_use_relative_reloc(false)
2848 && !gsym->is_from_dynobj()
2849 && !gsym->is_undefined()
2850 && !gsym->is_preemptible())
2852 // Use an IRELATIVE reloc for a locally defined
2853 // STT_GNU_IFUNC symbol. This makes a function
2854 // address in a PIE executable match the address in a
2855 // shared library that it links against.
2856 Reloc_section* rela_dyn =
2857 target->rela_irelative_section(layout);
2858 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2859 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2860 output_section, object,
2862 reloc.get_r_offset(),
2863 reloc.get_r_addend());
2865 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2866 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2867 && gsym->can_use_relative_reloc(false))
2869 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2870 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2871 output_section, object,
2873 reloc.get_r_offset(),
2874 reloc.get_r_addend(), false);
2878 this->check_non_pic(object, r_type, gsym);
2879 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2880 rela_dyn->add_global(gsym, r_type, output_section, object,
2881 data_shndx, reloc.get_r_offset(),
2882 reloc.get_r_addend());
2888 case elfcpp::R_X86_64_PC64:
2889 case elfcpp::R_X86_64_PC32:
2890 case elfcpp::R_X86_64_PC32_BND:
2891 case elfcpp::R_X86_64_PC16:
2892 case elfcpp::R_X86_64_PC8:
2894 // Make a PLT entry if necessary.
2895 if (gsym->needs_plt_entry())
2896 target->make_plt_entry(symtab, layout, gsym);
2897 // Make a dynamic relocation if necessary.
2898 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2900 if (parameters->options().output_is_executable()
2901 && gsym->may_need_copy_reloc())
2903 target->copy_reloc(symtab, layout, object,
2904 data_shndx, output_section, gsym, reloc);
2908 this->check_non_pic(object, r_type, gsym);
2909 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2910 rela_dyn->add_global(gsym, r_type, output_section, object,
2911 data_shndx, reloc.get_r_offset(),
2912 reloc.get_r_addend());
2918 case elfcpp::R_X86_64_GOT64:
2919 case elfcpp::R_X86_64_GOT32:
2920 case elfcpp::R_X86_64_GOTPCREL64:
2921 case elfcpp::R_X86_64_GOTPCREL:
2922 case elfcpp::R_X86_64_GOTPCRELX:
2923 case elfcpp::R_X86_64_REX_GOTPCRELX:
2924 case elfcpp::R_X86_64_GOTPLT64:
2926 // The symbol requires a GOT entry.
2927 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2929 // If we convert this from
2930 // mov foo@GOTPCREL(%rip), %reg
2931 // to lea foo(%rip), %reg.
2932 // in Relocate::relocate, then there is nothing to do here.
2933 if ((r_type == elfcpp::R_X86_64_GOTPCREL
2934 || r_type == elfcpp::R_X86_64_GOTPCRELX
2935 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
2936 && reloc.get_r_offset() >= 2
2937 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
2939 section_size_type stype;
2940 const unsigned char* view = object->section_contents(data_shndx,
2942 if (view[reloc.get_r_offset() - 2] == 0x8b)
2946 if (gsym->final_value_is_known())
2948 // For a STT_GNU_IFUNC symbol we want the PLT address.
2949 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2950 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2952 got->add_global(gsym, GOT_TYPE_STANDARD);
2956 // If this symbol is not fully resolved, we need to add a
2957 // dynamic relocation for it.
2958 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2960 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2962 // 1) The symbol may be defined in some other module.
2964 // 2) We are building a shared library and this is a
2965 // protected symbol; using GLOB_DAT means that the dynamic
2966 // linker can use the address of the PLT in the main
2967 // executable when appropriate so that function address
2968 // comparisons work.
2970 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2971 // code, again so that function address comparisons work.
2972 if (gsym->is_from_dynobj()
2973 || gsym->is_undefined()
2974 || gsym->is_preemptible()
2975 || (gsym->visibility() == elfcpp::STV_PROTECTED
2976 && parameters->options().shared())
2977 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2978 && parameters->options().output_is_position_independent()))
2979 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2980 elfcpp::R_X86_64_GLOB_DAT);
2983 // For a STT_GNU_IFUNC symbol we want to write the PLT
2984 // offset into the GOT, so that function pointer
2985 // comparisons work correctly.
2987 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2988 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2991 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2992 // Tell the dynamic linker to use the PLT address
2993 // when resolving relocations.
2994 if (gsym->is_from_dynobj()
2995 && !parameters->options().shared())
2996 gsym->set_needs_dynsym_value();
3000 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3001 rela_dyn->add_global_relative(gsym,
3002 elfcpp::R_X86_64_RELATIVE,
3003 got, got_off, 0, false);
3010 case elfcpp::R_X86_64_PLT32:
3011 case elfcpp::R_X86_64_PLT32_BND:
3012 // If the symbol is fully resolved, this is just a PC32 reloc.
3013 // Otherwise we need a PLT entry.
3014 if (gsym->final_value_is_known())
3016 // If building a shared library, we can also skip the PLT entry
3017 // if the symbol is defined in the output file and is protected
3019 if (gsym->is_defined()
3020 && !gsym->is_from_dynobj()
3021 && !gsym->is_preemptible())
3023 target->make_plt_entry(symtab, layout, gsym);
3026 case elfcpp::R_X86_64_GOTPC32:
3027 case elfcpp::R_X86_64_GOTOFF64:
3028 case elfcpp::R_X86_64_GOTPC64:
3029 case elfcpp::R_X86_64_PLTOFF64:
3030 // We need a GOT section.
3031 target->got_section(symtab, layout);
3032 // For PLTOFF64, we also need a PLT entry (but only if the
3033 // symbol is not fully resolved).
3034 if (r_type == elfcpp::R_X86_64_PLTOFF64
3035 && !gsym->final_value_is_known())
3036 target->make_plt_entry(symtab, layout, gsym);
3039 case elfcpp::R_X86_64_COPY:
3040 case elfcpp::R_X86_64_GLOB_DAT:
3041 case elfcpp::R_X86_64_JUMP_SLOT:
3042 case elfcpp::R_X86_64_RELATIVE:
3043 case elfcpp::R_X86_64_IRELATIVE:
3044 // These are outstanding tls relocs, which are unexpected when linking
3045 case elfcpp::R_X86_64_TPOFF64:
3046 case elfcpp::R_X86_64_DTPMOD64:
3047 case elfcpp::R_X86_64_TLSDESC:
3048 gold_error(_("%s: unexpected reloc %u in object file"),
3049 object->name().c_str(), r_type);
3052 // These are initial tls relocs, which are expected for global()
3053 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3054 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3055 case elfcpp::R_X86_64_TLSDESC_CALL:
3056 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3057 case elfcpp::R_X86_64_DTPOFF32:
3058 case elfcpp::R_X86_64_DTPOFF64:
3059 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3060 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3062 // For the Initial-Exec model, we can treat undef symbols as final
3063 // when building an executable.
3064 const bool is_final = (gsym->final_value_is_known() ||
3065 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3066 gsym->is_undefined() &&
3067 parameters->options().output_is_executable()));
3068 const tls::Tls_optimization optimized_type
3069 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3072 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3073 if (optimized_type == tls::TLSOPT_NONE)
3075 // Create a pair of GOT entries for the module index and
3076 // dtv-relative offset.
3077 Output_data_got<64, false>* got
3078 = target->got_section(symtab, layout);
3079 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3080 target->rela_dyn_section(layout),
3081 elfcpp::R_X86_64_DTPMOD64,
3082 elfcpp::R_X86_64_DTPOFF64);
3084 else if (optimized_type == tls::TLSOPT_TO_IE)
3086 // Create a GOT entry for the tp-relative offset.
3087 Output_data_got<64, false>* got
3088 = target->got_section(symtab, layout);
3089 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3090 target->rela_dyn_section(layout),
3091 elfcpp::R_X86_64_TPOFF64);
3093 else if (optimized_type != tls::TLSOPT_TO_LE)
3094 unsupported_reloc_global(object, r_type, gsym);
3097 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3098 target->define_tls_base_symbol(symtab, layout);
3099 if (optimized_type == tls::TLSOPT_NONE)
3101 // Create reserved PLT and GOT entries for the resolver.
3102 target->reserve_tlsdesc_entries(symtab, layout);
3104 // Create a double GOT entry with an R_X86_64_TLSDESC
3105 // reloc. The R_X86_64_TLSDESC reloc is resolved
3106 // lazily, so the GOT entry needs to be in an area in
3107 // .got.plt, not .got. Call got_section to make sure
3108 // the section has been created.
3109 target->got_section(symtab, layout);
3110 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3111 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3112 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3113 elfcpp::R_X86_64_TLSDESC, 0);
3115 else if (optimized_type == tls::TLSOPT_TO_IE)
3117 // Create a GOT entry for the tp-relative offset.
3118 Output_data_got<64, false>* got
3119 = target->got_section(symtab, layout);
3120 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3121 target->rela_dyn_section(layout),
3122 elfcpp::R_X86_64_TPOFF64);
3124 else if (optimized_type != tls::TLSOPT_TO_LE)
3125 unsupported_reloc_global(object, r_type, gsym);
3128 case elfcpp::R_X86_64_TLSDESC_CALL:
3131 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3132 if (optimized_type == tls::TLSOPT_NONE)
3134 // Create a GOT entry for the module index.
3135 target->got_mod_index_entry(symtab, layout, object);
3137 else if (optimized_type != tls::TLSOPT_TO_LE)
3138 unsupported_reloc_global(object, r_type, gsym);
3141 case elfcpp::R_X86_64_DTPOFF32:
3142 case elfcpp::R_X86_64_DTPOFF64:
3145 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3146 layout->set_has_static_tls();
3147 if (optimized_type == tls::TLSOPT_NONE)
3149 // Create a GOT entry for the tp-relative offset.
3150 Output_data_got<64, false>* got
3151 = target->got_section(symtab, layout);
3152 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3153 target->rela_dyn_section(layout),
3154 elfcpp::R_X86_64_TPOFF64);
3156 else if (optimized_type != tls::TLSOPT_TO_LE)
3157 unsupported_reloc_global(object, r_type, gsym);
3160 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3161 layout->set_has_static_tls();
3162 if (parameters->options().shared())
3163 unsupported_reloc_global(object, r_type, gsym);
3172 case elfcpp::R_X86_64_SIZE32:
3173 case elfcpp::R_X86_64_SIZE64:
3175 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3176 object->name().c_str(), r_type,
3177 gsym->demangled_name().c_str());
3184 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3186 Sized_relobj_file<size, false>* object,
3187 unsigned int data_shndx,
3188 unsigned int sh_type,
3189 const unsigned char* prelocs,
3191 Output_section* output_section,
3192 bool needs_special_offset_handling,
3193 size_t local_symbol_count,
3194 const unsigned char* plocal_symbols)
3197 if (sh_type == elfcpp::SHT_REL)
3202 gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3203 typename Target_x86_64<size>::Scan,
3204 typename Target_x86_64<size>::Relocatable_size_for_reloc>(
3213 needs_special_offset_handling,
3218 // Scan relocations for a section.
3222 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3224 Sized_relobj_file<size, false>* object,
3225 unsigned int data_shndx,
3226 unsigned int sh_type,
3227 const unsigned char* prelocs,
3229 Output_section* output_section,
3230 bool needs_special_offset_handling,
3231 size_t local_symbol_count,
3232 const unsigned char* plocal_symbols)
3234 if (sh_type == elfcpp::SHT_REL)
3236 gold_error(_("%s: unsupported REL reloc section"),
3237 object->name().c_str());
3241 gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3242 typename Target_x86_64<size>::Scan>(
3251 needs_special_offset_handling,
3256 // Finalize the sections.
3260 Target_x86_64<size>::do_finalize_sections(
3262 const Input_objects*,
3263 Symbol_table* symtab)
3265 const Reloc_section* rel_plt = (this->plt_ == NULL
3267 : this->plt_->rela_plt());
3268 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3269 this->rela_dyn_, true, false);
3271 // Fill in some more dynamic tags.
3272 Output_data_dynamic* const odyn = layout->dynamic_data();
3275 if (this->plt_ != NULL
3276 && this->plt_->output_section() != NULL
3277 && this->plt_->has_tlsdesc_entry())
3279 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3280 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3281 this->got_->finalize_data_size();
3282 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3283 this->plt_, plt_offset);
3284 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3285 this->got_, got_offset);
3289 // Emit any relocs we saved in an attempt to avoid generating COPY
3291 if (this->copy_relocs_.any_saved_relocs())
3292 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3294 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3295 // the .got.plt section.
3296 Symbol* sym = this->global_offset_table_;
3299 uint64_t data_size = this->got_plt_->current_data_size();
3300 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3303 if (parameters->doing_static_link()
3304 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3306 // If linking statically, make sure that the __rela_iplt symbols
3307 // were defined if necessary, even if we didn't create a PLT.
3308 static const Define_symbol_in_segment syms[] =
3311 "__rela_iplt_start", // name
3312 elfcpp::PT_LOAD, // segment_type
3313 elfcpp::PF_W, // segment_flags_set
3314 elfcpp::PF(0), // segment_flags_clear
3317 elfcpp::STT_NOTYPE, // type
3318 elfcpp::STB_GLOBAL, // binding
3319 elfcpp::STV_HIDDEN, // visibility
3321 Symbol::SEGMENT_START, // offset_from_base
3325 "__rela_iplt_end", // name
3326 elfcpp::PT_LOAD, // segment_type
3327 elfcpp::PF_W, // segment_flags_set
3328 elfcpp::PF(0), // segment_flags_clear
3331 elfcpp::STT_NOTYPE, // type
3332 elfcpp::STB_GLOBAL, // binding
3333 elfcpp::STV_HIDDEN, // visibility
3335 Symbol::SEGMENT_START, // offset_from_base
3340 symtab->define_symbols(layout, 2, syms,
3341 layout->script_options()->saw_sections_clause());
3345 // Perform a relocation.
3349 Target_x86_64<size>::Relocate::relocate(
3350 const Relocate_info<size, false>* relinfo,
3351 Target_x86_64<size>* target,
3354 const elfcpp::Rela<size, false>& rela,
3355 unsigned int r_type,
3356 const Sized_symbol<size>* gsym,
3357 const Symbol_value<size>* psymval,
3358 unsigned char* view,
3359 typename elfcpp::Elf_types<size>::Elf_Addr address,
3360 section_size_type view_size)
3362 if (this->skip_call_tls_get_addr_)
3364 if ((r_type != elfcpp::R_X86_64_PLT32
3365 && r_type != elfcpp::R_X86_64_PLT32_BND
3366 && r_type != elfcpp::R_X86_64_PC32_BND
3367 && r_type != elfcpp::R_X86_64_PC32)
3369 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3371 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3372 _("missing expected TLS relocation"));
3376 this->skip_call_tls_get_addr_ = false;
3384 const Sized_relobj_file<size, false>* object = relinfo->object;
3386 // Pick the value to use for symbols defined in the PLT.
3387 Symbol_value<size> symval;
3389 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3391 symval.set_output_value(target->plt_address_for_global(gsym));
3394 else if (gsym == NULL && psymval->is_ifunc_symbol())
3396 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3397 if (object->local_has_plt_offset(r_sym))
3399 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3404 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3406 // Get the GOT offset if needed.
3407 // The GOT pointer points to the end of the GOT section.
3408 // We need to subtract the size of the GOT section to get
3409 // the actual offset to use in the relocation.
3410 bool have_got_offset = false;
3411 // Since the actual offset is always negative, we use signed int to
3412 // support 64-bit GOT relocations.
3416 case elfcpp::R_X86_64_GOT32:
3417 case elfcpp::R_X86_64_GOT64:
3418 case elfcpp::R_X86_64_GOTPLT64:
3419 case elfcpp::R_X86_64_GOTPCREL64:
3422 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3423 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3427 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3428 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3429 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3430 - target->got_size());
3432 have_got_offset = true;
3441 case elfcpp::R_X86_64_NONE:
3442 case elfcpp::R_X86_64_GNU_VTINHERIT:
3443 case elfcpp::R_X86_64_GNU_VTENTRY:
3446 case elfcpp::R_X86_64_64:
3447 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
3450 case elfcpp::R_X86_64_PC64:
3451 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
3455 case elfcpp::R_X86_64_32:
3456 // FIXME: we need to verify that value + addend fits into 32 bits:
3457 // uint64_t x = value + addend;
3458 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3459 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3460 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3463 case elfcpp::R_X86_64_32S:
3464 // FIXME: we need to verify that value + addend fits into 32 bits:
3465 // int64_t x = value + addend; // note this quantity is signed!
3466 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3467 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3470 case elfcpp::R_X86_64_PC32:
3471 case elfcpp::R_X86_64_PC32_BND:
3472 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3476 case elfcpp::R_X86_64_16:
3477 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
3480 case elfcpp::R_X86_64_PC16:
3481 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3485 case elfcpp::R_X86_64_8:
3486 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
3489 case elfcpp::R_X86_64_PC8:
3490 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3494 case elfcpp::R_X86_64_PLT32:
3495 case elfcpp::R_X86_64_PLT32_BND:
3496 gold_assert(gsym == NULL
3497 || gsym->has_plt_offset()
3498 || gsym->final_value_is_known()
3499 || (gsym->is_defined()
3500 && !gsym->is_from_dynobj()
3501 && !gsym->is_preemptible()));
3502 // Note: while this code looks the same as for R_X86_64_PC32, it
3503 // behaves differently because psymval was set to point to
3504 // the PLT entry, rather than the symbol, in Scan::global().
3505 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3509 case elfcpp::R_X86_64_PLTOFF64:
3512 gold_assert(gsym->has_plt_offset()
3513 || gsym->final_value_is_known());
3514 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3515 // This is the address of GLOBAL_OFFSET_TABLE.
3516 got_address = target->got_plt_section()->address();
3517 Relocate_functions<size, false>::rela64(view, object, psymval,
3518 addend - got_address);
3522 case elfcpp::R_X86_64_GOT32:
3523 gold_assert(have_got_offset);
3524 Relocate_functions<size, false>::rela32(view, got_offset, addend);
3527 case elfcpp::R_X86_64_GOTPC32:
3530 typename elfcpp::Elf_types<size>::Elf_Addr value;
3531 value = target->got_plt_section()->address();
3532 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3536 case elfcpp::R_X86_64_GOT64:
3537 case elfcpp::R_X86_64_GOTPLT64:
3538 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3540 gold_assert(have_got_offset);
3541 Relocate_functions<size, false>::rela64(view, got_offset, addend);
3544 case elfcpp::R_X86_64_GOTPC64:
3547 typename elfcpp::Elf_types<size>::Elf_Addr value;
3548 value = target->got_plt_section()->address();
3549 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3553 case elfcpp::R_X86_64_GOTOFF64:
3555 typename elfcpp::Elf_types<size>::Elf_Addr value;
3556 value = (psymval->value(object, 0)
3557 - target->got_plt_section()->address());
3558 Relocate_functions<size, false>::rela64(view, value, addend);
3562 case elfcpp::R_X86_64_GOTPCREL:
3563 case elfcpp::R_X86_64_GOTPCRELX:
3564 case elfcpp::R_X86_64_REX_GOTPCRELX:
3567 // mov foo@GOTPCREL(%rip), %reg
3568 // to lea foo(%rip), %reg.
3570 if (rela.get_r_offset() >= 2
3572 && ((gsym == NULL && !psymval->is_ifunc_symbol())
3574 && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
3577 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3584 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3585 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3589 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3590 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3591 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3592 - target->got_size());
3594 typename elfcpp::Elf_types<size>::Elf_Addr value;
3595 value = target->got_plt_section()->address() + got_offset;
3596 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3601 case elfcpp::R_X86_64_GOTPCREL64:
3603 gold_assert(have_got_offset);
3604 typename elfcpp::Elf_types<size>::Elf_Addr value;
3605 value = target->got_plt_section()->address() + got_offset;
3606 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3610 case elfcpp::R_X86_64_COPY:
3611 case elfcpp::R_X86_64_GLOB_DAT:
3612 case elfcpp::R_X86_64_JUMP_SLOT:
3613 case elfcpp::R_X86_64_RELATIVE:
3614 case elfcpp::R_X86_64_IRELATIVE:
3615 // These are outstanding tls relocs, which are unexpected when linking
3616 case elfcpp::R_X86_64_TPOFF64:
3617 case elfcpp::R_X86_64_DTPMOD64:
3618 case elfcpp::R_X86_64_TLSDESC:
3619 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3620 _("unexpected reloc %u in object file"),
3624 // These are initial tls relocs, which are expected when linking
3625 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3626 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3627 case elfcpp::R_X86_64_TLSDESC_CALL:
3628 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3629 case elfcpp::R_X86_64_DTPOFF32:
3630 case elfcpp::R_X86_64_DTPOFF64:
3631 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3632 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3633 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3634 view, address, view_size);
3637 case elfcpp::R_X86_64_SIZE32:
3638 case elfcpp::R_X86_64_SIZE64:
3640 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3641 _("unsupported reloc %u"),
3649 // Perform a TLS relocation.
3653 Target_x86_64<size>::Relocate::relocate_tls(
3654 const Relocate_info<size, false>* relinfo,
3655 Target_x86_64<size>* target,
3657 const elfcpp::Rela<size, false>& rela,
3658 unsigned int r_type,
3659 const Sized_symbol<size>* gsym,
3660 const Symbol_value<size>* psymval,
3661 unsigned char* view,
3662 typename elfcpp::Elf_types<size>::Elf_Addr address,
3663 section_size_type view_size)
3665 Output_segment* tls_segment = relinfo->layout->tls_segment();
3667 const Sized_relobj_file<size, false>* object = relinfo->object;
3668 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3669 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3670 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3672 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3674 const bool is_final = (gsym == NULL
3675 ? !parameters->options().shared()
3676 : gsym->final_value_is_known());
3677 tls::Tls_optimization optimized_type
3678 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3681 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3682 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3684 // If this code sequence is used in a non-executable section,
3685 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3686 // on the assumption that it's being used by itself in a debug
3687 // section. Therefore, in the unlikely event that the code
3688 // sequence appears in a non-executable section, we simply
3689 // leave it unoptimized.
3690 optimized_type = tls::TLSOPT_NONE;
3692 if (optimized_type == tls::TLSOPT_TO_LE)
3694 if (tls_segment == NULL)
3696 gold_assert(parameters->errors()->error_count() > 0
3697 || issue_undefined_symbol_error(gsym));
3700 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3701 rela, r_type, value, view,
3707 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3708 ? GOT_TYPE_TLS_OFFSET
3709 : GOT_TYPE_TLS_PAIR);
3710 unsigned int got_offset;
3713 gold_assert(gsym->has_got_offset(got_type));
3714 got_offset = gsym->got_offset(got_type) - target->got_size();
3718 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3719 gold_assert(object->local_has_got_offset(r_sym, got_type));
3720 got_offset = (object->local_got_offset(r_sym, got_type)
3721 - target->got_size());
3723 if (optimized_type == tls::TLSOPT_TO_IE)
3725 value = target->got_plt_section()->address() + got_offset;
3726 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3727 value, view, address, view_size);
3730 else if (optimized_type == tls::TLSOPT_NONE)
3732 // Relocate the field with the offset of the pair of GOT
3734 value = target->got_plt_section()->address() + got_offset;
3735 Relocate_functions<size, false>::pcrela32(view, value, addend,
3740 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3741 _("unsupported reloc %u"), r_type);
3744 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3745 case elfcpp::R_X86_64_TLSDESC_CALL:
3746 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3748 // See above comment for R_X86_64_TLSGD.
3749 optimized_type = tls::TLSOPT_NONE;
3751 if (optimized_type == tls::TLSOPT_TO_LE)
3753 if (tls_segment == NULL)
3755 gold_assert(parameters->errors()->error_count() > 0
3756 || issue_undefined_symbol_error(gsym));
3759 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3760 rela, r_type, value, view,
3766 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3767 ? GOT_TYPE_TLS_OFFSET
3768 : GOT_TYPE_TLS_DESC);
3769 unsigned int got_offset = 0;
3770 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3771 && optimized_type == tls::TLSOPT_NONE)
3773 // We created GOT entries in the .got.tlsdesc portion of
3774 // the .got.plt section, but the offset stored in the
3775 // symbol is the offset within .got.tlsdesc.
3776 got_offset = (target->got_size()
3777 + target->got_plt_section()->data_size());
3781 gold_assert(gsym->has_got_offset(got_type));
3782 got_offset += gsym->got_offset(got_type) - target->got_size();
3786 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3787 gold_assert(object->local_has_got_offset(r_sym, got_type));
3788 got_offset += (object->local_got_offset(r_sym, got_type)
3789 - target->got_size());
3791 if (optimized_type == tls::TLSOPT_TO_IE)
3793 if (tls_segment == NULL)
3795 gold_assert(parameters->errors()->error_count() > 0
3796 || issue_undefined_symbol_error(gsym));
3799 value = target->got_plt_section()->address() + got_offset;
3800 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3801 rela, r_type, value, view, address,
3805 else if (optimized_type == tls::TLSOPT_NONE)
3807 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3809 // Relocate the field with the offset of the pair of GOT
3811 value = target->got_plt_section()->address() + got_offset;
3812 Relocate_functions<size, false>::pcrela32(view, value, addend,
3818 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3819 _("unsupported reloc %u"), r_type);
3822 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3823 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3825 // See above comment for R_X86_64_TLSGD.
3826 optimized_type = tls::TLSOPT_NONE;
3828 if (optimized_type == tls::TLSOPT_TO_LE)
3830 if (tls_segment == NULL)
3832 gold_assert(parameters->errors()->error_count() > 0
3833 || issue_undefined_symbol_error(gsym));
3836 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3837 value, view, view_size);
3840 else if (optimized_type == tls::TLSOPT_NONE)
3842 // Relocate the field with the offset of the GOT entry for
3843 // the module index.
3844 unsigned int got_offset;
3845 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3846 - target->got_size());
3847 value = target->got_plt_section()->address() + got_offset;
3848 Relocate_functions<size, false>::pcrela32(view, value, addend,
3852 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3853 _("unsupported reloc %u"), r_type);
3856 case elfcpp::R_X86_64_DTPOFF32:
3857 // This relocation type is used in debugging information.
3858 // In that case we need to not optimize the value. If the
3859 // section is not executable, then we assume we should not
3860 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3861 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3863 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3865 if (tls_segment == NULL)
3867 gold_assert(parameters->errors()->error_count() > 0
3868 || issue_undefined_symbol_error(gsym));
3871 value -= tls_segment->memsz();
3873 Relocate_functions<size, false>::rela32(view, value, addend);
3876 case elfcpp::R_X86_64_DTPOFF64:
3877 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3878 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3880 if (tls_segment == NULL)
3882 gold_assert(parameters->errors()->error_count() > 0
3883 || issue_undefined_symbol_error(gsym));
3886 value -= tls_segment->memsz();
3888 Relocate_functions<size, false>::rela64(view, value, addend);
3891 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3893 && gsym->is_undefined()
3894 && parameters->options().output_is_executable())
3896 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3898 r_type, value, view,
3902 else if (optimized_type == tls::TLSOPT_TO_LE)
3904 if (tls_segment == NULL)
3906 gold_assert(parameters->errors()->error_count() > 0
3907 || issue_undefined_symbol_error(gsym));
3910 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3912 r_type, value, view,
3916 else if (optimized_type == tls::TLSOPT_NONE)
3918 // Relocate the field with the offset of the GOT entry for
3919 // the tp-relative offset of the symbol.
3920 unsigned int got_offset;
3923 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3924 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3925 - target->got_size());
3929 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3930 gold_assert(object->local_has_got_offset(r_sym,
3931 GOT_TYPE_TLS_OFFSET));
3932 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3933 - target->got_size());
3935 value = target->got_plt_section()->address() + got_offset;
3936 Relocate_functions<size, false>::pcrela32(view, value, addend,
3940 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3941 _("unsupported reloc type %u"),
3945 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3946 if (tls_segment == NULL)
3948 gold_assert(parameters->errors()->error_count() > 0
3949 || issue_undefined_symbol_error(gsym));
3952 value -= tls_segment->memsz();
3953 Relocate_functions<size, false>::rela32(view, value, addend);
3958 // Do a relocation in which we convert a TLS General-Dynamic to an
3963 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3964 const Relocate_info<size, false>* relinfo,
3967 const elfcpp::Rela<size, false>& rela,
3969 typename elfcpp::Elf_types<size>::Elf_Addr value,
3970 unsigned char* view,
3971 typename elfcpp::Elf_types<size>::Elf_Addr address,
3972 section_size_type view_size)
3975 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3976 // .word 0x6666; rex64; call __tls_get_addr
3977 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3979 // leaq foo@tlsgd(%rip),%rdi;
3980 // .word 0x6666; rex64; call __tls_get_addr
3981 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3983 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3984 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3985 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3989 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3991 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3992 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3993 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3998 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4000 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4001 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4002 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4006 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4007 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4010 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4012 this->skip_call_tls_get_addr_ = true;
4015 // Do a relocation in which we convert a TLS General-Dynamic to a
4020 Target_x86_64<size>::Relocate::tls_gd_to_le(
4021 const Relocate_info<size, false>* relinfo,
4023 Output_segment* tls_segment,
4024 const elfcpp::Rela<size, false>& rela,
4026 typename elfcpp::Elf_types<size>::Elf_Addr value,
4027 unsigned char* view,
4028 section_size_type view_size)
4031 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4032 // .word 0x6666; rex64; call __tls_get_addr
4033 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4035 // leaq foo@tlsgd(%rip),%rdi;
4036 // .word 0x6666; rex64; call __tls_get_addr
4037 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4039 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4040 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4041 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
4045 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4047 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4048 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4049 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4054 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4056 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4057 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4059 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4063 value -= tls_segment->memsz();
4064 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4066 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4068 this->skip_call_tls_get_addr_ = true;
4071 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4075 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4076 const Relocate_info<size, false>* relinfo,
4079 const elfcpp::Rela<size, false>& rela,
4080 unsigned int r_type,
4081 typename elfcpp::Elf_types<size>::Elf_Addr value,
4082 unsigned char* view,
4083 typename elfcpp::Elf_types<size>::Elf_Addr address,
4084 section_size_type view_size)
4086 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4088 // leaq foo@tlsdesc(%rip), %rax
4089 // ==> movq foo@gottpoff(%rip), %rax
4090 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4091 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4092 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4093 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4095 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4096 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4100 // call *foo@tlscall(%rax)
4102 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4103 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4104 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4105 view[0] == 0xff && view[1] == 0x10);
4111 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4115 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4116 const Relocate_info<size, false>* relinfo,
4118 Output_segment* tls_segment,
4119 const elfcpp::Rela<size, false>& rela,
4120 unsigned int r_type,
4121 typename elfcpp::Elf_types<size>::Elf_Addr value,
4122 unsigned char* view,
4123 section_size_type view_size)
4125 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4127 // leaq foo@tlsdesc(%rip), %rax
4128 // ==> movq foo@tpoff, %rax
4129 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4130 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4131 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4132 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4135 value -= tls_segment->memsz();
4136 Relocate_functions<size, false>::rela32(view, value, 0);
4140 // call *foo@tlscall(%rax)
4142 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4143 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4144 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4145 view[0] == 0xff && view[1] == 0x10);
4153 Target_x86_64<size>::Relocate::tls_ld_to_le(
4154 const Relocate_info<size, false>* relinfo,
4157 const elfcpp::Rela<size, false>& rela,
4159 typename elfcpp::Elf_types<size>::Elf_Addr,
4160 unsigned char* view,
4161 section_size_type view_size)
4163 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4165 // ... leq foo@dtpoff(%rax),%reg
4166 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4168 // ... leq foo@dtpoff(%rax),%reg
4169 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4171 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4172 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4174 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4175 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4177 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4180 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4182 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4184 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4186 this->skip_call_tls_get_addr_ = true;
4189 // Do a relocation in which we convert a TLS Initial-Exec to a
4194 Target_x86_64<size>::Relocate::tls_ie_to_le(
4195 const Relocate_info<size, false>* relinfo,
4197 Output_segment* tls_segment,
4198 const elfcpp::Rela<size, false>& rela,
4200 typename elfcpp::Elf_types<size>::Elf_Addr value,
4201 unsigned char* view,
4202 section_size_type view_size)
4204 // We need to examine the opcodes to figure out which instruction we
4207 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4208 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4210 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4211 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4213 unsigned char op1 = view[-3];
4214 unsigned char op2 = view[-2];
4215 unsigned char op3 = view[-1];
4216 unsigned char reg = op3 >> 3;
4223 else if (size == 32 && op1 == 0x44)
4226 view[-1] = 0xc0 | reg;
4230 // Special handling for %rsp.
4233 else if (size == 32 && op1 == 0x44)
4236 view[-1] = 0xc0 | reg;
4243 else if (size == 32 && op1 == 0x44)
4246 view[-1] = 0x80 | reg | (reg << 3);
4249 if (tls_segment != NULL)
4250 value -= tls_segment->memsz();
4251 Relocate_functions<size, false>::rela32(view, value, 0);
4254 // Relocate section data.
4258 Target_x86_64<size>::relocate_section(
4259 const Relocate_info<size, false>* relinfo,
4260 unsigned int sh_type,
4261 const unsigned char* prelocs,
4263 Output_section* output_section,
4264 bool needs_special_offset_handling,
4265 unsigned char* view,
4266 typename elfcpp::Elf_types<size>::Elf_Addr address,
4267 section_size_type view_size,
4268 const Reloc_symbol_changes* reloc_symbol_changes)
4270 gold_assert(sh_type == elfcpp::SHT_RELA);
4272 gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
4273 typename Target_x86_64<size>::Relocate,
4274 gold::Default_comdat_behavior>(
4280 needs_special_offset_handling,
4284 reloc_symbol_changes);
4287 // Apply an incremental relocation. Incremental relocations always refer
4288 // to global symbols.
4292 Target_x86_64<size>::apply_relocation(
4293 const Relocate_info<size, false>* relinfo,
4294 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4295 unsigned int r_type,
4296 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4298 unsigned char* view,
4299 typename elfcpp::Elf_types<size>::Elf_Addr address,
4300 section_size_type view_size)
4302 gold::apply_relocation<size, false, Target_x86_64<size>,
4303 typename Target_x86_64<size>::Relocate>(
4315 // Return the size of a relocation while scanning during a relocatable
4320 Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
4321 unsigned int r_type,
4326 case elfcpp::R_X86_64_NONE:
4327 case elfcpp::R_X86_64_GNU_VTINHERIT:
4328 case elfcpp::R_X86_64_GNU_VTENTRY:
4329 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4330 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4331 case elfcpp::R_X86_64_TLSDESC_CALL:
4332 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4333 case elfcpp::R_X86_64_DTPOFF32:
4334 case elfcpp::R_X86_64_DTPOFF64:
4335 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4336 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4339 case elfcpp::R_X86_64_64:
4340 case elfcpp::R_X86_64_PC64:
4341 case elfcpp::R_X86_64_GOTOFF64:
4342 case elfcpp::R_X86_64_GOTPC64:
4343 case elfcpp::R_X86_64_PLTOFF64:
4344 case elfcpp::R_X86_64_GOT64:
4345 case elfcpp::R_X86_64_GOTPCREL64:
4346 case elfcpp::R_X86_64_GOTPCREL:
4347 case elfcpp::R_X86_64_GOTPCRELX:
4348 case elfcpp::R_X86_64_REX_GOTPCRELX:
4349 case elfcpp::R_X86_64_GOTPLT64:
4352 case elfcpp::R_X86_64_32:
4353 case elfcpp::R_X86_64_32S:
4354 case elfcpp::R_X86_64_PC32:
4355 case elfcpp::R_X86_64_PC32_BND:
4356 case elfcpp::R_X86_64_PLT32:
4357 case elfcpp::R_X86_64_PLT32_BND:
4358 case elfcpp::R_X86_64_GOTPC32:
4359 case elfcpp::R_X86_64_GOT32:
4362 case elfcpp::R_X86_64_16:
4363 case elfcpp::R_X86_64_PC16:
4366 case elfcpp::R_X86_64_8:
4367 case elfcpp::R_X86_64_PC8:
4370 case elfcpp::R_X86_64_COPY:
4371 case elfcpp::R_X86_64_GLOB_DAT:
4372 case elfcpp::R_X86_64_JUMP_SLOT:
4373 case elfcpp::R_X86_64_RELATIVE:
4374 case elfcpp::R_X86_64_IRELATIVE:
4375 // These are outstanding tls relocs, which are unexpected when linking
4376 case elfcpp::R_X86_64_TPOFF64:
4377 case elfcpp::R_X86_64_DTPMOD64:
4378 case elfcpp::R_X86_64_TLSDESC:
4379 object->error(_("unexpected reloc %u in object file"), r_type);
4382 case elfcpp::R_X86_64_SIZE32:
4383 case elfcpp::R_X86_64_SIZE64:
4385 object->error(_("unsupported reloc %u against local symbol"), r_type);
4390 // Scan the relocs during a relocatable link.
4394 Target_x86_64<size>::scan_relocatable_relocs(
4395 Symbol_table* symtab,
4397 Sized_relobj_file<size, false>* object,
4398 unsigned int data_shndx,
4399 unsigned int sh_type,
4400 const unsigned char* prelocs,
4402 Output_section* output_section,
4403 bool needs_special_offset_handling,
4404 size_t local_symbol_count,
4405 const unsigned char* plocal_symbols,
4406 Relocatable_relocs* rr)
4408 gold_assert(sh_type == elfcpp::SHT_RELA);
4410 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4411 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4413 gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
4414 Scan_relocatable_relocs>(
4422 needs_special_offset_handling,
4428 // Relocate a section during a relocatable link.
4432 Target_x86_64<size>::relocate_relocs(
4433 const Relocate_info<size, false>* relinfo,
4434 unsigned int sh_type,
4435 const unsigned char* prelocs,
4437 Output_section* output_section,
4438 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4439 const Relocatable_relocs* rr,
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,
4462 // Return the value to use for a dynamic which requires special
4463 // treatment. This is how we support equality comparisons of function
4464 // pointers across shared library boundaries, as described in the
4465 // processor specific ABI supplement.
4469 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4471 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4472 return this->plt_address_for_global(gsym);
4475 // Return a string used to fill a code section with nops to take up
4476 // the specified length.
4480 Target_x86_64<size>::do_code_fill(section_size_type length) const
4484 // Build a jmpq instruction to skip over the bytes.
4485 unsigned char jmp[5];
4487 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4488 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4489 + std::string(length - 5, static_cast<char>(0x90)));
4492 // Nop sequences of various lengths.
4493 const char nop1[1] = { '\x90' }; // nop
4494 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4495 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4496 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4498 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4500 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4501 '\x44', '\x00', '\x00' };
4502 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4503 '\x00', '\x00', '\x00',
4505 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4506 '\x00', '\x00', '\x00',
4508 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4509 '\x84', '\x00', '\x00',
4510 '\x00', '\x00', '\x00' };
4511 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4512 '\x1f', '\x84', '\x00',
4513 '\x00', '\x00', '\x00',
4515 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4516 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4517 '\x00', '\x00', '\x00',
4519 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4520 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4521 '\x84', '\x00', '\x00',
4522 '\x00', '\x00', '\x00' };
4523 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4524 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4525 '\x1f', '\x84', '\x00',
4526 '\x00', '\x00', '\x00',
4528 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4529 '\x66', '\x66', '\x2e', // data16
4530 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4531 '\x00', '\x00', '\x00',
4533 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4534 '\x66', '\x66', '\x66', // data16; data16
4535 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4536 '\x84', '\x00', '\x00',
4537 '\x00', '\x00', '\x00' };
4539 const char* nops[16] = {
4541 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4542 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4545 return std::string(nops[length], length);
4548 // Return the addend to use for a target specific relocation. The
4549 // only target specific relocation is R_X86_64_TLSDESC for a local
4550 // symbol. We want to set the addend is the offset of the local
4551 // symbol in the TLS segment.
4555 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4558 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4559 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4560 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4561 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4562 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4563 gold_assert(psymval->is_tls_symbol());
4564 // The value of a TLS symbol is the offset in the TLS segment.
4565 return psymval->value(ti.object, 0);
4568 // Return the value to use for the base of a DW_EH_PE_datarel offset
4569 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4570 // assembler can not write out the difference between two labels in
4571 // different sections, so instead of using a pc-relative value they
4572 // use an offset from the GOT.
4576 Target_x86_64<size>::do_ehframe_datarel_base() const
4578 gold_assert(this->global_offset_table_ != NULL);
4579 Symbol* sym = this->global_offset_table_;
4580 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4581 return ssym->value();
4584 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4585 // compiled with -fsplit-stack. The function calls non-split-stack
4586 // code. We have to change the function so that it always ensures
4587 // that it has enough stack space to run some random function.
4589 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
4590 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
4591 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
4593 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4594 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
4595 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4599 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4600 section_offset_type fnoffset,
4601 section_size_type fnsize,
4602 unsigned char* view,
4603 section_size_type view_size,
4605 std::string* to) const
4607 const char* const cmp_insn = reinterpret_cast<const char*>
4608 (size == 32 ? cmp_insn_32 : cmp_insn_64);
4609 const char* const lea_r10_insn = reinterpret_cast<const char*>
4610 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
4611 const char* const lea_r11_insn = reinterpret_cast<const char*>
4612 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
4614 const size_t cmp_insn_len =
4615 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
4616 const size_t lea_r10_insn_len =
4617 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
4618 const size_t lea_r11_insn_len =
4619 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
4620 const size_t nop_len = (size == 32 ? 7 : 8);
4622 // The function starts with a comparison of the stack pointer and a
4623 // field in the TCB. This is followed by a jump.
4626 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
4627 && fnsize > nop_len + 1)
4629 // We will call __morestack if the carry flag is set after this
4630 // comparison. We turn the comparison into an stc instruction
4632 view[fnoffset] = '\xf9';
4633 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
4635 // lea NN(%rsp),%r10
4636 // lea NN(%rsp),%r11
4637 else if ((this->match_view(view, view_size, fnoffset,
4638 lea_r10_insn, lea_r10_insn_len)
4639 || this->match_view(view, view_size, fnoffset,
4640 lea_r11_insn, lea_r11_insn_len))
4643 // This is loading an offset from the stack pointer for a
4644 // comparison. The offset is negative, so we decrease the
4645 // offset by the amount of space we need for the stack. This
4646 // means we will avoid calling __morestack if there happens to
4647 // be plenty of space on the stack already.
4648 unsigned char* pval = view + fnoffset + 4;
4649 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4650 val -= parameters->options().split_stack_adjust_size();
4651 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4655 if (!object->has_no_split_stack())
4656 object->error(_("failed to match split-stack sequence at "
4657 "section %u offset %0zx"),
4658 shndx, static_cast<size_t>(fnoffset));
4662 // We have to change the function so that it calls
4663 // __morestack_non_split instead of __morestack. The former will
4664 // allocate additional stack space.
4665 *from = "__morestack";
4666 *to = "__morestack_non_split";
4669 // The selector for x86_64 object files. Note this is never instantiated
4670 // directly. It's only used in Target_selector_x86_64_nacl, below.
4673 class Target_selector_x86_64 : public Target_selector_freebsd
4676 Target_selector_x86_64()
4677 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4679 ? "elf64-x86-64" : "elf32-x86-64"),
4681 ? "elf64-x86-64-freebsd"
4682 : "elf32-x86-64-freebsd"),
4683 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4687 do_instantiate_target()
4688 { return new Target_x86_64<size>(); }
4692 // NaCl variant. It uses different PLT contents.
4695 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4698 Output_data_plt_x86_64_nacl(Layout* layout,
4699 Output_data_got<64, false>* got,
4700 Output_data_got_plt_x86_64* got_plt,
4701 Output_data_space* got_irelative)
4702 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4703 got, got_plt, got_irelative)
4706 Output_data_plt_x86_64_nacl(Layout* layout,
4707 Output_data_got<64, false>* got,
4708 Output_data_got_plt_x86_64* got_plt,
4709 Output_data_space* got_irelative,
4710 unsigned int plt_count)
4711 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4712 got, got_plt, got_irelative,
4717 virtual unsigned int
4718 do_get_plt_entry_size() const
4719 { return plt_entry_size; }
4722 do_add_eh_frame(Layout* layout)
4724 layout->add_eh_frame_for_plt(this,
4725 this->plt_eh_frame_cie,
4726 this->plt_eh_frame_cie_size,
4728 plt_eh_frame_fde_size);
4732 do_fill_first_plt_entry(unsigned char* pov,
4733 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4734 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4736 virtual unsigned int
4737 do_fill_plt_entry(unsigned char* pov,
4738 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4739 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4740 unsigned int got_offset,
4741 unsigned int plt_offset,
4742 unsigned int plt_index);
4745 do_fill_tlsdesc_entry(unsigned char* pov,
4746 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4747 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4748 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4749 unsigned int tlsdesc_got_offset,
4750 unsigned int plt_offset);
4753 // The size of an entry in the PLT.
4754 static const int plt_entry_size = 64;
4756 // The first entry in the PLT.
4757 static const unsigned char first_plt_entry[plt_entry_size];
4759 // Other entries in the PLT for an executable.
4760 static const unsigned char plt_entry[plt_entry_size];
4762 // The reserved TLSDESC entry in the PLT for an executable.
4763 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4765 // The .eh_frame unwind information for the PLT.
4766 static const int plt_eh_frame_fde_size = 32;
4767 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4771 class Target_x86_64_nacl : public Target_x86_64<size>
4774 Target_x86_64_nacl()
4775 : Target_x86_64<size>(&x86_64_nacl_info)
4778 virtual Output_data_plt_x86_64<size>*
4779 do_make_data_plt(Layout* layout,
4780 Output_data_got<64, false>* got,
4781 Output_data_got_plt_x86_64* got_plt,
4782 Output_data_space* got_irelative)
4784 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4788 virtual Output_data_plt_x86_64<size>*
4789 do_make_data_plt(Layout* layout,
4790 Output_data_got<64, false>* got,
4791 Output_data_got_plt_x86_64* got_plt,
4792 Output_data_space* got_irelative,
4793 unsigned int plt_count)
4795 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4801 do_code_fill(section_size_type length) const;
4804 static const Target::Target_info x86_64_nacl_info;
4808 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4811 false, // is_big_endian
4812 elfcpp::EM_X86_64, // machine_code
4813 false, // has_make_symbol
4814 false, // has_resolve
4815 true, // has_code_fill
4816 true, // is_default_stack_executable
4817 true, // can_icf_inline_merge_sections
4819 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4820 0x20000, // default_text_segment_address
4821 0x10000, // abi_pagesize (overridable by -z max-page-size)
4822 0x10000, // common_pagesize (overridable by -z common-page-size)
4823 true, // isolate_execinstr
4824 0x10000000, // rosegment_gap
4825 elfcpp::SHN_UNDEF, // small_common_shndx
4826 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4827 0, // small_common_section_flags
4828 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4829 NULL, // attributes_section
4830 NULL, // attributes_vendor
4831 "_start", // entry_symbol_name
4832 32, // hash_entry_size
4836 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4839 false, // is_big_endian
4840 elfcpp::EM_X86_64, // machine_code
4841 false, // has_make_symbol
4842 false, // has_resolve
4843 true, // has_code_fill
4844 true, // is_default_stack_executable
4845 true, // can_icf_inline_merge_sections
4847 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4848 0x20000, // default_text_segment_address
4849 0x10000, // abi_pagesize (overridable by -z max-page-size)
4850 0x10000, // common_pagesize (overridable by -z common-page-size)
4851 true, // isolate_execinstr
4852 0x10000000, // rosegment_gap
4853 elfcpp::SHN_UNDEF, // small_common_shndx
4854 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4855 0, // small_common_section_flags
4856 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4857 NULL, // attributes_section
4858 NULL, // attributes_vendor
4859 "_start", // entry_symbol_name
4860 32, // hash_entry_size
4863 #define NACLMASK 0xe0 // 32-byte alignment mask.
4865 // The first entry in the PLT.
4869 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4871 0xff, 0x35, // pushq contents of memory address
4872 0, 0, 0, 0, // replaced with address of .got + 8
4873 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4874 0, 0, 0, 0, // replaced with address of .got + 16
4875 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4876 0x4d, 0x01, 0xfb, // add %r15, %r11
4877 0x41, 0xff, 0xe3, // jmpq *%r11
4879 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4880 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4882 // 32 bytes of nop to pad out to the standard size
4883 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4884 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4885 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4886 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4887 0x66, // excess data32 prefix
4893 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4895 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4896 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4898 memcpy(pov, first_plt_entry, plt_entry_size);
4899 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4901 - (plt_address + 2 + 4)));
4902 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4904 - (plt_address + 9 + 4)));
4907 // Subsequent entries in the PLT.
4911 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4913 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4914 0, 0, 0, 0, // replaced with address of symbol in .got
4915 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4916 0x4d, 0x01, 0xfb, // add %r15, %r11
4917 0x41, 0xff, 0xe3, // jmpq *%r11
4919 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4920 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4921 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4923 // Lazy GOT entries point here (32-byte aligned).
4924 0x68, // pushq immediate
4925 0, 0, 0, 0, // replaced with index into relocation table
4926 0xe9, // jmp relative
4927 0, 0, 0, 0, // replaced with offset to start of .plt0
4929 // 22 bytes of nop to pad out to the standard size.
4930 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4931 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4932 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4937 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4939 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4940 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4941 unsigned int got_offset,
4942 unsigned int plt_offset,
4943 unsigned int plt_index)
4945 memcpy(pov, plt_entry, plt_entry_size);
4946 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4947 (got_address + got_offset
4948 - (plt_address + plt_offset
4951 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4952 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4953 - (plt_offset + 38 + 4));
4958 // The reserved TLSDESC entry in the PLT.
4962 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4964 0xff, 0x35, // pushq x(%rip)
4965 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4966 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4967 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4968 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4969 0x4d, 0x01, 0xfb, // add %r15, %r11
4970 0x41, 0xff, 0xe3, // jmpq *%r11
4972 // 41 bytes of nop to pad out to the standard size.
4973 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4974 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4975 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4976 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4977 0x66, 0x66, // excess data32 prefixes
4978 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4983 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
4985 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4986 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4987 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4988 unsigned int tlsdesc_got_offset,
4989 unsigned int plt_offset)
4991 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
4992 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4994 - (plt_address + plt_offset
4996 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4998 + tlsdesc_got_offset
4999 - (plt_address + plt_offset
5003 // The .eh_frame unwind information for the PLT.
5007 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5009 0, 0, 0, 0, // Replaced with offset to .plt.
5010 0, 0, 0, 0, // Replaced with size of .plt.
5011 0, // Augmentation size.
5012 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5013 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5014 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5015 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5016 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5017 13, // Block length.
5018 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5019 elfcpp::DW_OP_breg16, 0, // Push %rip.
5020 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5021 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5022 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5023 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5024 elfcpp::DW_OP_lit3, // Push 3.
5025 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5026 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5027 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5031 // Return a string used to fill a code section with nops.
5032 // For NaCl, long NOPs are only valid if they do not cross
5033 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5036 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5038 return std::string(length, static_cast<char>(0x90));
5041 // The selector for x86_64-nacl object files.
5044 class Target_selector_x86_64_nacl
5045 : public Target_selector_nacl<Target_selector_x86_64<size>,
5046 Target_x86_64_nacl<size> >
5049 Target_selector_x86_64_nacl()
5050 : Target_selector_nacl<Target_selector_x86_64<size>,
5051 Target_x86_64_nacl<size> >("x86-64",
5053 ? "elf64-x86-64-nacl"
5054 : "elf32-x86-64-nacl",
5057 : "elf32_x86_64_nacl")
5061 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5062 Target_selector_x86_64_nacl<32> target_selector_x32;
5064 } // End anonymous namespace.