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