Document the GDB 10.1 release in gdb/ChangeLog
[binutils-gdb.git] / gold / x86_64.cc
blob378bac16f78db660793e002f4c9f964658f7dc0c
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2020 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 class Output_data_plt_x86_64_bnd : public Output_data_plt_x86_64<64>
435 public:
436 Output_data_plt_x86_64_bnd(Layout* layout,
437 Output_data_got<64, false>* got,
438 Output_data_got_plt_x86_64* got_plt,
439 Output_data_space* got_irelative)
440 : Output_data_plt_x86_64<64>(layout, plt_entry_size,
441 got, got_plt, got_irelative),
442 aplt_offset_(0)
445 Output_data_plt_x86_64_bnd(Layout* layout,
446 Output_data_got<64, false>* got,
447 Output_data_got_plt_x86_64* got_plt,
448 Output_data_space* got_irelative,
449 unsigned int plt_count)
450 : Output_data_plt_x86_64<64>(layout, plt_entry_size,
451 got, got_plt, got_irelative,
452 plt_count),
453 aplt_offset_(0)
456 protected:
457 virtual unsigned int
458 do_get_plt_entry_size() const
459 { return plt_entry_size; }
461 // Return the PLT address to use for a global symbol.
462 uint64_t
463 do_address_for_global(const Symbol*);
465 // Return the PLT address to use for a local symbol.
466 uint64_t
467 do_address_for_local(const Relobj*, unsigned int symndx);
469 virtual void
470 do_add_eh_frame(Layout* layout)
472 layout->add_eh_frame_for_plt(this,
473 this->plt_eh_frame_cie,
474 this->plt_eh_frame_cie_size,
475 plt_eh_frame_fde,
476 plt_eh_frame_fde_size);
479 virtual void
480 do_fill_first_plt_entry(unsigned char* pov,
481 elfcpp::Elf_types<64>::Elf_Addr got_addr,
482 elfcpp::Elf_types<64>::Elf_Addr plt_addr);
484 virtual unsigned int
485 do_fill_plt_entry(unsigned char* pov,
486 elfcpp::Elf_types<64>::Elf_Addr got_address,
487 elfcpp::Elf_types<64>::Elf_Addr plt_address,
488 unsigned int got_offset,
489 unsigned int plt_offset,
490 unsigned int plt_index);
492 virtual void
493 do_fill_tlsdesc_entry(unsigned char* pov,
494 elfcpp::Elf_types<64>::Elf_Addr got_address,
495 elfcpp::Elf_types<64>::Elf_Addr plt_address,
496 elfcpp::Elf_types<64>::Elf_Addr got_base,
497 unsigned int tlsdesc_got_offset,
498 unsigned int plt_offset);
500 void
501 fill_aplt_entry(unsigned char* pov,
502 elfcpp::Elf_types<64>::Elf_Addr got_address,
503 elfcpp::Elf_types<64>::Elf_Addr plt_address,
504 unsigned int got_offset,
505 unsigned int plt_offset,
506 unsigned int plt_index);
508 private:
509 // Set the final size.
510 void
511 set_final_data_size();
513 // Write out the BND PLT data.
514 void
515 do_write(Output_file*);
517 // Offset of the Additional PLT (if using -z bndplt).
518 unsigned int aplt_offset_;
520 // The size of an entry in the PLT.
521 static const int plt_entry_size = 16;
523 // The size of an entry in the additional PLT.
524 static const int aplt_entry_size = 8;
526 // The first entry in the PLT.
527 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
528 // procedure linkage table for both programs and shared objects."
529 static const unsigned char first_plt_entry[plt_entry_size];
531 // Other entries in the PLT for an executable.
532 static const unsigned char plt_entry[plt_entry_size];
534 // Entries in the additional PLT.
535 static const unsigned char aplt_entry[aplt_entry_size];
537 // The reserved TLSDESC entry in the PLT for an executable.
538 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
540 // The .eh_frame unwind information for the PLT.
541 static const int plt_eh_frame_fde_size = 32;
542 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
545 // We use this PLT when Indirect Branch Tracking (IBT) is enabled.
547 template <int size>
548 class Output_data_plt_x86_64_ibt : public Output_data_plt_x86_64<size>
550 public:
551 Output_data_plt_x86_64_ibt(Layout* layout,
552 Output_data_got<64, false>* got,
553 Output_data_got_plt_x86_64* got_plt,
554 Output_data_space* got_irelative)
555 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
556 got, got_plt, got_irelative),
557 aplt_offset_(0)
560 Output_data_plt_x86_64_ibt(Layout* layout,
561 Output_data_got<64, false>* got,
562 Output_data_got_plt_x86_64* got_plt,
563 Output_data_space* got_irelative,
564 unsigned int plt_count)
565 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
566 got, got_plt, got_irelative,
567 plt_count),
568 aplt_offset_(0)
571 protected:
572 virtual unsigned int
573 do_get_plt_entry_size() const
574 { return plt_entry_size; }
576 // Return the PLT address to use for a global symbol.
577 uint64_t
578 do_address_for_global(const Symbol*);
580 // Return the PLT address to use for a local symbol.
581 uint64_t
582 do_address_for_local(const Relobj*, unsigned int symndx);
584 virtual void
585 do_add_eh_frame(Layout* layout)
587 layout->add_eh_frame_for_plt(this,
588 this->plt_eh_frame_cie,
589 this->plt_eh_frame_cie_size,
590 plt_eh_frame_fde,
591 plt_eh_frame_fde_size);
594 virtual void
595 do_fill_first_plt_entry(unsigned char* pov,
596 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
597 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
599 virtual unsigned int
600 do_fill_plt_entry(unsigned char* pov,
601 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
602 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
603 unsigned int got_offset,
604 unsigned int plt_offset,
605 unsigned int plt_index);
607 virtual void
608 do_fill_tlsdesc_entry(unsigned char* pov,
609 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
610 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
611 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
612 unsigned int tlsdesc_got_offset,
613 unsigned int plt_offset);
615 void
616 fill_aplt_entry(unsigned char* pov,
617 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
618 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
619 unsigned int got_offset,
620 unsigned int plt_offset,
621 unsigned int plt_index);
623 private:
624 // Set the final size.
625 void
626 set_final_data_size();
628 // Write out the BND PLT data.
629 void
630 do_write(Output_file*);
632 // Offset of the Additional PLT (if using -z bndplt).
633 unsigned int aplt_offset_;
635 // The size of an entry in the PLT.
636 static const int plt_entry_size = 16;
638 // The size of an entry in the additional PLT.
639 static const int aplt_entry_size = 16;
641 // The first entry in the PLT.
642 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
643 // procedure linkage table for both programs and shared objects."
644 static const unsigned char first_plt_entry[plt_entry_size];
646 // Other entries in the PLT for an executable.
647 static const unsigned char plt_entry[plt_entry_size];
649 // Entries in the additional PLT.
650 static const unsigned char aplt_entry[aplt_entry_size];
652 // The reserved TLSDESC entry in the PLT for an executable.
653 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
655 // The .eh_frame unwind information for the PLT.
656 static const int plt_eh_frame_fde_size = 32;
657 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
660 template<int size>
661 class Lazy_view
663 public:
664 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
665 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
668 inline unsigned char
669 operator[](size_t offset)
671 if (this->view_ == NULL)
672 this->view_ = this->object_->section_contents(this->data_shndx_,
673 &this->view_size_,
674 true);
675 if (offset >= this->view_size_)
676 return 0;
677 return this->view_[offset];
680 private:
681 Sized_relobj_file<size, false>* object_;
682 unsigned int data_shndx_;
683 const unsigned char* view_;
684 section_size_type view_size_;
687 // The x86_64 target class.
688 // See the ABI at
689 // http://www.x86-64.org/documentation/abi.pdf
690 // TLS info comes from
691 // http://people.redhat.com/drepper/tls.pdf
692 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
694 template<int size>
695 class Target_x86_64 : public Sized_target<size, false>
697 public:
698 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
699 // uses only Elf64_Rela relocation entries with explicit addends."
700 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
702 Target_x86_64(const Target::Target_info* info = &x86_64_info)
703 : Sized_target<size, false>(info),
704 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
705 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
706 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
707 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
708 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
709 feature_1_(0), feature_2_used_(0), feature_2_needed_(0),
710 object_isa_1_used_(0), object_feature_1_(0),
711 object_feature_2_used_(0), seen_first_object_(false)
714 // Hook for a new output section.
715 void
716 do_new_output_section(Output_section*) const;
718 // Scan the relocations to look for symbol adjustments.
719 void
720 gc_process_relocs(Symbol_table* symtab,
721 Layout* layout,
722 Sized_relobj_file<size, false>* object,
723 unsigned int data_shndx,
724 unsigned int sh_type,
725 const unsigned char* prelocs,
726 size_t reloc_count,
727 Output_section* output_section,
728 bool needs_special_offset_handling,
729 size_t local_symbol_count,
730 const unsigned char* plocal_symbols);
732 // Scan the relocations to look for symbol adjustments.
733 void
734 scan_relocs(Symbol_table* symtab,
735 Layout* layout,
736 Sized_relobj_file<size, false>* object,
737 unsigned int data_shndx,
738 unsigned int sh_type,
739 const unsigned char* prelocs,
740 size_t reloc_count,
741 Output_section* output_section,
742 bool needs_special_offset_handling,
743 size_t local_symbol_count,
744 const unsigned char* plocal_symbols);
746 // Finalize the sections.
747 void
748 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
750 // Return the value to use for a dynamic which requires special
751 // treatment.
752 uint64_t
753 do_dynsym_value(const Symbol*) const;
755 // Relocate a section.
756 void
757 relocate_section(const Relocate_info<size, false>*,
758 unsigned int sh_type,
759 const unsigned char* prelocs,
760 size_t reloc_count,
761 Output_section* output_section,
762 bool needs_special_offset_handling,
763 unsigned char* view,
764 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
765 section_size_type view_size,
766 const Reloc_symbol_changes*);
768 // Scan the relocs during a relocatable link.
769 void
770 scan_relocatable_relocs(Symbol_table* symtab,
771 Layout* layout,
772 Sized_relobj_file<size, false>* object,
773 unsigned int data_shndx,
774 unsigned int sh_type,
775 const unsigned char* prelocs,
776 size_t reloc_count,
777 Output_section* output_section,
778 bool needs_special_offset_handling,
779 size_t local_symbol_count,
780 const unsigned char* plocal_symbols,
781 Relocatable_relocs*);
783 // Scan the relocs for --emit-relocs.
784 void
785 emit_relocs_scan(Symbol_table* symtab,
786 Layout* layout,
787 Sized_relobj_file<size, false>* object,
788 unsigned int data_shndx,
789 unsigned int sh_type,
790 const unsigned char* prelocs,
791 size_t reloc_count,
792 Output_section* output_section,
793 bool needs_special_offset_handling,
794 size_t local_symbol_count,
795 const unsigned char* plocal_syms,
796 Relocatable_relocs* rr);
798 // Emit relocations for a section.
799 void
800 relocate_relocs(
801 const Relocate_info<size, false>*,
802 unsigned int sh_type,
803 const unsigned char* prelocs,
804 size_t reloc_count,
805 Output_section* output_section,
806 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
807 unsigned char* view,
808 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
809 section_size_type view_size,
810 unsigned char* reloc_view,
811 section_size_type reloc_view_size);
813 // Return a string used to fill a code section with nops.
814 std::string
815 do_code_fill(section_size_type length) const;
817 // Return whether SYM is defined by the ABI.
818 bool
819 do_is_defined_by_abi(const Symbol* sym) const
820 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
822 // Return the symbol index to use for a target specific relocation.
823 // The only target specific relocation is R_X86_64_TLSDESC for a
824 // local symbol, which is an absolute reloc.
825 unsigned int
826 do_reloc_symbol_index(void*, unsigned int r_type) const
828 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
829 return 0;
832 // Return the addend to use for a target specific relocation.
833 uint64_t
834 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
836 // Return the PLT section.
837 uint64_t
838 do_plt_address_for_global(const Symbol* gsym) const
839 { return this->plt_section()->address_for_global(gsym); }
841 uint64_t
842 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
843 { return this->plt_section()->address_for_local(relobj, symndx); }
845 // This function should be defined in targets that can use relocation
846 // types to determine (implemented in local_reloc_may_be_function_pointer
847 // and global_reloc_may_be_function_pointer)
848 // if a function's pointer is taken. ICF uses this in safe mode to only
849 // fold those functions whose pointer is defintely not taken. For x86_64
850 // pie binaries, safe ICF cannot be done by looking at only relocation
851 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
852 // opcode is checked as well to distinguish a function call from taking
853 // a function's pointer.
854 bool
855 do_can_check_for_function_pointers() const
856 { return true; }
858 // Return the base for a DW_EH_PE_datarel encoding.
859 uint64_t
860 do_ehframe_datarel_base() const;
862 // Adjust -fsplit-stack code which calls non-split-stack code.
863 void
864 do_calls_non_split(Relobj* object, unsigned int shndx,
865 section_offset_type fnoffset, section_size_type fnsize,
866 const unsigned char* prelocs, size_t reloc_count,
867 unsigned char* view, section_size_type view_size,
868 std::string* from, std::string* to) const;
870 // Return the size of the GOT section.
871 section_size_type
872 got_size() const
874 gold_assert(this->got_ != NULL);
875 return this->got_->data_size();
878 // Return the number of entries in the GOT.
879 unsigned int
880 got_entry_count() const
882 if (this->got_ == NULL)
883 return 0;
884 return this->got_size() / 8;
887 // Return the number of entries in the PLT.
888 unsigned int
889 plt_entry_count() const;
891 // Return the offset of the first non-reserved PLT entry.
892 unsigned int
893 first_plt_entry_offset() const;
895 // Return the size of each PLT entry.
896 unsigned int
897 plt_entry_size() const;
899 // Return the size of each GOT entry.
900 unsigned int
901 got_entry_size() const
902 { return 8; };
904 // Create the GOT section for an incremental update.
905 Output_data_got_base*
906 init_got_plt_for_update(Symbol_table* symtab,
907 Layout* layout,
908 unsigned int got_count,
909 unsigned int plt_count);
911 // Reserve a GOT entry for a local symbol, and regenerate any
912 // necessary dynamic relocations.
913 void
914 reserve_local_got_entry(unsigned int got_index,
915 Sized_relobj<size, false>* obj,
916 unsigned int r_sym,
917 unsigned int got_type);
919 // Reserve a GOT entry for a global symbol, and regenerate any
920 // necessary dynamic relocations.
921 void
922 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
923 unsigned int got_type);
925 // Register an existing PLT entry for a global symbol.
926 void
927 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
928 Symbol* gsym);
930 // Force a COPY relocation for a given symbol.
931 void
932 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
934 // Apply an incremental relocation.
935 void
936 apply_relocation(const Relocate_info<size, false>* relinfo,
937 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
938 unsigned int r_type,
939 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
940 const Symbol* gsym,
941 unsigned char* view,
942 typename elfcpp::Elf_types<size>::Elf_Addr address,
943 section_size_type view_size);
945 // Add a new reloc argument, returning the index in the vector.
946 size_t
947 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
949 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
950 return this->tlsdesc_reloc_info_.size() - 1;
953 Output_data_plt_x86_64<size>*
954 make_data_plt(Layout* layout,
955 Output_data_got<64, false>* got,
956 Output_data_got_plt_x86_64* got_plt,
957 Output_data_space* got_irelative)
959 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
962 Output_data_plt_x86_64<size>*
963 make_data_plt(Layout* layout,
964 Output_data_got<64, false>* got,
965 Output_data_got_plt_x86_64* got_plt,
966 Output_data_space* got_irelative,
967 unsigned int plt_count)
969 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
970 plt_count);
973 virtual Output_data_plt_x86_64<size>*
974 do_make_data_plt(Layout* layout,
975 Output_data_got<64, false>* got,
976 Output_data_got_plt_x86_64* got_plt,
977 Output_data_space* got_irelative);
979 virtual Output_data_plt_x86_64<size>*
980 do_make_data_plt(Layout* layout,
981 Output_data_got<64, false>* got,
982 Output_data_got_plt_x86_64* got_plt,
983 Output_data_space* got_irelative,
984 unsigned int plt_count);
986 private:
987 // The class which scans relocations.
988 class Scan
990 public:
991 Scan()
992 : issued_non_pic_error_(false)
995 static inline int
996 get_reference_flags(unsigned int r_type);
998 inline void
999 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
1000 Sized_relobj_file<size, false>* object,
1001 unsigned int data_shndx,
1002 Output_section* output_section,
1003 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
1004 const elfcpp::Sym<size, false>& lsym,
1005 bool is_discarded);
1007 inline void
1008 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
1009 Sized_relobj_file<size, false>* object,
1010 unsigned int data_shndx,
1011 Output_section* output_section,
1012 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
1013 Symbol* gsym);
1015 inline bool
1016 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
1017 Target_x86_64* target,
1018 Sized_relobj_file<size, false>* object,
1019 unsigned int data_shndx,
1020 Output_section* output_section,
1021 const elfcpp::Rela<size, false>& reloc,
1022 unsigned int r_type,
1023 const elfcpp::Sym<size, false>& lsym);
1025 inline bool
1026 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
1027 Target_x86_64* target,
1028 Sized_relobj_file<size, false>* object,
1029 unsigned int data_shndx,
1030 Output_section* output_section,
1031 const elfcpp::Rela<size, false>& reloc,
1032 unsigned int r_type,
1033 Symbol* gsym);
1035 private:
1036 static void
1037 unsupported_reloc_local(Sized_relobj_file<size, false>*,
1038 unsigned int r_type);
1040 static void
1041 unsupported_reloc_global(Sized_relobj_file<size, false>*,
1042 unsigned int r_type, Symbol*);
1044 void
1045 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
1047 inline bool
1048 possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
1049 unsigned int src_indx,
1050 unsigned int r_offset,
1051 unsigned int r_type);
1053 bool
1054 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
1055 unsigned int r_type);
1057 // Whether we have issued an error about a non-PIC compilation.
1058 bool issued_non_pic_error_;
1061 // The class which implements relocation.
1062 class Relocate
1064 public:
1065 Relocate()
1066 : skip_call_tls_get_addr_(false)
1069 ~Relocate()
1071 if (this->skip_call_tls_get_addr_)
1073 // FIXME: This needs to specify the location somehow.
1074 gold_error(_("missing expected TLS relocation"));
1078 // Do a relocation. Return false if the caller should not issue
1079 // any warnings about this relocation.
1080 inline bool
1081 relocate(const Relocate_info<size, false>*, unsigned int,
1082 Target_x86_64*, Output_section*, size_t, const unsigned char*,
1083 const Sized_symbol<size>*, const Symbol_value<size>*,
1084 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1085 section_size_type);
1087 private:
1088 // Do a TLS relocation.
1089 inline void
1090 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
1091 size_t relnum, const elfcpp::Rela<size, false>&,
1092 unsigned int r_type, const Sized_symbol<size>*,
1093 const Symbol_value<size>*,
1094 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1095 section_size_type);
1097 // Do a TLS General-Dynamic to Initial-Exec transition.
1098 inline void
1099 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1100 const elfcpp::Rela<size, false>&, unsigned int r_type,
1101 typename elfcpp::Elf_types<size>::Elf_Addr value,
1102 unsigned char* view,
1103 typename elfcpp::Elf_types<size>::Elf_Addr,
1104 section_size_type view_size);
1106 // Do a TLS General-Dynamic to Local-Exec transition.
1107 inline void
1108 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1109 Output_segment* tls_segment,
1110 const elfcpp::Rela<size, false>&, unsigned int r_type,
1111 typename elfcpp::Elf_types<size>::Elf_Addr value,
1112 unsigned char* view,
1113 section_size_type view_size);
1115 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1116 inline void
1117 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1118 const elfcpp::Rela<size, false>&, unsigned int r_type,
1119 typename elfcpp::Elf_types<size>::Elf_Addr value,
1120 unsigned char* view,
1121 typename elfcpp::Elf_types<size>::Elf_Addr,
1122 section_size_type view_size);
1124 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1125 inline void
1126 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1127 Output_segment* tls_segment,
1128 const elfcpp::Rela<size, false>&, unsigned int r_type,
1129 typename elfcpp::Elf_types<size>::Elf_Addr value,
1130 unsigned char* view,
1131 section_size_type view_size);
1133 // Do a TLS Local-Dynamic to Local-Exec transition.
1134 inline void
1135 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
1136 Output_segment* tls_segment,
1137 const elfcpp::Rela<size, false>&, unsigned int r_type,
1138 typename elfcpp::Elf_types<size>::Elf_Addr value,
1139 unsigned char* view,
1140 section_size_type view_size);
1142 // Do a TLS Initial-Exec to Local-Exec transition.
1143 static inline void
1144 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
1145 Output_segment* tls_segment,
1146 const elfcpp::Rela<size, false>&, unsigned int r_type,
1147 typename elfcpp::Elf_types<size>::Elf_Addr value,
1148 unsigned char* view,
1149 section_size_type view_size);
1151 // This is set if we should skip the next reloc, which should be a
1152 // PLT32 reloc against ___tls_get_addr.
1153 bool skip_call_tls_get_addr_;
1156 // Check if relocation against this symbol is a candidate for
1157 // conversion from
1158 // mov foo@GOTPCREL(%rip), %reg
1159 // to lea foo(%rip), %reg.
1160 template<class View_type>
1161 static inline bool
1162 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
1163 size_t r_offset, View_type* view)
1165 gold_assert(gsym != NULL);
1166 // We cannot do the conversion unless it's one of these relocations.
1167 if (r_type != elfcpp::R_X86_64_GOTPCREL
1168 && r_type != elfcpp::R_X86_64_GOTPCRELX
1169 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
1170 return false;
1171 // We cannot convert references to IFUNC symbols, or to symbols that
1172 // are not local to the current module.
1173 // We can't do predefined symbols because they may become undefined
1174 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1175 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1176 || gsym->is_undefined()
1177 || gsym->is_predefined()
1178 || gsym->is_from_dynobj()
1179 || gsym->is_preemptible())
1180 return false;
1181 // If we are building a shared object and the symbol is protected, we may
1182 // need to go through the GOT.
1183 if (parameters->options().shared()
1184 && gsym->visibility() == elfcpp::STV_PROTECTED)
1185 return false;
1186 // We cannot convert references to the _DYNAMIC symbol.
1187 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1188 return false;
1189 // Check for a MOV opcode.
1190 return (*view)[r_offset - 2] == 0x8b;
1193 // Convert
1194 // callq *foo@GOTPCRELX(%rip) to
1195 // addr32 callq foo
1196 // and jmpq *foo@GOTPCRELX(%rip) to
1197 // jmpq foo
1198 // nop
1199 template<class View_type>
1200 static inline bool
1201 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1202 size_t r_offset, View_type* view)
1204 gold_assert(gsym != NULL);
1205 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1206 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1207 return false;
1208 // We cannot convert references to IFUNC symbols, or to symbols that
1209 // are not local to the current module.
1210 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1211 || gsym->is_undefined ()
1212 || gsym->is_from_dynobj()
1213 || gsym->is_preemptible())
1214 return false;
1215 // Check for a CALLQ or JMPQ opcode.
1216 return ((*view)[r_offset - 2] == 0xff
1217 && ((*view)[r_offset - 1] == 0x15
1218 || (*view)[r_offset - 1] == 0x25));
1221 // Adjust TLS relocation type based on the options and whether this
1222 // is a local symbol.
1223 static tls::Tls_optimization
1224 optimize_tls_reloc(bool is_final, int r_type);
1226 // Get the GOT section, creating it if necessary.
1227 Output_data_got<64, false>*
1228 got_section(Symbol_table*, Layout*);
1230 // Get the GOT PLT section.
1231 Output_data_got_plt_x86_64*
1232 got_plt_section() const
1234 gold_assert(this->got_plt_ != NULL);
1235 return this->got_plt_;
1238 // Get the GOT section for TLSDESC entries.
1239 Output_data_got<64, false>*
1240 got_tlsdesc_section() const
1242 gold_assert(this->got_tlsdesc_ != NULL);
1243 return this->got_tlsdesc_;
1246 // Create the PLT section.
1247 void
1248 make_plt_section(Symbol_table* symtab, Layout* layout);
1250 // Create a PLT entry for a global symbol.
1251 void
1252 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1254 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1255 void
1256 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1257 Sized_relobj_file<size, false>* relobj,
1258 unsigned int local_sym_index);
1260 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1261 void
1262 define_tls_base_symbol(Symbol_table*, Layout*);
1264 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1265 void
1266 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1268 // Create a GOT entry for the TLS module index.
1269 unsigned int
1270 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1271 Sized_relobj_file<size, false>* object);
1273 // Get the PLT section.
1274 Output_data_plt_x86_64<size>*
1275 plt_section() const
1277 gold_assert(this->plt_ != NULL);
1278 return this->plt_;
1281 // Get the dynamic reloc section, creating it if necessary.
1282 Reloc_section*
1283 rela_dyn_section(Layout*);
1285 // Get the section to use for TLSDESC relocations.
1286 Reloc_section*
1287 rela_tlsdesc_section(Layout*) const;
1289 // Get the section to use for IRELATIVE relocations.
1290 Reloc_section*
1291 rela_irelative_section(Layout*);
1293 // Add a potential copy relocation.
1294 void
1295 copy_reloc(Symbol_table* symtab, Layout* layout,
1296 Sized_relobj_file<size, false>* object,
1297 unsigned int shndx, Output_section* output_section,
1298 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1300 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1301 this->copy_relocs_.copy_reloc(symtab, layout,
1302 symtab->get_sized_symbol<size>(sym),
1303 object, shndx, output_section,
1304 r_type, reloc.get_r_offset(),
1305 reloc.get_r_addend(),
1306 this->rela_dyn_section(layout));
1309 // Record a target-specific program property in the .note.gnu.property
1310 // section.
1311 void
1312 record_gnu_property(unsigned int, unsigned int, size_t,
1313 const unsigned char*, const Object*);
1315 // Merge the target-specific program properties from the current object.
1316 void
1317 merge_gnu_properties(const Object*);
1319 // Finalize the target-specific program properties and add them back to
1320 // the layout.
1321 void
1322 do_finalize_gnu_properties(Layout*) const;
1324 // Information about this specific target which we pass to the
1325 // general Target structure.
1326 static const Target::Target_info x86_64_info;
1328 // The types of GOT entries needed for this platform.
1329 // These values are exposed to the ABI in an incremental link.
1330 // Do not renumber existing values without changing the version
1331 // number of the .gnu_incremental_inputs section.
1332 enum Got_type
1334 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1335 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1336 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1337 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1340 // This type is used as the argument to the target specific
1341 // relocation routines. The only target specific reloc is
1342 // R_X86_64_TLSDESC against a local symbol.
1343 struct Tlsdesc_info
1345 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1346 : object(a_object), r_sym(a_r_sym)
1349 // The object in which the local symbol is defined.
1350 Sized_relobj_file<size, false>* object;
1351 // The local symbol index in the object.
1352 unsigned int r_sym;
1355 // The GOT section.
1356 Output_data_got<64, false>* got_;
1357 // The PLT section.
1358 Output_data_plt_x86_64<size>* plt_;
1359 // The GOT PLT section.
1360 Output_data_got_plt_x86_64* got_plt_;
1361 // The GOT section for IRELATIVE relocations.
1362 Output_data_space* got_irelative_;
1363 // The GOT section for TLSDESC relocations.
1364 Output_data_got<64, false>* got_tlsdesc_;
1365 // The _GLOBAL_OFFSET_TABLE_ symbol.
1366 Symbol* global_offset_table_;
1367 // The dynamic reloc section.
1368 Reloc_section* rela_dyn_;
1369 // The section to use for IRELATIVE relocs.
1370 Reloc_section* rela_irelative_;
1371 // Relocs saved to avoid a COPY reloc.
1372 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1373 // Offset of the GOT entry for the TLS module index.
1374 unsigned int got_mod_index_offset_;
1375 // We handle R_X86_64_TLSDESC against a local symbol as a target
1376 // specific relocation. Here we store the object and local symbol
1377 // index for the relocation.
1378 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1379 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1380 bool tls_base_symbol_defined_;
1381 // Target-specific program properties, from .note.gnu.property section.
1382 // Each bit represents a specific feature.
1383 uint32_t isa_1_used_;
1384 uint32_t isa_1_needed_;
1385 uint32_t feature_1_;
1386 uint32_t feature_2_used_;
1387 uint32_t feature_2_needed_;
1388 // Target-specific properties from the current object.
1389 // These bits get ORed into ISA_1_USED_ after all properties for the object
1390 // have been processed. But if either is all zeroes (as when the property
1391 // is absent from an object), the result should be all zeroes.
1392 // (See PR ld/23486.)
1393 uint32_t object_isa_1_used_;
1394 // These bits get ANDed into FEATURE_1_ after all properties for the object
1395 // have been processed.
1396 uint32_t object_feature_1_;
1397 uint32_t object_feature_2_used_;
1398 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1399 bool seen_first_object_;
1402 template<>
1403 const Target::Target_info Target_x86_64<64>::x86_64_info =
1405 64, // size
1406 false, // is_big_endian
1407 elfcpp::EM_X86_64, // machine_code
1408 false, // has_make_symbol
1409 false, // has_resolve
1410 true, // has_code_fill
1411 true, // is_default_stack_executable
1412 true, // can_icf_inline_merge_sections
1413 '\0', // wrap_char
1414 "/lib/ld64.so.1", // program interpreter
1415 0x400000, // default_text_segment_address
1416 0x1000, // abi_pagesize (overridable by -z max-page-size)
1417 0x1000, // common_pagesize (overridable by -z common-page-size)
1418 false, // isolate_execinstr
1419 0, // rosegment_gap
1420 elfcpp::SHN_UNDEF, // small_common_shndx
1421 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1422 0, // small_common_section_flags
1423 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1424 NULL, // attributes_section
1425 NULL, // attributes_vendor
1426 "_start", // entry_symbol_name
1427 32, // hash_entry_size
1428 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1431 template<>
1432 const Target::Target_info Target_x86_64<32>::x86_64_info =
1434 32, // size
1435 false, // is_big_endian
1436 elfcpp::EM_X86_64, // machine_code
1437 false, // has_make_symbol
1438 false, // has_resolve
1439 true, // has_code_fill
1440 true, // is_default_stack_executable
1441 true, // can_icf_inline_merge_sections
1442 '\0', // wrap_char
1443 "/libx32/ldx32.so.1", // program interpreter
1444 0x400000, // default_text_segment_address
1445 0x1000, // abi_pagesize (overridable by -z max-page-size)
1446 0x1000, // common_pagesize (overridable by -z common-page-size)
1447 false, // isolate_execinstr
1448 0, // rosegment_gap
1449 elfcpp::SHN_UNDEF, // small_common_shndx
1450 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1451 0, // small_common_section_flags
1452 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1453 NULL, // attributes_section
1454 NULL, // attributes_vendor
1455 "_start", // entry_symbol_name
1456 32, // hash_entry_size
1457 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1460 // This is called when a new output section is created. This is where
1461 // we handle the SHF_X86_64_LARGE.
1463 template<int size>
1464 void
1465 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1467 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1468 os->set_is_large_section();
1471 // Get the GOT section, creating it if necessary.
1473 template<int size>
1474 Output_data_got<64, false>*
1475 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1477 if (this->got_ == NULL)
1479 gold_assert(symtab != NULL && layout != NULL);
1481 // When using -z now, we can treat .got.plt as a relro section.
1482 // Without -z now, it is modified after program startup by lazy
1483 // PLT relocations.
1484 bool is_got_plt_relro = parameters->options().now();
1485 Output_section_order got_order = (is_got_plt_relro
1486 ? ORDER_RELRO
1487 : ORDER_RELRO_LAST);
1488 Output_section_order got_plt_order = (is_got_plt_relro
1489 ? ORDER_RELRO
1490 : ORDER_NON_RELRO_FIRST);
1492 this->got_ = new Output_data_got<64, false>();
1494 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1495 (elfcpp::SHF_ALLOC
1496 | elfcpp::SHF_WRITE),
1497 this->got_, got_order, true);
1499 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1500 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1501 (elfcpp::SHF_ALLOC
1502 | elfcpp::SHF_WRITE),
1503 this->got_plt_, got_plt_order,
1504 is_got_plt_relro);
1506 // The first three entries are reserved.
1507 this->got_plt_->set_current_data_size(3 * 8);
1509 if (!is_got_plt_relro)
1511 // Those bytes can go into the relro segment.
1512 layout->increase_relro(3 * 8);
1515 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1516 this->global_offset_table_ =
1517 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1518 Symbol_table::PREDEFINED,
1519 this->got_plt_,
1520 0, 0, elfcpp::STT_OBJECT,
1521 elfcpp::STB_LOCAL,
1522 elfcpp::STV_HIDDEN, 0,
1523 false, false);
1525 // If there are any IRELATIVE relocations, they get GOT entries
1526 // in .got.plt after the jump slot entries.
1527 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1528 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1529 (elfcpp::SHF_ALLOC
1530 | elfcpp::SHF_WRITE),
1531 this->got_irelative_,
1532 got_plt_order, is_got_plt_relro);
1534 // If there are any TLSDESC relocations, they get GOT entries in
1535 // .got.plt after the jump slot and IRELATIVE entries.
1536 this->got_tlsdesc_ = new Output_data_got<64, false>();
1537 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1538 (elfcpp::SHF_ALLOC
1539 | elfcpp::SHF_WRITE),
1540 this->got_tlsdesc_,
1541 got_plt_order, is_got_plt_relro);
1544 return this->got_;
1547 // Get the dynamic reloc section, creating it if necessary.
1549 template<int size>
1550 typename Target_x86_64<size>::Reloc_section*
1551 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1553 if (this->rela_dyn_ == NULL)
1555 gold_assert(layout != NULL);
1556 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1557 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1558 elfcpp::SHF_ALLOC, this->rela_dyn_,
1559 ORDER_DYNAMIC_RELOCS, false);
1561 return this->rela_dyn_;
1564 // Get the section to use for IRELATIVE relocs, creating it if
1565 // necessary. These go in .rela.dyn, but only after all other dynamic
1566 // relocations. They need to follow the other dynamic relocations so
1567 // that they can refer to global variables initialized by those
1568 // relocs.
1570 template<int size>
1571 typename Target_x86_64<size>::Reloc_section*
1572 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1574 if (this->rela_irelative_ == NULL)
1576 // Make sure we have already created the dynamic reloc section.
1577 this->rela_dyn_section(layout);
1578 this->rela_irelative_ = new Reloc_section(false);
1579 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1580 elfcpp::SHF_ALLOC, this->rela_irelative_,
1581 ORDER_DYNAMIC_RELOCS, false);
1582 gold_assert(this->rela_dyn_->output_section()
1583 == this->rela_irelative_->output_section());
1585 return this->rela_irelative_;
1588 // Record a target-specific program property from the .note.gnu.property
1589 // section.
1590 template<int size>
1591 void
1592 Target_x86_64<size>::record_gnu_property(
1593 unsigned int, unsigned int pr_type,
1594 size_t pr_datasz, const unsigned char* pr_data,
1595 const Object* object)
1597 uint32_t val = 0;
1599 switch (pr_type)
1601 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
1602 case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
1603 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
1604 case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
1605 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1606 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1607 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1608 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1609 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1610 if (pr_datasz != 4)
1612 gold_warning(_("%s: corrupt .note.gnu.property section "
1613 "(pr_datasz for property %d is not 4)"),
1614 object->name().c_str(), pr_type);
1615 return;
1617 val = elfcpp::Swap<32, false>::readval(pr_data);
1618 break;
1619 default:
1620 gold_warning(_("%s: unknown program property type 0x%x "
1621 "in .note.gnu.property section"),
1622 object->name().c_str(), pr_type);
1623 break;
1626 switch (pr_type)
1628 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
1629 this->object_isa_1_used_ |= val;
1630 break;
1631 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
1632 this->isa_1_needed_ |= val;
1633 break;
1634 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
1635 // If we see multiple feature props in one object, OR them together.
1636 this->object_feature_1_ |= val;
1637 break;
1638 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
1639 this->object_feature_2_used_ |= val;
1640 break;
1641 case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
1642 this->feature_2_needed_ |= val;
1643 break;
1647 // Merge the target-specific program properties from the current object.
1648 template<int size>
1649 void
1650 Target_x86_64<size>::merge_gnu_properties(const Object*)
1652 if (this->seen_first_object_)
1654 // If any object is missing the ISA_1_USED property, we must omit
1655 // it from the output file.
1656 if (this->object_isa_1_used_ == 0)
1657 this->isa_1_used_ = 0;
1658 else if (this->isa_1_used_ != 0)
1659 this->isa_1_used_ |= this->object_isa_1_used_;
1660 this->feature_1_ &= this->object_feature_1_;
1661 // If any object is missing the FEATURE_2_USED property, we must
1662 // omit it from the output file.
1663 if (this->object_feature_2_used_ == 0)
1664 this->feature_2_used_ = 0;
1665 else if (this->feature_2_used_ != 0)
1666 this->feature_2_used_ |= this->object_feature_2_used_;
1668 else
1670 this->isa_1_used_ = this->object_isa_1_used_;
1671 this->feature_1_ = this->object_feature_1_;
1672 this->feature_2_used_ = this->object_feature_2_used_;
1673 this->seen_first_object_ = true;
1675 this->object_isa_1_used_ = 0;
1676 this->object_feature_1_ = 0;
1677 this->object_feature_2_used_ = 0;
1680 static inline void
1681 add_property(Layout* layout, unsigned int pr_type, uint32_t val)
1683 unsigned char buf[4];
1684 elfcpp::Swap<32, false>::writeval(buf, val);
1685 layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
1688 // Finalize the target-specific program properties and add them back to
1689 // the layout.
1690 template<int size>
1691 void
1692 Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const
1694 if (this->isa_1_used_ != 0)
1695 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
1696 this->isa_1_used_);
1697 if (this->isa_1_needed_ != 0)
1698 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
1699 this->isa_1_needed_);
1700 if (this->feature_1_ != 0)
1701 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
1702 this->feature_1_);
1703 if (this->feature_2_used_ != 0)
1704 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
1705 this->feature_2_used_);
1706 if (this->feature_2_needed_ != 0)
1707 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
1708 this->feature_2_needed_);
1711 // Write the first three reserved words of the .got.plt section.
1712 // The remainder of the section is written while writing the PLT
1713 // in Output_data_plt_i386::do_write.
1715 void
1716 Output_data_got_plt_x86_64::do_write(Output_file* of)
1718 // The first entry in the GOT is the address of the .dynamic section
1719 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1720 // We saved space for them when we created the section in
1721 // Target_x86_64::got_section.
1722 const off_t got_file_offset = this->offset();
1723 gold_assert(this->data_size() >= 24);
1724 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1725 Output_section* dynamic = this->layout_->dynamic_section();
1726 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1727 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1728 memset(got_view + 8, 0, 16);
1729 of->write_output_view(got_file_offset, 24, got_view);
1732 // Initialize the PLT section.
1734 template<int size>
1735 void
1736 Output_data_plt_x86_64<size>::init(Layout* layout)
1738 this->rel_ = new Reloc_section(false);
1739 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1740 elfcpp::SHF_ALLOC, this->rel_,
1741 ORDER_DYNAMIC_PLT_RELOCS, false);
1744 template<int size>
1745 void
1746 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1748 os->set_entsize(this->get_plt_entry_size());
1751 // Add an entry to the PLT.
1753 template<int size>
1754 void
1755 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1756 Symbol* gsym)
1758 gold_assert(!gsym->has_plt_offset());
1760 unsigned int plt_index;
1761 off_t plt_offset;
1762 section_offset_type got_offset;
1764 unsigned int* pcount;
1765 unsigned int offset;
1766 unsigned int reserved;
1767 Output_section_data_build* got;
1768 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1769 && gsym->can_use_relative_reloc(false))
1771 pcount = &this->irelative_count_;
1772 offset = 0;
1773 reserved = 0;
1774 got = this->got_irelative_;
1776 else
1778 pcount = &this->count_;
1779 offset = 1;
1780 reserved = 3;
1781 got = this->got_plt_;
1784 if (!this->is_data_size_valid())
1786 // Note that when setting the PLT offset for a non-IRELATIVE
1787 // entry we skip the initial reserved PLT entry.
1788 plt_index = *pcount + offset;
1789 plt_offset = plt_index * this->get_plt_entry_size();
1791 ++*pcount;
1793 got_offset = (plt_index - offset + reserved) * 8;
1794 gold_assert(got_offset == got->current_data_size());
1796 // Every PLT entry needs a GOT entry which points back to the PLT
1797 // entry (this will be changed by the dynamic linker, normally
1798 // lazily when the function is called).
1799 got->set_current_data_size(got_offset + 8);
1801 else
1803 // FIXME: This is probably not correct for IRELATIVE relocs.
1805 // For incremental updates, find an available slot.
1806 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1807 this->get_plt_entry_size(), 0);
1808 if (plt_offset == -1)
1809 gold_fallback(_("out of patch space (PLT);"
1810 " relink with --incremental-full"));
1812 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1813 // can be calculated from the PLT index, adjusting for the three
1814 // reserved entries at the beginning of the GOT.
1815 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1816 got_offset = (plt_index - offset + reserved) * 8;
1819 gsym->set_plt_offset(plt_offset);
1821 // Every PLT entry needs a reloc.
1822 this->add_relocation(symtab, layout, gsym, got_offset);
1824 // Note that we don't need to save the symbol. The contents of the
1825 // PLT are independent of which symbols are used. The symbols only
1826 // appear in the relocations.
1829 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1830 // the PLT offset.
1832 template<int size>
1833 unsigned int
1834 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1835 Symbol_table* symtab,
1836 Layout* layout,
1837 Sized_relobj_file<size, false>* relobj,
1838 unsigned int local_sym_index)
1840 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1841 ++this->irelative_count_;
1843 section_offset_type got_offset = this->got_irelative_->current_data_size();
1845 // Every PLT entry needs a GOT entry which points back to the PLT
1846 // entry.
1847 this->got_irelative_->set_current_data_size(got_offset + 8);
1849 // Every PLT entry needs a reloc.
1850 Reloc_section* rela = this->rela_irelative(symtab, layout);
1851 rela->add_symbolless_local_addend(relobj, local_sym_index,
1852 elfcpp::R_X86_64_IRELATIVE,
1853 this->got_irelative_, got_offset, 0);
1855 return plt_offset;
1858 // Add the relocation for a PLT entry.
1860 template<int size>
1861 void
1862 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1863 Layout* layout,
1864 Symbol* gsym,
1865 unsigned int got_offset)
1867 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1868 && gsym->can_use_relative_reloc(false))
1870 Reloc_section* rela = this->rela_irelative(symtab, layout);
1871 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1872 this->got_irelative_, got_offset, 0);
1874 else
1876 gsym->set_needs_dynsym_entry();
1877 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1878 got_offset, 0);
1882 // Return where the TLSDESC relocations should go, creating it if
1883 // necessary. These follow the JUMP_SLOT relocations.
1885 template<int size>
1886 typename Output_data_plt_x86_64<size>::Reloc_section*
1887 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1889 if (this->tlsdesc_rel_ == NULL)
1891 this->tlsdesc_rel_ = new Reloc_section(false);
1892 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1893 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1894 ORDER_DYNAMIC_PLT_RELOCS, false);
1895 gold_assert(this->tlsdesc_rel_->output_section()
1896 == this->rel_->output_section());
1898 return this->tlsdesc_rel_;
1901 // Return where the IRELATIVE relocations should go in the PLT. These
1902 // follow the JUMP_SLOT and the TLSDESC relocations.
1904 template<int size>
1905 typename Output_data_plt_x86_64<size>::Reloc_section*
1906 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1907 Layout* layout)
1909 if (this->irelative_rel_ == NULL)
1911 // Make sure we have a place for the TLSDESC relocations, in
1912 // case we see any later on.
1913 this->rela_tlsdesc(layout);
1914 this->irelative_rel_ = new Reloc_section(false);
1915 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1916 elfcpp::SHF_ALLOC, this->irelative_rel_,
1917 ORDER_DYNAMIC_PLT_RELOCS, false);
1918 gold_assert(this->irelative_rel_->output_section()
1919 == this->rel_->output_section());
1921 if (parameters->doing_static_link())
1923 // A statically linked executable will only have a .rela.plt
1924 // section to hold R_X86_64_IRELATIVE relocs for
1925 // STT_GNU_IFUNC symbols. The library will use these
1926 // symbols to locate the IRELATIVE relocs at program startup
1927 // time.
1928 symtab->define_in_output_data("__rela_iplt_start", NULL,
1929 Symbol_table::PREDEFINED,
1930 this->irelative_rel_, 0, 0,
1931 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1932 elfcpp::STV_HIDDEN, 0, false, true);
1933 symtab->define_in_output_data("__rela_iplt_end", NULL,
1934 Symbol_table::PREDEFINED,
1935 this->irelative_rel_, 0, 0,
1936 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1937 elfcpp::STV_HIDDEN, 0, true, true);
1940 return this->irelative_rel_;
1943 // Return the PLT address to use for a global symbol.
1945 template<int size>
1946 uint64_t
1947 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1949 uint64_t offset = 0;
1950 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1951 && gsym->can_use_relative_reloc(false))
1952 offset = (this->count_ + 1) * this->get_plt_entry_size();
1953 return this->address() + offset + gsym->plt_offset();
1956 // Return the PLT address to use for a local symbol. These are always
1957 // IRELATIVE relocs.
1959 template<int size>
1960 uint64_t
1961 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1962 unsigned int r_sym)
1964 return (this->address()
1965 + (this->count_ + 1) * this->get_plt_entry_size()
1966 + object->local_plt_offset(r_sym));
1969 // Set the final size.
1970 template<int size>
1971 void
1972 Output_data_plt_x86_64<size>::set_final_data_size()
1974 // Number of regular and IFUNC PLT entries, plus the first entry.
1975 unsigned int count = this->count_ + this->irelative_count_ + 1;
1976 // Count the TLSDESC entry, if present.
1977 if (this->has_tlsdesc_entry())
1978 ++count;
1979 this->set_data_size(count * this->get_plt_entry_size());
1982 // The first entry in the PLT for an executable.
1984 template<int size>
1985 const unsigned char
1986 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1988 // From AMD64 ABI Draft 0.98, page 76
1989 0xff, 0x35, // pushq contents of memory address
1990 0, 0, 0, 0, // replaced with address of .got + 8
1991 0xff, 0x25, // jmp indirect
1992 0, 0, 0, 0, // replaced with address of .got + 16
1993 0x90, 0x90, 0x90, 0x90 // noop (x4)
1996 template<int size>
1997 void
1998 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1999 unsigned char* pov,
2000 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2001 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
2003 memcpy(pov, first_plt_entry, plt_entry_size);
2004 // We do a jmp relative to the PC at the end of this instruction.
2005 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2006 (got_address + 8
2007 - (plt_address + 6)));
2008 elfcpp::Swap<32, false>::writeval(pov + 8,
2009 (got_address + 16
2010 - (plt_address + 12)));
2013 // Subsequent entries in the PLT for an executable.
2015 template<int size>
2016 const unsigned char
2017 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
2019 // From AMD64 ABI Draft 0.98, page 76
2020 0xff, 0x25, // jmpq indirect
2021 0, 0, 0, 0, // replaced with address of symbol in .got
2022 0x68, // pushq immediate
2023 0, 0, 0, 0, // replaced with offset into relocation table
2024 0xe9, // jmpq relative
2025 0, 0, 0, 0 // replaced with offset to start of .plt
2028 template<int size>
2029 unsigned int
2030 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
2031 unsigned char* pov,
2032 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2033 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2034 unsigned int got_offset,
2035 unsigned int plt_offset,
2036 unsigned int plt_index)
2038 // Check PC-relative offset overflow in PLT entry.
2039 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2040 - (plt_address + plt_offset + 6));
2041 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2042 gold_error(_("PC-relative offset overflow in PLT entry %d"),
2043 plt_index + 1);
2045 memcpy(pov, plt_entry, plt_entry_size);
2046 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2047 plt_got_pcrel_offset);
2049 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
2050 elfcpp::Swap<32, false>::writeval(pov + 12,
2051 - (plt_offset + plt_entry_size));
2053 return 6;
2056 // The reserved TLSDESC entry in the PLT for an executable.
2058 template<int size>
2059 const unsigned char
2060 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
2062 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2063 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2064 0xff, 0x35, // pushq x(%rip)
2065 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2066 0xff, 0x25, // jmpq *y(%rip)
2067 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2068 0x0f, 0x1f, // nop
2069 0x40, 0
2072 template<int size>
2073 void
2074 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
2075 unsigned char* pov,
2076 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2077 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2078 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2079 unsigned int tlsdesc_got_offset,
2080 unsigned int plt_offset)
2082 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2083 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2084 (got_address + 8
2085 - (plt_address + plt_offset
2086 + 6)));
2087 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
2088 (got_base
2089 + tlsdesc_got_offset
2090 - (plt_address + plt_offset
2091 + 12)));
2094 // Return the APLT address to use for a global symbol (for -z bndplt).
2096 uint64_t
2097 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
2099 uint64_t offset = this->aplt_offset_;
2100 // Convert the PLT offset into an APLT offset.
2101 unsigned int plt_offset = gsym->plt_offset();
2102 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2103 && gsym->can_use_relative_reloc(false))
2104 offset += this->regular_count() * aplt_entry_size;
2105 else
2106 plt_offset -= plt_entry_size;
2107 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
2108 return this->address() + offset + plt_offset;
2111 // Return the PLT address to use for a local symbol. These are always
2112 // IRELATIVE relocs.
2114 uint64_t
2115 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
2116 unsigned int r_sym)
2118 // Convert the PLT offset into an APLT offset.
2119 const Sized_relobj_file<64, false>* sized_relobj =
2120 static_cast<const Sized_relobj_file<64, false>*>(object);
2121 const Symbol_value<64>* psymval = sized_relobj->local_symbol(r_sym);
2122 unsigned int plt_offset = ((object->local_plt_offset(r_sym)
2123 - (psymval->is_ifunc_symbol()
2124 ? 0 : plt_entry_size))
2125 / (plt_entry_size / aplt_entry_size));
2126 return (this->address()
2127 + this->aplt_offset_
2128 + this->regular_count() * aplt_entry_size
2129 + plt_offset);
2132 // Set the final size.
2133 void
2134 Output_data_plt_x86_64_bnd::set_final_data_size()
2136 // Number of regular and IFUNC PLT entries.
2137 unsigned int count = this->entry_count();
2138 // Count the first entry and the TLSDESC entry, if present.
2139 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2140 unsigned int plt_size = (count + extra) * plt_entry_size;
2141 // Offset of the APLT.
2142 this->aplt_offset_ = plt_size;
2143 // Size of the APLT.
2144 plt_size += count * aplt_entry_size;
2145 this->set_data_size(plt_size);
2148 // The first entry in the BND PLT.
2150 const unsigned char
2151 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
2153 // From AMD64 ABI Draft 0.98, page 76
2154 0xff, 0x35, // pushq contents of memory address
2155 0, 0, 0, 0, // replaced with address of .got + 8
2156 0xf2, 0xff, 0x25, // bnd jmp indirect
2157 0, 0, 0, 0, // replaced with address of .got + 16
2158 0x0f, 0x1f, 0x00 // nop
2161 void
2162 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
2163 unsigned char* pov,
2164 elfcpp::Elf_types<64>::Elf_Addr got_address,
2165 elfcpp::Elf_types<64>::Elf_Addr plt_address)
2167 memcpy(pov, first_plt_entry, plt_entry_size);
2168 // We do a jmp relative to the PC at the end of this instruction.
2169 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2170 (got_address + 8
2171 - (plt_address + 6)));
2172 elfcpp::Swap<32, false>::writeval(pov + 9,
2173 (got_address + 16
2174 - (plt_address + 13)));
2177 // Subsequent entries in the BND PLT.
2179 const unsigned char
2180 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
2182 // From AMD64 ABI Draft 0.99.8, page 139
2183 0x68, // pushq immediate
2184 0, 0, 0, 0, // replaced with offset into relocation table
2185 0xf2, 0xe9, // bnd jmpq relative
2186 0, 0, 0, 0, // replaced with offset to start of .plt
2187 0x0f, 0x1f, 0x44, 0, 0 // nop
2190 // Entries in the BND Additional PLT.
2192 const unsigned char
2193 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
2195 // From AMD64 ABI Draft 0.99.8, page 139
2196 0xf2, 0xff, 0x25, // bnd jmpq indirect
2197 0, 0, 0, 0, // replaced with address of symbol in .got
2198 0x90, // nop
2201 unsigned int
2202 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
2203 unsigned char* pov,
2204 elfcpp::Elf_types<64>::Elf_Addr,
2205 elfcpp::Elf_types<64>::Elf_Addr,
2206 unsigned int,
2207 unsigned int plt_offset,
2208 unsigned int plt_index)
2210 memcpy(pov, plt_entry, plt_entry_size);
2211 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
2212 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
2213 return 0;
2216 void
2217 Output_data_plt_x86_64_bnd::fill_aplt_entry(
2218 unsigned char* pov,
2219 elfcpp::Elf_types<64>::Elf_Addr got_address,
2220 elfcpp::Elf_types<64>::Elf_Addr plt_address,
2221 unsigned int got_offset,
2222 unsigned int plt_offset,
2223 unsigned int plt_index)
2225 // Check PC-relative offset overflow in PLT entry.
2226 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2227 - (plt_address + plt_offset + 7));
2228 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2229 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2230 plt_index + 1);
2232 memcpy(pov, aplt_entry, aplt_entry_size);
2233 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
2236 // The reserved TLSDESC entry in the PLT for an executable.
2238 const unsigned char
2239 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
2241 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2242 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2243 0xff, 0x35, // pushq x(%rip)
2244 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2245 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2246 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2247 0x0f, 0x1f, 0 // nop
2250 void
2251 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
2252 unsigned char* pov,
2253 elfcpp::Elf_types<64>::Elf_Addr got_address,
2254 elfcpp::Elf_types<64>::Elf_Addr plt_address,
2255 elfcpp::Elf_types<64>::Elf_Addr got_base,
2256 unsigned int tlsdesc_got_offset,
2257 unsigned int plt_offset)
2259 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2260 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2261 (got_address + 8
2262 - (plt_address + plt_offset
2263 + 6)));
2264 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
2265 (got_base
2266 + tlsdesc_got_offset
2267 - (plt_address + plt_offset
2268 + 13)));
2271 // Return the APLT address to use for a global symbol (for IBT).
2273 template<int size>
2274 uint64_t
2275 Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym)
2277 uint64_t offset = this->aplt_offset_;
2278 // Convert the PLT offset into an APLT offset.
2279 unsigned int plt_offset = gsym->plt_offset();
2280 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2281 && gsym->can_use_relative_reloc(false))
2282 offset += this->regular_count() * aplt_entry_size;
2283 else
2284 plt_offset -= plt_entry_size;
2285 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
2286 return this->address() + offset + plt_offset;
2289 // Return the PLT address to use for a local symbol. These are always
2290 // IRELATIVE relocs.
2292 template<int size>
2293 uint64_t
2294 Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object,
2295 unsigned int r_sym)
2297 // Convert the PLT offset into an APLT offset.
2298 const Sized_relobj_file<size, false>* sized_relobj =
2299 static_cast<const Sized_relobj_file<size, false>*>(object);
2300 const Symbol_value<size>* psymval = sized_relobj->local_symbol(r_sym);
2301 unsigned int plt_offset = ((object->local_plt_offset(r_sym)
2302 - (psymval->is_ifunc_symbol()
2303 ? 0 : plt_entry_size))
2304 / (plt_entry_size / aplt_entry_size));
2305 return (this->address()
2306 + this->aplt_offset_
2307 + this->regular_count() * aplt_entry_size
2308 + plt_offset);
2311 // Set the final size.
2313 template<int size>
2314 void
2315 Output_data_plt_x86_64_ibt<size>::set_final_data_size()
2317 // Number of regular and IFUNC PLT entries.
2318 unsigned int count = this->entry_count();
2319 // Count the first entry and the TLSDESC entry, if present.
2320 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
2321 unsigned int plt_size = (count + extra) * plt_entry_size;
2322 // Offset of the APLT.
2323 this->aplt_offset_ = plt_size;
2324 // Size of the APLT.
2325 plt_size += count * aplt_entry_size;
2326 this->set_data_size(plt_size);
2329 // The first entry in the IBT PLT.
2331 template<>
2332 const unsigned char
2333 Output_data_plt_x86_64_ibt<32>::first_plt_entry[plt_entry_size] =
2335 // MPX isn't supported for x32, so we don't need the BND prefix.
2336 // From AMD64 ABI Draft 0.98, page 76
2337 0xff, 0x35, // pushq contents of memory address
2338 0, 0, 0, 0, // replaced with address of .got + 8
2339 0xff, 0x25, // jmp indirect
2340 0, 0, 0, 0, // replaced with address of .got + 16
2341 0x90, 0x90, 0x90, 0x90 // noop (x4)
2344 template<>
2345 const unsigned char
2346 Output_data_plt_x86_64_ibt<64>::first_plt_entry[plt_entry_size] =
2348 // Use the BND prefix so that IBT is compatible with MPX.
2349 0xff, 0x35, // pushq contents of memory address
2350 0, 0, 0, 0, // replaced with address of .got + 8
2351 0xf2, 0xff, 0x25, // bnd jmp indirect
2352 0, 0, 0, 0, // replaced with address of .got + 16
2353 0x0f, 0x1f, 0x00 // nop
2356 template<int size>
2357 void
2358 Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry(
2359 unsigned char* pov,
2360 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2361 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
2363 // Offsets to the addresses needing relocation.
2364 const unsigned int roff1 = 2;
2365 const unsigned int roff2 = (size == 32) ? 8 : 9;
2367 memcpy(pov, first_plt_entry, plt_entry_size);
2368 // We do a jmp relative to the PC at the end of this instruction.
2369 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1,
2370 (got_address + 8
2371 - (plt_address + roff1 + 4)));
2372 elfcpp::Swap<32, false>::writeval(pov + roff2,
2373 (got_address + 16
2374 - (plt_address + roff2 + 4)));
2377 // Subsequent entries in the IBT PLT.
2379 template<>
2380 const unsigned char
2381 Output_data_plt_x86_64_ibt<32>::plt_entry[plt_entry_size] =
2383 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2384 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2385 0x68, // pushq immediate
2386 0, 0, 0, 0, // replaced with offset into relocation table
2387 0xe9, // jmpq relative
2388 0, 0, 0, 0, // replaced with offset to start of .plt
2389 0x90, 0x90 // nop
2392 template<>
2393 const unsigned char
2394 Output_data_plt_x86_64_ibt<64>::plt_entry[plt_entry_size] =
2396 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2397 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2398 0x68, // pushq immediate
2399 0, 0, 0, 0, // replaced with offset into relocation table
2400 0xf2, 0xe9, // bnd jmpq relative
2401 0, 0, 0, 0, // replaced with offset to start of .plt
2402 0x90 // nop
2405 // Entries in the IBT Additional PLT.
2407 template<>
2408 const unsigned char
2409 Output_data_plt_x86_64_ibt<32>::aplt_entry[aplt_entry_size] =
2411 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2412 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2413 0xff, 0x25, // jmpq indirect
2414 0, 0, 0, 0, // replaced with address of symbol in .got
2415 0x0f, 0x1f, 0x04, 0x00, // nop
2416 0x90, 0x90 // nop
2419 template<>
2420 const unsigned char
2421 Output_data_plt_x86_64_ibt<64>::aplt_entry[aplt_entry_size] =
2423 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2424 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2425 0xf2, 0xff, 0x25, // bnd jmpq indirect
2426 0, 0, 0, 0, // replaced with address of symbol in .got
2427 0x0f, 0x1f, 0x04, 0x00, // nop
2428 0x90, // nop
2431 template<int size>
2432 unsigned int
2433 Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry(
2434 unsigned char* pov,
2435 typename elfcpp::Elf_types<size>::Elf_Addr,
2436 typename elfcpp::Elf_types<size>::Elf_Addr,
2437 unsigned int,
2438 unsigned int plt_offset,
2439 unsigned int plt_index)
2441 // Offsets to the addresses needing relocation.
2442 const unsigned int roff1 = 5;
2443 const unsigned int roff2 = (size == 32) ? 10 : 11;
2445 memcpy(pov, plt_entry, plt_entry_size);
2446 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index);
2447 elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4));
2448 return 0;
2451 template<int size>
2452 void
2453 Output_data_plt_x86_64_ibt<size>::fill_aplt_entry(
2454 unsigned char* pov,
2455 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2456 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2457 unsigned int got_offset,
2458 unsigned int plt_offset,
2459 unsigned int plt_index)
2461 // Offset to the address needing relocation.
2462 const unsigned int roff = (size == 32) ? 6 : 7;
2464 // Check PC-relative offset overflow in PLT entry.
2465 uint64_t plt_got_pcrel_offset = (got_address + got_offset
2466 - (plt_address + plt_offset + roff + 4));
2467 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
2468 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2469 plt_index + 1);
2471 memcpy(pov, aplt_entry, aplt_entry_size);
2472 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset);
2475 // The reserved TLSDESC entry in the IBT PLT for an executable.
2477 template<int size>
2478 const unsigned char
2479 Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] =
2481 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2482 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2483 0xff, 0x35, // pushq x(%rip)
2484 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2485 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2486 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2487 0x0f, 0x1f, 0 // nop
2490 template<int size>
2491 void
2492 Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry(
2493 unsigned char* pov,
2494 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
2495 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
2496 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
2497 unsigned int tlsdesc_got_offset,
2498 unsigned int plt_offset)
2500 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
2501 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
2502 (got_address + 8
2503 - (plt_address + plt_offset
2504 + 6)));
2505 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
2506 (got_base
2507 + tlsdesc_got_offset
2508 - (plt_address + plt_offset
2509 + 13)));
2512 // The .eh_frame unwind information for the PLT.
2514 template<int size>
2515 const unsigned char
2516 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
2518 1, // CIE version.
2519 'z', // Augmentation: augmentation size included.
2520 'R', // Augmentation: FDE encoding included.
2521 '\0', // End of augmentation string.
2522 1, // Code alignment factor.
2523 0x78, // Data alignment factor.
2524 16, // Return address column.
2525 1, // Augmentation size.
2526 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
2527 | elfcpp::DW_EH_PE_sdata4),
2528 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2529 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2530 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2531 elfcpp::DW_CFA_nop
2534 template<int size>
2535 const unsigned char
2536 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2538 0, 0, 0, 0, // Replaced with offset to .plt.
2539 0, 0, 0, 0, // Replaced with size of .plt.
2540 0, // Augmentation size.
2541 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2542 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2543 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2544 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2545 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2546 11, // Block length.
2547 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2548 elfcpp::DW_OP_breg16, 0, // Push %rip.
2549 elfcpp::DW_OP_lit15, // Push 0xf.
2550 elfcpp::DW_OP_and, // & (%rip & 0xf).
2551 elfcpp::DW_OP_lit11, // Push 0xb.
2552 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2553 elfcpp::DW_OP_lit3, // Push 3.
2554 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2555 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2556 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2557 elfcpp::DW_CFA_nop,
2558 elfcpp::DW_CFA_nop,
2559 elfcpp::DW_CFA_nop
2562 // The .eh_frame unwind information for the BND PLT.
2563 const unsigned char
2564 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2566 0, 0, 0, 0, // Replaced with offset to .plt.
2567 0, 0, 0, 0, // Replaced with size of .plt.
2568 0, // Augmentation size.
2569 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2570 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2571 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2572 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2573 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2574 11, // Block length.
2575 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2576 elfcpp::DW_OP_breg16, 0, // Push %rip.
2577 elfcpp::DW_OP_lit15, // Push 0xf.
2578 elfcpp::DW_OP_and, // & (%rip & 0xf).
2579 elfcpp::DW_OP_lit5, // Push 5.
2580 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
2581 elfcpp::DW_OP_lit3, // Push 3.
2582 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
2583 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2584 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2585 elfcpp::DW_CFA_nop,
2586 elfcpp::DW_CFA_nop,
2587 elfcpp::DW_CFA_nop
2590 // The .eh_frame unwind information for the BND PLT.
2591 template<int size>
2592 const unsigned char
2593 Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2595 0, 0, 0, 0, // Replaced with offset to .plt.
2596 0, 0, 0, 0, // Replaced with size of .plt.
2597 0, // Augmentation size.
2598 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2599 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2600 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2601 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2602 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2603 11, // Block length.
2604 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2605 elfcpp::DW_OP_breg16, 0, // Push %rip.
2606 elfcpp::DW_OP_lit15, // Push 0xf.
2607 elfcpp::DW_OP_and, // & (%rip & 0xf).
2608 elfcpp::DW_OP_lit9, // Push 9.
2609 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9)
2610 elfcpp::DW_OP_lit3, // Push 3.
2611 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3)
2612 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2613 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2614 elfcpp::DW_CFA_nop,
2615 elfcpp::DW_CFA_nop,
2616 elfcpp::DW_CFA_nop
2619 // Write out the PLT. This uses the hand-coded instructions above,
2620 // and adjusts them as needed. This is specified by the AMD64 ABI.
2622 template<int size>
2623 void
2624 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2626 const off_t offset = this->offset();
2627 const section_size_type oview_size =
2628 convert_to_section_size_type(this->data_size());
2629 unsigned char* const oview = of->get_output_view(offset, oview_size);
2631 const off_t got_file_offset = this->got_plt_->offset();
2632 gold_assert(parameters->incremental_update()
2633 || (got_file_offset + this->got_plt_->data_size()
2634 == this->got_irelative_->offset()));
2635 const section_size_type got_size =
2636 convert_to_section_size_type(this->got_plt_->data_size()
2637 + this->got_irelative_->data_size());
2638 unsigned char* const got_view = of->get_output_view(got_file_offset,
2639 got_size);
2641 unsigned char* pov = oview;
2643 // The base address of the .plt section.
2644 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2645 // The base address of the .got section.
2646 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2647 // The base address of the PLT portion of the .got section,
2648 // which is where the GOT pointer will point, and where the
2649 // three reserved GOT entries are located.
2650 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2651 = this->got_plt_->address();
2653 this->fill_first_plt_entry(pov, got_address, plt_address);
2654 pov += this->get_plt_entry_size();
2656 // The first three entries in the GOT are reserved, and are written
2657 // by Output_data_got_plt_x86_64::do_write.
2658 unsigned char* got_pov = got_view + 24;
2660 unsigned int plt_offset = this->get_plt_entry_size();
2661 unsigned int got_offset = 24;
2662 const unsigned int count = this->count_ + this->irelative_count_;
2663 for (unsigned int plt_index = 0;
2664 plt_index < count;
2665 ++plt_index,
2666 pov += this->get_plt_entry_size(),
2667 got_pov += 8,
2668 plt_offset += this->get_plt_entry_size(),
2669 got_offset += 8)
2671 // Set and adjust the PLT entry itself.
2672 unsigned int lazy_offset = this->fill_plt_entry(pov,
2673 got_address, plt_address,
2674 got_offset, plt_offset,
2675 plt_index);
2677 // Set the entry in the GOT.
2678 elfcpp::Swap<64, false>::writeval(got_pov,
2679 plt_address + plt_offset + lazy_offset);
2682 if (this->has_tlsdesc_entry())
2684 // Set and adjust the reserved TLSDESC PLT entry.
2685 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2686 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2687 tlsdesc_got_offset, plt_offset);
2688 pov += this->get_plt_entry_size();
2691 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2692 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2694 of->write_output_view(offset, oview_size, oview);
2695 of->write_output_view(got_file_offset, got_size, got_view);
2698 // Write out the BND PLT.
2700 void
2701 Output_data_plt_x86_64_bnd::do_write(Output_file* of)
2703 const off_t offset = this->offset();
2704 const section_size_type oview_size =
2705 convert_to_section_size_type(this->data_size());
2706 unsigned char* const oview = of->get_output_view(offset, oview_size);
2708 Output_data_got<64, false>* got = this->got();
2709 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2710 Output_data_space* got_irelative = this->got_irelative();
2712 const off_t got_file_offset = got_plt->offset();
2713 gold_assert(parameters->incremental_update()
2714 || (got_file_offset + got_plt->data_size()
2715 == got_irelative->offset()));
2716 const section_size_type got_size =
2717 convert_to_section_size_type(got_plt->data_size()
2718 + got_irelative->data_size());
2719 unsigned char* const got_view = of->get_output_view(got_file_offset,
2720 got_size);
2722 unsigned char* pov = oview;
2724 // The base address of the .plt section.
2725 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2726 // The base address of the .got section.
2727 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2728 // The base address of the PLT portion of the .got section,
2729 // which is where the GOT pointer will point, and where the
2730 // three reserved GOT entries are located.
2731 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2733 this->fill_first_plt_entry(pov, got_address, plt_address);
2734 pov += plt_entry_size;
2736 // The first three entries in the GOT are reserved, and are written
2737 // by Output_data_got_plt_x86_64::do_write.
2738 unsigned char* got_pov = got_view + 24;
2740 unsigned int plt_offset = plt_entry_size;
2741 unsigned int got_offset = 24;
2742 const unsigned int count = this->entry_count();
2743 for (unsigned int plt_index = 0;
2744 plt_index < count;
2745 ++plt_index,
2746 pov += plt_entry_size,
2747 got_pov += 8,
2748 plt_offset += plt_entry_size,
2749 got_offset += 8)
2751 // Set and adjust the PLT entry itself.
2752 unsigned int lazy_offset = this->fill_plt_entry(pov,
2753 got_address, plt_address,
2754 got_offset, plt_offset,
2755 plt_index);
2757 // Set the entry in the GOT.
2758 elfcpp::Swap<64, false>::writeval(got_pov,
2759 plt_address + plt_offset + lazy_offset);
2762 if (this->has_tlsdesc_entry())
2764 // Set and adjust the reserved TLSDESC PLT entry.
2765 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2766 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2767 tlsdesc_got_offset, plt_offset);
2768 pov += this->get_plt_entry_size();
2769 plt_offset += plt_entry_size;
2772 // Write the additional PLT.
2773 got_offset = 24;
2774 for (unsigned int plt_index = 0;
2775 plt_index < count;
2776 ++plt_index,
2777 pov += aplt_entry_size,
2778 plt_offset += aplt_entry_size,
2779 got_offset += 8)
2781 // Set and adjust the APLT entry.
2782 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2783 plt_offset, plt_index);
2786 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2787 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2789 of->write_output_view(offset, oview_size, oview);
2790 of->write_output_view(got_file_offset, got_size, got_view);
2793 // Write out the IBT PLT.
2795 template<int size>
2796 void
2797 Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of)
2799 const off_t offset = this->offset();
2800 const section_size_type oview_size =
2801 convert_to_section_size_type(this->data_size());
2802 unsigned char* const oview = of->get_output_view(offset, oview_size);
2804 Output_data_got<64, false>* got = this->got();
2805 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2806 Output_data_space* got_irelative = this->got_irelative();
2808 const off_t got_file_offset = got_plt->offset();
2809 gold_assert(parameters->incremental_update()
2810 || (got_file_offset + got_plt->data_size()
2811 == got_irelative->offset()));
2812 const section_size_type got_size =
2813 convert_to_section_size_type(got_plt->data_size()
2814 + got_irelative->data_size());
2815 unsigned char* const got_view = of->get_output_view(got_file_offset,
2816 got_size);
2818 unsigned char* pov = oview;
2820 // The base address of the .plt section.
2821 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2822 // The base address of the .got section.
2823 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2824 // The base address of the PLT portion of the .got section,
2825 // which is where the GOT pointer will point, and where the
2826 // three reserved GOT entries are located.
2827 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2829 this->fill_first_plt_entry(pov, got_address, plt_address);
2830 pov += plt_entry_size;
2832 // The first three entries in the GOT are reserved, and are written
2833 // by Output_data_got_plt_x86_64::do_write.
2834 unsigned char* got_pov = got_view + 24;
2836 unsigned int plt_offset = plt_entry_size;
2837 unsigned int got_offset = 24;
2838 const unsigned int count = this->entry_count();
2839 for (unsigned int plt_index = 0;
2840 plt_index < count;
2841 ++plt_index,
2842 pov += plt_entry_size,
2843 got_pov += 8,
2844 plt_offset += plt_entry_size,
2845 got_offset += 8)
2847 // Set and adjust the PLT entry itself.
2848 unsigned int lazy_offset = this->fill_plt_entry(pov,
2849 got_address, plt_address,
2850 got_offset, plt_offset,
2851 plt_index);
2853 // Set the entry in the GOT.
2854 elfcpp::Swap<64, false>::writeval(got_pov,
2855 plt_address + plt_offset + lazy_offset);
2858 if (this->has_tlsdesc_entry())
2860 // Set and adjust the reserved TLSDESC PLT entry.
2861 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2862 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2863 tlsdesc_got_offset, plt_offset);
2864 pov += this->get_plt_entry_size();
2865 plt_offset += plt_entry_size;
2868 // Write the additional PLT.
2869 got_offset = 24;
2870 for (unsigned int plt_index = 0;
2871 plt_index < count;
2872 ++plt_index,
2873 pov += aplt_entry_size,
2874 plt_offset += aplt_entry_size,
2875 got_offset += 8)
2877 // Set and adjust the APLT entry.
2878 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2879 plt_offset, plt_index);
2882 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2883 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2885 of->write_output_view(offset, oview_size, oview);
2886 of->write_output_view(got_file_offset, got_size, got_view);
2889 // Create the PLT section.
2891 template<int size>
2892 void
2893 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2895 if (this->plt_ == NULL)
2897 // Create the GOT sections first.
2898 this->got_section(symtab, layout);
2900 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2901 this->got_irelative_);
2903 // Add unwind information if requested.
2904 if (parameters->options().ld_generated_unwind_info())
2905 this->plt_->add_eh_frame(layout);
2907 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2908 (elfcpp::SHF_ALLOC
2909 | elfcpp::SHF_EXECINSTR),
2910 this->plt_, ORDER_PLT, false);
2912 // Make the sh_info field of .rela.plt point to .plt.
2913 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2914 rela_plt_os->set_info_section(this->plt_->output_section());
2918 template<>
2919 Output_data_plt_x86_64<32>*
2920 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2921 Output_data_got<64, false>* got,
2922 Output_data_got_plt_x86_64* got_plt,
2923 Output_data_space* got_irelative)
2925 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2926 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2927 got_irelative);
2928 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2929 got_irelative);
2932 template<>
2933 Output_data_plt_x86_64<64>*
2934 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2935 Output_data_got<64, false>* got,
2936 Output_data_got_plt_x86_64* got_plt,
2937 Output_data_space* got_irelative)
2939 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2940 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2941 got_irelative);
2942 else if (parameters->options().bndplt())
2943 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2944 got_irelative);
2945 else
2946 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2947 got_irelative);
2950 template<>
2951 Output_data_plt_x86_64<32>*
2952 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2953 Output_data_got<64, false>* got,
2954 Output_data_got_plt_x86_64* got_plt,
2955 Output_data_space* got_irelative,
2956 unsigned int plt_count)
2958 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2959 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
2960 got_irelative, plt_count);
2961 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2962 got_irelative, plt_count);
2965 template<>
2966 Output_data_plt_x86_64<64>*
2967 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2968 Output_data_got<64, false>* got,
2969 Output_data_got_plt_x86_64* got_plt,
2970 Output_data_space* got_irelative,
2971 unsigned int plt_count)
2973 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
2974 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
2975 got_irelative, plt_count);
2976 else if (parameters->options().bndplt())
2977 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2978 got_irelative, plt_count);
2979 else
2980 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2981 got_irelative,
2982 plt_count);
2985 // Return the section for TLSDESC relocations.
2987 template<int size>
2988 typename Target_x86_64<size>::Reloc_section*
2989 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2991 return this->plt_section()->rela_tlsdesc(layout);
2994 // Create a PLT entry for a global symbol.
2996 template<int size>
2997 void
2998 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2999 Symbol* gsym)
3001 if (gsym->has_plt_offset())
3002 return;
3004 if (this->plt_ == NULL)
3005 this->make_plt_section(symtab, layout);
3007 this->plt_->add_entry(symtab, layout, gsym);
3010 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
3012 template<int size>
3013 void
3014 Target_x86_64<size>::make_local_ifunc_plt_entry(
3015 Symbol_table* symtab, Layout* layout,
3016 Sized_relobj_file<size, false>* relobj,
3017 unsigned int local_sym_index)
3019 if (relobj->local_has_plt_offset(local_sym_index))
3020 return;
3021 if (this->plt_ == NULL)
3022 this->make_plt_section(symtab, layout);
3023 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
3024 relobj,
3025 local_sym_index);
3026 relobj->set_local_plt_offset(local_sym_index, plt_offset);
3029 // Return the number of entries in the PLT.
3031 template<int size>
3032 unsigned int
3033 Target_x86_64<size>::plt_entry_count() const
3035 if (this->plt_ == NULL)
3036 return 0;
3037 return this->plt_->entry_count();
3040 // Return the offset of the first non-reserved PLT entry.
3042 template<int size>
3043 unsigned int
3044 Target_x86_64<size>::first_plt_entry_offset() const
3046 if (this->plt_ == NULL)
3047 return 0;
3048 return this->plt_->first_plt_entry_offset();
3051 // Return the size of each PLT entry.
3053 template<int size>
3054 unsigned int
3055 Target_x86_64<size>::plt_entry_size() const
3057 if (this->plt_ == NULL)
3058 return 0;
3059 return this->plt_->get_plt_entry_size();
3062 // Create the GOT and PLT sections for an incremental update.
3064 template<int size>
3065 Output_data_got_base*
3066 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
3067 Layout* layout,
3068 unsigned int got_count,
3069 unsigned int plt_count)
3071 gold_assert(this->got_ == NULL);
3073 this->got_ = new Output_data_got<64, false>(got_count * 8);
3074 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3075 (elfcpp::SHF_ALLOC
3076 | elfcpp::SHF_WRITE),
3077 this->got_, ORDER_RELRO_LAST,
3078 true);
3080 // Add the three reserved entries.
3081 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
3082 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3083 (elfcpp::SHF_ALLOC
3084 | elfcpp::SHF_WRITE),
3085 this->got_plt_, ORDER_NON_RELRO_FIRST,
3086 false);
3088 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3089 this->global_offset_table_ =
3090 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3091 Symbol_table::PREDEFINED,
3092 this->got_plt_,
3093 0, 0, elfcpp::STT_OBJECT,
3094 elfcpp::STB_LOCAL,
3095 elfcpp::STV_HIDDEN, 0,
3096 false, false);
3098 // If there are any TLSDESC relocations, they get GOT entries in
3099 // .got.plt after the jump slot entries.
3100 // FIXME: Get the count for TLSDESC entries.
3101 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
3102 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3103 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3104 this->got_tlsdesc_,
3105 ORDER_NON_RELRO_FIRST, false);
3107 // If there are any IRELATIVE relocations, they get GOT entries in
3108 // .got.plt after the jump slot and TLSDESC entries.
3109 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
3110 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3111 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3112 this->got_irelative_,
3113 ORDER_NON_RELRO_FIRST, false);
3115 // Create the PLT section.
3116 this->plt_ = this->make_data_plt(layout, this->got_,
3117 this->got_plt_,
3118 this->got_irelative_,
3119 plt_count);
3121 // Add unwind information if requested.
3122 if (parameters->options().ld_generated_unwind_info())
3123 this->plt_->add_eh_frame(layout);
3125 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3126 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
3127 this->plt_, ORDER_PLT, false);
3129 // Make the sh_info field of .rela.plt point to .plt.
3130 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
3131 rela_plt_os->set_info_section(this->plt_->output_section());
3133 // Create the rela_dyn section.
3134 this->rela_dyn_section(layout);
3136 return this->got_;
3139 // Reserve a GOT entry for a local symbol, and regenerate any
3140 // necessary dynamic relocations.
3142 template<int size>
3143 void
3144 Target_x86_64<size>::reserve_local_got_entry(
3145 unsigned int got_index,
3146 Sized_relobj<size, false>* obj,
3147 unsigned int r_sym,
3148 unsigned int got_type)
3150 unsigned int got_offset = got_index * 8;
3151 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
3153 this->got_->reserve_local(got_index, obj, r_sym, got_type);
3154 switch (got_type)
3156 case GOT_TYPE_STANDARD:
3157 if (parameters->options().output_is_position_independent())
3158 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
3159 this->got_, got_offset, 0, false);
3160 break;
3161 case GOT_TYPE_TLS_OFFSET:
3162 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
3163 this->got_, got_offset, 0);
3164 break;
3165 case GOT_TYPE_TLS_PAIR:
3166 this->got_->reserve_slot(got_index + 1);
3167 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
3168 this->got_, got_offset, 0);
3169 break;
3170 case GOT_TYPE_TLS_DESC:
3171 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
3172 // this->got_->reserve_slot(got_index + 1);
3173 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3174 // this->got_, got_offset, 0);
3175 break;
3176 default:
3177 gold_unreachable();
3181 // Reserve a GOT entry for a global symbol, and regenerate any
3182 // necessary dynamic relocations.
3184 template<int size>
3185 void
3186 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
3187 Symbol* gsym,
3188 unsigned int got_type)
3190 unsigned int got_offset = got_index * 8;
3191 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
3193 this->got_->reserve_global(got_index, gsym, got_type);
3194 switch (got_type)
3196 case GOT_TYPE_STANDARD:
3197 if (!gsym->final_value_is_known())
3199 if (gsym->is_from_dynobj()
3200 || gsym->is_undefined()
3201 || gsym->is_preemptible()
3202 || gsym->type() == elfcpp::STT_GNU_IFUNC)
3203 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
3204 this->got_, got_offset, 0);
3205 else
3206 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3207 this->got_, got_offset, 0, false);
3209 break;
3210 case GOT_TYPE_TLS_OFFSET:
3211 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
3212 this->got_, got_offset, 0, false);
3213 break;
3214 case GOT_TYPE_TLS_PAIR:
3215 this->got_->reserve_slot(got_index + 1);
3216 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
3217 this->got_, got_offset, 0, false);
3218 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
3219 this->got_, got_offset + 8, 0, false);
3220 break;
3221 case GOT_TYPE_TLS_DESC:
3222 this->got_->reserve_slot(got_index + 1);
3223 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
3224 this->got_, got_offset, 0, false);
3225 break;
3226 default:
3227 gold_unreachable();
3231 // Register an existing PLT entry for a global symbol.
3233 template<int size>
3234 void
3235 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
3236 Layout* layout,
3237 unsigned int plt_index,
3238 Symbol* gsym)
3240 gold_assert(this->plt_ != NULL);
3241 gold_assert(!gsym->has_plt_offset());
3243 this->plt_->reserve_slot(plt_index);
3245 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
3247 unsigned int got_offset = (plt_index + 3) * 8;
3248 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
3251 // Force a COPY relocation for a given symbol.
3253 template<int size>
3254 void
3255 Target_x86_64<size>::emit_copy_reloc(
3256 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
3258 this->copy_relocs_.emit_copy_reloc(symtab,
3259 symtab->get_sized_symbol<size>(sym),
3261 offset,
3262 this->rela_dyn_section(NULL));
3265 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3267 template<int size>
3268 void
3269 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
3270 Layout* layout)
3272 if (this->tls_base_symbol_defined_)
3273 return;
3275 Output_segment* tls_segment = layout->tls_segment();
3276 if (tls_segment != NULL)
3278 bool is_exec = parameters->options().output_is_executable();
3279 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
3280 Symbol_table::PREDEFINED,
3281 tls_segment, 0, 0,
3282 elfcpp::STT_TLS,
3283 elfcpp::STB_LOCAL,
3284 elfcpp::STV_HIDDEN, 0,
3285 (is_exec
3286 ? Symbol::SEGMENT_END
3287 : Symbol::SEGMENT_START),
3288 true);
3290 this->tls_base_symbol_defined_ = true;
3293 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3295 template<int size>
3296 void
3297 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
3298 Layout* layout)
3300 if (this->plt_ == NULL)
3301 this->make_plt_section(symtab, layout);
3303 if (!this->plt_->has_tlsdesc_entry())
3305 // Allocate the TLSDESC_GOT entry.
3306 Output_data_got<64, false>* got = this->got_section(symtab, layout);
3307 unsigned int got_offset = got->add_constant(0);
3309 // Allocate the TLSDESC_PLT entry.
3310 this->plt_->reserve_tlsdesc_entry(got_offset);
3314 // Create a GOT entry for the TLS module index.
3316 template<int size>
3317 unsigned int
3318 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3319 Sized_relobj_file<size, false>* object)
3321 if (this->got_mod_index_offset_ == -1U)
3323 gold_assert(symtab != NULL && layout != NULL && object != NULL);
3324 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
3325 Output_data_got<64, false>* got = this->got_section(symtab, layout);
3326 unsigned int got_offset = got->add_constant(0);
3327 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
3328 got_offset, 0);
3329 got->add_constant(0);
3330 this->got_mod_index_offset_ = got_offset;
3332 return this->got_mod_index_offset_;
3335 // Optimize the TLS relocation type based on what we know about the
3336 // symbol. IS_FINAL is true if the final address of this symbol is
3337 // known at link time.
3339 template<int size>
3340 tls::Tls_optimization
3341 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
3343 // If we are generating a shared library, then we can't do anything
3344 // in the linker.
3345 if (parameters->options().shared())
3346 return tls::TLSOPT_NONE;
3348 switch (r_type)
3350 case elfcpp::R_X86_64_TLSGD:
3351 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3352 case elfcpp::R_X86_64_TLSDESC_CALL:
3353 // These are General-Dynamic which permits fully general TLS
3354 // access. Since we know that we are generating an executable,
3355 // we can convert this to Initial-Exec. If we also know that
3356 // this is a local symbol, we can further switch to Local-Exec.
3357 if (is_final)
3358 return tls::TLSOPT_TO_LE;
3359 return tls::TLSOPT_TO_IE;
3361 case elfcpp::R_X86_64_TLSLD:
3362 // This is Local-Dynamic, which refers to a local symbol in the
3363 // dynamic TLS block. Since we know that we generating an
3364 // executable, we can switch to Local-Exec.
3365 return tls::TLSOPT_TO_LE;
3367 case elfcpp::R_X86_64_DTPOFF32:
3368 case elfcpp::R_X86_64_DTPOFF64:
3369 // Another Local-Dynamic reloc.
3370 return tls::TLSOPT_TO_LE;
3372 case elfcpp::R_X86_64_GOTTPOFF:
3373 // These are Initial-Exec relocs which get the thread offset
3374 // from the GOT. If we know that we are linking against the
3375 // local symbol, we can switch to Local-Exec, which links the
3376 // thread offset into the instruction.
3377 if (is_final)
3378 return tls::TLSOPT_TO_LE;
3379 return tls::TLSOPT_NONE;
3381 case elfcpp::R_X86_64_TPOFF32:
3382 // When we already have Local-Exec, there is nothing further we
3383 // can do.
3384 return tls::TLSOPT_NONE;
3386 default:
3387 gold_unreachable();
3391 // Get the Reference_flags for a particular relocation.
3393 template<int size>
3395 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
3397 switch (r_type)
3399 case elfcpp::R_X86_64_NONE:
3400 case elfcpp::R_X86_64_GNU_VTINHERIT:
3401 case elfcpp::R_X86_64_GNU_VTENTRY:
3402 case elfcpp::R_X86_64_GOTPC32:
3403 case elfcpp::R_X86_64_GOTPC64:
3404 // No symbol reference.
3405 return 0;
3407 case elfcpp::R_X86_64_64:
3408 case elfcpp::R_X86_64_32:
3409 case elfcpp::R_X86_64_32S:
3410 case elfcpp::R_X86_64_16:
3411 case elfcpp::R_X86_64_8:
3412 return Symbol::ABSOLUTE_REF;
3414 case elfcpp::R_X86_64_PC64:
3415 case elfcpp::R_X86_64_PC32:
3416 case elfcpp::R_X86_64_PC32_BND:
3417 case elfcpp::R_X86_64_PC16:
3418 case elfcpp::R_X86_64_PC8:
3419 case elfcpp::R_X86_64_GOTOFF64:
3420 return Symbol::RELATIVE_REF;
3422 case elfcpp::R_X86_64_PLT32:
3423 case elfcpp::R_X86_64_PLT32_BND:
3424 case elfcpp::R_X86_64_PLTOFF64:
3425 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
3427 case elfcpp::R_X86_64_GOT64:
3428 case elfcpp::R_X86_64_GOT32:
3429 case elfcpp::R_X86_64_GOTPCREL64:
3430 case elfcpp::R_X86_64_GOTPCREL:
3431 case elfcpp::R_X86_64_GOTPCRELX:
3432 case elfcpp::R_X86_64_REX_GOTPCRELX:
3433 case elfcpp::R_X86_64_GOTPLT64:
3434 // Absolute in GOT.
3435 return Symbol::ABSOLUTE_REF;
3437 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3438 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3439 case elfcpp::R_X86_64_TLSDESC_CALL:
3440 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3441 case elfcpp::R_X86_64_DTPOFF32:
3442 case elfcpp::R_X86_64_DTPOFF64:
3443 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3444 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3445 return Symbol::TLS_REF;
3447 case elfcpp::R_X86_64_COPY:
3448 case elfcpp::R_X86_64_GLOB_DAT:
3449 case elfcpp::R_X86_64_JUMP_SLOT:
3450 case elfcpp::R_X86_64_RELATIVE:
3451 case elfcpp::R_X86_64_IRELATIVE:
3452 case elfcpp::R_X86_64_TPOFF64:
3453 case elfcpp::R_X86_64_DTPMOD64:
3454 case elfcpp::R_X86_64_TLSDESC:
3455 case elfcpp::R_X86_64_SIZE32:
3456 case elfcpp::R_X86_64_SIZE64:
3457 default:
3458 // Not expected. We will give an error later.
3459 return 0;
3463 // Report an unsupported relocation against a local symbol.
3465 template<int size>
3466 void
3467 Target_x86_64<size>::Scan::unsupported_reloc_local(
3468 Sized_relobj_file<size, false>* object,
3469 unsigned int r_type)
3471 gold_error(_("%s: unsupported reloc %u against local symbol"),
3472 object->name().c_str(), r_type);
3475 // We are about to emit a dynamic relocation of type R_TYPE. If the
3476 // dynamic linker does not support it, issue an error. The GNU linker
3477 // only issues a non-PIC error for an allocated read-only section.
3478 // Here we know the section is allocated, but we don't know that it is
3479 // read-only. But we check for all the relocation types which the
3480 // glibc dynamic linker supports, so it seems appropriate to issue an
3481 // error even if the section is not read-only. If GSYM is not NULL,
3482 // it is the symbol the relocation is against; if it is NULL, the
3483 // relocation is against a local symbol.
3485 template<int size>
3486 void
3487 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
3488 Symbol* gsym)
3490 switch (r_type)
3492 // These are the relocation types supported by glibc for x86_64
3493 // which should always work.
3494 case elfcpp::R_X86_64_RELATIVE:
3495 case elfcpp::R_X86_64_IRELATIVE:
3496 case elfcpp::R_X86_64_GLOB_DAT:
3497 case elfcpp::R_X86_64_JUMP_SLOT:
3498 case elfcpp::R_X86_64_DTPMOD64:
3499 case elfcpp::R_X86_64_DTPOFF64:
3500 case elfcpp::R_X86_64_TPOFF64:
3501 case elfcpp::R_X86_64_64:
3502 case elfcpp::R_X86_64_COPY:
3503 return;
3505 // glibc supports these reloc types, but they can overflow.
3506 case elfcpp::R_X86_64_PC32:
3507 case elfcpp::R_X86_64_PC32_BND:
3508 // A PC relative reference is OK against a local symbol or if
3509 // the symbol is defined locally.
3510 if (gsym == NULL
3511 || (!gsym->is_from_dynobj()
3512 && !gsym->is_undefined()
3513 && !gsym->is_preemptible()))
3514 return;
3515 // Fall through.
3516 case elfcpp::R_X86_64_32:
3517 // R_X86_64_32 is OK for x32.
3518 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3519 return;
3520 if (this->issued_non_pic_error_)
3521 return;
3522 gold_assert(parameters->options().output_is_position_independent());
3523 if (gsym == NULL)
3524 object->error(_("requires dynamic R_X86_64_32 reloc which may "
3525 "overflow at runtime; recompile with -fPIC"));
3526 else
3528 const char *r_name;
3529 switch (r_type)
3531 case elfcpp::R_X86_64_32:
3532 r_name = "R_X86_64_32";
3533 break;
3534 case elfcpp::R_X86_64_PC32:
3535 r_name = "R_X86_64_PC32";
3536 break;
3537 case elfcpp::R_X86_64_PC32_BND:
3538 r_name = "R_X86_64_PC32_BND";
3539 break;
3540 default:
3541 gold_unreachable();
3542 break;
3544 object->error(_("requires dynamic %s reloc against '%s' "
3545 "which may overflow at runtime; recompile "
3546 "with -fPIC"),
3547 r_name, gsym->name());
3549 this->issued_non_pic_error_ = true;
3550 return;
3552 default:
3553 // This prevents us from issuing more than one error per reloc
3554 // section. But we can still wind up issuing more than one
3555 // error per object file.
3556 if (this->issued_non_pic_error_)
3557 return;
3558 gold_assert(parameters->options().output_is_position_independent());
3559 object->error(_("requires unsupported dynamic reloc %u; "
3560 "recompile with -fPIC"),
3561 r_type);
3562 this->issued_non_pic_error_ = true;
3563 return;
3565 case elfcpp::R_X86_64_NONE:
3566 gold_unreachable();
3570 // Return whether we need to make a PLT entry for a relocation of the
3571 // given type against a STT_GNU_IFUNC symbol.
3573 template<int size>
3574 bool
3575 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
3576 Sized_relobj_file<size, false>* object,
3577 unsigned int r_type)
3579 int flags = Scan::get_reference_flags(r_type);
3580 if (flags & Symbol::TLS_REF)
3581 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3582 object->name().c_str(), r_type);
3583 return flags != 0;
3586 // Scan a relocation for a local symbol.
3588 template<int size>
3589 inline void
3590 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
3591 Layout* layout,
3592 Target_x86_64<size>* target,
3593 Sized_relobj_file<size, false>* object,
3594 unsigned int data_shndx,
3595 Output_section* output_section,
3596 const elfcpp::Rela<size, false>& reloc,
3597 unsigned int r_type,
3598 const elfcpp::Sym<size, false>& lsym,
3599 bool is_discarded)
3601 if (is_discarded)
3602 return;
3604 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3605 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3606 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3608 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3609 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3612 switch (r_type)
3614 case elfcpp::R_X86_64_NONE:
3615 case elfcpp::R_X86_64_GNU_VTINHERIT:
3616 case elfcpp::R_X86_64_GNU_VTENTRY:
3617 break;
3619 case elfcpp::R_X86_64_64:
3620 // If building a shared library (or a position-independent
3621 // executable), we need to create a dynamic relocation for this
3622 // location. The relocation applied at link time will apply the
3623 // link-time value, so we flag the location with an
3624 // R_X86_64_RELATIVE relocation so the dynamic loader can
3625 // relocate it easily.
3626 if (parameters->options().output_is_position_independent())
3628 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3629 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3630 rela_dyn->add_local_relative(object, r_sym,
3631 (size == 32
3632 ? elfcpp::R_X86_64_RELATIVE64
3633 : elfcpp::R_X86_64_RELATIVE),
3634 output_section, data_shndx,
3635 reloc.get_r_offset(),
3636 reloc.get_r_addend(), is_ifunc);
3638 break;
3640 case elfcpp::R_X86_64_32:
3641 case elfcpp::R_X86_64_32S:
3642 case elfcpp::R_X86_64_16:
3643 case elfcpp::R_X86_64_8:
3644 // If building a shared library (or a position-independent
3645 // executable), we need to create a dynamic relocation for this
3646 // location. We can't use an R_X86_64_RELATIVE relocation
3647 // because that is always a 64-bit relocation.
3648 if (parameters->options().output_is_position_independent())
3650 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3651 if (size == 32 && r_type == elfcpp::R_X86_64_32)
3653 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3654 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3655 rela_dyn->add_local_relative(object, r_sym,
3656 elfcpp::R_X86_64_RELATIVE,
3657 output_section, data_shndx,
3658 reloc.get_r_offset(),
3659 reloc.get_r_addend(), is_ifunc);
3660 break;
3663 this->check_non_pic(object, r_type, NULL);
3665 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3666 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3667 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3668 rela_dyn->add_local(object, r_sym, r_type, output_section,
3669 data_shndx, reloc.get_r_offset(),
3670 reloc.get_r_addend());
3671 else
3673 gold_assert(lsym.get_st_value() == 0);
3674 unsigned int shndx = lsym.get_st_shndx();
3675 bool is_ordinary;
3676 shndx = object->adjust_sym_shndx(r_sym, shndx,
3677 &is_ordinary);
3678 if (!is_ordinary)
3679 object->error(_("section symbol %u has bad shndx %u"),
3680 r_sym, shndx);
3681 else
3682 rela_dyn->add_local_section(object, shndx,
3683 r_type, output_section,
3684 data_shndx, reloc.get_r_offset(),
3685 reloc.get_r_addend());
3688 break;
3690 case elfcpp::R_X86_64_PC64:
3691 case elfcpp::R_X86_64_PC32:
3692 case elfcpp::R_X86_64_PC32_BND:
3693 case elfcpp::R_X86_64_PC16:
3694 case elfcpp::R_X86_64_PC8:
3695 break;
3697 case elfcpp::R_X86_64_PLT32:
3698 case elfcpp::R_X86_64_PLT32_BND:
3699 // Since we know this is a local symbol, we can handle this as a
3700 // PC32 reloc.
3701 break;
3703 case elfcpp::R_X86_64_GOTPC32:
3704 case elfcpp::R_X86_64_GOTOFF64:
3705 case elfcpp::R_X86_64_GOTPC64:
3706 case elfcpp::R_X86_64_PLTOFF64:
3707 // We need a GOT section.
3708 target->got_section(symtab, layout);
3709 // For PLTOFF64, we'd normally want a PLT section, but since we
3710 // know this is a local symbol, no PLT is needed.
3711 break;
3713 case elfcpp::R_X86_64_GOT64:
3714 case elfcpp::R_X86_64_GOT32:
3715 case elfcpp::R_X86_64_GOTPCREL64:
3716 case elfcpp::R_X86_64_GOTPCREL:
3717 case elfcpp::R_X86_64_GOTPCRELX:
3718 case elfcpp::R_X86_64_REX_GOTPCRELX:
3719 case elfcpp::R_X86_64_GOTPLT64:
3721 // The symbol requires a GOT section.
3722 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3724 // If the relocation symbol isn't IFUNC,
3725 // and is local, then we will convert
3726 // mov foo@GOTPCREL(%rip), %reg
3727 // to lea foo(%rip), %reg.
3728 // in Relocate::relocate.
3729 if (!parameters->incremental()
3730 && (r_type == elfcpp::R_X86_64_GOTPCREL
3731 || r_type == elfcpp::R_X86_64_GOTPCRELX
3732 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3733 && reloc.get_r_offset() >= 2
3734 && !is_ifunc)
3736 section_size_type stype;
3737 const unsigned char* view = object->section_contents(data_shndx,
3738 &stype, true);
3739 if (view[reloc.get_r_offset() - 2] == 0x8b)
3740 break;
3743 // The symbol requires a GOT entry.
3744 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3746 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3747 // lets function pointers compare correctly with shared
3748 // libraries. Otherwise we would need an IRELATIVE reloc.
3749 bool is_new;
3750 if (is_ifunc)
3751 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3752 else
3753 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3754 if (is_new)
3756 // If we are generating a shared object, we need to add a
3757 // dynamic relocation for this symbol's GOT entry.
3758 if (parameters->options().output_is_position_independent())
3760 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3761 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3762 if (r_type != elfcpp::R_X86_64_GOT32)
3764 unsigned int got_offset =
3765 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3766 rela_dyn->add_local_relative(object, r_sym,
3767 elfcpp::R_X86_64_RELATIVE,
3768 got, got_offset, 0, is_ifunc);
3770 else
3772 this->check_non_pic(object, r_type, NULL);
3774 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3775 rela_dyn->add_local(
3776 object, r_sym, r_type, got,
3777 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3781 // For GOTPLT64, we'd normally want a PLT section, but since
3782 // we know this is a local symbol, no PLT is needed.
3784 break;
3786 case elfcpp::R_X86_64_COPY:
3787 case elfcpp::R_X86_64_GLOB_DAT:
3788 case elfcpp::R_X86_64_JUMP_SLOT:
3789 case elfcpp::R_X86_64_RELATIVE:
3790 case elfcpp::R_X86_64_IRELATIVE:
3791 // These are outstanding tls relocs, which are unexpected when linking
3792 case elfcpp::R_X86_64_TPOFF64:
3793 case elfcpp::R_X86_64_DTPMOD64:
3794 case elfcpp::R_X86_64_TLSDESC:
3795 gold_error(_("%s: unexpected reloc %u in object file"),
3796 object->name().c_str(), r_type);
3797 break;
3799 // These are initial tls relocs, which are expected when linking
3800 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3801 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3802 case elfcpp::R_X86_64_TLSDESC_CALL:
3803 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3804 case elfcpp::R_X86_64_DTPOFF32:
3805 case elfcpp::R_X86_64_DTPOFF64:
3806 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3807 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3809 bool output_is_shared = parameters->options().shared();
3810 const tls::Tls_optimization optimized_type
3811 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3812 r_type);
3813 switch (r_type)
3815 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3816 if (optimized_type == tls::TLSOPT_NONE)
3818 // Create a pair of GOT entries for the module index and
3819 // dtv-relative offset.
3820 Output_data_got<64, false>* got
3821 = target->got_section(symtab, layout);
3822 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3823 unsigned int shndx = lsym.get_st_shndx();
3824 bool is_ordinary;
3825 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3826 if (!is_ordinary)
3827 object->error(_("local symbol %u has bad shndx %u"),
3828 r_sym, shndx);
3829 else
3830 got->add_local_pair_with_rel(object, r_sym,
3831 shndx,
3832 GOT_TYPE_TLS_PAIR,
3833 target->rela_dyn_section(layout),
3834 elfcpp::R_X86_64_DTPMOD64);
3836 else if (optimized_type != tls::TLSOPT_TO_LE)
3837 unsupported_reloc_local(object, r_type);
3838 break;
3840 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3841 target->define_tls_base_symbol(symtab, layout);
3842 if (optimized_type == tls::TLSOPT_NONE)
3844 // Create reserved PLT and GOT entries for the resolver.
3845 target->reserve_tlsdesc_entries(symtab, layout);
3847 // Generate a double GOT entry with an
3848 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3849 // is resolved lazily, so the GOT entry needs to be in
3850 // an area in .got.plt, not .got. Call got_section to
3851 // make sure the section has been created.
3852 target->got_section(symtab, layout);
3853 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3854 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3855 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3857 unsigned int got_offset = got->add_constant(0);
3858 got->add_constant(0);
3859 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3860 got_offset);
3861 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3862 // We store the arguments we need in a vector, and
3863 // use the index into the vector as the parameter
3864 // to pass to the target specific routines.
3865 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3866 void* arg = reinterpret_cast<void*>(intarg);
3867 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3868 got, got_offset, 0);
3871 else if (optimized_type != tls::TLSOPT_TO_LE)
3872 unsupported_reloc_local(object, r_type);
3873 break;
3875 case elfcpp::R_X86_64_TLSDESC_CALL:
3876 break;
3878 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3879 if (optimized_type == tls::TLSOPT_NONE)
3881 // Create a GOT entry for the module index.
3882 target->got_mod_index_entry(symtab, layout, object);
3884 else if (optimized_type != tls::TLSOPT_TO_LE)
3885 unsupported_reloc_local(object, r_type);
3886 break;
3888 case elfcpp::R_X86_64_DTPOFF32:
3889 case elfcpp::R_X86_64_DTPOFF64:
3890 break;
3892 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3893 layout->set_has_static_tls();
3894 if (optimized_type == tls::TLSOPT_NONE)
3896 // Create a GOT entry for the tp-relative offset.
3897 Output_data_got<64, false>* got
3898 = target->got_section(symtab, layout);
3899 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3900 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3901 target->rela_dyn_section(layout),
3902 elfcpp::R_X86_64_TPOFF64);
3904 else if (optimized_type != tls::TLSOPT_TO_LE)
3905 unsupported_reloc_local(object, r_type);
3906 break;
3908 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3909 layout->set_has_static_tls();
3910 if (output_is_shared)
3911 unsupported_reloc_local(object, r_type);
3912 break;
3914 default:
3915 gold_unreachable();
3918 break;
3920 case elfcpp::R_X86_64_SIZE32:
3921 case elfcpp::R_X86_64_SIZE64:
3922 default:
3923 gold_error(_("%s: unsupported reloc %u against local symbol"),
3924 object->name().c_str(), r_type);
3925 break;
3930 // Report an unsupported relocation against a global symbol.
3932 template<int size>
3933 void
3934 Target_x86_64<size>::Scan::unsupported_reloc_global(
3935 Sized_relobj_file<size, false>* object,
3936 unsigned int r_type,
3937 Symbol* gsym)
3939 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3940 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3943 // Returns true if this relocation type could be that of a function pointer.
3944 template<int size>
3945 inline bool
3946 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3947 Sized_relobj_file<size, false>* src_obj,
3948 unsigned int src_indx,
3949 unsigned int r_offset,
3950 unsigned int r_type)
3952 switch (r_type)
3954 case elfcpp::R_X86_64_64:
3955 case elfcpp::R_X86_64_32:
3956 case elfcpp::R_X86_64_32S:
3957 case elfcpp::R_X86_64_16:
3958 case elfcpp::R_X86_64_8:
3959 case elfcpp::R_X86_64_GOT64:
3960 case elfcpp::R_X86_64_GOT32:
3961 case elfcpp::R_X86_64_GOTPCREL64:
3962 case elfcpp::R_X86_64_GOTPCREL:
3963 case elfcpp::R_X86_64_GOTPCRELX:
3964 case elfcpp::R_X86_64_REX_GOTPCRELX:
3965 case elfcpp::R_X86_64_GOTPLT64:
3967 return true;
3969 case elfcpp::R_X86_64_PC32:
3971 // This relocation may be used both for function calls and
3972 // for taking address of a function. We distinguish between
3973 // them by checking the opcodes.
3974 uint64_t sh_flags = src_obj->section_flags(src_indx);
3975 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3976 if (is_executable)
3978 section_size_type stype;
3979 const unsigned char* view = src_obj->section_contents(src_indx,
3980 &stype,
3981 true);
3983 // call
3984 if (r_offset >= 1
3985 && view[r_offset - 1] == 0xe8)
3986 return false;
3988 // jmp
3989 if (r_offset >= 1
3990 && view[r_offset - 1] == 0xe9)
3991 return false;
3993 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3994 if (r_offset >= 2
3995 && view[r_offset - 2] == 0x0f
3996 && view[r_offset - 1] >= 0x80
3997 && view[r_offset - 1] <= 0x8f)
3998 return false;
4001 // Be conservative and treat all others as function pointers.
4002 return true;
4005 return false;
4008 // For safe ICF, scan a relocation for a local symbol to check if it
4009 // corresponds to a function pointer being taken. In that case mark
4010 // the function whose pointer was taken as not foldable.
4012 template<int size>
4013 inline bool
4014 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
4015 Symbol_table* ,
4016 Layout* ,
4017 Target_x86_64<size>* ,
4018 Sized_relobj_file<size, false>* src_obj,
4019 unsigned int src_indx,
4020 Output_section* ,
4021 const elfcpp::Rela<size, false>& reloc,
4022 unsigned int r_type,
4023 const elfcpp::Sym<size, false>&)
4025 return possible_function_pointer_reloc(src_obj, src_indx,
4026 reloc.get_r_offset(), r_type);
4029 // For safe ICF, scan a relocation for a global symbol to check if it
4030 // corresponds to a function pointer being taken. In that case mark
4031 // the function whose pointer was taken as not foldable.
4033 template<int size>
4034 inline bool
4035 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
4036 Symbol_table*,
4037 Layout* ,
4038 Target_x86_64<size>* ,
4039 Sized_relobj_file<size, false>* src_obj,
4040 unsigned int src_indx,
4041 Output_section* ,
4042 const elfcpp::Rela<size, false>& reloc,
4043 unsigned int r_type,
4044 Symbol*)
4046 return possible_function_pointer_reloc(src_obj, src_indx,
4047 reloc.get_r_offset(), r_type);
4050 // Scan a relocation for a global symbol.
4052 template<int size>
4053 inline void
4054 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
4055 Layout* layout,
4056 Target_x86_64<size>* target,
4057 Sized_relobj_file<size, false>* object,
4058 unsigned int data_shndx,
4059 Output_section* output_section,
4060 const elfcpp::Rela<size, false>& reloc,
4061 unsigned int r_type,
4062 Symbol* gsym)
4064 // A STT_GNU_IFUNC symbol may require a PLT entry.
4065 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4066 && this->reloc_needs_plt_for_ifunc(object, r_type))
4067 target->make_plt_entry(symtab, layout, gsym);
4069 switch (r_type)
4071 case elfcpp::R_X86_64_NONE:
4072 case elfcpp::R_X86_64_GNU_VTINHERIT:
4073 case elfcpp::R_X86_64_GNU_VTENTRY:
4074 break;
4076 case elfcpp::R_X86_64_64:
4077 case elfcpp::R_X86_64_32:
4078 case elfcpp::R_X86_64_32S:
4079 case elfcpp::R_X86_64_16:
4080 case elfcpp::R_X86_64_8:
4082 // Make a PLT entry if necessary.
4083 if (gsym->needs_plt_entry())
4085 target->make_plt_entry(symtab, layout, gsym);
4086 // Since this is not a PC-relative relocation, we may be
4087 // taking the address of a function. In that case we need to
4088 // set the entry in the dynamic symbol table to the address of
4089 // the PLT entry.
4090 if (gsym->is_from_dynobj() && !parameters->options().shared())
4091 gsym->set_needs_dynsym_value();
4093 // Make a dynamic relocation if necessary.
4094 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4096 if (!parameters->options().output_is_position_independent()
4097 && gsym->may_need_copy_reloc())
4099 target->copy_reloc(symtab, layout, object,
4100 data_shndx, output_section, gsym, reloc);
4102 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
4103 || (size == 32 && r_type == elfcpp::R_X86_64_32))
4104 && gsym->type() == elfcpp::STT_GNU_IFUNC
4105 && gsym->can_use_relative_reloc(false)
4106 && !gsym->is_from_dynobj()
4107 && !gsym->is_undefined()
4108 && !gsym->is_preemptible())
4110 // Use an IRELATIVE reloc for a locally defined
4111 // STT_GNU_IFUNC symbol. This makes a function
4112 // address in a PIE executable match the address in a
4113 // shared library that it links against.
4114 Reloc_section* rela_dyn =
4115 target->rela_irelative_section(layout);
4116 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
4117 rela_dyn->add_symbolless_global_addend(gsym, r_type,
4118 output_section, object,
4119 data_shndx,
4120 reloc.get_r_offset(),
4121 reloc.get_r_addend());
4123 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
4124 || (size == 32 && r_type == elfcpp::R_X86_64_32))
4125 && gsym->can_use_relative_reloc(false))
4127 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4128 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
4129 output_section, object,
4130 data_shndx,
4131 reloc.get_r_offset(),
4132 reloc.get_r_addend(), false);
4134 else
4136 this->check_non_pic(object, r_type, gsym);
4137 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4138 rela_dyn->add_global(gsym, r_type, output_section, object,
4139 data_shndx, reloc.get_r_offset(),
4140 reloc.get_r_addend());
4144 break;
4146 case elfcpp::R_X86_64_PC64:
4147 case elfcpp::R_X86_64_PC32:
4148 case elfcpp::R_X86_64_PC32_BND:
4149 case elfcpp::R_X86_64_PC16:
4150 case elfcpp::R_X86_64_PC8:
4152 // Make a PLT entry if necessary.
4153 if (gsym->needs_plt_entry())
4154 target->make_plt_entry(symtab, layout, gsym);
4155 // Make a dynamic relocation if necessary.
4156 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4158 if (parameters->options().output_is_executable()
4159 && gsym->may_need_copy_reloc())
4161 target->copy_reloc(symtab, layout, object,
4162 data_shndx, output_section, gsym, reloc);
4164 else
4166 this->check_non_pic(object, r_type, gsym);
4167 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4168 rela_dyn->add_global(gsym, r_type, output_section, object,
4169 data_shndx, reloc.get_r_offset(),
4170 reloc.get_r_addend());
4174 break;
4176 case elfcpp::R_X86_64_GOT64:
4177 case elfcpp::R_X86_64_GOT32:
4178 case elfcpp::R_X86_64_GOTPCREL64:
4179 case elfcpp::R_X86_64_GOTPCREL:
4180 case elfcpp::R_X86_64_GOTPCRELX:
4181 case elfcpp::R_X86_64_REX_GOTPCRELX:
4182 case elfcpp::R_X86_64_GOTPLT64:
4184 // The symbol requires a GOT entry.
4185 Output_data_got<64, false>* got = target->got_section(symtab, layout);
4187 // If we convert this from
4188 // mov foo@GOTPCREL(%rip), %reg
4189 // to lea foo(%rip), %reg.
4190 // OR
4191 // if we convert
4192 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
4193 // (callq|jmpq) foo
4194 // in Relocate::relocate, then there is nothing to do here.
4195 // We cannot make these optimizations in incremental linking mode,
4196 // because we look at the opcode to decide whether or not to make
4197 // change, and during an incremental update, the change may have
4198 // already been applied.
4200 Lazy_view<size> view(object, data_shndx);
4201 size_t r_offset = reloc.get_r_offset();
4202 if (!parameters->incremental()
4203 && r_offset >= 2
4204 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4205 r_offset, &view))
4206 break;
4208 if (!parameters->incremental()
4209 && r_offset >= 2
4210 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
4211 r_offset,
4212 &view))
4213 break;
4215 if (gsym->final_value_is_known())
4217 // For a STT_GNU_IFUNC symbol we want the PLT address.
4218 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4219 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4220 else
4221 got->add_global(gsym, GOT_TYPE_STANDARD);
4223 else
4225 // If this symbol is not fully resolved, we need to add a
4226 // dynamic relocation for it.
4227 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4229 // Use a GLOB_DAT rather than a RELATIVE reloc if:
4231 // 1) The symbol may be defined in some other module.
4233 // 2) We are building a shared library and this is a
4234 // protected symbol; using GLOB_DAT means that the dynamic
4235 // linker can use the address of the PLT in the main
4236 // executable when appropriate so that function address
4237 // comparisons work.
4239 // 3) This is a STT_GNU_IFUNC symbol in position dependent
4240 // code, again so that function address comparisons work.
4241 if (gsym->is_from_dynobj()
4242 || gsym->is_undefined()
4243 || gsym->is_preemptible()
4244 || (gsym->visibility() == elfcpp::STV_PROTECTED
4245 && parameters->options().shared())
4246 || (gsym->type() == elfcpp::STT_GNU_IFUNC
4247 && parameters->options().output_is_position_independent()))
4248 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
4249 elfcpp::R_X86_64_GLOB_DAT);
4250 else
4252 // For a STT_GNU_IFUNC symbol we want to write the PLT
4253 // offset into the GOT, so that function pointer
4254 // comparisons work correctly.
4255 bool is_new;
4256 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
4257 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
4258 else
4260 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4261 // Tell the dynamic linker to use the PLT address
4262 // when resolving relocations.
4263 if (gsym->is_from_dynobj()
4264 && !parameters->options().shared())
4265 gsym->set_needs_dynsym_value();
4267 if (is_new)
4269 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
4270 rela_dyn->add_global_relative(gsym,
4271 elfcpp::R_X86_64_RELATIVE,
4272 got, got_off, 0, false);
4277 break;
4279 case elfcpp::R_X86_64_PLT32:
4280 case elfcpp::R_X86_64_PLT32_BND:
4281 // If the symbol is fully resolved, this is just a PC32 reloc.
4282 // Otherwise we need a PLT entry.
4283 if (gsym->final_value_is_known())
4284 break;
4285 // If building a shared library, we can also skip the PLT entry
4286 // if the symbol is defined in the output file and is protected
4287 // or hidden.
4288 if (gsym->is_defined()
4289 && !gsym->is_from_dynobj()
4290 && !gsym->is_preemptible())
4291 break;
4292 target->make_plt_entry(symtab, layout, gsym);
4293 break;
4295 case elfcpp::R_X86_64_GOTPC32:
4296 case elfcpp::R_X86_64_GOTOFF64:
4297 case elfcpp::R_X86_64_GOTPC64:
4298 case elfcpp::R_X86_64_PLTOFF64:
4299 // We need a GOT section.
4300 target->got_section(symtab, layout);
4301 // For PLTOFF64, we also need a PLT entry (but only if the
4302 // symbol is not fully resolved).
4303 if (r_type == elfcpp::R_X86_64_PLTOFF64
4304 && !gsym->final_value_is_known())
4305 target->make_plt_entry(symtab, layout, gsym);
4306 break;
4308 case elfcpp::R_X86_64_COPY:
4309 case elfcpp::R_X86_64_GLOB_DAT:
4310 case elfcpp::R_X86_64_JUMP_SLOT:
4311 case elfcpp::R_X86_64_RELATIVE:
4312 case elfcpp::R_X86_64_IRELATIVE:
4313 // These are outstanding tls relocs, which are unexpected when linking
4314 case elfcpp::R_X86_64_TPOFF64:
4315 case elfcpp::R_X86_64_DTPMOD64:
4316 case elfcpp::R_X86_64_TLSDESC:
4317 gold_error(_("%s: unexpected reloc %u in object file"),
4318 object->name().c_str(), r_type);
4319 break;
4321 // These are initial tls relocs, which are expected for global()
4322 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4323 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4324 case elfcpp::R_X86_64_TLSDESC_CALL:
4325 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4326 case elfcpp::R_X86_64_DTPOFF32:
4327 case elfcpp::R_X86_64_DTPOFF64:
4328 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4329 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4331 // For the Initial-Exec model, we can treat undef symbols as final
4332 // when building an executable.
4333 const bool is_final = (gsym->final_value_is_known() ||
4334 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
4335 gsym->is_undefined() &&
4336 parameters->options().output_is_executable()));
4337 const tls::Tls_optimization optimized_type
4338 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4339 switch (r_type)
4341 case elfcpp::R_X86_64_TLSGD: // General-dynamic
4342 if (optimized_type == tls::TLSOPT_NONE)
4344 // Create a pair of GOT entries for the module index and
4345 // dtv-relative offset.
4346 Output_data_got<64, false>* got
4347 = target->got_section(symtab, layout);
4348 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
4349 target->rela_dyn_section(layout),
4350 elfcpp::R_X86_64_DTPMOD64,
4351 elfcpp::R_X86_64_DTPOFF64);
4353 else if (optimized_type == tls::TLSOPT_TO_IE)
4355 // Create a GOT entry for the tp-relative offset.
4356 Output_data_got<64, false>* got
4357 = target->got_section(symtab, layout);
4358 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4359 target->rela_dyn_section(layout),
4360 elfcpp::R_X86_64_TPOFF64);
4362 else if (optimized_type != tls::TLSOPT_TO_LE)
4363 unsupported_reloc_global(object, r_type, gsym);
4364 break;
4366 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
4367 target->define_tls_base_symbol(symtab, layout);
4368 if (optimized_type == tls::TLSOPT_NONE)
4370 // Create reserved PLT and GOT entries for the resolver.
4371 target->reserve_tlsdesc_entries(symtab, layout);
4373 // Create a double GOT entry with an R_X86_64_TLSDESC
4374 // reloc. The R_X86_64_TLSDESC reloc is resolved
4375 // lazily, so the GOT entry needs to be in an area in
4376 // .got.plt, not .got. Call got_section to make sure
4377 // the section has been created.
4378 target->got_section(symtab, layout);
4379 Output_data_got<64, false>* got = target->got_tlsdesc_section();
4380 Reloc_section* rt = target->rela_tlsdesc_section(layout);
4381 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
4382 elfcpp::R_X86_64_TLSDESC, 0);
4384 else if (optimized_type == tls::TLSOPT_TO_IE)
4386 // Create a GOT entry for the tp-relative offset.
4387 Output_data_got<64, false>* got
4388 = target->got_section(symtab, layout);
4389 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4390 target->rela_dyn_section(layout),
4391 elfcpp::R_X86_64_TPOFF64);
4393 else if (optimized_type != tls::TLSOPT_TO_LE)
4394 unsupported_reloc_global(object, r_type, gsym);
4395 break;
4397 case elfcpp::R_X86_64_TLSDESC_CALL:
4398 break;
4400 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4401 if (optimized_type == tls::TLSOPT_NONE)
4403 // Create a GOT entry for the module index.
4404 target->got_mod_index_entry(symtab, layout, object);
4406 else if (optimized_type != tls::TLSOPT_TO_LE)
4407 unsupported_reloc_global(object, r_type, gsym);
4408 break;
4410 case elfcpp::R_X86_64_DTPOFF32:
4411 case elfcpp::R_X86_64_DTPOFF64:
4412 break;
4414 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4415 layout->set_has_static_tls();
4416 if (optimized_type == tls::TLSOPT_NONE)
4418 // Create a GOT entry for the tp-relative offset.
4419 Output_data_got<64, false>* got
4420 = target->got_section(symtab, layout);
4421 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4422 target->rela_dyn_section(layout),
4423 elfcpp::R_X86_64_TPOFF64);
4425 else if (optimized_type != tls::TLSOPT_TO_LE)
4426 unsupported_reloc_global(object, r_type, gsym);
4427 break;
4429 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4430 layout->set_has_static_tls();
4431 if (parameters->options().shared())
4432 unsupported_reloc_global(object, r_type, gsym);
4433 break;
4435 default:
4436 gold_unreachable();
4439 break;
4441 case elfcpp::R_X86_64_SIZE32:
4442 case elfcpp::R_X86_64_SIZE64:
4443 default:
4444 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4445 object->name().c_str(), r_type,
4446 gsym->demangled_name().c_str());
4447 break;
4451 template<int size>
4452 void
4453 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
4454 Layout* layout,
4455 Sized_relobj_file<size, false>* object,
4456 unsigned int data_shndx,
4457 unsigned int sh_type,
4458 const unsigned char* prelocs,
4459 size_t reloc_count,
4460 Output_section* output_section,
4461 bool needs_special_offset_handling,
4462 size_t local_symbol_count,
4463 const unsigned char* plocal_symbols)
4465 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4466 Classify_reloc;
4468 if (sh_type == elfcpp::SHT_REL)
4470 return;
4473 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
4474 Classify_reloc>(
4475 symtab,
4476 layout,
4477 this,
4478 object,
4479 data_shndx,
4480 prelocs,
4481 reloc_count,
4482 output_section,
4483 needs_special_offset_handling,
4484 local_symbol_count,
4485 plocal_symbols);
4488 // Scan relocations for a section.
4490 template<int size>
4491 void
4492 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
4493 Layout* layout,
4494 Sized_relobj_file<size, false>* object,
4495 unsigned int data_shndx,
4496 unsigned int sh_type,
4497 const unsigned char* prelocs,
4498 size_t reloc_count,
4499 Output_section* output_section,
4500 bool needs_special_offset_handling,
4501 size_t local_symbol_count,
4502 const unsigned char* plocal_symbols)
4504 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
4505 Classify_reloc;
4507 if (sh_type == elfcpp::SHT_REL)
4509 gold_error(_("%s: unsupported REL reloc section"),
4510 object->name().c_str());
4511 return;
4514 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
4515 symtab,
4516 layout,
4517 this,
4518 object,
4519 data_shndx,
4520 prelocs,
4521 reloc_count,
4522 output_section,
4523 needs_special_offset_handling,
4524 local_symbol_count,
4525 plocal_symbols);
4528 // Finalize the sections.
4530 template<int size>
4531 void
4532 Target_x86_64<size>::do_finalize_sections(
4533 Layout* layout,
4534 const Input_objects*,
4535 Symbol_table* symtab)
4537 const Reloc_section* rel_plt = (this->plt_ == NULL
4538 ? NULL
4539 : this->plt_->rela_plt());
4540 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4541 this->rela_dyn_, true, false);
4543 // Fill in some more dynamic tags.
4544 Output_data_dynamic* const odyn = layout->dynamic_data();
4545 if (odyn != NULL)
4547 if (this->plt_ != NULL
4548 && this->plt_->output_section() != NULL
4549 && this->plt_->has_tlsdesc_entry())
4551 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
4552 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
4553 this->got_->finalize_data_size();
4554 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
4555 this->plt_, plt_offset);
4556 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
4557 this->got_, got_offset);
4561 // Emit any relocs we saved in an attempt to avoid generating COPY
4562 // relocs.
4563 if (this->copy_relocs_.any_saved_relocs())
4564 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4566 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4567 // the .got.plt section.
4568 Symbol* sym = this->global_offset_table_;
4569 if (sym != NULL)
4571 uint64_t data_size = this->got_plt_->current_data_size();
4572 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4575 if (parameters->doing_static_link()
4576 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4578 // If linking statically, make sure that the __rela_iplt symbols
4579 // were defined if necessary, even if we didn't create a PLT.
4580 static const Define_symbol_in_segment syms[] =
4583 "__rela_iplt_start", // name
4584 elfcpp::PT_LOAD, // segment_type
4585 elfcpp::PF_W, // segment_flags_set
4586 elfcpp::PF(0), // segment_flags_clear
4587 0, // value
4588 0, // size
4589 elfcpp::STT_NOTYPE, // type
4590 elfcpp::STB_GLOBAL, // binding
4591 elfcpp::STV_HIDDEN, // visibility
4592 0, // nonvis
4593 Symbol::SEGMENT_START, // offset_from_base
4594 true // only_if_ref
4597 "__rela_iplt_end", // name
4598 elfcpp::PT_LOAD, // segment_type
4599 elfcpp::PF_W, // segment_flags_set
4600 elfcpp::PF(0), // segment_flags_clear
4601 0, // value
4602 0, // size
4603 elfcpp::STT_NOTYPE, // type
4604 elfcpp::STB_GLOBAL, // binding
4605 elfcpp::STV_HIDDEN, // visibility
4606 0, // nonvis
4607 Symbol::SEGMENT_START, // offset_from_base
4608 true // only_if_ref
4612 symtab->define_symbols(layout, 2, syms,
4613 layout->script_options()->saw_sections_clause());
4617 // For x32, we need to handle PC-relative relocations using full 64-bit
4618 // arithmetic, so that we can detect relocation overflows properly.
4619 // This class overrides the pcrela32_check methods from the defaults in
4620 // Relocate_functions in reloc.h.
4622 template<int size>
4623 class X86_64_relocate_functions : public Relocate_functions<size, false>
4625 public:
4626 typedef Relocate_functions<size, false> Base;
4628 // Do a simple PC relative relocation with the addend in the
4629 // relocation.
4630 static inline typename Base::Reloc_status
4631 pcrela32_check(unsigned char* view,
4632 typename elfcpp::Elf_types<64>::Elf_Addr value,
4633 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4634 typename elfcpp::Elf_types<64>::Elf_Addr address)
4636 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4637 Valtype* wv = reinterpret_cast<Valtype*>(view);
4638 value = value + addend - address;
4639 elfcpp::Swap<32, false>::writeval(wv, value);
4640 return (Bits<32>::has_overflow(value)
4641 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4644 // Do a simple PC relative relocation with a Symbol_value with the
4645 // addend in the relocation.
4646 static inline typename Base::Reloc_status
4647 pcrela32_check(unsigned char* view,
4648 const Sized_relobj_file<size, false>* object,
4649 const Symbol_value<size>* psymval,
4650 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4651 typename elfcpp::Elf_types<64>::Elf_Addr address)
4653 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4654 Valtype* wv = reinterpret_cast<Valtype*>(view);
4655 typename elfcpp::Elf_types<64>::Elf_Addr value;
4656 if (addend >= 0)
4657 value = psymval->value(object, addend);
4658 else
4660 // For negative addends, get the symbol value without
4661 // the addend, then add the addend using 64-bit arithmetic.
4662 value = psymval->value(object, 0);
4663 value += addend;
4665 value -= address;
4666 elfcpp::Swap<32, false>::writeval(wv, value);
4667 return (Bits<32>::has_overflow(value)
4668 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4672 // Perform a relocation.
4674 template<int size>
4675 inline bool
4676 Target_x86_64<size>::Relocate::relocate(
4677 const Relocate_info<size, false>* relinfo,
4678 unsigned int,
4679 Target_x86_64<size>* target,
4680 Output_section*,
4681 size_t relnum,
4682 const unsigned char* preloc,
4683 const Sized_symbol<size>* gsym,
4684 const Symbol_value<size>* psymval,
4685 unsigned char* view,
4686 typename elfcpp::Elf_types<size>::Elf_Addr address,
4687 section_size_type view_size)
4689 typedef X86_64_relocate_functions<size> Reloc_funcs;
4690 const elfcpp::Rela<size, false> rela(preloc);
4691 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4693 if (this->skip_call_tls_get_addr_)
4695 if ((r_type != elfcpp::R_X86_64_PLT32
4696 && r_type != elfcpp::R_X86_64_GOTPCREL
4697 && r_type != elfcpp::R_X86_64_GOTPCRELX
4698 && r_type != elfcpp::R_X86_64_PLT32_BND
4699 && r_type != elfcpp::R_X86_64_PC32_BND
4700 && r_type != elfcpp::R_X86_64_PC32)
4701 || gsym == NULL
4702 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4704 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4705 _("missing expected TLS relocation"));
4706 this->skip_call_tls_get_addr_ = false;
4708 else
4710 this->skip_call_tls_get_addr_ = false;
4711 return false;
4715 if (view == NULL)
4716 return true;
4718 const Sized_relobj_file<size, false>* object = relinfo->object;
4720 // Pick the value to use for symbols defined in the PLT.
4721 Symbol_value<size> symval;
4722 if (gsym != NULL
4723 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4725 symval.set_output_value(target->plt_address_for_global(gsym));
4726 psymval = &symval;
4728 else if (gsym == NULL && psymval->is_ifunc_symbol())
4730 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4731 if (object->local_has_plt_offset(r_sym))
4733 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4734 psymval = &symval;
4738 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4740 // Get the GOT offset if needed.
4741 // The GOT pointer points to the end of the GOT section.
4742 // We need to subtract the size of the GOT section to get
4743 // the actual offset to use in the relocation.
4744 bool have_got_offset = false;
4745 // Since the actual offset is always negative, we use signed int to
4746 // support 64-bit GOT relocations.
4747 int got_offset = 0;
4748 switch (r_type)
4750 case elfcpp::R_X86_64_GOT32:
4751 case elfcpp::R_X86_64_GOT64:
4752 case elfcpp::R_X86_64_GOTPLT64:
4753 case elfcpp::R_X86_64_GOTPCREL64:
4754 if (gsym != NULL)
4756 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4757 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4759 else
4761 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4762 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4763 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4764 - target->got_size());
4766 have_got_offset = true;
4767 break;
4769 default:
4770 break;
4773 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4775 switch (r_type)
4777 case elfcpp::R_X86_64_NONE:
4778 case elfcpp::R_X86_64_GNU_VTINHERIT:
4779 case elfcpp::R_X86_64_GNU_VTENTRY:
4780 break;
4782 case elfcpp::R_X86_64_64:
4783 Reloc_funcs::rela64(view, object, psymval, addend);
4784 break;
4786 case elfcpp::R_X86_64_PC64:
4787 Reloc_funcs::pcrela64(view, object, psymval, addend,
4788 address);
4789 break;
4791 case elfcpp::R_X86_64_32:
4792 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4793 Reloc_funcs::CHECK_UNSIGNED);
4794 break;
4796 case elfcpp::R_X86_64_32S:
4797 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4798 Reloc_funcs::CHECK_SIGNED);
4799 break;
4801 case elfcpp::R_X86_64_PC32:
4802 case elfcpp::R_X86_64_PC32_BND:
4803 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4804 address);
4805 break;
4807 case elfcpp::R_X86_64_16:
4808 Reloc_funcs::rela16(view, object, psymval, addend);
4809 break;
4811 case elfcpp::R_X86_64_PC16:
4812 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4813 break;
4815 case elfcpp::R_X86_64_8:
4816 Reloc_funcs::rela8(view, object, psymval, addend);
4817 break;
4819 case elfcpp::R_X86_64_PC8:
4820 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4821 break;
4823 case elfcpp::R_X86_64_PLT32:
4824 case elfcpp::R_X86_64_PLT32_BND:
4825 gold_assert(gsym == NULL
4826 || gsym->has_plt_offset()
4827 || gsym->final_value_is_known()
4828 || (gsym->is_defined()
4829 && !gsym->is_from_dynobj()
4830 && !gsym->is_preemptible()));
4831 // Note: while this code looks the same as for R_X86_64_PC32, it
4832 // behaves differently because psymval was set to point to
4833 // the PLT entry, rather than the symbol, in Scan::global().
4834 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4835 address);
4836 break;
4838 case elfcpp::R_X86_64_PLTOFF64:
4840 gold_assert(gsym);
4841 gold_assert(gsym->has_plt_offset()
4842 || gsym->final_value_is_known());
4843 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4844 // This is the address of GLOBAL_OFFSET_TABLE.
4845 got_address = target->got_plt_section()->address();
4846 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4848 break;
4850 case elfcpp::R_X86_64_GOT32:
4851 gold_assert(have_got_offset);
4852 Reloc_funcs::rela32(view, got_offset, addend);
4853 break;
4855 case elfcpp::R_X86_64_GOTPC32:
4857 gold_assert(gsym);
4858 typename elfcpp::Elf_types<size>::Elf_Addr value;
4859 value = target->got_plt_section()->address();
4860 Reloc_funcs::pcrela32_check(view, value, addend, address);
4862 break;
4864 case elfcpp::R_X86_64_GOT64:
4865 case elfcpp::R_X86_64_GOTPLT64:
4866 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4867 // GOT64.
4868 gold_assert(have_got_offset);
4869 Reloc_funcs::rela64(view, got_offset, addend);
4870 break;
4872 case elfcpp::R_X86_64_GOTPC64:
4874 gold_assert(gsym);
4875 typename elfcpp::Elf_types<size>::Elf_Addr value;
4876 value = target->got_plt_section()->address();
4877 Reloc_funcs::pcrela64(view, value, addend, address);
4879 break;
4881 case elfcpp::R_X86_64_GOTOFF64:
4883 typename elfcpp::Elf_types<size>::Elf_Addr reladdr;
4884 reladdr = target->got_plt_section()->address();
4885 Reloc_funcs::pcrela64(view, object, psymval, addend, reladdr);
4887 break;
4889 case elfcpp::R_X86_64_GOTPCREL:
4890 case elfcpp::R_X86_64_GOTPCRELX:
4891 case elfcpp::R_X86_64_REX_GOTPCRELX:
4893 // Convert
4894 // mov foo@GOTPCREL(%rip), %reg
4895 // to lea foo(%rip), %reg.
4896 // if possible.
4897 if (!parameters->incremental()
4898 && ((gsym == NULL
4899 && rela.get_r_offset() >= 2
4900 && view[-2] == 0x8b
4901 && !psymval->is_ifunc_symbol())
4902 || (gsym != NULL
4903 && rela.get_r_offset() >= 2
4904 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4905 0, &view))))
4907 view[-2] = 0x8d;
4908 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4910 // Convert
4911 // callq *foo@GOTPCRELX(%rip) to
4912 // addr32 callq foo
4913 // and jmpq *foo@GOTPCRELX(%rip) to
4914 // jmpq foo
4915 // nop
4916 else if (!parameters->incremental()
4917 && gsym != NULL
4918 && rela.get_r_offset() >= 2
4919 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4920 r_type,
4921 0, &view))
4923 if (view[-1] == 0x15)
4925 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4926 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4927 view[-2] = 0x67;
4928 view[-1] = 0xe8;
4929 // Convert GOTPCRELX to 32-bit pc relative reloc.
4930 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4932 else
4934 // Convert jmpq *foo@GOTPCRELX(%rip) to
4935 // jmpq foo
4936 // nop
4937 // The opcode of direct jmpq is 0xe9.
4938 view[-2] = 0xe9;
4939 // The opcode of nop is 0x90.
4940 view[3] = 0x90;
4941 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4942 // relative and since the instruction following the jmpq is now
4943 // the nop, offset the address by 1 byte. The start of the
4944 // relocation also moves ahead by 1 byte.
4945 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4946 address - 1);
4949 else
4951 if (gsym != NULL)
4953 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4954 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4955 - target->got_size());
4957 else
4959 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4960 gold_assert(object->local_has_got_offset(r_sym,
4961 GOT_TYPE_STANDARD));
4962 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4963 - target->got_size());
4965 typename elfcpp::Elf_types<size>::Elf_Addr value;
4966 value = target->got_plt_section()->address() + got_offset;
4967 Reloc_funcs::pcrela32_check(view, value, addend, address);
4970 break;
4972 case elfcpp::R_X86_64_GOTPCREL64:
4974 gold_assert(have_got_offset);
4975 typename elfcpp::Elf_types<size>::Elf_Addr value;
4976 value = target->got_plt_section()->address() + got_offset;
4977 Reloc_funcs::pcrela64(view, value, addend, address);
4979 break;
4981 case elfcpp::R_X86_64_COPY:
4982 case elfcpp::R_X86_64_GLOB_DAT:
4983 case elfcpp::R_X86_64_JUMP_SLOT:
4984 case elfcpp::R_X86_64_RELATIVE:
4985 case elfcpp::R_X86_64_IRELATIVE:
4986 // These are outstanding tls relocs, which are unexpected when linking
4987 case elfcpp::R_X86_64_TPOFF64:
4988 case elfcpp::R_X86_64_DTPMOD64:
4989 case elfcpp::R_X86_64_TLSDESC:
4990 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4991 _("unexpected reloc %u in object file"),
4992 r_type);
4993 break;
4995 // These are initial tls relocs, which are expected when linking
4996 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4997 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4998 case elfcpp::R_X86_64_TLSDESC_CALL:
4999 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
5000 case elfcpp::R_X86_64_DTPOFF32:
5001 case elfcpp::R_X86_64_DTPOFF64:
5002 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
5003 case elfcpp::R_X86_64_TPOFF32: // Local-exec
5004 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
5005 view, address, view_size);
5006 break;
5008 case elfcpp::R_X86_64_SIZE32:
5009 case elfcpp::R_X86_64_SIZE64:
5010 default:
5011 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5012 _("unsupported reloc %u"),
5013 r_type);
5014 break;
5017 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
5019 if (gsym == NULL)
5021 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5022 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5023 _("relocation overflow: "
5024 "reference to local symbol %u in %s"),
5025 r_sym, object->name().c_str());
5027 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
5029 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5030 _("relocation overflow: "
5031 "reference to '%s' defined in %s"),
5032 gsym->name(),
5033 gsym->object()->name().c_str());
5035 else
5037 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5038 _("relocation overflow: reference to '%s'"),
5039 gsym->name());
5043 return true;
5046 // Perform a TLS relocation.
5048 template<int size>
5049 inline void
5050 Target_x86_64<size>::Relocate::relocate_tls(
5051 const Relocate_info<size, false>* relinfo,
5052 Target_x86_64<size>* target,
5053 size_t relnum,
5054 const elfcpp::Rela<size, false>& rela,
5055 unsigned int r_type,
5056 const Sized_symbol<size>* gsym,
5057 const Symbol_value<size>* psymval,
5058 unsigned char* view,
5059 typename elfcpp::Elf_types<size>::Elf_Addr address,
5060 section_size_type view_size)
5062 Output_segment* tls_segment = relinfo->layout->tls_segment();
5064 const Sized_relobj_file<size, false>* object = relinfo->object;
5065 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5066 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
5067 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
5069 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
5071 const bool is_final = (gsym == NULL
5072 ? !parameters->options().shared()
5073 : gsym->final_value_is_known());
5074 tls::Tls_optimization optimized_type
5075 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
5076 switch (r_type)
5078 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
5079 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5081 // If this code sequence is used in a non-executable section,
5082 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
5083 // on the assumption that it's being used by itself in a debug
5084 // section. Therefore, in the unlikely event that the code
5085 // sequence appears in a non-executable section, we simply
5086 // leave it unoptimized.
5087 optimized_type = tls::TLSOPT_NONE;
5089 if (optimized_type == tls::TLSOPT_TO_LE)
5091 if (tls_segment == NULL)
5093 gold_assert(parameters->errors()->error_count() > 0
5094 || issue_undefined_symbol_error(gsym));
5095 return;
5097 this->tls_gd_to_le(relinfo, relnum, tls_segment,
5098 rela, r_type, value, view,
5099 view_size);
5100 break;
5102 else
5104 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
5105 ? GOT_TYPE_TLS_OFFSET
5106 : GOT_TYPE_TLS_PAIR);
5107 unsigned int got_offset;
5108 if (gsym != NULL)
5110 gold_assert(gsym->has_got_offset(got_type));
5111 got_offset = gsym->got_offset(got_type) - target->got_size();
5113 else
5115 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5116 gold_assert(object->local_has_got_offset(r_sym, got_type));
5117 got_offset = (object->local_got_offset(r_sym, got_type)
5118 - target->got_size());
5120 if (optimized_type == tls::TLSOPT_TO_IE)
5122 value = target->got_plt_section()->address() + got_offset;
5123 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
5124 value, view, address, view_size);
5125 break;
5127 else if (optimized_type == tls::TLSOPT_NONE)
5129 // Relocate the field with the offset of the pair of GOT
5130 // entries.
5131 value = target->got_plt_section()->address() + got_offset;
5132 Relocate_functions<size, false>::pcrela32(view, value, addend,
5133 address);
5134 break;
5137 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5138 _("unsupported reloc %u"), r_type);
5139 break;
5141 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
5142 case elfcpp::R_X86_64_TLSDESC_CALL:
5143 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5145 // See above comment for R_X86_64_TLSGD.
5146 optimized_type = tls::TLSOPT_NONE;
5148 if (optimized_type == tls::TLSOPT_TO_LE)
5150 if (tls_segment == NULL)
5152 gold_assert(parameters->errors()->error_count() > 0
5153 || issue_undefined_symbol_error(gsym));
5154 return;
5156 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
5157 rela, r_type, value, view,
5158 view_size);
5159 break;
5161 else
5163 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
5164 ? GOT_TYPE_TLS_OFFSET
5165 : GOT_TYPE_TLS_DESC);
5166 unsigned int got_offset = 0;
5167 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
5168 && optimized_type == tls::TLSOPT_NONE)
5170 // We created GOT entries in the .got.tlsdesc portion of
5171 // the .got.plt section, but the offset stored in the
5172 // symbol is the offset within .got.tlsdesc.
5173 got_offset = (target->got_size()
5174 + target->got_plt_section()->data_size());
5176 if (gsym != NULL)
5178 gold_assert(gsym->has_got_offset(got_type));
5179 got_offset += gsym->got_offset(got_type) - target->got_size();
5181 else
5183 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5184 gold_assert(object->local_has_got_offset(r_sym, got_type));
5185 got_offset += (object->local_got_offset(r_sym, got_type)
5186 - target->got_size());
5188 if (optimized_type == tls::TLSOPT_TO_IE)
5190 value = target->got_plt_section()->address() + got_offset;
5191 this->tls_desc_gd_to_ie(relinfo, relnum,
5192 rela, r_type, value, view, address,
5193 view_size);
5194 break;
5196 else if (optimized_type == tls::TLSOPT_NONE)
5198 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5200 // Relocate the field with the offset of the pair of GOT
5201 // entries.
5202 value = target->got_plt_section()->address() + got_offset;
5203 Relocate_functions<size, false>::pcrela32(view, value, addend,
5204 address);
5206 break;
5209 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5210 _("unsupported reloc %u"), r_type);
5211 break;
5213 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
5214 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
5216 // See above comment for R_X86_64_TLSGD.
5217 optimized_type = tls::TLSOPT_NONE;
5219 if (optimized_type == tls::TLSOPT_TO_LE)
5221 if (tls_segment == NULL)
5223 gold_assert(parameters->errors()->error_count() > 0
5224 || issue_undefined_symbol_error(gsym));
5225 return;
5227 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
5228 value, view, view_size);
5229 break;
5231 else if (optimized_type == tls::TLSOPT_NONE)
5233 // Relocate the field with the offset of the GOT entry for
5234 // the module index.
5235 unsigned int got_offset;
5236 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
5237 - target->got_size());
5238 value = target->got_plt_section()->address() + got_offset;
5239 Relocate_functions<size, false>::pcrela32(view, value, addend,
5240 address);
5241 break;
5243 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5244 _("unsupported reloc %u"), r_type);
5245 break;
5247 case elfcpp::R_X86_64_DTPOFF32:
5248 // This relocation type is used in debugging information.
5249 // In that case we need to not optimize the value. If the
5250 // section is not executable, then we assume we should not
5251 // optimize this reloc. See comments above for R_X86_64_TLSGD,
5252 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
5253 // R_X86_64_TLSLD.
5254 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
5256 if (tls_segment == NULL)
5258 gold_assert(parameters->errors()->error_count() > 0
5259 || issue_undefined_symbol_error(gsym));
5260 return;
5262 value -= tls_segment->memsz();
5264 Relocate_functions<size, false>::rela32(view, value, addend);
5265 break;
5267 case elfcpp::R_X86_64_DTPOFF64:
5268 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
5269 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
5271 if (tls_segment == NULL)
5273 gold_assert(parameters->errors()->error_count() > 0
5274 || issue_undefined_symbol_error(gsym));
5275 return;
5277 value -= tls_segment->memsz();
5279 Relocate_functions<size, false>::rela64(view, value, addend);
5280 break;
5282 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
5283 if (gsym != NULL
5284 && gsym->is_undefined()
5285 && parameters->options().output_is_executable())
5287 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
5288 NULL, rela,
5289 r_type, value, view,
5290 view_size);
5291 break;
5293 else if (optimized_type == tls::TLSOPT_TO_LE)
5295 if (tls_segment == NULL)
5297 gold_assert(parameters->errors()->error_count() > 0
5298 || issue_undefined_symbol_error(gsym));
5299 return;
5301 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
5302 tls_segment, rela,
5303 r_type, value, view,
5304 view_size);
5305 break;
5307 else if (optimized_type == tls::TLSOPT_NONE)
5309 // Relocate the field with the offset of the GOT entry for
5310 // the tp-relative offset of the symbol.
5311 unsigned int got_offset;
5312 if (gsym != NULL)
5314 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
5315 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
5316 - target->got_size());
5318 else
5320 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5321 gold_assert(object->local_has_got_offset(r_sym,
5322 GOT_TYPE_TLS_OFFSET));
5323 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
5324 - target->got_size());
5326 value = target->got_plt_section()->address() + got_offset;
5327 Relocate_functions<size, false>::pcrela32(view, value, addend,
5328 address);
5329 break;
5331 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5332 _("unsupported reloc type %u"),
5333 r_type);
5334 break;
5336 case elfcpp::R_X86_64_TPOFF32: // Local-exec
5337 if (tls_segment == NULL)
5339 gold_assert(parameters->errors()->error_count() > 0
5340 || issue_undefined_symbol_error(gsym));
5341 return;
5343 value -= tls_segment->memsz();
5344 Relocate_functions<size, false>::rela32(view, value, addend);
5345 break;
5349 // Do a relocation in which we convert a TLS General-Dynamic to an
5350 // Initial-Exec.
5352 template<int size>
5353 inline void
5354 Target_x86_64<size>::Relocate::tls_gd_to_ie(
5355 const Relocate_info<size, false>* relinfo,
5356 size_t relnum,
5357 const elfcpp::Rela<size, false>& rela,
5358 unsigned int,
5359 typename elfcpp::Elf_types<size>::Elf_Addr value,
5360 unsigned char* view,
5361 typename elfcpp::Elf_types<size>::Elf_Addr address,
5362 section_size_type view_size)
5364 // For SIZE == 64:
5365 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5366 // .word 0x6666; rex64; call __tls_get_addr@PLT
5367 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5368 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5369 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5370 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5371 // For SIZE == 32:
5372 // leaq foo@tlsgd(%rip),%rdi;
5373 // .word 0x6666; rex64; call __tls_get_addr@PLT
5374 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5375 // leaq foo@tlsgd(%rip),%rdi;
5376 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5377 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5379 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5380 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5381 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5382 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5384 if (size == 64)
5386 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5387 -4);
5388 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5389 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5390 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5391 16);
5393 else
5395 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5396 -3);
5397 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5398 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5399 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5400 15);
5403 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5404 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
5405 address);
5407 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5408 // We can skip it.
5409 this->skip_call_tls_get_addr_ = true;
5412 // Do a relocation in which we convert a TLS General-Dynamic to a
5413 // Local-Exec.
5415 template<int size>
5416 inline void
5417 Target_x86_64<size>::Relocate::tls_gd_to_le(
5418 const Relocate_info<size, false>* relinfo,
5419 size_t relnum,
5420 Output_segment* tls_segment,
5421 const elfcpp::Rela<size, false>& rela,
5422 unsigned int,
5423 typename elfcpp::Elf_types<size>::Elf_Addr value,
5424 unsigned char* view,
5425 section_size_type view_size)
5427 // For SIZE == 64:
5428 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5429 // .word 0x6666; rex64; call __tls_get_addr@PLT
5430 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5431 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5432 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5433 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5434 // For SIZE == 32:
5435 // leaq foo@tlsgd(%rip),%rdi;
5436 // .word 0x6666; rex64; call __tls_get_addr@PLT
5437 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5438 // leaq foo@tlsgd(%rip),%rdi;
5439 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5440 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5442 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
5443 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5444 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
5445 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
5447 if (size == 64)
5449 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5450 -4);
5451 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5452 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
5453 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5454 16);
5456 else
5458 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
5459 -3);
5460 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5461 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
5463 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5464 15);
5467 value -= tls_segment->memsz();
5468 Relocate_functions<size, false>::rela32(view + 8, value, 0);
5470 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5471 // We can skip it.
5472 this->skip_call_tls_get_addr_ = true;
5475 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5477 template<int size>
5478 inline void
5479 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
5480 const Relocate_info<size, false>* relinfo,
5481 size_t relnum,
5482 const elfcpp::Rela<size, false>& rela,
5483 unsigned int r_type,
5484 typename elfcpp::Elf_types<size>::Elf_Addr value,
5485 unsigned char* view,
5486 typename elfcpp::Elf_types<size>::Elf_Addr address,
5487 section_size_type view_size)
5489 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5491 // LP64: leaq foo@tlsdesc(%rip), %rax
5492 // ==> movq foo@gottpoff(%rip), %rax
5493 // X32: rex leal foo@tlsdesc(%rip), %eax
5494 // ==> rex movl foo@gottpoff(%rip), %eax
5495 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5496 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5497 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5498 (((view[-3] & 0xfb) == 0x48
5499 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5500 && view[-2] == 0x8d
5501 && (view[-1] & 0xc7) == 0x05));
5502 view[-2] = 0x8b;
5503 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5504 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
5506 else
5508 // LP64: call *foo@tlscall(%rax)
5509 // ==> xchg %ax, %ax
5510 // X32: call *foo@tlscall(%eax)
5511 // ==> nopl (%rax)
5512 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5513 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5514 int prefix = 0;
5515 if (size == 32 && view[0] == 0x67)
5517 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5518 view_size, 3);
5519 prefix = 1;
5521 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5522 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5523 if (prefix)
5525 view[0] = 0x0f;
5526 view[1] = 0x1f;
5527 view[2] = 0x00;
5529 else
5531 view[0] = 0x66;
5532 view[1] = 0x90;
5537 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5539 template<int size>
5540 inline void
5541 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
5542 const Relocate_info<size, false>* relinfo,
5543 size_t relnum,
5544 Output_segment* tls_segment,
5545 const elfcpp::Rela<size, false>& rela,
5546 unsigned int r_type,
5547 typename elfcpp::Elf_types<size>::Elf_Addr value,
5548 unsigned char* view,
5549 section_size_type view_size)
5551 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
5553 // LP64: leaq foo@tlsdesc(%rip), %rax
5554 // ==> movq foo@tpoff, %rax
5555 // X32: rex leal foo@tlsdesc(%rip), %eax
5556 // ==> rex movl foo@tpoff, %eax
5557 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5558 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5559 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5560 (((view[-3] & 0xfb) == 0x48
5561 || (size == 32 && (view[-3] & 0xfb) == 0x40))
5562 && view[-2] == 0x8d
5563 && (view[-1] & 0xc7) == 0x05));
5564 view[-3] = (view[-3] & 0x48) | ((view[-3] >> 2) & 1);
5565 view[-2] = 0xc7;
5566 view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
5567 value -= tls_segment->memsz();
5568 Relocate_functions<size, false>::rela32(view, value, 0);
5570 else
5572 // LP64: call *foo@tlscall(%rax)
5573 // ==> xchg %ax, %ax
5574 // X32: call *foo@tlscall(%eax)
5575 // ==> nopl (%rax)
5576 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
5577 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
5578 int prefix = 0;
5579 if (size == 32 && view[0] == 0x67)
5581 tls::check_range(relinfo, relnum, rela.get_r_offset(),
5582 view_size, 3);
5583 prefix = 1;
5585 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5586 view[prefix] == 0xff && view[prefix + 1] == 0x10);
5587 if (prefix)
5589 view[0] = 0x0f;
5590 view[1] = 0x1f;
5591 view[2] = 0x00;
5593 else
5595 view[0] = 0x66;
5596 view[1] = 0x90;
5601 template<int size>
5602 inline void
5603 Target_x86_64<size>::Relocate::tls_ld_to_le(
5604 const Relocate_info<size, false>* relinfo,
5605 size_t relnum,
5606 Output_segment*,
5607 const elfcpp::Rela<size, false>& rela,
5608 unsigned int,
5609 typename elfcpp::Elf_types<size>::Elf_Addr,
5610 unsigned char* view,
5611 section_size_type view_size)
5613 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5614 // For SIZE == 64:
5615 // ... leq foo@dtpoff(%rax),%reg
5616 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5617 // For SIZE == 32:
5618 // ... leq foo@dtpoff(%rax),%reg
5619 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5620 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5621 // For SIZE == 64:
5622 // ... leq foo@dtpoff(%rax),%reg
5623 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5624 // For SIZE == 32:
5625 // ... leq foo@dtpoff(%rax),%reg
5626 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5628 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5629 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
5631 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5632 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
5634 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
5635 view[4] == 0xe8 || view[4] == 0xff);
5637 if (view[4] == 0xe8)
5639 if (size == 64)
5640 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5641 else
5642 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5644 else
5646 if (size == 64)
5647 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5648 13);
5649 else
5650 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5651 13);
5654 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5655 // We can skip it.
5656 this->skip_call_tls_get_addr_ = true;
5659 // Do a relocation in which we convert a TLS Initial-Exec to a
5660 // Local-Exec.
5662 template<int size>
5663 inline void
5664 Target_x86_64<size>::Relocate::tls_ie_to_le(
5665 const Relocate_info<size, false>* relinfo,
5666 size_t relnum,
5667 Output_segment* tls_segment,
5668 const elfcpp::Rela<size, false>& rela,
5669 unsigned int,
5670 typename elfcpp::Elf_types<size>::Elf_Addr value,
5671 unsigned char* view,
5672 section_size_type view_size)
5674 // We need to examine the opcodes to figure out which instruction we
5675 // are looking at.
5677 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5678 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5680 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
5681 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
5683 unsigned char op1 = view[-3];
5684 unsigned char op2 = view[-2];
5685 unsigned char op3 = view[-1];
5686 unsigned char reg = op3 >> 3;
5688 if (op2 == 0x8b)
5690 // movq
5691 if (op1 == 0x4c)
5692 view[-3] = 0x49;
5693 else if (size == 32 && op1 == 0x44)
5694 view[-3] = 0x41;
5695 view[-2] = 0xc7;
5696 view[-1] = 0xc0 | reg;
5698 else if (reg == 4)
5700 // Special handling for %rsp.
5701 if (op1 == 0x4c)
5702 view[-3] = 0x49;
5703 else if (size == 32 && op1 == 0x44)
5704 view[-3] = 0x41;
5705 view[-2] = 0x81;
5706 view[-1] = 0xc0 | reg;
5708 else
5710 // addq
5711 if (op1 == 0x4c)
5712 view[-3] = 0x4d;
5713 else if (size == 32 && op1 == 0x44)
5714 view[-3] = 0x45;
5715 view[-2] = 0x8d;
5716 view[-1] = 0x80 | reg | (reg << 3);
5719 if (tls_segment != NULL)
5720 value -= tls_segment->memsz();
5721 Relocate_functions<size, false>::rela32(view, value, 0);
5724 // Relocate section data.
5726 template<int size>
5727 void
5728 Target_x86_64<size>::relocate_section(
5729 const Relocate_info<size, false>* relinfo,
5730 unsigned int sh_type,
5731 const unsigned char* prelocs,
5732 size_t reloc_count,
5733 Output_section* output_section,
5734 bool needs_special_offset_handling,
5735 unsigned char* view,
5736 typename elfcpp::Elf_types<size>::Elf_Addr address,
5737 section_size_type view_size,
5738 const Reloc_symbol_changes* reloc_symbol_changes)
5740 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5741 Classify_reloc;
5743 gold_assert(sh_type == elfcpp::SHT_RELA);
5745 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5746 gold::Default_comdat_behavior, Classify_reloc>(
5747 relinfo,
5748 this,
5749 prelocs,
5750 reloc_count,
5751 output_section,
5752 needs_special_offset_handling,
5753 view,
5754 address,
5755 view_size,
5756 reloc_symbol_changes);
5759 // Apply an incremental relocation. Incremental relocations always refer
5760 // to global symbols.
5762 template<int size>
5763 void
5764 Target_x86_64<size>::apply_relocation(
5765 const Relocate_info<size, false>* relinfo,
5766 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5767 unsigned int r_type,
5768 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5769 const Symbol* gsym,
5770 unsigned char* view,
5771 typename elfcpp::Elf_types<size>::Elf_Addr address,
5772 section_size_type view_size)
5774 gold::apply_relocation<size, false, Target_x86_64<size>,
5775 typename Target_x86_64<size>::Relocate>(
5776 relinfo,
5777 this,
5778 r_offset,
5779 r_type,
5780 r_addend,
5781 gsym,
5782 view,
5783 address,
5784 view_size);
5787 // Scan the relocs during a relocatable link.
5789 template<int size>
5790 void
5791 Target_x86_64<size>::scan_relocatable_relocs(
5792 Symbol_table* symtab,
5793 Layout* layout,
5794 Sized_relobj_file<size, false>* object,
5795 unsigned int data_shndx,
5796 unsigned int sh_type,
5797 const unsigned char* prelocs,
5798 size_t reloc_count,
5799 Output_section* output_section,
5800 bool needs_special_offset_handling,
5801 size_t local_symbol_count,
5802 const unsigned char* plocal_symbols,
5803 Relocatable_relocs* rr)
5805 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5806 Classify_reloc;
5807 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5808 Scan_relocatable_relocs;
5810 gold_assert(sh_type == elfcpp::SHT_RELA);
5812 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5813 symtab,
5814 layout,
5815 object,
5816 data_shndx,
5817 prelocs,
5818 reloc_count,
5819 output_section,
5820 needs_special_offset_handling,
5821 local_symbol_count,
5822 plocal_symbols,
5823 rr);
5826 // Scan the relocs for --emit-relocs.
5828 template<int size>
5829 void
5830 Target_x86_64<size>::emit_relocs_scan(
5831 Symbol_table* symtab,
5832 Layout* layout,
5833 Sized_relobj_file<size, false>* object,
5834 unsigned int data_shndx,
5835 unsigned int sh_type,
5836 const unsigned char* prelocs,
5837 size_t reloc_count,
5838 Output_section* output_section,
5839 bool needs_special_offset_handling,
5840 size_t local_symbol_count,
5841 const unsigned char* plocal_syms,
5842 Relocatable_relocs* rr)
5844 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5845 Classify_reloc;
5846 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5847 Emit_relocs_strategy;
5849 gold_assert(sh_type == elfcpp::SHT_RELA);
5851 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5852 symtab,
5853 layout,
5854 object,
5855 data_shndx,
5856 prelocs,
5857 reloc_count,
5858 output_section,
5859 needs_special_offset_handling,
5860 local_symbol_count,
5861 plocal_syms,
5862 rr);
5865 // Relocate a section during a relocatable link.
5867 template<int size>
5868 void
5869 Target_x86_64<size>::relocate_relocs(
5870 const Relocate_info<size, false>* relinfo,
5871 unsigned int sh_type,
5872 const unsigned char* prelocs,
5873 size_t reloc_count,
5874 Output_section* output_section,
5875 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5876 unsigned char* view,
5877 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5878 section_size_type view_size,
5879 unsigned char* reloc_view,
5880 section_size_type reloc_view_size)
5882 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5883 Classify_reloc;
5885 gold_assert(sh_type == elfcpp::SHT_RELA);
5887 gold::relocate_relocs<size, false, Classify_reloc>(
5888 relinfo,
5889 prelocs,
5890 reloc_count,
5891 output_section,
5892 offset_in_output_section,
5893 view,
5894 view_address,
5895 view_size,
5896 reloc_view,
5897 reloc_view_size);
5900 // Return the value to use for a dynamic which requires special
5901 // treatment. This is how we support equality comparisons of function
5902 // pointers across shared library boundaries, as described in the
5903 // processor specific ABI supplement.
5905 template<int size>
5906 uint64_t
5907 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5909 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5910 return this->plt_address_for_global(gsym);
5913 // Return a string used to fill a code section with nops to take up
5914 // the specified length.
5916 template<int size>
5917 std::string
5918 Target_x86_64<size>::do_code_fill(section_size_type length) const
5920 if (length >= 16)
5922 // Build a jmpq instruction to skip over the bytes.
5923 unsigned char jmp[5];
5924 jmp[0] = 0xe9;
5925 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5926 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5927 + std::string(length - 5, static_cast<char>(0x90)));
5930 // Nop sequences of various lengths.
5931 const char nop1[1] = { '\x90' }; // nop
5932 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5933 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5934 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5935 '\x00'};
5936 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5937 '\x00', '\x00' };
5938 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5939 '\x44', '\x00', '\x00' };
5940 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5941 '\x00', '\x00', '\x00',
5942 '\x00' };
5943 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5944 '\x00', '\x00', '\x00',
5945 '\x00', '\x00' };
5946 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5947 '\x84', '\x00', '\x00',
5948 '\x00', '\x00', '\x00' };
5949 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5950 '\x1f', '\x84', '\x00',
5951 '\x00', '\x00', '\x00',
5952 '\x00' };
5953 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5954 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5955 '\x00', '\x00', '\x00',
5956 '\x00', '\x00' };
5957 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5958 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5959 '\x84', '\x00', '\x00',
5960 '\x00', '\x00', '\x00' };
5961 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5962 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5963 '\x1f', '\x84', '\x00',
5964 '\x00', '\x00', '\x00',
5965 '\x00' };
5966 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5967 '\x66', '\x66', '\x2e', // data16
5968 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5969 '\x00', '\x00', '\x00',
5970 '\x00', '\x00' };
5971 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5972 '\x66', '\x66', '\x66', // data16; data16
5973 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5974 '\x84', '\x00', '\x00',
5975 '\x00', '\x00', '\x00' };
5977 const char* nops[16] = {
5978 NULL,
5979 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5980 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5983 return std::string(nops[length], length);
5986 // Return the addend to use for a target specific relocation. The
5987 // only target specific relocation is R_X86_64_TLSDESC for a local
5988 // symbol. We want to set the addend is the offset of the local
5989 // symbol in the TLS segment.
5991 template<int size>
5992 uint64_t
5993 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5994 uint64_t) const
5996 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5997 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5998 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5999 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
6000 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
6001 gold_assert(psymval->is_tls_symbol());
6002 // The value of a TLS symbol is the offset in the TLS segment.
6003 return psymval->value(ti.object, 0);
6006 // Return the value to use for the base of a DW_EH_PE_datarel offset
6007 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
6008 // assembler can not write out the difference between two labels in
6009 // different sections, so instead of using a pc-relative value they
6010 // use an offset from the GOT.
6012 template<int size>
6013 uint64_t
6014 Target_x86_64<size>::do_ehframe_datarel_base() const
6016 gold_assert(this->global_offset_table_ != NULL);
6017 Symbol* sym = this->global_offset_table_;
6018 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
6019 return ssym->value();
6022 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6023 // compiled with -fsplit-stack. The function calls non-split-stack
6024 // code. We have to change the function so that it always ensures
6025 // that it has enough stack space to run some random function.
6027 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
6028 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
6029 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
6031 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
6032 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
6033 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
6035 template<int size>
6036 void
6037 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
6038 section_offset_type fnoffset,
6039 section_size_type fnsize,
6040 const unsigned char*,
6041 size_t,
6042 unsigned char* view,
6043 section_size_type view_size,
6044 std::string* from,
6045 std::string* to) const
6047 const char* const cmp_insn = reinterpret_cast<const char*>
6048 (size == 32 ? cmp_insn_32 : cmp_insn_64);
6049 const char* const lea_r10_insn = reinterpret_cast<const char*>
6050 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
6051 const char* const lea_r11_insn = reinterpret_cast<const char*>
6052 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
6054 const size_t cmp_insn_len =
6055 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
6056 const size_t lea_r10_insn_len =
6057 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
6058 const size_t lea_r11_insn_len =
6059 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
6060 const size_t nop_len = (size == 32 ? 7 : 8);
6062 // The function starts with a comparison of the stack pointer and a
6063 // field in the TCB. This is followed by a jump.
6065 // cmp %fs:NN,%rsp
6066 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
6067 && fnsize > nop_len + 1)
6069 // We will call __morestack if the carry flag is set after this
6070 // comparison. We turn the comparison into an stc instruction
6071 // and some nops.
6072 view[fnoffset] = '\xf9';
6073 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
6075 // lea NN(%rsp),%r10
6076 // lea NN(%rsp),%r11
6077 else if ((this->match_view(view, view_size, fnoffset,
6078 lea_r10_insn, lea_r10_insn_len)
6079 || this->match_view(view, view_size, fnoffset,
6080 lea_r11_insn, lea_r11_insn_len))
6081 && fnsize > 8)
6083 // This is loading an offset from the stack pointer for a
6084 // comparison. The offset is negative, so we decrease the
6085 // offset by the amount of space we need for the stack. This
6086 // means we will avoid calling __morestack if there happens to
6087 // be plenty of space on the stack already.
6088 unsigned char* pval = view + fnoffset + 4;
6089 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
6090 val -= parameters->options().split_stack_adjust_size();
6091 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
6093 else
6095 if (!object->has_no_split_stack())
6096 object->error(_("failed to match split-stack sequence at "
6097 "section %u offset %0zx"),
6098 shndx, static_cast<size_t>(fnoffset));
6099 return;
6102 // We have to change the function so that it calls
6103 // __morestack_non_split instead of __morestack. The former will
6104 // allocate additional stack space.
6105 *from = "__morestack";
6106 *to = "__morestack_non_split";
6109 // The selector for x86_64 object files. Note this is never instantiated
6110 // directly. It's only used in Target_selector_x86_64_nacl, below.
6112 template<int size>
6113 class Target_selector_x86_64 : public Target_selector_freebsd
6115 public:
6116 Target_selector_x86_64()
6117 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
6118 (size == 64
6119 ? "elf64-x86-64" : "elf32-x86-64"),
6120 (size == 64
6121 ? "elf64-x86-64-freebsd"
6122 : "elf32-x86-64-freebsd"),
6123 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
6126 Target*
6127 do_instantiate_target()
6128 { return new Target_x86_64<size>(); }
6132 // NaCl variant. It uses different PLT contents.
6134 template<int size>
6135 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
6137 public:
6138 Output_data_plt_x86_64_nacl(Layout* layout,
6139 Output_data_got<64, false>* got,
6140 Output_data_got_plt_x86_64* got_plt,
6141 Output_data_space* got_irelative)
6142 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
6143 got, got_plt, got_irelative)
6146 Output_data_plt_x86_64_nacl(Layout* layout,
6147 Output_data_got<64, false>* got,
6148 Output_data_got_plt_x86_64* got_plt,
6149 Output_data_space* got_irelative,
6150 unsigned int plt_count)
6151 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
6152 got, got_plt, got_irelative,
6153 plt_count)
6156 protected:
6157 virtual unsigned int
6158 do_get_plt_entry_size() const
6159 { return plt_entry_size; }
6161 virtual void
6162 do_add_eh_frame(Layout* layout)
6164 layout->add_eh_frame_for_plt(this,
6165 this->plt_eh_frame_cie,
6166 this->plt_eh_frame_cie_size,
6167 plt_eh_frame_fde,
6168 plt_eh_frame_fde_size);
6171 virtual void
6172 do_fill_first_plt_entry(unsigned char* pov,
6173 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
6174 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
6176 virtual unsigned int
6177 do_fill_plt_entry(unsigned char* pov,
6178 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6179 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6180 unsigned int got_offset,
6181 unsigned int plt_offset,
6182 unsigned int plt_index);
6184 virtual void
6185 do_fill_tlsdesc_entry(unsigned char* pov,
6186 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6187 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6188 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6189 unsigned int tlsdesc_got_offset,
6190 unsigned int plt_offset);
6192 private:
6193 // The size of an entry in the PLT.
6194 static const int plt_entry_size = 64;
6196 // The first entry in the PLT.
6197 static const unsigned char first_plt_entry[plt_entry_size];
6199 // Other entries in the PLT for an executable.
6200 static const unsigned char plt_entry[plt_entry_size];
6202 // The reserved TLSDESC entry in the PLT for an executable.
6203 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
6205 // The .eh_frame unwind information for the PLT.
6206 static const int plt_eh_frame_fde_size = 32;
6207 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
6210 template<int size>
6211 class Target_x86_64_nacl : public Target_x86_64<size>
6213 public:
6214 Target_x86_64_nacl()
6215 : Target_x86_64<size>(&x86_64_nacl_info)
6218 virtual Output_data_plt_x86_64<size>*
6219 do_make_data_plt(Layout* layout,
6220 Output_data_got<64, false>* got,
6221 Output_data_got_plt_x86_64* got_plt,
6222 Output_data_space* got_irelative)
6224 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
6225 got_irelative);
6228 virtual Output_data_plt_x86_64<size>*
6229 do_make_data_plt(Layout* layout,
6230 Output_data_got<64, false>* got,
6231 Output_data_got_plt_x86_64* got_plt,
6232 Output_data_space* got_irelative,
6233 unsigned int plt_count)
6235 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
6236 got_irelative,
6237 plt_count);
6240 virtual std::string
6241 do_code_fill(section_size_type length) const;
6243 private:
6244 static const Target::Target_info x86_64_nacl_info;
6247 template<>
6248 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
6250 64, // size
6251 false, // is_big_endian
6252 elfcpp::EM_X86_64, // machine_code
6253 false, // has_make_symbol
6254 false, // has_resolve
6255 true, // has_code_fill
6256 true, // is_default_stack_executable
6257 true, // can_icf_inline_merge_sections
6258 '\0', // wrap_char
6259 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
6260 0x20000, // default_text_segment_address
6261 0x10000, // abi_pagesize (overridable by -z max-page-size)
6262 0x10000, // common_pagesize (overridable by -z common-page-size)
6263 true, // isolate_execinstr
6264 0x10000000, // rosegment_gap
6265 elfcpp::SHN_UNDEF, // small_common_shndx
6266 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
6267 0, // small_common_section_flags
6268 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
6269 NULL, // attributes_section
6270 NULL, // attributes_vendor
6271 "_start", // entry_symbol_name
6272 32, // hash_entry_size
6273 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
6276 template<>
6277 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
6279 32, // size
6280 false, // is_big_endian
6281 elfcpp::EM_X86_64, // machine_code
6282 false, // has_make_symbol
6283 false, // has_resolve
6284 true, // has_code_fill
6285 true, // is_default_stack_executable
6286 true, // can_icf_inline_merge_sections
6287 '\0', // wrap_char
6288 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
6289 0x20000, // default_text_segment_address
6290 0x10000, // abi_pagesize (overridable by -z max-page-size)
6291 0x10000, // common_pagesize (overridable by -z common-page-size)
6292 true, // isolate_execinstr
6293 0x10000000, // rosegment_gap
6294 elfcpp::SHN_UNDEF, // small_common_shndx
6295 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
6296 0, // small_common_section_flags
6297 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
6298 NULL, // attributes_section
6299 NULL, // attributes_vendor
6300 "_start", // entry_symbol_name
6301 32, // hash_entry_size
6302 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
6305 #define NACLMASK 0xe0 // 32-byte alignment mask.
6307 // The first entry in the PLT.
6309 template<int size>
6310 const unsigned char
6311 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
6313 0xff, 0x35, // pushq contents of memory address
6314 0, 0, 0, 0, // replaced with address of .got + 8
6315 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
6316 0, 0, 0, 0, // replaced with address of .got + 16
6317 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6318 0x4d, 0x01, 0xfb, // add %r15, %r11
6319 0x41, 0xff, 0xe3, // jmpq *%r11
6321 // 9-byte nop sequence to pad out to the next 32-byte boundary.
6322 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
6324 // 32 bytes of nop to pad out to the standard size
6325 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6326 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6327 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6328 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6329 0x66, // excess data32 prefix
6330 0x90 // nop
6333 template<int size>
6334 void
6335 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
6336 unsigned char* pov,
6337 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6338 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
6340 memcpy(pov, first_plt_entry, plt_entry_size);
6341 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6342 (got_address + 8
6343 - (plt_address + 2 + 4)));
6344 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6345 (got_address + 16
6346 - (plt_address + 9 + 4)));
6349 // Subsequent entries in the PLT.
6351 template<int size>
6352 const unsigned char
6353 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
6355 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6356 0, 0, 0, 0, // replaced with address of symbol in .got
6357 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6358 0x4d, 0x01, 0xfb, // add %r15, %r11
6359 0x41, 0xff, 0xe3, // jmpq *%r11
6361 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6362 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6363 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6365 // Lazy GOT entries point here (32-byte aligned).
6366 0x68, // pushq immediate
6367 0, 0, 0, 0, // replaced with index into relocation table
6368 0xe9, // jmp relative
6369 0, 0, 0, 0, // replaced with offset to start of .plt0
6371 // 22 bytes of nop to pad out to the standard size.
6372 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6373 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6374 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6377 template<int size>
6378 unsigned int
6379 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
6380 unsigned char* pov,
6381 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6382 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6383 unsigned int got_offset,
6384 unsigned int plt_offset,
6385 unsigned int plt_index)
6387 memcpy(pov, plt_entry, plt_entry_size);
6388 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
6389 (got_address + got_offset
6390 - (plt_address + plt_offset
6391 + 3 + 4)));
6393 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
6394 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
6395 - (plt_offset + 38 + 4));
6397 return 32;
6400 // The reserved TLSDESC entry in the PLT.
6402 template<int size>
6403 const unsigned char
6404 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
6406 0xff, 0x35, // pushq x(%rip)
6407 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6408 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6409 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6410 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
6411 0x4d, 0x01, 0xfb, // add %r15, %r11
6412 0x41, 0xff, 0xe3, // jmpq *%r11
6414 // 41 bytes of nop to pad out to the standard size.
6415 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6416 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6417 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6418 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6419 0x66, 0x66, // excess data32 prefixes
6420 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6423 template<int size>
6424 void
6425 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
6426 unsigned char* pov,
6427 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
6428 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
6429 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
6430 unsigned int tlsdesc_got_offset,
6431 unsigned int plt_offset)
6433 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
6434 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
6435 (got_address + 8
6436 - (plt_address + plt_offset
6437 + 2 + 4)));
6438 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
6439 (got_base
6440 + tlsdesc_got_offset
6441 - (plt_address + plt_offset
6442 + 9 + 4)));
6445 // The .eh_frame unwind information for the PLT.
6447 template<int size>
6448 const unsigned char
6449 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
6451 0, 0, 0, 0, // Replaced with offset to .plt.
6452 0, 0, 0, 0, // Replaced with size of .plt.
6453 0, // Augmentation size.
6454 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
6455 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
6456 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
6457 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
6458 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
6459 13, // Block length.
6460 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
6461 elfcpp::DW_OP_breg16, 0, // Push %rip.
6462 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
6463 elfcpp::DW_OP_and, // & (%rip & 0x3f).
6464 elfcpp::DW_OP_const1u, 37, // Push 0x25.
6465 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
6466 elfcpp::DW_OP_lit3, // Push 3.
6467 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
6468 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6469 elfcpp::DW_CFA_nop, // Align to 32 bytes.
6470 elfcpp::DW_CFA_nop
6473 // Return a string used to fill a code section with nops.
6474 // For NaCl, long NOPs are only valid if they do not cross
6475 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6476 template<int size>
6477 std::string
6478 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
6480 return std::string(length, static_cast<char>(0x90));
6483 // The selector for x86_64-nacl object files.
6485 template<int size>
6486 class Target_selector_x86_64_nacl
6487 : public Target_selector_nacl<Target_selector_x86_64<size>,
6488 Target_x86_64_nacl<size> >
6490 public:
6491 Target_selector_x86_64_nacl()
6492 : Target_selector_nacl<Target_selector_x86_64<size>,
6493 Target_x86_64_nacl<size> >("x86-64",
6494 size == 64
6495 ? "elf64-x86-64-nacl"
6496 : "elf32-x86-64-nacl",
6497 size == 64
6498 ? "elf_x86_64_nacl"
6499 : "elf32_x86_64_nacl")
6503 Target_selector_x86_64_nacl<64> target_selector_x86_64;
6504 Target_selector_x86_64_nacl<32> target_selector_x32;
6506 } // End anonymous namespace.