1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_x86_64
;
49 // The x86_64 target class.
51 // http://www.x86-64.org/documentation/abi.pdf
52 // TLS info comes from
53 // http://people.redhat.com/drepper/tls.pdf
54 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
56 class Target_x86_64
: public Target_freebsd
<64, false>
59 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
60 // uses only Elf64_Rela relocation entries with explicit addends."
61 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
64 : Target_freebsd
<64, false>(&x86_64_info
),
65 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
66 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
67 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
70 // Hook for a new output section.
72 do_new_output_section(Output_section
*) const;
74 // Scan the relocations to look for symbol adjustments.
76 gc_process_relocs(const General_options
& options
,
79 Sized_relobj
<64, false>* object
,
80 unsigned int data_shndx
,
82 const unsigned char* prelocs
,
84 Output_section
* output_section
,
85 bool needs_special_offset_handling
,
86 size_t local_symbol_count
,
87 const unsigned char* plocal_symbols
);
89 // Scan the relocations to look for symbol adjustments.
91 scan_relocs(const General_options
& options
,
94 Sized_relobj
<64, false>* object
,
95 unsigned int data_shndx
,
97 const unsigned char* prelocs
,
99 Output_section
* output_section
,
100 bool needs_special_offset_handling
,
101 size_t local_symbol_count
,
102 const unsigned char* plocal_symbols
);
104 // Finalize the sections.
106 do_finalize_sections(Layout
*);
108 // Return the value to use for a dynamic which requires special
111 do_dynsym_value(const Symbol
*) const;
113 // Relocate a section.
115 relocate_section(const Relocate_info
<64, false>*,
116 unsigned int sh_type
,
117 const unsigned char* prelocs
,
119 Output_section
* output_section
,
120 bool needs_special_offset_handling
,
122 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
123 section_size_type view_size
);
125 // Scan the relocs during a relocatable link.
127 scan_relocatable_relocs(const General_options
& options
,
128 Symbol_table
* symtab
,
130 Sized_relobj
<64, false>* object
,
131 unsigned int data_shndx
,
132 unsigned int sh_type
,
133 const unsigned char* prelocs
,
135 Output_section
* output_section
,
136 bool needs_special_offset_handling
,
137 size_t local_symbol_count
,
138 const unsigned char* plocal_symbols
,
139 Relocatable_relocs
*);
141 // Relocate a section during a relocatable link.
143 relocate_for_relocatable(const Relocate_info
<64, false>*,
144 unsigned int sh_type
,
145 const unsigned char* prelocs
,
147 Output_section
* output_section
,
148 off_t offset_in_output_section
,
149 const Relocatable_relocs
*,
151 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
152 section_size_type view_size
,
153 unsigned char* reloc_view
,
154 section_size_type reloc_view_size
);
156 // Return a string used to fill a code section with nops.
158 do_code_fill(section_size_type length
) const;
160 // Return whether SYM is defined by the ABI.
162 do_is_defined_by_abi(const Symbol
* sym
) const
163 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
165 // Return the size of the GOT section.
169 gold_assert(this->got_
!= NULL
);
170 return this->got_
->data_size();
174 // The class which scans relocations.
179 : issued_non_pic_error_(false)
183 local(const General_options
& options
, Symbol_table
* symtab
,
184 Layout
* layout
, Target_x86_64
* target
,
185 Sized_relobj
<64, false>* object
,
186 unsigned int data_shndx
,
187 Output_section
* output_section
,
188 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
189 const elfcpp::Sym
<64, false>& lsym
);
192 global(const General_options
& options
, Symbol_table
* symtab
,
193 Layout
* layout
, Target_x86_64
* target
,
194 Sized_relobj
<64, false>* object
,
195 unsigned int data_shndx
,
196 Output_section
* output_section
,
197 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
202 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
205 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
209 check_non_pic(Relobj
*, unsigned int r_type
);
211 // Whether we have issued an error about a non-PIC compilation.
212 bool issued_non_pic_error_
;
215 // The class which implements relocation.
220 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
225 if (this->skip_call_tls_get_addr_
)
227 // FIXME: This needs to specify the location somehow.
228 gold_error(_("missing expected TLS relocation"));
232 // Do a relocation. Return false if the caller should not issue
233 // any warnings about this relocation.
235 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
236 size_t relnum
, const elfcpp::Rela
<64, false>&,
237 unsigned int r_type
, const Sized_symbol
<64>*,
238 const Symbol_value
<64>*,
239 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
243 // Do a TLS relocation.
245 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
246 size_t relnum
, const elfcpp::Rela
<64, false>&,
247 unsigned int r_type
, const Sized_symbol
<64>*,
248 const Symbol_value
<64>*,
249 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
252 // Do a TLS General-Dynamic to Initial-Exec transition.
254 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
255 Output_segment
* tls_segment
,
256 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
257 elfcpp::Elf_types
<64>::Elf_Addr value
,
259 elfcpp::Elf_types
<64>::Elf_Addr
,
260 section_size_type view_size
);
262 // Do a TLS General-Dynamic to Local-Exec transition.
264 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
265 Output_segment
* tls_segment
,
266 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
267 elfcpp::Elf_types
<64>::Elf_Addr value
,
269 section_size_type view_size
);
271 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
273 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
274 Output_segment
* tls_segment
,
275 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
276 elfcpp::Elf_types
<64>::Elf_Addr value
,
278 elfcpp::Elf_types
<64>::Elf_Addr
,
279 section_size_type view_size
);
281 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
283 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
284 Output_segment
* tls_segment
,
285 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
286 elfcpp::Elf_types
<64>::Elf_Addr value
,
288 section_size_type view_size
);
290 // Do a TLS Local-Dynamic to Local-Exec transition.
292 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
293 Output_segment
* tls_segment
,
294 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
295 elfcpp::Elf_types
<64>::Elf_Addr value
,
297 section_size_type view_size
);
299 // Do a TLS Initial-Exec to Local-Exec transition.
301 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
302 Output_segment
* tls_segment
,
303 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
304 elfcpp::Elf_types
<64>::Elf_Addr value
,
306 section_size_type view_size
);
308 // This is set if we should skip the next reloc, which should be a
309 // PLT32 reloc against ___tls_get_addr.
310 bool skip_call_tls_get_addr_
;
312 // This is set if we see a relocation which could load the address
313 // of the TLS block. Whether we see such a relocation determines
314 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
315 // in debugging sections.
316 bool saw_tls_block_reloc_
;
319 // A class which returns the size required for a relocation type,
320 // used while scanning relocs during a relocatable link.
321 class Relocatable_size_for_reloc
325 get_size_for_reloc(unsigned int, Relobj
*);
328 // Adjust TLS relocation type based on the options and whether this
329 // is a local symbol.
330 static tls::Tls_optimization
331 optimize_tls_reloc(bool is_final
, int r_type
);
333 // Get the GOT section, creating it if necessary.
334 Output_data_got
<64, false>*
335 got_section(Symbol_table
*, Layout
*);
337 // Get the GOT PLT section.
339 got_plt_section() const
341 gold_assert(this->got_plt_
!= NULL
);
342 return this->got_plt_
;
345 // Create the PLT section.
347 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
349 // Create a PLT entry for a global symbol.
351 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
353 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
355 define_tls_base_symbol(Symbol_table
*, Layout
*);
357 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
359 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
361 // Create a GOT entry for the TLS module index.
363 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
364 Sized_relobj
<64, false>* object
);
366 // Get the PLT section.
367 Output_data_plt_x86_64
*
370 gold_assert(this->plt_
!= NULL
);
374 // Get the dynamic reloc section, creating it if necessary.
376 rela_dyn_section(Layout
*);
378 // Add a potential copy relocation.
380 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
381 Sized_relobj
<64, false>* object
,
382 unsigned int shndx
, Output_section
* output_section
,
383 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
385 this->copy_relocs_
.copy_reloc(symtab
, layout
,
386 symtab
->get_sized_symbol
<64>(sym
),
387 object
, shndx
, output_section
,
388 reloc
, this->rela_dyn_section(layout
));
391 // Information about this specific target which we pass to the
392 // general Target structure.
393 static const Target::Target_info x86_64_info
;
397 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
398 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
399 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
400 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
404 Output_data_got
<64, false>* got_
;
406 Output_data_plt_x86_64
* plt_
;
407 // The GOT PLT section.
408 Output_data_space
* got_plt_
;
409 // The dynamic reloc section.
410 Reloc_section
* rela_dyn_
;
411 // Relocs saved to avoid a COPY reloc.
412 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
413 // Space for variables copied with a COPY reloc.
414 Output_data_space
* dynbss_
;
415 // Offset of the GOT entry for the TLS module index.
416 unsigned int got_mod_index_offset_
;
417 // True if the _TLS_MODULE_BASE_ symbol has been defined.
418 bool tls_base_symbol_defined_
;
421 const Target::Target_info
Target_x86_64::x86_64_info
=
424 false, // is_big_endian
425 elfcpp::EM_X86_64
, // machine_code
426 false, // has_make_symbol
427 false, // has_resolve
428 true, // has_code_fill
429 true, // is_default_stack_executable
431 "/lib/ld64.so.1", // program interpreter
432 0x400000, // default_text_segment_address
433 0x1000, // abi_pagesize (overridable by -z max-page-size)
434 0x1000, // common_pagesize (overridable by -z common-page-size)
435 elfcpp::SHN_UNDEF
, // small_common_shndx
436 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
437 0, // small_common_section_flags
438 elfcpp::SHF_X86_64_LARGE
// large_common_section_flags
441 // This is called when a new output section is created. This is where
442 // we handle the SHF_X86_64_LARGE.
445 Target_x86_64::do_new_output_section(Output_section
*os
) const
447 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
448 os
->set_is_large_section();
451 // Get the GOT section, creating it if necessary.
453 Output_data_got
<64, false>*
454 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
456 if (this->got_
== NULL
)
458 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
460 this->got_
= new Output_data_got
<64, false>();
463 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
465 | elfcpp::SHF_WRITE
),
469 // The old GNU linker creates a .got.plt section. We just
470 // create another set of data in the .got section. Note that we
471 // always create a PLT if we create a GOT, although the PLT
473 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
474 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
476 | elfcpp::SHF_WRITE
),
480 // The first three entries are reserved.
481 this->got_plt_
->set_current_data_size(3 * 8);
483 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
484 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
486 0, 0, elfcpp::STT_OBJECT
,
488 elfcpp::STV_HIDDEN
, 0,
495 // Get the dynamic reloc section, creating it if necessary.
497 Target_x86_64::Reloc_section
*
498 Target_x86_64::rela_dyn_section(Layout
* layout
)
500 if (this->rela_dyn_
== NULL
)
502 gold_assert(layout
!= NULL
);
503 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
504 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
505 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
507 return this->rela_dyn_
;
510 // A class to handle the PLT data.
512 class Output_data_plt_x86_64
: public Output_section_data
515 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
517 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
520 // Add an entry to the PLT.
522 add_entry(Symbol
* gsym
);
524 // Add the reserved TLSDESC_PLT entry to the PLT.
526 reserve_tlsdesc_entry(unsigned int got_offset
)
527 { this->tlsdesc_got_offset_
= got_offset
; }
529 // Return true if a TLSDESC_PLT entry has been reserved.
531 has_tlsdesc_entry() const
532 { return this->tlsdesc_got_offset_
!= -1U; }
534 // Return the GOT offset for the reserved TLSDESC_PLT entry.
536 get_tlsdesc_got_offset() const
537 { return this->tlsdesc_got_offset_
; }
539 // Return the offset of the reserved TLSDESC_PLT entry.
541 get_tlsdesc_plt_offset() const
542 { return (this->count_
+ 1) * plt_entry_size
; }
544 // Return the .rel.plt section data.
547 { return this->rel_
; }
551 do_adjust_output_section(Output_section
* os
);
553 // Write to a map file.
555 do_print_to_mapfile(Mapfile
* mapfile
) const
556 { mapfile
->print_output_data(this, _("** PLT")); }
559 // The size of an entry in the PLT.
560 static const int plt_entry_size
= 16;
562 // The first entry in the PLT.
563 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
564 // procedure linkage table for both programs and shared objects."
565 static unsigned char first_plt_entry
[plt_entry_size
];
567 // Other entries in the PLT for an executable.
568 static unsigned char plt_entry
[plt_entry_size
];
570 // The reserved TLSDESC entry in the PLT for an executable.
571 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
573 // Set the final size.
575 set_final_data_size();
577 // Write out the PLT data.
579 do_write(Output_file
*);
581 // The reloc section.
584 Output_data_got
<64, false>* got_
;
585 // The .got.plt section.
586 Output_data_space
* got_plt_
;
587 // The number of PLT entries.
589 // Offset of the reserved TLSDESC_GOT entry when needed.
590 unsigned int tlsdesc_got_offset_
;
593 // Create the PLT section. The ordinary .got section is an argument,
594 // since we need to refer to the start. We also create our own .got
595 // section just for PLT entries.
597 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
598 Output_data_got
<64, false>* got
,
599 Output_data_space
* got_plt
)
600 : Output_section_data(8), got_(got
), got_plt_(got_plt
), count_(0),
601 tlsdesc_got_offset_(-1U)
603 this->rel_
= new Reloc_section(false);
604 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
605 elfcpp::SHF_ALLOC
, this->rel_
);
609 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
611 os
->set_entsize(plt_entry_size
);
614 // Add an entry to the PLT.
617 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
619 gold_assert(!gsym
->has_plt_offset());
621 // Note that when setting the PLT offset we skip the initial
622 // reserved PLT entry.
623 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
627 section_offset_type got_offset
= this->got_plt_
->current_data_size();
629 // Every PLT entry needs a GOT entry which points back to the PLT
630 // entry (this will be changed by the dynamic linker, normally
631 // lazily when the function is called).
632 this->got_plt_
->set_current_data_size(got_offset
+ 8);
634 // Every PLT entry needs a reloc.
635 gsym
->set_needs_dynsym_entry();
636 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
639 // Note that we don't need to save the symbol. The contents of the
640 // PLT are independent of which symbols are used. The symbols only
641 // appear in the relocations.
644 // Set the final size.
646 Output_data_plt_x86_64::set_final_data_size()
648 unsigned int count
= this->count_
;
649 if (this->has_tlsdesc_entry())
651 this->set_data_size((count
+ 1) * plt_entry_size
);
654 // The first entry in the PLT for an executable.
656 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
658 // From AMD64 ABI Draft 0.98, page 76
659 0xff, 0x35, // pushq contents of memory address
660 0, 0, 0, 0, // replaced with address of .got + 8
661 0xff, 0x25, // jmp indirect
662 0, 0, 0, 0, // replaced with address of .got + 16
663 0x90, 0x90, 0x90, 0x90 // noop (x4)
666 // Subsequent entries in the PLT for an executable.
668 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
670 // From AMD64 ABI Draft 0.98, page 76
671 0xff, 0x25, // jmpq indirect
672 0, 0, 0, 0, // replaced with address of symbol in .got
673 0x68, // pushq immediate
674 0, 0, 0, 0, // replaced with offset into relocation table
675 0xe9, // jmpq relative
676 0, 0, 0, 0 // replaced with offset to start of .plt
679 // The reserved TLSDESC entry in the PLT for an executable.
681 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
683 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
684 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
685 0xff, 0x35, // pushq x(%rip)
686 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
687 0xff, 0x25, // jmpq *y(%rip)
688 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
693 // Write out the PLT. This uses the hand-coded instructions above,
694 // and adjusts them as needed. This is specified by the AMD64 ABI.
697 Output_data_plt_x86_64::do_write(Output_file
* of
)
699 const off_t offset
= this->offset();
700 const section_size_type oview_size
=
701 convert_to_section_size_type(this->data_size());
702 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
704 const off_t got_file_offset
= this->got_plt_
->offset();
705 const section_size_type got_size
=
706 convert_to_section_size_type(this->got_plt_
->data_size());
707 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
710 unsigned char* pov
= oview
;
712 // The base address of the .plt section.
713 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
714 // The base address of the .got section.
715 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
716 // The base address of the PLT portion of the .got section,
717 // which is where the GOT pointer will point, and where the
718 // three reserved GOT entries are located.
719 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
721 memcpy(pov
, first_plt_entry
, plt_entry_size
);
722 // We do a jmp relative to the PC at the end of this instruction.
723 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
725 - (plt_address
+ 6)));
726 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
728 - (plt_address
+ 12)));
729 pov
+= plt_entry_size
;
731 unsigned char* got_pov
= got_view
;
733 memset(got_pov
, 0, 24);
736 unsigned int plt_offset
= plt_entry_size
;
737 unsigned int got_offset
= 24;
738 const unsigned int count
= this->count_
;
739 for (unsigned int plt_index
= 0;
742 pov
+= plt_entry_size
,
744 plt_offset
+= plt_entry_size
,
747 // Set and adjust the PLT entry itself.
748 memcpy(pov
, plt_entry
, plt_entry_size
);
749 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
750 (got_address
+ got_offset
751 - (plt_address
+ plt_offset
754 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
755 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
756 - (plt_offset
+ plt_entry_size
));
758 // Set the entry in the GOT.
759 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
762 if (this->has_tlsdesc_entry())
764 // Set and adjust the reserved TLSDESC PLT entry.
765 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
766 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
767 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
769 - (plt_address
+ plt_offset
771 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
774 - (plt_address
+ plt_offset
776 pov
+= plt_entry_size
;
779 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
780 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
782 of
->write_output_view(offset
, oview_size
, oview
);
783 of
->write_output_view(got_file_offset
, got_size
, got_view
);
786 // Create the PLT section.
789 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
791 if (this->plt_
== NULL
)
793 // Create the GOT sections first.
794 this->got_section(symtab
, layout
);
796 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
798 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
800 | elfcpp::SHF_EXECINSTR
),
805 // Create a PLT entry for a global symbol.
808 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
811 if (gsym
->has_plt_offset())
814 if (this->plt_
== NULL
)
815 this->make_plt_section(symtab
, layout
);
817 this->plt_
->add_entry(gsym
);
820 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
823 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
825 if (this->tls_base_symbol_defined_
)
828 Output_segment
* tls_segment
= layout
->tls_segment();
829 if (tls_segment
!= NULL
)
831 bool is_exec
= parameters
->options().output_is_executable();
832 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
836 elfcpp::STV_HIDDEN
, 0,
838 ? Symbol::SEGMENT_END
839 : Symbol::SEGMENT_START
),
842 this->tls_base_symbol_defined_
= true;
845 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
848 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
851 if (this->plt_
== NULL
)
852 this->make_plt_section(symtab
, layout
);
854 if (!this->plt_
->has_tlsdesc_entry())
856 // Allocate the TLSDESC_GOT entry.
857 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
858 unsigned int got_offset
= got
->add_constant(0);
860 // Allocate the TLSDESC_PLT entry.
861 this->plt_
->reserve_tlsdesc_entry(got_offset
);
865 // Create a GOT entry for the TLS module index.
868 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
869 Sized_relobj
<64, false>* object
)
871 if (this->got_mod_index_offset_
== -1U)
873 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
874 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
875 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
876 unsigned int got_offset
= got
->add_constant(0);
877 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
879 got
->add_constant(0);
880 this->got_mod_index_offset_
= got_offset
;
882 return this->got_mod_index_offset_
;
885 // Optimize the TLS relocation type based on what we know about the
886 // symbol. IS_FINAL is true if the final address of this symbol is
887 // known at link time.
889 tls::Tls_optimization
890 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
892 // If we are generating a shared library, then we can't do anything
894 if (parameters
->options().shared())
895 return tls::TLSOPT_NONE
;
899 case elfcpp::R_X86_64_TLSGD
:
900 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
901 case elfcpp::R_X86_64_TLSDESC_CALL
:
902 // These are General-Dynamic which permits fully general TLS
903 // access. Since we know that we are generating an executable,
904 // we can convert this to Initial-Exec. If we also know that
905 // this is a local symbol, we can further switch to Local-Exec.
907 return tls::TLSOPT_TO_LE
;
908 return tls::TLSOPT_TO_IE
;
910 case elfcpp::R_X86_64_TLSLD
:
911 // This is Local-Dynamic, which refers to a local symbol in the
912 // dynamic TLS block. Since we know that we generating an
913 // executable, we can switch to Local-Exec.
914 return tls::TLSOPT_TO_LE
;
916 case elfcpp::R_X86_64_DTPOFF32
:
917 case elfcpp::R_X86_64_DTPOFF64
:
918 // Another Local-Dynamic reloc.
919 return tls::TLSOPT_TO_LE
;
921 case elfcpp::R_X86_64_GOTTPOFF
:
922 // These are Initial-Exec relocs which get the thread offset
923 // from the GOT. If we know that we are linking against the
924 // local symbol, we can switch to Local-Exec, which links the
925 // thread offset into the instruction.
927 return tls::TLSOPT_TO_LE
;
928 return tls::TLSOPT_NONE
;
930 case elfcpp::R_X86_64_TPOFF32
:
931 // When we already have Local-Exec, there is nothing further we
933 return tls::TLSOPT_NONE
;
940 // Report an unsupported relocation against a local symbol.
943 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
946 gold_error(_("%s: unsupported reloc %u against local symbol"),
947 object
->name().c_str(), r_type
);
950 // We are about to emit a dynamic relocation of type R_TYPE. If the
951 // dynamic linker does not support it, issue an error. The GNU linker
952 // only issues a non-PIC error for an allocated read-only section.
953 // Here we know the section is allocated, but we don't know that it is
954 // read-only. But we check for all the relocation types which the
955 // glibc dynamic linker supports, so it seems appropriate to issue an
956 // error even if the section is not read-only.
959 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
963 // These are the relocation types supported by glibc for x86_64.
964 case elfcpp::R_X86_64_RELATIVE
:
965 case elfcpp::R_X86_64_GLOB_DAT
:
966 case elfcpp::R_X86_64_JUMP_SLOT
:
967 case elfcpp::R_X86_64_DTPMOD64
:
968 case elfcpp::R_X86_64_DTPOFF64
:
969 case elfcpp::R_X86_64_TPOFF64
:
970 case elfcpp::R_X86_64_64
:
971 case elfcpp::R_X86_64_32
:
972 case elfcpp::R_X86_64_PC32
:
973 case elfcpp::R_X86_64_COPY
:
977 // This prevents us from issuing more than one error per reloc
978 // section. But we can still wind up issuing more than one
979 // error per object file.
980 if (this->issued_non_pic_error_
)
982 gold_assert(parameters
->options().output_is_position_independent());
983 object
->error(_("requires unsupported dynamic reloc; "
984 "recompile with -fPIC"));
985 this->issued_non_pic_error_
= true;
988 case elfcpp::R_X86_64_NONE
:
993 // Scan a relocation for a local symbol.
996 Target_x86_64::Scan::local(const General_options
&,
997 Symbol_table
* symtab
,
999 Target_x86_64
* target
,
1000 Sized_relobj
<64, false>* object
,
1001 unsigned int data_shndx
,
1002 Output_section
* output_section
,
1003 const elfcpp::Rela
<64, false>& reloc
,
1004 unsigned int r_type
,
1005 const elfcpp::Sym
<64, false>& lsym
)
1009 case elfcpp::R_X86_64_NONE
:
1010 case elfcpp::R_386_GNU_VTINHERIT
:
1011 case elfcpp::R_386_GNU_VTENTRY
:
1014 case elfcpp::R_X86_64_64
:
1015 // If building a shared library (or a position-independent
1016 // executable), we need to create a dynamic relocation for this
1017 // location. The relocation applied at link time will apply the
1018 // link-time value, so we flag the location with an
1019 // R_X86_64_RELATIVE relocation so the dynamic loader can
1020 // relocate it easily.
1021 if (parameters
->options().output_is_position_independent())
1023 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1024 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1025 rela_dyn
->add_local_relative(object
, r_sym
,
1026 elfcpp::R_X86_64_RELATIVE
,
1027 output_section
, data_shndx
,
1028 reloc
.get_r_offset(),
1029 reloc
.get_r_addend());
1033 case elfcpp::R_X86_64_32
:
1034 case elfcpp::R_X86_64_32S
:
1035 case elfcpp::R_X86_64_16
:
1036 case elfcpp::R_X86_64_8
:
1037 // If building a shared library (or a position-independent
1038 // executable), we need to create a dynamic relocation for this
1039 // location. We can't use an R_X86_64_RELATIVE relocation
1040 // because that is always a 64-bit relocation.
1041 if (parameters
->options().output_is_position_independent())
1043 this->check_non_pic(object
, r_type
);
1045 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1046 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1047 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1048 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1049 data_shndx
, reloc
.get_r_offset(),
1050 reloc
.get_r_addend());
1053 gold_assert(lsym
.get_st_value() == 0);
1054 unsigned int shndx
= lsym
.get_st_shndx();
1056 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1059 object
->error(_("section symbol %u has bad shndx %u"),
1062 rela_dyn
->add_local_section(object
, shndx
,
1063 r_type
, output_section
,
1064 data_shndx
, reloc
.get_r_offset(),
1065 reloc
.get_r_addend());
1070 case elfcpp::R_X86_64_PC64
:
1071 case elfcpp::R_X86_64_PC32
:
1072 case elfcpp::R_X86_64_PC16
:
1073 case elfcpp::R_X86_64_PC8
:
1076 case elfcpp::R_X86_64_PLT32
:
1077 // Since we know this is a local symbol, we can handle this as a
1081 case elfcpp::R_X86_64_GOTPC32
:
1082 case elfcpp::R_X86_64_GOTOFF64
:
1083 case elfcpp::R_X86_64_GOTPC64
:
1084 case elfcpp::R_X86_64_PLTOFF64
:
1085 // We need a GOT section.
1086 target
->got_section(symtab
, layout
);
1087 // For PLTOFF64, we'd normally want a PLT section, but since we
1088 // know this is a local symbol, no PLT is needed.
1091 case elfcpp::R_X86_64_GOT64
:
1092 case elfcpp::R_X86_64_GOT32
:
1093 case elfcpp::R_X86_64_GOTPCREL64
:
1094 case elfcpp::R_X86_64_GOTPCREL
:
1095 case elfcpp::R_X86_64_GOTPLT64
:
1097 // The symbol requires a GOT entry.
1098 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1099 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1100 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1102 // If we are generating a shared object, we need to add a
1103 // dynamic relocation for this symbol's GOT entry.
1104 if (parameters
->options().output_is_position_independent())
1106 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1107 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1108 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1109 rela_dyn
->add_local_relative(
1110 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1111 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1114 this->check_non_pic(object
, r_type
);
1116 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1117 rela_dyn
->add_local(
1118 object
, r_sym
, r_type
, got
,
1119 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1123 // For GOTPLT64, we'd normally want a PLT section, but since
1124 // we know this is a local symbol, no PLT is needed.
1128 case elfcpp::R_X86_64_COPY
:
1129 case elfcpp::R_X86_64_GLOB_DAT
:
1130 case elfcpp::R_X86_64_JUMP_SLOT
:
1131 case elfcpp::R_X86_64_RELATIVE
:
1132 // These are outstanding tls relocs, which are unexpected when linking
1133 case elfcpp::R_X86_64_TPOFF64
:
1134 case elfcpp::R_X86_64_DTPMOD64
:
1135 case elfcpp::R_X86_64_TLSDESC
:
1136 gold_error(_("%s: unexpected reloc %u in object file"),
1137 object
->name().c_str(), r_type
);
1140 // These are initial tls relocs, which are expected when linking
1141 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1142 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1143 case elfcpp::R_X86_64_TLSDESC_CALL
:
1144 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1145 case elfcpp::R_X86_64_DTPOFF32
:
1146 case elfcpp::R_X86_64_DTPOFF64
:
1147 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1148 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1150 bool output_is_shared
= parameters
->options().shared();
1151 const tls::Tls_optimization optimized_type
1152 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1155 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1156 if (optimized_type
== tls::TLSOPT_NONE
)
1158 // Create a pair of GOT entries for the module index and
1159 // dtv-relative offset.
1160 Output_data_got
<64, false>* got
1161 = target
->got_section(symtab
, layout
);
1162 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1163 unsigned int shndx
= lsym
.get_st_shndx();
1165 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1167 object
->error(_("local symbol %u has bad shndx %u"),
1170 got
->add_local_pair_with_rela(object
, r_sym
,
1173 target
->rela_dyn_section(layout
),
1174 elfcpp::R_X86_64_DTPMOD64
, 0);
1176 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1177 unsupported_reloc_local(object
, r_type
);
1180 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1181 target
->define_tls_base_symbol(symtab
, layout
);
1182 if (optimized_type
== tls::TLSOPT_NONE
)
1184 // Create reserved PLT and GOT entries for the resolver.
1185 target
->reserve_tlsdesc_entries(symtab
, layout
);
1187 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1188 Output_data_got
<64, false>* got
1189 = target
->got_section(symtab
, layout
);
1190 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1191 unsigned int shndx
= lsym
.get_st_shndx();
1193 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1195 object
->error(_("local symbol %u has bad shndx %u"),
1198 got
->add_local_pair_with_rela(object
, r_sym
,
1201 target
->rela_dyn_section(layout
),
1202 elfcpp::R_X86_64_TLSDESC
, 0);
1204 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1205 unsupported_reloc_local(object
, r_type
);
1208 case elfcpp::R_X86_64_TLSDESC_CALL
:
1211 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1212 if (optimized_type
== tls::TLSOPT_NONE
)
1214 // Create a GOT entry for the module index.
1215 target
->got_mod_index_entry(symtab
, layout
, object
);
1217 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1218 unsupported_reloc_local(object
, r_type
);
1221 case elfcpp::R_X86_64_DTPOFF32
:
1222 case elfcpp::R_X86_64_DTPOFF64
:
1225 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1226 layout
->set_has_static_tls();
1227 if (optimized_type
== tls::TLSOPT_NONE
)
1229 // Create a GOT entry for the tp-relative offset.
1230 Output_data_got
<64, false>* got
1231 = target
->got_section(symtab
, layout
);
1232 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1233 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1234 target
->rela_dyn_section(layout
),
1235 elfcpp::R_X86_64_TPOFF64
);
1237 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1238 unsupported_reloc_local(object
, r_type
);
1241 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1242 layout
->set_has_static_tls();
1243 if (output_is_shared
)
1244 unsupported_reloc_local(object
, r_type
);
1253 case elfcpp::R_X86_64_SIZE32
:
1254 case elfcpp::R_X86_64_SIZE64
:
1256 gold_error(_("%s: unsupported reloc %u against local symbol"),
1257 object
->name().c_str(), r_type
);
1263 // Report an unsupported relocation against a global symbol.
1266 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1267 unsigned int r_type
,
1270 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1271 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1274 // Scan a relocation for a global symbol.
1277 Target_x86_64::Scan::global(const General_options
&,
1278 Symbol_table
* symtab
,
1280 Target_x86_64
* target
,
1281 Sized_relobj
<64, false>* object
,
1282 unsigned int data_shndx
,
1283 Output_section
* output_section
,
1284 const elfcpp::Rela
<64, false>& reloc
,
1285 unsigned int r_type
,
1290 case elfcpp::R_X86_64_NONE
:
1291 case elfcpp::R_386_GNU_VTINHERIT
:
1292 case elfcpp::R_386_GNU_VTENTRY
:
1295 case elfcpp::R_X86_64_64
:
1296 case elfcpp::R_X86_64_32
:
1297 case elfcpp::R_X86_64_32S
:
1298 case elfcpp::R_X86_64_16
:
1299 case elfcpp::R_X86_64_8
:
1301 // Make a PLT entry if necessary.
1302 if (gsym
->needs_plt_entry())
1304 target
->make_plt_entry(symtab
, layout
, gsym
);
1305 // Since this is not a PC-relative relocation, we may be
1306 // taking the address of a function. In that case we need to
1307 // set the entry in the dynamic symbol table to the address of
1309 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1310 gsym
->set_needs_dynsym_value();
1312 // Make a dynamic relocation if necessary.
1313 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1315 if (gsym
->may_need_copy_reloc())
1317 target
->copy_reloc(symtab
, layout
, object
,
1318 data_shndx
, output_section
, gsym
, reloc
);
1320 else if (r_type
== elfcpp::R_X86_64_64
1321 && gsym
->can_use_relative_reloc(false))
1323 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1324 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1325 output_section
, object
,
1326 data_shndx
, reloc
.get_r_offset(),
1327 reloc
.get_r_addend());
1331 this->check_non_pic(object
, r_type
);
1332 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1333 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1334 data_shndx
, reloc
.get_r_offset(),
1335 reloc
.get_r_addend());
1341 case elfcpp::R_X86_64_PC64
:
1342 case elfcpp::R_X86_64_PC32
:
1343 case elfcpp::R_X86_64_PC16
:
1344 case elfcpp::R_X86_64_PC8
:
1346 // Make a PLT entry if necessary.
1347 if (gsym
->needs_plt_entry())
1348 target
->make_plt_entry(symtab
, layout
, gsym
);
1349 // Make a dynamic relocation if necessary.
1350 int flags
= Symbol::NON_PIC_REF
;
1351 if (gsym
->type() == elfcpp::STT_FUNC
)
1352 flags
|= Symbol::FUNCTION_CALL
;
1353 if (gsym
->needs_dynamic_reloc(flags
))
1355 if (gsym
->may_need_copy_reloc())
1357 target
->copy_reloc(symtab
, layout
, object
,
1358 data_shndx
, output_section
, gsym
, reloc
);
1362 this->check_non_pic(object
, r_type
);
1363 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1364 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1365 data_shndx
, reloc
.get_r_offset(),
1366 reloc
.get_r_addend());
1372 case elfcpp::R_X86_64_GOT64
:
1373 case elfcpp::R_X86_64_GOT32
:
1374 case elfcpp::R_X86_64_GOTPCREL64
:
1375 case elfcpp::R_X86_64_GOTPCREL
:
1376 case elfcpp::R_X86_64_GOTPLT64
:
1378 // The symbol requires a GOT entry.
1379 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1380 if (gsym
->final_value_is_known())
1381 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1384 // If this symbol is not fully resolved, we need to add a
1385 // dynamic relocation for it.
1386 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1387 if (gsym
->is_from_dynobj()
1388 || gsym
->is_undefined()
1389 || gsym
->is_preemptible())
1390 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1391 elfcpp::R_X86_64_GLOB_DAT
);
1394 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1395 rela_dyn
->add_global_relative(
1396 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1397 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1400 // For GOTPLT64, we also need a PLT entry (but only if the
1401 // symbol is not fully resolved).
1402 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1403 && !gsym
->final_value_is_known())
1404 target
->make_plt_entry(symtab
, layout
, gsym
);
1408 case elfcpp::R_X86_64_PLT32
:
1409 // If the symbol is fully resolved, this is just a PC32 reloc.
1410 // Otherwise we need a PLT entry.
1411 if (gsym
->final_value_is_known())
1413 // If building a shared library, we can also skip the PLT entry
1414 // if the symbol is defined in the output file and is protected
1416 if (gsym
->is_defined()
1417 && !gsym
->is_from_dynobj()
1418 && !gsym
->is_preemptible())
1420 target
->make_plt_entry(symtab
, layout
, gsym
);
1423 case elfcpp::R_X86_64_GOTPC32
:
1424 case elfcpp::R_X86_64_GOTOFF64
:
1425 case elfcpp::R_X86_64_GOTPC64
:
1426 case elfcpp::R_X86_64_PLTOFF64
:
1427 // We need a GOT section.
1428 target
->got_section(symtab
, layout
);
1429 // For PLTOFF64, we also need a PLT entry (but only if the
1430 // symbol is not fully resolved).
1431 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1432 && !gsym
->final_value_is_known())
1433 target
->make_plt_entry(symtab
, layout
, gsym
);
1436 case elfcpp::R_X86_64_COPY
:
1437 case elfcpp::R_X86_64_GLOB_DAT
:
1438 case elfcpp::R_X86_64_JUMP_SLOT
:
1439 case elfcpp::R_X86_64_RELATIVE
:
1440 // These are outstanding tls relocs, which are unexpected when linking
1441 case elfcpp::R_X86_64_TPOFF64
:
1442 case elfcpp::R_X86_64_DTPMOD64
:
1443 case elfcpp::R_X86_64_TLSDESC
:
1444 gold_error(_("%s: unexpected reloc %u in object file"),
1445 object
->name().c_str(), r_type
);
1448 // These are initial tls relocs, which are expected for global()
1449 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1450 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1451 case elfcpp::R_X86_64_TLSDESC_CALL
:
1452 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1453 case elfcpp::R_X86_64_DTPOFF32
:
1454 case elfcpp::R_X86_64_DTPOFF64
:
1455 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1456 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1458 const bool is_final
= gsym
->final_value_is_known();
1459 const tls::Tls_optimization optimized_type
1460 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1463 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1464 if (optimized_type
== tls::TLSOPT_NONE
)
1466 // Create a pair of GOT entries for the module index and
1467 // dtv-relative offset.
1468 Output_data_got
<64, false>* got
1469 = target
->got_section(symtab
, layout
);
1470 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1471 target
->rela_dyn_section(layout
),
1472 elfcpp::R_X86_64_DTPMOD64
,
1473 elfcpp::R_X86_64_DTPOFF64
);
1475 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1477 // Create a GOT entry for the tp-relative offset.
1478 Output_data_got
<64, false>* got
1479 = target
->got_section(symtab
, layout
);
1480 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1481 target
->rela_dyn_section(layout
),
1482 elfcpp::R_X86_64_TPOFF64
);
1484 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1485 unsupported_reloc_global(object
, r_type
, gsym
);
1488 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1489 target
->define_tls_base_symbol(symtab
, layout
);
1490 if (optimized_type
== tls::TLSOPT_NONE
)
1492 // Create reserved PLT and GOT entries for the resolver.
1493 target
->reserve_tlsdesc_entries(symtab
, layout
);
1495 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1496 Output_data_got
<64, false>* got
1497 = target
->got_section(symtab
, layout
);
1498 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
,
1499 target
->rela_dyn_section(layout
),
1500 elfcpp::R_X86_64_TLSDESC
, 0);
1502 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1504 // Create a GOT entry for the tp-relative offset.
1505 Output_data_got
<64, false>* got
1506 = target
->got_section(symtab
, layout
);
1507 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1508 target
->rela_dyn_section(layout
),
1509 elfcpp::R_X86_64_TPOFF64
);
1511 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1512 unsupported_reloc_global(object
, r_type
, gsym
);
1515 case elfcpp::R_X86_64_TLSDESC_CALL
:
1518 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1519 if (optimized_type
== tls::TLSOPT_NONE
)
1521 // Create a GOT entry for the module index.
1522 target
->got_mod_index_entry(symtab
, layout
, object
);
1524 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1525 unsupported_reloc_global(object
, r_type
, gsym
);
1528 case elfcpp::R_X86_64_DTPOFF32
:
1529 case elfcpp::R_X86_64_DTPOFF64
:
1532 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1533 layout
->set_has_static_tls();
1534 if (optimized_type
== tls::TLSOPT_NONE
)
1536 // Create a GOT entry for the tp-relative offset.
1537 Output_data_got
<64, false>* got
1538 = target
->got_section(symtab
, layout
);
1539 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1540 target
->rela_dyn_section(layout
),
1541 elfcpp::R_X86_64_TPOFF64
);
1543 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1544 unsupported_reloc_global(object
, r_type
, gsym
);
1547 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1548 layout
->set_has_static_tls();
1549 if (parameters
->options().shared())
1550 unsupported_reloc_local(object
, r_type
);
1559 case elfcpp::R_X86_64_SIZE32
:
1560 case elfcpp::R_X86_64_SIZE64
:
1562 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1563 object
->name().c_str(), r_type
,
1564 gsym
->demangled_name().c_str());
1570 Target_x86_64::gc_process_relocs(const General_options
& options
,
1571 Symbol_table
* symtab
,
1573 Sized_relobj
<64, false>* object
,
1574 unsigned int data_shndx
,
1575 unsigned int sh_type
,
1576 const unsigned char* prelocs
,
1578 Output_section
* output_section
,
1579 bool needs_special_offset_handling
,
1580 size_t local_symbol_count
,
1581 const unsigned char* plocal_symbols
)
1584 if (sh_type
== elfcpp::SHT_REL
)
1589 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1590 Target_x86_64::Scan
>(
1600 needs_special_offset_handling
,
1605 // Scan relocations for a section.
1608 Target_x86_64::scan_relocs(const General_options
& options
,
1609 Symbol_table
* symtab
,
1611 Sized_relobj
<64, false>* object
,
1612 unsigned int data_shndx
,
1613 unsigned int sh_type
,
1614 const unsigned char* prelocs
,
1616 Output_section
* output_section
,
1617 bool needs_special_offset_handling
,
1618 size_t local_symbol_count
,
1619 const unsigned char* plocal_symbols
)
1621 if (sh_type
== elfcpp::SHT_REL
)
1623 gold_error(_("%s: unsupported REL reloc section"),
1624 object
->name().c_str());
1628 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1629 Target_x86_64::Scan
>(
1639 needs_special_offset_handling
,
1644 // Finalize the sections.
1647 Target_x86_64::do_finalize_sections(Layout
* layout
)
1649 // Fill in some more dynamic tags.
1650 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1653 if (this->got_plt_
!= NULL
)
1654 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1656 if (this->plt_
!= NULL
)
1658 const Output_data
* od
= this->plt_
->rel_plt();
1659 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1660 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1661 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1662 if (this->plt_
->has_tlsdesc_entry())
1664 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1665 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1666 this->got_
->finalize_data_size();
1667 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1668 this->plt_
, plt_offset
);
1669 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1670 this->got_
, got_offset
);
1674 if (this->rela_dyn_
!= NULL
)
1676 const Output_data
* od
= this->rela_dyn_
;
1677 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1678 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1679 odyn
->add_constant(elfcpp::DT_RELAENT
,
1680 elfcpp::Elf_sizes
<64>::rela_size
);
1683 if (!parameters
->options().shared())
1685 // The value of the DT_DEBUG tag is filled in by the dynamic
1686 // linker at run time, and used by the debugger.
1687 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1691 // Emit any relocs we saved in an attempt to avoid generating COPY
1693 if (this->copy_relocs_
.any_saved_relocs())
1694 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1697 // Perform a relocation.
1700 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1701 Target_x86_64
* target
,
1704 const elfcpp::Rela
<64, false>& rela
,
1705 unsigned int r_type
,
1706 const Sized_symbol
<64>* gsym
,
1707 const Symbol_value
<64>* psymval
,
1708 unsigned char* view
,
1709 elfcpp::Elf_types
<64>::Elf_Addr address
,
1710 section_size_type view_size
)
1712 if (this->skip_call_tls_get_addr_
)
1714 if ((r_type
!= elfcpp::R_X86_64_PLT32
1715 && r_type
!= elfcpp::R_X86_64_PC32
)
1717 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1719 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1720 _("missing expected TLS relocation"));
1724 this->skip_call_tls_get_addr_
= false;
1729 // Pick the value to use for symbols defined in shared objects.
1730 Symbol_value
<64> symval
;
1732 && gsym
->use_plt_offset(r_type
== elfcpp::R_X86_64_PC64
1733 || r_type
== elfcpp::R_X86_64_PC32
1734 || r_type
== elfcpp::R_X86_64_PC16
1735 || r_type
== elfcpp::R_X86_64_PC8
))
1737 symval
.set_output_value(target
->plt_section()->address()
1738 + gsym
->plt_offset());
1742 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1743 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1745 // Get the GOT offset if needed.
1746 // The GOT pointer points to the end of the GOT section.
1747 // We need to subtract the size of the GOT section to get
1748 // the actual offset to use in the relocation.
1749 bool have_got_offset
= false;
1750 unsigned int got_offset
= 0;
1753 case elfcpp::R_X86_64_GOT32
:
1754 case elfcpp::R_X86_64_GOT64
:
1755 case elfcpp::R_X86_64_GOTPLT64
:
1756 case elfcpp::R_X86_64_GOTPCREL
:
1757 case elfcpp::R_X86_64_GOTPCREL64
:
1760 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1761 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1765 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1766 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1767 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1768 - target
->got_size());
1770 have_got_offset
= true;
1779 case elfcpp::R_X86_64_NONE
:
1780 case elfcpp::R_386_GNU_VTINHERIT
:
1781 case elfcpp::R_386_GNU_VTENTRY
:
1784 case elfcpp::R_X86_64_64
:
1785 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1788 case elfcpp::R_X86_64_PC64
:
1789 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1793 case elfcpp::R_X86_64_32
:
1794 // FIXME: we need to verify that value + addend fits into 32 bits:
1795 // uint64_t x = value + addend;
1796 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1797 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1798 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1801 case elfcpp::R_X86_64_32S
:
1802 // FIXME: we need to verify that value + addend fits into 32 bits:
1803 // int64_t x = value + addend; // note this quantity is signed!
1804 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1805 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1808 case elfcpp::R_X86_64_PC32
:
1809 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1813 case elfcpp::R_X86_64_16
:
1814 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1817 case elfcpp::R_X86_64_PC16
:
1818 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1822 case elfcpp::R_X86_64_8
:
1823 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1826 case elfcpp::R_X86_64_PC8
:
1827 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1831 case elfcpp::R_X86_64_PLT32
:
1832 gold_assert(gsym
== NULL
1833 || gsym
->has_plt_offset()
1834 || gsym
->final_value_is_known()
1835 || (gsym
->is_defined()
1836 && !gsym
->is_from_dynobj()
1837 && !gsym
->is_preemptible()));
1838 // Note: while this code looks the same as for R_X86_64_PC32, it
1839 // behaves differently because psymval was set to point to
1840 // the PLT entry, rather than the symbol, in Scan::global().
1841 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1845 case elfcpp::R_X86_64_PLTOFF64
:
1848 gold_assert(gsym
->has_plt_offset()
1849 || gsym
->final_value_is_known());
1850 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1851 got_address
= target
->got_section(NULL
, NULL
)->address();
1852 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1853 addend
- got_address
);
1856 case elfcpp::R_X86_64_GOT32
:
1857 gold_assert(have_got_offset
);
1858 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1861 case elfcpp::R_X86_64_GOTPC32
:
1864 elfcpp::Elf_types
<64>::Elf_Addr value
;
1865 value
= target
->got_plt_section()->address();
1866 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1870 case elfcpp::R_X86_64_GOT64
:
1871 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1872 // Since we always add a PLT entry, this is equivalent.
1873 case elfcpp::R_X86_64_GOTPLT64
:
1874 gold_assert(have_got_offset
);
1875 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1878 case elfcpp::R_X86_64_GOTPC64
:
1881 elfcpp::Elf_types
<64>::Elf_Addr value
;
1882 value
= target
->got_plt_section()->address();
1883 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1887 case elfcpp::R_X86_64_GOTOFF64
:
1889 elfcpp::Elf_types
<64>::Elf_Addr value
;
1890 value
= (psymval
->value(object
, 0)
1891 - target
->got_plt_section()->address());
1892 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1896 case elfcpp::R_X86_64_GOTPCREL
:
1898 gold_assert(have_got_offset
);
1899 elfcpp::Elf_types
<64>::Elf_Addr value
;
1900 value
= target
->got_plt_section()->address() + got_offset
;
1901 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1905 case elfcpp::R_X86_64_GOTPCREL64
:
1907 gold_assert(have_got_offset
);
1908 elfcpp::Elf_types
<64>::Elf_Addr value
;
1909 value
= target
->got_plt_section()->address() + got_offset
;
1910 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1914 case elfcpp::R_X86_64_COPY
:
1915 case elfcpp::R_X86_64_GLOB_DAT
:
1916 case elfcpp::R_X86_64_JUMP_SLOT
:
1917 case elfcpp::R_X86_64_RELATIVE
:
1918 // These are outstanding tls relocs, which are unexpected when linking
1919 case elfcpp::R_X86_64_TPOFF64
:
1920 case elfcpp::R_X86_64_DTPMOD64
:
1921 case elfcpp::R_X86_64_TLSDESC
:
1922 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1923 _("unexpected reloc %u in object file"),
1927 // These are initial tls relocs, which are expected when linking
1928 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1929 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1930 case elfcpp::R_X86_64_TLSDESC_CALL
:
1931 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1932 case elfcpp::R_X86_64_DTPOFF32
:
1933 case elfcpp::R_X86_64_DTPOFF64
:
1934 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1935 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1936 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1937 view
, address
, view_size
);
1940 case elfcpp::R_X86_64_SIZE32
:
1941 case elfcpp::R_X86_64_SIZE64
:
1943 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1944 _("unsupported reloc %u"),
1952 // Perform a TLS relocation.
1955 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1956 Target_x86_64
* target
,
1958 const elfcpp::Rela
<64, false>& rela
,
1959 unsigned int r_type
,
1960 const Sized_symbol
<64>* gsym
,
1961 const Symbol_value
<64>* psymval
,
1962 unsigned char* view
,
1963 elfcpp::Elf_types
<64>::Elf_Addr address
,
1964 section_size_type view_size
)
1966 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1968 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1969 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1971 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1973 const bool is_final
= (gsym
== NULL
1974 ? !parameters
->options().output_is_position_independent()
1975 : gsym
->final_value_is_known());
1976 const tls::Tls_optimization optimized_type
1977 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1980 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1981 this->saw_tls_block_reloc_
= true;
1982 if (optimized_type
== tls::TLSOPT_TO_LE
)
1984 gold_assert(tls_segment
!= NULL
);
1985 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1986 rela
, r_type
, value
, view
,
1992 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1993 ? GOT_TYPE_TLS_OFFSET
1994 : GOT_TYPE_TLS_PAIR
);
1995 unsigned int got_offset
;
1998 gold_assert(gsym
->has_got_offset(got_type
));
1999 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2003 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2004 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2005 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2006 - target
->got_size());
2008 if (optimized_type
== tls::TLSOPT_TO_IE
)
2010 gold_assert(tls_segment
!= NULL
);
2011 value
= target
->got_plt_section()->address() + got_offset
;
2012 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2013 value
, view
, address
, view_size
);
2016 else if (optimized_type
== tls::TLSOPT_NONE
)
2018 // Relocate the field with the offset of the pair of GOT
2020 value
= target
->got_plt_section()->address() + got_offset
;
2021 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2026 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2027 _("unsupported reloc %u"), r_type
);
2030 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2031 case elfcpp::R_X86_64_TLSDESC_CALL
:
2032 this->saw_tls_block_reloc_
= true;
2033 if (optimized_type
== tls::TLSOPT_TO_LE
)
2035 gold_assert(tls_segment
!= NULL
);
2036 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2037 rela
, r_type
, value
, view
,
2043 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2044 ? GOT_TYPE_TLS_OFFSET
2045 : GOT_TYPE_TLS_DESC
);
2046 unsigned int got_offset
;
2049 gold_assert(gsym
->has_got_offset(got_type
));
2050 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2054 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2055 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2056 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2057 - target
->got_size());
2059 if (optimized_type
== tls::TLSOPT_TO_IE
)
2061 gold_assert(tls_segment
!= NULL
);
2062 value
= target
->got_plt_section()->address() + got_offset
;
2063 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2064 rela
, r_type
, value
, view
, address
,
2068 else if (optimized_type
== tls::TLSOPT_NONE
)
2070 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2072 // Relocate the field with the offset of the pair of GOT
2074 value
= target
->got_plt_section()->address() + got_offset
;
2075 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2081 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2082 _("unsupported reloc %u"), r_type
);
2085 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2086 this->saw_tls_block_reloc_
= true;
2087 if (optimized_type
== tls::TLSOPT_TO_LE
)
2089 gold_assert(tls_segment
!= NULL
);
2090 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2091 value
, view
, view_size
);
2094 else if (optimized_type
== tls::TLSOPT_NONE
)
2096 // Relocate the field with the offset of the GOT entry for
2097 // the module index.
2098 unsigned int got_offset
;
2099 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2100 - target
->got_size());
2101 value
= target
->got_plt_section()->address() + got_offset
;
2102 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2106 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2107 _("unsupported reloc %u"), r_type
);
2110 case elfcpp::R_X86_64_DTPOFF32
:
2111 if (optimized_type
== tls::TLSOPT_TO_LE
)
2113 // This relocation type is used in debugging information.
2114 // In that case we need to not optimize the value. If we
2115 // haven't seen a TLSLD reloc, then we assume we should not
2116 // optimize this reloc.
2117 if (this->saw_tls_block_reloc_
)
2119 gold_assert(tls_segment
!= NULL
);
2120 value
-= tls_segment
->memsz();
2123 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2126 case elfcpp::R_X86_64_DTPOFF64
:
2127 if (optimized_type
== tls::TLSOPT_TO_LE
)
2129 // See R_X86_64_DTPOFF32, just above, for why we test this.
2130 if (this->saw_tls_block_reloc_
)
2132 gold_assert(tls_segment
!= NULL
);
2133 value
-= tls_segment
->memsz();
2136 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2139 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2140 if (optimized_type
== tls::TLSOPT_TO_LE
)
2142 gold_assert(tls_segment
!= NULL
);
2143 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2144 rela
, r_type
, value
, view
,
2148 else if (optimized_type
== tls::TLSOPT_NONE
)
2150 // Relocate the field with the offset of the GOT entry for
2151 // the tp-relative offset of the symbol.
2152 unsigned int got_offset
;
2155 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2156 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2157 - target
->got_size());
2161 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2162 gold_assert(object
->local_has_got_offset(r_sym
,
2163 GOT_TYPE_TLS_OFFSET
));
2164 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2165 - target
->got_size());
2167 value
= target
->got_plt_section()->address() + got_offset
;
2168 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2171 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2172 _("unsupported reloc type %u"),
2176 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2177 value
-= tls_segment
->memsz();
2178 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2183 // Do a relocation in which we convert a TLS General-Dynamic to an
2187 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2190 const elfcpp::Rela
<64, false>& rela
,
2192 elfcpp::Elf_types
<64>::Elf_Addr value
,
2193 unsigned char* view
,
2194 elfcpp::Elf_types
<64>::Elf_Addr address
,
2195 section_size_type view_size
)
2197 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2198 // .word 0x6666; rex64; call __tls_get_addr
2199 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2201 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2202 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2204 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2205 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2206 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2207 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2209 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2211 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2212 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2214 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2216 this->skip_call_tls_get_addr_
= true;
2219 // Do a relocation in which we convert a TLS General-Dynamic to a
2223 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2225 Output_segment
* tls_segment
,
2226 const elfcpp::Rela
<64, false>& rela
,
2228 elfcpp::Elf_types
<64>::Elf_Addr value
,
2229 unsigned char* view
,
2230 section_size_type view_size
)
2232 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2233 // .word 0x6666; rex64; call __tls_get_addr
2234 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2236 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2237 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2239 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2240 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2241 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2242 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2244 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2246 value
-= tls_segment
->memsz();
2247 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2249 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2251 this->skip_call_tls_get_addr_
= true;
2254 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2257 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2258 const Relocate_info
<64, false>* relinfo
,
2261 const elfcpp::Rela
<64, false>& rela
,
2262 unsigned int r_type
,
2263 elfcpp::Elf_types
<64>::Elf_Addr value
,
2264 unsigned char* view
,
2265 elfcpp::Elf_types
<64>::Elf_Addr address
,
2266 section_size_type view_size
)
2268 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2270 // leaq foo@tlsdesc(%rip), %rax
2271 // ==> movq foo@gottpoff(%rip), %rax
2272 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2273 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2274 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2275 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2277 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2278 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2282 // call *foo@tlscall(%rax)
2284 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2285 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2286 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2287 view
[0] == 0xff && view
[1] == 0x10);
2293 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2296 Target_x86_64::Relocate::tls_desc_gd_to_le(
2297 const Relocate_info
<64, false>* relinfo
,
2299 Output_segment
* tls_segment
,
2300 const elfcpp::Rela
<64, false>& rela
,
2301 unsigned int r_type
,
2302 elfcpp::Elf_types
<64>::Elf_Addr value
,
2303 unsigned char* view
,
2304 section_size_type view_size
)
2306 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2308 // leaq foo@tlsdesc(%rip), %rax
2309 // ==> movq foo@tpoff, %rax
2310 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2311 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2312 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2313 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2316 value
-= tls_segment
->memsz();
2317 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2321 // call *foo@tlscall(%rax)
2323 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2324 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2325 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2326 view
[0] == 0xff && view
[1] == 0x10);
2333 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2336 const elfcpp::Rela
<64, false>& rela
,
2338 elfcpp::Elf_types
<64>::Elf_Addr
,
2339 unsigned char* view
,
2340 section_size_type view_size
)
2342 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2343 // ... leq foo@dtpoff(%rax),%reg
2344 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2346 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2347 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2349 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2350 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2352 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2354 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2356 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2358 this->skip_call_tls_get_addr_
= true;
2361 // Do a relocation in which we convert a TLS Initial-Exec to a
2365 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2367 Output_segment
* tls_segment
,
2368 const elfcpp::Rela
<64, false>& rela
,
2370 elfcpp::Elf_types
<64>::Elf_Addr value
,
2371 unsigned char* view
,
2372 section_size_type view_size
)
2374 // We need to examine the opcodes to figure out which instruction we
2377 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2378 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2380 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2381 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2383 unsigned char op1
= view
[-3];
2384 unsigned char op2
= view
[-2];
2385 unsigned char op3
= view
[-1];
2386 unsigned char reg
= op3
>> 3;
2394 view
[-1] = 0xc0 | reg
;
2398 // Special handling for %rsp.
2402 view
[-1] = 0xc0 | reg
;
2410 view
[-1] = 0x80 | reg
| (reg
<< 3);
2413 value
-= tls_segment
->memsz();
2414 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2417 // Relocate section data.
2420 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2421 unsigned int sh_type
,
2422 const unsigned char* prelocs
,
2424 Output_section
* output_section
,
2425 bool needs_special_offset_handling
,
2426 unsigned char* view
,
2427 elfcpp::Elf_types
<64>::Elf_Addr address
,
2428 section_size_type view_size
)
2430 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2432 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2433 Target_x86_64::Relocate
>(
2439 needs_special_offset_handling
,
2445 // Return the size of a relocation while scanning during a relocatable
2449 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2450 unsigned int r_type
,
2455 case elfcpp::R_X86_64_NONE
:
2456 case elfcpp::R_386_GNU_VTINHERIT
:
2457 case elfcpp::R_386_GNU_VTENTRY
:
2458 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2459 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2460 case elfcpp::R_X86_64_TLSDESC_CALL
:
2461 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2462 case elfcpp::R_X86_64_DTPOFF32
:
2463 case elfcpp::R_X86_64_DTPOFF64
:
2464 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2465 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2468 case elfcpp::R_X86_64_64
:
2469 case elfcpp::R_X86_64_PC64
:
2470 case elfcpp::R_X86_64_GOTOFF64
:
2471 case elfcpp::R_X86_64_GOTPC64
:
2472 case elfcpp::R_X86_64_PLTOFF64
:
2473 case elfcpp::R_X86_64_GOT64
:
2474 case elfcpp::R_X86_64_GOTPCREL64
:
2475 case elfcpp::R_X86_64_GOTPCREL
:
2476 case elfcpp::R_X86_64_GOTPLT64
:
2479 case elfcpp::R_X86_64_32
:
2480 case elfcpp::R_X86_64_32S
:
2481 case elfcpp::R_X86_64_PC32
:
2482 case elfcpp::R_X86_64_PLT32
:
2483 case elfcpp::R_X86_64_GOTPC32
:
2484 case elfcpp::R_X86_64_GOT32
:
2487 case elfcpp::R_X86_64_16
:
2488 case elfcpp::R_X86_64_PC16
:
2491 case elfcpp::R_X86_64_8
:
2492 case elfcpp::R_X86_64_PC8
:
2495 case elfcpp::R_X86_64_COPY
:
2496 case elfcpp::R_X86_64_GLOB_DAT
:
2497 case elfcpp::R_X86_64_JUMP_SLOT
:
2498 case elfcpp::R_X86_64_RELATIVE
:
2499 // These are outstanding tls relocs, which are unexpected when linking
2500 case elfcpp::R_X86_64_TPOFF64
:
2501 case elfcpp::R_X86_64_DTPMOD64
:
2502 case elfcpp::R_X86_64_TLSDESC
:
2503 object
->error(_("unexpected reloc %u in object file"), r_type
);
2506 case elfcpp::R_X86_64_SIZE32
:
2507 case elfcpp::R_X86_64_SIZE64
:
2509 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2514 // Scan the relocs during a relocatable link.
2517 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2518 Symbol_table
* symtab
,
2520 Sized_relobj
<64, false>* object
,
2521 unsigned int data_shndx
,
2522 unsigned int sh_type
,
2523 const unsigned char* prelocs
,
2525 Output_section
* output_section
,
2526 bool needs_special_offset_handling
,
2527 size_t local_symbol_count
,
2528 const unsigned char* plocal_symbols
,
2529 Relocatable_relocs
* rr
)
2531 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2533 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2534 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2536 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2537 Scan_relocatable_relocs
>(
2546 needs_special_offset_handling
,
2552 // Relocate a section during a relocatable link.
2555 Target_x86_64::relocate_for_relocatable(
2556 const Relocate_info
<64, false>* relinfo
,
2557 unsigned int sh_type
,
2558 const unsigned char* prelocs
,
2560 Output_section
* output_section
,
2561 off_t offset_in_output_section
,
2562 const Relocatable_relocs
* rr
,
2563 unsigned char* view
,
2564 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2565 section_size_type view_size
,
2566 unsigned char* reloc_view
,
2567 section_size_type reloc_view_size
)
2569 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2571 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2576 offset_in_output_section
,
2585 // Return the value to use for a dynamic which requires special
2586 // treatment. This is how we support equality comparisons of function
2587 // pointers across shared library boundaries, as described in the
2588 // processor specific ABI supplement.
2591 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2593 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2594 return this->plt_section()->address() + gsym
->plt_offset();
2597 // Return a string used to fill a code section with nops to take up
2598 // the specified length.
2601 Target_x86_64::do_code_fill(section_size_type length
) const
2605 // Build a jmpq instruction to skip over the bytes.
2606 unsigned char jmp
[5];
2608 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2609 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2610 + std::string(length
- 5, '\0'));
2613 // Nop sequences of various lengths.
2614 const char nop1
[1] = { 0x90 }; // nop
2615 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2616 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2617 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2618 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2620 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2622 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2624 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2625 0x00, 0x00, 0x00, 0x00 };
2626 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2627 0x00, 0x00, 0x00, 0x00,
2629 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2630 0x84, 0x00, 0x00, 0x00,
2632 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2633 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2635 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2636 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2637 0x00, 0x00, 0x00, 0x00 };
2638 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2639 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2640 0x00, 0x00, 0x00, 0x00,
2642 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2643 0x66, 0x2e, 0x0f, 0x1f, // data16
2644 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2646 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2647 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2648 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2651 const char* nops
[16] = {
2653 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2654 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2657 return std::string(nops
[length
], length
);
2660 // The selector for x86_64 object files.
2662 class Target_selector_x86_64
: public Target_selector_freebsd
2665 Target_selector_x86_64()
2666 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
2667 "elf64-x86-64-freebsd")
2671 do_instantiate_target()
2672 { return new Target_x86_64(); }
2676 Target_selector_x86_64 target_selector_x86_64
;
2678 } // End anonymous namespace.