Update year range in copyright notice of binutils files
[binutils-gdb.git] / gold / x86_64.cc
blob58e191a055aa2cb726003b96c71dc2a41280cc19
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_4_GOTTPOFF:
2924 if (r_offset <= 4 || *(reloc_view - 4) != 0xd5)
2925 return tls::TLSOPT_NONE;
2926 // Fall through.
2927 case elfcpp::R_X86_64_GOTTPOFF:
2928 // These are Initial-Exec relocs which get the thread offset
2929 // from the GOT. If we know that we are linking against the
2930 // local symbol, we can switch to Local-Exec, which links the
2931 // thread offset into the instruction.
2932 if (is_final)
2933 return tls::TLSOPT_TO_LE;
2934 return tls::TLSOPT_NONE;
2936 case elfcpp::R_X86_64_TPOFF32:
2937 // When we already have Local-Exec, there is nothing further we
2938 // can do.
2939 return tls::TLSOPT_NONE;
2941 default:
2942 gold_unreachable();
2946 // Get the Reference_flags for a particular relocation.
2948 template<int size>
2950 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2952 switch (r_type)
2954 case elfcpp::R_X86_64_NONE:
2955 case elfcpp::R_X86_64_GNU_VTINHERIT:
2956 case elfcpp::R_X86_64_GNU_VTENTRY:
2957 case elfcpp::R_X86_64_GOTPC32:
2958 case elfcpp::R_X86_64_GOTPC64:
2959 // No symbol reference.
2960 return 0;
2962 case elfcpp::R_X86_64_64:
2963 case elfcpp::R_X86_64_32:
2964 case elfcpp::R_X86_64_32S:
2965 case elfcpp::R_X86_64_16:
2966 case elfcpp::R_X86_64_8:
2967 return Symbol::ABSOLUTE_REF;
2969 case elfcpp::R_X86_64_PC64:
2970 case elfcpp::R_X86_64_PC32:
2971 case elfcpp::R_X86_64_PC16:
2972 case elfcpp::R_X86_64_PC8:
2973 case elfcpp::R_X86_64_GOTOFF64:
2974 return Symbol::RELATIVE_REF;
2976 case elfcpp::R_X86_64_PLT32:
2977 case elfcpp::R_X86_64_PLTOFF64:
2978 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2980 case elfcpp::R_X86_64_GOT64:
2981 case elfcpp::R_X86_64_GOT32:
2982 case elfcpp::R_X86_64_GOTPCREL64:
2983 case elfcpp::R_X86_64_GOTPCREL:
2984 case elfcpp::R_X86_64_GOTPCRELX:
2985 case elfcpp::R_X86_64_REX_GOTPCRELX:
2986 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
2987 case elfcpp::R_X86_64_GOTPLT64:
2988 // Absolute in GOT.
2989 return Symbol::ABSOLUTE_REF;
2991 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2992 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2993 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
2994 case elfcpp::R_X86_64_TLSDESC_CALL:
2995 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2996 case elfcpp::R_X86_64_DTPOFF32:
2997 case elfcpp::R_X86_64_DTPOFF64:
2998 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2999 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3000 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3001 return Symbol::TLS_REF;
3003 case elfcpp::R_X86_64_COPY:
3004 case elfcpp::R_X86_64_GLOB_DAT:
3005 case elfcpp::R_X86_64_JUMP_SLOT:
3006 case elfcpp::R_X86_64_RELATIVE:
3007 case elfcpp::R_X86_64_IRELATIVE:
3008 case elfcpp::R_X86_64_TPOFF64:
3009 case elfcpp::R_X86_64_DTPMOD64:
3010 case elfcpp::R_X86_64_TLSDESC:
3011 case elfcpp::R_X86_64_SIZE32:
3012 case elfcpp::R_X86_64_SIZE64:
3013 default:
3014 // Not expected. We will give an error later.
3015 return 0;
3019 // Report an unsupported relocation against a local symbol.
3021 template<int size>
3022 void
3023 Target_x86_64<size>::Scan::unsupported_reloc_local(
3024 Sized_relobj_file<size, false>* object,
3025 unsigned int r_type)
3027 gold_error(_("%s: unsupported reloc %u against local symbol"),
3028 object->name().c_str(), r_type);
3031 // We are about to emit a dynamic relocation of type R_TYPE. If the
3032 // dynamic linker does not support it, issue an error. The GNU linker
3033 // only issues a non-PIC error for an allocated read-only section.
3034 // Here we know the section is allocated, but we don't know that it is
3035 // read-only. But we check for all the relocation types which the
3036 // glibc dynamic linker supports, so it seems appropriate to issue an
3037 // error even if the section is not read-only. If GSYM is not NULL,
3038 // it is the symbol the relocation is against; if it is NULL, the
3039 // relocation is against a local symbol.
3041 template<int size>
3042 void
3043 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
3044 Symbol* gsym)
3046 switch (r_type)
3048 // These are the relocation types supported by glibc for x86_64
3049 // which should always work.
3050 case elfcpp::R_X86_64_RELATIVE:
3051 case elfcpp::R_X86_64_IRELATIVE:
3052 case elfcpp::R_X86_64_GLOB_DAT:
3053 case elfcpp::R_X86_64_JUMP_SLOT:
3054 case elfcpp::R_X86_64_DTPMOD64:
3055 case elfcpp::R_X86_64_DTPOFF64:
3056 case elfcpp::R_X86_64_TPOFF64:
3057 case elfcpp::R_X86_64_64:
3058 case elfcpp::R_X86_64_COPY:
3059 return;
3061 // glibc supports these reloc types, but they can overflow.
3062 case elfcpp::R_X86_64_PC32:
3063 // A PC relative reference is OK against a local symbol or if
3064 // the symbol is defined locally.
3065 if (gsym == NULL
3066 || (!gsym->is_from_dynobj()
3067 && !gsym->is_undefined()
3068 && !gsym->is_preemptible()))
3069 return;
3070 // Fall through.
3071 case elfcpp::R_X86_64_32:
3072 // R_X86_64_32 is OK for x32.
3073 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3074 return;
3075 if (this->issued_non_pic_error_)
3076 return;
3077 gold_assert(parameters->options().output_is_position_independent());
3078 if (gsym == NULL)
3079 object->error(_("requires dynamic R_X86_64_32 reloc which may "
3080 "overflow at runtime; recompile with -fPIC"));
3081 else
3083 const char *r_name;
3084 switch (r_type)
3086 case elfcpp::R_X86_64_32:
3087 r_name = "R_X86_64_32";
3088 break;
3089 case elfcpp::R_X86_64_PC32:
3090 r_name = "R_X86_64_PC32";
3091 break;
3092 default:
3093 gold_unreachable();
3094 break;
3096 object->error(_("requires dynamic %s reloc against '%s' "
3097 "which may overflow at runtime; recompile "
3098 "with -fPIC"),
3099 r_name, gsym->name());
3101 this->issued_non_pic_error_ = true;
3102 return;
3104 default:
3105 // This prevents us from issuing more than one error per reloc
3106 // section. But we can still wind up issuing more than one
3107 // error per object file.
3108 if (this->issued_non_pic_error_)
3109 return;
3110 gold_assert(parameters->options().output_is_position_independent());
3111 object->error(_("requires unsupported dynamic reloc %u; "
3112 "recompile with -fPIC"),
3113 r_type);
3114 this->issued_non_pic_error_ = true;
3115 return;
3117 case elfcpp::R_X86_64_NONE:
3118 gold_unreachable();
3122 // Return whether we need to make a PLT entry for a relocation of the
3123 // given type against a STT_GNU_IFUNC symbol.
3125 template<int size>
3126 bool
3127 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
3128 Sized_relobj_file<size, false>* object,
3129 unsigned int r_type)
3131 int flags = Scan::get_reference_flags(r_type);
3132 if (flags & Symbol::TLS_REF)
3133 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3134 object->name().c_str(), r_type);
3135 return flags != 0;
3138 // Scan a relocation for a local symbol.
3140 template<int size>
3141 inline void
3142 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
3143 Layout* layout,
3144 Target_x86_64<size>* target,
3145 Sized_relobj_file<size, false>* object,
3146 unsigned int data_shndx,
3147 Output_section* output_section,
3148 const elfcpp::Rela<size, false>& reloc,
3149 unsigned int r_type,
3150 const elfcpp::Sym<size, false>& lsym,
3151 bool is_discarded)
3153 if (is_discarded)
3154 return;
3156 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3157 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3158 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3160 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3161 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3164 const unsigned char* reloc_view = NULL;
3166 switch (r_type)
3168 case elfcpp::R_X86_64_NONE:
3169 case elfcpp::R_X86_64_GNU_VTINHERIT:
3170 case elfcpp::R_X86_64_GNU_VTENTRY:
3171 break;
3173 case elfcpp::R_X86_64_64:
3174 // If building a shared library (or a position-independent
3175 // executable), we need to create a dynamic relocation for this
3176 // location. The relocation applied at link time will apply the
3177 // link-time value, so we flag the location with an
3178 // R_X86_64_RELATIVE relocation so the dynamic loader can
3179 // relocate it easily.
3180 if (parameters->options().output_is_position_independent())
3182 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3183 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3184 rela_dyn->add_local_relative(object, r_sym,
3185 (size == 32
3186 ? elfcpp::R_X86_64_RELATIVE64
3187 : elfcpp::R_X86_64_RELATIVE),
3188 output_section, data_shndx,
3189 reloc.get_r_offset(),
3190 reloc.get_r_addend(), is_ifunc);
3192 break;
3194 case elfcpp::R_X86_64_32:
3195 case elfcpp::R_X86_64_32S:
3196 case elfcpp::R_X86_64_16:
3197 case elfcpp::R_X86_64_8:
3198 // If building a shared library (or a position-independent
3199 // executable), we need to create a dynamic relocation for this
3200 // location. We can't use an R_X86_64_RELATIVE relocation
3201 // because that is always a 64-bit relocation.
3202 if (parameters->options().output_is_position_independent())
3204 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3205 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3207 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3208 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3209 rela_dyn->add_local_relative(object, r_sym,
3210 elfcpp::R_X86_64_RELATIVE,
3211 output_section, data_shndx,
3212 reloc.get_r_offset(),
3213 reloc.get_r_addend(), is_ifunc);
3214 break;
3217 this->check_non_pic(object, r_type, NULL);
3219 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3220 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3221 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3222 rela_dyn->add_local(object, r_sym, r_type, output_section,
3223 data_shndx, reloc.get_r_offset(),
3224 reloc.get_r_addend());
3225 else
3227 gold_assert(lsym.get_st_value() == 0);
3228 unsigned int shndx = lsym.get_st_shndx();
3229 bool is_ordinary;
3230 shndx = object->adjust_sym_shndx(r_sym, shndx,
3231 &is_ordinary);
3232 if (!is_ordinary)
3233 object->error(_("section symbol %u has bad shndx %u"),
3234 r_sym, shndx);
3235 else
3236 rela_dyn->add_local_section(object, shndx,
3237 r_type, output_section,
3238 data_shndx, reloc.get_r_offset(),
3239 reloc.get_r_addend());
3242 break;
3244 case elfcpp::R_X86_64_PC64:
3245 case elfcpp::R_X86_64_PC32:
3246 case elfcpp::R_X86_64_PC16:
3247 case elfcpp::R_X86_64_PC8:
3248 break;
3250 case elfcpp::R_X86_64_PLT32:
3251 // Since we know this is a local symbol, we can handle this as a
3252 // PC32 reloc.
3253 break;
3255 case elfcpp::R_X86_64_GOTPC32:
3256 case elfcpp::R_X86_64_GOTOFF64:
3257 case elfcpp::R_X86_64_GOTPC64:
3258 case elfcpp::R_X86_64_PLTOFF64:
3259 // We need a GOT section.
3260 target->got_section(symtab, layout);
3261 // For PLTOFF64, we'd normally want a PLT section, but since we
3262 // know this is a local symbol, no PLT is needed.
3263 break;
3265 case elfcpp::R_X86_64_GOT64:
3266 case elfcpp::R_X86_64_GOT32:
3267 case elfcpp::R_X86_64_GOTPCREL64:
3268 case elfcpp::R_X86_64_GOTPCREL:
3269 case elfcpp::R_X86_64_GOTPCRELX:
3270 case elfcpp::R_X86_64_REX_GOTPCRELX:
3271 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3272 case elfcpp::R_X86_64_GOTPLT64:
3274 // The symbol requires a GOT section.
3275 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3277 // If the relocation symbol isn't IFUNC,
3278 // and is local, then we will convert
3279 // mov foo@GOTPCREL(%rip), %reg
3280 // to lea foo(%rip), %reg.
3281 // in Relocate::relocate.
3282 size_t r_offset = reloc.get_r_offset();
3283 if (!parameters->incremental()
3284 && (((r_type == elfcpp::R_X86_64_GOTPCREL
3285 || r_type == elfcpp::R_X86_64_GOTPCRELX
3286 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3287 && r_offset >= 2)
3288 || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3289 && r_offset >= 4))
3290 && reloc.get_r_addend() == -4
3291 && !is_ifunc)
3293 section_size_type stype;
3294 const unsigned char* view = object->section_contents(data_shndx,
3295 &stype, true);
3296 if (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3297 && view[r_offset - 4] != 0xd5)
3298 goto need_got;
3300 if (view[r_offset - 2] == 0x8b)
3301 break;
3304 need_got:
3306 // The symbol requires a GOT entry.
3307 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3309 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3310 // lets function pointers compare correctly with shared
3311 // libraries. Otherwise we would need an IRELATIVE reloc.
3312 bool is_new;
3313 if (is_ifunc)
3314 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3315 else
3316 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3317 if (is_new)
3319 // If we are generating a shared object, we need to add a
3320 // dynamic relocation for this symbol's GOT entry.
3321 if (parameters->options().output_is_position_independent())
3323 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3324 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3325 if (r_type != elfcpp::R_X86_64_GOT32)
3327 unsigned int got_offset =
3328 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3329 rela_dyn->add_local_relative(object, r_sym,
3330 elfcpp::R_X86_64_RELATIVE,
3331 got, got_offset, 0, is_ifunc);
3333 else
3335 this->check_non_pic(object, r_type, NULL);
3337 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3338 rela_dyn->add_local(
3339 object, r_sym, r_type, got,
3340 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3344 // For GOTPLT64, we'd normally want a PLT section, but since
3345 // we know this is a local symbol, no PLT is needed.
3347 break;
3349 case elfcpp::R_X86_64_COPY:
3350 case elfcpp::R_X86_64_GLOB_DAT:
3351 case elfcpp::R_X86_64_JUMP_SLOT:
3352 case elfcpp::R_X86_64_RELATIVE:
3353 case elfcpp::R_X86_64_IRELATIVE:
3354 // These are outstanding tls relocs, which are unexpected when linking
3355 case elfcpp::R_X86_64_TPOFF64:
3356 case elfcpp::R_X86_64_DTPMOD64:
3357 case elfcpp::R_X86_64_TLSDESC:
3358 gold_error(_("%s: unexpected reloc %u in object file"),
3359 object->name().c_str(), r_type);
3360 break;
3362 // These are initial tls relocs, which are expected when linking
3363 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3364 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3366 section_size_type stype;
3367 reloc_view = object->section_contents(data_shndx, &stype, true);
3369 // Fall through.
3370 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3371 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3372 case elfcpp::R_X86_64_TLSDESC_CALL:
3373 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3374 case elfcpp::R_X86_64_DTPOFF32:
3375 case elfcpp::R_X86_64_DTPOFF64:
3376 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3377 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3379 bool output_is_shared = parameters->options().shared();
3380 size_t r_offset = reloc.get_r_offset();
3381 const tls::Tls_optimization optimized_type
3382 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3383 r_type, r_offset,
3384 reloc_view + r_offset);
3385 switch (r_type)
3387 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3388 if (optimized_type == tls::TLSOPT_NONE)
3390 // Create a pair of GOT entries for the module index and
3391 // dtv-relative offset.
3392 Output_data_got<64, false>* got
3393 = target->got_section(symtab, layout);
3394 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3395 unsigned int shndx = lsym.get_st_shndx();
3396 bool is_ordinary;
3397 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3398 if (!is_ordinary)
3399 object->error(_("local symbol %u has bad shndx %u"),
3400 r_sym, shndx);
3401 else
3402 got->add_local_pair_with_rel(object, r_sym,
3403 shndx,
3404 GOT_TYPE_TLS_PAIR,
3405 target->rela_dyn_section(layout),
3406 elfcpp::R_X86_64_DTPMOD64);
3408 else if (optimized_type != tls::TLSOPT_TO_LE)
3409 unsupported_reloc_local(object, r_type);
3410 break;
3412 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3413 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3414 target->define_tls_base_symbol(symtab, layout);
3415 if (optimized_type == tls::TLSOPT_NONE)
3417 // Create reserved PLT and GOT entries for the resolver.
3418 target->reserve_tlsdesc_entries(symtab, layout);
3420 // Generate a double GOT entry with an
3421 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3422 // is resolved lazily, so the GOT entry needs to be in
3423 // an area in .got.plt, not .got. Call got_section to
3424 // make sure the section has been created.
3425 target->got_section(symtab, layout);
3426 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3427 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3428 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3430 unsigned int got_offset = got->add_constant(0);
3431 got->add_constant(0);
3432 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3433 got_offset);
3434 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3435 // We store the arguments we need in a vector, and
3436 // use the index into the vector as the parameter
3437 // to pass to the target specific routines.
3438 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3439 void* arg = reinterpret_cast<void*>(intarg);
3440 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3441 got, got_offset, 0);
3444 else if (optimized_type != tls::TLSOPT_TO_LE)
3445 unsupported_reloc_local(object, r_type);
3446 break;
3448 case elfcpp::R_X86_64_TLSDESC_CALL:
3449 break;
3451 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3452 if (optimized_type == tls::TLSOPT_NONE)
3454 // Create a GOT entry for the module index.
3455 target->got_mod_index_entry(symtab, layout, object);
3457 else if (optimized_type != tls::TLSOPT_TO_LE)
3458 unsupported_reloc_local(object, r_type);
3459 break;
3461 case elfcpp::R_X86_64_DTPOFF32:
3462 case elfcpp::R_X86_64_DTPOFF64:
3463 break;
3465 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3466 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3467 layout->set_has_static_tls();
3468 if (optimized_type == tls::TLSOPT_NONE)
3470 // Create a GOT entry for the tp-relative offset.
3471 Output_data_got<64, false>* got
3472 = target->got_section(symtab, layout);
3473 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3474 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3475 target->rela_dyn_section(layout),
3476 elfcpp::R_X86_64_TPOFF64);
3478 else if (optimized_type != tls::TLSOPT_TO_LE)
3479 unsupported_reloc_local(object, r_type);
3480 break;
3482 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3483 layout->set_has_static_tls();
3484 if (output_is_shared)
3485 unsupported_reloc_local(object, r_type);
3486 break;
3488 default:
3489 gold_unreachable();
3492 break;
3494 case elfcpp::R_X86_64_SIZE32:
3495 case elfcpp::R_X86_64_SIZE64:
3496 default:
3497 gold_error(_("%s: unsupported reloc %u against local symbol"),
3498 object->name().c_str(), r_type);
3499 break;
3504 // Report an unsupported relocation against a global symbol.
3506 template<int size>
3507 void
3508 Target_x86_64<size>::Scan::unsupported_reloc_global(
3509 Sized_relobj_file<size, false>* object,
3510 unsigned int r_type,
3511 Symbol* gsym)
3513 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3514 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3517 // Returns true if this relocation type could be that of a function pointer.
3518 template<int size>
3519 inline bool
3520 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3521 Sized_relobj_file<size, false>* src_obj,
3522 unsigned int src_indx,
3523 unsigned int r_offset,
3524 unsigned int r_type)
3526 switch (r_type)
3528 case elfcpp::R_X86_64_64:
3529 case elfcpp::R_X86_64_32:
3530 case elfcpp::R_X86_64_32S:
3531 case elfcpp::R_X86_64_16:
3532 case elfcpp::R_X86_64_8:
3533 case elfcpp::R_X86_64_GOT64:
3534 case elfcpp::R_X86_64_GOT32:
3535 case elfcpp::R_X86_64_GOTPCREL64:
3536 case elfcpp::R_X86_64_GOTPCREL:
3537 case elfcpp::R_X86_64_GOTPCRELX:
3538 case elfcpp::R_X86_64_REX_GOTPCRELX:
3539 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3540 case elfcpp::R_X86_64_GOTPLT64:
3542 return true;
3544 case elfcpp::R_X86_64_PC32:
3546 // This relocation may be used both for function calls and
3547 // for taking address of a function. We distinguish between
3548 // them by checking the opcodes.
3549 uint64_t sh_flags = src_obj->section_flags(src_indx);
3550 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3551 if (is_executable)
3553 section_size_type stype;
3554 const unsigned char* view = src_obj->section_contents(src_indx,
3555 &stype,
3556 true);
3558 // call
3559 if (r_offset >= 1
3560 && view[r_offset - 1] == 0xe8)
3561 return false;
3563 // jmp
3564 if (r_offset >= 1
3565 && view[r_offset - 1] == 0xe9)
3566 return false;
3568 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3569 if (r_offset >= 2
3570 && view[r_offset - 2] == 0x0f
3571 && view[r_offset - 1] >= 0x80
3572 && view[r_offset - 1] <= 0x8f)
3573 return false;
3576 // Be conservative and treat all others as function pointers.
3577 return true;
3580 return false;
3583 // For safe ICF, scan a relocation for a local symbol to check if it
3584 // corresponds to a function pointer being taken. In that case mark
3585 // the function whose pointer was taken as not foldable.
3587 template<int size>
3588 inline bool
3589 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3590 Symbol_table* ,
3591 Layout* ,
3592 Target_x86_64<size>* ,
3593 Sized_relobj_file<size, false>* src_obj,
3594 unsigned int src_indx,
3595 Output_section* ,
3596 const elfcpp::Rela<size, false>& reloc,
3597 unsigned int r_type,
3598 const elfcpp::Sym<size, false>&)
3600 return possible_function_pointer_reloc(src_obj, src_indx,
3601 reloc.get_r_offset(), r_type);
3604 // For safe ICF, scan a relocation for a global symbol to check if it
3605 // corresponds to a function pointer being taken. In that case mark
3606 // the function whose pointer was taken as not foldable.
3608 template<int size>
3609 inline bool
3610 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3611 Symbol_table*,
3612 Layout* ,
3613 Target_x86_64<size>* ,
3614 Sized_relobj_file<size, false>* src_obj,
3615 unsigned int src_indx,
3616 Output_section* ,
3617 const elfcpp::Rela<size, false>& reloc,
3618 unsigned int r_type,
3619 Symbol*)
3621 return possible_function_pointer_reloc(src_obj, src_indx,
3622 reloc.get_r_offset(), r_type);
3625 // Scan a relocation for a global symbol.
3627 template<int size>
3628 inline void
3629 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3630 Layout* layout,
3631 Target_x86_64<size>* target,
3632 Sized_relobj_file<size, false>* object,
3633 unsigned int data_shndx,
3634 Output_section* output_section,
3635 const elfcpp::Rela<size, false>& reloc,
3636 unsigned int r_type,
3637 Symbol* gsym)
3639 // A STT_GNU_IFUNC symbol may require a PLT entry.
3640 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3641 && this->reloc_needs_plt_for_ifunc(object, r_type))
3642 target->make_plt_entry(symtab, layout, gsym);
3644 const unsigned char *reloc_view = NULL;
3646 switch (r_type)
3648 case elfcpp::R_X86_64_NONE:
3649 case elfcpp::R_X86_64_GNU_VTINHERIT:
3650 case elfcpp::R_X86_64_GNU_VTENTRY:
3651 break;
3653 case elfcpp::R_X86_64_64:
3654 case elfcpp::R_X86_64_32:
3655 case elfcpp::R_X86_64_32S:
3656 case elfcpp::R_X86_64_16:
3657 case elfcpp::R_X86_64_8:
3659 // Make a PLT entry if necessary.
3660 if (gsym->needs_plt_entry())
3662 target->make_plt_entry(symtab, layout, gsym);
3663 // Since this is not a PC-relative relocation, we may be
3664 // taking the address of a function. In that case we need to
3665 // set the entry in the dynamic symbol table to the address of
3666 // the PLT entry.
3667 if (gsym->is_from_dynobj() && !parameters->options().shared())
3668 gsym->set_needs_dynsym_value();
3670 // Make a dynamic relocation if necessary.
3671 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3673 if (!parameters->options().output_is_position_independent()
3674 && gsym->may_need_copy_reloc())
3676 target->copy_reloc(symtab, layout, object,
3677 data_shndx, output_section, gsym, reloc);
3679 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3680 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3681 && gsym->type() == elfcpp::STT_GNU_IFUNC
3682 && gsym->can_use_relative_reloc(false)
3683 && !gsym->is_from_dynobj()
3684 && !gsym->is_undefined()
3685 && !gsym->is_preemptible())
3687 // Use an IRELATIVE reloc for a locally defined
3688 // STT_GNU_IFUNC symbol. This makes a function
3689 // address in a PIE executable match the address in a
3690 // shared library that it links against.
3691 Reloc_section* rela_dyn =
3692 target->rela_irelative_section(layout);
3693 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3694 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3695 output_section, object,
3696 data_shndx,
3697 reloc.get_r_offset(),
3698 reloc.get_r_addend());
3700 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3701 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3702 && gsym->can_use_relative_reloc(false))
3704 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3705 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3706 output_section, object,
3707 data_shndx,
3708 reloc.get_r_offset(),
3709 reloc.get_r_addend(), false);
3711 else
3713 this->check_non_pic(object, r_type, gsym);
3714 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3715 rela_dyn->add_global(gsym, r_type, output_section, object,
3716 data_shndx, reloc.get_r_offset(),
3717 reloc.get_r_addend());
3721 break;
3723 case elfcpp::R_X86_64_PC64:
3724 case elfcpp::R_X86_64_PC32:
3725 case elfcpp::R_X86_64_PC16:
3726 case elfcpp::R_X86_64_PC8:
3728 // Make a PLT entry if necessary.
3729 if (gsym->needs_plt_entry())
3730 target->make_plt_entry(symtab, layout, gsym);
3731 // Make a dynamic relocation if necessary.
3732 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3734 if (parameters->options().output_is_executable()
3735 && gsym->may_need_copy_reloc())
3737 target->copy_reloc(symtab, layout, object,
3738 data_shndx, output_section, gsym, reloc);
3740 else
3742 this->check_non_pic(object, r_type, gsym);
3743 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3744 rela_dyn->add_global(gsym, r_type, output_section, object,
3745 data_shndx, reloc.get_r_offset(),
3746 reloc.get_r_addend());
3750 break;
3752 case elfcpp::R_X86_64_GOT64:
3753 case elfcpp::R_X86_64_GOT32:
3754 case elfcpp::R_X86_64_GOTPCREL64:
3755 case elfcpp::R_X86_64_GOTPCREL:
3756 case elfcpp::R_X86_64_GOTPCRELX:
3757 case elfcpp::R_X86_64_REX_GOTPCRELX:
3758 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
3759 case elfcpp::R_X86_64_GOTPLT64:
3761 // The symbol requires a GOT entry.
3762 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3764 // If we convert this from
3765 // mov foo@GOTPCREL(%rip), %reg
3766 // to lea foo(%rip), %reg.
3767 // OR
3768 // if we convert
3769 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3770 // (callq|jmpq) foo
3771 // in Relocate::relocate, then there is nothing to do here.
3772 // We cannot make these optimizations in incremental linking mode,
3773 // because we look at the opcode to decide whether or not to make
3774 // change, and during an incremental update, the change may have
3775 // already been applied.
3777 Lazy_view<size> view(object, data_shndx);
3778 size_t r_offset = reloc.get_r_offset();
3779 if (!parameters->incremental()
3780 && reloc.get_r_addend() == -4
3781 && ((r_type != elfcpp::R_X86_64_CODE_4_GOTPCRELX
3782 && r_offset >= 2)
3783 || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX
3784 && r_offset >= 4
3785 && view[r_offset - 4] == 0xd5))
3786 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3787 r_offset, &view))
3788 break;
3790 if (!parameters->incremental()
3791 && r_offset >= 2
3792 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3793 r_offset,
3794 &view))
3795 break;
3797 if (gsym->final_value_is_known())
3799 // For a STT_GNU_IFUNC symbol we want the PLT address.
3800 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3801 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3802 else
3803 got->add_global(gsym, GOT_TYPE_STANDARD);
3805 else
3807 // If this symbol is not fully resolved, we need to add a
3808 // dynamic relocation for it.
3809 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3811 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3813 // 1) The symbol may be defined in some other module.
3815 // 2) We are building a shared library and this is a
3816 // protected symbol; using GLOB_DAT means that the dynamic
3817 // linker can use the address of the PLT in the main
3818 // executable when appropriate so that function address
3819 // comparisons work.
3821 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3822 // code, again so that function address comparisons work.
3823 if (gsym->is_from_dynobj()
3824 || gsym->is_undefined()
3825 || gsym->is_preemptible()
3826 || (gsym->visibility() == elfcpp::STV_PROTECTED
3827 && parameters->options().shared())
3828 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3829 && parameters->options().output_is_position_independent()))
3830 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3831 elfcpp::R_X86_64_GLOB_DAT);
3832 else
3834 // For a STT_GNU_IFUNC symbol we want to write the PLT
3835 // offset into the GOT, so that function pointer
3836 // comparisons work correctly.
3837 bool is_new;
3838 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3839 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3840 else
3842 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3843 // Tell the dynamic linker to use the PLT address
3844 // when resolving relocations.
3845 if (gsym->is_from_dynobj()
3846 && !parameters->options().shared())
3847 gsym->set_needs_dynsym_value();
3849 if (is_new)
3851 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3852 rela_dyn->add_global_relative(gsym,
3853 elfcpp::R_X86_64_RELATIVE,
3854 got, got_off, 0, false);
3859 break;
3861 case elfcpp::R_X86_64_PLT32:
3862 // If the symbol is fully resolved, this is just a PC32 reloc.
3863 // Otherwise we need a PLT entry.
3864 if (gsym->final_value_is_known())
3865 break;
3866 // If building a shared library, we can also skip the PLT entry
3867 // if the symbol is defined in the output file and is protected
3868 // or hidden.
3869 if (gsym->is_defined()
3870 && !gsym->is_from_dynobj()
3871 && !gsym->is_preemptible())
3872 break;
3873 target->make_plt_entry(symtab, layout, gsym);
3874 break;
3876 case elfcpp::R_X86_64_GOTPC32:
3877 case elfcpp::R_X86_64_GOTOFF64:
3878 case elfcpp::R_X86_64_GOTPC64:
3879 case elfcpp::R_X86_64_PLTOFF64:
3880 // We need a GOT section.
3881 target->got_section(symtab, layout);
3882 // For PLTOFF64, we also need a PLT entry (but only if the
3883 // symbol is not fully resolved).
3884 if (r_type == elfcpp::R_X86_64_PLTOFF64
3885 && !gsym->final_value_is_known())
3886 target->make_plt_entry(symtab, layout, gsym);
3887 break;
3889 case elfcpp::R_X86_64_COPY:
3890 case elfcpp::R_X86_64_GLOB_DAT:
3891 case elfcpp::R_X86_64_JUMP_SLOT:
3892 case elfcpp::R_X86_64_RELATIVE:
3893 case elfcpp::R_X86_64_IRELATIVE:
3894 // These are outstanding tls relocs, which are unexpected when linking
3895 case elfcpp::R_X86_64_TPOFF64:
3896 case elfcpp::R_X86_64_DTPMOD64:
3897 case elfcpp::R_X86_64_TLSDESC:
3898 gold_error(_("%s: unexpected reloc %u in object file"),
3899 object->name().c_str(), r_type);
3900 break;
3902 // These are initial tls relocs, which are expected for global()
3903 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3904 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
3906 section_size_type stype;
3907 reloc_view = object->section_contents(data_shndx, &stype, true);
3909 // Fall through.
3910 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3911 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3912 case elfcpp::R_X86_64_TLSDESC_CALL:
3913 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3914 case elfcpp::R_X86_64_DTPOFF32:
3915 case elfcpp::R_X86_64_DTPOFF64:
3916 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3917 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3919 // For the Initial-Exec model, we can treat undef symbols as final
3920 // when building an executable.
3921 const bool is_final = (gsym->final_value_is_known() ||
3922 ((r_type == elfcpp::R_X86_64_GOTTPOFF ||
3923 r_type == elfcpp::R_X86_64_CODE_4_GOTTPOFF) &&
3924 gsym->is_undefined() &&
3925 parameters->options().output_is_executable()));
3926 size_t r_offset = reloc.get_r_offset();
3927 const tls::Tls_optimization optimized_type
3928 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type,
3929 r_offset,
3930 reloc_view + r_offset);
3931 switch (r_type)
3933 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3934 if (optimized_type == tls::TLSOPT_NONE)
3936 // Create a pair of GOT entries for the module index and
3937 // dtv-relative offset.
3938 Output_data_got<64, false>* got
3939 = target->got_section(symtab, layout);
3940 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3941 target->rela_dyn_section(layout),
3942 elfcpp::R_X86_64_DTPMOD64,
3943 elfcpp::R_X86_64_DTPOFF64);
3945 else if (optimized_type == tls::TLSOPT_TO_IE)
3947 // Create a GOT entry for the tp-relative offset.
3948 Output_data_got<64, false>* got
3949 = target->got_section(symtab, layout);
3950 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3951 target->rela_dyn_section(layout),
3952 elfcpp::R_X86_64_TPOFF64);
3954 else if (optimized_type != tls::TLSOPT_TO_LE)
3955 unsupported_reloc_global(object, r_type, gsym);
3956 break;
3958 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3959 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
3960 target->define_tls_base_symbol(symtab, layout);
3961 if (optimized_type == tls::TLSOPT_NONE)
3963 // Create reserved PLT and GOT entries for the resolver.
3964 target->reserve_tlsdesc_entries(symtab, layout);
3966 // Create a double GOT entry with an R_X86_64_TLSDESC
3967 // reloc. The R_X86_64_TLSDESC reloc is resolved
3968 // lazily, so the GOT entry needs to be in an area in
3969 // .got.plt, not .got. Call got_section to make sure
3970 // the section has been created.
3971 target->got_section(symtab, layout);
3972 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3973 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3974 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3975 elfcpp::R_X86_64_TLSDESC, 0);
3977 else if (optimized_type == tls::TLSOPT_TO_IE)
3979 // Create a GOT entry for the tp-relative offset.
3980 Output_data_got<64, false>* got
3981 = target->got_section(symtab, layout);
3982 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3983 target->rela_dyn_section(layout),
3984 elfcpp::R_X86_64_TPOFF64);
3986 else if (optimized_type != tls::TLSOPT_TO_LE)
3987 unsupported_reloc_global(object, r_type, gsym);
3988 break;
3990 case elfcpp::R_X86_64_TLSDESC_CALL:
3991 break;
3993 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3994 if (optimized_type == tls::TLSOPT_NONE)
3996 // Create a GOT entry for the module index.
3997 target->got_mod_index_entry(symtab, layout, object);
3999 else if (optimized_type != tls::TLSOPT_TO_LE)
4000 unsupported_reloc_global(object, r_type, gsym);
4001 break;
4003 case elfcpp::R_X86_64_DTPOFF32:
4004 case elfcpp::R_X86_64_DTPOFF64:
4005 break;
4007 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4008 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4009 layout->set_has_static_tls();
4010 if (optimized_type == tls::TLSOPT_NONE)
4012 // Create a GOT entry for the tp-relative offset.
4013 Output_data_got<64, false>* got
4014 = target->got_section(symtab, layout);
4015 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4016 target->rela_dyn_section(layout),
4017 elfcpp::R_X86_64_TPOFF64);
4019 else if (optimized_type != tls::TLSOPT_TO_LE)
4020 unsupported_reloc_global(object, r_type, gsym);
4021 break;
4023 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4024 layout->set_has_static_tls();
4025 if (parameters->options().shared())
4026 unsupported_reloc_global(object, r_type, gsym);
4027 break;
4029 default:
4030 gold_unreachable();
4033 break;
4035 case elfcpp::R_X86_64_SIZE32:
4036 case elfcpp::R_X86_64_SIZE64:
4037 default:
4038 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4039 object->name().c_str(), r_type,
4040 gsym->demangled_name().c_str());
4041 break;
4045 template<int size>
4046 void
4047 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
4048 Layout* layout,
4049 Sized_relobj_file<size, false>* object,
4050 unsigned int data_shndx,
4051 unsigned int sh_type,
4052 const unsigned char* prelocs,
4053 size_t reloc_count,
4054 Output_section* output_section,
4055 bool needs_special_offset_handling,
4056 size_t local_symbol_count,
4057 const unsigned char* plocal_symbols)
4059 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4060 Classify_reloc;
4062 if (sh_type == elfcpp::SHT_REL)
4064 return;
4067 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
4068 Classify_reloc>(
4069 symtab,
4070 layout,
4071 this,
4072 object,
4073 data_shndx,
4074 prelocs,
4075 reloc_count,
4076 output_section,
4077 needs_special_offset_handling,
4078 local_symbol_count,
4079 plocal_symbols);
4082 // Scan relocations for a section.
4084 template<int size>
4085 void
4086 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
4087 Layout* layout,
4088 Sized_relobj_file<size, false>* object,
4089 unsigned int data_shndx,
4090 unsigned int sh_type,
4091 const unsigned char* prelocs,
4092 size_t reloc_count,
4093 Output_section* output_section,
4094 bool needs_special_offset_handling,
4095 size_t local_symbol_count,
4096 const unsigned char* plocal_symbols)
4098 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4099 Classify_reloc;
4101 if (sh_type == elfcpp::SHT_REL)
4103 gold_error(_("%s: unsupported REL reloc section"),
4104 object->name().c_str());
4105 return;
4108 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
4109 symtab,
4110 layout,
4111 this,
4112 object,
4113 data_shndx,
4114 prelocs,
4115 reloc_count,
4116 output_section,
4117 needs_special_offset_handling,
4118 local_symbol_count,
4119 plocal_symbols);
4122 // Finalize the sections.
4124 template<int size>
4125 void
4126 Target_x86_64<size>::do_finalize_sections(
4127 Layout* layout,
4128 const Input_objects*,
4129 Symbol_table* symtab)
4131 const Reloc_section* rel_plt = (this->plt_ == NULL
4132 ? NULL
4133 : this->plt_->rela_plt());
4134 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4135 this->rela_dyn_, true, false, false);
4137 // Fill in some more dynamic tags.
4138 Output_data_dynamic* const odyn = layout->dynamic_data();
4139 if (odyn != NULL)
4141 if (this->plt_ != NULL
4142 && this->plt_->output_section() != NULL
4143 && this->plt_->has_tlsdesc_entry())
4145 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
4146 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
4147 this->got_->finalize_data_size();
4148 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
4149 this->plt_, plt_offset);
4150 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
4151 this->got_, got_offset);
4155 // Emit any relocs we saved in an attempt to avoid generating COPY
4156 // relocs.
4157 if (this->copy_relocs_.any_saved_relocs())
4158 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4160 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4161 // the .got.plt section.
4162 Symbol* sym = this->global_offset_table_;
4163 if (sym != NULL)
4165 uint64_t data_size = this->got_plt_->current_data_size();
4166 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4169 if (parameters->doing_static_link()
4170 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4172 // If linking statically, make sure that the __rela_iplt symbols
4173 // were defined if necessary, even if we didn't create a PLT.
4174 static const Define_symbol_in_segment syms[] =
4177 "__rela_iplt_start", // name
4178 elfcpp::PT_LOAD, // segment_type
4179 elfcpp::PF_W, // segment_flags_set
4180 elfcpp::PF(0), // segment_flags_clear
4181 0, // value
4182 0, // size
4183 elfcpp::STT_NOTYPE, // type
4184 elfcpp::STB_GLOBAL, // binding
4185 elfcpp::STV_HIDDEN, // visibility
4186 0, // nonvis
4187 Symbol::SEGMENT_START, // offset_from_base
4188 true // only_if_ref
4191 "__rela_iplt_end", // name
4192 elfcpp::PT_LOAD, // segment_type
4193 elfcpp::PF_W, // segment_flags_set
4194 elfcpp::PF(0), // segment_flags_clear
4195 0, // value
4196 0, // size
4197 elfcpp::STT_NOTYPE, // type
4198 elfcpp::STB_GLOBAL, // binding
4199 elfcpp::STV_HIDDEN, // visibility
4200 0, // nonvis
4201 Symbol::SEGMENT_START, // offset_from_base
4202 true // only_if_ref
4206 symtab->define_symbols(layout, 2, syms,
4207 layout->script_options()->saw_sections_clause());
4211 // For x32, we need to handle PC-relative relocations using full 64-bit
4212 // arithmetic, so that we can detect relocation overflows properly.
4213 // This class overrides the pcrela32_check methods from the defaults in
4214 // Relocate_functions in reloc.h.
4216 template<int size>
4217 class X86_64_relocate_functions : public Relocate_functions<size, false>
4219 public:
4220 typedef Relocate_functions<size, false> Base;
4222 // Do a simple PC relative relocation with the addend in the
4223 // relocation.
4224 static inline typename Base::Reloc_status
4225 pcrela32_check(unsigned char* view,
4226 typename elfcpp::Elf_types<64>::Elf_Addr value,
4227 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4228 typename elfcpp::Elf_types<64>::Elf_Addr address)
4230 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4231 Valtype* wv = reinterpret_cast<Valtype*>(view);
4232 value = value + addend - address;
4233 elfcpp::Swap<32, false>::writeval(wv, value);
4234 return (Bits<32>::has_overflow(value)
4235 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4238 // Do a simple PC relative relocation with a Symbol_value with the
4239 // addend in the relocation.
4240 static inline typename Base::Reloc_status
4241 pcrela32_check(unsigned char* view,
4242 const Sized_relobj_file<size, false>* object,
4243 const Symbol_value<size>* psymval,
4244 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4245 typename elfcpp::Elf_types<64>::Elf_Addr address)
4247 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4248 Valtype* wv = reinterpret_cast<Valtype*>(view);
4249 typename elfcpp::Elf_types<64>::Elf_Addr value;
4250 if (addend >= 0)
4251 value = psymval->value(object, addend);
4252 else
4254 // For negative addends, get the symbol value without
4255 // the addend, then add the addend using 64-bit arithmetic.
4256 value = psymval->value(object, 0);
4257 value += addend;
4259 value -= address;
4260 elfcpp::Swap<32, false>::writeval(wv, value);
4261 return (Bits<32>::has_overflow(value)
4262 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4266 // Perform a relocation.
4268 template<int size>
4269 inline bool
4270 Target_x86_64<size>::Relocate::relocate(
4271 const Relocate_info<size, false>* relinfo,
4272 unsigned int,
4273 Target_x86_64<size>* target,
4274 Output_section*,
4275 size_t relnum,
4276 const unsigned char* preloc,
4277 const Sized_symbol<size>* gsym,
4278 const Symbol_value<size>* psymval,
4279 unsigned char* view,
4280 typename elfcpp::Elf_types<size>::Elf_Addr address,
4281 section_size_type view_size)
4283 typedef X86_64_relocate_functions<size> Reloc_funcs;
4284 const elfcpp::Rela<size, false> rela(preloc);
4285 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4287 if (this->skip_call_tls_get_addr_)
4289 if ((r_type != elfcpp::R_X86_64_PLT32
4290 && r_type != elfcpp::R_X86_64_GOTPCREL
4291 && r_type != elfcpp::R_X86_64_GOTPCRELX
4292 && r_type != elfcpp::R_X86_64_PC32)
4293 || gsym == NULL
4294 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4296 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4297 _("missing expected TLS relocation"));
4298 this->skip_call_tls_get_addr_ = false;
4300 else
4302 this->skip_call_tls_get_addr_ = false;
4303 return false;
4307 if (view == NULL)
4308 return true;
4310 const Sized_relobj_file<size, false>* object = relinfo->object;
4312 // Pick the value to use for symbols defined in the PLT.
4313 Symbol_value<size> symval;
4314 if (gsym != NULL
4315 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4317 symval.set_output_value(target->plt_address_for_global(gsym));
4318 psymval = &symval;
4320 else if (gsym == NULL && psymval->is_ifunc_symbol())
4322 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4323 if (object->local_has_plt_offset(r_sym))
4325 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4326 psymval = &symval;
4330 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4332 // Get the GOT offset if needed.
4333 // The GOT pointer points to the end of the GOT section.
4334 // We need to subtract the size of the GOT section to get
4335 // the actual offset to use in the relocation.
4336 bool have_got_offset = false;
4337 // Since the actual offset is always negative, we use signed int to
4338 // support 64-bit GOT relocations.
4339 int got_offset = 0;
4340 switch (r_type)
4342 case elfcpp::R_X86_64_GOT32:
4343 case elfcpp::R_X86_64_GOT64:
4344 case elfcpp::R_X86_64_GOTPLT64:
4345 case elfcpp::R_X86_64_GOTPCREL64:
4346 if (gsym != NULL)
4348 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4349 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4351 else
4353 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4354 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4355 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4356 - target->got_size());
4358 have_got_offset = true;
4359 break;
4361 default:
4362 break;
4365 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4367 switch (r_type)
4369 case elfcpp::R_X86_64_NONE:
4370 case elfcpp::R_X86_64_GNU_VTINHERIT:
4371 case elfcpp::R_X86_64_GNU_VTENTRY:
4372 break;
4374 case elfcpp::R_X86_64_64:
4375 Reloc_funcs::rela64(view, object, psymval, addend);
4376 break;
4378 case elfcpp::R_X86_64_PC64:
4379 Reloc_funcs::pcrela64(view, object, psymval, addend,
4380 address);
4381 break;
4383 case elfcpp::R_X86_64_32:
4384 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4385 Reloc_funcs::CHECK_UNSIGNED);
4386 break;
4388 case elfcpp::R_X86_64_32S:
4389 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4390 Reloc_funcs::CHECK_SIGNED);
4391 break;
4393 case elfcpp::R_X86_64_PC32:
4394 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4395 address);
4396 break;
4398 case elfcpp::R_X86_64_16:
4399 Reloc_funcs::rela16(view, object, psymval, addend);
4400 break;
4402 case elfcpp::R_X86_64_PC16:
4403 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4404 break;
4406 case elfcpp::R_X86_64_8:
4407 Reloc_funcs::rela8(view, object, psymval, addend);
4408 break;
4410 case elfcpp::R_X86_64_PC8:
4411 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4412 break;
4414 case elfcpp::R_X86_64_PLT32:
4415 gold_assert(gsym == NULL
4416 || gsym->has_plt_offset()
4417 || gsym->final_value_is_known()
4418 || (gsym->is_defined()
4419 && !gsym->is_from_dynobj()
4420 && !gsym->is_preemptible()));
4421 // Note: while this code looks the same as for R_X86_64_PC32, it
4422 // behaves differently because psymval was set to point to
4423 // the PLT entry, rather than the symbol, in Scan::global().
4424 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4425 address);
4426 break;
4428 case elfcpp::R_X86_64_PLTOFF64:
4430 gold_assert(gsym);
4431 gold_assert(gsym->has_plt_offset()
4432 || gsym->final_value_is_known());
4433 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4434 // This is the address of GLOBAL_OFFSET_TABLE.
4435 got_address = target->got_plt_section()->address();
4436 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4438 break;
4440 case elfcpp::R_X86_64_GOT32:
4441 gold_assert(have_got_offset);
4442 Reloc_funcs::rela32(view, got_offset, addend);
4443 break;
4445 case elfcpp::R_X86_64_GOTPC32:
4447 gold_assert(gsym);
4448 typename elfcpp::Elf_types<size>::Elf_Addr value;
4449 value = target->got_plt_section()->address();
4450 Reloc_funcs::pcrela32_check(view, value, addend, address);
4452 break;
4454 case elfcpp::R_X86_64_GOT64:
4455 case elfcpp::R_X86_64_GOTPLT64:
4456 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4457 // GOT64.
4458 gold_assert(have_got_offset);
4459 Reloc_funcs::rela64(view, got_offset, addend);
4460 break;
4462 case elfcpp::R_X86_64_GOTPC64:
4464 gold_assert(gsym);
4465 typename elfcpp::Elf_types<size>::Elf_Addr value;
4466 value = target->got_plt_section()->address();
4467 Reloc_funcs::pcrela64(view, value, addend, address);
4469 break;
4471 case elfcpp::R_X86_64_GOTOFF64:
4473 typename elfcpp::Elf_types<size>::Elf_Addr reladdr;
4474 reladdr = target->got_plt_section()->address();
4475 Reloc_funcs::pcrela64(view, object, psymval, addend, reladdr);
4477 break;
4479 case elfcpp::R_X86_64_GOTPCREL:
4480 case elfcpp::R_X86_64_GOTPCRELX:
4481 case elfcpp::R_X86_64_REX_GOTPCRELX:
4482 case elfcpp::R_X86_64_CODE_4_GOTPCRELX:
4484 bool converted_p = false;
4486 if (rela.get_r_addend() == -4)
4488 // Convert
4489 // mov foo@GOTPCREL(%rip), %reg
4490 // to lea foo(%rip), %reg.
4491 // if possible.
4492 if (!parameters->incremental()
4493 && ((gsym == NULL
4494 && rela.get_r_offset() >= 2
4495 && view[-2] == 0x8b
4496 && !psymval->is_ifunc_symbol())
4497 || (gsym != NULL
4498 && rela.get_r_offset() >= 2
4499 && Target_x86_64<size>::can_convert_mov_to_lea(gsym,
4500 r_type,
4502 &view))))
4504 view[-2] = 0x8d;
4505 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4506 converted_p = true;
4508 // Convert
4509 // callq *foo@GOTPCRELX(%rip) to
4510 // addr32 callq foo
4511 // and jmpq *foo@GOTPCRELX(%rip) to
4512 // jmpq foo
4513 // nop
4514 else if (!parameters->incremental()
4515 && gsym != NULL
4516 && rela.get_r_offset() >= 2
4517 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4518 r_type,
4520 &view))
4522 if (view[-1] == 0x15)
4524 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4525 // Opcode of addr32 is 0x67 and opcode of direct callq
4526 // is 0xe8.
4527 view[-2] = 0x67;
4528 view[-1] = 0xe8;
4529 // Convert GOTPCRELX to 32-bit pc relative reloc.
4530 Reloc_funcs::pcrela32(view, object, psymval, addend,
4531 address);
4532 converted_p = true;
4534 else
4536 // Convert jmpq *foo@GOTPCRELX(%rip) to
4537 // jmpq foo
4538 // nop
4539 // The opcode of direct jmpq is 0xe9.
4540 view[-2] = 0xe9;
4541 // The opcode of nop is 0x90.
4542 view[3] = 0x90;
4543 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq
4544 // is rip relative and since the instruction following
4545 // the jmpq is now the nop, offset the address by 1
4546 // byte. The start of the relocation also moves ahead
4547 // by 1 byte.
4548 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4549 address - 1);
4550 converted_p = true;
4555 if (!converted_p)
4557 if (gsym != NULL)
4559 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4560 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4561 - target->got_size());
4563 else
4565 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4566 gold_assert(object->local_has_got_offset(r_sym,
4567 GOT_TYPE_STANDARD));
4568 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4569 - target->got_size());
4571 typename elfcpp::Elf_types<size>::Elf_Addr value;
4572 value = target->got_plt_section()->address() + got_offset;
4573 Reloc_funcs::pcrela32_check(view, value, addend, address);
4576 break;
4578 case elfcpp::R_X86_64_GOTPCREL64:
4580 gold_assert(have_got_offset);
4581 typename elfcpp::Elf_types<size>::Elf_Addr value;
4582 value = target->got_plt_section()->address() + got_offset;
4583 Reloc_funcs::pcrela64(view, value, addend, address);
4585 break;
4587 case elfcpp::R_X86_64_COPY:
4588 case elfcpp::R_X86_64_GLOB_DAT:
4589 case elfcpp::R_X86_64_JUMP_SLOT:
4590 case elfcpp::R_X86_64_RELATIVE:
4591 case elfcpp::R_X86_64_IRELATIVE:
4592 // These are outstanding tls relocs, which are unexpected when linking
4593 case elfcpp::R_X86_64_TPOFF64:
4594 case elfcpp::R_X86_64_DTPMOD64:
4595 case elfcpp::R_X86_64_TLSDESC:
4596 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4597 _("unexpected reloc %u in object file"),
4598 r_type);
4599 break;
4601 // These are initial tls relocs, which are expected when linking
4602 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4603 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4604 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
4605 case elfcpp::R_X86_64_TLSDESC_CALL:
4606 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4607 case elfcpp::R_X86_64_DTPOFF32:
4608 case elfcpp::R_X86_64_DTPOFF64:
4609 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4610 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4611 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4612 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4613 view, address, view_size);
4614 break;
4616 case elfcpp::R_X86_64_SIZE32:
4617 case elfcpp::R_X86_64_SIZE64:
4618 default:
4619 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4620 _("unsupported reloc %u"),
4621 r_type);
4622 break;
4625 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4627 if (gsym == NULL)
4629 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4630 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4631 _("relocation overflow: "
4632 "reference to local symbol %u in %s"),
4633 r_sym, object->name().c_str());
4635 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4637 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4638 _("relocation overflow: "
4639 "reference to '%s' defined in %s"),
4640 gsym->name(),
4641 gsym->object()->name().c_str());
4643 else
4645 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4646 _("relocation overflow: reference to '%s'"),
4647 gsym->name());
4651 return true;
4654 // Perform a TLS relocation.
4656 template<int size>
4657 inline void
4658 Target_x86_64<size>::Relocate::relocate_tls(
4659 const Relocate_info<size, false>* relinfo,
4660 Target_x86_64<size>* target,
4661 size_t relnum,
4662 const elfcpp::Rela<size, false>& rela,
4663 unsigned int r_type,
4664 const Sized_symbol<size>* gsym,
4665 const Symbol_value<size>* psymval,
4666 unsigned char* view,
4667 typename elfcpp::Elf_types<size>::Elf_Addr address,
4668 section_size_type view_size)
4670 Output_segment* tls_segment = relinfo->layout->tls_segment();
4672 const Sized_relobj_file<size, false>* object = relinfo->object;
4673 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4674 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4675 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4677 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4679 const bool is_final = (gsym == NULL
4680 ? !parameters->options().shared()
4681 : gsym->final_value_is_known());
4682 size_t r_offset = rela.get_r_offset();
4683 tls::Tls_optimization optimized_type
4684 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type,
4685 r_offset, view);
4686 switch (r_type)
4688 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4689 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4691 // If this code sequence is used in a non-executable section,
4692 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4693 // on the assumption that it's being used by itself in a debug
4694 // section. Therefore, in the unlikely event that the code
4695 // sequence appears in a non-executable section, we simply
4696 // leave it unoptimized.
4697 optimized_type = tls::TLSOPT_NONE;
4699 if (optimized_type == tls::TLSOPT_TO_LE)
4701 if (tls_segment == NULL)
4703 gold_assert(parameters->errors()->error_count() > 0
4704 || issue_undefined_symbol_error(gsym));
4705 return;
4707 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4708 rela, r_type, value, view,
4709 view_size);
4710 break;
4712 else
4714 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4715 ? GOT_TYPE_TLS_OFFSET
4716 : GOT_TYPE_TLS_PAIR);
4717 unsigned int got_offset;
4718 if (gsym != NULL)
4720 gold_assert(gsym->has_got_offset(got_type));
4721 got_offset = gsym->got_offset(got_type) - target->got_size();
4723 else
4725 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4726 gold_assert(object->local_has_got_offset(r_sym, got_type));
4727 got_offset = (object->local_got_offset(r_sym, got_type)
4728 - target->got_size());
4730 if (optimized_type == tls::TLSOPT_TO_IE)
4732 value = target->got_plt_section()->address() + got_offset;
4733 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4734 value, view, address, view_size);
4735 break;
4737 else if (optimized_type == tls::TLSOPT_NONE)
4739 // Relocate the field with the offset of the pair of GOT
4740 // entries.
4741 value = target->got_plt_section()->address() + got_offset;
4742 Relocate_functions<size, false>::pcrela32(view, value, addend,
4743 address);
4744 break;
4747 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4748 _("unsupported reloc %u"), r_type);
4749 break;
4751 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4752 case elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC:
4753 case elfcpp::R_X86_64_TLSDESC_CALL:
4754 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4756 // See above comment for R_X86_64_TLSGD.
4757 optimized_type = tls::TLSOPT_NONE;
4759 if (optimized_type == tls::TLSOPT_TO_LE)
4761 if (tls_segment == NULL)
4763 gold_assert(parameters->errors()->error_count() > 0
4764 || issue_undefined_symbol_error(gsym));
4765 return;
4767 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4768 rela, r_type, value, view,
4769 view_size);
4770 break;
4772 else
4774 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4775 ? GOT_TYPE_TLS_OFFSET
4776 : GOT_TYPE_TLS_DESC);
4777 unsigned int got_offset = 0;
4778 if ((r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4779 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
4780 && optimized_type == tls::TLSOPT_NONE)
4782 // We created GOT entries in the .got.tlsdesc portion of
4783 // the .got.plt section, but the offset stored in the
4784 // symbol is the offset within .got.tlsdesc.
4785 got_offset = (target->got_size()
4786 + target->got_plt_section()->data_size());
4788 if (gsym != NULL)
4790 gold_assert(gsym->has_got_offset(got_type));
4791 got_offset += gsym->got_offset(got_type) - target->got_size();
4793 else
4795 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4796 gold_assert(object->local_has_got_offset(r_sym, got_type));
4797 got_offset += (object->local_got_offset(r_sym, got_type)
4798 - target->got_size());
4800 if (optimized_type == tls::TLSOPT_TO_IE)
4802 value = target->got_plt_section()->address() + got_offset;
4803 this->tls_desc_gd_to_ie(relinfo, relnum,
4804 rela, r_type, value, view, address,
4805 view_size);
4806 break;
4808 else if (optimized_type == tls::TLSOPT_NONE)
4810 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4811 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
4813 // Relocate the field with the offset of the pair of GOT
4814 // entries.
4815 value = target->got_plt_section()->address() + got_offset;
4816 Relocate_functions<size, false>::pcrela32(view, value, addend,
4817 address);
4819 break;
4822 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4823 _("unsupported reloc %u"), r_type);
4824 break;
4826 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4827 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4829 // See above comment for R_X86_64_TLSGD.
4830 optimized_type = tls::TLSOPT_NONE;
4832 if (optimized_type == tls::TLSOPT_TO_LE)
4834 if (tls_segment == NULL)
4836 gold_assert(parameters->errors()->error_count() > 0
4837 || issue_undefined_symbol_error(gsym));
4838 return;
4840 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4841 value, view, view_size);
4842 break;
4844 else if (optimized_type == tls::TLSOPT_NONE)
4846 // Relocate the field with the offset of the GOT entry for
4847 // the module index.
4848 unsigned int got_offset;
4849 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4850 - target->got_size());
4851 value = target->got_plt_section()->address() + got_offset;
4852 Relocate_functions<size, false>::pcrela32(view, value, addend,
4853 address);
4854 break;
4856 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4857 _("unsupported reloc %u"), r_type);
4858 break;
4860 case elfcpp::R_X86_64_DTPOFF32:
4861 // This relocation type is used in debugging information.
4862 // In that case we need to not optimize the value. If the
4863 // section is not executable, then we assume we should not
4864 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4865 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4866 // R_X86_64_TLSLD.
4867 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4869 if (tls_segment == NULL)
4871 gold_assert(parameters->errors()->error_count() > 0
4872 || issue_undefined_symbol_error(gsym));
4873 return;
4875 value -= tls_segment->memsz();
4877 Relocate_functions<size, false>::rela32(view, value, addend);
4878 break;
4880 case elfcpp::R_X86_64_DTPOFF64:
4881 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4882 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4884 if (tls_segment == NULL)
4886 gold_assert(parameters->errors()->error_count() > 0
4887 || issue_undefined_symbol_error(gsym));
4888 return;
4890 value -= tls_segment->memsz();
4892 Relocate_functions<size, false>::rela64(view, value, addend);
4893 break;
4895 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4896 case elfcpp::R_X86_64_CODE_4_GOTTPOFF:
4897 if (gsym != NULL
4898 && gsym->is_undefined()
4899 && parameters->options().output_is_executable())
4901 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4902 NULL, rela,
4903 r_type, value, view,
4904 view_size);
4905 break;
4907 else if (optimized_type == tls::TLSOPT_TO_LE)
4909 if (tls_segment == NULL)
4911 gold_assert(parameters->errors()->error_count() > 0
4912 || issue_undefined_symbol_error(gsym));
4913 return;
4915 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4916 tls_segment, rela,
4917 r_type, value, view,
4918 view_size);
4919 break;
4921 else if (optimized_type == tls::TLSOPT_NONE)
4923 // Relocate the field with the offset of the GOT entry for
4924 // the tp-relative offset of the symbol.
4925 unsigned int got_offset;
4926 if (gsym != NULL)
4928 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4929 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4930 - target->got_size());
4932 else
4934 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4935 gold_assert(object->local_has_got_offset(r_sym,
4936 GOT_TYPE_TLS_OFFSET));
4937 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4938 - target->got_size());
4940 value = target->got_plt_section()->address() + got_offset;
4941 Relocate_functions<size, false>::pcrela32(view, value, addend,
4942 address);
4943 break;
4945 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4946 _("unsupported reloc type %u"),
4947 r_type);
4948 break;
4950 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4951 if (tls_segment == NULL)
4953 gold_assert(parameters->errors()->error_count() > 0
4954 || issue_undefined_symbol_error(gsym));
4955 return;
4957 value -= tls_segment->memsz();
4958 Relocate_functions<size, false>::rela32(view, value, addend);
4959 break;
4963 // Do a relocation in which we convert a TLS General-Dynamic to an
4964 // Initial-Exec.
4966 template<int size>
4967 inline void
4968 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4969 const Relocate_info<size, false>* relinfo,
4970 size_t relnum,
4971 const elfcpp::Rela<size, false>& rela,
4972 unsigned int,
4973 typename elfcpp::Elf_types<size>::Elf_Addr value,
4974 unsigned char* view,
4975 typename elfcpp::Elf_types<size>::Elf_Addr address,
4976 section_size_type view_size)
4978 // For SIZE == 64:
4979 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4980 // .word 0x6666; rex64; call __tls_get_addr@PLT
4981 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4982 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4983 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4984 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4985 // For SIZE == 32:
4986 // leaq foo@tlsgd(%rip),%rdi;
4987 // .word 0x6666; rex64; call __tls_get_addr@PLT
4988 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4989 // leaq foo@tlsgd(%rip),%rdi;
4990 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4991 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4993 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4994 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4995 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4996 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4998 if (size == 64)
5000 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5001 -4);
5002 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5003 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5004 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5005 16);
5007 else
5009 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5010 -3);
5011 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5012 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5013 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5014 15);
5017 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5018 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
5019 address);
5021 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5022 // We can skip it.
5023 this->skip_call_tls_get_addr_ = true;
5026 // Do a relocation in which we convert a TLS General-Dynamic to a
5027 // Local-Exec.
5029 template<int size>
5030 inline void
5031 Target_x86_64<size>::Relocate::tls_gd_to_le(
5032 const Relocate_info<size, false>* relinfo,
5033 size_t relnum,
5034 Output_segment* tls_segment,
5035 const elfcpp::Rela<size, false>& rela,
5036 unsigned int,
5037 typename elfcpp::Elf_types<size>::Elf_Addr value,
5038 unsigned char* view,
5039 section_size_type view_size)
5041 // For SIZE == 64:
5042 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5043 // .word 0x6666; rex64; call __tls_get_addr@PLT
5044 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5045 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5046 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5047 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5048 // For SIZE == 32:
5049 // leaq foo@tlsgd(%rip),%rdi;
5050 // .word 0x6666; rex64; call __tls_get_addr@PLT
5051 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5052 // leaq foo@tlsgd(%rip),%rdi;
5053 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5054 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5056 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5057 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5058 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5059 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5061 if (size == 64)
5063 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5064 -4);
5065 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5066 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5067 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5068 16);
5070 else
5072 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5073 -3);
5074 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5075 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5077 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5078 15);
5081 value -= tls_segment->memsz();
5082 Relocate_functions<size, false>::rela32(view + 8, value, 0);
5084 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5085 // We can skip it.
5086 this->skip_call_tls_get_addr_ = true;
5089 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5091 template<int size>
5092 inline void
5093 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
5094 const Relocate_info<size, false>* relinfo,
5095 size_t relnum,
5096 const elfcpp::Rela<size, false>& rela,
5097 unsigned int r_type,
5098 typename elfcpp::Elf_types<size>::Elf_Addr value,
5099 unsigned char* view,
5100 typename elfcpp::Elf_types<size>::Elf_Addr address,
5101 section_size_type view_size)
5103 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
5104 || r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
5106 // LP64: leaq foo@tlsdesc(%rip), %rax
5107 // ==> movq foo@gottpoff(%rip), %rax
5108 // X32: rex leal foo@tlsdesc(%rip), %eax
5109 // ==> rex movl foo@gottpoff(%rip), %eax
5110 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5111 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5112 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5113 ((r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC
5114 || (view[-3] & 0xfb) == 0x48
5115 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5116 && view[-2] == 0x8d
5117 && (view[-1] & 0xc7) == 0x05));
5118 view[-2] = 0x8b;
5119 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5120 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
5122 else
5124 // LP64: call *foo@tlscall(%rax)
5125 // ==> xchg %ax, %ax
5126 // X32: call *foo@tlscall(%eax)
5127 // ==> nopl (%rax)
5128 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5129 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5130 int prefix = 0;
5131 if (size == 32 && view[0] == 0x67)
5133 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5134 view_size, 3);
5135 prefix = 1;
5137 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5138 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5139 if (prefix)
5141 view[0] = 0x0f;
5142 view[1] = 0x1f;
5143 view[2] = 0x00;
5145 else
5147 view[0] = 0x66;
5148 view[1] = 0x90;
5153 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5155 template<int size>
5156 inline void
5157 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
5158 const Relocate_info<size, false>* relinfo,
5159 size_t relnum,
5160 Output_segment* tls_segment,
5161 const elfcpp::Rela<size, false>& rela,
5162 unsigned int r_type,
5163 typename elfcpp::Elf_types<size>::Elf_Addr value,
5164 unsigned char* view,
5165 section_size_type view_size)
5167 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5169 // LP64: leaq foo@tlsdesc(%rip), %rax
5170 // ==> movq foo@tpoff, %rax
5171 // X32: rex leal foo@tlsdesc(%rip), %eax
5172 // ==> rex movl foo@tpoff, %eax
5173 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5174 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5175 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5176 (((view[-3] & 0xfb) == 0x48
5177 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5178 && view[-2] == 0x8d
5179 && (view[-1] & 0xc7) == 0x05));
5180 view[-3] = (view[-3] & 0x48) | ((view[-3] >> 2) & 1);
5181 view[-2] = 0xc7;
5182 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5183 value -= tls_segment->memsz();
5184 Relocate_functions<size, false>::rela32(view, value, 0);
5186 else if (r_type == elfcpp::R_X86_64_CODE_4_GOTPC32_TLSDESC)
5188 // REX2: lea foo@tlsdesc(%rip), %reg
5189 // ==> mov foo@tpoff, %reg
5190 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5191 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5192 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5193 (view[-2] == 0x8d
5194 && (view[-1] & 0xc7) == 0x05));
5195 unsigned char rex2_mask = 4 | 4 << 4;
5196 view[-3] = (view[-3] & ~rex2_mask) | ((view[-3] & rex2_mask) >> 2);
5197 view[-2] = 0xc7;
5198 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5199 value -= tls_segment->memsz();
5200 Relocate_functions<size, false>::rela32(view, value, 0);
5202 else
5204 // LP64: call *foo@tlscall(%rax)
5205 // ==> xchg %ax, %ax
5206 // X32: call *foo@tlscall(%eax)
5207 // ==> nopl (%rax)
5208 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5209 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5210 int prefix = 0;
5211 if (size == 32 && view[0] == 0x67)
5213 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5214 view_size, 3);
5215 prefix = 1;
5217 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5218 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5219 if (prefix)
5221 view[0] = 0x0f;
5222 view[1] = 0x1f;
5223 view[2] = 0x00;
5225 else
5227 view[0] = 0x66;
5228 view[1] = 0x90;
5233 template<int size>
5234 inline void
5235 Target_x86_64<size>::Relocate::tls_ld_to_le(
5236 const Relocate_info<size, false>* relinfo,
5237 size_t relnum,
5238 Output_segment*,
5239 const elfcpp::Rela<size, false>& rela,
5240 unsigned int,
5241 typename elfcpp::Elf_types<size>::Elf_Addr,
5242 unsigned char* view,
5243 section_size_type view_size)
5245 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5246 // For SIZE == 64:
5247 // ... leq foo@dtpoff(%rax),%reg
5248 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5249 // For SIZE == 32:
5250 // ... leq foo@dtpoff(%rax),%reg
5251 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5252 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5253 // For SIZE == 64:
5254 // ... leq foo@dtpoff(%rax),%reg
5255 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5256 // For SIZE == 32:
5257 // ... leq foo@dtpoff(%rax),%reg
5258 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5260 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5261 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
5263 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5264 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
5266 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5267 view[4] == 0xe8 || view[4] == 0xff);
5269 if (view[4] == 0xe8)
5271 if (size == 64)
5272 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5273 else
5274 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5276 else
5278 if (size == 64)
5279 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5280 13);
5281 else
5282 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5283 13);
5286 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5287 // We can skip it.
5288 this->skip_call_tls_get_addr_ = true;
5291 // Do a relocation in which we convert a TLS Initial-Exec to a
5292 // Local-Exec.
5294 template<int size>
5295 inline void
5296 Target_x86_64<size>::Relocate::tls_ie_to_le(
5297 const Relocate_info<size, false>* relinfo,
5298 size_t relnum,
5299 Output_segment* tls_segment,
5300 const elfcpp::Rela<size, false>& rela,
5301 unsigned int r_type,
5302 typename elfcpp::Elf_types<size>::Elf_Addr value,
5303 unsigned char* view,
5304 section_size_type view_size)
5306 // We need to examine the opcodes to figure out which instruction we
5307 // are looking at.
5309 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5310 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5312 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5313 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5315 unsigned char op1 = view[-3];
5316 unsigned char op2 = view[-2];
5317 unsigned char op3 = view[-1];
5318 unsigned char reg = op3 >> 3;
5320 if (r_type == elfcpp::R_X86_64_GOTTPOFF)
5322 if (op2 == 0x8b)
5324 // movq
5325 if (op1 == 0x4c)
5326 view[-3] = 0x49;
5327 else if (size == 32 && op1 == 0x44)
5328 view[-3] = 0x41;
5329 view[-2] = 0xc7;
5330 view[-1] = 0xc0 | reg;
5332 else if (reg == 4)
5334 // Special handling for %rsp.
5335 if (op1 == 0x4c)
5336 view[-3] = 0x49;
5337 else if (size == 32 && op1 == 0x44)
5338 view[-3] = 0x41;
5339 view[-2] = 0x81;
5340 view[-1] = 0xc0 | reg;
5342 else
5344 // addq
5345 if (op1 == 0x4c)
5346 view[-3] = 0x4d;
5347 else if (size == 32 && op1 == 0x44)
5348 view[-3] = 0x45;
5349 view[-2] = 0x8d;
5350 view[-1] = 0x80 | reg | (reg << 3);
5353 else
5355 if (op2 == 0x8b)
5356 op2 = 0xc7;
5357 else
5358 op2 = 0x81;
5360 unsigned char rex2_mask = 4 | 4 << 4;
5361 view[-3] = (view[-3] & ~rex2_mask) | ((view[-3] & rex2_mask) >> 2);
5362 view[-2] = op2;
5363 view[-1] = 0xc0 | reg;
5366 if (tls_segment != NULL)
5367 value -= tls_segment->memsz();
5368 Relocate_functions<size, false>::rela32(view, value, 0);
5371 // Relocate section data.
5373 template<int size>
5374 void
5375 Target_x86_64<size>::relocate_section(
5376 const Relocate_info<size, false>* relinfo,
5377 unsigned int sh_type,
5378 const unsigned char* prelocs,
5379 size_t reloc_count,
5380 Output_section* output_section,
5381 bool needs_special_offset_handling,
5382 unsigned char* view,
5383 typename elfcpp::Elf_types<size>::Elf_Addr address,
5384 section_size_type view_size,
5385 const Reloc_symbol_changes* reloc_symbol_changes)
5387 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5388 Classify_reloc;
5390 gold_assert(sh_type == elfcpp::SHT_RELA);
5392 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5393 gold::Default_comdat_behavior, Classify_reloc>(
5394 relinfo,
5395 this,
5396 prelocs,
5397 reloc_count,
5398 output_section,
5399 needs_special_offset_handling,
5400 view,
5401 address,
5402 view_size,
5403 reloc_symbol_changes);
5406 // Apply an incremental relocation. Incremental relocations always refer
5407 // to global symbols.
5409 template<int size>
5410 void
5411 Target_x86_64<size>::apply_relocation(
5412 const Relocate_info<size, false>* relinfo,
5413 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5414 unsigned int r_type,
5415 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5416 const Symbol* gsym,
5417 unsigned char* view,
5418 typename elfcpp::Elf_types<size>::Elf_Addr address,
5419 section_size_type view_size)
5421 gold::apply_relocation<size, false, Target_x86_64<size>,
5422 typename Target_x86_64<size>::Relocate>(
5423 relinfo,
5424 this,
5425 r_offset,
5426 r_type,
5427 r_addend,
5428 gsym,
5429 view,
5430 address,
5431 view_size);
5434 // Scan the relocs during a relocatable link.
5436 template<int size>
5437 void
5438 Target_x86_64<size>::scan_relocatable_relocs(
5439 Symbol_table* symtab,
5440 Layout* layout,
5441 Sized_relobj_file<size, false>* object,
5442 unsigned int data_shndx,
5443 unsigned int sh_type,
5444 const unsigned char* prelocs,
5445 size_t reloc_count,
5446 Output_section* output_section,
5447 bool needs_special_offset_handling,
5448 size_t local_symbol_count,
5449 const unsigned char* plocal_symbols,
5450 Relocatable_relocs* rr)
5452 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5453 Classify_reloc;
5454 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5455 Scan_relocatable_relocs;
5457 gold_assert(sh_type == elfcpp::SHT_RELA);
5459 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5460 symtab,
5461 layout,
5462 object,
5463 data_shndx,
5464 prelocs,
5465 reloc_count,
5466 output_section,
5467 needs_special_offset_handling,
5468 local_symbol_count,
5469 plocal_symbols,
5470 rr);
5473 // Scan the relocs for --emit-relocs.
5475 template<int size>
5476 void
5477 Target_x86_64<size>::emit_relocs_scan(
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_syms,
5489 Relocatable_relocs* rr)
5491 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5492 Classify_reloc;
5493 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5494 Emit_relocs_strategy;
5496 gold_assert(sh_type == elfcpp::SHT_RELA);
5498 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
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_syms,
5509 rr);
5512 // Relocate a section during a relocatable link.
5514 template<int size>
5515 void
5516 Target_x86_64<size>::relocate_relocs(
5517 const Relocate_info<size, false>* relinfo,
5518 unsigned int sh_type,
5519 const unsigned char* prelocs,
5520 size_t reloc_count,
5521 Output_section* output_section,
5522 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5523 unsigned char* view,
5524 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5525 section_size_type view_size,
5526 unsigned char* reloc_view,
5527 section_size_type reloc_view_size)
5529 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5530 Classify_reloc;
5532 gold_assert(sh_type == elfcpp::SHT_RELA);
5534 gold::relocate_relocs<size, false, Classify_reloc>(
5535 relinfo,
5536 prelocs,
5537 reloc_count,
5538 output_section,
5539 offset_in_output_section,
5540 view,
5541 view_address,
5542 view_size,
5543 reloc_view,
5544 reloc_view_size);
5547 // Return the value to use for a dynamic which requires special
5548 // treatment. This is how we support equality comparisons of function
5549 // pointers across shared library boundaries, as described in the
5550 // processor specific ABI supplement.
5552 template<int size>
5553 uint64_t
5554 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5556 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5557 return this->plt_address_for_global(gsym);
5560 // Return a string used to fill a code section with nops to take up
5561 // the specified length.
5563 template<int size>
5564 std::string
5565 Target_x86_64<size>::do_code_fill(section_size_type length) const
5567 if (length >= 16)
5569 // Build a jmpq instruction to skip over the bytes.
5570 unsigned char jmp[5];
5571 jmp[0] = 0xe9;
5572 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5573 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5574 + std::string(length - 5, static_cast<char>(0x90)));
5577 // Nop sequences of various lengths.
5578 const char nop1[1] = { '\x90' }; // nop
5579 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5580 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5581 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5582 '\x00'};
5583 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5584 '\x00', '\x00' };
5585 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5586 '\x44', '\x00', '\x00' };
5587 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5588 '\x00', '\x00', '\x00',
5589 '\x00' };
5590 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5591 '\x00', '\x00', '\x00',
5592 '\x00', '\x00' };
5593 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5594 '\x84', '\x00', '\x00',
5595 '\x00', '\x00', '\x00' };
5596 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5597 '\x1f', '\x84', '\x00',
5598 '\x00', '\x00', '\x00',
5599 '\x00' };
5600 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5601 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5602 '\x00', '\x00', '\x00',
5603 '\x00', '\x00' };
5604 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5605 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5606 '\x84', '\x00', '\x00',
5607 '\x00', '\x00', '\x00' };
5608 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5609 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5610 '\x1f', '\x84', '\x00',
5611 '\x00', '\x00', '\x00',
5612 '\x00' };
5613 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5614 '\x66', '\x66', '\x2e', // data16
5615 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5616 '\x00', '\x00', '\x00',
5617 '\x00', '\x00' };
5618 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5619 '\x66', '\x66', '\x66', // data16; data16
5620 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5621 '\x84', '\x00', '\x00',
5622 '\x00', '\x00', '\x00' };
5624 const char* nops[16] = {
5625 NULL,
5626 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5627 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5630 return std::string(nops[length], length);
5633 // Return the addend to use for a target specific relocation. The
5634 // only target specific relocation is R_X86_64_TLSDESC for a local
5635 // symbol. We want to set the addend is the offset of the local
5636 // symbol in the TLS segment.
5638 template<int size>
5639 uint64_t
5640 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5641 uint64_t) const
5643 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5644 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5645 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5646 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5647 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5648 gold_assert(psymval->is_tls_symbol());
5649 // The value of a TLS symbol is the offset in the TLS segment.
5650 return psymval->value(ti.object, 0);
5653 // Return the value to use for the base of a DW_EH_PE_datarel offset
5654 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5655 // assembler can not write out the difference between two labels in
5656 // different sections, so instead of using a pc-relative value they
5657 // use an offset from the GOT.
5659 template<int size>
5660 uint64_t
5661 Target_x86_64<size>::do_ehframe_datarel_base() const
5663 gold_assert(this->global_offset_table_ != NULL);
5664 Symbol* sym = this->global_offset_table_;
5665 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5666 return ssym->value();
5669 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5670 // compiled with -fsplit-stack. The function calls non-split-stack
5671 // code. We have to change the function so that it always ensures
5672 // that it has enough stack space to run some random function.
5674 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5675 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5676 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5678 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5679 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5680 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5682 template<int size>
5683 void
5684 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5685 section_offset_type fnoffset,
5686 section_size_type fnsize,
5687 const unsigned char*,
5688 size_t,
5689 unsigned char* view,
5690 section_size_type view_size,
5691 std::string* from,
5692 std::string* to) const
5694 const char* const cmp_insn = reinterpret_cast<const char*>
5695 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5696 const char* const lea_r10_insn = reinterpret_cast<const char*>
5697 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5698 const char* const lea_r11_insn = reinterpret_cast<const char*>
5699 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5701 const size_t cmp_insn_len =
5702 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5703 const size_t lea_r10_insn_len =
5704 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5705 const size_t lea_r11_insn_len =
5706 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5707 const size_t nop_len = (size == 32 ? 7 : 8);
5709 // The function starts with a comparison of the stack pointer and a
5710 // field in the TCB. This is followed by a jump.
5712 // cmp %fs:NN,%rsp
5713 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5714 && fnsize > nop_len + 1)
5716 // We will call __morestack if the carry flag is set after this
5717 // comparison. We turn the comparison into an stc instruction
5718 // and some nops.
5719 view[fnoffset] = '\xf9';
5720 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5722 // lea NN(%rsp),%r10
5723 // lea NN(%rsp),%r11
5724 else if ((this->match_view(view, view_size, fnoffset,
5725 lea_r10_insn, lea_r10_insn_len)
5726 || this->match_view(view, view_size, fnoffset,
5727 lea_r11_insn, lea_r11_insn_len))
5728 && fnsize > 8)
5730 // This is loading an offset from the stack pointer for a
5731 // comparison. The offset is negative, so we decrease the
5732 // offset by the amount of space we need for the stack. This
5733 // means we will avoid calling __morestack if there happens to
5734 // be plenty of space on the stack already.
5735 unsigned char* pval = view + fnoffset + 4;
5736 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5737 val -= parameters->options().split_stack_adjust_size();
5738 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5740 else
5742 if (!object->has_no_split_stack())
5743 object->error(_("failed to match split-stack sequence at "
5744 "section %u offset %0zx"),
5745 shndx, static_cast<size_t>(fnoffset));
5746 return;
5749 // We have to change the function so that it calls
5750 // __morestack_non_split instead of __morestack. The former will
5751 // allocate additional stack space.
5752 *from = "__morestack";
5753 *to = "__morestack_non_split";
5756 // The selector for x86_64 object files. Note this is never instantiated
5757 // directly. It's only used in Target_selector_x86_64_nacl, below.
5759 template<int size>
5760 class Target_selector_x86_64 : public Target_selector_freebsd
5762 public:
5763 Target_selector_x86_64()
5764 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5765 (size == 64
5766 ? "elf64-x86-64" : "elf32-x86-64"),
5767 (size == 64
5768 ? "elf64-x86-64-freebsd"
5769 : "elf32-x86-64-freebsd"),
5770 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5773 Target*
5774 do_instantiate_target()
5775 { return new Target_x86_64<size>(); }
5779 // NaCl variant. It uses different PLT contents.
5781 template<int size>
5782 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5784 public:
5785 Output_data_plt_x86_64_nacl(Layout* layout,
5786 Output_data_got<64, false>* got,
5787 Output_data_got_plt_x86_64* got_plt,
5788 Output_data_space* got_irelative)
5789 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5790 got, got_plt, got_irelative)
5793 Output_data_plt_x86_64_nacl(Layout* layout,
5794 Output_data_got<64, false>* got,
5795 Output_data_got_plt_x86_64* got_plt,
5796 Output_data_space* got_irelative,
5797 unsigned int plt_count)
5798 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5799 got, got_plt, got_irelative,
5800 plt_count)
5803 protected:
5804 virtual unsigned int
5805 do_get_plt_entry_size() const
5806 { return plt_entry_size; }
5808 virtual void
5809 do_add_eh_frame(Layout* layout)
5811 layout->add_eh_frame_for_plt(this,
5812 this->plt_eh_frame_cie,
5813 this->plt_eh_frame_cie_size,
5814 plt_eh_frame_fde,
5815 plt_eh_frame_fde_size);
5818 virtual void
5819 do_fill_first_plt_entry(unsigned char* pov,
5820 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5821 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5823 virtual unsigned int
5824 do_fill_plt_entry(unsigned char* pov,
5825 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5826 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5827 unsigned int got_offset,
5828 unsigned int plt_offset,
5829 unsigned int plt_index);
5831 virtual void
5832 do_fill_tlsdesc_entry(unsigned char* pov,
5833 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5834 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5835 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5836 unsigned int tlsdesc_got_offset,
5837 unsigned int plt_offset);
5839 private:
5840 // The size of an entry in the PLT.
5841 static const int plt_entry_size = 64;
5843 // The first entry in the PLT.
5844 static const unsigned char first_plt_entry[plt_entry_size];
5846 // Other entries in the PLT for an executable.
5847 static const unsigned char plt_entry[plt_entry_size];
5849 // The reserved TLSDESC entry in the PLT for an executable.
5850 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5852 // The .eh_frame unwind information for the PLT.
5853 static const int plt_eh_frame_fde_size = 32;
5854 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5857 template<int size>
5858 class Target_x86_64_nacl : public Target_x86_64<size>
5860 public:
5861 Target_x86_64_nacl()
5862 : Target_x86_64<size>(&x86_64_nacl_info)
5865 virtual Output_data_plt_x86_64<size>*
5866 do_make_data_plt(Layout* layout,
5867 Output_data_got<64, false>* got,
5868 Output_data_got_plt_x86_64* got_plt,
5869 Output_data_space* got_irelative)
5871 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5872 got_irelative);
5875 virtual Output_data_plt_x86_64<size>*
5876 do_make_data_plt(Layout* layout,
5877 Output_data_got<64, false>* got,
5878 Output_data_got_plt_x86_64* got_plt,
5879 Output_data_space* got_irelative,
5880 unsigned int plt_count)
5882 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5883 got_irelative,
5884 plt_count);
5887 virtual std::string
5888 do_code_fill(section_size_type length) const;
5890 private:
5891 static const Target::Target_info x86_64_nacl_info;
5894 template<>
5895 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5897 64, // size
5898 false, // is_big_endian
5899 elfcpp::EM_X86_64, // machine_code
5900 false, // has_make_symbol
5901 false, // has_resolve
5902 true, // has_code_fill
5903 true, // is_default_stack_executable
5904 true, // can_icf_inline_merge_sections
5905 '\0', // wrap_char
5906 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5907 0x20000, // default_text_segment_address
5908 0x10000, // abi_pagesize (overridable by -z max-page-size)
5909 0x10000, // common_pagesize (overridable by -z common-page-size)
5910 true, // isolate_execinstr
5911 0x10000000, // rosegment_gap
5912 elfcpp::SHN_UNDEF, // small_common_shndx
5913 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5914 0, // small_common_section_flags
5915 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5916 NULL, // attributes_section
5917 NULL, // attributes_vendor
5918 "_start", // entry_symbol_name
5919 32, // hash_entry_size
5920 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5923 template<>
5924 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5926 32, // size
5927 false, // is_big_endian
5928 elfcpp::EM_X86_64, // machine_code
5929 false, // has_make_symbol
5930 false, // has_resolve
5931 true, // has_code_fill
5932 true, // is_default_stack_executable
5933 true, // can_icf_inline_merge_sections
5934 '\0', // wrap_char
5935 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5936 0x20000, // default_text_segment_address
5937 0x10000, // abi_pagesize (overridable by -z max-page-size)
5938 0x10000, // common_pagesize (overridable by -z common-page-size)
5939 true, // isolate_execinstr
5940 0x10000000, // rosegment_gap
5941 elfcpp::SHN_UNDEF, // small_common_shndx
5942 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5943 0, // small_common_section_flags
5944 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5945 NULL, // attributes_section
5946 NULL, // attributes_vendor
5947 "_start", // entry_symbol_name
5948 32, // hash_entry_size
5949 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5952 #define NACLMASK 0xe0 // 32-byte alignment mask.
5954 // The first entry in the PLT.
5956 template<int size>
5957 const unsigned char
5958 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5960 0xff, 0x35, // pushq contents of memory address
5961 0, 0, 0, 0, // replaced with address of .got + 8
5962 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5963 0, 0, 0, 0, // replaced with address of .got + 16
5964 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5965 0x4d, 0x01, 0xfb, // add %r15, %r11
5966 0x41, 0xff, 0xe3, // jmpq *%r11
5968 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5969 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5971 // 32 bytes of nop to pad out to the standard size
5972 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5973 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5974 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5975 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5976 0x66, // excess data32 prefix
5977 0x90 // nop
5980 template<int size>
5981 void
5982 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5983 unsigned char* pov,
5984 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5985 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5987 memcpy(pov, first_plt_entry, plt_entry_size);
5988 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5989 (got_address + 8
5990 - (plt_address + 2 + 4)));
5991 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5992 (got_address + 16
5993 - (plt_address + 9 + 4)));
5996 // Subsequent entries in the PLT.
5998 template<int size>
5999 const unsigned char
6000 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
6002 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6003 0, 0, 0, 0, // replaced with address of symbol in .got
6004 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6005 0x4d, 0x01, 0xfb, // add %r15, %r11
6006 0x41, 0xff, 0xe3, // jmpq *%r11
6008 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6009 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6010 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6012 // Lazy GOT entries point here (32-byte aligned).
6013 0x68, // pushq immediate
6014 0, 0, 0, 0, // replaced with index into relocation table
6015 0xe9, // jmp relative
6016 0, 0, 0, 0, // replaced with offset to start of .plt0
6018 // 22 bytes of nop to pad out to the standard size.
6019 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6020 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6021 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6024 template<int size>
6025 unsigned int
6026 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
6027 unsigned char* pov,
6028 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6029 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6030 unsigned int got_offset,
6031 unsigned int plt_offset,
6032 unsigned int plt_index)
6034 memcpy(pov, plt_entry, plt_entry_size);
6035 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
6036 (got_address + got_offset
6037 - (plt_address + plt_offset
6038 + 3 + 4)));
6040 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
6041 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
6042 - (plt_offset + 38 + 4));
6044 return 32;
6047 // The reserved TLSDESC entry in the PLT.
6049 template<int size>
6050 const unsigned char
6051 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
6053 0xff, 0x35, // pushq x(%rip)
6054 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6055 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6056 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6057 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6058 0x4d, 0x01, 0xfb, // add %r15, %r11
6059 0x41, 0xff, 0xe3, // jmpq *%r11
6061 // 41 bytes of nop to pad out to the standard size.
6062 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6063 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6064 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6065 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6066 0x66, 0x66, // excess data32 prefixes
6067 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6070 template<int size>
6071 void
6072 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
6073 unsigned char* pov,
6074 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6075 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6076 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6077 unsigned int tlsdesc_got_offset,
6078 unsigned int plt_offset)
6080 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
6081 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6082 (got_address + 8
6083 - (plt_address + plt_offset
6084 + 2 + 4)));
6085 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6086 (got_base
6087 + tlsdesc_got_offset
6088 - (plt_address + plt_offset
6089 + 9 + 4)));
6092 // The .eh_frame unwind information for the PLT.
6094 template<int size>
6095 const unsigned char
6096 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
6098 0, 0, 0, 0, // Replaced with offset to .plt.
6099 0, 0, 0, 0, // Replaced with size of .plt.
6100 0, // Augmentation size.
6101 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
6102 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
6103 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
6104 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
6105 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
6106 13, // Block length.
6107 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
6108 elfcpp::DW_OP_breg16, 0, // Push %rip.
6109 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
6110 elfcpp::DW_OP_and, // & (%rip & 0x3f).
6111 elfcpp::DW_OP_const1u, 37, // Push 0x25.
6112 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
6113 elfcpp::DW_OP_lit3, // Push 3.
6114 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
6115 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6116 elfcpp::DW_CFA_nop, // Align to 32 bytes.
6117 elfcpp::DW_CFA_nop
6120 // Return a string used to fill a code section with nops.
6121 // For NaCl, long NOPs are only valid if they do not cross
6122 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6123 template<int size>
6124 std::string
6125 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
6127 return std::string(length, static_cast<char>(0x90));
6130 // The selector for x86_64-nacl object files.
6132 template<int size>
6133 class Target_selector_x86_64_nacl
6134 : public Target_selector_nacl<Target_selector_x86_64<size>,
6135 Target_x86_64_nacl<size> >
6137 public:
6138 Target_selector_x86_64_nacl()
6139 : Target_selector_nacl<Target_selector_x86_64<size>,
6140 Target_x86_64_nacl<size> >("x86-64",
6141 size == 64
6142 ? "elf64-x86-64-nacl"
6143 : "elf32-x86-64-nacl",
6144 size == 64
6145 ? "elf_x86_64_nacl"
6146 : "elf32_x86_64_nacl")
6150 Target_selector_x86_64_nacl<64> target_selector_x86_64;
6151 Target_selector_x86_64_nacl<32> target_selector_x32;
6153 } // End anonymous namespace.