PR binutils/4334
[binutils.git] / gold / x86_64.cc
blob973787fe7d14c96d47af02368ef0aa2d4c5cc038
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 "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
40 namespace
43 using namespace gold;
45 class Output_data_plt_x86_64;
47 // The x86_64 target class.
48 // See the ABI at
49 // http://www.x86-64.org/documentation/abi.pdf
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_x86_64 : public Sized_target<64, false>
56 public:
57 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
58 // uses only Elf64_Rela relocation entries with explicit addends."
59 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
61 Target_x86_64()
62 : Sized_target<64, false>(&x86_64_info),
63 got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
64 copy_relocs_(NULL), dynbss_(NULL), got_mod_index_offset_(-1U)
65 { }
67 // Scan the relocations to look for symbol adjustments.
68 void
69 scan_relocs(const General_options& options,
70 Symbol_table* symtab,
71 Layout* layout,
72 Sized_relobj<64, false>* object,
73 unsigned int data_shndx,
74 unsigned int sh_type,
75 const unsigned char* prelocs,
76 size_t reloc_count,
77 Output_section* output_section,
78 bool needs_special_offset_handling,
79 size_t local_symbol_count,
80 const unsigned char* plocal_symbols);
82 // Finalize the sections.
83 void
84 do_finalize_sections(Layout*);
86 // Return the value to use for a dynamic which requires special
87 // treatment.
88 uint64_t
89 do_dynsym_value(const Symbol*) const;
91 // Relocate a section.
92 void
93 relocate_section(const Relocate_info<64, false>*,
94 unsigned int sh_type,
95 const unsigned char* prelocs,
96 size_t reloc_count,
97 Output_section* output_section,
98 bool needs_special_offset_handling,
99 unsigned char* view,
100 elfcpp::Elf_types<64>::Elf_Addr view_address,
101 section_size_type view_size);
103 // Scan the relocs during a relocatable link.
104 void
105 scan_relocatable_relocs(const General_options& options,
106 Symbol_table* symtab,
107 Layout* layout,
108 Sized_relobj<64, false>* object,
109 unsigned int data_shndx,
110 unsigned int sh_type,
111 const unsigned char* prelocs,
112 size_t reloc_count,
113 Output_section* output_section,
114 bool needs_special_offset_handling,
115 size_t local_symbol_count,
116 const unsigned char* plocal_symbols,
117 Relocatable_relocs*);
119 // Relocate a section during a relocatable link.
120 void
121 relocate_for_relocatable(const Relocate_info<64, false>*,
122 unsigned int sh_type,
123 const unsigned char* prelocs,
124 size_t reloc_count,
125 Output_section* output_section,
126 off_t offset_in_output_section,
127 const Relocatable_relocs*,
128 unsigned char* view,
129 elfcpp::Elf_types<64>::Elf_Addr view_address,
130 section_size_type view_size,
131 unsigned char* reloc_view,
132 section_size_type reloc_view_size);
134 // Return a string used to fill a code section with nops.
135 std::string
136 do_code_fill(section_size_type length) const;
138 // Return whether SYM is defined by the ABI.
139 bool
140 do_is_defined_by_abi(Symbol* sym) const
141 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
143 // Return the size of the GOT section.
144 section_size_type
145 got_size()
147 gold_assert(this->got_ != NULL);
148 return this->got_->data_size();
151 private:
152 // The class which scans relocations.
153 struct Scan
155 inline void
156 local(const General_options& options, Symbol_table* symtab,
157 Layout* layout, Target_x86_64* target,
158 Sized_relobj<64, false>* object,
159 unsigned int data_shndx,
160 Output_section* output_section,
161 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
162 const elfcpp::Sym<64, false>& lsym);
164 inline void
165 global(const General_options& options, Symbol_table* symtab,
166 Layout* layout, Target_x86_64* target,
167 Sized_relobj<64, false>* object,
168 unsigned int data_shndx,
169 Output_section* output_section,
170 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
171 Symbol* gsym);
173 static void
174 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
176 static void
177 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
178 Symbol*);
181 // The class which implements relocation.
182 class Relocate
184 public:
185 Relocate()
186 : skip_call_tls_get_addr_(false)
189 ~Relocate()
191 if (this->skip_call_tls_get_addr_)
193 // FIXME: This needs to specify the location somehow.
194 gold_error(_("missing expected TLS relocation"));
198 // Do a relocation. Return false if the caller should not issue
199 // any warnings about this relocation.
200 inline bool
201 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
202 const elfcpp::Rela<64, false>&,
203 unsigned int r_type, const Sized_symbol<64>*,
204 const Symbol_value<64>*,
205 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
206 section_size_type);
208 private:
209 // Do a TLS relocation.
210 inline void
211 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
212 size_t relnum, const elfcpp::Rela<64, false>&,
213 unsigned int r_type, const Sized_symbol<64>*,
214 const Symbol_value<64>*,
215 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
216 section_size_type);
218 // Do a TLS General-Dynamic to Local-Exec transition.
219 inline void
220 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
221 Output_segment* tls_segment,
222 const elfcpp::Rela<64, false>&, unsigned int r_type,
223 elfcpp::Elf_types<64>::Elf_Addr value,
224 unsigned char* view,
225 section_size_type view_size);
227 // Do a TLS General-Dynamic to Local-Exec transition.
228 inline void
229 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
230 Output_segment* tls_segment,
231 const elfcpp::Rela<64, false>&, unsigned int r_type,
232 elfcpp::Elf_types<64>::Elf_Addr value,
233 unsigned char* view,
234 section_size_type view_size);
236 // Do a TLS Local-Dynamic to Local-Exec transition.
237 inline void
238 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
239 Output_segment* tls_segment,
240 const elfcpp::Rela<64, false>&, unsigned int r_type,
241 elfcpp::Elf_types<64>::Elf_Addr value,
242 unsigned char* view,
243 section_size_type view_size);
245 // Do a TLS Initial-Exec to Local-Exec transition.
246 static inline void
247 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
248 Output_segment* tls_segment,
249 const elfcpp::Rela<64, false>&, unsigned int r_type,
250 elfcpp::Elf_types<64>::Elf_Addr value,
251 unsigned char* view,
252 section_size_type view_size);
254 // This is set if we should skip the next reloc, which should be a
255 // PLT32 reloc against ___tls_get_addr.
256 bool skip_call_tls_get_addr_;
259 // A class which returns the size required for a relocation type,
260 // used while scanning relocs during a relocatable link.
261 class Relocatable_size_for_reloc
263 public:
264 unsigned int
265 get_size_for_reloc(unsigned int, Relobj*);
268 // Adjust TLS relocation type based on the options and whether this
269 // is a local symbol.
270 static tls::Tls_optimization
271 optimize_tls_reloc(bool is_final, int r_type);
273 // Get the GOT section, creating it if necessary.
274 Output_data_got<64, false>*
275 got_section(Symbol_table*, Layout*);
277 // Get the GOT PLT section.
278 Output_data_space*
279 got_plt_section() const
281 gold_assert(this->got_plt_ != NULL);
282 return this->got_plt_;
285 // Create a PLT entry for a global symbol.
286 void
287 make_plt_entry(Symbol_table*, Layout*, Symbol*);
289 // Create a GOT entry for the TLS module index.
290 unsigned int
291 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
292 Sized_relobj<64, false>* object);
294 // Get the PLT section.
295 Output_data_plt_x86_64*
296 plt_section() const
298 gold_assert(this->plt_ != NULL);
299 return this->plt_;
302 // Get the dynamic reloc section, creating it if necessary.
303 Reloc_section*
304 rela_dyn_section(Layout*);
306 // Return true if the symbol may need a COPY relocation.
307 // References from an executable object to non-function symbols
308 // defined in a dynamic object may need a COPY relocation.
309 bool
310 may_need_copy_reloc(Symbol* gsym)
312 return (!parameters->options().shared()
313 && gsym->is_from_dynobj()
314 && gsym->type() != elfcpp::STT_FUNC);
317 // Copy a relocation against a global symbol.
318 void
319 copy_reloc(const General_options*, Symbol_table*, Layout*,
320 Sized_relobj<64, false>*, unsigned int,
321 Output_section*, Symbol*, const elfcpp::Rela<64, false>&);
323 // Information about this specific target which we pass to the
324 // general Target structure.
325 static const Target::Target_info x86_64_info;
327 enum Got_type
329 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
330 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
331 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
332 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
335 // The GOT section.
336 Output_data_got<64, false>* got_;
337 // The PLT section.
338 Output_data_plt_x86_64* plt_;
339 // The GOT PLT section.
340 Output_data_space* got_plt_;
341 // The dynamic reloc section.
342 Reloc_section* rela_dyn_;
343 // Relocs saved to avoid a COPY reloc.
344 Copy_relocs<64, false>* copy_relocs_;
345 // Space for variables copied with a COPY reloc.
346 Output_data_space* dynbss_;
347 // Offset of the GOT entry for the TLS module index;
348 unsigned int got_mod_index_offset_;
351 const Target::Target_info Target_x86_64::x86_64_info =
353 64, // size
354 false, // is_big_endian
355 elfcpp::EM_X86_64, // machine_code
356 false, // has_make_symbol
357 false, // has_resolve
358 true, // has_code_fill
359 true, // is_default_stack_executable
360 "/lib/ld64.so.1", // program interpreter
361 0x400000, // default_text_segment_address
362 0x1000, // abi_pagesize (overridable by -z max-page-size)
363 0x1000 // common_pagesize (overridable by -z common-page-size)
366 // Get the GOT section, creating it if necessary.
368 Output_data_got<64, false>*
369 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
371 if (this->got_ == NULL)
373 gold_assert(symtab != NULL && layout != NULL);
375 this->got_ = new Output_data_got<64, false>();
377 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
378 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
379 this->got_);
381 // The old GNU linker creates a .got.plt section. We just
382 // create another set of data in the .got section. Note that we
383 // always create a PLT if we create a GOT, although the PLT
384 // might be empty.
385 this->got_plt_ = new Output_data_space(8);
386 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
387 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
388 this->got_plt_);
390 // The first three entries are reserved.
391 this->got_plt_->set_current_data_size(3 * 8);
393 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
394 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
395 this->got_plt_,
396 0, 0, elfcpp::STT_OBJECT,
397 elfcpp::STB_LOCAL,
398 elfcpp::STV_HIDDEN, 0,
399 false, false);
402 return this->got_;
405 // Get the dynamic reloc section, creating it if necessary.
407 Target_x86_64::Reloc_section*
408 Target_x86_64::rela_dyn_section(Layout* layout)
410 if (this->rela_dyn_ == NULL)
412 gold_assert(layout != NULL);
413 this->rela_dyn_ = new Reloc_section();
414 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
415 elfcpp::SHF_ALLOC, this->rela_dyn_);
417 return this->rela_dyn_;
420 // A class to handle the PLT data.
422 class Output_data_plt_x86_64 : public Output_section_data
424 public:
425 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
427 Output_data_plt_x86_64(Layout*, Output_data_space*);
429 // Add an entry to the PLT.
430 void
431 add_entry(Symbol* gsym);
433 // Return the .rel.plt section data.
434 const Reloc_section*
435 rel_plt() const
436 { return this->rel_; }
438 protected:
439 void
440 do_adjust_output_section(Output_section* os);
442 private:
443 // The size of an entry in the PLT.
444 static const int plt_entry_size = 16;
446 // The first entry in the PLT.
447 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
448 // procedure linkage table for both programs and shared objects."
449 static unsigned char first_plt_entry[plt_entry_size];
451 // Other entries in the PLT for an executable.
452 static unsigned char plt_entry[plt_entry_size];
454 // Set the final size.
455 void
456 set_final_data_size()
457 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
459 // Write out the PLT data.
460 void
461 do_write(Output_file*);
463 // The reloc section.
464 Reloc_section* rel_;
465 // The .got.plt section.
466 Output_data_space* got_plt_;
467 // The number of PLT entries.
468 unsigned int count_;
471 // Create the PLT section. The ordinary .got section is an argument,
472 // since we need to refer to the start. We also create our own .got
473 // section just for PLT entries.
475 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
476 Output_data_space* got_plt)
477 : Output_section_data(8), got_plt_(got_plt), count_(0)
479 this->rel_ = new Reloc_section();
480 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
481 elfcpp::SHF_ALLOC, this->rel_);
484 void
485 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
487 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
488 // linker, and so do we.
489 os->set_entsize(4);
492 // Add an entry to the PLT.
494 void
495 Output_data_plt_x86_64::add_entry(Symbol* gsym)
497 gold_assert(!gsym->has_plt_offset());
499 // Note that when setting the PLT offset we skip the initial
500 // reserved PLT entry.
501 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
503 ++this->count_;
505 section_offset_type got_offset = this->got_plt_->current_data_size();
507 // Every PLT entry needs a GOT entry which points back to the PLT
508 // entry (this will be changed by the dynamic linker, normally
509 // lazily when the function is called).
510 this->got_plt_->set_current_data_size(got_offset + 8);
512 // Every PLT entry needs a reloc.
513 gsym->set_needs_dynsym_entry();
514 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
515 got_offset, 0);
517 // Note that we don't need to save the symbol. The contents of the
518 // PLT are independent of which symbols are used. The symbols only
519 // appear in the relocations.
522 // The first entry in the PLT for an executable.
524 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
526 // From AMD64 ABI Draft 0.98, page 76
527 0xff, 0x35, // pushq contents of memory address
528 0, 0, 0, 0, // replaced with address of .got + 8
529 0xff, 0x25, // jmp indirect
530 0, 0, 0, 0, // replaced with address of .got + 16
531 0x90, 0x90, 0x90, 0x90 // noop (x4)
534 // Subsequent entries in the PLT for an executable.
536 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
538 // From AMD64 ABI Draft 0.98, page 76
539 0xff, 0x25, // jmpq indirect
540 0, 0, 0, 0, // replaced with address of symbol in .got
541 0x68, // pushq immediate
542 0, 0, 0, 0, // replaced with offset into relocation table
543 0xe9, // jmpq relative
544 0, 0, 0, 0 // replaced with offset to start of .plt
547 // Write out the PLT. This uses the hand-coded instructions above,
548 // and adjusts them as needed. This is specified by the AMD64 ABI.
550 void
551 Output_data_plt_x86_64::do_write(Output_file* of)
553 const off_t offset = this->offset();
554 const section_size_type oview_size =
555 convert_to_section_size_type(this->data_size());
556 unsigned char* const oview = of->get_output_view(offset, oview_size);
558 const off_t got_file_offset = this->got_plt_->offset();
559 const section_size_type got_size =
560 convert_to_section_size_type(this->got_plt_->data_size());
561 unsigned char* const got_view = of->get_output_view(got_file_offset,
562 got_size);
564 unsigned char* pov = oview;
566 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
567 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
569 memcpy(pov, first_plt_entry, plt_entry_size);
570 // We do a jmp relative to the PC at the end of this instruction.
571 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
572 - (plt_address + 6));
573 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
574 - (plt_address + 12));
575 pov += plt_entry_size;
577 unsigned char* got_pov = got_view;
579 memset(got_pov, 0, 24);
580 got_pov += 24;
582 unsigned int plt_offset = plt_entry_size;
583 unsigned int got_offset = 24;
584 const unsigned int count = this->count_;
585 for (unsigned int plt_index = 0;
586 plt_index < count;
587 ++plt_index,
588 pov += plt_entry_size,
589 got_pov += 8,
590 plt_offset += plt_entry_size,
591 got_offset += 8)
593 // Set and adjust the PLT entry itself.
594 memcpy(pov, plt_entry, plt_entry_size);
595 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
596 (got_address + got_offset
597 - (plt_address + plt_offset
598 + 6)));
600 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
601 elfcpp::Swap<32, false>::writeval(pov + 12,
602 - (plt_offset + plt_entry_size));
604 // Set the entry in the GOT.
605 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
608 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
609 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
611 of->write_output_view(offset, oview_size, oview);
612 of->write_output_view(got_file_offset, got_size, got_view);
615 // Create a PLT entry for a global symbol.
617 void
618 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
619 Symbol* gsym)
621 if (gsym->has_plt_offset())
622 return;
624 if (this->plt_ == NULL)
626 // Create the GOT sections first.
627 this->got_section(symtab, layout);
629 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
630 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
631 (elfcpp::SHF_ALLOC
632 | elfcpp::SHF_EXECINSTR),
633 this->plt_);
636 this->plt_->add_entry(gsym);
639 // Create a GOT entry for the TLS module index.
641 unsigned int
642 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
643 Sized_relobj<64, false>* object)
645 if (this->got_mod_index_offset_ == -1U)
647 gold_assert(symtab != NULL && layout != NULL && object != NULL);
648 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
649 Output_data_got<64, false>* got = this->got_section(symtab, layout);
650 unsigned int got_offset = got->add_constant(0);
651 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
652 got_offset, 0);
653 got->add_constant(0);
654 this->got_mod_index_offset_ = got_offset;
656 return this->got_mod_index_offset_;
659 // Handle a relocation against a non-function symbol defined in a
660 // dynamic object. The traditional way to handle this is to generate
661 // a COPY relocation to copy the variable at runtime from the shared
662 // object into the executable's data segment. However, this is
663 // undesirable in general, as if the size of the object changes in the
664 // dynamic object, the executable will no longer work correctly. If
665 // this relocation is in a writable section, then we can create a
666 // dynamic reloc and the dynamic linker will resolve it to the correct
667 // address at runtime. However, we do not want do that if the
668 // relocation is in a read-only section, as it would prevent the
669 // readonly segment from being shared. And if we have to eventually
670 // generate a COPY reloc, then any dynamic relocations will be
671 // useless. So this means that if this is a writable section, we need
672 // to save the relocation until we see whether we have to create a
673 // COPY relocation for this symbol for any other relocation.
675 void
676 Target_x86_64::copy_reloc(const General_options* options,
677 Symbol_table* symtab,
678 Layout* layout,
679 Sized_relobj<64, false>* object,
680 unsigned int data_shndx,
681 Output_section* output_section,
682 Symbol* gsym,
683 const elfcpp::Rela<64, false>& rela)
685 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(gsym);
687 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
688 data_shndx, ssym))
690 // So far we do not need a COPY reloc. Save this relocation.
691 // If it turns out that we never need a COPY reloc for this
692 // symbol, then we will emit the relocation.
693 if (this->copy_relocs_ == NULL)
694 this->copy_relocs_ = new Copy_relocs<64, false>();
695 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
697 else
699 // Allocate space for this symbol in the .bss section.
701 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
703 // There is no defined way to determine the required alignment
704 // of the symbol. We pick the alignment based on the size. We
705 // set an arbitrary maximum of 256.
706 unsigned int align;
707 for (align = 1; align < 512; align <<= 1)
708 if ((symsize & align) != 0)
709 break;
711 if (this->dynbss_ == NULL)
713 this->dynbss_ = new Output_data_space(align);
714 layout->add_output_section_data(".bss",
715 elfcpp::SHT_NOBITS,
716 (elfcpp::SHF_ALLOC
717 | elfcpp::SHF_WRITE),
718 this->dynbss_);
721 Output_data_space* dynbss = this->dynbss_;
723 if (align > dynbss->addralign())
724 dynbss->set_space_alignment(align);
726 section_size_type dynbss_size = dynbss->current_data_size();
727 dynbss_size = align_address(dynbss_size, align);
728 section_size_type offset = dynbss_size;
729 dynbss->set_current_data_size(dynbss_size + symsize);
731 symtab->define_with_copy_reloc(ssym, dynbss, offset);
733 // Add the COPY reloc.
734 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
735 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
740 // Optimize the TLS relocation type based on what we know about the
741 // symbol. IS_FINAL is true if the final address of this symbol is
742 // known at link time.
744 tls::Tls_optimization
745 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
747 // If we are generating a shared library, then we can't do anything
748 // in the linker.
749 if (parameters->options().shared())
750 return tls::TLSOPT_NONE;
752 switch (r_type)
754 case elfcpp::R_X86_64_TLSGD:
755 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
756 case elfcpp::R_X86_64_TLSDESC_CALL:
757 // These are General-Dynamic which permits fully general TLS
758 // access. Since we know that we are generating an executable,
759 // we can convert this to Initial-Exec. If we also know that
760 // this is a local symbol, we can further switch to Local-Exec.
761 if (is_final)
762 return tls::TLSOPT_TO_LE;
763 return tls::TLSOPT_TO_IE;
765 case elfcpp::R_X86_64_TLSLD:
766 // This is Local-Dynamic, which refers to a local symbol in the
767 // dynamic TLS block. Since we know that we generating an
768 // executable, we can switch to Local-Exec.
769 return tls::TLSOPT_TO_LE;
771 case elfcpp::R_X86_64_DTPOFF32:
772 case elfcpp::R_X86_64_DTPOFF64:
773 // Another Local-Dynamic reloc.
774 return tls::TLSOPT_TO_LE;
776 case elfcpp::R_X86_64_GOTTPOFF:
777 // These are Initial-Exec relocs which get the thread offset
778 // from the GOT. If we know that we are linking against the
779 // local symbol, we can switch to Local-Exec, which links the
780 // thread offset into the instruction.
781 if (is_final)
782 return tls::TLSOPT_TO_LE;
783 return tls::TLSOPT_NONE;
785 case elfcpp::R_X86_64_TPOFF32:
786 // When we already have Local-Exec, there is nothing further we
787 // can do.
788 return tls::TLSOPT_NONE;
790 default:
791 gold_unreachable();
795 // Report an unsupported relocation against a local symbol.
797 void
798 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
799 unsigned int r_type)
801 gold_error(_("%s: unsupported reloc %u against local symbol"),
802 object->name().c_str(), r_type);
805 // Scan a relocation for a local symbol.
807 inline void
808 Target_x86_64::Scan::local(const General_options&,
809 Symbol_table* symtab,
810 Layout* layout,
811 Target_x86_64* target,
812 Sized_relobj<64, false>* object,
813 unsigned int data_shndx,
814 Output_section* output_section,
815 const elfcpp::Rela<64, false>& reloc,
816 unsigned int r_type,
817 const elfcpp::Sym<64, false>& lsym)
819 switch (r_type)
821 case elfcpp::R_X86_64_NONE:
822 case elfcpp::R_386_GNU_VTINHERIT:
823 case elfcpp::R_386_GNU_VTENTRY:
824 break;
826 case elfcpp::R_X86_64_64:
827 // If building a shared library (or a position-independent
828 // executable), we need to create a dynamic relocation for this
829 // location. The relocation applied at link time will apply the
830 // link-time value, so we flag the location with an
831 // R_X86_64_RELATIVE relocation so the dynamic loader can
832 // relocate it easily.
833 if (parameters->options().output_is_position_independent())
835 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
836 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
837 rela_dyn->add_local_relative(object, r_sym,
838 elfcpp::R_X86_64_RELATIVE,
839 output_section, data_shndx,
840 reloc.get_r_offset(),
841 reloc.get_r_addend());
843 break;
845 case elfcpp::R_X86_64_32:
846 case elfcpp::R_X86_64_32S:
847 case elfcpp::R_X86_64_16:
848 case elfcpp::R_X86_64_8:
849 // If building a shared library (or a position-independent
850 // executable), we need to create a dynamic relocation for this
851 // location. We can't use an R_X86_64_RELATIVE relocation
852 // because that is always a 64-bit relocation.
853 if (parameters->options().output_is_position_independent())
855 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
856 if (lsym.get_st_type() != elfcpp::STT_SECTION)
858 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
859 rela_dyn->add_local(object, r_sym, r_type, output_section,
860 data_shndx, reloc.get_r_offset(),
861 reloc.get_r_addend());
863 else
865 gold_assert(lsym.get_st_value() == 0);
866 rela_dyn->add_local_section(object, lsym.get_st_shndx(),
867 r_type, output_section,
868 data_shndx, reloc.get_r_offset(),
869 reloc.get_r_addend());
872 break;
874 case elfcpp::R_X86_64_PC64:
875 case elfcpp::R_X86_64_PC32:
876 case elfcpp::R_X86_64_PC16:
877 case elfcpp::R_X86_64_PC8:
878 break;
880 case elfcpp::R_X86_64_PLT32:
881 // Since we know this is a local symbol, we can handle this as a
882 // PC32 reloc.
883 break;
885 case elfcpp::R_X86_64_GOTPC32:
886 case elfcpp::R_X86_64_GOTOFF64:
887 case elfcpp::R_X86_64_GOTPC64:
888 case elfcpp::R_X86_64_PLTOFF64:
889 // We need a GOT section.
890 target->got_section(symtab, layout);
891 // For PLTOFF64, we'd normally want a PLT section, but since we
892 // know this is a local symbol, no PLT is needed.
893 break;
895 case elfcpp::R_X86_64_GOT64:
896 case elfcpp::R_X86_64_GOT32:
897 case elfcpp::R_X86_64_GOTPCREL64:
898 case elfcpp::R_X86_64_GOTPCREL:
899 case elfcpp::R_X86_64_GOTPLT64:
901 // The symbol requires a GOT entry.
902 Output_data_got<64, false>* got = target->got_section(symtab, layout);
903 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
904 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
906 // If we are generating a shared object, we need to add a
907 // dynamic relocation for this symbol's GOT entry.
908 if (parameters->options().output_is_position_independent())
910 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
911 // R_X86_64_RELATIVE assumes a 64-bit relocation.
912 if (r_type != elfcpp::R_X86_64_GOT32)
913 rela_dyn->add_local_relative(
914 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
915 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
916 else
918 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
919 rela_dyn->add_local(
920 object, r_sym, r_type, got,
921 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
925 // For GOTPLT64, we'd normally want a PLT section, but since
926 // we know this is a local symbol, no PLT is needed.
928 break;
930 case elfcpp::R_X86_64_COPY:
931 case elfcpp::R_X86_64_GLOB_DAT:
932 case elfcpp::R_X86_64_JUMP_SLOT:
933 case elfcpp::R_X86_64_RELATIVE:
934 // These are outstanding tls relocs, which are unexpected when linking
935 case elfcpp::R_X86_64_TPOFF64:
936 case elfcpp::R_X86_64_DTPMOD64:
937 case elfcpp::R_X86_64_TLSDESC:
938 gold_error(_("%s: unexpected reloc %u in object file"),
939 object->name().c_str(), r_type);
940 break;
942 // These are initial tls relocs, which are expected when linking
943 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
944 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
945 case elfcpp::R_X86_64_TLSDESC_CALL:
946 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
947 case elfcpp::R_X86_64_DTPOFF32:
948 case elfcpp::R_X86_64_DTPOFF64:
949 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
950 case elfcpp::R_X86_64_TPOFF32: // Local-exec
952 bool output_is_shared = parameters->options().shared();
953 const tls::Tls_optimization optimized_type
954 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
955 switch (r_type)
957 case elfcpp::R_X86_64_TLSGD: // General-dynamic
958 if (optimized_type == tls::TLSOPT_NONE)
960 // Create a pair of GOT entries for the module index and
961 // dtv-relative offset.
962 Output_data_got<64, false>* got
963 = target->got_section(symtab, layout);
964 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
965 got->add_local_pair_with_rela(object, r_sym,
966 lsym.get_st_shndx(),
967 GOT_TYPE_TLS_PAIR,
968 target->rela_dyn_section(layout),
969 elfcpp::R_X86_64_DTPMOD64, 0);
971 else if (optimized_type != tls::TLSOPT_TO_LE)
972 unsupported_reloc_local(object, r_type);
973 break;
975 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
976 case elfcpp::R_X86_64_TLSDESC_CALL:
977 // FIXME: If not relaxing to LE, we need to generate
978 // a GOT entry with a R_x86_64_TLSDESC reloc.
979 if (optimized_type != tls::TLSOPT_TO_LE)
980 unsupported_reloc_local(object, r_type);
981 break;
983 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
984 if (optimized_type == tls::TLSOPT_NONE)
986 // Create a GOT entry for the module index.
987 target->got_mod_index_entry(symtab, layout, object);
989 else if (optimized_type != tls::TLSOPT_TO_LE)
990 unsupported_reloc_local(object, r_type);
991 break;
993 case elfcpp::R_X86_64_DTPOFF32:
994 case elfcpp::R_X86_64_DTPOFF64:
995 break;
997 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
998 layout->set_has_static_tls();
999 if (optimized_type == tls::TLSOPT_NONE)
1001 // Create a GOT entry for the tp-relative offset.
1002 Output_data_got<64, false>* got
1003 = target->got_section(symtab, layout);
1004 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1005 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1006 target->rela_dyn_section(layout),
1007 elfcpp::R_X86_64_TPOFF64);
1009 else if (optimized_type != tls::TLSOPT_TO_LE)
1010 unsupported_reloc_local(object, r_type);
1011 break;
1013 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1014 layout->set_has_static_tls();
1015 if (output_is_shared)
1016 unsupported_reloc_local(object, r_type);
1017 break;
1019 default:
1020 gold_unreachable();
1023 break;
1025 case elfcpp::R_X86_64_SIZE32:
1026 case elfcpp::R_X86_64_SIZE64:
1027 default:
1028 gold_error(_("%s: unsupported reloc %u against local symbol"),
1029 object->name().c_str(), r_type);
1030 break;
1035 // Report an unsupported relocation against a global symbol.
1037 void
1038 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1039 unsigned int r_type,
1040 Symbol* gsym)
1042 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1043 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1046 // Scan a relocation for a global symbol.
1048 inline void
1049 Target_x86_64::Scan::global(const General_options& options,
1050 Symbol_table* symtab,
1051 Layout* layout,
1052 Target_x86_64* target,
1053 Sized_relobj<64, false>* object,
1054 unsigned int data_shndx,
1055 Output_section* output_section,
1056 const elfcpp::Rela<64, false>& reloc,
1057 unsigned int r_type,
1058 Symbol* gsym)
1060 switch (r_type)
1062 case elfcpp::R_X86_64_NONE:
1063 case elfcpp::R_386_GNU_VTINHERIT:
1064 case elfcpp::R_386_GNU_VTENTRY:
1065 break;
1067 case elfcpp::R_X86_64_64:
1068 case elfcpp::R_X86_64_32:
1069 case elfcpp::R_X86_64_32S:
1070 case elfcpp::R_X86_64_16:
1071 case elfcpp::R_X86_64_8:
1073 // Make a PLT entry if necessary.
1074 if (gsym->needs_plt_entry())
1076 target->make_plt_entry(symtab, layout, gsym);
1077 // Since this is not a PC-relative relocation, we may be
1078 // taking the address of a function. In that case we need to
1079 // set the entry in the dynamic symbol table to the address of
1080 // the PLT entry.
1081 if (gsym->is_from_dynobj() && !parameters->options().shared())
1082 gsym->set_needs_dynsym_value();
1084 // Make a dynamic relocation if necessary.
1085 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1087 if (target->may_need_copy_reloc(gsym))
1089 target->copy_reloc(&options, symtab, layout, object,
1090 data_shndx, output_section, gsym, reloc);
1092 else if (r_type == elfcpp::R_X86_64_64
1093 && gsym->can_use_relative_reloc(false))
1095 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1096 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1097 output_section, object,
1098 data_shndx, reloc.get_r_offset(),
1099 reloc.get_r_addend());
1101 else
1103 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1104 rela_dyn->add_global(gsym, r_type, output_section, object,
1105 data_shndx, reloc.get_r_offset(),
1106 reloc.get_r_addend());
1110 break;
1112 case elfcpp::R_X86_64_PC64:
1113 case elfcpp::R_X86_64_PC32:
1114 case elfcpp::R_X86_64_PC16:
1115 case elfcpp::R_X86_64_PC8:
1117 // Make a PLT entry if necessary.
1118 if (gsym->needs_plt_entry())
1119 target->make_plt_entry(symtab, layout, gsym);
1120 // Make a dynamic relocation if necessary.
1121 int flags = Symbol::NON_PIC_REF;
1122 if (gsym->type() == elfcpp::STT_FUNC)
1123 flags |= Symbol::FUNCTION_CALL;
1124 if (gsym->needs_dynamic_reloc(flags))
1126 if (target->may_need_copy_reloc(gsym))
1128 target->copy_reloc(&options, symtab, layout, object,
1129 data_shndx, output_section, gsym, reloc);
1131 else
1133 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1134 rela_dyn->add_global(gsym, r_type, output_section, object,
1135 data_shndx, reloc.get_r_offset(),
1136 reloc.get_r_addend());
1140 break;
1142 case elfcpp::R_X86_64_GOT64:
1143 case elfcpp::R_X86_64_GOT32:
1144 case elfcpp::R_X86_64_GOTPCREL64:
1145 case elfcpp::R_X86_64_GOTPCREL:
1146 case elfcpp::R_X86_64_GOTPLT64:
1148 // The symbol requires a GOT entry.
1149 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1150 if (gsym->final_value_is_known())
1151 got->add_global(gsym, GOT_TYPE_STANDARD);
1152 else
1154 // If this symbol is not fully resolved, we need to add a
1155 // dynamic relocation for it.
1156 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1157 if (gsym->is_from_dynobj()
1158 || gsym->is_undefined()
1159 || gsym->is_preemptible())
1160 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1161 elfcpp::R_X86_64_GLOB_DAT);
1162 else
1164 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1165 rela_dyn->add_global_relative(
1166 gsym, elfcpp::R_X86_64_RELATIVE, got,
1167 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1170 // For GOTPLT64, we also need a PLT entry (but only if the
1171 // symbol is not fully resolved).
1172 if (r_type == elfcpp::R_X86_64_GOTPLT64
1173 && !gsym->final_value_is_known())
1174 target->make_plt_entry(symtab, layout, gsym);
1176 break;
1178 case elfcpp::R_X86_64_PLT32:
1179 // If the symbol is fully resolved, this is just a PC32 reloc.
1180 // Otherwise we need a PLT entry.
1181 if (gsym->final_value_is_known())
1182 break;
1183 // If building a shared library, we can also skip the PLT entry
1184 // if the symbol is defined in the output file and is protected
1185 // or hidden.
1186 if (gsym->is_defined()
1187 && !gsym->is_from_dynobj()
1188 && !gsym->is_preemptible())
1189 break;
1190 target->make_plt_entry(symtab, layout, gsym);
1191 break;
1193 case elfcpp::R_X86_64_GOTPC32:
1194 case elfcpp::R_X86_64_GOTOFF64:
1195 case elfcpp::R_X86_64_GOTPC64:
1196 case elfcpp::R_X86_64_PLTOFF64:
1197 // We need a GOT section.
1198 target->got_section(symtab, layout);
1199 // For PLTOFF64, we also need a PLT entry (but only if the
1200 // symbol is not fully resolved).
1201 if (r_type == elfcpp::R_X86_64_PLTOFF64
1202 && !gsym->final_value_is_known())
1203 target->make_plt_entry(symtab, layout, gsym);
1204 break;
1206 case elfcpp::R_X86_64_COPY:
1207 case elfcpp::R_X86_64_GLOB_DAT:
1208 case elfcpp::R_X86_64_JUMP_SLOT:
1209 case elfcpp::R_X86_64_RELATIVE:
1210 // These are outstanding tls relocs, which are unexpected when linking
1211 case elfcpp::R_X86_64_TPOFF64:
1212 case elfcpp::R_X86_64_DTPMOD64:
1213 case elfcpp::R_X86_64_TLSDESC:
1214 gold_error(_("%s: unexpected reloc %u in object file"),
1215 object->name().c_str(), r_type);
1216 break;
1218 // These are initial tls relocs, which are expected for global()
1219 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1220 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1221 case elfcpp::R_X86_64_TLSDESC_CALL:
1222 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1223 case elfcpp::R_X86_64_DTPOFF32:
1224 case elfcpp::R_X86_64_DTPOFF64:
1225 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1226 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1228 const bool is_final = gsym->final_value_is_known();
1229 const tls::Tls_optimization optimized_type
1230 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1231 switch (r_type)
1233 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1234 if (optimized_type == tls::TLSOPT_NONE)
1236 // Create a pair of GOT entries for the module index and
1237 // dtv-relative offset.
1238 Output_data_got<64, false>* got
1239 = target->got_section(symtab, layout);
1240 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1241 target->rela_dyn_section(layout),
1242 elfcpp::R_X86_64_DTPMOD64,
1243 elfcpp::R_X86_64_DTPOFF64);
1245 else if (optimized_type == tls::TLSOPT_TO_IE)
1247 // Create a GOT entry for the tp-relative offset.
1248 Output_data_got<64, false>* got
1249 = target->got_section(symtab, layout);
1250 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1251 target->rela_dyn_section(layout),
1252 elfcpp::R_X86_64_TPOFF64);
1254 else if (optimized_type != tls::TLSOPT_TO_LE)
1255 unsupported_reloc_global(object, r_type, gsym);
1256 break;
1258 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1259 case elfcpp::R_X86_64_TLSDESC_CALL:
1260 // FIXME: If not relaxing to LE, we need to generate
1261 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1262 if (optimized_type != tls::TLSOPT_TO_LE)
1263 unsupported_reloc_global(object, r_type, gsym);
1264 break;
1266 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1267 if (optimized_type == tls::TLSOPT_NONE)
1269 // Create a GOT entry for the module index.
1270 target->got_mod_index_entry(symtab, layout, object);
1272 else if (optimized_type != tls::TLSOPT_TO_LE)
1273 unsupported_reloc_global(object, r_type, gsym);
1274 break;
1276 case elfcpp::R_X86_64_DTPOFF32:
1277 case elfcpp::R_X86_64_DTPOFF64:
1278 break;
1280 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1281 layout->set_has_static_tls();
1282 if (optimized_type == tls::TLSOPT_NONE)
1284 // Create a GOT entry for the tp-relative offset.
1285 Output_data_got<64, false>* got
1286 = target->got_section(symtab, layout);
1287 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1288 target->rela_dyn_section(layout),
1289 elfcpp::R_X86_64_TPOFF64);
1291 else if (optimized_type != tls::TLSOPT_TO_LE)
1292 unsupported_reloc_global(object, r_type, gsym);
1293 break;
1295 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1296 layout->set_has_static_tls();
1297 if (parameters->options().shared())
1298 unsupported_reloc_local(object, r_type);
1299 break;
1301 default:
1302 gold_unreachable();
1305 break;
1307 case elfcpp::R_X86_64_SIZE32:
1308 case elfcpp::R_X86_64_SIZE64:
1309 default:
1310 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1311 object->name().c_str(), r_type,
1312 gsym->demangled_name().c_str());
1313 break;
1317 // Scan relocations for a section.
1319 void
1320 Target_x86_64::scan_relocs(const General_options& options,
1321 Symbol_table* symtab,
1322 Layout* layout,
1323 Sized_relobj<64, false>* object,
1324 unsigned int data_shndx,
1325 unsigned int sh_type,
1326 const unsigned char* prelocs,
1327 size_t reloc_count,
1328 Output_section* output_section,
1329 bool needs_special_offset_handling,
1330 size_t local_symbol_count,
1331 const unsigned char* plocal_symbols)
1333 if (sh_type == elfcpp::SHT_REL)
1335 gold_error(_("%s: unsupported REL reloc section"),
1336 object->name().c_str());
1337 return;
1340 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1341 Target_x86_64::Scan>(
1342 options,
1343 symtab,
1344 layout,
1345 this,
1346 object,
1347 data_shndx,
1348 prelocs,
1349 reloc_count,
1350 output_section,
1351 needs_special_offset_handling,
1352 local_symbol_count,
1353 plocal_symbols);
1356 // Finalize the sections.
1358 void
1359 Target_x86_64::do_finalize_sections(Layout* layout)
1361 // Fill in some more dynamic tags.
1362 Output_data_dynamic* const odyn = layout->dynamic_data();
1363 if (odyn != NULL)
1365 if (this->got_plt_ != NULL)
1366 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1368 if (this->plt_ != NULL)
1370 const Output_data* od = this->plt_->rel_plt();
1371 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1372 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1373 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1376 if (this->rela_dyn_ != NULL)
1378 const Output_data* od = this->rela_dyn_;
1379 odyn->add_section_address(elfcpp::DT_RELA, od);
1380 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1381 odyn->add_constant(elfcpp::DT_RELAENT,
1382 elfcpp::Elf_sizes<64>::rela_size);
1385 if (!parameters->options().shared())
1387 // The value of the DT_DEBUG tag is filled in by the dynamic
1388 // linker at run time, and used by the debugger.
1389 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1393 // Emit any relocs we saved in an attempt to avoid generating COPY
1394 // relocs.
1395 if (this->copy_relocs_ == NULL)
1396 return;
1397 if (this->copy_relocs_->any_to_emit())
1399 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1400 this->copy_relocs_->emit(rela_dyn);
1402 delete this->copy_relocs_;
1403 this->copy_relocs_ = NULL;
1406 // Perform a relocation.
1408 inline bool
1409 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1410 Target_x86_64* target,
1411 size_t relnum,
1412 const elfcpp::Rela<64, false>& rela,
1413 unsigned int r_type,
1414 const Sized_symbol<64>* gsym,
1415 const Symbol_value<64>* psymval,
1416 unsigned char* view,
1417 elfcpp::Elf_types<64>::Elf_Addr address,
1418 section_size_type view_size)
1420 if (this->skip_call_tls_get_addr_)
1422 if (r_type != elfcpp::R_X86_64_PLT32
1423 || gsym == NULL
1424 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1426 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1427 _("missing expected TLS relocation"));
1429 else
1431 this->skip_call_tls_get_addr_ = false;
1432 return false;
1436 // Pick the value to use for symbols defined in shared objects.
1437 Symbol_value<64> symval;
1438 if (gsym != NULL
1439 && (gsym->is_from_dynobj()
1440 || (parameters->options().shared()
1441 && (gsym->is_undefined() || gsym->is_preemptible())))
1442 && gsym->has_plt_offset())
1444 symval.set_output_value(target->plt_section()->address()
1445 + gsym->plt_offset());
1446 psymval = &symval;
1449 const Sized_relobj<64, false>* object = relinfo->object;
1450 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1452 // Get the GOT offset if needed.
1453 // The GOT pointer points to the end of the GOT section.
1454 // We need to subtract the size of the GOT section to get
1455 // the actual offset to use in the relocation.
1456 bool have_got_offset = false;
1457 unsigned int got_offset = 0;
1458 switch (r_type)
1460 case elfcpp::R_X86_64_GOT32:
1461 case elfcpp::R_X86_64_GOT64:
1462 case elfcpp::R_X86_64_GOTPLT64:
1463 case elfcpp::R_X86_64_GOTPCREL:
1464 case elfcpp::R_X86_64_GOTPCREL64:
1465 if (gsym != NULL)
1467 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1468 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1470 else
1472 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1473 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1474 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1475 - target->got_size());
1477 have_got_offset = true;
1478 break;
1480 default:
1481 break;
1484 switch (r_type)
1486 case elfcpp::R_X86_64_NONE:
1487 case elfcpp::R_386_GNU_VTINHERIT:
1488 case elfcpp::R_386_GNU_VTENTRY:
1489 break;
1491 case elfcpp::R_X86_64_64:
1492 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1493 break;
1495 case elfcpp::R_X86_64_PC64:
1496 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1497 address);
1498 break;
1500 case elfcpp::R_X86_64_32:
1501 // FIXME: we need to verify that value + addend fits into 32 bits:
1502 // uint64_t x = value + addend;
1503 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1504 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1505 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1506 break;
1508 case elfcpp::R_X86_64_32S:
1509 // FIXME: we need to verify that value + addend fits into 32 bits:
1510 // int64_t x = value + addend; // note this quantity is signed!
1511 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1512 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1513 break;
1515 case elfcpp::R_X86_64_PC32:
1516 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1517 address);
1518 break;
1520 case elfcpp::R_X86_64_16:
1521 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1522 break;
1524 case elfcpp::R_X86_64_PC16:
1525 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1526 address);
1527 break;
1529 case elfcpp::R_X86_64_8:
1530 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1531 break;
1533 case elfcpp::R_X86_64_PC8:
1534 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1535 address);
1536 break;
1538 case elfcpp::R_X86_64_PLT32:
1539 gold_assert(gsym == NULL
1540 || gsym->has_plt_offset()
1541 || gsym->final_value_is_known()
1542 || (gsym->is_defined()
1543 && !gsym->is_from_dynobj()
1544 && !gsym->is_preemptible()));
1545 // Note: while this code looks the same as for R_X86_64_PC32, it
1546 // behaves differently because psymval was set to point to
1547 // the PLT entry, rather than the symbol, in Scan::global().
1548 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1549 address);
1550 break;
1552 case elfcpp::R_X86_64_PLTOFF64:
1554 gold_assert(gsym);
1555 gold_assert(gsym->has_plt_offset()
1556 || gsym->final_value_is_known());
1557 elfcpp::Elf_types<64>::Elf_Addr got_address;
1558 got_address = target->got_section(NULL, NULL)->address();
1559 Relocate_functions<64, false>::rela64(view, object, psymval,
1560 addend - got_address);
1563 case elfcpp::R_X86_64_GOT32:
1564 gold_assert(have_got_offset);
1565 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1566 break;
1568 case elfcpp::R_X86_64_GOTPC32:
1570 gold_assert(gsym);
1571 elfcpp::Elf_types<64>::Elf_Addr value;
1572 value = target->got_plt_section()->address();
1573 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1575 break;
1577 case elfcpp::R_X86_64_GOT64:
1578 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1579 // Since we always add a PLT entry, this is equivalent.
1580 case elfcpp::R_X86_64_GOTPLT64:
1581 gold_assert(have_got_offset);
1582 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1583 break;
1585 case elfcpp::R_X86_64_GOTPC64:
1587 gold_assert(gsym);
1588 elfcpp::Elf_types<64>::Elf_Addr value;
1589 value = target->got_plt_section()->address();
1590 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1592 break;
1594 case elfcpp::R_X86_64_GOTOFF64:
1596 elfcpp::Elf_types<64>::Elf_Addr value;
1597 value = (psymval->value(object, 0)
1598 - target->got_plt_section()->address());
1599 Relocate_functions<64, false>::rela64(view, value, addend);
1601 break;
1603 case elfcpp::R_X86_64_GOTPCREL:
1605 gold_assert(have_got_offset);
1606 elfcpp::Elf_types<64>::Elf_Addr value;
1607 value = target->got_plt_section()->address() + got_offset;
1608 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1610 break;
1612 case elfcpp::R_X86_64_GOTPCREL64:
1614 gold_assert(have_got_offset);
1615 elfcpp::Elf_types<64>::Elf_Addr value;
1616 value = target->got_plt_section()->address() + got_offset;
1617 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1619 break;
1621 case elfcpp::R_X86_64_COPY:
1622 case elfcpp::R_X86_64_GLOB_DAT:
1623 case elfcpp::R_X86_64_JUMP_SLOT:
1624 case elfcpp::R_X86_64_RELATIVE:
1625 // These are outstanding tls relocs, which are unexpected when linking
1626 case elfcpp::R_X86_64_TPOFF64:
1627 case elfcpp::R_X86_64_DTPMOD64:
1628 case elfcpp::R_X86_64_TLSDESC:
1629 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1630 _("unexpected reloc %u in object file"),
1631 r_type);
1632 break;
1634 // These are initial tls relocs, which are expected when linking
1635 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1636 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1637 case elfcpp::R_X86_64_TLSDESC_CALL:
1638 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1639 case elfcpp::R_X86_64_DTPOFF32:
1640 case elfcpp::R_X86_64_DTPOFF64:
1641 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1642 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1643 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1644 view, address, view_size);
1645 break;
1647 case elfcpp::R_X86_64_SIZE32:
1648 case elfcpp::R_X86_64_SIZE64:
1649 default:
1650 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1651 _("unsupported reloc %u"),
1652 r_type);
1653 break;
1656 return true;
1659 // Perform a TLS relocation.
1661 inline void
1662 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1663 Target_x86_64* target,
1664 size_t relnum,
1665 const elfcpp::Rela<64, false>& rela,
1666 unsigned int r_type,
1667 const Sized_symbol<64>* gsym,
1668 const Symbol_value<64>* psymval,
1669 unsigned char* view,
1670 elfcpp::Elf_types<64>::Elf_Addr address,
1671 section_size_type view_size)
1673 Output_segment* tls_segment = relinfo->layout->tls_segment();
1675 const Sized_relobj<64, false>* object = relinfo->object;
1676 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1678 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1680 const bool is_final = (gsym == NULL
1681 ? !parameters->options().output_is_position_independent()
1682 : gsym->final_value_is_known());
1683 const tls::Tls_optimization optimized_type
1684 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1685 switch (r_type)
1687 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1688 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1689 case elfcpp::R_X86_64_TLSDESC_CALL:
1690 if (optimized_type == tls::TLSOPT_TO_LE)
1692 gold_assert(tls_segment != NULL);
1693 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1694 rela, r_type, value, view,
1695 view_size);
1696 break;
1698 else
1700 unsigned int got_offset;
1701 if (gsym != NULL)
1703 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_PAIR));
1704 got_offset = (gsym->got_offset(GOT_TYPE_TLS_PAIR)
1705 - target->got_size());
1707 else
1709 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1710 gold_assert(object->local_has_got_offset(r_sym,
1711 GOT_TYPE_TLS_PAIR));
1712 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR)
1713 - target->got_size());
1715 if (optimized_type == tls::TLSOPT_TO_IE)
1717 gold_assert(tls_segment != NULL);
1718 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1719 got_offset, view, view_size);
1720 break;
1722 else if (optimized_type == tls::TLSOPT_NONE)
1724 // Relocate the field with the offset of the pair of GOT
1725 // entries.
1726 value = target->got_plt_section()->address() + got_offset;
1727 Relocate_functions<64, false>::pcrela32(view, value, addend,
1728 address);
1729 break;
1732 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1733 _("unsupported reloc %u"), r_type);
1734 break;
1736 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1737 if (optimized_type == tls::TLSOPT_TO_LE)
1739 gold_assert(tls_segment != NULL);
1740 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1741 value, view, view_size);
1742 break;
1744 else if (optimized_type == tls::TLSOPT_NONE)
1746 // Relocate the field with the offset of the GOT entry for
1747 // the module index.
1748 unsigned int got_offset;
1749 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1750 - target->got_size());
1751 value = target->got_plt_section()->address() + got_offset;
1752 Relocate_functions<64, false>::pcrela32(view, value, addend,
1753 address);
1754 break;
1756 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1757 _("unsupported reloc %u"), r_type);
1758 break;
1760 case elfcpp::R_X86_64_DTPOFF32:
1761 gold_assert(tls_segment != NULL);
1762 if (optimized_type == tls::TLSOPT_TO_LE)
1763 value -= tls_segment->memsz();
1764 Relocate_functions<64, false>::rela32(view, value, 0);
1765 break;
1767 case elfcpp::R_X86_64_DTPOFF64:
1768 gold_assert(tls_segment != NULL);
1769 if (optimized_type == tls::TLSOPT_TO_LE)
1770 value -= tls_segment->memsz();
1771 Relocate_functions<64, false>::rela64(view, value, 0);
1772 break;
1774 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1775 if (optimized_type == tls::TLSOPT_TO_LE)
1777 gold_assert(tls_segment != NULL);
1778 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1779 rela, r_type, value, view,
1780 view_size);
1781 break;
1783 else if (optimized_type == tls::TLSOPT_NONE)
1785 // Relocate the field with the offset of the GOT entry for
1786 // the tp-relative offset of the symbol.
1787 unsigned int got_offset;
1788 if (gsym != NULL)
1790 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
1791 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
1792 - target->got_size());
1794 else
1796 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1797 gold_assert(object->local_has_got_offset(r_sym,
1798 GOT_TYPE_TLS_OFFSET));
1799 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
1800 - target->got_size());
1802 value = target->got_plt_section()->address() + got_offset;
1803 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1804 break;
1806 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1807 _("unsupported reloc type %u"),
1808 r_type);
1809 break;
1811 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1812 value -= tls_segment->memsz();
1813 Relocate_functions<64, false>::rela32(view, value, 0);
1814 break;
1818 // Do a relocation in which we convert a TLS General-Dynamic to an
1819 // Initial-Exec.
1821 inline void
1822 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1823 size_t relnum,
1824 Output_segment* tls_segment,
1825 const elfcpp::Rela<64, false>& rela,
1826 unsigned int,
1827 elfcpp::Elf_types<64>::Elf_Addr value,
1828 unsigned char* view,
1829 section_size_type view_size)
1831 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1832 // .word 0x6666; rex64; call __tls_get_addr
1833 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1835 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1836 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1838 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1839 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1840 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1841 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1843 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1845 value -= tls_segment->memsz();
1846 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1848 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1849 // We can skip it.
1850 this->skip_call_tls_get_addr_ = true;
1853 // Do a relocation in which we convert a TLS General-Dynamic to a
1854 // Local-Exec.
1856 inline void
1857 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1858 size_t relnum,
1859 Output_segment* tls_segment,
1860 const elfcpp::Rela<64, false>& rela,
1861 unsigned int,
1862 elfcpp::Elf_types<64>::Elf_Addr value,
1863 unsigned char* view,
1864 section_size_type view_size)
1866 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1867 // .word 0x6666; rex64; call __tls_get_addr
1868 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1870 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1871 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1873 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1874 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1875 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1876 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1878 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1880 value -= tls_segment->memsz();
1881 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1883 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1884 // We can skip it.
1885 this->skip_call_tls_get_addr_ = true;
1888 inline void
1889 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1890 size_t relnum,
1891 Output_segment*,
1892 const elfcpp::Rela<64, false>& rela,
1893 unsigned int,
1894 elfcpp::Elf_types<64>::Elf_Addr,
1895 unsigned char* view,
1896 section_size_type view_size)
1898 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1899 // ... leq foo@dtpoff(%rax),%reg
1900 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1902 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1903 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1905 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1906 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1908 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1910 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1912 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1913 // We can skip it.
1914 this->skip_call_tls_get_addr_ = true;
1917 // Do a relocation in which we convert a TLS Initial-Exec to a
1918 // Local-Exec.
1920 inline void
1921 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1922 size_t relnum,
1923 Output_segment* tls_segment,
1924 const elfcpp::Rela<64, false>& rela,
1925 unsigned int,
1926 elfcpp::Elf_types<64>::Elf_Addr value,
1927 unsigned char* view,
1928 section_size_type view_size)
1930 // We need to examine the opcodes to figure out which instruction we
1931 // are looking at.
1933 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1934 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1936 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1937 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1939 unsigned char op1 = view[-3];
1940 unsigned char op2 = view[-2];
1941 unsigned char op3 = view[-1];
1942 unsigned char reg = op3 >> 3;
1944 if (op2 == 0x8b)
1946 // movq
1947 if (op1 == 0x4c)
1948 view[-3] = 0x49;
1949 view[-2] = 0xc7;
1950 view[-1] = 0xc0 | reg;
1952 else if (reg == 4)
1954 // Special handling for %rsp.
1955 if (op1 == 0x4c)
1956 view[-3] = 0x49;
1957 view[-2] = 0x81;
1958 view[-1] = 0xc0 | reg;
1960 else
1962 // addq
1963 if (op1 == 0x4c)
1964 view[-3] = 0x4d;
1965 view[-2] = 0x8d;
1966 view[-1] = 0x80 | reg | (reg << 3);
1969 value -= tls_segment->memsz();
1970 Relocate_functions<64, false>::rela32(view, value, 0);
1973 // Relocate section data.
1975 void
1976 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1977 unsigned int sh_type,
1978 const unsigned char* prelocs,
1979 size_t reloc_count,
1980 Output_section* output_section,
1981 bool needs_special_offset_handling,
1982 unsigned char* view,
1983 elfcpp::Elf_types<64>::Elf_Addr address,
1984 section_size_type view_size)
1986 gold_assert(sh_type == elfcpp::SHT_RELA);
1988 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1989 Target_x86_64::Relocate>(
1990 relinfo,
1991 this,
1992 prelocs,
1993 reloc_count,
1994 output_section,
1995 needs_special_offset_handling,
1996 view,
1997 address,
1998 view_size);
2001 // Return the size of a relocation while scanning during a relocatable
2002 // link.
2004 unsigned int
2005 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2006 unsigned int r_type,
2007 Relobj* object)
2009 switch (r_type)
2011 case elfcpp::R_X86_64_NONE:
2012 case elfcpp::R_386_GNU_VTINHERIT:
2013 case elfcpp::R_386_GNU_VTENTRY:
2014 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2015 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2016 case elfcpp::R_X86_64_TLSDESC_CALL:
2017 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2018 case elfcpp::R_X86_64_DTPOFF32:
2019 case elfcpp::R_X86_64_DTPOFF64:
2020 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2021 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2022 return 0;
2024 case elfcpp::R_X86_64_64:
2025 case elfcpp::R_X86_64_PC64:
2026 case elfcpp::R_X86_64_GOTOFF64:
2027 case elfcpp::R_X86_64_GOTPC64:
2028 case elfcpp::R_X86_64_PLTOFF64:
2029 case elfcpp::R_X86_64_GOT64:
2030 case elfcpp::R_X86_64_GOTPCREL64:
2031 case elfcpp::R_X86_64_GOTPCREL:
2032 case elfcpp::R_X86_64_GOTPLT64:
2033 return 8;
2035 case elfcpp::R_X86_64_32:
2036 case elfcpp::R_X86_64_32S:
2037 case elfcpp::R_X86_64_PC32:
2038 case elfcpp::R_X86_64_PLT32:
2039 case elfcpp::R_X86_64_GOTPC32:
2040 case elfcpp::R_X86_64_GOT32:
2041 return 4;
2043 case elfcpp::R_X86_64_16:
2044 case elfcpp::R_X86_64_PC16:
2045 return 2;
2047 case elfcpp::R_X86_64_8:
2048 case elfcpp::R_X86_64_PC8:
2049 return 1;
2051 case elfcpp::R_X86_64_COPY:
2052 case elfcpp::R_X86_64_GLOB_DAT:
2053 case elfcpp::R_X86_64_JUMP_SLOT:
2054 case elfcpp::R_X86_64_RELATIVE:
2055 // These are outstanding tls relocs, which are unexpected when linking
2056 case elfcpp::R_X86_64_TPOFF64:
2057 case elfcpp::R_X86_64_DTPMOD64:
2058 case elfcpp::R_X86_64_TLSDESC:
2059 object->error(_("unexpected reloc %u in object file"), r_type);
2060 return 0;
2062 case elfcpp::R_X86_64_SIZE32:
2063 case elfcpp::R_X86_64_SIZE64:
2064 default:
2065 object->error(_("unsupported reloc %u against local symbol"), r_type);
2066 return 0;
2070 // Scan the relocs during a relocatable link.
2072 void
2073 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2074 Symbol_table* symtab,
2075 Layout* layout,
2076 Sized_relobj<64, false>* object,
2077 unsigned int data_shndx,
2078 unsigned int sh_type,
2079 const unsigned char* prelocs,
2080 size_t reloc_count,
2081 Output_section* output_section,
2082 bool needs_special_offset_handling,
2083 size_t local_symbol_count,
2084 const unsigned char* plocal_symbols,
2085 Relocatable_relocs* rr)
2087 gold_assert(sh_type == elfcpp::SHT_RELA);
2089 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2090 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2092 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2093 Scan_relocatable_relocs>(
2094 options,
2095 symtab,
2096 layout,
2097 object,
2098 data_shndx,
2099 prelocs,
2100 reloc_count,
2101 output_section,
2102 needs_special_offset_handling,
2103 local_symbol_count,
2104 plocal_symbols,
2105 rr);
2108 // Relocate a section during a relocatable link.
2110 void
2111 Target_x86_64::relocate_for_relocatable(
2112 const Relocate_info<64, false>* relinfo,
2113 unsigned int sh_type,
2114 const unsigned char* prelocs,
2115 size_t reloc_count,
2116 Output_section* output_section,
2117 off_t offset_in_output_section,
2118 const Relocatable_relocs* rr,
2119 unsigned char* view,
2120 elfcpp::Elf_types<64>::Elf_Addr view_address,
2121 section_size_type view_size,
2122 unsigned char* reloc_view,
2123 section_size_type reloc_view_size)
2125 gold_assert(sh_type == elfcpp::SHT_RELA);
2127 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2128 relinfo,
2129 prelocs,
2130 reloc_count,
2131 output_section,
2132 offset_in_output_section,
2134 view,
2135 view_address,
2136 view_size,
2137 reloc_view,
2138 reloc_view_size);
2141 // Return the value to use for a dynamic which requires special
2142 // treatment. This is how we support equality comparisons of function
2143 // pointers across shared library boundaries, as described in the
2144 // processor specific ABI supplement.
2146 uint64_t
2147 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2149 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2150 return this->plt_section()->address() + gsym->plt_offset();
2153 // Return a string used to fill a code section with nops to take up
2154 // the specified length.
2156 std::string
2157 Target_x86_64::do_code_fill(section_size_type length) const
2159 if (length >= 16)
2161 // Build a jmpq instruction to skip over the bytes.
2162 unsigned char jmp[5];
2163 jmp[0] = 0xe9;
2164 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2165 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2166 + std::string(length - 5, '\0'));
2169 // Nop sequences of various lengths.
2170 const char nop1[1] = { 0x90 }; // nop
2171 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2172 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2173 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2174 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2175 0x00 }; // leal 0(%esi,1),%esi
2176 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2177 0x00, 0x00 };
2178 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2179 0x00, 0x00, 0x00 };
2180 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2181 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2182 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2183 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2184 0x00 };
2185 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2186 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2187 0x00, 0x00 };
2188 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2189 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2190 0x00, 0x00, 0x00 };
2191 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2192 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2193 0x00, 0x00, 0x00, 0x00 };
2194 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2195 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2196 0x27, 0x00, 0x00, 0x00,
2197 0x00 };
2198 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2199 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2200 0xbc, 0x27, 0x00, 0x00,
2201 0x00, 0x00 };
2202 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2203 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2204 0x90, 0x90, 0x90, 0x90,
2205 0x90, 0x90, 0x90 };
2207 const char* nops[16] = {
2208 NULL,
2209 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2210 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2213 return std::string(nops[length], length);
2216 // The selector for x86_64 object files.
2218 class Target_selector_x86_64 : public Target_selector
2220 public:
2221 Target_selector_x86_64()
2222 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2225 Target*
2226 do_instantiate_target()
2227 { return new Target_x86_64(); }
2230 Target_selector_x86_64 target_selector_x86_64;
2232 } // End anonymous namespace.