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