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