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