* listing.c (print_timestamp): Use localtime rather than
[binutils.git] / gold / x86_64.cc
blob34f54c8e761533d2c81aff8c09ebf5f7e50e9f73
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 class Scan
155 public:
156 Scan()
157 : issued_non_pic_error_(false)
160 inline void
161 local(const General_options& options, Symbol_table* symtab,
162 Layout* layout, Target_x86_64* target,
163 Sized_relobj<64, false>* object,
164 unsigned int data_shndx,
165 Output_section* output_section,
166 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
167 const elfcpp::Sym<64, false>& lsym);
169 inline void
170 global(const General_options& options, Symbol_table* symtab,
171 Layout* layout, Target_x86_64* target,
172 Sized_relobj<64, false>* object,
173 unsigned int data_shndx,
174 Output_section* output_section,
175 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
176 Symbol* gsym);
178 private:
179 static void
180 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
182 static void
183 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
184 Symbol*);
186 void
187 check_non_pic(Relobj*, unsigned int r_type);
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_;
193 // The class which implements relocation.
194 class Relocate
196 public:
197 Relocate()
198 : skip_call_tls_get_addr_(false)
201 ~Relocate()
203 if (this->skip_call_tls_get_addr_)
205 // FIXME: This needs to specify the location somehow.
206 gold_error(_("missing expected TLS relocation"));
210 // Do a relocation. Return false if the caller should not issue
211 // any warnings about this relocation.
212 inline bool
213 relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
214 const elfcpp::Rela<64, false>&,
215 unsigned int r_type, const Sized_symbol<64>*,
216 const Symbol_value<64>*,
217 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
218 section_size_type);
220 private:
221 // Do a TLS relocation.
222 inline void
223 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
224 size_t relnum, const elfcpp::Rela<64, false>&,
225 unsigned int r_type, const Sized_symbol<64>*,
226 const Symbol_value<64>*,
227 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
228 section_size_type);
230 // Do a TLS General-Dynamic to Local-Exec transition.
231 inline void
232 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
233 Output_segment* tls_segment,
234 const elfcpp::Rela<64, false>&, unsigned int r_type,
235 elfcpp::Elf_types<64>::Elf_Addr value,
236 unsigned char* view,
237 section_size_type view_size);
239 // Do a TLS General-Dynamic to Local-Exec transition.
240 inline void
241 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
242 Output_segment* tls_segment,
243 const elfcpp::Rela<64, false>&, unsigned int r_type,
244 elfcpp::Elf_types<64>::Elf_Addr value,
245 unsigned char* view,
246 section_size_type view_size);
248 // Do a TLS Local-Dynamic to Local-Exec transition.
249 inline void
250 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
251 Output_segment* tls_segment,
252 const elfcpp::Rela<64, false>&, unsigned int r_type,
253 elfcpp::Elf_types<64>::Elf_Addr value,
254 unsigned char* view,
255 section_size_type view_size);
257 // Do a TLS Initial-Exec to Local-Exec transition.
258 static inline void
259 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
260 Output_segment* tls_segment,
261 const elfcpp::Rela<64, false>&, unsigned int r_type,
262 elfcpp::Elf_types<64>::Elf_Addr value,
263 unsigned char* view,
264 section_size_type view_size);
266 // This is set if we should skip the next reloc, which should be a
267 // PLT32 reloc against ___tls_get_addr.
268 bool skip_call_tls_get_addr_;
271 // A class which returns the size required for a relocation type,
272 // used while scanning relocs during a relocatable link.
273 class Relocatable_size_for_reloc
275 public:
276 unsigned int
277 get_size_for_reloc(unsigned int, Relobj*);
280 // Adjust TLS relocation type based on the options and whether this
281 // is a local symbol.
282 static tls::Tls_optimization
283 optimize_tls_reloc(bool is_final, int r_type);
285 // Get the GOT section, creating it if necessary.
286 Output_data_got<64, false>*
287 got_section(Symbol_table*, Layout*);
289 // Get the GOT PLT section.
290 Output_data_space*
291 got_plt_section() const
293 gold_assert(this->got_plt_ != NULL);
294 return this->got_plt_;
297 // Create a PLT entry for a global symbol.
298 void
299 make_plt_entry(Symbol_table*, Layout*, Symbol*);
301 // Create a GOT entry for the TLS module index.
302 unsigned int
303 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
304 Sized_relobj<64, false>* object);
306 // Get the PLT section.
307 Output_data_plt_x86_64*
308 plt_section() const
310 gold_assert(this->plt_ != NULL);
311 return this->plt_;
314 // Get the dynamic reloc section, creating it if necessary.
315 Reloc_section*
316 rela_dyn_section(Layout*);
318 // Return true if the symbol may need a COPY relocation.
319 // References from an executable object to non-function symbols
320 // defined in a dynamic object may need a COPY relocation.
321 bool
322 may_need_copy_reloc(Symbol* gsym)
324 return (!parameters->options().shared()
325 && gsym->is_from_dynobj()
326 && gsym->type() != elfcpp::STT_FUNC);
329 // Copy a relocation against a global symbol.
330 void
331 copy_reloc(const General_options*, Symbol_table*, Layout*,
332 Sized_relobj<64, false>*, unsigned int,
333 Output_section*, Symbol*, const elfcpp::Rela<64, false>&);
335 // Information about this specific target which we pass to the
336 // general Target structure.
337 static const Target::Target_info x86_64_info;
339 enum Got_type
341 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
342 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
343 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
344 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
347 // The GOT section.
348 Output_data_got<64, false>* got_;
349 // The PLT section.
350 Output_data_plt_x86_64* plt_;
351 // The GOT PLT section.
352 Output_data_space* got_plt_;
353 // The dynamic reloc section.
354 Reloc_section* rela_dyn_;
355 // Relocs saved to avoid a COPY reloc.
356 Copy_relocs<64, false>* copy_relocs_;
357 // Space for variables copied with a COPY reloc.
358 Output_data_space* dynbss_;
359 // Offset of the GOT entry for the TLS module index;
360 unsigned int got_mod_index_offset_;
363 const Target::Target_info Target_x86_64::x86_64_info =
365 64, // size
366 false, // is_big_endian
367 elfcpp::EM_X86_64, // machine_code
368 false, // has_make_symbol
369 false, // has_resolve
370 true, // has_code_fill
371 true, // is_default_stack_executable
372 '\0', // wrap_char
373 "/lib/ld64.so.1", // program interpreter
374 0x400000, // default_text_segment_address
375 0x1000, // abi_pagesize (overridable by -z max-page-size)
376 0x1000 // common_pagesize (overridable by -z common-page-size)
379 // Get the GOT section, creating it if necessary.
381 Output_data_got<64, false>*
382 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
384 if (this->got_ == NULL)
386 gold_assert(symtab != NULL && layout != NULL);
388 this->got_ = new Output_data_got<64, false>();
390 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
391 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
392 this->got_);
394 // The old GNU linker creates a .got.plt section. We just
395 // create another set of data in the .got section. Note that we
396 // always create a PLT if we create a GOT, although the PLT
397 // might be empty.
398 this->got_plt_ = new Output_data_space(8);
399 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
400 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
401 this->got_plt_);
403 // The first three entries are reserved.
404 this->got_plt_->set_current_data_size(3 * 8);
406 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
407 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
408 this->got_plt_,
409 0, 0, elfcpp::STT_OBJECT,
410 elfcpp::STB_LOCAL,
411 elfcpp::STV_HIDDEN, 0,
412 false, false);
415 return this->got_;
418 // Get the dynamic reloc section, creating it if necessary.
420 Target_x86_64::Reloc_section*
421 Target_x86_64::rela_dyn_section(Layout* layout)
423 if (this->rela_dyn_ == NULL)
425 gold_assert(layout != NULL);
426 this->rela_dyn_ = new Reloc_section();
427 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
428 elfcpp::SHF_ALLOC, this->rela_dyn_);
430 return this->rela_dyn_;
433 // A class to handle the PLT data.
435 class Output_data_plt_x86_64 : public Output_section_data
437 public:
438 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
440 Output_data_plt_x86_64(Layout*, Output_data_space*);
442 // Add an entry to the PLT.
443 void
444 add_entry(Symbol* gsym);
446 // Return the .rel.plt section data.
447 const Reloc_section*
448 rel_plt() const
449 { return this->rel_; }
451 protected:
452 void
453 do_adjust_output_section(Output_section* os);
455 private:
456 // The size of an entry in the PLT.
457 static const int plt_entry_size = 16;
459 // The first entry in the PLT.
460 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
461 // procedure linkage table for both programs and shared objects."
462 static unsigned char first_plt_entry[plt_entry_size];
464 // Other entries in the PLT for an executable.
465 static unsigned char plt_entry[plt_entry_size];
467 // Set the final size.
468 void
469 set_final_data_size()
470 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
472 // Write out the PLT data.
473 void
474 do_write(Output_file*);
476 // The reloc section.
477 Reloc_section* rel_;
478 // The .got.plt section.
479 Output_data_space* got_plt_;
480 // The number of PLT entries.
481 unsigned int count_;
484 // Create the PLT section. The ordinary .got section is an argument,
485 // since we need to refer to the start. We also create our own .got
486 // section just for PLT entries.
488 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
489 Output_data_space* got_plt)
490 : Output_section_data(8), got_plt_(got_plt), count_(0)
492 this->rel_ = new Reloc_section();
493 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
494 elfcpp::SHF_ALLOC, this->rel_);
497 void
498 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
500 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
501 // linker, and so do we.
502 os->set_entsize(4);
505 // Add an entry to the PLT.
507 void
508 Output_data_plt_x86_64::add_entry(Symbol* gsym)
510 gold_assert(!gsym->has_plt_offset());
512 // Note that when setting the PLT offset we skip the initial
513 // reserved PLT entry.
514 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
516 ++this->count_;
518 section_offset_type got_offset = this->got_plt_->current_data_size();
520 // Every PLT entry needs a GOT entry which points back to the PLT
521 // entry (this will be changed by the dynamic linker, normally
522 // lazily when the function is called).
523 this->got_plt_->set_current_data_size(got_offset + 8);
525 // Every PLT entry needs a reloc.
526 gsym->set_needs_dynsym_entry();
527 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
528 got_offset, 0);
530 // Note that we don't need to save the symbol. The contents of the
531 // PLT are independent of which symbols are used. The symbols only
532 // appear in the relocations.
535 // The first entry in the PLT for an executable.
537 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
539 // From AMD64 ABI Draft 0.98, page 76
540 0xff, 0x35, // pushq contents of memory address
541 0, 0, 0, 0, // replaced with address of .got + 8
542 0xff, 0x25, // jmp indirect
543 0, 0, 0, 0, // replaced with address of .got + 16
544 0x90, 0x90, 0x90, 0x90 // noop (x4)
547 // Subsequent entries in the PLT for an executable.
549 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
551 // From AMD64 ABI Draft 0.98, page 76
552 0xff, 0x25, // jmpq indirect
553 0, 0, 0, 0, // replaced with address of symbol in .got
554 0x68, // pushq immediate
555 0, 0, 0, 0, // replaced with offset into relocation table
556 0xe9, // jmpq relative
557 0, 0, 0, 0 // replaced with offset to start of .plt
560 // Write out the PLT. This uses the hand-coded instructions above,
561 // and adjusts them as needed. This is specified by the AMD64 ABI.
563 void
564 Output_data_plt_x86_64::do_write(Output_file* of)
566 const off_t offset = this->offset();
567 const section_size_type oview_size =
568 convert_to_section_size_type(this->data_size());
569 unsigned char* const oview = of->get_output_view(offset, oview_size);
571 const off_t got_file_offset = this->got_plt_->offset();
572 const section_size_type got_size =
573 convert_to_section_size_type(this->got_plt_->data_size());
574 unsigned char* const got_view = of->get_output_view(got_file_offset,
575 got_size);
577 unsigned char* pov = oview;
579 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
580 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
582 memcpy(pov, first_plt_entry, plt_entry_size);
583 // We do a jmp relative to the PC at the end of this instruction.
584 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
585 - (plt_address + 6));
586 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
587 - (plt_address + 12));
588 pov += plt_entry_size;
590 unsigned char* got_pov = got_view;
592 memset(got_pov, 0, 24);
593 got_pov += 24;
595 unsigned int plt_offset = plt_entry_size;
596 unsigned int got_offset = 24;
597 const unsigned int count = this->count_;
598 for (unsigned int plt_index = 0;
599 plt_index < count;
600 ++plt_index,
601 pov += plt_entry_size,
602 got_pov += 8,
603 plt_offset += plt_entry_size,
604 got_offset += 8)
606 // Set and adjust the PLT entry itself.
607 memcpy(pov, plt_entry, plt_entry_size);
608 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
609 (got_address + got_offset
610 - (plt_address + plt_offset
611 + 6)));
613 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
614 elfcpp::Swap<32, false>::writeval(pov + 12,
615 - (plt_offset + plt_entry_size));
617 // Set the entry in the GOT.
618 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
621 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
622 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
624 of->write_output_view(offset, oview_size, oview);
625 of->write_output_view(got_file_offset, got_size, got_view);
628 // Create a PLT entry for a global symbol.
630 void
631 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
632 Symbol* gsym)
634 if (gsym->has_plt_offset())
635 return;
637 if (this->plt_ == NULL)
639 // Create the GOT sections first.
640 this->got_section(symtab, layout);
642 this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
643 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
644 (elfcpp::SHF_ALLOC
645 | elfcpp::SHF_EXECINSTR),
646 this->plt_);
649 this->plt_->add_entry(gsym);
652 // Create a GOT entry for the TLS module index.
654 unsigned int
655 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
656 Sized_relobj<64, false>* object)
658 if (this->got_mod_index_offset_ == -1U)
660 gold_assert(symtab != NULL && layout != NULL && object != NULL);
661 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
662 Output_data_got<64, false>* got = this->got_section(symtab, layout);
663 unsigned int got_offset = got->add_constant(0);
664 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
665 got_offset, 0);
666 got->add_constant(0);
667 this->got_mod_index_offset_ = got_offset;
669 return this->got_mod_index_offset_;
672 // Handle a relocation against a non-function symbol defined in a
673 // dynamic object. The traditional way to handle this is to generate
674 // a COPY relocation to copy the variable at runtime from the shared
675 // object into the executable's data segment. However, this is
676 // undesirable in general, as if the size of the object changes in the
677 // dynamic object, the executable will no longer work correctly. If
678 // this relocation is in a writable section, then we can create a
679 // dynamic reloc and the dynamic linker will resolve it to the correct
680 // address at runtime. However, we do not want do that if the
681 // relocation is in a read-only section, as it would prevent the
682 // readonly segment from being shared. And if we have to eventually
683 // generate a COPY reloc, then any dynamic relocations will be
684 // useless. So this means that if this is a writable section, we need
685 // to save the relocation until we see whether we have to create a
686 // COPY relocation for this symbol for any other relocation.
688 void
689 Target_x86_64::copy_reloc(const General_options* options,
690 Symbol_table* symtab,
691 Layout* layout,
692 Sized_relobj<64, false>* object,
693 unsigned int data_shndx,
694 Output_section* output_section,
695 Symbol* gsym,
696 const elfcpp::Rela<64, false>& rela)
698 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(gsym);
700 if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
701 data_shndx, ssym))
703 // So far we do not need a COPY reloc. Save this relocation.
704 // If it turns out that we never need a COPY reloc for this
705 // symbol, then we will emit the relocation.
706 if (this->copy_relocs_ == NULL)
707 this->copy_relocs_ = new Copy_relocs<64, false>();
708 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
710 else
712 // Allocate space for this symbol in the .bss section.
714 elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
716 // There is no defined way to determine the required alignment
717 // of the symbol. We pick the alignment based on the size. We
718 // set an arbitrary maximum of 256.
719 unsigned int align;
720 for (align = 1; align < 512; align <<= 1)
721 if ((symsize & align) != 0)
722 break;
724 if (this->dynbss_ == NULL)
726 this->dynbss_ = new Output_data_space(align);
727 layout->add_output_section_data(".bss",
728 elfcpp::SHT_NOBITS,
729 (elfcpp::SHF_ALLOC
730 | elfcpp::SHF_WRITE),
731 this->dynbss_);
734 Output_data_space* dynbss = this->dynbss_;
736 if (align > dynbss->addralign())
737 dynbss->set_space_alignment(align);
739 section_size_type dynbss_size = dynbss->current_data_size();
740 dynbss_size = align_address(dynbss_size, align);
741 section_size_type offset = dynbss_size;
742 dynbss->set_current_data_size(dynbss_size + symsize);
744 symtab->define_with_copy_reloc(ssym, dynbss, offset);
746 // Add the COPY reloc.
747 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
748 rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
753 // Optimize the TLS relocation type based on what we know about the
754 // symbol. IS_FINAL is true if the final address of this symbol is
755 // known at link time.
757 tls::Tls_optimization
758 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
760 // If we are generating a shared library, then we can't do anything
761 // in the linker.
762 if (parameters->options().shared())
763 return tls::TLSOPT_NONE;
765 switch (r_type)
767 case elfcpp::R_X86_64_TLSGD:
768 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
769 case elfcpp::R_X86_64_TLSDESC_CALL:
770 // These are General-Dynamic which permits fully general TLS
771 // access. Since we know that we are generating an executable,
772 // we can convert this to Initial-Exec. If we also know that
773 // this is a local symbol, we can further switch to Local-Exec.
774 if (is_final)
775 return tls::TLSOPT_TO_LE;
776 return tls::TLSOPT_TO_IE;
778 case elfcpp::R_X86_64_TLSLD:
779 // This is Local-Dynamic, which refers to a local symbol in the
780 // dynamic TLS block. Since we know that we generating an
781 // executable, we can switch to Local-Exec.
782 return tls::TLSOPT_TO_LE;
784 case elfcpp::R_X86_64_DTPOFF32:
785 case elfcpp::R_X86_64_DTPOFF64:
786 // Another Local-Dynamic reloc.
787 return tls::TLSOPT_TO_LE;
789 case elfcpp::R_X86_64_GOTTPOFF:
790 // These are Initial-Exec relocs which get the thread offset
791 // from the GOT. If we know that we are linking against the
792 // local symbol, we can switch to Local-Exec, which links the
793 // thread offset into the instruction.
794 if (is_final)
795 return tls::TLSOPT_TO_LE;
796 return tls::TLSOPT_NONE;
798 case elfcpp::R_X86_64_TPOFF32:
799 // When we already have Local-Exec, there is nothing further we
800 // can do.
801 return tls::TLSOPT_NONE;
803 default:
804 gold_unreachable();
808 // Report an unsupported relocation against a local symbol.
810 void
811 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
812 unsigned int r_type)
814 gold_error(_("%s: unsupported reloc %u against local symbol"),
815 object->name().c_str(), r_type);
818 // We are about to emit a dynamic relocation of type R_TYPE. If the
819 // dynamic linker does not support it, issue an error. The GNU linker
820 // only issues a non-PIC error for an allocated read-only section.
821 // Here we know the section is allocated, but we don't know that it is
822 // read-only. But we check for all the relocation types which the
823 // glibc dynamic linker supports, so it seems appropriate to issue an
824 // error even if the section is not read-only.
826 void
827 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
829 switch (r_type)
831 // These are the relocation types supported by glibc for x86_64.
832 case elfcpp::R_X86_64_RELATIVE:
833 case elfcpp::R_X86_64_GLOB_DAT:
834 case elfcpp::R_X86_64_JUMP_SLOT:
835 case elfcpp::R_X86_64_DTPMOD64:
836 case elfcpp::R_X86_64_DTPOFF64:
837 case elfcpp::R_X86_64_TPOFF64:
838 case elfcpp::R_X86_64_64:
839 case elfcpp::R_X86_64_32:
840 case elfcpp::R_X86_64_PC32:
841 case elfcpp::R_X86_64_COPY:
842 return;
844 default:
845 // This prevents us from issuing more than one error per reloc
846 // section. But we can still wind up issuing more than one
847 // error per object file.
848 if (this->issued_non_pic_error_)
849 return;
850 object->error(_("requires unsupported dynamic reloc; "
851 "recompile with -fPIC"));
852 this->issued_non_pic_error_ = true;
853 return;
855 case elfcpp::R_X86_64_NONE:
856 gold_unreachable();
860 // Scan a relocation for a local symbol.
862 inline void
863 Target_x86_64::Scan::local(const General_options&,
864 Symbol_table* symtab,
865 Layout* layout,
866 Target_x86_64* target,
867 Sized_relobj<64, false>* object,
868 unsigned int data_shndx,
869 Output_section* output_section,
870 const elfcpp::Rela<64, false>& reloc,
871 unsigned int r_type,
872 const elfcpp::Sym<64, false>& lsym)
874 switch (r_type)
876 case elfcpp::R_X86_64_NONE:
877 case elfcpp::R_386_GNU_VTINHERIT:
878 case elfcpp::R_386_GNU_VTENTRY:
879 break;
881 case elfcpp::R_X86_64_64:
882 // If building a shared library (or a position-independent
883 // executable), we need to create a dynamic relocation for this
884 // location. The relocation applied at link time will apply the
885 // link-time value, so we flag the location with an
886 // R_X86_64_RELATIVE relocation so the dynamic loader can
887 // relocate it easily.
888 if (parameters->options().output_is_position_independent())
890 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
891 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
892 rela_dyn->add_local_relative(object, r_sym,
893 elfcpp::R_X86_64_RELATIVE,
894 output_section, data_shndx,
895 reloc.get_r_offset(),
896 reloc.get_r_addend());
898 break;
900 case elfcpp::R_X86_64_32:
901 case elfcpp::R_X86_64_32S:
902 case elfcpp::R_X86_64_16:
903 case elfcpp::R_X86_64_8:
904 // If building a shared library (or a position-independent
905 // executable), we need to create a dynamic relocation for this
906 // location. We can't use an R_X86_64_RELATIVE relocation
907 // because that is always a 64-bit relocation.
908 if (parameters->options().output_is_position_independent())
910 this->check_non_pic(object, r_type);
912 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
913 if (lsym.get_st_type() != elfcpp::STT_SECTION)
915 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
916 rela_dyn->add_local(object, r_sym, r_type, output_section,
917 data_shndx, reloc.get_r_offset(),
918 reloc.get_r_addend());
920 else
922 gold_assert(lsym.get_st_value() == 0);
923 rela_dyn->add_local_section(object, lsym.get_st_shndx(),
924 r_type, output_section,
925 data_shndx, reloc.get_r_offset(),
926 reloc.get_r_addend());
929 break;
931 case elfcpp::R_X86_64_PC64:
932 case elfcpp::R_X86_64_PC32:
933 case elfcpp::R_X86_64_PC16:
934 case elfcpp::R_X86_64_PC8:
935 break;
937 case elfcpp::R_X86_64_PLT32:
938 // Since we know this is a local symbol, we can handle this as a
939 // PC32 reloc.
940 break;
942 case elfcpp::R_X86_64_GOTPC32:
943 case elfcpp::R_X86_64_GOTOFF64:
944 case elfcpp::R_X86_64_GOTPC64:
945 case elfcpp::R_X86_64_PLTOFF64:
946 // We need a GOT section.
947 target->got_section(symtab, layout);
948 // For PLTOFF64, we'd normally want a PLT section, but since we
949 // know this is a local symbol, no PLT is needed.
950 break;
952 case elfcpp::R_X86_64_GOT64:
953 case elfcpp::R_X86_64_GOT32:
954 case elfcpp::R_X86_64_GOTPCREL64:
955 case elfcpp::R_X86_64_GOTPCREL:
956 case elfcpp::R_X86_64_GOTPLT64:
958 // The symbol requires a GOT entry.
959 Output_data_got<64, false>* got = target->got_section(symtab, layout);
960 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
961 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
963 // If we are generating a shared object, we need to add a
964 // dynamic relocation for this symbol's GOT entry.
965 if (parameters->options().output_is_position_independent())
967 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
968 // R_X86_64_RELATIVE assumes a 64-bit relocation.
969 if (r_type != elfcpp::R_X86_64_GOT32)
970 rela_dyn->add_local_relative(
971 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
972 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
973 else
975 this->check_non_pic(object, r_type);
977 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
978 rela_dyn->add_local(
979 object, r_sym, r_type, got,
980 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
984 // For GOTPLT64, we'd normally want a PLT section, but since
985 // we know this is a local symbol, no PLT is needed.
987 break;
989 case elfcpp::R_X86_64_COPY:
990 case elfcpp::R_X86_64_GLOB_DAT:
991 case elfcpp::R_X86_64_JUMP_SLOT:
992 case elfcpp::R_X86_64_RELATIVE:
993 // These are outstanding tls relocs, which are unexpected when linking
994 case elfcpp::R_X86_64_TPOFF64:
995 case elfcpp::R_X86_64_DTPMOD64:
996 case elfcpp::R_X86_64_TLSDESC:
997 gold_error(_("%s: unexpected reloc %u in object file"),
998 object->name().c_str(), r_type);
999 break;
1001 // These are initial tls relocs, which are expected when linking
1002 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1003 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1004 case elfcpp::R_X86_64_TLSDESC_CALL:
1005 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1006 case elfcpp::R_X86_64_DTPOFF32:
1007 case elfcpp::R_X86_64_DTPOFF64:
1008 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1009 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1011 bool output_is_shared = parameters->options().shared();
1012 const tls::Tls_optimization optimized_type
1013 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1014 switch (r_type)
1016 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1017 if (optimized_type == tls::TLSOPT_NONE)
1019 // Create a pair of GOT entries for the module index and
1020 // dtv-relative offset.
1021 Output_data_got<64, false>* got
1022 = target->got_section(symtab, layout);
1023 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1024 got->add_local_pair_with_rela(object, r_sym,
1025 lsym.get_st_shndx(),
1026 GOT_TYPE_TLS_PAIR,
1027 target->rela_dyn_section(layout),
1028 elfcpp::R_X86_64_DTPMOD64, 0);
1030 else if (optimized_type != tls::TLSOPT_TO_LE)
1031 unsupported_reloc_local(object, r_type);
1032 break;
1034 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1035 case elfcpp::R_X86_64_TLSDESC_CALL:
1036 // FIXME: If not relaxing to LE, we need to generate
1037 // a GOT entry with a R_x86_64_TLSDESC reloc.
1038 if (optimized_type != tls::TLSOPT_TO_LE)
1039 unsupported_reloc_local(object, r_type);
1040 break;
1042 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1043 if (optimized_type == tls::TLSOPT_NONE)
1045 // Create a GOT entry for the module index.
1046 target->got_mod_index_entry(symtab, layout, object);
1048 else if (optimized_type != tls::TLSOPT_TO_LE)
1049 unsupported_reloc_local(object, r_type);
1050 break;
1052 case elfcpp::R_X86_64_DTPOFF32:
1053 case elfcpp::R_X86_64_DTPOFF64:
1054 break;
1056 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1057 layout->set_has_static_tls();
1058 if (optimized_type == tls::TLSOPT_NONE)
1060 // Create a GOT entry for the tp-relative offset.
1061 Output_data_got<64, false>* got
1062 = target->got_section(symtab, layout);
1063 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1064 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1065 target->rela_dyn_section(layout),
1066 elfcpp::R_X86_64_TPOFF64);
1068 else if (optimized_type != tls::TLSOPT_TO_LE)
1069 unsupported_reloc_local(object, r_type);
1070 break;
1072 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1073 layout->set_has_static_tls();
1074 if (output_is_shared)
1075 unsupported_reloc_local(object, r_type);
1076 break;
1078 default:
1079 gold_unreachable();
1082 break;
1084 case elfcpp::R_X86_64_SIZE32:
1085 case elfcpp::R_X86_64_SIZE64:
1086 default:
1087 gold_error(_("%s: unsupported reloc %u against local symbol"),
1088 object->name().c_str(), r_type);
1089 break;
1094 // Report an unsupported relocation against a global symbol.
1096 void
1097 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1098 unsigned int r_type,
1099 Symbol* gsym)
1101 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1102 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1105 // Scan a relocation for a global symbol.
1107 inline void
1108 Target_x86_64::Scan::global(const General_options& options,
1109 Symbol_table* symtab,
1110 Layout* layout,
1111 Target_x86_64* target,
1112 Sized_relobj<64, false>* object,
1113 unsigned int data_shndx,
1114 Output_section* output_section,
1115 const elfcpp::Rela<64, false>& reloc,
1116 unsigned int r_type,
1117 Symbol* gsym)
1119 switch (r_type)
1121 case elfcpp::R_X86_64_NONE:
1122 case elfcpp::R_386_GNU_VTINHERIT:
1123 case elfcpp::R_386_GNU_VTENTRY:
1124 break;
1126 case elfcpp::R_X86_64_64:
1127 case elfcpp::R_X86_64_32:
1128 case elfcpp::R_X86_64_32S:
1129 case elfcpp::R_X86_64_16:
1130 case elfcpp::R_X86_64_8:
1132 // Make a PLT entry if necessary.
1133 if (gsym->needs_plt_entry())
1135 target->make_plt_entry(symtab, layout, gsym);
1136 // Since this is not a PC-relative relocation, we may be
1137 // taking the address of a function. In that case we need to
1138 // set the entry in the dynamic symbol table to the address of
1139 // the PLT entry.
1140 if (gsym->is_from_dynobj() && !parameters->options().shared())
1141 gsym->set_needs_dynsym_value();
1143 // Make a dynamic relocation if necessary.
1144 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1146 if (target->may_need_copy_reloc(gsym))
1148 target->copy_reloc(&options, symtab, layout, object,
1149 data_shndx, output_section, gsym, reloc);
1151 else if (r_type == elfcpp::R_X86_64_64
1152 && gsym->can_use_relative_reloc(false))
1154 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1155 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1156 output_section, object,
1157 data_shndx, reloc.get_r_offset(),
1158 reloc.get_r_addend());
1160 else
1162 this->check_non_pic(object, r_type);
1163 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1164 rela_dyn->add_global(gsym, r_type, output_section, object,
1165 data_shndx, reloc.get_r_offset(),
1166 reloc.get_r_addend());
1170 break;
1172 case elfcpp::R_X86_64_PC64:
1173 case elfcpp::R_X86_64_PC32:
1174 case elfcpp::R_X86_64_PC16:
1175 case elfcpp::R_X86_64_PC8:
1177 // Make a PLT entry if necessary.
1178 if (gsym->needs_plt_entry())
1179 target->make_plt_entry(symtab, layout, gsym);
1180 // Make a dynamic relocation if necessary.
1181 int flags = Symbol::NON_PIC_REF;
1182 if (gsym->type() == elfcpp::STT_FUNC)
1183 flags |= Symbol::FUNCTION_CALL;
1184 if (gsym->needs_dynamic_reloc(flags))
1186 if (target->may_need_copy_reloc(gsym))
1188 target->copy_reloc(&options, symtab, layout, object,
1189 data_shndx, output_section, gsym, reloc);
1191 else
1193 this->check_non_pic(object, r_type);
1194 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1195 rela_dyn->add_global(gsym, r_type, output_section, object,
1196 data_shndx, reloc.get_r_offset(),
1197 reloc.get_r_addend());
1201 break;
1203 case elfcpp::R_X86_64_GOT64:
1204 case elfcpp::R_X86_64_GOT32:
1205 case elfcpp::R_X86_64_GOTPCREL64:
1206 case elfcpp::R_X86_64_GOTPCREL:
1207 case elfcpp::R_X86_64_GOTPLT64:
1209 // The symbol requires a GOT entry.
1210 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1211 if (gsym->final_value_is_known())
1212 got->add_global(gsym, GOT_TYPE_STANDARD);
1213 else
1215 // If this symbol is not fully resolved, we need to add a
1216 // dynamic relocation for it.
1217 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1218 if (gsym->is_from_dynobj()
1219 || gsym->is_undefined()
1220 || gsym->is_preemptible())
1221 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1222 elfcpp::R_X86_64_GLOB_DAT);
1223 else
1225 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1226 rela_dyn->add_global_relative(
1227 gsym, elfcpp::R_X86_64_RELATIVE, got,
1228 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1231 // For GOTPLT64, we also need a PLT entry (but only if the
1232 // symbol is not fully resolved).
1233 if (r_type == elfcpp::R_X86_64_GOTPLT64
1234 && !gsym->final_value_is_known())
1235 target->make_plt_entry(symtab, layout, gsym);
1237 break;
1239 case elfcpp::R_X86_64_PLT32:
1240 // If the symbol is fully resolved, this is just a PC32 reloc.
1241 // Otherwise we need a PLT entry.
1242 if (gsym->final_value_is_known())
1243 break;
1244 // If building a shared library, we can also skip the PLT entry
1245 // if the symbol is defined in the output file and is protected
1246 // or hidden.
1247 if (gsym->is_defined()
1248 && !gsym->is_from_dynobj()
1249 && !gsym->is_preemptible())
1250 break;
1251 target->make_plt_entry(symtab, layout, gsym);
1252 break;
1254 case elfcpp::R_X86_64_GOTPC32:
1255 case elfcpp::R_X86_64_GOTOFF64:
1256 case elfcpp::R_X86_64_GOTPC64:
1257 case elfcpp::R_X86_64_PLTOFF64:
1258 // We need a GOT section.
1259 target->got_section(symtab, layout);
1260 // For PLTOFF64, we also need a PLT entry (but only if the
1261 // symbol is not fully resolved).
1262 if (r_type == elfcpp::R_X86_64_PLTOFF64
1263 && !gsym->final_value_is_known())
1264 target->make_plt_entry(symtab, layout, gsym);
1265 break;
1267 case elfcpp::R_X86_64_COPY:
1268 case elfcpp::R_X86_64_GLOB_DAT:
1269 case elfcpp::R_X86_64_JUMP_SLOT:
1270 case elfcpp::R_X86_64_RELATIVE:
1271 // These are outstanding tls relocs, which are unexpected when linking
1272 case elfcpp::R_X86_64_TPOFF64:
1273 case elfcpp::R_X86_64_DTPMOD64:
1274 case elfcpp::R_X86_64_TLSDESC:
1275 gold_error(_("%s: unexpected reloc %u in object file"),
1276 object->name().c_str(), r_type);
1277 break;
1279 // These are initial tls relocs, which are expected for global()
1280 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1281 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1282 case elfcpp::R_X86_64_TLSDESC_CALL:
1283 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1284 case elfcpp::R_X86_64_DTPOFF32:
1285 case elfcpp::R_X86_64_DTPOFF64:
1286 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1287 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1289 const bool is_final = gsym->final_value_is_known();
1290 const tls::Tls_optimization optimized_type
1291 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1292 switch (r_type)
1294 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1295 if (optimized_type == tls::TLSOPT_NONE)
1297 // Create a pair of GOT entries for the module index and
1298 // dtv-relative offset.
1299 Output_data_got<64, false>* got
1300 = target->got_section(symtab, layout);
1301 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1302 target->rela_dyn_section(layout),
1303 elfcpp::R_X86_64_DTPMOD64,
1304 elfcpp::R_X86_64_DTPOFF64);
1306 else if (optimized_type == tls::TLSOPT_TO_IE)
1308 // Create a GOT entry for the tp-relative offset.
1309 Output_data_got<64, false>* got
1310 = target->got_section(symtab, layout);
1311 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1312 target->rela_dyn_section(layout),
1313 elfcpp::R_X86_64_TPOFF64);
1315 else if (optimized_type != tls::TLSOPT_TO_LE)
1316 unsupported_reloc_global(object, r_type, gsym);
1317 break;
1319 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1320 case elfcpp::R_X86_64_TLSDESC_CALL:
1321 // FIXME: If not relaxing to LE, we need to generate
1322 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1323 if (optimized_type != tls::TLSOPT_TO_LE)
1324 unsupported_reloc_global(object, r_type, gsym);
1325 break;
1327 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1328 if (optimized_type == tls::TLSOPT_NONE)
1330 // Create a GOT entry for the module index.
1331 target->got_mod_index_entry(symtab, layout, object);
1333 else if (optimized_type != tls::TLSOPT_TO_LE)
1334 unsupported_reloc_global(object, r_type, gsym);
1335 break;
1337 case elfcpp::R_X86_64_DTPOFF32:
1338 case elfcpp::R_X86_64_DTPOFF64:
1339 break;
1341 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1342 layout->set_has_static_tls();
1343 if (optimized_type == tls::TLSOPT_NONE)
1345 // Create a GOT entry for the tp-relative offset.
1346 Output_data_got<64, false>* got
1347 = target->got_section(symtab, layout);
1348 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1349 target->rela_dyn_section(layout),
1350 elfcpp::R_X86_64_TPOFF64);
1352 else if (optimized_type != tls::TLSOPT_TO_LE)
1353 unsupported_reloc_global(object, r_type, gsym);
1354 break;
1356 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1357 layout->set_has_static_tls();
1358 if (parameters->options().shared())
1359 unsupported_reloc_local(object, r_type);
1360 break;
1362 default:
1363 gold_unreachable();
1366 break;
1368 case elfcpp::R_X86_64_SIZE32:
1369 case elfcpp::R_X86_64_SIZE64:
1370 default:
1371 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1372 object->name().c_str(), r_type,
1373 gsym->demangled_name().c_str());
1374 break;
1378 // Scan relocations for a section.
1380 void
1381 Target_x86_64::scan_relocs(const General_options& options,
1382 Symbol_table* symtab,
1383 Layout* layout,
1384 Sized_relobj<64, false>* object,
1385 unsigned int data_shndx,
1386 unsigned int sh_type,
1387 const unsigned char* prelocs,
1388 size_t reloc_count,
1389 Output_section* output_section,
1390 bool needs_special_offset_handling,
1391 size_t local_symbol_count,
1392 const unsigned char* plocal_symbols)
1394 if (sh_type == elfcpp::SHT_REL)
1396 gold_error(_("%s: unsupported REL reloc section"),
1397 object->name().c_str());
1398 return;
1401 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1402 Target_x86_64::Scan>(
1403 options,
1404 symtab,
1405 layout,
1406 this,
1407 object,
1408 data_shndx,
1409 prelocs,
1410 reloc_count,
1411 output_section,
1412 needs_special_offset_handling,
1413 local_symbol_count,
1414 plocal_symbols);
1417 // Finalize the sections.
1419 void
1420 Target_x86_64::do_finalize_sections(Layout* layout)
1422 // Fill in some more dynamic tags.
1423 Output_data_dynamic* const odyn = layout->dynamic_data();
1424 if (odyn != NULL)
1426 if (this->got_plt_ != NULL)
1427 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1429 if (this->plt_ != NULL)
1431 const Output_data* od = this->plt_->rel_plt();
1432 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1433 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1434 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1437 if (this->rela_dyn_ != NULL)
1439 const Output_data* od = this->rela_dyn_;
1440 odyn->add_section_address(elfcpp::DT_RELA, od);
1441 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1442 odyn->add_constant(elfcpp::DT_RELAENT,
1443 elfcpp::Elf_sizes<64>::rela_size);
1446 if (!parameters->options().shared())
1448 // The value of the DT_DEBUG tag is filled in by the dynamic
1449 // linker at run time, and used by the debugger.
1450 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1454 // Emit any relocs we saved in an attempt to avoid generating COPY
1455 // relocs.
1456 if (this->copy_relocs_ == NULL)
1457 return;
1458 if (this->copy_relocs_->any_to_emit())
1460 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1461 this->copy_relocs_->emit(rela_dyn);
1463 delete this->copy_relocs_;
1464 this->copy_relocs_ = NULL;
1467 // Perform a relocation.
1469 inline bool
1470 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1471 Target_x86_64* target,
1472 size_t relnum,
1473 const elfcpp::Rela<64, false>& rela,
1474 unsigned int r_type,
1475 const Sized_symbol<64>* gsym,
1476 const Symbol_value<64>* psymval,
1477 unsigned char* view,
1478 elfcpp::Elf_types<64>::Elf_Addr address,
1479 section_size_type view_size)
1481 if (this->skip_call_tls_get_addr_)
1483 if (r_type != elfcpp::R_X86_64_PLT32
1484 || gsym == NULL
1485 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1487 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1488 _("missing expected TLS relocation"));
1490 else
1492 this->skip_call_tls_get_addr_ = false;
1493 return false;
1497 // Pick the value to use for symbols defined in shared objects.
1498 Symbol_value<64> symval;
1499 if (gsym != NULL
1500 && (gsym->is_from_dynobj()
1501 || (parameters->options().shared()
1502 && (gsym->is_undefined() || gsym->is_preemptible())))
1503 && gsym->has_plt_offset())
1505 symval.set_output_value(target->plt_section()->address()
1506 + gsym->plt_offset());
1507 psymval = &symval;
1510 const Sized_relobj<64, false>* object = relinfo->object;
1511 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1513 // Get the GOT offset if needed.
1514 // The GOT pointer points to the end of the GOT section.
1515 // We need to subtract the size of the GOT section to get
1516 // the actual offset to use in the relocation.
1517 bool have_got_offset = false;
1518 unsigned int got_offset = 0;
1519 switch (r_type)
1521 case elfcpp::R_X86_64_GOT32:
1522 case elfcpp::R_X86_64_GOT64:
1523 case elfcpp::R_X86_64_GOTPLT64:
1524 case elfcpp::R_X86_64_GOTPCREL:
1525 case elfcpp::R_X86_64_GOTPCREL64:
1526 if (gsym != NULL)
1528 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1529 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1531 else
1533 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1534 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1535 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1536 - target->got_size());
1538 have_got_offset = true;
1539 break;
1541 default:
1542 break;
1545 switch (r_type)
1547 case elfcpp::R_X86_64_NONE:
1548 case elfcpp::R_386_GNU_VTINHERIT:
1549 case elfcpp::R_386_GNU_VTENTRY:
1550 break;
1552 case elfcpp::R_X86_64_64:
1553 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1554 break;
1556 case elfcpp::R_X86_64_PC64:
1557 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1558 address);
1559 break;
1561 case elfcpp::R_X86_64_32:
1562 // FIXME: we need to verify that value + addend fits into 32 bits:
1563 // uint64_t x = value + addend;
1564 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1565 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1566 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1567 break;
1569 case elfcpp::R_X86_64_32S:
1570 // FIXME: we need to verify that value + addend fits into 32 bits:
1571 // int64_t x = value + addend; // note this quantity is signed!
1572 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1573 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1574 break;
1576 case elfcpp::R_X86_64_PC32:
1577 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1578 address);
1579 break;
1581 case elfcpp::R_X86_64_16:
1582 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1583 break;
1585 case elfcpp::R_X86_64_PC16:
1586 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1587 address);
1588 break;
1590 case elfcpp::R_X86_64_8:
1591 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1592 break;
1594 case elfcpp::R_X86_64_PC8:
1595 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1596 address);
1597 break;
1599 case elfcpp::R_X86_64_PLT32:
1600 gold_assert(gsym == NULL
1601 || gsym->has_plt_offset()
1602 || gsym->final_value_is_known()
1603 || (gsym->is_defined()
1604 && !gsym->is_from_dynobj()
1605 && !gsym->is_preemptible()));
1606 // Note: while this code looks the same as for R_X86_64_PC32, it
1607 // behaves differently because psymval was set to point to
1608 // the PLT entry, rather than the symbol, in Scan::global().
1609 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1610 address);
1611 break;
1613 case elfcpp::R_X86_64_PLTOFF64:
1615 gold_assert(gsym);
1616 gold_assert(gsym->has_plt_offset()
1617 || gsym->final_value_is_known());
1618 elfcpp::Elf_types<64>::Elf_Addr got_address;
1619 got_address = target->got_section(NULL, NULL)->address();
1620 Relocate_functions<64, false>::rela64(view, object, psymval,
1621 addend - got_address);
1624 case elfcpp::R_X86_64_GOT32:
1625 gold_assert(have_got_offset);
1626 Relocate_functions<64, false>::rela32(view, got_offset, addend);
1627 break;
1629 case elfcpp::R_X86_64_GOTPC32:
1631 gold_assert(gsym);
1632 elfcpp::Elf_types<64>::Elf_Addr value;
1633 value = target->got_plt_section()->address();
1634 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1636 break;
1638 case elfcpp::R_X86_64_GOT64:
1639 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1640 // Since we always add a PLT entry, this is equivalent.
1641 case elfcpp::R_X86_64_GOTPLT64:
1642 gold_assert(have_got_offset);
1643 Relocate_functions<64, false>::rela64(view, got_offset, addend);
1644 break;
1646 case elfcpp::R_X86_64_GOTPC64:
1648 gold_assert(gsym);
1649 elfcpp::Elf_types<64>::Elf_Addr value;
1650 value = target->got_plt_section()->address();
1651 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1653 break;
1655 case elfcpp::R_X86_64_GOTOFF64:
1657 elfcpp::Elf_types<64>::Elf_Addr value;
1658 value = (psymval->value(object, 0)
1659 - target->got_plt_section()->address());
1660 Relocate_functions<64, false>::rela64(view, value, addend);
1662 break;
1664 case elfcpp::R_X86_64_GOTPCREL:
1666 gold_assert(have_got_offset);
1667 elfcpp::Elf_types<64>::Elf_Addr value;
1668 value = target->got_plt_section()->address() + got_offset;
1669 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1671 break;
1673 case elfcpp::R_X86_64_GOTPCREL64:
1675 gold_assert(have_got_offset);
1676 elfcpp::Elf_types<64>::Elf_Addr value;
1677 value = target->got_plt_section()->address() + got_offset;
1678 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1680 break;
1682 case elfcpp::R_X86_64_COPY:
1683 case elfcpp::R_X86_64_GLOB_DAT:
1684 case elfcpp::R_X86_64_JUMP_SLOT:
1685 case elfcpp::R_X86_64_RELATIVE:
1686 // These are outstanding tls relocs, which are unexpected when linking
1687 case elfcpp::R_X86_64_TPOFF64:
1688 case elfcpp::R_X86_64_DTPMOD64:
1689 case elfcpp::R_X86_64_TLSDESC:
1690 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1691 _("unexpected reloc %u in object file"),
1692 r_type);
1693 break;
1695 // These are initial tls relocs, which are expected when linking
1696 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1697 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1698 case elfcpp::R_X86_64_TLSDESC_CALL:
1699 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1700 case elfcpp::R_X86_64_DTPOFF32:
1701 case elfcpp::R_X86_64_DTPOFF64:
1702 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1703 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1704 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1705 view, address, view_size);
1706 break;
1708 case elfcpp::R_X86_64_SIZE32:
1709 case elfcpp::R_X86_64_SIZE64:
1710 default:
1711 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1712 _("unsupported reloc %u"),
1713 r_type);
1714 break;
1717 return true;
1720 // Perform a TLS relocation.
1722 inline void
1723 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1724 Target_x86_64* target,
1725 size_t relnum,
1726 const elfcpp::Rela<64, false>& rela,
1727 unsigned int r_type,
1728 const Sized_symbol<64>* gsym,
1729 const Symbol_value<64>* psymval,
1730 unsigned char* view,
1731 elfcpp::Elf_types<64>::Elf_Addr address,
1732 section_size_type view_size)
1734 Output_segment* tls_segment = relinfo->layout->tls_segment();
1736 const Sized_relobj<64, false>* object = relinfo->object;
1737 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1739 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1741 const bool is_final = (gsym == NULL
1742 ? !parameters->options().output_is_position_independent()
1743 : gsym->final_value_is_known());
1744 const tls::Tls_optimization optimized_type
1745 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1746 switch (r_type)
1748 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1749 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1750 case elfcpp::R_X86_64_TLSDESC_CALL:
1751 if (optimized_type == tls::TLSOPT_TO_LE)
1753 gold_assert(tls_segment != NULL);
1754 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1755 rela, r_type, value, view,
1756 view_size);
1757 break;
1759 else
1761 unsigned int got_offset;
1762 if (gsym != NULL)
1764 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_PAIR));
1765 got_offset = (gsym->got_offset(GOT_TYPE_TLS_PAIR)
1766 - target->got_size());
1768 else
1770 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1771 gold_assert(object->local_has_got_offset(r_sym,
1772 GOT_TYPE_TLS_PAIR));
1773 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR)
1774 - target->got_size());
1776 if (optimized_type == tls::TLSOPT_TO_IE)
1778 gold_assert(tls_segment != NULL);
1779 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1780 got_offset, view, view_size);
1781 break;
1783 else if (optimized_type == tls::TLSOPT_NONE)
1785 // Relocate the field with the offset of the pair of GOT
1786 // entries.
1787 value = target->got_plt_section()->address() + got_offset;
1788 Relocate_functions<64, false>::pcrela32(view, value, addend,
1789 address);
1790 break;
1793 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1794 _("unsupported reloc %u"), r_type);
1795 break;
1797 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1798 if (optimized_type == tls::TLSOPT_TO_LE)
1800 gold_assert(tls_segment != NULL);
1801 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1802 value, view, view_size);
1803 break;
1805 else if (optimized_type == tls::TLSOPT_NONE)
1807 // Relocate the field with the offset of the GOT entry for
1808 // the module index.
1809 unsigned int got_offset;
1810 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1811 - target->got_size());
1812 value = target->got_plt_section()->address() + got_offset;
1813 Relocate_functions<64, false>::pcrela32(view, value, addend,
1814 address);
1815 break;
1817 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1818 _("unsupported reloc %u"), r_type);
1819 break;
1821 case elfcpp::R_X86_64_DTPOFF32:
1822 gold_assert(tls_segment != NULL);
1823 if (optimized_type == tls::TLSOPT_TO_LE)
1824 value -= tls_segment->memsz();
1825 Relocate_functions<64, false>::rela32(view, value, 0);
1826 break;
1828 case elfcpp::R_X86_64_DTPOFF64:
1829 gold_assert(tls_segment != NULL);
1830 if (optimized_type == tls::TLSOPT_TO_LE)
1831 value -= tls_segment->memsz();
1832 Relocate_functions<64, false>::rela64(view, value, 0);
1833 break;
1835 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1836 if (optimized_type == tls::TLSOPT_TO_LE)
1838 gold_assert(tls_segment != NULL);
1839 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1840 rela, r_type, value, view,
1841 view_size);
1842 break;
1844 else if (optimized_type == tls::TLSOPT_NONE)
1846 // Relocate the field with the offset of the GOT entry for
1847 // the tp-relative offset of the symbol.
1848 unsigned int got_offset;
1849 if (gsym != NULL)
1851 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
1852 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
1853 - target->got_size());
1855 else
1857 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1858 gold_assert(object->local_has_got_offset(r_sym,
1859 GOT_TYPE_TLS_OFFSET));
1860 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
1861 - target->got_size());
1863 value = target->got_plt_section()->address() + got_offset;
1864 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1865 break;
1867 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1868 _("unsupported reloc type %u"),
1869 r_type);
1870 break;
1872 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1873 value -= tls_segment->memsz();
1874 Relocate_functions<64, false>::rela32(view, value, 0);
1875 break;
1879 // Do a relocation in which we convert a TLS General-Dynamic to an
1880 // Initial-Exec.
1882 inline void
1883 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1884 size_t relnum,
1885 Output_segment* tls_segment,
1886 const elfcpp::Rela<64, false>& rela,
1887 unsigned int,
1888 elfcpp::Elf_types<64>::Elf_Addr value,
1889 unsigned char* view,
1890 section_size_type view_size)
1892 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1893 // .word 0x6666; rex64; call __tls_get_addr
1894 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1896 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1897 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1899 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1900 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1901 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1902 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1904 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1906 value -= tls_segment->memsz();
1907 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1909 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1910 // We can skip it.
1911 this->skip_call_tls_get_addr_ = true;
1914 // Do a relocation in which we convert a TLS General-Dynamic to a
1915 // Local-Exec.
1917 inline void
1918 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1919 size_t relnum,
1920 Output_segment* tls_segment,
1921 const elfcpp::Rela<64, false>& rela,
1922 unsigned int,
1923 elfcpp::Elf_types<64>::Elf_Addr value,
1924 unsigned char* view,
1925 section_size_type view_size)
1927 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1928 // .word 0x6666; rex64; call __tls_get_addr
1929 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1931 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1932 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1934 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1935 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1936 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1937 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1939 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1941 value -= tls_segment->memsz();
1942 Relocate_functions<64, false>::rela32(view + 8, value, 0);
1944 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1945 // We can skip it.
1946 this->skip_call_tls_get_addr_ = true;
1949 inline void
1950 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1951 size_t relnum,
1952 Output_segment*,
1953 const elfcpp::Rela<64, false>& rela,
1954 unsigned int,
1955 elfcpp::Elf_types<64>::Elf_Addr,
1956 unsigned char* view,
1957 section_size_type view_size)
1959 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1960 // ... leq foo@dtpoff(%rax),%reg
1961 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1963 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1964 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1966 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1967 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1969 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1971 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1973 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1974 // We can skip it.
1975 this->skip_call_tls_get_addr_ = true;
1978 // Do a relocation in which we convert a TLS Initial-Exec to a
1979 // Local-Exec.
1981 inline void
1982 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1983 size_t relnum,
1984 Output_segment* tls_segment,
1985 const elfcpp::Rela<64, false>& rela,
1986 unsigned int,
1987 elfcpp::Elf_types<64>::Elf_Addr value,
1988 unsigned char* view,
1989 section_size_type view_size)
1991 // We need to examine the opcodes to figure out which instruction we
1992 // are looking at.
1994 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1995 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1997 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1998 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2000 unsigned char op1 = view[-3];
2001 unsigned char op2 = view[-2];
2002 unsigned char op3 = view[-1];
2003 unsigned char reg = op3 >> 3;
2005 if (op2 == 0x8b)
2007 // movq
2008 if (op1 == 0x4c)
2009 view[-3] = 0x49;
2010 view[-2] = 0xc7;
2011 view[-1] = 0xc0 | reg;
2013 else if (reg == 4)
2015 // Special handling for %rsp.
2016 if (op1 == 0x4c)
2017 view[-3] = 0x49;
2018 view[-2] = 0x81;
2019 view[-1] = 0xc0 | reg;
2021 else
2023 // addq
2024 if (op1 == 0x4c)
2025 view[-3] = 0x4d;
2026 view[-2] = 0x8d;
2027 view[-1] = 0x80 | reg | (reg << 3);
2030 value -= tls_segment->memsz();
2031 Relocate_functions<64, false>::rela32(view, value, 0);
2034 // Relocate section data.
2036 void
2037 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2038 unsigned int sh_type,
2039 const unsigned char* prelocs,
2040 size_t reloc_count,
2041 Output_section* output_section,
2042 bool needs_special_offset_handling,
2043 unsigned char* view,
2044 elfcpp::Elf_types<64>::Elf_Addr address,
2045 section_size_type view_size)
2047 gold_assert(sh_type == elfcpp::SHT_RELA);
2049 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2050 Target_x86_64::Relocate>(
2051 relinfo,
2052 this,
2053 prelocs,
2054 reloc_count,
2055 output_section,
2056 needs_special_offset_handling,
2057 view,
2058 address,
2059 view_size);
2062 // Return the size of a relocation while scanning during a relocatable
2063 // link.
2065 unsigned int
2066 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2067 unsigned int r_type,
2068 Relobj* object)
2070 switch (r_type)
2072 case elfcpp::R_X86_64_NONE:
2073 case elfcpp::R_386_GNU_VTINHERIT:
2074 case elfcpp::R_386_GNU_VTENTRY:
2075 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2076 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2077 case elfcpp::R_X86_64_TLSDESC_CALL:
2078 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2079 case elfcpp::R_X86_64_DTPOFF32:
2080 case elfcpp::R_X86_64_DTPOFF64:
2081 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2082 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2083 return 0;
2085 case elfcpp::R_X86_64_64:
2086 case elfcpp::R_X86_64_PC64:
2087 case elfcpp::R_X86_64_GOTOFF64:
2088 case elfcpp::R_X86_64_GOTPC64:
2089 case elfcpp::R_X86_64_PLTOFF64:
2090 case elfcpp::R_X86_64_GOT64:
2091 case elfcpp::R_X86_64_GOTPCREL64:
2092 case elfcpp::R_X86_64_GOTPCREL:
2093 case elfcpp::R_X86_64_GOTPLT64:
2094 return 8;
2096 case elfcpp::R_X86_64_32:
2097 case elfcpp::R_X86_64_32S:
2098 case elfcpp::R_X86_64_PC32:
2099 case elfcpp::R_X86_64_PLT32:
2100 case elfcpp::R_X86_64_GOTPC32:
2101 case elfcpp::R_X86_64_GOT32:
2102 return 4;
2104 case elfcpp::R_X86_64_16:
2105 case elfcpp::R_X86_64_PC16:
2106 return 2;
2108 case elfcpp::R_X86_64_8:
2109 case elfcpp::R_X86_64_PC8:
2110 return 1;
2112 case elfcpp::R_X86_64_COPY:
2113 case elfcpp::R_X86_64_GLOB_DAT:
2114 case elfcpp::R_X86_64_JUMP_SLOT:
2115 case elfcpp::R_X86_64_RELATIVE:
2116 // These are outstanding tls relocs, which are unexpected when linking
2117 case elfcpp::R_X86_64_TPOFF64:
2118 case elfcpp::R_X86_64_DTPMOD64:
2119 case elfcpp::R_X86_64_TLSDESC:
2120 object->error(_("unexpected reloc %u in object file"), r_type);
2121 return 0;
2123 case elfcpp::R_X86_64_SIZE32:
2124 case elfcpp::R_X86_64_SIZE64:
2125 default:
2126 object->error(_("unsupported reloc %u against local symbol"), r_type);
2127 return 0;
2131 // Scan the relocs during a relocatable link.
2133 void
2134 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2135 Symbol_table* symtab,
2136 Layout* layout,
2137 Sized_relobj<64, false>* object,
2138 unsigned int data_shndx,
2139 unsigned int sh_type,
2140 const unsigned char* prelocs,
2141 size_t reloc_count,
2142 Output_section* output_section,
2143 bool needs_special_offset_handling,
2144 size_t local_symbol_count,
2145 const unsigned char* plocal_symbols,
2146 Relocatable_relocs* rr)
2148 gold_assert(sh_type == elfcpp::SHT_RELA);
2150 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2151 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2153 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2154 Scan_relocatable_relocs>(
2155 options,
2156 symtab,
2157 layout,
2158 object,
2159 data_shndx,
2160 prelocs,
2161 reloc_count,
2162 output_section,
2163 needs_special_offset_handling,
2164 local_symbol_count,
2165 plocal_symbols,
2166 rr);
2169 // Relocate a section during a relocatable link.
2171 void
2172 Target_x86_64::relocate_for_relocatable(
2173 const Relocate_info<64, false>* relinfo,
2174 unsigned int sh_type,
2175 const unsigned char* prelocs,
2176 size_t reloc_count,
2177 Output_section* output_section,
2178 off_t offset_in_output_section,
2179 const Relocatable_relocs* rr,
2180 unsigned char* view,
2181 elfcpp::Elf_types<64>::Elf_Addr view_address,
2182 section_size_type view_size,
2183 unsigned char* reloc_view,
2184 section_size_type reloc_view_size)
2186 gold_assert(sh_type == elfcpp::SHT_RELA);
2188 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2189 relinfo,
2190 prelocs,
2191 reloc_count,
2192 output_section,
2193 offset_in_output_section,
2195 view,
2196 view_address,
2197 view_size,
2198 reloc_view,
2199 reloc_view_size);
2202 // Return the value to use for a dynamic which requires special
2203 // treatment. This is how we support equality comparisons of function
2204 // pointers across shared library boundaries, as described in the
2205 // processor specific ABI supplement.
2207 uint64_t
2208 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2210 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2211 return this->plt_section()->address() + gsym->plt_offset();
2214 // Return a string used to fill a code section with nops to take up
2215 // the specified length.
2217 std::string
2218 Target_x86_64::do_code_fill(section_size_type length) const
2220 if (length >= 16)
2222 // Build a jmpq instruction to skip over the bytes.
2223 unsigned char jmp[5];
2224 jmp[0] = 0xe9;
2225 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2226 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2227 + std::string(length - 5, '\0'));
2230 // Nop sequences of various lengths.
2231 const char nop1[1] = { 0x90 }; // nop
2232 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2233 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2234 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2235 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2236 0x00 }; // leal 0(%esi,1),%esi
2237 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2238 0x00, 0x00 };
2239 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2240 0x00, 0x00, 0x00 };
2241 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2242 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2243 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2244 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2245 0x00 };
2246 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2247 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2248 0x00, 0x00 };
2249 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2250 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2251 0x00, 0x00, 0x00 };
2252 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2253 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2254 0x00, 0x00, 0x00, 0x00 };
2255 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2256 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2257 0x27, 0x00, 0x00, 0x00,
2258 0x00 };
2259 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2260 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2261 0xbc, 0x27, 0x00, 0x00,
2262 0x00, 0x00 };
2263 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2264 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2265 0x90, 0x90, 0x90, 0x90,
2266 0x90, 0x90, 0x90 };
2268 const char* nops[16] = {
2269 NULL,
2270 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2271 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2274 return std::string(nops[length], length);
2277 // The selector for x86_64 object files.
2279 class Target_selector_x86_64 : public Target_selector
2281 public:
2282 Target_selector_x86_64()
2283 : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64")
2286 Target*
2287 do_instantiate_target()
2288 { return new Target_x86_64(); }
2291 Target_selector_x86_64 target_selector_x86_64;
2293 } // End anonymous namespace.