arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / gold / x86_64.cc
blobf77430bfb3d7c43d3dc2ba2ad63a2fee1db30e4f
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2024 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 && r_type != elfcpp::R_X86_64_CODE_4_GOTPCRELX)
1059 return false;
1060 // We cannot convert references to IFUNC symbols, or to symbols that
1061 // are not local to the current module.
1062 // We can't do predefined symbols because they may become undefined
1063 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1064 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1065 || gsym->is_undefined()
1066 || gsym->is_predefined()
1067 || gsym->is_from_dynobj()
1068 || gsym->is_preemptible())
1069 return false;
1070 // If we are building a shared object and the symbol is protected, we may
1071 // need to go through the GOT.
1072 if (parameters->options().shared()
1073 && gsym->visibility() == elfcpp::STV_PROTECTED)
1074 return false;
1075 // We cannot convert references to the _DYNAMIC symbol.
1076 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1077 return false;
1078 // Check for a MOV opcode.
1079 return (*view)[r_offset - 2] == 0x8b;
1082 // Convert
1083 // callq *foo@GOTPCRELX(%rip) to
1084 // addr32 callq foo
1085 // and jmpq *foo@GOTPCRELX(%rip) to
1086 // jmpq foo
1087 // nop
1088 template<class View_type>
1089 static inline bool
1090 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1091 size_t r_offset, View_type* view)
1093 gold_assert(gsym != NULL);
1094 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1095 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1096 return false;
1097 // We cannot convert references to IFUNC symbols, or to symbols that
1098 // are not local to the current module.
1099 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1100 || gsym->is_undefined ()
1101 || gsym->is_from_dynobj()
1102 || gsym->is_preemptible())
1103 return false;
1104 // Check for a CALLQ or JMPQ opcode.
1105 return ((*view)[r_offset - 2] == 0xff
1106 && ((*view)[r_offset - 1] == 0x15
1107 || (*view)[r_offset - 1] == 0x25));
1110 // Adjust TLS relocation type based on the options and whether this
1111 // is a local symbol.
1112 static tls::Tls_optimization
1113 optimize_tls_reloc(bool is_final, int r_type, size_t r_offset,
1114 const unsigned char* reloc_view);
1116 // Get the GOT section, creating it if necessary.
1117 Output_data_got<64, false>*
1118 got_section(Symbol_table*, Layout*);
1120 // Get the GOT PLT section.
1121 Output_data_got_plt_x86_64*
1122 got_plt_section() const
1124 gold_assert(this->got_plt_ != NULL);
1125 return this->got_plt_;
1128 // Get the GOT section for TLSDESC entries.
1129 Output_data_got<64, false>*
1130 got_tlsdesc_section() const
1132 gold_assert(this->got_tlsdesc_ != NULL);
1133 return this->got_tlsdesc_;
1136 // Create the PLT section.
1137 void
1138 make_plt_section(Symbol_table* symtab, Layout* layout);
1140 // Create a PLT entry for a global symbol.
1141 void
1142 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1144 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1145 void
1146 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1147 Sized_relobj_file<size, false>* relobj,
1148 unsigned int local_sym_index);
1150 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1151 void
1152 define_tls_base_symbol(Symbol_table*, Layout*);
1154 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1155 void
1156 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1158 // Create a GOT entry for the TLS module index.
1159 unsigned int
1160 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1161 Sized_relobj_file<size, false>* object);
1163 // Get the PLT section.
1164 Output_data_plt_x86_64<size>*
1165 plt_section() const
1167 gold_assert(this->plt_ != NULL);
1168 return this->plt_;
1171 // Get the dynamic reloc section, creating it if necessary.
1172 Reloc_section*
1173 rela_dyn_section(Layout*);
1175 // Get the section to use for TLSDESC relocations.
1176 Reloc_section*
1177 rela_tlsdesc_section(Layout*) const;
1179 // Get the section to use for IRELATIVE relocations.
1180 Reloc_section*
1181 rela_irelative_section(Layout*);
1183 // Add a potential copy relocation.
1184 void
1185 copy_reloc(Symbol_table* symtab, Layout* layout,
1186 Sized_relobj_file<size, false>* object,
1187 unsigned int shndx, Output_section* output_section,
1188 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1190 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1191 this->copy_relocs_.copy_reloc(symtab, layout,
1192 symtab->get_sized_symbol<size>(sym),
1193 object, shndx, output_section,
1194 r_type, reloc.get_r_offset(),
1195 reloc.get_r_addend(),
1196 this->rela_dyn_section(layout));
1199 // Record a target-specific program property in the .note.gnu.property
1200 // section.
1201 void
1202 record_gnu_property(unsigned int, unsigned int, size_t,
1203 const unsigned char*, const Object*);
1205 // Merge the target-specific program properties from the current object.
1206 void
1207 merge_gnu_properties(const Object*);
1209 // Finalize the target-specific program properties and add them back to
1210 // the layout.
1211 void
1212 do_finalize_gnu_properties(Layout*) const;
1214 // Information about this specific target which we pass to the
1215 // general Target structure.
1216 static const Target::Target_info x86_64_info;
1218 // The types of GOT entries needed for this platform.
1219 // These values are exposed to the ABI in an incremental link.
1220 // Do not renumber existing values without changing the version
1221 // number of the .gnu_incremental_inputs section.
1222 enum Got_type
1224 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1225 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1226 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1227 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1230 // This type is used as the argument to the target specific
1231 // relocation routines. The only target specific reloc is
1232 // R_X86_64_TLSDESC against a local symbol.
1233 struct Tlsdesc_info
1235 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1236 : object(a_object), r_sym(a_r_sym)
1239 // The object in which the local symbol is defined.
1240 Sized_relobj_file<size, false>* object;
1241 // The local symbol index in the object.
1242 unsigned int r_sym;
1245 // The GOT section.
1246 Output_data_got<64, false>* got_;
1247 // The PLT section.
1248 Output_data_plt_x86_64<size>* plt_;
1249 // The GOT PLT section.
1250 Output_data_got_plt_x86_64* got_plt_;
1251 // The GOT section for IRELATIVE relocations.
1252 Output_data_space* got_irelative_;
1253 // The GOT section for TLSDESC relocations.
1254 Output_data_got<64, false>* got_tlsdesc_;
1255 // The _GLOBAL_OFFSET_TABLE_ symbol.
1256 Symbol* global_offset_table_;
1257 // The dynamic reloc section.
1258 Reloc_section* rela_dyn_;
1259 // The section to use for IRELATIVE relocs.
1260 Reloc_section* rela_irelative_;
1261 // Relocs saved to avoid a COPY reloc.
1262 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1263 // Offset of the GOT entry for the TLS module index.
1264 unsigned int got_mod_index_offset_;
1265 // We handle R_X86_64_TLSDESC against a local symbol as a target
1266 // specific relocation. Here we store the object and local symbol
1267 // index for the relocation.
1268 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1269 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1270 bool tls_base_symbol_defined_;
1271 // Target-specific program properties, from .note.gnu.property section.
1272 // Each bit represents a specific feature.
1273 uint32_t isa_1_used_;
1274 uint32_t isa_1_needed_;
1275 uint32_t feature_1_;
1276 uint32_t feature_2_used_;
1277 uint32_t feature_2_needed_;
1278 // Target-specific properties from the current object.
1279 // These bits get ORed into ISA_1_USED_ after all properties for the object
1280 // have been processed. But if either is all zeroes (as when the property
1281 // is absent from an object), the result should be all zeroes.
1282 // (See PR ld/23486.)
1283 uint32_t object_isa_1_used_;
1284 // These bits get ANDed into FEATURE_1_ after all properties for the object
1285 // have been processed.
1286 uint32_t object_feature_1_;
1287 uint32_t object_feature_2_used_;
1288 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1289 bool seen_first_object_;
1292 template<>
1293 const Target::Target_info Target_x86_64<64>::x86_64_info =
1295 64, // size
1296 false, // is_big_endian
1297 elfcpp::EM_X86_64, // machine_code
1298 false, // has_make_symbol
1299 false, // has_resolve
1300 true, // has_code_fill
1301 true, // is_default_stack_executable
1302 true, // can_icf_inline_merge_sections
1303 '\0', // wrap_char
1304 "/lib/ld64.so.1", // program interpreter
1305 0x400000, // default_text_segment_address
1306 0x1000, // abi_pagesize (overridable by -z max-page-size)
1307 0x1000, // common_pagesize (overridable by -z common-page-size)
1308 false, // isolate_execinstr
1309 0, // rosegment_gap
1310 elfcpp::SHN_UNDEF, // small_common_shndx
1311 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1312 0, // small_common_section_flags
1313 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1314 NULL, // attributes_section
1315 NULL, // attributes_vendor
1316 "_start", // entry_symbol_name
1317 32, // hash_entry_size
1318 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1321 template<>
1322 const Target::Target_info Target_x86_64<32>::x86_64_info =
1324 32, // size
1325 false, // is_big_endian
1326 elfcpp::EM_X86_64, // machine_code
1327 false, // has_make_symbol
1328 false, // has_resolve
1329 true, // has_code_fill
1330 true, // is_default_stack_executable
1331 true, // can_icf_inline_merge_sections
1332 '\0', // wrap_char
1333 "/libx32/ldx32.so.1", // program interpreter
1334 0x400000, // default_text_segment_address
1335 0x1000, // abi_pagesize (overridable by -z max-page-size)
1336 0x1000, // common_pagesize (overridable by -z common-page-size)
1337 false, // isolate_execinstr
1338 0, // rosegment_gap
1339 elfcpp::SHN_UNDEF, // small_common_shndx
1340 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1341 0, // small_common_section_flags
1342 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1343 NULL, // attributes_section
1344 NULL, // attributes_vendor
1345 "_start", // entry_symbol_name
1346 32, // hash_entry_size
1347 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1350 // This is called when a new output section is created. This is where
1351 // we handle the SHF_X86_64_LARGE.
1353 template<int size>
1354 void
1355 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1357 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1358 os->set_is_large_section();
1361 // Get the GOT section, creating it if necessary.
1363 template<int size>
1364 Output_data_got<64, false>*
1365 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1367 if (this->got_ == NULL)
1369 gold_assert(symtab != NULL && layout != NULL);
1371 // When using -z now, we can treat .got.plt as a relro section.
1372 // Without -z now, it is modified after program startup by lazy
1373 // PLT relocations.
1374 bool is_got_plt_relro = parameters->options().now();
1375 Output_section_order got_order = (is_got_plt_relro
1376 ? ORDER_RELRO
1377 : ORDER_RELRO_LAST);
1378 Output_section_order got_plt_order = (is_got_plt_relro
1379 ? ORDER_RELRO
1380 : ORDER_NON_RELRO_FIRST);
1382 this->got_ = new Output_data_got<64, false>();
1384 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1385 (elfcpp::SHF_ALLOC
1386 | elfcpp::SHF_WRITE),
1387 this->got_, got_order, true);
1389 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1390 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1391 (elfcpp::SHF_ALLOC
1392 | elfcpp::SHF_WRITE),
1393 this->got_plt_, got_plt_order,
1394 is_got_plt_relro);
1396 // The first three entries are reserved.
1397 this->got_plt_->set_current_data_size(3 * 8);
1399 if (!is_got_plt_relro)
1401 // Those bytes can go into the relro segment.
1402 layout->increase_relro(3 * 8);
1405 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1406 this->global_offset_table_ =
1407 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1408 Symbol_table::PREDEFINED,
1409 this->got_plt_,
1410 0, 0, elfcpp::STT_OBJECT,
1411 elfcpp::STB_LOCAL,
1412 elfcpp::STV_HIDDEN, 0,
1413 false, false);
1415 // If there are any IRELATIVE relocations, they get GOT entries
1416 // in .got.plt after the jump slot entries.
1417 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1418 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1419 (elfcpp::SHF_ALLOC
1420 | elfcpp::SHF_WRITE),
1421 this->got_irelative_,
1422 got_plt_order, is_got_plt_relro);
1424 // If there are any TLSDESC relocations, they get GOT entries in
1425 // .got.plt after the jump slot and IRELATIVE entries.
1426 this->got_tlsdesc_ = new Output_data_got<64, false>();
1427 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1428 (elfcpp::SHF_ALLOC
1429 | elfcpp::SHF_WRITE),
1430 this->got_tlsdesc_,
1431 got_plt_order, is_got_plt_relro);
1434 return this->got_;
1437 // Get the dynamic reloc section, creating it if necessary.
1439 template<int size>
1440 typename Target_x86_64<size>::Reloc_section*
1441 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1443 if (this->rela_dyn_ == NULL)
1445 gold_assert(layout != NULL);
1446 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1447 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1448 elfcpp::SHF_ALLOC, this->rela_dyn_,
1449 ORDER_DYNAMIC_RELOCS, false);
1451 return this->rela_dyn_;
1454 // Get the section to use for IRELATIVE relocs, creating it if
1455 // necessary. These go in .rela.dyn, but only after all other dynamic
1456 // relocations. They need to follow the other dynamic relocations so
1457 // that they can refer to global variables initialized by those
1458 // relocs.
1460 template<int size>
1461 typename Target_x86_64<size>::Reloc_section*
1462 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1464 if (this->rela_irelative_ == NULL)
1466 // Make sure we have already created the dynamic reloc section.
1467 this->rela_dyn_section(layout);
1468 this->rela_irelative_ = new Reloc_section(false);
1469 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1470 elfcpp::SHF_ALLOC, this->rela_irelative_,
1471 ORDER_DYNAMIC_RELOCS, false);
1472 gold_assert(this->rela_dyn_->output_section()
1473 == this->rela_irelative_->output_section());
1475 return this->rela_irelative_;
1478 // Record a target-specific program property from the .note.gnu.property
1479 // section.
1480 template<int size>
1481 void
1482 Target_x86_64<size>::record_gnu_property(
1483 unsigned int, unsigned int pr_type,
1484 size_t pr_datasz, const unsigned char* pr_data,
1485 const Object* object)
1487 uint32_t val = 0;
1489 switch (pr_type)
1491 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
1492 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
1493 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
1494 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
1495 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1496 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1497 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1498 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1499 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1500 if (pr_datasz != 4)
1502 gold_warning(_("%s: corrupt .note.gnu.property section "
1503 "(pr_datasz for property %d is not 4)"),
1504 object->name().c_str(), pr_type);
1505 return;
1507 val = elfcpp::Swap<32, false>::readval(pr_data);
1508 break;
1509 default:
1510 gold_warning(_("%s: unknown program property type 0x%x "
1511 "in .note.gnu.property section"),
1512 object->name().c_str(), pr_type);
1513 break;
1516 switch (pr_type)
1518 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1519 this->object_isa_1_used_ |= val;
1520 break;
1521 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1522 this->isa_1_needed_ |= val;
1523 break;
1524 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1525 // If we see multiple feature props in one object, OR them together.
1526 this->object_feature_1_ |= val;
1527 break;
1528 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1529 this->object_feature_2_used_ |= val;
1530 break;
1531 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1532 this->feature_2_needed_ |= val;
1533 break;
1537 // Merge the target-specific program properties from the current object.
1538 template<int size>
1539 void
1540 Target_x86_64<size>::merge_gnu_properties(const Object*)
1542 if (this->seen_first_object_)
1544 // If any object is missing the ISA_1_USED property, we must omit
1545 // it from the output file.
1546 if (this->object_isa_1_used_ == 0)
1547 this->isa_1_used_ = 0;
1548 else if (this->isa_1_used_ != 0)
1549 this->isa_1_used_ |= this->object_isa_1_used_;
1550 this->feature_1_ &= this->object_feature_1_;
1551 // If any object is missing the FEATURE_2_USED property, we must
1552 // omit it from the output file.
1553 if (this->object_feature_2_used_ == 0)
1554 this->feature_2_used_ = 0;
1555 else if (this->feature_2_used_ != 0)
1556 this->feature_2_used_ |= this->object_feature_2_used_;
1558 else
1560 this->isa_1_used_ = this->object_isa_1_used_;
1561 this->feature_1_ = this->object_feature_1_;
1562 this->feature_2_used_ = this->object_feature_2_used_;
1563 this->seen_first_object_ = true;
1565 this->object_isa_1_used_ = 0;
1566 this->object_feature_1_ = 0;
1567 this->object_feature_2_used_ = 0;
1570 static inline void
1571 add_property(Layout* layout, unsigned int pr_type, uint32_t val)
1573 unsigned char buf[4];
1574 elfcpp::Swap<32, false>::writeval(buf, val);
1575 layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
1578 // Finalize the target-specific program properties and add them back to
1579 // the layout.
1580 template<int size>
1581 void
1582 Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const
1584 if (this->isa_1_used_ != 0)
1585 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
1586 this->isa_1_used_);
1587 if (this->isa_1_needed_ != 0)
1588 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
1589 this->isa_1_needed_);
1590 if (this->feature_1_ != 0)
1591 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
1592 this->feature_1_);
1593 if (this->feature_2_used_ != 0)
1594 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
1595 this->feature_2_used_);
1596 if (this->feature_2_needed_ != 0)
1597 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
1598 this->feature_2_needed_);
1601 // Write the first three reserved words of the .got.plt section.
1602 // The remainder of the section is written while writing the PLT
1603 // in Output_data_plt_i386::do_write.
1605 void
1606 Output_data_got_plt_x86_64::do_write(Output_file* of)
1608 // The first entry in the GOT is the address of the .dynamic section
1609 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1610 // We saved space for them when we created the section in
1611 // Target_x86_64::got_section.
1612 const off_t got_file_offset = this->offset();
1613 gold_assert(this->data_size() >= 24);
1614 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1615 Output_section* dynamic = this->layout_->dynamic_section();
1616 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1617 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1618 memset(got_view + 8, 0, 16);
1619 of->write_output_view(got_file_offset, 24, got_view);
1622 // Initialize the PLT section.
1624 template<int size>
1625 void
1626 Output_data_plt_x86_64<size>::init(Layout* layout)
1628 this->rel_ = new Reloc_section(false);
1629 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1630 elfcpp::SHF_ALLOC, this->rel_,
1631 ORDER_DYNAMIC_PLT_RELOCS, false);
1634 template<int size>
1635 void
1636 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1638 os->set_entsize(this->get_plt_entry_size());
1641 // Add an entry to the PLT.
1643 template<int size>
1644 void
1645 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1646 Symbol* gsym)
1648 gold_assert(!gsym->has_plt_offset());
1650 unsigned int plt_index;
1651 off_t plt_offset;
1652 section_offset_type got_offset;
1654 unsigned int* pcount;
1655 unsigned int offset;
1656 unsigned int reserved;
1657 Output_section_data_build* got;
1658 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1659 && gsym->can_use_relative_reloc(false))
1661 pcount = &this->irelative_count_;
1662 offset = 0;
1663 reserved = 0;
1664 got = this->got_irelative_;
1666 else
1668 pcount = &this->count_;
1669 offset = 1;
1670 reserved = 3;
1671 got = this->got_plt_;
1674 if (!this->is_data_size_valid())
1676 // Note that when setting the PLT offset for a non-IRELATIVE
1677 // entry we skip the initial reserved PLT entry.
1678 plt_index = *pcount + offset;
1679 plt_offset = plt_index * this->get_plt_entry_size();
1681 ++*pcount;
1683 got_offset = (plt_index - offset + reserved) * 8;
1684 gold_assert(got_offset == got->current_data_size());
1686 // Every PLT entry needs a GOT entry which points back to the PLT
1687 // entry (this will be changed by the dynamic linker, normally
1688 // lazily when the function is called).
1689 got->set_current_data_size(got_offset + 8);
1691 else
1693 // FIXME: This is probably not correct for IRELATIVE relocs.
1695 // For incremental updates, find an available slot.
1696 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1697 this->get_plt_entry_size(), 0);
1698 if (plt_offset == -1)
1699 gold_fallback(_("out of patch space (PLT);"
1700 " relink with --incremental-full"));
1702 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1703 // can be calculated from the PLT index, adjusting for the three
1704 // reserved entries at the beginning of the GOT.
1705 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1706 got_offset = (plt_index - offset + reserved) * 8;
1709 gsym->set_plt_offset(plt_offset);
1711 // Every PLT entry needs a reloc.
1712 this->add_relocation(symtab, layout, gsym, got_offset);
1714 // Note that we don't need to save the symbol. The contents of the
1715 // PLT are independent of which symbols are used. The symbols only
1716 // appear in the relocations.
1719 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1720 // the PLT offset.
1722 template<int size>
1723 unsigned int
1724 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1725 Symbol_table* symtab,
1726 Layout* layout,
1727 Sized_relobj_file<size, false>* relobj,
1728 unsigned int local_sym_index)
1730 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1731 ++this->irelative_count_;
1733 section_offset_type got_offset = this->got_irelative_->current_data_size();
1735 // Every PLT entry needs a GOT entry which points back to the PLT
1736 // entry.
1737 this->got_irelative_->set_current_data_size(got_offset + 8);
1739 // Every PLT entry needs a reloc.
1740 Reloc_section* rela = this->rela_irelative(symtab, layout);
1741 rela->add_symbolless_local_addend(relobj, local_sym_index,
1742 elfcpp::R_X86_64_IRELATIVE,
1743 this->got_irelative_, got_offset, 0);
1745 return plt_offset;
1748 // Add the relocation for a PLT entry.
1750 template<int size>
1751 void
1752 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1753 Layout* layout,
1754 Symbol* gsym,
1755 unsigned int got_offset)
1757 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1758 && gsym->can_use_relative_reloc(false))
1760 Reloc_section* rela = this->rela_irelative(symtab, layout);
1761 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1762 this->got_irelative_, got_offset, 0);
1764 else
1766 gsym->set_needs_dynsym_entry();
1767 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1768 got_offset, 0);
1772 // Return where the TLSDESC relocations should go, creating it if
1773 // necessary. These follow the JUMP_SLOT relocations.
1775 template<int size>
1776 typename Output_data_plt_x86_64<size>::Reloc_section*
1777 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1779 if (this->tlsdesc_rel_ == NULL)
1781 this->tlsdesc_rel_ = new Reloc_section(false);
1782 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1783 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1784 ORDER_DYNAMIC_PLT_RELOCS, false);
1785 gold_assert(this->tlsdesc_rel_->output_section()
1786 == this->rel_->output_section());
1788 return this->tlsdesc_rel_;
1791 // Return where the IRELATIVE relocations should go in the PLT. These
1792 // follow the JUMP_SLOT and the TLSDESC relocations.
1794 template<int size>
1795 typename Output_data_plt_x86_64<size>::Reloc_section*
1796 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1797 Layout* layout)
1799 if (this->irelative_rel_ == NULL)
1801 // Make sure we have a place for the TLSDESC relocations, in
1802 // case we see any later on.
1803 this->rela_tlsdesc(layout);
1804 this->irelative_rel_ = new Reloc_section(false);
1805 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1806 elfcpp::SHF_ALLOC, this->irelative_rel_,
1807 ORDER_DYNAMIC_PLT_RELOCS, false);
1808 gold_assert(this->irelative_rel_->output_section()
1809 == this->rel_->output_section());
1811 if (parameters->doing_static_link())
1813 // A statically linked executable will only have a .rela.plt
1814 // section to hold R_X86_64_IRELATIVE relocs for
1815 // STT_GNU_IFUNC symbols. The library will use these
1816 // symbols to locate the IRELATIVE relocs at program startup
1817 // time.
1818 symtab->define_in_output_data("__rela_iplt_start", NULL,
1819 Symbol_table::PREDEFINED,
1820 this->irelative_rel_, 0, 0,
1821 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1822 elfcpp::STV_HIDDEN, 0, false, true);
1823 symtab->define_in_output_data("__rela_iplt_end", NULL,
1824 Symbol_table::PREDEFINED,
1825 this->irelative_rel_, 0, 0,
1826 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1827 elfcpp::STV_HIDDEN, 0, true, true);
1830 return this->irelative_rel_;
1833 // Return the PLT address to use for a global symbol.
1835 template<int size>
1836 uint64_t
1837 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1839 uint64_t offset = 0;
1840 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1841 && gsym->can_use_relative_reloc(false))
1842 offset = (this->count_ + 1) * this->get_plt_entry_size();
1843 return this->address() + offset + gsym->plt_offset();
1846 // Return the PLT address to use for a local symbol. These are always
1847 // IRELATIVE relocs.
1849 template<int size>
1850 uint64_t
1851 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1852 unsigned int r_sym)
1854 return (this->address()
1855 + (this->count_ + 1) * this->get_plt_entry_size()
1856 + object->local_plt_offset(r_sym));
1859 // Set the final size.
1860 template<int size>
1861 void
1862 Output_data_plt_x86_64<size>::set_final_data_size()
1864 // Number of regular and IFUNC PLT entries, plus the first entry.
1865 unsigned int count = this->count_ + this->irelative_count_ + 1;
1866 // Count the TLSDESC entry, if present.
1867 if (this->has_tlsdesc_entry())
1868 ++count;
1869 this->set_data_size(count * this->get_plt_entry_size());
1872 // The first entry in the PLT for an executable.
1874 template<int size>
1875 const unsigned char
1876 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1878 // From AMD64 ABI Draft 0.98, page 76
1879 0xff, 0x35, // pushq contents of memory address
1880 0, 0, 0, 0, // replaced with address of .got + 8
1881 0xff, 0x25, // jmp indirect
1882 0, 0, 0, 0, // replaced with address of .got + 16
1883 0x90, 0x90, 0x90, 0x90 // noop (x4)
1886 template<int size>
1887 void
1888 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1889 unsigned char* pov,
1890 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1891 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1893 memcpy(pov, first_plt_entry, plt_entry_size);
1894 // We do a jmp relative to the PC at the end of this instruction.
1895 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1896 (got_address + 8
1897 - (plt_address + 6)));
1898 elfcpp::Swap<32, false>::writeval(pov + 8,
1899 (got_address + 16
1900 - (plt_address + 12)));
1903 // Subsequent entries in the PLT for an executable.
1905 template<int size>
1906 const unsigned char
1907 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1909 // From AMD64 ABI Draft 0.98, page 76
1910 0xff, 0x25, // jmpq indirect
1911 0, 0, 0, 0, // replaced with address of symbol in .got
1912 0x68, // pushq immediate
1913 0, 0, 0, 0, // replaced with offset into relocation table
1914 0xe9, // jmpq relative
1915 0, 0, 0, 0 // replaced with offset to start of .plt
1918 template<int size>
1919 unsigned int
1920 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1921 unsigned char* pov,
1922 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1923 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1924 unsigned int got_offset,
1925 unsigned int plt_offset,
1926 unsigned int plt_index)
1928 // Check PC-relative offset overflow in PLT entry.
1929 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1930 - (plt_address + plt_offset + 6));
1931 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1932 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1933 plt_index + 1);
1935 memcpy(pov, plt_entry, plt_entry_size);
1936 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1937 plt_got_pcrel_offset);
1939 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1940 elfcpp::Swap<32, false>::writeval(pov + 12,
1941 - (plt_offset + plt_entry_size));
1943 return 6;
1946 // The reserved TLSDESC entry in the PLT for an executable.
1948 template<int size>
1949 const unsigned char
1950 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1952 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1953 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1954 0xff, 0x35, // pushq x(%rip)
1955 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1956 0xff, 0x25, // jmpq *y(%rip)
1957 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1958 0x0f, 0x1f, // nop
1959 0x40, 0
1962 template<int size>
1963 void
1964 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1965 unsigned char* pov,
1966 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1967 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1968 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1969 unsigned int tlsdesc_got_offset,
1970 unsigned int plt_offset)
1972 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1973 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1974 (got_address + 8
1975 - (plt_address + plt_offset
1976 + 6)));
1977 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1978 (got_base
1979 + tlsdesc_got_offset
1980 - (plt_address + plt_offset
1981 + 12)));
1984 // Return the APLT address to use for a global symbol (for IBT).
1986 template<int size>
1987 uint64_t
1988 Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym)
1990 uint64_t offset = this->aplt_offset_;
1991 // Convert the PLT offset into an APLT offset.
1992 unsigned int plt_offset = gsym->plt_offset();
1993 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1994 && gsym->can_use_relative_reloc(false))
1995 offset += this->regular_count() * aplt_entry_size;
1996 else
1997 plt_offset -= plt_entry_size;
1998 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
1999 return this->address() + offset + plt_offset;
2002 // Return the PLT address to use for a local symbol. These are always
2003 // IRELATIVE relocs.
2005 template<int size>
2006 uint64_t
2007 Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object,
2008 unsigned int r_sym)
2010 // Convert the PLT offset into an APLT offset.
2011 const Sized_relobj_file<size, false>* sized_relobj =
2012 static_cast<const Sized_relobj_file<size, false>*>(object);
2013 const Symbol_value<size>* psymval = sized_relobj->local_symbol(r_sym);
2014 unsigned int plt_offset = ((object->local_plt_offset(r_sym)
2015 - (psymval->is_ifunc_symbol()
2016 ? 0 : plt_entry_size))
2017 / (plt_entry_size / aplt_entry_size));
2018 return (this->address()
2019 + this->aplt_offset_
2020 + this->regular_count() * aplt_entry_size
2021 + plt_offset);
2024 // Set the final size.
2026 template<int size>
2027 void
2028 Output_data_plt_x86_64_ibt<size>::set_final_data_size()
2030 // Number of regular and IFUNC PLT entries.
2031 unsigned int count = this->entry_count();
2032 // Count the first entry and the TLSDESC entry, if present.
2033 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2034 unsigned int plt_size = (count + extra) * plt_entry_size;
2035 // Offset of the APLT.
2036 this->aplt_offset_ = plt_size;
2037 // Size of the APLT.
2038 plt_size += count * aplt_entry_size;
2039 this->set_data_size(plt_size);
2042 // The first entry in the IBT PLT.
2044 template<int size>
2045 const unsigned char
2046 Output_data_plt_x86_64_ibt<size>::first_plt_entry[plt_entry_size] =
2048 0xff, 0x35, // pushq contents of memory address
2049 0, 0, 0, 0, // replaced with address of .got + 8
2050 0xff, 0x25, // jmp indirect
2051 0, 0, 0, 0, // replaced with address of .got + 16
2052 0x90, 0x90, 0x90, 0x90 // noop (x4)
2055 template<int size>
2056 void
2057 Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry(
2058 unsigned char* pov,
2059 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2060 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
2062 // Offsets to the addresses needing relocation.
2063 const unsigned int roff1 = 2;
2064 const unsigned int roff2 = 8;
2066 memcpy(pov, first_plt_entry, plt_entry_size);
2067 // We do a jmp relative to the PC at the end of this instruction.
2068 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1,
2069 (got_address + 8
2070 - (plt_address + roff1 + 4)));
2071 elfcpp::Swap<32, false>::writeval(pov + roff2,
2072 (got_address + 16
2073 - (plt_address + roff2 + 4)));
2076 // Subsequent entries in the IBT PLT.
2078 template<int size>
2079 const unsigned char
2080 Output_data_plt_x86_64_ibt<size>::plt_entry[plt_entry_size] =
2082 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2083 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2084 0x68, // pushq immediate
2085 0, 0, 0, 0, // replaced with offset into relocation table
2086 0xe9, // jmpq relative
2087 0, 0, 0, 0, // replaced with offset to start of .plt
2088 0x90, 0x90 // nop
2091 // Entries in the IBT Additional PLT.
2093 template<int size>
2094 const unsigned char
2095 Output_data_plt_x86_64_ibt<size>::aplt_entry[aplt_entry_size] =
2097 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2098 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2099 0xff, 0x25, // jmpq indirect
2100 0, 0, 0, 0, // replaced with address of symbol in .got
2101 0x0f, 0x1f, 0x04, 0x00, // nop
2102 0x90, 0x90 // nop
2105 template<int size>
2106 unsigned int
2107 Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry(
2108 unsigned char* pov,
2109 typename elfcpp::Elf_types<size>::Elf_Addr,
2110 typename elfcpp::Elf_types<size>::Elf_Addr,
2111 unsigned int,
2112 unsigned int plt_offset,
2113 unsigned int plt_index)
2115 // Offsets to the addresses needing relocation.
2116 const unsigned int roff1 = 5;
2117 const unsigned int roff2 = 10;
2119 memcpy(pov, plt_entry, plt_entry_size);
2120 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index);
2121 elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4));
2122 return 0;
2125 template<int size>
2126 void
2127 Output_data_plt_x86_64_ibt<size>::fill_aplt_entry(
2128 unsigned char* pov,
2129 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2130 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2131 unsigned int got_offset,
2132 unsigned int plt_offset,
2133 unsigned int plt_index)
2135 // Offset to the address needing relocation.
2136 const unsigned int roff = 6;
2138 // Check PC-relative offset overflow in PLT entry.
2139 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2140 - (plt_address + plt_offset + roff + 4));
2141 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2142 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2143 plt_index + 1);
2145 memcpy(pov, aplt_entry, aplt_entry_size);
2146 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset);
2149 // The reserved TLSDESC entry in the IBT PLT for an executable.
2151 template<int size>
2152 const unsigned char
2153 Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] =
2155 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2156 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2157 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2158 0xff, 0x35, // pushq x(%rip)
2159 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2160 0xff, 0x25, // jmpq *y(%rip)
2161 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2164 template<int size>
2165 void
2166 Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry(
2167 unsigned char* pov,
2168 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2169 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2170 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2171 unsigned int tlsdesc_got_offset,
2172 unsigned int plt_offset)
2174 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2175 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6,
2176 (got_address + 8
2177 - (plt_address + plt_offset
2178 + 10)));
2179 elfcpp::Swap_unaligned<32, false>::writeval(pov + 12,
2180 (got_base
2181 + tlsdesc_got_offset
2182 - (plt_address + plt_offset
2183 + 16)));
2186 // The .eh_frame unwind information for the PLT.
2188 template<int size>
2189 const unsigned char
2190 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
2192 1, // CIE version.
2193 'z', // Augmentation: augmentation size included.
2194 'R', // Augmentation: FDE encoding included.
2195 '\0', // End of augmentation string.
2196 1, // Code alignment factor.
2197 0x78, // Data alignment factor.
2198 16, // Return address column.
2199 1, // Augmentation size.
2200 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
2201 | elfcpp::DW_EH_PE_sdata4),
2202 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2203 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2204 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2205 elfcpp::DW_CFA_nop
2208 template<int size>
2209 const unsigned char
2210 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2212 0, 0, 0, 0, // Replaced with offset to .plt.
2213 0, 0, 0, 0, // Replaced with size of .plt.
2214 0, // Augmentation size.
2215 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2216 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2217 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2218 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2219 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2220 11, // Block length.
2221 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2222 elfcpp::DW_OP_breg16, 0, // Push %rip.
2223 elfcpp::DW_OP_lit15, // Push 0xf.
2224 elfcpp::DW_OP_and, // & (%rip & 0xf).
2225 elfcpp::DW_OP_lit11, // Push 0xb.
2226 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2227 elfcpp::DW_OP_lit3, // Push 3.
2228 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2229 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2230 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2231 elfcpp::DW_CFA_nop,
2232 elfcpp::DW_CFA_nop,
2233 elfcpp::DW_CFA_nop
2236 // The .eh_frame unwind information for the PLT.
2237 template<int size>
2238 const unsigned char
2239 Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2241 0, 0, 0, 0, // Replaced with offset to .plt.
2242 0, 0, 0, 0, // Replaced with size of .plt.
2243 0, // Augmentation size.
2244 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2245 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2246 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2247 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2248 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2249 11, // Block length.
2250 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2251 elfcpp::DW_OP_breg16, 0, // Push %rip.
2252 elfcpp::DW_OP_lit15, // Push 0xf.
2253 elfcpp::DW_OP_and, // & (%rip & 0xf).
2254 elfcpp::DW_OP_lit9, // Push 9.
2255 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9)
2256 elfcpp::DW_OP_lit3, // Push 3.
2257 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3)
2258 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2259 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2260 elfcpp::DW_CFA_nop,
2261 elfcpp::DW_CFA_nop,
2262 elfcpp::DW_CFA_nop
2265 // Write out the PLT. This uses the hand-coded instructions above,
2266 // and adjusts them as needed. This is specified by the AMD64 ABI.
2268 template<int size>
2269 void
2270 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2272 const off_t offset = this->offset();
2273 const section_size_type oview_size =
2274 convert_to_section_size_type(this->data_size());
2275 unsigned char* const oview = of->get_output_view(offset, oview_size);
2277 const off_t got_file_offset = this->got_plt_->offset();
2278 gold_assert(parameters->incremental_update()
2279 || (got_file_offset + this->got_plt_->data_size()
2280 == this->got_irelative_->offset()));
2281 const section_size_type got_size =
2282 convert_to_section_size_type(this->got_plt_->data_size()
2283 + this->got_irelative_->data_size());
2284 unsigned char* const got_view = of->get_output_view(got_file_offset,
2285 got_size);
2287 unsigned char* pov = oview;
2289 // The base address of the .plt section.
2290 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2291 // The base address of the .got section.
2292 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2293 // The base address of the PLT portion of the .got section,
2294 // which is where the GOT pointer will point, and where the
2295 // three reserved GOT entries are located.
2296 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2297 = this->got_plt_->address();
2299 this->fill_first_plt_entry(pov, got_address, plt_address);
2300 pov += this->get_plt_entry_size();
2302 // The first three entries in the GOT are reserved, and are written
2303 // by Output_data_got_plt_x86_64::do_write.
2304 unsigned char* got_pov = got_view + 24;
2306 unsigned int plt_offset = this->get_plt_entry_size();
2307 unsigned int got_offset = 24;
2308 const unsigned int count = this->count_ + this->irelative_count_;
2309 for (unsigned int plt_index = 0;
2310 plt_index < count;
2311 ++plt_index,
2312 pov += this->get_plt_entry_size(),
2313 got_pov += 8,
2314 plt_offset += this->get_plt_entry_size(),
2315 got_offset += 8)
2317 // Set and adjust the PLT entry itself.
2318 unsigned int lazy_offset = this->fill_plt_entry(pov,
2319 got_address, plt_address,
2320 got_offset, plt_offset,
2321 plt_index);
2323 // Set the entry in the GOT.
2324 elfcpp::Swap<64, false>::writeval(got_pov,
2325 plt_address + plt_offset + lazy_offset);
2328 if (this->has_tlsdesc_entry())
2330 // Set and adjust the reserved TLSDESC PLT entry.
2331 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2332 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2333 tlsdesc_got_offset, plt_offset);
2334 pov += this->get_plt_entry_size();
2337 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2338 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2340 of->write_output_view(offset, oview_size, oview);
2341 of->write_output_view(got_file_offset, got_size, got_view);
2344 // Write out the IBT PLT.
2346 template<int size>
2347 void
2348 Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of)
2350 const off_t offset = this->offset();
2351 const section_size_type oview_size =
2352 convert_to_section_size_type(this->data_size());
2353 unsigned char* const oview = of->get_output_view(offset, oview_size);
2355 Output_data_got<64, false>* got = this->got();
2356 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2357 Output_data_space* got_irelative = this->got_irelative();
2359 const off_t got_file_offset = got_plt->offset();
2360 gold_assert(parameters->incremental_update()
2361 || (got_file_offset + got_plt->data_size()
2362 == got_irelative->offset()));
2363 const section_size_type got_size =
2364 convert_to_section_size_type(got_plt->data_size()
2365 + got_irelative->data_size());
2366 unsigned char* const got_view = of->get_output_view(got_file_offset,
2367 got_size);
2369 unsigned char* pov = oview;
2371 // The base address of the .plt section.
2372 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2373 // The base address of the .got section.
2374 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2375 // The base address of the PLT portion of the .got section,
2376 // which is where the GOT pointer will point, and where the
2377 // three reserved GOT entries are located.
2378 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2380 this->fill_first_plt_entry(pov, got_address, plt_address);
2381 pov += plt_entry_size;
2383 // The first three entries in the GOT are reserved, and are written
2384 // by Output_data_got_plt_x86_64::do_write.
2385 unsigned char* got_pov = got_view + 24;
2387 unsigned int plt_offset = plt_entry_size;
2388 unsigned int got_offset = 24;
2389 const unsigned int count = this->entry_count();
2390 for (unsigned int plt_index = 0;
2391 plt_index < count;
2392 ++plt_index,
2393 pov += plt_entry_size,
2394 got_pov += 8,
2395 plt_offset += plt_entry_size,
2396 got_offset += 8)
2398 // Set and adjust the PLT entry itself.
2399 unsigned int lazy_offset = this->fill_plt_entry(pov,
2400 got_address, plt_address,
2401 got_offset, plt_offset,
2402 plt_index);
2404 // Set the entry in the GOT.
2405 elfcpp::Swap<64, false>::writeval(got_pov,
2406 plt_address + plt_offset + lazy_offset);
2409 if (this->has_tlsdesc_entry())
2411 // Set and adjust the reserved TLSDESC PLT entry.
2412 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2413 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2414 tlsdesc_got_offset, plt_offset);
2415 pov += this->get_plt_entry_size();
2416 plt_offset += plt_entry_size;
2419 // Write the additional PLT.
2420 got_offset = 24;
2421 for (unsigned int plt_index = 0;
2422 plt_index < count;
2423 ++plt_index,
2424 pov += aplt_entry_size,
2425 plt_offset += aplt_entry_size,
2426 got_offset += 8)
2428 // Set and adjust the APLT entry.
2429 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2430 plt_offset, plt_index);
2433 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2434 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2436 of->write_output_view(offset, oview_size, oview);
2437 of->write_output_view(got_file_offset, got_size, got_view);
2440 // Create the PLT section.
2442 template<int size>
2443 void
2444 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2446 if (this->plt_ == NULL)
2448 // Create the GOT sections first.
2449 this->got_section(symtab, layout);
2451 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2452 this->got_irelative_);
2454 // Add unwind information if requested.
2455 if (parameters->options().ld_generated_unwind_info())
2456 this->plt_->add_eh_frame(layout);
2458 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2459 (elfcpp::SHF_ALLOC
2460 | elfcpp::SHF_EXECINSTR),
2461 this->plt_, ORDER_PLT, false);
2463 // Make the sh_info field of .rela.plt point to .plt.
2464 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2465 rela_plt_os->set_info_section(this->plt_->output_section());
2469 template<>
2470 Output_data_plt_x86_64<32>*
2471 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2472 Output_data_got<64, false>* got,
2473 Output_data_got_plt_x86_64* got_plt,
2474 Output_data_space* got_irelative)
2476 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2477 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2478 got_irelative);
2479 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2480 got_irelative);
2483 template<>
2484 Output_data_plt_x86_64<64>*
2485 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2486 Output_data_got<64, false>* got,
2487 Output_data_got_plt_x86_64* got_plt,
2488 Output_data_space* got_irelative)
2490 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2491 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2492 got_irelative);
2493 else
2494 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2495 got_irelative);
2498 template<>
2499 Output_data_plt_x86_64<32>*
2500 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2501 Output_data_got<64, false>* got,
2502 Output_data_got_plt_x86_64* got_plt,
2503 Output_data_space* got_irelative,
2504 unsigned int plt_count)
2506 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2507 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2508 got_irelative, plt_count);
2509 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2510 got_irelative, plt_count);
2513 template<>
2514 Output_data_plt_x86_64<64>*
2515 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2516 Output_data_got<64, false>* got,
2517 Output_data_got_plt_x86_64* got_plt,
2518 Output_data_space* got_irelative,
2519 unsigned int plt_count)
2521 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2522 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2523 got_irelative, plt_count);
2524 else
2525 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2526 got_irelative,
2527 plt_count);
2530 // Return the section for TLSDESC relocations.
2532 template<int size>
2533 typename Target_x86_64<size>::Reloc_section*
2534 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2536 return this->plt_section()->rela_tlsdesc(layout);
2539 // Create a PLT entry for a global symbol.
2541 template<int size>
2542 void
2543 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2544 Symbol* gsym)
2546 if (gsym->has_plt_offset())
2547 return;
2549 if (this->plt_ == NULL)
2550 this->make_plt_section(symtab, layout);
2552 this->plt_->add_entry(symtab, layout, gsym);
2555 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2557 template<int size>
2558 void
2559 Target_x86_64<size>::make_local_ifunc_plt_entry(
2560 Symbol_table* symtab, Layout* layout,
2561 Sized_relobj_file<size, false>* relobj,
2562 unsigned int local_sym_index)
2564 if (relobj->local_has_plt_offset(local_sym_index))
2565 return;
2566 if (this->plt_ == NULL)
2567 this->make_plt_section(symtab, layout);
2568 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2569 relobj,
2570 local_sym_index);
2571 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2574 // Return the number of entries in the PLT.
2576 template<int size>
2577 unsigned int
2578 Target_x86_64<size>::plt_entry_count() const
2580 if (this->plt_ == NULL)
2581 return 0;
2582 return this->plt_->entry_count();
2585 // Return the offset of the first non-reserved PLT entry.
2587 template<int size>
2588 unsigned int
2589 Target_x86_64<size>::first_plt_entry_offset() const
2591 if (this->plt_ == NULL)
2592 return 0;
2593 return this->plt_->first_plt_entry_offset();
2596 // Return the size of each PLT entry.
2598 template<int size>
2599 unsigned int
2600 Target_x86_64<size>::plt_entry_size() const
2602 if (this->plt_ == NULL)
2603 return 0;
2604 return this->plt_->get_plt_entry_size();
2607 // Create the GOT and PLT sections for an incremental update.
2609 template<int size>
2610 Output_data_got_base*
2611 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
2612 Layout* layout,
2613 unsigned int got_count,
2614 unsigned int plt_count)
2616 gold_assert(this->got_ == NULL);
2618 this->got_ = new Output_data_got<64, false>(got_count * 8);
2619 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2620 (elfcpp::SHF_ALLOC
2621 | elfcpp::SHF_WRITE),
2622 this->got_, ORDER_RELRO_LAST,
2623 true);
2625 // Add the three reserved entries.
2626 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
2627 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2628 (elfcpp::SHF_ALLOC
2629 | elfcpp::SHF_WRITE),
2630 this->got_plt_, ORDER_NON_RELRO_FIRST,
2631 false);
2633 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2634 this->global_offset_table_ =
2635 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2636 Symbol_table::PREDEFINED,
2637 this->got_plt_,
2638 0, 0, elfcpp::STT_OBJECT,
2639 elfcpp::STB_LOCAL,
2640 elfcpp::STV_HIDDEN, 0,
2641 false, false);
2643 // If there are any TLSDESC relocations, they get GOT entries in
2644 // .got.plt after the jump slot entries.
2645 // FIXME: Get the count for TLSDESC entries.
2646 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
2647 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2648 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2649 this->got_tlsdesc_,
2650 ORDER_NON_RELRO_FIRST, false);
2652 // If there are any IRELATIVE relocations, they get GOT entries in
2653 // .got.plt after the jump slot and TLSDESC entries.
2654 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2655 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2656 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2657 this->got_irelative_,
2658 ORDER_NON_RELRO_FIRST, false);
2660 // Create the PLT section.
2661 this->plt_ = this->make_data_plt(layout, this->got_,
2662 this->got_plt_,
2663 this->got_irelative_,
2664 plt_count);
2666 // Add unwind information if requested.
2667 if (parameters->options().ld_generated_unwind_info())
2668 this->plt_->add_eh_frame(layout);
2670 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2671 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2672 this->plt_, ORDER_PLT, false);
2674 // Make the sh_info field of .rela.plt point to .plt.
2675 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2676 rela_plt_os->set_info_section(this->plt_->output_section());
2678 // Create the rela_dyn section.
2679 this->rela_dyn_section(layout);
2681 return this->got_;
2684 // Reserve a GOT entry for a local symbol, and regenerate any
2685 // necessary dynamic relocations.
2687 template<int size>
2688 void
2689 Target_x86_64<size>::reserve_local_got_entry(
2690 unsigned int got_index,
2691 Sized_relobj<size, false>* obj,
2692 unsigned int r_sym,
2693 unsigned int got_type)
2695 unsigned int got_offset = got_index * 8;
2696 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2698 this->got_->reserve_local(got_index, obj, r_sym, got_type);
2699 switch (got_type)
2701 case GOT_TYPE_STANDARD:
2702 if (parameters->options().output_is_position_independent())
2703 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
2704 this->got_, got_offset, 0, false);
2705 break;
2706 case GOT_TYPE_TLS_OFFSET:
2707 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
2708 this->got_, got_offset, 0);
2709 break;
2710 case GOT_TYPE_TLS_PAIR:
2711 this->got_->reserve_slot(got_index + 1);
2712 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
2713 this->got_, got_offset, 0);
2714 break;
2715 case GOT_TYPE_TLS_DESC:
2716 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2717 // this->got_->reserve_slot(got_index + 1);
2718 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2719 // this->got_, got_offset, 0);
2720 break;
2721 default:
2722 gold_unreachable();
2726 // Reserve a GOT entry for a global symbol, and regenerate any
2727 // necessary dynamic relocations.
2729 template<int size>
2730 void
2731 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
2732 Symbol* gsym,
2733 unsigned int got_type)
2735 unsigned int got_offset = got_index * 8;
2736 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2738 this->got_->reserve_global(got_index, gsym, got_type);
2739 switch (got_type)
2741 case GOT_TYPE_STANDARD:
2742 if (!gsym->final_value_is_known())
2744 if (gsym->is_from_dynobj()
2745 || gsym->is_undefined()
2746 || gsym->is_preemptible()
2747 || gsym->type() == elfcpp::STT_GNU_IFUNC)
2748 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
2749 this->got_, got_offset, 0);
2750 else
2751 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2752 this->got_, got_offset, 0, false);
2754 break;
2755 case GOT_TYPE_TLS_OFFSET:
2756 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
2757 this->got_, got_offset, 0, false);
2758 break;
2759 case GOT_TYPE_TLS_PAIR:
2760 this->got_->reserve_slot(got_index + 1);
2761 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
2762 this->got_, got_offset, 0, false);
2763 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
2764 this->got_, got_offset + 8, 0, false);
2765 break;
2766 case GOT_TYPE_TLS_DESC:
2767 this->got_->reserve_slot(got_index + 1);
2768 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2769 this->got_, got_offset, 0, false);
2770 break;
2771 default:
2772 gold_unreachable();
2776 // Register an existing PLT entry for a global symbol.
2778 template<int size>
2779 void
2780 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2781 Layout* layout,
2782 unsigned int plt_index,
2783 Symbol* gsym)
2785 gold_assert(this->plt_ != NULL);
2786 gold_assert(!gsym->has_plt_offset());
2788 this->plt_->reserve_slot(plt_index);
2790 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2792 unsigned int got_offset = (plt_index + 3) * 8;
2793 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2796 // Force a COPY relocation for a given symbol.
2798 template<int size>
2799 void
2800 Target_x86_64<size>::emit_copy_reloc(
2801 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2803 this->copy_relocs_.emit_copy_reloc(symtab,
2804 symtab->get_sized_symbol<size>(sym),
2806 offset,
2807 this->rela_dyn_section(NULL));
2810 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2812 template<int size>
2813 void
2814 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2815 Layout* layout)
2817 if (this->tls_base_symbol_defined_)
2818 return;
2820 Output_segment* tls_segment = layout->tls_segment();
2821 if (tls_segment != NULL)
2823 bool is_exec = parameters->options().output_is_executable();
2824 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2825 Symbol_table::PREDEFINED,
2826 tls_segment, 0, 0,
2827 elfcpp::STT_TLS,
2828 elfcpp::STB_LOCAL,
2829 elfcpp::STV_HIDDEN, 0,
2830 (is_exec
2831 ? Symbol::SEGMENT_END
2832 : Symbol::SEGMENT_START),
2833 true);
2835 this->tls_base_symbol_defined_ = true;
2838 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2840 template<int size>
2841 void
2842 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2843 Layout* layout)
2845 if (this->plt_ == NULL)
2846 this->make_plt_section(symtab, layout);
2848 if (!this->plt_->has_tlsdesc_entry())
2850 // Allocate the TLSDESC_GOT entry.
2851 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2852 unsigned int got_offset = got->add_constant(0);
2854 // Allocate the TLSDESC_PLT entry.
2855 this->plt_->reserve_tlsdesc_entry(got_offset);
2859 // Create a GOT entry for the TLS module index.
2861 template<int size>
2862 unsigned int
2863 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2864 Sized_relobj_file<size, false>* object)
2866 if (this->got_mod_index_offset_ == -1U)
2868 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2869 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2870 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2871 unsigned int got_offset = got->add_constant(0);
2872 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2873 got_offset, 0);
2874 got->add_constant(0);
2875 this->got_mod_index_offset_ = got_offset;
2877 return this->got_mod_index_offset_;
2880 // Optimize the TLS relocation type based on what we know about the
2881 // symbol. IS_FINAL is true if the final address of this symbol is
2882 // known at link time. RELOC_VIEW points to the relocation offset.
2884 template<int size>
2885 tls::Tls_optimization
2886 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type,
2887 size_t r_offset,
2888 const unsigned char* reloc_view)
2890 // If we are generating a shared library, then we can't do anything
2891 // in the linker.
2892 if (parameters->options().shared())
2893 return tls::TLSOPT_NONE;
2895 switch (r_type)
2897 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
2898 if (r_offset <= 4 || *(reloc_view - 4) != 0xd5)
2899 return tls::TLSOPT_NONE;
2900 // Fall through.
2901 case elfcpp::R_X86_64_TLSGD:
2902 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2903 case elfcpp::R_X86_64_TLSDESC_CALL:
2904 // These are General-Dynamic which permits fully general TLS
2905 // access. Since we know that we are generating an executable,
2906 // we can convert this to Initial-Exec. If we also know that
2907 // this is a local symbol, we can further switch to Local-Exec.
2908 if (is_final)
2909 return tls::TLSOPT_TO_LE;
2910 return tls::TLSOPT_TO_IE;
2912 case elfcpp::R_X86_64_TLSLD:
2913 // This is Local-Dynamic, which refers to a local symbol in the
2914 // dynamic TLS block. Since we know that we generating an
2915 // executable, we can switch to Local-Exec.
2916 return tls::TLSOPT_TO_LE;
2918 case elfcpp::R_X86_64_DTPOFF32:
2919 case elfcpp::R_X86_64_DTPOFF64:
2920 // Another Local-Dynamic reloc.
2921 return tls::TLSOPT_TO_LE;
2923 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
2924 if (r_offset <= 6 || *(reloc_view - 6) != 0x62)
2925 return tls::TLSOPT_NONE;
2926 goto handle_gottpoff;
2928 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
2929 if (r_offset <= 4 || *(reloc_view - 4) != 0xd5)
2930 return tls::TLSOPT_NONE;
2931 // Fall through.
2932 case elfcpp::R_X86_64_GOTTPOFF:
2933 // These are Initial-Exec relocs which get the thread offset
2934 // from the GOT. If we know that we are linking against the
2935 // local symbol, we can switch to Local-Exec, which links the
2936 // thread offset into the instruction.
2937 handle_gottpoff:
2938 if (is_final)
2939 return tls::TLSOPT_TO_LE;
2940 return tls::TLSOPT_NONE;
2942 case elfcpp::R_X86_64_TPOFF32:
2943 // When we already have Local-Exec, there is nothing further we
2944 // can do.
2945 return tls::TLSOPT_NONE;
2947 default:
2948 gold_unreachable();
2952 // Get the Reference_flags for a particular relocation.
2954 template<int size>
2956 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2958 switch (r_type)
2960 case elfcpp::R_X86_64_NONE:
2961 case elfcpp::R_X86_64_GNU_VTINHERIT:
2962 case elfcpp::R_X86_64_GNU_VTENTRY:
2963 case elfcpp::R_X86_64_GOTPC32:
2964 case elfcpp::R_X86_64_GOTPC64:
2965 // No symbol reference.
2966 return 0;
2968 case elfcpp::R_X86_64_64:
2969 case elfcpp::R_X86_64_32:
2970 case elfcpp::R_X86_64_32S:
2971 case elfcpp::R_X86_64_16:
2972 case elfcpp::R_X86_64_8:
2973 return Symbol::ABSOLUTE_REF;
2975 case elfcpp::R_X86_64_PC64:
2976 case elfcpp::R_X86_64_PC32:
2977 case elfcpp::R_X86_64_PC16:
2978 case elfcpp::R_X86_64_PC8:
2979 case elfcpp::R_X86_64_GOTOFF64:
2980 return Symbol::RELATIVE_REF;
2982 case elfcpp::R_X86_64_PLT32:
2983 case elfcpp::R_X86_64_PLTOFF64:
2984 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2986 case elfcpp::R_X86_64_GOT64:
2987 case elfcpp::R_X86_64_GOT32:
2988 case elfcpp::R_X86_64_GOTPCREL64:
2989 case elfcpp::R_X86_64_GOTPCREL:
2990 case elfcpp::R_X86_64_GOTPCRELX:
2991 case elfcpp::R_X86_64_REX_GOTPCRELX:
2992 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
2993 case elfcpp::R_X86_64_GOTPLT64:
2994 // Absolute in GOT.
2995 return Symbol::ABSOLUTE_REF;
2997 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2998 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2999 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3000 case elfcpp::R_X86_64_TLSDESC_CALL:
3001 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3002 case elfcpp::R_X86_64_DTPOFF32:
3003 case elfcpp::R_X86_64_DTPOFF64:
3004 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3005 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3006 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
3007 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3008 return Symbol::TLS_REF;
3010 case elfcpp::R_X86_64_COPY:
3011 case elfcpp::R_X86_64_GLOB_DAT:
3012 case elfcpp::R_X86_64_JUMP_SLOT:
3013 case elfcpp::R_X86_64_RELATIVE:
3014 case elfcpp::R_X86_64_IRELATIVE:
3015 case elfcpp::R_X86_64_TPOFF64:
3016 case elfcpp::R_X86_64_DTPMOD64:
3017 case elfcpp::R_X86_64_TLSDESC:
3018 case elfcpp::R_X86_64_SIZE32:
3019 case elfcpp::R_X86_64_SIZE64:
3020 default:
3021 // Not expected. We will give an error later.
3022 return 0;
3026 // Report an unsupported relocation against a local symbol.
3028 template<int size>
3029 void
3030 Target_x86_64<size>::Scan::unsupported_reloc_local(
3031 Sized_relobj_file<size, false>* object,
3032 unsigned int r_type)
3034 gold_error(_("%s: unsupported reloc %u against local symbol"),
3035 object->name().c_str(), r_type);
3038 // We are about to emit a dynamic relocation of type R_TYPE. If the
3039 // dynamic linker does not support it, issue an error. The GNU linker
3040 // only issues a non-PIC error for an allocated read-only section.
3041 // Here we know the section is allocated, but we don't know that it is
3042 // read-only. But we check for all the relocation types which the
3043 // glibc dynamic linker supports, so it seems appropriate to issue an
3044 // error even if the section is not read-only. If GSYM is not NULL,
3045 // it is the symbol the relocation is against; if it is NULL, the
3046 // relocation is against a local symbol.
3048 template<int size>
3049 void
3050 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
3051 Symbol* gsym)
3053 switch (r_type)
3055 // These are the relocation types supported by glibc for x86_64
3056 // which should always work.
3057 case elfcpp::R_X86_64_RELATIVE:
3058 case elfcpp::R_X86_64_IRELATIVE:
3059 case elfcpp::R_X86_64_GLOB_DAT:
3060 case elfcpp::R_X86_64_JUMP_SLOT:
3061 case elfcpp::R_X86_64_DTPMOD64:
3062 case elfcpp::R_X86_64_DTPOFF64:
3063 case elfcpp::R_X86_64_TPOFF64:
3064 case elfcpp::R_X86_64_64:
3065 case elfcpp::R_X86_64_COPY:
3066 return;
3068 // glibc supports these reloc types, but they can overflow.
3069 case elfcpp::R_X86_64_PC32:
3070 // A PC relative reference is OK against a local symbol or if
3071 // the symbol is defined locally.
3072 if (gsym == NULL
3073 || (!gsym->is_from_dynobj()
3074 && !gsym->is_undefined()
3075 && !gsym->is_preemptible()))
3076 return;
3077 // Fall through.
3078 case elfcpp::R_X86_64_32:
3079 // R_X86_64_32 is OK for x32.
3080 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3081 return;
3082 if (this->issued_non_pic_error_)
3083 return;
3084 gold_assert(parameters->options().output_is_position_independent());
3085 if (gsym == NULL)
3086 object->error(_("requires dynamic R_X86_64_32 reloc which may "
3087 "overflow at runtime; recompile with -fPIC"));
3088 else
3090 const char *r_name;
3091 switch (r_type)
3093 case elfcpp::R_X86_64_32:
3094 r_name = "R_X86_64_32";
3095 break;
3096 case elfcpp::R_X86_64_PC32:
3097 r_name = "R_X86_64_PC32";
3098 break;
3099 default:
3100 gold_unreachable();
3101 break;
3103 object->error(_("requires dynamic %s reloc against '%s' "
3104 "which may overflow at runtime; recompile "
3105 "with -fPIC"),
3106 r_name, gsym->name());
3108 this->issued_non_pic_error_ = true;
3109 return;
3111 default:
3112 // This prevents us from issuing more than one error per reloc
3113 // section. But we can still wind up issuing more than one
3114 // error per object file.
3115 if (this->issued_non_pic_error_)
3116 return;
3117 gold_assert(parameters->options().output_is_position_independent());
3118 object->error(_("requires unsupported dynamic reloc %u; "
3119 "recompile with -fPIC"),
3120 r_type);
3121 this->issued_non_pic_error_ = true;
3122 return;
3124 case elfcpp::R_X86_64_NONE:
3125 gold_unreachable();
3129 // Return whether we need to make a PLT entry for a relocation of the
3130 // given type against a STT_GNU_IFUNC symbol.
3132 template<int size>
3133 bool
3134 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
3135 Sized_relobj_file<size, false>* object,
3136 unsigned int r_type)
3138 int flags = Scan::get_reference_flags(r_type);
3139 if (flags & Symbol::TLS_REF)
3140 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3141 object->name().c_str(), r_type);
3142 return flags != 0;
3145 // Scan a relocation for a local symbol.
3147 template<int size>
3148 inline void
3149 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
3150 Layout* layout,
3151 Target_x86_64<size>* target,
3152 Sized_relobj_file<size, false>* object,
3153 unsigned int data_shndx,
3154 Output_section* output_section,
3155 const elfcpp::Rela<size, false>& reloc,
3156 unsigned int r_type,
3157 const elfcpp::Sym<size, false>& lsym,
3158 bool is_discarded)
3160 if (is_discarded)
3161 return;
3163 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3164 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3165 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3167 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3168 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3171 const unsigned char* reloc_view = NULL;
3173 switch (r_type)
3175 case elfcpp::R_X86_64_NONE:
3176 case elfcpp::R_X86_64_GNU_VTINHERIT:
3177 case elfcpp::R_X86_64_GNU_VTENTRY:
3178 break;
3180 case elfcpp::R_X86_64_64:
3181 // If building a shared library (or a position-independent
3182 // executable), we need to create a dynamic relocation for this
3183 // location. The relocation applied at link time will apply the
3184 // link-time value, so we flag the location with an
3185 // R_X86_64_RELATIVE relocation so the dynamic loader can
3186 // relocate it easily.
3187 if (parameters->options().output_is_position_independent())
3189 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3190 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3191 rela_dyn->add_local_relative(object, r_sym,
3192 (size == 32
3193 ? elfcpp::R_X86_64_RELATIVE64
3194 : elfcpp::R_X86_64_RELATIVE),
3195 output_section, data_shndx,
3196 reloc.get_r_offset(),
3197 reloc.get_r_addend(), is_ifunc);
3199 break;
3201 case elfcpp::R_X86_64_32:
3202 case elfcpp::R_X86_64_32S:
3203 case elfcpp::R_X86_64_16:
3204 case elfcpp::R_X86_64_8:
3205 // If building a shared library (or a position-independent
3206 // executable), we need to create a dynamic relocation for this
3207 // location. We can't use an R_X86_64_RELATIVE relocation
3208 // because that is always a 64-bit relocation.
3209 if (parameters->options().output_is_position_independent())
3211 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3212 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3214 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3215 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3216 rela_dyn->add_local_relative(object, r_sym,
3217 elfcpp::R_X86_64_RELATIVE,
3218 output_section, data_shndx,
3219 reloc.get_r_offset(),
3220 reloc.get_r_addend(), is_ifunc);
3221 break;
3224 this->check_non_pic(object, r_type, NULL);
3226 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3227 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3228 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3229 rela_dyn->add_local(object, r_sym, r_type, output_section,
3230 data_shndx, reloc.get_r_offset(),
3231 reloc.get_r_addend());
3232 else
3234 gold_assert(lsym.get_st_value() == 0);
3235 unsigned int shndx = lsym.get_st_shndx();
3236 bool is_ordinary;
3237 shndx = object->adjust_sym_shndx(r_sym, shndx,
3238 &is_ordinary);
3239 if (!is_ordinary)
3240 object->error(_("section symbol %u has bad shndx %u"),
3241 r_sym, shndx);
3242 else
3243 rela_dyn->add_local_section(object, shndx,
3244 r_type, output_section,
3245 data_shndx, reloc.get_r_offset(),
3246 reloc.get_r_addend());
3249 break;
3251 case elfcpp::R_X86_64_PC64:
3252 case elfcpp::R_X86_64_PC32:
3253 case elfcpp::R_X86_64_PC16:
3254 case elfcpp::R_X86_64_PC8:
3255 break;
3257 case elfcpp::R_X86_64_PLT32:
3258 // Since we know this is a local symbol, we can handle this as a
3259 // PC32 reloc.
3260 break;
3262 case elfcpp::R_X86_64_GOTPC32:
3263 case elfcpp::R_X86_64_GOTOFF64:
3264 case elfcpp::R_X86_64_GOTPC64:
3265 case elfcpp::R_X86_64_PLTOFF64:
3266 // We need a GOT section.
3267 target->got_section(symtab, layout);
3268 // For PLTOFF64, we'd normally want a PLT section, but since we
3269 // know this is a local symbol, no PLT is needed.
3270 break;
3272 case elfcpp::R_X86_64_GOT64:
3273 case elfcpp::R_X86_64_GOT32:
3274 case elfcpp::R_X86_64_GOTPCREL64:
3275 case elfcpp::R_X86_64_GOTPCREL:
3276 case elfcpp::R_X86_64_GOTPCRELX:
3277 case elfcpp::R_X86_64_REX_GOTPCRELX:
3278 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3279 case elfcpp::R_X86_64_GOTPLT64:
3281 // The symbol requires a GOT section.
3282 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3284 // If the relocation symbol isn't IFUNC,
3285 // and is local, then we will convert
3286 // mov foo@GOTPCREL(%rip), %reg
3287 // to lea foo(%rip), %reg.
3288 // in Relocate::relocate.
3289 size_t r_offset = reloc.get_r_offset();
3290 if (!parameters->incremental()
3291 && (((r_type == elfcpp::R_X86_64_GOTPCREL
3292 || r_type == elfcpp::R_X86_64_GOTPCRELX
3293 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3294 && r_offset >= 2)
3295 || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3296 && r_offset >= 4))
3297 && reloc.get_r_addend() == -4
3298 && !is_ifunc)
3300 section_size_type stype;
3301 const unsigned char* view = object->section_contents(data_shndx,
3302 &stype, true);
3303 if (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3304 && view[r_offset - 4] != 0xd5)
3305 goto need_got;
3307 if (view[r_offset - 2] == 0x8b)
3308 break;
3311 need_got:
3313 // The symbol requires a GOT entry.
3314 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3316 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3317 // lets function pointers compare correctly with shared
3318 // libraries. Otherwise we would need an IRELATIVE reloc.
3319 bool is_new;
3320 if (is_ifunc)
3321 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3322 else
3323 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3324 if (is_new)
3326 // If we are generating a shared object, we need to add a
3327 // dynamic relocation for this symbol's GOT entry.
3328 if (parameters->options().output_is_position_independent())
3330 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3331 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3332 if (r_type != elfcpp::R_X86_64_GOT32)
3334 unsigned int got_offset =
3335 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3336 rela_dyn->add_local_relative(object, r_sym,
3337 elfcpp::R_X86_64_RELATIVE,
3338 got, got_offset, 0, is_ifunc);
3340 else
3342 this->check_non_pic(object, r_type, NULL);
3344 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3345 rela_dyn->add_local(
3346 object, r_sym, r_type, got,
3347 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3351 // For GOTPLT64, we'd normally want a PLT section, but since
3352 // we know this is a local symbol, no PLT is needed.
3354 break;
3356 case elfcpp::R_X86_64_COPY:
3357 case elfcpp::R_X86_64_GLOB_DAT:
3358 case elfcpp::R_X86_64_JUMP_SLOT:
3359 case elfcpp::R_X86_64_RELATIVE:
3360 case elfcpp::R_X86_64_IRELATIVE:
3361 // These are outstanding tls relocs, which are unexpected when linking
3362 case elfcpp::R_X86_64_TPOFF64:
3363 case elfcpp::R_X86_64_DTPMOD64:
3364 case elfcpp::R_X86_64_TLSDESC:
3365 gold_error(_("%s: unexpected reloc %u in object file"),
3366 object->name().c_str(), r_type);
3367 break;
3369 // These are initial tls relocs, which are expected when linking
3370 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3371 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3372 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
3374 section_size_type stype;
3375 reloc_view = object->section_contents(data_shndx, &stype, true);
3377 // Fall through.
3378 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3379 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3380 case elfcpp::R_X86_64_TLSDESC_CALL:
3381 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3382 case elfcpp::R_X86_64_DTPOFF32:
3383 case elfcpp::R_X86_64_DTPOFF64:
3384 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3385 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3387 bool output_is_shared = parameters->options().shared();
3388 size_t r_offset = reloc.get_r_offset();
3389 const tls::Tls_optimization optimized_type
3390 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3391 r_type, r_offset,
3392 reloc_view + r_offset);
3393 switch (r_type)
3395 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3396 if (optimized_type == tls::TLSOPT_NONE)
3398 // Create a pair of GOT entries for the module index and
3399 // dtv-relative offset.
3400 Output_data_got<64, false>* got
3401 = target->got_section(symtab, layout);
3402 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3403 unsigned int shndx = lsym.get_st_shndx();
3404 bool is_ordinary;
3405 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3406 if (!is_ordinary)
3407 object->error(_("local symbol %u has bad shndx %u"),
3408 r_sym, shndx);
3409 else
3410 got->add_local_pair_with_rel(object, r_sym,
3411 shndx,
3412 GOT_TYPE_TLS_PAIR,
3413 target->rela_dyn_section(layout),
3414 elfcpp::R_X86_64_DTPMOD64);
3416 else if (optimized_type != tls::TLSOPT_TO_LE)
3417 unsupported_reloc_local(object, r_type);
3418 break;
3420 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3421 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3422 target->define_tls_base_symbol(symtab, layout);
3423 if (optimized_type == tls::TLSOPT_NONE)
3425 // Create reserved PLT and GOT entries for the resolver.
3426 target->reserve_tlsdesc_entries(symtab, layout);
3428 // Generate a double GOT entry with an
3429 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3430 // is resolved lazily, so the GOT entry needs to be in
3431 // an area in .got.plt, not .got. Call got_section to
3432 // make sure the section has been created.
3433 target->got_section(symtab, layout);
3434 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3435 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3436 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3438 unsigned int got_offset = got->add_constant(0);
3439 got->add_constant(0);
3440 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3441 got_offset);
3442 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3443 // We store the arguments we need in a vector, and
3444 // use the index into the vector as the parameter
3445 // to pass to the target specific routines.
3446 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3447 void* arg = reinterpret_cast<void*>(intarg);
3448 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3449 got, got_offset, 0);
3452 else if (optimized_type != tls::TLSOPT_TO_LE)
3453 unsupported_reloc_local(object, r_type);
3454 break;
3456 case elfcpp::R_X86_64_TLSDESC_CALL:
3457 break;
3459 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3460 if (optimized_type == tls::TLSOPT_NONE)
3462 // Create a GOT entry for the module index.
3463 target->got_mod_index_entry(symtab, layout, object);
3465 else if (optimized_type != tls::TLSOPT_TO_LE)
3466 unsupported_reloc_local(object, r_type);
3467 break;
3469 case elfcpp::R_X86_64_DTPOFF32:
3470 case elfcpp::R_X86_64_DTPOFF64:
3471 break;
3473 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3474 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3475 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
3476 layout->set_has_static_tls();
3477 if (optimized_type == tls::TLSOPT_NONE)
3479 // Create a GOT entry for the tp-relative offset.
3480 Output_data_got<64, false>* got
3481 = target->got_section(symtab, layout);
3482 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3483 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3484 target->rela_dyn_section(layout),
3485 elfcpp::R_X86_64_TPOFF64);
3487 else if (optimized_type != tls::TLSOPT_TO_LE)
3488 unsupported_reloc_local(object, r_type);
3489 break;
3491 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3492 layout->set_has_static_tls();
3493 if (output_is_shared)
3494 unsupported_reloc_local(object, r_type);
3495 break;
3497 default:
3498 gold_unreachable();
3501 break;
3503 case elfcpp::R_X86_64_SIZE32:
3504 case elfcpp::R_X86_64_SIZE64:
3505 default:
3506 gold_error(_("%s: unsupported reloc %u against local symbol"),
3507 object->name().c_str(), r_type);
3508 break;
3513 // Report an unsupported relocation against a global symbol.
3515 template<int size>
3516 void
3517 Target_x86_64<size>::Scan::unsupported_reloc_global(
3518 Sized_relobj_file<size, false>* object,
3519 unsigned int r_type,
3520 Symbol* gsym)
3522 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3523 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3526 // Returns true if this relocation type could be that of a function pointer.
3527 template<int size>
3528 inline bool
3529 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3530 Sized_relobj_file<size, false>* src_obj,
3531 unsigned int src_indx,
3532 unsigned int r_offset,
3533 unsigned int r_type)
3535 switch (r_type)
3537 case elfcpp::R_X86_64_64:
3538 case elfcpp::R_X86_64_32:
3539 case elfcpp::R_X86_64_32S:
3540 case elfcpp::R_X86_64_16:
3541 case elfcpp::R_X86_64_8:
3542 case elfcpp::R_X86_64_GOT64:
3543 case elfcpp::R_X86_64_GOT32:
3544 case elfcpp::R_X86_64_GOTPCREL64:
3545 case elfcpp::R_X86_64_GOTPCREL:
3546 case elfcpp::R_X86_64_GOTPCRELX:
3547 case elfcpp::R_X86_64_REX_GOTPCRELX:
3548 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3549 case elfcpp::R_X86_64_GOTPLT64:
3551 return true;
3553 case elfcpp::R_X86_64_PC32:
3555 // This relocation may be used both for function calls and
3556 // for taking address of a function. We distinguish between
3557 // them by checking the opcodes.
3558 uint64_t sh_flags = src_obj->section_flags(src_indx);
3559 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3560 if (is_executable)
3562 section_size_type stype;
3563 const unsigned char* view = src_obj->section_contents(src_indx,
3564 &stype,
3565 true);
3567 // call
3568 if (r_offset >= 1
3569 && view[r_offset - 1] == 0xe8)
3570 return false;
3572 // jmp
3573 if (r_offset >= 1
3574 && view[r_offset - 1] == 0xe9)
3575 return false;
3577 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3578 if (r_offset >= 2
3579 && view[r_offset - 2] == 0x0f
3580 && view[r_offset - 1] >= 0x80
3581 && view[r_offset - 1] <= 0x8f)
3582 return false;
3585 // Be conservative and treat all others as function pointers.
3586 return true;
3589 return false;
3592 // For safe ICF, scan a relocation for a local symbol to check if it
3593 // corresponds to a function pointer being taken. In that case mark
3594 // the function whose pointer was taken as not foldable.
3596 template<int size>
3597 inline bool
3598 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3599 Symbol_table* ,
3600 Layout* ,
3601 Target_x86_64<size>* ,
3602 Sized_relobj_file<size, false>* src_obj,
3603 unsigned int src_indx,
3604 Output_section* ,
3605 const elfcpp::Rela<size, false>& reloc,
3606 unsigned int r_type,
3607 const elfcpp::Sym<size, false>&)
3609 return possible_function_pointer_reloc(src_obj, src_indx,
3610 reloc.get_r_offset(), r_type);
3613 // For safe ICF, scan a relocation for a global symbol to check if it
3614 // corresponds to a function pointer being taken. In that case mark
3615 // the function whose pointer was taken as not foldable.
3617 template<int size>
3618 inline bool
3619 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3620 Symbol_table*,
3621 Layout* ,
3622 Target_x86_64<size>* ,
3623 Sized_relobj_file<size, false>* src_obj,
3624 unsigned int src_indx,
3625 Output_section* ,
3626 const elfcpp::Rela<size, false>& reloc,
3627 unsigned int r_type,
3628 Symbol*)
3630 return possible_function_pointer_reloc(src_obj, src_indx,
3631 reloc.get_r_offset(), r_type);
3634 // Scan a relocation for a global symbol.
3636 template<int size>
3637 inline void
3638 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3639 Layout* layout,
3640 Target_x86_64<size>* target,
3641 Sized_relobj_file<size, false>* object,
3642 unsigned int data_shndx,
3643 Output_section* output_section,
3644 const elfcpp::Rela<size, false>& reloc,
3645 unsigned int r_type,
3646 Symbol* gsym)
3648 // A STT_GNU_IFUNC symbol may require a PLT entry.
3649 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3650 && this->reloc_needs_plt_for_ifunc(object, r_type))
3651 target->make_plt_entry(symtab, layout, gsym);
3653 const unsigned char *reloc_view = NULL;
3655 switch (r_type)
3657 case elfcpp::R_X86_64_NONE:
3658 case elfcpp::R_X86_64_GNU_VTINHERIT:
3659 case elfcpp::R_X86_64_GNU_VTENTRY:
3660 break;
3662 case elfcpp::R_X86_64_64:
3663 case elfcpp::R_X86_64_32:
3664 case elfcpp::R_X86_64_32S:
3665 case elfcpp::R_X86_64_16:
3666 case elfcpp::R_X86_64_8:
3668 // Make a PLT entry if necessary.
3669 if (gsym->needs_plt_entry())
3671 target->make_plt_entry(symtab, layout, gsym);
3672 // Since this is not a PC-relative relocation, we may be
3673 // taking the address of a function. In that case we need to
3674 // set the entry in the dynamic symbol table to the address of
3675 // the PLT entry.
3676 if (gsym->is_from_dynobj() && !parameters->options().shared())
3677 gsym->set_needs_dynsym_value();
3679 // Make a dynamic relocation if necessary.
3680 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3682 if (!parameters->options().output_is_position_independent()
3683 && gsym->may_need_copy_reloc())
3685 target->copy_reloc(symtab, layout, object,
3686 data_shndx, output_section, gsym, reloc);
3688 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3689 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3690 && gsym->type() == elfcpp::STT_GNU_IFUNC
3691 && gsym->can_use_relative_reloc(false)
3692 && !gsym->is_from_dynobj()
3693 && !gsym->is_undefined()
3694 && !gsym->is_preemptible())
3696 // Use an IRELATIVE reloc for a locally defined
3697 // STT_GNU_IFUNC symbol. This makes a function
3698 // address in a PIE executable match the address in a
3699 // shared library that it links against.
3700 Reloc_section* rela_dyn =
3701 target->rela_irelative_section(layout);
3702 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3703 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3704 output_section, object,
3705 data_shndx,
3706 reloc.get_r_offset(),
3707 reloc.get_r_addend());
3709 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3710 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3711 && gsym->can_use_relative_reloc(false))
3713 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3714 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3715 output_section, object,
3716 data_shndx,
3717 reloc.get_r_offset(),
3718 reloc.get_r_addend(), false);
3720 else
3722 this->check_non_pic(object, r_type, gsym);
3723 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3724 rela_dyn->add_global(gsym, r_type, output_section, object,
3725 data_shndx, reloc.get_r_offset(),
3726 reloc.get_r_addend());
3730 break;
3732 case elfcpp::R_X86_64_PC64:
3733 case elfcpp::R_X86_64_PC32:
3734 case elfcpp::R_X86_64_PC16:
3735 case elfcpp::R_X86_64_PC8:
3737 // Make a PLT entry if necessary.
3738 if (gsym->needs_plt_entry())
3739 target->make_plt_entry(symtab, layout, gsym);
3740 // Make a dynamic relocation if necessary.
3741 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3743 if (parameters->options().output_is_executable()
3744 && gsym->may_need_copy_reloc())
3746 target->copy_reloc(symtab, layout, object,
3747 data_shndx, output_section, gsym, reloc);
3749 else
3751 this->check_non_pic(object, r_type, gsym);
3752 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3753 rela_dyn->add_global(gsym, r_type, output_section, object,
3754 data_shndx, reloc.get_r_offset(),
3755 reloc.get_r_addend());
3759 break;
3761 case elfcpp::R_X86_64_GOT64:
3762 case elfcpp::R_X86_64_GOT32:
3763 case elfcpp::R_X86_64_GOTPCREL64:
3764 case elfcpp::R_X86_64_GOTPCREL:
3765 case elfcpp::R_X86_64_GOTPCRELX:
3766 case elfcpp::R_X86_64_REX_GOTPCRELX:
3767 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3768 case elfcpp::R_X86_64_GOTPLT64:
3770 // The symbol requires a GOT entry.
3771 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3773 // If we convert this from
3774 // mov foo@GOTPCREL(%rip), %reg
3775 // to lea foo(%rip), %reg.
3776 // OR
3777 // if we convert
3778 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3779 // (callq|jmpq) foo
3780 // in Relocate::relocate, then there is nothing to do here.
3781 // We cannot make these optimizations in incremental linking mode,
3782 // because we look at the opcode to decide whether or not to make
3783 // change, and during an incremental update, the change may have
3784 // already been applied.
3786 Lazy_view<size> view(object, data_shndx);
3787 size_t r_offset = reloc.get_r_offset();
3788 if (!parameters->incremental()
3789 && reloc.get_r_addend() == -4
3790 && ((r_type != elfcpp::R_X86_64_CODE_4_GOTPCRELX
3791 && r_offset >= 2)
3792 || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3793 && r_offset >= 4
3794 && view[r_offset - 4] == 0xd5))
3795 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3796 r_offset, &view))
3797 break;
3799 if (!parameters->incremental()
3800 && r_offset >= 2
3801 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3802 r_offset,
3803 &view))
3804 break;
3806 if (gsym->final_value_is_known())
3808 // For a STT_GNU_IFUNC symbol we want the PLT address.
3809 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3810 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3811 else
3812 got->add_global(gsym, GOT_TYPE_STANDARD);
3814 else
3816 // If this symbol is not fully resolved, we need to add a
3817 // dynamic relocation for it.
3818 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3820 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3822 // 1) The symbol may be defined in some other module.
3824 // 2) We are building a shared library and this is a
3825 // protected symbol; using GLOB_DAT means that the dynamic
3826 // linker can use the address of the PLT in the main
3827 // executable when appropriate so that function address
3828 // comparisons work.
3830 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3831 // code, again so that function address comparisons work.
3832 if (gsym->is_from_dynobj()
3833 || gsym->is_undefined()
3834 || gsym->is_preemptible()
3835 || (gsym->visibility() == elfcpp::STV_PROTECTED
3836 && parameters->options().shared())
3837 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3838 && parameters->options().output_is_position_independent()))
3839 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3840 elfcpp::R_X86_64_GLOB_DAT);
3841 else
3843 // For a STT_GNU_IFUNC symbol we want to write the PLT
3844 // offset into the GOT, so that function pointer
3845 // comparisons work correctly.
3846 bool is_new;
3847 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3848 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3849 else
3851 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3852 // Tell the dynamic linker to use the PLT address
3853 // when resolving relocations.
3854 if (gsym->is_from_dynobj()
3855 && !parameters->options().shared())
3856 gsym->set_needs_dynsym_value();
3858 if (is_new)
3860 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3861 rela_dyn->add_global_relative(gsym,
3862 elfcpp::R_X86_64_RELATIVE,
3863 got, got_off, 0, false);
3868 break;
3870 case elfcpp::R_X86_64_PLT32:
3871 // If the symbol is fully resolved, this is just a PC32 reloc.
3872 // Otherwise we need a PLT entry.
3873 if (gsym->final_value_is_known())
3874 break;
3875 // If building a shared library, we can also skip the PLT entry
3876 // if the symbol is defined in the output file and is protected
3877 // or hidden.
3878 if (gsym->is_defined()
3879 && !gsym->is_from_dynobj()
3880 && !gsym->is_preemptible())
3881 break;
3882 target->make_plt_entry(symtab, layout, gsym);
3883 break;
3885 case elfcpp::R_X86_64_GOTPC32:
3886 case elfcpp::R_X86_64_GOTOFF64:
3887 case elfcpp::R_X86_64_GOTPC64:
3888 case elfcpp::R_X86_64_PLTOFF64:
3889 // We need a GOT section.
3890 target->got_section(symtab, layout);
3891 // For PLTOFF64, we also need a PLT entry (but only if the
3892 // symbol is not fully resolved).
3893 if (r_type == elfcpp::R_X86_64_PLTOFF64
3894 && !gsym->final_value_is_known())
3895 target->make_plt_entry(symtab, layout, gsym);
3896 break;
3898 case elfcpp::R_X86_64_COPY:
3899 case elfcpp::R_X86_64_GLOB_DAT:
3900 case elfcpp::R_X86_64_JUMP_SLOT:
3901 case elfcpp::R_X86_64_RELATIVE:
3902 case elfcpp::R_X86_64_IRELATIVE:
3903 // These are outstanding tls relocs, which are unexpected when linking
3904 case elfcpp::R_X86_64_TPOFF64:
3905 case elfcpp::R_X86_64_DTPMOD64:
3906 case elfcpp::R_X86_64_TLSDESC:
3907 gold_error(_("%s: unexpected reloc %u in object file"),
3908 object->name().c_str(), r_type);
3909 break;
3911 // These are initial tls relocs, which are expected for global()
3912 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3913 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3914 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
3916 section_size_type stype;
3917 reloc_view = object->section_contents(data_shndx, &stype, true);
3919 // Fall through.
3920 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3921 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3922 case elfcpp::R_X86_64_TLSDESC_CALL:
3923 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3924 case elfcpp::R_X86_64_DTPOFF32:
3925 case elfcpp::R_X86_64_DTPOFF64:
3926 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3927 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3929 // For the Initial-Exec model, we can treat undef symbols as final
3930 // when building an executable.
3931 const bool is_final = (gsym->final_value_is_known() ||
3932 ((r_type == elfcpp::R_X86_64_GOTTPOFF ||
3933 r_type == elfcpp::R_X86_64_CODE_4_GOTTPOFF||
3934 r_type == elfcpp::R_X86_64_CODE_6_GOTTPOFF) &&
3935 gsym->is_undefined() &&
3936 parameters->options().output_is_executable()));
3937 size_t r_offset = reloc.get_r_offset();
3938 const tls::Tls_optimization optimized_type
3939 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type,
3940 r_offset,
3941 reloc_view + r_offset);
3942 switch (r_type)
3944 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3945 if (optimized_type == tls::TLSOPT_NONE)
3947 // Create a pair of GOT entries for the module index and
3948 // dtv-relative offset.
3949 Output_data_got<64, false>* got
3950 = target->got_section(symtab, layout);
3951 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3952 target->rela_dyn_section(layout),
3953 elfcpp::R_X86_64_DTPMOD64,
3954 elfcpp::R_X86_64_DTPOFF64);
3956 else if (optimized_type == tls::TLSOPT_TO_IE)
3958 // Create a GOT entry for the tp-relative offset.
3959 Output_data_got<64, false>* got
3960 = target->got_section(symtab, layout);
3961 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3962 target->rela_dyn_section(layout),
3963 elfcpp::R_X86_64_TPOFF64);
3965 else if (optimized_type != tls::TLSOPT_TO_LE)
3966 unsupported_reloc_global(object, r_type, gsym);
3967 break;
3969 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3970 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3971 target->define_tls_base_symbol(symtab, layout);
3972 if (optimized_type == tls::TLSOPT_NONE)
3974 // Create reserved PLT and GOT entries for the resolver.
3975 target->reserve_tlsdesc_entries(symtab, layout);
3977 // Create a double GOT entry with an R_X86_64_TLSDESC
3978 // reloc. The R_X86_64_TLSDESC reloc is resolved
3979 // lazily, so the GOT entry needs to be in an area in
3980 // .got.plt, not .got. Call got_section to make sure
3981 // the section has been created.
3982 target->got_section(symtab, layout);
3983 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3984 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3985 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3986 elfcpp::R_X86_64_TLSDESC, 0);
3988 else if (optimized_type == tls::TLSOPT_TO_IE)
3990 // Create a GOT entry for the tp-relative offset.
3991 Output_data_got<64, false>* got
3992 = target->got_section(symtab, layout);
3993 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3994 target->rela_dyn_section(layout),
3995 elfcpp::R_X86_64_TPOFF64);
3997 else if (optimized_type != tls::TLSOPT_TO_LE)
3998 unsupported_reloc_global(object, r_type, gsym);
3999 break;
4001 case elfcpp::R_X86_64_TLSDESC_CALL:
4002 break;
4004 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4005 if (optimized_type == tls::TLSOPT_NONE)
4007 // Create a GOT entry for the module index.
4008 target->got_mod_index_entry(symtab, layout, object);
4010 else if (optimized_type != tls::TLSOPT_TO_LE)
4011 unsupported_reloc_global(object, r_type, gsym);
4012 break;
4014 case elfcpp::R_X86_64_DTPOFF32:
4015 case elfcpp::R_X86_64_DTPOFF64:
4016 break;
4018 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4019 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4020 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
4021 layout->set_has_static_tls();
4022 if (optimized_type == tls::TLSOPT_NONE)
4024 // Create a GOT entry for the tp-relative offset.
4025 Output_data_got<64, false>* got
4026 = target->got_section(symtab, layout);
4027 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4028 target->rela_dyn_section(layout),
4029 elfcpp::R_X86_64_TPOFF64);
4031 else if (optimized_type != tls::TLSOPT_TO_LE)
4032 unsupported_reloc_global(object, r_type, gsym);
4033 break;
4035 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4036 layout->set_has_static_tls();
4037 if (parameters->options().shared())
4038 unsupported_reloc_global(object, r_type, gsym);
4039 break;
4041 default:
4042 gold_unreachable();
4045 break;
4047 case elfcpp::R_X86_64_SIZE32:
4048 case elfcpp::R_X86_64_SIZE64:
4049 default:
4050 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4051 object->name().c_str(), r_type,
4052 gsym->demangled_name().c_str());
4053 break;
4057 template<int size>
4058 void
4059 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
4060 Layout* layout,
4061 Sized_relobj_file<size, false>* object,
4062 unsigned int data_shndx,
4063 unsigned int sh_type,
4064 const unsigned char* prelocs,
4065 size_t reloc_count,
4066 Output_section* output_section,
4067 bool needs_special_offset_handling,
4068 size_t local_symbol_count,
4069 const unsigned char* plocal_symbols)
4071 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4072 Classify_reloc;
4074 if (sh_type == elfcpp::SHT_REL)
4076 return;
4079 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
4080 Classify_reloc>(
4081 symtab,
4082 layout,
4083 this,
4084 object,
4085 data_shndx,
4086 prelocs,
4087 reloc_count,
4088 output_section,
4089 needs_special_offset_handling,
4090 local_symbol_count,
4091 plocal_symbols);
4094 // Scan relocations for a section.
4096 template<int size>
4097 void
4098 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
4099 Layout* layout,
4100 Sized_relobj_file<size, false>* object,
4101 unsigned int data_shndx,
4102 unsigned int sh_type,
4103 const unsigned char* prelocs,
4104 size_t reloc_count,
4105 Output_section* output_section,
4106 bool needs_special_offset_handling,
4107 size_t local_symbol_count,
4108 const unsigned char* plocal_symbols)
4110 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4111 Classify_reloc;
4113 if (sh_type == elfcpp::SHT_REL)
4115 gold_error(_("%s: unsupported REL reloc section"),
4116 object->name().c_str());
4117 return;
4120 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
4121 symtab,
4122 layout,
4123 this,
4124 object,
4125 data_shndx,
4126 prelocs,
4127 reloc_count,
4128 output_section,
4129 needs_special_offset_handling,
4130 local_symbol_count,
4131 plocal_symbols);
4134 // Finalize the sections.
4136 template<int size>
4137 void
4138 Target_x86_64<size>::do_finalize_sections(
4139 Layout* layout,
4140 const Input_objects*,
4141 Symbol_table* symtab)
4143 const Reloc_section* rel_plt = (this->plt_ == NULL
4144 ? NULL
4145 : this->plt_->rela_plt());
4146 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4147 this->rela_dyn_, true, false, false);
4149 // Fill in some more dynamic tags.
4150 Output_data_dynamic* const odyn = layout->dynamic_data();
4151 if (odyn != NULL)
4153 if (this->plt_ != NULL
4154 && this->plt_->output_section() != NULL
4155 && this->plt_->has_tlsdesc_entry())
4157 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
4158 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
4159 this->got_->finalize_data_size();
4160 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
4161 this->plt_, plt_offset);
4162 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
4163 this->got_, got_offset);
4167 // Emit any relocs we saved in an attempt to avoid generating COPY
4168 // relocs.
4169 if (this->copy_relocs_.any_saved_relocs())
4170 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4172 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4173 // the .got.plt section.
4174 Symbol* sym = this->global_offset_table_;
4175 if (sym != NULL)
4177 uint64_t data_size = this->got_plt_->current_data_size();
4178 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4181 if (parameters->doing_static_link()
4182 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4184 // If linking statically, make sure that the __rela_iplt symbols
4185 // were defined if necessary, even if we didn't create a PLT.
4186 static const Define_symbol_in_segment syms[] =
4189 "__rela_iplt_start", // name
4190 elfcpp::PT_LOAD, // segment_type
4191 elfcpp::PF_W, // segment_flags_set
4192 elfcpp::PF(0), // segment_flags_clear
4193 0, // value
4194 0, // size
4195 elfcpp::STT_NOTYPE, // type
4196 elfcpp::STB_GLOBAL, // binding
4197 elfcpp::STV_HIDDEN, // visibility
4198 0, // nonvis
4199 Symbol::SEGMENT_START, // offset_from_base
4200 true // only_if_ref
4203 "__rela_iplt_end", // name
4204 elfcpp::PT_LOAD, // segment_type
4205 elfcpp::PF_W, // segment_flags_set
4206 elfcpp::PF(0), // segment_flags_clear
4207 0, // value
4208 0, // size
4209 elfcpp::STT_NOTYPE, // type
4210 elfcpp::STB_GLOBAL, // binding
4211 elfcpp::STV_HIDDEN, // visibility
4212 0, // nonvis
4213 Symbol::SEGMENT_START, // offset_from_base
4214 true // only_if_ref
4218 symtab->define_symbols(layout, 2, syms,
4219 layout->script_options()->saw_sections_clause());
4223 // For x32, we need to handle PC-relative relocations using full 64-bit
4224 // arithmetic, so that we can detect relocation overflows properly.
4225 // This class overrides the pcrela32_check methods from the defaults in
4226 // Relocate_functions in reloc.h.
4228 template<int size>
4229 class X86_64_relocate_functions : public Relocate_functions<size, false>
4231 public:
4232 typedef Relocate_functions<size, false> Base;
4234 // Do a simple PC relative relocation with the addend in the
4235 // relocation.
4236 static inline typename Base::Reloc_status
4237 pcrela32_check(unsigned char* view,
4238 typename elfcpp::Elf_types<64>::Elf_Addr value,
4239 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4240 typename elfcpp::Elf_types<64>::Elf_Addr address)
4242 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4243 Valtype* wv = reinterpret_cast<Valtype*>(view);
4244 value = value + addend - address;
4245 elfcpp::Swap<32, false>::writeval(wv, value);
4246 return (Bits<32>::has_overflow(value)
4247 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4250 // Do a simple PC relative relocation with a Symbol_value with the
4251 // addend in the relocation.
4252 static inline typename Base::Reloc_status
4253 pcrela32_check(unsigned char* view,
4254 const Sized_relobj_file<size, false>* object,
4255 const Symbol_value<size>* psymval,
4256 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4257 typename elfcpp::Elf_types<64>::Elf_Addr address)
4259 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4260 Valtype* wv = reinterpret_cast<Valtype*>(view);
4261 typename elfcpp::Elf_types<64>::Elf_Addr value;
4262 if (addend >= 0)
4263 value = psymval->value(object, addend);
4264 else
4266 // For negative addends, get the symbol value without
4267 // the addend, then add the addend using 64-bit arithmetic.
4268 value = psymval->value(object, 0);
4269 value += addend;
4271 value -= address;
4272 elfcpp::Swap<32, false>::writeval(wv, value);
4273 return (Bits<32>::has_overflow(value)
4274 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4278 // Perform a relocation.
4280 template<int size>
4281 inline bool
4282 Target_x86_64<size>::Relocate::relocate(
4283 const Relocate_info<size, false>* relinfo,
4284 unsigned int,
4285 Target_x86_64<size>* target,
4286 Output_section*,
4287 size_t relnum,
4288 const unsigned char* preloc,
4289 const Sized_symbol<size>* gsym,
4290 const Symbol_value<size>* psymval,
4291 unsigned char* view,
4292 typename elfcpp::Elf_types<size>::Elf_Addr address,
4293 section_size_type view_size)
4295 typedef X86_64_relocate_functions<size> Reloc_funcs;
4296 const elfcpp::Rela<size, false> rela(preloc);
4297 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4299 if (this->skip_call_tls_get_addr_)
4301 if ((r_type != elfcpp::R_X86_64_PLT32
4302 && r_type != elfcpp::R_X86_64_GOTPCREL
4303 && r_type != elfcpp::R_X86_64_GOTPCRELX
4304 && r_type != elfcpp::R_X86_64_PC32)
4305 || gsym == NULL
4306 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4308 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4309 _("missing expected TLS relocation"));
4310 this->skip_call_tls_get_addr_ = false;
4312 else
4314 this->skip_call_tls_get_addr_ = false;
4315 return false;
4319 if (view == NULL)
4320 return true;
4322 const Sized_relobj_file<size, false>* object = relinfo->object;
4324 // Pick the value to use for symbols defined in the PLT.
4325 Symbol_value<size> symval;
4326 if (gsym != NULL
4327 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4329 symval.set_output_value(target->plt_address_for_global(gsym));
4330 psymval = &symval;
4332 else if (gsym == NULL && psymval->is_ifunc_symbol())
4334 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4335 if (object->local_has_plt_offset(r_sym))
4337 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4338 psymval = &symval;
4342 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4344 // Get the GOT offset if needed.
4345 // The GOT pointer points to the end of the GOT section.
4346 // We need to subtract the size of the GOT section to get
4347 // the actual offset to use in the relocation.
4348 bool have_got_offset = false;
4349 // Since the actual offset is always negative, we use signed int to
4350 // support 64-bit GOT relocations.
4351 int got_offset = 0;
4352 switch (r_type)
4354 case elfcpp::R_X86_64_GOT32:
4355 case elfcpp::R_X86_64_GOT64:
4356 case elfcpp::R_X86_64_GOTPLT64:
4357 case elfcpp::R_X86_64_GOTPCREL64:
4358 if (gsym != NULL)
4360 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4361 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4363 else
4365 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4366 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4367 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4368 - target->got_size());
4370 have_got_offset = true;
4371 break;
4373 default:
4374 break;
4377 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4379 switch (r_type)
4381 case elfcpp::R_X86_64_NONE:
4382 case elfcpp::R_X86_64_GNU_VTINHERIT:
4383 case elfcpp::R_X86_64_GNU_VTENTRY:
4384 break;
4386 case elfcpp::R_X86_64_64:
4387 Reloc_funcs::rela64(view, object, psymval, addend);
4388 break;
4390 case elfcpp::R_X86_64_PC64:
4391 Reloc_funcs::pcrela64(view, object, psymval, addend,
4392 address);
4393 break;
4395 case elfcpp::R_X86_64_32:
4396 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4397 Reloc_funcs::CHECK_UNSIGNED);
4398 break;
4400 case elfcpp::R_X86_64_32S:
4401 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4402 Reloc_funcs::CHECK_SIGNED);
4403 break;
4405 case elfcpp::R_X86_64_PC32:
4406 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4407 address);
4408 break;
4410 case elfcpp::R_X86_64_16:
4411 Reloc_funcs::rela16(view, object, psymval, addend);
4412 break;
4414 case elfcpp::R_X86_64_PC16:
4415 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4416 break;
4418 case elfcpp::R_X86_64_8:
4419 Reloc_funcs::rela8(view, object, psymval, addend);
4420 break;
4422 case elfcpp::R_X86_64_PC8:
4423 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4424 break;
4426 case elfcpp::R_X86_64_PLT32:
4427 gold_assert(gsym == NULL
4428 || gsym->has_plt_offset()
4429 || gsym->final_value_is_known()
4430 || (gsym->is_defined()
4431 && !gsym->is_from_dynobj()
4432 && !gsym->is_preemptible()));
4433 // Note: while this code looks the same as for R_X86_64_PC32, it
4434 // behaves differently because psymval was set to point to
4435 // the PLT entry, rather than the symbol, in Scan::global().
4436 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4437 address);
4438 break;
4440 case elfcpp::R_X86_64_PLTOFF64:
4442 gold_assert(gsym);
4443 gold_assert(gsym->has_plt_offset()
4444 || gsym->final_value_is_known());
4445 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4446 // This is the address of GLOBAL_OFFSET_TABLE.
4447 got_address = target->got_plt_section()->address();
4448 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4450 break;
4452 case elfcpp::R_X86_64_GOT32:
4453 gold_assert(have_got_offset);
4454 Reloc_funcs::rela32(view, got_offset, addend);
4455 break;
4457 case elfcpp::R_X86_64_GOTPC32:
4459 gold_assert(gsym);
4460 typename elfcpp::Elf_types<size>::Elf_Addr value;
4461 value = target->got_plt_section()->address();
4462 Reloc_funcs::pcrela32_check(view, value, addend, address);
4464 break;
4466 case elfcpp::R_X86_64_GOT64:
4467 case elfcpp::R_X86_64_GOTPLT64:
4468 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4469 // GOT64.
4470 gold_assert(have_got_offset);
4471 Reloc_funcs::rela64(view, got_offset, addend);
4472 break;
4474 case elfcpp::R_X86_64_GOTPC64:
4476 gold_assert(gsym);
4477 typename elfcpp::Elf_types<size>::Elf_Addr value;
4478 value = target->got_plt_section()->address();
4479 Reloc_funcs::pcrela64(view, value, addend, address);
4481 break;
4483 case elfcpp::R_X86_64_GOTOFF64:
4485 typename elfcpp::Elf_types<size>::Elf_Addr reladdr;
4486 reladdr = target->got_plt_section()->address();
4487 Reloc_funcs::pcrela64(view, object, psymval, addend, reladdr);
4489 break;
4491 case elfcpp::R_X86_64_GOTPCREL:
4492 case elfcpp::R_X86_64_GOTPCRELX:
4493 case elfcpp::R_X86_64_REX_GOTPCRELX:
4494 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
4496 bool converted_p = false;
4498 if (rela.get_r_addend() == -4)
4500 // Convert
4501 // mov foo@GOTPCREL(%rip), %reg
4502 // to lea foo(%rip), %reg.
4503 // if possible.
4504 if (!parameters->incremental()
4505 && ((gsym == NULL
4506 && rela.get_r_offset() >= 2
4507 && view[-2] == 0x8b
4508 && !psymval->is_ifunc_symbol())
4509 || (gsym != NULL
4510 && rela.get_r_offset() >= 2
4511 && Target_x86_64<size>::can_convert_mov_to_lea(gsym,
4512 r_type,
4514 &view))))
4516 view[-2] = 0x8d;
4517 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4518 converted_p = true;
4520 // Convert
4521 // callq *foo@GOTPCRELX(%rip) to
4522 // addr32 callq foo
4523 // and jmpq *foo@GOTPCRELX(%rip) to
4524 // jmpq foo
4525 // nop
4526 else if (!parameters->incremental()
4527 && gsym != NULL
4528 && rela.get_r_offset() >= 2
4529 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4530 r_type,
4532 &view))
4534 if (view[-1] == 0x15)
4536 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4537 // Opcode of addr32 is 0x67 and opcode of direct callq
4538 // is 0xe8.
4539 view[-2] = 0x67;
4540 view[-1] = 0xe8;
4541 // Convert GOTPCRELX to 32-bit pc relative reloc.
4542 Reloc_funcs::pcrela32(view, object, psymval, addend,
4543 address);
4544 converted_p = true;
4546 else
4548 // Convert jmpq *foo@GOTPCRELX(%rip) to
4549 // jmpq foo
4550 // nop
4551 // The opcode of direct jmpq is 0xe9.
4552 view[-2] = 0xe9;
4553 // The opcode of nop is 0x90.
4554 view[3] = 0x90;
4555 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq
4556 // is rip relative and since the instruction following
4557 // the jmpq is now the nop, offset the address by 1
4558 // byte. The start of the relocation also moves ahead
4559 // by 1 byte.
4560 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4561 address - 1);
4562 converted_p = true;
4567 if (!converted_p)
4569 if (gsym != NULL)
4571 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4572 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4573 - target->got_size());
4575 else
4577 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4578 gold_assert(object->local_has_got_offset(r_sym,
4579 GOT_TYPE_STANDARD));
4580 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4581 - target->got_size());
4583 typename elfcpp::Elf_types<size>::Elf_Addr value;
4584 value = target->got_plt_section()->address() + got_offset;
4585 Reloc_funcs::pcrela32_check(view, value, addend, address);
4588 break;
4590 case elfcpp::R_X86_64_GOTPCREL64:
4592 gold_assert(have_got_offset);
4593 typename elfcpp::Elf_types<size>::Elf_Addr value;
4594 value = target->got_plt_section()->address() + got_offset;
4595 Reloc_funcs::pcrela64(view, value, addend, address);
4597 break;
4599 case elfcpp::R_X86_64_COPY:
4600 case elfcpp::R_X86_64_GLOB_DAT:
4601 case elfcpp::R_X86_64_JUMP_SLOT:
4602 case elfcpp::R_X86_64_RELATIVE:
4603 case elfcpp::R_X86_64_IRELATIVE:
4604 // These are outstanding tls relocs, which are unexpected when linking
4605 case elfcpp::R_X86_64_TPOFF64:
4606 case elfcpp::R_X86_64_DTPMOD64:
4607 case elfcpp::R_X86_64_TLSDESC:
4608 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4609 _("unexpected reloc %u in object file"),
4610 r_type);
4611 break;
4613 // These are initial tls relocs, which are expected when linking
4614 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4615 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4616 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
4617 case elfcpp::R_X86_64_TLSDESC_CALL:
4618 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4619 case elfcpp::R_X86_64_DTPOFF32:
4620 case elfcpp::R_X86_64_DTPOFF64:
4621 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4622 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4623 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
4624 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4625 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4626 view, address, view_size);
4627 break;
4629 case elfcpp::R_X86_64_SIZE32:
4630 case elfcpp::R_X86_64_SIZE64:
4631 default:
4632 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4633 _("unsupported reloc %u"),
4634 r_type);
4635 break;
4638 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4640 if (gsym == NULL)
4642 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4643 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4644 _("relocation overflow: "
4645 "reference to local symbol %u in %s"),
4646 r_sym, object->name().c_str());
4648 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4650 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4651 _("relocation overflow: "
4652 "reference to '%s' defined in %s"),
4653 gsym->name(),
4654 gsym->object()->name().c_str());
4656 else
4658 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4659 _("relocation overflow: reference to '%s'"),
4660 gsym->name());
4664 return true;
4667 // Perform a TLS relocation.
4669 template<int size>
4670 inline void
4671 Target_x86_64<size>::Relocate::relocate_tls(
4672 const Relocate_info<size, false>* relinfo,
4673 Target_x86_64<size>* target,
4674 size_t relnum,
4675 const elfcpp::Rela<size, false>& rela,
4676 unsigned int r_type,
4677 const Sized_symbol<size>* gsym,
4678 const Symbol_value<size>* psymval,
4679 unsigned char* view,
4680 typename elfcpp::Elf_types<size>::Elf_Addr address,
4681 section_size_type view_size)
4683 Output_segment* tls_segment = relinfo->layout->tls_segment();
4685 const Sized_relobj_file<size, false>* object = relinfo->object;
4686 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4687 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4688 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4690 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4692 const bool is_final = (gsym == NULL
4693 ? !parameters->options().shared()
4694 : gsym->final_value_is_known());
4695 size_t r_offset = rela.get_r_offset();
4696 tls::Tls_optimization optimized_type
4697 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type,
4698 r_offset, view);
4699 switch (r_type)
4701 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4702 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4704 // If this code sequence is used in a non-executable section,
4705 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4706 // on the assumption that it's being used by itself in a debug
4707 // section. Therefore, in the unlikely event that the code
4708 // sequence appears in a non-executable section, we simply
4709 // leave it unoptimized.
4710 optimized_type = tls::TLSOPT_NONE;
4712 if (optimized_type == tls::TLSOPT_TO_LE)
4714 if (tls_segment == NULL)
4716 gold_assert(parameters->errors()->error_count() > 0
4717 || issue_undefined_symbol_error(gsym));
4718 return;
4720 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4721 rela, r_type, value, view,
4722 view_size);
4723 break;
4725 else
4727 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4728 ? GOT_TYPE_TLS_OFFSET
4729 : GOT_TYPE_TLS_PAIR);
4730 unsigned int got_offset;
4731 if (gsym != NULL)
4733 gold_assert(gsym->has_got_offset(got_type));
4734 got_offset = gsym->got_offset(got_type) - target->got_size();
4736 else
4738 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4739 gold_assert(object->local_has_got_offset(r_sym, got_type));
4740 got_offset = (object->local_got_offset(r_sym, got_type)
4741 - target->got_size());
4743 if (optimized_type == tls::TLSOPT_TO_IE)
4745 value = target->got_plt_section()->address() + got_offset;
4746 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4747 value, view, address, view_size);
4748 break;
4750 else if (optimized_type == tls::TLSOPT_NONE)
4752 // Relocate the field with the offset of the pair of GOT
4753 // entries.
4754 value = target->got_plt_section()->address() + got_offset;
4755 Relocate_functions<size, false>::pcrela32(view, value, addend,
4756 address);
4757 break;
4760 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4761 _("unsupported reloc %u"), r_type);
4762 break;
4764 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4765 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
4766 case elfcpp::R_X86_64_TLSDESC_CALL:
4767 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4769 // See above comment for R_X86_64_TLSGD.
4770 optimized_type = tls::TLSOPT_NONE;
4772 if (optimized_type == tls::TLSOPT_TO_LE)
4774 if (tls_segment == NULL)
4776 gold_assert(parameters->errors()->error_count() > 0
4777 || issue_undefined_symbol_error(gsym));
4778 return;
4780 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4781 rela, r_type, value, view,
4782 view_size);
4783 break;
4785 else
4787 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4788 ? GOT_TYPE_TLS_OFFSET
4789 : GOT_TYPE_TLS_DESC);
4790 unsigned int got_offset = 0;
4791 if ((r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4792 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
4793 && optimized_type == tls::TLSOPT_NONE)
4795 // We created GOT entries in the .got.tlsdesc portion of
4796 // the .got.plt section, but the offset stored in the
4797 // symbol is the offset within .got.tlsdesc.
4798 got_offset = (target->got_size()
4799 + target->got_plt_section()->data_size());
4801 if (gsym != NULL)
4803 gold_assert(gsym->has_got_offset(got_type));
4804 got_offset += gsym->got_offset(got_type) - target->got_size();
4806 else
4808 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4809 gold_assert(object->local_has_got_offset(r_sym, got_type));
4810 got_offset += (object->local_got_offset(r_sym, got_type)
4811 - target->got_size());
4813 if (optimized_type == tls::TLSOPT_TO_IE)
4815 value = target->got_plt_section()->address() + got_offset;
4816 this->tls_desc_gd_to_ie(relinfo, relnum,
4817 rela, r_type, value, view, address,
4818 view_size);
4819 break;
4821 else if (optimized_type == tls::TLSOPT_NONE)
4823 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4824 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
4826 // Relocate the field with the offset of the pair of GOT
4827 // entries.
4828 value = target->got_plt_section()->address() + got_offset;
4829 Relocate_functions<size, false>::pcrela32(view, value, addend,
4830 address);
4832 break;
4835 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4836 _("unsupported reloc %u"), r_type);
4837 break;
4839 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4840 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4842 // See above comment for R_X86_64_TLSGD.
4843 optimized_type = tls::TLSOPT_NONE;
4845 if (optimized_type == tls::TLSOPT_TO_LE)
4847 if (tls_segment == NULL)
4849 gold_assert(parameters->errors()->error_count() > 0
4850 || issue_undefined_symbol_error(gsym));
4851 return;
4853 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4854 value, view, view_size);
4855 break;
4857 else if (optimized_type == tls::TLSOPT_NONE)
4859 // Relocate the field with the offset of the GOT entry for
4860 // the module index.
4861 unsigned int got_offset;
4862 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4863 - target->got_size());
4864 value = target->got_plt_section()->address() + got_offset;
4865 Relocate_functions<size, false>::pcrela32(view, value, addend,
4866 address);
4867 break;
4869 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4870 _("unsupported reloc %u"), r_type);
4871 break;
4873 case elfcpp::R_X86_64_DTPOFF32:
4874 // This relocation type is used in debugging information.
4875 // In that case we need to not optimize the value. If the
4876 // section is not executable, then we assume we should not
4877 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4878 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4879 // R_X86_64_TLSLD.
4880 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4882 if (tls_segment == NULL)
4884 gold_assert(parameters->errors()->error_count() > 0
4885 || issue_undefined_symbol_error(gsym));
4886 return;
4888 value -= tls_segment->memsz();
4890 Relocate_functions<size, false>::rela32(view, value, addend);
4891 break;
4893 case elfcpp::R_X86_64_DTPOFF64:
4894 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4895 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4897 if (tls_segment == NULL)
4899 gold_assert(parameters->errors()->error_count() > 0
4900 || issue_undefined_symbol_error(gsym));
4901 return;
4903 value -= tls_segment->memsz();
4905 Relocate_functions<size, false>::rela64(view, value, addend);
4906 break;
4908 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4909 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4910 case elfcpp::R_X86_64_CODE_6_GOTTPOFF:
4911 if (gsym != NULL
4912 && gsym->is_undefined()
4913 && parameters->options().output_is_executable())
4915 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4916 NULL, rela,
4917 r_type, value, view,
4918 view_size);
4919 break;
4921 else if (optimized_type == tls::TLSOPT_TO_LE)
4923 if (tls_segment == NULL)
4925 gold_assert(parameters->errors()->error_count() > 0
4926 || issue_undefined_symbol_error(gsym));
4927 return;
4929 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4930 tls_segment, rela,
4931 r_type, value, view,
4932 view_size);
4933 break;
4935 else if (optimized_type == tls::TLSOPT_NONE)
4937 // Relocate the field with the offset of the GOT entry for
4938 // the tp-relative offset of the symbol.
4939 unsigned int got_offset;
4940 if (gsym != NULL)
4942 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4943 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4944 - target->got_size());
4946 else
4948 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4949 gold_assert(object->local_has_got_offset(r_sym,
4950 GOT_TYPE_TLS_OFFSET));
4951 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4952 - target->got_size());
4954 value = target->got_plt_section()->address() + got_offset;
4955 Relocate_functions<size, false>::pcrela32(view, value, addend,
4956 address);
4957 break;
4959 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4960 _("unsupported reloc type %u"),
4961 r_type);
4962 break;
4964 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4965 if (tls_segment == NULL)
4967 gold_assert(parameters->errors()->error_count() > 0
4968 || issue_undefined_symbol_error(gsym));
4969 return;
4971 value -= tls_segment->memsz();
4972 Relocate_functions<size, false>::rela32(view, value, addend);
4973 break;
4977 // Do a relocation in which we convert a TLS General-Dynamic to an
4978 // Initial-Exec.
4980 template<int size>
4981 inline void
4982 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4983 const Relocate_info<size, false>* relinfo,
4984 size_t relnum,
4985 const elfcpp::Rela<size, false>& rela,
4986 unsigned int,
4987 typename elfcpp::Elf_types<size>::Elf_Addr value,
4988 unsigned char* view,
4989 typename elfcpp::Elf_types<size>::Elf_Addr address,
4990 section_size_type view_size)
4992 // For SIZE == 64:
4993 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4994 // .word 0x6666; rex64; call __tls_get_addr@PLT
4995 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4996 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4997 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4998 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4999 // For SIZE == 32:
5000 // leaq foo@tlsgd(%rip),%rdi;
5001 // .word 0x6666; rex64; call __tls_get_addr@PLT
5002 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5003 // leaq foo@tlsgd(%rip),%rdi;
5004 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5005 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5007 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5008 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5009 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5010 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5012 if (size == 64)
5014 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5015 -4);
5016 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5017 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5018 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5019 16);
5021 else
5023 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5024 -3);
5025 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5026 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5027 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5028 15);
5031 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5032 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
5033 address);
5035 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5036 // We can skip it.
5037 this->skip_call_tls_get_addr_ = true;
5040 // Do a relocation in which we convert a TLS General-Dynamic to a
5041 // Local-Exec.
5043 template<int size>
5044 inline void
5045 Target_x86_64<size>::Relocate::tls_gd_to_le(
5046 const Relocate_info<size, false>* relinfo,
5047 size_t relnum,
5048 Output_segment* tls_segment,
5049 const elfcpp::Rela<size, false>& rela,
5050 unsigned int,
5051 typename elfcpp::Elf_types<size>::Elf_Addr value,
5052 unsigned char* view,
5053 section_size_type view_size)
5055 // For SIZE == 64:
5056 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5057 // .word 0x6666; rex64; call __tls_get_addr@PLT
5058 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5059 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5060 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5061 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5062 // For SIZE == 32:
5063 // leaq foo@tlsgd(%rip),%rdi;
5064 // .word 0x6666; rex64; call __tls_get_addr@PLT
5065 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5066 // leaq foo@tlsgd(%rip),%rdi;
5067 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5068 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5070 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5071 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5072 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5073 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5075 if (size == 64)
5077 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5078 -4);
5079 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5080 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5081 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5082 16);
5084 else
5086 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5087 -3);
5088 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5089 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5091 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5092 15);
5095 value -= tls_segment->memsz();
5096 Relocate_functions<size, false>::rela32(view + 8, value, 0);
5098 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5099 // We can skip it.
5100 this->skip_call_tls_get_addr_ = true;
5103 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5105 template<int size>
5106 inline void
5107 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
5108 const Relocate_info<size, false>* relinfo,
5109 size_t relnum,
5110 const elfcpp::Rela<size, false>& rela,
5111 unsigned int r_type,
5112 typename elfcpp::Elf_types<size>::Elf_Addr value,
5113 unsigned char* view,
5114 typename elfcpp::Elf_types<size>::Elf_Addr address,
5115 section_size_type view_size)
5117 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
5118 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
5120 // LP64: leaq foo@tlsdesc(%rip), %rax
5121 // ==> movq foo@gottpoff(%rip), %rax
5122 // X32: rex leal foo@tlsdesc(%rip), %eax
5123 // ==> rex movl foo@gottpoff(%rip), %eax
5124 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5125 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5126 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5127 ((r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC
5128 || (view[-3] & 0xfb) == 0x48
5129 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5130 && view[-2] == 0x8d
5131 && (view[-1] & 0xc7) == 0x05));
5132 view[-2] = 0x8b;
5133 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5134 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
5136 else
5138 // LP64: call *foo@tlscall(%rax)
5139 // ==> xchg %ax, %ax
5140 // X32: call *foo@tlscall(%eax)
5141 // ==> nopl (%rax)
5142 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5143 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5144 int prefix = 0;
5145 if (size == 32 && view[0] == 0x67)
5147 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5148 view_size, 3);
5149 prefix = 1;
5151 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5152 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5153 if (prefix)
5155 view[0] = 0x0f;
5156 view[1] = 0x1f;
5157 view[2] = 0x00;
5159 else
5161 view[0] = 0x66;
5162 view[1] = 0x90;
5167 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5169 template<int size>
5170 inline void
5171 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
5172 const Relocate_info<size, false>* relinfo,
5173 size_t relnum,
5174 Output_segment* tls_segment,
5175 const elfcpp::Rela<size, false>& rela,
5176 unsigned int r_type,
5177 typename elfcpp::Elf_types<size>::Elf_Addr value,
5178 unsigned char* view,
5179 section_size_type view_size)
5181 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5183 // LP64: leaq foo@tlsdesc(%rip), %rax
5184 // ==> movq foo@tpoff, %rax
5185 // X32: rex leal foo@tlsdesc(%rip), %eax
5186 // ==> rex movl foo@tpoff, %eax
5187 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5188 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5189 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5190 (((view[-3] & 0xfb) == 0x48
5191 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5192 && view[-2] == 0x8d
5193 && (view[-1] & 0xc7) == 0x05));
5194 view[-3] = (view[-3] & 0x48) | ((view[-3] >> 2) & 1);
5195 view[-2] = 0xc7;
5196 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5197 value -= tls_segment->memsz();
5198 Relocate_functions<size, false>::rela32(view, value, 0);
5200 else if (r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
5202 // REX2: lea foo@tlsdesc(%rip), %reg
5203 // ==> mov foo@tpoff, %reg
5204 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5205 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5206 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5207 (view[-2] == 0x8d
5208 && (view[-1] & 0xc7) == 0x05));
5209 unsigned char rex2_mask = 4 | 4 << 4;
5210 view[-3] = (view[-3] & ~rex2_mask) | ((view[-3] & rex2_mask) >> 2);
5211 view[-2] = 0xc7;
5212 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5213 value -= tls_segment->memsz();
5214 Relocate_functions<size, false>::rela32(view, value, 0);
5216 else
5218 // LP64: call *foo@tlscall(%rax)
5219 // ==> xchg %ax, %ax
5220 // X32: call *foo@tlscall(%eax)
5221 // ==> nopl (%rax)
5222 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5223 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5224 int prefix = 0;
5225 if (size == 32 && view[0] == 0x67)
5227 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5228 view_size, 3);
5229 prefix = 1;
5231 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5232 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5233 if (prefix)
5235 view[0] = 0x0f;
5236 view[1] = 0x1f;
5237 view[2] = 0x00;
5239 else
5241 view[0] = 0x66;
5242 view[1] = 0x90;
5247 template<int size>
5248 inline void
5249 Target_x86_64<size>::Relocate::tls_ld_to_le(
5250 const Relocate_info<size, false>* relinfo,
5251 size_t relnum,
5252 Output_segment*,
5253 const elfcpp::Rela<size, false>& rela,
5254 unsigned int,
5255 typename elfcpp::Elf_types<size>::Elf_Addr,
5256 unsigned char* view,
5257 section_size_type view_size)
5259 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5260 // For SIZE == 64:
5261 // ... leq foo@dtpoff(%rax),%reg
5262 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5263 // For SIZE == 32:
5264 // ... leq foo@dtpoff(%rax),%reg
5265 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5266 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5267 // For SIZE == 64:
5268 // ... leq foo@dtpoff(%rax),%reg
5269 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5270 // For SIZE == 32:
5271 // ... leq foo@dtpoff(%rax),%reg
5272 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5274 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5275 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
5277 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5278 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
5280 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5281 view[4] == 0xe8 || view[4] == 0xff);
5283 if (view[4] == 0xe8)
5285 if (size == 64)
5286 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5287 else
5288 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5290 else
5292 if (size == 64)
5293 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5294 13);
5295 else
5296 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5297 13);
5300 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5301 // We can skip it.
5302 this->skip_call_tls_get_addr_ = true;
5305 // Do a relocation in which we convert a TLS Initial-Exec to a
5306 // Local-Exec.
5308 template<int size>
5309 inline void
5310 Target_x86_64<size>::Relocate::tls_ie_to_le(
5311 const Relocate_info<size, false>* relinfo,
5312 size_t relnum,
5313 Output_segment* tls_segment,
5314 const elfcpp::Rela<size, false>& rela,
5315 unsigned int r_type,
5316 typename elfcpp::Elf_types<size>::Elf_Addr value,
5317 unsigned char* view,
5318 section_size_type view_size)
5320 // We need to examine the opcodes to figure out which instruction we
5321 // are looking at.
5323 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5324 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5325 // addq %reg1,foo@gottpoff(%rip),%reg2 ==> addq $YY,%reg1,%reg2
5326 // addq foo@gottpoff(%rip),%reg1,%reg2 ==> addq $YY,%reg1,%reg2
5328 int off1;
5329 if (r_type == elfcpp::R_X86_64_CODE_6_GOTTPOFF)
5330 off1 = -5;
5331 else
5332 off1 = -3;
5334 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, off1);
5335 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5337 unsigned char op1 = view[off1];
5338 unsigned char op2 = view[-2];
5339 unsigned char op3 = view[-1];
5340 unsigned char reg = op3 >> 3;
5342 if (r_type == elfcpp::R_X86_64_GOTTPOFF)
5344 if (op2 == 0x8b)
5346 // movq
5347 if (op1 == 0x4c)
5348 view[-3] = 0x49;
5349 else if (size == 32 && op1 == 0x44)
5350 view[-3] = 0x41;
5351 view[-2] = 0xc7;
5352 view[-1] = 0xc0 | reg;
5354 else if (reg == 4)
5356 // Special handling for %rsp.
5357 if (op1 == 0x4c)
5358 view[-3] = 0x49;
5359 else if (size == 32 && op1 == 0x44)
5360 view[-3] = 0x41;
5361 view[-2] = 0x81;
5362 view[-1] = 0xc0 | reg;
5364 else
5366 // addq
5367 if (op1 == 0x4c)
5368 view[-3] = 0x4d;
5369 else if (size == 32 && op1 == 0x44)
5370 view[-3] = 0x45;
5371 view[-2] = 0x8d;
5372 view[-1] = 0x80 | reg | (reg << 3);
5375 else if (r_type == elfcpp::R_X86_64_CODE_4_GOTTPOFF)
5377 if (op2 == 0x8b)
5378 op2 = 0xc7;
5379 else
5380 op2 = 0x81;
5382 unsigned char rex2_mask = 4 | 4 << 4;
5383 view[-3] = (view[-3] & ~rex2_mask) | ((view[-3] & rex2_mask) >> 2);
5384 view[-2] = op2;
5385 view[-1] = 0xc0 | reg;
5387 else
5389 unsigned char updated_op1 = op1;
5391 // Set the R bits since they is inverted.
5392 updated_op1 |= 1 << 7 | 1 << 4;
5394 // Update the B bits from the R bits.
5395 if ((op1 & (1 << 7)) == 0)
5396 updated_op1 &= ~(1 << 5);
5397 if ((op1 & (1 << 4)) == 0)
5398 updated_op1 |= 1 << 3;
5400 view[-5] = updated_op1;
5401 view[-2] = 0x81;
5402 view[-1] = 0xc0 | reg;
5405 if (tls_segment != NULL)
5406 value -= tls_segment->memsz();
5407 Relocate_functions<size, false>::rela32(view, value, 0);
5410 // Relocate section data.
5412 template<int size>
5413 void
5414 Target_x86_64<size>::relocate_section(
5415 const Relocate_info<size, false>* relinfo,
5416 unsigned int sh_type,
5417 const unsigned char* prelocs,
5418 size_t reloc_count,
5419 Output_section* output_section,
5420 bool needs_special_offset_handling,
5421 unsigned char* view,
5422 typename elfcpp::Elf_types<size>::Elf_Addr address,
5423 section_size_type view_size,
5424 const Reloc_symbol_changes* reloc_symbol_changes)
5426 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5427 Classify_reloc;
5429 gold_assert(sh_type == elfcpp::SHT_RELA);
5431 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5432 gold::Default_comdat_behavior, Classify_reloc>(
5433 relinfo,
5434 this,
5435 prelocs,
5436 reloc_count,
5437 output_section,
5438 needs_special_offset_handling,
5439 view,
5440 address,
5441 view_size,
5442 reloc_symbol_changes);
5445 // Apply an incremental relocation. Incremental relocations always refer
5446 // to global symbols.
5448 template<int size>
5449 void
5450 Target_x86_64<size>::apply_relocation(
5451 const Relocate_info<size, false>* relinfo,
5452 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5453 unsigned int r_type,
5454 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5455 const Symbol* gsym,
5456 unsigned char* view,
5457 typename elfcpp::Elf_types<size>::Elf_Addr address,
5458 section_size_type view_size)
5460 gold::apply_relocation<size, false, Target_x86_64<size>,
5461 typename Target_x86_64<size>::Relocate>(
5462 relinfo,
5463 this,
5464 r_offset,
5465 r_type,
5466 r_addend,
5467 gsym,
5468 view,
5469 address,
5470 view_size);
5473 // Scan the relocs during a relocatable link.
5475 template<int size>
5476 void
5477 Target_x86_64<size>::scan_relocatable_relocs(
5478 Symbol_table* symtab,
5479 Layout* layout,
5480 Sized_relobj_file<size, false>* object,
5481 unsigned int data_shndx,
5482 unsigned int sh_type,
5483 const unsigned char* prelocs,
5484 size_t reloc_count,
5485 Output_section* output_section,
5486 bool needs_special_offset_handling,
5487 size_t local_symbol_count,
5488 const unsigned char* plocal_symbols,
5489 Relocatable_relocs* rr)
5491 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5492 Classify_reloc;
5493 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5494 Scan_relocatable_relocs;
5496 gold_assert(sh_type == elfcpp::SHT_RELA);
5498 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5499 symtab,
5500 layout,
5501 object,
5502 data_shndx,
5503 prelocs,
5504 reloc_count,
5505 output_section,
5506 needs_special_offset_handling,
5507 local_symbol_count,
5508 plocal_symbols,
5509 rr);
5512 // Scan the relocs for --emit-relocs.
5514 template<int size>
5515 void
5516 Target_x86_64<size>::emit_relocs_scan(
5517 Symbol_table* symtab,
5518 Layout* layout,
5519 Sized_relobj_file<size, false>* object,
5520 unsigned int data_shndx,
5521 unsigned int sh_type,
5522 const unsigned char* prelocs,
5523 size_t reloc_count,
5524 Output_section* output_section,
5525 bool needs_special_offset_handling,
5526 size_t local_symbol_count,
5527 const unsigned char* plocal_syms,
5528 Relocatable_relocs* rr)
5530 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5531 Classify_reloc;
5532 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5533 Emit_relocs_strategy;
5535 gold_assert(sh_type == elfcpp::SHT_RELA);
5537 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5538 symtab,
5539 layout,
5540 object,
5541 data_shndx,
5542 prelocs,
5543 reloc_count,
5544 output_section,
5545 needs_special_offset_handling,
5546 local_symbol_count,
5547 plocal_syms,
5548 rr);
5551 // Relocate a section during a relocatable link.
5553 template<int size>
5554 void
5555 Target_x86_64<size>::relocate_relocs(
5556 const Relocate_info<size, false>* relinfo,
5557 unsigned int sh_type,
5558 const unsigned char* prelocs,
5559 size_t reloc_count,
5560 Output_section* output_section,
5561 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5562 unsigned char* view,
5563 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5564 section_size_type view_size,
5565 unsigned char* reloc_view,
5566 section_size_type reloc_view_size)
5568 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5569 Classify_reloc;
5571 gold_assert(sh_type == elfcpp::SHT_RELA);
5573 gold::relocate_relocs<size, false, Classify_reloc>(
5574 relinfo,
5575 prelocs,
5576 reloc_count,
5577 output_section,
5578 offset_in_output_section,
5579 view,
5580 view_address,
5581 view_size,
5582 reloc_view,
5583 reloc_view_size);
5586 // Return the value to use for a dynamic which requires special
5587 // treatment. This is how we support equality comparisons of function
5588 // pointers across shared library boundaries, as described in the
5589 // processor specific ABI supplement.
5591 template<int size>
5592 uint64_t
5593 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5595 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5596 return this->plt_address_for_global(gsym);
5599 // Return a string used to fill a code section with nops to take up
5600 // the specified length.
5602 template<int size>
5603 std::string
5604 Target_x86_64<size>::do_code_fill(section_size_type length) const
5606 if (length >= 16)
5608 // Build a jmpq instruction to skip over the bytes.
5609 unsigned char jmp[5];
5610 jmp[0] = 0xe9;
5611 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5612 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5613 + std::string(length - 5, static_cast<char>(0x90)));
5616 // Nop sequences of various lengths.
5617 const char nop1[1] = { '\x90' }; // nop
5618 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5619 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5620 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5621 '\x00'};
5622 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5623 '\x00', '\x00' };
5624 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5625 '\x44', '\x00', '\x00' };
5626 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5627 '\x00', '\x00', '\x00',
5628 '\x00' };
5629 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5630 '\x00', '\x00', '\x00',
5631 '\x00', '\x00' };
5632 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5633 '\x84', '\x00', '\x00',
5634 '\x00', '\x00', '\x00' };
5635 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5636 '\x1f', '\x84', '\x00',
5637 '\x00', '\x00', '\x00',
5638 '\x00' };
5639 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5640 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5641 '\x00', '\x00', '\x00',
5642 '\x00', '\x00' };
5643 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5644 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5645 '\x84', '\x00', '\x00',
5646 '\x00', '\x00', '\x00' };
5647 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5648 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5649 '\x1f', '\x84', '\x00',
5650 '\x00', '\x00', '\x00',
5651 '\x00' };
5652 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5653 '\x66', '\x66', '\x2e', // data16
5654 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5655 '\x00', '\x00', '\x00',
5656 '\x00', '\x00' };
5657 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5658 '\x66', '\x66', '\x66', // data16; data16
5659 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5660 '\x84', '\x00', '\x00',
5661 '\x00', '\x00', '\x00' };
5663 const char* nops[16] = {
5664 NULL,
5665 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5666 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5669 return std::string(nops[length], length);
5672 // Return the addend to use for a target specific relocation. The
5673 // only target specific relocation is R_X86_64_TLSDESC for a local
5674 // symbol. We want to set the addend is the offset of the local
5675 // symbol in the TLS segment.
5677 template<int size>
5678 uint64_t
5679 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5680 uint64_t) const
5682 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5683 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5684 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5685 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5686 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5687 gold_assert(psymval->is_tls_symbol());
5688 // The value of a TLS symbol is the offset in the TLS segment.
5689 return psymval->value(ti.object, 0);
5692 // Return the value to use for the base of a DW_EH_PE_datarel offset
5693 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5694 // assembler can not write out the difference between two labels in
5695 // different sections, so instead of using a pc-relative value they
5696 // use an offset from the GOT.
5698 template<int size>
5699 uint64_t
5700 Target_x86_64<size>::do_ehframe_datarel_base() const
5702 gold_assert(this->global_offset_table_ != NULL);
5703 Symbol* sym = this->global_offset_table_;
5704 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5705 return ssym->value();
5708 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5709 // compiled with -fsplit-stack. The function calls non-split-stack
5710 // code. We have to change the function so that it always ensures
5711 // that it has enough stack space to run some random function.
5713 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5714 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5715 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5717 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5718 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5719 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5721 template<int size>
5722 void
5723 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5724 section_offset_type fnoffset,
5725 section_size_type fnsize,
5726 const unsigned char*,
5727 size_t,
5728 unsigned char* view,
5729 section_size_type view_size,
5730 std::string* from,
5731 std::string* to) const
5733 const char* const cmp_insn = reinterpret_cast<const char*>
5734 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5735 const char* const lea_r10_insn = reinterpret_cast<const char*>
5736 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5737 const char* const lea_r11_insn = reinterpret_cast<const char*>
5738 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5740 const size_t cmp_insn_len =
5741 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5742 const size_t lea_r10_insn_len =
5743 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5744 const size_t lea_r11_insn_len =
5745 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5746 const size_t nop_len = (size == 32 ? 7 : 8);
5748 // The function starts with a comparison of the stack pointer and a
5749 // field in the TCB. This is followed by a jump.
5751 // cmp %fs:NN,%rsp
5752 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5753 && fnsize > nop_len + 1)
5755 // We will call __morestack if the carry flag is set after this
5756 // comparison. We turn the comparison into an stc instruction
5757 // and some nops.
5758 view[fnoffset] = '\xf9';
5759 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5761 // lea NN(%rsp),%r10
5762 // lea NN(%rsp),%r11
5763 else if ((this->match_view(view, view_size, fnoffset,
5764 lea_r10_insn, lea_r10_insn_len)
5765 || this->match_view(view, view_size, fnoffset,
5766 lea_r11_insn, lea_r11_insn_len))
5767 && fnsize > 8)
5769 // This is loading an offset from the stack pointer for a
5770 // comparison. The offset is negative, so we decrease the
5771 // offset by the amount of space we need for the stack. This
5772 // means we will avoid calling __morestack if there happens to
5773 // be plenty of space on the stack already.
5774 unsigned char* pval = view + fnoffset + 4;
5775 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5776 val -= parameters->options().split_stack_adjust_size();
5777 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5779 else
5781 if (!object->has_no_split_stack())
5782 object->error(_("failed to match split-stack sequence at "
5783 "section %u offset %0zx"),
5784 shndx, static_cast<size_t>(fnoffset));
5785 return;
5788 // We have to change the function so that it calls
5789 // __morestack_non_split instead of __morestack. The former will
5790 // allocate additional stack space.
5791 *from = "__morestack";
5792 *to = "__morestack_non_split";
5795 // The selector for x86_64 object files. Note this is never instantiated
5796 // directly. It's only used in Target_selector_x86_64_nacl, below.
5798 template<int size>
5799 class Target_selector_x86_64 : public Target_selector_freebsd
5801 public:
5802 Target_selector_x86_64()
5803 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5804 (size == 64
5805 ? "elf64-x86-64" : "elf32-x86-64"),
5806 (size == 64
5807 ? "elf64-x86-64-freebsd"
5808 : "elf32-x86-64-freebsd"),
5809 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5812 Target*
5813 do_instantiate_target()
5814 { return new Target_x86_64<size>(); }
5818 // NaCl variant. It uses different PLT contents.
5820 template<int size>
5821 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5823 public:
5824 Output_data_plt_x86_64_nacl(Layout* layout,
5825 Output_data_got<64, false>* got,
5826 Output_data_got_plt_x86_64* got_plt,
5827 Output_data_space* got_irelative)
5828 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5829 got, got_plt, got_irelative)
5832 Output_data_plt_x86_64_nacl(Layout* layout,
5833 Output_data_got<64, false>* got,
5834 Output_data_got_plt_x86_64* got_plt,
5835 Output_data_space* got_irelative,
5836 unsigned int plt_count)
5837 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5838 got, got_plt, got_irelative,
5839 plt_count)
5842 protected:
5843 virtual unsigned int
5844 do_get_plt_entry_size() const
5845 { return plt_entry_size; }
5847 virtual void
5848 do_add_eh_frame(Layout* layout)
5850 layout->add_eh_frame_for_plt(this,
5851 this->plt_eh_frame_cie,
5852 this->plt_eh_frame_cie_size,
5853 plt_eh_frame_fde,
5854 plt_eh_frame_fde_size);
5857 virtual void
5858 do_fill_first_plt_entry(unsigned char* pov,
5859 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5860 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5862 virtual unsigned int
5863 do_fill_plt_entry(unsigned char* pov,
5864 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5865 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5866 unsigned int got_offset,
5867 unsigned int plt_offset,
5868 unsigned int plt_index);
5870 virtual void
5871 do_fill_tlsdesc_entry(unsigned char* pov,
5872 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5873 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5874 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5875 unsigned int tlsdesc_got_offset,
5876 unsigned int plt_offset);
5878 private:
5879 // The size of an entry in the PLT.
5880 static const int plt_entry_size = 64;
5882 // The first entry in the PLT.
5883 static const unsigned char first_plt_entry[plt_entry_size];
5885 // Other entries in the PLT for an executable.
5886 static const unsigned char plt_entry[plt_entry_size];
5888 // The reserved TLSDESC entry in the PLT for an executable.
5889 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5891 // The .eh_frame unwind information for the PLT.
5892 static const int plt_eh_frame_fde_size = 32;
5893 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5896 template<int size>
5897 class Target_x86_64_nacl : public Target_x86_64<size>
5899 public:
5900 Target_x86_64_nacl()
5901 : Target_x86_64<size>(&x86_64_nacl_info)
5904 virtual Output_data_plt_x86_64<size>*
5905 do_make_data_plt(Layout* layout,
5906 Output_data_got<64, false>* got,
5907 Output_data_got_plt_x86_64* got_plt,
5908 Output_data_space* got_irelative)
5910 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5911 got_irelative);
5914 virtual Output_data_plt_x86_64<size>*
5915 do_make_data_plt(Layout* layout,
5916 Output_data_got<64, false>* got,
5917 Output_data_got_plt_x86_64* got_plt,
5918 Output_data_space* got_irelative,
5919 unsigned int plt_count)
5921 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5922 got_irelative,
5923 plt_count);
5926 virtual std::string
5927 do_code_fill(section_size_type length) const;
5929 private:
5930 static const Target::Target_info x86_64_nacl_info;
5933 template<>
5934 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5936 64, // size
5937 false, // is_big_endian
5938 elfcpp::EM_X86_64, // machine_code
5939 false, // has_make_symbol
5940 false, // has_resolve
5941 true, // has_code_fill
5942 true, // is_default_stack_executable
5943 true, // can_icf_inline_merge_sections
5944 '\0', // wrap_char
5945 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5946 0x20000, // default_text_segment_address
5947 0x10000, // abi_pagesize (overridable by -z max-page-size)
5948 0x10000, // common_pagesize (overridable by -z common-page-size)
5949 true, // isolate_execinstr
5950 0x10000000, // rosegment_gap
5951 elfcpp::SHN_UNDEF, // small_common_shndx
5952 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5953 0, // small_common_section_flags
5954 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5955 NULL, // attributes_section
5956 NULL, // attributes_vendor
5957 "_start", // entry_symbol_name
5958 32, // hash_entry_size
5959 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5962 template<>
5963 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5965 32, // size
5966 false, // is_big_endian
5967 elfcpp::EM_X86_64, // machine_code
5968 false, // has_make_symbol
5969 false, // has_resolve
5970 true, // has_code_fill
5971 true, // is_default_stack_executable
5972 true, // can_icf_inline_merge_sections
5973 '\0', // wrap_char
5974 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5975 0x20000, // default_text_segment_address
5976 0x10000, // abi_pagesize (overridable by -z max-page-size)
5977 0x10000, // common_pagesize (overridable by -z common-page-size)
5978 true, // isolate_execinstr
5979 0x10000000, // rosegment_gap
5980 elfcpp::SHN_UNDEF, // small_common_shndx
5981 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5982 0, // small_common_section_flags
5983 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5984 NULL, // attributes_section
5985 NULL, // attributes_vendor
5986 "_start", // entry_symbol_name
5987 32, // hash_entry_size
5988 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5991 #define NACLMASK 0xe0 // 32-byte alignment mask.
5993 // The first entry in the PLT.
5995 template<int size>
5996 const unsigned char
5997 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5999 0xff, 0x35, // pushq contents of memory address
6000 0, 0, 0, 0, // replaced with address of .got + 8
6001 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
6002 0, 0, 0, 0, // replaced with address of .got + 16
6003 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6004 0x4d, 0x01, 0xfb, // add %r15, %r11
6005 0x41, 0xff, 0xe3, // jmpq *%r11
6007 // 9-byte nop sequence to pad out to the next 32-byte boundary.
6008 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
6010 // 32 bytes of nop to pad out to the standard size
6011 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6012 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6013 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6014 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6015 0x66, // excess data32 prefix
6016 0x90 // nop
6019 template<int size>
6020 void
6021 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
6022 unsigned char* pov,
6023 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6024 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
6026 memcpy(pov, first_plt_entry, plt_entry_size);
6027 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6028 (got_address + 8
6029 - (plt_address + 2 + 4)));
6030 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6031 (got_address + 16
6032 - (plt_address + 9 + 4)));
6035 // Subsequent entries in the PLT.
6037 template<int size>
6038 const unsigned char
6039 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
6041 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6042 0, 0, 0, 0, // replaced with address of symbol in .got
6043 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6044 0x4d, 0x01, 0xfb, // add %r15, %r11
6045 0x41, 0xff, 0xe3, // jmpq *%r11
6047 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6048 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6049 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6051 // Lazy GOT entries point here (32-byte aligned).
6052 0x68, // pushq immediate
6053 0, 0, 0, 0, // replaced with index into relocation table
6054 0xe9, // jmp relative
6055 0, 0, 0, 0, // replaced with offset to start of .plt0
6057 // 22 bytes of nop to pad out to the standard size.
6058 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6059 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6060 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6063 template<int size>
6064 unsigned int
6065 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
6066 unsigned char* pov,
6067 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6068 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6069 unsigned int got_offset,
6070 unsigned int plt_offset,
6071 unsigned int plt_index)
6073 memcpy(pov, plt_entry, plt_entry_size);
6074 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
6075 (got_address + got_offset
6076 - (plt_address + plt_offset
6077 + 3 + 4)));
6079 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
6080 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
6081 - (plt_offset + 38 + 4));
6083 return 32;
6086 // The reserved TLSDESC entry in the PLT.
6088 template<int size>
6089 const unsigned char
6090 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
6092 0xff, 0x35, // pushq x(%rip)
6093 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6094 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6095 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6096 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6097 0x4d, 0x01, 0xfb, // add %r15, %r11
6098 0x41, 0xff, 0xe3, // jmpq *%r11
6100 // 41 bytes of nop to pad out to the standard size.
6101 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6102 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6103 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6104 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6105 0x66, 0x66, // excess data32 prefixes
6106 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6109 template<int size>
6110 void
6111 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
6112 unsigned char* pov,
6113 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6114 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6115 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6116 unsigned int tlsdesc_got_offset,
6117 unsigned int plt_offset)
6119 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
6120 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6121 (got_address + 8
6122 - (plt_address + plt_offset
6123 + 2 + 4)));
6124 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6125 (got_base
6126 + tlsdesc_got_offset
6127 - (plt_address + plt_offset
6128 + 9 + 4)));
6131 // The .eh_frame unwind information for the PLT.
6133 template<int size>
6134 const unsigned char
6135 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
6137 0, 0, 0, 0, // Replaced with offset to .plt.
6138 0, 0, 0, 0, // Replaced with size of .plt.
6139 0, // Augmentation size.
6140 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
6141 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
6142 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
6143 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
6144 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
6145 13, // Block length.
6146 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
6147 elfcpp::DW_OP_breg16, 0, // Push %rip.
6148 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
6149 elfcpp::DW_OP_and, // & (%rip & 0x3f).
6150 elfcpp::DW_OP_const1u, 37, // Push 0x25.
6151 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
6152 elfcpp::DW_OP_lit3, // Push 3.
6153 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
6154 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6155 elfcpp::DW_CFA_nop, // Align to 32 bytes.
6156 elfcpp::DW_CFA_nop
6159 // Return a string used to fill a code section with nops.
6160 // For NaCl, long NOPs are only valid if they do not cross
6161 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6162 template<int size>
6163 std::string
6164 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
6166 return std::string(length, static_cast<char>(0x90));
6169 // The selector for x86_64-nacl object files.
6171 template<int size>
6172 class Target_selector_x86_64_nacl
6173 : public Target_selector_nacl<Target_selector_x86_64<size>,
6174 Target_x86_64_nacl<size> >
6176 public:
6177 Target_selector_x86_64_nacl()
6178 : Target_selector_nacl<Target_selector_x86_64<size>,
6179 Target_x86_64_nacl<size> >("x86-64",
6180 size == 64
6181 ? "elf64-x86-64-nacl"
6182 : "elf32-x86-64-nacl",
6183 size == 64
6184 ? "elf_x86_64_nacl"
6185 : "elf32_x86_64_nacl")
6189 Target_selector_x86_64_nacl<64> target_selector_x86_64;
6190 Target_selector_x86_64_nacl<32> target_selector_x32;
6192 } // End anonymous namespace.