Updated Russian translation for the bfd directory
[binutils-gdb.git] / gold / x86_64.cc
blob928dfa8ea34cd2fd86486a268bf3844e1874e2ed
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2023 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.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "x86_64.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "freebsd.h"
42 #include "nacl.h"
43 #include "gc.h"
44 #include "icf.h"
46 namespace
49 using namespace gold;
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64 : public Output_section_data_build
55 public:
56 Output_data_got_plt_x86_64(Layout* layout)
57 : Output_section_data_build(8),
58 layout_(layout)
59 { }
61 Output_data_got_plt_x86_64(Layout* layout, off_t data_size)
62 : Output_section_data_build(data_size, 8),
63 layout_(layout)
64 { }
66 protected:
67 // Write out the PLT data.
68 void
69 do_write(Output_file*);
71 // Write to a map file.
72 void
73 do_print_to_mapfile(Mapfile* mapfile) const
74 { mapfile->print_output_data(this, "** GOT PLT"); }
76 private:
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
79 Layout* layout_;
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
87 template<int size>
88 class Output_data_plt_x86_64 : public Output_section_data
90 public:
91 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
93 Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
94 Output_data_got<64, false>* got,
95 Output_data_got_plt_x86_64* got_plt,
96 Output_data_space* got_irelative)
97 : Output_section_data(addralign), tlsdesc_rel_(NULL),
98 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
99 got_irelative_(got_irelative), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout); }
103 Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
104 Output_data_got<64, false>* got,
105 Output_data_got_plt_x86_64* got_plt,
106 Output_data_space* got_irelative,
107 unsigned int plt_count)
108 : Output_section_data((plt_count + 1) * plt_entry_size,
109 plt_entry_size, false),
110 tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
111 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
114 this->init(layout);
116 // Initialize the free list and reserve the first entry.
117 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
118 this->free_list_.remove(0, plt_entry_size);
121 // Initialize the PLT section.
122 void
123 init(Layout* layout);
125 // Add an entry to the PLT.
126 void
127 add_entry(Symbol_table*, Layout*, Symbol* gsym);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
130 unsigned int
131 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
132 Sized_relobj_file<size, false>* relobj,
133 unsigned int local_sym_index);
135 // Add the relocation for a PLT entry.
136 void
137 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
138 unsigned int got_offset);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
141 void
142 reserve_tlsdesc_entry(unsigned int got_offset)
143 { this->tlsdesc_got_offset_ = got_offset; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
146 bool
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_ != -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
151 unsigned int
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
156 unsigned int
157 get_tlsdesc_plt_offset() const
159 return ((this->count_ + this->irelative_count_ + 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
164 Reloc_section*
165 rela_plt()
166 { return this->rel_; }
168 // Return where the TLSDESC relocations should go.
169 Reloc_section*
170 rela_tlsdesc(Layout*);
172 // Return where the IRELATIVE relocations should go in the PLT
173 // relocations.
174 Reloc_section*
175 rela_irelative(Symbol_table*, Layout*);
177 // Return whether we created a section for IRELATIVE relocations.
178 bool
179 has_irelative_section() const
180 { return this->irelative_rel_ != NULL; }
182 // Get count of regular PLT entries.
183 unsigned int
184 regular_count() const
185 { return this->count_; }
187 // Return the total number of PLT entries.
188 unsigned int
189 entry_count() const
190 { return this->count_ + this->irelative_count_; }
192 // Return the offset of the first non-reserved PLT entry.
193 unsigned int
194 first_plt_entry_offset()
195 { return this->get_plt_entry_size(); }
197 // Return the size of a PLT entry.
198 unsigned int
199 get_plt_entry_size() const
200 { return this->do_get_plt_entry_size(); }
202 // Reserve a slot in the PLT for an existing symbol in an incremental update.
203 void
204 reserve_slot(unsigned int plt_index)
206 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
207 (plt_index + 2) * this->get_plt_entry_size());
210 // Return the PLT address to use for a global symbol.
211 uint64_t
212 address_for_global(const Symbol* sym)
213 { return do_address_for_global(sym); }
215 // Return the PLT address to use for a local symbol.
216 uint64_t
217 address_for_local(const Relobj* obj, unsigned int symndx)
218 { return do_address_for_local(obj, symndx); }
220 // Add .eh_frame information for the PLT.
221 void
222 add_eh_frame(Layout* layout)
223 { this->do_add_eh_frame(layout); }
225 protected:
226 Output_data_got<64, false>*
227 got() const
228 { return this->got_; }
230 Output_data_got_plt_x86_64*
231 got_plt() const
232 { return this->got_plt_; }
234 Output_data_space*
235 got_irelative() const
236 { return this->got_irelative_; }
238 // Fill in the first PLT entry.
239 void
240 fill_first_plt_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 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
245 // Fill in a normal PLT entry. Returns the offset into the entry that
246 // should be the initial GOT slot value.
247 unsigned int
248 fill_plt_entry(unsigned char* pov,
249 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
250 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
251 unsigned int got_offset,
252 unsigned int plt_offset,
253 unsigned int plt_index)
255 return this->do_fill_plt_entry(pov, got_address, plt_address,
256 got_offset, plt_offset, plt_index);
259 // Fill in the reserved TLSDESC PLT entry.
260 void
261 fill_tlsdesc_entry(unsigned char* pov,
262 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
263 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
264 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
265 unsigned int tlsdesc_got_offset,
266 unsigned int plt_offset)
268 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
269 tlsdesc_got_offset, plt_offset);
272 virtual unsigned int
273 do_get_plt_entry_size() const = 0;
275 virtual void
276 do_fill_first_plt_entry(unsigned char* pov,
277 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
278 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
279 = 0;
281 virtual unsigned int
282 do_fill_plt_entry(unsigned char* pov,
283 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
284 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
285 unsigned int got_offset,
286 unsigned int plt_offset,
287 unsigned int plt_index) = 0;
289 virtual void
290 do_fill_tlsdesc_entry(unsigned char* pov,
291 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
292 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
293 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
294 unsigned int tlsdesc_got_offset,
295 unsigned int plt_offset) = 0;
297 // Return the PLT address to use for a global symbol.
298 virtual uint64_t
299 do_address_for_global(const Symbol* sym);
301 // Return the PLT address to use for a local symbol.
302 virtual uint64_t
303 do_address_for_local(const Relobj* obj, unsigned int symndx);
305 virtual void
306 do_add_eh_frame(Layout* layout) = 0;
308 void
309 do_adjust_output_section(Output_section* os);
311 // Write to a map file.
312 void
313 do_print_to_mapfile(Mapfile* mapfile) const
314 { mapfile->print_output_data(this, _("** PLT")); }
316 // The CIE of the .eh_frame unwind information for the PLT.
317 static const int plt_eh_frame_cie_size = 16;
318 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
320 private:
321 // Set the final size.
322 void
323 set_final_data_size();
325 // Write out the PLT data.
326 void
327 do_write(Output_file*);
329 // The reloc section.
330 Reloc_section* rel_;
331 // The TLSDESC relocs, if necessary. These must follow the regular
332 // PLT relocs.
333 Reloc_section* tlsdesc_rel_;
334 // The IRELATIVE relocs, if necessary. These must follow the
335 // regular PLT relocations and the TLSDESC relocations.
336 Reloc_section* irelative_rel_;
337 // The .got section.
338 Output_data_got<64, false>* got_;
339 // The .got.plt section.
340 Output_data_got_plt_x86_64* got_plt_;
341 // The part of the .got.plt section used for IRELATIVE relocs.
342 Output_data_space* got_irelative_;
343 // The number of PLT entries.
344 unsigned int count_;
345 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
346 // follow the regular PLT entries.
347 unsigned int irelative_count_;
348 // Offset of the reserved TLSDESC_GOT entry when needed.
349 unsigned int tlsdesc_got_offset_;
350 // List of available regions within the section, for incremental
351 // update links.
352 Free_list free_list_;
355 template<int size>
356 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
358 public:
359 Output_data_plt_x86_64_standard(Layout* layout,
360 Output_data_got<64, false>* got,
361 Output_data_got_plt_x86_64* got_plt,
362 Output_data_space* got_irelative)
363 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
364 got, got_plt, got_irelative)
367 Output_data_plt_x86_64_standard(Layout* layout,
368 Output_data_got<64, false>* got,
369 Output_data_got_plt_x86_64* got_plt,
370 Output_data_space* got_irelative,
371 unsigned int plt_count)
372 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
373 got, got_plt, got_irelative,
374 plt_count)
377 protected:
378 virtual unsigned int
379 do_get_plt_entry_size() const
380 { return plt_entry_size; }
382 virtual void
383 do_add_eh_frame(Layout* layout)
385 layout->add_eh_frame_for_plt(this,
386 this->plt_eh_frame_cie,
387 this->plt_eh_frame_cie_size,
388 plt_eh_frame_fde,
389 plt_eh_frame_fde_size);
392 virtual void
393 do_fill_first_plt_entry(unsigned char* pov,
394 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
395 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
397 virtual unsigned int
398 do_fill_plt_entry(unsigned char* pov,
399 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
400 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
401 unsigned int got_offset,
402 unsigned int plt_offset,
403 unsigned int plt_index);
405 virtual void
406 do_fill_tlsdesc_entry(unsigned char* pov,
407 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
408 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
409 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
410 unsigned int tlsdesc_got_offset,
411 unsigned int plt_offset);
413 private:
414 // The size of an entry in the PLT.
415 static const int plt_entry_size = 16;
417 // The first entry in the PLT.
418 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
419 // procedure linkage table for both programs and shared objects."
420 static const unsigned char first_plt_entry[plt_entry_size];
422 // Other entries in the PLT for an executable.
423 static const unsigned char plt_entry[plt_entry_size];
425 // The reserved TLSDESC entry in the PLT for an executable.
426 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
428 // The .eh_frame unwind information for the PLT.
429 static const int plt_eh_frame_fde_size = 32;
430 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
433 // We use this PLT when Indirect Branch Tracking (IBT) is enabled.
435 template <int size>
436 class Output_data_plt_x86_64_ibt : public Output_data_plt_x86_64<size>
438 public:
439 Output_data_plt_x86_64_ibt(Layout* layout,
440 Output_data_got<64, false>* got,
441 Output_data_got_plt_x86_64* got_plt,
442 Output_data_space* got_irelative)
443 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
444 got, got_plt, got_irelative),
445 aplt_offset_(0)
448 Output_data_plt_x86_64_ibt(Layout* layout,
449 Output_data_got<64, false>* got,
450 Output_data_got_plt_x86_64* got_plt,
451 Output_data_space* got_irelative,
452 unsigned int plt_count)
453 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
454 got, got_plt, got_irelative,
455 plt_count),
456 aplt_offset_(0)
459 protected:
460 virtual unsigned int
461 do_get_plt_entry_size() const
462 { return plt_entry_size; }
464 // Return the PLT address to use for a global symbol.
465 uint64_t
466 do_address_for_global(const Symbol*);
468 // Return the PLT address to use for a local symbol.
469 uint64_t
470 do_address_for_local(const Relobj*, unsigned int symndx);
472 virtual void
473 do_add_eh_frame(Layout* layout)
475 layout->add_eh_frame_for_plt(this,
476 this->plt_eh_frame_cie,
477 this->plt_eh_frame_cie_size,
478 plt_eh_frame_fde,
479 plt_eh_frame_fde_size);
482 virtual void
483 do_fill_first_plt_entry(unsigned char* pov,
484 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
485 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
487 virtual unsigned int
488 do_fill_plt_entry(unsigned char* pov,
489 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
490 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
491 unsigned int got_offset,
492 unsigned int plt_offset,
493 unsigned int plt_index);
495 virtual void
496 do_fill_tlsdesc_entry(unsigned char* pov,
497 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
498 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
499 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
500 unsigned int tlsdesc_got_offset,
501 unsigned int plt_offset);
503 void
504 fill_aplt_entry(unsigned char* pov,
505 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
506 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
507 unsigned int got_offset,
508 unsigned int plt_offset,
509 unsigned int plt_index);
511 private:
512 // Set the final size.
513 void
514 set_final_data_size();
516 // Write out the PLT data.
517 void
518 do_write(Output_file*);
520 // Offset of the Additional PLT (if using -z bndplt).
521 unsigned int aplt_offset_;
523 // The size of an entry in the PLT.
524 static const int plt_entry_size = 16;
526 // The size of an entry in the additional PLT.
527 static const int aplt_entry_size = 16;
529 // The first entry in the PLT.
530 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
531 // procedure linkage table for both programs and shared objects."
532 static const unsigned char first_plt_entry[plt_entry_size];
534 // Other entries in the PLT for an executable.
535 static const unsigned char plt_entry[plt_entry_size];
537 // Entries in the additional PLT.
538 static const unsigned char aplt_entry[aplt_entry_size];
540 // The reserved TLSDESC entry in the PLT for an executable.
541 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
543 // The .eh_frame unwind information for the PLT.
544 static const int plt_eh_frame_fde_size = 32;
545 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
548 template<int size>
549 class Lazy_view
551 public:
552 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
553 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
556 inline unsigned char
557 operator[](size_t offset)
559 if (this->view_ == NULL)
560 this->view_ = this->object_->section_contents(this->data_shndx_,
561 &this->view_size_,
562 true);
563 if (offset >= this->view_size_)
564 return 0;
565 return this->view_[offset];
568 private:
569 Sized_relobj_file<size, false>* object_;
570 unsigned int data_shndx_;
571 const unsigned char* view_;
572 section_size_type view_size_;
575 // The x86_64 target class.
576 // See the ABI at
577 // http://www.x86-64.org/documentation/abi.pdf
578 // TLS info comes from
579 // http://people.redhat.com/drepper/tls.pdf
580 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
582 template<int size>
583 class Target_x86_64 : public Sized_target<size, false>
585 public:
586 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
587 // uses only Elf64_Rela relocation entries with explicit addends."
588 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
590 Target_x86_64(const Target::Target_info* info = &x86_64_info)
591 : Sized_target<size, false>(info),
592 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
593 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
594 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
595 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
596 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
597 feature_1_(0), feature_2_used_(0), feature_2_needed_(0),
598 object_isa_1_used_(0), object_feature_1_(0),
599 object_feature_2_used_(0), seen_first_object_(false)
602 // Hook for a new output section.
603 void
604 do_new_output_section(Output_section*) const;
606 // Scan the relocations to look for symbol adjustments.
607 void
608 gc_process_relocs(Symbol_table* symtab,
609 Layout* layout,
610 Sized_relobj_file<size, false>* object,
611 unsigned int data_shndx,
612 unsigned int sh_type,
613 const unsigned char* prelocs,
614 size_t reloc_count,
615 Output_section* output_section,
616 bool needs_special_offset_handling,
617 size_t local_symbol_count,
618 const unsigned char* plocal_symbols);
620 // Scan the relocations to look for symbol adjustments.
621 void
622 scan_relocs(Symbol_table* symtab,
623 Layout* layout,
624 Sized_relobj_file<size, false>* object,
625 unsigned int data_shndx,
626 unsigned int sh_type,
627 const unsigned char* prelocs,
628 size_t reloc_count,
629 Output_section* output_section,
630 bool needs_special_offset_handling,
631 size_t local_symbol_count,
632 const unsigned char* plocal_symbols);
634 // Finalize the sections.
635 void
636 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
638 // Return the value to use for a dynamic which requires special
639 // treatment.
640 uint64_t
641 do_dynsym_value(const Symbol*) const;
643 // Relocate a section.
644 void
645 relocate_section(const Relocate_info<size, false>*,
646 unsigned int sh_type,
647 const unsigned char* prelocs,
648 size_t reloc_count,
649 Output_section* output_section,
650 bool needs_special_offset_handling,
651 unsigned char* view,
652 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
653 section_size_type view_size,
654 const Reloc_symbol_changes*);
656 // Scan the relocs during a relocatable link.
657 void
658 scan_relocatable_relocs(Symbol_table* symtab,
659 Layout* layout,
660 Sized_relobj_file<size, false>* object,
661 unsigned int data_shndx,
662 unsigned int sh_type,
663 const unsigned char* prelocs,
664 size_t reloc_count,
665 Output_section* output_section,
666 bool needs_special_offset_handling,
667 size_t local_symbol_count,
668 const unsigned char* plocal_symbols,
669 Relocatable_relocs*);
671 // Scan the relocs for --emit-relocs.
672 void
673 emit_relocs_scan(Symbol_table* symtab,
674 Layout* layout,
675 Sized_relobj_file<size, false>* object,
676 unsigned int data_shndx,
677 unsigned int sh_type,
678 const unsigned char* prelocs,
679 size_t reloc_count,
680 Output_section* output_section,
681 bool needs_special_offset_handling,
682 size_t local_symbol_count,
683 const unsigned char* plocal_syms,
684 Relocatable_relocs* rr);
686 // Emit relocations for a section.
687 void
688 relocate_relocs(
689 const Relocate_info<size, false>*,
690 unsigned int sh_type,
691 const unsigned char* prelocs,
692 size_t reloc_count,
693 Output_section* output_section,
694 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
695 unsigned char* view,
696 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
697 section_size_type view_size,
698 unsigned char* reloc_view,
699 section_size_type reloc_view_size);
701 // Return a string used to fill a code section with nops.
702 std::string
703 do_code_fill(section_size_type length) const;
705 // Return whether SYM is defined by the ABI.
706 bool
707 do_is_defined_by_abi(const Symbol* sym) const
708 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
710 // Return the symbol index to use for a target specific relocation.
711 // The only target specific relocation is R_X86_64_TLSDESC for a
712 // local symbol, which is an absolute reloc.
713 unsigned int
714 do_reloc_symbol_index(void*, unsigned int r_type) const
716 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
717 return 0;
720 // Return the addend to use for a target specific relocation.
721 uint64_t
722 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
724 // Return the PLT section.
725 uint64_t
726 do_plt_address_for_global(const Symbol* gsym) const
727 { return this->plt_section()->address_for_global(gsym); }
729 uint64_t
730 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
731 { return this->plt_section()->address_for_local(relobj, symndx); }
733 // This function should be defined in targets that can use relocation
734 // types to determine (implemented in local_reloc_may_be_function_pointer
735 // and global_reloc_may_be_function_pointer)
736 // if a function's pointer is taken. ICF uses this in safe mode to only
737 // fold those functions whose pointer is defintely not taken. For x86_64
738 // pie binaries, safe ICF cannot be done by looking at only relocation
739 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
740 // opcode is checked as well to distinguish a function call from taking
741 // a function's pointer.
742 bool
743 do_can_check_for_function_pointers() const
744 { return true; }
746 // Return the base for a DW_EH_PE_datarel encoding.
747 uint64_t
748 do_ehframe_datarel_base() const;
750 // Adjust -fsplit-stack code which calls non-split-stack code.
751 void
752 do_calls_non_split(Relobj* object, unsigned int shndx,
753 section_offset_type fnoffset, section_size_type fnsize,
754 const unsigned char* prelocs, size_t reloc_count,
755 unsigned char* view, section_size_type view_size,
756 std::string* from, std::string* to) const;
758 // Return the size of the GOT section.
759 section_size_type
760 got_size() const
762 gold_assert(this->got_ != NULL);
763 return this->got_->data_size();
766 // Return the number of entries in the GOT.
767 unsigned int
768 got_entry_count() const
770 if (this->got_ == NULL)
771 return 0;
772 return this->got_size() / 8;
775 // Return the number of entries in the PLT.
776 unsigned int
777 plt_entry_count() const;
779 // Return the offset of the first non-reserved PLT entry.
780 unsigned int
781 first_plt_entry_offset() const;
783 // Return the size of each PLT entry.
784 unsigned int
785 plt_entry_size() const;
787 // Return the size of each GOT entry.
788 unsigned int
789 got_entry_size() const
790 { return 8; };
792 // Create the GOT section for an incremental update.
793 Output_data_got_base*
794 init_got_plt_for_update(Symbol_table* symtab,
795 Layout* layout,
796 unsigned int got_count,
797 unsigned int plt_count);
799 // Reserve a GOT entry for a local symbol, and regenerate any
800 // necessary dynamic relocations.
801 void
802 reserve_local_got_entry(unsigned int got_index,
803 Sized_relobj<size, false>* obj,
804 unsigned int r_sym,
805 unsigned int got_type);
807 // Reserve a GOT entry for a global symbol, and regenerate any
808 // necessary dynamic relocations.
809 void
810 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
811 unsigned int got_type);
813 // Register an existing PLT entry for a global symbol.
814 void
815 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
816 Symbol* gsym);
818 // Force a COPY relocation for a given symbol.
819 void
820 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
822 // Apply an incremental relocation.
823 void
824 apply_relocation(const Relocate_info<size, false>* relinfo,
825 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
826 unsigned int r_type,
827 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
828 const Symbol* gsym,
829 unsigned char* view,
830 typename elfcpp::Elf_types<size>::Elf_Addr address,
831 section_size_type view_size);
833 // Add a new reloc argument, returning the index in the vector.
834 size_t
835 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
837 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
838 return this->tlsdesc_reloc_info_.size() - 1;
841 Output_data_plt_x86_64<size>*
842 make_data_plt(Layout* layout,
843 Output_data_got<64, false>* got,
844 Output_data_got_plt_x86_64* got_plt,
845 Output_data_space* got_irelative)
847 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
850 Output_data_plt_x86_64<size>*
851 make_data_plt(Layout* layout,
852 Output_data_got<64, false>* got,
853 Output_data_got_plt_x86_64* got_plt,
854 Output_data_space* got_irelative,
855 unsigned int plt_count)
857 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
858 plt_count);
861 virtual Output_data_plt_x86_64<size>*
862 do_make_data_plt(Layout* layout,
863 Output_data_got<64, false>* got,
864 Output_data_got_plt_x86_64* got_plt,
865 Output_data_space* got_irelative);
867 virtual Output_data_plt_x86_64<size>*
868 do_make_data_plt(Layout* layout,
869 Output_data_got<64, false>* got,
870 Output_data_got_plt_x86_64* got_plt,
871 Output_data_space* got_irelative,
872 unsigned int plt_count);
874 private:
875 // The class which scans relocations.
876 class Scan
878 public:
879 Scan()
880 : issued_non_pic_error_(false)
883 static inline int
884 get_reference_flags(unsigned int r_type);
886 inline void
887 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
888 Sized_relobj_file<size, false>* object,
889 unsigned int data_shndx,
890 Output_section* output_section,
891 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
892 const elfcpp::Sym<size, false>& lsym,
893 bool is_discarded);
895 inline void
896 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
897 Sized_relobj_file<size, false>* object,
898 unsigned int data_shndx,
899 Output_section* output_section,
900 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
901 Symbol* gsym);
903 inline bool
904 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
905 Target_x86_64* target,
906 Sized_relobj_file<size, false>* object,
907 unsigned int data_shndx,
908 Output_section* output_section,
909 const elfcpp::Rela<size, false>& reloc,
910 unsigned int r_type,
911 const elfcpp::Sym<size, false>& lsym);
913 inline bool
914 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
915 Target_x86_64* target,
916 Sized_relobj_file<size, false>* object,
917 unsigned int data_shndx,
918 Output_section* output_section,
919 const elfcpp::Rela<size, false>& reloc,
920 unsigned int r_type,
921 Symbol* gsym);
923 private:
924 static void
925 unsupported_reloc_local(Sized_relobj_file<size, false>*,
926 unsigned int r_type);
928 static void
929 unsupported_reloc_global(Sized_relobj_file<size, false>*,
930 unsigned int r_type, Symbol*);
932 void
933 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
935 inline bool
936 possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
937 unsigned int src_indx,
938 unsigned int r_offset,
939 unsigned int r_type);
941 bool
942 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
943 unsigned int r_type);
945 // Whether we have issued an error about a non-PIC compilation.
946 bool issued_non_pic_error_;
949 // The class which implements relocation.
950 class Relocate
952 public:
953 Relocate()
954 : skip_call_tls_get_addr_(false)
957 ~Relocate()
959 if (this->skip_call_tls_get_addr_)
961 // FIXME: This needs to specify the location somehow.
962 gold_error(_("missing expected TLS relocation"));
966 // Do a relocation. Return false if the caller should not issue
967 // any warnings about this relocation.
968 inline bool
969 relocate(const Relocate_info<size, false>*, unsigned int,
970 Target_x86_64*, Output_section*, size_t, const unsigned char*,
971 const Sized_symbol<size>*, const Symbol_value<size>*,
972 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
973 section_size_type);
975 private:
976 // Do a TLS relocation.
977 inline void
978 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
979 size_t relnum, const elfcpp::Rela<size, false>&,
980 unsigned int r_type, const Sized_symbol<size>*,
981 const Symbol_value<size>*,
982 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
983 section_size_type);
985 // Do a TLS General-Dynamic to Initial-Exec transition.
986 inline void
987 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
988 const elfcpp::Rela<size, false>&, unsigned int r_type,
989 typename elfcpp::Elf_types<size>::Elf_Addr value,
990 unsigned char* view,
991 typename elfcpp::Elf_types<size>::Elf_Addr,
992 section_size_type view_size);
994 // Do a TLS General-Dynamic to Local-Exec transition.
995 inline void
996 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
997 Output_segment* tls_segment,
998 const elfcpp::Rela<size, false>&, unsigned int r_type,
999 typename elfcpp::Elf_types<size>::Elf_Addr value,
1000 unsigned char* view,
1001 section_size_type view_size);
1003 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1004 inline void
1005 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1006 const elfcpp::Rela<size, false>&, unsigned int r_type,
1007 typename elfcpp::Elf_types<size>::Elf_Addr value,
1008 unsigned char* view,
1009 typename elfcpp::Elf_types<size>::Elf_Addr,
1010 section_size_type view_size);
1012 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1013 inline void
1014 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1015 Output_segment* tls_segment,
1016 const elfcpp::Rela<size, false>&, unsigned int r_type,
1017 typename elfcpp::Elf_types<size>::Elf_Addr value,
1018 unsigned char* view,
1019 section_size_type view_size);
1021 // Do a TLS Local-Dynamic to Local-Exec transition.
1022 inline void
1023 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
1024 Output_segment* tls_segment,
1025 const elfcpp::Rela<size, false>&, unsigned int r_type,
1026 typename elfcpp::Elf_types<size>::Elf_Addr value,
1027 unsigned char* view,
1028 section_size_type view_size);
1030 // Do a TLS Initial-Exec to Local-Exec transition.
1031 static inline void
1032 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
1033 Output_segment* tls_segment,
1034 const elfcpp::Rela<size, false>&, unsigned int r_type,
1035 typename elfcpp::Elf_types<size>::Elf_Addr value,
1036 unsigned char* view,
1037 section_size_type view_size);
1039 // This is set if we should skip the next reloc, which should be a
1040 // PLT32 reloc against ___tls_get_addr.
1041 bool skip_call_tls_get_addr_;
1044 // Check if relocation against this symbol is a candidate for
1045 // conversion from
1046 // mov foo@GOTPCREL(%rip), %reg
1047 // to lea foo(%rip), %reg.
1048 template<class View_type>
1049 static inline bool
1050 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
1051 size_t r_offset, View_type* view)
1053 gold_assert(gsym != NULL);
1054 // We cannot do the conversion unless it's one of these relocations.
1055 if (r_type != elfcpp::R_X86_64_GOTPCREL
1056 && r_type != elfcpp::R_X86_64_GOTPCRELX
1057 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
1058 return false;
1059 // We cannot convert references to IFUNC symbols, or to symbols that
1060 // are not local to the current module.
1061 // We can't do predefined symbols because they may become undefined
1062 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1063 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1064 || gsym->is_undefined()
1065 || gsym->is_predefined()
1066 || gsym->is_from_dynobj()
1067 || gsym->is_preemptible())
1068 return false;
1069 // If we are building a shared object and the symbol is protected, we may
1070 // need to go through the GOT.
1071 if (parameters->options().shared()
1072 && gsym->visibility() == elfcpp::STV_PROTECTED)
1073 return false;
1074 // We cannot convert references to the _DYNAMIC symbol.
1075 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1076 return false;
1077 // Check for a MOV opcode.
1078 return (*view)[r_offset - 2] == 0x8b;
1081 // Convert
1082 // callq *foo@GOTPCRELX(%rip) to
1083 // addr32 callq foo
1084 // and jmpq *foo@GOTPCRELX(%rip) to
1085 // jmpq foo
1086 // nop
1087 template<class View_type>
1088 static inline bool
1089 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1090 size_t r_offset, View_type* view)
1092 gold_assert(gsym != NULL);
1093 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1094 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1095 return false;
1096 // We cannot convert references to IFUNC symbols, or to symbols that
1097 // are not local to the current module.
1098 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1099 || gsym->is_undefined ()
1100 || gsym->is_from_dynobj()
1101 || gsym->is_preemptible())
1102 return false;
1103 // Check for a CALLQ or JMPQ opcode.
1104 return ((*view)[r_offset - 2] == 0xff
1105 && ((*view)[r_offset - 1] == 0x15
1106 || (*view)[r_offset - 1] == 0x25));
1109 // Adjust TLS relocation type based on the options and whether this
1110 // is a local symbol.
1111 static tls::Tls_optimization
1112 optimize_tls_reloc(bool is_final, int r_type);
1114 // Get the GOT section, creating it if necessary.
1115 Output_data_got<64, false>*
1116 got_section(Symbol_table*, Layout*);
1118 // Get the GOT PLT section.
1119 Output_data_got_plt_x86_64*
1120 got_plt_section() const
1122 gold_assert(this->got_plt_ != NULL);
1123 return this->got_plt_;
1126 // Get the GOT section for TLSDESC entries.
1127 Output_data_got<64, false>*
1128 got_tlsdesc_section() const
1130 gold_assert(this->got_tlsdesc_ != NULL);
1131 return this->got_tlsdesc_;
1134 // Create the PLT section.
1135 void
1136 make_plt_section(Symbol_table* symtab, Layout* layout);
1138 // Create a PLT entry for a global symbol.
1139 void
1140 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1142 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1143 void
1144 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1145 Sized_relobj_file<size, false>* relobj,
1146 unsigned int local_sym_index);
1148 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1149 void
1150 define_tls_base_symbol(Symbol_table*, Layout*);
1152 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1153 void
1154 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1156 // Create a GOT entry for the TLS module index.
1157 unsigned int
1158 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1159 Sized_relobj_file<size, false>* object);
1161 // Get the PLT section.
1162 Output_data_plt_x86_64<size>*
1163 plt_section() const
1165 gold_assert(this->plt_ != NULL);
1166 return this->plt_;
1169 // Get the dynamic reloc section, creating it if necessary.
1170 Reloc_section*
1171 rela_dyn_section(Layout*);
1173 // Get the section to use for TLSDESC relocations.
1174 Reloc_section*
1175 rela_tlsdesc_section(Layout*) const;
1177 // Get the section to use for IRELATIVE relocations.
1178 Reloc_section*
1179 rela_irelative_section(Layout*);
1181 // Add a potential copy relocation.
1182 void
1183 copy_reloc(Symbol_table* symtab, Layout* layout,
1184 Sized_relobj_file<size, false>* object,
1185 unsigned int shndx, Output_section* output_section,
1186 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1188 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1189 this->copy_relocs_.copy_reloc(symtab, layout,
1190 symtab->get_sized_symbol<size>(sym),
1191 object, shndx, output_section,
1192 r_type, reloc.get_r_offset(),
1193 reloc.get_r_addend(),
1194 this->rela_dyn_section(layout));
1197 // Record a target-specific program property in the .note.gnu.property
1198 // section.
1199 void
1200 record_gnu_property(unsigned int, unsigned int, size_t,
1201 const unsigned char*, const Object*);
1203 // Merge the target-specific program properties from the current object.
1204 void
1205 merge_gnu_properties(const Object*);
1207 // Finalize the target-specific program properties and add them back to
1208 // the layout.
1209 void
1210 do_finalize_gnu_properties(Layout*) const;
1212 // Information about this specific target which we pass to the
1213 // general Target structure.
1214 static const Target::Target_info x86_64_info;
1216 // The types of GOT entries needed for this platform.
1217 // These values are exposed to the ABI in an incremental link.
1218 // Do not renumber existing values without changing the version
1219 // number of the .gnu_incremental_inputs section.
1220 enum Got_type
1222 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1223 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1224 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1225 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1228 // This type is used as the argument to the target specific
1229 // relocation routines. The only target specific reloc is
1230 // R_X86_64_TLSDESC against a local symbol.
1231 struct Tlsdesc_info
1233 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1234 : object(a_object), r_sym(a_r_sym)
1237 // The object in which the local symbol is defined.
1238 Sized_relobj_file<size, false>* object;
1239 // The local symbol index in the object.
1240 unsigned int r_sym;
1243 // The GOT section.
1244 Output_data_got<64, false>* got_;
1245 // The PLT section.
1246 Output_data_plt_x86_64<size>* plt_;
1247 // The GOT PLT section.
1248 Output_data_got_plt_x86_64* got_plt_;
1249 // The GOT section for IRELATIVE relocations.
1250 Output_data_space* got_irelative_;
1251 // The GOT section for TLSDESC relocations.
1252 Output_data_got<64, false>* got_tlsdesc_;
1253 // The _GLOBAL_OFFSET_TABLE_ symbol.
1254 Symbol* global_offset_table_;
1255 // The dynamic reloc section.
1256 Reloc_section* rela_dyn_;
1257 // The section to use for IRELATIVE relocs.
1258 Reloc_section* rela_irelative_;
1259 // Relocs saved to avoid a COPY reloc.
1260 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1261 // Offset of the GOT entry for the TLS module index.
1262 unsigned int got_mod_index_offset_;
1263 // We handle R_X86_64_TLSDESC against a local symbol as a target
1264 // specific relocation. Here we store the object and local symbol
1265 // index for the relocation.
1266 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1267 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1268 bool tls_base_symbol_defined_;
1269 // Target-specific program properties, from .note.gnu.property section.
1270 // Each bit represents a specific feature.
1271 uint32_t isa_1_used_;
1272 uint32_t isa_1_needed_;
1273 uint32_t feature_1_;
1274 uint32_t feature_2_used_;
1275 uint32_t feature_2_needed_;
1276 // Target-specific properties from the current object.
1277 // These bits get ORed into ISA_1_USED_ after all properties for the object
1278 // have been processed. But if either is all zeroes (as when the property
1279 // is absent from an object), the result should be all zeroes.
1280 // (See PR ld/23486.)
1281 uint32_t object_isa_1_used_;
1282 // These bits get ANDed into FEATURE_1_ after all properties for the object
1283 // have been processed.
1284 uint32_t object_feature_1_;
1285 uint32_t object_feature_2_used_;
1286 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1287 bool seen_first_object_;
1290 template<>
1291 const Target::Target_info Target_x86_64<64>::x86_64_info =
1293 64, // size
1294 false, // is_big_endian
1295 elfcpp::EM_X86_64, // machine_code
1296 false, // has_make_symbol
1297 false, // has_resolve
1298 true, // has_code_fill
1299 true, // is_default_stack_executable
1300 true, // can_icf_inline_merge_sections
1301 '\0', // wrap_char
1302 "/lib/ld64.so.1", // program interpreter
1303 0x400000, // default_text_segment_address
1304 0x1000, // abi_pagesize (overridable by -z max-page-size)
1305 0x1000, // common_pagesize (overridable by -z common-page-size)
1306 false, // isolate_execinstr
1307 0, // rosegment_gap
1308 elfcpp::SHN_UNDEF, // small_common_shndx
1309 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1310 0, // small_common_section_flags
1311 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1312 NULL, // attributes_section
1313 NULL, // attributes_vendor
1314 "_start", // entry_symbol_name
1315 32, // hash_entry_size
1316 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1319 template<>
1320 const Target::Target_info Target_x86_64<32>::x86_64_info =
1322 32, // size
1323 false, // is_big_endian
1324 elfcpp::EM_X86_64, // machine_code
1325 false, // has_make_symbol
1326 false, // has_resolve
1327 true, // has_code_fill
1328 true, // is_default_stack_executable
1329 true, // can_icf_inline_merge_sections
1330 '\0', // wrap_char
1331 "/libx32/ldx32.so.1", // program interpreter
1332 0x400000, // default_text_segment_address
1333 0x1000, // abi_pagesize (overridable by -z max-page-size)
1334 0x1000, // common_pagesize (overridable by -z common-page-size)
1335 false, // isolate_execinstr
1336 0, // rosegment_gap
1337 elfcpp::SHN_UNDEF, // small_common_shndx
1338 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1339 0, // small_common_section_flags
1340 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1341 NULL, // attributes_section
1342 NULL, // attributes_vendor
1343 "_start", // entry_symbol_name
1344 32, // hash_entry_size
1345 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1348 // This is called when a new output section is created. This is where
1349 // we handle the SHF_X86_64_LARGE.
1351 template<int size>
1352 void
1353 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1355 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1356 os->set_is_large_section();
1359 // Get the GOT section, creating it if necessary.
1361 template<int size>
1362 Output_data_got<64, false>*
1363 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1365 if (this->got_ == NULL)
1367 gold_assert(symtab != NULL && layout != NULL);
1369 // When using -z now, we can treat .got.plt as a relro section.
1370 // Without -z now, it is modified after program startup by lazy
1371 // PLT relocations.
1372 bool is_got_plt_relro = parameters->options().now();
1373 Output_section_order got_order = (is_got_plt_relro
1374 ? ORDER_RELRO
1375 : ORDER_RELRO_LAST);
1376 Output_section_order got_plt_order = (is_got_plt_relro
1377 ? ORDER_RELRO
1378 : ORDER_NON_RELRO_FIRST);
1380 this->got_ = new Output_data_got<64, false>();
1382 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1383 (elfcpp::SHF_ALLOC
1384 | elfcpp::SHF_WRITE),
1385 this->got_, got_order, true);
1387 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1388 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1389 (elfcpp::SHF_ALLOC
1390 | elfcpp::SHF_WRITE),
1391 this->got_plt_, got_plt_order,
1392 is_got_plt_relro);
1394 // The first three entries are reserved.
1395 this->got_plt_->set_current_data_size(3 * 8);
1397 if (!is_got_plt_relro)
1399 // Those bytes can go into the relro segment.
1400 layout->increase_relro(3 * 8);
1403 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1404 this->global_offset_table_ =
1405 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1406 Symbol_table::PREDEFINED,
1407 this->got_plt_,
1408 0, 0, elfcpp::STT_OBJECT,
1409 elfcpp::STB_LOCAL,
1410 elfcpp::STV_HIDDEN, 0,
1411 false, false);
1413 // If there are any IRELATIVE relocations, they get GOT entries
1414 // in .got.plt after the jump slot entries.
1415 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1416 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1417 (elfcpp::SHF_ALLOC
1418 | elfcpp::SHF_WRITE),
1419 this->got_irelative_,
1420 got_plt_order, is_got_plt_relro);
1422 // If there are any TLSDESC relocations, they get GOT entries in
1423 // .got.plt after the jump slot and IRELATIVE entries.
1424 this->got_tlsdesc_ = new Output_data_got<64, false>();
1425 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1426 (elfcpp::SHF_ALLOC
1427 | elfcpp::SHF_WRITE),
1428 this->got_tlsdesc_,
1429 got_plt_order, is_got_plt_relro);
1432 return this->got_;
1435 // Get the dynamic reloc section, creating it if necessary.
1437 template<int size>
1438 typename Target_x86_64<size>::Reloc_section*
1439 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1441 if (this->rela_dyn_ == NULL)
1443 gold_assert(layout != NULL);
1444 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1445 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1446 elfcpp::SHF_ALLOC, this->rela_dyn_,
1447 ORDER_DYNAMIC_RELOCS, false);
1449 return this->rela_dyn_;
1452 // Get the section to use for IRELATIVE relocs, creating it if
1453 // necessary. These go in .rela.dyn, but only after all other dynamic
1454 // relocations. They need to follow the other dynamic relocations so
1455 // that they can refer to global variables initialized by those
1456 // relocs.
1458 template<int size>
1459 typename Target_x86_64<size>::Reloc_section*
1460 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1462 if (this->rela_irelative_ == NULL)
1464 // Make sure we have already created the dynamic reloc section.
1465 this->rela_dyn_section(layout);
1466 this->rela_irelative_ = new Reloc_section(false);
1467 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1468 elfcpp::SHF_ALLOC, this->rela_irelative_,
1469 ORDER_DYNAMIC_RELOCS, false);
1470 gold_assert(this->rela_dyn_->output_section()
1471 == this->rela_irelative_->output_section());
1473 return this->rela_irelative_;
1476 // Record a target-specific program property from the .note.gnu.property
1477 // section.
1478 template<int size>
1479 void
1480 Target_x86_64<size>::record_gnu_property(
1481 unsigned int, unsigned int pr_type,
1482 size_t pr_datasz, const unsigned char* pr_data,
1483 const Object* object)
1485 uint32_t val = 0;
1487 switch (pr_type)
1489 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
1490 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
1491 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
1492 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
1493 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1494 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1495 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1496 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1497 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1498 if (pr_datasz != 4)
1500 gold_warning(_("%s: corrupt .note.gnu.property section "
1501 "(pr_datasz for property %d is not 4)"),
1502 object->name().c_str(), pr_type);
1503 return;
1505 val = elfcpp::Swap<32, false>::readval(pr_data);
1506 break;
1507 default:
1508 gold_warning(_("%s: unknown program property type 0x%x "
1509 "in .note.gnu.property section"),
1510 object->name().c_str(), pr_type);
1511 break;
1514 switch (pr_type)
1516 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1517 this->object_isa_1_used_ |= val;
1518 break;
1519 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1520 this->isa_1_needed_ |= val;
1521 break;
1522 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1523 // If we see multiple feature props in one object, OR them together.
1524 this->object_feature_1_ |= val;
1525 break;
1526 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1527 this->object_feature_2_used_ |= val;
1528 break;
1529 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1530 this->feature_2_needed_ |= val;
1531 break;
1535 // Merge the target-specific program properties from the current object.
1536 template<int size>
1537 void
1538 Target_x86_64<size>::merge_gnu_properties(const Object*)
1540 if (this->seen_first_object_)
1542 // If any object is missing the ISA_1_USED property, we must omit
1543 // it from the output file.
1544 if (this->object_isa_1_used_ == 0)
1545 this->isa_1_used_ = 0;
1546 else if (this->isa_1_used_ != 0)
1547 this->isa_1_used_ |= this->object_isa_1_used_;
1548 this->feature_1_ &= this->object_feature_1_;
1549 // If any object is missing the FEATURE_2_USED property, we must
1550 // omit it from the output file.
1551 if (this->object_feature_2_used_ == 0)
1552 this->feature_2_used_ = 0;
1553 else if (this->feature_2_used_ != 0)
1554 this->feature_2_used_ |= this->object_feature_2_used_;
1556 else
1558 this->isa_1_used_ = this->object_isa_1_used_;
1559 this->feature_1_ = this->object_feature_1_;
1560 this->feature_2_used_ = this->object_feature_2_used_;
1561 this->seen_first_object_ = true;
1563 this->object_isa_1_used_ = 0;
1564 this->object_feature_1_ = 0;
1565 this->object_feature_2_used_ = 0;
1568 static inline void
1569 add_property(Layout* layout, unsigned int pr_type, uint32_t val)
1571 unsigned char buf[4];
1572 elfcpp::Swap<32, false>::writeval(buf, val);
1573 layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
1576 // Finalize the target-specific program properties and add them back to
1577 // the layout.
1578 template<int size>
1579 void
1580 Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const
1582 if (this->isa_1_used_ != 0)
1583 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
1584 this->isa_1_used_);
1585 if (this->isa_1_needed_ != 0)
1586 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
1587 this->isa_1_needed_);
1588 if (this->feature_1_ != 0)
1589 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
1590 this->feature_1_);
1591 if (this->feature_2_used_ != 0)
1592 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
1593 this->feature_2_used_);
1594 if (this->feature_2_needed_ != 0)
1595 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
1596 this->feature_2_needed_);
1599 // Write the first three reserved words of the .got.plt section.
1600 // The remainder of the section is written while writing the PLT
1601 // in Output_data_plt_i386::do_write.
1603 void
1604 Output_data_got_plt_x86_64::do_write(Output_file* of)
1606 // The first entry in the GOT is the address of the .dynamic section
1607 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1608 // We saved space for them when we created the section in
1609 // Target_x86_64::got_section.
1610 const off_t got_file_offset = this->offset();
1611 gold_assert(this->data_size() >= 24);
1612 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1613 Output_section* dynamic = this->layout_->dynamic_section();
1614 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1615 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1616 memset(got_view + 8, 0, 16);
1617 of->write_output_view(got_file_offset, 24, got_view);
1620 // Initialize the PLT section.
1622 template<int size>
1623 void
1624 Output_data_plt_x86_64<size>::init(Layout* layout)
1626 this->rel_ = new Reloc_section(false);
1627 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1628 elfcpp::SHF_ALLOC, this->rel_,
1629 ORDER_DYNAMIC_PLT_RELOCS, false);
1632 template<int size>
1633 void
1634 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1636 os->set_entsize(this->get_plt_entry_size());
1639 // Add an entry to the PLT.
1641 template<int size>
1642 void
1643 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1644 Symbol* gsym)
1646 gold_assert(!gsym->has_plt_offset());
1648 unsigned int plt_index;
1649 off_t plt_offset;
1650 section_offset_type got_offset;
1652 unsigned int* pcount;
1653 unsigned int offset;
1654 unsigned int reserved;
1655 Output_section_data_build* got;
1656 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1657 && gsym->can_use_relative_reloc(false))
1659 pcount = &this->irelative_count_;
1660 offset = 0;
1661 reserved = 0;
1662 got = this->got_irelative_;
1664 else
1666 pcount = &this->count_;
1667 offset = 1;
1668 reserved = 3;
1669 got = this->got_plt_;
1672 if (!this->is_data_size_valid())
1674 // Note that when setting the PLT offset for a non-IRELATIVE
1675 // entry we skip the initial reserved PLT entry.
1676 plt_index = *pcount + offset;
1677 plt_offset = plt_index * this->get_plt_entry_size();
1679 ++*pcount;
1681 got_offset = (plt_index - offset + reserved) * 8;
1682 gold_assert(got_offset == got->current_data_size());
1684 // Every PLT entry needs a GOT entry which points back to the PLT
1685 // entry (this will be changed by the dynamic linker, normally
1686 // lazily when the function is called).
1687 got->set_current_data_size(got_offset + 8);
1689 else
1691 // FIXME: This is probably not correct for IRELATIVE relocs.
1693 // For incremental updates, find an available slot.
1694 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1695 this->get_plt_entry_size(), 0);
1696 if (plt_offset == -1)
1697 gold_fallback(_("out of patch space (PLT);"
1698 " relink with --incremental-full"));
1700 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1701 // can be calculated from the PLT index, adjusting for the three
1702 // reserved entries at the beginning of the GOT.
1703 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1704 got_offset = (plt_index - offset + reserved) * 8;
1707 gsym->set_plt_offset(plt_offset);
1709 // Every PLT entry needs a reloc.
1710 this->add_relocation(symtab, layout, gsym, got_offset);
1712 // Note that we don't need to save the symbol. The contents of the
1713 // PLT are independent of which symbols are used. The symbols only
1714 // appear in the relocations.
1717 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1718 // the PLT offset.
1720 template<int size>
1721 unsigned int
1722 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1723 Symbol_table* symtab,
1724 Layout* layout,
1725 Sized_relobj_file<size, false>* relobj,
1726 unsigned int local_sym_index)
1728 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1729 ++this->irelative_count_;
1731 section_offset_type got_offset = this->got_irelative_->current_data_size();
1733 // Every PLT entry needs a GOT entry which points back to the PLT
1734 // entry.
1735 this->got_irelative_->set_current_data_size(got_offset + 8);
1737 // Every PLT entry needs a reloc.
1738 Reloc_section* rela = this->rela_irelative(symtab, layout);
1739 rela->add_symbolless_local_addend(relobj, local_sym_index,
1740 elfcpp::R_X86_64_IRELATIVE,
1741 this->got_irelative_, got_offset, 0);
1743 return plt_offset;
1746 // Add the relocation for a PLT entry.
1748 template<int size>
1749 void
1750 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1751 Layout* layout,
1752 Symbol* gsym,
1753 unsigned int got_offset)
1755 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1756 && gsym->can_use_relative_reloc(false))
1758 Reloc_section* rela = this->rela_irelative(symtab, layout);
1759 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1760 this->got_irelative_, got_offset, 0);
1762 else
1764 gsym->set_needs_dynsym_entry();
1765 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1766 got_offset, 0);
1770 // Return where the TLSDESC relocations should go, creating it if
1771 // necessary. These follow the JUMP_SLOT relocations.
1773 template<int size>
1774 typename Output_data_plt_x86_64<size>::Reloc_section*
1775 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1777 if (this->tlsdesc_rel_ == NULL)
1779 this->tlsdesc_rel_ = new Reloc_section(false);
1780 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1781 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1782 ORDER_DYNAMIC_PLT_RELOCS, false);
1783 gold_assert(this->tlsdesc_rel_->output_section()
1784 == this->rel_->output_section());
1786 return this->tlsdesc_rel_;
1789 // Return where the IRELATIVE relocations should go in the PLT. These
1790 // follow the JUMP_SLOT and the TLSDESC relocations.
1792 template<int size>
1793 typename Output_data_plt_x86_64<size>::Reloc_section*
1794 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1795 Layout* layout)
1797 if (this->irelative_rel_ == NULL)
1799 // Make sure we have a place for the TLSDESC relocations, in
1800 // case we see any later on.
1801 this->rela_tlsdesc(layout);
1802 this->irelative_rel_ = new Reloc_section(false);
1803 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1804 elfcpp::SHF_ALLOC, this->irelative_rel_,
1805 ORDER_DYNAMIC_PLT_RELOCS, false);
1806 gold_assert(this->irelative_rel_->output_section()
1807 == this->rel_->output_section());
1809 if (parameters->doing_static_link())
1811 // A statically linked executable will only have a .rela.plt
1812 // section to hold R_X86_64_IRELATIVE relocs for
1813 // STT_GNU_IFUNC symbols. The library will use these
1814 // symbols to locate the IRELATIVE relocs at program startup
1815 // time.
1816 symtab->define_in_output_data("__rela_iplt_start", NULL,
1817 Symbol_table::PREDEFINED,
1818 this->irelative_rel_, 0, 0,
1819 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1820 elfcpp::STV_HIDDEN, 0, false, true);
1821 symtab->define_in_output_data("__rela_iplt_end", NULL,
1822 Symbol_table::PREDEFINED,
1823 this->irelative_rel_, 0, 0,
1824 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1825 elfcpp::STV_HIDDEN, 0, true, true);
1828 return this->irelative_rel_;
1831 // Return the PLT address to use for a global symbol.
1833 template<int size>
1834 uint64_t
1835 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1837 uint64_t offset = 0;
1838 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1839 && gsym->can_use_relative_reloc(false))
1840 offset = (this->count_ + 1) * this->get_plt_entry_size();
1841 return this->address() + offset + gsym->plt_offset();
1844 // Return the PLT address to use for a local symbol. These are always
1845 // IRELATIVE relocs.
1847 template<int size>
1848 uint64_t
1849 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1850 unsigned int r_sym)
1852 return (this->address()
1853 + (this->count_ + 1) * this->get_plt_entry_size()
1854 + object->local_plt_offset(r_sym));
1857 // Set the final size.
1858 template<int size>
1859 void
1860 Output_data_plt_x86_64<size>::set_final_data_size()
1862 // Number of regular and IFUNC PLT entries, plus the first entry.
1863 unsigned int count = this->count_ + this->irelative_count_ + 1;
1864 // Count the TLSDESC entry, if present.
1865 if (this->has_tlsdesc_entry())
1866 ++count;
1867 this->set_data_size(count * this->get_plt_entry_size());
1870 // The first entry in the PLT for an executable.
1872 template<int size>
1873 const unsigned char
1874 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1876 // From AMD64 ABI Draft 0.98, page 76
1877 0xff, 0x35, // pushq contents of memory address
1878 0, 0, 0, 0, // replaced with address of .got + 8
1879 0xff, 0x25, // jmp indirect
1880 0, 0, 0, 0, // replaced with address of .got + 16
1881 0x90, 0x90, 0x90, 0x90 // noop (x4)
1884 template<int size>
1885 void
1886 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1887 unsigned char* pov,
1888 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1889 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1891 memcpy(pov, first_plt_entry, plt_entry_size);
1892 // We do a jmp relative to the PC at the end of this instruction.
1893 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1894 (got_address + 8
1895 - (plt_address + 6)));
1896 elfcpp::Swap<32, false>::writeval(pov + 8,
1897 (got_address + 16
1898 - (plt_address + 12)));
1901 // Subsequent entries in the PLT for an executable.
1903 template<int size>
1904 const unsigned char
1905 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1907 // From AMD64 ABI Draft 0.98, page 76
1908 0xff, 0x25, // jmpq indirect
1909 0, 0, 0, 0, // replaced with address of symbol in .got
1910 0x68, // pushq immediate
1911 0, 0, 0, 0, // replaced with offset into relocation table
1912 0xe9, // jmpq relative
1913 0, 0, 0, 0 // replaced with offset to start of .plt
1916 template<int size>
1917 unsigned int
1918 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1919 unsigned char* pov,
1920 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1921 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1922 unsigned int got_offset,
1923 unsigned int plt_offset,
1924 unsigned int plt_index)
1926 // Check PC-relative offset overflow in PLT entry.
1927 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1928 - (plt_address + plt_offset + 6));
1929 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1930 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1931 plt_index + 1);
1933 memcpy(pov, plt_entry, plt_entry_size);
1934 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1935 plt_got_pcrel_offset);
1937 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1938 elfcpp::Swap<32, false>::writeval(pov + 12,
1939 - (plt_offset + plt_entry_size));
1941 return 6;
1944 // The reserved TLSDESC entry in the PLT for an executable.
1946 template<int size>
1947 const unsigned char
1948 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1950 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1951 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1952 0xff, 0x35, // pushq x(%rip)
1953 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1954 0xff, 0x25, // jmpq *y(%rip)
1955 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1956 0x0f, 0x1f, // nop
1957 0x40, 0
1960 template<int size>
1961 void
1962 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1963 unsigned char* pov,
1964 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1965 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1966 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1967 unsigned int tlsdesc_got_offset,
1968 unsigned int plt_offset)
1970 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1971 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1972 (got_address + 8
1973 - (plt_address + plt_offset
1974 + 6)));
1975 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1976 (got_base
1977 + tlsdesc_got_offset
1978 - (plt_address + plt_offset
1979 + 12)));
1982 // Return the APLT address to use for a global symbol (for IBT).
1984 template<int size>
1985 uint64_t
1986 Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym)
1988 uint64_t offset = this->aplt_offset_;
1989 // Convert the PLT offset into an APLT offset.
1990 unsigned int plt_offset = gsym->plt_offset();
1991 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1992 && gsym->can_use_relative_reloc(false))
1993 offset += this->regular_count() * aplt_entry_size;
1994 else
1995 plt_offset -= plt_entry_size;
1996 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
1997 return this->address() + offset + plt_offset;
2000 // Return the PLT address to use for a local symbol. These are always
2001 // IRELATIVE relocs.
2003 template<int size>
2004 uint64_t
2005 Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object,
2006 unsigned int r_sym)
2008 // Convert the PLT offset into an APLT offset.
2009 const Sized_relobj_file<size, false>* sized_relobj =
2010 static_cast<const Sized_relobj_file<size, false>*>(object);
2011 const Symbol_value<size>* psymval = sized_relobj->local_symbol(r_sym);
2012 unsigned int plt_offset = ((object->local_plt_offset(r_sym)
2013 - (psymval->is_ifunc_symbol()
2014 ? 0 : plt_entry_size))
2015 / (plt_entry_size / aplt_entry_size));
2016 return (this->address()
2017 + this->aplt_offset_
2018 + this->regular_count() * aplt_entry_size
2019 + plt_offset);
2022 // Set the final size.
2024 template<int size>
2025 void
2026 Output_data_plt_x86_64_ibt<size>::set_final_data_size()
2028 // Number of regular and IFUNC PLT entries.
2029 unsigned int count = this->entry_count();
2030 // Count the first entry and the TLSDESC entry, if present.
2031 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2032 unsigned int plt_size = (count + extra) * plt_entry_size;
2033 // Offset of the APLT.
2034 this->aplt_offset_ = plt_size;
2035 // Size of the APLT.
2036 plt_size += count * aplt_entry_size;
2037 this->set_data_size(plt_size);
2040 // The first entry in the IBT PLT.
2042 template<int size>
2043 const unsigned char
2044 Output_data_plt_x86_64_ibt<size>::first_plt_entry[plt_entry_size] =
2046 0xff, 0x35, // pushq contents of memory address
2047 0, 0, 0, 0, // replaced with address of .got + 8
2048 0xff, 0x25, // jmp indirect
2049 0, 0, 0, 0, // replaced with address of .got + 16
2050 0x90, 0x90, 0x90, 0x90 // noop (x4)
2053 template<int size>
2054 void
2055 Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry(
2056 unsigned char* pov,
2057 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2058 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
2060 // Offsets to the addresses needing relocation.
2061 const unsigned int roff1 = 2;
2062 const unsigned int roff2 = 8;
2064 memcpy(pov, first_plt_entry, plt_entry_size);
2065 // We do a jmp relative to the PC at the end of this instruction.
2066 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1,
2067 (got_address + 8
2068 - (plt_address + roff1 + 4)));
2069 elfcpp::Swap<32, false>::writeval(pov + roff2,
2070 (got_address + 16
2071 - (plt_address + roff2 + 4)));
2074 // Subsequent entries in the IBT PLT.
2076 template<int size>
2077 const unsigned char
2078 Output_data_plt_x86_64_ibt<size>::plt_entry[plt_entry_size] =
2080 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2081 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2082 0x68, // pushq immediate
2083 0, 0, 0, 0, // replaced with offset into relocation table
2084 0xe9, // jmpq relative
2085 0, 0, 0, 0, // replaced with offset to start of .plt
2086 0x90, 0x90 // nop
2089 // Entries in the IBT Additional PLT.
2091 template<int size>
2092 const unsigned char
2093 Output_data_plt_x86_64_ibt<size>::aplt_entry[aplt_entry_size] =
2095 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2096 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2097 0xff, 0x25, // jmpq indirect
2098 0, 0, 0, 0, // replaced with address of symbol in .got
2099 0x0f, 0x1f, 0x04, 0x00, // nop
2100 0x90, 0x90 // nop
2103 template<int size>
2104 unsigned int
2105 Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry(
2106 unsigned char* pov,
2107 typename elfcpp::Elf_types<size>::Elf_Addr,
2108 typename elfcpp::Elf_types<size>::Elf_Addr,
2109 unsigned int,
2110 unsigned int plt_offset,
2111 unsigned int plt_index)
2113 // Offsets to the addresses needing relocation.
2114 const unsigned int roff1 = 5;
2115 const unsigned int roff2 = 10;
2117 memcpy(pov, plt_entry, plt_entry_size);
2118 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index);
2119 elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4));
2120 return 0;
2123 template<int size>
2124 void
2125 Output_data_plt_x86_64_ibt<size>::fill_aplt_entry(
2126 unsigned char* pov,
2127 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2128 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2129 unsigned int got_offset,
2130 unsigned int plt_offset,
2131 unsigned int plt_index)
2133 // Offset to the address needing relocation.
2134 const unsigned int roff = 6;
2136 // Check PC-relative offset overflow in PLT entry.
2137 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2138 - (plt_address + plt_offset + roff + 4));
2139 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2140 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2141 plt_index + 1);
2143 memcpy(pov, aplt_entry, aplt_entry_size);
2144 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset);
2147 // The reserved TLSDESC entry in the IBT PLT for an executable.
2149 template<int size>
2150 const unsigned char
2151 Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] =
2153 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2154 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2155 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2156 0xff, 0x35, // pushq x(%rip)
2157 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2158 0xff, 0x25, // jmpq *y(%rip)
2159 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2162 template<int size>
2163 void
2164 Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry(
2165 unsigned char* pov,
2166 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2167 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2168 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2169 unsigned int tlsdesc_got_offset,
2170 unsigned int plt_offset)
2172 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2173 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6,
2174 (got_address + 8
2175 - (plt_address + plt_offset
2176 + 10)));
2177 elfcpp::Swap_unaligned<32, false>::writeval(pov + 12,
2178 (got_base
2179 + tlsdesc_got_offset
2180 - (plt_address + plt_offset
2181 + 16)));
2184 // The .eh_frame unwind information for the PLT.
2186 template<int size>
2187 const unsigned char
2188 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
2190 1, // CIE version.
2191 'z', // Augmentation: augmentation size included.
2192 'R', // Augmentation: FDE encoding included.
2193 '\0', // End of augmentation string.
2194 1, // Code alignment factor.
2195 0x78, // Data alignment factor.
2196 16, // Return address column.
2197 1, // Augmentation size.
2198 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
2199 | elfcpp::DW_EH_PE_sdata4),
2200 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2201 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2202 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2203 elfcpp::DW_CFA_nop
2206 template<int size>
2207 const unsigned char
2208 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2210 0, 0, 0, 0, // Replaced with offset to .plt.
2211 0, 0, 0, 0, // Replaced with size of .plt.
2212 0, // Augmentation size.
2213 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2214 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2215 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2216 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2217 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2218 11, // Block length.
2219 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2220 elfcpp::DW_OP_breg16, 0, // Push %rip.
2221 elfcpp::DW_OP_lit15, // Push 0xf.
2222 elfcpp::DW_OP_and, // & (%rip & 0xf).
2223 elfcpp::DW_OP_lit11, // Push 0xb.
2224 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2225 elfcpp::DW_OP_lit3, // Push 3.
2226 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2227 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2228 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2229 elfcpp::DW_CFA_nop,
2230 elfcpp::DW_CFA_nop,
2231 elfcpp::DW_CFA_nop
2234 // The .eh_frame unwind information for the PLT.
2235 template<int size>
2236 const unsigned char
2237 Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2239 0, 0, 0, 0, // Replaced with offset to .plt.
2240 0, 0, 0, 0, // Replaced with size of .plt.
2241 0, // Augmentation size.
2242 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2243 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2244 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2245 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2246 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2247 11, // Block length.
2248 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2249 elfcpp::DW_OP_breg16, 0, // Push %rip.
2250 elfcpp::DW_OP_lit15, // Push 0xf.
2251 elfcpp::DW_OP_and, // & (%rip & 0xf).
2252 elfcpp::DW_OP_lit9, // Push 9.
2253 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9)
2254 elfcpp::DW_OP_lit3, // Push 3.
2255 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3)
2256 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2257 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2258 elfcpp::DW_CFA_nop,
2259 elfcpp::DW_CFA_nop,
2260 elfcpp::DW_CFA_nop
2263 // Write out the PLT. This uses the hand-coded instructions above,
2264 // and adjusts them as needed. This is specified by the AMD64 ABI.
2266 template<int size>
2267 void
2268 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2270 const off_t offset = this->offset();
2271 const section_size_type oview_size =
2272 convert_to_section_size_type(this->data_size());
2273 unsigned char* const oview = of->get_output_view(offset, oview_size);
2275 const off_t got_file_offset = this->got_plt_->offset();
2276 gold_assert(parameters->incremental_update()
2277 || (got_file_offset + this->got_plt_->data_size()
2278 == this->got_irelative_->offset()));
2279 const section_size_type got_size =
2280 convert_to_section_size_type(this->got_plt_->data_size()
2281 + this->got_irelative_->data_size());
2282 unsigned char* const got_view = of->get_output_view(got_file_offset,
2283 got_size);
2285 unsigned char* pov = oview;
2287 // The base address of the .plt section.
2288 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2289 // The base address of the .got section.
2290 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2291 // The base address of the PLT portion of the .got section,
2292 // which is where the GOT pointer will point, and where the
2293 // three reserved GOT entries are located.
2294 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2295 = this->got_plt_->address();
2297 this->fill_first_plt_entry(pov, got_address, plt_address);
2298 pov += this->get_plt_entry_size();
2300 // The first three entries in the GOT are reserved, and are written
2301 // by Output_data_got_plt_x86_64::do_write.
2302 unsigned char* got_pov = got_view + 24;
2304 unsigned int plt_offset = this->get_plt_entry_size();
2305 unsigned int got_offset = 24;
2306 const unsigned int count = this->count_ + this->irelative_count_;
2307 for (unsigned int plt_index = 0;
2308 plt_index < count;
2309 ++plt_index,
2310 pov += this->get_plt_entry_size(),
2311 got_pov += 8,
2312 plt_offset += this->get_plt_entry_size(),
2313 got_offset += 8)
2315 // Set and adjust the PLT entry itself.
2316 unsigned int lazy_offset = this->fill_plt_entry(pov,
2317 got_address, plt_address,
2318 got_offset, plt_offset,
2319 plt_index);
2321 // Set the entry in the GOT.
2322 elfcpp::Swap<64, false>::writeval(got_pov,
2323 plt_address + plt_offset + lazy_offset);
2326 if (this->has_tlsdesc_entry())
2328 // Set and adjust the reserved TLSDESC PLT entry.
2329 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2330 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2331 tlsdesc_got_offset, plt_offset);
2332 pov += this->get_plt_entry_size();
2335 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2336 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2338 of->write_output_view(offset, oview_size, oview);
2339 of->write_output_view(got_file_offset, got_size, got_view);
2342 // Write out the IBT PLT.
2344 template<int size>
2345 void
2346 Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of)
2348 const off_t offset = this->offset();
2349 const section_size_type oview_size =
2350 convert_to_section_size_type(this->data_size());
2351 unsigned char* const oview = of->get_output_view(offset, oview_size);
2353 Output_data_got<64, false>* got = this->got();
2354 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2355 Output_data_space* got_irelative = this->got_irelative();
2357 const off_t got_file_offset = got_plt->offset();
2358 gold_assert(parameters->incremental_update()
2359 || (got_file_offset + got_plt->data_size()
2360 == got_irelative->offset()));
2361 const section_size_type got_size =
2362 convert_to_section_size_type(got_plt->data_size()
2363 + got_irelative->data_size());
2364 unsigned char* const got_view = of->get_output_view(got_file_offset,
2365 got_size);
2367 unsigned char* pov = oview;
2369 // The base address of the .plt section.
2370 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2371 // The base address of the .got section.
2372 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2373 // The base address of the PLT portion of the .got section,
2374 // which is where the GOT pointer will point, and where the
2375 // three reserved GOT entries are located.
2376 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2378 this->fill_first_plt_entry(pov, got_address, plt_address);
2379 pov += plt_entry_size;
2381 // The first three entries in the GOT are reserved, and are written
2382 // by Output_data_got_plt_x86_64::do_write.
2383 unsigned char* got_pov = got_view + 24;
2385 unsigned int plt_offset = plt_entry_size;
2386 unsigned int got_offset = 24;
2387 const unsigned int count = this->entry_count();
2388 for (unsigned int plt_index = 0;
2389 plt_index < count;
2390 ++plt_index,
2391 pov += plt_entry_size,
2392 got_pov += 8,
2393 plt_offset += plt_entry_size,
2394 got_offset += 8)
2396 // Set and adjust the PLT entry itself.
2397 unsigned int lazy_offset = this->fill_plt_entry(pov,
2398 got_address, plt_address,
2399 got_offset, plt_offset,
2400 plt_index);
2402 // Set the entry in the GOT.
2403 elfcpp::Swap<64, false>::writeval(got_pov,
2404 plt_address + plt_offset + lazy_offset);
2407 if (this->has_tlsdesc_entry())
2409 // Set and adjust the reserved TLSDESC PLT entry.
2410 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2411 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2412 tlsdesc_got_offset, plt_offset);
2413 pov += this->get_plt_entry_size();
2414 plt_offset += plt_entry_size;
2417 // Write the additional PLT.
2418 got_offset = 24;
2419 for (unsigned int plt_index = 0;
2420 plt_index < count;
2421 ++plt_index,
2422 pov += aplt_entry_size,
2423 plt_offset += aplt_entry_size,
2424 got_offset += 8)
2426 // Set and adjust the APLT entry.
2427 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2428 plt_offset, plt_index);
2431 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2432 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2434 of->write_output_view(offset, oview_size, oview);
2435 of->write_output_view(got_file_offset, got_size, got_view);
2438 // Create the PLT section.
2440 template<int size>
2441 void
2442 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2444 if (this->plt_ == NULL)
2446 // Create the GOT sections first.
2447 this->got_section(symtab, layout);
2449 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2450 this->got_irelative_);
2452 // Add unwind information if requested.
2453 if (parameters->options().ld_generated_unwind_info())
2454 this->plt_->add_eh_frame(layout);
2456 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2457 (elfcpp::SHF_ALLOC
2458 | elfcpp::SHF_EXECINSTR),
2459 this->plt_, ORDER_PLT, false);
2461 // Make the sh_info field of .rela.plt point to .plt.
2462 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2463 rela_plt_os->set_info_section(this->plt_->output_section());
2467 template<>
2468 Output_data_plt_x86_64<32>*
2469 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2470 Output_data_got<64, false>* got,
2471 Output_data_got_plt_x86_64* got_plt,
2472 Output_data_space* got_irelative)
2474 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2475 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2476 got_irelative);
2477 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2478 got_irelative);
2481 template<>
2482 Output_data_plt_x86_64<64>*
2483 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2484 Output_data_got<64, false>* got,
2485 Output_data_got_plt_x86_64* got_plt,
2486 Output_data_space* got_irelative)
2488 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2489 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2490 got_irelative);
2491 else
2492 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2493 got_irelative);
2496 template<>
2497 Output_data_plt_x86_64<32>*
2498 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2499 Output_data_got<64, false>* got,
2500 Output_data_got_plt_x86_64* got_plt,
2501 Output_data_space* got_irelative,
2502 unsigned int plt_count)
2504 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2505 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2506 got_irelative, plt_count);
2507 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2508 got_irelative, plt_count);
2511 template<>
2512 Output_data_plt_x86_64<64>*
2513 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2514 Output_data_got<64, false>* got,
2515 Output_data_got_plt_x86_64* got_plt,
2516 Output_data_space* got_irelative,
2517 unsigned int plt_count)
2519 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2520 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2521 got_irelative, plt_count);
2522 else
2523 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2524 got_irelative,
2525 plt_count);
2528 // Return the section for TLSDESC relocations.
2530 template<int size>
2531 typename Target_x86_64<size>::Reloc_section*
2532 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2534 return this->plt_section()->rela_tlsdesc(layout);
2537 // Create a PLT entry for a global symbol.
2539 template<int size>
2540 void
2541 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2542 Symbol* gsym)
2544 if (gsym->has_plt_offset())
2545 return;
2547 if (this->plt_ == NULL)
2548 this->make_plt_section(symtab, layout);
2550 this->plt_->add_entry(symtab, layout, gsym);
2553 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2555 template<int size>
2556 void
2557 Target_x86_64<size>::make_local_ifunc_plt_entry(
2558 Symbol_table* symtab, Layout* layout,
2559 Sized_relobj_file<size, false>* relobj,
2560 unsigned int local_sym_index)
2562 if (relobj->local_has_plt_offset(local_sym_index))
2563 return;
2564 if (this->plt_ == NULL)
2565 this->make_plt_section(symtab, layout);
2566 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2567 relobj,
2568 local_sym_index);
2569 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2572 // Return the number of entries in the PLT.
2574 template<int size>
2575 unsigned int
2576 Target_x86_64<size>::plt_entry_count() const
2578 if (this->plt_ == NULL)
2579 return 0;
2580 return this->plt_->entry_count();
2583 // Return the offset of the first non-reserved PLT entry.
2585 template<int size>
2586 unsigned int
2587 Target_x86_64<size>::first_plt_entry_offset() const
2589 if (this->plt_ == NULL)
2590 return 0;
2591 return this->plt_->first_plt_entry_offset();
2594 // Return the size of each PLT entry.
2596 template<int size>
2597 unsigned int
2598 Target_x86_64<size>::plt_entry_size() const
2600 if (this->plt_ == NULL)
2601 return 0;
2602 return this->plt_->get_plt_entry_size();
2605 // Create the GOT and PLT sections for an incremental update.
2607 template<int size>
2608 Output_data_got_base*
2609 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
2610 Layout* layout,
2611 unsigned int got_count,
2612 unsigned int plt_count)
2614 gold_assert(this->got_ == NULL);
2616 this->got_ = new Output_data_got<64, false>(got_count * 8);
2617 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2618 (elfcpp::SHF_ALLOC
2619 | elfcpp::SHF_WRITE),
2620 this->got_, ORDER_RELRO_LAST,
2621 true);
2623 // Add the three reserved entries.
2624 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
2625 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2626 (elfcpp::SHF_ALLOC
2627 | elfcpp::SHF_WRITE),
2628 this->got_plt_, ORDER_NON_RELRO_FIRST,
2629 false);
2631 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2632 this->global_offset_table_ =
2633 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2634 Symbol_table::PREDEFINED,
2635 this->got_plt_,
2636 0, 0, elfcpp::STT_OBJECT,
2637 elfcpp::STB_LOCAL,
2638 elfcpp::STV_HIDDEN, 0,
2639 false, false);
2641 // If there are any TLSDESC relocations, they get GOT entries in
2642 // .got.plt after the jump slot entries.
2643 // FIXME: Get the count for TLSDESC entries.
2644 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
2645 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2646 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2647 this->got_tlsdesc_,
2648 ORDER_NON_RELRO_FIRST, false);
2650 // If there are any IRELATIVE relocations, they get GOT entries in
2651 // .got.plt after the jump slot and TLSDESC entries.
2652 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2653 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2654 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2655 this->got_irelative_,
2656 ORDER_NON_RELRO_FIRST, false);
2658 // Create the PLT section.
2659 this->plt_ = this->make_data_plt(layout, this->got_,
2660 this->got_plt_,
2661 this->got_irelative_,
2662 plt_count);
2664 // Add unwind information if requested.
2665 if (parameters->options().ld_generated_unwind_info())
2666 this->plt_->add_eh_frame(layout);
2668 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2669 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2670 this->plt_, ORDER_PLT, false);
2672 // Make the sh_info field of .rela.plt point to .plt.
2673 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2674 rela_plt_os->set_info_section(this->plt_->output_section());
2676 // Create the rela_dyn section.
2677 this->rela_dyn_section(layout);
2679 return this->got_;
2682 // Reserve a GOT entry for a local symbol, and regenerate any
2683 // necessary dynamic relocations.
2685 template<int size>
2686 void
2687 Target_x86_64<size>::reserve_local_got_entry(
2688 unsigned int got_index,
2689 Sized_relobj<size, false>* obj,
2690 unsigned int r_sym,
2691 unsigned int got_type)
2693 unsigned int got_offset = got_index * 8;
2694 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2696 this->got_->reserve_local(got_index, obj, r_sym, got_type);
2697 switch (got_type)
2699 case GOT_TYPE_STANDARD:
2700 if (parameters->options().output_is_position_independent())
2701 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
2702 this->got_, got_offset, 0, false);
2703 break;
2704 case GOT_TYPE_TLS_OFFSET:
2705 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
2706 this->got_, got_offset, 0);
2707 break;
2708 case GOT_TYPE_TLS_PAIR:
2709 this->got_->reserve_slot(got_index + 1);
2710 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
2711 this->got_, got_offset, 0);
2712 break;
2713 case GOT_TYPE_TLS_DESC:
2714 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2715 // this->got_->reserve_slot(got_index + 1);
2716 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2717 // this->got_, got_offset, 0);
2718 break;
2719 default:
2720 gold_unreachable();
2724 // Reserve a GOT entry for a global symbol, and regenerate any
2725 // necessary dynamic relocations.
2727 template<int size>
2728 void
2729 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
2730 Symbol* gsym,
2731 unsigned int got_type)
2733 unsigned int got_offset = got_index * 8;
2734 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2736 this->got_->reserve_global(got_index, gsym, got_type);
2737 switch (got_type)
2739 case GOT_TYPE_STANDARD:
2740 if (!gsym->final_value_is_known())
2742 if (gsym->is_from_dynobj()
2743 || gsym->is_undefined()
2744 || gsym->is_preemptible()
2745 || gsym->type() == elfcpp::STT_GNU_IFUNC)
2746 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
2747 this->got_, got_offset, 0);
2748 else
2749 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2750 this->got_, got_offset, 0, false);
2752 break;
2753 case GOT_TYPE_TLS_OFFSET:
2754 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
2755 this->got_, got_offset, 0, false);
2756 break;
2757 case GOT_TYPE_TLS_PAIR:
2758 this->got_->reserve_slot(got_index + 1);
2759 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
2760 this->got_, got_offset, 0, false);
2761 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
2762 this->got_, got_offset + 8, 0, false);
2763 break;
2764 case GOT_TYPE_TLS_DESC:
2765 this->got_->reserve_slot(got_index + 1);
2766 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2767 this->got_, got_offset, 0, false);
2768 break;
2769 default:
2770 gold_unreachable();
2774 // Register an existing PLT entry for a global symbol.
2776 template<int size>
2777 void
2778 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2779 Layout* layout,
2780 unsigned int plt_index,
2781 Symbol* gsym)
2783 gold_assert(this->plt_ != NULL);
2784 gold_assert(!gsym->has_plt_offset());
2786 this->plt_->reserve_slot(plt_index);
2788 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2790 unsigned int got_offset = (plt_index + 3) * 8;
2791 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2794 // Force a COPY relocation for a given symbol.
2796 template<int size>
2797 void
2798 Target_x86_64<size>::emit_copy_reloc(
2799 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2801 this->copy_relocs_.emit_copy_reloc(symtab,
2802 symtab->get_sized_symbol<size>(sym),
2804 offset,
2805 this->rela_dyn_section(NULL));
2808 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2810 template<int size>
2811 void
2812 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2813 Layout* layout)
2815 if (this->tls_base_symbol_defined_)
2816 return;
2818 Output_segment* tls_segment = layout->tls_segment();
2819 if (tls_segment != NULL)
2821 bool is_exec = parameters->options().output_is_executable();
2822 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2823 Symbol_table::PREDEFINED,
2824 tls_segment, 0, 0,
2825 elfcpp::STT_TLS,
2826 elfcpp::STB_LOCAL,
2827 elfcpp::STV_HIDDEN, 0,
2828 (is_exec
2829 ? Symbol::SEGMENT_END
2830 : Symbol::SEGMENT_START),
2831 true);
2833 this->tls_base_symbol_defined_ = true;
2836 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2838 template<int size>
2839 void
2840 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2841 Layout* layout)
2843 if (this->plt_ == NULL)
2844 this->make_plt_section(symtab, layout);
2846 if (!this->plt_->has_tlsdesc_entry())
2848 // Allocate the TLSDESC_GOT entry.
2849 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2850 unsigned int got_offset = got->add_constant(0);
2852 // Allocate the TLSDESC_PLT entry.
2853 this->plt_->reserve_tlsdesc_entry(got_offset);
2857 // Create a GOT entry for the TLS module index.
2859 template<int size>
2860 unsigned int
2861 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2862 Sized_relobj_file<size, false>* object)
2864 if (this->got_mod_index_offset_ == -1U)
2866 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2867 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2868 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2869 unsigned int got_offset = got->add_constant(0);
2870 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2871 got_offset, 0);
2872 got->add_constant(0);
2873 this->got_mod_index_offset_ = got_offset;
2875 return this->got_mod_index_offset_;
2878 // Optimize the TLS relocation type based on what we know about the
2879 // symbol. IS_FINAL is true if the final address of this symbol is
2880 // known at link time.
2882 template<int size>
2883 tls::Tls_optimization
2884 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2886 // If we are generating a shared library, then we can't do anything
2887 // in the linker.
2888 if (parameters->options().shared())
2889 return tls::TLSOPT_NONE;
2891 switch (r_type)
2893 case elfcpp::R_X86_64_TLSGD:
2894 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2895 case elfcpp::R_X86_64_TLSDESC_CALL:
2896 // These are General-Dynamic which permits fully general TLS
2897 // access. Since we know that we are generating an executable,
2898 // we can convert this to Initial-Exec. If we also know that
2899 // this is a local symbol, we can further switch to Local-Exec.
2900 if (is_final)
2901 return tls::TLSOPT_TO_LE;
2902 return tls::TLSOPT_TO_IE;
2904 case elfcpp::R_X86_64_TLSLD:
2905 // This is Local-Dynamic, which refers to a local symbol in the
2906 // dynamic TLS block. Since we know that we generating an
2907 // executable, we can switch to Local-Exec.
2908 return tls::TLSOPT_TO_LE;
2910 case elfcpp::R_X86_64_DTPOFF32:
2911 case elfcpp::R_X86_64_DTPOFF64:
2912 // Another Local-Dynamic reloc.
2913 return tls::TLSOPT_TO_LE;
2915 case elfcpp::R_X86_64_GOTTPOFF:
2916 // These are Initial-Exec relocs which get the thread offset
2917 // from the GOT. If we know that we are linking against the
2918 // local symbol, we can switch to Local-Exec, which links the
2919 // thread offset into the instruction.
2920 if (is_final)
2921 return tls::TLSOPT_TO_LE;
2922 return tls::TLSOPT_NONE;
2924 case elfcpp::R_X86_64_TPOFF32:
2925 // When we already have Local-Exec, there is nothing further we
2926 // can do.
2927 return tls::TLSOPT_NONE;
2929 default:
2930 gold_unreachable();
2934 // Get the Reference_flags for a particular relocation.
2936 template<int size>
2938 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2940 switch (r_type)
2942 case elfcpp::R_X86_64_NONE:
2943 case elfcpp::R_X86_64_GNU_VTINHERIT:
2944 case elfcpp::R_X86_64_GNU_VTENTRY:
2945 case elfcpp::R_X86_64_GOTPC32:
2946 case elfcpp::R_X86_64_GOTPC64:
2947 // No symbol reference.
2948 return 0;
2950 case elfcpp::R_X86_64_64:
2951 case elfcpp::R_X86_64_32:
2952 case elfcpp::R_X86_64_32S:
2953 case elfcpp::R_X86_64_16:
2954 case elfcpp::R_X86_64_8:
2955 return Symbol::ABSOLUTE_REF;
2957 case elfcpp::R_X86_64_PC64:
2958 case elfcpp::R_X86_64_PC32:
2959 case elfcpp::R_X86_64_PC16:
2960 case elfcpp::R_X86_64_PC8:
2961 case elfcpp::R_X86_64_GOTOFF64:
2962 return Symbol::RELATIVE_REF;
2964 case elfcpp::R_X86_64_PLT32:
2965 case elfcpp::R_X86_64_PLTOFF64:
2966 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2968 case elfcpp::R_X86_64_GOT64:
2969 case elfcpp::R_X86_64_GOT32:
2970 case elfcpp::R_X86_64_GOTPCREL64:
2971 case elfcpp::R_X86_64_GOTPCREL:
2972 case elfcpp::R_X86_64_GOTPCRELX:
2973 case elfcpp::R_X86_64_REX_GOTPCRELX:
2974 case elfcpp::R_X86_64_GOTPLT64:
2975 // Absolute in GOT.
2976 return Symbol::ABSOLUTE_REF;
2978 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2979 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2980 case elfcpp::R_X86_64_TLSDESC_CALL:
2981 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2982 case elfcpp::R_X86_64_DTPOFF32:
2983 case elfcpp::R_X86_64_DTPOFF64:
2984 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2985 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2986 return Symbol::TLS_REF;
2988 case elfcpp::R_X86_64_COPY:
2989 case elfcpp::R_X86_64_GLOB_DAT:
2990 case elfcpp::R_X86_64_JUMP_SLOT:
2991 case elfcpp::R_X86_64_RELATIVE:
2992 case elfcpp::R_X86_64_IRELATIVE:
2993 case elfcpp::R_X86_64_TPOFF64:
2994 case elfcpp::R_X86_64_DTPMOD64:
2995 case elfcpp::R_X86_64_TLSDESC:
2996 case elfcpp::R_X86_64_SIZE32:
2997 case elfcpp::R_X86_64_SIZE64:
2998 default:
2999 // Not expected. We will give an error later.
3000 return 0;
3004 // Report an unsupported relocation against a local symbol.
3006 template<int size>
3007 void
3008 Target_x86_64<size>::Scan::unsupported_reloc_local(
3009 Sized_relobj_file<size, false>* object,
3010 unsigned int r_type)
3012 gold_error(_("%s: unsupported reloc %u against local symbol"),
3013 object->name().c_str(), r_type);
3016 // We are about to emit a dynamic relocation of type R_TYPE. If the
3017 // dynamic linker does not support it, issue an error. The GNU linker
3018 // only issues a non-PIC error for an allocated read-only section.
3019 // Here we know the section is allocated, but we don't know that it is
3020 // read-only. But we check for all the relocation types which the
3021 // glibc dynamic linker supports, so it seems appropriate to issue an
3022 // error even if the section is not read-only. If GSYM is not NULL,
3023 // it is the symbol the relocation is against; if it is NULL, the
3024 // relocation is against a local symbol.
3026 template<int size>
3027 void
3028 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
3029 Symbol* gsym)
3031 switch (r_type)
3033 // These are the relocation types supported by glibc for x86_64
3034 // which should always work.
3035 case elfcpp::R_X86_64_RELATIVE:
3036 case elfcpp::R_X86_64_IRELATIVE:
3037 case elfcpp::R_X86_64_GLOB_DAT:
3038 case elfcpp::R_X86_64_JUMP_SLOT:
3039 case elfcpp::R_X86_64_DTPMOD64:
3040 case elfcpp::R_X86_64_DTPOFF64:
3041 case elfcpp::R_X86_64_TPOFF64:
3042 case elfcpp::R_X86_64_64:
3043 case elfcpp::R_X86_64_COPY:
3044 return;
3046 // glibc supports these reloc types, but they can overflow.
3047 case elfcpp::R_X86_64_PC32:
3048 // A PC relative reference is OK against a local symbol or if
3049 // the symbol is defined locally.
3050 if (gsym == NULL
3051 || (!gsym->is_from_dynobj()
3052 && !gsym->is_undefined()
3053 && !gsym->is_preemptible()))
3054 return;
3055 // Fall through.
3056 case elfcpp::R_X86_64_32:
3057 // R_X86_64_32 is OK for x32.
3058 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3059 return;
3060 if (this->issued_non_pic_error_)
3061 return;
3062 gold_assert(parameters->options().output_is_position_independent());
3063 if (gsym == NULL)
3064 object->error(_("requires dynamic R_X86_64_32 reloc which may "
3065 "overflow at runtime; recompile with -fPIC"));
3066 else
3068 const char *r_name;
3069 switch (r_type)
3071 case elfcpp::R_X86_64_32:
3072 r_name = "R_X86_64_32";
3073 break;
3074 case elfcpp::R_X86_64_PC32:
3075 r_name = "R_X86_64_PC32";
3076 break;
3077 default:
3078 gold_unreachable();
3079 break;
3081 object->error(_("requires dynamic %s reloc against '%s' "
3082 "which may overflow at runtime; recompile "
3083 "with -fPIC"),
3084 r_name, gsym->name());
3086 this->issued_non_pic_error_ = true;
3087 return;
3089 default:
3090 // This prevents us from issuing more than one error per reloc
3091 // section. But we can still wind up issuing more than one
3092 // error per object file.
3093 if (this->issued_non_pic_error_)
3094 return;
3095 gold_assert(parameters->options().output_is_position_independent());
3096 object->error(_("requires unsupported dynamic reloc %u; "
3097 "recompile with -fPIC"),
3098 r_type);
3099 this->issued_non_pic_error_ = true;
3100 return;
3102 case elfcpp::R_X86_64_NONE:
3103 gold_unreachable();
3107 // Return whether we need to make a PLT entry for a relocation of the
3108 // given type against a STT_GNU_IFUNC symbol.
3110 template<int size>
3111 bool
3112 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
3113 Sized_relobj_file<size, false>* object,
3114 unsigned int r_type)
3116 int flags = Scan::get_reference_flags(r_type);
3117 if (flags & Symbol::TLS_REF)
3118 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3119 object->name().c_str(), r_type);
3120 return flags != 0;
3123 // Scan a relocation for a local symbol.
3125 template<int size>
3126 inline void
3127 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
3128 Layout* layout,
3129 Target_x86_64<size>* target,
3130 Sized_relobj_file<size, false>* object,
3131 unsigned int data_shndx,
3132 Output_section* output_section,
3133 const elfcpp::Rela<size, false>& reloc,
3134 unsigned int r_type,
3135 const elfcpp::Sym<size, false>& lsym,
3136 bool is_discarded)
3138 if (is_discarded)
3139 return;
3141 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3142 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3143 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3145 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3146 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3149 switch (r_type)
3151 case elfcpp::R_X86_64_NONE:
3152 case elfcpp::R_X86_64_GNU_VTINHERIT:
3153 case elfcpp::R_X86_64_GNU_VTENTRY:
3154 break;
3156 case elfcpp::R_X86_64_64:
3157 // If building a shared library (or a position-independent
3158 // executable), we need to create a dynamic relocation for this
3159 // location. The relocation applied at link time will apply the
3160 // link-time value, so we flag the location with an
3161 // R_X86_64_RELATIVE relocation so the dynamic loader can
3162 // relocate it easily.
3163 if (parameters->options().output_is_position_independent())
3165 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3166 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3167 rela_dyn->add_local_relative(object, r_sym,
3168 (size == 32
3169 ? elfcpp::R_X86_64_RELATIVE64
3170 : elfcpp::R_X86_64_RELATIVE),
3171 output_section, data_shndx,
3172 reloc.get_r_offset(),
3173 reloc.get_r_addend(), is_ifunc);
3175 break;
3177 case elfcpp::R_X86_64_32:
3178 case elfcpp::R_X86_64_32S:
3179 case elfcpp::R_X86_64_16:
3180 case elfcpp::R_X86_64_8:
3181 // If building a shared library (or a position-independent
3182 // executable), we need to create a dynamic relocation for this
3183 // location. We can't use an R_X86_64_RELATIVE relocation
3184 // because that is always a 64-bit relocation.
3185 if (parameters->options().output_is_position_independent())
3187 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3188 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3190 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3191 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3192 rela_dyn->add_local_relative(object, r_sym,
3193 elfcpp::R_X86_64_RELATIVE,
3194 output_section, data_shndx,
3195 reloc.get_r_offset(),
3196 reloc.get_r_addend(), is_ifunc);
3197 break;
3200 this->check_non_pic(object, r_type, NULL);
3202 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3203 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3204 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3205 rela_dyn->add_local(object, r_sym, r_type, output_section,
3206 data_shndx, reloc.get_r_offset(),
3207 reloc.get_r_addend());
3208 else
3210 gold_assert(lsym.get_st_value() == 0);
3211 unsigned int shndx = lsym.get_st_shndx();
3212 bool is_ordinary;
3213 shndx = object->adjust_sym_shndx(r_sym, shndx,
3214 &is_ordinary);
3215 if (!is_ordinary)
3216 object->error(_("section symbol %u has bad shndx %u"),
3217 r_sym, shndx);
3218 else
3219 rela_dyn->add_local_section(object, shndx,
3220 r_type, output_section,
3221 data_shndx, reloc.get_r_offset(),
3222 reloc.get_r_addend());
3225 break;
3227 case elfcpp::R_X86_64_PC64:
3228 case elfcpp::R_X86_64_PC32:
3229 case elfcpp::R_X86_64_PC16:
3230 case elfcpp::R_X86_64_PC8:
3231 break;
3233 case elfcpp::R_X86_64_PLT32:
3234 // Since we know this is a local symbol, we can handle this as a
3235 // PC32 reloc.
3236 break;
3238 case elfcpp::R_X86_64_GOTPC32:
3239 case elfcpp::R_X86_64_GOTOFF64:
3240 case elfcpp::R_X86_64_GOTPC64:
3241 case elfcpp::R_X86_64_PLTOFF64:
3242 // We need a GOT section.
3243 target->got_section(symtab, layout);
3244 // For PLTOFF64, we'd normally want a PLT section, but since we
3245 // know this is a local symbol, no PLT is needed.
3246 break;
3248 case elfcpp::R_X86_64_GOT64:
3249 case elfcpp::R_X86_64_GOT32:
3250 case elfcpp::R_X86_64_GOTPCREL64:
3251 case elfcpp::R_X86_64_GOTPCREL:
3252 case elfcpp::R_X86_64_GOTPCRELX:
3253 case elfcpp::R_X86_64_REX_GOTPCRELX:
3254 case elfcpp::R_X86_64_GOTPLT64:
3256 // The symbol requires a GOT section.
3257 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3259 // If the relocation symbol isn't IFUNC,
3260 // and is local, then we will convert
3261 // mov foo@GOTPCREL(%rip), %reg
3262 // to lea foo(%rip), %reg.
3263 // in Relocate::relocate.
3264 if (!parameters->incremental()
3265 && (r_type == elfcpp::R_X86_64_GOTPCREL
3266 || r_type == elfcpp::R_X86_64_GOTPCRELX
3267 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3268 && reloc.get_r_addend() == -4
3269 && reloc.get_r_offset() >= 2
3270 && !is_ifunc)
3272 section_size_type stype;
3273 const unsigned char* view = object->section_contents(data_shndx,
3274 &stype, true);
3275 if (view[reloc.get_r_offset() - 2] == 0x8b)
3276 break;
3279 // The symbol requires a GOT entry.
3280 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3282 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3283 // lets function pointers compare correctly with shared
3284 // libraries. Otherwise we would need an IRELATIVE reloc.
3285 bool is_new;
3286 if (is_ifunc)
3287 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3288 else
3289 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3290 if (is_new)
3292 // If we are generating a shared object, we need to add a
3293 // dynamic relocation for this symbol's GOT entry.
3294 if (parameters->options().output_is_position_independent())
3296 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3297 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3298 if (r_type != elfcpp::R_X86_64_GOT32)
3300 unsigned int got_offset =
3301 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3302 rela_dyn->add_local_relative(object, r_sym,
3303 elfcpp::R_X86_64_RELATIVE,
3304 got, got_offset, 0, is_ifunc);
3306 else
3308 this->check_non_pic(object, r_type, NULL);
3310 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3311 rela_dyn->add_local(
3312 object, r_sym, r_type, got,
3313 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3317 // For GOTPLT64, we'd normally want a PLT section, but since
3318 // we know this is a local symbol, no PLT is needed.
3320 break;
3322 case elfcpp::R_X86_64_COPY:
3323 case elfcpp::R_X86_64_GLOB_DAT:
3324 case elfcpp::R_X86_64_JUMP_SLOT:
3325 case elfcpp::R_X86_64_RELATIVE:
3326 case elfcpp::R_X86_64_IRELATIVE:
3327 // These are outstanding tls relocs, which are unexpected when linking
3328 case elfcpp::R_X86_64_TPOFF64:
3329 case elfcpp::R_X86_64_DTPMOD64:
3330 case elfcpp::R_X86_64_TLSDESC:
3331 gold_error(_("%s: unexpected reloc %u in object file"),
3332 object->name().c_str(), r_type);
3333 break;
3335 // These are initial tls relocs, which are expected when linking
3336 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3337 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3338 case elfcpp::R_X86_64_TLSDESC_CALL:
3339 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3340 case elfcpp::R_X86_64_DTPOFF32:
3341 case elfcpp::R_X86_64_DTPOFF64:
3342 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3343 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3345 bool output_is_shared = parameters->options().shared();
3346 const tls::Tls_optimization optimized_type
3347 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3348 r_type);
3349 switch (r_type)
3351 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3352 if (optimized_type == tls::TLSOPT_NONE)
3354 // Create a pair of GOT entries for the module index and
3355 // dtv-relative offset.
3356 Output_data_got<64, false>* got
3357 = target->got_section(symtab, layout);
3358 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3359 unsigned int shndx = lsym.get_st_shndx();
3360 bool is_ordinary;
3361 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3362 if (!is_ordinary)
3363 object->error(_("local symbol %u has bad shndx %u"),
3364 r_sym, shndx);
3365 else
3366 got->add_local_pair_with_rel(object, r_sym,
3367 shndx,
3368 GOT_TYPE_TLS_PAIR,
3369 target->rela_dyn_section(layout),
3370 elfcpp::R_X86_64_DTPMOD64);
3372 else if (optimized_type != tls::TLSOPT_TO_LE)
3373 unsupported_reloc_local(object, r_type);
3374 break;
3376 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3377 target->define_tls_base_symbol(symtab, layout);
3378 if (optimized_type == tls::TLSOPT_NONE)
3380 // Create reserved PLT and GOT entries for the resolver.
3381 target->reserve_tlsdesc_entries(symtab, layout);
3383 // Generate a double GOT entry with an
3384 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3385 // is resolved lazily, so the GOT entry needs to be in
3386 // an area in .got.plt, not .got. Call got_section to
3387 // make sure the section has been created.
3388 target->got_section(symtab, layout);
3389 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3390 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3391 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3393 unsigned int got_offset = got->add_constant(0);
3394 got->add_constant(0);
3395 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3396 got_offset);
3397 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3398 // We store the arguments we need in a vector, and
3399 // use the index into the vector as the parameter
3400 // to pass to the target specific routines.
3401 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3402 void* arg = reinterpret_cast<void*>(intarg);
3403 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3404 got, got_offset, 0);
3407 else if (optimized_type != tls::TLSOPT_TO_LE)
3408 unsupported_reloc_local(object, r_type);
3409 break;
3411 case elfcpp::R_X86_64_TLSDESC_CALL:
3412 break;
3414 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3415 if (optimized_type == tls::TLSOPT_NONE)
3417 // Create a GOT entry for the module index.
3418 target->got_mod_index_entry(symtab, layout, object);
3420 else if (optimized_type != tls::TLSOPT_TO_LE)
3421 unsupported_reloc_local(object, r_type);
3422 break;
3424 case elfcpp::R_X86_64_DTPOFF32:
3425 case elfcpp::R_X86_64_DTPOFF64:
3426 break;
3428 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3429 layout->set_has_static_tls();
3430 if (optimized_type == tls::TLSOPT_NONE)
3432 // Create a GOT entry for the tp-relative offset.
3433 Output_data_got<64, false>* got
3434 = target->got_section(symtab, layout);
3435 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3436 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3437 target->rela_dyn_section(layout),
3438 elfcpp::R_X86_64_TPOFF64);
3440 else if (optimized_type != tls::TLSOPT_TO_LE)
3441 unsupported_reloc_local(object, r_type);
3442 break;
3444 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3445 layout->set_has_static_tls();
3446 if (output_is_shared)
3447 unsupported_reloc_local(object, r_type);
3448 break;
3450 default:
3451 gold_unreachable();
3454 break;
3456 case elfcpp::R_X86_64_SIZE32:
3457 case elfcpp::R_X86_64_SIZE64:
3458 default:
3459 gold_error(_("%s: unsupported reloc %u against local symbol"),
3460 object->name().c_str(), r_type);
3461 break;
3466 // Report an unsupported relocation against a global symbol.
3468 template<int size>
3469 void
3470 Target_x86_64<size>::Scan::unsupported_reloc_global(
3471 Sized_relobj_file<size, false>* object,
3472 unsigned int r_type,
3473 Symbol* gsym)
3475 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3476 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3479 // Returns true if this relocation type could be that of a function pointer.
3480 template<int size>
3481 inline bool
3482 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3483 Sized_relobj_file<size, false>* src_obj,
3484 unsigned int src_indx,
3485 unsigned int r_offset,
3486 unsigned int r_type)
3488 switch (r_type)
3490 case elfcpp::R_X86_64_64:
3491 case elfcpp::R_X86_64_32:
3492 case elfcpp::R_X86_64_32S:
3493 case elfcpp::R_X86_64_16:
3494 case elfcpp::R_X86_64_8:
3495 case elfcpp::R_X86_64_GOT64:
3496 case elfcpp::R_X86_64_GOT32:
3497 case elfcpp::R_X86_64_GOTPCREL64:
3498 case elfcpp::R_X86_64_GOTPCREL:
3499 case elfcpp::R_X86_64_GOTPCRELX:
3500 case elfcpp::R_X86_64_REX_GOTPCRELX:
3501 case elfcpp::R_X86_64_GOTPLT64:
3503 return true;
3505 case elfcpp::R_X86_64_PC32:
3507 // This relocation may be used both for function calls and
3508 // for taking address of a function. We distinguish between
3509 // them by checking the opcodes.
3510 uint64_t sh_flags = src_obj->section_flags(src_indx);
3511 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3512 if (is_executable)
3514 section_size_type stype;
3515 const unsigned char* view = src_obj->section_contents(src_indx,
3516 &stype,
3517 true);
3519 // call
3520 if (r_offset >= 1
3521 && view[r_offset - 1] == 0xe8)
3522 return false;
3524 // jmp
3525 if (r_offset >= 1
3526 && view[r_offset - 1] == 0xe9)
3527 return false;
3529 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3530 if (r_offset >= 2
3531 && view[r_offset - 2] == 0x0f
3532 && view[r_offset - 1] >= 0x80
3533 && view[r_offset - 1] <= 0x8f)
3534 return false;
3537 // Be conservative and treat all others as function pointers.
3538 return true;
3541 return false;
3544 // For safe ICF, scan a relocation for a local symbol to check if it
3545 // corresponds to a function pointer being taken. In that case mark
3546 // the function whose pointer was taken as not foldable.
3548 template<int size>
3549 inline bool
3550 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3551 Symbol_table* ,
3552 Layout* ,
3553 Target_x86_64<size>* ,
3554 Sized_relobj_file<size, false>* src_obj,
3555 unsigned int src_indx,
3556 Output_section* ,
3557 const elfcpp::Rela<size, false>& reloc,
3558 unsigned int r_type,
3559 const elfcpp::Sym<size, false>&)
3561 return possible_function_pointer_reloc(src_obj, src_indx,
3562 reloc.get_r_offset(), r_type);
3565 // For safe ICF, scan a relocation for a global symbol to check if it
3566 // corresponds to a function pointer being taken. In that case mark
3567 // the function whose pointer was taken as not foldable.
3569 template<int size>
3570 inline bool
3571 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3572 Symbol_table*,
3573 Layout* ,
3574 Target_x86_64<size>* ,
3575 Sized_relobj_file<size, false>* src_obj,
3576 unsigned int src_indx,
3577 Output_section* ,
3578 const elfcpp::Rela<size, false>& reloc,
3579 unsigned int r_type,
3580 Symbol*)
3582 return possible_function_pointer_reloc(src_obj, src_indx,
3583 reloc.get_r_offset(), r_type);
3586 // Scan a relocation for a global symbol.
3588 template<int size>
3589 inline void
3590 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3591 Layout* layout,
3592 Target_x86_64<size>* target,
3593 Sized_relobj_file<size, false>* object,
3594 unsigned int data_shndx,
3595 Output_section* output_section,
3596 const elfcpp::Rela<size, false>& reloc,
3597 unsigned int r_type,
3598 Symbol* gsym)
3600 // A STT_GNU_IFUNC symbol may require a PLT entry.
3601 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3602 && this->reloc_needs_plt_for_ifunc(object, r_type))
3603 target->make_plt_entry(symtab, layout, gsym);
3605 switch (r_type)
3607 case elfcpp::R_X86_64_NONE:
3608 case elfcpp::R_X86_64_GNU_VTINHERIT:
3609 case elfcpp::R_X86_64_GNU_VTENTRY:
3610 break;
3612 case elfcpp::R_X86_64_64:
3613 case elfcpp::R_X86_64_32:
3614 case elfcpp::R_X86_64_32S:
3615 case elfcpp::R_X86_64_16:
3616 case elfcpp::R_X86_64_8:
3618 // Make a PLT entry if necessary.
3619 if (gsym->needs_plt_entry())
3621 target->make_plt_entry(symtab, layout, gsym);
3622 // Since this is not a PC-relative relocation, we may be
3623 // taking the address of a function. In that case we need to
3624 // set the entry in the dynamic symbol table to the address of
3625 // the PLT entry.
3626 if (gsym->is_from_dynobj() && !parameters->options().shared())
3627 gsym->set_needs_dynsym_value();
3629 // Make a dynamic relocation if necessary.
3630 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3632 if (!parameters->options().output_is_position_independent()
3633 && gsym->may_need_copy_reloc())
3635 target->copy_reloc(symtab, layout, object,
3636 data_shndx, output_section, gsym, reloc);
3638 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3639 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3640 && gsym->type() == elfcpp::STT_GNU_IFUNC
3641 && gsym->can_use_relative_reloc(false)
3642 && !gsym->is_from_dynobj()
3643 && !gsym->is_undefined()
3644 && !gsym->is_preemptible())
3646 // Use an IRELATIVE reloc for a locally defined
3647 // STT_GNU_IFUNC symbol. This makes a function
3648 // address in a PIE executable match the address in a
3649 // shared library that it links against.
3650 Reloc_section* rela_dyn =
3651 target->rela_irelative_section(layout);
3652 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3653 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3654 output_section, object,
3655 data_shndx,
3656 reloc.get_r_offset(),
3657 reloc.get_r_addend());
3659 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3660 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3661 && gsym->can_use_relative_reloc(false))
3663 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3664 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3665 output_section, object,
3666 data_shndx,
3667 reloc.get_r_offset(),
3668 reloc.get_r_addend(), false);
3670 else
3672 this->check_non_pic(object, r_type, gsym);
3673 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3674 rela_dyn->add_global(gsym, r_type, output_section, object,
3675 data_shndx, reloc.get_r_offset(),
3676 reloc.get_r_addend());
3680 break;
3682 case elfcpp::R_X86_64_PC64:
3683 case elfcpp::R_X86_64_PC32:
3684 case elfcpp::R_X86_64_PC16:
3685 case elfcpp::R_X86_64_PC8:
3687 // Make a PLT entry if necessary.
3688 if (gsym->needs_plt_entry())
3689 target->make_plt_entry(symtab, layout, gsym);
3690 // Make a dynamic relocation if necessary.
3691 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3693 if (parameters->options().output_is_executable()
3694 && gsym->may_need_copy_reloc())
3696 target->copy_reloc(symtab, layout, object,
3697 data_shndx, output_section, gsym, reloc);
3699 else
3701 this->check_non_pic(object, r_type, gsym);
3702 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3703 rela_dyn->add_global(gsym, r_type, output_section, object,
3704 data_shndx, reloc.get_r_offset(),
3705 reloc.get_r_addend());
3709 break;
3711 case elfcpp::R_X86_64_GOT64:
3712 case elfcpp::R_X86_64_GOT32:
3713 case elfcpp::R_X86_64_GOTPCREL64:
3714 case elfcpp::R_X86_64_GOTPCREL:
3715 case elfcpp::R_X86_64_GOTPCRELX:
3716 case elfcpp::R_X86_64_REX_GOTPCRELX:
3717 case elfcpp::R_X86_64_GOTPLT64:
3719 // The symbol requires a GOT entry.
3720 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3722 // If we convert this from
3723 // mov foo@GOTPCREL(%rip), %reg
3724 // to lea foo(%rip), %reg.
3725 // OR
3726 // if we convert
3727 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3728 // (callq|jmpq) foo
3729 // in Relocate::relocate, then there is nothing to do here.
3730 // We cannot make these optimizations in incremental linking mode,
3731 // because we look at the opcode to decide whether or not to make
3732 // change, and during an incremental update, the change may have
3733 // already been applied.
3735 Lazy_view<size> view(object, data_shndx);
3736 size_t r_offset = reloc.get_r_offset();
3737 if (!parameters->incremental()
3738 && reloc.get_r_addend() == -4
3739 && r_offset >= 2
3740 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3741 r_offset, &view))
3742 break;
3744 if (!parameters->incremental()
3745 && r_offset >= 2
3746 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3747 r_offset,
3748 &view))
3749 break;
3751 if (gsym->final_value_is_known())
3753 // For a STT_GNU_IFUNC symbol we want the PLT address.
3754 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3755 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3756 else
3757 got->add_global(gsym, GOT_TYPE_STANDARD);
3759 else
3761 // If this symbol is not fully resolved, we need to add a
3762 // dynamic relocation for it.
3763 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3765 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3767 // 1) The symbol may be defined in some other module.
3769 // 2) We are building a shared library and this is a
3770 // protected symbol; using GLOB_DAT means that the dynamic
3771 // linker can use the address of the PLT in the main
3772 // executable when appropriate so that function address
3773 // comparisons work.
3775 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3776 // code, again so that function address comparisons work.
3777 if (gsym->is_from_dynobj()
3778 || gsym->is_undefined()
3779 || gsym->is_preemptible()
3780 || (gsym->visibility() == elfcpp::STV_PROTECTED
3781 && parameters->options().shared())
3782 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3783 && parameters->options().output_is_position_independent()))
3784 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3785 elfcpp::R_X86_64_GLOB_DAT);
3786 else
3788 // For a STT_GNU_IFUNC symbol we want to write the PLT
3789 // offset into the GOT, so that function pointer
3790 // comparisons work correctly.
3791 bool is_new;
3792 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3793 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3794 else
3796 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3797 // Tell the dynamic linker to use the PLT address
3798 // when resolving relocations.
3799 if (gsym->is_from_dynobj()
3800 && !parameters->options().shared())
3801 gsym->set_needs_dynsym_value();
3803 if (is_new)
3805 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3806 rela_dyn->add_global_relative(gsym,
3807 elfcpp::R_X86_64_RELATIVE,
3808 got, got_off, 0, false);
3813 break;
3815 case elfcpp::R_X86_64_PLT32:
3816 // If the symbol is fully resolved, this is just a PC32 reloc.
3817 // Otherwise we need a PLT entry.
3818 if (gsym->final_value_is_known())
3819 break;
3820 // If building a shared library, we can also skip the PLT entry
3821 // if the symbol is defined in the output file and is protected
3822 // or hidden.
3823 if (gsym->is_defined()
3824 && !gsym->is_from_dynobj()
3825 && !gsym->is_preemptible())
3826 break;
3827 target->make_plt_entry(symtab, layout, gsym);
3828 break;
3830 case elfcpp::R_X86_64_GOTPC32:
3831 case elfcpp::R_X86_64_GOTOFF64:
3832 case elfcpp::R_X86_64_GOTPC64:
3833 case elfcpp::R_X86_64_PLTOFF64:
3834 // We need a GOT section.
3835 target->got_section(symtab, layout);
3836 // For PLTOFF64, we also need a PLT entry (but only if the
3837 // symbol is not fully resolved).
3838 if (r_type == elfcpp::R_X86_64_PLTOFF64
3839 && !gsym->final_value_is_known())
3840 target->make_plt_entry(symtab, layout, gsym);
3841 break;
3843 case elfcpp::R_X86_64_COPY:
3844 case elfcpp::R_X86_64_GLOB_DAT:
3845 case elfcpp::R_X86_64_JUMP_SLOT:
3846 case elfcpp::R_X86_64_RELATIVE:
3847 case elfcpp::R_X86_64_IRELATIVE:
3848 // These are outstanding tls relocs, which are unexpected when linking
3849 case elfcpp::R_X86_64_TPOFF64:
3850 case elfcpp::R_X86_64_DTPMOD64:
3851 case elfcpp::R_X86_64_TLSDESC:
3852 gold_error(_("%s: unexpected reloc %u in object file"),
3853 object->name().c_str(), r_type);
3854 break;
3856 // These are initial tls relocs, which are expected for global()
3857 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3858 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3859 case elfcpp::R_X86_64_TLSDESC_CALL:
3860 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3861 case elfcpp::R_X86_64_DTPOFF32:
3862 case elfcpp::R_X86_64_DTPOFF64:
3863 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3864 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3866 // For the Initial-Exec model, we can treat undef symbols as final
3867 // when building an executable.
3868 const bool is_final = (gsym->final_value_is_known() ||
3869 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3870 gsym->is_undefined() &&
3871 parameters->options().output_is_executable()));
3872 const tls::Tls_optimization optimized_type
3873 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3874 switch (r_type)
3876 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3877 if (optimized_type == tls::TLSOPT_NONE)
3879 // Create a pair of GOT entries for the module index and
3880 // dtv-relative offset.
3881 Output_data_got<64, false>* got
3882 = target->got_section(symtab, layout);
3883 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3884 target->rela_dyn_section(layout),
3885 elfcpp::R_X86_64_DTPMOD64,
3886 elfcpp::R_X86_64_DTPOFF64);
3888 else if (optimized_type == tls::TLSOPT_TO_IE)
3890 // Create a GOT entry for the tp-relative offset.
3891 Output_data_got<64, false>* got
3892 = target->got_section(symtab, layout);
3893 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3894 target->rela_dyn_section(layout),
3895 elfcpp::R_X86_64_TPOFF64);
3897 else if (optimized_type != tls::TLSOPT_TO_LE)
3898 unsupported_reloc_global(object, r_type, gsym);
3899 break;
3901 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3902 target->define_tls_base_symbol(symtab, layout);
3903 if (optimized_type == tls::TLSOPT_NONE)
3905 // Create reserved PLT and GOT entries for the resolver.
3906 target->reserve_tlsdesc_entries(symtab, layout);
3908 // Create a double GOT entry with an R_X86_64_TLSDESC
3909 // reloc. The R_X86_64_TLSDESC reloc is resolved
3910 // lazily, so the GOT entry needs to be in an area in
3911 // .got.plt, not .got. Call got_section to make sure
3912 // the section has been created.
3913 target->got_section(symtab, layout);
3914 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3915 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3916 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3917 elfcpp::R_X86_64_TLSDESC, 0);
3919 else if (optimized_type == tls::TLSOPT_TO_IE)
3921 // Create a GOT entry for the tp-relative offset.
3922 Output_data_got<64, false>* got
3923 = target->got_section(symtab, layout);
3924 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3925 target->rela_dyn_section(layout),
3926 elfcpp::R_X86_64_TPOFF64);
3928 else if (optimized_type != tls::TLSOPT_TO_LE)
3929 unsupported_reloc_global(object, r_type, gsym);
3930 break;
3932 case elfcpp::R_X86_64_TLSDESC_CALL:
3933 break;
3935 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3936 if (optimized_type == tls::TLSOPT_NONE)
3938 // Create a GOT entry for the module index.
3939 target->got_mod_index_entry(symtab, layout, object);
3941 else if (optimized_type != tls::TLSOPT_TO_LE)
3942 unsupported_reloc_global(object, r_type, gsym);
3943 break;
3945 case elfcpp::R_X86_64_DTPOFF32:
3946 case elfcpp::R_X86_64_DTPOFF64:
3947 break;
3949 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3950 layout->set_has_static_tls();
3951 if (optimized_type == tls::TLSOPT_NONE)
3953 // Create a GOT entry for the tp-relative offset.
3954 Output_data_got<64, false>* got
3955 = target->got_section(symtab, layout);
3956 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3957 target->rela_dyn_section(layout),
3958 elfcpp::R_X86_64_TPOFF64);
3960 else if (optimized_type != tls::TLSOPT_TO_LE)
3961 unsupported_reloc_global(object, r_type, gsym);
3962 break;
3964 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3965 layout->set_has_static_tls();
3966 if (parameters->options().shared())
3967 unsupported_reloc_global(object, r_type, gsym);
3968 break;
3970 default:
3971 gold_unreachable();
3974 break;
3976 case elfcpp::R_X86_64_SIZE32:
3977 case elfcpp::R_X86_64_SIZE64:
3978 default:
3979 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3980 object->name().c_str(), r_type,
3981 gsym->demangled_name().c_str());
3982 break;
3986 template<int size>
3987 void
3988 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3989 Layout* layout,
3990 Sized_relobj_file<size, false>* object,
3991 unsigned int data_shndx,
3992 unsigned int sh_type,
3993 const unsigned char* prelocs,
3994 size_t reloc_count,
3995 Output_section* output_section,
3996 bool needs_special_offset_handling,
3997 size_t local_symbol_count,
3998 const unsigned char* plocal_symbols)
4000 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4001 Classify_reloc;
4003 if (sh_type == elfcpp::SHT_REL)
4005 return;
4008 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
4009 Classify_reloc>(
4010 symtab,
4011 layout,
4012 this,
4013 object,
4014 data_shndx,
4015 prelocs,
4016 reloc_count,
4017 output_section,
4018 needs_special_offset_handling,
4019 local_symbol_count,
4020 plocal_symbols);
4023 // Scan relocations for a section.
4025 template<int size>
4026 void
4027 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
4028 Layout* layout,
4029 Sized_relobj_file<size, false>* object,
4030 unsigned int data_shndx,
4031 unsigned int sh_type,
4032 const unsigned char* prelocs,
4033 size_t reloc_count,
4034 Output_section* output_section,
4035 bool needs_special_offset_handling,
4036 size_t local_symbol_count,
4037 const unsigned char* plocal_symbols)
4039 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4040 Classify_reloc;
4042 if (sh_type == elfcpp::SHT_REL)
4044 gold_error(_("%s: unsupported REL reloc section"),
4045 object->name().c_str());
4046 return;
4049 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
4050 symtab,
4051 layout,
4052 this,
4053 object,
4054 data_shndx,
4055 prelocs,
4056 reloc_count,
4057 output_section,
4058 needs_special_offset_handling,
4059 local_symbol_count,
4060 plocal_symbols);
4063 // Finalize the sections.
4065 template<int size>
4066 void
4067 Target_x86_64<size>::do_finalize_sections(
4068 Layout* layout,
4069 const Input_objects*,
4070 Symbol_table* symtab)
4072 const Reloc_section* rel_plt = (this->plt_ == NULL
4073 ? NULL
4074 : this->plt_->rela_plt());
4075 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4076 this->rela_dyn_, true, false, false);
4078 // Fill in some more dynamic tags.
4079 Output_data_dynamic* const odyn = layout->dynamic_data();
4080 if (odyn != NULL)
4082 if (this->plt_ != NULL
4083 && this->plt_->output_section() != NULL
4084 && this->plt_->has_tlsdesc_entry())
4086 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
4087 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
4088 this->got_->finalize_data_size();
4089 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
4090 this->plt_, plt_offset);
4091 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
4092 this->got_, got_offset);
4096 // Emit any relocs we saved in an attempt to avoid generating COPY
4097 // relocs.
4098 if (this->copy_relocs_.any_saved_relocs())
4099 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4101 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4102 // the .got.plt section.
4103 Symbol* sym = this->global_offset_table_;
4104 if (sym != NULL)
4106 uint64_t data_size = this->got_plt_->current_data_size();
4107 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4110 if (parameters->doing_static_link()
4111 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4113 // If linking statically, make sure that the __rela_iplt symbols
4114 // were defined if necessary, even if we didn't create a PLT.
4115 static const Define_symbol_in_segment syms[] =
4118 "__rela_iplt_start", // name
4119 elfcpp::PT_LOAD, // segment_type
4120 elfcpp::PF_W, // segment_flags_set
4121 elfcpp::PF(0), // segment_flags_clear
4122 0, // value
4123 0, // size
4124 elfcpp::STT_NOTYPE, // type
4125 elfcpp::STB_GLOBAL, // binding
4126 elfcpp::STV_HIDDEN, // visibility
4127 0, // nonvis
4128 Symbol::SEGMENT_START, // offset_from_base
4129 true // only_if_ref
4132 "__rela_iplt_end", // name
4133 elfcpp::PT_LOAD, // segment_type
4134 elfcpp::PF_W, // segment_flags_set
4135 elfcpp::PF(0), // segment_flags_clear
4136 0, // value
4137 0, // size
4138 elfcpp::STT_NOTYPE, // type
4139 elfcpp::STB_GLOBAL, // binding
4140 elfcpp::STV_HIDDEN, // visibility
4141 0, // nonvis
4142 Symbol::SEGMENT_START, // offset_from_base
4143 true // only_if_ref
4147 symtab->define_symbols(layout, 2, syms,
4148 layout->script_options()->saw_sections_clause());
4152 // For x32, we need to handle PC-relative relocations using full 64-bit
4153 // arithmetic, so that we can detect relocation overflows properly.
4154 // This class overrides the pcrela32_check methods from the defaults in
4155 // Relocate_functions in reloc.h.
4157 template<int size>
4158 class X86_64_relocate_functions : public Relocate_functions<size, false>
4160 public:
4161 typedef Relocate_functions<size, false> Base;
4163 // Do a simple PC relative relocation with the addend in the
4164 // relocation.
4165 static inline typename Base::Reloc_status
4166 pcrela32_check(unsigned char* view,
4167 typename elfcpp::Elf_types<64>::Elf_Addr value,
4168 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4169 typename elfcpp::Elf_types<64>::Elf_Addr address)
4171 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4172 Valtype* wv = reinterpret_cast<Valtype*>(view);
4173 value = value + addend - address;
4174 elfcpp::Swap<32, false>::writeval(wv, value);
4175 return (Bits<32>::has_overflow(value)
4176 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4179 // Do a simple PC relative relocation with a Symbol_value with the
4180 // addend in the relocation.
4181 static inline typename Base::Reloc_status
4182 pcrela32_check(unsigned char* view,
4183 const Sized_relobj_file<size, false>* object,
4184 const Symbol_value<size>* psymval,
4185 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4186 typename elfcpp::Elf_types<64>::Elf_Addr address)
4188 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4189 Valtype* wv = reinterpret_cast<Valtype*>(view);
4190 typename elfcpp::Elf_types<64>::Elf_Addr value;
4191 if (addend >= 0)
4192 value = psymval->value(object, addend);
4193 else
4195 // For negative addends, get the symbol value without
4196 // the addend, then add the addend using 64-bit arithmetic.
4197 value = psymval->value(object, 0);
4198 value += addend;
4200 value -= address;
4201 elfcpp::Swap<32, false>::writeval(wv, value);
4202 return (Bits<32>::has_overflow(value)
4203 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4207 // Perform a relocation.
4209 template<int size>
4210 inline bool
4211 Target_x86_64<size>::Relocate::relocate(
4212 const Relocate_info<size, false>* relinfo,
4213 unsigned int,
4214 Target_x86_64<size>* target,
4215 Output_section*,
4216 size_t relnum,
4217 const unsigned char* preloc,
4218 const Sized_symbol<size>* gsym,
4219 const Symbol_value<size>* psymval,
4220 unsigned char* view,
4221 typename elfcpp::Elf_types<size>::Elf_Addr address,
4222 section_size_type view_size)
4224 typedef X86_64_relocate_functions<size> Reloc_funcs;
4225 const elfcpp::Rela<size, false> rela(preloc);
4226 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4228 if (this->skip_call_tls_get_addr_)
4230 if ((r_type != elfcpp::R_X86_64_PLT32
4231 && r_type != elfcpp::R_X86_64_GOTPCREL
4232 && r_type != elfcpp::R_X86_64_GOTPCRELX
4233 && r_type != elfcpp::R_X86_64_PC32)
4234 || gsym == NULL
4235 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4237 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4238 _("missing expected TLS relocation"));
4239 this->skip_call_tls_get_addr_ = false;
4241 else
4243 this->skip_call_tls_get_addr_ = false;
4244 return false;
4248 if (view == NULL)
4249 return true;
4251 const Sized_relobj_file<size, false>* object = relinfo->object;
4253 // Pick the value to use for symbols defined in the PLT.
4254 Symbol_value<size> symval;
4255 if (gsym != NULL
4256 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4258 symval.set_output_value(target->plt_address_for_global(gsym));
4259 psymval = &symval;
4261 else if (gsym == NULL && psymval->is_ifunc_symbol())
4263 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4264 if (object->local_has_plt_offset(r_sym))
4266 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4267 psymval = &symval;
4271 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4273 // Get the GOT offset if needed.
4274 // The GOT pointer points to the end of the GOT section.
4275 // We need to subtract the size of the GOT section to get
4276 // the actual offset to use in the relocation.
4277 bool have_got_offset = false;
4278 // Since the actual offset is always negative, we use signed int to
4279 // support 64-bit GOT relocations.
4280 int got_offset = 0;
4281 switch (r_type)
4283 case elfcpp::R_X86_64_GOT32:
4284 case elfcpp::R_X86_64_GOT64:
4285 case elfcpp::R_X86_64_GOTPLT64:
4286 case elfcpp::R_X86_64_GOTPCREL64:
4287 if (gsym != NULL)
4289 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4290 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4292 else
4294 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4295 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4296 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4297 - target->got_size());
4299 have_got_offset = true;
4300 break;
4302 default:
4303 break;
4306 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4308 switch (r_type)
4310 case elfcpp::R_X86_64_NONE:
4311 case elfcpp::R_X86_64_GNU_VTINHERIT:
4312 case elfcpp::R_X86_64_GNU_VTENTRY:
4313 break;
4315 case elfcpp::R_X86_64_64:
4316 Reloc_funcs::rela64(view, object, psymval, addend);
4317 break;
4319 case elfcpp::R_X86_64_PC64:
4320 Reloc_funcs::pcrela64(view, object, psymval, addend,
4321 address);
4322 break;
4324 case elfcpp::R_X86_64_32:
4325 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4326 Reloc_funcs::CHECK_UNSIGNED);
4327 break;
4329 case elfcpp::R_X86_64_32S:
4330 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4331 Reloc_funcs::CHECK_SIGNED);
4332 break;
4334 case elfcpp::R_X86_64_PC32:
4335 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4336 address);
4337 break;
4339 case elfcpp::R_X86_64_16:
4340 Reloc_funcs::rela16(view, object, psymval, addend);
4341 break;
4343 case elfcpp::R_X86_64_PC16:
4344 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4345 break;
4347 case elfcpp::R_X86_64_8:
4348 Reloc_funcs::rela8(view, object, psymval, addend);
4349 break;
4351 case elfcpp::R_X86_64_PC8:
4352 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4353 break;
4355 case elfcpp::R_X86_64_PLT32:
4356 gold_assert(gsym == NULL
4357 || gsym->has_plt_offset()
4358 || gsym->final_value_is_known()
4359 || (gsym->is_defined()
4360 && !gsym->is_from_dynobj()
4361 && !gsym->is_preemptible()));
4362 // Note: while this code looks the same as for R_X86_64_PC32, it
4363 // behaves differently because psymval was set to point to
4364 // the PLT entry, rather than the symbol, in Scan::global().
4365 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4366 address);
4367 break;
4369 case elfcpp::R_X86_64_PLTOFF64:
4371 gold_assert(gsym);
4372 gold_assert(gsym->has_plt_offset()
4373 || gsym->final_value_is_known());
4374 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4375 // This is the address of GLOBAL_OFFSET_TABLE.
4376 got_address = target->got_plt_section()->address();
4377 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4379 break;
4381 case elfcpp::R_X86_64_GOT32:
4382 gold_assert(have_got_offset);
4383 Reloc_funcs::rela32(view, got_offset, addend);
4384 break;
4386 case elfcpp::R_X86_64_GOTPC32:
4388 gold_assert(gsym);
4389 typename elfcpp::Elf_types<size>::Elf_Addr value;
4390 value = target->got_plt_section()->address();
4391 Reloc_funcs::pcrela32_check(view, value, addend, address);
4393 break;
4395 case elfcpp::R_X86_64_GOT64:
4396 case elfcpp::R_X86_64_GOTPLT64:
4397 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4398 // GOT64.
4399 gold_assert(have_got_offset);
4400 Reloc_funcs::rela64(view, got_offset, addend);
4401 break;
4403 case elfcpp::R_X86_64_GOTPC64:
4405 gold_assert(gsym);
4406 typename elfcpp::Elf_types<size>::Elf_Addr value;
4407 value = target->got_plt_section()->address();
4408 Reloc_funcs::pcrela64(view, value, addend, address);
4410 break;
4412 case elfcpp::R_X86_64_GOTOFF64:
4414 typename elfcpp::Elf_types<size>::Elf_Addr reladdr;
4415 reladdr = target->got_plt_section()->address();
4416 Reloc_funcs::pcrela64(view, object, psymval, addend, reladdr);
4418 break;
4420 case elfcpp::R_X86_64_GOTPCREL:
4421 case elfcpp::R_X86_64_GOTPCRELX:
4422 case elfcpp::R_X86_64_REX_GOTPCRELX:
4424 bool converted_p = false;
4426 if (rela.get_r_addend() == -4)
4428 // Convert
4429 // mov foo@GOTPCREL(%rip), %reg
4430 // to lea foo(%rip), %reg.
4431 // if possible.
4432 if (!parameters->incremental()
4433 && ((gsym == NULL
4434 && rela.get_r_offset() >= 2
4435 && view[-2] == 0x8b
4436 && !psymval->is_ifunc_symbol())
4437 || (gsym != NULL
4438 && rela.get_r_offset() >= 2
4439 && Target_x86_64<size>::can_convert_mov_to_lea(gsym,
4440 r_type,
4442 &view))))
4444 view[-2] = 0x8d;
4445 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4446 converted_p = true;
4448 // Convert
4449 // callq *foo@GOTPCRELX(%rip) to
4450 // addr32 callq foo
4451 // and jmpq *foo@GOTPCRELX(%rip) to
4452 // jmpq foo
4453 // nop
4454 else if (!parameters->incremental()
4455 && gsym != NULL
4456 && rela.get_r_offset() >= 2
4457 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4458 r_type,
4460 &view))
4462 if (view[-1] == 0x15)
4464 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4465 // Opcode of addr32 is 0x67 and opcode of direct callq
4466 // is 0xe8.
4467 view[-2] = 0x67;
4468 view[-1] = 0xe8;
4469 // Convert GOTPCRELX to 32-bit pc relative reloc.
4470 Reloc_funcs::pcrela32(view, object, psymval, addend,
4471 address);
4472 converted_p = true;
4474 else
4476 // Convert jmpq *foo@GOTPCRELX(%rip) to
4477 // jmpq foo
4478 // nop
4479 // The opcode of direct jmpq is 0xe9.
4480 view[-2] = 0xe9;
4481 // The opcode of nop is 0x90.
4482 view[3] = 0x90;
4483 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq
4484 // is rip relative and since the instruction following
4485 // the jmpq is now the nop, offset the address by 1
4486 // byte. The start of the relocation also moves ahead
4487 // by 1 byte.
4488 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4489 address - 1);
4490 converted_p = true;
4495 if (!converted_p)
4497 if (gsym != NULL)
4499 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4500 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4501 - target->got_size());
4503 else
4505 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4506 gold_assert(object->local_has_got_offset(r_sym,
4507 GOT_TYPE_STANDARD));
4508 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4509 - target->got_size());
4511 typename elfcpp::Elf_types<size>::Elf_Addr value;
4512 value = target->got_plt_section()->address() + got_offset;
4513 Reloc_funcs::pcrela32_check(view, value, addend, address);
4516 break;
4518 case elfcpp::R_X86_64_GOTPCREL64:
4520 gold_assert(have_got_offset);
4521 typename elfcpp::Elf_types<size>::Elf_Addr value;
4522 value = target->got_plt_section()->address() + got_offset;
4523 Reloc_funcs::pcrela64(view, value, addend, address);
4525 break;
4527 case elfcpp::R_X86_64_COPY:
4528 case elfcpp::R_X86_64_GLOB_DAT:
4529 case elfcpp::R_X86_64_JUMP_SLOT:
4530 case elfcpp::R_X86_64_RELATIVE:
4531 case elfcpp::R_X86_64_IRELATIVE:
4532 // These are outstanding tls relocs, which are unexpected when linking
4533 case elfcpp::R_X86_64_TPOFF64:
4534 case elfcpp::R_X86_64_DTPMOD64:
4535 case elfcpp::R_X86_64_TLSDESC:
4536 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4537 _("unexpected reloc %u in object file"),
4538 r_type);
4539 break;
4541 // These are initial tls relocs, which are expected when linking
4542 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4543 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4544 case elfcpp::R_X86_64_TLSDESC_CALL:
4545 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4546 case elfcpp::R_X86_64_DTPOFF32:
4547 case elfcpp::R_X86_64_DTPOFF64:
4548 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4549 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4550 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4551 view, address, view_size);
4552 break;
4554 case elfcpp::R_X86_64_SIZE32:
4555 case elfcpp::R_X86_64_SIZE64:
4556 default:
4557 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4558 _("unsupported reloc %u"),
4559 r_type);
4560 break;
4563 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4565 if (gsym == NULL)
4567 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4568 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4569 _("relocation overflow: "
4570 "reference to local symbol %u in %s"),
4571 r_sym, object->name().c_str());
4573 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4575 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4576 _("relocation overflow: "
4577 "reference to '%s' defined in %s"),
4578 gsym->name(),
4579 gsym->object()->name().c_str());
4581 else
4583 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4584 _("relocation overflow: reference to '%s'"),
4585 gsym->name());
4589 return true;
4592 // Perform a TLS relocation.
4594 template<int size>
4595 inline void
4596 Target_x86_64<size>::Relocate::relocate_tls(
4597 const Relocate_info<size, false>* relinfo,
4598 Target_x86_64<size>* target,
4599 size_t relnum,
4600 const elfcpp::Rela<size, false>& rela,
4601 unsigned int r_type,
4602 const Sized_symbol<size>* gsym,
4603 const Symbol_value<size>* psymval,
4604 unsigned char* view,
4605 typename elfcpp::Elf_types<size>::Elf_Addr address,
4606 section_size_type view_size)
4608 Output_segment* tls_segment = relinfo->layout->tls_segment();
4610 const Sized_relobj_file<size, false>* object = relinfo->object;
4611 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4612 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4613 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4615 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4617 const bool is_final = (gsym == NULL
4618 ? !parameters->options().shared()
4619 : gsym->final_value_is_known());
4620 tls::Tls_optimization optimized_type
4621 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4622 switch (r_type)
4624 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4625 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4627 // If this code sequence is used in a non-executable section,
4628 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4629 // on the assumption that it's being used by itself in a debug
4630 // section. Therefore, in the unlikely event that the code
4631 // sequence appears in a non-executable section, we simply
4632 // leave it unoptimized.
4633 optimized_type = tls::TLSOPT_NONE;
4635 if (optimized_type == tls::TLSOPT_TO_LE)
4637 if (tls_segment == NULL)
4639 gold_assert(parameters->errors()->error_count() > 0
4640 || issue_undefined_symbol_error(gsym));
4641 return;
4643 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4644 rela, r_type, value, view,
4645 view_size);
4646 break;
4648 else
4650 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4651 ? GOT_TYPE_TLS_OFFSET
4652 : GOT_TYPE_TLS_PAIR);
4653 unsigned int got_offset;
4654 if (gsym != NULL)
4656 gold_assert(gsym->has_got_offset(got_type));
4657 got_offset = gsym->got_offset(got_type) - target->got_size();
4659 else
4661 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4662 gold_assert(object->local_has_got_offset(r_sym, got_type));
4663 got_offset = (object->local_got_offset(r_sym, got_type)
4664 - target->got_size());
4666 if (optimized_type == tls::TLSOPT_TO_IE)
4668 value = target->got_plt_section()->address() + got_offset;
4669 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4670 value, view, address, view_size);
4671 break;
4673 else if (optimized_type == tls::TLSOPT_NONE)
4675 // Relocate the field with the offset of the pair of GOT
4676 // entries.
4677 value = target->got_plt_section()->address() + got_offset;
4678 Relocate_functions<size, false>::pcrela32(view, value, addend,
4679 address);
4680 break;
4683 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4684 _("unsupported reloc %u"), r_type);
4685 break;
4687 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4688 case elfcpp::R_X86_64_TLSDESC_CALL:
4689 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4691 // See above comment for R_X86_64_TLSGD.
4692 optimized_type = tls::TLSOPT_NONE;
4694 if (optimized_type == tls::TLSOPT_TO_LE)
4696 if (tls_segment == NULL)
4698 gold_assert(parameters->errors()->error_count() > 0
4699 || issue_undefined_symbol_error(gsym));
4700 return;
4702 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4703 rela, r_type, value, view,
4704 view_size);
4705 break;
4707 else
4709 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4710 ? GOT_TYPE_TLS_OFFSET
4711 : GOT_TYPE_TLS_DESC);
4712 unsigned int got_offset = 0;
4713 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4714 && optimized_type == tls::TLSOPT_NONE)
4716 // We created GOT entries in the .got.tlsdesc portion of
4717 // the .got.plt section, but the offset stored in the
4718 // symbol is the offset within .got.tlsdesc.
4719 got_offset = (target->got_size()
4720 + target->got_plt_section()->data_size());
4722 if (gsym != NULL)
4724 gold_assert(gsym->has_got_offset(got_type));
4725 got_offset += gsym->got_offset(got_type) - target->got_size();
4727 else
4729 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4730 gold_assert(object->local_has_got_offset(r_sym, got_type));
4731 got_offset += (object->local_got_offset(r_sym, got_type)
4732 - target->got_size());
4734 if (optimized_type == tls::TLSOPT_TO_IE)
4736 value = target->got_plt_section()->address() + got_offset;
4737 this->tls_desc_gd_to_ie(relinfo, relnum,
4738 rela, r_type, value, view, address,
4739 view_size);
4740 break;
4742 else if (optimized_type == tls::TLSOPT_NONE)
4744 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4746 // Relocate the field with the offset of the pair of GOT
4747 // entries.
4748 value = target->got_plt_section()->address() + got_offset;
4749 Relocate_functions<size, false>::pcrela32(view, value, addend,
4750 address);
4752 break;
4755 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4756 _("unsupported reloc %u"), r_type);
4757 break;
4759 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4760 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4762 // See above comment for R_X86_64_TLSGD.
4763 optimized_type = tls::TLSOPT_NONE;
4765 if (optimized_type == tls::TLSOPT_TO_LE)
4767 if (tls_segment == NULL)
4769 gold_assert(parameters->errors()->error_count() > 0
4770 || issue_undefined_symbol_error(gsym));
4771 return;
4773 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4774 value, view, view_size);
4775 break;
4777 else if (optimized_type == tls::TLSOPT_NONE)
4779 // Relocate the field with the offset of the GOT entry for
4780 // the module index.
4781 unsigned int got_offset;
4782 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4783 - target->got_size());
4784 value = target->got_plt_section()->address() + got_offset;
4785 Relocate_functions<size, false>::pcrela32(view, value, addend,
4786 address);
4787 break;
4789 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4790 _("unsupported reloc %u"), r_type);
4791 break;
4793 case elfcpp::R_X86_64_DTPOFF32:
4794 // This relocation type is used in debugging information.
4795 // In that case we need to not optimize the value. If the
4796 // section is not executable, then we assume we should not
4797 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4798 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4799 // R_X86_64_TLSLD.
4800 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4802 if (tls_segment == NULL)
4804 gold_assert(parameters->errors()->error_count() > 0
4805 || issue_undefined_symbol_error(gsym));
4806 return;
4808 value -= tls_segment->memsz();
4810 Relocate_functions<size, false>::rela32(view, value, addend);
4811 break;
4813 case elfcpp::R_X86_64_DTPOFF64:
4814 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4815 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4817 if (tls_segment == NULL)
4819 gold_assert(parameters->errors()->error_count() > 0
4820 || issue_undefined_symbol_error(gsym));
4821 return;
4823 value -= tls_segment->memsz();
4825 Relocate_functions<size, false>::rela64(view, value, addend);
4826 break;
4828 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4829 if (gsym != NULL
4830 && gsym->is_undefined()
4831 && parameters->options().output_is_executable())
4833 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4834 NULL, rela,
4835 r_type, value, view,
4836 view_size);
4837 break;
4839 else if (optimized_type == tls::TLSOPT_TO_LE)
4841 if (tls_segment == NULL)
4843 gold_assert(parameters->errors()->error_count() > 0
4844 || issue_undefined_symbol_error(gsym));
4845 return;
4847 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4848 tls_segment, rela,
4849 r_type, value, view,
4850 view_size);
4851 break;
4853 else if (optimized_type == tls::TLSOPT_NONE)
4855 // Relocate the field with the offset of the GOT entry for
4856 // the tp-relative offset of the symbol.
4857 unsigned int got_offset;
4858 if (gsym != NULL)
4860 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4861 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4862 - target->got_size());
4864 else
4866 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4867 gold_assert(object->local_has_got_offset(r_sym,
4868 GOT_TYPE_TLS_OFFSET));
4869 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4870 - target->got_size());
4872 value = target->got_plt_section()->address() + got_offset;
4873 Relocate_functions<size, false>::pcrela32(view, value, addend,
4874 address);
4875 break;
4877 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4878 _("unsupported reloc type %u"),
4879 r_type);
4880 break;
4882 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4883 if (tls_segment == NULL)
4885 gold_assert(parameters->errors()->error_count() > 0
4886 || issue_undefined_symbol_error(gsym));
4887 return;
4889 value -= tls_segment->memsz();
4890 Relocate_functions<size, false>::rela32(view, value, addend);
4891 break;
4895 // Do a relocation in which we convert a TLS General-Dynamic to an
4896 // Initial-Exec.
4898 template<int size>
4899 inline void
4900 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4901 const Relocate_info<size, false>* relinfo,
4902 size_t relnum,
4903 const elfcpp::Rela<size, false>& rela,
4904 unsigned int,
4905 typename elfcpp::Elf_types<size>::Elf_Addr value,
4906 unsigned char* view,
4907 typename elfcpp::Elf_types<size>::Elf_Addr address,
4908 section_size_type view_size)
4910 // For SIZE == 64:
4911 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4912 // .word 0x6666; rex64; call __tls_get_addr@PLT
4913 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4914 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4915 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4916 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4917 // For SIZE == 32:
4918 // leaq foo@tlsgd(%rip),%rdi;
4919 // .word 0x6666; rex64; call __tls_get_addr@PLT
4920 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4921 // leaq foo@tlsgd(%rip),%rdi;
4922 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4923 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4925 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4926 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4927 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4928 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4930 if (size == 64)
4932 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4933 -4);
4934 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4935 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4936 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4937 16);
4939 else
4941 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4942 -3);
4943 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4944 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4945 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4946 15);
4949 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4950 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4951 address);
4953 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4954 // We can skip it.
4955 this->skip_call_tls_get_addr_ = true;
4958 // Do a relocation in which we convert a TLS General-Dynamic to a
4959 // Local-Exec.
4961 template<int size>
4962 inline void
4963 Target_x86_64<size>::Relocate::tls_gd_to_le(
4964 const Relocate_info<size, false>* relinfo,
4965 size_t relnum,
4966 Output_segment* tls_segment,
4967 const elfcpp::Rela<size, false>& rela,
4968 unsigned int,
4969 typename elfcpp::Elf_types<size>::Elf_Addr value,
4970 unsigned char* view,
4971 section_size_type view_size)
4973 // For SIZE == 64:
4974 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4975 // .word 0x6666; rex64; call __tls_get_addr@PLT
4976 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4977 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4978 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4979 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4980 // For SIZE == 32:
4981 // leaq foo@tlsgd(%rip),%rdi;
4982 // .word 0x6666; rex64; call __tls_get_addr@PLT
4983 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4984 // leaq foo@tlsgd(%rip),%rdi;
4985 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4986 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4988 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4989 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4990 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4991 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4993 if (size == 64)
4995 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4996 -4);
4997 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4998 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4999 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5000 16);
5002 else
5004 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5005 -3);
5006 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5007 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5009 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5010 15);
5013 value -= tls_segment->memsz();
5014 Relocate_functions<size, false>::rela32(view + 8, value, 0);
5016 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5017 // We can skip it.
5018 this->skip_call_tls_get_addr_ = true;
5021 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5023 template<int size>
5024 inline void
5025 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
5026 const Relocate_info<size, false>* relinfo,
5027 size_t relnum,
5028 const elfcpp::Rela<size, false>& rela,
5029 unsigned int r_type,
5030 typename elfcpp::Elf_types<size>::Elf_Addr value,
5031 unsigned char* view,
5032 typename elfcpp::Elf_types<size>::Elf_Addr address,
5033 section_size_type view_size)
5035 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5037 // LP64: leaq foo@tlsdesc(%rip), %rax
5038 // ==> movq foo@gottpoff(%rip), %rax
5039 // X32: rex leal foo@tlsdesc(%rip), %eax
5040 // ==> rex movl foo@gottpoff(%rip), %eax
5041 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5042 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5043 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5044 (((view[-3] & 0xfb) == 0x48
5045 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5046 && view[-2] == 0x8d
5047 && (view[-1] & 0xc7) == 0x05));
5048 view[-2] = 0x8b;
5049 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5050 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
5052 else
5054 // LP64: call *foo@tlscall(%rax)
5055 // ==> xchg %ax, %ax
5056 // X32: call *foo@tlscall(%eax)
5057 // ==> nopl (%rax)
5058 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5059 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5060 int prefix = 0;
5061 if (size == 32 && view[0] == 0x67)
5063 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5064 view_size, 3);
5065 prefix = 1;
5067 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5068 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5069 if (prefix)
5071 view[0] = 0x0f;
5072 view[1] = 0x1f;
5073 view[2] = 0x00;
5075 else
5077 view[0] = 0x66;
5078 view[1] = 0x90;
5083 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5085 template<int size>
5086 inline void
5087 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
5088 const Relocate_info<size, false>* relinfo,
5089 size_t relnum,
5090 Output_segment* tls_segment,
5091 const elfcpp::Rela<size, false>& rela,
5092 unsigned int r_type,
5093 typename elfcpp::Elf_types<size>::Elf_Addr value,
5094 unsigned char* view,
5095 section_size_type view_size)
5097 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5099 // LP64: leaq foo@tlsdesc(%rip), %rax
5100 // ==> movq foo@tpoff, %rax
5101 // X32: rex leal foo@tlsdesc(%rip), %eax
5102 // ==> rex movl foo@tpoff, %eax
5103 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5104 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5105 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5106 (((view[-3] & 0xfb) == 0x48
5107 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5108 && view[-2] == 0x8d
5109 && (view[-1] & 0xc7) == 0x05));
5110 view[-3] = (view[-3] & 0x48) | ((view[-3] >> 2) & 1);
5111 view[-2] = 0xc7;
5112 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5113 value -= tls_segment->memsz();
5114 Relocate_functions<size, false>::rela32(view, value, 0);
5116 else
5118 // LP64: call *foo@tlscall(%rax)
5119 // ==> xchg %ax, %ax
5120 // X32: call *foo@tlscall(%eax)
5121 // ==> nopl (%rax)
5122 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5123 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5124 int prefix = 0;
5125 if (size == 32 && view[0] == 0x67)
5127 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5128 view_size, 3);
5129 prefix = 1;
5131 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5132 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5133 if (prefix)
5135 view[0] = 0x0f;
5136 view[1] = 0x1f;
5137 view[2] = 0x00;
5139 else
5141 view[0] = 0x66;
5142 view[1] = 0x90;
5147 template<int size>
5148 inline void
5149 Target_x86_64<size>::Relocate::tls_ld_to_le(
5150 const Relocate_info<size, false>* relinfo,
5151 size_t relnum,
5152 Output_segment*,
5153 const elfcpp::Rela<size, false>& rela,
5154 unsigned int,
5155 typename elfcpp::Elf_types<size>::Elf_Addr,
5156 unsigned char* view,
5157 section_size_type view_size)
5159 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5160 // For SIZE == 64:
5161 // ... leq foo@dtpoff(%rax),%reg
5162 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5163 // For SIZE == 32:
5164 // ... leq foo@dtpoff(%rax),%reg
5165 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5166 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5167 // For SIZE == 64:
5168 // ... leq foo@dtpoff(%rax),%reg
5169 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5170 // For SIZE == 32:
5171 // ... leq foo@dtpoff(%rax),%reg
5172 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5174 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5175 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
5177 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5178 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
5180 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5181 view[4] == 0xe8 || view[4] == 0xff);
5183 if (view[4] == 0xe8)
5185 if (size == 64)
5186 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5187 else
5188 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5190 else
5192 if (size == 64)
5193 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5194 13);
5195 else
5196 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5197 13);
5200 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5201 // We can skip it.
5202 this->skip_call_tls_get_addr_ = true;
5205 // Do a relocation in which we convert a TLS Initial-Exec to a
5206 // Local-Exec.
5208 template<int size>
5209 inline void
5210 Target_x86_64<size>::Relocate::tls_ie_to_le(
5211 const Relocate_info<size, false>* relinfo,
5212 size_t relnum,
5213 Output_segment* tls_segment,
5214 const elfcpp::Rela<size, false>& rela,
5215 unsigned int,
5216 typename elfcpp::Elf_types<size>::Elf_Addr value,
5217 unsigned char* view,
5218 section_size_type view_size)
5220 // We need to examine the opcodes to figure out which instruction we
5221 // are looking at.
5223 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5224 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5226 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5227 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5229 unsigned char op1 = view[-3];
5230 unsigned char op2 = view[-2];
5231 unsigned char op3 = view[-1];
5232 unsigned char reg = op3 >> 3;
5234 if (op2 == 0x8b)
5236 // movq
5237 if (op1 == 0x4c)
5238 view[-3] = 0x49;
5239 else if (size == 32 && op1 == 0x44)
5240 view[-3] = 0x41;
5241 view[-2] = 0xc7;
5242 view[-1] = 0xc0 | reg;
5244 else if (reg == 4)
5246 // Special handling for %rsp.
5247 if (op1 == 0x4c)
5248 view[-3] = 0x49;
5249 else if (size == 32 && op1 == 0x44)
5250 view[-3] = 0x41;
5251 view[-2] = 0x81;
5252 view[-1] = 0xc0 | reg;
5254 else
5256 // addq
5257 if (op1 == 0x4c)
5258 view[-3] = 0x4d;
5259 else if (size == 32 && op1 == 0x44)
5260 view[-3] = 0x45;
5261 view[-2] = 0x8d;
5262 view[-1] = 0x80 | reg | (reg << 3);
5265 if (tls_segment != NULL)
5266 value -= tls_segment->memsz();
5267 Relocate_functions<size, false>::rela32(view, value, 0);
5270 // Relocate section data.
5272 template<int size>
5273 void
5274 Target_x86_64<size>::relocate_section(
5275 const Relocate_info<size, false>* relinfo,
5276 unsigned int sh_type,
5277 const unsigned char* prelocs,
5278 size_t reloc_count,
5279 Output_section* output_section,
5280 bool needs_special_offset_handling,
5281 unsigned char* view,
5282 typename elfcpp::Elf_types<size>::Elf_Addr address,
5283 section_size_type view_size,
5284 const Reloc_symbol_changes* reloc_symbol_changes)
5286 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5287 Classify_reloc;
5289 gold_assert(sh_type == elfcpp::SHT_RELA);
5291 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5292 gold::Default_comdat_behavior, Classify_reloc>(
5293 relinfo,
5294 this,
5295 prelocs,
5296 reloc_count,
5297 output_section,
5298 needs_special_offset_handling,
5299 view,
5300 address,
5301 view_size,
5302 reloc_symbol_changes);
5305 // Apply an incremental relocation. Incremental relocations always refer
5306 // to global symbols.
5308 template<int size>
5309 void
5310 Target_x86_64<size>::apply_relocation(
5311 const Relocate_info<size, false>* relinfo,
5312 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5313 unsigned int r_type,
5314 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5315 const Symbol* gsym,
5316 unsigned char* view,
5317 typename elfcpp::Elf_types<size>::Elf_Addr address,
5318 section_size_type view_size)
5320 gold::apply_relocation<size, false, Target_x86_64<size>,
5321 typename Target_x86_64<size>::Relocate>(
5322 relinfo,
5323 this,
5324 r_offset,
5325 r_type,
5326 r_addend,
5327 gsym,
5328 view,
5329 address,
5330 view_size);
5333 // Scan the relocs during a relocatable link.
5335 template<int size>
5336 void
5337 Target_x86_64<size>::scan_relocatable_relocs(
5338 Symbol_table* symtab,
5339 Layout* layout,
5340 Sized_relobj_file<size, false>* object,
5341 unsigned int data_shndx,
5342 unsigned int sh_type,
5343 const unsigned char* prelocs,
5344 size_t reloc_count,
5345 Output_section* output_section,
5346 bool needs_special_offset_handling,
5347 size_t local_symbol_count,
5348 const unsigned char* plocal_symbols,
5349 Relocatable_relocs* rr)
5351 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5352 Classify_reloc;
5353 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5354 Scan_relocatable_relocs;
5356 gold_assert(sh_type == elfcpp::SHT_RELA);
5358 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5359 symtab,
5360 layout,
5361 object,
5362 data_shndx,
5363 prelocs,
5364 reloc_count,
5365 output_section,
5366 needs_special_offset_handling,
5367 local_symbol_count,
5368 plocal_symbols,
5369 rr);
5372 // Scan the relocs for --emit-relocs.
5374 template<int size>
5375 void
5376 Target_x86_64<size>::emit_relocs_scan(
5377 Symbol_table* symtab,
5378 Layout* layout,
5379 Sized_relobj_file<size, false>* object,
5380 unsigned int data_shndx,
5381 unsigned int sh_type,
5382 const unsigned char* prelocs,
5383 size_t reloc_count,
5384 Output_section* output_section,
5385 bool needs_special_offset_handling,
5386 size_t local_symbol_count,
5387 const unsigned char* plocal_syms,
5388 Relocatable_relocs* rr)
5390 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5391 Classify_reloc;
5392 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5393 Emit_relocs_strategy;
5395 gold_assert(sh_type == elfcpp::SHT_RELA);
5397 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5398 symtab,
5399 layout,
5400 object,
5401 data_shndx,
5402 prelocs,
5403 reloc_count,
5404 output_section,
5405 needs_special_offset_handling,
5406 local_symbol_count,
5407 plocal_syms,
5408 rr);
5411 // Relocate a section during a relocatable link.
5413 template<int size>
5414 void
5415 Target_x86_64<size>::relocate_relocs(
5416 const Relocate_info<size, false>* relinfo,
5417 unsigned int sh_type,
5418 const unsigned char* prelocs,
5419 size_t reloc_count,
5420 Output_section* output_section,
5421 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5422 unsigned char* view,
5423 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5424 section_size_type view_size,
5425 unsigned char* reloc_view,
5426 section_size_type reloc_view_size)
5428 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5429 Classify_reloc;
5431 gold_assert(sh_type == elfcpp::SHT_RELA);
5433 gold::relocate_relocs<size, false, Classify_reloc>(
5434 relinfo,
5435 prelocs,
5436 reloc_count,
5437 output_section,
5438 offset_in_output_section,
5439 view,
5440 view_address,
5441 view_size,
5442 reloc_view,
5443 reloc_view_size);
5446 // Return the value to use for a dynamic which requires special
5447 // treatment. This is how we support equality comparisons of function
5448 // pointers across shared library boundaries, as described in the
5449 // processor specific ABI supplement.
5451 template<int size>
5452 uint64_t
5453 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5455 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5456 return this->plt_address_for_global(gsym);
5459 // Return a string used to fill a code section with nops to take up
5460 // the specified length.
5462 template<int size>
5463 std::string
5464 Target_x86_64<size>::do_code_fill(section_size_type length) const
5466 if (length >= 16)
5468 // Build a jmpq instruction to skip over the bytes.
5469 unsigned char jmp[5];
5470 jmp[0] = 0xe9;
5471 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5472 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5473 + std::string(length - 5, static_cast<char>(0x90)));
5476 // Nop sequences of various lengths.
5477 const char nop1[1] = { '\x90' }; // nop
5478 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5479 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5480 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5481 '\x00'};
5482 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5483 '\x00', '\x00' };
5484 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5485 '\x44', '\x00', '\x00' };
5486 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5487 '\x00', '\x00', '\x00',
5488 '\x00' };
5489 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5490 '\x00', '\x00', '\x00',
5491 '\x00', '\x00' };
5492 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5493 '\x84', '\x00', '\x00',
5494 '\x00', '\x00', '\x00' };
5495 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5496 '\x1f', '\x84', '\x00',
5497 '\x00', '\x00', '\x00',
5498 '\x00' };
5499 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5500 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5501 '\x00', '\x00', '\x00',
5502 '\x00', '\x00' };
5503 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5504 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5505 '\x84', '\x00', '\x00',
5506 '\x00', '\x00', '\x00' };
5507 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5508 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5509 '\x1f', '\x84', '\x00',
5510 '\x00', '\x00', '\x00',
5511 '\x00' };
5512 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5513 '\x66', '\x66', '\x2e', // data16
5514 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5515 '\x00', '\x00', '\x00',
5516 '\x00', '\x00' };
5517 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5518 '\x66', '\x66', '\x66', // data16; data16
5519 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5520 '\x84', '\x00', '\x00',
5521 '\x00', '\x00', '\x00' };
5523 const char* nops[16] = {
5524 NULL,
5525 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5526 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5529 return std::string(nops[length], length);
5532 // Return the addend to use for a target specific relocation. The
5533 // only target specific relocation is R_X86_64_TLSDESC for a local
5534 // symbol. We want to set the addend is the offset of the local
5535 // symbol in the TLS segment.
5537 template<int size>
5538 uint64_t
5539 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5540 uint64_t) const
5542 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5543 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5544 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5545 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5546 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5547 gold_assert(psymval->is_tls_symbol());
5548 // The value of a TLS symbol is the offset in the TLS segment.
5549 return psymval->value(ti.object, 0);
5552 // Return the value to use for the base of a DW_EH_PE_datarel offset
5553 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5554 // assembler can not write out the difference between two labels in
5555 // different sections, so instead of using a pc-relative value they
5556 // use an offset from the GOT.
5558 template<int size>
5559 uint64_t
5560 Target_x86_64<size>::do_ehframe_datarel_base() const
5562 gold_assert(this->global_offset_table_ != NULL);
5563 Symbol* sym = this->global_offset_table_;
5564 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5565 return ssym->value();
5568 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5569 // compiled with -fsplit-stack. The function calls non-split-stack
5570 // code. We have to change the function so that it always ensures
5571 // that it has enough stack space to run some random function.
5573 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5574 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5575 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5577 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5578 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5579 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5581 template<int size>
5582 void
5583 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5584 section_offset_type fnoffset,
5585 section_size_type fnsize,
5586 const unsigned char*,
5587 size_t,
5588 unsigned char* view,
5589 section_size_type view_size,
5590 std::string* from,
5591 std::string* to) const
5593 const char* const cmp_insn = reinterpret_cast<const char*>
5594 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5595 const char* const lea_r10_insn = reinterpret_cast<const char*>
5596 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5597 const char* const lea_r11_insn = reinterpret_cast<const char*>
5598 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5600 const size_t cmp_insn_len =
5601 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5602 const size_t lea_r10_insn_len =
5603 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5604 const size_t lea_r11_insn_len =
5605 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5606 const size_t nop_len = (size == 32 ? 7 : 8);
5608 // The function starts with a comparison of the stack pointer and a
5609 // field in the TCB. This is followed by a jump.
5611 // cmp %fs:NN,%rsp
5612 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5613 && fnsize > nop_len + 1)
5615 // We will call __morestack if the carry flag is set after this
5616 // comparison. We turn the comparison into an stc instruction
5617 // and some nops.
5618 view[fnoffset] = '\xf9';
5619 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5621 // lea NN(%rsp),%r10
5622 // lea NN(%rsp),%r11
5623 else if ((this->match_view(view, view_size, fnoffset,
5624 lea_r10_insn, lea_r10_insn_len)
5625 || this->match_view(view, view_size, fnoffset,
5626 lea_r11_insn, lea_r11_insn_len))
5627 && fnsize > 8)
5629 // This is loading an offset from the stack pointer for a
5630 // comparison. The offset is negative, so we decrease the
5631 // offset by the amount of space we need for the stack. This
5632 // means we will avoid calling __morestack if there happens to
5633 // be plenty of space on the stack already.
5634 unsigned char* pval = view + fnoffset + 4;
5635 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5636 val -= parameters->options().split_stack_adjust_size();
5637 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5639 else
5641 if (!object->has_no_split_stack())
5642 object->error(_("failed to match split-stack sequence at "
5643 "section %u offset %0zx"),
5644 shndx, static_cast<size_t>(fnoffset));
5645 return;
5648 // We have to change the function so that it calls
5649 // __morestack_non_split instead of __morestack. The former will
5650 // allocate additional stack space.
5651 *from = "__morestack";
5652 *to = "__morestack_non_split";
5655 // The selector for x86_64 object files. Note this is never instantiated
5656 // directly. It's only used in Target_selector_x86_64_nacl, below.
5658 template<int size>
5659 class Target_selector_x86_64 : public Target_selector_freebsd
5661 public:
5662 Target_selector_x86_64()
5663 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5664 (size == 64
5665 ? "elf64-x86-64" : "elf32-x86-64"),
5666 (size == 64
5667 ? "elf64-x86-64-freebsd"
5668 : "elf32-x86-64-freebsd"),
5669 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5672 Target*
5673 do_instantiate_target()
5674 { return new Target_x86_64<size>(); }
5678 // NaCl variant. It uses different PLT contents.
5680 template<int size>
5681 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5683 public:
5684 Output_data_plt_x86_64_nacl(Layout* layout,
5685 Output_data_got<64, false>* got,
5686 Output_data_got_plt_x86_64* got_plt,
5687 Output_data_space* got_irelative)
5688 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5689 got, got_plt, got_irelative)
5692 Output_data_plt_x86_64_nacl(Layout* layout,
5693 Output_data_got<64, false>* got,
5694 Output_data_got_plt_x86_64* got_plt,
5695 Output_data_space* got_irelative,
5696 unsigned int plt_count)
5697 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5698 got, got_plt, got_irelative,
5699 plt_count)
5702 protected:
5703 virtual unsigned int
5704 do_get_plt_entry_size() const
5705 { return plt_entry_size; }
5707 virtual void
5708 do_add_eh_frame(Layout* layout)
5710 layout->add_eh_frame_for_plt(this,
5711 this->plt_eh_frame_cie,
5712 this->plt_eh_frame_cie_size,
5713 plt_eh_frame_fde,
5714 plt_eh_frame_fde_size);
5717 virtual void
5718 do_fill_first_plt_entry(unsigned char* pov,
5719 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5720 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5722 virtual unsigned int
5723 do_fill_plt_entry(unsigned char* pov,
5724 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5725 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5726 unsigned int got_offset,
5727 unsigned int plt_offset,
5728 unsigned int plt_index);
5730 virtual void
5731 do_fill_tlsdesc_entry(unsigned char* pov,
5732 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5733 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5734 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5735 unsigned int tlsdesc_got_offset,
5736 unsigned int plt_offset);
5738 private:
5739 // The size of an entry in the PLT.
5740 static const int plt_entry_size = 64;
5742 // The first entry in the PLT.
5743 static const unsigned char first_plt_entry[plt_entry_size];
5745 // Other entries in the PLT for an executable.
5746 static const unsigned char plt_entry[plt_entry_size];
5748 // The reserved TLSDESC entry in the PLT for an executable.
5749 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5751 // The .eh_frame unwind information for the PLT.
5752 static const int plt_eh_frame_fde_size = 32;
5753 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5756 template<int size>
5757 class Target_x86_64_nacl : public Target_x86_64<size>
5759 public:
5760 Target_x86_64_nacl()
5761 : Target_x86_64<size>(&x86_64_nacl_info)
5764 virtual Output_data_plt_x86_64<size>*
5765 do_make_data_plt(Layout* layout,
5766 Output_data_got<64, false>* got,
5767 Output_data_got_plt_x86_64* got_plt,
5768 Output_data_space* got_irelative)
5770 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5771 got_irelative);
5774 virtual Output_data_plt_x86_64<size>*
5775 do_make_data_plt(Layout* layout,
5776 Output_data_got<64, false>* got,
5777 Output_data_got_plt_x86_64* got_plt,
5778 Output_data_space* got_irelative,
5779 unsigned int plt_count)
5781 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5782 got_irelative,
5783 plt_count);
5786 virtual std::string
5787 do_code_fill(section_size_type length) const;
5789 private:
5790 static const Target::Target_info x86_64_nacl_info;
5793 template<>
5794 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5796 64, // size
5797 false, // is_big_endian
5798 elfcpp::EM_X86_64, // machine_code
5799 false, // has_make_symbol
5800 false, // has_resolve
5801 true, // has_code_fill
5802 true, // is_default_stack_executable
5803 true, // can_icf_inline_merge_sections
5804 '\0', // wrap_char
5805 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5806 0x20000, // default_text_segment_address
5807 0x10000, // abi_pagesize (overridable by -z max-page-size)
5808 0x10000, // common_pagesize (overridable by -z common-page-size)
5809 true, // isolate_execinstr
5810 0x10000000, // rosegment_gap
5811 elfcpp::SHN_UNDEF, // small_common_shndx
5812 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5813 0, // small_common_section_flags
5814 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5815 NULL, // attributes_section
5816 NULL, // attributes_vendor
5817 "_start", // entry_symbol_name
5818 32, // hash_entry_size
5819 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5822 template<>
5823 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5825 32, // size
5826 false, // is_big_endian
5827 elfcpp::EM_X86_64, // machine_code
5828 false, // has_make_symbol
5829 false, // has_resolve
5830 true, // has_code_fill
5831 true, // is_default_stack_executable
5832 true, // can_icf_inline_merge_sections
5833 '\0', // wrap_char
5834 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5835 0x20000, // default_text_segment_address
5836 0x10000, // abi_pagesize (overridable by -z max-page-size)
5837 0x10000, // common_pagesize (overridable by -z common-page-size)
5838 true, // isolate_execinstr
5839 0x10000000, // rosegment_gap
5840 elfcpp::SHN_UNDEF, // small_common_shndx
5841 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5842 0, // small_common_section_flags
5843 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5844 NULL, // attributes_section
5845 NULL, // attributes_vendor
5846 "_start", // entry_symbol_name
5847 32, // hash_entry_size
5848 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5851 #define NACLMASK 0xe0 // 32-byte alignment mask.
5853 // The first entry in the PLT.
5855 template<int size>
5856 const unsigned char
5857 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5859 0xff, 0x35, // pushq contents of memory address
5860 0, 0, 0, 0, // replaced with address of .got + 8
5861 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5862 0, 0, 0, 0, // replaced with address of .got + 16
5863 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5864 0x4d, 0x01, 0xfb, // add %r15, %r11
5865 0x41, 0xff, 0xe3, // jmpq *%r11
5867 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5868 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5870 // 32 bytes of nop to pad out to the standard size
5871 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5872 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5873 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5874 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5875 0x66, // excess data32 prefix
5876 0x90 // nop
5879 template<int size>
5880 void
5881 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5882 unsigned char* pov,
5883 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5884 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5886 memcpy(pov, first_plt_entry, plt_entry_size);
5887 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5888 (got_address + 8
5889 - (plt_address + 2 + 4)));
5890 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5891 (got_address + 16
5892 - (plt_address + 9 + 4)));
5895 // Subsequent entries in the PLT.
5897 template<int size>
5898 const unsigned char
5899 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
5901 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5902 0, 0, 0, 0, // replaced with address of symbol in .got
5903 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5904 0x4d, 0x01, 0xfb, // add %r15, %r11
5905 0x41, 0xff, 0xe3, // jmpq *%r11
5907 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5908 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5909 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5911 // Lazy GOT entries point here (32-byte aligned).
5912 0x68, // pushq immediate
5913 0, 0, 0, 0, // replaced with index into relocation table
5914 0xe9, // jmp relative
5915 0, 0, 0, 0, // replaced with offset to start of .plt0
5917 // 22 bytes of nop to pad out to the standard size.
5918 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5919 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5920 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5923 template<int size>
5924 unsigned int
5925 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
5926 unsigned char* pov,
5927 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5928 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5929 unsigned int got_offset,
5930 unsigned int plt_offset,
5931 unsigned int plt_index)
5933 memcpy(pov, plt_entry, plt_entry_size);
5934 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
5935 (got_address + got_offset
5936 - (plt_address + plt_offset
5937 + 3 + 4)));
5939 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
5940 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
5941 - (plt_offset + 38 + 4));
5943 return 32;
5946 // The reserved TLSDESC entry in the PLT.
5948 template<int size>
5949 const unsigned char
5950 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
5952 0xff, 0x35, // pushq x(%rip)
5953 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5954 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5955 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5956 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5957 0x4d, 0x01, 0xfb, // add %r15, %r11
5958 0x41, 0xff, 0xe3, // jmpq *%r11
5960 // 41 bytes of nop to pad out to the standard size.
5961 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5962 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5963 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5964 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5965 0x66, 0x66, // excess data32 prefixes
5966 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5969 template<int size>
5970 void
5971 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5972 unsigned char* pov,
5973 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5974 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5975 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5976 unsigned int tlsdesc_got_offset,
5977 unsigned int plt_offset)
5979 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5980 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5981 (got_address + 8
5982 - (plt_address + plt_offset
5983 + 2 + 4)));
5984 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5985 (got_base
5986 + tlsdesc_got_offset
5987 - (plt_address + plt_offset
5988 + 9 + 4)));
5991 // The .eh_frame unwind information for the PLT.
5993 template<int size>
5994 const unsigned char
5995 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5997 0, 0, 0, 0, // Replaced with offset to .plt.
5998 0, 0, 0, 0, // Replaced with size of .plt.
5999 0, // Augmentation size.
6000 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
6001 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
6002 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
6003 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
6004 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
6005 13, // Block length.
6006 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
6007 elfcpp::DW_OP_breg16, 0, // Push %rip.
6008 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
6009 elfcpp::DW_OP_and, // & (%rip & 0x3f).
6010 elfcpp::DW_OP_const1u, 37, // Push 0x25.
6011 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
6012 elfcpp::DW_OP_lit3, // Push 3.
6013 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
6014 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6015 elfcpp::DW_CFA_nop, // Align to 32 bytes.
6016 elfcpp::DW_CFA_nop
6019 // Return a string used to fill a code section with nops.
6020 // For NaCl, long NOPs are only valid if they do not cross
6021 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6022 template<int size>
6023 std::string
6024 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
6026 return std::string(length, static_cast<char>(0x90));
6029 // The selector for x86_64-nacl object files.
6031 template<int size>
6032 class Target_selector_x86_64_nacl
6033 : public Target_selector_nacl<Target_selector_x86_64<size>,
6034 Target_x86_64_nacl<size> >
6036 public:
6037 Target_selector_x86_64_nacl()
6038 : Target_selector_nacl<Target_selector_x86_64<size>,
6039 Target_x86_64_nacl<size> >("x86-64",
6040 size == 64
6041 ? "elf64-x86-64-nacl"
6042 : "elf32-x86-64-nacl",
6043 size == 64
6044 ? "elf_x86_64_nacl"
6045 : "elf32_x86_64_nacl")
6049 Target_selector_x86_64_nacl<64> target_selector_x86_64;
6050 Target_selector_x86_64_nacl<32> target_selector_x32;
6052 } // End anonymous namespace.