2007-10-03 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / x86_64.cc
blobdf760512d0d7383b0a6f5f5c813eeb5d07ab62ad
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 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
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30 // 02110-1301, USA.
32 #include "gold.h"
34 #include <cstring>
36 #include "elfcpp.h"
37 #include "parameters.h"
38 #include "reloc.h"
39 #include "x86_64.h"
40 #include "object.h"
41 #include "symtab.h"
42 #include "layout.h"
43 #include "output.h"
44 #include "target.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
48 namespace
51 using namespace gold;
53 class Output_data_plt_x86_64;
55 // The x86_64 target class.
56 // See the ABI at http://www.x86-64.org/documentation/abi.pdf
58 class Target_x86_64 : public Sized_target<64, false>
60 public:
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
65 Target_x86_64()
66 : Sized_target<64, false>(&x86_64_info),
67 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
68 copy_relocs_(NULL), dynbss_(NULL)
69 { }
71 // Scan the relocations to look for symbol adjustments.
72 void
73 scan_relocs(const General_options& options,
74 Symbol_table* symtab,
75 Layout* layout,
76 Sized_relobj<64, false>* object,
77 unsigned int data_shndx,
78 unsigned int sh_type,
79 const unsigned char* prelocs,
80 size_t reloc_count,
81 size_t local_symbol_count,
82 const unsigned char* plocal_symbols,
83 Symbol** global_symbols);
85 // Finalize the sections.
86 void
87 do_finalize_sections(Layout*);
89 // Return the value to use for a dynamic which requires special
90 // treatment.
91 uint64_t
92 do_dynsym_value(const Symbol*) const;
94 // Relocate a section.
95 void
96 relocate_section(const Relocate_info<64, false>*,
97 unsigned int sh_type,
98 const unsigned char* prelocs,
99 size_t reloc_count,
100 unsigned char* view,
101 elfcpp::Elf_types<64>::Elf_Addr view_address,
102 off_t view_size);
104 // Return a string used to fill a code section with nops.
105 std::string
106 do_code_fill(off_t length);
108 private:
109 // The class which scans relocations.
110 struct Scan
112 inline void
113 local(const General_options& options, Symbol_table* symtab,
114 Layout* layout, Target_x86_64* target,
115 Sized_relobj<64, false>* object,
116 unsigned int data_shndx,
117 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
118 const elfcpp::Sym<64, false>& lsym);
120 inline void
121 global(const General_options& options, Symbol_table* symtab,
122 Layout* layout, Target_x86_64* target,
123 Sized_relobj<64, false>* object,
124 unsigned int data_shndx,
125 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
126 Symbol* gsym);
129 // The class which implements relocation.
130 class Relocate
132 public:
133 Relocate()
134 : skip_call_tls_get_addr_(false)
137 ~Relocate()
139 if (this->skip_call_tls_get_addr_)
141 // FIXME: This needs to specify the location somehow.
142 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
143 program_name);
144 gold_exit(false);
148 // Do a relocation. Return false if the caller should not issue
149 // any warnings about this relocation.
150 inline bool
151 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
152 const elfcpp::Rela<64, false>&,
153 unsigned int r_type, const Sized_symbol<64>*,
154 const Symbol_value<64>*,
155 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
156 off_t);
158 private:
159 // Do a TLS relocation.
160 inline void
161 relocate_tls(const Relocate_info<64, false>*, size_t relnum,
162 const elfcpp::Rela<64, false>&,
163 unsigned int r_type, const Sized_symbol<64>*,
164 const Symbol_value<64>*,
165 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
167 // Do a TLS Initial-Exec to Local-Exec transition.
168 static inline void
169 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
170 Output_segment* tls_segment,
171 const elfcpp::Rela<64, false>&, unsigned int r_type,
172 elfcpp::Elf_types<64>::Elf_Addr value,
173 unsigned char* view,
174 off_t view_size);
176 // Do a TLS Global-Dynamic to Local-Exec transition.
177 inline void
178 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
179 Output_segment* tls_segment,
180 const elfcpp::Rela<64, false>&, unsigned int r_type,
181 elfcpp::Elf_types<64>::Elf_Addr value,
182 unsigned char* view,
183 off_t view_size);
185 // Check the range for a TLS relocation.
186 static inline void
187 check_range(const Relocate_info<64, false>*, size_t relnum,
188 const elfcpp::Rela<64, false>&, off_t, off_t);
190 // Check the validity of a TLS relocation. This is like assert.
191 static inline void
192 check_tls(const Relocate_info<64, false>*, size_t relnum,
193 const elfcpp::Rela<64, false>&, bool);
195 // This is set if we should skip the next reloc, which should be a
196 // PLT32 reloc against ___tls_get_addr.
197 bool skip_call_tls_get_addr_;
200 // Adjust TLS relocation type based on the options and whether this
201 // is a local symbol.
202 static unsigned int
203 optimize_tls_reloc(bool is_final, int r_type);
205 // Get the GOT section, creating it if necessary.
206 Output_data_got<64, false>*
207 got_section(Symbol_table*, Layout*);
209 // Create a PLT entry for a global symbol.
210 void
211 make_plt_entry(Symbol_table*, Layout*, Symbol*);
213 // Get the PLT section.
214 Output_data_plt_x86_64*
215 plt_section() const
217 gold_assert(this->plt_ != NULL);
218 return this->plt_;
221 // Get the dynamic reloc section, creating it if necessary.
222 Reloc_section*
223 rel_dyn_section(Layout*);
225 // Copy a relocation against a global symbol.
226 void
227 copy_reloc(const General_options*, Symbol_table*, Layout*,
228 Sized_relobj<64, false>*, unsigned int,
229 Symbol*, const elfcpp::Rela<64, false>&);
231 // Information about this specific target which we pass to the
232 // general Target structure.
233 static const Target::Target_info x86_64_info;
235 // The GOT section.
236 Output_data_got<64, false>* got_;
237 // The PLT section.
238 Output_data_plt_x86_64* plt_;
239 // The GOT PLT section.
240 Output_data_space* got_plt_;
241 // The dynamic reloc section.
242 Reloc_section* rel_dyn_;
243 // Relocs saved to avoid a COPY reloc.
244 Copy_relocs<64, false>* copy_relocs_;
245 // Space for variables copied with a COPY reloc.
246 Output_data_space* dynbss_;
249 const Target::Target_info Target_x86_64::x86_64_info =
251 64, // size
252 false, // is_big_endian
253 elfcpp::EM_X86_64, // machine_code
254 false, // has_make_symbol
255 false, // has_resolve
256 true, // has_code_fill
257 "/lib/ld64.so.1", // program interpreter
258 0x400000, // text_segment_address
259 0x1000, // abi_pagesize
260 0x1000 // common_pagesize
263 // Get the GOT section, creating it if necessary.
265 Output_data_got<64, false>*
266 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
268 if (this->got_ == NULL)
270 gold_assert(symtab != NULL && layout != NULL);
272 this->got_ = new Output_data_got<64, false>();
274 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
275 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
276 this->got_);
278 // The old GNU linker creates a .got.plt section. We just
279 // create another set of data in the .got section. Note that we
280 // always create a PLT if we create a GOT, although the PLT
281 // might be empty.
282 // TODO(csilvers): do we really need an alignment of 8?
283 this->got_plt_ = new Output_data_space(8);
284 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
285 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
286 this->got_plt_);
288 // The first three entries are reserved.
289 this->got_plt_->set_space_size(3 * 8);
291 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
292 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
293 this->got_plt_,
294 0, 0, elfcpp::STT_OBJECT,
295 elfcpp::STB_LOCAL,
296 elfcpp::STV_HIDDEN, 0,
297 false, false);
300 return this->got_;
303 // Get the dynamic reloc section, creating it if necessary.
305 Target_x86_64::Reloc_section*
306 Target_x86_64::rel_dyn_section(Layout* layout)
308 if (this->rel_dyn_ == NULL)
310 gold_assert(layout != NULL);
311 this->rel_dyn_ = new Reloc_section();
312 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
313 elfcpp::SHF_ALLOC, this->rel_dyn_);
315 return this->rel_dyn_;
318 // A class to handle the PLT data.
320 class Output_data_plt_x86_64 : public Output_section_data
322 public:
323 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
325 Output_data_plt_x86_64(Layout*, Output_data_space*);
327 // Add an entry to the PLT.
328 void
329 add_entry(Symbol* gsym);
331 // Return the .rel.plt section data.
332 const Reloc_section*
333 rel_plt() const
334 { return this->rel_; }
336 protected:
337 void
338 do_adjust_output_section(Output_section* os);
340 private:
341 // The size of an entry in the PLT.
342 static const int plt_entry_size = 16;
344 // The first entry in the PLT.
345 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
346 // procedure linkage table for both programs and shared objects."
347 static unsigned char first_plt_entry[plt_entry_size];
349 // Other entries in the PLT for an executable.
350 static unsigned char plt_entry[plt_entry_size];
352 // Set the final size.
353 void
354 do_set_address(uint64_t, off_t)
355 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
357 // Write out the PLT data.
358 void
359 do_write(Output_file*);
361 // The reloc section.
362 Reloc_section* rel_;
363 // The .got.plt section.
364 Output_data_space* got_plt_;
365 // The number of PLT entries.
366 unsigned int count_;
369 // Create the PLT section. The ordinary .got section is an argument,
370 // since we need to refer to the start. We also create our own .got
371 // section just for PLT entries.
373 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
374 Output_data_space* got_plt)
375 // TODO(csilvers): do we really need an alignment of 8?
376 : Output_section_data(8), got_plt_(got_plt), count_(0)
378 this->rel_ = new Reloc_section();
379 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
380 elfcpp::SHF_ALLOC, this->rel_);
383 void
384 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
386 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
387 // linker, and so do we.
388 os->set_entsize(4);
391 // Add an entry to the PLT.
393 void
394 Output_data_plt_x86_64::add_entry(Symbol* gsym)
396 gold_assert(!gsym->has_plt_offset());
398 // Note that when setting the PLT offset we skip the initial
399 // reserved PLT entry.
400 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
402 ++this->count_;
404 off_t got_offset = this->got_plt_->data_size();
406 // Every PLT entry needs a GOT entry which points back to the PLT
407 // entry (this will be changed by the dynamic linker, normally
408 // lazily when the function is called).
409 this->got_plt_->set_space_size(got_offset + 8);
411 // Every PLT entry needs a reloc.
412 gsym->set_needs_dynsym_entry();
413 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
414 got_offset, 0);
416 // Note that we don't need to save the symbol. The contents of the
417 // PLT are independent of which symbols are used. The symbols only
418 // appear in the relocations.
421 // The first entry in the PLT for an executable.
423 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
425 // From AMD64 ABI Draft 0.98, page 76
426 0xff, 0x35, // pushq contents of memory address
427 0, 0, 0, 0, // replaced with address of .got + 4
428 0xff, 0x25, // jmp indirect
429 0, 0, 0, 0, // replaced with address of .got + 8
430 0x90, 0x90, 0x90, 0x90 // noop (x4)
433 // Subsequent entries in the PLT for an executable.
435 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
437 // From AMD64 ABI Draft 0.98, page 76
438 0xff, 0x25, // jmpq indirect
439 0, 0, 0, 0, // replaced with address of symbol in .got
440 0x68, // pushq immediate
441 0, 0, 0, 0, // replaced with offset into relocation table
442 0xe9, // jmpq relative
443 0, 0, 0, 0 // replaced with offset to start of .plt
446 // Write out the PLT. This uses the hand-coded instructions above,
447 // and adjusts them as needed. This is specified by the AMD64 ABI.
449 void
450 Output_data_plt_x86_64::do_write(Output_file* of)
452 const off_t offset = this->offset();
453 const off_t oview_size = this->data_size();
454 unsigned char* const oview = of->get_output_view(offset, oview_size);
456 const off_t got_file_offset = this->got_plt_->offset();
457 const off_t got_size = this->got_plt_->data_size();
458 unsigned char* const got_view = of->get_output_view(got_file_offset,
459 got_size);
461 unsigned char* pov = oview;
463 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
464 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
466 memcpy(pov, first_plt_entry, plt_entry_size);
467 if (!parameters->output_is_shared())
469 // We do a jmp relative to the PC at the end of this instruction.
470 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
471 - (plt_address + 6));
472 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
473 - (plt_address + 12));
475 pov += plt_entry_size;
477 unsigned char* got_pov = got_view;
479 memset(got_pov, 0, 24);
480 got_pov += 24;
482 unsigned int plt_offset = plt_entry_size;
483 unsigned int got_offset = 24;
484 const unsigned int count = this->count_;
485 for (unsigned int plt_index = 0;
486 plt_index < count;
487 ++plt_index,
488 pov += plt_entry_size,
489 got_pov += 8,
490 plt_offset += plt_entry_size,
491 got_offset += 8)
493 // Set and adjust the PLT entry itself.
494 memcpy(pov, plt_entry, plt_entry_size);
495 if (parameters->output_is_shared())
496 // FIXME(csilvers): what's the right thing to write here?
497 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
498 else
499 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
500 (got_address + got_offset
501 - (plt_address + plt_offset
502 + 6)));
504 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
505 elfcpp::Swap<32, false>::writeval(pov + 12,
506 - (plt_offset + plt_entry_size));
508 // Set the entry in the GOT.
509 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
512 gold_assert(pov - oview == oview_size);
513 gold_assert(got_pov - got_view == got_size);
515 of->write_output_view(offset, oview_size, oview);
516 of->write_output_view(got_file_offset, got_size, got_view);
519 // Create a PLT entry for a global symbol.
521 void
522 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
523 Symbol* gsym)
525 if (gsym->has_plt_offset())
526 return;
528 if (this->plt_ == NULL)
530 // Create the GOT sections first.
531 this->got_section(symtab, layout);
533 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
534 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
535 (elfcpp::SHF_ALLOC
536 | elfcpp::SHF_EXECINSTR),
537 this->plt_);
540 this->plt_->add_entry(gsym);
543 // Handle a relocation against a non-function symbol defined in a
544 // dynamic object. The traditional way to handle this is to generate
545 // a COPY relocation to copy the variable at runtime from the shared
546 // object into the executable's data segment. However, this is
547 // undesirable in general, as if the size of the object changes in the
548 // dynamic object, the executable will no longer work correctly. If
549 // this relocation is in a writable section, then we can create a
550 // dynamic reloc and the dynamic linker will resolve it to the correct
551 // address at runtime. However, we do not want do that if the
552 // relocation is in a read-only section, as it would prevent the
553 // readonly segment from being shared. And if we have to eventually
554 // generate a COPY reloc, then any dynamic relocations will be
555 // useless. So this means that if this is a writable section, we need
556 // to save the relocation until we see whether we have to create a
557 // COPY relocation for this symbol for any other relocation.
559 void
560 Target_x86_64::copy_reloc(const General_options* options,
561 Symbol_table* symtab,
562 Layout* layout,
563 Sized_relobj<64, false>* object,
564 unsigned int data_shndx, Symbol* gsym,
565 const elfcpp::Rela<64, false>& rel)
567 Sized_symbol<64>* ssym;
568 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
569 SELECT_SIZE(64));
571 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
572 data_shndx, ssym))
574 // So far we do not need a COPY reloc. Save this relocation.
575 // If it turns out that we never need a COPY reloc for this
576 // symbol, then we will emit the relocation.
577 if (this->copy_relocs_ == NULL)
578 this->copy_relocs_ = new Copy_relocs<64, false>();
579 this->copy_relocs_->save(ssym, object, data_shndx, rel);
581 else
583 // Allocate space for this symbol in the .bss section.
585 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
587 // There is no defined way to determine the required alignment
588 // of the symbol. We pick the alignment based on the size. We
589 // set an arbitrary maximum of 256.
590 unsigned int align;
591 for (align = 1; align < 512; align <<= 1)
592 if ((symsize & align) != 0)
593 break;
595 if (this->dynbss_ == NULL)
597 this->dynbss_ = new Output_data_space(align);
598 layout->add_output_section_data(".bss",
599 elfcpp::SHT_NOBITS,
600 (elfcpp::SHF_ALLOC
601 | elfcpp::SHF_WRITE),
602 this->dynbss_);
605 Output_data_space* dynbss = this->dynbss_;
607 if (align > dynbss->addralign())
608 dynbss->set_space_alignment(align);
610 off_t dynbss_size = dynbss->data_size();
611 dynbss_size = align_address(dynbss_size, align);
612 off_t offset = dynbss_size;
613 dynbss->set_space_size(dynbss_size + symsize);
615 // Define the symbol in the .dynbss section.
616 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
617 dynbss, offset, symsize, ssym->type(),
618 ssym->binding(), ssym->visibility(),
619 ssym->nonvis(), false, false);
621 // Add the COPY reloc.
622 ssym->set_needs_dynsym_entry();
623 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
624 rel_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset,
625 rel.get_r_addend());
630 // Optimize the TLS relocation type based on what we know about the
631 // symbol. IS_FINAL is true if the final address of this symbol is
632 // known at link time.
634 unsigned int
635 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
637 return is_final ? r_type : 0;
638 #if 0
639 // If we are generating a shared library, then we can't do anything
640 // in the linker.
641 if (parameters->output_is_shared())
642 return r_type;
644 switch (r_type)
646 case elfcpp::R_X86_64_TLSGD:
647 // These are Global-Dynamic which permits fully general TLS
648 // access. Since we know that we are generating an executable,
649 // we can convert this to Initial-Exec. If we also know that
650 // this is a local symbol, we can further switch to Local-Exec.
651 if (is_final)
652 return elfcpp::R_X86_64_TLS_LE_64;
653 return elfcpp::R_X86_64_TLS_IE_64;
655 case elfcpp::R_X86_64_TLS_LDM:
656 // This is Local-Dynamic, which refers to a local symbol in the
657 // dynamic TLS block. Since we know that we generating an
658 // executable, we can switch to Local-Exec.
659 return elfcpp::R_X86_64_TLS_LE_64;
661 case elfcpp::R_X86_64_TLS_LDO_64:
662 // Another type of Local-Dynamic relocation.
663 return elfcpp::R_X86_64_TLS_LE;
665 case elfcpp::R_X86_64_TLS_IE:
666 case elfcpp::R_X86_64_TLS_GOTIE:
667 case elfcpp::R_X86_64_TLS_IE_64:
668 // These are Initial-Exec relocs which get the thread offset
669 // from the GOT. If we know that we are linking against the
670 // local symbol, we can switch to Local-Exec, which links the
671 // thread offset into the instruction.
672 if (is_final)
673 return elfcpp::R_X86_64_TLS_LE_64;
674 return r_type;
676 case elfcpp::R_X86_64_TLS_LE:
677 case elfcpp::R_X86_64_TLS_LE_64:
678 // When we already have Local-Exec, there is nothing further we
679 // can do.
680 return r_type;
682 default:
683 gold_unreachable();
685 #endif
688 // Scan a relocation for a local symbol.
690 inline void
691 Target_x86_64::Scan::local(const General_options&,
692 Symbol_table* symtab,
693 Layout* layout,
694 Target_x86_64* target,
695 Sized_relobj<64, false>* object,
696 unsigned int,
697 const elfcpp::Rela<64, false>&,
698 unsigned int r_type,
699 const elfcpp::Sym<64, false>&)
701 switch (r_type)
703 case elfcpp::R_X86_64_NONE:
704 case elfcpp::R_386_GNU_VTINHERIT:
705 case elfcpp::R_386_GNU_VTENTRY:
706 break;
708 case elfcpp::R_X86_64_64:
709 case elfcpp::R_X86_64_32:
710 case elfcpp::R_X86_64_32S:
711 case elfcpp::R_X86_64_16:
712 case elfcpp::R_X86_64_8:
713 // FIXME: If we are generating a shared object we need to copy
714 // this relocation into the object.
715 gold_assert(!parameters->output_is_shared());
716 break;
718 case elfcpp::R_X86_64_PC64:
719 case elfcpp::R_X86_64_PC32:
720 case elfcpp::R_X86_64_PC16:
721 case elfcpp::R_X86_64_PC8:
722 break;
724 case elfcpp::R_X86_64_GOTPCREL:
725 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
726 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
727 case elfcpp::R_X86_64_GOTOFF64:
728 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
729 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
730 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
731 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
732 // We need a GOT section.
733 target->got_section(symtab, layout);
734 break;
736 case elfcpp::R_X86_64_COPY:
737 case elfcpp::R_X86_64_GLOB_DAT:
738 case elfcpp::R_X86_64_JUMP_SLOT:
739 case elfcpp::R_X86_64_RELATIVE:
740 case elfcpp::R_X86_64_TPOFF64:
741 case elfcpp::R_X86_64_TPOFF32:
742 case elfcpp::R_X86_64_DTPMOD64:
743 case elfcpp::R_X86_64_DTPOFF64:
744 case elfcpp::R_X86_64_DTPOFF32:
745 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
746 case elfcpp::R_X86_64_TLSDESC:
747 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
748 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
749 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
750 program_name, object->name().c_str(), r_type);
751 gold_exit(false);
752 break;
754 #if 0
755 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
756 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
758 case elfcpp::R_386_TLS_IE:
759 case elfcpp::R_386_TLS_GOTIE:
760 case elfcpp::R_386_TLS_LE:
761 case elfcpp::R_386_TLS_GD:
762 case elfcpp::R_386_TLS_LDM:
763 case elfcpp::R_386_TLS_LDO_64:
764 case elfcpp::R_386_TLS_IE_64:
765 case elfcpp::R_386_TLS_LE_64:
767 bool output_is_shared = parameters->output_is_shared();
768 r_type = Target_x86_64::optimize_tls_reloc(!output_is_shared,
769 r_type);
770 switch (r_type)
772 case elfcpp::R_X86_64_TLS_LE:
773 case elfcpp::R_X86_64_TLS_LE_64:
774 // FIXME: If generating a shared object, we need to copy
775 // this relocation into the object.
776 gold_assert(!output_is_shared);
777 break;
779 case elfcpp::R_X86_64_TLS_IE:
780 case elfcpp::R_X86_64_TLS_GOTIE:
781 case elfcpp::R_X86_64_TLS_GD:
782 case elfcpp::R_X86_64_TLS_LDM:
783 case elfcpp::R_X86_64_TLS_LDO_64:
784 case elfcpp::R_X86_64_TLS_IE_64:
785 fprintf(stderr,
786 _("%s: %s: unsupported reloc %u against local symbol\n"),
787 program_name, object->name().c_str(), r_type);
788 break;
791 break;
792 #endif
794 case elfcpp::R_X86_64_GOT32:
795 case elfcpp::R_X86_64_PLT32:
796 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
797 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
798 #if 0
799 case elfcpp::R_386_TLS_GD_64:
800 case elfcpp::R_386_TLS_GD_PUSH:
801 case elfcpp::R_386_TLS_GD_CALL:
802 case elfcpp::R_386_TLS_GD_POP:
803 case elfcpp::R_386_TLS_LDM_64:
804 case elfcpp::R_386_TLS_LDM_PUSH:
805 case elfcpp::R_386_TLS_LDM_CALL:
806 case elfcpp::R_386_TLS_LDM_POP:
807 #endif
808 default:
809 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
810 program_name, object->name().c_str(), r_type);
811 break;
816 // Scan a relocation for a global symbol.
818 inline void
819 Target_x86_64::Scan::global(const General_options& options,
820 Symbol_table* symtab,
821 Layout* layout,
822 Target_x86_64* target,
823 Sized_relobj<64, false>* object,
824 unsigned int data_shndx,
825 const elfcpp::Rela<64, false>& reloc,
826 unsigned int r_type,
827 Symbol* gsym)
829 switch (r_type)
831 case elfcpp::R_X86_64_NONE:
832 case elfcpp::R_386_GNU_VTINHERIT:
833 case elfcpp::R_386_GNU_VTENTRY:
834 break;
836 case elfcpp::R_X86_64_64:
837 case elfcpp::R_X86_64_PC64:
838 case elfcpp::R_X86_64_32:
839 case elfcpp::R_X86_64_32S:
840 case elfcpp::R_X86_64_PC32:
841 case elfcpp::R_X86_64_16:
842 case elfcpp::R_X86_64_PC16:
843 case elfcpp::R_X86_64_8:
844 case elfcpp::R_X86_64_PC8:
845 // FIXME: If we are generating a shared object we may need to
846 // copy this relocation into the object. If this symbol is
847 // defined in a shared object, we may need to copy this
848 // relocation in order to avoid a COPY relocation.
849 gold_assert(!parameters->output_is_shared());
851 if (gsym->is_from_dynobj())
853 // This symbol is defined in a dynamic object. If it is a
854 // function, we make a PLT entry. Otherwise we need to
855 // either generate a COPY reloc or copy this reloc.
856 if (gsym->type() == elfcpp::STT_FUNC)
858 target->make_plt_entry(symtab, layout, gsym);
860 // If this is not a PC relative reference, then we may
861 // be taking the address of the function. In that case
862 // we need to set the entry in the dynamic symbol table
863 // to the address of the PLT entry.
864 if (r_type != elfcpp::R_X86_64_PC64
865 && r_type != elfcpp::R_X86_64_PC32
866 && r_type != elfcpp::R_X86_64_PC16
867 && r_type != elfcpp::R_X86_64_PC8)
868 gsym->set_needs_dynsym_value();
870 else
871 target->copy_reloc(&options, symtab, layout, object, data_shndx,
872 gsym, reloc);
875 break;
877 case elfcpp::R_X86_64_GOT32:
879 // The symbol requires a GOT entry.
880 Output_data_got<64, false>* got = target->got_section(symtab, layout);
881 if (got->add_global(gsym))
883 // If this symbol is not fully resolved, we need to add a
884 // dynamic relocation for it.
885 if (!gsym->final_value_is_known())
887 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
888 rel_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
889 gsym->got_offset(), reloc.get_r_addend());
893 break;
895 case elfcpp::R_X86_64_PLT32:
896 // If the symbol is fully resolved, this is just a PC32 reloc.
897 // Otherwise we need a PLT entry.
898 if (gsym->final_value_is_known())
899 break;
900 target->make_plt_entry(symtab, layout, gsym);
901 break;
903 case elfcpp::R_X86_64_GOTPCREL:
904 case elfcpp::R_X86_64_GOTPC32: // TODO(csilvers): correct?
905 case elfcpp::R_X86_64_GOT64: // TODO(csilvers): correct?
906 case elfcpp::R_X86_64_GOTOFF64:
907 case elfcpp::R_X86_64_GOTPC64: // TODO(csilvers): correct?
908 case elfcpp::R_X86_64_GOTPCREL64: // TODO(csilvers): correct?
909 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
910 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): correct?
911 // We need a GOT section.
912 target->got_section(symtab, layout);
913 break;
915 case elfcpp::R_X86_64_COPY:
916 case elfcpp::R_X86_64_GLOB_DAT:
917 case elfcpp::R_X86_64_JUMP_SLOT:
918 case elfcpp::R_X86_64_RELATIVE:
919 case elfcpp::R_X86_64_TPOFF64:
920 case elfcpp::R_X86_64_TPOFF32:
921 case elfcpp::R_X86_64_DTPMOD64:
922 case elfcpp::R_X86_64_DTPOFF64:
923 case elfcpp::R_X86_64_DTPOFF32:
924 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
925 case elfcpp::R_X86_64_TLSDESC:
926 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
927 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
928 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
929 program_name, object->name().c_str(), r_type);
930 gold_exit(false);
931 break;
933 #if 0
934 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
935 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
937 case elfcpp::R_386_TLS_IE:
938 case elfcpp::R_386_TLS_GOTIE:
939 case elfcpp::R_386_TLS_LE:
940 case elfcpp::R_386_TLS_GD:
941 case elfcpp::R_386_TLS_LDM:
942 case elfcpp::R_386_TLS_LDO_64:
943 case elfcpp::R_386_TLS_IE_64:
944 case elfcpp::R_386_TLS_LE_64:
946 const bool is_final = gsym->final_value_is_known();
947 r_type = Target_x86_64::optimize_tls_reloc(is_final, r_type);
948 switch (r_type)
950 case elfcpp::R_386_TLS_LE:
951 case elfcpp::R_386_TLS_LE_64:
952 // FIXME: If generating a shared object, we need to copy
953 // this relocation into the object.
954 gold_assert(!parameters->output_is_shared());
955 break;
957 case elfcpp::R_386_TLS_IE:
958 case elfcpp::R_386_TLS_GOTIE:
959 case elfcpp::R_386_TLS_GD:
960 case elfcpp::R_386_TLS_LDM:
961 case elfcpp::R_386_TLS_LDO_64:
962 case elfcpp::R_386_TLS_IE_64:
963 fprintf(stderr,
964 _("%s: %s: unsupported reloc %u "
965 "against global symbol %s\n"),
966 program_name, object->name().c_str(), r_type,
967 gsym->name());
968 break;
971 break;
972 #endif
974 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
975 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
976 #if 0
977 case elfcpp::R_386_TLS_GD_64:
978 case elfcpp::R_386_TLS_GD_PUSH:
979 case elfcpp::R_386_TLS_GD_CALL:
980 case elfcpp::R_386_TLS_GD_POP:
981 case elfcpp::R_386_TLS_LDM_64:
982 case elfcpp::R_386_TLS_LDM_PUSH:
983 case elfcpp::R_386_TLS_LDM_CALL:
984 case elfcpp::R_386_TLS_LDM_POP:
985 #endif
986 default:
987 fprintf(stderr,
988 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
989 program_name, object->name().c_str(), r_type, gsym->name());
990 break;
994 // Scan relocations for a section.
996 void
997 Target_x86_64::scan_relocs(const General_options& options,
998 Symbol_table* symtab,
999 Layout* layout,
1000 Sized_relobj<64, false>* object,
1001 unsigned int data_shndx,
1002 unsigned int sh_type,
1003 const unsigned char* prelocs,
1004 size_t reloc_count,
1005 size_t local_symbol_count,
1006 const unsigned char* plocal_symbols,
1007 Symbol** global_symbols)
1009 if (sh_type == elfcpp::SHT_REL)
1011 fprintf(stderr, _("%s: %s: unsupported REL reloc section\n"),
1012 program_name, object->name().c_str());
1013 gold_exit(false);
1016 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1017 Target_x86_64::Scan>(
1018 options,
1019 symtab,
1020 layout,
1021 this,
1022 object,
1023 data_shndx,
1024 prelocs,
1025 reloc_count,
1026 local_symbol_count,
1027 plocal_symbols,
1028 global_symbols);
1031 // Finalize the sections.
1033 void
1034 Target_x86_64::do_finalize_sections(Layout* layout)
1036 // Fill in some more dynamic tags.
1037 Output_data_dynamic* const odyn = layout->dynamic_data();
1038 if (odyn != NULL)
1040 if (this->got_plt_ != NULL)
1041 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1043 if (this->plt_ != NULL)
1045 const Output_data* od = this->plt_->rel_plt();
1046 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1047 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1048 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1051 if (this->rel_dyn_ != NULL)
1053 const Output_data* od = this->rel_dyn_;
1054 odyn->add_section_address(elfcpp::DT_RELA, od);
1055 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1056 odyn->add_constant(elfcpp::DT_RELAENT,
1057 elfcpp::Elf_sizes<64>::rela_size);
1060 if (!parameters->output_is_shared())
1062 // The value of the DT_DEBUG tag is filled in by the dynamic
1063 // linker at run time, and used by the debugger.
1064 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1068 // Emit any relocs we saved in an attempt to avoid generating COPY
1069 // relocs.
1070 if (this->copy_relocs_ == NULL)
1071 return;
1072 if (this->copy_relocs_->any_to_emit())
1074 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1075 this->copy_relocs_->emit(rel_dyn);
1077 delete this->copy_relocs_;
1078 this->copy_relocs_ = NULL;
1081 // Perform a relocation.
1083 inline bool
1084 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1085 Target_x86_64* target,
1086 size_t relnum,
1087 const elfcpp::Rela<64, false>& rel,
1088 unsigned int r_type,
1089 const Sized_symbol<64>* gsym,
1090 const Symbol_value<64>* psymval,
1091 unsigned char* view,
1092 elfcpp::Elf_types<64>::Elf_Addr address,
1093 off_t view_size)
1095 if (this->skip_call_tls_get_addr_)
1097 if (r_type != elfcpp::R_X86_64_PLT32
1098 || gsym == NULL
1099 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1101 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1102 program_name,
1103 relinfo->location(relnum, rel.get_r_offset()).c_str());
1104 gold_exit(false);
1107 this->skip_call_tls_get_addr_ = false;
1109 return false;
1112 // Pick the value to use for symbols defined in shared objects.
1113 Symbol_value<64> symval;
1114 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1116 symval.set_output_value(target->plt_section()->address()
1117 + gsym->plt_offset());
1118 psymval = &symval;
1121 const Sized_relobj<64, false>* object = relinfo->object;
1122 const elfcpp::Elf_Xword addend = rel.get_r_addend();
1124 switch (r_type)
1126 case elfcpp::R_X86_64_NONE:
1127 case elfcpp::R_386_GNU_VTINHERIT:
1128 case elfcpp::R_386_GNU_VTENTRY:
1129 break;
1131 case elfcpp::R_X86_64_64:
1132 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1133 break;
1135 case elfcpp::R_X86_64_PC64:
1136 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1137 address);
1138 break;
1140 case elfcpp::R_X86_64_32:
1141 // FIXME: we need to verify that value + addend fits into 32 bits:
1142 // uint64_t x = value + addend;
1143 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1144 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1145 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1146 break;
1148 case elfcpp::R_X86_64_32S:
1149 // FIXME: we need to verify that value + addend fits into 32 bits:
1150 // int64_t x = value + addend; // note this quantity is signed!
1151 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1152 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1153 break;
1155 case elfcpp::R_X86_64_PC32:
1156 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1157 address);
1158 break;
1160 case elfcpp::R_X86_64_16:
1161 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1162 break;
1164 case elfcpp::R_X86_64_PC16:
1165 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1166 address);
1167 break;
1169 case elfcpp::R_X86_64_8:
1170 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1171 break;
1173 case elfcpp::R_X86_64_PC8:
1174 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1175 address);
1176 break;
1178 case elfcpp::R_X86_64_PLT32:
1179 gold_assert(gsym->has_plt_offset()
1180 || gsym->final_value_is_known());
1181 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1182 address);
1183 break;
1185 case elfcpp::R_X86_64_GOT32:
1186 // Local GOT offsets not yet supported.
1187 gold_assert(gsym);
1188 gold_assert(gsym->has_got_offset());
1189 Relocate_functions<64, false>::rela32(view, gsym->got_offset(), addend);
1190 break;
1192 case elfcpp::R_X86_64_GOTPC32:
1194 gold_assert(gsym);
1195 elfcpp::Elf_types<64>::Elf_Addr value;
1196 value = target->got_section(NULL, NULL)->address();
1197 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1199 break;
1201 case elfcpp::R_X86_64_GOT64:
1202 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1203 // Since we always add a PLT entry, this is equivalent.
1204 case elfcpp::R_X86_64_GOTPLT64: // TODO(csilvers): correct?
1205 // Local GOT offsets not yet supported.
1206 gold_assert(gsym);
1207 gold_assert(gsym->has_got_offset());
1208 Relocate_functions<64, false>::rela64(view, gsym->got_offset(), addend);
1209 break;
1211 case elfcpp::R_X86_64_GOTPC64:
1213 gold_assert(gsym);
1214 elfcpp::Elf_types<64>::Elf_Addr value;
1215 value = target->got_section(NULL, NULL)->address();
1216 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1218 break;
1220 case elfcpp::R_X86_64_GOTOFF64:
1222 elfcpp::Elf_types<64>::Elf_Addr value;
1223 value = (psymval->value(object, 0)
1224 - target->got_section(NULL, NULL)->address());
1225 Relocate_functions<64, false>::rela64(view, value, addend);
1227 break;
1229 case elfcpp::R_X86_64_GOTPCREL:
1231 gold_assert(gsym);
1232 elfcpp::Elf_types<64>::Elf_Addr value;
1233 // FIXME(csilvers): this is probably totally wrong for G + GOT
1234 value = (target->got_section(NULL, NULL)->address()
1235 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1236 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1238 break;
1240 case elfcpp::R_X86_64_GOTPCREL64:
1242 gold_assert(gsym);
1243 elfcpp::Elf_types<64>::Elf_Addr value;
1244 // FIXME(csilvers): this is probably totally wrong for G + GOT
1245 value = (target->got_section(NULL, NULL)->address()
1246 + (gsym->has_got_offset() ? gsym->got_offset() : 0));
1247 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1249 break;
1251 case elfcpp::R_X86_64_COPY:
1252 case elfcpp::R_X86_64_GLOB_DAT:
1253 case elfcpp::R_X86_64_JUMP_SLOT:
1254 case elfcpp::R_X86_64_RELATIVE:
1255 case elfcpp::R_X86_64_TPOFF64:
1256 case elfcpp::R_X86_64_TPOFF32:
1257 case elfcpp::R_X86_64_DTPMOD64:
1258 case elfcpp::R_X86_64_DTPOFF64:
1259 case elfcpp::R_X86_64_DTPOFF32:
1260 case elfcpp::R_X86_64_GOTTPOFF: // TODO(csilvers): correct?
1261 case elfcpp::R_X86_64_TLSDESC:
1262 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // TODO(csilvers): correct?
1263 case elfcpp::R_X86_64_TLSDESC_CALL: // TODO(csilvers): correct?
1264 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1265 program_name,
1266 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1267 r_type);
1268 gold_exit(false);
1269 break;
1271 #if 0
1272 case elfcpp::R_X86_64_TLSGD: // TODO(csilvers): correct?
1273 case elfcpp::R_X86_64_TLSLD: // TODO(csilvers): correct?
1275 case elfcpp::R_386_TLS_IE:
1276 case elfcpp::R_386_TLS_GOTIE:
1277 case elfcpp::R_386_TLS_LE:
1278 case elfcpp::R_386_TLS_GD:
1279 case elfcpp::R_386_TLS_LDM:
1280 case elfcpp::R_386_TLS_LDO_64:
1281 case elfcpp::R_386_TLS_IE_64:
1282 case elfcpp::R_386_TLS_LE_64:
1283 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1284 address, view_size);
1285 break;
1286 #else
1287 view_size++; // this is to make view_size used
1288 #endif
1290 case elfcpp::R_X86_64_SIZE32: // TODO(csilvers): correct?
1291 case elfcpp::R_X86_64_SIZE64: // TODO(csilvers): correct?
1292 case elfcpp::R_X86_64_PLTOFF64: // TODO(csilvers): implement me!
1293 #if 0
1294 case elfcpp::R_386_TLS_GD_64:
1295 case elfcpp::R_386_TLS_GD_PUSH:
1296 case elfcpp::R_386_TLS_GD_CALL:
1297 case elfcpp::R_386_TLS_GD_POP:
1298 case elfcpp::R_386_TLS_LDM_64:
1299 case elfcpp::R_386_TLS_LDM_PUSH:
1300 case elfcpp::R_386_TLS_LDM_CALL:
1301 case elfcpp::R_386_TLS_LDM_POP:
1302 #endif
1303 default:
1304 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1305 program_name,
1306 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1307 r_type);
1308 // gold_exit(false);
1309 break;
1312 return true;
1315 // Perform a TLS relocation.
1317 inline void
1318 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>*, // relinfo,
1319 size_t , // relnum,
1320 const elfcpp::Rela<64, false>& , // rel,
1321 unsigned int , // r_type,
1322 const Sized_symbol<64>* , // gsym,
1323 const Symbol_value<64>* , // psymval,
1324 unsigned char* , // view,
1325 elfcpp::Elf_types<64>::Elf_Addr,
1326 off_t)// view_size)
1328 #if 0
1329 Output_segment* tls_segment = relinfo->layout->tls_segment();
1330 if (tls_segment == NULL)
1332 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1333 program_name,
1334 relinfo->location(relnum, rel.get_r_offset()).c_str());
1335 gold_exit(false);
1338 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1340 const bool is_final = (gsym == NULL
1341 ? !parameters->output_is_shared()
1342 : gsym->final_value_is_known());
1343 const unsigned int opt_r_type =
1344 Target_x86_64::optimize_tls_reloc(is_final, r_type);
1345 switch (r_type)
1347 case elfcpp::R_X86_64_TLS_LE_64:
1348 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1349 Relocate_functions<64, false>::rel64(view, value);
1350 break;
1352 case elfcpp::R_X86_64_TLS_LE:
1353 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1354 Relocate_functions<64, false>::rel64(view, value);
1355 break;
1357 case elfcpp::R_X86_64_TLS_IE:
1358 case elfcpp::R_X86_64_TLS_GOTIE:
1359 case elfcpp::R_X86_64_TLS_IE_64:
1360 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1362 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1363 rel, r_type, value, view,
1364 view_size);
1365 break;
1367 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1368 program_name,
1369 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1370 r_type);
1371 // gold_exit(false);
1372 break;
1374 case elfcpp::R_X86_64_TLS_GD:
1375 if (opt_r_type == elfcpp::R_X86_64_TLS_LE_64)
1377 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1378 rel, r_type, value, view,
1379 view_size);
1380 break;
1382 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1383 program_name,
1384 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1385 r_type);
1386 // gold_exit(false);
1387 break;
1389 case elfcpp::R_X86_64_TLS_LDM:
1390 case elfcpp::R_X86_64_TLS_LDO_64:
1391 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1392 program_name,
1393 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1394 r_type);
1395 // gold_exit(false);
1396 break;
1398 #endif
1401 // Do a relocation in which we convert a TLS Initial-Exec to a
1402 // Local-Exec.
1404 inline void
1405 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* , // relinfo,
1406 size_t , // relnum,
1407 Output_segment* , // tls_segment,
1408 const elfcpp::Rela<64, false>& , // rel,
1409 unsigned int , // r_type,
1410 elfcpp::Elf_types<64>::Elf_Addr , // value,
1411 unsigned char* , // view,
1412 off_t) // view_size)
1414 #if 0
1415 // We have to actually change the instructions, which means that we
1416 // need to examine the opcodes to figure out which instruction we
1417 // are looking at.
1418 if (r_type == elfcpp::R_X86_64_TLS_IE)
1420 // movl %gs:XX,%eax ==> movl $YY,%eax
1421 // movl %gs:XX,%reg ==> movl $YY,%reg
1422 // addl %gs:XX,%reg ==> addl $YY,%reg
1423 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1424 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1426 unsigned char op1 = view[-1];
1427 if (op1 == 0xa1)
1429 // movl XX,%eax ==> movl $YY,%eax
1430 view[-1] = 0xb8;
1432 else
1434 Target_x86_64::Relocate::check_range(relinfo, relnum, rel,
1435 view_size, -2);
1437 unsigned char op2 = view[-2];
1438 if (op2 == 0x8b)
1440 // movl XX,%reg ==> movl $YY,%reg
1441 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1442 (op1 & 0xc7) == 0x05);
1443 view[-2] = 0xc7;
1444 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1446 else if (op2 == 0x03)
1448 // addl XX,%reg ==> addl $YY,%reg
1449 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1450 (op1 & 0xc7) == 0x05);
1451 view[-2] = 0x81;
1452 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1454 else
1455 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1458 else
1460 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1461 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1462 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1463 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1464 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1466 unsigned char op1 = view[-1];
1467 unsigned char op2 = view[-2];
1468 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1469 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1470 if (op2 == 0x8b)
1472 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1473 view[-2] = 0xc7;
1474 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1476 else if (op2 == 0x2b)
1478 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1479 view[-2] = 0x81;
1480 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1482 else if (op2 == 0x03)
1484 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1485 view[-2] = 0x81;
1486 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1488 else
1489 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel, 0);
1492 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1493 if (r_type == elfcpp::R_X86_64_TLS_IE || r_type == elfcpp::R_X86_64_TLS_GOTIE)
1494 value = - value;
1496 Relocate_functions<64, false>::rel64(view, value);
1497 #endif
1500 // Do a relocation in which we convert a TLS Global-Dynamic to a
1501 // Local-Exec.
1503 inline void
1504 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* , // relinfo,
1505 size_t , // relnum,
1506 Output_segment* , // tls_segment,
1507 const elfcpp::Rela<64, false>& , // rel,
1508 unsigned int,
1509 elfcpp::Elf_types<64>::Elf_Addr , // value,
1510 unsigned char* , // view,
1511 off_t)// view_size)
1513 #if 0
1514 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1515 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1516 // leal foo(%reg),%eax; call ___tls_get_addr
1517 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1519 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1520 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1522 unsigned char op1 = view[-1];
1523 unsigned char op2 = view[-2];
1525 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1526 op2 == 0x8d || op2 == 0x04);
1527 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1528 view[4] == 0xe8);
1530 int roff = 5;
1532 if (op2 == 0x04)
1534 Target_x86_64::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1535 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1536 view[-3] == 0x8d);
1537 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1538 ((op1 & 0xc7) == 0x05
1539 && op1 != (4 << 3)));
1540 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1542 else
1544 Target_x86_64::Relocate::check_tls(relinfo, relnum, rel,
1545 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1546 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1548 // There is a trailing nop. Use the size byte subl.
1549 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1550 roff = 6;
1552 else
1554 // Use the five byte subl.
1555 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1559 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1560 Relocate_functions<64, false>::rel64(view + roff, value);
1562 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1563 // We can skip it.
1564 this->skip_call_tls_get_addr_ = true;
1565 #endif
1568 // Check the range for a TLS relocation.
1570 inline void
1571 Target_x86_64::Relocate::check_range(const Relocate_info<64, false>* relinfo,
1572 size_t relnum,
1573 const elfcpp::Rela<64, false>& rel,
1574 off_t view_size, off_t off)
1576 off_t offset = rel.get_r_offset() + off;
1577 if (offset < 0 || offset > view_size)
1579 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1580 program_name,
1581 relinfo->location(relnum, rel.get_r_offset()).c_str());
1582 gold_exit(false);
1586 // Check the validity of a TLS relocation. This is like assert.
1588 inline void
1589 Target_x86_64::Relocate::check_tls(const Relocate_info<64, false>* relinfo,
1590 size_t relnum,
1591 const elfcpp::Rela<64, false>& rel,
1592 bool valid)
1594 if (!valid)
1596 fprintf(stderr,
1597 _("%s: %s: TLS relocation against invalid instruction\n"),
1598 program_name,
1599 relinfo->location(relnum, rel.get_r_offset()).c_str());
1600 gold_exit(false);
1604 // Relocate section data.
1606 void
1607 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1608 unsigned int sh_type,
1609 const unsigned char* prelocs,
1610 size_t reloc_count,
1611 unsigned char* view,
1612 elfcpp::Elf_types<64>::Elf_Addr address,
1613 off_t view_size)
1615 gold_assert(sh_type == elfcpp::SHT_RELA);
1617 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1618 Target_x86_64::Relocate>(
1619 relinfo,
1620 this,
1621 prelocs,
1622 reloc_count,
1623 view,
1624 address,
1625 view_size);
1628 // Return the value to use for a dynamic which requires special
1629 // treatment. This is how we support equality comparisons of function
1630 // pointers across shared library boundaries, as described in the
1631 // processor specific ABI supplement.
1633 uint64_t
1634 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1636 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1637 return this->plt_section()->address() + gsym->plt_offset();
1640 // Return a string used to fill a code section with nops to take up
1641 // the specified length.
1643 std::string
1644 Target_x86_64::do_code_fill(off_t length)
1646 if (length >= 16)
1648 // Build a jmpq instruction to skip over the bytes.
1649 unsigned char jmp[5];
1650 jmp[0] = 0xe9;
1651 elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1652 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1653 + std::string(length - 5, '\0'));
1656 // Nop sequences of various lengths.
1657 const char nop1[1] = { 0x90 }; // nop
1658 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1659 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1660 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1661 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1662 0x00 }; // leal 0(%esi,1),%esi
1663 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1664 0x00, 0x00 };
1665 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1666 0x00, 0x00, 0x00 };
1667 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1668 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1669 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1670 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1671 0x00 };
1672 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1673 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1674 0x00, 0x00 };
1675 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1676 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1677 0x00, 0x00, 0x00 };
1678 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1679 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1680 0x00, 0x00, 0x00, 0x00 };
1681 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1682 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1683 0x27, 0x00, 0x00, 0x00,
1684 0x00 };
1685 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1686 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1687 0xbc, 0x27, 0x00, 0x00,
1688 0x00, 0x00 };
1689 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1690 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1691 0x90, 0x90, 0x90, 0x90,
1692 0x90, 0x90, 0x90 };
1694 const char* nops[16] = {
1695 NULL,
1696 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1697 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1700 return std::string(nops[length], length);
1703 // The selector for x86_64 object files.
1705 class Target_selector_x86_64 : public Target_selector
1707 public:
1708 Target_selector_x86_64()
1709 : Target_selector(elfcpp::EM_X86_64, 64, false)
1712 Target*
1713 recognize(int machine, int osabi, int abiversion);
1715 private:
1716 Target_x86_64* target_;
1719 // Recognize an x86_64 object file when we already know that the machine
1720 // number is EM_X86_64.
1722 Target*
1723 Target_selector_x86_64::recognize(int, int, int)
1725 if (this->target_ == NULL)
1726 this->target_ = new Target_x86_64();
1727 return this->target_;
1730 Target_selector_x86_64 target_selector_x86_64;
1732 } // End anonymous namespace.