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