Add a testcase for PR gas/11867.
[binutils.git] / gold / x86_64.cc
blob54f07db9701f6550c35e42ec64ee4605815eeae7
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
42 #include "icf.h"
44 namespace
47 using namespace gold;
49 class Output_data_plt_x86_64;
51 // The x86_64 target class.
52 // See the ABI at
53 // http://www.x86-64.org/documentation/abi.pdf
54 // TLS info comes from
55 // http://people.redhat.com/drepper/tls.pdf
56 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
58 class Target_x86_64 : public Target_freebsd<64, false>
60 public:
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
65 Target_x86_64()
66 : Target_freebsd<64, false>(&x86_64_info),
67 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
68 global_offset_table_(NULL), rela_dyn_(NULL),
69 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
70 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
71 tls_base_symbol_defined_(false)
72 { }
74 // This function should be defined in targets that can use relocation
75 // types to determine (implemented in local_reloc_may_be_function_pointer
76 // and global_reloc_may_be_function_pointer)
77 // if a function's pointer is taken. ICF uses this in safe mode to only
78 // fold those functions whose pointer is defintely not taken. For x86_64
79 // pie binaries, safe ICF cannot be done by looking at relocation types.
80 inline bool
81 can_check_for_function_pointers() const
82 { return !parameters->options().pie(); }
84 // Hook for a new output section.
85 void
86 do_new_output_section(Output_section*) const;
88 // Scan the relocations to look for symbol adjustments.
89 void
90 gc_process_relocs(Symbol_table* symtab,
91 Layout* layout,
92 Sized_relobj<64, false>* object,
93 unsigned int data_shndx,
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 size_t local_symbol_count,
100 const unsigned char* plocal_symbols);
102 // Scan the relocations to look for symbol adjustments.
103 void
104 scan_relocs(Symbol_table* symtab,
105 Layout* layout,
106 Sized_relobj<64, false>* object,
107 unsigned int data_shndx,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
110 size_t reloc_count,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
113 size_t local_symbol_count,
114 const unsigned char* plocal_symbols);
116 // Finalize the sections.
117 void
118 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
120 // Return the value to use for a dynamic which requires special
121 // treatment.
122 uint64_t
123 do_dynsym_value(const Symbol*) const;
125 // Relocate a section.
126 void
127 relocate_section(const Relocate_info<64, false>*,
128 unsigned int sh_type,
129 const unsigned char* prelocs,
130 size_t reloc_count,
131 Output_section* output_section,
132 bool needs_special_offset_handling,
133 unsigned char* view,
134 elfcpp::Elf_types<64>::Elf_Addr view_address,
135 section_size_type view_size,
136 const Reloc_symbol_changes*);
138 // Scan the relocs during a relocatable link.
139 void
140 scan_relocatable_relocs(Symbol_table* symtab,
141 Layout* layout,
142 Sized_relobj<64, false>* object,
143 unsigned int data_shndx,
144 unsigned int sh_type,
145 const unsigned char* prelocs,
146 size_t reloc_count,
147 Output_section* output_section,
148 bool needs_special_offset_handling,
149 size_t local_symbol_count,
150 const unsigned char* plocal_symbols,
151 Relocatable_relocs*);
153 // Relocate a section during a relocatable link.
154 void
155 relocate_for_relocatable(const Relocate_info<64, false>*,
156 unsigned int sh_type,
157 const unsigned char* prelocs,
158 size_t reloc_count,
159 Output_section* output_section,
160 off_t offset_in_output_section,
161 const Relocatable_relocs*,
162 unsigned char* view,
163 elfcpp::Elf_types<64>::Elf_Addr view_address,
164 section_size_type view_size,
165 unsigned char* reloc_view,
166 section_size_type reloc_view_size);
168 // Return a string used to fill a code section with nops.
169 std::string
170 do_code_fill(section_size_type length) const;
172 // Return whether SYM is defined by the ABI.
173 bool
174 do_is_defined_by_abi(const Symbol* sym) const
175 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
177 // Return the symbol index to use for a target specific relocation.
178 // The only target specific relocation is R_X86_64_TLSDESC for a
179 // local symbol, which is an absolute reloc.
180 unsigned int
181 do_reloc_symbol_index(void*, unsigned int r_type) const
183 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
184 return 0;
187 // Return the addend to use for a target specific relocation.
188 uint64_t
189 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
191 // Adjust -fstack-split code which calls non-stack-split code.
192 void
193 do_calls_non_split(Relobj* object, unsigned int shndx,
194 section_offset_type fnoffset, section_size_type fnsize,
195 unsigned char* view, section_size_type view_size,
196 std::string* from, std::string* to) const;
198 // Return the size of the GOT section.
199 section_size_type
200 got_size()
202 gold_assert(this->got_ != NULL);
203 return this->got_->data_size();
206 // Add a new reloc argument, returning the index in the vector.
207 size_t
208 add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
210 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
211 return this->tlsdesc_reloc_info_.size() - 1;
214 private:
215 // The class which scans relocations.
216 class Scan
218 public:
219 Scan()
220 : issued_non_pic_error_(false)
223 inline void
224 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
225 Sized_relobj<64, false>* object,
226 unsigned int data_shndx,
227 Output_section* output_section,
228 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
229 const elfcpp::Sym<64, false>& lsym);
231 inline void
232 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
233 Sized_relobj<64, false>* object,
234 unsigned int data_shndx,
235 Output_section* output_section,
236 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
237 Symbol* gsym);
239 inline bool
240 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
241 Target_x86_64* target,
242 Sized_relobj<64, false>* object,
243 unsigned int data_shndx,
244 Output_section* output_section,
245 const elfcpp::Rela<64, false>& reloc,
246 unsigned int r_type,
247 const elfcpp::Sym<64, false>& lsym);
249 inline bool
250 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
251 Target_x86_64* target,
252 Sized_relobj<64, false>* object,
253 unsigned int data_shndx,
254 Output_section* output_section,
255 const elfcpp::Rela<64, false>& reloc,
256 unsigned int r_type,
257 Symbol* gsym);
259 private:
260 static void
261 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
263 static void
264 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
265 Symbol*);
267 void
268 check_non_pic(Relobj*, unsigned int r_type);
270 inline bool
271 possible_function_pointer_reloc(unsigned int r_type);
273 // Whether we have issued an error about a non-PIC compilation.
274 bool issued_non_pic_error_;
277 // The class which implements relocation.
278 class Relocate
280 public:
281 Relocate()
282 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
285 ~Relocate()
287 if (this->skip_call_tls_get_addr_)
289 // FIXME: This needs to specify the location somehow.
290 gold_error(_("missing expected TLS relocation"));
294 // Do a relocation. Return false if the caller should not issue
295 // any warnings about this relocation.
296 inline bool
297 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
298 size_t relnum, const elfcpp::Rela<64, false>&,
299 unsigned int r_type, const Sized_symbol<64>*,
300 const Symbol_value<64>*,
301 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
302 section_size_type);
304 private:
305 // Do a TLS relocation.
306 inline void
307 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
308 size_t relnum, const elfcpp::Rela<64, false>&,
309 unsigned int r_type, const Sized_symbol<64>*,
310 const Symbol_value<64>*,
311 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
312 section_size_type);
314 // Do a TLS General-Dynamic to Initial-Exec transition.
315 inline void
316 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
317 Output_segment* tls_segment,
318 const elfcpp::Rela<64, false>&, unsigned int r_type,
319 elfcpp::Elf_types<64>::Elf_Addr value,
320 unsigned char* view,
321 elfcpp::Elf_types<64>::Elf_Addr,
322 section_size_type view_size);
324 // Do a TLS General-Dynamic to Local-Exec transition.
325 inline void
326 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
327 Output_segment* tls_segment,
328 const elfcpp::Rela<64, false>&, unsigned int r_type,
329 elfcpp::Elf_types<64>::Elf_Addr value,
330 unsigned char* view,
331 section_size_type view_size);
333 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
334 inline void
335 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
336 Output_segment* tls_segment,
337 const elfcpp::Rela<64, false>&, unsigned int r_type,
338 elfcpp::Elf_types<64>::Elf_Addr value,
339 unsigned char* view,
340 elfcpp::Elf_types<64>::Elf_Addr,
341 section_size_type view_size);
343 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
344 inline void
345 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
346 Output_segment* tls_segment,
347 const elfcpp::Rela<64, false>&, unsigned int r_type,
348 elfcpp::Elf_types<64>::Elf_Addr value,
349 unsigned char* view,
350 section_size_type view_size);
352 // Do a TLS Local-Dynamic to Local-Exec transition.
353 inline void
354 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
355 Output_segment* tls_segment,
356 const elfcpp::Rela<64, false>&, unsigned int r_type,
357 elfcpp::Elf_types<64>::Elf_Addr value,
358 unsigned char* view,
359 section_size_type view_size);
361 // Do a TLS Initial-Exec to Local-Exec transition.
362 static inline void
363 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
364 Output_segment* tls_segment,
365 const elfcpp::Rela<64, false>&, unsigned int r_type,
366 elfcpp::Elf_types<64>::Elf_Addr value,
367 unsigned char* view,
368 section_size_type view_size);
370 // This is set if we should skip the next reloc, which should be a
371 // PLT32 reloc against ___tls_get_addr.
372 bool skip_call_tls_get_addr_;
374 // This is set if we see a relocation which could load the address
375 // of the TLS block. Whether we see such a relocation determines
376 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
377 // in debugging sections.
378 bool saw_tls_block_reloc_;
381 // A class which returns the size required for a relocation type,
382 // used while scanning relocs during a relocatable link.
383 class Relocatable_size_for_reloc
385 public:
386 unsigned int
387 get_size_for_reloc(unsigned int, Relobj*);
390 // Adjust TLS relocation type based on the options and whether this
391 // is a local symbol.
392 static tls::Tls_optimization
393 optimize_tls_reloc(bool is_final, int r_type);
395 // Get the GOT section, creating it if necessary.
396 Output_data_got<64, false>*
397 got_section(Symbol_table*, Layout*);
399 // Get the GOT PLT section.
400 Output_data_space*
401 got_plt_section() const
403 gold_assert(this->got_plt_ != NULL);
404 return this->got_plt_;
407 // Get the GOT section for TLSDESC entries.
408 Output_data_got<64, false>*
409 got_tlsdesc_section() const
411 gold_assert(this->got_tlsdesc_ != NULL);
412 return this->got_tlsdesc_;
415 // Create the PLT section.
416 void
417 make_plt_section(Symbol_table* symtab, Layout* layout);
419 // Create a PLT entry for a global symbol.
420 void
421 make_plt_entry(Symbol_table*, Layout*, Symbol*);
423 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
424 void
425 define_tls_base_symbol(Symbol_table*, Layout*);
427 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
428 void
429 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
431 // Create a GOT entry for the TLS module index.
432 unsigned int
433 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
434 Sized_relobj<64, false>* object);
436 // Get the PLT section.
437 Output_data_plt_x86_64*
438 plt_section() const
440 gold_assert(this->plt_ != NULL);
441 return this->plt_;
444 // Get the dynamic reloc section, creating it if necessary.
445 Reloc_section*
446 rela_dyn_section(Layout*);
448 // Get the section to use for TLSDESC relocations.
449 Reloc_section*
450 rela_tlsdesc_section(Layout*) const;
452 // Add a potential copy relocation.
453 void
454 copy_reloc(Symbol_table* symtab, Layout* layout,
455 Sized_relobj<64, false>* object,
456 unsigned int shndx, Output_section* output_section,
457 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
459 this->copy_relocs_.copy_reloc(symtab, layout,
460 symtab->get_sized_symbol<64>(sym),
461 object, shndx, output_section,
462 reloc, this->rela_dyn_section(layout));
465 // Information about this specific target which we pass to the
466 // general Target structure.
467 static const Target::Target_info x86_64_info;
469 enum Got_type
471 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
472 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
473 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
474 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
477 // This type is used as the argument to the target specific
478 // relocation routines. The only target specific reloc is
479 // R_X86_64_TLSDESC against a local symbol.
480 struct Tlsdesc_info
482 Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
483 : object(a_object), r_sym(a_r_sym)
486 // The object in which the local symbol is defined.
487 Sized_relobj<64, false>* object;
488 // The local symbol index in the object.
489 unsigned int r_sym;
492 // The GOT section.
493 Output_data_got<64, false>* got_;
494 // The PLT section.
495 Output_data_plt_x86_64* plt_;
496 // The GOT PLT section.
497 Output_data_space* got_plt_;
498 // The GOT section for TLSDESC relocations.
499 Output_data_got<64, false>* got_tlsdesc_;
500 // The _GLOBAL_OFFSET_TABLE_ symbol.
501 Symbol* global_offset_table_;
502 // The dynamic reloc section.
503 Reloc_section* rela_dyn_;
504 // Relocs saved to avoid a COPY reloc.
505 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
506 // Space for variables copied with a COPY reloc.
507 Output_data_space* dynbss_;
508 // Offset of the GOT entry for the TLS module index.
509 unsigned int got_mod_index_offset_;
510 // We handle R_X86_64_TLSDESC against a local symbol as a target
511 // specific relocation. Here we store the object and local symbol
512 // index for the relocation.
513 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
514 // True if the _TLS_MODULE_BASE_ symbol has been defined.
515 bool tls_base_symbol_defined_;
518 const Target::Target_info Target_x86_64::x86_64_info =
520 64, // size
521 false, // is_big_endian
522 elfcpp::EM_X86_64, // machine_code
523 false, // has_make_symbol
524 false, // has_resolve
525 true, // has_code_fill
526 true, // is_default_stack_executable
527 '\0', // wrap_char
528 "/lib/ld64.so.1", // program interpreter
529 0x400000, // default_text_segment_address
530 0x1000, // abi_pagesize (overridable by -z max-page-size)
531 0x1000, // common_pagesize (overridable by -z common-page-size)
532 elfcpp::SHN_UNDEF, // small_common_shndx
533 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
534 0, // small_common_section_flags
535 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
536 NULL, // attributes_section
537 NULL // attributes_vendor
540 // This is called when a new output section is created. This is where
541 // we handle the SHF_X86_64_LARGE.
543 void
544 Target_x86_64::do_new_output_section(Output_section *os) const
546 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
547 os->set_is_large_section();
550 // Get the GOT section, creating it if necessary.
552 Output_data_got<64, false>*
553 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
555 if (this->got_ == NULL)
557 gold_assert(symtab != NULL && layout != NULL);
559 this->got_ = new Output_data_got<64, false>();
561 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
562 (elfcpp::SHF_ALLOC
563 | elfcpp::SHF_WRITE),
564 this->got_, false, true, true, false);
566 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
567 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
568 (elfcpp::SHF_ALLOC
569 | elfcpp::SHF_WRITE),
570 this->got_plt_, false, false, false,
571 true);
573 // The first three entries are reserved.
574 this->got_plt_->set_current_data_size(3 * 8);
576 // Those bytes can go into the relro segment.
577 layout->increase_relro(3 * 8);
579 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
580 this->global_offset_table_ =
581 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
582 Symbol_table::PREDEFINED,
583 this->got_plt_,
584 0, 0, elfcpp::STT_OBJECT,
585 elfcpp::STB_LOCAL,
586 elfcpp::STV_HIDDEN, 0,
587 false, false);
589 // If there are any TLSDESC relocations, they get GOT entries in
590 // .got.plt after the jump slot entries.
591 this->got_tlsdesc_ = new Output_data_got<64, false>();
592 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
593 (elfcpp::SHF_ALLOC
594 | elfcpp::SHF_WRITE),
595 this->got_tlsdesc_, false, false, false,
596 true);
599 return this->got_;
602 // Get the dynamic reloc section, creating it if necessary.
604 Target_x86_64::Reloc_section*
605 Target_x86_64::rela_dyn_section(Layout* layout)
607 if (this->rela_dyn_ == NULL)
609 gold_assert(layout != NULL);
610 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
611 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
612 elfcpp::SHF_ALLOC, this->rela_dyn_, true,
613 false, false, false);
615 return this->rela_dyn_;
618 // A class to handle the PLT data.
620 class Output_data_plt_x86_64 : public Output_section_data
622 public:
623 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
625 Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
626 Output_data_space*);
628 // Add an entry to the PLT.
629 void
630 add_entry(Symbol* gsym);
632 // Add the reserved TLSDESC_PLT entry to the PLT.
633 void
634 reserve_tlsdesc_entry(unsigned int got_offset)
635 { this->tlsdesc_got_offset_ = got_offset; }
637 // Return true if a TLSDESC_PLT entry has been reserved.
638 bool
639 has_tlsdesc_entry() const
640 { return this->tlsdesc_got_offset_ != -1U; }
642 // Return the GOT offset for the reserved TLSDESC_PLT entry.
643 unsigned int
644 get_tlsdesc_got_offset() const
645 { return this->tlsdesc_got_offset_; }
647 // Return the offset of the reserved TLSDESC_PLT entry.
648 unsigned int
649 get_tlsdesc_plt_offset() const
650 { return (this->count_ + 1) * plt_entry_size; }
652 // Return the .rela.plt section data.
653 const Reloc_section*
654 rela_plt() const
655 { return this->rel_; }
657 // Return where the TLSDESC relocations should go.
658 Reloc_section*
659 rela_tlsdesc(Layout*);
661 protected:
662 void
663 do_adjust_output_section(Output_section* os);
665 // Write to a map file.
666 void
667 do_print_to_mapfile(Mapfile* mapfile) const
668 { mapfile->print_output_data(this, _("** PLT")); }
670 private:
671 // The size of an entry in the PLT.
672 static const int plt_entry_size = 16;
674 // The first entry in the PLT.
675 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
676 // procedure linkage table for both programs and shared objects."
677 static unsigned char first_plt_entry[plt_entry_size];
679 // Other entries in the PLT for an executable.
680 static unsigned char plt_entry[plt_entry_size];
682 // The reserved TLSDESC entry in the PLT for an executable.
683 static unsigned char tlsdesc_plt_entry[plt_entry_size];
685 // Set the final size.
686 void
687 set_final_data_size();
689 // Write out the PLT data.
690 void
691 do_write(Output_file*);
693 // The reloc section.
694 Reloc_section* rel_;
695 // The TLSDESC relocs, if necessary. These must follow the regular
696 // PLT relocs.
697 Reloc_section* tlsdesc_rel_;
698 // The .got section.
699 Output_data_got<64, false>* got_;
700 // The .got.plt section.
701 Output_data_space* got_plt_;
702 // The number of PLT entries.
703 unsigned int count_;
704 // Offset of the reserved TLSDESC_GOT entry when needed.
705 unsigned int tlsdesc_got_offset_;
708 // Create the PLT section. The ordinary .got section is an argument,
709 // since we need to refer to the start. We also create our own .got
710 // section just for PLT entries.
712 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
713 Output_data_got<64, false>* got,
714 Output_data_space* got_plt)
715 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
716 count_(0), tlsdesc_got_offset_(-1U)
718 this->rel_ = new Reloc_section(false);
719 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
720 elfcpp::SHF_ALLOC, this->rel_, true,
721 false, false, false);
724 void
725 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
727 os->set_entsize(plt_entry_size);
730 // Add an entry to the PLT.
732 void
733 Output_data_plt_x86_64::add_entry(Symbol* gsym)
735 gold_assert(!gsym->has_plt_offset());
737 // Note that when setting the PLT offset we skip the initial
738 // reserved PLT entry.
739 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
741 ++this->count_;
743 section_offset_type got_offset = this->got_plt_->current_data_size();
745 // Every PLT entry needs a GOT entry which points back to the PLT
746 // entry (this will be changed by the dynamic linker, normally
747 // lazily when the function is called).
748 this->got_plt_->set_current_data_size(got_offset + 8);
750 // Every PLT entry needs a reloc.
751 gsym->set_needs_dynsym_entry();
752 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
753 got_offset, 0);
755 // Note that we don't need to save the symbol. The contents of the
756 // PLT are independent of which symbols are used. The symbols only
757 // appear in the relocations.
760 // Return where the TLSDESC relocations should go, creating it if
761 // necessary. These follow the JUMP_SLOT relocations.
763 Output_data_plt_x86_64::Reloc_section*
764 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
766 if (this->tlsdesc_rel_ == NULL)
768 this->tlsdesc_rel_ = new Reloc_section(false);
769 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
770 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
771 true, false, false, false);
772 gold_assert(this->tlsdesc_rel_->output_section() ==
773 this->rel_->output_section());
775 return this->tlsdesc_rel_;
778 // Set the final size.
779 void
780 Output_data_plt_x86_64::set_final_data_size()
782 unsigned int count = this->count_;
783 if (this->has_tlsdesc_entry())
784 ++count;
785 this->set_data_size((count + 1) * plt_entry_size);
788 // The first entry in the PLT for an executable.
790 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
792 // From AMD64 ABI Draft 0.98, page 76
793 0xff, 0x35, // pushq contents of memory address
794 0, 0, 0, 0, // replaced with address of .got + 8
795 0xff, 0x25, // jmp indirect
796 0, 0, 0, 0, // replaced with address of .got + 16
797 0x90, 0x90, 0x90, 0x90 // noop (x4)
800 // Subsequent entries in the PLT for an executable.
802 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
804 // From AMD64 ABI Draft 0.98, page 76
805 0xff, 0x25, // jmpq indirect
806 0, 0, 0, 0, // replaced with address of symbol in .got
807 0x68, // pushq immediate
808 0, 0, 0, 0, // replaced with offset into relocation table
809 0xe9, // jmpq relative
810 0, 0, 0, 0 // replaced with offset to start of .plt
813 // The reserved TLSDESC entry in the PLT for an executable.
815 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
817 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
818 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
819 0xff, 0x35, // pushq x(%rip)
820 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
821 0xff, 0x25, // jmpq *y(%rip)
822 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
823 0x0f, 0x1f, // nop
824 0x40, 0
827 // Write out the PLT. This uses the hand-coded instructions above,
828 // and adjusts them as needed. This is specified by the AMD64 ABI.
830 void
831 Output_data_plt_x86_64::do_write(Output_file* of)
833 const off_t offset = this->offset();
834 const section_size_type oview_size =
835 convert_to_section_size_type(this->data_size());
836 unsigned char* const oview = of->get_output_view(offset, oview_size);
838 const off_t got_file_offset = this->got_plt_->offset();
839 const section_size_type got_size =
840 convert_to_section_size_type(this->got_plt_->data_size());
841 unsigned char* const got_view = of->get_output_view(got_file_offset,
842 got_size);
844 unsigned char* pov = oview;
846 // The base address of the .plt section.
847 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
848 // The base address of the .got section.
849 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
850 // The base address of the PLT portion of the .got section,
851 // which is where the GOT pointer will point, and where the
852 // three reserved GOT entries are located.
853 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
855 memcpy(pov, first_plt_entry, plt_entry_size);
856 // We do a jmp relative to the PC at the end of this instruction.
857 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
858 (got_address + 8
859 - (plt_address + 6)));
860 elfcpp::Swap<32, false>::writeval(pov + 8,
861 (got_address + 16
862 - (plt_address + 12)));
863 pov += plt_entry_size;
865 unsigned char* got_pov = got_view;
867 memset(got_pov, 0, 24);
868 got_pov += 24;
870 unsigned int plt_offset = plt_entry_size;
871 unsigned int got_offset = 24;
872 const unsigned int count = this->count_;
873 for (unsigned int plt_index = 0;
874 plt_index < count;
875 ++plt_index,
876 pov += plt_entry_size,
877 got_pov += 8,
878 plt_offset += plt_entry_size,
879 got_offset += 8)
881 // Set and adjust the PLT entry itself.
882 memcpy(pov, plt_entry, plt_entry_size);
883 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
884 (got_address + got_offset
885 - (plt_address + plt_offset
886 + 6)));
888 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
889 elfcpp::Swap<32, false>::writeval(pov + 12,
890 - (plt_offset + plt_entry_size));
892 // Set the entry in the GOT.
893 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
896 if (this->has_tlsdesc_entry())
898 // Set and adjust the reserved TLSDESC PLT entry.
899 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
900 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
901 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
902 (got_address + 8
903 - (plt_address + plt_offset
904 + 6)));
905 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
906 (got_base
907 + tlsdesc_got_offset
908 - (plt_address + plt_offset
909 + 12)));
910 pov += plt_entry_size;
913 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
914 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
916 of->write_output_view(offset, oview_size, oview);
917 of->write_output_view(got_file_offset, got_size, got_view);
920 // Create the PLT section.
922 void
923 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
925 if (this->plt_ == NULL)
927 // Create the GOT sections first.
928 this->got_section(symtab, layout);
930 this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
931 this->got_plt_);
932 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
933 (elfcpp::SHF_ALLOC
934 | elfcpp::SHF_EXECINSTR),
935 this->plt_, false, false, false, false);
939 // Return the section for TLSDESC relocations.
941 Target_x86_64::Reloc_section*
942 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
944 return this->plt_section()->rela_tlsdesc(layout);
947 // Create a PLT entry for a global symbol.
949 void
950 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
951 Symbol* gsym)
953 if (gsym->has_plt_offset())
954 return;
956 if (this->plt_ == NULL)
957 this->make_plt_section(symtab, layout);
959 this->plt_->add_entry(gsym);
962 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
964 void
965 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
967 if (this->tls_base_symbol_defined_)
968 return;
970 Output_segment* tls_segment = layout->tls_segment();
971 if (tls_segment != NULL)
973 bool is_exec = parameters->options().output_is_executable();
974 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
975 Symbol_table::PREDEFINED,
976 tls_segment, 0, 0,
977 elfcpp::STT_TLS,
978 elfcpp::STB_LOCAL,
979 elfcpp::STV_HIDDEN, 0,
980 (is_exec
981 ? Symbol::SEGMENT_END
982 : Symbol::SEGMENT_START),
983 true);
985 this->tls_base_symbol_defined_ = true;
988 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
990 void
991 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
992 Layout* layout)
994 if (this->plt_ == NULL)
995 this->make_plt_section(symtab, layout);
997 if (!this->plt_->has_tlsdesc_entry())
999 // Allocate the TLSDESC_GOT entry.
1000 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1001 unsigned int got_offset = got->add_constant(0);
1003 // Allocate the TLSDESC_PLT entry.
1004 this->plt_->reserve_tlsdesc_entry(got_offset);
1008 // Create a GOT entry for the TLS module index.
1010 unsigned int
1011 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1012 Sized_relobj<64, false>* object)
1014 if (this->got_mod_index_offset_ == -1U)
1016 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1017 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1018 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1019 unsigned int got_offset = got->add_constant(0);
1020 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1021 got_offset, 0);
1022 got->add_constant(0);
1023 this->got_mod_index_offset_ = got_offset;
1025 return this->got_mod_index_offset_;
1028 // Optimize the TLS relocation type based on what we know about the
1029 // symbol. IS_FINAL is true if the final address of this symbol is
1030 // known at link time.
1032 tls::Tls_optimization
1033 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1035 // If we are generating a shared library, then we can't do anything
1036 // in the linker.
1037 if (parameters->options().shared())
1038 return tls::TLSOPT_NONE;
1040 switch (r_type)
1042 case elfcpp::R_X86_64_TLSGD:
1043 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1044 case elfcpp::R_X86_64_TLSDESC_CALL:
1045 // These are General-Dynamic which permits fully general TLS
1046 // access. Since we know that we are generating an executable,
1047 // we can convert this to Initial-Exec. If we also know that
1048 // this is a local symbol, we can further switch to Local-Exec.
1049 if (is_final)
1050 return tls::TLSOPT_TO_LE;
1051 return tls::TLSOPT_TO_IE;
1053 case elfcpp::R_X86_64_TLSLD:
1054 // This is Local-Dynamic, which refers to a local symbol in the
1055 // dynamic TLS block. Since we know that we generating an
1056 // executable, we can switch to Local-Exec.
1057 return tls::TLSOPT_TO_LE;
1059 case elfcpp::R_X86_64_DTPOFF32:
1060 case elfcpp::R_X86_64_DTPOFF64:
1061 // Another Local-Dynamic reloc.
1062 return tls::TLSOPT_TO_LE;
1064 case elfcpp::R_X86_64_GOTTPOFF:
1065 // These are Initial-Exec relocs which get the thread offset
1066 // from the GOT. If we know that we are linking against the
1067 // local symbol, we can switch to Local-Exec, which links the
1068 // thread offset into the instruction.
1069 if (is_final)
1070 return tls::TLSOPT_TO_LE;
1071 return tls::TLSOPT_NONE;
1073 case elfcpp::R_X86_64_TPOFF32:
1074 // When we already have Local-Exec, there is nothing further we
1075 // can do.
1076 return tls::TLSOPT_NONE;
1078 default:
1079 gold_unreachable();
1083 // Report an unsupported relocation against a local symbol.
1085 void
1086 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1087 unsigned int r_type)
1089 gold_error(_("%s: unsupported reloc %u against local symbol"),
1090 object->name().c_str(), r_type);
1093 // We are about to emit a dynamic relocation of type R_TYPE. If the
1094 // dynamic linker does not support it, issue an error. The GNU linker
1095 // only issues a non-PIC error for an allocated read-only section.
1096 // Here we know the section is allocated, but we don't know that it is
1097 // read-only. But we check for all the relocation types which the
1098 // glibc dynamic linker supports, so it seems appropriate to issue an
1099 // error even if the section is not read-only.
1101 void
1102 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1104 switch (r_type)
1106 // These are the relocation types supported by glibc for x86_64.
1107 case elfcpp::R_X86_64_RELATIVE:
1108 case elfcpp::R_X86_64_GLOB_DAT:
1109 case elfcpp::R_X86_64_JUMP_SLOT:
1110 case elfcpp::R_X86_64_DTPMOD64:
1111 case elfcpp::R_X86_64_DTPOFF64:
1112 case elfcpp::R_X86_64_TPOFF64:
1113 case elfcpp::R_X86_64_64:
1114 case elfcpp::R_X86_64_32:
1115 case elfcpp::R_X86_64_PC32:
1116 case elfcpp::R_X86_64_COPY:
1117 return;
1119 default:
1120 // This prevents us from issuing more than one error per reloc
1121 // section. But we can still wind up issuing more than one
1122 // error per object file.
1123 if (this->issued_non_pic_error_)
1124 return;
1125 gold_assert(parameters->options().output_is_position_independent());
1126 object->error(_("requires unsupported dynamic reloc; "
1127 "recompile with -fPIC"));
1128 this->issued_non_pic_error_ = true;
1129 return;
1131 case elfcpp::R_X86_64_NONE:
1132 gold_unreachable();
1136 // Scan a relocation for a local symbol.
1138 inline void
1139 Target_x86_64::Scan::local(Symbol_table* symtab,
1140 Layout* layout,
1141 Target_x86_64* target,
1142 Sized_relobj<64, false>* object,
1143 unsigned int data_shndx,
1144 Output_section* output_section,
1145 const elfcpp::Rela<64, false>& reloc,
1146 unsigned int r_type,
1147 const elfcpp::Sym<64, false>& lsym)
1149 switch (r_type)
1151 case elfcpp::R_X86_64_NONE:
1152 case elfcpp::R_386_GNU_VTINHERIT:
1153 case elfcpp::R_386_GNU_VTENTRY:
1154 break;
1156 case elfcpp::R_X86_64_64:
1157 // If building a shared library (or a position-independent
1158 // executable), we need to create a dynamic relocation for this
1159 // location. The relocation applied at link time will apply the
1160 // link-time value, so we flag the location with an
1161 // R_X86_64_RELATIVE relocation so the dynamic loader can
1162 // relocate it easily.
1163 if (parameters->options().output_is_position_independent())
1165 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1166 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1167 rela_dyn->add_local_relative(object, r_sym,
1168 elfcpp::R_X86_64_RELATIVE,
1169 output_section, data_shndx,
1170 reloc.get_r_offset(),
1171 reloc.get_r_addend());
1173 break;
1175 case elfcpp::R_X86_64_32:
1176 case elfcpp::R_X86_64_32S:
1177 case elfcpp::R_X86_64_16:
1178 case elfcpp::R_X86_64_8:
1179 // If building a shared library (or a position-independent
1180 // executable), we need to create a dynamic relocation for this
1181 // location. We can't use an R_X86_64_RELATIVE relocation
1182 // because that is always a 64-bit relocation.
1183 if (parameters->options().output_is_position_independent())
1185 this->check_non_pic(object, r_type);
1187 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1188 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1189 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1190 rela_dyn->add_local(object, r_sym, r_type, output_section,
1191 data_shndx, reloc.get_r_offset(),
1192 reloc.get_r_addend());
1193 else
1195 gold_assert(lsym.get_st_value() == 0);
1196 unsigned int shndx = lsym.get_st_shndx();
1197 bool is_ordinary;
1198 shndx = object->adjust_sym_shndx(r_sym, shndx,
1199 &is_ordinary);
1200 if (!is_ordinary)
1201 object->error(_("section symbol %u has bad shndx %u"),
1202 r_sym, shndx);
1203 else
1204 rela_dyn->add_local_section(object, shndx,
1205 r_type, output_section,
1206 data_shndx, reloc.get_r_offset(),
1207 reloc.get_r_addend());
1210 break;
1212 case elfcpp::R_X86_64_PC64:
1213 case elfcpp::R_X86_64_PC32:
1214 case elfcpp::R_X86_64_PC16:
1215 case elfcpp::R_X86_64_PC8:
1216 break;
1218 case elfcpp::R_X86_64_PLT32:
1219 // Since we know this is a local symbol, we can handle this as a
1220 // PC32 reloc.
1221 break;
1223 case elfcpp::R_X86_64_GOTPC32:
1224 case elfcpp::R_X86_64_GOTOFF64:
1225 case elfcpp::R_X86_64_GOTPC64:
1226 case elfcpp::R_X86_64_PLTOFF64:
1227 // We need a GOT section.
1228 target->got_section(symtab, layout);
1229 // For PLTOFF64, we'd normally want a PLT section, but since we
1230 // know this is a local symbol, no PLT is needed.
1231 break;
1233 case elfcpp::R_X86_64_GOT64:
1234 case elfcpp::R_X86_64_GOT32:
1235 case elfcpp::R_X86_64_GOTPCREL64:
1236 case elfcpp::R_X86_64_GOTPCREL:
1237 case elfcpp::R_X86_64_GOTPLT64:
1239 // The symbol requires a GOT entry.
1240 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1241 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1242 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1244 // If we are generating a shared object, we need to add a
1245 // dynamic relocation for this symbol's GOT entry.
1246 if (parameters->options().output_is_position_independent())
1248 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1249 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1250 if (r_type != elfcpp::R_X86_64_GOT32)
1251 rela_dyn->add_local_relative(
1252 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1253 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1254 else
1256 this->check_non_pic(object, r_type);
1258 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1259 rela_dyn->add_local(
1260 object, r_sym, r_type, got,
1261 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1265 // For GOTPLT64, we'd normally want a PLT section, but since
1266 // we know this is a local symbol, no PLT is needed.
1268 break;
1270 case elfcpp::R_X86_64_COPY:
1271 case elfcpp::R_X86_64_GLOB_DAT:
1272 case elfcpp::R_X86_64_JUMP_SLOT:
1273 case elfcpp::R_X86_64_RELATIVE:
1274 // These are outstanding tls relocs, which are unexpected when linking
1275 case elfcpp::R_X86_64_TPOFF64:
1276 case elfcpp::R_X86_64_DTPMOD64:
1277 case elfcpp::R_X86_64_TLSDESC:
1278 gold_error(_("%s: unexpected reloc %u in object file"),
1279 object->name().c_str(), r_type);
1280 break;
1282 // These are initial tls relocs, which are expected when linking
1283 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1284 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1285 case elfcpp::R_X86_64_TLSDESC_CALL:
1286 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1287 case elfcpp::R_X86_64_DTPOFF32:
1288 case elfcpp::R_X86_64_DTPOFF64:
1289 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1290 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1292 bool output_is_shared = parameters->options().shared();
1293 const tls::Tls_optimization optimized_type
1294 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1295 switch (r_type)
1297 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1298 if (optimized_type == tls::TLSOPT_NONE)
1300 // Create a pair of GOT entries for the module index and
1301 // dtv-relative offset.
1302 Output_data_got<64, false>* got
1303 = target->got_section(symtab, layout);
1304 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1305 unsigned int shndx = lsym.get_st_shndx();
1306 bool is_ordinary;
1307 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1308 if (!is_ordinary)
1309 object->error(_("local symbol %u has bad shndx %u"),
1310 r_sym, shndx);
1311 else
1312 got->add_local_pair_with_rela(object, r_sym,
1313 shndx,
1314 GOT_TYPE_TLS_PAIR,
1315 target->rela_dyn_section(layout),
1316 elfcpp::R_X86_64_DTPMOD64, 0);
1318 else if (optimized_type != tls::TLSOPT_TO_LE)
1319 unsupported_reloc_local(object, r_type);
1320 break;
1322 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1323 target->define_tls_base_symbol(symtab, layout);
1324 if (optimized_type == tls::TLSOPT_NONE)
1326 // Create reserved PLT and GOT entries for the resolver.
1327 target->reserve_tlsdesc_entries(symtab, layout);
1329 // Generate a double GOT entry with an
1330 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1331 // is resolved lazily, so the GOT entry needs to be in
1332 // an area in .got.plt, not .got. Call got_section to
1333 // make sure the section has been created.
1334 target->got_section(symtab, layout);
1335 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1336 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1337 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1339 unsigned int got_offset = got->add_constant(0);
1340 got->add_constant(0);
1341 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1342 got_offset);
1343 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1344 // We store the arguments we need in a vector, and
1345 // use the index into the vector as the parameter
1346 // to pass to the target specific routines.
1347 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1348 void* arg = reinterpret_cast<void*>(intarg);
1349 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1350 got, got_offset, 0);
1353 else if (optimized_type != tls::TLSOPT_TO_LE)
1354 unsupported_reloc_local(object, r_type);
1355 break;
1357 case elfcpp::R_X86_64_TLSDESC_CALL:
1358 break;
1360 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1361 if (optimized_type == tls::TLSOPT_NONE)
1363 // Create a GOT entry for the module index.
1364 target->got_mod_index_entry(symtab, layout, object);
1366 else if (optimized_type != tls::TLSOPT_TO_LE)
1367 unsupported_reloc_local(object, r_type);
1368 break;
1370 case elfcpp::R_X86_64_DTPOFF32:
1371 case elfcpp::R_X86_64_DTPOFF64:
1372 break;
1374 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1375 layout->set_has_static_tls();
1376 if (optimized_type == tls::TLSOPT_NONE)
1378 // Create a GOT entry for the tp-relative offset.
1379 Output_data_got<64, false>* got
1380 = target->got_section(symtab, layout);
1381 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1382 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1383 target->rela_dyn_section(layout),
1384 elfcpp::R_X86_64_TPOFF64);
1386 else if (optimized_type != tls::TLSOPT_TO_LE)
1387 unsupported_reloc_local(object, r_type);
1388 break;
1390 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1391 layout->set_has_static_tls();
1392 if (output_is_shared)
1393 unsupported_reloc_local(object, r_type);
1394 break;
1396 default:
1397 gold_unreachable();
1400 break;
1402 case elfcpp::R_X86_64_SIZE32:
1403 case elfcpp::R_X86_64_SIZE64:
1404 default:
1405 gold_error(_("%s: unsupported reloc %u against local symbol"),
1406 object->name().c_str(), r_type);
1407 break;
1412 // Report an unsupported relocation against a global symbol.
1414 void
1415 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1416 unsigned int r_type,
1417 Symbol* gsym)
1419 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1420 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1423 // Returns true if this relocation type could be that of a function pointer.
1424 inline bool
1425 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1427 switch (r_type)
1429 case elfcpp::R_X86_64_64:
1430 case elfcpp::R_X86_64_32:
1431 case elfcpp::R_X86_64_32S:
1432 case elfcpp::R_X86_64_16:
1433 case elfcpp::R_X86_64_8:
1434 case elfcpp::R_X86_64_GOT64:
1435 case elfcpp::R_X86_64_GOT32:
1436 case elfcpp::R_X86_64_GOTPCREL64:
1437 case elfcpp::R_X86_64_GOTPCREL:
1438 case elfcpp::R_X86_64_GOTPLT64:
1440 return true;
1443 return false;
1446 // For safe ICF, scan a relocation for a local symbol to check if it
1447 // corresponds to a function pointer being taken. In that case mark
1448 // the function whose pointer was taken as not foldable.
1450 inline bool
1451 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1452 Symbol_table* ,
1453 Layout* ,
1454 Target_x86_64* ,
1455 Sized_relobj<64, false>* ,
1456 unsigned int ,
1457 Output_section* ,
1458 const elfcpp::Rela<64, false>& ,
1459 unsigned int r_type,
1460 const elfcpp::Sym<64, false>&)
1462 // When building a shared library, do not fold any local symbols as it is
1463 // not possible to distinguish pointer taken versus a call by looking at
1464 // the relocation types.
1465 return (parameters->options().shared()
1466 || possible_function_pointer_reloc(r_type));
1469 // For safe ICF, scan a relocation for a global symbol to check if it
1470 // corresponds to a function pointer being taken. In that case mark
1471 // the function whose pointer was taken as not foldable.
1473 inline bool
1474 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1475 Symbol_table*,
1476 Layout* ,
1477 Target_x86_64* ,
1478 Sized_relobj<64, false>* ,
1479 unsigned int ,
1480 Output_section* ,
1481 const elfcpp::Rela<64, false>& ,
1482 unsigned int r_type,
1483 Symbol* gsym)
1485 // When building a shared library, do not fold symbols whose visibility
1486 // is hidden, internal or protected.
1487 return ((parameters->options().shared()
1488 && (gsym->visibility() == elfcpp::STV_INTERNAL
1489 || gsym->visibility() == elfcpp::STV_PROTECTED
1490 || gsym->visibility() == elfcpp::STV_HIDDEN))
1491 || possible_function_pointer_reloc(r_type));
1494 // Scan a relocation for a global symbol.
1496 inline void
1497 Target_x86_64::Scan::global(Symbol_table* symtab,
1498 Layout* layout,
1499 Target_x86_64* target,
1500 Sized_relobj<64, false>* object,
1501 unsigned int data_shndx,
1502 Output_section* output_section,
1503 const elfcpp::Rela<64, false>& reloc,
1504 unsigned int r_type,
1505 Symbol* gsym)
1507 switch (r_type)
1509 case elfcpp::R_X86_64_NONE:
1510 case elfcpp::R_386_GNU_VTINHERIT:
1511 case elfcpp::R_386_GNU_VTENTRY:
1512 break;
1514 case elfcpp::R_X86_64_64:
1515 case elfcpp::R_X86_64_32:
1516 case elfcpp::R_X86_64_32S:
1517 case elfcpp::R_X86_64_16:
1518 case elfcpp::R_X86_64_8:
1520 // Make a PLT entry if necessary.
1521 if (gsym->needs_plt_entry())
1523 target->make_plt_entry(symtab, layout, gsym);
1524 // Since this is not a PC-relative relocation, we may be
1525 // taking the address of a function. In that case we need to
1526 // set the entry in the dynamic symbol table to the address of
1527 // the PLT entry.
1528 if (gsym->is_from_dynobj() && !parameters->options().shared())
1529 gsym->set_needs_dynsym_value();
1531 // Make a dynamic relocation if necessary.
1532 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1534 if (gsym->may_need_copy_reloc())
1536 target->copy_reloc(symtab, layout, object,
1537 data_shndx, output_section, gsym, reloc);
1539 else if (r_type == elfcpp::R_X86_64_64
1540 && gsym->can_use_relative_reloc(false))
1542 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1543 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1544 output_section, object,
1545 data_shndx, reloc.get_r_offset(),
1546 reloc.get_r_addend());
1548 else
1550 this->check_non_pic(object, r_type);
1551 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1552 rela_dyn->add_global(gsym, r_type, output_section, object,
1553 data_shndx, reloc.get_r_offset(),
1554 reloc.get_r_addend());
1558 break;
1560 case elfcpp::R_X86_64_PC64:
1561 case elfcpp::R_X86_64_PC32:
1562 case elfcpp::R_X86_64_PC16:
1563 case elfcpp::R_X86_64_PC8:
1565 // Make a PLT entry if necessary.
1566 if (gsym->needs_plt_entry())
1567 target->make_plt_entry(symtab, layout, gsym);
1568 // Make a dynamic relocation if necessary.
1569 int flags = Symbol::NON_PIC_REF;
1570 if (gsym->is_func())
1571 flags |= Symbol::FUNCTION_CALL;
1572 if (gsym->needs_dynamic_reloc(flags))
1574 if (gsym->may_need_copy_reloc())
1576 target->copy_reloc(symtab, layout, object,
1577 data_shndx, output_section, gsym, reloc);
1579 else
1581 this->check_non_pic(object, r_type);
1582 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1583 rela_dyn->add_global(gsym, r_type, output_section, object,
1584 data_shndx, reloc.get_r_offset(),
1585 reloc.get_r_addend());
1589 break;
1591 case elfcpp::R_X86_64_GOT64:
1592 case elfcpp::R_X86_64_GOT32:
1593 case elfcpp::R_X86_64_GOTPCREL64:
1594 case elfcpp::R_X86_64_GOTPCREL:
1595 case elfcpp::R_X86_64_GOTPLT64:
1597 // The symbol requires a GOT entry.
1598 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1599 if (gsym->final_value_is_known())
1600 got->add_global(gsym, GOT_TYPE_STANDARD);
1601 else
1603 // If this symbol is not fully resolved, we need to add a
1604 // dynamic relocation for it.
1605 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1606 if (gsym->is_from_dynobj()
1607 || gsym->is_undefined()
1608 || gsym->is_preemptible())
1609 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1610 elfcpp::R_X86_64_GLOB_DAT);
1611 else
1613 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1614 rela_dyn->add_global_relative(
1615 gsym, elfcpp::R_X86_64_RELATIVE, got,
1616 gsym->got_offset(GOT_TYPE_STANDARD), 0);
1619 // For GOTPLT64, we also need a PLT entry (but only if the
1620 // symbol is not fully resolved).
1621 if (r_type == elfcpp::R_X86_64_GOTPLT64
1622 && !gsym->final_value_is_known())
1623 target->make_plt_entry(symtab, layout, gsym);
1625 break;
1627 case elfcpp::R_X86_64_PLT32:
1628 // If the symbol is fully resolved, this is just a PC32 reloc.
1629 // Otherwise we need a PLT entry.
1630 if (gsym->final_value_is_known())
1631 break;
1632 // If building a shared library, we can also skip the PLT entry
1633 // if the symbol is defined in the output file and is protected
1634 // or hidden.
1635 if (gsym->is_defined()
1636 && !gsym->is_from_dynobj()
1637 && !gsym->is_preemptible())
1638 break;
1639 target->make_plt_entry(symtab, layout, gsym);
1640 break;
1642 case elfcpp::R_X86_64_GOTPC32:
1643 case elfcpp::R_X86_64_GOTOFF64:
1644 case elfcpp::R_X86_64_GOTPC64:
1645 case elfcpp::R_X86_64_PLTOFF64:
1646 // We need a GOT section.
1647 target->got_section(symtab, layout);
1648 // For PLTOFF64, we also need a PLT entry (but only if the
1649 // symbol is not fully resolved).
1650 if (r_type == elfcpp::R_X86_64_PLTOFF64
1651 && !gsym->final_value_is_known())
1652 target->make_plt_entry(symtab, layout, gsym);
1653 break;
1655 case elfcpp::R_X86_64_COPY:
1656 case elfcpp::R_X86_64_GLOB_DAT:
1657 case elfcpp::R_X86_64_JUMP_SLOT:
1658 case elfcpp::R_X86_64_RELATIVE:
1659 // These are outstanding tls relocs, which are unexpected when linking
1660 case elfcpp::R_X86_64_TPOFF64:
1661 case elfcpp::R_X86_64_DTPMOD64:
1662 case elfcpp::R_X86_64_TLSDESC:
1663 gold_error(_("%s: unexpected reloc %u in object file"),
1664 object->name().c_str(), r_type);
1665 break;
1667 // These are initial tls relocs, which are expected for global()
1668 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1669 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1670 case elfcpp::R_X86_64_TLSDESC_CALL:
1671 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1672 case elfcpp::R_X86_64_DTPOFF32:
1673 case elfcpp::R_X86_64_DTPOFF64:
1674 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1675 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1677 const bool is_final = gsym->final_value_is_known();
1678 const tls::Tls_optimization optimized_type
1679 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1680 switch (r_type)
1682 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1683 if (optimized_type == tls::TLSOPT_NONE)
1685 // Create a pair of GOT entries for the module index and
1686 // dtv-relative offset.
1687 Output_data_got<64, false>* got
1688 = target->got_section(symtab, layout);
1689 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1690 target->rela_dyn_section(layout),
1691 elfcpp::R_X86_64_DTPMOD64,
1692 elfcpp::R_X86_64_DTPOFF64);
1694 else if (optimized_type == tls::TLSOPT_TO_IE)
1696 // Create a GOT entry for the tp-relative offset.
1697 Output_data_got<64, false>* got
1698 = target->got_section(symtab, layout);
1699 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1700 target->rela_dyn_section(layout),
1701 elfcpp::R_X86_64_TPOFF64);
1703 else if (optimized_type != tls::TLSOPT_TO_LE)
1704 unsupported_reloc_global(object, r_type, gsym);
1705 break;
1707 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1708 target->define_tls_base_symbol(symtab, layout);
1709 if (optimized_type == tls::TLSOPT_NONE)
1711 // Create reserved PLT and GOT entries for the resolver.
1712 target->reserve_tlsdesc_entries(symtab, layout);
1714 // Create a double GOT entry with an R_X86_64_TLSDESC
1715 // reloc. The R_X86_64_TLSDESC reloc is resolved
1716 // lazily, so the GOT entry needs to be in an area in
1717 // .got.plt, not .got. Call got_section to make sure
1718 // the section has been created.
1719 target->got_section(symtab, layout);
1720 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1721 Reloc_section *rt = target->rela_tlsdesc_section(layout);
1722 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
1723 elfcpp::R_X86_64_TLSDESC, 0);
1725 else if (optimized_type == tls::TLSOPT_TO_IE)
1727 // Create a GOT entry for the tp-relative offset.
1728 Output_data_got<64, false>* got
1729 = target->got_section(symtab, layout);
1730 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1731 target->rela_dyn_section(layout),
1732 elfcpp::R_X86_64_TPOFF64);
1734 else if (optimized_type != tls::TLSOPT_TO_LE)
1735 unsupported_reloc_global(object, r_type, gsym);
1736 break;
1738 case elfcpp::R_X86_64_TLSDESC_CALL:
1739 break;
1741 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1742 if (optimized_type == tls::TLSOPT_NONE)
1744 // Create a GOT entry for the module index.
1745 target->got_mod_index_entry(symtab, layout, object);
1747 else if (optimized_type != tls::TLSOPT_TO_LE)
1748 unsupported_reloc_global(object, r_type, gsym);
1749 break;
1751 case elfcpp::R_X86_64_DTPOFF32:
1752 case elfcpp::R_X86_64_DTPOFF64:
1753 break;
1755 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1756 layout->set_has_static_tls();
1757 if (optimized_type == tls::TLSOPT_NONE)
1759 // Create a GOT entry for the tp-relative offset.
1760 Output_data_got<64, false>* got
1761 = target->got_section(symtab, layout);
1762 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1763 target->rela_dyn_section(layout),
1764 elfcpp::R_X86_64_TPOFF64);
1766 else if (optimized_type != tls::TLSOPT_TO_LE)
1767 unsupported_reloc_global(object, r_type, gsym);
1768 break;
1770 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1771 layout->set_has_static_tls();
1772 if (parameters->options().shared())
1773 unsupported_reloc_local(object, r_type);
1774 break;
1776 default:
1777 gold_unreachable();
1780 break;
1782 case elfcpp::R_X86_64_SIZE32:
1783 case elfcpp::R_X86_64_SIZE64:
1784 default:
1785 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1786 object->name().c_str(), r_type,
1787 gsym->demangled_name().c_str());
1788 break;
1792 void
1793 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
1794 Layout* layout,
1795 Sized_relobj<64, false>* object,
1796 unsigned int data_shndx,
1797 unsigned int sh_type,
1798 const unsigned char* prelocs,
1799 size_t reloc_count,
1800 Output_section* output_section,
1801 bool needs_special_offset_handling,
1802 size_t local_symbol_count,
1803 const unsigned char* plocal_symbols)
1806 if (sh_type == elfcpp::SHT_REL)
1808 return;
1811 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1812 Target_x86_64::Scan,
1813 Target_x86_64::Relocatable_size_for_reloc>(
1814 symtab,
1815 layout,
1816 this,
1817 object,
1818 data_shndx,
1819 prelocs,
1820 reloc_count,
1821 output_section,
1822 needs_special_offset_handling,
1823 local_symbol_count,
1824 plocal_symbols);
1827 // Scan relocations for a section.
1829 void
1830 Target_x86_64::scan_relocs(Symbol_table* symtab,
1831 Layout* layout,
1832 Sized_relobj<64, false>* object,
1833 unsigned int data_shndx,
1834 unsigned int sh_type,
1835 const unsigned char* prelocs,
1836 size_t reloc_count,
1837 Output_section* output_section,
1838 bool needs_special_offset_handling,
1839 size_t local_symbol_count,
1840 const unsigned char* plocal_symbols)
1842 if (sh_type == elfcpp::SHT_REL)
1844 gold_error(_("%s: unsupported REL reloc section"),
1845 object->name().c_str());
1846 return;
1849 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1850 Target_x86_64::Scan>(
1851 symtab,
1852 layout,
1853 this,
1854 object,
1855 data_shndx,
1856 prelocs,
1857 reloc_count,
1858 output_section,
1859 needs_special_offset_handling,
1860 local_symbol_count,
1861 plocal_symbols);
1864 // Finalize the sections.
1866 void
1867 Target_x86_64::do_finalize_sections(
1868 Layout* layout,
1869 const Input_objects*,
1870 Symbol_table* symtab)
1872 const Reloc_section* rel_plt = (this->plt_ == NULL
1873 ? NULL
1874 : this->plt_->rela_plt());
1875 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
1876 this->rela_dyn_, true, false);
1878 // Fill in some more dynamic tags.
1879 Output_data_dynamic* const odyn = layout->dynamic_data();
1880 if (odyn != NULL)
1882 if (this->plt_ != NULL
1883 && this->plt_->output_section() != NULL
1884 && this->plt_->has_tlsdesc_entry())
1886 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1887 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1888 this->got_->finalize_data_size();
1889 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1890 this->plt_, plt_offset);
1891 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1892 this->got_, got_offset);
1896 // Emit any relocs we saved in an attempt to avoid generating COPY
1897 // relocs.
1898 if (this->copy_relocs_.any_saved_relocs())
1899 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1901 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1902 // the .got.plt section.
1903 Symbol* sym = this->global_offset_table_;
1904 if (sym != NULL)
1906 uint64_t data_size = this->got_plt_->current_data_size();
1907 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
1911 // Perform a relocation.
1913 inline bool
1914 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1915 Target_x86_64* target,
1916 Output_section*,
1917 size_t relnum,
1918 const elfcpp::Rela<64, false>& rela,
1919 unsigned int r_type,
1920 const Sized_symbol<64>* gsym,
1921 const Symbol_value<64>* psymval,
1922 unsigned char* view,
1923 elfcpp::Elf_types<64>::Elf_Addr address,
1924 section_size_type view_size)
1926 if (this->skip_call_tls_get_addr_)
1928 if ((r_type != elfcpp::R_X86_64_PLT32
1929 && r_type != elfcpp::R_X86_64_PC32)
1930 || gsym == NULL
1931 || strcmp(gsym->name(), "__tls_get_addr") != 0)
1933 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1934 _("missing expected TLS relocation"));
1936 else
1938 this->skip_call_tls_get_addr_ = false;
1939 return false;
1943 // Pick the value to use for symbols defined in shared objects.
1944 Symbol_value<64> symval;
1945 if (gsym != NULL
1946 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1947 || r_type == elfcpp::R_X86_64_PC32
1948 || r_type == elfcpp::R_X86_64_PC16
1949 || r_type == elfcpp::R_X86_64_PC8))
1951 symval.set_output_value(target->plt_section()->address()
1952 + gsym->plt_offset());
1953 psymval = &symval;
1956 const Sized_relobj<64, false>* object = relinfo->object;
1957 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1959 // Get the GOT offset if needed.
1960 // The GOT pointer points to the end of the GOT section.
1961 // We need to subtract the size of the GOT section to get
1962 // the actual offset to use in the relocation.
1963 bool have_got_offset = false;
1964 unsigned int got_offset = 0;
1965 switch (r_type)
1967 case elfcpp::R_X86_64_GOT32:
1968 case elfcpp::R_X86_64_GOT64:
1969 case elfcpp::R_X86_64_GOTPLT64:
1970 case elfcpp::R_X86_64_GOTPCREL:
1971 case elfcpp::R_X86_64_GOTPCREL64:
1972 if (gsym != NULL)
1974 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1975 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1977 else
1979 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1980 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1981 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1982 - target->got_size());
1984 have_got_offset = true;
1985 break;
1987 default:
1988 break;
1991 switch (r_type)
1993 case elfcpp::R_X86_64_NONE:
1994 case elfcpp::R_386_GNU_VTINHERIT:
1995 case elfcpp::R_386_GNU_VTENTRY:
1996 break;
1998 case elfcpp::R_X86_64_64:
1999 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2000 break;
2002 case elfcpp::R_X86_64_PC64:
2003 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2004 address);
2005 break;
2007 case elfcpp::R_X86_64_32:
2008 // FIXME: we need to verify that value + addend fits into 32 bits:
2009 // uint64_t x = value + addend;
2010 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2011 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2012 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2013 break;
2015 case elfcpp::R_X86_64_32S:
2016 // FIXME: we need to verify that value + addend fits into 32 bits:
2017 // int64_t x = value + addend; // note this quantity is signed!
2018 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2019 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2020 break;
2022 case elfcpp::R_X86_64_PC32:
2023 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2024 address);
2025 break;
2027 case elfcpp::R_X86_64_16:
2028 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2029 break;
2031 case elfcpp::R_X86_64_PC16:
2032 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2033 address);
2034 break;
2036 case elfcpp::R_X86_64_8:
2037 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2038 break;
2040 case elfcpp::R_X86_64_PC8:
2041 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2042 address);
2043 break;
2045 case elfcpp::R_X86_64_PLT32:
2046 gold_assert(gsym == NULL
2047 || gsym->has_plt_offset()
2048 || gsym->final_value_is_known()
2049 || (gsym->is_defined()
2050 && !gsym->is_from_dynobj()
2051 && !gsym->is_preemptible()));
2052 // Note: while this code looks the same as for R_X86_64_PC32, it
2053 // behaves differently because psymval was set to point to
2054 // the PLT entry, rather than the symbol, in Scan::global().
2055 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2056 address);
2057 break;
2059 case elfcpp::R_X86_64_PLTOFF64:
2061 gold_assert(gsym);
2062 gold_assert(gsym->has_plt_offset()
2063 || gsym->final_value_is_known());
2064 elfcpp::Elf_types<64>::Elf_Addr got_address;
2065 got_address = target->got_section(NULL, NULL)->address();
2066 Relocate_functions<64, false>::rela64(view, object, psymval,
2067 addend - got_address);
2070 case elfcpp::R_X86_64_GOT32:
2071 gold_assert(have_got_offset);
2072 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2073 break;
2075 case elfcpp::R_X86_64_GOTPC32:
2077 gold_assert(gsym);
2078 elfcpp::Elf_types<64>::Elf_Addr value;
2079 value = target->got_plt_section()->address();
2080 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2082 break;
2084 case elfcpp::R_X86_64_GOT64:
2085 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2086 // Since we always add a PLT entry, this is equivalent.
2087 case elfcpp::R_X86_64_GOTPLT64:
2088 gold_assert(have_got_offset);
2089 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2090 break;
2092 case elfcpp::R_X86_64_GOTPC64:
2094 gold_assert(gsym);
2095 elfcpp::Elf_types<64>::Elf_Addr value;
2096 value = target->got_plt_section()->address();
2097 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2099 break;
2101 case elfcpp::R_X86_64_GOTOFF64:
2103 elfcpp::Elf_types<64>::Elf_Addr value;
2104 value = (psymval->value(object, 0)
2105 - target->got_plt_section()->address());
2106 Relocate_functions<64, false>::rela64(view, value, addend);
2108 break;
2110 case elfcpp::R_X86_64_GOTPCREL:
2112 gold_assert(have_got_offset);
2113 elfcpp::Elf_types<64>::Elf_Addr value;
2114 value = target->got_plt_section()->address() + got_offset;
2115 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2117 break;
2119 case elfcpp::R_X86_64_GOTPCREL64:
2121 gold_assert(have_got_offset);
2122 elfcpp::Elf_types<64>::Elf_Addr value;
2123 value = target->got_plt_section()->address() + got_offset;
2124 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2126 break;
2128 case elfcpp::R_X86_64_COPY:
2129 case elfcpp::R_X86_64_GLOB_DAT:
2130 case elfcpp::R_X86_64_JUMP_SLOT:
2131 case elfcpp::R_X86_64_RELATIVE:
2132 // These are outstanding tls relocs, which are unexpected when linking
2133 case elfcpp::R_X86_64_TPOFF64:
2134 case elfcpp::R_X86_64_DTPMOD64:
2135 case elfcpp::R_X86_64_TLSDESC:
2136 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2137 _("unexpected reloc %u in object file"),
2138 r_type);
2139 break;
2141 // These are initial tls relocs, which are expected when linking
2142 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2143 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2144 case elfcpp::R_X86_64_TLSDESC_CALL:
2145 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2146 case elfcpp::R_X86_64_DTPOFF32:
2147 case elfcpp::R_X86_64_DTPOFF64:
2148 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2149 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2150 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2151 view, address, view_size);
2152 break;
2154 case elfcpp::R_X86_64_SIZE32:
2155 case elfcpp::R_X86_64_SIZE64:
2156 default:
2157 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2158 _("unsupported reloc %u"),
2159 r_type);
2160 break;
2163 return true;
2166 // Perform a TLS relocation.
2168 inline void
2169 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2170 Target_x86_64* target,
2171 size_t relnum,
2172 const elfcpp::Rela<64, false>& rela,
2173 unsigned int r_type,
2174 const Sized_symbol<64>* gsym,
2175 const Symbol_value<64>* psymval,
2176 unsigned char* view,
2177 elfcpp::Elf_types<64>::Elf_Addr address,
2178 section_size_type view_size)
2180 Output_segment* tls_segment = relinfo->layout->tls_segment();
2182 const Sized_relobj<64, false>* object = relinfo->object;
2183 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2185 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2187 const bool is_final = (gsym == NULL
2188 ? !parameters->options().shared()
2189 : gsym->final_value_is_known());
2190 const tls::Tls_optimization optimized_type
2191 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2192 switch (r_type)
2194 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2195 this->saw_tls_block_reloc_ = true;
2196 if (optimized_type == tls::TLSOPT_TO_LE)
2198 gold_assert(tls_segment != NULL);
2199 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2200 rela, r_type, value, view,
2201 view_size);
2202 break;
2204 else
2206 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2207 ? GOT_TYPE_TLS_OFFSET
2208 : GOT_TYPE_TLS_PAIR);
2209 unsigned int got_offset;
2210 if (gsym != NULL)
2212 gold_assert(gsym->has_got_offset(got_type));
2213 got_offset = gsym->got_offset(got_type) - target->got_size();
2215 else
2217 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2218 gold_assert(object->local_has_got_offset(r_sym, got_type));
2219 got_offset = (object->local_got_offset(r_sym, got_type)
2220 - target->got_size());
2222 if (optimized_type == tls::TLSOPT_TO_IE)
2224 gold_assert(tls_segment != NULL);
2225 value = target->got_plt_section()->address() + got_offset;
2226 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2227 value, view, address, view_size);
2228 break;
2230 else if (optimized_type == tls::TLSOPT_NONE)
2232 // Relocate the field with the offset of the pair of GOT
2233 // entries.
2234 value = target->got_plt_section()->address() + got_offset;
2235 Relocate_functions<64, false>::pcrela32(view, value, addend,
2236 address);
2237 break;
2240 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2241 _("unsupported reloc %u"), r_type);
2242 break;
2244 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2245 case elfcpp::R_X86_64_TLSDESC_CALL:
2246 this->saw_tls_block_reloc_ = true;
2247 if (optimized_type == tls::TLSOPT_TO_LE)
2249 gold_assert(tls_segment != NULL);
2250 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2251 rela, r_type, value, view,
2252 view_size);
2253 break;
2255 else
2257 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2258 ? GOT_TYPE_TLS_OFFSET
2259 : GOT_TYPE_TLS_DESC);
2260 unsigned int got_offset = 0;
2261 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2262 && optimized_type == tls::TLSOPT_NONE)
2264 // We created GOT entries in the .got.tlsdesc portion of
2265 // the .got.plt section, but the offset stored in the
2266 // symbol is the offset within .got.tlsdesc.
2267 got_offset = (target->got_size()
2268 + target->got_plt_section()->data_size());
2270 if (gsym != NULL)
2272 gold_assert(gsym->has_got_offset(got_type));
2273 got_offset += gsym->got_offset(got_type) - target->got_size();
2275 else
2277 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2278 gold_assert(object->local_has_got_offset(r_sym, got_type));
2279 got_offset += (object->local_got_offset(r_sym, got_type)
2280 - target->got_size());
2282 if (optimized_type == tls::TLSOPT_TO_IE)
2284 gold_assert(tls_segment != NULL);
2285 value = target->got_plt_section()->address() + got_offset;
2286 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2287 rela, r_type, value, view, address,
2288 view_size);
2289 break;
2291 else if (optimized_type == tls::TLSOPT_NONE)
2293 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2295 // Relocate the field with the offset of the pair of GOT
2296 // entries.
2297 value = target->got_plt_section()->address() + got_offset;
2298 Relocate_functions<64, false>::pcrela32(view, value, addend,
2299 address);
2301 break;
2304 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2305 _("unsupported reloc %u"), r_type);
2306 break;
2308 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2309 this->saw_tls_block_reloc_ = true;
2310 if (optimized_type == tls::TLSOPT_TO_LE)
2312 gold_assert(tls_segment != NULL);
2313 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2314 value, view, view_size);
2315 break;
2317 else if (optimized_type == tls::TLSOPT_NONE)
2319 // Relocate the field with the offset of the GOT entry for
2320 // the module index.
2321 unsigned int got_offset;
2322 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2323 - target->got_size());
2324 value = target->got_plt_section()->address() + got_offset;
2325 Relocate_functions<64, false>::pcrela32(view, value, addend,
2326 address);
2327 break;
2329 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2330 _("unsupported reloc %u"), r_type);
2331 break;
2333 case elfcpp::R_X86_64_DTPOFF32:
2334 if (optimized_type == tls::TLSOPT_TO_LE)
2336 // This relocation type is used in debugging information.
2337 // In that case we need to not optimize the value. If we
2338 // haven't seen a TLSLD reloc, then we assume we should not
2339 // optimize this reloc.
2340 if (this->saw_tls_block_reloc_)
2342 gold_assert(tls_segment != NULL);
2343 value -= tls_segment->memsz();
2346 Relocate_functions<64, false>::rela32(view, value, addend);
2347 break;
2349 case elfcpp::R_X86_64_DTPOFF64:
2350 if (optimized_type == tls::TLSOPT_TO_LE)
2352 // See R_X86_64_DTPOFF32, just above, for why we test this.
2353 if (this->saw_tls_block_reloc_)
2355 gold_assert(tls_segment != NULL);
2356 value -= tls_segment->memsz();
2359 Relocate_functions<64, false>::rela64(view, value, addend);
2360 break;
2362 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2363 if (optimized_type == tls::TLSOPT_TO_LE)
2365 gold_assert(tls_segment != NULL);
2366 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2367 rela, r_type, value, view,
2368 view_size);
2369 break;
2371 else if (optimized_type == tls::TLSOPT_NONE)
2373 // Relocate the field with the offset of the GOT entry for
2374 // the tp-relative offset of the symbol.
2375 unsigned int got_offset;
2376 if (gsym != NULL)
2378 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2379 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2380 - target->got_size());
2382 else
2384 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2385 gold_assert(object->local_has_got_offset(r_sym,
2386 GOT_TYPE_TLS_OFFSET));
2387 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2388 - target->got_size());
2390 value = target->got_plt_section()->address() + got_offset;
2391 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2392 break;
2394 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2395 _("unsupported reloc type %u"),
2396 r_type);
2397 break;
2399 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2400 value -= tls_segment->memsz();
2401 Relocate_functions<64, false>::rela32(view, value, addend);
2402 break;
2406 // Do a relocation in which we convert a TLS General-Dynamic to an
2407 // Initial-Exec.
2409 inline void
2410 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2411 size_t relnum,
2412 Output_segment*,
2413 const elfcpp::Rela<64, false>& rela,
2414 unsigned int,
2415 elfcpp::Elf_types<64>::Elf_Addr value,
2416 unsigned char* view,
2417 elfcpp::Elf_types<64>::Elf_Addr address,
2418 section_size_type view_size)
2420 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2421 // .word 0x6666; rex64; call __tls_get_addr
2422 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2424 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2425 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2427 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2428 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2429 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2430 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2432 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2434 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2435 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2437 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2438 // We can skip it.
2439 this->skip_call_tls_get_addr_ = true;
2442 // Do a relocation in which we convert a TLS General-Dynamic to a
2443 // Local-Exec.
2445 inline void
2446 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2447 size_t relnum,
2448 Output_segment* tls_segment,
2449 const elfcpp::Rela<64, false>& rela,
2450 unsigned int,
2451 elfcpp::Elf_types<64>::Elf_Addr value,
2452 unsigned char* view,
2453 section_size_type view_size)
2455 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2456 // .word 0x6666; rex64; call __tls_get_addr
2457 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2459 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2460 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2462 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2463 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2464 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2465 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2467 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2469 value -= tls_segment->memsz();
2470 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2472 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2473 // We can skip it.
2474 this->skip_call_tls_get_addr_ = true;
2477 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2479 inline void
2480 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2481 const Relocate_info<64, false>* relinfo,
2482 size_t relnum,
2483 Output_segment*,
2484 const elfcpp::Rela<64, false>& rela,
2485 unsigned int r_type,
2486 elfcpp::Elf_types<64>::Elf_Addr value,
2487 unsigned char* view,
2488 elfcpp::Elf_types<64>::Elf_Addr address,
2489 section_size_type view_size)
2491 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2493 // leaq foo@tlsdesc(%rip), %rax
2494 // ==> movq foo@gottpoff(%rip), %rax
2495 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2496 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2497 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2498 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2499 view[-2] = 0x8b;
2500 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2501 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2503 else
2505 // call *foo@tlscall(%rax)
2506 // ==> nop; nop
2507 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2508 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2509 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2510 view[0] == 0xff && view[1] == 0x10);
2511 view[0] = 0x66;
2512 view[1] = 0x90;
2516 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2518 inline void
2519 Target_x86_64::Relocate::tls_desc_gd_to_le(
2520 const Relocate_info<64, false>* relinfo,
2521 size_t relnum,
2522 Output_segment* tls_segment,
2523 const elfcpp::Rela<64, false>& rela,
2524 unsigned int r_type,
2525 elfcpp::Elf_types<64>::Elf_Addr value,
2526 unsigned char* view,
2527 section_size_type view_size)
2529 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2531 // leaq foo@tlsdesc(%rip), %rax
2532 // ==> movq foo@tpoff, %rax
2533 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2534 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2535 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2536 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2537 view[-2] = 0xc7;
2538 view[-1] = 0xc0;
2539 value -= tls_segment->memsz();
2540 Relocate_functions<64, false>::rela32(view, value, 0);
2542 else
2544 // call *foo@tlscall(%rax)
2545 // ==> nop; nop
2546 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2547 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2548 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2549 view[0] == 0xff && view[1] == 0x10);
2550 view[0] = 0x66;
2551 view[1] = 0x90;
2555 inline void
2556 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2557 size_t relnum,
2558 Output_segment*,
2559 const elfcpp::Rela<64, false>& rela,
2560 unsigned int,
2561 elfcpp::Elf_types<64>::Elf_Addr,
2562 unsigned char* view,
2563 section_size_type view_size)
2565 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2566 // ... leq foo@dtpoff(%rax),%reg
2567 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2569 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2570 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2572 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2573 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2575 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2577 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2579 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2580 // We can skip it.
2581 this->skip_call_tls_get_addr_ = true;
2584 // Do a relocation in which we convert a TLS Initial-Exec to a
2585 // Local-Exec.
2587 inline void
2588 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2589 size_t relnum,
2590 Output_segment* tls_segment,
2591 const elfcpp::Rela<64, false>& rela,
2592 unsigned int,
2593 elfcpp::Elf_types<64>::Elf_Addr value,
2594 unsigned char* view,
2595 section_size_type view_size)
2597 // We need to examine the opcodes to figure out which instruction we
2598 // are looking at.
2600 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2601 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2603 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2604 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2606 unsigned char op1 = view[-3];
2607 unsigned char op2 = view[-2];
2608 unsigned char op3 = view[-1];
2609 unsigned char reg = op3 >> 3;
2611 if (op2 == 0x8b)
2613 // movq
2614 if (op1 == 0x4c)
2615 view[-3] = 0x49;
2616 view[-2] = 0xc7;
2617 view[-1] = 0xc0 | reg;
2619 else if (reg == 4)
2621 // Special handling for %rsp.
2622 if (op1 == 0x4c)
2623 view[-3] = 0x49;
2624 view[-2] = 0x81;
2625 view[-1] = 0xc0 | reg;
2627 else
2629 // addq
2630 if (op1 == 0x4c)
2631 view[-3] = 0x4d;
2632 view[-2] = 0x8d;
2633 view[-1] = 0x80 | reg | (reg << 3);
2636 value -= tls_segment->memsz();
2637 Relocate_functions<64, false>::rela32(view, value, 0);
2640 // Relocate section data.
2642 void
2643 Target_x86_64::relocate_section(
2644 const Relocate_info<64, false>* relinfo,
2645 unsigned int sh_type,
2646 const unsigned char* prelocs,
2647 size_t reloc_count,
2648 Output_section* output_section,
2649 bool needs_special_offset_handling,
2650 unsigned char* view,
2651 elfcpp::Elf_types<64>::Elf_Addr address,
2652 section_size_type view_size,
2653 const Reloc_symbol_changes* reloc_symbol_changes)
2655 gold_assert(sh_type == elfcpp::SHT_RELA);
2657 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2658 Target_x86_64::Relocate>(
2659 relinfo,
2660 this,
2661 prelocs,
2662 reloc_count,
2663 output_section,
2664 needs_special_offset_handling,
2665 view,
2666 address,
2667 view_size,
2668 reloc_symbol_changes);
2671 // Return the size of a relocation while scanning during a relocatable
2672 // link.
2674 unsigned int
2675 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2676 unsigned int r_type,
2677 Relobj* object)
2679 switch (r_type)
2681 case elfcpp::R_X86_64_NONE:
2682 case elfcpp::R_386_GNU_VTINHERIT:
2683 case elfcpp::R_386_GNU_VTENTRY:
2684 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2685 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2686 case elfcpp::R_X86_64_TLSDESC_CALL:
2687 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2688 case elfcpp::R_X86_64_DTPOFF32:
2689 case elfcpp::R_X86_64_DTPOFF64:
2690 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2691 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2692 return 0;
2694 case elfcpp::R_X86_64_64:
2695 case elfcpp::R_X86_64_PC64:
2696 case elfcpp::R_X86_64_GOTOFF64:
2697 case elfcpp::R_X86_64_GOTPC64:
2698 case elfcpp::R_X86_64_PLTOFF64:
2699 case elfcpp::R_X86_64_GOT64:
2700 case elfcpp::R_X86_64_GOTPCREL64:
2701 case elfcpp::R_X86_64_GOTPCREL:
2702 case elfcpp::R_X86_64_GOTPLT64:
2703 return 8;
2705 case elfcpp::R_X86_64_32:
2706 case elfcpp::R_X86_64_32S:
2707 case elfcpp::R_X86_64_PC32:
2708 case elfcpp::R_X86_64_PLT32:
2709 case elfcpp::R_X86_64_GOTPC32:
2710 case elfcpp::R_X86_64_GOT32:
2711 return 4;
2713 case elfcpp::R_X86_64_16:
2714 case elfcpp::R_X86_64_PC16:
2715 return 2;
2717 case elfcpp::R_X86_64_8:
2718 case elfcpp::R_X86_64_PC8:
2719 return 1;
2721 case elfcpp::R_X86_64_COPY:
2722 case elfcpp::R_X86_64_GLOB_DAT:
2723 case elfcpp::R_X86_64_JUMP_SLOT:
2724 case elfcpp::R_X86_64_RELATIVE:
2725 // These are outstanding tls relocs, which are unexpected when linking
2726 case elfcpp::R_X86_64_TPOFF64:
2727 case elfcpp::R_X86_64_DTPMOD64:
2728 case elfcpp::R_X86_64_TLSDESC:
2729 object->error(_("unexpected reloc %u in object file"), r_type);
2730 return 0;
2732 case elfcpp::R_X86_64_SIZE32:
2733 case elfcpp::R_X86_64_SIZE64:
2734 default:
2735 object->error(_("unsupported reloc %u against local symbol"), r_type);
2736 return 0;
2740 // Scan the relocs during a relocatable link.
2742 void
2743 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
2744 Layout* layout,
2745 Sized_relobj<64, false>* object,
2746 unsigned int data_shndx,
2747 unsigned int sh_type,
2748 const unsigned char* prelocs,
2749 size_t reloc_count,
2750 Output_section* output_section,
2751 bool needs_special_offset_handling,
2752 size_t local_symbol_count,
2753 const unsigned char* plocal_symbols,
2754 Relocatable_relocs* rr)
2756 gold_assert(sh_type == elfcpp::SHT_RELA);
2758 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2759 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2761 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2762 Scan_relocatable_relocs>(
2763 symtab,
2764 layout,
2765 object,
2766 data_shndx,
2767 prelocs,
2768 reloc_count,
2769 output_section,
2770 needs_special_offset_handling,
2771 local_symbol_count,
2772 plocal_symbols,
2773 rr);
2776 // Relocate a section during a relocatable link.
2778 void
2779 Target_x86_64::relocate_for_relocatable(
2780 const Relocate_info<64, false>* relinfo,
2781 unsigned int sh_type,
2782 const unsigned char* prelocs,
2783 size_t reloc_count,
2784 Output_section* output_section,
2785 off_t offset_in_output_section,
2786 const Relocatable_relocs* rr,
2787 unsigned char* view,
2788 elfcpp::Elf_types<64>::Elf_Addr view_address,
2789 section_size_type view_size,
2790 unsigned char* reloc_view,
2791 section_size_type reloc_view_size)
2793 gold_assert(sh_type == elfcpp::SHT_RELA);
2795 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2796 relinfo,
2797 prelocs,
2798 reloc_count,
2799 output_section,
2800 offset_in_output_section,
2802 view,
2803 view_address,
2804 view_size,
2805 reloc_view,
2806 reloc_view_size);
2809 // Return the value to use for a dynamic which requires special
2810 // treatment. This is how we support equality comparisons of function
2811 // pointers across shared library boundaries, as described in the
2812 // processor specific ABI supplement.
2814 uint64_t
2815 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2817 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2818 return this->plt_section()->address() + gsym->plt_offset();
2821 // Return a string used to fill a code section with nops to take up
2822 // the specified length.
2824 std::string
2825 Target_x86_64::do_code_fill(section_size_type length) const
2827 if (length >= 16)
2829 // Build a jmpq instruction to skip over the bytes.
2830 unsigned char jmp[5];
2831 jmp[0] = 0xe9;
2832 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2833 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2834 + std::string(length - 5, '\0'));
2837 // Nop sequences of various lengths.
2838 const char nop1[1] = { 0x90 }; // nop
2839 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2840 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2841 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2842 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2843 0x00 };
2844 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2845 0x00, 0x00 };
2846 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2847 0x00, 0x00, 0x00 };
2848 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2849 0x00, 0x00, 0x00, 0x00 };
2850 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2851 0x00, 0x00, 0x00, 0x00,
2852 0x00 };
2853 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2854 0x84, 0x00, 0x00, 0x00,
2855 0x00, 0x00 };
2856 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2857 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2858 0x00, 0x00, 0x00 };
2859 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2860 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2861 0x00, 0x00, 0x00, 0x00 };
2862 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2863 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2864 0x00, 0x00, 0x00, 0x00,
2865 0x00 };
2866 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2867 0x66, 0x2e, 0x0f, 0x1f, // data16
2868 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2869 0x00, 0x00 };
2870 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2871 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2872 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2873 0x00, 0x00, 0x00 };
2875 const char* nops[16] = {
2876 NULL,
2877 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2878 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2881 return std::string(nops[length], length);
2884 // Return the addend to use for a target specific relocation. The
2885 // only target specific relocation is R_X86_64_TLSDESC for a local
2886 // symbol. We want to set the addend is the offset of the local
2887 // symbol in the TLS segment.
2889 uint64_t
2890 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
2891 uint64_t) const
2893 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
2894 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
2895 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
2896 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
2897 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
2898 gold_assert(psymval->is_tls_symbol());
2899 // The value of a TLS symbol is the offset in the TLS segment.
2900 return psymval->value(ti.object, 0);
2903 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2904 // compiled with -fstack-split. The function calls non-stack-split
2905 // code. We have to change the function so that it always ensures
2906 // that it has enough stack space to run some random function.
2908 void
2909 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2910 section_offset_type fnoffset,
2911 section_size_type fnsize,
2912 unsigned char* view,
2913 section_size_type view_size,
2914 std::string* from,
2915 std::string* to) const
2917 // The function starts with a comparison of the stack pointer and a
2918 // field in the TCB. This is followed by a jump.
2920 // cmp %fs:NN,%rsp
2921 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2922 && fnsize > 9)
2924 // We will call __morestack if the carry flag is set after this
2925 // comparison. We turn the comparison into an stc instruction
2926 // and some nops.
2927 view[fnoffset] = '\xf9';
2928 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2930 // lea NN(%rsp),%r10
2931 // lea NN(%rsp),%r11
2932 else if ((this->match_view(view, view_size, fnoffset,
2933 "\x4c\x8d\x94\x24", 4)
2934 || this->match_view(view, view_size, fnoffset,
2935 "\x4c\x8d\x9c\x24", 4))
2936 && fnsize > 8)
2938 // This is loading an offset from the stack pointer for a
2939 // comparison. The offset is negative, so we decrease the
2940 // offset by the amount of space we need for the stack. This
2941 // means we will avoid calling __morestack if there happens to
2942 // be plenty of space on the stack already.
2943 unsigned char* pval = view + fnoffset + 4;
2944 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2945 val -= parameters->options().split_stack_adjust_size();
2946 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2948 else
2950 if (!object->has_no_split_stack())
2951 object->error(_("failed to match split-stack sequence at "
2952 "section %u offset %0zx"),
2953 shndx, static_cast<size_t>(fnoffset));
2954 return;
2957 // We have to change the function so that it calls
2958 // __morestack_non_split instead of __morestack. The former will
2959 // allocate additional stack space.
2960 *from = "__morestack";
2961 *to = "__morestack_non_split";
2964 // The selector for x86_64 object files.
2966 class Target_selector_x86_64 : public Target_selector_freebsd
2968 public:
2969 Target_selector_x86_64()
2970 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2971 "elf64-x86-64-freebsd")
2974 Target*
2975 do_instantiate_target()
2976 { return new Target_x86_64(); }
2980 Target_selector_x86_64 target_selector_x86_64;
2982 } // End anonymous namespace.