* config/tc-avr.c (mcu_types): Add atmega16u4.
[binutils.git] / gold / x86_64.cc
blob2daa9bf44c59ea71443842df1230d6ee1f39f936
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 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"
41 namespace
44 using namespace gold;
46 class Output_data_plt_x86_64;
48 // The x86_64 target class.
49 // See the ABI at
50 // http://www.x86-64.org/documentation/abi.pdf
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_x86_64 : public Sized_target<64, false>
57 public:
58 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
59 // uses only Elf64_Rela relocation entries with explicit addends."
60 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
62 Target_x86_64()
63 : Sized_target<64, false>(&x86_64_info),
64 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
65 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
66 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
67 { }
69 // Scan the relocations to look for symbol adjustments.
70 void
71 scan_relocs(const General_options& options,
72 Symbol_table* symtab,
73 Layout* layout,
74 Sized_relobj<64, false>* object,
75 unsigned int data_shndx,
76 unsigned int sh_type,
77 const unsigned char* prelocs,
78 size_t reloc_count,
79 Output_section* output_section,
80 bool needs_special_offset_handling,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols);
84 // Finalize the sections.
85 void
86 do_finalize_sections(Layout*);
88 // Return the value to use for a dynamic which requires special
89 // treatment.
90 uint64_t
91 do_dynsym_value(const Symbol*) const;
93 // Relocate a section.
94 void
95 relocate_section(const Relocate_info<64, false>*,
96 unsigned int sh_type,
97 const unsigned char* prelocs,
98 size_t reloc_count,
99 Output_section* output_section,
100 bool needs_special_offset_handling,
101 unsigned char* view,
102 elfcpp::Elf_types<64>::Elf_Addr view_address,
103 section_size_type view_size);
105 // Scan the relocs during a relocatable link.
106 void
107 scan_relocatable_relocs(const General_options& options,
108 Symbol_table* symtab,
109 Layout* layout,
110 Sized_relobj<64, false>* object,
111 unsigned int data_shndx,
112 unsigned int sh_type,
113 const unsigned char* prelocs,
114 size_t reloc_count,
115 Output_section* output_section,
116 bool needs_special_offset_handling,
117 size_t local_symbol_count,
118 const unsigned char* plocal_symbols,
119 Relocatable_relocs*);
121 // Relocate a section during a relocatable link.
122 void
123 relocate_for_relocatable(const Relocate_info<64, false>*,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
126 size_t reloc_count,
127 Output_section* output_section,
128 off_t offset_in_output_section,
129 const Relocatable_relocs*,
130 unsigned char* view,
131 elfcpp::Elf_types<64>::Elf_Addr view_address,
132 section_size_type view_size,
133 unsigned char* reloc_view,
134 section_size_type reloc_view_size);
136 // Return a string used to fill a code section with nops.
137 std::string
138 do_code_fill(section_size_type length) const;
140 // Return whether SYM is defined by the ABI.
141 bool
142 do_is_defined_by_abi(Symbol* sym) const
143 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
145 // Return the size of the GOT section.
146 section_size_type
147 got_size()
149 gold_assert(this->got_ != NULL);
150 return this->got_->data_size();
153 private:
154 // The class which scans relocations.
155 class Scan
157 public:
158 Scan()
159 : issued_non_pic_error_(false)
162 inline void
163 local(const General_options& options, Symbol_table* symtab,
164 Layout* layout, Target_x86_64* target,
165 Sized_relobj<64, false>* object,
166 unsigned int data_shndx,
167 Output_section* output_section,
168 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
169 const elfcpp::Sym<64, false>& lsym);
171 inline void
172 global(const General_options& options, Symbol_table* symtab,
173 Layout* layout, Target_x86_64* target,
174 Sized_relobj<64, false>* object,
175 unsigned int data_shndx,
176 Output_section* output_section,
177 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
178 Symbol* gsym);
180 private:
181 static void
182 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
184 static void
185 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
186 Symbol*);
188 void
189 check_non_pic(Relobj*, unsigned int r_type);
191 // Whether we have issued an error about a non-PIC compilation.
192 bool issued_non_pic_error_;
195 // The class which implements relocation.
196 class Relocate
198 public:
199 Relocate()
200 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
203 ~Relocate()
205 if (this->skip_call_tls_get_addr_)
207 // FIXME: This needs to specify the location somehow.
208 gold_error(_("missing expected TLS relocation"));
212 // Do a relocation. Return false if the caller should not issue
213 // any warnings about this relocation.
214 inline bool
215 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
216 const elfcpp::Rela<64, false>&,
217 unsigned int r_type, const Sized_symbol<64>*,
218 const Symbol_value<64>*,
219 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
220 section_size_type);
222 private:
223 // Do a TLS relocation.
224 inline void
225 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
226 size_t relnum, const elfcpp::Rela<64, false>&,
227 unsigned int r_type, const Sized_symbol<64>*,
228 const Symbol_value<64>*,
229 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
230 section_size_type);
232 // Do a TLS General-Dynamic to Initial-Exec transition.
233 inline void
234 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
235 Output_segment* tls_segment,
236 const elfcpp::Rela<64, false>&, unsigned int r_type,
237 elfcpp::Elf_types<64>::Elf_Addr value,
238 unsigned char* view,
239 elfcpp::Elf_types<64>::Elf_Addr,
240 section_size_type view_size);
242 // Do a TLS General-Dynamic to Local-Exec transition.
243 inline void
244 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
245 Output_segment* tls_segment,
246 const elfcpp::Rela<64, false>&, unsigned int r_type,
247 elfcpp::Elf_types<64>::Elf_Addr value,
248 unsigned char* view,
249 section_size_type view_size);
251 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
252 inline void
253 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
254 Output_segment* tls_segment,
255 const elfcpp::Rela<64, false>&, unsigned int r_type,
256 elfcpp::Elf_types<64>::Elf_Addr value,
257 unsigned char* view,
258 elfcpp::Elf_types<64>::Elf_Addr,
259 section_size_type view_size);
261 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
262 inline void
263 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
264 Output_segment* tls_segment,
265 const elfcpp::Rela<64, false>&, unsigned int r_type,
266 elfcpp::Elf_types<64>::Elf_Addr value,
267 unsigned char* view,
268 section_size_type view_size);
270 // Do a TLS Local-Dynamic to Local-Exec transition.
271 inline void
272 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
273 Output_segment* tls_segment,
274 const elfcpp::Rela<64, false>&, unsigned int r_type,
275 elfcpp::Elf_types<64>::Elf_Addr value,
276 unsigned char* view,
277 section_size_type view_size);
279 // Do a TLS Initial-Exec to Local-Exec transition.
280 static inline void
281 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
282 Output_segment* tls_segment,
283 const elfcpp::Rela<64, false>&, unsigned int r_type,
284 elfcpp::Elf_types<64>::Elf_Addr value,
285 unsigned char* view,
286 section_size_type view_size);
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_;
292 // This is set if we see a relocation which could load the address
293 // of the TLS block. Whether we see such a relocation determines
294 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
295 // in debugging sections.
296 bool saw_tls_block_reloc_;
299 // A class which returns the size required for a relocation type,
300 // used while scanning relocs during a relocatable link.
301 class Relocatable_size_for_reloc
303 public:
304 unsigned int
305 get_size_for_reloc(unsigned int, Relobj*);
308 // Adjust TLS relocation type based on the options and whether this
309 // is a local symbol.
310 static tls::Tls_optimization
311 optimize_tls_reloc(bool is_final, int r_type);
313 // Get the GOT section, creating it if necessary.
314 Output_data_got<64, false>*
315 got_section(Symbol_table*, Layout*);
317 // Get the GOT PLT section.
318 Output_data_space*
319 got_plt_section() const
321 gold_assert(this->got_plt_ != NULL);
322 return this->got_plt_;
325 // Create the PLT section.
326 void
327 make_plt_section(Symbol_table* symtab, Layout* layout);
329 // Create a PLT entry for a global symbol.
330 void
331 make_plt_entry(Symbol_table*, Layout*, Symbol*);
333 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
334 void
335 define_tls_base_symbol(Symbol_table*, Layout*);
337 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
338 void
339 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
341 // Create a GOT entry for the TLS module index.
342 unsigned int
343 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
344 Sized_relobj<64, false>* object);
346 // Get the PLT section.
347 Output_data_plt_x86_64*
348 plt_section() const
350 gold_assert(this->plt_ != NULL);
351 return this->plt_;
354 // Get the dynamic reloc section, creating it if necessary.
355 Reloc_section*
356 rela_dyn_section(Layout*);
358 // Return true if the symbol may need a COPY relocation.
359 // References from an executable object to non-function symbols
360 // defined in a dynamic object may need a COPY relocation.
361 bool
362 may_need_copy_reloc(Symbol* gsym)
364 return (!parameters->options().shared()
365 && gsym->is_from_dynobj()
366 && gsym->type() != elfcpp::STT_FUNC);
369 // Add a potential copy relocation.
370 void
371 copy_reloc(Symbol_table* symtab, Layout* layout,
372 Sized_relobj<64, false>* object,
373 unsigned int shndx, Output_section* output_section,
374 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
376 this->copy_relocs_.copy_reloc(symtab, layout,
377 symtab->get_sized_symbol<64>(sym),
378 object, shndx, output_section,
379 reloc, this->rela_dyn_section(layout));
382 // Information about this specific target which we pass to the
383 // general Target structure.
384 static const Target::Target_info x86_64_info;
386 enum Got_type
388 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
389 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
390 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
391 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
394 // The GOT section.
395 Output_data_got<64, false>* got_;
396 // The PLT section.
397 Output_data_plt_x86_64* plt_;
398 // The GOT PLT section.
399 Output_data_space* got_plt_;
400 // The dynamic reloc section.
401 Reloc_section* rela_dyn_;
402 // Relocs saved to avoid a COPY reloc.
403 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
404 // Space for variables copied with a COPY reloc.
405 Output_data_space* dynbss_;
406 // Offset of the GOT entry for the TLS module index.
407 unsigned int got_mod_index_offset_;
408 // True if the _TLS_MODULE_BASE_ symbol has been defined.
409 bool tls_base_symbol_defined_;
412 const Target::Target_info Target_x86_64::x86_64_info =
414 64, // size
415 false, // is_big_endian
416 elfcpp::EM_X86_64, // machine_code
417 false, // has_make_symbol
418 false, // has_resolve
419 true, // has_code_fill
420 true, // is_default_stack_executable
421 '\0', // wrap_char
422 "/lib/ld64.so.1", // program interpreter
423 0x400000, // default_text_segment_address
424 0x1000, // abi_pagesize (overridable by -z max-page-size)
425 0x1000 // common_pagesize (overridable by -z common-page-size)
428 // Get the GOT section, creating it if necessary.
430 Output_data_got<64, false>*
431 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
433 if (this->got_ == NULL)
435 gold_assert(symtab != NULL && layout != NULL);
437 this->got_ = new Output_data_got<64, false>();
439 Output_section* os;
440 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
441 (elfcpp::SHF_ALLOC
442 | elfcpp::SHF_WRITE),
443 this->got_);
444 os->set_is_relro();
446 // The old GNU linker creates a .got.plt section. We just
447 // create another set of data in the .got section. Note that we
448 // always create a PLT if we create a GOT, although the PLT
449 // might be empty.
450 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
451 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
452 (elfcpp::SHF_ALLOC
453 | elfcpp::SHF_WRITE),
454 this->got_plt_);
455 os->set_is_relro();
457 // The first three entries are reserved.
458 this->got_plt_->set_current_data_size(3 * 8);
460 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
461 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
462 this->got_plt_,
463 0, 0, elfcpp::STT_OBJECT,
464 elfcpp::STB_LOCAL,
465 elfcpp::STV_HIDDEN, 0,
466 false, false);
469 return this->got_;
472 // Get the dynamic reloc section, creating it if necessary.
474 Target_x86_64::Reloc_section*
475 Target_x86_64::rela_dyn_section(Layout* layout)
477 if (this->rela_dyn_ == NULL)
479 gold_assert(layout != NULL);
480 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
481 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
482 elfcpp::SHF_ALLOC, this->rela_dyn_);
484 return this->rela_dyn_;
487 // A class to handle the PLT data.
489 class Output_data_plt_x86_64 : public Output_section_data
491 public:
492 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
494 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
495 Output_data_space*);
497 // Add an entry to the PLT.
498 void
499 add_entry(Symbol* gsym);
501 // Add the reserved TLSDESC_PLT entry to the PLT.
502 void
503 reserve_tlsdesc_entry(unsigned int got_offset)
504 { this->tlsdesc_got_offset_ = got_offset; }
506 // Return true if a TLSDESC_PLT entry has been reserved.
507 bool
508 has_tlsdesc_entry() const
509 { return this->tlsdesc_got_offset_ != -1U; }
511 // Return the GOT offset for the reserved TLSDESC_PLT entry.
512 unsigned int
513 get_tlsdesc_got_offset() const
514 { return this->tlsdesc_got_offset_; }
516 // Return the offset of the reserved TLSDESC_PLT entry.
517 unsigned int
518 get_tlsdesc_plt_offset() const
519 { return (this->count_ + 1) * plt_entry_size; }
521 // Return the .rel.plt section data.
522 const Reloc_section*
523 rel_plt() const
524 { return this->rel_; }
526 protected:
527 void
528 do_adjust_output_section(Output_section* os);
530 // Write to a map file.
531 void
532 do_print_to_mapfile(Mapfile* mapfile) const
533 { mapfile->print_output_data(this, _("** PLT")); }
535 private:
536 // The size of an entry in the PLT.
537 static const int plt_entry_size = 16;
539 // The first entry in the PLT.
540 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
541 // procedure linkage table for both programs and shared objects."
542 static unsigned char first_plt_entry[plt_entry_size];
544 // Other entries in the PLT for an executable.
545 static unsigned char plt_entry[plt_entry_size];
547 // The reserved TLSDESC entry in the PLT for an executable.
548 static unsigned char tlsdesc_plt_entry[plt_entry_size];
550 // Set the final size.
551 void
552 set_final_data_size();
554 // Write out the PLT data.
555 void
556 do_write(Output_file*);
558 // The reloc section.
559 Reloc_section* rel_;
560 // The .got section.
561 Output_data_got<64, false>* got_;
562 // The .got.plt section.
563 Output_data_space* got_plt_;
564 // The number of PLT entries.
565 unsigned int count_;
566 // Offset of the reserved TLSDESC_GOT entry when needed.
567 unsigned int tlsdesc_got_offset_;
570 // Create the PLT section. The ordinary .got section is an argument,
571 // since we need to refer to the start. We also create our own .got
572 // section just for PLT entries.
574 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
575 Output_data_got<64, false>* got,
576 Output_data_space* got_plt)
577 : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0),
578 tlsdesc_got_offset_(-1U)
580 this->rel_ = new Reloc_section(false);
581 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
582 elfcpp::SHF_ALLOC, this->rel_);
585 void
586 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
588 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
589 // linker, and so do we.
590 os->set_entsize(4);
593 // Add an entry to the PLT.
595 void
596 Output_data_plt_x86_64::add_entry(Symbol* gsym)
598 gold_assert(!gsym->has_plt_offset());
600 // Note that when setting the PLT offset we skip the initial
601 // reserved PLT entry.
602 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
604 ++this->count_;
606 section_offset_type got_offset = this->got_plt_->current_data_size();
608 // Every PLT entry needs a GOT entry which points back to the PLT
609 // entry (this will be changed by the dynamic linker, normally
610 // lazily when the function is called).
611 this->got_plt_->set_current_data_size(got_offset + 8);
613 // Every PLT entry needs a reloc.
614 gsym->set_needs_dynsym_entry();
615 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
616 got_offset, 0);
618 // Note that we don't need to save the symbol. The contents of the
619 // PLT are independent of which symbols are used. The symbols only
620 // appear in the relocations.
623 // Set the final size.
624 void
625 Output_data_plt_x86_64::set_final_data_size()
627 unsigned int count = this->count_;
628 if (this->has_tlsdesc_entry())
629 ++count;
630 this->set_data_size((count + 1) * plt_entry_size);
633 // The first entry in the PLT for an executable.
635 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
637 // From AMD64 ABI Draft 0.98, page 76
638 0xff, 0x35, // pushq contents of memory address
639 0, 0, 0, 0, // replaced with address of .got + 8
640 0xff, 0x25, // jmp indirect
641 0, 0, 0, 0, // replaced with address of .got + 16
642 0x90, 0x90, 0x90, 0x90 // noop (x4)
645 // Subsequent entries in the PLT for an executable.
647 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
649 // From AMD64 ABI Draft 0.98, page 76
650 0xff, 0x25, // jmpq indirect
651 0, 0, 0, 0, // replaced with address of symbol in .got
652 0x68, // pushq immediate
653 0, 0, 0, 0, // replaced with offset into relocation table
654 0xe9, // jmpq relative
655 0, 0, 0, 0 // replaced with offset to start of .plt
658 // The reserved TLSDESC entry in the PLT for an executable.
660 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
662 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
663 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
664 0xff, 0x35, // pushq x(%rip)
665 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
666 0xff, 0x25, // jmpq *y(%rip)
667 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
668 0x0f, 0x1f, // nop
669 0x40, 0
672 // Write out the PLT. This uses the hand-coded instructions above,
673 // and adjusts them as needed. This is specified by the AMD64 ABI.
675 void
676 Output_data_plt_x86_64::do_write(Output_file* of)
678 const off_t offset = this->offset();
679 const section_size_type oview_size =
680 convert_to_section_size_type(this->data_size());
681 unsigned char* const oview = of->get_output_view(offset, oview_size);
683 const off_t got_file_offset = this->got_plt_->offset();
684 const section_size_type got_size =
685 convert_to_section_size_type(this->got_plt_->data_size());
686 unsigned char* const got_view = of->get_output_view(got_file_offset,
687 got_size);
689 unsigned char* pov = oview;
691 // The base address of the .plt section.
692 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
693 // The base address of the .got section.
694 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
695 // The base address of the PLT portion of the .got section,
696 // which is where the GOT pointer will point, and where the
697 // three reserved GOT entries are located.
698 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
700 memcpy(pov, first_plt_entry, plt_entry_size);
701 // We do a jmp relative to the PC at the end of this instruction.
702 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
703 (got_address + 8
704 - (plt_address + 6)));
705 elfcpp::Swap<32, false>::writeval(pov + 8,
706 (got_address + 16
707 - (plt_address + 12)));
708 pov += plt_entry_size;
710 unsigned char* got_pov = got_view;
712 memset(got_pov, 0, 24);
713 got_pov += 24;
715 unsigned int plt_offset = plt_entry_size;
716 unsigned int got_offset = 24;
717 const unsigned int count = this->count_;
718 for (unsigned int plt_index = 0;
719 plt_index < count;
720 ++plt_index,
721 pov += plt_entry_size,
722 got_pov += 8,
723 plt_offset += plt_entry_size,
724 got_offset += 8)
726 // Set and adjust the PLT entry itself.
727 memcpy(pov, plt_entry, plt_entry_size);
728 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
729 (got_address + got_offset
730 - (plt_address + plt_offset
731 + 6)));
733 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
734 elfcpp::Swap<32, false>::writeval(pov + 12,
735 - (plt_offset + plt_entry_size));
737 // Set the entry in the GOT.
738 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
741 if (this->has_tlsdesc_entry())
743 // Set and adjust the reserved TLSDESC PLT entry.
744 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
745 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
746 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
747 (got_address + 8
748 - (plt_address + plt_offset
749 + 6)));
750 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
751 (got_base
752 + tlsdesc_got_offset
753 - (plt_address + plt_offset
754 + 12)));
755 pov += plt_entry_size;
758 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
759 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
761 of->write_output_view(offset, oview_size, oview);
762 of->write_output_view(got_file_offset, got_size, got_view);
765 // Create the PLT section.
767 void
768 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
770 if (this->plt_ == NULL)
772 // Create the GOT sections first.
773 this->got_section(symtab, layout);
775 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
776 this->got_plt_);
777 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
778 (elfcpp::SHF_ALLOC
779 | elfcpp::SHF_EXECINSTR),
780 this->plt_);
784 // Create a PLT entry for a global symbol.
786 void
787 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
788 Symbol* gsym)
790 if (gsym->has_plt_offset())
791 return;
793 if (this->plt_ == NULL)
794 this->make_plt_section(symtab, layout);
796 this->plt_->add_entry(gsym);
799 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
801 void
802 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
804 if (this->tls_base_symbol_defined_)
805 return;
807 Output_segment* tls_segment = layout->tls_segment();
808 if (tls_segment != NULL)
810 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
811 tls_segment, 0, 0,
812 elfcpp::STT_TLS,
813 elfcpp::STB_LOCAL,
814 elfcpp::STV_HIDDEN, 0,
815 Symbol::SEGMENT_END, true);
817 this->tls_base_symbol_defined_ = true;
820 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
822 void
823 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
824 Layout* layout)
826 if (this->plt_ == NULL)
827 this->make_plt_section(symtab, layout);
829 if (!this->plt_->has_tlsdesc_entry())
831 // Allocate the TLSDESC_GOT entry.
832 Output_data_got<64, false>* got = this->got_section(symtab, layout);
833 unsigned int got_offset = got->add_constant(0);
835 // Allocate the TLSDESC_PLT entry.
836 this->plt_->reserve_tlsdesc_entry(got_offset);
840 // Create a GOT entry for the TLS module index.
842 unsigned int
843 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
844 Sized_relobj<64, false>* object)
846 if (this->got_mod_index_offset_ == -1U)
848 gold_assert(symtab != NULL && layout != NULL && object != NULL);
849 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
850 Output_data_got<64, false>* got = this->got_section(symtab, layout);
851 unsigned int got_offset = got->add_constant(0);
852 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
853 got_offset, 0);
854 got->add_constant(0);
855 this->got_mod_index_offset_ = got_offset;
857 return this->got_mod_index_offset_;
860 // Optimize the TLS relocation type based on what we know about the
861 // symbol. IS_FINAL is true if the final address of this symbol is
862 // known at link time.
864 tls::Tls_optimization
865 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
867 // If we are generating a shared library, then we can't do anything
868 // in the linker.
869 if (parameters->options().shared())
870 return tls::TLSOPT_NONE;
872 switch (r_type)
874 case elfcpp::R_X86_64_TLSGD:
875 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
876 case elfcpp::R_X86_64_TLSDESC_CALL:
877 // These are General-Dynamic which permits fully general TLS
878 // access. Since we know that we are generating an executable,
879 // we can convert this to Initial-Exec. If we also know that
880 // this is a local symbol, we can further switch to Local-Exec.
881 if (is_final)
882 return tls::TLSOPT_TO_LE;
883 return tls::TLSOPT_TO_IE;
885 case elfcpp::R_X86_64_TLSLD:
886 // This is Local-Dynamic, which refers to a local symbol in the
887 // dynamic TLS block. Since we know that we generating an
888 // executable, we can switch to Local-Exec.
889 return tls::TLSOPT_TO_LE;
891 case elfcpp::R_X86_64_DTPOFF32:
892 case elfcpp::R_X86_64_DTPOFF64:
893 // Another Local-Dynamic reloc.
894 return tls::TLSOPT_TO_LE;
896 case elfcpp::R_X86_64_GOTTPOFF:
897 // These are Initial-Exec relocs which get the thread offset
898 // from the GOT. If we know that we are linking against the
899 // local symbol, we can switch to Local-Exec, which links the
900 // thread offset into the instruction.
901 if (is_final)
902 return tls::TLSOPT_TO_LE;
903 return tls::TLSOPT_NONE;
905 case elfcpp::R_X86_64_TPOFF32:
906 // When we already have Local-Exec, there is nothing further we
907 // can do.
908 return tls::TLSOPT_NONE;
910 default:
911 gold_unreachable();
915 // Report an unsupported relocation against a local symbol.
917 void
918 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
919 unsigned int r_type)
921 gold_error(_("%s: unsupported reloc %u against local symbol"),
922 object->name().c_str(), r_type);
925 // We are about to emit a dynamic relocation of type R_TYPE. If the
926 // dynamic linker does not support it, issue an error. The GNU linker
927 // only issues a non-PIC error for an allocated read-only section.
928 // Here we know the section is allocated, but we don't know that it is
929 // read-only. But we check for all the relocation types which the
930 // glibc dynamic linker supports, so it seems appropriate to issue an
931 // error even if the section is not read-only.
933 void
934 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
936 switch (r_type)
938 // These are the relocation types supported by glibc for x86_64.
939 case elfcpp::R_X86_64_RELATIVE:
940 case elfcpp::R_X86_64_GLOB_DAT:
941 case elfcpp::R_X86_64_JUMP_SLOT:
942 case elfcpp::R_X86_64_DTPMOD64:
943 case elfcpp::R_X86_64_DTPOFF64:
944 case elfcpp::R_X86_64_TPOFF64:
945 case elfcpp::R_X86_64_64:
946 case elfcpp::R_X86_64_32:
947 case elfcpp::R_X86_64_PC32:
948 case elfcpp::R_X86_64_COPY:
949 return;
951 default:
952 // This prevents us from issuing more than one error per reloc
953 // section. But we can still wind up issuing more than one
954 // error per object file.
955 if (this->issued_non_pic_error_)
956 return;
957 object->error(_("requires unsupported dynamic reloc; "
958 "recompile with -fPIC"));
959 this->issued_non_pic_error_ = true;
960 return;
962 case elfcpp::R_X86_64_NONE:
963 gold_unreachable();
967 // Scan a relocation for a local symbol.
969 inline void
970 Target_x86_64::Scan::local(const General_options&,
971 Symbol_table* symtab,
972 Layout* layout,
973 Target_x86_64* target,
974 Sized_relobj<64, false>* object,
975 unsigned int data_shndx,
976 Output_section* output_section,
977 const elfcpp::Rela<64, false>& reloc,
978 unsigned int r_type,
979 const elfcpp::Sym<64, false>& lsym)
981 switch (r_type)
983 case elfcpp::R_X86_64_NONE:
984 case elfcpp::R_386_GNU_VTINHERIT:
985 case elfcpp::R_386_GNU_VTENTRY:
986 break;
988 case elfcpp::R_X86_64_64:
989 // If building a shared library (or a position-independent
990 // executable), we need to create a dynamic relocation for this
991 // location. The relocation applied at link time will apply the
992 // link-time value, so we flag the location with an
993 // R_X86_64_RELATIVE relocation so the dynamic loader can
994 // relocate it easily.
995 if (parameters->options().output_is_position_independent())
997 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
998 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
999 rela_dyn->add_local_relative(object, r_sym,
1000 elfcpp::R_X86_64_RELATIVE,
1001 output_section, data_shndx,
1002 reloc.get_r_offset(),
1003 reloc.get_r_addend());
1005 break;
1007 case elfcpp::R_X86_64_32:
1008 case elfcpp::R_X86_64_32S:
1009 case elfcpp::R_X86_64_16:
1010 case elfcpp::R_X86_64_8:
1011 // If building a shared library (or a position-independent
1012 // executable), we need to create a dynamic relocation for this
1013 // location. We can't use an R_X86_64_RELATIVE relocation
1014 // because that is always a 64-bit relocation.
1015 if (parameters->options().output_is_position_independent())
1017 this->check_non_pic(object, r_type);
1019 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1020 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1021 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1022 rela_dyn->add_local(object, r_sym, r_type, output_section,
1023 data_shndx, reloc.get_r_offset(),
1024 reloc.get_r_addend());
1025 else
1027 gold_assert(lsym.get_st_value() == 0);
1028 unsigned int shndx = lsym.get_st_shndx();
1029 bool is_ordinary;
1030 shndx = object->adjust_sym_shndx(r_sym, shndx,
1031 &is_ordinary);
1032 if (!is_ordinary)
1033 object->error(_("section symbol %u has bad shndx %u"),
1034 r_sym, shndx);
1035 else
1036 rela_dyn->add_local_section(object, shndx,
1037 r_type, output_section,
1038 data_shndx, reloc.get_r_offset(),
1039 reloc.get_r_addend());
1042 break;
1044 case elfcpp::R_X86_64_PC64:
1045 case elfcpp::R_X86_64_PC32:
1046 case elfcpp::R_X86_64_PC16:
1047 case elfcpp::R_X86_64_PC8:
1048 break;
1050 case elfcpp::R_X86_64_PLT32:
1051 // Since we know this is a local symbol, we can handle this as a
1052 // PC32 reloc.
1053 break;
1055 case elfcpp::R_X86_64_GOTPC32:
1056 case elfcpp::R_X86_64_GOTOFF64:
1057 case elfcpp::R_X86_64_GOTPC64:
1058 case elfcpp::R_X86_64_PLTOFF64:
1059 // We need a GOT section.
1060 target->got_section(symtab, layout);
1061 // For PLTOFF64, we'd normally want a PLT section, but since we
1062 // know this is a local symbol, no PLT is needed.
1063 break;
1065 case elfcpp::R_X86_64_GOT64:
1066 case elfcpp::R_X86_64_GOT32:
1067 case elfcpp::R_X86_64_GOTPCREL64:
1068 case elfcpp::R_X86_64_GOTPCREL:
1069 case elfcpp::R_X86_64_GOTPLT64:
1071 // The symbol requires a GOT entry.
1072 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1073 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1074 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1076 // If we are generating a shared object, we need to add a
1077 // dynamic relocation for this symbol's GOT entry.
1078 if (parameters->options().output_is_position_independent())
1080 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1081 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1082 if (r_type != elfcpp::R_X86_64_GOT32)
1083 rela_dyn->add_local_relative(
1084 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1085 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1086 else
1088 this->check_non_pic(object, r_type);
1090 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1091 rela_dyn->add_local(
1092 object, r_sym, r_type, got,
1093 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1097 // For GOTPLT64, we'd normally want a PLT section, but since
1098 // we know this is a local symbol, no PLT is needed.
1100 break;
1102 case elfcpp::R_X86_64_COPY:
1103 case elfcpp::R_X86_64_GLOB_DAT:
1104 case elfcpp::R_X86_64_JUMP_SLOT:
1105 case elfcpp::R_X86_64_RELATIVE:
1106 // These are outstanding tls relocs, which are unexpected when linking
1107 case elfcpp::R_X86_64_TPOFF64:
1108 case elfcpp::R_X86_64_DTPMOD64:
1109 case elfcpp::R_X86_64_TLSDESC:
1110 gold_error(_("%s: unexpected reloc %u in object file"),
1111 object->name().c_str(), r_type);
1112 break;
1114 // These are initial tls relocs, which are expected when linking
1115 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1116 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1117 case elfcpp::R_X86_64_TLSDESC_CALL:
1118 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1119 case elfcpp::R_X86_64_DTPOFF32:
1120 case elfcpp::R_X86_64_DTPOFF64:
1121 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1122 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1124 bool output_is_shared = parameters->options().shared();
1125 const tls::Tls_optimization optimized_type
1126 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1127 switch (r_type)
1129 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1130 if (optimized_type == tls::TLSOPT_NONE)
1132 // Create a pair of GOT entries for the module index and
1133 // dtv-relative offset.
1134 Output_data_got<64, false>* got
1135 = target->got_section(symtab, layout);
1136 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1137 unsigned int shndx = lsym.get_st_shndx();
1138 bool is_ordinary;
1139 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1140 if (!is_ordinary)
1141 object->error(_("local symbol %u has bad shndx %u"),
1142 r_sym, shndx);
1143 else
1144 got->add_local_pair_with_rela(object, r_sym,
1145 shndx,
1146 GOT_TYPE_TLS_PAIR,
1147 target->rela_dyn_section(layout),
1148 elfcpp::R_X86_64_DTPMOD64, 0);
1150 else if (optimized_type != tls::TLSOPT_TO_LE)
1151 unsupported_reloc_local(object, r_type);
1152 break;
1154 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1155 target->define_tls_base_symbol(symtab, layout);
1156 if (optimized_type == tls::TLSOPT_NONE)
1158 // Create reserved PLT and GOT entries for the resolver.
1159 target->reserve_tlsdesc_entries(symtab, layout);
1161 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1162 Output_data_got<64, false>* got
1163 = target->got_section(symtab, layout);
1164 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1165 unsigned int shndx = lsym.get_st_shndx();
1166 bool is_ordinary;
1167 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1168 if (!is_ordinary)
1169 object->error(_("local symbol %u has bad shndx %u"),
1170 r_sym, shndx);
1171 else
1172 got->add_local_pair_with_rela(object, r_sym,
1173 shndx,
1174 GOT_TYPE_TLS_DESC,
1175 target->rela_dyn_section(layout),
1176 elfcpp::R_X86_64_TLSDESC, 0);
1178 else if (optimized_type != tls::TLSOPT_TO_LE)
1179 unsupported_reloc_local(object, r_type);
1180 break;
1182 case elfcpp::R_X86_64_TLSDESC_CALL:
1183 break;
1185 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1186 if (optimized_type == tls::TLSOPT_NONE)
1188 // Create a GOT entry for the module index.
1189 target->got_mod_index_entry(symtab, layout, object);
1191 else if (optimized_type != tls::TLSOPT_TO_LE)
1192 unsupported_reloc_local(object, r_type);
1193 break;
1195 case elfcpp::R_X86_64_DTPOFF32:
1196 case elfcpp::R_X86_64_DTPOFF64:
1197 break;
1199 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1200 layout->set_has_static_tls();
1201 if (optimized_type == tls::TLSOPT_NONE)
1203 // Create a GOT entry for the tp-relative offset.
1204 Output_data_got<64, false>* got
1205 = target->got_section(symtab, layout);
1206 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1207 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1208 target->rela_dyn_section(layout),
1209 elfcpp::R_X86_64_TPOFF64);
1211 else if (optimized_type != tls::TLSOPT_TO_LE)
1212 unsupported_reloc_local(object, r_type);
1213 break;
1215 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1216 layout->set_has_static_tls();
1217 if (output_is_shared)
1218 unsupported_reloc_local(object, r_type);
1219 break;
1221 default:
1222 gold_unreachable();
1225 break;
1227 case elfcpp::R_X86_64_SIZE32:
1228 case elfcpp::R_X86_64_SIZE64:
1229 default:
1230 gold_error(_("%s: unsupported reloc %u against local symbol"),
1231 object->name().c_str(), r_type);
1232 break;
1237 // Report an unsupported relocation against a global symbol.
1239 void
1240 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1241 unsigned int r_type,
1242 Symbol* gsym)
1244 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1245 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1248 // Scan a relocation for a global symbol.
1250 inline void
1251 Target_x86_64::Scan::global(const General_options&,
1252 Symbol_table* symtab,
1253 Layout* layout,
1254 Target_x86_64* target,
1255 Sized_relobj<64, false>* object,
1256 unsigned int data_shndx,
1257 Output_section* output_section,
1258 const elfcpp::Rela<64, false>& reloc,
1259 unsigned int r_type,
1260 Symbol* gsym)
1262 switch (r_type)
1264 case elfcpp::R_X86_64_NONE:
1265 case elfcpp::R_386_GNU_VTINHERIT:
1266 case elfcpp::R_386_GNU_VTENTRY:
1267 break;
1269 case elfcpp::R_X86_64_64:
1270 case elfcpp::R_X86_64_32:
1271 case elfcpp::R_X86_64_32S:
1272 case elfcpp::R_X86_64_16:
1273 case elfcpp::R_X86_64_8:
1275 // Make a PLT entry if necessary.
1276 if (gsym->needs_plt_entry())
1278 target->make_plt_entry(symtab, layout, gsym);
1279 // Since this is not a PC-relative relocation, we may be
1280 // taking the address of a function. In that case we need to
1281 // set the entry in the dynamic symbol table to the address of
1282 // the PLT entry.
1283 if (gsym->is_from_dynobj() && !parameters->options().shared())
1284 gsym->set_needs_dynsym_value();
1286 // Make a dynamic relocation if necessary.
1287 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1289 if (target->may_need_copy_reloc(gsym))
1291 target->copy_reloc(symtab, layout, object,
1292 data_shndx, output_section, gsym, reloc);
1294 else if (r_type == elfcpp::R_X86_64_64
1295 && gsym->can_use_relative_reloc(false))
1297 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1298 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1299 output_section, object,
1300 data_shndx, reloc.get_r_offset(),
1301 reloc.get_r_addend());
1303 else
1305 this->check_non_pic(object, r_type);
1306 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1307 rela_dyn->add_global(gsym, r_type, output_section, object,
1308 data_shndx, reloc.get_r_offset(),
1309 reloc.get_r_addend());
1313 break;
1315 case elfcpp::R_X86_64_PC64:
1316 case elfcpp::R_X86_64_PC32:
1317 case elfcpp::R_X86_64_PC16:
1318 case elfcpp::R_X86_64_PC8:
1320 // Make a PLT entry if necessary.
1321 if (gsym->needs_plt_entry())
1322 target->make_plt_entry(symtab, layout, gsym);
1323 // Make a dynamic relocation if necessary.
1324 int flags = Symbol::NON_PIC_REF;
1325 if (gsym->type() == elfcpp::STT_FUNC)
1326 flags |= Symbol::FUNCTION_CALL;
1327 if (gsym->needs_dynamic_reloc(flags))
1329 if (target->may_need_copy_reloc(gsym))
1331 target->copy_reloc(symtab, layout, object,
1332 data_shndx, output_section, gsym, reloc);
1334 else
1336 this->check_non_pic(object, r_type);
1337 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1338 rela_dyn->add_global(gsym, r_type, output_section, object,
1339 data_shndx, reloc.get_r_offset(),
1340 reloc.get_r_addend());
1344 break;
1346 case elfcpp::R_X86_64_GOT64:
1347 case elfcpp::R_X86_64_GOT32:
1348 case elfcpp::R_X86_64_GOTPCREL64:
1349 case elfcpp::R_X86_64_GOTPCREL:
1350 case elfcpp::R_X86_64_GOTPLT64:
1352 // The symbol requires a GOT entry.
1353 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1354 if (gsym->final_value_is_known())
1355 got->add_global(gsym, GOT_TYPE_STANDARD);
1356 else
1358 // If this symbol is not fully resolved, we need to add a
1359 // dynamic relocation for it.
1360 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1361 if (gsym->is_from_dynobj()
1362 || gsym->is_undefined()
1363 || gsym->is_preemptible())
1364 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1365 elfcpp::R_X86_64_GLOB_DAT);
1366 else
1368 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1369 rela_dyn->add_global_relative(
1370 gsym, elfcpp::R_X86_64_RELATIVE, got,
1371 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1374 // For GOTPLT64, we also need a PLT entry (but only if the
1375 // symbol is not fully resolved).
1376 if (r_type == elfcpp::R_X86_64_GOTPLT64
1377 && !gsym->final_value_is_known())
1378 target->make_plt_entry(symtab, layout, gsym);
1380 break;
1382 case elfcpp::R_X86_64_PLT32:
1383 // If the symbol is fully resolved, this is just a PC32 reloc.
1384 // Otherwise we need a PLT entry.
1385 if (gsym->final_value_is_known())
1386 break;
1387 // If building a shared library, we can also skip the PLT entry
1388 // if the symbol is defined in the output file and is protected
1389 // or hidden.
1390 if (gsym->is_defined()
1391 && !gsym->is_from_dynobj()
1392 && !gsym->is_preemptible())
1393 break;
1394 target->make_plt_entry(symtab, layout, gsym);
1395 break;
1397 case elfcpp::R_X86_64_GOTPC32:
1398 case elfcpp::R_X86_64_GOTOFF64:
1399 case elfcpp::R_X86_64_GOTPC64:
1400 case elfcpp::R_X86_64_PLTOFF64:
1401 // We need a GOT section.
1402 target->got_section(symtab, layout);
1403 // For PLTOFF64, we also need a PLT entry (but only if the
1404 // symbol is not fully resolved).
1405 if (r_type == elfcpp::R_X86_64_PLTOFF64
1406 && !gsym->final_value_is_known())
1407 target->make_plt_entry(symtab, layout, gsym);
1408 break;
1410 case elfcpp::R_X86_64_COPY:
1411 case elfcpp::R_X86_64_GLOB_DAT:
1412 case elfcpp::R_X86_64_JUMP_SLOT:
1413 case elfcpp::R_X86_64_RELATIVE:
1414 // These are outstanding tls relocs, which are unexpected when linking
1415 case elfcpp::R_X86_64_TPOFF64:
1416 case elfcpp::R_X86_64_DTPMOD64:
1417 case elfcpp::R_X86_64_TLSDESC:
1418 gold_error(_("%s: unexpected reloc %u in object file"),
1419 object->name().c_str(), r_type);
1420 break;
1422 // These are initial tls relocs, which are expected for global()
1423 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1424 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1425 case elfcpp::R_X86_64_TLSDESC_CALL:
1426 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1427 case elfcpp::R_X86_64_DTPOFF32:
1428 case elfcpp::R_X86_64_DTPOFF64:
1429 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1430 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1432 const bool is_final = gsym->final_value_is_known();
1433 const tls::Tls_optimization optimized_type
1434 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1435 switch (r_type)
1437 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1438 if (optimized_type == tls::TLSOPT_NONE)
1440 // Create a pair of GOT entries for the module index and
1441 // dtv-relative offset.
1442 Output_data_got<64, false>* got
1443 = target->got_section(symtab, layout);
1444 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1445 target->rela_dyn_section(layout),
1446 elfcpp::R_X86_64_DTPMOD64,
1447 elfcpp::R_X86_64_DTPOFF64);
1449 else if (optimized_type == tls::TLSOPT_TO_IE)
1451 // Create a GOT entry for the tp-relative offset.
1452 Output_data_got<64, false>* got
1453 = target->got_section(symtab, layout);
1454 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1455 target->rela_dyn_section(layout),
1456 elfcpp::R_X86_64_TPOFF64);
1458 else if (optimized_type != tls::TLSOPT_TO_LE)
1459 unsupported_reloc_global(object, r_type, gsym);
1460 break;
1462 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1463 target->define_tls_base_symbol(symtab, layout);
1464 if (optimized_type == tls::TLSOPT_NONE)
1466 // Create reserved PLT and GOT entries for the resolver.
1467 target->reserve_tlsdesc_entries(symtab, layout);
1469 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1470 Output_data_got<64, false>* got
1471 = target->got_section(symtab, layout);
1472 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1473 target->rela_dyn_section(layout),
1474 elfcpp::R_X86_64_TLSDESC, 0);
1476 else if (optimized_type == tls::TLSOPT_TO_IE)
1478 // Create a GOT entry for the tp-relative offset.
1479 Output_data_got<64, false>* got
1480 = target->got_section(symtab, layout);
1481 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1482 target->rela_dyn_section(layout),
1483 elfcpp::R_X86_64_TPOFF64);
1485 else if (optimized_type != tls::TLSOPT_TO_LE)
1486 unsupported_reloc_global(object, r_type, gsym);
1487 break;
1489 case elfcpp::R_X86_64_TLSDESC_CALL:
1490 break;
1492 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1493 if (optimized_type == tls::TLSOPT_NONE)
1495 // Create a GOT entry for the module index.
1496 target->got_mod_index_entry(symtab, layout, object);
1498 else if (optimized_type != tls::TLSOPT_TO_LE)
1499 unsupported_reloc_global(object, r_type, gsym);
1500 break;
1502 case elfcpp::R_X86_64_DTPOFF32:
1503 case elfcpp::R_X86_64_DTPOFF64:
1504 break;
1506 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1507 layout->set_has_static_tls();
1508 if (optimized_type == tls::TLSOPT_NONE)
1510 // Create a GOT entry for the tp-relative offset.
1511 Output_data_got<64, false>* got
1512 = target->got_section(symtab, layout);
1513 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1514 target->rela_dyn_section(layout),
1515 elfcpp::R_X86_64_TPOFF64);
1517 else if (optimized_type != tls::TLSOPT_TO_LE)
1518 unsupported_reloc_global(object, r_type, gsym);
1519 break;
1521 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1522 layout->set_has_static_tls();
1523 if (parameters->options().shared())
1524 unsupported_reloc_local(object, r_type);
1525 break;
1527 default:
1528 gold_unreachable();
1531 break;
1533 case elfcpp::R_X86_64_SIZE32:
1534 case elfcpp::R_X86_64_SIZE64:
1535 default:
1536 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1537 object->name().c_str(), r_type,
1538 gsym->demangled_name().c_str());
1539 break;
1543 // Scan relocations for a section.
1545 void
1546 Target_x86_64::scan_relocs(const General_options& options,
1547 Symbol_table* symtab,
1548 Layout* layout,
1549 Sized_relobj<64, false>* object,
1550 unsigned int data_shndx,
1551 unsigned int sh_type,
1552 const unsigned char* prelocs,
1553 size_t reloc_count,
1554 Output_section* output_section,
1555 bool needs_special_offset_handling,
1556 size_t local_symbol_count,
1557 const unsigned char* plocal_symbols)
1559 if (sh_type == elfcpp::SHT_REL)
1561 gold_error(_("%s: unsupported REL reloc section"),
1562 object->name().c_str());
1563 return;
1566 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1567 Target_x86_64::Scan>(
1568 options,
1569 symtab,
1570 layout,
1571 this,
1572 object,
1573 data_shndx,
1574 prelocs,
1575 reloc_count,
1576 output_section,
1577 needs_special_offset_handling,
1578 local_symbol_count,
1579 plocal_symbols);
1582 // Finalize the sections.
1584 void
1585 Target_x86_64::do_finalize_sections(Layout* layout)
1587 // Fill in some more dynamic tags.
1588 Output_data_dynamic* const odyn = layout->dynamic_data();
1589 if (odyn != NULL)
1591 if (this->got_plt_ != NULL)
1592 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1594 if (this->plt_ != NULL)
1596 const Output_data* od = this->plt_->rel_plt();
1597 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1598 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1599 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1600 if (this->plt_->has_tlsdesc_entry())
1602 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1603 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1604 this->got_->finalize_data_size();
1605 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1606 this->plt_, plt_offset);
1607 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1608 this->got_, got_offset);
1612 if (this->rela_dyn_ != NULL)
1614 const Output_data* od = this->rela_dyn_;
1615 odyn->add_section_address(elfcpp::DT_RELA, od);
1616 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1617 odyn->add_constant(elfcpp::DT_RELAENT,
1618 elfcpp::Elf_sizes<64>::rela_size);
1621 if (!parameters->options().shared())
1623 // The value of the DT_DEBUG tag is filled in by the dynamic
1624 // linker at run time, and used by the debugger.
1625 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1629 // Emit any relocs we saved in an attempt to avoid generating COPY
1630 // relocs.
1631 if (this->copy_relocs_.any_saved_relocs())
1632 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1635 // Perform a relocation.
1637 inline bool
1638 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1639 Target_x86_64* target,
1640 size_t relnum,
1641 const elfcpp::Rela<64, false>& rela,
1642 unsigned int r_type,
1643 const Sized_symbol<64>* gsym,
1644 const Symbol_value<64>* psymval,
1645 unsigned char* view,
1646 elfcpp::Elf_types<64>::Elf_Addr address,
1647 section_size_type view_size)
1649 if (this->skip_call_tls_get_addr_)
1651 if (r_type != elfcpp::R_X86_64_PLT32
1652 || gsym == NULL
1653 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1655 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1656 _("missing expected TLS relocation"));
1658 else
1660 this->skip_call_tls_get_addr_ = false;
1661 return false;
1665 // Pick the value to use for symbols defined in shared objects.
1666 Symbol_value<64> symval;
1667 if (gsym != NULL
1668 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1669 || r_type == elfcpp::R_X86_64_PC32
1670 || r_type == elfcpp::R_X86_64_PC16
1671 || r_type == elfcpp::R_X86_64_PC8))
1673 symval.set_output_value(target->plt_section()->address()
1674 + gsym->plt_offset());
1675 psymval = &symval;
1678 const Sized_relobj<64, false>* object = relinfo->object;
1679 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1681 // Get the GOT offset if needed.
1682 // The GOT pointer points to the end of the GOT section.
1683 // We need to subtract the size of the GOT section to get
1684 // the actual offset to use in the relocation.
1685 bool have_got_offset = false;
1686 unsigned int got_offset = 0;
1687 switch (r_type)
1689 case elfcpp::R_X86_64_GOT32:
1690 case elfcpp::R_X86_64_GOT64:
1691 case elfcpp::R_X86_64_GOTPLT64:
1692 case elfcpp::R_X86_64_GOTPCREL:
1693 case elfcpp::R_X86_64_GOTPCREL64:
1694 if (gsym != NULL)
1696 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1697 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1699 else
1701 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1702 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1703 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1704 - target->got_size());
1706 have_got_offset = true;
1707 break;
1709 default:
1710 break;
1713 switch (r_type)
1715 case elfcpp::R_X86_64_NONE:
1716 case elfcpp::R_386_GNU_VTINHERIT:
1717 case elfcpp::R_386_GNU_VTENTRY:
1718 break;
1720 case elfcpp::R_X86_64_64:
1721 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1722 break;
1724 case elfcpp::R_X86_64_PC64:
1725 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1726 address);
1727 break;
1729 case elfcpp::R_X86_64_32:
1730 // FIXME: we need to verify that value + addend fits into 32 bits:
1731 // uint64_t x = value + addend;
1732 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1733 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1734 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1735 break;
1737 case elfcpp::R_X86_64_32S:
1738 // FIXME: we need to verify that value + addend fits into 32 bits:
1739 // int64_t x = value + addend; // note this quantity is signed!
1740 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1741 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1742 break;
1744 case elfcpp::R_X86_64_PC32:
1745 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1746 address);
1747 break;
1749 case elfcpp::R_X86_64_16:
1750 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1751 break;
1753 case elfcpp::R_X86_64_PC16:
1754 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1755 address);
1756 break;
1758 case elfcpp::R_X86_64_8:
1759 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1760 break;
1762 case elfcpp::R_X86_64_PC8:
1763 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1764 address);
1765 break;
1767 case elfcpp::R_X86_64_PLT32:
1768 gold_assert(gsym == NULL
1769 || gsym->has_plt_offset()
1770 || gsym->final_value_is_known()
1771 || (gsym->is_defined()
1772 && !gsym->is_from_dynobj()
1773 && !gsym->is_preemptible()));
1774 // Note: while this code looks the same as for R_X86_64_PC32, it
1775 // behaves differently because psymval was set to point to
1776 // the PLT entry, rather than the symbol, in Scan::global().
1777 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1778 address);
1779 break;
1781 case elfcpp::R_X86_64_PLTOFF64:
1783 gold_assert(gsym);
1784 gold_assert(gsym->has_plt_offset()
1785 || gsym->final_value_is_known());
1786 elfcpp::Elf_types<64>::Elf_Addr got_address;
1787 got_address = target->got_section(NULL, NULL)->address();
1788 Relocate_functions<64, false>::rela64(view, object, psymval,
1789 addend - got_address);
1792 case elfcpp::R_X86_64_GOT32:
1793 gold_assert(have_got_offset);
1794 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1795 break;
1797 case elfcpp::R_X86_64_GOTPC32:
1799 gold_assert(gsym);
1800 elfcpp::Elf_types<64>::Elf_Addr value;
1801 value = target->got_plt_section()->address();
1802 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1804 break;
1806 case elfcpp::R_X86_64_GOT64:
1807 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1808 // Since we always add a PLT entry, this is equivalent.
1809 case elfcpp::R_X86_64_GOTPLT64:
1810 gold_assert(have_got_offset);
1811 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1812 break;
1814 case elfcpp::R_X86_64_GOTPC64:
1816 gold_assert(gsym);
1817 elfcpp::Elf_types<64>::Elf_Addr value;
1818 value = target->got_plt_section()->address();
1819 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1821 break;
1823 case elfcpp::R_X86_64_GOTOFF64:
1825 elfcpp::Elf_types<64>::Elf_Addr value;
1826 value = (psymval->value(object, 0)
1827 - target->got_plt_section()->address());
1828 Relocate_functions<64, false>::rela64(view, value, addend);
1830 break;
1832 case elfcpp::R_X86_64_GOTPCREL:
1834 gold_assert(have_got_offset);
1835 elfcpp::Elf_types<64>::Elf_Addr value;
1836 value = target->got_plt_section()->address() + got_offset;
1837 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1839 break;
1841 case elfcpp::R_X86_64_GOTPCREL64:
1843 gold_assert(have_got_offset);
1844 elfcpp::Elf_types<64>::Elf_Addr value;
1845 value = target->got_plt_section()->address() + got_offset;
1846 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1848 break;
1850 case elfcpp::R_X86_64_COPY:
1851 case elfcpp::R_X86_64_GLOB_DAT:
1852 case elfcpp::R_X86_64_JUMP_SLOT:
1853 case elfcpp::R_X86_64_RELATIVE:
1854 // These are outstanding tls relocs, which are unexpected when linking
1855 case elfcpp::R_X86_64_TPOFF64:
1856 case elfcpp::R_X86_64_DTPMOD64:
1857 case elfcpp::R_X86_64_TLSDESC:
1858 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1859 _("unexpected reloc %u in object file"),
1860 r_type);
1861 break;
1863 // These are initial tls relocs, which are expected when linking
1864 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1865 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1866 case elfcpp::R_X86_64_TLSDESC_CALL:
1867 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1868 case elfcpp::R_X86_64_DTPOFF32:
1869 case elfcpp::R_X86_64_DTPOFF64:
1870 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1871 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1872 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1873 view, address, view_size);
1874 break;
1876 case elfcpp::R_X86_64_SIZE32:
1877 case elfcpp::R_X86_64_SIZE64:
1878 default:
1879 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1880 _("unsupported reloc %u"),
1881 r_type);
1882 break;
1885 return true;
1888 // Perform a TLS relocation.
1890 inline void
1891 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1892 Target_x86_64* target,
1893 size_t relnum,
1894 const elfcpp::Rela<64, false>& rela,
1895 unsigned int r_type,
1896 const Sized_symbol<64>* gsym,
1897 const Symbol_value<64>* psymval,
1898 unsigned char* view,
1899 elfcpp::Elf_types<64>::Elf_Addr address,
1900 section_size_type view_size)
1902 Output_segment* tls_segment = relinfo->layout->tls_segment();
1904 const Sized_relobj<64, false>* object = relinfo->object;
1905 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1907 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1909 const bool is_final = (gsym == NULL
1910 ? !parameters->options().output_is_position_independent()
1911 : gsym->final_value_is_known());
1912 const tls::Tls_optimization optimized_type
1913 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1914 switch (r_type)
1916 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1917 this->saw_tls_block_reloc_ = true;
1918 if (optimized_type == tls::TLSOPT_TO_LE)
1920 gold_assert(tls_segment != NULL);
1921 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1922 rela, r_type, value, view,
1923 view_size);
1924 break;
1926 else
1928 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1929 ? GOT_TYPE_TLS_OFFSET
1930 : GOT_TYPE_TLS_PAIR);
1931 unsigned int got_offset;
1932 if (gsym != NULL)
1934 gold_assert(gsym->has_got_offset(got_type));
1935 got_offset = gsym->got_offset(got_type) - target->got_size();
1937 else
1939 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1940 gold_assert(object->local_has_got_offset(r_sym, got_type));
1941 got_offset = (object->local_got_offset(r_sym, got_type)
1942 - target->got_size());
1944 if (optimized_type == tls::TLSOPT_TO_IE)
1946 gold_assert(tls_segment != NULL);
1947 value = target->got_plt_section()->address() + got_offset;
1948 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1949 value, view, address, view_size);
1950 break;
1952 else if (optimized_type == tls::TLSOPT_NONE)
1954 // Relocate the field with the offset of the pair of GOT
1955 // entries.
1956 value = target->got_plt_section()->address() + got_offset;
1957 Relocate_functions<64, false>::pcrela32(view, value, addend,
1958 address);
1959 break;
1962 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1963 _("unsupported reloc %u"), r_type);
1964 break;
1966 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1967 case elfcpp::R_X86_64_TLSDESC_CALL:
1968 this->saw_tls_block_reloc_ = true;
1969 if (optimized_type == tls::TLSOPT_TO_LE)
1971 gold_assert(tls_segment != NULL);
1972 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1973 rela, r_type, value, view,
1974 view_size);
1975 break;
1977 else
1979 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1980 ? GOT_TYPE_TLS_OFFSET
1981 : GOT_TYPE_TLS_DESC);
1982 unsigned int got_offset;
1983 if (gsym != NULL)
1985 gold_assert(gsym->has_got_offset(got_type));
1986 got_offset = gsym->got_offset(got_type) - target->got_size();
1988 else
1990 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1991 gold_assert(object->local_has_got_offset(r_sym, got_type));
1992 got_offset = (object->local_got_offset(r_sym, got_type)
1993 - target->got_size());
1995 if (optimized_type == tls::TLSOPT_TO_IE)
1997 gold_assert(tls_segment != NULL);
1998 value = target->got_plt_section()->address() + got_offset;
1999 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2000 rela, r_type, value, view, address,
2001 view_size);
2002 break;
2004 else if (optimized_type == tls::TLSOPT_NONE)
2006 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2008 // Relocate the field with the offset of the pair of GOT
2009 // entries.
2010 value = target->got_plt_section()->address() + got_offset;
2011 Relocate_functions<64, false>::pcrela32(view, value, addend,
2012 address);
2014 break;
2017 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2018 _("unsupported reloc %u"), r_type);
2019 break;
2021 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2022 this->saw_tls_block_reloc_ = true;
2023 if (optimized_type == tls::TLSOPT_TO_LE)
2025 gold_assert(tls_segment != NULL);
2026 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2027 value, view, view_size);
2028 break;
2030 else if (optimized_type == tls::TLSOPT_NONE)
2032 // Relocate the field with the offset of the GOT entry for
2033 // the module index.
2034 unsigned int got_offset;
2035 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2036 - target->got_size());
2037 value = target->got_plt_section()->address() + got_offset;
2038 Relocate_functions<64, false>::pcrela32(view, value, addend,
2039 address);
2040 break;
2042 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2043 _("unsupported reloc %u"), r_type);
2044 break;
2046 case elfcpp::R_X86_64_DTPOFF32:
2047 gold_assert(tls_segment != NULL);
2048 if (optimized_type == tls::TLSOPT_TO_LE)
2050 // This relocation type is used in debugging information.
2051 // In that case we need to not optimize the value. If we
2052 // haven't seen a TLSLD reloc, then we assume we should not
2053 // optimize this reloc.
2054 if (this->saw_tls_block_reloc_)
2055 value -= tls_segment->memsz();
2057 Relocate_functions<64, false>::rela32(view, value, addend);
2058 break;
2060 case elfcpp::R_X86_64_DTPOFF64:
2061 gold_assert(tls_segment != NULL);
2062 if (optimized_type == tls::TLSOPT_TO_LE)
2064 // See R_X86_64_DTPOFF32, just above, for why we test this.
2065 if (this->saw_tls_block_reloc_)
2066 value -= tls_segment->memsz();
2068 Relocate_functions<64, false>::rela64(view, value, addend);
2069 break;
2071 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2072 if (optimized_type == tls::TLSOPT_TO_LE)
2074 gold_assert(tls_segment != NULL);
2075 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2076 rela, r_type, value, view,
2077 view_size);
2078 break;
2080 else if (optimized_type == tls::TLSOPT_NONE)
2082 // Relocate the field with the offset of the GOT entry for
2083 // the tp-relative offset of the symbol.
2084 unsigned int got_offset;
2085 if (gsym != NULL)
2087 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2088 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2089 - target->got_size());
2091 else
2093 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2094 gold_assert(object->local_has_got_offset(r_sym,
2095 GOT_TYPE_TLS_OFFSET));
2096 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2097 - target->got_size());
2099 value = target->got_plt_section()->address() + got_offset;
2100 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2101 break;
2103 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2104 _("unsupported reloc type %u"),
2105 r_type);
2106 break;
2108 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2109 value -= tls_segment->memsz();
2110 Relocate_functions<64, false>::rela32(view, value, addend);
2111 break;
2115 // Do a relocation in which we convert a TLS General-Dynamic to an
2116 // Initial-Exec.
2118 inline void
2119 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2120 size_t relnum,
2121 Output_segment*,
2122 const elfcpp::Rela<64, false>& rela,
2123 unsigned int,
2124 elfcpp::Elf_types<64>::Elf_Addr value,
2125 unsigned char* view,
2126 elfcpp::Elf_types<64>::Elf_Addr address,
2127 section_size_type view_size)
2129 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2130 // .word 0x6666; rex64; call __tls_get_addr
2131 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2133 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2134 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2136 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2137 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2138 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2139 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2141 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2143 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2144 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2146 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2147 // We can skip it.
2148 this->skip_call_tls_get_addr_ = true;
2151 // Do a relocation in which we convert a TLS General-Dynamic to a
2152 // Local-Exec.
2154 inline void
2155 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2156 size_t relnum,
2157 Output_segment* tls_segment,
2158 const elfcpp::Rela<64, false>& rela,
2159 unsigned int,
2160 elfcpp::Elf_types<64>::Elf_Addr value,
2161 unsigned char* view,
2162 section_size_type view_size)
2164 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2165 // .word 0x6666; rex64; call __tls_get_addr
2166 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2168 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2169 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2171 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2172 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2173 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2174 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2176 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2178 value -= tls_segment->memsz();
2179 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2181 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2182 // We can skip it.
2183 this->skip_call_tls_get_addr_ = true;
2186 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2188 inline void
2189 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2190 const Relocate_info<64, false>* relinfo,
2191 size_t relnum,
2192 Output_segment*,
2193 const elfcpp::Rela<64, false>& rela,
2194 unsigned int r_type,
2195 elfcpp::Elf_types<64>::Elf_Addr value,
2196 unsigned char* view,
2197 elfcpp::Elf_types<64>::Elf_Addr address,
2198 section_size_type view_size)
2200 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2202 // leaq foo@tlsdesc(%rip), %rax
2203 // ==> movq foo@gottpoff(%rip), %rax
2204 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2205 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2206 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2207 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2208 view[-2] = 0x8b;
2209 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2210 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2212 else
2214 // call *foo@tlscall(%rax)
2215 // ==> nop; nop
2216 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2217 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2218 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2219 view[0] == 0xff && view[1] == 0x10);
2220 view[0] = 0x66;
2221 view[1] = 0x90;
2225 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2227 inline void
2228 Target_x86_64::Relocate::tls_desc_gd_to_le(
2229 const Relocate_info<64, false>* relinfo,
2230 size_t relnum,
2231 Output_segment* tls_segment,
2232 const elfcpp::Rela<64, false>& rela,
2233 unsigned int r_type,
2234 elfcpp::Elf_types<64>::Elf_Addr value,
2235 unsigned char* view,
2236 section_size_type view_size)
2238 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2240 // leaq foo@tlsdesc(%rip), %rax
2241 // ==> movq foo@tpoff, %rax
2242 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2243 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2244 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2245 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2246 view[-2] = 0xc7;
2247 view[-1] = 0xc0;
2248 value -= tls_segment->memsz();
2249 Relocate_functions<64, false>::rela32(view, value, 0);
2251 else
2253 // call *foo@tlscall(%rax)
2254 // ==> nop; nop
2255 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2256 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2257 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2258 view[0] == 0xff && view[1] == 0x10);
2259 view[0] = 0x66;
2260 view[1] = 0x90;
2264 inline void
2265 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2266 size_t relnum,
2267 Output_segment*,
2268 const elfcpp::Rela<64, false>& rela,
2269 unsigned int,
2270 elfcpp::Elf_types<64>::Elf_Addr,
2271 unsigned char* view,
2272 section_size_type view_size)
2274 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2275 // ... leq foo@dtpoff(%rax),%reg
2276 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2278 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2279 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2281 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2282 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2284 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2286 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2288 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2289 // We can skip it.
2290 this->skip_call_tls_get_addr_ = true;
2293 // Do a relocation in which we convert a TLS Initial-Exec to a
2294 // Local-Exec.
2296 inline void
2297 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2298 size_t relnum,
2299 Output_segment* tls_segment,
2300 const elfcpp::Rela<64, false>& rela,
2301 unsigned int,
2302 elfcpp::Elf_types<64>::Elf_Addr value,
2303 unsigned char* view,
2304 section_size_type view_size)
2306 // We need to examine the opcodes to figure out which instruction we
2307 // are looking at.
2309 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2310 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2312 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2313 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2315 unsigned char op1 = view[-3];
2316 unsigned char op2 = view[-2];
2317 unsigned char op3 = view[-1];
2318 unsigned char reg = op3 >> 3;
2320 if (op2 == 0x8b)
2322 // movq
2323 if (op1 == 0x4c)
2324 view[-3] = 0x49;
2325 view[-2] = 0xc7;
2326 view[-1] = 0xc0 | reg;
2328 else if (reg == 4)
2330 // Special handling for %rsp.
2331 if (op1 == 0x4c)
2332 view[-3] = 0x49;
2333 view[-2] = 0x81;
2334 view[-1] = 0xc0 | reg;
2336 else
2338 // addq
2339 if (op1 == 0x4c)
2340 view[-3] = 0x4d;
2341 view[-2] = 0x8d;
2342 view[-1] = 0x80 | reg | (reg << 3);
2345 value -= tls_segment->memsz();
2346 Relocate_functions<64, false>::rela32(view, value, 0);
2349 // Relocate section data.
2351 void
2352 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2353 unsigned int sh_type,
2354 const unsigned char* prelocs,
2355 size_t reloc_count,
2356 Output_section* output_section,
2357 bool needs_special_offset_handling,
2358 unsigned char* view,
2359 elfcpp::Elf_types<64>::Elf_Addr address,
2360 section_size_type view_size)
2362 gold_assert(sh_type == elfcpp::SHT_RELA);
2364 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2365 Target_x86_64::Relocate>(
2366 relinfo,
2367 this,
2368 prelocs,
2369 reloc_count,
2370 output_section,
2371 needs_special_offset_handling,
2372 view,
2373 address,
2374 view_size);
2377 // Return the size of a relocation while scanning during a relocatable
2378 // link.
2380 unsigned int
2381 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2382 unsigned int r_type,
2383 Relobj* object)
2385 switch (r_type)
2387 case elfcpp::R_X86_64_NONE:
2388 case elfcpp::R_386_GNU_VTINHERIT:
2389 case elfcpp::R_386_GNU_VTENTRY:
2390 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2391 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2392 case elfcpp::R_X86_64_TLSDESC_CALL:
2393 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2394 case elfcpp::R_X86_64_DTPOFF32:
2395 case elfcpp::R_X86_64_DTPOFF64:
2396 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2397 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2398 return 0;
2400 case elfcpp::R_X86_64_64:
2401 case elfcpp::R_X86_64_PC64:
2402 case elfcpp::R_X86_64_GOTOFF64:
2403 case elfcpp::R_X86_64_GOTPC64:
2404 case elfcpp::R_X86_64_PLTOFF64:
2405 case elfcpp::R_X86_64_GOT64:
2406 case elfcpp::R_X86_64_GOTPCREL64:
2407 case elfcpp::R_X86_64_GOTPCREL:
2408 case elfcpp::R_X86_64_GOTPLT64:
2409 return 8;
2411 case elfcpp::R_X86_64_32:
2412 case elfcpp::R_X86_64_32S:
2413 case elfcpp::R_X86_64_PC32:
2414 case elfcpp::R_X86_64_PLT32:
2415 case elfcpp::R_X86_64_GOTPC32:
2416 case elfcpp::R_X86_64_GOT32:
2417 return 4;
2419 case elfcpp::R_X86_64_16:
2420 case elfcpp::R_X86_64_PC16:
2421 return 2;
2423 case elfcpp::R_X86_64_8:
2424 case elfcpp::R_X86_64_PC8:
2425 return 1;
2427 case elfcpp::R_X86_64_COPY:
2428 case elfcpp::R_X86_64_GLOB_DAT:
2429 case elfcpp::R_X86_64_JUMP_SLOT:
2430 case elfcpp::R_X86_64_RELATIVE:
2431 // These are outstanding tls relocs, which are unexpected when linking
2432 case elfcpp::R_X86_64_TPOFF64:
2433 case elfcpp::R_X86_64_DTPMOD64:
2434 case elfcpp::R_X86_64_TLSDESC:
2435 object->error(_("unexpected reloc %u in object file"), r_type);
2436 return 0;
2438 case elfcpp::R_X86_64_SIZE32:
2439 case elfcpp::R_X86_64_SIZE64:
2440 default:
2441 object->error(_("unsupported reloc %u against local symbol"), r_type);
2442 return 0;
2446 // Scan the relocs during a relocatable link.
2448 void
2449 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2450 Symbol_table* symtab,
2451 Layout* layout,
2452 Sized_relobj<64, false>* object,
2453 unsigned int data_shndx,
2454 unsigned int sh_type,
2455 const unsigned char* prelocs,
2456 size_t reloc_count,
2457 Output_section* output_section,
2458 bool needs_special_offset_handling,
2459 size_t local_symbol_count,
2460 const unsigned char* plocal_symbols,
2461 Relocatable_relocs* rr)
2463 gold_assert(sh_type == elfcpp::SHT_RELA);
2465 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2466 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2468 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2469 Scan_relocatable_relocs>(
2470 options,
2471 symtab,
2472 layout,
2473 object,
2474 data_shndx,
2475 prelocs,
2476 reloc_count,
2477 output_section,
2478 needs_special_offset_handling,
2479 local_symbol_count,
2480 plocal_symbols,
2481 rr);
2484 // Relocate a section during a relocatable link.
2486 void
2487 Target_x86_64::relocate_for_relocatable(
2488 const Relocate_info<64, false>* relinfo,
2489 unsigned int sh_type,
2490 const unsigned char* prelocs,
2491 size_t reloc_count,
2492 Output_section* output_section,
2493 off_t offset_in_output_section,
2494 const Relocatable_relocs* rr,
2495 unsigned char* view,
2496 elfcpp::Elf_types<64>::Elf_Addr view_address,
2497 section_size_type view_size,
2498 unsigned char* reloc_view,
2499 section_size_type reloc_view_size)
2501 gold_assert(sh_type == elfcpp::SHT_RELA);
2503 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2504 relinfo,
2505 prelocs,
2506 reloc_count,
2507 output_section,
2508 offset_in_output_section,
2510 view,
2511 view_address,
2512 view_size,
2513 reloc_view,
2514 reloc_view_size);
2517 // Return the value to use for a dynamic which requires special
2518 // treatment. This is how we support equality comparisons of function
2519 // pointers across shared library boundaries, as described in the
2520 // processor specific ABI supplement.
2522 uint64_t
2523 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2525 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2526 return this->plt_section()->address() + gsym->plt_offset();
2529 // Return a string used to fill a code section with nops to take up
2530 // the specified length.
2532 std::string
2533 Target_x86_64::do_code_fill(section_size_type length) const
2535 if (length >= 16)
2537 // Build a jmpq instruction to skip over the bytes.
2538 unsigned char jmp[5];
2539 jmp[0] = 0xe9;
2540 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2541 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2542 + std::string(length - 5, '\0'));
2545 // Nop sequences of various lengths.
2546 const char nop1[1] = { 0x90 }; // nop
2547 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2548 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2549 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2550 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2551 0x00 }; // leal 0(%esi,1),%esi
2552 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2553 0x00, 0x00 };
2554 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2555 0x00, 0x00, 0x00 };
2556 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2557 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2558 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2559 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2560 0x00 };
2561 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2562 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2563 0x00, 0x00 };
2564 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2565 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2566 0x00, 0x00, 0x00 };
2567 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2568 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2569 0x00, 0x00, 0x00, 0x00 };
2570 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2571 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2572 0x27, 0x00, 0x00, 0x00,
2573 0x00 };
2574 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2575 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2576 0xbc, 0x27, 0x00, 0x00,
2577 0x00, 0x00 };
2578 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2579 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2580 0x90, 0x90, 0x90, 0x90,
2581 0x90, 0x90, 0x90 };
2583 const char* nops[16] = {
2584 NULL,
2585 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2586 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2589 return std::string(nops[length], length);
2592 // The selector for x86_64 object files.
2594 class Target_selector_x86_64 : public Target_selector
2596 public:
2597 Target_selector_x86_64()
2598 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2601 Target*
2602 do_instantiate_target()
2603 { return new Target_x86_64(); }
2606 Target_selector_x86_64 target_selector_x86_64;
2608 } // End anonymous namespace.