1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2014 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 PLT data.
52 // This is an abstract base class that handles most of the linker details
53 // but does not know the actual contents of PLT entries. The derived
54 // classes below fill in those details.
57 class Output_data_plt_x86_64 : public Output_section_data
60 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
62 Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
63 Output_data_got<64, false>* got,
64 Output_data_space* got_plt,
65 Output_data_space* got_irelative)
66 : Output_section_data(addralign), layout_(layout), tlsdesc_rel_(NULL),
67 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
68 got_irelative_(got_irelative), count_(0), irelative_count_(0),
69 tlsdesc_got_offset_(-1U), free_list_()
70 { this->init(layout); }
72 Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
73 Output_data_got<64, false>* got,
74 Output_data_space* got_plt,
75 Output_data_space* got_irelative,
76 unsigned int plt_count)
77 : Output_section_data((plt_count + 1) * plt_entry_size,
78 plt_entry_size, false),
79 layout_(layout), tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
80 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
81 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
85 // Initialize the free list and reserve the first entry.
86 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
87 this->free_list_.remove(0, plt_entry_size);
90 // Initialize the PLT section.
94 // Add an entry to the PLT.
96 add_entry(Symbol_table*, Layout*, Symbol* gsym);
98 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
100 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
101 Sized_relobj_file<size, false>* relobj,
102 unsigned int local_sym_index);
104 // Add the relocation for a PLT entry.
106 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
107 unsigned int got_offset);
109 // Add the reserved TLSDESC_PLT entry to the PLT.
111 reserve_tlsdesc_entry(unsigned int got_offset)
112 { this->tlsdesc_got_offset_ = got_offset; }
114 // Return true if a TLSDESC_PLT entry has been reserved.
116 has_tlsdesc_entry() const
117 { return this->tlsdesc_got_offset_ != -1U; }
119 // Return the GOT offset for the reserved TLSDESC_PLT entry.
121 get_tlsdesc_got_offset() const
122 { return this->tlsdesc_got_offset_; }
124 // Return the offset of the reserved TLSDESC_PLT entry.
126 get_tlsdesc_plt_offset() const
128 return ((this->count_ + this->irelative_count_ + 1)
129 * this->get_plt_entry_size());
132 // Return the .rela.plt section data.
135 { return this->rel_; }
137 // Return where the TLSDESC relocations should go.
139 rela_tlsdesc(Layout*);
141 // Return where the IRELATIVE relocations should go in the PLT
144 rela_irelative(Symbol_table*, Layout*);
146 // Return whether we created a section for IRELATIVE relocations.
148 has_irelative_section() const
149 { return this->irelative_rel_ != NULL; }
151 // Return the number of PLT entries.
154 { return this->count_ + this->irelative_count_; }
156 // Return the offset of the first non-reserved PLT entry.
158 first_plt_entry_offset()
159 { return this->get_plt_entry_size(); }
161 // Return the size of a PLT entry.
163 get_plt_entry_size() const
164 { return this->do_get_plt_entry_size(); }
166 // Reserve a slot in the PLT for an existing symbol in an incremental update.
168 reserve_slot(unsigned int plt_index)
170 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
171 (plt_index + 2) * this->get_plt_entry_size());
174 // Return the PLT address to use for a global symbol.
176 address_for_global(const Symbol*);
178 // Return the PLT address to use for a local symbol.
180 address_for_local(const Relobj*, unsigned int symndx);
182 // Add .eh_frame information for the PLT.
184 add_eh_frame(Layout* layout)
185 { this->do_add_eh_frame(layout); }
188 // Fill in the first PLT entry.
190 fill_first_plt_entry(unsigned char* pov,
191 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
192 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
193 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
195 // Fill in a normal PLT entry. Returns the offset into the entry that
196 // should be the initial GOT slot value.
198 fill_plt_entry(unsigned char* pov,
199 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
200 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
201 unsigned int got_offset,
202 unsigned int plt_offset,
203 unsigned int plt_index)
205 return this->do_fill_plt_entry(pov, got_address, plt_address,
206 got_offset, plt_offset, plt_index);
209 // Fill in the reserved TLSDESC PLT entry.
211 fill_tlsdesc_entry(unsigned char* pov,
212 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
213 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
214 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
215 unsigned int tlsdesc_got_offset,
216 unsigned int plt_offset)
218 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
219 tlsdesc_got_offset, plt_offset);
223 do_get_plt_entry_size() const = 0;
226 do_fill_first_plt_entry(unsigned char* pov,
227 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
228 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
232 do_fill_plt_entry(unsigned char* pov,
233 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
234 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
235 unsigned int got_offset,
236 unsigned int plt_offset,
237 unsigned int plt_index) = 0;
240 do_fill_tlsdesc_entry(unsigned char* pov,
241 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
242 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
243 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
244 unsigned int tlsdesc_got_offset,
245 unsigned int plt_offset) = 0;
248 do_add_eh_frame(Layout* layout) = 0;
251 do_adjust_output_section(Output_section* os);
253 // Write to a map file.
255 do_print_to_mapfile(Mapfile* mapfile) const
256 { mapfile->print_output_data(this, _("** PLT")); }
258 // The CIE of the .eh_frame unwind information for the PLT.
259 static const int plt_eh_frame_cie_size = 16;
260 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
263 // Set the final size.
265 set_final_data_size();
267 // Write out the PLT data.
269 do_write(Output_file*);
271 // A pointer to the Layout class, so that we can find the .dynamic
272 // section when we write out the GOT PLT section.
274 // The reloc section.
276 // The TLSDESC relocs, if necessary. These must follow the regular
278 Reloc_section* tlsdesc_rel_;
279 // The IRELATIVE relocs, if necessary. These must follow the
280 // regular PLT relocations and the TLSDESC relocations.
281 Reloc_section* irelative_rel_;
283 Output_data_got<64, false>* got_;
284 // The .got.plt section.
285 Output_data_space* got_plt_;
286 // The part of the .got.plt section used for IRELATIVE relocs.
287 Output_data_space* got_irelative_;
288 // The number of PLT entries.
290 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
291 // follow the regular PLT entries.
292 unsigned int irelative_count_;
293 // Offset of the reserved TLSDESC_GOT entry when needed.
294 unsigned int tlsdesc_got_offset_;
295 // List of available regions within the section, for incremental
297 Free_list free_list_;
301 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
304 Output_data_plt_x86_64_standard(Layout* layout,
305 Output_data_got<64, false>* got,
306 Output_data_space* got_plt,
307 Output_data_space* got_irelative)
308 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
309 got, got_plt, got_irelative)
312 Output_data_plt_x86_64_standard(Layout* layout,
313 Output_data_got<64, false>* got,
314 Output_data_space* got_plt,
315 Output_data_space* got_irelative,
316 unsigned int plt_count)
317 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
318 got, got_plt, got_irelative,
324 do_get_plt_entry_size() const
325 { return plt_entry_size; }
328 do_add_eh_frame(Layout* layout)
330 layout->add_eh_frame_for_plt(this,
331 this->plt_eh_frame_cie,
332 this->plt_eh_frame_cie_size,
334 plt_eh_frame_fde_size);
338 do_fill_first_plt_entry(unsigned char* pov,
339 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
340 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
343 do_fill_plt_entry(unsigned char* pov,
344 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
345 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
346 unsigned int got_offset,
347 unsigned int plt_offset,
348 unsigned int plt_index);
351 do_fill_tlsdesc_entry(unsigned char* pov,
352 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
353 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
354 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
355 unsigned int tlsdesc_got_offset,
356 unsigned int plt_offset);
359 // The size of an entry in the PLT.
360 static const int plt_entry_size = 16;
362 // The first entry in the PLT.
363 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
364 // procedure linkage table for both programs and shared objects."
365 static const unsigned char first_plt_entry[plt_entry_size];
367 // Other entries in the PLT for an executable.
368 static const unsigned char plt_entry[plt_entry_size];
370 // The reserved TLSDESC entry in the PLT for an executable.
371 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
373 // The .eh_frame unwind information for the PLT.
374 static const int plt_eh_frame_fde_size = 32;
375 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
378 // The x86_64 target class.
380 // http://www.x86-64.org/documentation/abi.pdf
381 // TLS info comes from
382 // http://people.redhat.com/drepper/tls.pdf
383 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
386 class Target_x86_64 : public Sized_target<size, false>
389 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
390 // uses only Elf64_Rela relocation entries with explicit addends."
391 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
393 Target_x86_64(const Target::Target_info* info = &x86_64_info)
394 : Sized_target<size, false>(info),
395 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
396 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
397 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
398 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
399 tls_base_symbol_defined_(false)
402 // Hook for a new output section.
404 do_new_output_section(Output_section*) const;
406 // Scan the relocations to look for symbol adjustments.
408 gc_process_relocs(Symbol_table* symtab,
410 Sized_relobj_file<size, false>* object,
411 unsigned int data_shndx,
412 unsigned int sh_type,
413 const unsigned char* prelocs,
415 Output_section* output_section,
416 bool needs_special_offset_handling,
417 size_t local_symbol_count,
418 const unsigned char* plocal_symbols);
420 // Scan the relocations to look for symbol adjustments.
422 scan_relocs(Symbol_table* symtab,
424 Sized_relobj_file<size, false>* object,
425 unsigned int data_shndx,
426 unsigned int sh_type,
427 const unsigned char* prelocs,
429 Output_section* output_section,
430 bool needs_special_offset_handling,
431 size_t local_symbol_count,
432 const unsigned char* plocal_symbols);
434 // Finalize the sections.
436 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
438 // Return the value to use for a dynamic which requires special
441 do_dynsym_value(const Symbol*) const;
443 // Relocate a section.
445 relocate_section(const Relocate_info<size, false>*,
446 unsigned int sh_type,
447 const unsigned char* prelocs,
449 Output_section* output_section,
450 bool needs_special_offset_handling,
452 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
453 section_size_type view_size,
454 const Reloc_symbol_changes*);
456 // Scan the relocs during a relocatable link.
458 scan_relocatable_relocs(Symbol_table* symtab,
460 Sized_relobj_file<size, false>* object,
461 unsigned int data_shndx,
462 unsigned int sh_type,
463 const unsigned char* prelocs,
465 Output_section* output_section,
466 bool needs_special_offset_handling,
467 size_t local_symbol_count,
468 const unsigned char* plocal_symbols,
469 Relocatable_relocs*);
471 // Emit relocations for a section.
474 const Relocate_info<size, false>*,
475 unsigned int sh_type,
476 const unsigned char* prelocs,
478 Output_section* output_section,
479 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
480 const Relocatable_relocs*,
482 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
483 section_size_type view_size,
484 unsigned char* reloc_view,
485 section_size_type reloc_view_size);
487 // Return a string used to fill a code section with nops.
489 do_code_fill(section_size_type length) const;
491 // Return whether SYM is defined by the ABI.
493 do_is_defined_by_abi(const Symbol* sym) const
494 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
496 // Return the symbol index to use for a target specific relocation.
497 // The only target specific relocation is R_X86_64_TLSDESC for a
498 // local symbol, which is an absolute reloc.
500 do_reloc_symbol_index(void*, unsigned int r_type) const
502 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
506 // Return the addend to use for a target specific relocation.
508 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
510 // Return the PLT section.
512 do_plt_address_for_global(const Symbol* gsym) const
513 { return this->plt_section()->address_for_global(gsym); }
516 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
517 { return this->plt_section()->address_for_local(relobj, symndx); }
519 // This function should be defined in targets that can use relocation
520 // types to determine (implemented in local_reloc_may_be_function_pointer
521 // and global_reloc_may_be_function_pointer)
522 // if a function's pointer is taken. ICF uses this in safe mode to only
523 // fold those functions whose pointer is defintely not taken. For x86_64
524 // pie binaries, safe ICF cannot be done by looking at relocation types.
526 do_can_check_for_function_pointers() const
527 { return !parameters->options().pie(); }
529 // Return the base for a DW_EH_PE_datarel encoding.
531 do_ehframe_datarel_base() const;
533 // Adjust -fsplit-stack code which calls non-split-stack code.
535 do_calls_non_split(Relobj* object, unsigned int shndx,
536 section_offset_type fnoffset, section_size_type fnsize,
537 unsigned char* view, section_size_type view_size,
538 std::string* from, std::string* to) const;
540 // Return the size of the GOT section.
544 gold_assert(this->got_ != NULL);
545 return this->got_->data_size();
548 // Return the number of entries in the GOT.
550 got_entry_count() const
552 if (this->got_ == NULL)
554 return this->got_size() / 8;
557 // Return the number of entries in the PLT.
559 plt_entry_count() const;
561 // Return the offset of the first non-reserved PLT entry.
563 first_plt_entry_offset() const;
565 // Return the size of each PLT entry.
567 plt_entry_size() const;
569 // Create the GOT section for an incremental update.
570 Output_data_got_base*
571 init_got_plt_for_update(Symbol_table* symtab,
573 unsigned int got_count,
574 unsigned int plt_count);
576 // Reserve a GOT entry for a local symbol, and regenerate any
577 // necessary dynamic relocations.
579 reserve_local_got_entry(unsigned int got_index,
580 Sized_relobj<size, false>* obj,
582 unsigned int got_type);
584 // Reserve a GOT entry for a global symbol, and regenerate any
585 // necessary dynamic relocations.
587 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
588 unsigned int got_type);
590 // Register an existing PLT entry for a global symbol.
592 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
595 // Force a COPY relocation for a given symbol.
597 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
599 // Apply an incremental relocation.
601 apply_relocation(const Relocate_info<size, false>* relinfo,
602 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
604 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
607 typename elfcpp::Elf_types<size>::Elf_Addr address,
608 section_size_type view_size);
610 // Add a new reloc argument, returning the index in the vector.
612 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
614 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
615 return this->tlsdesc_reloc_info_.size() - 1;
618 Output_data_plt_x86_64<size>*
619 make_data_plt(Layout* layout,
620 Output_data_got<64, false>* got,
621 Output_data_space* got_plt,
622 Output_data_space* got_irelative)
624 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
627 Output_data_plt_x86_64<size>*
628 make_data_plt(Layout* layout,
629 Output_data_got<64, false>* got,
630 Output_data_space* got_plt,
631 Output_data_space* got_irelative,
632 unsigned int plt_count)
634 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
638 virtual Output_data_plt_x86_64<size>*
639 do_make_data_plt(Layout* layout,
640 Output_data_got<64, false>* got,
641 Output_data_space* got_plt,
642 Output_data_space* got_irelative)
644 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
648 virtual Output_data_plt_x86_64<size>*
649 do_make_data_plt(Layout* layout,
650 Output_data_got<64, false>* got,
651 Output_data_space* got_plt,
652 Output_data_space* got_irelative,
653 unsigned int plt_count)
655 return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
661 // The class which scans relocations.
666 : issued_non_pic_error_(false)
670 get_reference_flags(unsigned int r_type);
673 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
674 Sized_relobj_file<size, false>* object,
675 unsigned int data_shndx,
676 Output_section* output_section,
677 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
678 const elfcpp::Sym<size, false>& lsym,
682 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
683 Sized_relobj_file<size, false>* object,
684 unsigned int data_shndx,
685 Output_section* output_section,
686 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
690 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
691 Target_x86_64* target,
692 Sized_relobj_file<size, false>* object,
693 unsigned int data_shndx,
694 Output_section* output_section,
695 const elfcpp::Rela<size, false>& reloc,
697 const elfcpp::Sym<size, false>& lsym);
700 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
701 Target_x86_64* target,
702 Sized_relobj_file<size, false>* object,
703 unsigned int data_shndx,
704 Output_section* output_section,
705 const elfcpp::Rela<size, false>& reloc,
711 unsupported_reloc_local(Sized_relobj_file<size, false>*,
712 unsigned int r_type);
715 unsupported_reloc_global(Sized_relobj_file<size, false>*,
716 unsigned int r_type, Symbol*);
719 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
722 possible_function_pointer_reloc(unsigned int r_type);
725 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
726 unsigned int r_type);
728 // Whether we have issued an error about a non-PIC compilation.
729 bool issued_non_pic_error_;
732 // The class which implements relocation.
737 : skip_call_tls_get_addr_(false)
742 if (this->skip_call_tls_get_addr_)
744 // FIXME: This needs to specify the location somehow.
745 gold_error(_("missing expected TLS relocation"));
749 // Do a relocation. Return false if the caller should not issue
750 // any warnings about this relocation.
752 relocate(const Relocate_info<size, false>*, Target_x86_64*,
754 size_t relnum, const elfcpp::Rela<size, false>&,
755 unsigned int r_type, const Sized_symbol<size>*,
756 const Symbol_value<size>*,
757 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
761 // Do a TLS relocation.
763 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
764 size_t relnum, const elfcpp::Rela<size, false>&,
765 unsigned int r_type, const Sized_symbol<size>*,
766 const Symbol_value<size>*,
767 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
770 // Do a TLS General-Dynamic to Initial-Exec transition.
772 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
773 Output_segment* tls_segment,
774 const elfcpp::Rela<size, false>&, unsigned int r_type,
775 typename elfcpp::Elf_types<size>::Elf_Addr value,
777 typename elfcpp::Elf_types<size>::Elf_Addr,
778 section_size_type view_size);
780 // Do a TLS General-Dynamic to Local-Exec transition.
782 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
783 Output_segment* tls_segment,
784 const elfcpp::Rela<size, false>&, unsigned int r_type,
785 typename elfcpp::Elf_types<size>::Elf_Addr value,
787 section_size_type view_size);
789 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
791 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
792 Output_segment* tls_segment,
793 const elfcpp::Rela<size, false>&, unsigned int r_type,
794 typename elfcpp::Elf_types<size>::Elf_Addr value,
796 typename elfcpp::Elf_types<size>::Elf_Addr,
797 section_size_type view_size);
799 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
801 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
802 Output_segment* tls_segment,
803 const elfcpp::Rela<size, false>&, unsigned int r_type,
804 typename elfcpp::Elf_types<size>::Elf_Addr value,
806 section_size_type view_size);
808 // Do a TLS Local-Dynamic to Local-Exec transition.
810 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
811 Output_segment* tls_segment,
812 const elfcpp::Rela<size, false>&, unsigned int r_type,
813 typename elfcpp::Elf_types<size>::Elf_Addr value,
815 section_size_type view_size);
817 // Do a TLS Initial-Exec to Local-Exec transition.
819 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
820 Output_segment* tls_segment,
821 const elfcpp::Rela<size, false>&, unsigned int r_type,
822 typename elfcpp::Elf_types<size>::Elf_Addr value,
824 section_size_type view_size);
826 // This is set if we should skip the next reloc, which should be a
827 // PLT32 reloc against ___tls_get_addr.
828 bool skip_call_tls_get_addr_;
831 // A class which returns the size required for a relocation type,
832 // used while scanning relocs during a relocatable link.
833 class Relocatable_size_for_reloc
837 get_size_for_reloc(unsigned int, Relobj*);
840 // Adjust TLS relocation type based on the options and whether this
841 // is a local symbol.
842 static tls::Tls_optimization
843 optimize_tls_reloc(bool is_final, int r_type);
845 // Get the GOT section, creating it if necessary.
846 Output_data_got<64, false>*
847 got_section(Symbol_table*, Layout*);
849 // Get the GOT PLT section.
851 got_plt_section() const
853 gold_assert(this->got_plt_ != NULL);
854 return this->got_plt_;
857 // Get the GOT section for TLSDESC entries.
858 Output_data_got<64, false>*
859 got_tlsdesc_section() const
861 gold_assert(this->got_tlsdesc_ != NULL);
862 return this->got_tlsdesc_;
865 // Create the PLT section.
867 make_plt_section(Symbol_table* symtab, Layout* layout);
869 // Create a PLT entry for a global symbol.
871 make_plt_entry(Symbol_table*, Layout*, Symbol*);
873 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
875 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
876 Sized_relobj_file<size, false>* relobj,
877 unsigned int local_sym_index);
879 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
881 define_tls_base_symbol(Symbol_table*, Layout*);
883 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
885 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
887 // Create a GOT entry for the TLS module index.
889 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
890 Sized_relobj_file<size, false>* object);
892 // Get the PLT section.
893 Output_data_plt_x86_64<size>*
896 gold_assert(this->plt_ != NULL);
900 // Get the dynamic reloc section, creating it if necessary.
902 rela_dyn_section(Layout*);
904 // Get the section to use for TLSDESC relocations.
906 rela_tlsdesc_section(Layout*) const;
908 // Get the section to use for IRELATIVE relocations.
910 rela_irelative_section(Layout*);
912 // Add a potential copy relocation.
914 copy_reloc(Symbol_table* symtab, Layout* layout,
915 Sized_relobj_file<size, false>* object,
916 unsigned int shndx, Output_section* output_section,
917 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
919 this->copy_relocs_.copy_reloc(symtab, layout,
920 symtab->get_sized_symbol<size>(sym),
921 object, shndx, output_section,
922 reloc, this->rela_dyn_section(layout));
925 // Information about this specific target which we pass to the
926 // general Target structure.
927 static const Target::Target_info x86_64_info;
929 // The types of GOT entries needed for this platform.
930 // These values are exposed to the ABI in an incremental link.
931 // Do not renumber existing values without changing the version
932 // number of the .gnu_incremental_inputs section.
935 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
936 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
937 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
938 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
941 // This type is used as the argument to the target specific
942 // relocation routines. The only target specific reloc is
943 // R_X86_64_TLSDESC against a local symbol.
946 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
947 : object(a_object), r_sym(a_r_sym)
950 // The object in which the local symbol is defined.
951 Sized_relobj_file<size, false>* object;
952 // The local symbol index in the object.
957 Output_data_got<64, false>* got_;
959 Output_data_plt_x86_64<size>* plt_;
960 // The GOT PLT section.
961 Output_data_space* got_plt_;
962 // The GOT section for IRELATIVE relocations.
963 Output_data_space* got_irelative_;
964 // The GOT section for TLSDESC relocations.
965 Output_data_got<64, false>* got_tlsdesc_;
966 // The _GLOBAL_OFFSET_TABLE_ symbol.
967 Symbol* global_offset_table_;
968 // The dynamic reloc section.
969 Reloc_section* rela_dyn_;
970 // The section to use for IRELATIVE relocs.
971 Reloc_section* rela_irelative_;
972 // Relocs saved to avoid a COPY reloc.
973 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
974 // Offset of the GOT entry for the TLS module index.
975 unsigned int got_mod_index_offset_;
976 // We handle R_X86_64_TLSDESC against a local symbol as a target
977 // specific relocation. Here we store the object and local symbol
978 // index for the relocation.
979 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
980 // True if the _TLS_MODULE_BASE_ symbol has been defined.
981 bool tls_base_symbol_defined_;
985 const Target::Target_info Target_x86_64<64>::x86_64_info =
988 false, // is_big_endian
989 elfcpp::EM_X86_64, // machine_code
990 false, // has_make_symbol
991 false, // has_resolve
992 true, // has_code_fill
993 true, // is_default_stack_executable
994 true, // can_icf_inline_merge_sections
996 "/lib/ld64.so.1", // program interpreter
997 0x400000, // default_text_segment_address
998 0x1000, // abi_pagesize (overridable by -z max-page-size)
999 0x1000, // common_pagesize (overridable by -z common-page-size)
1000 false, // isolate_execinstr
1002 elfcpp::SHN_UNDEF, // small_common_shndx
1003 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1004 0, // small_common_section_flags
1005 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1006 NULL, // attributes_section
1007 NULL, // attributes_vendor
1008 "_start" // entry_symbol_name
1012 const Target::Target_info Target_x86_64<32>::x86_64_info =
1015 false, // is_big_endian
1016 elfcpp::EM_X86_64, // machine_code
1017 false, // has_make_symbol
1018 false, // has_resolve
1019 true, // has_code_fill
1020 true, // is_default_stack_executable
1021 true, // can_icf_inline_merge_sections
1023 "/libx32/ldx32.so.1", // program interpreter
1024 0x400000, // default_text_segment_address
1025 0x1000, // abi_pagesize (overridable by -z max-page-size)
1026 0x1000, // common_pagesize (overridable by -z common-page-size)
1027 false, // isolate_execinstr
1029 elfcpp::SHN_UNDEF, // small_common_shndx
1030 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1031 0, // small_common_section_flags
1032 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1033 NULL, // attributes_section
1034 NULL, // attributes_vendor
1035 "_start" // entry_symbol_name
1038 // This is called when a new output section is created. This is where
1039 // we handle the SHF_X86_64_LARGE.
1043 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1045 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1046 os->set_is_large_section();
1049 // Get the GOT section, creating it if necessary.
1052 Output_data_got<64, false>*
1053 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1055 if (this->got_ == NULL)
1057 gold_assert(symtab != NULL && layout != NULL);
1059 // When using -z now, we can treat .got.plt as a relro section.
1060 // Without -z now, it is modified after program startup by lazy
1062 bool is_got_plt_relro = parameters->options().now();
1063 Output_section_order got_order = (is_got_plt_relro
1065 : ORDER_RELRO_LAST);
1066 Output_section_order got_plt_order = (is_got_plt_relro
1068 : ORDER_NON_RELRO_FIRST);
1070 this->got_ = new Output_data_got<64, false>();
1072 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1074 | elfcpp::SHF_WRITE),
1075 this->got_, got_order, true);
1077 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
1078 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1080 | elfcpp::SHF_WRITE),
1081 this->got_plt_, got_plt_order,
1084 // The first three entries are reserved.
1085 this->got_plt_->set_current_data_size(3 * 8);
1087 if (!is_got_plt_relro)
1089 // Those bytes can go into the relro segment.
1090 layout->increase_relro(3 * 8);
1093 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1094 this->global_offset_table_ =
1095 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1096 Symbol_table::PREDEFINED,
1098 0, 0, elfcpp::STT_OBJECT,
1100 elfcpp::STV_HIDDEN, 0,
1103 // If there are any IRELATIVE relocations, they get GOT entries
1104 // in .got.plt after the jump slot entries.
1105 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1106 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1108 | elfcpp::SHF_WRITE),
1109 this->got_irelative_,
1110 got_plt_order, is_got_plt_relro);
1112 // If there are any TLSDESC relocations, they get GOT entries in
1113 // .got.plt after the jump slot and IRELATIVE entries.
1114 this->got_tlsdesc_ = new Output_data_got<64, false>();
1115 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1117 | elfcpp::SHF_WRITE),
1119 got_plt_order, is_got_plt_relro);
1125 // Get the dynamic reloc section, creating it if necessary.
1128 typename Target_x86_64<size>::Reloc_section*
1129 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1131 if (this->rela_dyn_ == NULL)
1133 gold_assert(layout != NULL);
1134 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1135 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1136 elfcpp::SHF_ALLOC, this->rela_dyn_,
1137 ORDER_DYNAMIC_RELOCS, false);
1139 return this->rela_dyn_;
1142 // Get the section to use for IRELATIVE relocs, creating it if
1143 // necessary. These go in .rela.dyn, but only after all other dynamic
1144 // relocations. They need to follow the other dynamic relocations so
1145 // that they can refer to global variables initialized by those
1149 typename Target_x86_64<size>::Reloc_section*
1150 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1152 if (this->rela_irelative_ == NULL)
1154 // Make sure we have already created the dynamic reloc section.
1155 this->rela_dyn_section(layout);
1156 this->rela_irelative_ = new Reloc_section(false);
1157 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1158 elfcpp::SHF_ALLOC, this->rela_irelative_,
1159 ORDER_DYNAMIC_RELOCS, false);
1160 gold_assert(this->rela_dyn_->output_section()
1161 == this->rela_irelative_->output_section());
1163 return this->rela_irelative_;
1166 // Initialize the PLT section.
1170 Output_data_plt_x86_64<size>::init(Layout* layout)
1172 this->rel_ = new Reloc_section(false);
1173 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1174 elfcpp::SHF_ALLOC, this->rel_,
1175 ORDER_DYNAMIC_PLT_RELOCS, false);
1180 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1182 os->set_entsize(this->get_plt_entry_size());
1185 // Add an entry to the PLT.
1189 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1192 gold_assert(!gsym->has_plt_offset());
1194 unsigned int plt_index;
1196 section_offset_type got_offset;
1198 unsigned int* pcount;
1199 unsigned int offset;
1200 unsigned int reserved;
1201 Output_data_space* got;
1202 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1203 && gsym->can_use_relative_reloc(false))
1205 pcount = &this->irelative_count_;
1208 got = this->got_irelative_;
1212 pcount = &this->count_;
1215 got = this->got_plt_;
1218 if (!this->is_data_size_valid())
1220 // Note that when setting the PLT offset for a non-IRELATIVE
1221 // entry we skip the initial reserved PLT entry.
1222 plt_index = *pcount + offset;
1223 plt_offset = plt_index * this->get_plt_entry_size();
1227 got_offset = (plt_index - offset + reserved) * 8;
1228 gold_assert(got_offset == got->current_data_size());
1230 // Every PLT entry needs a GOT entry which points back to the PLT
1231 // entry (this will be changed by the dynamic linker, normally
1232 // lazily when the function is called).
1233 got->set_current_data_size(got_offset + 8);
1237 // FIXME: This is probably not correct for IRELATIVE relocs.
1239 // For incremental updates, find an available slot.
1240 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1241 this->get_plt_entry_size(), 0);
1242 if (plt_offset == -1)
1243 gold_fallback(_("out of patch space (PLT);"
1244 " relink with --incremental-full"));
1246 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1247 // can be calculated from the PLT index, adjusting for the three
1248 // reserved entries at the beginning of the GOT.
1249 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1250 got_offset = (plt_index - offset + reserved) * 8;
1253 gsym->set_plt_offset(plt_offset);
1255 // Every PLT entry needs a reloc.
1256 this->add_relocation(symtab, layout, gsym, got_offset);
1258 // Note that we don't need to save the symbol. The contents of the
1259 // PLT are independent of which symbols are used. The symbols only
1260 // appear in the relocations.
1263 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1268 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1269 Symbol_table* symtab,
1271 Sized_relobj_file<size, false>* relobj,
1272 unsigned int local_sym_index)
1274 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1275 ++this->irelative_count_;
1277 section_offset_type got_offset = this->got_irelative_->current_data_size();
1279 // Every PLT entry needs a GOT entry which points back to the PLT
1281 this->got_irelative_->set_current_data_size(got_offset + 8);
1283 // Every PLT entry needs a reloc.
1284 Reloc_section* rela = this->rela_irelative(symtab, layout);
1285 rela->add_symbolless_local_addend(relobj, local_sym_index,
1286 elfcpp::R_X86_64_IRELATIVE,
1287 this->got_irelative_, got_offset, 0);
1292 // Add the relocation for a PLT entry.
1296 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1299 unsigned int got_offset)
1301 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1302 && gsym->can_use_relative_reloc(false))
1304 Reloc_section* rela = this->rela_irelative(symtab, layout);
1305 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1306 this->got_irelative_, got_offset, 0);
1310 gsym->set_needs_dynsym_entry();
1311 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1316 // Return where the TLSDESC relocations should go, creating it if
1317 // necessary. These follow the JUMP_SLOT relocations.
1320 typename Output_data_plt_x86_64<size>::Reloc_section*
1321 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1323 if (this->tlsdesc_rel_ == NULL)
1325 this->tlsdesc_rel_ = new Reloc_section(false);
1326 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1327 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1328 ORDER_DYNAMIC_PLT_RELOCS, false);
1329 gold_assert(this->tlsdesc_rel_->output_section()
1330 == this->rel_->output_section());
1332 return this->tlsdesc_rel_;
1335 // Return where the IRELATIVE relocations should go in the PLT. These
1336 // follow the JUMP_SLOT and the TLSDESC relocations.
1339 typename Output_data_plt_x86_64<size>::Reloc_section*
1340 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1343 if (this->irelative_rel_ == NULL)
1345 // Make sure we have a place for the TLSDESC relocations, in
1346 // case we see any later on.
1347 this->rela_tlsdesc(layout);
1348 this->irelative_rel_ = new Reloc_section(false);
1349 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1350 elfcpp::SHF_ALLOC, this->irelative_rel_,
1351 ORDER_DYNAMIC_PLT_RELOCS, false);
1352 gold_assert(this->irelative_rel_->output_section()
1353 == this->rel_->output_section());
1355 if (parameters->doing_static_link())
1357 // A statically linked executable will only have a .rela.plt
1358 // section to hold R_X86_64_IRELATIVE relocs for
1359 // STT_GNU_IFUNC symbols. The library will use these
1360 // symbols to locate the IRELATIVE relocs at program startup
1362 symtab->define_in_output_data("__rela_iplt_start", NULL,
1363 Symbol_table::PREDEFINED,
1364 this->irelative_rel_, 0, 0,
1365 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1366 elfcpp::STV_HIDDEN, 0, false, true);
1367 symtab->define_in_output_data("__rela_iplt_end", NULL,
1368 Symbol_table::PREDEFINED,
1369 this->irelative_rel_, 0, 0,
1370 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1371 elfcpp::STV_HIDDEN, 0, true, true);
1374 return this->irelative_rel_;
1377 // Return the PLT address to use for a global symbol.
1381 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
1383 uint64_t offset = 0;
1384 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1385 && gsym->can_use_relative_reloc(false))
1386 offset = (this->count_ + 1) * this->get_plt_entry_size();
1387 return this->address() + offset + gsym->plt_offset();
1390 // Return the PLT address to use for a local symbol. These are always
1391 // IRELATIVE relocs.
1395 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
1398 return (this->address()
1399 + (this->count_ + 1) * this->get_plt_entry_size()
1400 + object->local_plt_offset(r_sym));
1403 // Set the final size.
1406 Output_data_plt_x86_64<size>::set_final_data_size()
1408 unsigned int count = this->count_ + this->irelative_count_;
1409 if (this->has_tlsdesc_entry())
1411 this->set_data_size((count + 1) * this->get_plt_entry_size());
1414 // The first entry in the PLT for an executable.
1418 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1420 // From AMD64 ABI Draft 0.98, page 76
1421 0xff, 0x35, // pushq contents of memory address
1422 0, 0, 0, 0, // replaced with address of .got + 8
1423 0xff, 0x25, // jmp indirect
1424 0, 0, 0, 0, // replaced with address of .got + 16
1425 0x90, 0x90, 0x90, 0x90 // noop (x4)
1430 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1432 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1433 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1435 memcpy(pov, first_plt_entry, plt_entry_size);
1436 // We do a jmp relative to the PC at the end of this instruction.
1437 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1439 - (plt_address + 6)));
1440 elfcpp::Swap<32, false>::writeval(pov + 8,
1442 - (plt_address + 12)));
1445 // Subsequent entries in the PLT for an executable.
1449 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1451 // From AMD64 ABI Draft 0.98, page 76
1452 0xff, 0x25, // jmpq indirect
1453 0, 0, 0, 0, // replaced with address of symbol in .got
1454 0x68, // pushq immediate
1455 0, 0, 0, 0, // replaced with offset into relocation table
1456 0xe9, // jmpq relative
1457 0, 0, 0, 0 // replaced with offset to start of .plt
1462 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1464 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1465 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1466 unsigned int got_offset,
1467 unsigned int plt_offset,
1468 unsigned int plt_index)
1470 memcpy(pov, plt_entry, plt_entry_size);
1471 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1472 (got_address + got_offset
1473 - (plt_address + plt_offset
1476 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1477 elfcpp::Swap<32, false>::writeval(pov + 12,
1478 - (plt_offset + plt_entry_size));
1483 // The reserved TLSDESC entry in the PLT for an executable.
1487 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1489 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1490 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1491 0xff, 0x35, // pushq x(%rip)
1492 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1493 0xff, 0x25, // jmpq *y(%rip)
1494 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1501 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1503 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1504 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1505 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1506 unsigned int tlsdesc_got_offset,
1507 unsigned int plt_offset)
1509 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1510 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1512 - (plt_address + plt_offset
1514 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1516 + tlsdesc_got_offset
1517 - (plt_address + plt_offset
1521 // The .eh_frame unwind information for the PLT.
1525 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1528 'z', // Augmentation: augmentation size included.
1529 'R', // Augmentation: FDE encoding included.
1530 '\0', // End of augmentation string.
1531 1, // Code alignment factor.
1532 0x78, // Data alignment factor.
1533 16, // Return address column.
1534 1, // Augmentation size.
1535 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1536 | elfcpp::DW_EH_PE_sdata4),
1537 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1538 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1539 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1545 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1547 0, 0, 0, 0, // Replaced with offset to .plt.
1548 0, 0, 0, 0, // Replaced with size of .plt.
1549 0, // Augmentation size.
1550 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
1551 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1552 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
1553 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1554 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1555 11, // Block length.
1556 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
1557 elfcpp::DW_OP_breg16, 0, // Push %rip.
1558 elfcpp::DW_OP_lit15, // Push 0xf.
1559 elfcpp::DW_OP_and, // & (%rip & 0xf).
1560 elfcpp::DW_OP_lit11, // Push 0xb.
1561 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
1562 elfcpp::DW_OP_lit3, // Push 3.
1563 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
1564 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1565 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1571 // Write out the PLT. This uses the hand-coded instructions above,
1572 // and adjusts them as needed. This is specified by the AMD64 ABI.
1576 Output_data_plt_x86_64<size>::do_write(Output_file* of)
1578 const off_t offset = this->offset();
1579 const section_size_type oview_size =
1580 convert_to_section_size_type(this->data_size());
1581 unsigned char* const oview = of->get_output_view(offset, oview_size);
1583 const off_t got_file_offset = this->got_plt_->offset();
1584 gold_assert(parameters->incremental_update()
1585 || (got_file_offset + this->got_plt_->data_size()
1586 == this->got_irelative_->offset()));
1587 const section_size_type got_size =
1588 convert_to_section_size_type(this->got_plt_->data_size()
1589 + this->got_irelative_->data_size());
1590 unsigned char* const got_view = of->get_output_view(got_file_offset,
1593 unsigned char* pov = oview;
1595 // The base address of the .plt section.
1596 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1597 // The base address of the .got section.
1598 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
1599 // The base address of the PLT portion of the .got section,
1600 // which is where the GOT pointer will point, and where the
1601 // three reserved GOT entries are located.
1602 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1603 = this->got_plt_->address();
1605 this->fill_first_plt_entry(pov, got_address, plt_address);
1606 pov += this->get_plt_entry_size();
1608 unsigned char* got_pov = got_view;
1610 // The first entry in the GOT is the address of the .dynamic section
1611 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1612 // We saved space for them when we created the section in
1613 // Target_x86_64::got_section.
1614 Output_section* dynamic = this->layout_->dynamic_section();
1615 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1616 elfcpp::Swap<64, false>::writeval(got_pov, dynamic_addr);
1618 memset(got_pov, 0, 16);
1621 unsigned int plt_offset = this->get_plt_entry_size();
1622 unsigned int got_offset = 24;
1623 const unsigned int count = this->count_ + this->irelative_count_;
1624 for (unsigned int plt_index = 0;
1627 pov += this->get_plt_entry_size(),
1629 plt_offset += this->get_plt_entry_size(),
1632 // Set and adjust the PLT entry itself.
1633 unsigned int lazy_offset = this->fill_plt_entry(pov,
1634 got_address, plt_address,
1635 got_offset, plt_offset,
1638 // Set the entry in the GOT.
1639 elfcpp::Swap<64, false>::writeval(got_pov,
1640 plt_address + plt_offset + lazy_offset);
1643 if (this->has_tlsdesc_entry())
1645 // Set and adjust the reserved TLSDESC PLT entry.
1646 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1647 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
1648 tlsdesc_got_offset, plt_offset);
1649 pov += this->get_plt_entry_size();
1652 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1653 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1655 of->write_output_view(offset, oview_size, oview);
1656 of->write_output_view(got_file_offset, got_size, got_view);
1659 // Create the PLT section.
1663 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1665 if (this->plt_ == NULL)
1667 // Create the GOT sections first.
1668 this->got_section(symtab, layout);
1670 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
1671 this->got_irelative_);
1673 // Add unwind information if requested.
1674 if (parameters->options().ld_generated_unwind_info())
1675 this->plt_->add_eh_frame(layout);
1677 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1679 | elfcpp::SHF_EXECINSTR),
1680 this->plt_, ORDER_PLT, false);
1682 // Make the sh_info field of .rela.plt point to .plt.
1683 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1684 rela_plt_os->set_info_section(this->plt_->output_section());
1688 // Return the section for TLSDESC relocations.
1691 typename Target_x86_64<size>::Reloc_section*
1692 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
1694 return this->plt_section()->rela_tlsdesc(layout);
1697 // Create a PLT entry for a global symbol.
1701 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1704 if (gsym->has_plt_offset())
1707 if (this->plt_ == NULL)
1708 this->make_plt_section(symtab, layout);
1710 this->plt_->add_entry(symtab, layout, gsym);
1713 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1717 Target_x86_64<size>::make_local_ifunc_plt_entry(
1718 Symbol_table* symtab, Layout* layout,
1719 Sized_relobj_file<size, false>* relobj,
1720 unsigned int local_sym_index)
1722 if (relobj->local_has_plt_offset(local_sym_index))
1724 if (this->plt_ == NULL)
1725 this->make_plt_section(symtab, layout);
1726 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1729 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1732 // Return the number of entries in the PLT.
1736 Target_x86_64<size>::plt_entry_count() const
1738 if (this->plt_ == NULL)
1740 return this->plt_->entry_count();
1743 // Return the offset of the first non-reserved PLT entry.
1747 Target_x86_64<size>::first_plt_entry_offset() const
1749 return this->plt_->first_plt_entry_offset();
1752 // Return the size of each PLT entry.
1756 Target_x86_64<size>::plt_entry_size() const
1758 return this->plt_->get_plt_entry_size();
1761 // Create the GOT and PLT sections for an incremental update.
1764 Output_data_got_base*
1765 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
1767 unsigned int got_count,
1768 unsigned int plt_count)
1770 gold_assert(this->got_ == NULL);
1772 this->got_ = new Output_data_got<64, false>(got_count * 8);
1773 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1775 | elfcpp::SHF_WRITE),
1776 this->got_, ORDER_RELRO_LAST,
1779 // Add the three reserved entries.
1780 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1781 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1783 | elfcpp::SHF_WRITE),
1784 this->got_plt_, ORDER_NON_RELRO_FIRST,
1787 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1788 this->global_offset_table_ =
1789 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1790 Symbol_table::PREDEFINED,
1792 0, 0, elfcpp::STT_OBJECT,
1794 elfcpp::STV_HIDDEN, 0,
1797 // If there are any TLSDESC relocations, they get GOT entries in
1798 // .got.plt after the jump slot entries.
1799 // FIXME: Get the count for TLSDESC entries.
1800 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1801 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1802 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1804 ORDER_NON_RELRO_FIRST, false);
1806 // If there are any IRELATIVE relocations, they get GOT entries in
1807 // .got.plt after the jump slot and TLSDESC entries.
1808 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1809 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1810 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1811 this->got_irelative_,
1812 ORDER_NON_RELRO_FIRST, false);
1814 // Create the PLT section.
1815 this->plt_ = this->make_data_plt(layout, this->got_,
1817 this->got_irelative_,
1820 // Add unwind information if requested.
1821 if (parameters->options().ld_generated_unwind_info())
1822 this->plt_->add_eh_frame(layout);
1824 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1825 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1826 this->plt_, ORDER_PLT, false);
1828 // Make the sh_info field of .rela.plt point to .plt.
1829 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1830 rela_plt_os->set_info_section(this->plt_->output_section());
1832 // Create the rela_dyn section.
1833 this->rela_dyn_section(layout);
1838 // Reserve a GOT entry for a local symbol, and regenerate any
1839 // necessary dynamic relocations.
1843 Target_x86_64<size>::reserve_local_got_entry(
1844 unsigned int got_index,
1845 Sized_relobj<size, false>* obj,
1847 unsigned int got_type)
1849 unsigned int got_offset = got_index * 8;
1850 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1852 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1855 case GOT_TYPE_STANDARD:
1856 if (parameters->options().output_is_position_independent())
1857 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1858 this->got_, got_offset, 0, false);
1860 case GOT_TYPE_TLS_OFFSET:
1861 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1862 this->got_, got_offset, 0);
1864 case GOT_TYPE_TLS_PAIR:
1865 this->got_->reserve_slot(got_index + 1);
1866 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1867 this->got_, got_offset, 0);
1869 case GOT_TYPE_TLS_DESC:
1870 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1871 // this->got_->reserve_slot(got_index + 1);
1872 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1873 // this->got_, got_offset, 0);
1880 // Reserve a GOT entry for a global symbol, and regenerate any
1881 // necessary dynamic relocations.
1885 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
1887 unsigned int got_type)
1889 unsigned int got_offset = got_index * 8;
1890 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1892 this->got_->reserve_global(got_index, gsym, got_type);
1895 case GOT_TYPE_STANDARD:
1896 if (!gsym->final_value_is_known())
1898 if (gsym->is_from_dynobj()
1899 || gsym->is_undefined()
1900 || gsym->is_preemptible()
1901 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1902 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1903 this->got_, got_offset, 0);
1905 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1906 this->got_, got_offset, 0, false);
1909 case GOT_TYPE_TLS_OFFSET:
1910 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1911 this->got_, got_offset, 0, false);
1913 case GOT_TYPE_TLS_PAIR:
1914 this->got_->reserve_slot(got_index + 1);
1915 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1916 this->got_, got_offset, 0, false);
1917 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1918 this->got_, got_offset + 8, 0, false);
1920 case GOT_TYPE_TLS_DESC:
1921 this->got_->reserve_slot(got_index + 1);
1922 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1923 this->got_, got_offset, 0, false);
1930 // Register an existing PLT entry for a global symbol.
1934 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
1936 unsigned int plt_index,
1939 gold_assert(this->plt_ != NULL);
1940 gold_assert(!gsym->has_plt_offset());
1942 this->plt_->reserve_slot(plt_index);
1944 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1946 unsigned int got_offset = (plt_index + 3) * 8;
1947 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
1950 // Force a COPY relocation for a given symbol.
1954 Target_x86_64<size>::emit_copy_reloc(
1955 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1957 this->copy_relocs_.emit_copy_reloc(symtab,
1958 symtab->get_sized_symbol<size>(sym),
1961 this->rela_dyn_section(NULL));
1964 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1968 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
1971 if (this->tls_base_symbol_defined_)
1974 Output_segment* tls_segment = layout->tls_segment();
1975 if (tls_segment != NULL)
1977 bool is_exec = parameters->options().output_is_executable();
1978 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1979 Symbol_table::PREDEFINED,
1983 elfcpp::STV_HIDDEN, 0,
1985 ? Symbol::SEGMENT_END
1986 : Symbol::SEGMENT_START),
1989 this->tls_base_symbol_defined_ = true;
1992 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1996 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
1999 if (this->plt_ == NULL)
2000 this->make_plt_section(symtab, layout);
2002 if (!this->plt_->has_tlsdesc_entry())
2004 // Allocate the TLSDESC_GOT entry.
2005 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2006 unsigned int got_offset = got->add_constant(0);
2008 // Allocate the TLSDESC_PLT entry.
2009 this->plt_->reserve_tlsdesc_entry(got_offset);
2013 // Create a GOT entry for the TLS module index.
2017 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2018 Sized_relobj_file<size, false>* object)
2020 if (this->got_mod_index_offset_ == -1U)
2022 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2023 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2024 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2025 unsigned int got_offset = got->add_constant(0);
2026 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2028 got->add_constant(0);
2029 this->got_mod_index_offset_ = got_offset;
2031 return this->got_mod_index_offset_;
2034 // Optimize the TLS relocation type based on what we know about the
2035 // symbol. IS_FINAL is true if the final address of this symbol is
2036 // known at link time.
2039 tls::Tls_optimization
2040 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2042 // If we are generating a shared library, then we can't do anything
2044 if (parameters->options().shared())
2045 return tls::TLSOPT_NONE;
2049 case elfcpp::R_X86_64_TLSGD:
2050 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2051 case elfcpp::R_X86_64_TLSDESC_CALL:
2052 // These are General-Dynamic which permits fully general TLS
2053 // access. Since we know that we are generating an executable,
2054 // we can convert this to Initial-Exec. If we also know that
2055 // this is a local symbol, we can further switch to Local-Exec.
2057 return tls::TLSOPT_TO_LE;
2058 return tls::TLSOPT_TO_IE;
2060 case elfcpp::R_X86_64_TLSLD:
2061 // This is Local-Dynamic, which refers to a local symbol in the
2062 // dynamic TLS block. Since we know that we generating an
2063 // executable, we can switch to Local-Exec.
2064 return tls::TLSOPT_TO_LE;
2066 case elfcpp::R_X86_64_DTPOFF32:
2067 case elfcpp::R_X86_64_DTPOFF64:
2068 // Another Local-Dynamic reloc.
2069 return tls::TLSOPT_TO_LE;
2071 case elfcpp::R_X86_64_GOTTPOFF:
2072 // These are Initial-Exec relocs which get the thread offset
2073 // from the GOT. If we know that we are linking against the
2074 // local symbol, we can switch to Local-Exec, which links the
2075 // thread offset into the instruction.
2077 return tls::TLSOPT_TO_LE;
2078 return tls::TLSOPT_NONE;
2080 case elfcpp::R_X86_64_TPOFF32:
2081 // When we already have Local-Exec, there is nothing further we
2083 return tls::TLSOPT_NONE;
2090 // Get the Reference_flags for a particular relocation.
2094 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2098 case elfcpp::R_X86_64_NONE:
2099 case elfcpp::R_X86_64_GNU_VTINHERIT:
2100 case elfcpp::R_X86_64_GNU_VTENTRY:
2101 case elfcpp::R_X86_64_GOTPC32:
2102 case elfcpp::R_X86_64_GOTPC64:
2103 // No symbol reference.
2106 case elfcpp::R_X86_64_64:
2107 case elfcpp::R_X86_64_32:
2108 case elfcpp::R_X86_64_32S:
2109 case elfcpp::R_X86_64_16:
2110 case elfcpp::R_X86_64_8:
2111 return Symbol::ABSOLUTE_REF;
2113 case elfcpp::R_X86_64_PC64:
2114 case elfcpp::R_X86_64_PC32:
2115 case elfcpp::R_X86_64_PC32_BND:
2116 case elfcpp::R_X86_64_PC16:
2117 case elfcpp::R_X86_64_PC8:
2118 case elfcpp::R_X86_64_GOTOFF64:
2119 return Symbol::RELATIVE_REF;
2121 case elfcpp::R_X86_64_PLT32:
2122 case elfcpp::R_X86_64_PLT32_BND:
2123 case elfcpp::R_X86_64_PLTOFF64:
2124 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2126 case elfcpp::R_X86_64_GOT64:
2127 case elfcpp::R_X86_64_GOT32:
2128 case elfcpp::R_X86_64_GOTPCREL64:
2129 case elfcpp::R_X86_64_GOTPCREL:
2130 case elfcpp::R_X86_64_GOTPLT64:
2132 return Symbol::ABSOLUTE_REF;
2134 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2135 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2136 case elfcpp::R_X86_64_TLSDESC_CALL:
2137 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2138 case elfcpp::R_X86_64_DTPOFF32:
2139 case elfcpp::R_X86_64_DTPOFF64:
2140 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2141 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2142 return Symbol::TLS_REF;
2144 case elfcpp::R_X86_64_COPY:
2145 case elfcpp::R_X86_64_GLOB_DAT:
2146 case elfcpp::R_X86_64_JUMP_SLOT:
2147 case elfcpp::R_X86_64_RELATIVE:
2148 case elfcpp::R_X86_64_IRELATIVE:
2149 case elfcpp::R_X86_64_TPOFF64:
2150 case elfcpp::R_X86_64_DTPMOD64:
2151 case elfcpp::R_X86_64_TLSDESC:
2152 case elfcpp::R_X86_64_SIZE32:
2153 case elfcpp::R_X86_64_SIZE64:
2155 // Not expected. We will give an error later.
2160 // Report an unsupported relocation against a local symbol.
2164 Target_x86_64<size>::Scan::unsupported_reloc_local(
2165 Sized_relobj_file<size, false>* object,
2166 unsigned int r_type)
2168 gold_error(_("%s: unsupported reloc %u against local symbol"),
2169 object->name().c_str(), r_type);
2172 // We are about to emit a dynamic relocation of type R_TYPE. If the
2173 // dynamic linker does not support it, issue an error. The GNU linker
2174 // only issues a non-PIC error for an allocated read-only section.
2175 // Here we know the section is allocated, but we don't know that it is
2176 // read-only. But we check for all the relocation types which the
2177 // glibc dynamic linker supports, so it seems appropriate to issue an
2178 // error even if the section is not read-only. If GSYM is not NULL,
2179 // it is the symbol the relocation is against; if it is NULL, the
2180 // relocation is against a local symbol.
2184 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2189 // These are the relocation types supported by glibc for x86_64
2190 // which should always work.
2191 case elfcpp::R_X86_64_RELATIVE:
2192 case elfcpp::R_X86_64_IRELATIVE:
2193 case elfcpp::R_X86_64_GLOB_DAT:
2194 case elfcpp::R_X86_64_JUMP_SLOT:
2195 case elfcpp::R_X86_64_DTPMOD64:
2196 case elfcpp::R_X86_64_DTPOFF64:
2197 case elfcpp::R_X86_64_TPOFF64:
2198 case elfcpp::R_X86_64_64:
2199 case elfcpp::R_X86_64_COPY:
2202 // glibc supports these reloc types, but they can overflow.
2203 case elfcpp::R_X86_64_PC32:
2204 case elfcpp::R_X86_64_PC32_BND:
2205 // A PC relative reference is OK against a local symbol or if
2206 // the symbol is defined locally.
2208 || (!gsym->is_from_dynobj()
2209 && !gsym->is_undefined()
2210 && !gsym->is_preemptible()))
2213 case elfcpp::R_X86_64_32:
2214 // R_X86_64_32 is OK for x32.
2215 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2217 if (this->issued_non_pic_error_)
2219 gold_assert(parameters->options().output_is_position_independent());
2221 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2222 "overflow at runtime; recompile with -fPIC"));
2228 case elfcpp::R_X86_64_32:
2229 r_name = "R_X86_64_32";
2231 case elfcpp::R_X86_64_PC32:
2232 r_name = "R_X86_64_PC32";
2234 case elfcpp::R_X86_64_PC32_BND:
2235 r_name = "R_X86_64_PC32_BND";
2241 object->error(_("requires dynamic %s reloc against '%s' "
2242 "which may overflow at runtime; recompile "
2244 r_name, gsym->name());
2246 this->issued_non_pic_error_ = true;
2250 // This prevents us from issuing more than one error per reloc
2251 // section. But we can still wind up issuing more than one
2252 // error per object file.
2253 if (this->issued_non_pic_error_)
2255 gold_assert(parameters->options().output_is_position_independent());
2256 object->error(_("requires unsupported dynamic reloc %u; "
2257 "recompile with -fPIC"),
2259 this->issued_non_pic_error_ = true;
2262 case elfcpp::R_X86_64_NONE:
2267 // Return whether we need to make a PLT entry for a relocation of the
2268 // given type against a STT_GNU_IFUNC symbol.
2272 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2273 Sized_relobj_file<size, false>* object,
2274 unsigned int r_type)
2276 int flags = Scan::get_reference_flags(r_type);
2277 if (flags & Symbol::TLS_REF)
2278 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2279 object->name().c_str(), r_type);
2283 // Scan a relocation for a local symbol.
2287 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2289 Target_x86_64<size>* target,
2290 Sized_relobj_file<size, false>* object,
2291 unsigned int data_shndx,
2292 Output_section* output_section,
2293 const elfcpp::Rela<size, false>& reloc,
2294 unsigned int r_type,
2295 const elfcpp::Sym<size, false>& lsym,
2301 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2302 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2303 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2305 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2306 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2311 case elfcpp::R_X86_64_NONE:
2312 case elfcpp::R_X86_64_GNU_VTINHERIT:
2313 case elfcpp::R_X86_64_GNU_VTENTRY:
2316 case elfcpp::R_X86_64_64:
2317 // If building a shared library (or a position-independent
2318 // executable), we need to create a dynamic relocation for this
2319 // location. The relocation applied at link time will apply the
2320 // link-time value, so we flag the location with an
2321 // R_X86_64_RELATIVE relocation so the dynamic loader can
2322 // relocate it easily.
2323 if (parameters->options().output_is_position_independent())
2325 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2326 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2327 rela_dyn->add_local_relative(object, r_sym,
2329 ? elfcpp::R_X86_64_RELATIVE64
2330 : elfcpp::R_X86_64_RELATIVE),
2331 output_section, data_shndx,
2332 reloc.get_r_offset(),
2333 reloc.get_r_addend(), is_ifunc);
2337 case elfcpp::R_X86_64_32:
2338 case elfcpp::R_X86_64_32S:
2339 case elfcpp::R_X86_64_16:
2340 case elfcpp::R_X86_64_8:
2341 // If building a shared library (or a position-independent
2342 // executable), we need to create a dynamic relocation for this
2343 // location. We can't use an R_X86_64_RELATIVE relocation
2344 // because that is always a 64-bit relocation.
2345 if (parameters->options().output_is_position_independent())
2347 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2348 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2350 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2351 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2352 rela_dyn->add_local_relative(object, r_sym,
2353 elfcpp::R_X86_64_RELATIVE,
2354 output_section, data_shndx,
2355 reloc.get_r_offset(),
2356 reloc.get_r_addend(), is_ifunc);
2360 this->check_non_pic(object, r_type, NULL);
2362 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2363 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2364 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2365 rela_dyn->add_local(object, r_sym, r_type, output_section,
2366 data_shndx, reloc.get_r_offset(),
2367 reloc.get_r_addend());
2370 gold_assert(lsym.get_st_value() == 0);
2371 unsigned int shndx = lsym.get_st_shndx();
2373 shndx = object->adjust_sym_shndx(r_sym, shndx,
2376 object->error(_("section symbol %u has bad shndx %u"),
2379 rela_dyn->add_local_section(object, shndx,
2380 r_type, output_section,
2381 data_shndx, reloc.get_r_offset(),
2382 reloc.get_r_addend());
2387 case elfcpp::R_X86_64_PC64:
2388 case elfcpp::R_X86_64_PC32:
2389 case elfcpp::R_X86_64_PC32_BND:
2390 case elfcpp::R_X86_64_PC16:
2391 case elfcpp::R_X86_64_PC8:
2394 case elfcpp::R_X86_64_PLT32:
2395 case elfcpp::R_X86_64_PLT32_BND:
2396 // Since we know this is a local symbol, we can handle this as a
2400 case elfcpp::R_X86_64_GOTPC32:
2401 case elfcpp::R_X86_64_GOTOFF64:
2402 case elfcpp::R_X86_64_GOTPC64:
2403 case elfcpp::R_X86_64_PLTOFF64:
2404 // We need a GOT section.
2405 target->got_section(symtab, layout);
2406 // For PLTOFF64, we'd normally want a PLT section, but since we
2407 // know this is a local symbol, no PLT is needed.
2410 case elfcpp::R_X86_64_GOT64:
2411 case elfcpp::R_X86_64_GOT32:
2412 case elfcpp::R_X86_64_GOTPCREL64:
2413 case elfcpp::R_X86_64_GOTPCREL:
2414 case elfcpp::R_X86_64_GOTPLT64:
2416 // The symbol requires a GOT entry.
2417 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2418 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2420 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2421 // lets function pointers compare correctly with shared
2422 // libraries. Otherwise we would need an IRELATIVE reloc.
2425 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2427 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2430 // If we are generating a shared object, we need to add a
2431 // dynamic relocation for this symbol's GOT entry.
2432 if (parameters->options().output_is_position_independent())
2434 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2435 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2436 if (r_type != elfcpp::R_X86_64_GOT32)
2438 unsigned int got_offset =
2439 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2440 rela_dyn->add_local_relative(object, r_sym,
2441 elfcpp::R_X86_64_RELATIVE,
2442 got, got_offset, 0, is_ifunc);
2446 this->check_non_pic(object, r_type, NULL);
2448 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2449 rela_dyn->add_local(
2450 object, r_sym, r_type, got,
2451 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
2455 // For GOTPLT64, we'd normally want a PLT section, but since
2456 // we know this is a local symbol, no PLT is needed.
2460 case elfcpp::R_X86_64_COPY:
2461 case elfcpp::R_X86_64_GLOB_DAT:
2462 case elfcpp::R_X86_64_JUMP_SLOT:
2463 case elfcpp::R_X86_64_RELATIVE:
2464 case elfcpp::R_X86_64_IRELATIVE:
2465 // These are outstanding tls relocs, which are unexpected when linking
2466 case elfcpp::R_X86_64_TPOFF64:
2467 case elfcpp::R_X86_64_DTPMOD64:
2468 case elfcpp::R_X86_64_TLSDESC:
2469 gold_error(_("%s: unexpected reloc %u in object file"),
2470 object->name().c_str(), r_type);
2473 // These are initial tls relocs, which are expected when linking
2474 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2475 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2476 case elfcpp::R_X86_64_TLSDESC_CALL:
2477 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2478 case elfcpp::R_X86_64_DTPOFF32:
2479 case elfcpp::R_X86_64_DTPOFF64:
2480 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2481 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2483 bool output_is_shared = parameters->options().shared();
2484 const tls::Tls_optimization optimized_type
2485 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
2489 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2490 if (optimized_type == tls::TLSOPT_NONE)
2492 // Create a pair of GOT entries for the module index and
2493 // dtv-relative offset.
2494 Output_data_got<64, false>* got
2495 = target->got_section(symtab, layout);
2496 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2497 unsigned int shndx = lsym.get_st_shndx();
2499 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2501 object->error(_("local symbol %u has bad shndx %u"),
2504 got->add_local_pair_with_rel(object, r_sym,
2507 target->rela_dyn_section(layout),
2508 elfcpp::R_X86_64_DTPMOD64);
2510 else if (optimized_type != tls::TLSOPT_TO_LE)
2511 unsupported_reloc_local(object, r_type);
2514 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2515 target->define_tls_base_symbol(symtab, layout);
2516 if (optimized_type == tls::TLSOPT_NONE)
2518 // Create reserved PLT and GOT entries for the resolver.
2519 target->reserve_tlsdesc_entries(symtab, layout);
2521 // Generate a double GOT entry with an
2522 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2523 // is resolved lazily, so the GOT entry needs to be in
2524 // an area in .got.plt, not .got. Call got_section to
2525 // make sure the section has been created.
2526 target->got_section(symtab, layout);
2527 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2528 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2529 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2531 unsigned int got_offset = got->add_constant(0);
2532 got->add_constant(0);
2533 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2535 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2536 // We store the arguments we need in a vector, and
2537 // use the index into the vector as the parameter
2538 // to pass to the target specific routines.
2539 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
2540 void* arg = reinterpret_cast<void*>(intarg);
2541 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2542 got, got_offset, 0);
2545 else if (optimized_type != tls::TLSOPT_TO_LE)
2546 unsupported_reloc_local(object, r_type);
2549 case elfcpp::R_X86_64_TLSDESC_CALL:
2552 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2553 if (optimized_type == tls::TLSOPT_NONE)
2555 // Create a GOT entry for the module index.
2556 target->got_mod_index_entry(symtab, layout, object);
2558 else if (optimized_type != tls::TLSOPT_TO_LE)
2559 unsupported_reloc_local(object, r_type);
2562 case elfcpp::R_X86_64_DTPOFF32:
2563 case elfcpp::R_X86_64_DTPOFF64:
2566 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2567 layout->set_has_static_tls();
2568 if (optimized_type == tls::TLSOPT_NONE)
2570 // Create a GOT entry for the tp-relative offset.
2571 Output_data_got<64, false>* got
2572 = target->got_section(symtab, layout);
2573 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2574 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2575 target->rela_dyn_section(layout),
2576 elfcpp::R_X86_64_TPOFF64);
2578 else if (optimized_type != tls::TLSOPT_TO_LE)
2579 unsupported_reloc_local(object, r_type);
2582 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2583 layout->set_has_static_tls();
2584 if (output_is_shared)
2585 unsupported_reloc_local(object, r_type);
2594 case elfcpp::R_X86_64_SIZE32:
2595 case elfcpp::R_X86_64_SIZE64:
2597 gold_error(_("%s: unsupported reloc %u against local symbol"),
2598 object->name().c_str(), r_type);
2604 // Report an unsupported relocation against a global symbol.
2608 Target_x86_64<size>::Scan::unsupported_reloc_global(
2609 Sized_relobj_file<size, false>* object,
2610 unsigned int r_type,
2613 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2614 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2617 // Returns true if this relocation type could be that of a function pointer.
2620 Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
2624 case elfcpp::R_X86_64_64:
2625 case elfcpp::R_X86_64_32:
2626 case elfcpp::R_X86_64_32S:
2627 case elfcpp::R_X86_64_16:
2628 case elfcpp::R_X86_64_8:
2629 case elfcpp::R_X86_64_GOT64:
2630 case elfcpp::R_X86_64_GOT32:
2631 case elfcpp::R_X86_64_GOTPCREL64:
2632 case elfcpp::R_X86_64_GOTPCREL:
2633 case elfcpp::R_X86_64_GOTPLT64:
2641 // For safe ICF, scan a relocation for a local symbol to check if it
2642 // corresponds to a function pointer being taken. In that case mark
2643 // the function whose pointer was taken as not foldable.
2647 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
2650 Target_x86_64<size>* ,
2651 Sized_relobj_file<size, false>* ,
2654 const elfcpp::Rela<size, false>& ,
2655 unsigned int r_type,
2656 const elfcpp::Sym<size, false>&)
2658 // When building a shared library, do not fold any local symbols as it is
2659 // not possible to distinguish pointer taken versus a call by looking at
2660 // the relocation types.
2661 return (parameters->options().shared()
2662 || possible_function_pointer_reloc(r_type));
2665 // For safe ICF, scan a relocation for a global symbol to check if it
2666 // corresponds to a function pointer being taken. In that case mark
2667 // the function whose pointer was taken as not foldable.
2671 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
2674 Target_x86_64<size>* ,
2675 Sized_relobj_file<size, false>* ,
2678 const elfcpp::Rela<size, false>& ,
2679 unsigned int r_type,
2682 // When building a shared library, do not fold symbols whose visibility
2683 // is hidden, internal or protected.
2684 return ((parameters->options().shared()
2685 && (gsym->visibility() == elfcpp::STV_INTERNAL
2686 || gsym->visibility() == elfcpp::STV_PROTECTED
2687 || gsym->visibility() == elfcpp::STV_HIDDEN))
2688 || possible_function_pointer_reloc(r_type));
2691 // Scan a relocation for a global symbol.
2695 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
2697 Target_x86_64<size>* target,
2698 Sized_relobj_file<size, false>* object,
2699 unsigned int data_shndx,
2700 Output_section* output_section,
2701 const elfcpp::Rela<size, false>& reloc,
2702 unsigned int r_type,
2705 // A STT_GNU_IFUNC symbol may require a PLT entry.
2706 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2707 && this->reloc_needs_plt_for_ifunc(object, r_type))
2708 target->make_plt_entry(symtab, layout, gsym);
2712 case elfcpp::R_X86_64_NONE:
2713 case elfcpp::R_X86_64_GNU_VTINHERIT:
2714 case elfcpp::R_X86_64_GNU_VTENTRY:
2717 case elfcpp::R_X86_64_64:
2718 case elfcpp::R_X86_64_32:
2719 case elfcpp::R_X86_64_32S:
2720 case elfcpp::R_X86_64_16:
2721 case elfcpp::R_X86_64_8:
2723 // Make a PLT entry if necessary.
2724 if (gsym->needs_plt_entry())
2726 target->make_plt_entry(symtab, layout, gsym);
2727 // Since this is not a PC-relative relocation, we may be
2728 // taking the address of a function. In that case we need to
2729 // set the entry in the dynamic symbol table to the address of
2731 if (gsym->is_from_dynobj() && !parameters->options().shared())
2732 gsym->set_needs_dynsym_value();
2734 // Make a dynamic relocation if necessary.
2735 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2737 if (gsym->may_need_copy_reloc())
2739 target->copy_reloc(symtab, layout, object,
2740 data_shndx, output_section, gsym, reloc);
2742 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2743 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2744 && gsym->type() == elfcpp::STT_GNU_IFUNC
2745 && gsym->can_use_relative_reloc(false)
2746 && !gsym->is_from_dynobj()
2747 && !gsym->is_undefined()
2748 && !gsym->is_preemptible())
2750 // Use an IRELATIVE reloc for a locally defined
2751 // STT_GNU_IFUNC symbol. This makes a function
2752 // address in a PIE executable match the address in a
2753 // shared library that it links against.
2754 Reloc_section* rela_dyn =
2755 target->rela_irelative_section(layout);
2756 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2757 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2758 output_section, object,
2760 reloc.get_r_offset(),
2761 reloc.get_r_addend());
2763 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
2764 || (size == 32 && r_type == elfcpp::R_X86_64_32))
2765 && gsym->can_use_relative_reloc(false))
2767 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2768 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2769 output_section, object,
2771 reloc.get_r_offset(),
2772 reloc.get_r_addend(), false);
2776 this->check_non_pic(object, r_type, gsym);
2777 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2778 rela_dyn->add_global(gsym, r_type, output_section, object,
2779 data_shndx, reloc.get_r_offset(),
2780 reloc.get_r_addend());
2786 case elfcpp::R_X86_64_PC64:
2787 case elfcpp::R_X86_64_PC32:
2788 case elfcpp::R_X86_64_PC32_BND:
2789 case elfcpp::R_X86_64_PC16:
2790 case elfcpp::R_X86_64_PC8:
2792 // Make a PLT entry if necessary.
2793 if (gsym->needs_plt_entry())
2794 target->make_plt_entry(symtab, layout, gsym);
2795 // Make a dynamic relocation if necessary.
2796 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2798 if (gsym->may_need_copy_reloc())
2800 target->copy_reloc(symtab, layout, object,
2801 data_shndx, output_section, gsym, reloc);
2805 this->check_non_pic(object, r_type, gsym);
2806 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2807 rela_dyn->add_global(gsym, r_type, output_section, object,
2808 data_shndx, reloc.get_r_offset(),
2809 reloc.get_r_addend());
2815 case elfcpp::R_X86_64_GOT64:
2816 case elfcpp::R_X86_64_GOT32:
2817 case elfcpp::R_X86_64_GOTPCREL64:
2818 case elfcpp::R_X86_64_GOTPCREL:
2819 case elfcpp::R_X86_64_GOTPLT64:
2821 // The symbol requires a GOT entry.
2822 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2823 if (gsym->final_value_is_known())
2825 // For a STT_GNU_IFUNC symbol we want the PLT address.
2826 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2827 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2829 got->add_global(gsym, GOT_TYPE_STANDARD);
2833 // If this symbol is not fully resolved, we need to add a
2834 // dynamic relocation for it.
2835 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2837 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2839 // 1) The symbol may be defined in some other module.
2841 // 2) We are building a shared library and this is a
2842 // protected symbol; using GLOB_DAT means that the dynamic
2843 // linker can use the address of the PLT in the main
2844 // executable when appropriate so that function address
2845 // comparisons work.
2847 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2848 // code, again so that function address comparisons work.
2849 if (gsym->is_from_dynobj()
2850 || gsym->is_undefined()
2851 || gsym->is_preemptible()
2852 || (gsym->visibility() == elfcpp::STV_PROTECTED
2853 && parameters->options().shared())
2854 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2855 && parameters->options().output_is_position_independent()))
2856 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2857 elfcpp::R_X86_64_GLOB_DAT);
2860 // For a STT_GNU_IFUNC symbol we want to write the PLT
2861 // offset into the GOT, so that function pointer
2862 // comparisons work correctly.
2864 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2865 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2868 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2869 // Tell the dynamic linker to use the PLT address
2870 // when resolving relocations.
2871 if (gsym->is_from_dynobj()
2872 && !parameters->options().shared())
2873 gsym->set_needs_dynsym_value();
2877 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2878 rela_dyn->add_global_relative(gsym,
2879 elfcpp::R_X86_64_RELATIVE,
2880 got, got_off, 0, false);
2884 // For GOTPLT64, we also need a PLT entry (but only if the
2885 // symbol is not fully resolved).
2886 if (r_type == elfcpp::R_X86_64_GOTPLT64
2887 && !gsym->final_value_is_known())
2888 target->make_plt_entry(symtab, layout, gsym);
2892 case elfcpp::R_X86_64_PLT32:
2893 case elfcpp::R_X86_64_PLT32_BND:
2894 // If the symbol is fully resolved, this is just a PC32 reloc.
2895 // Otherwise we need a PLT entry.
2896 if (gsym->final_value_is_known())
2898 // If building a shared library, we can also skip the PLT entry
2899 // if the symbol is defined in the output file and is protected
2901 if (gsym->is_defined()
2902 && !gsym->is_from_dynobj()
2903 && !gsym->is_preemptible())
2905 target->make_plt_entry(symtab, layout, gsym);
2908 case elfcpp::R_X86_64_GOTPC32:
2909 case elfcpp::R_X86_64_GOTOFF64:
2910 case elfcpp::R_X86_64_GOTPC64:
2911 case elfcpp::R_X86_64_PLTOFF64:
2912 // We need a GOT section.
2913 target->got_section(symtab, layout);
2914 // For PLTOFF64, we also need a PLT entry (but only if the
2915 // symbol is not fully resolved).
2916 if (r_type == elfcpp::R_X86_64_PLTOFF64
2917 && !gsym->final_value_is_known())
2918 target->make_plt_entry(symtab, layout, gsym);
2921 case elfcpp::R_X86_64_COPY:
2922 case elfcpp::R_X86_64_GLOB_DAT:
2923 case elfcpp::R_X86_64_JUMP_SLOT:
2924 case elfcpp::R_X86_64_RELATIVE:
2925 case elfcpp::R_X86_64_IRELATIVE:
2926 // These are outstanding tls relocs, which are unexpected when linking
2927 case elfcpp::R_X86_64_TPOFF64:
2928 case elfcpp::R_X86_64_DTPMOD64:
2929 case elfcpp::R_X86_64_TLSDESC:
2930 gold_error(_("%s: unexpected reloc %u in object file"),
2931 object->name().c_str(), r_type);
2934 // These are initial tls relocs, which are expected for global()
2935 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2936 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2937 case elfcpp::R_X86_64_TLSDESC_CALL:
2938 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2939 case elfcpp::R_X86_64_DTPOFF32:
2940 case elfcpp::R_X86_64_DTPOFF64:
2941 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2942 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2944 const bool is_final = gsym->final_value_is_known();
2945 const tls::Tls_optimization optimized_type
2946 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
2949 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2950 if (optimized_type == tls::TLSOPT_NONE)
2952 // Create a pair of GOT entries for the module index and
2953 // dtv-relative offset.
2954 Output_data_got<64, false>* got
2955 = target->got_section(symtab, layout);
2956 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2957 target->rela_dyn_section(layout),
2958 elfcpp::R_X86_64_DTPMOD64,
2959 elfcpp::R_X86_64_DTPOFF64);
2961 else if (optimized_type == tls::TLSOPT_TO_IE)
2963 // Create a GOT entry for the tp-relative offset.
2964 Output_data_got<64, false>* got
2965 = target->got_section(symtab, layout);
2966 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2967 target->rela_dyn_section(layout),
2968 elfcpp::R_X86_64_TPOFF64);
2970 else if (optimized_type != tls::TLSOPT_TO_LE)
2971 unsupported_reloc_global(object, r_type, gsym);
2974 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2975 target->define_tls_base_symbol(symtab, layout);
2976 if (optimized_type == tls::TLSOPT_NONE)
2978 // Create reserved PLT and GOT entries for the resolver.
2979 target->reserve_tlsdesc_entries(symtab, layout);
2981 // Create a double GOT entry with an R_X86_64_TLSDESC
2982 // reloc. The R_X86_64_TLSDESC reloc is resolved
2983 // lazily, so the GOT entry needs to be in an area in
2984 // .got.plt, not .got. Call got_section to make sure
2985 // the section has been created.
2986 target->got_section(symtab, layout);
2987 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2988 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2989 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2990 elfcpp::R_X86_64_TLSDESC, 0);
2992 else if (optimized_type == tls::TLSOPT_TO_IE)
2994 // Create a GOT entry for the tp-relative offset.
2995 Output_data_got<64, false>* got
2996 = target->got_section(symtab, layout);
2997 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2998 target->rela_dyn_section(layout),
2999 elfcpp::R_X86_64_TPOFF64);
3001 else if (optimized_type != tls::TLSOPT_TO_LE)
3002 unsupported_reloc_global(object, r_type, gsym);
3005 case elfcpp::R_X86_64_TLSDESC_CALL:
3008 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3009 if (optimized_type == tls::TLSOPT_NONE)
3011 // Create a GOT entry for the module index.
3012 target->got_mod_index_entry(symtab, layout, object);
3014 else if (optimized_type != tls::TLSOPT_TO_LE)
3015 unsupported_reloc_global(object, r_type, gsym);
3018 case elfcpp::R_X86_64_DTPOFF32:
3019 case elfcpp::R_X86_64_DTPOFF64:
3022 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3023 layout->set_has_static_tls();
3024 if (optimized_type == tls::TLSOPT_NONE)
3026 // Create a GOT entry for the tp-relative offset.
3027 Output_data_got<64, false>* got
3028 = target->got_section(symtab, layout);
3029 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3030 target->rela_dyn_section(layout),
3031 elfcpp::R_X86_64_TPOFF64);
3033 else if (optimized_type != tls::TLSOPT_TO_LE)
3034 unsupported_reloc_global(object, r_type, gsym);
3037 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3038 layout->set_has_static_tls();
3039 if (parameters->options().shared())
3040 unsupported_reloc_global(object, r_type, gsym);
3049 case elfcpp::R_X86_64_SIZE32:
3050 case elfcpp::R_X86_64_SIZE64:
3052 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3053 object->name().c_str(), r_type,
3054 gsym->demangled_name().c_str());
3061 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3063 Sized_relobj_file<size, false>* object,
3064 unsigned int data_shndx,
3065 unsigned int sh_type,
3066 const unsigned char* prelocs,
3068 Output_section* output_section,
3069 bool needs_special_offset_handling,
3070 size_t local_symbol_count,
3071 const unsigned char* plocal_symbols)
3074 if (sh_type == elfcpp::SHT_REL)
3079 gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3080 typename Target_x86_64<size>::Scan,
3081 typename Target_x86_64<size>::Relocatable_size_for_reloc>(
3090 needs_special_offset_handling,
3095 // Scan relocations for a section.
3099 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3101 Sized_relobj_file<size, false>* object,
3102 unsigned int data_shndx,
3103 unsigned int sh_type,
3104 const unsigned char* prelocs,
3106 Output_section* output_section,
3107 bool needs_special_offset_handling,
3108 size_t local_symbol_count,
3109 const unsigned char* plocal_symbols)
3111 if (sh_type == elfcpp::SHT_REL)
3113 gold_error(_("%s: unsupported REL reloc section"),
3114 object->name().c_str());
3118 gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
3119 typename Target_x86_64<size>::Scan>(
3128 needs_special_offset_handling,
3133 // Finalize the sections.
3137 Target_x86_64<size>::do_finalize_sections(
3139 const Input_objects*,
3140 Symbol_table* symtab)
3142 const Reloc_section* rel_plt = (this->plt_ == NULL
3144 : this->plt_->rela_plt());
3145 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3146 this->rela_dyn_, true, false);
3148 // Fill in some more dynamic tags.
3149 Output_data_dynamic* const odyn = layout->dynamic_data();
3152 if (this->plt_ != NULL
3153 && this->plt_->output_section() != NULL
3154 && this->plt_->has_tlsdesc_entry())
3156 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3157 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3158 this->got_->finalize_data_size();
3159 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3160 this->plt_, plt_offset);
3161 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3162 this->got_, got_offset);
3166 // Emit any relocs we saved in an attempt to avoid generating COPY
3168 if (this->copy_relocs_.any_saved_relocs())
3169 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3171 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3172 // the .got.plt section.
3173 Symbol* sym = this->global_offset_table_;
3176 uint64_t data_size = this->got_plt_->current_data_size();
3177 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3180 if (parameters->doing_static_link()
3181 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3183 // If linking statically, make sure that the __rela_iplt symbols
3184 // were defined if necessary, even if we didn't create a PLT.
3185 static const Define_symbol_in_segment syms[] =
3188 "__rela_iplt_start", // name
3189 elfcpp::PT_LOAD, // segment_type
3190 elfcpp::PF_W, // segment_flags_set
3191 elfcpp::PF(0), // segment_flags_clear
3194 elfcpp::STT_NOTYPE, // type
3195 elfcpp::STB_GLOBAL, // binding
3196 elfcpp::STV_HIDDEN, // visibility
3198 Symbol::SEGMENT_START, // offset_from_base
3202 "__rela_iplt_end", // name
3203 elfcpp::PT_LOAD, // segment_type
3204 elfcpp::PF_W, // segment_flags_set
3205 elfcpp::PF(0), // segment_flags_clear
3208 elfcpp::STT_NOTYPE, // type
3209 elfcpp::STB_GLOBAL, // binding
3210 elfcpp::STV_HIDDEN, // visibility
3212 Symbol::SEGMENT_START, // offset_from_base
3217 symtab->define_symbols(layout, 2, syms,
3218 layout->script_options()->saw_sections_clause());
3222 // Perform a relocation.
3226 Target_x86_64<size>::Relocate::relocate(
3227 const Relocate_info<size, false>* relinfo,
3228 Target_x86_64<size>* target,
3231 const elfcpp::Rela<size, false>& rela,
3232 unsigned int r_type,
3233 const Sized_symbol<size>* gsym,
3234 const Symbol_value<size>* psymval,
3235 unsigned char* view,
3236 typename elfcpp::Elf_types<size>::Elf_Addr address,
3237 section_size_type view_size)
3239 if (this->skip_call_tls_get_addr_)
3241 if ((r_type != elfcpp::R_X86_64_PLT32
3242 && r_type != elfcpp::R_X86_64_PLT32_BND
3243 && r_type != elfcpp::R_X86_64_PC32_BND
3244 && r_type != elfcpp::R_X86_64_PC32)
3246 || strcmp(gsym->name(), "__tls_get_addr") != 0)
3248 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3249 _("missing expected TLS relocation"));
3253 this->skip_call_tls_get_addr_ = false;
3261 const Sized_relobj_file<size, false>* object = relinfo->object;
3263 // Pick the value to use for symbols defined in the PLT.
3264 Symbol_value<size> symval;
3266 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3268 symval.set_output_value(target->plt_address_for_global(gsym));
3271 else if (gsym == NULL && psymval->is_ifunc_symbol())
3273 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3274 if (object->local_has_plt_offset(r_sym))
3276 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3281 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3283 // Get the GOT offset if needed.
3284 // The GOT pointer points to the end of the GOT section.
3285 // We need to subtract the size of the GOT section to get
3286 // the actual offset to use in the relocation.
3287 bool have_got_offset = false;
3288 unsigned int got_offset = 0;
3291 case elfcpp::R_X86_64_GOT32:
3292 case elfcpp::R_X86_64_GOT64:
3293 case elfcpp::R_X86_64_GOTPLT64:
3294 case elfcpp::R_X86_64_GOTPCREL:
3295 case elfcpp::R_X86_64_GOTPCREL64:
3298 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3299 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
3303 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3304 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3305 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3306 - target->got_size());
3308 have_got_offset = true;
3317 case elfcpp::R_X86_64_NONE:
3318 case elfcpp::R_X86_64_GNU_VTINHERIT:
3319 case elfcpp::R_X86_64_GNU_VTENTRY:
3322 case elfcpp::R_X86_64_64:
3323 Relocate_functions<size, false>::rela64(view, object, psymval, addend);
3326 case elfcpp::R_X86_64_PC64:
3327 Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
3331 case elfcpp::R_X86_64_32:
3332 // FIXME: we need to verify that value + addend fits into 32 bits:
3333 // uint64_t x = value + addend;
3334 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3335 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3336 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3339 case elfcpp::R_X86_64_32S:
3340 // FIXME: we need to verify that value + addend fits into 32 bits:
3341 // int64_t x = value + addend; // note this quantity is signed!
3342 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3343 Relocate_functions<size, false>::rela32(view, object, psymval, addend);
3346 case elfcpp::R_X86_64_PC32:
3347 case elfcpp::R_X86_64_PC32_BND:
3348 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3352 case elfcpp::R_X86_64_16:
3353 Relocate_functions<size, false>::rela16(view, object, psymval, addend);
3356 case elfcpp::R_X86_64_PC16:
3357 Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
3361 case elfcpp::R_X86_64_8:
3362 Relocate_functions<size, false>::rela8(view, object, psymval, addend);
3365 case elfcpp::R_X86_64_PC8:
3366 Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
3370 case elfcpp::R_X86_64_PLT32:
3371 case elfcpp::R_X86_64_PLT32_BND:
3372 gold_assert(gsym == NULL
3373 || gsym->has_plt_offset()
3374 || gsym->final_value_is_known()
3375 || (gsym->is_defined()
3376 && !gsym->is_from_dynobj()
3377 && !gsym->is_preemptible()));
3378 // Note: while this code looks the same as for R_X86_64_PC32, it
3379 // behaves differently because psymval was set to point to
3380 // the PLT entry, rather than the symbol, in Scan::global().
3381 Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
3385 case elfcpp::R_X86_64_PLTOFF64:
3388 gold_assert(gsym->has_plt_offset()
3389 || gsym->final_value_is_known());
3390 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
3391 got_address = target->got_section(NULL, NULL)->address();
3392 Relocate_functions<size, false>::rela64(view, object, psymval,
3393 addend - got_address);
3396 case elfcpp::R_X86_64_GOT32:
3397 gold_assert(have_got_offset);
3398 Relocate_functions<size, false>::rela32(view, got_offset, addend);
3401 case elfcpp::R_X86_64_GOTPC32:
3404 typename elfcpp::Elf_types<size>::Elf_Addr value;
3405 value = target->got_plt_section()->address();
3406 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3410 case elfcpp::R_X86_64_GOT64:
3411 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3412 // Since we always add a PLT entry, this is equivalent.
3413 case elfcpp::R_X86_64_GOTPLT64:
3414 gold_assert(have_got_offset);
3415 Relocate_functions<size, false>::rela64(view, got_offset, addend);
3418 case elfcpp::R_X86_64_GOTPC64:
3421 typename elfcpp::Elf_types<size>::Elf_Addr value;
3422 value = target->got_plt_section()->address();
3423 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3427 case elfcpp::R_X86_64_GOTOFF64:
3429 typename elfcpp::Elf_types<size>::Elf_Addr value;
3430 value = (psymval->value(object, 0)
3431 - target->got_plt_section()->address());
3432 Relocate_functions<size, false>::rela64(view, value, addend);
3436 case elfcpp::R_X86_64_GOTPCREL:
3438 gold_assert(have_got_offset);
3439 typename elfcpp::Elf_types<size>::Elf_Addr value;
3440 value = target->got_plt_section()->address() + got_offset;
3441 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3445 case elfcpp::R_X86_64_GOTPCREL64:
3447 gold_assert(have_got_offset);
3448 typename elfcpp::Elf_types<size>::Elf_Addr value;
3449 value = target->got_plt_section()->address() + got_offset;
3450 Relocate_functions<size, false>::pcrela64(view, value, addend, address);
3454 case elfcpp::R_X86_64_COPY:
3455 case elfcpp::R_X86_64_GLOB_DAT:
3456 case elfcpp::R_X86_64_JUMP_SLOT:
3457 case elfcpp::R_X86_64_RELATIVE:
3458 case elfcpp::R_X86_64_IRELATIVE:
3459 // These are outstanding tls relocs, which are unexpected when linking
3460 case elfcpp::R_X86_64_TPOFF64:
3461 case elfcpp::R_X86_64_DTPMOD64:
3462 case elfcpp::R_X86_64_TLSDESC:
3463 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3464 _("unexpected reloc %u in object file"),
3468 // These are initial tls relocs, which are expected when linking
3469 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3470 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3471 case elfcpp::R_X86_64_TLSDESC_CALL:
3472 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3473 case elfcpp::R_X86_64_DTPOFF32:
3474 case elfcpp::R_X86_64_DTPOFF64:
3475 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3476 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3477 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3478 view, address, view_size);
3481 case elfcpp::R_X86_64_SIZE32:
3482 case elfcpp::R_X86_64_SIZE64:
3484 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3485 _("unsupported reloc %u"),
3493 // Perform a TLS relocation.
3497 Target_x86_64<size>::Relocate::relocate_tls(
3498 const Relocate_info<size, false>* relinfo,
3499 Target_x86_64<size>* target,
3501 const elfcpp::Rela<size, false>& rela,
3502 unsigned int r_type,
3503 const Sized_symbol<size>* gsym,
3504 const Symbol_value<size>* psymval,
3505 unsigned char* view,
3506 typename elfcpp::Elf_types<size>::Elf_Addr address,
3507 section_size_type view_size)
3509 Output_segment* tls_segment = relinfo->layout->tls_segment();
3511 const Sized_relobj_file<size, false>* object = relinfo->object;
3512 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3513 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
3514 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
3516 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
3518 const bool is_final = (gsym == NULL
3519 ? !parameters->options().shared()
3520 : gsym->final_value_is_known());
3521 tls::Tls_optimization optimized_type
3522 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3525 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3526 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3528 // If this code sequence is used in a non-executable section,
3529 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3530 // on the assumption that it's being used by itself in a debug
3531 // section. Therefore, in the unlikely event that the code
3532 // sequence appears in a non-executable section, we simply
3533 // leave it unoptimized.
3534 optimized_type = tls::TLSOPT_NONE;
3536 if (optimized_type == tls::TLSOPT_TO_LE)
3538 if (tls_segment == NULL)
3540 gold_assert(parameters->errors()->error_count() > 0
3541 || issue_undefined_symbol_error(gsym));
3544 this->tls_gd_to_le(relinfo, relnum, tls_segment,
3545 rela, r_type, value, view,
3551 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3552 ? GOT_TYPE_TLS_OFFSET
3553 : GOT_TYPE_TLS_PAIR);
3554 unsigned int got_offset;
3557 gold_assert(gsym->has_got_offset(got_type));
3558 got_offset = gsym->got_offset(got_type) - target->got_size();
3562 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3563 gold_assert(object->local_has_got_offset(r_sym, got_type));
3564 got_offset = (object->local_got_offset(r_sym, got_type)
3565 - target->got_size());
3567 if (optimized_type == tls::TLSOPT_TO_IE)
3569 value = target->got_plt_section()->address() + got_offset;
3570 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
3571 value, view, address, view_size);
3574 else if (optimized_type == tls::TLSOPT_NONE)
3576 // Relocate the field with the offset of the pair of GOT
3578 value = target->got_plt_section()->address() + got_offset;
3579 Relocate_functions<size, false>::pcrela32(view, value, addend,
3584 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3585 _("unsupported reloc %u"), r_type);
3588 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3589 case elfcpp::R_X86_64_TLSDESC_CALL:
3590 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3592 // See above comment for R_X86_64_TLSGD.
3593 optimized_type = tls::TLSOPT_NONE;
3595 if (optimized_type == tls::TLSOPT_TO_LE)
3597 if (tls_segment == NULL)
3599 gold_assert(parameters->errors()->error_count() > 0
3600 || issue_undefined_symbol_error(gsym));
3603 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3604 rela, r_type, value, view,
3610 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3611 ? GOT_TYPE_TLS_OFFSET
3612 : GOT_TYPE_TLS_DESC);
3613 unsigned int got_offset = 0;
3614 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
3615 && optimized_type == tls::TLSOPT_NONE)
3617 // We created GOT entries in the .got.tlsdesc portion of
3618 // the .got.plt section, but the offset stored in the
3619 // symbol is the offset within .got.tlsdesc.
3620 got_offset = (target->got_size()
3621 + target->got_plt_section()->data_size());
3625 gold_assert(gsym->has_got_offset(got_type));
3626 got_offset += gsym->got_offset(got_type) - target->got_size();
3630 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3631 gold_assert(object->local_has_got_offset(r_sym, got_type));
3632 got_offset += (object->local_got_offset(r_sym, got_type)
3633 - target->got_size());
3635 if (optimized_type == tls::TLSOPT_TO_IE)
3637 if (tls_segment == NULL)
3639 gold_assert(parameters->errors()->error_count() > 0
3640 || issue_undefined_symbol_error(gsym));
3643 value = target->got_plt_section()->address() + got_offset;
3644 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
3645 rela, r_type, value, view, address,
3649 else if (optimized_type == tls::TLSOPT_NONE)
3651 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3653 // Relocate the field with the offset of the pair of GOT
3655 value = target->got_plt_section()->address() + got_offset;
3656 Relocate_functions<size, false>::pcrela32(view, value, addend,
3662 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3663 _("unsupported reloc %u"), r_type);
3666 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3667 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
3669 // See above comment for R_X86_64_TLSGD.
3670 optimized_type = tls::TLSOPT_NONE;
3672 if (optimized_type == tls::TLSOPT_TO_LE)
3674 if (tls_segment == NULL)
3676 gold_assert(parameters->errors()->error_count() > 0
3677 || issue_undefined_symbol_error(gsym));
3680 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
3681 value, view, view_size);
3684 else if (optimized_type == tls::TLSOPT_NONE)
3686 // Relocate the field with the offset of the GOT entry for
3687 // the module index.
3688 unsigned int got_offset;
3689 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3690 - target->got_size());
3691 value = target->got_plt_section()->address() + got_offset;
3692 Relocate_functions<size, false>::pcrela32(view, value, addend,
3696 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3697 _("unsupported reloc %u"), r_type);
3700 case elfcpp::R_X86_64_DTPOFF32:
3701 // This relocation type is used in debugging information.
3702 // In that case we need to not optimize the value. If the
3703 // section is not executable, then we assume we should not
3704 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3705 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3707 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3709 if (tls_segment == NULL)
3711 gold_assert(parameters->errors()->error_count() > 0
3712 || issue_undefined_symbol_error(gsym));
3715 value -= tls_segment->memsz();
3717 Relocate_functions<size, false>::rela32(view, value, addend);
3720 case elfcpp::R_X86_64_DTPOFF64:
3721 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3722 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3724 if (tls_segment == NULL)
3726 gold_assert(parameters->errors()->error_count() > 0
3727 || issue_undefined_symbol_error(gsym));
3730 value -= tls_segment->memsz();
3732 Relocate_functions<size, false>::rela64(view, value, addend);
3735 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3736 if (optimized_type == tls::TLSOPT_TO_LE)
3738 if (tls_segment == NULL)
3740 gold_assert(parameters->errors()->error_count() > 0
3741 || issue_undefined_symbol_error(gsym));
3744 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3746 r_type, value, view,
3750 else if (optimized_type == tls::TLSOPT_NONE)
3752 // Relocate the field with the offset of the GOT entry for
3753 // the tp-relative offset of the symbol.
3754 unsigned int got_offset;
3757 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3758 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3759 - target->got_size());
3763 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3764 gold_assert(object->local_has_got_offset(r_sym,
3765 GOT_TYPE_TLS_OFFSET));
3766 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3767 - target->got_size());
3769 value = target->got_plt_section()->address() + got_offset;
3770 Relocate_functions<size, false>::pcrela32(view, value, addend,
3774 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3775 _("unsupported reloc type %u"),
3779 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3780 if (tls_segment == NULL)
3782 gold_assert(parameters->errors()->error_count() > 0
3783 || issue_undefined_symbol_error(gsym));
3786 value -= tls_segment->memsz();
3787 Relocate_functions<size, false>::rela32(view, value, addend);
3792 // Do a relocation in which we convert a TLS General-Dynamic to an
3797 Target_x86_64<size>::Relocate::tls_gd_to_ie(
3798 const Relocate_info<size, false>* relinfo,
3801 const elfcpp::Rela<size, false>& rela,
3803 typename elfcpp::Elf_types<size>::Elf_Addr value,
3804 unsigned char* view,
3805 typename elfcpp::Elf_types<size>::Elf_Addr address,
3806 section_size_type view_size)
3809 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3810 // .word 0x6666; rex64; call __tls_get_addr
3811 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3813 // leaq foo@tlsgd(%rip),%rdi;
3814 // .word 0x6666; rex64; call __tls_get_addr
3815 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3817 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3818 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3819 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3823 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3825 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3826 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3827 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3832 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3834 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3835 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
3836 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3840 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3841 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
3844 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3846 this->skip_call_tls_get_addr_ = true;
3849 // Do a relocation in which we convert a TLS General-Dynamic to a
3854 Target_x86_64<size>::Relocate::tls_gd_to_le(
3855 const Relocate_info<size, false>* relinfo,
3857 Output_segment* tls_segment,
3858 const elfcpp::Rela<size, false>& rela,
3860 typename elfcpp::Elf_types<size>::Elf_Addr value,
3861 unsigned char* view,
3862 section_size_type view_size)
3865 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3866 // .word 0x6666; rex64; call __tls_get_addr
3867 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3869 // leaq foo@tlsgd(%rip),%rdi;
3870 // .word 0x6666; rex64; call __tls_get_addr
3871 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
3873 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3874 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3875 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3879 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3881 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3882 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3883 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3888 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
3890 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3891 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
3893 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3897 value -= tls_segment->memsz();
3898 Relocate_functions<size, false>::rela32(view + 8, value, 0);
3900 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3902 this->skip_call_tls_get_addr_ = true;
3905 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3909 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
3910 const Relocate_info<size, false>* relinfo,
3913 const elfcpp::Rela<size, false>& rela,
3914 unsigned int r_type,
3915 typename elfcpp::Elf_types<size>::Elf_Addr value,
3916 unsigned char* view,
3917 typename elfcpp::Elf_types<size>::Elf_Addr address,
3918 section_size_type view_size)
3920 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3922 // leaq foo@tlsdesc(%rip), %rax
3923 // ==> movq foo@gottpoff(%rip), %rax
3924 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3925 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3926 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3927 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3929 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3930 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
3934 // call *foo@tlscall(%rax)
3936 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3937 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3938 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3939 view[0] == 0xff && view[1] == 0x10);
3945 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3949 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
3950 const Relocate_info<size, false>* relinfo,
3952 Output_segment* tls_segment,
3953 const elfcpp::Rela<size, false>& rela,
3954 unsigned int r_type,
3955 typename elfcpp::Elf_types<size>::Elf_Addr value,
3956 unsigned char* view,
3957 section_size_type view_size)
3959 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3961 // leaq foo@tlsdesc(%rip), %rax
3962 // ==> movq foo@tpoff, %rax
3963 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3964 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3965 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3966 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3969 value -= tls_segment->memsz();
3970 Relocate_functions<size, false>::rela32(view, value, 0);
3974 // call *foo@tlscall(%rax)
3976 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3977 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3978 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3979 view[0] == 0xff && view[1] == 0x10);
3987 Target_x86_64<size>::Relocate::tls_ld_to_le(
3988 const Relocate_info<size, false>* relinfo,
3991 const elfcpp::Rela<size, false>& rela,
3993 typename elfcpp::Elf_types<size>::Elf_Addr,
3994 unsigned char* view,
3995 section_size_type view_size)
3997 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3999 // ... leq foo@dtpoff(%rax),%reg
4000 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4002 // ... leq foo@dtpoff(%rax),%reg
4003 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4005 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4006 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4008 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4009 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4011 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
4014 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4016 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4018 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4020 this->skip_call_tls_get_addr_ = true;
4023 // Do a relocation in which we convert a TLS Initial-Exec to a
4028 Target_x86_64<size>::Relocate::tls_ie_to_le(
4029 const Relocate_info<size, false>* relinfo,
4031 Output_segment* tls_segment,
4032 const elfcpp::Rela<size, false>& rela,
4034 typename elfcpp::Elf_types<size>::Elf_Addr value,
4035 unsigned char* view,
4036 section_size_type view_size)
4038 // We need to examine the opcodes to figure out which instruction we
4041 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4042 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4044 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4045 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4047 unsigned char op1 = view[-3];
4048 unsigned char op2 = view[-2];
4049 unsigned char op3 = view[-1];
4050 unsigned char reg = op3 >> 3;
4058 view[-1] = 0xc0 | reg;
4062 // Special handling for %rsp.
4066 view[-1] = 0xc0 | reg;
4074 view[-1] = 0x80 | reg | (reg << 3);
4077 value -= tls_segment->memsz();
4078 Relocate_functions<size, false>::rela32(view, value, 0);
4081 // Relocate section data.
4085 Target_x86_64<size>::relocate_section(
4086 const Relocate_info<size, false>* relinfo,
4087 unsigned int sh_type,
4088 const unsigned char* prelocs,
4090 Output_section* output_section,
4091 bool needs_special_offset_handling,
4092 unsigned char* view,
4093 typename elfcpp::Elf_types<size>::Elf_Addr address,
4094 section_size_type view_size,
4095 const Reloc_symbol_changes* reloc_symbol_changes)
4097 gold_assert(sh_type == elfcpp::SHT_RELA);
4099 gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
4100 typename Target_x86_64<size>::Relocate,
4101 gold::Default_comdat_behavior>(
4107 needs_special_offset_handling,
4111 reloc_symbol_changes);
4114 // Apply an incremental relocation. Incremental relocations always refer
4115 // to global symbols.
4119 Target_x86_64<size>::apply_relocation(
4120 const Relocate_info<size, false>* relinfo,
4121 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4122 unsigned int r_type,
4123 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4125 unsigned char* view,
4126 typename elfcpp::Elf_types<size>::Elf_Addr address,
4127 section_size_type view_size)
4129 gold::apply_relocation<size, false, Target_x86_64<size>,
4130 typename Target_x86_64<size>::Relocate>(
4142 // Return the size of a relocation while scanning during a relocatable
4147 Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
4148 unsigned int r_type,
4153 case elfcpp::R_X86_64_NONE:
4154 case elfcpp::R_X86_64_GNU_VTINHERIT:
4155 case elfcpp::R_X86_64_GNU_VTENTRY:
4156 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4157 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4158 case elfcpp::R_X86_64_TLSDESC_CALL:
4159 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4160 case elfcpp::R_X86_64_DTPOFF32:
4161 case elfcpp::R_X86_64_DTPOFF64:
4162 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4163 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4166 case elfcpp::R_X86_64_64:
4167 case elfcpp::R_X86_64_PC64:
4168 case elfcpp::R_X86_64_GOTOFF64:
4169 case elfcpp::R_X86_64_GOTPC64:
4170 case elfcpp::R_X86_64_PLTOFF64:
4171 case elfcpp::R_X86_64_GOT64:
4172 case elfcpp::R_X86_64_GOTPCREL64:
4173 case elfcpp::R_X86_64_GOTPCREL:
4174 case elfcpp::R_X86_64_GOTPLT64:
4177 case elfcpp::R_X86_64_32:
4178 case elfcpp::R_X86_64_32S:
4179 case elfcpp::R_X86_64_PC32:
4180 case elfcpp::R_X86_64_PC32_BND:
4181 case elfcpp::R_X86_64_PLT32:
4182 case elfcpp::R_X86_64_PLT32_BND:
4183 case elfcpp::R_X86_64_GOTPC32:
4184 case elfcpp::R_X86_64_GOT32:
4187 case elfcpp::R_X86_64_16:
4188 case elfcpp::R_X86_64_PC16:
4191 case elfcpp::R_X86_64_8:
4192 case elfcpp::R_X86_64_PC8:
4195 case elfcpp::R_X86_64_COPY:
4196 case elfcpp::R_X86_64_GLOB_DAT:
4197 case elfcpp::R_X86_64_JUMP_SLOT:
4198 case elfcpp::R_X86_64_RELATIVE:
4199 case elfcpp::R_X86_64_IRELATIVE:
4200 // These are outstanding tls relocs, which are unexpected when linking
4201 case elfcpp::R_X86_64_TPOFF64:
4202 case elfcpp::R_X86_64_DTPMOD64:
4203 case elfcpp::R_X86_64_TLSDESC:
4204 object->error(_("unexpected reloc %u in object file"), r_type);
4207 case elfcpp::R_X86_64_SIZE32:
4208 case elfcpp::R_X86_64_SIZE64:
4210 object->error(_("unsupported reloc %u against local symbol"), r_type);
4215 // Scan the relocs during a relocatable link.
4219 Target_x86_64<size>::scan_relocatable_relocs(
4220 Symbol_table* symtab,
4222 Sized_relobj_file<size, false>* object,
4223 unsigned int data_shndx,
4224 unsigned int sh_type,
4225 const unsigned char* prelocs,
4227 Output_section* output_section,
4228 bool needs_special_offset_handling,
4229 size_t local_symbol_count,
4230 const unsigned char* plocal_symbols,
4231 Relocatable_relocs* rr)
4233 gold_assert(sh_type == elfcpp::SHT_RELA);
4235 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4236 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4238 gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
4239 Scan_relocatable_relocs>(
4247 needs_special_offset_handling,
4253 // Relocate a section during a relocatable link.
4257 Target_x86_64<size>::relocate_relocs(
4258 const Relocate_info<size, false>* relinfo,
4259 unsigned int sh_type,
4260 const unsigned char* prelocs,
4262 Output_section* output_section,
4263 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4264 const Relocatable_relocs* rr,
4265 unsigned char* view,
4266 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4267 section_size_type view_size,
4268 unsigned char* reloc_view,
4269 section_size_type reloc_view_size)
4271 gold_assert(sh_type == elfcpp::SHT_RELA);
4273 gold::relocate_relocs<size, false, elfcpp::SHT_RELA>(
4278 offset_in_output_section,
4287 // Return the value to use for a dynamic which requires special
4288 // treatment. This is how we support equality comparisons of function
4289 // pointers across shared library boundaries, as described in the
4290 // processor specific ABI supplement.
4294 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
4296 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4297 return this->plt_address_for_global(gsym);
4300 // Return a string used to fill a code section with nops to take up
4301 // the specified length.
4305 Target_x86_64<size>::do_code_fill(section_size_type length) const
4309 // Build a jmpq instruction to skip over the bytes.
4310 unsigned char jmp[5];
4312 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
4313 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
4314 + std::string(length - 5, static_cast<char>(0x90)));
4317 // Nop sequences of various lengths.
4318 const char nop1[1] = { '\x90' }; // nop
4319 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
4320 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4321 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4323 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4325 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4326 '\x44', '\x00', '\x00' };
4327 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4328 '\x00', '\x00', '\x00',
4330 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4331 '\x00', '\x00', '\x00',
4333 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4334 '\x84', '\x00', '\x00',
4335 '\x00', '\x00', '\x00' };
4336 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4337 '\x1f', '\x84', '\x00',
4338 '\x00', '\x00', '\x00',
4340 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
4341 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4342 '\x00', '\x00', '\x00',
4344 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
4345 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4346 '\x84', '\x00', '\x00',
4347 '\x00', '\x00', '\x00' };
4348 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4349 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4350 '\x1f', '\x84', '\x00',
4351 '\x00', '\x00', '\x00',
4353 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4354 '\x66', '\x66', '\x2e', // data16
4355 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4356 '\x00', '\x00', '\x00',
4358 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4359 '\x66', '\x66', '\x66', // data16; data16
4360 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4361 '\x84', '\x00', '\x00',
4362 '\x00', '\x00', '\x00' };
4364 const char* nops[16] = {
4366 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
4367 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
4370 return std::string(nops[length], length);
4373 // Return the addend to use for a target specific relocation. The
4374 // only target specific relocation is R_X86_64_TLSDESC for a local
4375 // symbol. We want to set the addend is the offset of the local
4376 // symbol in the TLS segment.
4380 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
4383 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
4384 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4385 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4386 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4387 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4388 gold_assert(psymval->is_tls_symbol());
4389 // The value of a TLS symbol is the offset in the TLS segment.
4390 return psymval->value(ti.object, 0);
4393 // Return the value to use for the base of a DW_EH_PE_datarel offset
4394 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4395 // assembler can not write out the difference between two labels in
4396 // different sections, so instead of using a pc-relative value they
4397 // use an offset from the GOT.
4401 Target_x86_64<size>::do_ehframe_datarel_base() const
4403 gold_assert(this->global_offset_table_ != NULL);
4404 Symbol* sym = this->global_offset_table_;
4405 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4406 return ssym->value();
4409 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4410 // compiled with -fsplit-stack. The function calls non-split-stack
4411 // code. We have to change the function so that it always ensures
4412 // that it has enough stack space to run some random function.
4416 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4417 section_offset_type fnoffset,
4418 section_size_type fnsize,
4419 unsigned char* view,
4420 section_size_type view_size,
4422 std::string* to) const
4424 // The function starts with a comparison of the stack pointer and a
4425 // field in the TCB. This is followed by a jump.
4428 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
4431 // We will call __morestack if the carry flag is set after this
4432 // comparison. We turn the comparison into an stc instruction
4434 view[fnoffset] = '\xf9';
4435 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
4437 // lea NN(%rsp),%r10
4438 // lea NN(%rsp),%r11
4439 else if ((this->match_view(view, view_size, fnoffset,
4440 "\x4c\x8d\x94\x24", 4)
4441 || this->match_view(view, view_size, fnoffset,
4442 "\x4c\x8d\x9c\x24", 4))
4445 // This is loading an offset from the stack pointer for a
4446 // comparison. The offset is negative, so we decrease the
4447 // offset by the amount of space we need for the stack. This
4448 // means we will avoid calling __morestack if there happens to
4449 // be plenty of space on the stack already.
4450 unsigned char* pval = view + fnoffset + 4;
4451 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4452 val -= parameters->options().split_stack_adjust_size();
4453 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4457 if (!object->has_no_split_stack())
4458 object->error(_("failed to match split-stack sequence at "
4459 "section %u offset %0zx"),
4460 shndx, static_cast<size_t>(fnoffset));
4464 // We have to change the function so that it calls
4465 // __morestack_non_split instead of __morestack. The former will
4466 // allocate additional stack space.
4467 *from = "__morestack";
4468 *to = "__morestack_non_split";
4471 // The selector for x86_64 object files. Note this is never instantiated
4472 // directly. It's only used in Target_selector_x86_64_nacl, below.
4475 class Target_selector_x86_64 : public Target_selector_freebsd
4478 Target_selector_x86_64()
4479 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
4481 ? "elf64-x86-64" : "elf32-x86-64"),
4483 ? "elf64-x86-64-freebsd"
4484 : "elf32-x86-64-freebsd"),
4485 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
4489 do_instantiate_target()
4490 { return new Target_x86_64<size>(); }
4494 // NaCl variant. It uses different PLT contents.
4497 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
4500 Output_data_plt_x86_64_nacl(Layout* layout,
4501 Output_data_got<64, false>* got,
4502 Output_data_space* got_plt,
4503 Output_data_space* got_irelative)
4504 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4505 got, got_plt, got_irelative)
4508 Output_data_plt_x86_64_nacl(Layout* layout,
4509 Output_data_got<64, false>* got,
4510 Output_data_space* got_plt,
4511 Output_data_space* got_irelative,
4512 unsigned int plt_count)
4513 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
4514 got, got_plt, got_irelative,
4519 virtual unsigned int
4520 do_get_plt_entry_size() const
4521 { return plt_entry_size; }
4524 do_add_eh_frame(Layout* layout)
4526 layout->add_eh_frame_for_plt(this,
4527 this->plt_eh_frame_cie,
4528 this->plt_eh_frame_cie_size,
4530 plt_eh_frame_fde_size);
4534 do_fill_first_plt_entry(unsigned char* pov,
4535 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
4536 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
4538 virtual unsigned int
4539 do_fill_plt_entry(unsigned char* pov,
4540 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4541 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4542 unsigned int got_offset,
4543 unsigned int plt_offset,
4544 unsigned int plt_index);
4547 do_fill_tlsdesc_entry(unsigned char* pov,
4548 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4549 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4550 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4551 unsigned int tlsdesc_got_offset,
4552 unsigned int plt_offset);
4555 // The size of an entry in the PLT.
4556 static const int plt_entry_size = 64;
4558 // The first entry in the PLT.
4559 static const unsigned char first_plt_entry[plt_entry_size];
4561 // Other entries in the PLT for an executable.
4562 static const unsigned char plt_entry[plt_entry_size];
4564 // The reserved TLSDESC entry in the PLT for an executable.
4565 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
4567 // The .eh_frame unwind information for the PLT.
4568 static const int plt_eh_frame_fde_size = 32;
4569 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4573 class Target_x86_64_nacl : public Target_x86_64<size>
4576 Target_x86_64_nacl()
4577 : Target_x86_64<size>(&x86_64_nacl_info)
4580 virtual Output_data_plt_x86_64<size>*
4581 do_make_data_plt(Layout* layout,
4582 Output_data_got<64, false>* got,
4583 Output_data_space* got_plt,
4584 Output_data_space* got_irelative)
4586 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4590 virtual Output_data_plt_x86_64<size>*
4591 do_make_data_plt(Layout* layout,
4592 Output_data_got<64, false>* got,
4593 Output_data_space* got_plt,
4594 Output_data_space* got_irelative,
4595 unsigned int plt_count)
4597 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
4603 do_code_fill(section_size_type length) const;
4606 static const Target::Target_info x86_64_nacl_info;
4610 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
4613 false, // is_big_endian
4614 elfcpp::EM_X86_64, // machine_code
4615 false, // has_make_symbol
4616 false, // has_resolve
4617 true, // has_code_fill
4618 true, // is_default_stack_executable
4619 true, // can_icf_inline_merge_sections
4621 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4622 0x20000, // default_text_segment_address
4623 0x10000, // abi_pagesize (overridable by -z max-page-size)
4624 0x10000, // common_pagesize (overridable by -z common-page-size)
4625 true, // isolate_execinstr
4626 0x10000000, // rosegment_gap
4627 elfcpp::SHN_UNDEF, // small_common_shndx
4628 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4629 0, // small_common_section_flags
4630 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4631 NULL, // attributes_section
4632 NULL, // attributes_vendor
4633 "_start" // entry_symbol_name
4637 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
4640 false, // is_big_endian
4641 elfcpp::EM_X86_64, // machine_code
4642 false, // has_make_symbol
4643 false, // has_resolve
4644 true, // has_code_fill
4645 true, // is_default_stack_executable
4646 true, // can_icf_inline_merge_sections
4648 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4649 0x20000, // default_text_segment_address
4650 0x10000, // abi_pagesize (overridable by -z max-page-size)
4651 0x10000, // common_pagesize (overridable by -z common-page-size)
4652 true, // isolate_execinstr
4653 0x10000000, // rosegment_gap
4654 elfcpp::SHN_UNDEF, // small_common_shndx
4655 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
4656 0, // small_common_section_flags
4657 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
4658 NULL, // attributes_section
4659 NULL, // attributes_vendor
4660 "_start" // entry_symbol_name
4663 #define NACLMASK 0xe0 // 32-byte alignment mask.
4665 // The first entry in the PLT.
4669 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
4671 0xff, 0x35, // pushq contents of memory address
4672 0, 0, 0, 0, // replaced with address of .got + 8
4673 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4674 0, 0, 0, 0, // replaced with address of .got + 16
4675 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4676 0x4d, 0x01, 0xfb, // add %r15, %r11
4677 0x41, 0xff, 0xe3, // jmpq *%r11
4679 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4680 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4682 // 32 bytes of nop to pad out to the standard size
4683 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4684 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4685 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4686 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4687 0x66, // excess data32 prefix
4693 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
4695 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4696 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
4698 memcpy(pov, first_plt_entry, plt_entry_size);
4699 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4701 - (plt_address + 2 + 4)));
4702 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4704 - (plt_address + 9 + 4)));
4707 // Subsequent entries in the PLT.
4711 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
4713 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4714 0, 0, 0, 0, // replaced with address of symbol in .got
4715 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4716 0x4d, 0x01, 0xfb, // add %r15, %r11
4717 0x41, 0xff, 0xe3, // jmpq *%r11
4719 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4720 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4721 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4723 // Lazy GOT entries point here (32-byte aligned).
4724 0x68, // pushq immediate
4725 0, 0, 0, 0, // replaced with index into relocation table
4726 0xe9, // jmp relative
4727 0, 0, 0, 0, // replaced with offset to start of .plt0
4729 // 22 bytes of nop to pad out to the standard size.
4730 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4731 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4732 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4737 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
4739 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4740 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4741 unsigned int got_offset,
4742 unsigned int plt_offset,
4743 unsigned int plt_index)
4745 memcpy(pov, plt_entry, plt_entry_size);
4746 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
4747 (got_address + got_offset
4748 - (plt_address + plt_offset
4751 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
4752 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
4753 - (plt_offset + 38 + 4));
4758 // The reserved TLSDESC entry in the PLT.
4762 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
4764 0xff, 0x35, // pushq x(%rip)
4765 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4766 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4767 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4768 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
4769 0x4d, 0x01, 0xfb, // add %r15, %r11
4770 0x41, 0xff, 0xe3, // jmpq *%r11
4772 // 41 bytes of nop to pad out to the standard size.
4773 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4774 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4775 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4776 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4777 0x66, 0x66, // excess data32 prefixes
4778 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4783 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
4785 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
4786 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
4787 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
4788 unsigned int tlsdesc_got_offset,
4789 unsigned int plt_offset)
4791 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
4792 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4794 - (plt_address + plt_offset
4796 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
4798 + tlsdesc_got_offset
4799 - (plt_address + plt_offset
4803 // The .eh_frame unwind information for the PLT.
4807 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4809 0, 0, 0, 0, // Replaced with offset to .plt.
4810 0, 0, 0, 0, // Replaced with size of .plt.
4811 0, // Augmentation size.
4812 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
4813 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
4814 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
4815 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
4816 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
4817 13, // Block length.
4818 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
4819 elfcpp::DW_OP_breg16, 0, // Push %rip.
4820 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
4821 elfcpp::DW_OP_and, // & (%rip & 0x3f).
4822 elfcpp::DW_OP_const1u, 37, // Push 0x25.
4823 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
4824 elfcpp::DW_OP_lit3, // Push 3.
4825 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
4826 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
4827 elfcpp::DW_CFA_nop, // Align to 32 bytes.
4831 // Return a string used to fill a code section with nops.
4832 // For NaCl, long NOPs are only valid if they do not cross
4833 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4836 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
4838 return std::string(length, static_cast<char>(0x90));
4841 // The selector for x86_64-nacl object files.
4844 class Target_selector_x86_64_nacl
4845 : public Target_selector_nacl<Target_selector_x86_64<size>,
4846 Target_x86_64_nacl<size> >
4849 Target_selector_x86_64_nacl()
4850 : Target_selector_nacl<Target_selector_x86_64<size>,
4851 Target_x86_64_nacl<size> >("x86-64",
4853 ? "elf64-x86-64-nacl"
4854 : "elf32-x86-64-nacl",
4857 : "elf32_x86_64_nacl")
4861 Target_selector_x86_64_nacl<64> target_selector_x86_64;
4862 Target_selector_x86_64_nacl<32> target_selector_x32;
4864 } // End anonymous namespace.