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