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