pda
[binutils.git] / gold / x86_64.cc
blob2972180d213277c49ff01be849d77de958daabc5
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
43 namespace
46 using namespace gold;
48 class Output_data_plt_x86_64;
50 // The x86_64 target class.
51 // See the ABI at
52 // http://www.x86-64.org/documentation/abi.pdf
53 // TLS info comes from
54 // http://people.redhat.com/drepper/tls.pdf
55 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
57 class Target_x86_64 : public Target_freebsd<64, false>
59 public:
60 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
61 // uses only Elf64_Rela relocation entries with explicit addends."
62 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64 Target_x86_64()
65 : Target_freebsd<64, false>(&x86_64_info),
66 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
67 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
68 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
69 { }
71 // Hook for a new output section.
72 void
73 do_new_output_section(Output_section*) const;
75 // Scan the relocations to look for symbol adjustments.
76 void
77 gc_process_relocs(Symbol_table* symtab,
78 Layout* layout,
79 Sized_relobj<64, false>* object,
80 unsigned int data_shndx,
81 unsigned int sh_type,
82 const unsigned char* prelocs,
83 size_t reloc_count,
84 Output_section* output_section,
85 bool needs_special_offset_handling,
86 size_t local_symbol_count,
87 const unsigned char* plocal_symbols);
89 // Scan the relocations to look for symbol adjustments.
90 void
91 scan_relocs(Symbol_table* symtab,
92 Layout* layout,
93 Sized_relobj<64, false>* object,
94 unsigned int data_shndx,
95 unsigned int sh_type,
96 const unsigned char* prelocs,
97 size_t reloc_count,
98 Output_section* output_section,
99 bool needs_special_offset_handling,
100 size_t local_symbol_count,
101 const unsigned char* plocal_symbols);
103 // Finalize the sections.
104 void
105 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
107 // Return the value to use for a dynamic which requires special
108 // treatment.
109 uint64_t
110 do_dynsym_value(const Symbol*) const;
112 // Relocate a section.
113 void
114 relocate_section(const Relocate_info<64, false>*,
115 unsigned int sh_type,
116 const unsigned char* prelocs,
117 size_t reloc_count,
118 Output_section* output_section,
119 bool needs_special_offset_handling,
120 unsigned char* view,
121 elfcpp::Elf_types<64>::Elf_Addr view_address,
122 section_size_type view_size,
123 const Reloc_symbol_changes*);
125 // Scan the relocs during a relocatable link.
126 void
127 scan_relocatable_relocs(Symbol_table* symtab,
128 Layout* layout,
129 Sized_relobj<64, false>* object,
130 unsigned int data_shndx,
131 unsigned int sh_type,
132 const unsigned char* prelocs,
133 size_t reloc_count,
134 Output_section* output_section,
135 bool needs_special_offset_handling,
136 size_t local_symbol_count,
137 const unsigned char* plocal_symbols,
138 Relocatable_relocs*);
140 // Relocate a section during a relocatable link.
141 void
142 relocate_for_relocatable(const Relocate_info<64, false>*,
143 unsigned int sh_type,
144 const unsigned char* prelocs,
145 size_t reloc_count,
146 Output_section* output_section,
147 off_t offset_in_output_section,
148 const Relocatable_relocs*,
149 unsigned char* view,
150 elfcpp::Elf_types<64>::Elf_Addr view_address,
151 section_size_type view_size,
152 unsigned char* reloc_view,
153 section_size_type reloc_view_size);
155 // Return a string used to fill a code section with nops.
156 std::string
157 do_code_fill(section_size_type length) const;
159 // Return whether SYM is defined by the ABI.
160 bool
161 do_is_defined_by_abi(const Symbol* sym) const
162 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
164 // Adjust -fstack-split code which calls non-stack-split code.
165 void
166 do_calls_non_split(Relobj* object, unsigned int shndx,
167 section_offset_type fnoffset, section_size_type fnsize,
168 unsigned char* view, section_size_type view_size,
169 std::string* from, std::string* to) const;
171 // Return the size of the GOT section.
172 section_size_type
173 got_size()
175 gold_assert(this->got_ != NULL);
176 return this->got_->data_size();
179 private:
180 // The class which scans relocations.
181 class Scan
183 public:
184 Scan()
185 : issued_non_pic_error_(false)
188 inline void
189 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
190 Sized_relobj<64, false>* object,
191 unsigned int data_shndx,
192 Output_section* output_section,
193 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
194 const elfcpp::Sym<64, false>& lsym);
196 inline void
197 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
198 Sized_relobj<64, false>* object,
199 unsigned int data_shndx,
200 Output_section* output_section,
201 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
202 Symbol* gsym);
204 private:
205 static void
206 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
208 static void
209 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
210 Symbol*);
212 void
213 check_non_pic(Relobj*, unsigned int r_type);
215 // Whether we have issued an error about a non-PIC compilation.
216 bool issued_non_pic_error_;
219 // The class which implements relocation.
220 class Relocate
222 public:
223 Relocate()
224 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
227 ~Relocate()
229 if (this->skip_call_tls_get_addr_)
231 // FIXME: This needs to specify the location somehow.
232 gold_error(_("missing expected TLS relocation"));
236 // Do a relocation. Return false if the caller should not issue
237 // any warnings about this relocation.
238 inline bool
239 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
240 size_t relnum, const elfcpp::Rela<64, false>&,
241 unsigned int r_type, const Sized_symbol<64>*,
242 const Symbol_value<64>*,
243 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
244 section_size_type);
246 private:
247 // Do a TLS relocation.
248 inline void
249 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
250 size_t relnum, const elfcpp::Rela<64, false>&,
251 unsigned int r_type, const Sized_symbol<64>*,
252 const Symbol_value<64>*,
253 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
254 section_size_type);
256 // Do a TLS General-Dynamic to Initial-Exec transition.
257 inline void
258 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
259 Output_segment* tls_segment,
260 const elfcpp::Rela<64, false>&, unsigned int r_type,
261 elfcpp::Elf_types<64>::Elf_Addr value,
262 unsigned char* view,
263 elfcpp::Elf_types<64>::Elf_Addr,
264 section_size_type view_size);
266 // Do a TLS General-Dynamic to Local-Exec transition.
267 inline void
268 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
269 Output_segment* tls_segment,
270 const elfcpp::Rela<64, false>&, unsigned int r_type,
271 elfcpp::Elf_types<64>::Elf_Addr value,
272 unsigned char* view,
273 section_size_type view_size);
275 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
276 inline void
277 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
278 Output_segment* tls_segment,
279 const elfcpp::Rela<64, false>&, unsigned int r_type,
280 elfcpp::Elf_types<64>::Elf_Addr value,
281 unsigned char* view,
282 elfcpp::Elf_types<64>::Elf_Addr,
283 section_size_type view_size);
285 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
286 inline void
287 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
288 Output_segment* tls_segment,
289 const elfcpp::Rela<64, false>&, unsigned int r_type,
290 elfcpp::Elf_types<64>::Elf_Addr value,
291 unsigned char* view,
292 section_size_type view_size);
294 // Do a TLS Local-Dynamic to Local-Exec transition.
295 inline void
296 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
297 Output_segment* tls_segment,
298 const elfcpp::Rela<64, false>&, unsigned int r_type,
299 elfcpp::Elf_types<64>::Elf_Addr value,
300 unsigned char* view,
301 section_size_type view_size);
303 // Do a TLS Initial-Exec to Local-Exec transition.
304 static inline void
305 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
306 Output_segment* tls_segment,
307 const elfcpp::Rela<64, false>&, unsigned int r_type,
308 elfcpp::Elf_types<64>::Elf_Addr value,
309 unsigned char* view,
310 section_size_type view_size);
312 // This is set if we should skip the next reloc, which should be a
313 // PLT32 reloc against ___tls_get_addr.
314 bool skip_call_tls_get_addr_;
316 // This is set if we see a relocation which could load the address
317 // of the TLS block. Whether we see such a relocation determines
318 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
319 // in debugging sections.
320 bool saw_tls_block_reloc_;
323 // A class which returns the size required for a relocation type,
324 // used while scanning relocs during a relocatable link.
325 class Relocatable_size_for_reloc
327 public:
328 unsigned int
329 get_size_for_reloc(unsigned int, Relobj*);
332 // Adjust TLS relocation type based on the options and whether this
333 // is a local symbol.
334 static tls::Tls_optimization
335 optimize_tls_reloc(bool is_final, int r_type);
337 // Get the GOT section, creating it if necessary.
338 Output_data_got<64, false>*
339 got_section(Symbol_table*, Layout*);
341 // Get the GOT PLT section.
342 Output_data_space*
343 got_plt_section() const
345 gold_assert(this->got_plt_ != NULL);
346 return this->got_plt_;
349 // Create the PLT section.
350 void
351 make_plt_section(Symbol_table* symtab, Layout* layout);
353 // Create a PLT entry for a global symbol.
354 void
355 make_plt_entry(Symbol_table*, Layout*, Symbol*);
357 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
358 void
359 define_tls_base_symbol(Symbol_table*, Layout*);
361 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
362 void
363 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
365 // Create a GOT entry for the TLS module index.
366 unsigned int
367 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
368 Sized_relobj<64, false>* object);
370 // Get the PLT section.
371 Output_data_plt_x86_64*
372 plt_section() const
374 gold_assert(this->plt_ != NULL);
375 return this->plt_;
378 // Get the dynamic reloc section, creating it if necessary.
379 Reloc_section*
380 rela_dyn_section(Layout*);
382 // Add a potential copy relocation.
383 void
384 copy_reloc(Symbol_table* symtab, Layout* layout,
385 Sized_relobj<64, false>* object,
386 unsigned int shndx, Output_section* output_section,
387 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
389 this->copy_relocs_.copy_reloc(symtab, layout,
390 symtab->get_sized_symbol<64>(sym),
391 object, shndx, output_section,
392 reloc, this->rela_dyn_section(layout));
395 // Information about this specific target which we pass to the
396 // general Target structure.
397 static const Target::Target_info x86_64_info;
399 enum Got_type
401 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
402 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
403 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
404 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
407 // The GOT section.
408 Output_data_got<64, false>* got_;
409 // The PLT section.
410 Output_data_plt_x86_64* plt_;
411 // The GOT PLT section.
412 Output_data_space* got_plt_;
413 // The dynamic reloc section.
414 Reloc_section* rela_dyn_;
415 // Relocs saved to avoid a COPY reloc.
416 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
417 // Space for variables copied with a COPY reloc.
418 Output_data_space* dynbss_;
419 // Offset of the GOT entry for the TLS module index.
420 unsigned int got_mod_index_offset_;
421 // True if the _TLS_MODULE_BASE_ symbol has been defined.
422 bool tls_base_symbol_defined_;
425 const Target::Target_info Target_x86_64::x86_64_info =
427 64, // size
428 false, // is_big_endian
429 elfcpp::EM_X86_64, // machine_code
430 false, // has_make_symbol
431 false, // has_resolve
432 true, // has_code_fill
433 true, // is_default_stack_executable
434 '\0', // wrap_char
435 "/lib/ld64.so.1", // program interpreter
436 0x400000, // default_text_segment_address
437 0x1000, // abi_pagesize (overridable by -z max-page-size)
438 0x1000, // common_pagesize (overridable by -z common-page-size)
439 elfcpp::SHN_UNDEF, // small_common_shndx
440 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
441 0, // small_common_section_flags
442 elfcpp::SHF_X86_64_LARGE // large_common_section_flags
445 // This is called when a new output section is created. This is where
446 // we handle the SHF_X86_64_LARGE.
448 void
449 Target_x86_64::do_new_output_section(Output_section *os) const
451 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
452 os->set_is_large_section();
455 // Get the GOT section, creating it if necessary.
457 Output_data_got<64, false>*
458 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
460 if (this->got_ == NULL)
462 gold_assert(symtab != NULL && layout != NULL);
464 this->got_ = new Output_data_got<64, false>();
466 Output_section* os;
467 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
468 (elfcpp::SHF_ALLOC
469 | elfcpp::SHF_WRITE),
470 this->got_, false);
471 os->set_is_relro();
473 // The old GNU linker creates a .got.plt section. We just
474 // create another set of data in the .got section. Note that we
475 // always create a PLT if we create a GOT, although the PLT
476 // might be empty.
477 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
478 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
479 (elfcpp::SHF_ALLOC
480 | elfcpp::SHF_WRITE),
481 this->got_plt_, false);
482 os->set_is_relro();
484 // The first three entries are reserved.
485 this->got_plt_->set_current_data_size(3 * 8);
487 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
488 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
489 this->got_plt_,
490 0, 0, elfcpp::STT_OBJECT,
491 elfcpp::STB_LOCAL,
492 elfcpp::STV_HIDDEN, 0,
493 false, false);
496 return this->got_;
499 // Get the dynamic reloc section, creating it if necessary.
501 Target_x86_64::Reloc_section*
502 Target_x86_64::rela_dyn_section(Layout* layout)
504 if (this->rela_dyn_ == NULL)
506 gold_assert(layout != NULL);
507 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
508 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
509 elfcpp::SHF_ALLOC, this->rela_dyn_, true);
511 return this->rela_dyn_;
514 // A class to handle the PLT data.
516 class Output_data_plt_x86_64 : public Output_section_data
518 public:
519 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
521 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
522 Output_data_space*);
524 // Add an entry to the PLT.
525 void
526 add_entry(Symbol* gsym);
528 // Add the reserved TLSDESC_PLT entry to the PLT.
529 void
530 reserve_tlsdesc_entry(unsigned int got_offset)
531 { this->tlsdesc_got_offset_ = got_offset; }
533 // Return true if a TLSDESC_PLT entry has been reserved.
534 bool
535 has_tlsdesc_entry() const
536 { return this->tlsdesc_got_offset_ != -1U; }
538 // Return the GOT offset for the reserved TLSDESC_PLT entry.
539 unsigned int
540 get_tlsdesc_got_offset() const
541 { return this->tlsdesc_got_offset_; }
543 // Return the offset of the reserved TLSDESC_PLT entry.
544 unsigned int
545 get_tlsdesc_plt_offset() const
546 { return (this->count_ + 1) * plt_entry_size; }
548 // Return the .rel.plt section data.
549 const Reloc_section*
550 rel_plt() const
551 { return this->rel_; }
553 protected:
554 void
555 do_adjust_output_section(Output_section* os);
557 // Write to a map file.
558 void
559 do_print_to_mapfile(Mapfile* mapfile) const
560 { mapfile->print_output_data(this, _("** PLT")); }
562 private:
563 // The size of an entry in the PLT.
564 static const int plt_entry_size = 16;
566 // The first entry in the PLT.
567 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
568 // procedure linkage table for both programs and shared objects."
569 static unsigned char first_plt_entry[plt_entry_size];
571 // Other entries in the PLT for an executable.
572 static unsigned char plt_entry[plt_entry_size];
574 // The reserved TLSDESC entry in the PLT for an executable.
575 static unsigned char tlsdesc_plt_entry[plt_entry_size];
577 // Set the final size.
578 void
579 set_final_data_size();
581 // Write out the PLT data.
582 void
583 do_write(Output_file*);
585 // The reloc section.
586 Reloc_section* rel_;
587 // The .got section.
588 Output_data_got<64, false>* got_;
589 // The .got.plt section.
590 Output_data_space* got_plt_;
591 // The number of PLT entries.
592 unsigned int count_;
593 // Offset of the reserved TLSDESC_GOT entry when needed.
594 unsigned int tlsdesc_got_offset_;
597 // Create the PLT section. The ordinary .got section is an argument,
598 // since we need to refer to the start. We also create our own .got
599 // section just for PLT entries.
601 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
602 Output_data_got<64, false>* got,
603 Output_data_space* got_plt)
604 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
605 tlsdesc_got_offset_(-1U)
607 this->rel_ = new Reloc_section(false);
608 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
609 elfcpp::SHF_ALLOC, this->rel_, true);
612 void
613 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
615 os->set_entsize(plt_entry_size);
618 // Add an entry to the PLT.
620 void
621 Output_data_plt_x86_64::add_entry(Symbol* gsym)
623 gold_assert(!gsym->has_plt_offset());
625 // Note that when setting the PLT offset we skip the initial
626 // reserved PLT entry.
627 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
629 ++this->count_;
631 section_offset_type got_offset = this->got_plt_->current_data_size();
633 // Every PLT entry needs a GOT entry which points back to the PLT
634 // entry (this will be changed by the dynamic linker, normally
635 // lazily when the function is called).
636 this->got_plt_->set_current_data_size(got_offset + 8);
638 // Every PLT entry needs a reloc.
639 gsym->set_needs_dynsym_entry();
640 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
641 got_offset, 0);
643 // Note that we don't need to save the symbol. The contents of the
644 // PLT are independent of which symbols are used. The symbols only
645 // appear in the relocations.
648 // Set the final size.
649 void
650 Output_data_plt_x86_64::set_final_data_size()
652 unsigned int count = this->count_;
653 if (this->has_tlsdesc_entry())
654 ++count;
655 this->set_data_size((count + 1) * plt_entry_size);
658 // The first entry in the PLT for an executable.
660 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
662 // From AMD64 ABI Draft 0.98, page 76
663 0xff, 0x35, // pushq contents of memory address
664 0, 0, 0, 0, // replaced with address of .got + 8
665 0xff, 0x25, // jmp indirect
666 0, 0, 0, 0, // replaced with address of .got + 16
667 0x90, 0x90, 0x90, 0x90 // noop (x4)
670 // Subsequent entries in the PLT for an executable.
672 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
674 // From AMD64 ABI Draft 0.98, page 76
675 0xff, 0x25, // jmpq indirect
676 0, 0, 0, 0, // replaced with address of symbol in .got
677 0x68, // pushq immediate
678 0, 0, 0, 0, // replaced with offset into relocation table
679 0xe9, // jmpq relative
680 0, 0, 0, 0 // replaced with offset to start of .plt
683 // The reserved TLSDESC entry in the PLT for an executable.
685 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
687 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
688 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
689 0xff, 0x35, // pushq x(%rip)
690 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
691 0xff, 0x25, // jmpq *y(%rip)
692 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
693 0x0f, 0x1f, // nop
694 0x40, 0
697 // Write out the PLT. This uses the hand-coded instructions above,
698 // and adjusts them as needed. This is specified by the AMD64 ABI.
700 void
701 Output_data_plt_x86_64::do_write(Output_file* of)
703 const off_t offset = this->offset();
704 const section_size_type oview_size =
705 convert_to_section_size_type(this->data_size());
706 unsigned char* const oview = of->get_output_view(offset, oview_size);
708 const off_t got_file_offset = this->got_plt_->offset();
709 const section_size_type got_size =
710 convert_to_section_size_type(this->got_plt_->data_size());
711 unsigned char* const got_view = of->get_output_view(got_file_offset,
712 got_size);
714 unsigned char* pov = oview;
716 // The base address of the .plt section.
717 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
718 // The base address of the .got section.
719 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
720 // The base address of the PLT portion of the .got section,
721 // which is where the GOT pointer will point, and where the
722 // three reserved GOT entries are located.
723 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
725 memcpy(pov, first_plt_entry, plt_entry_size);
726 // We do a jmp relative to the PC at the end of this instruction.
727 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
728 (got_address + 8
729 - (plt_address + 6)));
730 elfcpp::Swap<32, false>::writeval(pov + 8,
731 (got_address + 16
732 - (plt_address + 12)));
733 pov += plt_entry_size;
735 unsigned char* got_pov = got_view;
737 memset(got_pov, 0, 24);
738 got_pov += 24;
740 unsigned int plt_offset = plt_entry_size;
741 unsigned int got_offset = 24;
742 const unsigned int count = this->count_;
743 for (unsigned int plt_index = 0;
744 plt_index < count;
745 ++plt_index,
746 pov += plt_entry_size,
747 got_pov += 8,
748 plt_offset += plt_entry_size,
749 got_offset += 8)
751 // Set and adjust the PLT entry itself.
752 memcpy(pov, plt_entry, plt_entry_size);
753 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
754 (got_address + got_offset
755 - (plt_address + plt_offset
756 + 6)));
758 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
759 elfcpp::Swap<32, false>::writeval(pov + 12,
760 - (plt_offset + plt_entry_size));
762 // Set the entry in the GOT.
763 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
766 if (this->has_tlsdesc_entry())
768 // Set and adjust the reserved TLSDESC PLT entry.
769 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
770 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
771 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
772 (got_address + 8
773 - (plt_address + plt_offset
774 + 6)));
775 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
776 (got_base
777 + tlsdesc_got_offset
778 - (plt_address + plt_offset
779 + 12)));
780 pov += plt_entry_size;
783 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
784 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
786 of->write_output_view(offset, oview_size, oview);
787 of->write_output_view(got_file_offset, got_size, got_view);
790 // Create the PLT section.
792 void
793 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
795 if (this->plt_ == NULL)
797 // Create the GOT sections first.
798 this->got_section(symtab, layout);
800 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
801 this->got_plt_);
802 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
803 (elfcpp::SHF_ALLOC
804 | elfcpp::SHF_EXECINSTR),
805 this->plt_, false);
809 // Create a PLT entry for a global symbol.
811 void
812 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
813 Symbol* gsym)
815 if (gsym->has_plt_offset())
816 return;
818 if (this->plt_ == NULL)
819 this->make_plt_section(symtab, layout);
821 this->plt_->add_entry(gsym);
824 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
826 void
827 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
829 if (this->tls_base_symbol_defined_)
830 return;
832 Output_segment* tls_segment = layout->tls_segment();
833 if (tls_segment != NULL)
835 bool is_exec = parameters->options().output_is_executable();
836 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
837 tls_segment, 0, 0,
838 elfcpp::STT_TLS,
839 elfcpp::STB_LOCAL,
840 elfcpp::STV_HIDDEN, 0,
841 (is_exec
842 ? Symbol::SEGMENT_END
843 : Symbol::SEGMENT_START),
844 true);
846 this->tls_base_symbol_defined_ = true;
849 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
851 void
852 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
853 Layout* layout)
855 if (this->plt_ == NULL)
856 this->make_plt_section(symtab, layout);
858 if (!this->plt_->has_tlsdesc_entry())
860 // Allocate the TLSDESC_GOT entry.
861 Output_data_got<64, false>* got = this->got_section(symtab, layout);
862 unsigned int got_offset = got->add_constant(0);
864 // Allocate the TLSDESC_PLT entry.
865 this->plt_->reserve_tlsdesc_entry(got_offset);
869 // Create a GOT entry for the TLS module index.
871 unsigned int
872 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
873 Sized_relobj<64, false>* object)
875 if (this->got_mod_index_offset_ == -1U)
877 gold_assert(symtab != NULL && layout != NULL && object != NULL);
878 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
879 Output_data_got<64, false>* got = this->got_section(symtab, layout);
880 unsigned int got_offset = got->add_constant(0);
881 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
882 got_offset, 0);
883 got->add_constant(0);
884 this->got_mod_index_offset_ = got_offset;
886 return this->got_mod_index_offset_;
889 // Optimize the TLS relocation type based on what we know about the
890 // symbol. IS_FINAL is true if the final address of this symbol is
891 // known at link time.
893 tls::Tls_optimization
894 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
896 // If we are generating a shared library, then we can't do anything
897 // in the linker.
898 if (parameters->options().shared())
899 return tls::TLSOPT_NONE;
901 switch (r_type)
903 case elfcpp::R_X86_64_TLSGD:
904 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
905 case elfcpp::R_X86_64_TLSDESC_CALL:
906 // These are General-Dynamic which permits fully general TLS
907 // access. Since we know that we are generating an executable,
908 // we can convert this to Initial-Exec. If we also know that
909 // this is a local symbol, we can further switch to Local-Exec.
910 if (is_final)
911 return tls::TLSOPT_TO_LE;
912 return tls::TLSOPT_TO_IE;
914 case elfcpp::R_X86_64_TLSLD:
915 // This is Local-Dynamic, which refers to a local symbol in the
916 // dynamic TLS block. Since we know that we generating an
917 // executable, we can switch to Local-Exec.
918 return tls::TLSOPT_TO_LE;
920 case elfcpp::R_X86_64_DTPOFF32:
921 case elfcpp::R_X86_64_DTPOFF64:
922 // Another Local-Dynamic reloc.
923 return tls::TLSOPT_TO_LE;
925 case elfcpp::R_X86_64_GOTTPOFF:
926 // These are Initial-Exec relocs which get the thread offset
927 // from the GOT. If we know that we are linking against the
928 // local symbol, we can switch to Local-Exec, which links the
929 // thread offset into the instruction.
930 if (is_final)
931 return tls::TLSOPT_TO_LE;
932 return tls::TLSOPT_NONE;
934 case elfcpp::R_X86_64_TPOFF32:
935 // When we already have Local-Exec, there is nothing further we
936 // can do.
937 return tls::TLSOPT_NONE;
939 default:
940 gold_unreachable();
944 // Report an unsupported relocation against a local symbol.
946 void
947 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
948 unsigned int r_type)
950 gold_error(_("%s: unsupported reloc %u against local symbol"),
951 object->name().c_str(), r_type);
954 // We are about to emit a dynamic relocation of type R_TYPE. If the
955 // dynamic linker does not support it, issue an error. The GNU linker
956 // only issues a non-PIC error for an allocated read-only section.
957 // Here we know the section is allocated, but we don't know that it is
958 // read-only. But we check for all the relocation types which the
959 // glibc dynamic linker supports, so it seems appropriate to issue an
960 // error even if the section is not read-only.
962 void
963 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
965 switch (r_type)
967 // These are the relocation types supported by glibc for x86_64.
968 case elfcpp::R_X86_64_RELATIVE:
969 case elfcpp::R_X86_64_GLOB_DAT:
970 case elfcpp::R_X86_64_JUMP_SLOT:
971 case elfcpp::R_X86_64_DTPMOD64:
972 case elfcpp::R_X86_64_DTPOFF64:
973 case elfcpp::R_X86_64_TPOFF64:
974 case elfcpp::R_X86_64_64:
975 case elfcpp::R_X86_64_32:
976 case elfcpp::R_X86_64_PC32:
977 case elfcpp::R_X86_64_COPY:
978 return;
980 default:
981 // This prevents us from issuing more than one error per reloc
982 // section. But we can still wind up issuing more than one
983 // error per object file.
984 if (this->issued_non_pic_error_)
985 return;
986 gold_assert(parameters->options().output_is_position_independent());
987 object->error(_("requires unsupported dynamic reloc; "
988 "recompile with -fPIC"));
989 this->issued_non_pic_error_ = true;
990 return;
992 case elfcpp::R_X86_64_NONE:
993 gold_unreachable();
997 // Scan a relocation for a local symbol.
999 inline void
1000 Target_x86_64::Scan::local(Symbol_table* symtab,
1001 Layout* layout,
1002 Target_x86_64* target,
1003 Sized_relobj<64, false>* object,
1004 unsigned int data_shndx,
1005 Output_section* output_section,
1006 const elfcpp::Rela<64, false>& reloc,
1007 unsigned int r_type,
1008 const elfcpp::Sym<64, false>& lsym)
1010 switch (r_type)
1012 case elfcpp::R_X86_64_NONE:
1013 case elfcpp::R_386_GNU_VTINHERIT:
1014 case elfcpp::R_386_GNU_VTENTRY:
1015 break;
1017 case elfcpp::R_X86_64_64:
1018 // If building a shared library (or a position-independent
1019 // executable), we need to create a dynamic relocation for this
1020 // location. The relocation applied at link time will apply the
1021 // link-time value, so we flag the location with an
1022 // R_X86_64_RELATIVE relocation so the dynamic loader can
1023 // relocate it easily.
1024 if (parameters->options().output_is_position_independent())
1026 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1027 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1028 rela_dyn->add_local_relative(object, r_sym,
1029 elfcpp::R_X86_64_RELATIVE,
1030 output_section, data_shndx,
1031 reloc.get_r_offset(),
1032 reloc.get_r_addend());
1034 break;
1036 case elfcpp::R_X86_64_32:
1037 case elfcpp::R_X86_64_32S:
1038 case elfcpp::R_X86_64_16:
1039 case elfcpp::R_X86_64_8:
1040 // If building a shared library (or a position-independent
1041 // executable), we need to create a dynamic relocation for this
1042 // location. We can't use an R_X86_64_RELATIVE relocation
1043 // because that is always a 64-bit relocation.
1044 if (parameters->options().output_is_position_independent())
1046 this->check_non_pic(object, r_type);
1048 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1049 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1050 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1051 rela_dyn->add_local(object, r_sym, r_type, output_section,
1052 data_shndx, reloc.get_r_offset(),
1053 reloc.get_r_addend());
1054 else
1056 gold_assert(lsym.get_st_value() == 0);
1057 unsigned int shndx = lsym.get_st_shndx();
1058 bool is_ordinary;
1059 shndx = object->adjust_sym_shndx(r_sym, shndx,
1060 &is_ordinary);
1061 if (!is_ordinary)
1062 object->error(_("section symbol %u has bad shndx %u"),
1063 r_sym, shndx);
1064 else
1065 rela_dyn->add_local_section(object, shndx,
1066 r_type, output_section,
1067 data_shndx, reloc.get_r_offset(),
1068 reloc.get_r_addend());
1071 break;
1073 case elfcpp::R_X86_64_PC64:
1074 case elfcpp::R_X86_64_PC32:
1075 case elfcpp::R_X86_64_PC16:
1076 case elfcpp::R_X86_64_PC8:
1077 break;
1079 case elfcpp::R_X86_64_PLT32:
1080 // Since we know this is a local symbol, we can handle this as a
1081 // PC32 reloc.
1082 break;
1084 case elfcpp::R_X86_64_GOTPC32:
1085 case elfcpp::R_X86_64_GOTOFF64:
1086 case elfcpp::R_X86_64_GOTPC64:
1087 case elfcpp::R_X86_64_PLTOFF64:
1088 // We need a GOT section.
1089 target->got_section(symtab, layout);
1090 // For PLTOFF64, we'd normally want a PLT section, but since we
1091 // know this is a local symbol, no PLT is needed.
1092 break;
1094 case elfcpp::R_X86_64_GOT64:
1095 case elfcpp::R_X86_64_GOT32:
1096 case elfcpp::R_X86_64_GOTPCREL64:
1097 case elfcpp::R_X86_64_GOTPCREL:
1098 case elfcpp::R_X86_64_GOTPLT64:
1100 // The symbol requires a GOT entry.
1101 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1102 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1103 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1105 // If we are generating a shared object, we need to add a
1106 // dynamic relocation for this symbol's GOT entry.
1107 if (parameters->options().output_is_position_independent())
1109 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1110 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1111 if (r_type != elfcpp::R_X86_64_GOT32)
1112 rela_dyn->add_local_relative(
1113 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1114 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1115 else
1117 this->check_non_pic(object, r_type);
1119 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1120 rela_dyn->add_local(
1121 object, r_sym, r_type, got,
1122 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1126 // For GOTPLT64, we'd normally want a PLT section, but since
1127 // we know this is a local symbol, no PLT is needed.
1129 break;
1131 case elfcpp::R_X86_64_COPY:
1132 case elfcpp::R_X86_64_GLOB_DAT:
1133 case elfcpp::R_X86_64_JUMP_SLOT:
1134 case elfcpp::R_X86_64_RELATIVE:
1135 // These are outstanding tls relocs, which are unexpected when linking
1136 case elfcpp::R_X86_64_TPOFF64:
1137 case elfcpp::R_X86_64_DTPMOD64:
1138 case elfcpp::R_X86_64_TLSDESC:
1139 gold_error(_("%s: unexpected reloc %u in object file"),
1140 object->name().c_str(), r_type);
1141 break;
1143 // These are initial tls relocs, which are expected when linking
1144 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1145 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1146 case elfcpp::R_X86_64_TLSDESC_CALL:
1147 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1148 case elfcpp::R_X86_64_DTPOFF32:
1149 case elfcpp::R_X86_64_DTPOFF64:
1150 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1151 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1153 bool output_is_shared = parameters->options().shared();
1154 const tls::Tls_optimization optimized_type
1155 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1156 switch (r_type)
1158 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1159 if (optimized_type == tls::TLSOPT_NONE)
1161 // Create a pair of GOT entries for the module index and
1162 // dtv-relative offset.
1163 Output_data_got<64, false>* got
1164 = target->got_section(symtab, layout);
1165 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1166 unsigned int shndx = lsym.get_st_shndx();
1167 bool is_ordinary;
1168 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1169 if (!is_ordinary)
1170 object->error(_("local symbol %u has bad shndx %u"),
1171 r_sym, shndx);
1172 else
1173 got->add_local_pair_with_rela(object, r_sym,
1174 shndx,
1175 GOT_TYPE_TLS_PAIR,
1176 target->rela_dyn_section(layout),
1177 elfcpp::R_X86_64_DTPMOD64, 0);
1179 else if (optimized_type != tls::TLSOPT_TO_LE)
1180 unsupported_reloc_local(object, r_type);
1181 break;
1183 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1184 target->define_tls_base_symbol(symtab, layout);
1185 if (optimized_type == tls::TLSOPT_NONE)
1187 // Create reserved PLT and GOT entries for the resolver.
1188 target->reserve_tlsdesc_entries(symtab, layout);
1190 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1191 Output_data_got<64, false>* got
1192 = target->got_section(symtab, layout);
1193 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1194 unsigned int shndx = lsym.get_st_shndx();
1195 bool is_ordinary;
1196 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1197 if (!is_ordinary)
1198 object->error(_("local symbol %u has bad shndx %u"),
1199 r_sym, shndx);
1200 else
1201 got->add_local_pair_with_rela(object, r_sym,
1202 shndx,
1203 GOT_TYPE_TLS_DESC,
1204 target->rela_dyn_section(layout),
1205 elfcpp::R_X86_64_TLSDESC, 0);
1207 else if (optimized_type != tls::TLSOPT_TO_LE)
1208 unsupported_reloc_local(object, r_type);
1209 break;
1211 case elfcpp::R_X86_64_TLSDESC_CALL:
1212 break;
1214 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1215 if (optimized_type == tls::TLSOPT_NONE)
1217 // Create a GOT entry for the module index.
1218 target->got_mod_index_entry(symtab, layout, object);
1220 else if (optimized_type != tls::TLSOPT_TO_LE)
1221 unsupported_reloc_local(object, r_type);
1222 break;
1224 case elfcpp::R_X86_64_DTPOFF32:
1225 case elfcpp::R_X86_64_DTPOFF64:
1226 break;
1228 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1229 layout->set_has_static_tls();
1230 if (optimized_type == tls::TLSOPT_NONE)
1232 // Create a GOT entry for the tp-relative offset.
1233 Output_data_got<64, false>* got
1234 = target->got_section(symtab, layout);
1235 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1236 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1237 target->rela_dyn_section(layout),
1238 elfcpp::R_X86_64_TPOFF64);
1240 else if (optimized_type != tls::TLSOPT_TO_LE)
1241 unsupported_reloc_local(object, r_type);
1242 break;
1244 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1245 layout->set_has_static_tls();
1246 if (output_is_shared)
1247 unsupported_reloc_local(object, r_type);
1248 break;
1250 default:
1251 gold_unreachable();
1254 break;
1256 case elfcpp::R_X86_64_SIZE32:
1257 case elfcpp::R_X86_64_SIZE64:
1258 default:
1259 gold_error(_("%s: unsupported reloc %u against local symbol"),
1260 object->name().c_str(), r_type);
1261 break;
1266 // Report an unsupported relocation against a global symbol.
1268 void
1269 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1270 unsigned int r_type,
1271 Symbol* gsym)
1273 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1274 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1277 // Scan a relocation for a global symbol.
1279 inline void
1280 Target_x86_64::Scan::global(Symbol_table* symtab,
1281 Layout* layout,
1282 Target_x86_64* target,
1283 Sized_relobj<64, false>* object,
1284 unsigned int data_shndx,
1285 Output_section* output_section,
1286 const elfcpp::Rela<64, false>& reloc,
1287 unsigned int r_type,
1288 Symbol* gsym)
1290 switch (r_type)
1292 case elfcpp::R_X86_64_NONE:
1293 case elfcpp::R_386_GNU_VTINHERIT:
1294 case elfcpp::R_386_GNU_VTENTRY:
1295 break;
1297 case elfcpp::R_X86_64_64:
1298 case elfcpp::R_X86_64_32:
1299 case elfcpp::R_X86_64_32S:
1300 case elfcpp::R_X86_64_16:
1301 case elfcpp::R_X86_64_8:
1303 // Make a PLT entry if necessary.
1304 if (gsym->needs_plt_entry())
1306 target->make_plt_entry(symtab, layout, gsym);
1307 // Since this is not a PC-relative relocation, we may be
1308 // taking the address of a function. In that case we need to
1309 // set the entry in the dynamic symbol table to the address of
1310 // the PLT entry.
1311 if (gsym->is_from_dynobj() && !parameters->options().shared())
1312 gsym->set_needs_dynsym_value();
1314 // Make a dynamic relocation if necessary.
1315 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1317 if (gsym->may_need_copy_reloc())
1319 target->copy_reloc(symtab, layout, object,
1320 data_shndx, output_section, gsym, reloc);
1322 else if (r_type == elfcpp::R_X86_64_64
1323 && gsym->can_use_relative_reloc(false))
1325 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1326 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1327 output_section, object,
1328 data_shndx, reloc.get_r_offset(),
1329 reloc.get_r_addend());
1331 else
1333 this->check_non_pic(object, r_type);
1334 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1335 rela_dyn->add_global(gsym, r_type, output_section, object,
1336 data_shndx, reloc.get_r_offset(),
1337 reloc.get_r_addend());
1341 break;
1343 case elfcpp::R_X86_64_PC64:
1344 case elfcpp::R_X86_64_PC32:
1345 case elfcpp::R_X86_64_PC16:
1346 case elfcpp::R_X86_64_PC8:
1348 // Make a PLT entry if necessary.
1349 if (gsym->needs_plt_entry())
1350 target->make_plt_entry(symtab, layout, gsym);
1351 // Make a dynamic relocation if necessary.
1352 int flags = Symbol::NON_PIC_REF;
1353 if (gsym->type() == elfcpp::STT_FUNC)
1354 flags |= Symbol::FUNCTION_CALL;
1355 if (gsym->needs_dynamic_reloc(flags))
1357 if (gsym->may_need_copy_reloc())
1359 target->copy_reloc(symtab, layout, object,
1360 data_shndx, output_section, gsym, reloc);
1362 else
1364 this->check_non_pic(object, r_type);
1365 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1366 rela_dyn->add_global(gsym, r_type, output_section, object,
1367 data_shndx, reloc.get_r_offset(),
1368 reloc.get_r_addend());
1372 break;
1374 case elfcpp::R_X86_64_GOT64:
1375 case elfcpp::R_X86_64_GOT32:
1376 case elfcpp::R_X86_64_GOTPCREL64:
1377 case elfcpp::R_X86_64_GOTPCREL:
1378 case elfcpp::R_X86_64_GOTPLT64:
1380 // The symbol requires a GOT entry.
1381 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1382 if (gsym->final_value_is_known())
1383 got->add_global(gsym, GOT_TYPE_STANDARD);
1384 else
1386 // If this symbol is not fully resolved, we need to add a
1387 // dynamic relocation for it.
1388 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1389 if (gsym->is_from_dynobj()
1390 || gsym->is_undefined()
1391 || gsym->is_preemptible())
1392 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1393 elfcpp::R_X86_64_GLOB_DAT);
1394 else
1396 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1397 rela_dyn->add_global_relative(
1398 gsym, elfcpp::R_X86_64_RELATIVE, got,
1399 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1402 // For GOTPLT64, we also need a PLT entry (but only if the
1403 // symbol is not fully resolved).
1404 if (r_type == elfcpp::R_X86_64_GOTPLT64
1405 && !gsym->final_value_is_known())
1406 target->make_plt_entry(symtab, layout, gsym);
1408 break;
1410 case elfcpp::R_X86_64_PLT32:
1411 // If the symbol is fully resolved, this is just a PC32 reloc.
1412 // Otherwise we need a PLT entry.
1413 if (gsym->final_value_is_known())
1414 break;
1415 // If building a shared library, we can also skip the PLT entry
1416 // if the symbol is defined in the output file and is protected
1417 // or hidden.
1418 if (gsym->is_defined()
1419 && !gsym->is_from_dynobj()
1420 && !gsym->is_preemptible())
1421 break;
1422 target->make_plt_entry(symtab, layout, gsym);
1423 break;
1425 case elfcpp::R_X86_64_GOTPC32:
1426 case elfcpp::R_X86_64_GOTOFF64:
1427 case elfcpp::R_X86_64_GOTPC64:
1428 case elfcpp::R_X86_64_PLTOFF64:
1429 // We need a GOT section.
1430 target->got_section(symtab, layout);
1431 // For PLTOFF64, we also need a PLT entry (but only if the
1432 // symbol is not fully resolved).
1433 if (r_type == elfcpp::R_X86_64_PLTOFF64
1434 && !gsym->final_value_is_known())
1435 target->make_plt_entry(symtab, layout, gsym);
1436 break;
1438 case elfcpp::R_X86_64_COPY:
1439 case elfcpp::R_X86_64_GLOB_DAT:
1440 case elfcpp::R_X86_64_JUMP_SLOT:
1441 case elfcpp::R_X86_64_RELATIVE:
1442 // These are outstanding tls relocs, which are unexpected when linking
1443 case elfcpp::R_X86_64_TPOFF64:
1444 case elfcpp::R_X86_64_DTPMOD64:
1445 case elfcpp::R_X86_64_TLSDESC:
1446 gold_error(_("%s: unexpected reloc %u in object file"),
1447 object->name().c_str(), r_type);
1448 break;
1450 // These are initial tls relocs, which are expected for global()
1451 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1452 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1453 case elfcpp::R_X86_64_TLSDESC_CALL:
1454 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1455 case elfcpp::R_X86_64_DTPOFF32:
1456 case elfcpp::R_X86_64_DTPOFF64:
1457 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1458 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1460 const bool is_final = gsym->final_value_is_known();
1461 const tls::Tls_optimization optimized_type
1462 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1463 switch (r_type)
1465 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1466 if (optimized_type == tls::TLSOPT_NONE)
1468 // Create a pair of GOT entries for the module index and
1469 // dtv-relative offset.
1470 Output_data_got<64, false>* got
1471 = target->got_section(symtab, layout);
1472 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1473 target->rela_dyn_section(layout),
1474 elfcpp::R_X86_64_DTPMOD64,
1475 elfcpp::R_X86_64_DTPOFF64);
1477 else if (optimized_type == tls::TLSOPT_TO_IE)
1479 // Create a GOT entry for the tp-relative offset.
1480 Output_data_got<64, false>* got
1481 = target->got_section(symtab, layout);
1482 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1483 target->rela_dyn_section(layout),
1484 elfcpp::R_X86_64_TPOFF64);
1486 else if (optimized_type != tls::TLSOPT_TO_LE)
1487 unsupported_reloc_global(object, r_type, gsym);
1488 break;
1490 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1491 target->define_tls_base_symbol(symtab, layout);
1492 if (optimized_type == tls::TLSOPT_NONE)
1494 // Create reserved PLT and GOT entries for the resolver.
1495 target->reserve_tlsdesc_entries(symtab, layout);
1497 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1498 Output_data_got<64, false>* got
1499 = target->got_section(symtab, layout);
1500 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1501 target->rela_dyn_section(layout),
1502 elfcpp::R_X86_64_TLSDESC, 0);
1504 else if (optimized_type == tls::TLSOPT_TO_IE)
1506 // Create a GOT entry for the tp-relative offset.
1507 Output_data_got<64, false>* got
1508 = target->got_section(symtab, layout);
1509 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1510 target->rela_dyn_section(layout),
1511 elfcpp::R_X86_64_TPOFF64);
1513 else if (optimized_type != tls::TLSOPT_TO_LE)
1514 unsupported_reloc_global(object, r_type, gsym);
1515 break;
1517 case elfcpp::R_X86_64_TLSDESC_CALL:
1518 break;
1520 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1521 if (optimized_type == tls::TLSOPT_NONE)
1523 // Create a GOT entry for the module index.
1524 target->got_mod_index_entry(symtab, layout, object);
1526 else if (optimized_type != tls::TLSOPT_TO_LE)
1527 unsupported_reloc_global(object, r_type, gsym);
1528 break;
1530 case elfcpp::R_X86_64_DTPOFF32:
1531 case elfcpp::R_X86_64_DTPOFF64:
1532 break;
1534 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1535 layout->set_has_static_tls();
1536 if (optimized_type == tls::TLSOPT_NONE)
1538 // Create a GOT entry for the tp-relative offset.
1539 Output_data_got<64, false>* got
1540 = target->got_section(symtab, layout);
1541 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1542 target->rela_dyn_section(layout),
1543 elfcpp::R_X86_64_TPOFF64);
1545 else if (optimized_type != tls::TLSOPT_TO_LE)
1546 unsupported_reloc_global(object, r_type, gsym);
1547 break;
1549 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1550 layout->set_has_static_tls();
1551 if (parameters->options().shared())
1552 unsupported_reloc_local(object, r_type);
1553 break;
1555 default:
1556 gold_unreachable();
1559 break;
1561 case elfcpp::R_X86_64_SIZE32:
1562 case elfcpp::R_X86_64_SIZE64:
1563 default:
1564 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1565 object->name().c_str(), r_type,
1566 gsym->demangled_name().c_str());
1567 break;
1571 void
1572 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
1573 Layout* layout,
1574 Sized_relobj<64, false>* object,
1575 unsigned int data_shndx,
1576 unsigned int sh_type,
1577 const unsigned char* prelocs,
1578 size_t reloc_count,
1579 Output_section* output_section,
1580 bool needs_special_offset_handling,
1581 size_t local_symbol_count,
1582 const unsigned char* plocal_symbols)
1585 if (sh_type == elfcpp::SHT_REL)
1587 return;
1590 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1591 Target_x86_64::Scan>(
1592 symtab,
1593 layout,
1594 this,
1595 object,
1596 data_shndx,
1597 prelocs,
1598 reloc_count,
1599 output_section,
1600 needs_special_offset_handling,
1601 local_symbol_count,
1602 plocal_symbols);
1605 // Scan relocations for a section.
1607 void
1608 Target_x86_64::scan_relocs(Symbol_table* symtab,
1609 Layout* layout,
1610 Sized_relobj<64, false>* object,
1611 unsigned int data_shndx,
1612 unsigned int sh_type,
1613 const unsigned char* prelocs,
1614 size_t reloc_count,
1615 Output_section* output_section,
1616 bool needs_special_offset_handling,
1617 size_t local_symbol_count,
1618 const unsigned char* plocal_symbols)
1620 if (sh_type == elfcpp::SHT_REL)
1622 gold_error(_("%s: unsupported REL reloc section"),
1623 object->name().c_str());
1624 return;
1627 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1628 Target_x86_64::Scan>(
1629 symtab,
1630 layout,
1631 this,
1632 object,
1633 data_shndx,
1634 prelocs,
1635 reloc_count,
1636 output_section,
1637 needs_special_offset_handling,
1638 local_symbol_count,
1639 plocal_symbols);
1642 // Finalize the sections.
1644 void
1645 Target_x86_64::do_finalize_sections(
1646 Layout* layout,
1647 const Input_objects*,
1648 Symbol_table*)
1650 // Fill in some more dynamic tags.
1651 Output_data_dynamic* const odyn = layout->dynamic_data();
1652 if (odyn != NULL)
1654 if (this->got_plt_ != NULL
1655 && this->got_plt_->output_section() != NULL)
1656 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1658 if (this->plt_ != NULL
1659 && this->plt_->output_section() != NULL)
1661 const Output_data* od = this->plt_->rel_plt();
1662 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1663 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1664 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1665 if (this->plt_->has_tlsdesc_entry())
1667 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1668 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1669 this->got_->finalize_data_size();
1670 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1671 this->plt_, plt_offset);
1672 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1673 this->got_, got_offset);
1677 if (this->rela_dyn_ != NULL
1678 && this->rela_dyn_->output_section() != NULL)
1680 const Output_data* od = this->rela_dyn_;
1681 odyn->add_section_address(elfcpp::DT_RELA, od);
1682 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1683 odyn->add_constant(elfcpp::DT_RELAENT,
1684 elfcpp::Elf_sizes<64>::rela_size);
1687 if (!parameters->options().shared())
1689 // The value of the DT_DEBUG tag is filled in by the dynamic
1690 // linker at run time, and used by the debugger.
1691 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1695 // Emit any relocs we saved in an attempt to avoid generating COPY
1696 // relocs.
1697 if (this->copy_relocs_.any_saved_relocs())
1698 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1701 // Perform a relocation.
1703 inline bool
1704 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1705 Target_x86_64* target,
1706 Output_section*,
1707 size_t relnum,
1708 const elfcpp::Rela<64, false>& rela,
1709 unsigned int r_type,
1710 const Sized_symbol<64>* gsym,
1711 const Symbol_value<64>* psymval,
1712 unsigned char* view,
1713 elfcpp::Elf_types<64>::Elf_Addr address,
1714 section_size_type view_size)
1716 if (this->skip_call_tls_get_addr_)
1718 if ((r_type != elfcpp::R_X86_64_PLT32
1719 && r_type != elfcpp::R_X86_64_PC32)
1720 || gsym == NULL
1721 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1723 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1724 _("missing expected TLS relocation"));
1726 else
1728 this->skip_call_tls_get_addr_ = false;
1729 return false;
1733 // Pick the value to use for symbols defined in shared objects.
1734 Symbol_value<64> symval;
1735 if (gsym != NULL
1736 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1737 || r_type == elfcpp::R_X86_64_PC32
1738 || r_type == elfcpp::R_X86_64_PC16
1739 || r_type == elfcpp::R_X86_64_PC8))
1741 symval.set_output_value(target->plt_section()->address()
1742 + gsym->plt_offset());
1743 psymval = &symval;
1746 const Sized_relobj<64, false>* object = relinfo->object;
1747 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1749 // Get the GOT offset if needed.
1750 // The GOT pointer points to the end of the GOT section.
1751 // We need to subtract the size of the GOT section to get
1752 // the actual offset to use in the relocation.
1753 bool have_got_offset = false;
1754 unsigned int got_offset = 0;
1755 switch (r_type)
1757 case elfcpp::R_X86_64_GOT32:
1758 case elfcpp::R_X86_64_GOT64:
1759 case elfcpp::R_X86_64_GOTPLT64:
1760 case elfcpp::R_X86_64_GOTPCREL:
1761 case elfcpp::R_X86_64_GOTPCREL64:
1762 if (gsym != NULL)
1764 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1765 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1767 else
1769 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1770 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1771 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1772 - target->got_size());
1774 have_got_offset = true;
1775 break;
1777 default:
1778 break;
1781 switch (r_type)
1783 case elfcpp::R_X86_64_NONE:
1784 case elfcpp::R_386_GNU_VTINHERIT:
1785 case elfcpp::R_386_GNU_VTENTRY:
1786 break;
1788 case elfcpp::R_X86_64_64:
1789 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1790 break;
1792 case elfcpp::R_X86_64_PC64:
1793 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1794 address);
1795 break;
1797 case elfcpp::R_X86_64_32:
1798 // FIXME: we need to verify that value + addend fits into 32 bits:
1799 // uint64_t x = value + addend;
1800 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1801 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1802 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1803 break;
1805 case elfcpp::R_X86_64_32S:
1806 // FIXME: we need to verify that value + addend fits into 32 bits:
1807 // int64_t x = value + addend; // note this quantity is signed!
1808 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1809 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1810 break;
1812 case elfcpp::R_X86_64_PC32:
1813 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1814 address);
1815 break;
1817 case elfcpp::R_X86_64_16:
1818 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1819 break;
1821 case elfcpp::R_X86_64_PC16:
1822 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1823 address);
1824 break;
1826 case elfcpp::R_X86_64_8:
1827 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1828 break;
1830 case elfcpp::R_X86_64_PC8:
1831 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1832 address);
1833 break;
1835 case elfcpp::R_X86_64_PLT32:
1836 gold_assert(gsym == NULL
1837 || gsym->has_plt_offset()
1838 || gsym->final_value_is_known()
1839 || (gsym->is_defined()
1840 && !gsym->is_from_dynobj()
1841 && !gsym->is_preemptible()));
1842 // Note: while this code looks the same as for R_X86_64_PC32, it
1843 // behaves differently because psymval was set to point to
1844 // the PLT entry, rather than the symbol, in Scan::global().
1845 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1846 address);
1847 break;
1849 case elfcpp::R_X86_64_PLTOFF64:
1851 gold_assert(gsym);
1852 gold_assert(gsym->has_plt_offset()
1853 || gsym->final_value_is_known());
1854 elfcpp::Elf_types<64>::Elf_Addr got_address;
1855 got_address = target->got_section(NULL, NULL)->address();
1856 Relocate_functions<64, false>::rela64(view, object, psymval,
1857 addend - got_address);
1860 case elfcpp::R_X86_64_GOT32:
1861 gold_assert(have_got_offset);
1862 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1863 break;
1865 case elfcpp::R_X86_64_GOTPC32:
1867 gold_assert(gsym);
1868 elfcpp::Elf_types<64>::Elf_Addr value;
1869 value = target->got_plt_section()->address();
1870 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1872 break;
1874 case elfcpp::R_X86_64_GOT64:
1875 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1876 // Since we always add a PLT entry, this is equivalent.
1877 case elfcpp::R_X86_64_GOTPLT64:
1878 gold_assert(have_got_offset);
1879 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1880 break;
1882 case elfcpp::R_X86_64_GOTPC64:
1884 gold_assert(gsym);
1885 elfcpp::Elf_types<64>::Elf_Addr value;
1886 value = target->got_plt_section()->address();
1887 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1889 break;
1891 case elfcpp::R_X86_64_GOTOFF64:
1893 elfcpp::Elf_types<64>::Elf_Addr value;
1894 value = (psymval->value(object, 0)
1895 - target->got_plt_section()->address());
1896 Relocate_functions<64, false>::rela64(view, value, addend);
1898 break;
1900 case elfcpp::R_X86_64_GOTPCREL:
1902 gold_assert(have_got_offset);
1903 elfcpp::Elf_types<64>::Elf_Addr value;
1904 value = target->got_plt_section()->address() + got_offset;
1905 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1907 break;
1909 case elfcpp::R_X86_64_GOTPCREL64:
1911 gold_assert(have_got_offset);
1912 elfcpp::Elf_types<64>::Elf_Addr value;
1913 value = target->got_plt_section()->address() + got_offset;
1914 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1916 break;
1918 case elfcpp::R_X86_64_COPY:
1919 case elfcpp::R_X86_64_GLOB_DAT:
1920 case elfcpp::R_X86_64_JUMP_SLOT:
1921 case elfcpp::R_X86_64_RELATIVE:
1922 // These are outstanding tls relocs, which are unexpected when linking
1923 case elfcpp::R_X86_64_TPOFF64:
1924 case elfcpp::R_X86_64_DTPMOD64:
1925 case elfcpp::R_X86_64_TLSDESC:
1926 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1927 _("unexpected reloc %u in object file"),
1928 r_type);
1929 break;
1931 // These are initial tls relocs, which are expected when linking
1932 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1933 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1934 case elfcpp::R_X86_64_TLSDESC_CALL:
1935 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1936 case elfcpp::R_X86_64_DTPOFF32:
1937 case elfcpp::R_X86_64_DTPOFF64:
1938 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1939 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1940 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1941 view, address, view_size);
1942 break;
1944 case elfcpp::R_X86_64_SIZE32:
1945 case elfcpp::R_X86_64_SIZE64:
1946 default:
1947 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1948 _("unsupported reloc %u"),
1949 r_type);
1950 break;
1953 return true;
1956 // Perform a TLS relocation.
1958 inline void
1959 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1960 Target_x86_64* target,
1961 size_t relnum,
1962 const elfcpp::Rela<64, false>& rela,
1963 unsigned int r_type,
1964 const Sized_symbol<64>* gsym,
1965 const Symbol_value<64>* psymval,
1966 unsigned char* view,
1967 elfcpp::Elf_types<64>::Elf_Addr address,
1968 section_size_type view_size)
1970 Output_segment* tls_segment = relinfo->layout->tls_segment();
1972 const Sized_relobj<64, false>* object = relinfo->object;
1973 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1975 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1977 const bool is_final = (gsym == NULL
1978 ? !parameters->options().output_is_position_independent()
1979 : gsym->final_value_is_known());
1980 const tls::Tls_optimization optimized_type
1981 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1982 switch (r_type)
1984 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1985 this->saw_tls_block_reloc_ = true;
1986 if (optimized_type == tls::TLSOPT_TO_LE)
1988 gold_assert(tls_segment != NULL);
1989 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1990 rela, r_type, value, view,
1991 view_size);
1992 break;
1994 else
1996 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1997 ? GOT_TYPE_TLS_OFFSET
1998 : GOT_TYPE_TLS_PAIR);
1999 unsigned int got_offset;
2000 if (gsym != NULL)
2002 gold_assert(gsym->has_got_offset(got_type));
2003 got_offset = gsym->got_offset(got_type) - target->got_size();
2005 else
2007 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2008 gold_assert(object->local_has_got_offset(r_sym, got_type));
2009 got_offset = (object->local_got_offset(r_sym, got_type)
2010 - target->got_size());
2012 if (optimized_type == tls::TLSOPT_TO_IE)
2014 gold_assert(tls_segment != NULL);
2015 value = target->got_plt_section()->address() + got_offset;
2016 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2017 value, view, address, view_size);
2018 break;
2020 else if (optimized_type == tls::TLSOPT_NONE)
2022 // Relocate the field with the offset of the pair of GOT
2023 // entries.
2024 value = target->got_plt_section()->address() + got_offset;
2025 Relocate_functions<64, false>::pcrela32(view, value, addend,
2026 address);
2027 break;
2030 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2031 _("unsupported reloc %u"), r_type);
2032 break;
2034 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2035 case elfcpp::R_X86_64_TLSDESC_CALL:
2036 this->saw_tls_block_reloc_ = true;
2037 if (optimized_type == tls::TLSOPT_TO_LE)
2039 gold_assert(tls_segment != NULL);
2040 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2041 rela, r_type, value, view,
2042 view_size);
2043 break;
2045 else
2047 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2048 ? GOT_TYPE_TLS_OFFSET
2049 : GOT_TYPE_TLS_DESC);
2050 unsigned int got_offset;
2051 if (gsym != NULL)
2053 gold_assert(gsym->has_got_offset(got_type));
2054 got_offset = gsym->got_offset(got_type) - target->got_size();
2056 else
2058 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2059 gold_assert(object->local_has_got_offset(r_sym, got_type));
2060 got_offset = (object->local_got_offset(r_sym, got_type)
2061 - target->got_size());
2063 if (optimized_type == tls::TLSOPT_TO_IE)
2065 gold_assert(tls_segment != NULL);
2066 value = target->got_plt_section()->address() + got_offset;
2067 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2068 rela, r_type, value, view, address,
2069 view_size);
2070 break;
2072 else if (optimized_type == tls::TLSOPT_NONE)
2074 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2076 // Relocate the field with the offset of the pair of GOT
2077 // entries.
2078 value = target->got_plt_section()->address() + got_offset;
2079 Relocate_functions<64, false>::pcrela32(view, value, addend,
2080 address);
2082 break;
2085 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2086 _("unsupported reloc %u"), r_type);
2087 break;
2089 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2090 this->saw_tls_block_reloc_ = true;
2091 if (optimized_type == tls::TLSOPT_TO_LE)
2093 gold_assert(tls_segment != NULL);
2094 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2095 value, view, view_size);
2096 break;
2098 else if (optimized_type == tls::TLSOPT_NONE)
2100 // Relocate the field with the offset of the GOT entry for
2101 // the module index.
2102 unsigned int got_offset;
2103 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2104 - target->got_size());
2105 value = target->got_plt_section()->address() + got_offset;
2106 Relocate_functions<64, false>::pcrela32(view, value, addend,
2107 address);
2108 break;
2110 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2111 _("unsupported reloc %u"), r_type);
2112 break;
2114 case elfcpp::R_X86_64_DTPOFF32:
2115 if (optimized_type == tls::TLSOPT_TO_LE)
2117 // This relocation type is used in debugging information.
2118 // In that case we need to not optimize the value. If we
2119 // haven't seen a TLSLD reloc, then we assume we should not
2120 // optimize this reloc.
2121 if (this->saw_tls_block_reloc_)
2123 gold_assert(tls_segment != NULL);
2124 value -= tls_segment->memsz();
2127 Relocate_functions<64, false>::rela32(view, value, addend);
2128 break;
2130 case elfcpp::R_X86_64_DTPOFF64:
2131 if (optimized_type == tls::TLSOPT_TO_LE)
2133 // See R_X86_64_DTPOFF32, just above, for why we test this.
2134 if (this->saw_tls_block_reloc_)
2136 gold_assert(tls_segment != NULL);
2137 value -= tls_segment->memsz();
2140 Relocate_functions<64, false>::rela64(view, value, addend);
2141 break;
2143 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2144 if (optimized_type == tls::TLSOPT_TO_LE)
2146 gold_assert(tls_segment != NULL);
2147 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2148 rela, r_type, value, view,
2149 view_size);
2150 break;
2152 else if (optimized_type == tls::TLSOPT_NONE)
2154 // Relocate the field with the offset of the GOT entry for
2155 // the tp-relative offset of the symbol.
2156 unsigned int got_offset;
2157 if (gsym != NULL)
2159 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2160 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2161 - target->got_size());
2163 else
2165 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2166 gold_assert(object->local_has_got_offset(r_sym,
2167 GOT_TYPE_TLS_OFFSET));
2168 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2169 - target->got_size());
2171 value = target->got_plt_section()->address() + got_offset;
2172 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2173 break;
2175 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2176 _("unsupported reloc type %u"),
2177 r_type);
2178 break;
2180 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2181 value -= tls_segment->memsz();
2182 Relocate_functions<64, false>::rela32(view, value, addend);
2183 break;
2187 // Do a relocation in which we convert a TLS General-Dynamic to an
2188 // Initial-Exec.
2190 inline void
2191 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2192 size_t relnum,
2193 Output_segment*,
2194 const elfcpp::Rela<64, false>& rela,
2195 unsigned int,
2196 elfcpp::Elf_types<64>::Elf_Addr value,
2197 unsigned char* view,
2198 elfcpp::Elf_types<64>::Elf_Addr address,
2199 section_size_type view_size)
2201 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2202 // .word 0x6666; rex64; call __tls_get_addr
2203 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2205 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2206 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2208 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2209 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2210 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2211 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2213 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2215 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2216 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2218 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2219 // We can skip it.
2220 this->skip_call_tls_get_addr_ = true;
2223 // Do a relocation in which we convert a TLS General-Dynamic to a
2224 // Local-Exec.
2226 inline void
2227 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2228 size_t relnum,
2229 Output_segment* tls_segment,
2230 const elfcpp::Rela<64, false>& rela,
2231 unsigned int,
2232 elfcpp::Elf_types<64>::Elf_Addr value,
2233 unsigned char* view,
2234 section_size_type view_size)
2236 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2237 // .word 0x6666; rex64; call __tls_get_addr
2238 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2240 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2241 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2243 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2244 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2245 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2246 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2248 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2250 value -= tls_segment->memsz();
2251 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2253 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2254 // We can skip it.
2255 this->skip_call_tls_get_addr_ = true;
2258 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2260 inline void
2261 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2262 const Relocate_info<64, false>* relinfo,
2263 size_t relnum,
2264 Output_segment*,
2265 const elfcpp::Rela<64, false>& rela,
2266 unsigned int r_type,
2267 elfcpp::Elf_types<64>::Elf_Addr value,
2268 unsigned char* view,
2269 elfcpp::Elf_types<64>::Elf_Addr address,
2270 section_size_type view_size)
2272 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2274 // leaq foo@tlsdesc(%rip), %rax
2275 // ==> movq foo@gottpoff(%rip), %rax
2276 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2277 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2278 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2279 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2280 view[-2] = 0x8b;
2281 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2282 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2284 else
2286 // call *foo@tlscall(%rax)
2287 // ==> nop; nop
2288 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2289 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2290 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2291 view[0] == 0xff && view[1] == 0x10);
2292 view[0] = 0x66;
2293 view[1] = 0x90;
2297 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2299 inline void
2300 Target_x86_64::Relocate::tls_desc_gd_to_le(
2301 const Relocate_info<64, false>* relinfo,
2302 size_t relnum,
2303 Output_segment* tls_segment,
2304 const elfcpp::Rela<64, false>& rela,
2305 unsigned int r_type,
2306 elfcpp::Elf_types<64>::Elf_Addr value,
2307 unsigned char* view,
2308 section_size_type view_size)
2310 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2312 // leaq foo@tlsdesc(%rip), %rax
2313 // ==> movq foo@tpoff, %rax
2314 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2315 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2316 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2317 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2318 view[-2] = 0xc7;
2319 view[-1] = 0xc0;
2320 value -= tls_segment->memsz();
2321 Relocate_functions<64, false>::rela32(view, value, 0);
2323 else
2325 // call *foo@tlscall(%rax)
2326 // ==> nop; nop
2327 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2328 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2329 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2330 view[0] == 0xff && view[1] == 0x10);
2331 view[0] = 0x66;
2332 view[1] = 0x90;
2336 inline void
2337 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2338 size_t relnum,
2339 Output_segment*,
2340 const elfcpp::Rela<64, false>& rela,
2341 unsigned int,
2342 elfcpp::Elf_types<64>::Elf_Addr,
2343 unsigned char* view,
2344 section_size_type view_size)
2346 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2347 // ... leq foo@dtpoff(%rax),%reg
2348 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2350 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2351 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2353 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2354 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2356 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2358 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2360 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2361 // We can skip it.
2362 this->skip_call_tls_get_addr_ = true;
2365 // Do a relocation in which we convert a TLS Initial-Exec to a
2366 // Local-Exec.
2368 inline void
2369 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2370 size_t relnum,
2371 Output_segment* tls_segment,
2372 const elfcpp::Rela<64, false>& rela,
2373 unsigned int,
2374 elfcpp::Elf_types<64>::Elf_Addr value,
2375 unsigned char* view,
2376 section_size_type view_size)
2378 // We need to examine the opcodes to figure out which instruction we
2379 // are looking at.
2381 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2382 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2384 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2385 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2387 unsigned char op1 = view[-3];
2388 unsigned char op2 = view[-2];
2389 unsigned char op3 = view[-1];
2390 unsigned char reg = op3 >> 3;
2392 if (op2 == 0x8b)
2394 // movq
2395 if (op1 == 0x4c)
2396 view[-3] = 0x49;
2397 view[-2] = 0xc7;
2398 view[-1] = 0xc0 | reg;
2400 else if (reg == 4)
2402 // Special handling for %rsp.
2403 if (op1 == 0x4c)
2404 view[-3] = 0x49;
2405 view[-2] = 0x81;
2406 view[-1] = 0xc0 | reg;
2408 else
2410 // addq
2411 if (op1 == 0x4c)
2412 view[-3] = 0x4d;
2413 view[-2] = 0x8d;
2414 view[-1] = 0x80 | reg | (reg << 3);
2417 value -= tls_segment->memsz();
2418 Relocate_functions<64, false>::rela32(view, value, 0);
2421 // Relocate section data.
2423 void
2424 Target_x86_64::relocate_section(
2425 const Relocate_info<64, false>* relinfo,
2426 unsigned int sh_type,
2427 const unsigned char* prelocs,
2428 size_t reloc_count,
2429 Output_section* output_section,
2430 bool needs_special_offset_handling,
2431 unsigned char* view,
2432 elfcpp::Elf_types<64>::Elf_Addr address,
2433 section_size_type view_size,
2434 const Reloc_symbol_changes* reloc_symbol_changes)
2436 gold_assert(sh_type == elfcpp::SHT_RELA);
2438 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2439 Target_x86_64::Relocate>(
2440 relinfo,
2441 this,
2442 prelocs,
2443 reloc_count,
2444 output_section,
2445 needs_special_offset_handling,
2446 view,
2447 address,
2448 view_size,
2449 reloc_symbol_changes);
2452 // Return the size of a relocation while scanning during a relocatable
2453 // link.
2455 unsigned int
2456 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2457 unsigned int r_type,
2458 Relobj* object)
2460 switch (r_type)
2462 case elfcpp::R_X86_64_NONE:
2463 case elfcpp::R_386_GNU_VTINHERIT:
2464 case elfcpp::R_386_GNU_VTENTRY:
2465 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2466 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2467 case elfcpp::R_X86_64_TLSDESC_CALL:
2468 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2469 case elfcpp::R_X86_64_DTPOFF32:
2470 case elfcpp::R_X86_64_DTPOFF64:
2471 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2472 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2473 return 0;
2475 case elfcpp::R_X86_64_64:
2476 case elfcpp::R_X86_64_PC64:
2477 case elfcpp::R_X86_64_GOTOFF64:
2478 case elfcpp::R_X86_64_GOTPC64:
2479 case elfcpp::R_X86_64_PLTOFF64:
2480 case elfcpp::R_X86_64_GOT64:
2481 case elfcpp::R_X86_64_GOTPCREL64:
2482 case elfcpp::R_X86_64_GOTPCREL:
2483 case elfcpp::R_X86_64_GOTPLT64:
2484 return 8;
2486 case elfcpp::R_X86_64_32:
2487 case elfcpp::R_X86_64_32S:
2488 case elfcpp::R_X86_64_PC32:
2489 case elfcpp::R_X86_64_PLT32:
2490 case elfcpp::R_X86_64_GOTPC32:
2491 case elfcpp::R_X86_64_GOT32:
2492 return 4;
2494 case elfcpp::R_X86_64_16:
2495 case elfcpp::R_X86_64_PC16:
2496 return 2;
2498 case elfcpp::R_X86_64_8:
2499 case elfcpp::R_X86_64_PC8:
2500 return 1;
2502 case elfcpp::R_X86_64_COPY:
2503 case elfcpp::R_X86_64_GLOB_DAT:
2504 case elfcpp::R_X86_64_JUMP_SLOT:
2505 case elfcpp::R_X86_64_RELATIVE:
2506 // These are outstanding tls relocs, which are unexpected when linking
2507 case elfcpp::R_X86_64_TPOFF64:
2508 case elfcpp::R_X86_64_DTPMOD64:
2509 case elfcpp::R_X86_64_TLSDESC:
2510 object->error(_("unexpected reloc %u in object file"), r_type);
2511 return 0;
2513 case elfcpp::R_X86_64_SIZE32:
2514 case elfcpp::R_X86_64_SIZE64:
2515 default:
2516 object->error(_("unsupported reloc %u against local symbol"), r_type);
2517 return 0;
2521 // Scan the relocs during a relocatable link.
2523 void
2524 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
2525 Layout* layout,
2526 Sized_relobj<64, false>* object,
2527 unsigned int data_shndx,
2528 unsigned int sh_type,
2529 const unsigned char* prelocs,
2530 size_t reloc_count,
2531 Output_section* output_section,
2532 bool needs_special_offset_handling,
2533 size_t local_symbol_count,
2534 const unsigned char* plocal_symbols,
2535 Relocatable_relocs* rr)
2537 gold_assert(sh_type == elfcpp::SHT_RELA);
2539 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2540 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2542 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2543 Scan_relocatable_relocs>(
2544 symtab,
2545 layout,
2546 object,
2547 data_shndx,
2548 prelocs,
2549 reloc_count,
2550 output_section,
2551 needs_special_offset_handling,
2552 local_symbol_count,
2553 plocal_symbols,
2554 rr);
2557 // Relocate a section during a relocatable link.
2559 void
2560 Target_x86_64::relocate_for_relocatable(
2561 const Relocate_info<64, false>* relinfo,
2562 unsigned int sh_type,
2563 const unsigned char* prelocs,
2564 size_t reloc_count,
2565 Output_section* output_section,
2566 off_t offset_in_output_section,
2567 const Relocatable_relocs* rr,
2568 unsigned char* view,
2569 elfcpp::Elf_types<64>::Elf_Addr view_address,
2570 section_size_type view_size,
2571 unsigned char* reloc_view,
2572 section_size_type reloc_view_size)
2574 gold_assert(sh_type == elfcpp::SHT_RELA);
2576 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2577 relinfo,
2578 prelocs,
2579 reloc_count,
2580 output_section,
2581 offset_in_output_section,
2583 view,
2584 view_address,
2585 view_size,
2586 reloc_view,
2587 reloc_view_size);
2590 // Return the value to use for a dynamic which requires special
2591 // treatment. This is how we support equality comparisons of function
2592 // pointers across shared library boundaries, as described in the
2593 // processor specific ABI supplement.
2595 uint64_t
2596 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2598 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2599 return this->plt_section()->address() + gsym->plt_offset();
2602 // Return a string used to fill a code section with nops to take up
2603 // the specified length.
2605 std::string
2606 Target_x86_64::do_code_fill(section_size_type length) const
2608 if (length >= 16)
2610 // Build a jmpq instruction to skip over the bytes.
2611 unsigned char jmp[5];
2612 jmp[0] = 0xe9;
2613 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2614 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2615 + std::string(length - 5, '\0'));
2618 // Nop sequences of various lengths.
2619 const char nop1[1] = { 0x90 }; // nop
2620 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2621 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2622 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2623 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2624 0x00 };
2625 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2626 0x00, 0x00 };
2627 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2628 0x00, 0x00, 0x00 };
2629 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2630 0x00, 0x00, 0x00, 0x00 };
2631 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2632 0x00, 0x00, 0x00, 0x00,
2633 0x00 };
2634 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2635 0x84, 0x00, 0x00, 0x00,
2636 0x00, 0x00 };
2637 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2638 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2639 0x00, 0x00, 0x00 };
2640 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2641 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2642 0x00, 0x00, 0x00, 0x00 };
2643 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2644 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2645 0x00, 0x00, 0x00, 0x00,
2646 0x00 };
2647 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2648 0x66, 0x2e, 0x0f, 0x1f, // data16
2649 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2650 0x00, 0x00 };
2651 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2652 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2653 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2654 0x00, 0x00, 0x00 };
2656 const char* nops[16] = {
2657 NULL,
2658 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2659 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2662 return std::string(nops[length], length);
2665 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2666 // compiled with -fstack-split. The function calls non-stack-split
2667 // code. We have to change the function so that it always ensures
2668 // that it has enough stack space to run some random function.
2670 void
2671 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2672 section_offset_type fnoffset,
2673 section_size_type fnsize,
2674 unsigned char* view,
2675 section_size_type view_size,
2676 std::string* from,
2677 std::string* to) const
2679 // The function starts with a comparison of the stack pointer and a
2680 // field in the TCB. This is followed by a jump.
2682 // cmp %fs:NN,%rsp
2683 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2684 && fnsize > 9)
2686 // We will call __morestack if the carry flag is set after this
2687 // comparison. We turn the comparison into an stc instruction
2688 // and some nops.
2689 view[fnoffset] = '\xf9';
2690 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2692 // lea NN(%rsp),%r10
2693 else if (this->match_view(view, view_size, fnoffset, "\x4c\x8d\x94\x24", 4)
2694 && fnsize > 8)
2696 // This is loading an offset from the stack pointer for a
2697 // comparison. The offset is negative, so we decrease the
2698 // offset by the amount of space we need for the stack. This
2699 // means we will avoid calling __morestack if there happens to
2700 // be plenty of space on the stack already.
2701 unsigned char* pval = view + fnoffset + 4;
2702 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2703 val -= parameters->options().split_stack_adjust_size();
2704 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2706 else
2708 if (!object->has_no_split_stack())
2709 object->error(_("failed to match split-stack sequence at "
2710 "section %u offset %0zx"),
2711 shndx, static_cast<size_t>(fnoffset));
2712 return;
2715 // We have to change the function so that it calls
2716 // __morestack_non_split instead of __morestack. The former will
2717 // allocate additional stack space.
2718 *from = "__morestack";
2719 *to = "__morestack_non_split";
2722 // The selector for x86_64 object files.
2724 class Target_selector_x86_64 : public Target_selector_freebsd
2726 public:
2727 Target_selector_x86_64()
2728 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2729 "elf64-x86-64-freebsd")
2732 Target*
2733 do_instantiate_target()
2734 { return new Target_x86_64(); }
2738 Target_selector_x86_64 target_selector_x86_64;
2740 } // End anonymous namespace.