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