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