603ac300f72f1b1ab37b3c2332528b5ce06d302e
[deliverable/binutils-gdb.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright (C) 2006-2018 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstring>
26
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"
45
46 namespace
47 {
48
49 using namespace gold;
50
51 // A class to handle the .got.plt section.
52
53 class Output_data_got_plt_x86_64 : public Output_section_data_build
54 {
55 public:
56 Output_data_got_plt_x86_64(Layout* layout)
57 : Output_section_data_build(8),
58 layout_(layout)
59 { }
60
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 { }
65
66 protected:
67 // Write out the PLT data.
68 void
69 do_write(Output_file*);
70
71 // Write to a map file.
72 void
73 do_print_to_mapfile(Mapfile* mapfile) const
74 { mapfile->print_output_data(this, "** GOT PLT"); }
75
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_;
80 };
81
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.
86
87 template<int size>
88 class Output_data_plt_x86_64 : public Output_section_data
89 {
90 public:
91 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
92
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); }
102
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_()
113 {
114 this->init(layout);
115
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);
119 }
120
121 // Initialize the PLT section.
122 void
123 init(Layout* layout);
124
125 // Add an entry to the PLT.
126 void
127 add_entry(Symbol_table*, Layout*, Symbol* gsym);
128
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);
134
135 // Add the relocation for a PLT entry.
136 void
137 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
138 unsigned int got_offset);
139
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; }
144
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; }
149
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_; }
154
155 // Return the offset of the reserved TLSDESC_PLT entry.
156 unsigned int
157 get_tlsdesc_plt_offset() const
158 {
159 return ((this->count_ + this->irelative_count_ + 1)
160 * this->get_plt_entry_size());
161 }
162
163 // Return the .rela.plt section data.
164 Reloc_section*
165 rela_plt()
166 { return this->rel_; }
167
168 // Return where the TLSDESC relocations should go.
169 Reloc_section*
170 rela_tlsdesc(Layout*);
171
172 // Return where the IRELATIVE relocations should go in the PLT
173 // relocations.
174 Reloc_section*
175 rela_irelative(Symbol_table*, Layout*);
176
177 // Return whether we created a section for IRELATIVE relocations.
178 bool
179 has_irelative_section() const
180 { return this->irelative_rel_ != NULL; }
181
182 // Get count of regular PLT entries.
183 unsigned int
184 regular_count() const
185 { return this->count_; }
186
187 // Return the total number of PLT entries.
188 unsigned int
189 entry_count() const
190 { return this->count_ + this->irelative_count_; }
191
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(); }
196
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(); }
201
202 // Reserve a slot in the PLT for an existing symbol in an incremental update.
203 void
204 reserve_slot(unsigned int plt_index)
205 {
206 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
207 (plt_index + 2) * this->get_plt_entry_size());
208 }
209
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); }
214
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); }
219
220 // Add .eh_frame information for the PLT.
221 void
222 add_eh_frame(Layout* layout)
223 { this->do_add_eh_frame(layout); }
224
225 protected:
226 Output_data_got<64, false>*
227 got() const
228 { return this->got_; }
229
230 Output_data_got_plt_x86_64*
231 got_plt() const
232 { return this->got_plt_; }
233
234 Output_data_space*
235 got_irelative() const
236 { return this->got_irelative_; }
237
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); }
244
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)
254 {
255 return this->do_fill_plt_entry(pov, got_address, plt_address,
256 got_offset, plt_offset, plt_index);
257 }
258
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)
267 {
268 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
269 tlsdesc_got_offset, plt_offset);
270 }
271
272 virtual unsigned int
273 do_get_plt_entry_size() const = 0;
274
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;
280
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;
288
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;
296
297 // Return the PLT address to use for a global symbol.
298 virtual uint64_t
299 do_address_for_global(const Symbol* sym);
300
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);
304
305 virtual void
306 do_add_eh_frame(Layout* layout) = 0;
307
308 void
309 do_adjust_output_section(Output_section* os);
310
311 // Write to a map file.
312 void
313 do_print_to_mapfile(Mapfile* mapfile) const
314 { mapfile->print_output_data(this, _("** PLT")); }
315
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];
319
320 private:
321 // Set the final size.
322 void
323 set_final_data_size();
324
325 // Write out the PLT data.
326 void
327 do_write(Output_file*);
328
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_;
353 };
354
355 template<int size>
356 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
357 {
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)
365 { }
366
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)
375 { }
376
377 protected:
378 virtual unsigned int
379 do_get_plt_entry_size() const
380 { return plt_entry_size; }
381
382 virtual void
383 do_add_eh_frame(Layout* layout)
384 {
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);
390 }
391
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);
396
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);
404
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);
412
413 private:
414 // The size of an entry in the PLT.
415 static const int plt_entry_size = 16;
416
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];
421
422 // Other entries in the PLT for an executable.
423 static const unsigned char plt_entry[plt_entry_size];
424
425 // The reserved TLSDESC entry in the PLT for an executable.
426 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
427
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];
431 };
432
433 class Output_data_plt_x86_64_bnd : public Output_data_plt_x86_64<64>
434 {
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)
443 { }
444
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)
454 { }
455
456 protected:
457 virtual unsigned int
458 do_get_plt_entry_size() const
459 { return plt_entry_size; }
460
461 // Return the PLT address to use for a global symbol.
462 uint64_t
463 do_address_for_global(const Symbol*);
464
465 // Return the PLT address to use for a local symbol.
466 uint64_t
467 do_address_for_local(const Relobj*, unsigned int symndx);
468
469 virtual void
470 do_add_eh_frame(Layout* layout)
471 {
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);
477 }
478
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);
483
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);
491
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);
499
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);
507
508 private:
509 // Set the final size.
510 void
511 set_final_data_size();
512
513 // Write out the BND PLT data.
514 void
515 do_write(Output_file*);
516
517 // Offset of the Additional PLT (if using -z bndplt).
518 unsigned int aplt_offset_;
519
520 // The size of an entry in the PLT.
521 static const int plt_entry_size = 16;
522
523 // The size of an entry in the additional PLT.
524 static const int aplt_entry_size = 8;
525
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];
530
531 // Other entries in the PLT for an executable.
532 static const unsigned char plt_entry[plt_entry_size];
533
534 // Entries in the additional PLT.
535 static const unsigned char aplt_entry[aplt_entry_size];
536
537 // The reserved TLSDESC entry in the PLT for an executable.
538 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
539
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];
543 };
544
545 template<int size>
546 class Lazy_view
547 {
548 public:
549 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
550 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
551 { }
552
553 inline unsigned char
554 operator[](size_t offset)
555 {
556 if (this->view_ == NULL)
557 this->view_ = this->object_->section_contents(this->data_shndx_,
558 &this->view_size_,
559 true);
560 if (offset >= this->view_size_)
561 return 0;
562 return this->view_[offset];
563 }
564
565 private:
566 Sized_relobj_file<size, false>* object_;
567 unsigned int data_shndx_;
568 const unsigned char* view_;
569 section_size_type view_size_;
570 };
571
572 // The x86_64 target class.
573 // See the ABI at
574 // http://www.x86-64.org/documentation/abi.pdf
575 // TLS info comes from
576 // http://people.redhat.com/drepper/tls.pdf
577 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
578
579 template<int size>
580 class Target_x86_64 : public Sized_target<size, false>
581 {
582 public:
583 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
584 // uses only Elf64_Rela relocation entries with explicit addends."
585 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
586
587 Target_x86_64(const Target::Target_info* info = &x86_64_info)
588 : Sized_target<size, false>(info),
589 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
590 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
591 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
592 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
593 tls_base_symbol_defined_(false)
594 { }
595
596 // Hook for a new output section.
597 void
598 do_new_output_section(Output_section*) const;
599
600 // Scan the relocations to look for symbol adjustments.
601 void
602 gc_process_relocs(Symbol_table* symtab,
603 Layout* layout,
604 Sized_relobj_file<size, false>* object,
605 unsigned int data_shndx,
606 unsigned int sh_type,
607 const unsigned char* prelocs,
608 size_t reloc_count,
609 Output_section* output_section,
610 bool needs_special_offset_handling,
611 size_t local_symbol_count,
612 const unsigned char* plocal_symbols);
613
614 // Scan the relocations to look for symbol adjustments.
615 void
616 scan_relocs(Symbol_table* symtab,
617 Layout* layout,
618 Sized_relobj_file<size, false>* object,
619 unsigned int data_shndx,
620 unsigned int sh_type,
621 const unsigned char* prelocs,
622 size_t reloc_count,
623 Output_section* output_section,
624 bool needs_special_offset_handling,
625 size_t local_symbol_count,
626 const unsigned char* plocal_symbols);
627
628 // Finalize the sections.
629 void
630 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
631
632 // Return the value to use for a dynamic which requires special
633 // treatment.
634 uint64_t
635 do_dynsym_value(const Symbol*) const;
636
637 // Relocate a section.
638 void
639 relocate_section(const Relocate_info<size, false>*,
640 unsigned int sh_type,
641 const unsigned char* prelocs,
642 size_t reloc_count,
643 Output_section* output_section,
644 bool needs_special_offset_handling,
645 unsigned char* view,
646 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
647 section_size_type view_size,
648 const Reloc_symbol_changes*);
649
650 // Scan the relocs during a relocatable link.
651 void
652 scan_relocatable_relocs(Symbol_table* symtab,
653 Layout* layout,
654 Sized_relobj_file<size, false>* object,
655 unsigned int data_shndx,
656 unsigned int sh_type,
657 const unsigned char* prelocs,
658 size_t reloc_count,
659 Output_section* output_section,
660 bool needs_special_offset_handling,
661 size_t local_symbol_count,
662 const unsigned char* plocal_symbols,
663 Relocatable_relocs*);
664
665 // Scan the relocs for --emit-relocs.
666 void
667 emit_relocs_scan(Symbol_table* symtab,
668 Layout* layout,
669 Sized_relobj_file<size, false>* object,
670 unsigned int data_shndx,
671 unsigned int sh_type,
672 const unsigned char* prelocs,
673 size_t reloc_count,
674 Output_section* output_section,
675 bool needs_special_offset_handling,
676 size_t local_symbol_count,
677 const unsigned char* plocal_syms,
678 Relocatable_relocs* rr);
679
680 // Emit relocations for a section.
681 void
682 relocate_relocs(
683 const Relocate_info<size, false>*,
684 unsigned int sh_type,
685 const unsigned char* prelocs,
686 size_t reloc_count,
687 Output_section* output_section,
688 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
689 unsigned char* view,
690 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
691 section_size_type view_size,
692 unsigned char* reloc_view,
693 section_size_type reloc_view_size);
694
695 // Return a string used to fill a code section with nops.
696 std::string
697 do_code_fill(section_size_type length) const;
698
699 // Return whether SYM is defined by the ABI.
700 bool
701 do_is_defined_by_abi(const Symbol* sym) const
702 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
703
704 // Return the symbol index to use for a target specific relocation.
705 // The only target specific relocation is R_X86_64_TLSDESC for a
706 // local symbol, which is an absolute reloc.
707 unsigned int
708 do_reloc_symbol_index(void*, unsigned int r_type) const
709 {
710 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
711 return 0;
712 }
713
714 // Return the addend to use for a target specific relocation.
715 uint64_t
716 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
717
718 // Return the PLT section.
719 uint64_t
720 do_plt_address_for_global(const Symbol* gsym) const
721 { return this->plt_section()->address_for_global(gsym); }
722
723 uint64_t
724 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
725 { return this->plt_section()->address_for_local(relobj, symndx); }
726
727 // This function should be defined in targets that can use relocation
728 // types to determine (implemented in local_reloc_may_be_function_pointer
729 // and global_reloc_may_be_function_pointer)
730 // if a function's pointer is taken. ICF uses this in safe mode to only
731 // fold those functions whose pointer is defintely not taken. For x86_64
732 // pie binaries, safe ICF cannot be done by looking at only relocation
733 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
734 // opcode is checked as well to distinguish a function call from taking
735 // a function's pointer.
736 bool
737 do_can_check_for_function_pointers() const
738 { return true; }
739
740 // Return the base for a DW_EH_PE_datarel encoding.
741 uint64_t
742 do_ehframe_datarel_base() const;
743
744 // Adjust -fsplit-stack code which calls non-split-stack code.
745 void
746 do_calls_non_split(Relobj* object, unsigned int shndx,
747 section_offset_type fnoffset, section_size_type fnsize,
748 const unsigned char* prelocs, size_t reloc_count,
749 unsigned char* view, section_size_type view_size,
750 std::string* from, std::string* to) const;
751
752 // Return the size of the GOT section.
753 section_size_type
754 got_size() const
755 {
756 gold_assert(this->got_ != NULL);
757 return this->got_->data_size();
758 }
759
760 // Return the number of entries in the GOT.
761 unsigned int
762 got_entry_count() const
763 {
764 if (this->got_ == NULL)
765 return 0;
766 return this->got_size() / 8;
767 }
768
769 // Return the number of entries in the PLT.
770 unsigned int
771 plt_entry_count() const;
772
773 // Return the offset of the first non-reserved PLT entry.
774 unsigned int
775 first_plt_entry_offset() const;
776
777 // Return the size of each PLT entry.
778 unsigned int
779 plt_entry_size() const;
780
781 // Return the size of each GOT entry.
782 unsigned int
783 got_entry_size() const
784 { return 8; };
785
786 // Create the GOT section for an incremental update.
787 Output_data_got_base*
788 init_got_plt_for_update(Symbol_table* symtab,
789 Layout* layout,
790 unsigned int got_count,
791 unsigned int plt_count);
792
793 // Reserve a GOT entry for a local symbol, and regenerate any
794 // necessary dynamic relocations.
795 void
796 reserve_local_got_entry(unsigned int got_index,
797 Sized_relobj<size, false>* obj,
798 unsigned int r_sym,
799 unsigned int got_type);
800
801 // Reserve a GOT entry for a global symbol, and regenerate any
802 // necessary dynamic relocations.
803 void
804 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
805 unsigned int got_type);
806
807 // Register an existing PLT entry for a global symbol.
808 void
809 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
810 Symbol* gsym);
811
812 // Force a COPY relocation for a given symbol.
813 void
814 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
815
816 // Apply an incremental relocation.
817 void
818 apply_relocation(const Relocate_info<size, false>* relinfo,
819 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
820 unsigned int r_type,
821 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
822 const Symbol* gsym,
823 unsigned char* view,
824 typename elfcpp::Elf_types<size>::Elf_Addr address,
825 section_size_type view_size);
826
827 // Add a new reloc argument, returning the index in the vector.
828 size_t
829 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
830 {
831 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
832 return this->tlsdesc_reloc_info_.size() - 1;
833 }
834
835 Output_data_plt_x86_64<size>*
836 make_data_plt(Layout* layout,
837 Output_data_got<64, false>* got,
838 Output_data_got_plt_x86_64* got_plt,
839 Output_data_space* got_irelative)
840 {
841 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
842 }
843
844 Output_data_plt_x86_64<size>*
845 make_data_plt(Layout* layout,
846 Output_data_got<64, false>* got,
847 Output_data_got_plt_x86_64* got_plt,
848 Output_data_space* got_irelative,
849 unsigned int plt_count)
850 {
851 return this->do_make_data_plt(layout, got, got_plt, got_irelative,
852 plt_count);
853 }
854
855 virtual Output_data_plt_x86_64<size>*
856 do_make_data_plt(Layout* layout,
857 Output_data_got<64, false>* got,
858 Output_data_got_plt_x86_64* got_plt,
859 Output_data_space* got_irelative);
860
861 virtual Output_data_plt_x86_64<size>*
862 do_make_data_plt(Layout* layout,
863 Output_data_got<64, false>* got,
864 Output_data_got_plt_x86_64* got_plt,
865 Output_data_space* got_irelative,
866 unsigned int plt_count);
867
868 private:
869 // The class which scans relocations.
870 class Scan
871 {
872 public:
873 Scan()
874 : issued_non_pic_error_(false)
875 { }
876
877 static inline int
878 get_reference_flags(unsigned int r_type);
879
880 inline void
881 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
882 Sized_relobj_file<size, false>* object,
883 unsigned int data_shndx,
884 Output_section* output_section,
885 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
886 const elfcpp::Sym<size, false>& lsym,
887 bool is_discarded);
888
889 inline void
890 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
891 Sized_relobj_file<size, false>* object,
892 unsigned int data_shndx,
893 Output_section* output_section,
894 const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
895 Symbol* gsym);
896
897 inline bool
898 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
899 Target_x86_64* target,
900 Sized_relobj_file<size, false>* object,
901 unsigned int data_shndx,
902 Output_section* output_section,
903 const elfcpp::Rela<size, false>& reloc,
904 unsigned int r_type,
905 const elfcpp::Sym<size, false>& lsym);
906
907 inline bool
908 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
909 Target_x86_64* target,
910 Sized_relobj_file<size, false>* object,
911 unsigned int data_shndx,
912 Output_section* output_section,
913 const elfcpp::Rela<size, false>& reloc,
914 unsigned int r_type,
915 Symbol* gsym);
916
917 private:
918 static void
919 unsupported_reloc_local(Sized_relobj_file<size, false>*,
920 unsigned int r_type);
921
922 static void
923 unsupported_reloc_global(Sized_relobj_file<size, false>*,
924 unsigned int r_type, Symbol*);
925
926 void
927 check_non_pic(Relobj*, unsigned int r_type, Symbol*);
928
929 inline bool
930 possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
931 unsigned int src_indx,
932 unsigned int r_offset,
933 unsigned int r_type);
934
935 bool
936 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
937 unsigned int r_type);
938
939 // Whether we have issued an error about a non-PIC compilation.
940 bool issued_non_pic_error_;
941 };
942
943 // The class which implements relocation.
944 class Relocate
945 {
946 public:
947 Relocate()
948 : skip_call_tls_get_addr_(false)
949 { }
950
951 ~Relocate()
952 {
953 if (this->skip_call_tls_get_addr_)
954 {
955 // FIXME: This needs to specify the location somehow.
956 gold_error(_("missing expected TLS relocation"));
957 }
958 }
959
960 // Do a relocation. Return false if the caller should not issue
961 // any warnings about this relocation.
962 inline bool
963 relocate(const Relocate_info<size, false>*, unsigned int,
964 Target_x86_64*, Output_section*, size_t, const unsigned char*,
965 const Sized_symbol<size>*, const Symbol_value<size>*,
966 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
967 section_size_type);
968
969 private:
970 // Do a TLS relocation.
971 inline void
972 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
973 size_t relnum, const elfcpp::Rela<size, false>&,
974 unsigned int r_type, const Sized_symbol<size>*,
975 const Symbol_value<size>*,
976 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
977 section_size_type);
978
979 // Do a TLS General-Dynamic to Initial-Exec transition.
980 inline void
981 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
982 const elfcpp::Rela<size, false>&, unsigned int r_type,
983 typename elfcpp::Elf_types<size>::Elf_Addr value,
984 unsigned char* view,
985 typename elfcpp::Elf_types<size>::Elf_Addr,
986 section_size_type view_size);
987
988 // Do a TLS General-Dynamic to Local-Exec transition.
989 inline void
990 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
991 Output_segment* tls_segment,
992 const elfcpp::Rela<size, false>&, unsigned int r_type,
993 typename elfcpp::Elf_types<size>::Elf_Addr value,
994 unsigned char* view,
995 section_size_type view_size);
996
997 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
998 inline void
999 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
1000 const elfcpp::Rela<size, false>&, unsigned int r_type,
1001 typename elfcpp::Elf_types<size>::Elf_Addr value,
1002 unsigned char* view,
1003 typename elfcpp::Elf_types<size>::Elf_Addr,
1004 section_size_type view_size);
1005
1006 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1007 inline void
1008 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
1009 Output_segment* tls_segment,
1010 const elfcpp::Rela<size, false>&, unsigned int r_type,
1011 typename elfcpp::Elf_types<size>::Elf_Addr value,
1012 unsigned char* view,
1013 section_size_type view_size);
1014
1015 // Do a TLS Local-Dynamic to Local-Exec transition.
1016 inline void
1017 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
1018 Output_segment* tls_segment,
1019 const elfcpp::Rela<size, false>&, unsigned int r_type,
1020 typename elfcpp::Elf_types<size>::Elf_Addr value,
1021 unsigned char* view,
1022 section_size_type view_size);
1023
1024 // Do a TLS Initial-Exec to Local-Exec transition.
1025 static inline void
1026 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
1027 Output_segment* tls_segment,
1028 const elfcpp::Rela<size, false>&, unsigned int r_type,
1029 typename elfcpp::Elf_types<size>::Elf_Addr value,
1030 unsigned char* view,
1031 section_size_type view_size);
1032
1033 // This is set if we should skip the next reloc, which should be a
1034 // PLT32 reloc against ___tls_get_addr.
1035 bool skip_call_tls_get_addr_;
1036 };
1037
1038 // Check if relocation against this symbol is a candidate for
1039 // conversion from
1040 // mov foo@GOTPCREL(%rip), %reg
1041 // to lea foo(%rip), %reg.
1042 template<class View_type>
1043 static inline bool
1044 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
1045 size_t r_offset, View_type* view)
1046 {
1047 gold_assert(gsym != NULL);
1048 // We cannot do the conversion unless it's one of these relocations.
1049 if (r_type != elfcpp::R_X86_64_GOTPCREL
1050 && r_type != elfcpp::R_X86_64_GOTPCRELX
1051 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
1052 return false;
1053 // We cannot convert references to IFUNC symbols, or to symbols that
1054 // are not local to the current module.
1055 // We can't do predefined symbols because they may become undefined
1056 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1057 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1058 || gsym->is_undefined()
1059 || gsym->is_predefined()
1060 || gsym->is_from_dynobj()
1061 || gsym->is_preemptible())
1062 return false;
1063 // If we are building a shared object and the symbol is protected, we may
1064 // need to go through the GOT.
1065 if (parameters->options().shared()
1066 && gsym->visibility() == elfcpp::STV_PROTECTED)
1067 return false;
1068 // We cannot convert references to the _DYNAMIC symbol.
1069 if (strcmp(gsym->name(), "_DYNAMIC") == 0)
1070 return false;
1071 // Check for a MOV opcode.
1072 return (*view)[r_offset - 2] == 0x8b;
1073 }
1074
1075 // Convert
1076 // callq *foo@GOTPCRELX(%rip) to
1077 // addr32 callq foo
1078 // and jmpq *foo@GOTPCRELX(%rip) to
1079 // jmpq foo
1080 // nop
1081 template<class View_type>
1082 static inline bool
1083 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
1084 size_t r_offset, View_type* view)
1085 {
1086 gold_assert(gsym != NULL);
1087 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1088 if (r_type != elfcpp::R_X86_64_GOTPCRELX)
1089 return false;
1090 // We cannot convert references to IFUNC symbols, or to symbols that
1091 // are not local to the current module.
1092 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1093 || gsym->is_undefined ()
1094 || gsym->is_from_dynobj()
1095 || gsym->is_preemptible())
1096 return false;
1097 // Check for a CALLQ or JMPQ opcode.
1098 return ((*view)[r_offset - 2] == 0xff
1099 && ((*view)[r_offset - 1] == 0x15
1100 || (*view)[r_offset - 1] == 0x25));
1101 }
1102
1103 // Adjust TLS relocation type based on the options and whether this
1104 // is a local symbol.
1105 static tls::Tls_optimization
1106 optimize_tls_reloc(bool is_final, int r_type);
1107
1108 // Get the GOT section, creating it if necessary.
1109 Output_data_got<64, false>*
1110 got_section(Symbol_table*, Layout*);
1111
1112 // Get the GOT PLT section.
1113 Output_data_got_plt_x86_64*
1114 got_plt_section() const
1115 {
1116 gold_assert(this->got_plt_ != NULL);
1117 return this->got_plt_;
1118 }
1119
1120 // Get the GOT section for TLSDESC entries.
1121 Output_data_got<64, false>*
1122 got_tlsdesc_section() const
1123 {
1124 gold_assert(this->got_tlsdesc_ != NULL);
1125 return this->got_tlsdesc_;
1126 }
1127
1128 // Create the PLT section.
1129 void
1130 make_plt_section(Symbol_table* symtab, Layout* layout);
1131
1132 // Create a PLT entry for a global symbol.
1133 void
1134 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1135
1136 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1137 void
1138 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1139 Sized_relobj_file<size, false>* relobj,
1140 unsigned int local_sym_index);
1141
1142 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1143 void
1144 define_tls_base_symbol(Symbol_table*, Layout*);
1145
1146 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1147 void
1148 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
1149
1150 // Create a GOT entry for the TLS module index.
1151 unsigned int
1152 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1153 Sized_relobj_file<size, false>* object);
1154
1155 // Get the PLT section.
1156 Output_data_plt_x86_64<size>*
1157 plt_section() const
1158 {
1159 gold_assert(this->plt_ != NULL);
1160 return this->plt_;
1161 }
1162
1163 // Get the dynamic reloc section, creating it if necessary.
1164 Reloc_section*
1165 rela_dyn_section(Layout*);
1166
1167 // Get the section to use for TLSDESC relocations.
1168 Reloc_section*
1169 rela_tlsdesc_section(Layout*) const;
1170
1171 // Get the section to use for IRELATIVE relocations.
1172 Reloc_section*
1173 rela_irelative_section(Layout*);
1174
1175 // Add a potential copy relocation.
1176 void
1177 copy_reloc(Symbol_table* symtab, Layout* layout,
1178 Sized_relobj_file<size, false>* object,
1179 unsigned int shndx, Output_section* output_section,
1180 Symbol* sym, const elfcpp::Rela<size, false>& reloc)
1181 {
1182 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1183 this->copy_relocs_.copy_reloc(symtab, layout,
1184 symtab->get_sized_symbol<size>(sym),
1185 object, shndx, output_section,
1186 r_type, reloc.get_r_offset(),
1187 reloc.get_r_addend(),
1188 this->rela_dyn_section(layout));
1189 }
1190
1191 // Information about this specific target which we pass to the
1192 // general Target structure.
1193 static const Target::Target_info x86_64_info;
1194
1195 // The types of GOT entries needed for this platform.
1196 // These values are exposed to the ABI in an incremental link.
1197 // Do not renumber existing values without changing the version
1198 // number of the .gnu_incremental_inputs section.
1199 enum Got_type
1200 {
1201 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1202 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1203 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1204 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
1205 };
1206
1207 // This type is used as the argument to the target specific
1208 // relocation routines. The only target specific reloc is
1209 // R_X86_64_TLSDESC against a local symbol.
1210 struct Tlsdesc_info
1211 {
1212 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
1213 : object(a_object), r_sym(a_r_sym)
1214 { }
1215
1216 // The object in which the local symbol is defined.
1217 Sized_relobj_file<size, false>* object;
1218 // The local symbol index in the object.
1219 unsigned int r_sym;
1220 };
1221
1222 // The GOT section.
1223 Output_data_got<64, false>* got_;
1224 // The PLT section.
1225 Output_data_plt_x86_64<size>* plt_;
1226 // The GOT PLT section.
1227 Output_data_got_plt_x86_64* got_plt_;
1228 // The GOT section for IRELATIVE relocations.
1229 Output_data_space* got_irelative_;
1230 // The GOT section for TLSDESC relocations.
1231 Output_data_got<64, false>* got_tlsdesc_;
1232 // The _GLOBAL_OFFSET_TABLE_ symbol.
1233 Symbol* global_offset_table_;
1234 // The dynamic reloc section.
1235 Reloc_section* rela_dyn_;
1236 // The section to use for IRELATIVE relocs.
1237 Reloc_section* rela_irelative_;
1238 // Relocs saved to avoid a COPY reloc.
1239 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
1240 // Offset of the GOT entry for the TLS module index.
1241 unsigned int got_mod_index_offset_;
1242 // We handle R_X86_64_TLSDESC against a local symbol as a target
1243 // specific relocation. Here we store the object and local symbol
1244 // index for the relocation.
1245 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
1246 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1247 bool tls_base_symbol_defined_;
1248 };
1249
1250 template<>
1251 const Target::Target_info Target_x86_64<64>::x86_64_info =
1252 {
1253 64, // size
1254 false, // is_big_endian
1255 elfcpp::EM_X86_64, // machine_code
1256 false, // has_make_symbol
1257 false, // has_resolve
1258 true, // has_code_fill
1259 true, // is_default_stack_executable
1260 true, // can_icf_inline_merge_sections
1261 '\0', // wrap_char
1262 "/lib/ld64.so.1", // program interpreter
1263 0x400000, // default_text_segment_address
1264 0x1000, // abi_pagesize (overridable by -z max-page-size)
1265 0x1000, // common_pagesize (overridable by -z common-page-size)
1266 false, // isolate_execinstr
1267 0, // rosegment_gap
1268 elfcpp::SHN_UNDEF, // small_common_shndx
1269 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1270 0, // small_common_section_flags
1271 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1272 NULL, // attributes_section
1273 NULL, // attributes_vendor
1274 "_start", // entry_symbol_name
1275 32, // hash_entry_size
1276 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1277 };
1278
1279 template<>
1280 const Target::Target_info Target_x86_64<32>::x86_64_info =
1281 {
1282 32, // size
1283 false, // is_big_endian
1284 elfcpp::EM_X86_64, // machine_code
1285 false, // has_make_symbol
1286 false, // has_resolve
1287 true, // has_code_fill
1288 true, // is_default_stack_executable
1289 true, // can_icf_inline_merge_sections
1290 '\0', // wrap_char
1291 "/libx32/ldx32.so.1", // program interpreter
1292 0x400000, // default_text_segment_address
1293 0x1000, // abi_pagesize (overridable by -z max-page-size)
1294 0x1000, // common_pagesize (overridable by -z common-page-size)
1295 false, // isolate_execinstr
1296 0, // rosegment_gap
1297 elfcpp::SHN_UNDEF, // small_common_shndx
1298 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
1299 0, // small_common_section_flags
1300 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
1301 NULL, // attributes_section
1302 NULL, // attributes_vendor
1303 "_start", // entry_symbol_name
1304 32, // hash_entry_size
1305 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
1306 };
1307
1308 // This is called when a new output section is created. This is where
1309 // we handle the SHF_X86_64_LARGE.
1310
1311 template<int size>
1312 void
1313 Target_x86_64<size>::do_new_output_section(Output_section* os) const
1314 {
1315 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
1316 os->set_is_large_section();
1317 }
1318
1319 // Get the GOT section, creating it if necessary.
1320
1321 template<int size>
1322 Output_data_got<64, false>*
1323 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
1324 {
1325 if (this->got_ == NULL)
1326 {
1327 gold_assert(symtab != NULL && layout != NULL);
1328
1329 // When using -z now, we can treat .got.plt as a relro section.
1330 // Without -z now, it is modified after program startup by lazy
1331 // PLT relocations.
1332 bool is_got_plt_relro = parameters->options().now();
1333 Output_section_order got_order = (is_got_plt_relro
1334 ? ORDER_RELRO
1335 : ORDER_RELRO_LAST);
1336 Output_section_order got_plt_order = (is_got_plt_relro
1337 ? ORDER_RELRO
1338 : ORDER_NON_RELRO_FIRST);
1339
1340 this->got_ = new Output_data_got<64, false>();
1341
1342 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1343 (elfcpp::SHF_ALLOC
1344 | elfcpp::SHF_WRITE),
1345 this->got_, got_order, true);
1346
1347 this->got_plt_ = new Output_data_got_plt_x86_64(layout);
1348 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1349 (elfcpp::SHF_ALLOC
1350 | elfcpp::SHF_WRITE),
1351 this->got_plt_, got_plt_order,
1352 is_got_plt_relro);
1353
1354 // The first three entries are reserved.
1355 this->got_plt_->set_current_data_size(3 * 8);
1356
1357 if (!is_got_plt_relro)
1358 {
1359 // Those bytes can go into the relro segment.
1360 layout->increase_relro(3 * 8);
1361 }
1362
1363 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1364 this->global_offset_table_ =
1365 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1366 Symbol_table::PREDEFINED,
1367 this->got_plt_,
1368 0, 0, elfcpp::STT_OBJECT,
1369 elfcpp::STB_LOCAL,
1370 elfcpp::STV_HIDDEN, 0,
1371 false, false);
1372
1373 // If there are any IRELATIVE relocations, they get GOT entries
1374 // in .got.plt after the jump slot entries.
1375 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
1376 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1377 (elfcpp::SHF_ALLOC
1378 | elfcpp::SHF_WRITE),
1379 this->got_irelative_,
1380 got_plt_order, is_got_plt_relro);
1381
1382 // If there are any TLSDESC relocations, they get GOT entries in
1383 // .got.plt after the jump slot and IRELATIVE entries.
1384 this->got_tlsdesc_ = new Output_data_got<64, false>();
1385 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1386 (elfcpp::SHF_ALLOC
1387 | elfcpp::SHF_WRITE),
1388 this->got_tlsdesc_,
1389 got_plt_order, is_got_plt_relro);
1390 }
1391
1392 return this->got_;
1393 }
1394
1395 // Get the dynamic reloc section, creating it if necessary.
1396
1397 template<int size>
1398 typename Target_x86_64<size>::Reloc_section*
1399 Target_x86_64<size>::rela_dyn_section(Layout* layout)
1400 {
1401 if (this->rela_dyn_ == NULL)
1402 {
1403 gold_assert(layout != NULL);
1404 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1405 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1406 elfcpp::SHF_ALLOC, this->rela_dyn_,
1407 ORDER_DYNAMIC_RELOCS, false);
1408 }
1409 return this->rela_dyn_;
1410 }
1411
1412 // Get the section to use for IRELATIVE relocs, creating it if
1413 // necessary. These go in .rela.dyn, but only after all other dynamic
1414 // relocations. They need to follow the other dynamic relocations so
1415 // that they can refer to global variables initialized by those
1416 // relocs.
1417
1418 template<int size>
1419 typename Target_x86_64<size>::Reloc_section*
1420 Target_x86_64<size>::rela_irelative_section(Layout* layout)
1421 {
1422 if (this->rela_irelative_ == NULL)
1423 {
1424 // Make sure we have already created the dynamic reloc section.
1425 this->rela_dyn_section(layout);
1426 this->rela_irelative_ = new Reloc_section(false);
1427 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1428 elfcpp::SHF_ALLOC, this->rela_irelative_,
1429 ORDER_DYNAMIC_RELOCS, false);
1430 gold_assert(this->rela_dyn_->output_section()
1431 == this->rela_irelative_->output_section());
1432 }
1433 return this->rela_irelative_;
1434 }
1435
1436 // Write the first three reserved words of the .got.plt section.
1437 // The remainder of the section is written while writing the PLT
1438 // in Output_data_plt_i386::do_write.
1439
1440 void
1441 Output_data_got_plt_x86_64::do_write(Output_file* of)
1442 {
1443 // The first entry in the GOT is the address of the .dynamic section
1444 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1445 // We saved space for them when we created the section in
1446 // Target_x86_64::got_section.
1447 const off_t got_file_offset = this->offset();
1448 gold_assert(this->data_size() >= 24);
1449 unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
1450 Output_section* dynamic = this->layout_->dynamic_section();
1451 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1452 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
1453 memset(got_view + 8, 0, 16);
1454 of->write_output_view(got_file_offset, 24, got_view);
1455 }
1456
1457 // Initialize the PLT section.
1458
1459 template<int size>
1460 void
1461 Output_data_plt_x86_64<size>::init(Layout* layout)
1462 {
1463 this->rel_ = new Reloc_section(false);
1464 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1465 elfcpp::SHF_ALLOC, this->rel_,
1466 ORDER_DYNAMIC_PLT_RELOCS, false);
1467 }
1468
1469 template<int size>
1470 void
1471 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
1472 {
1473 os->set_entsize(this->get_plt_entry_size());
1474 }
1475
1476 // Add an entry to the PLT.
1477
1478 template<int size>
1479 void
1480 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
1481 Symbol* gsym)
1482 {
1483 gold_assert(!gsym->has_plt_offset());
1484
1485 unsigned int plt_index;
1486 off_t plt_offset;
1487 section_offset_type got_offset;
1488
1489 unsigned int* pcount;
1490 unsigned int offset;
1491 unsigned int reserved;
1492 Output_section_data_build* got;
1493 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1494 && gsym->can_use_relative_reloc(false))
1495 {
1496 pcount = &this->irelative_count_;
1497 offset = 0;
1498 reserved = 0;
1499 got = this->got_irelative_;
1500 }
1501 else
1502 {
1503 pcount = &this->count_;
1504 offset = 1;
1505 reserved = 3;
1506 got = this->got_plt_;
1507 }
1508
1509 if (!this->is_data_size_valid())
1510 {
1511 // Note that when setting the PLT offset for a non-IRELATIVE
1512 // entry we skip the initial reserved PLT entry.
1513 plt_index = *pcount + offset;
1514 plt_offset = plt_index * this->get_plt_entry_size();
1515
1516 ++*pcount;
1517
1518 got_offset = (plt_index - offset + reserved) * 8;
1519 gold_assert(got_offset == got->current_data_size());
1520
1521 // Every PLT entry needs a GOT entry which points back to the PLT
1522 // entry (this will be changed by the dynamic linker, normally
1523 // lazily when the function is called).
1524 got->set_current_data_size(got_offset + 8);
1525 }
1526 else
1527 {
1528 // FIXME: This is probably not correct for IRELATIVE relocs.
1529
1530 // For incremental updates, find an available slot.
1531 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
1532 this->get_plt_entry_size(), 0);
1533 if (plt_offset == -1)
1534 gold_fallback(_("out of patch space (PLT);"
1535 " relink with --incremental-full"));
1536
1537 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1538 // can be calculated from the PLT index, adjusting for the three
1539 // reserved entries at the beginning of the GOT.
1540 plt_index = plt_offset / this->get_plt_entry_size() - 1;
1541 got_offset = (plt_index - offset + reserved) * 8;
1542 }
1543
1544 gsym->set_plt_offset(plt_offset);
1545
1546 // Every PLT entry needs a reloc.
1547 this->add_relocation(symtab, layout, gsym, got_offset);
1548
1549 // Note that we don't need to save the symbol. The contents of the
1550 // PLT are independent of which symbols are used. The symbols only
1551 // appear in the relocations.
1552 }
1553
1554 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1555 // the PLT offset.
1556
1557 template<int size>
1558 unsigned int
1559 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
1560 Symbol_table* symtab,
1561 Layout* layout,
1562 Sized_relobj_file<size, false>* relobj,
1563 unsigned int local_sym_index)
1564 {
1565 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1566 ++this->irelative_count_;
1567
1568 section_offset_type got_offset = this->got_irelative_->current_data_size();
1569
1570 // Every PLT entry needs a GOT entry which points back to the PLT
1571 // entry.
1572 this->got_irelative_->set_current_data_size(got_offset + 8);
1573
1574 // Every PLT entry needs a reloc.
1575 Reloc_section* rela = this->rela_irelative(symtab, layout);
1576 rela->add_symbolless_local_addend(relobj, local_sym_index,
1577 elfcpp::R_X86_64_IRELATIVE,
1578 this->got_irelative_, got_offset, 0);
1579
1580 return plt_offset;
1581 }
1582
1583 // Add the relocation for a PLT entry.
1584
1585 template<int size>
1586 void
1587 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
1588 Layout* layout,
1589 Symbol* gsym,
1590 unsigned int got_offset)
1591 {
1592 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1593 && gsym->can_use_relative_reloc(false))
1594 {
1595 Reloc_section* rela = this->rela_irelative(symtab, layout);
1596 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
1597 this->got_irelative_, got_offset, 0);
1598 }
1599 else
1600 {
1601 gsym->set_needs_dynsym_entry();
1602 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
1603 got_offset, 0);
1604 }
1605 }
1606
1607 // Return where the TLSDESC relocations should go, creating it if
1608 // necessary. These follow the JUMP_SLOT relocations.
1609
1610 template<int size>
1611 typename Output_data_plt_x86_64<size>::Reloc_section*
1612 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
1613 {
1614 if (this->tlsdesc_rel_ == NULL)
1615 {
1616 this->tlsdesc_rel_ = new Reloc_section(false);
1617 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1618 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
1619 ORDER_DYNAMIC_PLT_RELOCS, false);
1620 gold_assert(this->tlsdesc_rel_->output_section()
1621 == this->rel_->output_section());
1622 }
1623 return this->tlsdesc_rel_;
1624 }
1625
1626 // Return where the IRELATIVE relocations should go in the PLT. These
1627 // follow the JUMP_SLOT and the TLSDESC relocations.
1628
1629 template<int size>
1630 typename Output_data_plt_x86_64<size>::Reloc_section*
1631 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
1632 Layout* layout)
1633 {
1634 if (this->irelative_rel_ == NULL)
1635 {
1636 // Make sure we have a place for the TLSDESC relocations, in
1637 // case we see any later on.
1638 this->rela_tlsdesc(layout);
1639 this->irelative_rel_ = new Reloc_section(false);
1640 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1641 elfcpp::SHF_ALLOC, this->irelative_rel_,
1642 ORDER_DYNAMIC_PLT_RELOCS, false);
1643 gold_assert(this->irelative_rel_->output_section()
1644 == this->rel_->output_section());
1645
1646 if (parameters->doing_static_link())
1647 {
1648 // A statically linked executable will only have a .rela.plt
1649 // section to hold R_X86_64_IRELATIVE relocs for
1650 // STT_GNU_IFUNC symbols. The library will use these
1651 // symbols to locate the IRELATIVE relocs at program startup
1652 // time.
1653 symtab->define_in_output_data("__rela_iplt_start", NULL,
1654 Symbol_table::PREDEFINED,
1655 this->irelative_rel_, 0, 0,
1656 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1657 elfcpp::STV_HIDDEN, 0, false, true);
1658 symtab->define_in_output_data("__rela_iplt_end", NULL,
1659 Symbol_table::PREDEFINED,
1660 this->irelative_rel_, 0, 0,
1661 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1662 elfcpp::STV_HIDDEN, 0, true, true);
1663 }
1664 }
1665 return this->irelative_rel_;
1666 }
1667
1668 // Return the PLT address to use for a global symbol.
1669
1670 template<int size>
1671 uint64_t
1672 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
1673 {
1674 uint64_t offset = 0;
1675 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1676 && gsym->can_use_relative_reloc(false))
1677 offset = (this->count_ + 1) * this->get_plt_entry_size();
1678 return this->address() + offset + gsym->plt_offset();
1679 }
1680
1681 // Return the PLT address to use for a local symbol. These are always
1682 // IRELATIVE relocs.
1683
1684 template<int size>
1685 uint64_t
1686 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
1687 unsigned int r_sym)
1688 {
1689 return (this->address()
1690 + (this->count_ + 1) * this->get_plt_entry_size()
1691 + object->local_plt_offset(r_sym));
1692 }
1693
1694 // Set the final size.
1695 template<int size>
1696 void
1697 Output_data_plt_x86_64<size>::set_final_data_size()
1698 {
1699 // Number of regular and IFUNC PLT entries, plus the first entry.
1700 unsigned int count = this->count_ + this->irelative_count_ + 1;
1701 // Count the TLSDESC entry, if present.
1702 if (this->has_tlsdesc_entry())
1703 ++count;
1704 this->set_data_size(count * this->get_plt_entry_size());
1705 }
1706
1707 // The first entry in the PLT for an executable.
1708
1709 template<int size>
1710 const unsigned char
1711 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
1712 {
1713 // From AMD64 ABI Draft 0.98, page 76
1714 0xff, 0x35, // pushq contents of memory address
1715 0, 0, 0, 0, // replaced with address of .got + 8
1716 0xff, 0x25, // jmp indirect
1717 0, 0, 0, 0, // replaced with address of .got + 16
1718 0x90, 0x90, 0x90, 0x90 // noop (x4)
1719 };
1720
1721 template<int size>
1722 void
1723 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
1724 unsigned char* pov,
1725 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1726 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1727 {
1728 memcpy(pov, first_plt_entry, plt_entry_size);
1729 // We do a jmp relative to the PC at the end of this instruction.
1730 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1731 (got_address + 8
1732 - (plt_address + 6)));
1733 elfcpp::Swap<32, false>::writeval(pov + 8,
1734 (got_address + 16
1735 - (plt_address + 12)));
1736 }
1737
1738 // Subsequent entries in the PLT for an executable.
1739
1740 template<int size>
1741 const unsigned char
1742 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
1743 {
1744 // From AMD64 ABI Draft 0.98, page 76
1745 0xff, 0x25, // jmpq indirect
1746 0, 0, 0, 0, // replaced with address of symbol in .got
1747 0x68, // pushq immediate
1748 0, 0, 0, 0, // replaced with offset into relocation table
1749 0xe9, // jmpq relative
1750 0, 0, 0, 0 // replaced with offset to start of .plt
1751 };
1752
1753 template<int size>
1754 unsigned int
1755 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
1756 unsigned char* pov,
1757 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1758 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1759 unsigned int got_offset,
1760 unsigned int plt_offset,
1761 unsigned int plt_index)
1762 {
1763 // Check PC-relative offset overflow in PLT entry.
1764 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1765 - (plt_address + plt_offset + 6));
1766 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1767 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1768 plt_index + 1);
1769
1770 memcpy(pov, plt_entry, plt_entry_size);
1771 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1772 plt_got_pcrel_offset);
1773
1774 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1775 elfcpp::Swap<32, false>::writeval(pov + 12,
1776 - (plt_offset + plt_entry_size));
1777
1778 return 6;
1779 }
1780
1781 // The reserved TLSDESC entry in the PLT for an executable.
1782
1783 template<int size>
1784 const unsigned char
1785 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
1786 {
1787 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1788 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1789 0xff, 0x35, // pushq x(%rip)
1790 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1791 0xff, 0x25, // jmpq *y(%rip)
1792 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1793 0x0f, 0x1f, // nop
1794 0x40, 0
1795 };
1796
1797 template<int size>
1798 void
1799 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
1800 unsigned char* pov,
1801 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1802 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1803 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
1804 unsigned int tlsdesc_got_offset,
1805 unsigned int plt_offset)
1806 {
1807 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1808 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1809 (got_address + 8
1810 - (plt_address + plt_offset
1811 + 6)));
1812 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1813 (got_base
1814 + tlsdesc_got_offset
1815 - (plt_address + plt_offset
1816 + 12)));
1817 }
1818
1819 // Return the APLT address to use for a global symbol (for -z bndplt).
1820
1821 uint64_t
1822 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
1823 {
1824 uint64_t offset = this->aplt_offset_;
1825 // Convert the PLT offset into an APLT offset.
1826 unsigned int plt_offset = gsym->plt_offset();
1827 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1828 && gsym->can_use_relative_reloc(false))
1829 offset += this->regular_count() * aplt_entry_size;
1830 else
1831 plt_offset -= plt_entry_size;
1832 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
1833 return this->address() + offset + plt_offset;
1834 }
1835
1836 // Return the PLT address to use for a local symbol. These are always
1837 // IRELATIVE relocs.
1838
1839 uint64_t
1840 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
1841 unsigned int r_sym)
1842 {
1843 // Convert the PLT offset into an APLT offset.
1844 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size)
1845 / (plt_entry_size / aplt_entry_size));
1846 return (this->address()
1847 + this->aplt_offset_
1848 + this->regular_count() * aplt_entry_size
1849 + plt_offset);
1850 }
1851
1852 // Set the final size.
1853 void
1854 Output_data_plt_x86_64_bnd::set_final_data_size()
1855 {
1856 // Number of regular and IFUNC PLT entries.
1857 unsigned int count = this->entry_count();
1858 // Count the first entry and the TLSDESC entry, if present.
1859 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
1860 unsigned int plt_size = (count + extra) * plt_entry_size;
1861 // Offset of the APLT.
1862 this->aplt_offset_ = plt_size;
1863 // Size of the APLT.
1864 plt_size += count * aplt_entry_size;
1865 this->set_data_size(plt_size);
1866 }
1867
1868 // The first entry in the BND PLT.
1869
1870 const unsigned char
1871 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
1872 {
1873 // From AMD64 ABI Draft 0.98, page 76
1874 0xff, 0x35, // pushq contents of memory address
1875 0, 0, 0, 0, // replaced with address of .got + 8
1876 0xf2, 0xff, 0x25, // bnd jmp indirect
1877 0, 0, 0, 0, // replaced with address of .got + 16
1878 0x0f, 0x1f, 0x00 // nop
1879 };
1880
1881 void
1882 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
1883 unsigned char* pov,
1884 elfcpp::Elf_types<64>::Elf_Addr got_address,
1885 elfcpp::Elf_types<64>::Elf_Addr plt_address)
1886 {
1887 memcpy(pov, first_plt_entry, plt_entry_size);
1888 // We do a jmp relative to the PC at the end of this instruction.
1889 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1890 (got_address + 8
1891 - (plt_address + 6)));
1892 elfcpp::Swap<32, false>::writeval(pov + 9,
1893 (got_address + 16
1894 - (plt_address + 13)));
1895 }
1896
1897 // Subsequent entries in the BND PLT.
1898
1899 const unsigned char
1900 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
1901 {
1902 // From AMD64 ABI Draft 0.99.8, page 139
1903 0x68, // pushq immediate
1904 0, 0, 0, 0, // replaced with offset into relocation table
1905 0xf2, 0xe9, // bnd jmpq relative
1906 0, 0, 0, 0, // replaced with offset to start of .plt
1907 0x0f, 0x1f, 0x44, 0, 0 // nop
1908 };
1909
1910 // Entries in the BND Additional PLT.
1911
1912 const unsigned char
1913 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
1914 {
1915 // From AMD64 ABI Draft 0.99.8, page 139
1916 0xf2, 0xff, 0x25, // bnd jmpq indirect
1917 0, 0, 0, 0, // replaced with address of symbol in .got
1918 0x90, // nop
1919 };
1920
1921 unsigned int
1922 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
1923 unsigned char* pov,
1924 elfcpp::Elf_types<64>::Elf_Addr,
1925 elfcpp::Elf_types<64>::Elf_Addr,
1926 unsigned int,
1927 unsigned int plt_offset,
1928 unsigned int plt_index)
1929 {
1930 memcpy(pov, plt_entry, plt_entry_size);
1931 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
1932 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
1933 return 0;
1934 }
1935
1936 void
1937 Output_data_plt_x86_64_bnd::fill_aplt_entry(
1938 unsigned char* pov,
1939 elfcpp::Elf_types<64>::Elf_Addr got_address,
1940 elfcpp::Elf_types<64>::Elf_Addr plt_address,
1941 unsigned int got_offset,
1942 unsigned int plt_offset,
1943 unsigned int plt_index)
1944 {
1945 // Check PC-relative offset overflow in PLT entry.
1946 uint64_t plt_got_pcrel_offset = (got_address + got_offset
1947 - (plt_address + plt_offset + 7));
1948 if (Bits<32>::has_overflow(plt_got_pcrel_offset))
1949 gold_error(_("PC-relative offset overflow in APLT entry %d"),
1950 plt_index + 1);
1951
1952 memcpy(pov, aplt_entry, aplt_entry_size);
1953 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
1954 }
1955
1956 // The reserved TLSDESC entry in the PLT for an executable.
1957
1958 const unsigned char
1959 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
1960 {
1961 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1962 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1963 0xff, 0x35, // pushq x(%rip)
1964 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1965 0xf2, 0xff, 0x25, // jmpq *y(%rip)
1966 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1967 0x0f, 0x1f, 0 // nop
1968 };
1969
1970 void
1971 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
1972 unsigned char* pov,
1973 elfcpp::Elf_types<64>::Elf_Addr got_address,
1974 elfcpp::Elf_types<64>::Elf_Addr plt_address,
1975 elfcpp::Elf_types<64>::Elf_Addr got_base,
1976 unsigned int tlsdesc_got_offset,
1977 unsigned int plt_offset)
1978 {
1979 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1980 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1981 (got_address + 8
1982 - (plt_address + plt_offset
1983 + 6)));
1984 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
1985 (got_base
1986 + tlsdesc_got_offset
1987 - (plt_address + plt_offset
1988 + 13)));
1989 }
1990
1991 // The .eh_frame unwind information for the PLT.
1992
1993 template<int size>
1994 const unsigned char
1995 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1996 {
1997 1, // CIE version.
1998 'z', // Augmentation: augmentation size included.
1999 'R', // Augmentation: FDE encoding included.
2000 '\0', // End of augmentation string.
2001 1, // Code alignment factor.
2002 0x78, // Data alignment factor.
2003 16, // Return address column.
2004 1, // Augmentation size.
2005 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
2006 | elfcpp::DW_EH_PE_sdata4),
2007 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2008 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2009 elfcpp::DW_CFA_nop, // Align to 16 bytes.
2010 elfcpp::DW_CFA_nop
2011 };
2012
2013 template<int size>
2014 const unsigned char
2015 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2016 {
2017 0, 0, 0, 0, // Replaced with offset to .plt.
2018 0, 0, 0, 0, // Replaced with size of .plt.
2019 0, // Augmentation size.
2020 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2021 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2022 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2023 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2024 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2025 11, // Block length.
2026 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2027 elfcpp::DW_OP_breg16, 0, // Push %rip.
2028 elfcpp::DW_OP_lit15, // Push 0xf.
2029 elfcpp::DW_OP_and, // & (%rip & 0xf).
2030 elfcpp::DW_OP_lit11, // Push 0xb.
2031 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
2032 elfcpp::DW_OP_lit3, // Push 3.
2033 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
2034 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2035 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2036 elfcpp::DW_CFA_nop,
2037 elfcpp::DW_CFA_nop,
2038 elfcpp::DW_CFA_nop
2039 };
2040
2041 // The .eh_frame unwind information for the BND PLT.
2042 const unsigned char
2043 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
2044 {
2045 0, 0, 0, 0, // Replaced with offset to .plt.
2046 0, 0, 0, 0, // Replaced with size of .plt.
2047 0, // Augmentation size.
2048 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
2049 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
2050 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
2051 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
2052 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
2053 11, // Block length.
2054 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
2055 elfcpp::DW_OP_breg16, 0, // Push %rip.
2056 elfcpp::DW_OP_lit15, // Push 0xf.
2057 elfcpp::DW_OP_and, // & (%rip & 0xf).
2058 elfcpp::DW_OP_lit5, // Push 5.
2059 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
2060 elfcpp::DW_OP_lit3, // Push 3.
2061 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
2062 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2063 elfcpp::DW_CFA_nop, // Align to 32 bytes.
2064 elfcpp::DW_CFA_nop,
2065 elfcpp::DW_CFA_nop,
2066 elfcpp::DW_CFA_nop
2067 };
2068
2069 // Write out the PLT. This uses the hand-coded instructions above,
2070 // and adjusts them as needed. This is specified by the AMD64 ABI.
2071
2072 template<int size>
2073 void
2074 Output_data_plt_x86_64<size>::do_write(Output_file* of)
2075 {
2076 const off_t offset = this->offset();
2077 const section_size_type oview_size =
2078 convert_to_section_size_type(this->data_size());
2079 unsigned char* const oview = of->get_output_view(offset, oview_size);
2080
2081 const off_t got_file_offset = this->got_plt_->offset();
2082 gold_assert(parameters->incremental_update()
2083 || (got_file_offset + this->got_plt_->data_size()
2084 == this->got_irelative_->offset()));
2085 const section_size_type got_size =
2086 convert_to_section_size_type(this->got_plt_->data_size()
2087 + this->got_irelative_->data_size());
2088 unsigned char* const got_view = of->get_output_view(got_file_offset,
2089 got_size);
2090
2091 unsigned char* pov = oview;
2092
2093 // The base address of the .plt section.
2094 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2095 // The base address of the .got section.
2096 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
2097 // The base address of the PLT portion of the .got section,
2098 // which is where the GOT pointer will point, and where the
2099 // three reserved GOT entries are located.
2100 typename elfcpp::Elf_types<size>::Elf_Addr got_address
2101 = this->got_plt_->address();
2102
2103 this->fill_first_plt_entry(pov, got_address, plt_address);
2104 pov += this->get_plt_entry_size();
2105
2106 // The first three entries in the GOT are reserved, and are written
2107 // by Output_data_got_plt_x86_64::do_write.
2108 unsigned char* got_pov = got_view + 24;
2109
2110 unsigned int plt_offset = this->get_plt_entry_size();
2111 unsigned int got_offset = 24;
2112 const unsigned int count = this->count_ + this->irelative_count_;
2113 for (unsigned int plt_index = 0;
2114 plt_index < count;
2115 ++plt_index,
2116 pov += this->get_plt_entry_size(),
2117 got_pov += 8,
2118 plt_offset += this->get_plt_entry_size(),
2119 got_offset += 8)
2120 {
2121 // Set and adjust the PLT entry itself.
2122 unsigned int lazy_offset = this->fill_plt_entry(pov,
2123 got_address, plt_address,
2124 got_offset, plt_offset,
2125 plt_index);
2126
2127 // Set the entry in the GOT.
2128 elfcpp::Swap<64, false>::writeval(got_pov,
2129 plt_address + plt_offset + lazy_offset);
2130 }
2131
2132 if (this->has_tlsdesc_entry())
2133 {
2134 // Set and adjust the reserved TLSDESC PLT entry.
2135 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2136 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2137 tlsdesc_got_offset, plt_offset);
2138 pov += this->get_plt_entry_size();
2139 }
2140
2141 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2142 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2143
2144 of->write_output_view(offset, oview_size, oview);
2145 of->write_output_view(got_file_offset, got_size, got_view);
2146 }
2147
2148 // Write out the BND PLT.
2149
2150 void
2151 Output_data_plt_x86_64_bnd::do_write(Output_file* of)
2152 {
2153 const off_t offset = this->offset();
2154 const section_size_type oview_size =
2155 convert_to_section_size_type(this->data_size());
2156 unsigned char* const oview = of->get_output_view(offset, oview_size);
2157
2158 Output_data_got<64, false>* got = this->got();
2159 Output_data_got_plt_x86_64* got_plt = this->got_plt();
2160 Output_data_space* got_irelative = this->got_irelative();
2161
2162 const off_t got_file_offset = got_plt->offset();
2163 gold_assert(parameters->incremental_update()
2164 || (got_file_offset + got_plt->data_size()
2165 == got_irelative->offset()));
2166 const section_size_type got_size =
2167 convert_to_section_size_type(got_plt->data_size()
2168 + got_irelative->data_size());
2169 unsigned char* const got_view = of->get_output_view(got_file_offset,
2170 got_size);
2171
2172 unsigned char* pov = oview;
2173
2174 // The base address of the .plt section.
2175 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
2176 // The base address of the .got section.
2177 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
2178 // The base address of the PLT portion of the .got section,
2179 // which is where the GOT pointer will point, and where the
2180 // three reserved GOT entries are located.
2181 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
2182
2183 this->fill_first_plt_entry(pov, got_address, plt_address);
2184 pov += plt_entry_size;
2185
2186 // The first three entries in the GOT are reserved, and are written
2187 // by Output_data_got_plt_x86_64::do_write.
2188 unsigned char* got_pov = got_view + 24;
2189
2190 unsigned int plt_offset = plt_entry_size;
2191 unsigned int got_offset = 24;
2192 const unsigned int count = this->entry_count();
2193 for (unsigned int plt_index = 0;
2194 plt_index < count;
2195 ++plt_index,
2196 pov += plt_entry_size,
2197 got_pov += 8,
2198 plt_offset += plt_entry_size,
2199 got_offset += 8)
2200 {
2201 // Set and adjust the PLT entry itself.
2202 unsigned int lazy_offset = this->fill_plt_entry(pov,
2203 got_address, plt_address,
2204 got_offset, plt_offset,
2205 plt_index);
2206
2207 // Set the entry in the GOT.
2208 elfcpp::Swap<64, false>::writeval(got_pov,
2209 plt_address + plt_offset + lazy_offset);
2210 }
2211
2212 if (this->has_tlsdesc_entry())
2213 {
2214 // Set and adjust the reserved TLSDESC PLT entry.
2215 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
2216 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
2217 tlsdesc_got_offset, plt_offset);
2218 pov += this->get_plt_entry_size();
2219 }
2220
2221 // Write the additional PLT.
2222 got_offset = 24;
2223 for (unsigned int plt_index = 0;
2224 plt_index < count;
2225 ++plt_index,
2226 pov += aplt_entry_size,
2227 plt_offset += aplt_entry_size,
2228 got_offset += 8)
2229 {
2230 // Set and adjust the PLT entry itself.
2231 this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
2232 plt_offset, plt_index);
2233 }
2234
2235 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2236 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2237
2238 of->write_output_view(offset, oview_size, oview);
2239 of->write_output_view(got_file_offset, got_size, got_view);
2240 }
2241
2242 // Create the PLT section.
2243
2244 template<int size>
2245 void
2246 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
2247 {
2248 if (this->plt_ == NULL)
2249 {
2250 // Create the GOT sections first.
2251 this->got_section(symtab, layout);
2252
2253 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
2254 this->got_irelative_);
2255
2256 // Add unwind information if requested.
2257 if (parameters->options().ld_generated_unwind_info())
2258 this->plt_->add_eh_frame(layout);
2259
2260 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2261 (elfcpp::SHF_ALLOC
2262 | elfcpp::SHF_EXECINSTR),
2263 this->plt_, ORDER_PLT, false);
2264
2265 // Make the sh_info field of .rela.plt point to .plt.
2266 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2267 rela_plt_os->set_info_section(this->plt_->output_section());
2268 }
2269 }
2270
2271 template<>
2272 Output_data_plt_x86_64<32>*
2273 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2274 Output_data_got<64, false>* got,
2275 Output_data_got_plt_x86_64* got_plt,
2276 Output_data_space* got_irelative)
2277 {
2278 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2279 got_irelative);
2280 }
2281
2282 template<>
2283 Output_data_plt_x86_64<64>*
2284 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2285 Output_data_got<64, false>* got,
2286 Output_data_got_plt_x86_64* got_plt,
2287 Output_data_space* got_irelative)
2288 {
2289 if (parameters->options().bndplt())
2290 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2291 got_irelative);
2292 else
2293 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2294 got_irelative);
2295 }
2296
2297 template<>
2298 Output_data_plt_x86_64<32>*
2299 Target_x86_64<32>::do_make_data_plt(Layout* layout,
2300 Output_data_got<64, false>* got,
2301 Output_data_got_plt_x86_64* got_plt,
2302 Output_data_space* got_irelative,
2303 unsigned int plt_count)
2304 {
2305 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
2306 got_irelative,
2307 plt_count);
2308 }
2309
2310 template<>
2311 Output_data_plt_x86_64<64>*
2312 Target_x86_64<64>::do_make_data_plt(Layout* layout,
2313 Output_data_got<64, false>* got,
2314 Output_data_got_plt_x86_64* got_plt,
2315 Output_data_space* got_irelative,
2316 unsigned int plt_count)
2317 {
2318 if (parameters->options().bndplt())
2319 return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
2320 got_irelative, plt_count);
2321 else
2322 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
2323 got_irelative,
2324 plt_count);
2325 }
2326
2327 // Return the section for TLSDESC relocations.
2328
2329 template<int size>
2330 typename Target_x86_64<size>::Reloc_section*
2331 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
2332 {
2333 return this->plt_section()->rela_tlsdesc(layout);
2334 }
2335
2336 // Create a PLT entry for a global symbol.
2337
2338 template<int size>
2339 void
2340 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2341 Symbol* gsym)
2342 {
2343 if (gsym->has_plt_offset())
2344 return;
2345
2346 if (this->plt_ == NULL)
2347 this->make_plt_section(symtab, layout);
2348
2349 this->plt_->add_entry(symtab, layout, gsym);
2350 }
2351
2352 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2353
2354 template<int size>
2355 void
2356 Target_x86_64<size>::make_local_ifunc_plt_entry(
2357 Symbol_table* symtab, Layout* layout,
2358 Sized_relobj_file<size, false>* relobj,
2359 unsigned int local_sym_index)
2360 {
2361 if (relobj->local_has_plt_offset(local_sym_index))
2362 return;
2363 if (this->plt_ == NULL)
2364 this->make_plt_section(symtab, layout);
2365 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2366 relobj,
2367 local_sym_index);
2368 relobj->set_local_plt_offset(local_sym_index, plt_offset);
2369 }
2370
2371 // Return the number of entries in the PLT.
2372
2373 template<int size>
2374 unsigned int
2375 Target_x86_64<size>::plt_entry_count() const
2376 {
2377 if (this->plt_ == NULL)
2378 return 0;
2379 return this->plt_->entry_count();
2380 }
2381
2382 // Return the offset of the first non-reserved PLT entry.
2383
2384 template<int size>
2385 unsigned int
2386 Target_x86_64<size>::first_plt_entry_offset() const
2387 {
2388 if (this->plt_ == NULL)
2389 return 0;
2390 return this->plt_->first_plt_entry_offset();
2391 }
2392
2393 // Return the size of each PLT entry.
2394
2395 template<int size>
2396 unsigned int
2397 Target_x86_64<size>::plt_entry_size() const
2398 {
2399 if (this->plt_ == NULL)
2400 return 0;
2401 return this->plt_->get_plt_entry_size();
2402 }
2403
2404 // Create the GOT and PLT sections for an incremental update.
2405
2406 template<int size>
2407 Output_data_got_base*
2408 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
2409 Layout* layout,
2410 unsigned int got_count,
2411 unsigned int plt_count)
2412 {
2413 gold_assert(this->got_ == NULL);
2414
2415 this->got_ = new Output_data_got<64, false>(got_count * 8);
2416 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2417 (elfcpp::SHF_ALLOC
2418 | elfcpp::SHF_WRITE),
2419 this->got_, ORDER_RELRO_LAST,
2420 true);
2421
2422 // Add the three reserved entries.
2423 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
2424 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2425 (elfcpp::SHF_ALLOC
2426 | elfcpp::SHF_WRITE),
2427 this->got_plt_, ORDER_NON_RELRO_FIRST,
2428 false);
2429
2430 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2431 this->global_offset_table_ =
2432 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2433 Symbol_table::PREDEFINED,
2434 this->got_plt_,
2435 0, 0, elfcpp::STT_OBJECT,
2436 elfcpp::STB_LOCAL,
2437 elfcpp::STV_HIDDEN, 0,
2438 false, false);
2439
2440 // If there are any TLSDESC relocations, they get GOT entries in
2441 // .got.plt after the jump slot entries.
2442 // FIXME: Get the count for TLSDESC entries.
2443 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
2444 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2445 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2446 this->got_tlsdesc_,
2447 ORDER_NON_RELRO_FIRST, false);
2448
2449 // If there are any IRELATIVE relocations, they get GOT entries in
2450 // .got.plt after the jump slot and TLSDESC entries.
2451 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2452 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2453 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2454 this->got_irelative_,
2455 ORDER_NON_RELRO_FIRST, false);
2456
2457 // Create the PLT section.
2458 this->plt_ = this->make_data_plt(layout, this->got_,
2459 this->got_plt_,
2460 this->got_irelative_,
2461 plt_count);
2462
2463 // Add unwind information if requested.
2464 if (parameters->options().ld_generated_unwind_info())
2465 this->plt_->add_eh_frame(layout);
2466
2467 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2468 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2469 this->plt_, ORDER_PLT, false);
2470
2471 // Make the sh_info field of .rela.plt point to .plt.
2472 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2473 rela_plt_os->set_info_section(this->plt_->output_section());
2474
2475 // Create the rela_dyn section.
2476 this->rela_dyn_section(layout);
2477
2478 return this->got_;
2479 }
2480
2481 // Reserve a GOT entry for a local symbol, and regenerate any
2482 // necessary dynamic relocations.
2483
2484 template<int size>
2485 void
2486 Target_x86_64<size>::reserve_local_got_entry(
2487 unsigned int got_index,
2488 Sized_relobj<size, false>* obj,
2489 unsigned int r_sym,
2490 unsigned int got_type)
2491 {
2492 unsigned int got_offset = got_index * 8;
2493 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2494
2495 this->got_->reserve_local(got_index, obj, r_sym, got_type);
2496 switch (got_type)
2497 {
2498 case GOT_TYPE_STANDARD:
2499 if (parameters->options().output_is_position_independent())
2500 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
2501 this->got_, got_offset, 0, false);
2502 break;
2503 case GOT_TYPE_TLS_OFFSET:
2504 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
2505 this->got_, got_offset, 0);
2506 break;
2507 case GOT_TYPE_TLS_PAIR:
2508 this->got_->reserve_slot(got_index + 1);
2509 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
2510 this->got_, got_offset, 0);
2511 break;
2512 case GOT_TYPE_TLS_DESC:
2513 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2514 // this->got_->reserve_slot(got_index + 1);
2515 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2516 // this->got_, got_offset, 0);
2517 break;
2518 default:
2519 gold_unreachable();
2520 }
2521 }
2522
2523 // Reserve a GOT entry for a global symbol, and regenerate any
2524 // necessary dynamic relocations.
2525
2526 template<int size>
2527 void
2528 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
2529 Symbol* gsym,
2530 unsigned int got_type)
2531 {
2532 unsigned int got_offset = got_index * 8;
2533 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2534
2535 this->got_->reserve_global(got_index, gsym, got_type);
2536 switch (got_type)
2537 {
2538 case GOT_TYPE_STANDARD:
2539 if (!gsym->final_value_is_known())
2540 {
2541 if (gsym->is_from_dynobj()
2542 || gsym->is_undefined()
2543 || gsym->is_preemptible()
2544 || gsym->type() == elfcpp::STT_GNU_IFUNC)
2545 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
2546 this->got_, got_offset, 0);
2547 else
2548 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2549 this->got_, got_offset, 0, false);
2550 }
2551 break;
2552 case GOT_TYPE_TLS_OFFSET:
2553 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
2554 this->got_, got_offset, 0, false);
2555 break;
2556 case GOT_TYPE_TLS_PAIR:
2557 this->got_->reserve_slot(got_index + 1);
2558 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
2559 this->got_, got_offset, 0, false);
2560 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
2561 this->got_, got_offset + 8, 0, false);
2562 break;
2563 case GOT_TYPE_TLS_DESC:
2564 this->got_->reserve_slot(got_index + 1);
2565 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
2566 this->got_, got_offset, 0, false);
2567 break;
2568 default:
2569 gold_unreachable();
2570 }
2571 }
2572
2573 // Register an existing PLT entry for a global symbol.
2574
2575 template<int size>
2576 void
2577 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
2578 Layout* layout,
2579 unsigned int plt_index,
2580 Symbol* gsym)
2581 {
2582 gold_assert(this->plt_ != NULL);
2583 gold_assert(!gsym->has_plt_offset());
2584
2585 this->plt_->reserve_slot(plt_index);
2586
2587 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2588
2589 unsigned int got_offset = (plt_index + 3) * 8;
2590 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2591 }
2592
2593 // Force a COPY relocation for a given symbol.
2594
2595 template<int size>
2596 void
2597 Target_x86_64<size>::emit_copy_reloc(
2598 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2599 {
2600 this->copy_relocs_.emit_copy_reloc(symtab,
2601 symtab->get_sized_symbol<size>(sym),
2602 os,
2603 offset,
2604 this->rela_dyn_section(NULL));
2605 }
2606
2607 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2608
2609 template<int size>
2610 void
2611 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
2612 Layout* layout)
2613 {
2614 if (this->tls_base_symbol_defined_)
2615 return;
2616
2617 Output_segment* tls_segment = layout->tls_segment();
2618 if (tls_segment != NULL)
2619 {
2620 bool is_exec = parameters->options().output_is_executable();
2621 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
2622 Symbol_table::PREDEFINED,
2623 tls_segment, 0, 0,
2624 elfcpp::STT_TLS,
2625 elfcpp::STB_LOCAL,
2626 elfcpp::STV_HIDDEN, 0,
2627 (is_exec
2628 ? Symbol::SEGMENT_END
2629 : Symbol::SEGMENT_START),
2630 true);
2631 }
2632 this->tls_base_symbol_defined_ = true;
2633 }
2634
2635 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2636
2637 template<int size>
2638 void
2639 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
2640 Layout* layout)
2641 {
2642 if (this->plt_ == NULL)
2643 this->make_plt_section(symtab, layout);
2644
2645 if (!this->plt_->has_tlsdesc_entry())
2646 {
2647 // Allocate the TLSDESC_GOT entry.
2648 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2649 unsigned int got_offset = got->add_constant(0);
2650
2651 // Allocate the TLSDESC_PLT entry.
2652 this->plt_->reserve_tlsdesc_entry(got_offset);
2653 }
2654 }
2655
2656 // Create a GOT entry for the TLS module index.
2657
2658 template<int size>
2659 unsigned int
2660 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2661 Sized_relobj_file<size, false>* object)
2662 {
2663 if (this->got_mod_index_offset_ == -1U)
2664 {
2665 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2666 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2667 Output_data_got<64, false>* got = this->got_section(symtab, layout);
2668 unsigned int got_offset = got->add_constant(0);
2669 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
2670 got_offset, 0);
2671 got->add_constant(0);
2672 this->got_mod_index_offset_ = got_offset;
2673 }
2674 return this->got_mod_index_offset_;
2675 }
2676
2677 // Optimize the TLS relocation type based on what we know about the
2678 // symbol. IS_FINAL is true if the final address of this symbol is
2679 // known at link time.
2680
2681 template<int size>
2682 tls::Tls_optimization
2683 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
2684 {
2685 // If we are generating a shared library, then we can't do anything
2686 // in the linker.
2687 if (parameters->options().shared())
2688 return tls::TLSOPT_NONE;
2689
2690 switch (r_type)
2691 {
2692 case elfcpp::R_X86_64_TLSGD:
2693 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2694 case elfcpp::R_X86_64_TLSDESC_CALL:
2695 // These are General-Dynamic which permits fully general TLS
2696 // access. Since we know that we are generating an executable,
2697 // we can convert this to Initial-Exec. If we also know that
2698 // this is a local symbol, we can further switch to Local-Exec.
2699 if (is_final)
2700 return tls::TLSOPT_TO_LE;
2701 return tls::TLSOPT_TO_IE;
2702
2703 case elfcpp::R_X86_64_TLSLD:
2704 // This is Local-Dynamic, which refers to a local symbol in the
2705 // dynamic TLS block. Since we know that we generating an
2706 // executable, we can switch to Local-Exec.
2707 return tls::TLSOPT_TO_LE;
2708
2709 case elfcpp::R_X86_64_DTPOFF32:
2710 case elfcpp::R_X86_64_DTPOFF64:
2711 // Another Local-Dynamic reloc.
2712 return tls::TLSOPT_TO_LE;
2713
2714 case elfcpp::R_X86_64_GOTTPOFF:
2715 // These are Initial-Exec relocs which get the thread offset
2716 // from the GOT. If we know that we are linking against the
2717 // local symbol, we can switch to Local-Exec, which links the
2718 // thread offset into the instruction.
2719 if (is_final)
2720 return tls::TLSOPT_TO_LE;
2721 return tls::TLSOPT_NONE;
2722
2723 case elfcpp::R_X86_64_TPOFF32:
2724 // When we already have Local-Exec, there is nothing further we
2725 // can do.
2726 return tls::TLSOPT_NONE;
2727
2728 default:
2729 gold_unreachable();
2730 }
2731 }
2732
2733 // Get the Reference_flags for a particular relocation.
2734
2735 template<int size>
2736 int
2737 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
2738 {
2739 switch (r_type)
2740 {
2741 case elfcpp::R_X86_64_NONE:
2742 case elfcpp::R_X86_64_GNU_VTINHERIT:
2743 case elfcpp::R_X86_64_GNU_VTENTRY:
2744 case elfcpp::R_X86_64_GOTPC32:
2745 case elfcpp::R_X86_64_GOTPC64:
2746 // No symbol reference.
2747 return 0;
2748
2749 case elfcpp::R_X86_64_64:
2750 case elfcpp::R_X86_64_32:
2751 case elfcpp::R_X86_64_32S:
2752 case elfcpp::R_X86_64_16:
2753 case elfcpp::R_X86_64_8:
2754 return Symbol::ABSOLUTE_REF;
2755
2756 case elfcpp::R_X86_64_PC64:
2757 case elfcpp::R_X86_64_PC32:
2758 case elfcpp::R_X86_64_PC32_BND:
2759 case elfcpp::R_X86_64_PC16:
2760 case elfcpp::R_X86_64_PC8:
2761 case elfcpp::R_X86_64_GOTOFF64:
2762 return Symbol::RELATIVE_REF;
2763
2764 case elfcpp::R_X86_64_PLT32:
2765 case elfcpp::R_X86_64_PLT32_BND:
2766 case elfcpp::R_X86_64_PLTOFF64:
2767 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2768
2769 case elfcpp::R_X86_64_GOT64:
2770 case elfcpp::R_X86_64_GOT32:
2771 case elfcpp::R_X86_64_GOTPCREL64:
2772 case elfcpp::R_X86_64_GOTPCREL:
2773 case elfcpp::R_X86_64_GOTPCRELX:
2774 case elfcpp::R_X86_64_REX_GOTPCRELX:
2775 case elfcpp::R_X86_64_GOTPLT64:
2776 // Absolute in GOT.
2777 return Symbol::ABSOLUTE_REF;
2778
2779 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2780 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2781 case elfcpp::R_X86_64_TLSDESC_CALL:
2782 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2783 case elfcpp::R_X86_64_DTPOFF32:
2784 case elfcpp::R_X86_64_DTPOFF64:
2785 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2786 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2787 return Symbol::TLS_REF;
2788
2789 case elfcpp::R_X86_64_COPY:
2790 case elfcpp::R_X86_64_GLOB_DAT:
2791 case elfcpp::R_X86_64_JUMP_SLOT:
2792 case elfcpp::R_X86_64_RELATIVE:
2793 case elfcpp::R_X86_64_IRELATIVE:
2794 case elfcpp::R_X86_64_TPOFF64:
2795 case elfcpp::R_X86_64_DTPMOD64:
2796 case elfcpp::R_X86_64_TLSDESC:
2797 case elfcpp::R_X86_64_SIZE32:
2798 case elfcpp::R_X86_64_SIZE64:
2799 default:
2800 // Not expected. We will give an error later.
2801 return 0;
2802 }
2803 }
2804
2805 // Report an unsupported relocation against a local symbol.
2806
2807 template<int size>
2808 void
2809 Target_x86_64<size>::Scan::unsupported_reloc_local(
2810 Sized_relobj_file<size, false>* object,
2811 unsigned int r_type)
2812 {
2813 gold_error(_("%s: unsupported reloc %u against local symbol"),
2814 object->name().c_str(), r_type);
2815 }
2816
2817 // We are about to emit a dynamic relocation of type R_TYPE. If the
2818 // dynamic linker does not support it, issue an error. The GNU linker
2819 // only issues a non-PIC error for an allocated read-only section.
2820 // Here we know the section is allocated, but we don't know that it is
2821 // read-only. But we check for all the relocation types which the
2822 // glibc dynamic linker supports, so it seems appropriate to issue an
2823 // error even if the section is not read-only. If GSYM is not NULL,
2824 // it is the symbol the relocation is against; if it is NULL, the
2825 // relocation is against a local symbol.
2826
2827 template<int size>
2828 void
2829 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
2830 Symbol* gsym)
2831 {
2832 switch (r_type)
2833 {
2834 // These are the relocation types supported by glibc for x86_64
2835 // which should always work.
2836 case elfcpp::R_X86_64_RELATIVE:
2837 case elfcpp::R_X86_64_IRELATIVE:
2838 case elfcpp::R_X86_64_GLOB_DAT:
2839 case elfcpp::R_X86_64_JUMP_SLOT:
2840 case elfcpp::R_X86_64_DTPMOD64:
2841 case elfcpp::R_X86_64_DTPOFF64:
2842 case elfcpp::R_X86_64_TPOFF64:
2843 case elfcpp::R_X86_64_64:
2844 case elfcpp::R_X86_64_COPY:
2845 return;
2846
2847 // glibc supports these reloc types, but they can overflow.
2848 case elfcpp::R_X86_64_PC32:
2849 case elfcpp::R_X86_64_PC32_BND:
2850 // A PC relative reference is OK against a local symbol or if
2851 // the symbol is defined locally.
2852 if (gsym == NULL
2853 || (!gsym->is_from_dynobj()
2854 && !gsym->is_undefined()
2855 && !gsym->is_preemptible()))
2856 return;
2857 // Fall through.
2858 case elfcpp::R_X86_64_32:
2859 // R_X86_64_32 is OK for x32.
2860 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2861 return;
2862 if (this->issued_non_pic_error_)
2863 return;
2864 gold_assert(parameters->options().output_is_position_independent());
2865 if (gsym == NULL)
2866 object->error(_("requires dynamic R_X86_64_32 reloc which may "
2867 "overflow at runtime; recompile with -fPIC"));
2868 else
2869 {
2870 const char *r_name;
2871 switch (r_type)
2872 {
2873 case elfcpp::R_X86_64_32:
2874 r_name = "R_X86_64_32";
2875 break;
2876 case elfcpp::R_X86_64_PC32:
2877 r_name = "R_X86_64_PC32";
2878 break;
2879 case elfcpp::R_X86_64_PC32_BND:
2880 r_name = "R_X86_64_PC32_BND";
2881 break;
2882 default:
2883 gold_unreachable();
2884 break;
2885 }
2886 object->error(_("requires dynamic %s reloc against '%s' "
2887 "which may overflow at runtime; recompile "
2888 "with -fPIC"),
2889 r_name, gsym->name());
2890 }
2891 this->issued_non_pic_error_ = true;
2892 return;
2893
2894 default:
2895 // This prevents us from issuing more than one error per reloc
2896 // section. But we can still wind up issuing more than one
2897 // error per object file.
2898 if (this->issued_non_pic_error_)
2899 return;
2900 gold_assert(parameters->options().output_is_position_independent());
2901 object->error(_("requires unsupported dynamic reloc %u; "
2902 "recompile with -fPIC"),
2903 r_type);
2904 this->issued_non_pic_error_ = true;
2905 return;
2906
2907 case elfcpp::R_X86_64_NONE:
2908 gold_unreachable();
2909 }
2910 }
2911
2912 // Return whether we need to make a PLT entry for a relocation of the
2913 // given type against a STT_GNU_IFUNC symbol.
2914
2915 template<int size>
2916 bool
2917 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
2918 Sized_relobj_file<size, false>* object,
2919 unsigned int r_type)
2920 {
2921 int flags = Scan::get_reference_flags(r_type);
2922 if (flags & Symbol::TLS_REF)
2923 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2924 object->name().c_str(), r_type);
2925 return flags != 0;
2926 }
2927
2928 // Scan a relocation for a local symbol.
2929
2930 template<int size>
2931 inline void
2932 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
2933 Layout* layout,
2934 Target_x86_64<size>* target,
2935 Sized_relobj_file<size, false>* object,
2936 unsigned int data_shndx,
2937 Output_section* output_section,
2938 const elfcpp::Rela<size, false>& reloc,
2939 unsigned int r_type,
2940 const elfcpp::Sym<size, false>& lsym,
2941 bool is_discarded)
2942 {
2943 if (is_discarded)
2944 return;
2945
2946 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2947 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2948 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2949 {
2950 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2951 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2952 }
2953
2954 switch (r_type)
2955 {
2956 case elfcpp::R_X86_64_NONE:
2957 case elfcpp::R_X86_64_GNU_VTINHERIT:
2958 case elfcpp::R_X86_64_GNU_VTENTRY:
2959 break;
2960
2961 case elfcpp::R_X86_64_64:
2962 // If building a shared library (or a position-independent
2963 // executable), we need to create a dynamic relocation for this
2964 // location. The relocation applied at link time will apply the
2965 // link-time value, so we flag the location with an
2966 // R_X86_64_RELATIVE relocation so the dynamic loader can
2967 // relocate it easily.
2968 if (parameters->options().output_is_position_independent())
2969 {
2970 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2971 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2972 rela_dyn->add_local_relative(object, r_sym,
2973 (size == 32
2974 ? elfcpp::R_X86_64_RELATIVE64
2975 : elfcpp::R_X86_64_RELATIVE),
2976 output_section, data_shndx,
2977 reloc.get_r_offset(),
2978 reloc.get_r_addend(), is_ifunc);
2979 }
2980 break;
2981
2982 case elfcpp::R_X86_64_32:
2983 case elfcpp::R_X86_64_32S:
2984 case elfcpp::R_X86_64_16:
2985 case elfcpp::R_X86_64_8:
2986 // If building a shared library (or a position-independent
2987 // executable), we need to create a dynamic relocation for this
2988 // location. We can't use an R_X86_64_RELATIVE relocation
2989 // because that is always a 64-bit relocation.
2990 if (parameters->options().output_is_position_independent())
2991 {
2992 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2993 if (size == 32 && r_type == elfcpp::R_X86_64_32)
2994 {
2995 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2996 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2997 rela_dyn->add_local_relative(object, r_sym,
2998 elfcpp::R_X86_64_RELATIVE,
2999 output_section, data_shndx,
3000 reloc.get_r_offset(),
3001 reloc.get_r_addend(), is_ifunc);
3002 break;
3003 }
3004
3005 this->check_non_pic(object, r_type, NULL);
3006
3007 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3008 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3009 if (lsym.get_st_type() != elfcpp::STT_SECTION)
3010 rela_dyn->add_local(object, r_sym, r_type, output_section,
3011 data_shndx, reloc.get_r_offset(),
3012 reloc.get_r_addend());
3013 else
3014 {
3015 gold_assert(lsym.get_st_value() == 0);
3016 unsigned int shndx = lsym.get_st_shndx();
3017 bool is_ordinary;
3018 shndx = object->adjust_sym_shndx(r_sym, shndx,
3019 &is_ordinary);
3020 if (!is_ordinary)
3021 object->error(_("section symbol %u has bad shndx %u"),
3022 r_sym, shndx);
3023 else
3024 rela_dyn->add_local_section(object, shndx,
3025 r_type, output_section,
3026 data_shndx, reloc.get_r_offset(),
3027 reloc.get_r_addend());
3028 }
3029 }
3030 break;
3031
3032 case elfcpp::R_X86_64_PC64:
3033 case elfcpp::R_X86_64_PC32:
3034 case elfcpp::R_X86_64_PC32_BND:
3035 case elfcpp::R_X86_64_PC16:
3036 case elfcpp::R_X86_64_PC8:
3037 break;
3038
3039 case elfcpp::R_X86_64_PLT32:
3040 case elfcpp::R_X86_64_PLT32_BND:
3041 // Since we know this is a local symbol, we can handle this as a
3042 // PC32 reloc.
3043 break;
3044
3045 case elfcpp::R_X86_64_GOTPC32:
3046 case elfcpp::R_X86_64_GOTOFF64:
3047 case elfcpp::R_X86_64_GOTPC64:
3048 case elfcpp::R_X86_64_PLTOFF64:
3049 // We need a GOT section.
3050 target->got_section(symtab, layout);
3051 // For PLTOFF64, we'd normally want a PLT section, but since we
3052 // know this is a local symbol, no PLT is needed.
3053 break;
3054
3055 case elfcpp::R_X86_64_GOT64:
3056 case elfcpp::R_X86_64_GOT32:
3057 case elfcpp::R_X86_64_GOTPCREL64:
3058 case elfcpp::R_X86_64_GOTPCREL:
3059 case elfcpp::R_X86_64_GOTPCRELX:
3060 case elfcpp::R_X86_64_REX_GOTPCRELX:
3061 case elfcpp::R_X86_64_GOTPLT64:
3062 {
3063 // The symbol requires a GOT section.
3064 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3065
3066 // If the relocation symbol isn't IFUNC,
3067 // and is local, then we will convert
3068 // mov foo@GOTPCREL(%rip), %reg
3069 // to lea foo(%rip), %reg.
3070 // in Relocate::relocate.
3071 if (!parameters->incremental()
3072 && (r_type == elfcpp::R_X86_64_GOTPCREL
3073 || r_type == elfcpp::R_X86_64_GOTPCRELX
3074 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3075 && reloc.get_r_offset() >= 2
3076 && !is_ifunc)
3077 {
3078 section_size_type stype;
3079 const unsigned char* view = object->section_contents(data_shndx,
3080 &stype, true);
3081 if (view[reloc.get_r_offset() - 2] == 0x8b)
3082 break;
3083 }
3084
3085 // The symbol requires a GOT entry.
3086 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3087
3088 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3089 // lets function pointers compare correctly with shared
3090 // libraries. Otherwise we would need an IRELATIVE reloc.
3091 bool is_new;
3092 if (is_ifunc)
3093 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3094 else
3095 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3096 if (is_new)
3097 {
3098 // If we are generating a shared object, we need to add a
3099 // dynamic relocation for this symbol's GOT entry.
3100 if (parameters->options().output_is_position_independent())
3101 {
3102 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3103 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3104 if (r_type != elfcpp::R_X86_64_GOT32)
3105 {
3106 unsigned int got_offset =
3107 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3108 rela_dyn->add_local_relative(object, r_sym,
3109 elfcpp::R_X86_64_RELATIVE,
3110 got, got_offset, 0, is_ifunc);
3111 }
3112 else
3113 {
3114 this->check_non_pic(object, r_type, NULL);
3115
3116 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3117 rela_dyn->add_local(
3118 object, r_sym, r_type, got,
3119 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
3120 }
3121 }
3122 }
3123 // For GOTPLT64, we'd normally want a PLT section, but since
3124 // we know this is a local symbol, no PLT is needed.
3125 }
3126 break;
3127
3128 case elfcpp::R_X86_64_COPY:
3129 case elfcpp::R_X86_64_GLOB_DAT:
3130 case elfcpp::R_X86_64_JUMP_SLOT:
3131 case elfcpp::R_X86_64_RELATIVE:
3132 case elfcpp::R_X86_64_IRELATIVE:
3133 // These are outstanding tls relocs, which are unexpected when linking
3134 case elfcpp::R_X86_64_TPOFF64:
3135 case elfcpp::R_X86_64_DTPMOD64:
3136 case elfcpp::R_X86_64_TLSDESC:
3137 gold_error(_("%s: unexpected reloc %u in object file"),
3138 object->name().c_str(), r_type);
3139 break;
3140
3141 // These are initial tls relocs, which are expected when linking
3142 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3143 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3144 case elfcpp::R_X86_64_TLSDESC_CALL:
3145 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3146 case elfcpp::R_X86_64_DTPOFF32:
3147 case elfcpp::R_X86_64_DTPOFF64:
3148 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3149 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3150 {
3151 bool output_is_shared = parameters->options().shared();
3152 const tls::Tls_optimization optimized_type
3153 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
3154 r_type);
3155 switch (r_type)
3156 {
3157 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3158 if (optimized_type == tls::TLSOPT_NONE)
3159 {
3160 // Create a pair of GOT entries for the module index and
3161 // dtv-relative offset.
3162 Output_data_got<64, false>* got
3163 = target->got_section(symtab, layout);
3164 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3165 unsigned int shndx = lsym.get_st_shndx();
3166 bool is_ordinary;
3167 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3168 if (!is_ordinary)
3169 object->error(_("local symbol %u has bad shndx %u"),
3170 r_sym, shndx);
3171 else
3172 got->add_local_pair_with_rel(object, r_sym,
3173 shndx,
3174 GOT_TYPE_TLS_PAIR,
3175 target->rela_dyn_section(layout),
3176 elfcpp::R_X86_64_DTPMOD64);
3177 }
3178 else if (optimized_type != tls::TLSOPT_TO_LE)
3179 unsupported_reloc_local(object, r_type);
3180 break;
3181
3182 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3183 target->define_tls_base_symbol(symtab, layout);
3184 if (optimized_type == tls::TLSOPT_NONE)
3185 {
3186 // Create reserved PLT and GOT entries for the resolver.
3187 target->reserve_tlsdesc_entries(symtab, layout);
3188
3189 // Generate a double GOT entry with an
3190 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3191 // is resolved lazily, so the GOT entry needs to be in
3192 // an area in .got.plt, not .got. Call got_section to
3193 // make sure the section has been created.
3194 target->got_section(symtab, layout);
3195 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3196 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3197 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
3198 {
3199 unsigned int got_offset = got->add_constant(0);
3200 got->add_constant(0);
3201 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
3202 got_offset);
3203 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3204 // We store the arguments we need in a vector, and
3205 // use the index into the vector as the parameter
3206 // to pass to the target specific routines.
3207 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
3208 void* arg = reinterpret_cast<void*>(intarg);
3209 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3210 got, got_offset, 0);
3211 }
3212 }
3213 else if (optimized_type != tls::TLSOPT_TO_LE)
3214 unsupported_reloc_local(object, r_type);
3215 break;
3216
3217 case elfcpp::R_X86_64_TLSDESC_CALL:
3218 break;
3219
3220 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3221 if (optimized_type == tls::TLSOPT_NONE)
3222 {
3223 // Create a GOT entry for the module index.
3224 target->got_mod_index_entry(symtab, layout, object);
3225 }
3226 else if (optimized_type != tls::TLSOPT_TO_LE)
3227 unsupported_reloc_local(object, r_type);
3228 break;
3229
3230 case elfcpp::R_X86_64_DTPOFF32:
3231 case elfcpp::R_X86_64_DTPOFF64:
3232 break;
3233
3234 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3235 layout->set_has_static_tls();
3236 if (optimized_type == tls::TLSOPT_NONE)
3237 {
3238 // Create a GOT entry for the tp-relative offset.
3239 Output_data_got<64, false>* got
3240 = target->got_section(symtab, layout);
3241 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3242 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3243 target->rela_dyn_section(layout),
3244 elfcpp::R_X86_64_TPOFF64);
3245 }
3246 else if (optimized_type != tls::TLSOPT_TO_LE)
3247 unsupported_reloc_local(object, r_type);
3248 break;
3249
3250 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3251 layout->set_has_static_tls();
3252 if (output_is_shared)
3253 unsupported_reloc_local(object, r_type);
3254 break;
3255
3256 default:
3257 gold_unreachable();
3258 }
3259 }
3260 break;
3261
3262 case elfcpp::R_X86_64_SIZE32:
3263 case elfcpp::R_X86_64_SIZE64:
3264 default:
3265 gold_error(_("%s: unsupported reloc %u against local symbol"),
3266 object->name().c_str(), r_type);
3267 break;
3268 }
3269 }
3270
3271
3272 // Report an unsupported relocation against a global symbol.
3273
3274 template<int size>
3275 void
3276 Target_x86_64<size>::Scan::unsupported_reloc_global(
3277 Sized_relobj_file<size, false>* object,
3278 unsigned int r_type,
3279 Symbol* gsym)
3280 {
3281 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3282 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3283 }
3284
3285 // Returns true if this relocation type could be that of a function pointer.
3286 template<int size>
3287 inline bool
3288 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
3289 Sized_relobj_file<size, false>* src_obj,
3290 unsigned int src_indx,
3291 unsigned int r_offset,
3292 unsigned int r_type)
3293 {
3294 switch (r_type)
3295 {
3296 case elfcpp::R_X86_64_64:
3297 case elfcpp::R_X86_64_32:
3298 case elfcpp::R_X86_64_32S:
3299 case elfcpp::R_X86_64_16:
3300 case elfcpp::R_X86_64_8:
3301 case elfcpp::R_X86_64_GOT64:
3302 case elfcpp::R_X86_64_GOT32:
3303 case elfcpp::R_X86_64_GOTPCREL64:
3304 case elfcpp::R_X86_64_GOTPCREL:
3305 case elfcpp::R_X86_64_GOTPCRELX:
3306 case elfcpp::R_X86_64_REX_GOTPCRELX:
3307 case elfcpp::R_X86_64_GOTPLT64:
3308 {
3309 return true;
3310 }
3311 case elfcpp::R_X86_64_PC32:
3312 {
3313 // This relocation may be used both for function calls and
3314 // for taking address of a function. We distinguish between
3315 // them by checking the opcodes.
3316 uint64_t sh_flags = src_obj->section_flags(src_indx);
3317 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
3318 if (is_executable)
3319 {
3320 section_size_type stype;
3321 const unsigned char* view = src_obj->section_contents(src_indx,
3322 &stype,
3323 true);
3324
3325 // call
3326 if (r_offset >= 1
3327 && view[r_offset - 1] == 0xe8)
3328 return false;
3329
3330 // jmp
3331 if (r_offset >= 1
3332 && view[r_offset - 1] == 0xe9)
3333 return false;
3334
3335 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3336 if (r_offset >= 2
3337 && view[r_offset - 2] == 0x0f
3338 && view[r_offset - 1] >= 0x80
3339 && view[r_offset - 1] <= 0x8f)
3340 return false;
3341 }
3342
3343 // Be conservative and treat all others as function pointers.
3344 return true;
3345 }
3346 }
3347 return false;
3348 }
3349
3350 // For safe ICF, scan a relocation for a local symbol to check if it
3351 // corresponds to a function pointer being taken. In that case mark
3352 // the function whose pointer was taken as not foldable.
3353
3354 template<int size>
3355 inline bool
3356 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
3357 Symbol_table* ,
3358 Layout* ,
3359 Target_x86_64<size>* ,
3360 Sized_relobj_file<size, false>* src_obj,
3361 unsigned int src_indx,
3362 Output_section* ,
3363 const elfcpp::Rela<size, false>& reloc,
3364 unsigned int r_type,
3365 const elfcpp::Sym<size, false>&)
3366 {
3367 // When building a shared library, do not fold any local symbols as it is
3368 // not possible to distinguish pointer taken versus a call by looking at
3369 // the relocation types.
3370 if (parameters->options().shared())
3371 return true;
3372
3373 return possible_function_pointer_reloc(src_obj, src_indx,
3374 reloc.get_r_offset(), r_type);
3375 }
3376
3377 // For safe ICF, scan a relocation for a global symbol to check if it
3378 // corresponds to a function pointer being taken. In that case mark
3379 // the function whose pointer was taken as not foldable.
3380
3381 template<int size>
3382 inline bool
3383 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
3384 Symbol_table*,
3385 Layout* ,
3386 Target_x86_64<size>* ,
3387 Sized_relobj_file<size, false>* src_obj,
3388 unsigned int src_indx,
3389 Output_section* ,
3390 const elfcpp::Rela<size, false>& reloc,
3391 unsigned int r_type,
3392 Symbol* gsym)
3393 {
3394 // When building a shared library, do not fold symbols whose visibility
3395 // is hidden, internal or protected.
3396 if (parameters->options().shared()
3397 && (gsym->visibility() == elfcpp::STV_INTERNAL
3398 || gsym->visibility() == elfcpp::STV_PROTECTED
3399 || gsym->visibility() == elfcpp::STV_HIDDEN))
3400 return true;
3401
3402 return possible_function_pointer_reloc(src_obj, src_indx,
3403 reloc.get_r_offset(), r_type);
3404 }
3405
3406 // Scan a relocation for a global symbol.
3407
3408 template<int size>
3409 inline void
3410 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
3411 Layout* layout,
3412 Target_x86_64<size>* target,
3413 Sized_relobj_file<size, false>* object,
3414 unsigned int data_shndx,
3415 Output_section* output_section,
3416 const elfcpp::Rela<size, false>& reloc,
3417 unsigned int r_type,
3418 Symbol* gsym)
3419 {
3420 // A STT_GNU_IFUNC symbol may require a PLT entry.
3421 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3422 && this->reloc_needs_plt_for_ifunc(object, r_type))
3423 target->make_plt_entry(symtab, layout, gsym);
3424
3425 switch (r_type)
3426 {
3427 case elfcpp::R_X86_64_NONE:
3428 case elfcpp::R_X86_64_GNU_VTINHERIT:
3429 case elfcpp::R_X86_64_GNU_VTENTRY:
3430 break;
3431
3432 case elfcpp::R_X86_64_64:
3433 case elfcpp::R_X86_64_32:
3434 case elfcpp::R_X86_64_32S:
3435 case elfcpp::R_X86_64_16:
3436 case elfcpp::R_X86_64_8:
3437 {
3438 // Make a PLT entry if necessary.
3439 if (gsym->needs_plt_entry())
3440 {
3441 target->make_plt_entry(symtab, layout, gsym);
3442 // Since this is not a PC-relative relocation, we may be
3443 // taking the address of a function. In that case we need to
3444 // set the entry in the dynamic symbol table to the address of
3445 // the PLT entry.
3446 if (gsym->is_from_dynobj() && !parameters->options().shared())
3447 gsym->set_needs_dynsym_value();
3448 }
3449 // Make a dynamic relocation if necessary.
3450 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3451 {
3452 if (!parameters->options().output_is_position_independent()
3453 && gsym->may_need_copy_reloc())
3454 {
3455 target->copy_reloc(symtab, layout, object,
3456 data_shndx, output_section, gsym, reloc);
3457 }
3458 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3459 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3460 && gsym->type() == elfcpp::STT_GNU_IFUNC
3461 && gsym->can_use_relative_reloc(false)
3462 && !gsym->is_from_dynobj()
3463 && !gsym->is_undefined()
3464 && !gsym->is_preemptible())
3465 {
3466 // Use an IRELATIVE reloc for a locally defined
3467 // STT_GNU_IFUNC symbol. This makes a function
3468 // address in a PIE executable match the address in a
3469 // shared library that it links against.
3470 Reloc_section* rela_dyn =
3471 target->rela_irelative_section(layout);
3472 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
3473 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3474 output_section, object,
3475 data_shndx,
3476 reloc.get_r_offset(),
3477 reloc.get_r_addend());
3478 }
3479 else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
3480 || (size == 32 && r_type == elfcpp::R_X86_64_32))
3481 && gsym->can_use_relative_reloc(false))
3482 {
3483 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3484 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
3485 output_section, object,
3486 data_shndx,
3487 reloc.get_r_offset(),
3488 reloc.get_r_addend(), false);
3489 }
3490 else
3491 {
3492 this->check_non_pic(object, r_type, gsym);
3493 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3494 rela_dyn->add_global(gsym, r_type, output_section, object,
3495 data_shndx, reloc.get_r_offset(),
3496 reloc.get_r_addend());
3497 }
3498 }
3499 }
3500 break;
3501
3502 case elfcpp::R_X86_64_PC64:
3503 case elfcpp::R_X86_64_PC32:
3504 case elfcpp::R_X86_64_PC32_BND:
3505 case elfcpp::R_X86_64_PC16:
3506 case elfcpp::R_X86_64_PC8:
3507 {
3508 // Make a PLT entry if necessary.
3509 if (gsym->needs_plt_entry())
3510 target->make_plt_entry(symtab, layout, gsym);
3511 // Make a dynamic relocation if necessary.
3512 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3513 {
3514 if (parameters->options().output_is_executable()
3515 && gsym->may_need_copy_reloc())
3516 {
3517 target->copy_reloc(symtab, layout, object,
3518 data_shndx, output_section, gsym, reloc);
3519 }
3520 else
3521 {
3522 this->check_non_pic(object, r_type, gsym);
3523 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3524 rela_dyn->add_global(gsym, r_type, output_section, object,
3525 data_shndx, reloc.get_r_offset(),
3526 reloc.get_r_addend());
3527 }
3528 }
3529 }
3530 break;
3531
3532 case elfcpp::R_X86_64_GOT64:
3533 case elfcpp::R_X86_64_GOT32:
3534 case elfcpp::R_X86_64_GOTPCREL64:
3535 case elfcpp::R_X86_64_GOTPCREL:
3536 case elfcpp::R_X86_64_GOTPCRELX:
3537 case elfcpp::R_X86_64_REX_GOTPCRELX:
3538 case elfcpp::R_X86_64_GOTPLT64:
3539 {
3540 // The symbol requires a GOT entry.
3541 Output_data_got<64, false>* got = target->got_section(symtab, layout);
3542
3543 // If we convert this from
3544 // mov foo@GOTPCREL(%rip), %reg
3545 // to lea foo(%rip), %reg.
3546 // OR
3547 // if we convert
3548 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3549 // (callq|jmpq) foo
3550 // in Relocate::relocate, then there is nothing to do here.
3551 // We cannot make these optimizations in incremental linking mode,
3552 // because we look at the opcode to decide whether or not to make
3553 // change, and during an incremental update, the change may have
3554 // already been applied.
3555
3556 Lazy_view<size> view(object, data_shndx);
3557 size_t r_offset = reloc.get_r_offset();
3558 if (!parameters->incremental()
3559 && r_offset >= 2
3560 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3561 r_offset, &view))
3562 break;
3563
3564 if (!parameters->incremental()
3565 && r_offset >= 2
3566 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3567 r_offset,
3568 &view))
3569 break;
3570
3571 if (gsym->final_value_is_known())
3572 {
3573 // For a STT_GNU_IFUNC symbol we want the PLT address.
3574 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3575 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3576 else
3577 got->add_global(gsym, GOT_TYPE_STANDARD);
3578 }
3579 else
3580 {
3581 // If this symbol is not fully resolved, we need to add a
3582 // dynamic relocation for it.
3583 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3584
3585 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3586 //
3587 // 1) The symbol may be defined in some other module.
3588 //
3589 // 2) We are building a shared library and this is a
3590 // protected symbol; using GLOB_DAT means that the dynamic
3591 // linker can use the address of the PLT in the main
3592 // executable when appropriate so that function address
3593 // comparisons work.
3594 //
3595 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3596 // code, again so that function address comparisons work.
3597 if (gsym->is_from_dynobj()
3598 || gsym->is_undefined()
3599 || gsym->is_preemptible()
3600 || (gsym->visibility() == elfcpp::STV_PROTECTED
3601 && parameters->options().shared())
3602 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3603 && parameters->options().output_is_position_independent()))
3604 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3605 elfcpp::R_X86_64_GLOB_DAT);
3606 else
3607 {
3608 // For a STT_GNU_IFUNC symbol we want to write the PLT
3609 // offset into the GOT, so that function pointer
3610 // comparisons work correctly.
3611 bool is_new;
3612 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3613 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3614 else
3615 {
3616 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3617 // Tell the dynamic linker to use the PLT address
3618 // when resolving relocations.
3619 if (gsym->is_from_dynobj()
3620 && !parameters->options().shared())
3621 gsym->set_needs_dynsym_value();
3622 }
3623 if (is_new)
3624 {
3625 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3626 rela_dyn->add_global_relative(gsym,
3627 elfcpp::R_X86_64_RELATIVE,
3628 got, got_off, 0, false);
3629 }
3630 }
3631 }
3632 }
3633 break;
3634
3635 case elfcpp::R_X86_64_PLT32:
3636 case elfcpp::R_X86_64_PLT32_BND:
3637 // If the symbol is fully resolved, this is just a PC32 reloc.
3638 // Otherwise we need a PLT entry.
3639 if (gsym->final_value_is_known())
3640 break;
3641 // If building a shared library, we can also skip the PLT entry
3642 // if the symbol is defined in the output file and is protected
3643 // or hidden.
3644 if (gsym->is_defined()
3645 && !gsym->is_from_dynobj()
3646 && !gsym->is_preemptible())
3647 break;
3648 target->make_plt_entry(symtab, layout, gsym);
3649 break;
3650
3651 case elfcpp::R_X86_64_GOTPC32:
3652 case elfcpp::R_X86_64_GOTOFF64:
3653 case elfcpp::R_X86_64_GOTPC64:
3654 case elfcpp::R_X86_64_PLTOFF64:
3655 // We need a GOT section.
3656 target->got_section(symtab, layout);
3657 // For PLTOFF64, we also need a PLT entry (but only if the
3658 // symbol is not fully resolved).
3659 if (r_type == elfcpp::R_X86_64_PLTOFF64
3660 && !gsym->final_value_is_known())
3661 target->make_plt_entry(symtab, layout, gsym);
3662 break;
3663
3664 case elfcpp::R_X86_64_COPY:
3665 case elfcpp::R_X86_64_GLOB_DAT:
3666 case elfcpp::R_X86_64_JUMP_SLOT:
3667 case elfcpp::R_X86_64_RELATIVE:
3668 case elfcpp::R_X86_64_IRELATIVE:
3669 // These are outstanding tls relocs, which are unexpected when linking
3670 case elfcpp::R_X86_64_TPOFF64:
3671 case elfcpp::R_X86_64_DTPMOD64:
3672 case elfcpp::R_X86_64_TLSDESC:
3673 gold_error(_("%s: unexpected reloc %u in object file"),
3674 object->name().c_str(), r_type);
3675 break;
3676
3677 // These are initial tls relocs, which are expected for global()
3678 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3679 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3680 case elfcpp::R_X86_64_TLSDESC_CALL:
3681 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3682 case elfcpp::R_X86_64_DTPOFF32:
3683 case elfcpp::R_X86_64_DTPOFF64:
3684 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3685 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3686 {
3687 // For the Initial-Exec model, we can treat undef symbols as final
3688 // when building an executable.
3689 const bool is_final = (gsym->final_value_is_known() ||
3690 (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3691 gsym->is_undefined() &&
3692 parameters->options().output_is_executable()));
3693 const tls::Tls_optimization optimized_type
3694 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3695 switch (r_type)
3696 {
3697 case elfcpp::R_X86_64_TLSGD: // General-dynamic
3698 if (optimized_type == tls::TLSOPT_NONE)
3699 {
3700 // Create a pair of GOT entries for the module index and
3701 // dtv-relative offset.
3702 Output_data_got<64, false>* got
3703 = target->got_section(symtab, layout);
3704 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3705 target->rela_dyn_section(layout),
3706 elfcpp::R_X86_64_DTPMOD64,
3707 elfcpp::R_X86_64_DTPOFF64);
3708 }
3709 else if (optimized_type == tls::TLSOPT_TO_IE)
3710 {
3711 // Create a GOT entry for the tp-relative offset.
3712 Output_data_got<64, false>* got
3713 = target->got_section(symtab, layout);
3714 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3715 target->rela_dyn_section(layout),
3716 elfcpp::R_X86_64_TPOFF64);
3717 }
3718 else if (optimized_type != tls::TLSOPT_TO_LE)
3719 unsupported_reloc_global(object, r_type, gsym);
3720 break;
3721
3722 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3723 target->define_tls_base_symbol(symtab, layout);
3724 if (optimized_type == tls::TLSOPT_NONE)
3725 {
3726 // Create reserved PLT and GOT entries for the resolver.
3727 target->reserve_tlsdesc_entries(symtab, layout);
3728
3729 // Create a double GOT entry with an R_X86_64_TLSDESC
3730 // reloc. The R_X86_64_TLSDESC reloc is resolved
3731 // lazily, so the GOT entry needs to be in an area in
3732 // .got.plt, not .got. Call got_section to make sure
3733 // the section has been created.
3734 target->got_section(symtab, layout);
3735 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3736 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3737 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3738 elfcpp::R_X86_64_TLSDESC, 0);
3739 }
3740 else if (optimized_type == tls::TLSOPT_TO_IE)
3741 {
3742 // Create a GOT entry for the tp-relative offset.
3743 Output_data_got<64, false>* got
3744 = target->got_section(symtab, layout);
3745 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3746 target->rela_dyn_section(layout),
3747 elfcpp::R_X86_64_TPOFF64);
3748 }
3749 else if (optimized_type != tls::TLSOPT_TO_LE)
3750 unsupported_reloc_global(object, r_type, gsym);
3751 break;
3752
3753 case elfcpp::R_X86_64_TLSDESC_CALL:
3754 break;
3755
3756 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3757 if (optimized_type == tls::TLSOPT_NONE)
3758 {
3759 // Create a GOT entry for the module index.
3760 target->got_mod_index_entry(symtab, layout, object);
3761 }
3762 else if (optimized_type != tls::TLSOPT_TO_LE)
3763 unsupported_reloc_global(object, r_type, gsym);
3764 break;
3765
3766 case elfcpp::R_X86_64_DTPOFF32:
3767 case elfcpp::R_X86_64_DTPOFF64:
3768 break;
3769
3770 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3771 layout->set_has_static_tls();
3772 if (optimized_type == tls::TLSOPT_NONE)
3773 {
3774 // Create a GOT entry for the tp-relative offset.
3775 Output_data_got<64, false>* got
3776 = target->got_section(symtab, layout);
3777 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3778 target->rela_dyn_section(layout),
3779 elfcpp::R_X86_64_TPOFF64);
3780 }
3781 else if (optimized_type != tls::TLSOPT_TO_LE)
3782 unsupported_reloc_global(object, r_type, gsym);
3783 break;
3784
3785 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3786 layout->set_has_static_tls();
3787 if (parameters->options().shared())
3788 unsupported_reloc_global(object, r_type, gsym);
3789 break;
3790
3791 default:
3792 gold_unreachable();
3793 }
3794 }
3795 break;
3796
3797 case elfcpp::R_X86_64_SIZE32:
3798 case elfcpp::R_X86_64_SIZE64:
3799 default:
3800 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3801 object->name().c_str(), r_type,
3802 gsym->demangled_name().c_str());
3803 break;
3804 }
3805 }
3806
3807 template<int size>
3808 void
3809 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3810 Layout* layout,
3811 Sized_relobj_file<size, false>* object,
3812 unsigned int data_shndx,
3813 unsigned int sh_type,
3814 const unsigned char* prelocs,
3815 size_t reloc_count,
3816 Output_section* output_section,
3817 bool needs_special_offset_handling,
3818 size_t local_symbol_count,
3819 const unsigned char* plocal_symbols)
3820 {
3821 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3822 Classify_reloc;
3823
3824 if (sh_type == elfcpp::SHT_REL)
3825 {
3826 return;
3827 }
3828
3829 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3830 Classify_reloc>(
3831 symtab,
3832 layout,
3833 this,
3834 object,
3835 data_shndx,
3836 prelocs,
3837 reloc_count,
3838 output_section,
3839 needs_special_offset_handling,
3840 local_symbol_count,
3841 plocal_symbols);
3842
3843 }
3844 // Scan relocations for a section.
3845
3846 template<int size>
3847 void
3848 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3849 Layout* layout,
3850 Sized_relobj_file<size, false>* object,
3851 unsigned int data_shndx,
3852 unsigned int sh_type,
3853 const unsigned char* prelocs,
3854 size_t reloc_count,
3855 Output_section* output_section,
3856 bool needs_special_offset_handling,
3857 size_t local_symbol_count,
3858 const unsigned char* plocal_symbols)
3859 {
3860 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3861 Classify_reloc;
3862
3863 if (sh_type == elfcpp::SHT_REL)
3864 {
3865 gold_error(_("%s: unsupported REL reloc section"),
3866 object->name().c_str());
3867 return;
3868 }
3869
3870 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3871 symtab,
3872 layout,
3873 this,
3874 object,
3875 data_shndx,
3876 prelocs,
3877 reloc_count,
3878 output_section,
3879 needs_special_offset_handling,
3880 local_symbol_count,
3881 plocal_symbols);
3882 }
3883
3884 // Finalize the sections.
3885
3886 template<int size>
3887 void
3888 Target_x86_64<size>::do_finalize_sections(
3889 Layout* layout,
3890 const Input_objects*,
3891 Symbol_table* symtab)
3892 {
3893 const Reloc_section* rel_plt = (this->plt_ == NULL
3894 ? NULL
3895 : this->plt_->rela_plt());
3896 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3897 this->rela_dyn_, true, false);
3898
3899 // Fill in some more dynamic tags.
3900 Output_data_dynamic* const odyn = layout->dynamic_data();
3901 if (odyn != NULL)
3902 {
3903 if (this->plt_ != NULL
3904 && this->plt_->output_section() != NULL
3905 && this->plt_->has_tlsdesc_entry())
3906 {
3907 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3908 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3909 this->got_->finalize_data_size();
3910 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3911 this->plt_, plt_offset);
3912 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3913 this->got_, got_offset);
3914 }
3915 }
3916
3917 // Emit any relocs we saved in an attempt to avoid generating COPY
3918 // relocs.
3919 if (this->copy_relocs_.any_saved_relocs())
3920 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3921
3922 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3923 // the .got.plt section.
3924 Symbol* sym = this->global_offset_table_;
3925 if (sym != NULL)
3926 {
3927 uint64_t data_size = this->got_plt_->current_data_size();
3928 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3929 }
3930
3931 if (parameters->doing_static_link()
3932 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3933 {
3934 // If linking statically, make sure that the __rela_iplt symbols
3935 // were defined if necessary, even if we didn't create a PLT.
3936 static const Define_symbol_in_segment syms[] =
3937 {
3938 {
3939 "__rela_iplt_start", // name
3940 elfcpp::PT_LOAD, // segment_type
3941 elfcpp::PF_W, // segment_flags_set
3942 elfcpp::PF(0), // segment_flags_clear
3943 0, // value
3944 0, // size
3945 elfcpp::STT_NOTYPE, // type
3946 elfcpp::STB_GLOBAL, // binding
3947 elfcpp::STV_HIDDEN, // visibility
3948 0, // nonvis
3949 Symbol::SEGMENT_START, // offset_from_base
3950 true // only_if_ref
3951 },
3952 {
3953 "__rela_iplt_end", // name
3954 elfcpp::PT_LOAD, // segment_type
3955 elfcpp::PF_W, // segment_flags_set
3956 elfcpp::PF(0), // segment_flags_clear
3957 0, // value
3958 0, // size
3959 elfcpp::STT_NOTYPE, // type
3960 elfcpp::STB_GLOBAL, // binding
3961 elfcpp::STV_HIDDEN, // visibility
3962 0, // nonvis
3963 Symbol::SEGMENT_START, // offset_from_base
3964 true // only_if_ref
3965 }
3966 };
3967
3968 symtab->define_symbols(layout, 2, syms,
3969 layout->script_options()->saw_sections_clause());
3970 }
3971 }
3972
3973 // For x32, we need to handle PC-relative relocations using full 64-bit
3974 // arithmetic, so that we can detect relocation overflows properly.
3975 // This class overrides the pcrela32_check methods from the defaults in
3976 // Relocate_functions in reloc.h.
3977
3978 template<int size>
3979 class X86_64_relocate_functions : public Relocate_functions<size, false>
3980 {
3981 public:
3982 typedef Relocate_functions<size, false> Base;
3983
3984 // Do a simple PC relative relocation with the addend in the
3985 // relocation.
3986 static inline typename Base::Reloc_status
3987 pcrela32_check(unsigned char* view,
3988 typename elfcpp::Elf_types<64>::Elf_Addr value,
3989 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3990 typename elfcpp::Elf_types<64>::Elf_Addr address)
3991 {
3992 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3993 Valtype* wv = reinterpret_cast<Valtype*>(view);
3994 value = value + addend - address;
3995 elfcpp::Swap<32, false>::writeval(wv, value);
3996 return (Bits<32>::has_overflow(value)
3997 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3998 }
3999
4000 // Do a simple PC relative relocation with a Symbol_value with the
4001 // addend in the relocation.
4002 static inline typename Base::Reloc_status
4003 pcrela32_check(unsigned char* view,
4004 const Sized_relobj_file<size, false>* object,
4005 const Symbol_value<size>* psymval,
4006 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4007 typename elfcpp::Elf_types<64>::Elf_Addr address)
4008 {
4009 typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4010 Valtype* wv = reinterpret_cast<Valtype*>(view);
4011 typename elfcpp::Elf_types<64>::Elf_Addr value;
4012 if (addend >= 0)
4013 value = psymval->value(object, addend);
4014 else
4015 {
4016 // For negative addends, get the symbol value without
4017 // the addend, then add the addend using 64-bit arithmetic.
4018 value = psymval->value(object, 0);
4019 value += addend;
4020 }
4021 value -= address;
4022 elfcpp::Swap<32, false>::writeval(wv, value);
4023 return (Bits<32>::has_overflow(value)
4024 ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4025 }
4026 };
4027
4028 // Perform a relocation.
4029
4030 template<int size>
4031 inline bool
4032 Target_x86_64<size>::Relocate::relocate(
4033 const Relocate_info<size, false>* relinfo,
4034 unsigned int,
4035 Target_x86_64<size>* target,
4036 Output_section*,
4037 size_t relnum,
4038 const unsigned char* preloc,
4039 const Sized_symbol<size>* gsym,
4040 const Symbol_value<size>* psymval,
4041 unsigned char* view,
4042 typename elfcpp::Elf_types<size>::Elf_Addr address,
4043 section_size_type view_size)
4044 {
4045 typedef X86_64_relocate_functions<size> Reloc_funcs;
4046 const elfcpp::Rela<size, false> rela(preloc);
4047 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4048
4049 if (this->skip_call_tls_get_addr_)
4050 {
4051 if ((r_type != elfcpp::R_X86_64_PLT32
4052 && r_type != elfcpp::R_X86_64_GOTPCREL
4053 && r_type != elfcpp::R_X86_64_GOTPCRELX
4054 && r_type != elfcpp::R_X86_64_PLT32_BND
4055 && r_type != elfcpp::R_X86_64_PC32_BND
4056 && r_type != elfcpp::R_X86_64_PC32)
4057 || gsym == NULL
4058 || strcmp(gsym->name(), "__tls_get_addr") != 0)
4059 {
4060 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4061 _("missing expected TLS relocation"));
4062 this->skip_call_tls_get_addr_ = false;
4063 }
4064 else
4065 {
4066 this->skip_call_tls_get_addr_ = false;
4067 return false;
4068 }
4069 }
4070
4071 if (view == NULL)
4072 return true;
4073
4074 const Sized_relobj_file<size, false>* object = relinfo->object;
4075
4076 // Pick the value to use for symbols defined in the PLT.
4077 Symbol_value<size> symval;
4078 if (gsym != NULL
4079 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4080 {
4081 symval.set_output_value(target->plt_address_for_global(gsym));
4082 psymval = &symval;
4083 }
4084 else if (gsym == NULL && psymval->is_ifunc_symbol())
4085 {
4086 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4087 if (object->local_has_plt_offset(r_sym))
4088 {
4089 symval.set_output_value(target->plt_address_for_local(object, r_sym));
4090 psymval = &symval;
4091 }
4092 }
4093
4094 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4095
4096 // Get the GOT offset if needed.
4097 // The GOT pointer points to the end of the GOT section.
4098 // We need to subtract the size of the GOT section to get
4099 // the actual offset to use in the relocation.
4100 bool have_got_offset = false;
4101 // Since the actual offset is always negative, we use signed int to
4102 // support 64-bit GOT relocations.
4103 int got_offset = 0;
4104 switch (r_type)
4105 {
4106 case elfcpp::R_X86_64_GOT32:
4107 case elfcpp::R_X86_64_GOT64:
4108 case elfcpp::R_X86_64_GOTPLT64:
4109 case elfcpp::R_X86_64_GOTPCREL64:
4110 if (gsym != NULL)
4111 {
4112 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4113 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4114 }
4115 else
4116 {
4117 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4118 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4119 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4120 - target->got_size());
4121 }
4122 have_got_offset = true;
4123 break;
4124
4125 default:
4126 break;
4127 }
4128
4129 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4130
4131 switch (r_type)
4132 {
4133 case elfcpp::R_X86_64_NONE:
4134 case elfcpp::R_X86_64_GNU_VTINHERIT:
4135 case elfcpp::R_X86_64_GNU_VTENTRY:
4136 break;
4137
4138 case elfcpp::R_X86_64_64:
4139 Reloc_funcs::rela64(view, object, psymval, addend);
4140 break;
4141
4142 case elfcpp::R_X86_64_PC64:
4143 Reloc_funcs::pcrela64(view, object, psymval, addend,
4144 address);
4145 break;
4146
4147 case elfcpp::R_X86_64_32:
4148 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4149 Reloc_funcs::CHECK_UNSIGNED);
4150 break;
4151
4152 case elfcpp::R_X86_64_32S:
4153 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4154 Reloc_funcs::CHECK_SIGNED);
4155 break;
4156
4157 case elfcpp::R_X86_64_PC32:
4158 case elfcpp::R_X86_64_PC32_BND:
4159 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4160 address);
4161 break;
4162
4163 case elfcpp::R_X86_64_16:
4164 Reloc_funcs::rela16(view, object, psymval, addend);
4165 break;
4166
4167 case elfcpp::R_X86_64_PC16:
4168 Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4169 break;
4170
4171 case elfcpp::R_X86_64_8:
4172 Reloc_funcs::rela8(view, object, psymval, addend);
4173 break;
4174
4175 case elfcpp::R_X86_64_PC8:
4176 Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4177 break;
4178
4179 case elfcpp::R_X86_64_PLT32:
4180 case elfcpp::R_X86_64_PLT32_BND:
4181 gold_assert(gsym == NULL
4182 || gsym->has_plt_offset()
4183 || gsym->final_value_is_known()
4184 || (gsym->is_defined()
4185 && !gsym->is_from_dynobj()
4186 && !gsym->is_preemptible()));
4187 // Note: while this code looks the same as for R_X86_64_PC32, it
4188 // behaves differently because psymval was set to point to
4189 // the PLT entry, rather than the symbol, in Scan::global().
4190 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4191 address);
4192 break;
4193
4194 case elfcpp::R_X86_64_PLTOFF64:
4195 {
4196 gold_assert(gsym);
4197 gold_assert(gsym->has_plt_offset()
4198 || gsym->final_value_is_known());
4199 typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4200 // This is the address of GLOBAL_OFFSET_TABLE.
4201 got_address = target->got_plt_section()->address();
4202 Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4203 }
4204 break;
4205
4206 case elfcpp::R_X86_64_GOT32:
4207 gold_assert(have_got_offset);
4208 Reloc_funcs::rela32(view, got_offset, addend);
4209 break;
4210
4211 case elfcpp::R_X86_64_GOTPC32:
4212 {
4213 gold_assert(gsym);
4214 typename elfcpp::Elf_types<size>::Elf_Addr value;
4215 value = target->got_plt_section()->address();
4216 Reloc_funcs::pcrela32_check(view, value, addend, address);
4217 }
4218 break;
4219
4220 case elfcpp::R_X86_64_GOT64:
4221 case elfcpp::R_X86_64_GOTPLT64:
4222 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4223 // GOT64.
4224 gold_assert(have_got_offset);
4225 Reloc_funcs::rela64(view, got_offset, addend);
4226 break;
4227
4228 case elfcpp::R_X86_64_GOTPC64:
4229 {
4230 gold_assert(gsym);
4231 typename elfcpp::Elf_types<size>::Elf_Addr value;
4232 value = target->got_plt_section()->address();
4233 Reloc_funcs::pcrela64(view, value, addend, address);
4234 }
4235 break;
4236
4237 case elfcpp::R_X86_64_GOTOFF64:
4238 {
4239 typename elfcpp::Elf_types<size>::Elf_Addr value;
4240 value = (psymval->value(object, 0)
4241 - target->got_plt_section()->address());
4242 Reloc_funcs::rela64(view, value, addend);
4243 }
4244 break;
4245
4246 case elfcpp::R_X86_64_GOTPCREL:
4247 case elfcpp::R_X86_64_GOTPCRELX:
4248 case elfcpp::R_X86_64_REX_GOTPCRELX:
4249 {
4250 // Convert
4251 // mov foo@GOTPCREL(%rip), %reg
4252 // to lea foo(%rip), %reg.
4253 // if possible.
4254 if (!parameters->incremental()
4255 && ((gsym == NULL
4256 && rela.get_r_offset() >= 2
4257 && view[-2] == 0x8b
4258 && !psymval->is_ifunc_symbol())
4259 || (gsym != NULL
4260 && rela.get_r_offset() >= 2
4261 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4262 0, &view))))
4263 {
4264 view[-2] = 0x8d;
4265 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4266 }
4267 // Convert
4268 // callq *foo@GOTPCRELX(%rip) to
4269 // addr32 callq foo
4270 // and jmpq *foo@GOTPCRELX(%rip) to
4271 // jmpq foo
4272 // nop
4273 else if (!parameters->incremental()
4274 && gsym != NULL
4275 && rela.get_r_offset() >= 2
4276 && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4277 r_type,
4278 0, &view))
4279 {
4280 if (view[-1] == 0x15)
4281 {
4282 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4283 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4284 view[-2] = 0x67;
4285 view[-1] = 0xe8;
4286 // Convert GOTPCRELX to 32-bit pc relative reloc.
4287 Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4288 }
4289 else
4290 {
4291 // Convert jmpq *foo@GOTPCRELX(%rip) to
4292 // jmpq foo
4293 // nop
4294 // The opcode of direct jmpq is 0xe9.
4295 view[-2] = 0xe9;
4296 // The opcode of nop is 0x90.
4297 view[3] = 0x90;
4298 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4299 // relative and since the instruction following the jmpq is now
4300 // the nop, offset the address by 1 byte. The start of the
4301 // relocation also moves ahead by 1 byte.
4302 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4303 address - 1);
4304 }
4305 }
4306 else
4307 {
4308 if (gsym != NULL)
4309 {
4310 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4311 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4312 - target->got_size());
4313 }
4314 else
4315 {
4316 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4317 gold_assert(object->local_has_got_offset(r_sym,
4318 GOT_TYPE_STANDARD));
4319 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4320 - target->got_size());
4321 }
4322 typename elfcpp::Elf_types<size>::Elf_Addr value;
4323 value = target->got_plt_section()->address() + got_offset;
4324 Reloc_funcs::pcrela32_check(view, value, addend, address);
4325 }
4326 }
4327 break;
4328
4329 case elfcpp::R_X86_64_GOTPCREL64:
4330 {
4331 gold_assert(have_got_offset);
4332 typename elfcpp::Elf_types<size>::Elf_Addr value;
4333 value = target->got_plt_section()->address() + got_offset;
4334 Reloc_funcs::pcrela64(view, value, addend, address);
4335 }
4336 break;
4337
4338 case elfcpp::R_X86_64_COPY:
4339 case elfcpp::R_X86_64_GLOB_DAT:
4340 case elfcpp::R_X86_64_JUMP_SLOT:
4341 case elfcpp::R_X86_64_RELATIVE:
4342 case elfcpp::R_X86_64_IRELATIVE:
4343 // These are outstanding tls relocs, which are unexpected when linking
4344 case elfcpp::R_X86_64_TPOFF64:
4345 case elfcpp::R_X86_64_DTPMOD64:
4346 case elfcpp::R_X86_64_TLSDESC:
4347 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4348 _("unexpected reloc %u in object file"),
4349 r_type);
4350 break;
4351
4352 // These are initial tls relocs, which are expected when linking
4353 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4354 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4355 case elfcpp::R_X86_64_TLSDESC_CALL:
4356 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4357 case elfcpp::R_X86_64_DTPOFF32:
4358 case elfcpp::R_X86_64_DTPOFF64:
4359 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4360 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4361 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4362 view, address, view_size);
4363 break;
4364
4365 case elfcpp::R_X86_64_SIZE32:
4366 case elfcpp::R_X86_64_SIZE64:
4367 default:
4368 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4369 _("unsupported reloc %u"),
4370 r_type);
4371 break;
4372 }
4373
4374 if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4375 {
4376 if (gsym == NULL)
4377 {
4378 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4379 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4380 _("relocation overflow: "
4381 "reference to local symbol %u in %s"),
4382 r_sym, object->name().c_str());
4383 }
4384 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4385 {
4386 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4387 _("relocation overflow: "
4388 "reference to '%s' defined in %s"),
4389 gsym->name(),
4390 gsym->object()->name().c_str());
4391 }
4392 else
4393 {
4394 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4395 _("relocation overflow: reference to '%s'"),
4396 gsym->name());
4397 }
4398 }
4399
4400 return true;
4401 }
4402
4403 // Perform a TLS relocation.
4404
4405 template<int size>
4406 inline void
4407 Target_x86_64<size>::Relocate::relocate_tls(
4408 const Relocate_info<size, false>* relinfo,
4409 Target_x86_64<size>* target,
4410 size_t relnum,
4411 const elfcpp::Rela<size, false>& rela,
4412 unsigned int r_type,
4413 const Sized_symbol<size>* gsym,
4414 const Symbol_value<size>* psymval,
4415 unsigned char* view,
4416 typename elfcpp::Elf_types<size>::Elf_Addr address,
4417 section_size_type view_size)
4418 {
4419 Output_segment* tls_segment = relinfo->layout->tls_segment();
4420
4421 const Sized_relobj_file<size, false>* object = relinfo->object;
4422 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4423 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4424 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4425
4426 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4427
4428 const bool is_final = (gsym == NULL
4429 ? !parameters->options().shared()
4430 : gsym->final_value_is_known());
4431 tls::Tls_optimization optimized_type
4432 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4433 switch (r_type)
4434 {
4435 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
4436 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4437 {
4438 // If this code sequence is used in a non-executable section,
4439 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4440 // on the assumption that it's being used by itself in a debug
4441 // section. Therefore, in the unlikely event that the code
4442 // sequence appears in a non-executable section, we simply
4443 // leave it unoptimized.
4444 optimized_type = tls::TLSOPT_NONE;
4445 }
4446 if (optimized_type == tls::TLSOPT_TO_LE)
4447 {
4448 if (tls_segment == NULL)
4449 {
4450 gold_assert(parameters->errors()->error_count() > 0
4451 || issue_undefined_symbol_error(gsym));
4452 return;
4453 }
4454 this->tls_gd_to_le(relinfo, relnum, tls_segment,
4455 rela, r_type, value, view,
4456 view_size);
4457 break;
4458 }
4459 else
4460 {
4461 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4462 ? GOT_TYPE_TLS_OFFSET
4463 : GOT_TYPE_TLS_PAIR);
4464 unsigned int got_offset;
4465 if (gsym != NULL)
4466 {
4467 gold_assert(gsym->has_got_offset(got_type));
4468 got_offset = gsym->got_offset(got_type) - target->got_size();
4469 }
4470 else
4471 {
4472 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4473 gold_assert(object->local_has_got_offset(r_sym, got_type));
4474 got_offset = (object->local_got_offset(r_sym, got_type)
4475 - target->got_size());
4476 }
4477 if (optimized_type == tls::TLSOPT_TO_IE)
4478 {
4479 value = target->got_plt_section()->address() + got_offset;
4480 this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4481 value, view, address, view_size);
4482 break;
4483 }
4484 else if (optimized_type == tls::TLSOPT_NONE)
4485 {
4486 // Relocate the field with the offset of the pair of GOT
4487 // entries.
4488 value = target->got_plt_section()->address() + got_offset;
4489 Relocate_functions<size, false>::pcrela32(view, value, addend,
4490 address);
4491 break;
4492 }
4493 }
4494 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4495 _("unsupported reloc %u"), r_type);
4496 break;
4497
4498 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
4499 case elfcpp::R_X86_64_TLSDESC_CALL:
4500 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4501 {
4502 // See above comment for R_X86_64_TLSGD.
4503 optimized_type = tls::TLSOPT_NONE;
4504 }
4505 if (optimized_type == tls::TLSOPT_TO_LE)
4506 {
4507 if (tls_segment == NULL)
4508 {
4509 gold_assert(parameters->errors()->error_count() > 0
4510 || issue_undefined_symbol_error(gsym));
4511 return;
4512 }
4513 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4514 rela, r_type, value, view,
4515 view_size);
4516 break;
4517 }
4518 else
4519 {
4520 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4521 ? GOT_TYPE_TLS_OFFSET
4522 : GOT_TYPE_TLS_DESC);
4523 unsigned int got_offset = 0;
4524 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4525 && optimized_type == tls::TLSOPT_NONE)
4526 {
4527 // We created GOT entries in the .got.tlsdesc portion of
4528 // the .got.plt section, but the offset stored in the
4529 // symbol is the offset within .got.tlsdesc.
4530 got_offset = (target->got_size()
4531 + target->got_plt_section()->data_size());
4532 }
4533 if (gsym != NULL)
4534 {
4535 gold_assert(gsym->has_got_offset(got_type));
4536 got_offset += gsym->got_offset(got_type) - target->got_size();
4537 }
4538 else
4539 {
4540 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4541 gold_assert(object->local_has_got_offset(r_sym, got_type));
4542 got_offset += (object->local_got_offset(r_sym, got_type)
4543 - target->got_size());
4544 }
4545 if (optimized_type == tls::TLSOPT_TO_IE)
4546 {
4547 value = target->got_plt_section()->address() + got_offset;
4548 this->tls_desc_gd_to_ie(relinfo, relnum,
4549 rela, r_type, value, view, address,
4550 view_size);
4551 break;
4552 }
4553 else if (optimized_type == tls::TLSOPT_NONE)
4554 {
4555 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4556 {
4557 // Relocate the field with the offset of the pair of GOT
4558 // entries.
4559 value = target->got_plt_section()->address() + got_offset;
4560 Relocate_functions<size, false>::pcrela32(view, value, addend,
4561 address);
4562 }
4563 break;
4564 }
4565 }
4566 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4567 _("unsupported reloc %u"), r_type);
4568 break;
4569
4570 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
4571 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4572 {
4573 // See above comment for R_X86_64_TLSGD.
4574 optimized_type = tls::TLSOPT_NONE;
4575 }
4576 if (optimized_type == tls::TLSOPT_TO_LE)
4577 {
4578 if (tls_segment == NULL)
4579 {
4580 gold_assert(parameters->errors()->error_count() > 0
4581 || issue_undefined_symbol_error(gsym));
4582 return;
4583 }
4584 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4585 value, view, view_size);
4586 break;
4587 }
4588 else if (optimized_type == tls::TLSOPT_NONE)
4589 {
4590 // Relocate the field with the offset of the GOT entry for
4591 // the module index.
4592 unsigned int got_offset;
4593 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4594 - target->got_size());
4595 value = target->got_plt_section()->address() + got_offset;
4596 Relocate_functions<size, false>::pcrela32(view, value, addend,
4597 address);
4598 break;
4599 }
4600 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4601 _("unsupported reloc %u"), r_type);
4602 break;
4603
4604 case elfcpp::R_X86_64_DTPOFF32:
4605 // This relocation type is used in debugging information.
4606 // In that case we need to not optimize the value. If the
4607 // section is not executable, then we assume we should not
4608 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4609 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4610 // R_X86_64_TLSLD.
4611 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4612 {
4613 if (tls_segment == NULL)
4614 {
4615 gold_assert(parameters->errors()->error_count() > 0
4616 || issue_undefined_symbol_error(gsym));
4617 return;
4618 }
4619 value -= tls_segment->memsz();
4620 }
4621 Relocate_functions<size, false>::rela32(view, value, addend);
4622 break;
4623
4624 case elfcpp::R_X86_64_DTPOFF64:
4625 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4626 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4627 {
4628 if (tls_segment == NULL)
4629 {
4630 gold_assert(parameters->errors()->error_count() > 0
4631 || issue_undefined_symbol_error(gsym));
4632 return;
4633 }
4634 value -= tls_segment->memsz();
4635 }
4636 Relocate_functions<size, false>::rela64(view, value, addend);
4637 break;
4638
4639 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
4640 if (gsym != NULL
4641 && gsym->is_undefined()
4642 && parameters->options().output_is_executable())
4643 {
4644 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4645 NULL, rela,
4646 r_type, value, view,
4647 view_size);
4648 break;
4649 }
4650 else if (optimized_type == tls::TLSOPT_TO_LE)
4651 {
4652 if (tls_segment == NULL)
4653 {
4654 gold_assert(parameters->errors()->error_count() > 0
4655 || issue_undefined_symbol_error(gsym));
4656 return;
4657 }
4658 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4659 tls_segment, rela,
4660 r_type, value, view,
4661 view_size);
4662 break;
4663 }
4664 else if (optimized_type == tls::TLSOPT_NONE)
4665 {
4666 // Relocate the field with the offset of the GOT entry for
4667 // the tp-relative offset of the symbol.
4668 unsigned int got_offset;
4669 if (gsym != NULL)
4670 {
4671 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4672 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4673 - target->got_size());
4674 }
4675 else
4676 {
4677 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4678 gold_assert(object->local_has_got_offset(r_sym,
4679 GOT_TYPE_TLS_OFFSET));
4680 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4681 - target->got_size());
4682 }
4683 value = target->got_plt_section()->address() + got_offset;
4684 Relocate_functions<size, false>::pcrela32(view, value, addend,
4685 address);
4686 break;
4687 }
4688 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4689 _("unsupported reloc type %u"),
4690 r_type);
4691 break;
4692
4693 case elfcpp::R_X86_64_TPOFF32: // Local-exec
4694 if (tls_segment == NULL)
4695 {
4696 gold_assert(parameters->errors()->error_count() > 0
4697 || issue_undefined_symbol_error(gsym));
4698 return;
4699 }
4700 value -= tls_segment->memsz();
4701 Relocate_functions<size, false>::rela32(view, value, addend);
4702 break;
4703 }
4704 }
4705
4706 // Do a relocation in which we convert a TLS General-Dynamic to an
4707 // Initial-Exec.
4708
4709 template<int size>
4710 inline void
4711 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4712 const Relocate_info<size, false>* relinfo,
4713 size_t relnum,
4714 const elfcpp::Rela<size, false>& rela,
4715 unsigned int,
4716 typename elfcpp::Elf_types<size>::Elf_Addr value,
4717 unsigned char* view,
4718 typename elfcpp::Elf_types<size>::Elf_Addr address,
4719 section_size_type view_size)
4720 {
4721 // For SIZE == 64:
4722 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4723 // .word 0x6666; rex64; call __tls_get_addr@PLT
4724 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4725 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4726 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4727 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4728 // For SIZE == 32:
4729 // leaq foo@tlsgd(%rip),%rdi;
4730 // .word 0x6666; rex64; call __tls_get_addr@PLT
4731 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4732 // leaq foo@tlsgd(%rip),%rdi;
4733 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4734 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4735
4736 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4737 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4738 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4739 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4740
4741 if (size == 64)
4742 {
4743 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4744 -4);
4745 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4746 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4747 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4748 16);
4749 }
4750 else
4751 {
4752 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4753 -3);
4754 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4755 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4756 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4757 15);
4758 }
4759
4760 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4761 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4762 address);
4763
4764 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4765 // We can skip it.
4766 this->skip_call_tls_get_addr_ = true;
4767 }
4768
4769 // Do a relocation in which we convert a TLS General-Dynamic to a
4770 // Local-Exec.
4771
4772 template<int size>
4773 inline void
4774 Target_x86_64<size>::Relocate::tls_gd_to_le(
4775 const Relocate_info<size, false>* relinfo,
4776 size_t relnum,
4777 Output_segment* tls_segment,
4778 const elfcpp::Rela<size, false>& rela,
4779 unsigned int,
4780 typename elfcpp::Elf_types<size>::Elf_Addr value,
4781 unsigned char* view,
4782 section_size_type view_size)
4783 {
4784 // For SIZE == 64:
4785 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4786 // .word 0x6666; rex64; call __tls_get_addr@PLT
4787 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4788 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4789 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4790 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4791 // For SIZE == 32:
4792 // leaq foo@tlsgd(%rip),%rdi;
4793 // .word 0x6666; rex64; call __tls_get_addr@PLT
4794 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4795 // leaq foo@tlsgd(%rip),%rdi;
4796 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4797 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4798
4799 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4800 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4801 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4802 || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4803
4804 if (size == 64)
4805 {
4806 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4807 -4);
4808 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4809 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4810 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4811 16);
4812 }
4813 else
4814 {
4815 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4816 -3);
4817 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4818 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4819
4820 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4821 15);
4822 }
4823
4824 value -= tls_segment->memsz();
4825 Relocate_functions<size, false>::rela32(view + 8, value, 0);
4826
4827 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4828 // We can skip it.
4829 this->skip_call_tls_get_addr_ = true;
4830 }
4831
4832 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4833
4834 template<int size>
4835 inline void
4836 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4837 const Relocate_info<size, false>* relinfo,
4838 size_t relnum,
4839 const elfcpp::Rela<size, false>& rela,
4840 unsigned int r_type,
4841 typename elfcpp::Elf_types<size>::Elf_Addr value,
4842 unsigned char* view,
4843 typename elfcpp::Elf_types<size>::Elf_Addr address,
4844 section_size_type view_size)
4845 {
4846 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4847 {
4848 // leaq foo@tlsdesc(%rip), %rax
4849 // ==> movq foo@gottpoff(%rip), %rax
4850 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4851 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4852 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4853 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4854 view[-2] = 0x8b;
4855 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4856 Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4857 }
4858 else
4859 {
4860 // call *foo@tlscall(%rax)
4861 // ==> nop; nop
4862 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4863 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4864 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4865 view[0] == 0xff && view[1] == 0x10);
4866 view[0] = 0x66;
4867 view[1] = 0x90;
4868 }
4869 }
4870
4871 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4872
4873 template<int size>
4874 inline void
4875 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4876 const Relocate_info<size, false>* relinfo,
4877 size_t relnum,
4878 Output_segment* tls_segment,
4879 const elfcpp::Rela<size, false>& rela,
4880 unsigned int r_type,
4881 typename elfcpp::Elf_types<size>::Elf_Addr value,
4882 unsigned char* view,
4883 section_size_type view_size)
4884 {
4885 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4886 {
4887 // leaq foo@tlsdesc(%rip), %rax
4888 // ==> movq foo@tpoff, %rax
4889 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4890 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4891 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4892 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4893 view[-2] = 0xc7;
4894 view[-1] = 0xc0;
4895 value -= tls_segment->memsz();
4896 Relocate_functions<size, false>::rela32(view, value, 0);
4897 }
4898 else
4899 {
4900 // call *foo@tlscall(%rax)
4901 // ==> nop; nop
4902 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4903 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4904 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4905 view[0] == 0xff && view[1] == 0x10);
4906 view[0] = 0x66;
4907 view[1] = 0x90;
4908 }
4909 }
4910
4911 template<int size>
4912 inline void
4913 Target_x86_64<size>::Relocate::tls_ld_to_le(
4914 const Relocate_info<size, false>* relinfo,
4915 size_t relnum,
4916 Output_segment*,
4917 const elfcpp::Rela<size, false>& rela,
4918 unsigned int,
4919 typename elfcpp::Elf_types<size>::Elf_Addr,
4920 unsigned char* view,
4921 section_size_type view_size)
4922 {
4923 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4924 // For SIZE == 64:
4925 // ... leq foo@dtpoff(%rax),%reg
4926 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4927 // For SIZE == 32:
4928 // ... leq foo@dtpoff(%rax),%reg
4929 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4930 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
4931 // For SIZE == 64:
4932 // ... leq foo@dtpoff(%rax),%reg
4933 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4934 // For SIZE == 32:
4935 // ... leq foo@dtpoff(%rax),%reg
4936 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4937
4938 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4939 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4940
4941 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4942 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4943
4944 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4945 view[4] == 0xe8 || view[4] == 0xff);
4946
4947 if (view[4] == 0xe8)
4948 {
4949 if (size == 64)
4950 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4951 else
4952 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4953 }
4954 else
4955 {
4956 if (size == 64)
4957 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
4958 13);
4959 else
4960 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
4961 13);
4962 }
4963
4964 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4965 // We can skip it.
4966 this->skip_call_tls_get_addr_ = true;
4967 }
4968
4969 // Do a relocation in which we convert a TLS Initial-Exec to a
4970 // Local-Exec.
4971
4972 template<int size>
4973 inline void
4974 Target_x86_64<size>::Relocate::tls_ie_to_le(
4975 const Relocate_info<size, false>* relinfo,
4976 size_t relnum,
4977 Output_segment* tls_segment,
4978 const elfcpp::Rela<size, false>& rela,
4979 unsigned int,
4980 typename elfcpp::Elf_types<size>::Elf_Addr value,
4981 unsigned char* view,
4982 section_size_type view_size)
4983 {
4984 // We need to examine the opcodes to figure out which instruction we
4985 // are looking at.
4986
4987 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4988 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4989
4990 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4991 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4992
4993 unsigned char op1 = view[-3];
4994 unsigned char op2 = view[-2];
4995 unsigned char op3 = view[-1];
4996 unsigned char reg = op3 >> 3;
4997
4998 if (op2 == 0x8b)
4999 {
5000 // movq
5001 if (op1 == 0x4c)
5002 view[-3] = 0x49;
5003 else if (size == 32 && op1 == 0x44)
5004 view[-3] = 0x41;
5005 view[-2] = 0xc7;
5006 view[-1] = 0xc0 | reg;
5007 }
5008 else if (reg == 4)
5009 {
5010 // Special handling for %rsp.
5011 if (op1 == 0x4c)
5012 view[-3] = 0x49;
5013 else if (size == 32 && op1 == 0x44)
5014 view[-3] = 0x41;
5015 view[-2] = 0x81;
5016 view[-1] = 0xc0 | reg;
5017 }
5018 else
5019 {
5020 // addq
5021 if (op1 == 0x4c)
5022 view[-3] = 0x4d;
5023 else if (size == 32 && op1 == 0x44)
5024 view[-3] = 0x45;
5025 view[-2] = 0x8d;
5026 view[-1] = 0x80 | reg | (reg << 3);
5027 }
5028
5029 if (tls_segment != NULL)
5030 value -= tls_segment->memsz();
5031 Relocate_functions<size, false>::rela32(view, value, 0);
5032 }
5033
5034 // Relocate section data.
5035
5036 template<int size>
5037 void
5038 Target_x86_64<size>::relocate_section(
5039 const Relocate_info<size, false>* relinfo,
5040 unsigned int sh_type,
5041 const unsigned char* prelocs,
5042 size_t reloc_count,
5043 Output_section* output_section,
5044 bool needs_special_offset_handling,
5045 unsigned char* view,
5046 typename elfcpp::Elf_types<size>::Elf_Addr address,
5047 section_size_type view_size,
5048 const Reloc_symbol_changes* reloc_symbol_changes)
5049 {
5050 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5051 Classify_reloc;
5052
5053 gold_assert(sh_type == elfcpp::SHT_RELA);
5054
5055 gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5056 gold::Default_comdat_behavior, Classify_reloc>(
5057 relinfo,
5058 this,
5059 prelocs,
5060 reloc_count,
5061 output_section,
5062 needs_special_offset_handling,
5063 view,
5064 address,
5065 view_size,
5066 reloc_symbol_changes);
5067 }
5068
5069 // Apply an incremental relocation. Incremental relocations always refer
5070 // to global symbols.
5071
5072 template<int size>
5073 void
5074 Target_x86_64<size>::apply_relocation(
5075 const Relocate_info<size, false>* relinfo,
5076 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5077 unsigned int r_type,
5078 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5079 const Symbol* gsym,
5080 unsigned char* view,
5081 typename elfcpp::Elf_types<size>::Elf_Addr address,
5082 section_size_type view_size)
5083 {
5084 gold::apply_relocation<size, false, Target_x86_64<size>,
5085 typename Target_x86_64<size>::Relocate>(
5086 relinfo,
5087 this,
5088 r_offset,
5089 r_type,
5090 r_addend,
5091 gsym,
5092 view,
5093 address,
5094 view_size);
5095 }
5096
5097 // Scan the relocs during a relocatable link.
5098
5099 template<int size>
5100 void
5101 Target_x86_64<size>::scan_relocatable_relocs(
5102 Symbol_table* symtab,
5103 Layout* layout,
5104 Sized_relobj_file<size, false>* object,
5105 unsigned int data_shndx,
5106 unsigned int sh_type,
5107 const unsigned char* prelocs,
5108 size_t reloc_count,
5109 Output_section* output_section,
5110 bool needs_special_offset_handling,
5111 size_t local_symbol_count,
5112 const unsigned char* plocal_symbols,
5113 Relocatable_relocs* rr)
5114 {
5115 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5116 Classify_reloc;
5117 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5118 Scan_relocatable_relocs;
5119
5120 gold_assert(sh_type == elfcpp::SHT_RELA);
5121
5122 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5123 symtab,
5124 layout,
5125 object,
5126 data_shndx,
5127 prelocs,
5128 reloc_count,
5129 output_section,
5130 needs_special_offset_handling,
5131 local_symbol_count,
5132 plocal_symbols,
5133 rr);
5134 }
5135
5136 // Scan the relocs for --emit-relocs.
5137
5138 template<int size>
5139 void
5140 Target_x86_64<size>::emit_relocs_scan(
5141 Symbol_table* symtab,
5142 Layout* layout,
5143 Sized_relobj_file<size, false>* object,
5144 unsigned int data_shndx,
5145 unsigned int sh_type,
5146 const unsigned char* prelocs,
5147 size_t reloc_count,
5148 Output_section* output_section,
5149 bool needs_special_offset_handling,
5150 size_t local_symbol_count,
5151 const unsigned char* plocal_syms,
5152 Relocatable_relocs* rr)
5153 {
5154 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5155 Classify_reloc;
5156 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5157 Emit_relocs_strategy;
5158
5159 gold_assert(sh_type == elfcpp::SHT_RELA);
5160
5161 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5162 symtab,
5163 layout,
5164 object,
5165 data_shndx,
5166 prelocs,
5167 reloc_count,
5168 output_section,
5169 needs_special_offset_handling,
5170 local_symbol_count,
5171 plocal_syms,
5172 rr);
5173 }
5174
5175 // Relocate a section during a relocatable link.
5176
5177 template<int size>
5178 void
5179 Target_x86_64<size>::relocate_relocs(
5180 const Relocate_info<size, false>* relinfo,
5181 unsigned int sh_type,
5182 const unsigned char* prelocs,
5183 size_t reloc_count,
5184 Output_section* output_section,
5185 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5186 unsigned char* view,
5187 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5188 section_size_type view_size,
5189 unsigned char* reloc_view,
5190 section_size_type reloc_view_size)
5191 {
5192 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5193 Classify_reloc;
5194
5195 gold_assert(sh_type == elfcpp::SHT_RELA);
5196
5197 gold::relocate_relocs<size, false, Classify_reloc>(
5198 relinfo,
5199 prelocs,
5200 reloc_count,
5201 output_section,
5202 offset_in_output_section,
5203 view,
5204 view_address,
5205 view_size,
5206 reloc_view,
5207 reloc_view_size);
5208 }
5209
5210 // Return the value to use for a dynamic which requires special
5211 // treatment. This is how we support equality comparisons of function
5212 // pointers across shared library boundaries, as described in the
5213 // processor specific ABI supplement.
5214
5215 template<int size>
5216 uint64_t
5217 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5218 {
5219 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5220 return this->plt_address_for_global(gsym);
5221 }
5222
5223 // Return a string used to fill a code section with nops to take up
5224 // the specified length.
5225
5226 template<int size>
5227 std::string
5228 Target_x86_64<size>::do_code_fill(section_size_type length) const
5229 {
5230 if (length >= 16)
5231 {
5232 // Build a jmpq instruction to skip over the bytes.
5233 unsigned char jmp[5];
5234 jmp[0] = 0xe9;
5235 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5236 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5237 + std::string(length - 5, static_cast<char>(0x90)));
5238 }
5239
5240 // Nop sequences of various lengths.
5241 const char nop1[1] = { '\x90' }; // nop
5242 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
5243 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5244 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5245 '\x00'};
5246 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5247 '\x00', '\x00' };
5248 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5249 '\x44', '\x00', '\x00' };
5250 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5251 '\x00', '\x00', '\x00',
5252 '\x00' };
5253 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5254 '\x00', '\x00', '\x00',
5255 '\x00', '\x00' };
5256 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5257 '\x84', '\x00', '\x00',
5258 '\x00', '\x00', '\x00' };
5259 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5260 '\x1f', '\x84', '\x00',
5261 '\x00', '\x00', '\x00',
5262 '\x00' };
5263 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5264 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5265 '\x00', '\x00', '\x00',
5266 '\x00', '\x00' };
5267 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5268 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5269 '\x84', '\x00', '\x00',
5270 '\x00', '\x00', '\x00' };
5271 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5272 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5273 '\x1f', '\x84', '\x00',
5274 '\x00', '\x00', '\x00',
5275 '\x00' };
5276 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5277 '\x66', '\x66', '\x2e', // data16
5278 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5279 '\x00', '\x00', '\x00',
5280 '\x00', '\x00' };
5281 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5282 '\x66', '\x66', '\x66', // data16; data16
5283 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5284 '\x84', '\x00', '\x00',
5285 '\x00', '\x00', '\x00' };
5286
5287 const char* nops[16] = {
5288 NULL,
5289 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5290 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5291 };
5292
5293 return std::string(nops[length], length);
5294 }
5295
5296 // Return the addend to use for a target specific relocation. The
5297 // only target specific relocation is R_X86_64_TLSDESC for a local
5298 // symbol. We want to set the addend is the offset of the local
5299 // symbol in the TLS segment.
5300
5301 template<int size>
5302 uint64_t
5303 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5304 uint64_t) const
5305 {
5306 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5307 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5308 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5309 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5310 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5311 gold_assert(psymval->is_tls_symbol());
5312 // The value of a TLS symbol is the offset in the TLS segment.
5313 return psymval->value(ti.object, 0);
5314 }
5315
5316 // Return the value to use for the base of a DW_EH_PE_datarel offset
5317 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5318 // assembler can not write out the difference between two labels in
5319 // different sections, so instead of using a pc-relative value they
5320 // use an offset from the GOT.
5321
5322 template<int size>
5323 uint64_t
5324 Target_x86_64<size>::do_ehframe_datarel_base() const
5325 {
5326 gold_assert(this->global_offset_table_ != NULL);
5327 Symbol* sym = this->global_offset_table_;
5328 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5329 return ssym->value();
5330 }
5331
5332 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5333 // compiled with -fsplit-stack. The function calls non-split-stack
5334 // code. We have to change the function so that it always ensures
5335 // that it has enough stack space to run some random function.
5336
5337 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5338 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5339 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5340
5341 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5342 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5343 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5344
5345 template<int size>
5346 void
5347 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5348 section_offset_type fnoffset,
5349 section_size_type fnsize,
5350 const unsigned char*,
5351 size_t,
5352 unsigned char* view,
5353 section_size_type view_size,
5354 std::string* from,
5355 std::string* to) const
5356 {
5357 const char* const cmp_insn = reinterpret_cast<const char*>
5358 (size == 32 ? cmp_insn_32 : cmp_insn_64);
5359 const char* const lea_r10_insn = reinterpret_cast<const char*>
5360 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5361 const char* const lea_r11_insn = reinterpret_cast<const char*>
5362 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5363
5364 const size_t cmp_insn_len =
5365 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5366 const size_t lea_r10_insn_len =
5367 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5368 const size_t lea_r11_insn_len =
5369 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5370 const size_t nop_len = (size == 32 ? 7 : 8);
5371
5372 // The function starts with a comparison of the stack pointer and a
5373 // field in the TCB. This is followed by a jump.
5374
5375 // cmp %fs:NN,%rsp
5376 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5377 && fnsize > nop_len + 1)
5378 {
5379 // We will call __morestack if the carry flag is set after this
5380 // comparison. We turn the comparison into an stc instruction
5381 // and some nops.
5382 view[fnoffset] = '\xf9';
5383 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5384 }
5385 // lea NN(%rsp),%r10
5386 // lea NN(%rsp),%r11
5387 else if ((this->match_view(view, view_size, fnoffset,
5388 lea_r10_insn, lea_r10_insn_len)
5389 || this->match_view(view, view_size, fnoffset,
5390 lea_r11_insn, lea_r11_insn_len))
5391 && fnsize > 8)
5392 {
5393 // This is loading an offset from the stack pointer for a
5394 // comparison. The offset is negative, so we decrease the
5395 // offset by the amount of space we need for the stack. This
5396 // means we will avoid calling __morestack if there happens to
5397 // be plenty of space on the stack already.
5398 unsigned char* pval = view + fnoffset + 4;
5399 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5400 val -= parameters->options().split_stack_adjust_size();
5401 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5402 }
5403 else
5404 {
5405 if (!object->has_no_split_stack())
5406 object->error(_("failed to match split-stack sequence at "
5407 "section %u offset %0zx"),
5408 shndx, static_cast<size_t>(fnoffset));
5409 return;
5410 }
5411
5412 // We have to change the function so that it calls
5413 // __morestack_non_split instead of __morestack. The former will
5414 // allocate additional stack space.
5415 *from = "__morestack";
5416 *to = "__morestack_non_split";
5417 }
5418
5419 // The selector for x86_64 object files. Note this is never instantiated
5420 // directly. It's only used in Target_selector_x86_64_nacl, below.
5421
5422 template<int size>
5423 class Target_selector_x86_64 : public Target_selector_freebsd
5424 {
5425 public:
5426 Target_selector_x86_64()
5427 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5428 (size == 64
5429 ? "elf64-x86-64" : "elf32-x86-64"),
5430 (size == 64
5431 ? "elf64-x86-64-freebsd"
5432 : "elf32-x86-64-freebsd"),
5433 (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5434 { }
5435
5436 Target*
5437 do_instantiate_target()
5438 { return new Target_x86_64<size>(); }
5439
5440 };
5441
5442 // NaCl variant. It uses different PLT contents.
5443
5444 template<int size>
5445 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5446 {
5447 public:
5448 Output_data_plt_x86_64_nacl(Layout* layout,
5449 Output_data_got<64, false>* got,
5450 Output_data_got_plt_x86_64* got_plt,
5451 Output_data_space* got_irelative)
5452 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5453 got, got_plt, got_irelative)
5454 { }
5455
5456 Output_data_plt_x86_64_nacl(Layout* layout,
5457 Output_data_got<64, false>* got,
5458 Output_data_got_plt_x86_64* got_plt,
5459 Output_data_space* got_irelative,
5460 unsigned int plt_count)
5461 : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5462 got, got_plt, got_irelative,
5463 plt_count)
5464 { }
5465
5466 protected:
5467 virtual unsigned int
5468 do_get_plt_entry_size() const
5469 { return plt_entry_size; }
5470
5471 virtual void
5472 do_add_eh_frame(Layout* layout)
5473 {
5474 layout->add_eh_frame_for_plt(this,
5475 this->plt_eh_frame_cie,
5476 this->plt_eh_frame_cie_size,
5477 plt_eh_frame_fde,
5478 plt_eh_frame_fde_size);
5479 }
5480
5481 virtual void
5482 do_fill_first_plt_entry(unsigned char* pov,
5483 typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5484 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5485
5486 virtual unsigned int
5487 do_fill_plt_entry(unsigned char* pov,
5488 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5489 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5490 unsigned int got_offset,
5491 unsigned int plt_offset,
5492 unsigned int plt_index);
5493
5494 virtual void
5495 do_fill_tlsdesc_entry(unsigned char* pov,
5496 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5497 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5498 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5499 unsigned int tlsdesc_got_offset,
5500 unsigned int plt_offset);
5501
5502 private:
5503 // The size of an entry in the PLT.
5504 static const int plt_entry_size = 64;
5505
5506 // The first entry in the PLT.
5507 static const unsigned char first_plt_entry[plt_entry_size];
5508
5509 // Other entries in the PLT for an executable.
5510 static const unsigned char plt_entry[plt_entry_size];
5511
5512 // The reserved TLSDESC entry in the PLT for an executable.
5513 static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5514
5515 // The .eh_frame unwind information for the PLT.
5516 static const int plt_eh_frame_fde_size = 32;
5517 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5518 };
5519
5520 template<int size>
5521 class Target_x86_64_nacl : public Target_x86_64<size>
5522 {
5523 public:
5524 Target_x86_64_nacl()
5525 : Target_x86_64<size>(&x86_64_nacl_info)
5526 { }
5527
5528 virtual Output_data_plt_x86_64<size>*
5529 do_make_data_plt(Layout* layout,
5530 Output_data_got<64, false>* got,
5531 Output_data_got_plt_x86_64* got_plt,
5532 Output_data_space* got_irelative)
5533 {
5534 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5535 got_irelative);
5536 }
5537
5538 virtual Output_data_plt_x86_64<size>*
5539 do_make_data_plt(Layout* layout,
5540 Output_data_got<64, false>* got,
5541 Output_data_got_plt_x86_64* got_plt,
5542 Output_data_space* got_irelative,
5543 unsigned int plt_count)
5544 {
5545 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5546 got_irelative,
5547 plt_count);
5548 }
5549
5550 virtual std::string
5551 do_code_fill(section_size_type length) const;
5552
5553 private:
5554 static const Target::Target_info x86_64_nacl_info;
5555 };
5556
5557 template<>
5558 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5559 {
5560 64, // size
5561 false, // is_big_endian
5562 elfcpp::EM_X86_64, // machine_code
5563 false, // has_make_symbol
5564 false, // has_resolve
5565 true, // has_code_fill
5566 true, // is_default_stack_executable
5567 true, // can_icf_inline_merge_sections
5568 '\0', // wrap_char
5569 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5570 0x20000, // default_text_segment_address
5571 0x10000, // abi_pagesize (overridable by -z max-page-size)
5572 0x10000, // common_pagesize (overridable by -z common-page-size)
5573 true, // isolate_execinstr
5574 0x10000000, // rosegment_gap
5575 elfcpp::SHN_UNDEF, // small_common_shndx
5576 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5577 0, // small_common_section_flags
5578 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5579 NULL, // attributes_section
5580 NULL, // attributes_vendor
5581 "_start", // entry_symbol_name
5582 32, // hash_entry_size
5583 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5584 };
5585
5586 template<>
5587 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5588 {
5589 32, // size
5590 false, // is_big_endian
5591 elfcpp::EM_X86_64, // machine_code
5592 false, // has_make_symbol
5593 false, // has_resolve
5594 true, // has_code_fill
5595 true, // is_default_stack_executable
5596 true, // can_icf_inline_merge_sections
5597 '\0', // wrap_char
5598 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5599 0x20000, // default_text_segment_address
5600 0x10000, // abi_pagesize (overridable by -z max-page-size)
5601 0x10000, // common_pagesize (overridable by -z common-page-size)
5602 true, // isolate_execinstr
5603 0x10000000, // rosegment_gap
5604 elfcpp::SHN_UNDEF, // small_common_shndx
5605 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
5606 0, // small_common_section_flags
5607 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
5608 NULL, // attributes_section
5609 NULL, // attributes_vendor
5610 "_start", // entry_symbol_name
5611 32, // hash_entry_size
5612 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
5613 };
5614
5615 #define NACLMASK 0xe0 // 32-byte alignment mask.
5616
5617 // The first entry in the PLT.
5618
5619 template<int size>
5620 const unsigned char
5621 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5622 {
5623 0xff, 0x35, // pushq contents of memory address
5624 0, 0, 0, 0, // replaced with address of .got + 8
5625 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5626 0, 0, 0, 0, // replaced with address of .got + 16
5627 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5628 0x4d, 0x01, 0xfb, // add %r15, %r11
5629 0x41, 0xff, 0xe3, // jmpq *%r11
5630
5631 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5632 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5633
5634 // 32 bytes of nop to pad out to the standard size
5635 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5636 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5637 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5638 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5639 0x66, // excess data32 prefix
5640 0x90 // nop
5641 };
5642
5643 template<int size>
5644 void
5645 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5646 unsigned char* pov,
5647 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5648 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5649 {
5650 memcpy(pov, first_plt_entry, plt_entry_size);
5651 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5652 (got_address + 8
5653 - (plt_address + 2 + 4)));
5654 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5655 (got_address + 16
5656 - (plt_address + 9 + 4)));
5657 }
5658
5659 // Subsequent entries in the PLT.
5660
5661 template<int size>
5662 const unsigned char
5663 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
5664 {
5665 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5666 0, 0, 0, 0, // replaced with address of symbol in .got
5667 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5668 0x4d, 0x01, 0xfb, // add %r15, %r11
5669 0x41, 0xff, 0xe3, // jmpq *%r11
5670
5671 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5672 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5673 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5674
5675 // Lazy GOT entries point here (32-byte aligned).
5676 0x68, // pushq immediate
5677 0, 0, 0, 0, // replaced with index into relocation table
5678 0xe9, // jmp relative
5679 0, 0, 0, 0, // replaced with offset to start of .plt0
5680
5681 // 22 bytes of nop to pad out to the standard size.
5682 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5683 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5684 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5685 };
5686
5687 template<int size>
5688 unsigned int
5689 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
5690 unsigned char* pov,
5691 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5692 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5693 unsigned int got_offset,
5694 unsigned int plt_offset,
5695 unsigned int plt_index)
5696 {
5697 memcpy(pov, plt_entry, plt_entry_size);
5698 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
5699 (got_address + got_offset
5700 - (plt_address + plt_offset
5701 + 3 + 4)));
5702
5703 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
5704 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
5705 - (plt_offset + 38 + 4));
5706
5707 return 32;
5708 }
5709
5710 // The reserved TLSDESC entry in the PLT.
5711
5712 template<int size>
5713 const unsigned char
5714 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
5715 {
5716 0xff, 0x35, // pushq x(%rip)
5717 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5718 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5719 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5720 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
5721 0x4d, 0x01, 0xfb, // add %r15, %r11
5722 0x41, 0xff, 0xe3, // jmpq *%r11
5723
5724 // 41 bytes of nop to pad out to the standard size.
5725 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5726 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5727 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5728 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5729 0x66, 0x66, // excess data32 prefixes
5730 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5731 };
5732
5733 template<int size>
5734 void
5735 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5736 unsigned char* pov,
5737 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5738 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5739 typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5740 unsigned int tlsdesc_got_offset,
5741 unsigned int plt_offset)
5742 {
5743 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5744 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5745 (got_address + 8
5746 - (plt_address + plt_offset
5747 + 2 + 4)));
5748 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5749 (got_base
5750 + tlsdesc_got_offset
5751 - (plt_address + plt_offset
5752 + 9 + 4)));
5753 }
5754
5755 // The .eh_frame unwind information for the PLT.
5756
5757 template<int size>
5758 const unsigned char
5759 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5760 {
5761 0, 0, 0, 0, // Replaced with offset to .plt.
5762 0, 0, 0, 0, // Replaced with size of .plt.
5763 0, // Augmentation size.
5764 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
5765 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
5766 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
5767 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
5768 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
5769 13, // Block length.
5770 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
5771 elfcpp::DW_OP_breg16, 0, // Push %rip.
5772 elfcpp::DW_OP_const1u, 63, // Push 0x3f.
5773 elfcpp::DW_OP_and, // & (%rip & 0x3f).
5774 elfcpp::DW_OP_const1u, 37, // Push 0x25.
5775 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
5776 elfcpp::DW_OP_lit3, // Push 3.
5777 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
5778 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5779 elfcpp::DW_CFA_nop, // Align to 32 bytes.
5780 elfcpp::DW_CFA_nop
5781 };
5782
5783 // Return a string used to fill a code section with nops.
5784 // For NaCl, long NOPs are only valid if they do not cross
5785 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5786 template<int size>
5787 std::string
5788 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5789 {
5790 return std::string(length, static_cast<char>(0x90));
5791 }
5792
5793 // The selector for x86_64-nacl object files.
5794
5795 template<int size>
5796 class Target_selector_x86_64_nacl
5797 : public Target_selector_nacl<Target_selector_x86_64<size>,
5798 Target_x86_64_nacl<size> >
5799 {
5800 public:
5801 Target_selector_x86_64_nacl()
5802 : Target_selector_nacl<Target_selector_x86_64<size>,
5803 Target_x86_64_nacl<size> >("x86-64",
5804 size == 64
5805 ? "elf64-x86-64-nacl"
5806 : "elf32-x86-64-nacl",
5807 size == 64
5808 ? "elf_x86_64_nacl"
5809 : "elf32_x86_64_nacl")
5810 { }
5811 };
5812
5813 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5814 Target_selector_x86_64_nacl<32> target_selector_x32;
5815
5816 } // End anonymous namespace.
This page took 0.171556 seconds and 3 git commands to generate.