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